blob: c935cc4729e0beeadbe8e13aa06e80ca9a45c842 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -070078static void assemble_file(const char *, StrList *);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070085static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin3366e312018-02-07 14:14:36 -080090#ifndef ABORT_ON_PANIC
91# define ABORT_ON_PANIC 0
92#endif
93static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070094static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -080095
H. Peter Anvin6867acc2007-10-10 14:58:45 -070096bool tasm_compatible_mode = false;
H. Peter Anvina3d96d02018-06-15 17:51:39 -070097int pass0;
98int64_t passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080099static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000100int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800101int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000102
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700103struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800104
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800105const char *inname;
106const char *outname;
107static const char *listname;
108static const char *errname;
109
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700110static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700111
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000112/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800113const struct ofmt *ofmt = &OF_DEFAULT;
114const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400115const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000116
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000118
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400119FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300120struct optimization optimizing =
121 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100122static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400123
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800124iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400125static iflag_t cmd_cpu;
126
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800127struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800128bool in_absolute; /* Flag we are in ABSOLUTE seg */
129struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000130
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000132
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700134static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000135
H. Peter Anvine7469712016-02-18 02:20:59 -0800136static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400137
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400138#define OP_NORMAL (1u << 0)
139#define OP_PREPROCESS (1u << 1)
140#define OP_DEPEND (1u << 2)
141
142static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400143
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700144/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700145static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700146static bool depend_missing_ok = false;
147static const char *depend_target = NULL;
148static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700149StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000150
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700151static bool want_usage;
152static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800153bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700155static char *quote_for_pmake(const char *str);
156static char *quote_for_wmake(const char *str);
157static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700158
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700159/*
160 * Execution limits that can be set via a command-line option or %pragma
161 */
162
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700163#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700164
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700165int64_t nasm_limit[LIMIT_MAX+1] =
166{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700167
168struct limit_info {
169 const char *name;
170 const char *help;
171};
172static const struct limit_info limit_info[LIMIT_MAX+1] = {
173 { "passes", "total number of passes" },
174 { "stalled-passes", "number of passes without forward progress" },
175 { "macro-levels", "levels of macro expansion"},
176 { "rep", "%rep count" },
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700177 { "eval", "expression evaluation descent"},
178 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700179};
180
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700181enum directive_result
182nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700183{
184 int i;
185 int64_t val;
186 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700187 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188
189 for (i = 0; i <= LIMIT_MAX; i++) {
190 if (!nasm_stricmp(limit, limit_info[i].name))
191 break;
192 }
193 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700194 if (passn == 0)
195 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
196 else
197 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
198 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700199 return DIRR_ERROR;
200 }
201
202 if (!nasm_stricmp(valstr, "unlimited")) {
203 val = LIMIT_MAX_VAL;
204 } else {
205 val = readnum(valstr, &rn_error);
206 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700207 if (passn == 0)
208 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
209 else
210 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
211 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700212 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700214 if (val > LIMIT_MAX_VAL)
215 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700216 }
217
218 nasm_limit[i] = val;
219 return DIRR_OK;
220}
221
H. Peter Anvin892c4812018-05-30 14:43:46 -0700222int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400223{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700224 location.segment = segment;
225 if (segment == NO_SEG) {
226 location.offset = absolute.offset;
227 in_absolute = true;
228 } else {
229 location.offset = raa_read(offsets, segment);
230 in_absolute = false;
231 }
232 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400233}
234
235static void set_curr_offs(int64_t l_off)
236{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800237 if (in_absolute)
238 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400239 else
240 offsets = raa_write(offsets, location.segment, l_off);
241}
242
H. Peter Anvin892c4812018-05-30 14:43:46 -0700243static void increment_offset(int64_t delta)
244{
245 if (unlikely(delta == 0))
246 return;
247
248 location.offset += delta;
249 set_curr_offs(location.offset);
250}
251
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000252static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000254 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800256 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000257 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000259}
260
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261static void define_macros_early(void)
262{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700263 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700266 if (oct->have_local) {
267 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400268 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700269 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400270 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400272 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700273 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400274 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800275 }
276
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700277 if (oct->have_gm) {
278 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700280 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400281 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400283 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700284 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400285 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800286 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700287
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700288 if (oct->have_posix) {
289 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400290 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291 }
292}
293
294static void define_macros_late(void)
295{
296 char temp[128];
297
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400298 /*
299 * In case if output format is defined by alias
300 * we have to put shortname of the alias itself here
301 * otherwise ABI backward compatibility gets broken.
302 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400303 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400304 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400305 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800306}
307
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700308static void emit_dependencies(StrList *list)
309{
310 FILE *deps;
311 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700312 bool wmake = (quote_for_make == quote_for_wmake);
313 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700314 struct strlist_entry *l;
315
316 if (!list)
317 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700318
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700319 wrapstr = wmake ? " &\n " : " \\\n ";
320 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321
322 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700323 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400324 if (!deps) {
325 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
326 "unable to write dependency file `%s'", depend_file);
327 return;
328 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700329 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400330 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700331 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700332
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700333 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700334 list_for_each(l, list->head) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700335 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400336 len = strlen(file);
337 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700338 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400339 linepos = 1;
340 }
341 fprintf(deps, " %s", file);
342 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700343 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700344 }
345 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700346
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700347 list_for_each(l, list->head) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700348 if (depend_emit_phony) {
349 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700350 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700351 nasm_free(file);
352 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700353 }
354
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700355 strlist_free(list);
356
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700357 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400358 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700359}
360
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700361/* Convert a struct tm to a POSIX-style time constant */
362static int64_t make_posix_time(const struct tm *tm)
363{
364 int64_t t;
365 int64_t y = tm->tm_year;
366
367 /* See IEEE 1003.1:2004, section 4.14 */
368
369 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
370 t += tm->tm_yday;
371 t *= 24;
372 t += tm->tm_hour;
373 t *= 60;
374 t += tm->tm_min;
375 t *= 60;
376 t += tm->tm_sec;
377
378 return t;
379}
380
381static void timestamp(void)
382{
383 struct compile_time * const oct = &official_compile_time;
384 const struct tm *tp, *best_gm;
385
386 time(&oct->t);
387
388 best_gm = NULL;
389
390 tp = localtime(&oct->t);
391 if (tp) {
392 oct->local = *tp;
393 best_gm = &oct->local;
394 oct->have_local = true;
395 }
396
397 tp = gmtime(&oct->t);
398 if (tp) {
399 oct->gm = *tp;
400 best_gm = &oct->gm;
401 oct->have_gm = true;
402 if (!oct->have_local)
403 oct->local = oct->gm;
404 } else {
405 oct->gm = oct->local;
406 }
407
408 if (best_gm) {
409 oct->posix = make_posix_time(best_gm);
410 oct->have_posix = true;
411 }
412}
413
H. Peter Anvin038d8612007-04-12 16:54:50 +0000414int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000415{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700416 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800417
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800418 iflag_set_default_cpu(&cpu);
419 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400420
Charles Crayne2581c862008-09-10 19:21:52 -0700421 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700422 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400423 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000424
H. Peter Anvin97e15752007-09-25 08:47:47 -0700425 error_file = stderr;
426
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700427 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700428 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700429
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700430 /*
431 * We must call init_labels() before the command line parsing,
432 * because we may be setting prefixes/suffixes from the command
433 * line.
434 */
435 init_labels();
436
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000437 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000438 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000439
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000440 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400441 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000442
H. Peter Anvin55568c12016-10-03 19:46:49 -0700443 parse_cmdline(argc, argv, 1);
444 if (terminate_after_phase) {
445 if (want_usage)
446 usage();
447 return 1;
448 }
449
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700450 /*
451 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700452 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700453 */
454 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800455 define_macros_early();
456
H. Peter Anvin55568c12016-10-03 19:46:49 -0700457 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 if (terminate_after_phase) {
459 if (want_usage)
460 usage();
461 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000462 }
463
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800464 /* Save away the default state of warnings */
465 memcpy(warning_state_init, warning_state, sizeof warning_state);
466
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800467 if (!using_debug_info) {
468 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800469 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800470 } else if (!debug_format) {
471 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800472 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800473 } else {
474 dfmt = dfmt_find(ofmt, debug_format);
475 if (!dfmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700476 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800477 "unrecognized debug format `%s' for"
478 " output format `%s'",
479 debug_format, ofmt->shortname);
480 }
481 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000482
H. Peter Anvin76690a12002-04-30 20:52:49 +0000483 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400484 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300486 /*
487 * If no output file name provided and this
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300488 * is a preprocess mode, we're perfectly
489 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300490 */
491 if (!outname) {
492 if (!(operating_mode & OP_PREPROCESS))
493 outname = filename_set_extension(inname, ofmt->extension);
494 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800495
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000496 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800497 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000498
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700499 if (depend_file || (operating_mode & OP_DEPEND))
500 depend_list = strlist_allocate();
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700501
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700502 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400503 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700504
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400505 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000506 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700507
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400508 if (depend_missing_ok)
509 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700510
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700511 preproc->reset(inname, 0, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 while ((line = preproc->getline()))
514 nasm_free(line);
515 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400516 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000517 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700518 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000519 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800522 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700523 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700525 nasm_fatal_fl(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 "unable to open output file `%s'",
527 outname);
528 } else
529 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000530
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700531 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000532
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400533 /* pass = 1; */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700534 preproc->reset(inname, 3, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800535
536 /* Revert all warnings to the default state */
537 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700538
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 while ((line = preproc->getline())) {
540 /*
541 * We generate %line directives if needed for later programs
542 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000543 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 int altline = src_get(&linnum, &file_name);
545 if (altline) {
546 if (altline == 1 && lineinc == 1)
547 nasm_fputs("", ofile);
548 else {
549 lineinc = (altline != -1 || lineinc != 1);
550 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000551 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 file_name);
553 }
554 prior_linnum = linnum;
555 }
556 nasm_fputs(line, ofile);
557 nasm_free(line);
558 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 preproc->cleanup(0);
560 if (ofile)
561 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700562 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400564 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400565 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400567 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700568 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400569 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700570 nasm_fatal_fl(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400571 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400573 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400574 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700576 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200577
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400578 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800579 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400580 cleanup_labels();
581 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700582 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400583 nasm_error(ERR_NONFATAL|ERR_NOFILE,
584 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700585 terminate_after_phase = true;
586 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400587 }
588
589 if (ofile) {
590 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700591 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400592 remove(outname);
593 ofile = NULL;
594 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000596
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700597 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400598 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700599
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000600 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000602
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 raa_free(offsets);
604 saa_free(forwrefs);
605 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000606 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700607 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000608
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700609 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000610}
611
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612/*
613 * Get a parameter for a command line option.
614 * First arg must be in the form of e.g. -f...
615 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800616static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800618 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400619 if (p[2]) /* the parameter's in the option */
620 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800622 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400625 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627 return NULL;
628}
629
H. Peter Anvindc242712007-11-18 11:55:10 -0800630/*
631 * Copy a filename
632 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800633static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800634{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800635 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700636 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800637
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800638 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800639}
640
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700641/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700642 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700643 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700644static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700645{
646 const char *p;
647 char *os, *q;
648
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400649 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700650 size_t nbs = 0;
651
652 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400653 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700654
655 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400656 switch (*p) {
657 case ' ':
658 case '\t':
659 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
660 n += nbs + 2;
661 nbs = 0;
662 break;
663 case '$':
664 case '#':
665 nbs = 0;
666 n += 2;
667 break;
668 case '\\':
669 nbs++;
670 n++;
671 break;
672 default:
673 nbs = 0;
674 n++;
675 break;
676 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700677 }
678
679 /* Convert N backslashes at the end of filename to 2N backslashes */
680 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400681 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700682
683 os = q = nasm_malloc(n);
684
685 nbs = 0;
686 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400687 switch (*p) {
688 case ' ':
689 case '\t':
690 while (nbs--)
691 *q++ = '\\';
692 *q++ = '\\';
693 *q++ = *p;
694 break;
695 case '$':
696 *q++ = *p;
697 *q++ = *p;
698 nbs = 0;
699 break;
700 case '#':
701 *q++ = '\\';
702 *q++ = *p;
703 nbs = 0;
704 break;
705 case '\\':
706 *q++ = *p;
707 nbs++;
708 break;
709 default:
710 *q++ = *p;
711 nbs = 0;
712 break;
713 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700714 }
715 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400716 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700717
718 *q = '\0';
719
720 return os;
721}
722
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700723/*
724 * Convert a string to a Watcom make-safe form
725 */
726static char *quote_for_wmake(const char *str)
727{
728 const char *p;
729 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700730 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700731
732 size_t n = 1; /* Terminating zero */
733
734 if (!str)
735 return NULL;
736
737 for (p = str; *p; p++) {
738 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700739 case ' ':
740 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700741 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700742 quote = true;
743 n++;
744 break;
745 case '\"':
746 quote = true;
747 n += 2;
748 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700749 case '$':
750 case '#':
751 n += 2;
752 break;
753 default:
754 n++;
755 break;
756 }
757 }
758
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700759 if (quote)
760 n += 2;
761
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700762 os = q = nasm_malloc(n);
763
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700764 if (quote)
765 *q++ = '\"';
766
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700767 for (p = str; *p; p++) {
768 switch (*p) {
769 case '$':
770 case '#':
771 *q++ = '$';
772 *q++ = *p;
773 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700774 case '\"':
775 *q++ = *p;
776 *q++ = *p;
777 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700778 default:
779 *q++ = *p;
780 break;
781 }
782 }
783
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700784 if (quote)
785 *q++ = '\"';
786
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700787 *q = '\0';
788
789 return os;
790}
791
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100792enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800793 OPT_BOGUS,
794 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700795 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800796 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700797 OPT_MANGLE,
798 OPT_INCLUDE,
799 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700800 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700801 OPT_LIMIT,
802 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100803};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800804struct textargs {
805 const char *label;
806 enum text_options opt;
807 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700808 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800809};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800810static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700811 {"v", OPT_VERSION, false, 0},
812 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700813 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700814 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
815 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
816 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
817 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
818 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
819 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
820 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700821 {"include", OPT_INCLUDE, true, 0},
822 {"pragma", OPT_PRAGMA, true, 0},
823 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700824 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700825 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700826 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000827};
828
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400829static void show_version(void)
830{
Dale Curtis10760492018-10-31 13:03:37 -0700831 printf("NASM version %s%s\n",
832 nasm_version, nasm_compile_options);
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400833 exit(0);
834}
835
H. Peter Anvin423e3812007-11-15 17:12:29 -0800836static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700837static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000838{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000839 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800840 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000841
842 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800843 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000844
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400846 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
847 /* These parameters take values */
848 if (!(param = get_param(p, q, &advance)))
849 return advance;
850 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 switch (p[1]) {
853 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700854 if (pass == 1)
855 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700857
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400858 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700859 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800860 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400861 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700862
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400863 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700864 if (pass == 1) {
865 ofmt = ofmt_find(param, &ofmt_alias);
866 if (!ofmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700867 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700868 "unrecognised output format `%s' - "
869 "use -hf for a list", param);
870 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400871 }
872 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700873
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400874 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700875 if (pass == 2) {
876 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700877
H. Peter Anvin55568c12016-10-03 19:46:49 -0700878 if (!*param) {
879 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300880 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700881 } else {
882 while (*param) {
883 switch (*param) {
884 case '0': case '1': case '2': case '3': case '4':
885 case '5': case '6': case '7': case '8': case '9':
886 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700887
Chang S. Baea5786342018-08-15 23:22:21 +0300888 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
889 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700890 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300891 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700892 else
Chang S. Baea5786342018-08-15 23:22:21 +0300893 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700894 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700895
H. Peter Anvin55568c12016-10-03 19:46:49 -0700896 case 'v':
897 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400898 param++;
899 opt_verbose_info = true;
900 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700901
H. Peter Anvin55568c12016-10-03 19:46:49 -0700902 case 'x':
903 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300904 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700905 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700906
H. Peter Anvin55568c12016-10-03 19:46:49 -0700907 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700908 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700909 *param);
910 break;
911 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400912 }
Chang S. Baea5786342018-08-15 23:22:21 +0300913 if (optimizing.level > MAX_OPTIMIZE)
914 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400915 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400916 }
917 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800918
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400919 case 'p': /* pre-include */
920 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700921 if (pass == 2)
922 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400923 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800924
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400925 case 'd': /* pre-define */
926 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700927 if (pass == 2)
928 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400929 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800930
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400931 case 'u': /* un-define */
932 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700933 if (pass == 2)
934 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400935 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800936
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400937 case 'i': /* include search path */
938 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700939 if (pass == 2)
940 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400941 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800942
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700944 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800945 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400946 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800947
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400948 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700949 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800950 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800952
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400953 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700954 if (pass == 2) {
955 using_debug_info = true;
956 debug_format = param;
957 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400958 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800959
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400960 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700961 if (pass == 1) {
962 if (nasm_stricmp("vc", param) == 0)
963 nasm_set_verror(nasm_verror_vc);
964 else if (nasm_stricmp("gnu", param) == 0)
965 nasm_set_verror(nasm_verror_gnu);
966 else
H. Peter Anvinc5136902018-06-15 18:20:17 -0700967 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700968 "unrecognized error reporting format `%s'",
969 param);
970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800972
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700974 if (pass == 2) {
975 using_debug_info = true;
976 if (p[2])
977 debug_format = nasm_skip_spaces(p + 2);
978 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800980
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700982 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400983 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800985
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 case 'y':
987 printf("\nvalid debug formats for '%s' output format are"
988 " ('*' denotes default):\n", ofmt->shortname);
989 dfmt_list(ofmt, stdout);
990 exit(0);
991 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700994 if (pass == 2)
995 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800997
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400999 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001001
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001002 case 'e': /* preprocess only */
1003 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001004 if (pass == 1)
1005 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001007
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001008 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001009 if (pass == 1)
1010 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001012
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001013 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001014 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001015 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001016 if (!set_warning_status(param)) {
1017 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1018 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001019 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001020 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001021 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001024 if (pass == 1) {
1025 switch (p[2]) {
1026 case 'W':
1027 quote_for_make = quote_for_wmake;
1028 break;
1029 case 'D':
1030 case 'F':
1031 case 'T':
1032 case 'Q':
1033 advance = true;
1034 break;
1035 default:
1036 break;
1037 }
1038 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001039 switch (p[2]) {
1040 case 0:
1041 operating_mode = OP_DEPEND;
1042 break;
1043 case 'G':
1044 operating_mode = OP_DEPEND;
1045 depend_missing_ok = true;
1046 break;
1047 case 'P':
1048 depend_emit_phony = true;
1049 break;
1050 case 'D':
1051 operating_mode = OP_NORMAL;
1052 depend_file = q;
1053 advance = true;
1054 break;
1055 case 'F':
1056 depend_file = q;
1057 advance = true;
1058 break;
1059 case 'T':
1060 depend_target = q;
1061 advance = true;
1062 break;
1063 case 'Q':
1064 depend_target = quote_for_make(q);
1065 advance = true;
1066 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001067 case 'W':
1068 /* handled in pass 1 */
1069 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001070 default:
1071 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1072 "unknown dependency option `-M%c'", p[2]);
1073 break;
1074 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001075 }
1076 if (advance && (!q || !q[0])) {
1077 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1078 "option `-M%c' requires a parameter", p[2]);
1079 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001080 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001082
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 case '-':
1084 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001085 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001086 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001087 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001088
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001089 p += 2;
1090
1091 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001092 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 break;
1094 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001095
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001096 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001097 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001098 olen = strlen(tx->label);
1099
1100 if (olen > plen)
1101 continue;
1102
1103 if (nasm_memicmp(p, tx->label, olen))
1104 continue;
1105
1106 if (tx->label[olen-1] == '-')
1107 break; /* Incomplete option */
1108
1109 if (!p[olen] || p[olen] == '=')
1110 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001112
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001113 if (!tx->label) {
1114 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1115 "unrecognized option `--%s'", p);
1116 }
1117
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001118 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001119 if (param)
1120 *param++ = '\0';
1121
H. Peter Anvin3366e312018-02-07 14:14:36 -08001122 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001123 if (!param) {
1124 param = q;
1125 advance = true;
1126 }
1127
1128 /* Note: a null string is a valid parameter */
1129 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001130 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001131 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001132 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 break;
1134 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001135 } else {
1136 if (param) {
1137 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1138 "option `--%s' does not take an argument",
1139 p);
1140
1141 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001142 }
1143
1144 switch (tx->opt) {
1145 case OPT_VERSION:
1146 show_version();
1147 break;
1148 case OPT_ABORT_ON_PANIC:
1149 abort_on_panic = true;
1150 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001151 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001152 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001153 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001154 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001155 case OPT_INCLUDE:
1156 if (pass == 2)
1157 preproc->pre_include(q);
1158 break;
1159 case OPT_PRAGMA:
1160 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001161 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001162 break;
1163 case OPT_BEFORE:
1164 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001165 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001166 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001167 case OPT_LIMIT:
1168 if (pass == 2)
1169 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001170 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001171 case OPT_KEEP_ALL:
1172 keep_all = true;
1173 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001174 case OPT_HELP:
1175 help(0);
1176 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001177 default:
1178 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001180
1181 if (eqsave)
1182 *eqsave = '='; /* Restore = argument separator */
1183
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 break;
1185 }
1186
1187 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001188 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1189 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 break;
1191 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001192 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001193 /* In theory we could allow multiple input files... */
1194 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001195 }
1196
1197 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001198}
1199
H. Peter Anvineba20a72002-04-30 20:53:55 +00001200#define ARG_BUF_DELTA 128
1201
H. Peter Anvin55568c12016-10-03 19:46:49 -07001202static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001203{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001204 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001205 int bufsize, prevargsize;
1206
1207 bufsize = prevargsize = ARG_BUF_DELTA;
1208 buffer = nasm_malloc(ARG_BUF_DELTA);
1209 prevarg = nasm_malloc(ARG_BUF_DELTA);
1210 prevarg[0] = '\0';
1211
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 p = buffer;
1214 while (1) { /* Loop to handle long lines */
1215 q = fgets(p, bufsize - (p - buffer), rfile);
1216 if (!q)
1217 break;
1218 p += strlen(p);
1219 if (p > buffer && p[-1] == '\n')
1220 break;
1221 if (p - buffer > bufsize - 10) {
1222 int offset;
1223 offset = p - buffer;
1224 bufsize += ARG_BUF_DELTA;
1225 buffer = nasm_realloc(buffer, bufsize);
1226 p = buffer + offset;
1227 }
1228 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001229
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 if (!q && p == buffer) {
1231 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001232 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 nasm_free(buffer);
1234 nasm_free(prevarg);
1235 return;
1236 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001237
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 /*
1239 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1240 * them are present at the end of the line.
1241 */
1242 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001243
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001244 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001246
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001247 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001248
H. Peter Anvin55568c12016-10-03 19:46:49 -07001249 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001251
Charles Crayne192d5b52007-10-18 19:02:42 -07001252 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 prevargsize += ARG_BUF_DELTA;
1254 prevarg = nasm_realloc(prevarg, prevargsize);
1255 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001256 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001257 }
1258}
1259
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001260/* Function to process args from a string of args, rather than the
1261 * argv array. Used by the environment variable and response file
1262 * processing.
1263 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001264static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001266 char *p, *q, *arg, *prevarg;
1267 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001268
1269 p = args;
1270 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001272 arg = NULL;
1273 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 q = p;
1275 while (*p && *p != separator)
1276 p++;
1277 while (*p == separator)
1278 *p++ = '\0';
1279 prevarg = arg;
1280 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001281 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001283 }
1284 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001285 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001286}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001287
H. Peter Anvin55568c12016-10-03 19:46:49 -07001288static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001289{
1290 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001291 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001292 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001293 perror(file);
1294 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001295 }
1296 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001297 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001298 }
1299 fclose(f);
1300}
1301
H. Peter Anvin55568c12016-10-03 19:46:49 -07001302static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001303{
1304 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001305 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001306 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001307
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001308 /* Initialize all the warnings to their default state */
1309 for (i = 0; i < ERR_WARN_ALL; i++) {
1310 warning_state_init[i] = warning_state[i] =
1311 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1312 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001313
1314 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001315 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001316 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001317 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001318 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001320 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001322 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001323
1324 /*
1325 * Now process the actual command line.
1326 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001328 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 argv++;
1330 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001331 /*
1332 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 * arguments like the environment variable. This allows us
1334 * to have multiple arguments on a single line, which is
1335 * different to the -@resp file processing below for regular
1336 * NASM.
1337 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001338 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 argc--;
1340 argv++;
1341 }
1342 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001343 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001344 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001345 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001346 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001347 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 fclose(rfile);
1349 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001350 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 "unable to open response file `%s'", p);
1352 }
1353 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001354 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001355 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001356 }
1357
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001358 /*
1359 * Look for basic command line typos. This definitely doesn't
1360 * catch all errors, but it might help cases of fumbled fingers.
1361 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001362 if (pass != 2)
1363 return;
1364
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001365 if (!inname)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001366 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001367
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001368 else if ((errname && !strcmp(inname, errname)) ||
1369 (outname && !strcmp(inname, outname)) ||
1370 (listname && !strcmp(inname, listname)) ||
1371 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001372 nasm_fatal_fl(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001373
1374 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001375 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001376 if (!error_file) {
1377 error_file = stderr; /* Revert to default! */
H. Peter Anvinc5136902018-06-15 18:20:17 -07001378 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001379 "cannot open file `%s' for error messages",
1380 errname);
1381 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001382 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001383}
1384
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001385static void assemble_file(const char *fname, StrList *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001387 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001389 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001390 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001391 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001392
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001393 switch (cmd_sb) {
1394 case 16:
1395 break;
1396 case 32:
1397 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001398 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001399 break;
1400 case 64:
1401 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001402 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001403 break;
1404 default:
1405 panic();
1406 break;
1407 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001408
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001409 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001410 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001411 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001412 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1413 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001414
H. Peter Anvincac0b192017-03-28 16:12:30 -07001415 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 cpu = cmd_cpu;
1417 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001418 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001419 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001420 /* Remove the list file in case we die before the output pass */
1421 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001423 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001424 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001425 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 saa_rewind(forwrefs);
1427 forwref = saa_rstruct(forwrefs);
1428 raa_free(offsets);
1429 offsets = raa_init();
1430 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001431 location.segment = NO_SEG;
1432 location.offset = 0;
1433 if (passn == 1)
1434 location.known = true;
1435 ofmt->reset();
1436 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001437 preproc->reset(fname, pass1, pass1 == 2 ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001438
1439 /* Revert all warnings to the default state */
1440 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001441
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001443
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001445 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001446 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunovf7b44f62018-10-15 22:58:13 +03001447 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001448
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001449 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001450 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001451 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001452 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001453 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001454 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001455
H. Peter Anvinc7131682017-03-07 17:45:01 -08001456 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001457 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001458
Chang S. Baea5786342018-08-15 23:22:21 +03001459 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001460 if (forwref != NULL && globallineno == forwref->lineno) {
1461 output_ins.forw_ref = true;
1462 do {
1463 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1464 forwref = saa_rstruct(forwrefs);
1465 } while (forwref != NULL
1466 && forwref->lineno == globallineno);
1467 } else
1468 output_ins.forw_ref = false;
1469
1470 if (output_ins.forw_ref) {
1471 if (passn == 1) {
1472 for (i = 0; i < output_ins.operands; i++) {
1473 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1474 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1475 fwinf->lineno = globallineno;
1476 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001477 }
1478 }
1479 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001480 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001481 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001482
H. Peter Anvinc7131682017-03-07 17:45:01 -08001483 /* forw_ref */
1484 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001485 if (!output_ins.label) {
1486 nasm_error(ERR_NONFATAL, "EQU not preceded by label");
1487 } else if (output_ins.operands == 1 &&
1488 (output_ins.oprs[0].type & IMMEDIATE) &&
1489 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001490 define_label(output_ins.label,
1491 output_ins.oprs[0].segment,
1492 output_ins.oprs[0].offset, false);
1493 } else if (output_ins.operands == 2
1494 && (output_ins.oprs[0].type & IMMEDIATE)
1495 && (output_ins.oprs[0].type & COLON)
1496 && output_ins.oprs[0].segment == NO_SEG
1497 && output_ins.oprs[0].wrt == NO_SEG
1498 && (output_ins.oprs[1].type & IMMEDIATE)
1499 && output_ins.oprs[1].segment == NO_SEG
1500 && output_ins.oprs[1].wrt == NO_SEG) {
1501 define_label(output_ins.label,
1502 output_ins.oprs[0].offset | SEG_ABS,
1503 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001504 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001505 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001506 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001507 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001508 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001510 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001511
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001512 for (n = 1; n <= output_ins.times; n++) {
1513 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001514 int64_t l = insn_size(location.segment,
1515 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001516 globalbits, &output_ins);
1517
1518 /* if (using_debug_info) && output_ins.opcode != -1) */
1519 if (using_debug_info)
1520 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001521 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001522 int32_t typeinfo =
1523 TYS_ELEMENTS(output_ins.operands);
1524 switch (output_ins.opcode) {
1525 case I_RESB:
1526 typeinfo =
1527 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1528 break;
1529 case I_RESW:
1530 typeinfo =
1531 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1532 break;
1533 case I_RESD:
1534 typeinfo =
1535 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1536 break;
1537 case I_RESQ:
1538 typeinfo =
1539 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1540 break;
1541 case I_REST:
1542 typeinfo =
1543 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1544 break;
1545 case I_RESO:
1546 typeinfo =
1547 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1548 break;
1549 case I_RESY:
1550 typeinfo =
1551 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1552 break;
1553 case I_RESZ:
1554 typeinfo =
1555 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1556 break;
1557 case I_DB:
1558 typeinfo |= TY_BYTE;
1559 break;
1560 case I_DW:
1561 typeinfo |= TY_WORD;
1562 break;
1563 case I_DD:
1564 if (output_ins.eops_float)
1565 typeinfo |= TY_FLOAT;
1566 else
1567 typeinfo |= TY_DWORD;
1568 break;
1569 case I_DQ:
1570 typeinfo |= TY_QWORD;
1571 break;
1572 case I_DT:
1573 typeinfo |= TY_TBYTE;
1574 break;
1575 case I_DO:
1576 typeinfo |= TY_OWORD;
1577 break;
1578 case I_DY:
1579 typeinfo |= TY_YWORD;
1580 break;
1581 case I_DZ:
1582 typeinfo |= TY_ZWORD;
1583 break;
1584 default:
1585 typeinfo = TY_LABEL;
1586 break;
1587 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001588
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001589 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001590 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001591
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001592 /*
1593 * For INCBIN, let the code in assemble
1594 * handle TIMES, so we don't have to read the
1595 * input file over and over.
1596 */
1597 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001598 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001599 }
1600 /*
1601 * else l == -1 => invalid instruction, which will be
1602 * flagged as an error on pass 2
1603 */
1604 } else {
1605 if (n == 2)
1606 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001607 increment_offset(assemble(location.segment,
1608 location.offset,
1609 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001611 } /* not an EQU */
1612 }
1613 if (output_ins.times > 1)
1614 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001615
H. Peter Anvinc7131682017-03-07 17:45:01 -08001616 cleanup_insn(&output_ins);
1617
1618 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001621
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001622 if (global_offset_changed && !terminate_after_phase) {
1623 switch (pass0) {
1624 case 1:
1625 nasm_error(ERR_WARNING|ERR_WARN_PHASE,
1626 "phase error during stabilization pass, hoping for the best");
1627 break;
1628
1629 case 2:
1630 nasm_error(ERR_NONFATAL,
1631 "phase error during code generation pass");
1632 break;
1633
1634 default:
1635 /* This is normal, we'll keep going... */
1636 break;
1637 }
1638 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001639
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 if (pass1 == 1)
1641 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001642
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001643 /*
1644 * Always run at least two optimization passes (pass0 == 0);
1645 * things like subsections will fail miserably without that.
1646 * Once we commit to a stabilization pass (pass0 == 1), we can't
1647 * go back, and if something goes bad, we can only hope
1648 * that we don't end up with a phase error at the end.
1649 */
1650 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001652 } else if (global_offset_changed &&
1653 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001654 prev_offset_changed = global_offset_changed;
1655 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001656 } else {
1657 stall_count++;
1658 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001659
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001660 if (terminate_after_phase)
1661 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001662
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001663 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1664 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001665 /* We get here if the labels don't converge
1666 * Example: FOO equ FOO + 1
1667 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001668 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001669 "Can't find valid values for all labels "
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001670 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001671 nasm_error(ERR_NONFATAL,
1672 "Possible causes: recursive EQUs, macro abuse.");
1673 break;
1674 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001675 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001676
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001678 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001679 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001680 /* -On and -Ov switches */
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001681 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1682 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001683 }
1684}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001685
Ed Berosetfa771012002-06-09 20:56:40 +00001686/**
1687 * gnu style error reporting
1688 * This function prints an error message to error_file in the
1689 * style used by GNU. An example would be:
1690 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001691 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001692 * which the error occurs (or is detected) and "error:" is one of
1693 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001694 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001695 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001696 *
1697 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001698 * @param fmt the printf style format string
1699 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001700static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001701{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001702 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001703 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001704
Ed Berosetfa771012002-06-09 20:56:40 +00001705 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001707
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001708 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001709 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001710 if (!currentfile || (severity & ERR_TOPFILE)) {
1711 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1712 lineno = 0;
1713 }
1714 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001715
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001716 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001717 if (!lineno)
1718 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001719 else
1720 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001721 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001722
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001723 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001724}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001725
Ed Berosetfa771012002-06-09 20:56:40 +00001726/**
1727 * MS style error reporting
1728 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001729 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001730 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001731 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001732 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001733 * which the error occurs (or is detected) and "error:" is one of
1734 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001735 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001736 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001737 *
1738 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001739 * @param fmt the printf style format string
1740 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001741static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001742{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001743 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001744 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001745
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 if (is_suppressed_warning(severity))
1747 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001748
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001749 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001751
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001752 if (!skip_this_pass(severity)) {
1753 if (currentfile) {
1754 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001755 } else {
1756 fputs("nasm: ", error_file);
1757 }
Ed Berosetfa771012002-06-09 20:56:40 +00001758 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001759
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001760 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001761}
1762
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001763/*
1764 * check to see if this is a suppressable warning
1765 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001766static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001767{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001768 /* Not a warning at all */
1769 if ((severity & ERR_MASK) != ERR_WARNING)
1770 return false;
1771
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001772 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001773}
1774
Ed Berosetfa771012002-06-09 20:56:40 +00001775/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001776 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001777 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001778 * not in pass 1
1779 *
1780 * @param severity the severity of the warning or error
1781 * @return true if we should abort error/warning printing
1782 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001783static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001784{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001785 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001786 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001787 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1788 else
1789 return false;
1790}
1791
1792static bool warning_is_error(int severity)
1793{
1794 if (is_valid_warning(severity))
1795 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001796 else
1797 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001798}
1799
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001800static bool skip_this_pass(int severity)
1801{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001802 /*
1803 * See if it's a pass-specific error or warning which should be skipped.
1804 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1805 * they cannot be resumed from.
1806 */
1807 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001808 return false;
1809
H. Peter Anvin934f0472016-05-09 12:00:19 -07001810 /*
1811 * passn is 1 on the very first pass only.
1812 * pass0 is 2 on the code-generation (final) pass only.
1813 * These are the passes we care about in this case.
1814 */
1815 return (((severity & ERR_PASS1) && passn != 1) ||
1816 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001817}
1818
Ed Berosetfa771012002-06-09 20:56:40 +00001819/**
1820 * common error reporting
1821 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001822 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001823 * specific error message to error_file and may or may not return. It
1824 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001825 *
1826 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001827 * @param fmt the printf style format string
1828 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001829static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001830{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001831 char msg[1024];
1832 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001833
H. Peter Anvin7df04172008-06-10 18:27:38 -07001834 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001836 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 break;
1838 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001839 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 break;
1841 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001842 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 break;
1844 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001845 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 break;
1847 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001848 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001850 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001851 pfx = "";
1852 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001853 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001854
H. Peter Anvin934f0472016-05-09 12:00:19 -07001855 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001856 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001857 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001858 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1859 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001860
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001861 if (!skip_this_pass(severity))
1862 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001863
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001864 /* Are we recursing from error_list_macros? */
1865 if (severity & ERR_PP_LISTMACRO)
1866 return;
1867
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001868 /*
1869 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001870 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001871 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001872 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001873
H. Peter Anvin8f622462017-04-02 19:02:29 -07001874 if (skip_this_pass(severity))
1875 return;
1876
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001877 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001878 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001879
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001880 preproc->error_list_macros(severity);
1881
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001882 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 case ERR_DEBUG:
1884 /* no further action, by definition */
1885 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001886 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001887 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001888 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001889 terminate_after_phase = true;
1890 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001892 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 break;
1894 case ERR_FATAL:
1895 if (ofile) {
1896 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001897 if (!keep_all)
1898 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001899 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 }
1901 if (want_usage)
1902 usage();
1903 exit(1); /* instantly die */
1904 break; /* placate silly compilers */
1905 case ERR_PANIC:
1906 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001907
1908 if (abort_on_panic)
1909 abort(); /* halt, catch fire, dump core/stop debugger */
1910
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001911 if (ofile) {
1912 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001913 if (!keep_all)
1914 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001915 ofile = NULL;
1916 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 exit(3);
1918 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001919 }
1920}
1921
H. Peter Anvin734b1882002-04-30 21:01:08 +00001922static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001923{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001924 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001925}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001926
1927static void help(const char xopt)
1928{
1929 int i;
1930
1931 printf
1932 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1933 "[-l listfile]\n"
1934 " [options...] [--] filename\n"
1935 " or nasm -v (or --v) for version info\n\n"
1936 "\n"
1937 "Response files should contain command line parameters,\n"
1938 "one per line.\n"
1939 "\n"
1940 " -t assemble in SciTech TASM compatible mode\n");
1941 printf
1942 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1943 " -a don't preprocess (assemble only)\n"
1944 " -M generate Makefile dependencies on stdout\n"
1945 " -MG d:o, missing files assumed generated\n"
1946 " -MF file set Makefile dependency file\n"
1947 " -MD file assemble and generate dependencies\n"
1948 " -MT file dependency target name\n"
1949 " -MQ file dependency target name (quoted)\n"
1950 " -MP emit phony target\n\n"
1951 " -Zfile redirect error messages to file\n"
1952 " -s redirect error messages to stdout\n\n"
1953 " -g generate debugging information\n\n"
1954 " -F format select a debugging format\n\n"
1955 " -gformat same as -g -F format\n\n"
1956 " -o outfile write output to an outfile\n\n"
1957 " -f format select an output format\n\n"
1958 " -l listfile write listing to a listfile\n\n"
1959 " -Ipath add a pathname to the include file path\n");
1960 printf
1961 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1962 " -O0 no optimization\n"
1963 " -O1 minimal optimization\n"
1964 " -Ox multipass optimization (default)\n"
1965 " -Pfile pre-include a file (also --include)\n"
1966 " -Dmacro[=str] pre-define a macro\n"
1967 " -Umacro undefine a macro\n"
1968 " -Xformat specifiy error reporting format (gnu or vc)\n"
1969 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1970 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1971 " -w[+-]error[=foo]\n"
1972 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001973 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001974 " --pragma str pre-executes a specific %%pragma\n"
1975 " --before str add line (usually a preprocessor statement) before the input\n"
1976 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001977 " to all extern, common and global symbols (also --gprefix)\n"
1978 " --postfix str append the given string to all the given string\n"
1979 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001980 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001981 " --lpostfix str append the given string to all other symbols\n"
1982 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001983 " --limit-X val set execution limit X\n");
1984
1985 for (i = 0; i <= LIMIT_MAX; i++) {
1986 printf(" %-15s %s (default ",
1987 limit_info[i].name, limit_info[i].help);
1988 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001989 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001990 } else {
1991 printf("unlimited)\n");
1992 }
1993 }
1994
1995 printf("\nWarnings for the -W/-w options:\n");
1996
1997 for (i = 0; i <= ERR_WARN_ALL; i++)
1998 printf(" %-23s %s%s\n",
1999 warnings[i].name, warnings[i].help,
2000 i == ERR_WARN_ALL ? "\n" :
2001 warnings[i].enabled ? " (default on)" :
2002 " (default off)");
2003
2004 if (xopt == 'f') {
2005 printf("valid output formats for -f are"
2006 " (`*' denotes default):\n");
2007 ofmt_list(ofmt, stdout);
2008 } else {
2009 printf("For a list of valid output formats, use -hf.\n");
2010 printf("For a list of debug formats, use -f <format> -y.\n");
2011 }
2012}