blob: 676ff872de0b9aa9fcd3cfc1fd401203aea4b2d6 [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
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070047#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080058#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvin31387b22010-07-15 18:28:52 -070060/*
61 * This is the maximum number of optimization passes to do. If we ever
62 * find a case where the optimizer doesn't naturally converge, we might
63 * have to drop this value so the assembler doesn't appear to just hang.
64 */
65#define MAX_OPTIMIZE (INT_MAX >> 1)
66
H. Peter Anvine2c80182005-01-15 22:15:51 +000067struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 int lineno;
69 int operand;
70};
71
H. Peter Anvin55568c12016-10-03 19:46:49 -070072static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030073static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080074static bool skip_this_pass(errflags severity);
75static void nasm_verror_asm(errflags severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000076static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070077static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078
H. Peter Anvin77016c82018-12-10 22:29:49 -080079struct error_format {
80 const char *beforeline; /* Before line number, if present */
81 const char *afterline; /* After line number, if present */
82 const char *beforemsg; /* Before actual message */
83};
84
85static const struct error_format errfmt_gnu = { ":", "", ": " };
86static const struct error_format errfmt_msvc = { "(", ")", " : " };
87static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080088static struct strlist *warn_list;
H. Peter Anvin77016c82018-12-10 22:29:49 -080089
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070090unsigned int debug_nasm; /* Debugging messages? */
91
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080092static bool using_debug_info, opt_verbose_info;
93static const char *debug_format;
94
H. Peter Anvin3366e312018-02-07 14:14:36 -080095#ifndef ABORT_ON_PANIC
96# define ABORT_ON_PANIC 0
97#endif
98static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070099static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800100
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700101bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800102enum pass_type _pass_type;
103const char * const _pass_types[] =
104{
105 "init", "first", "optimize", "stabilize", "final"
106};
107int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000108int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800109int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000110
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700111struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800112
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800113const char *inname;
114const char *outname;
115static const char *listname;
116static const char *errname;
117
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700118static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700119
H. Peter Anvin338656c2016-02-17 20:59:22 -0800120const struct ofmt *ofmt = &OF_DEFAULT;
121const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400122const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000123
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800124FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000125
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400126FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300127struct optimization optimizing =
128 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100129static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400130
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800131iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400132static iflag_t cmd_cpu;
133
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800134struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800135bool in_absolute; /* Flag we are in ABSOLUTE seg */
136struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000137
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000138static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000139
H. Peter Anvine2c80182005-01-15 22:15:51 +0000140static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700141static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142
H. Peter Anvine7469712016-02-18 02:20:59 -0800143static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300144static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800145bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400146
H. Peter Anvin34754622018-11-28 12:36:53 -0800147#define OP_NORMAL (1U << 0)
148#define OP_PREPROCESS (1U << 1)
149#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400150
151static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400152
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700153/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700154static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700155static bool depend_missing_ok = false;
156static const char *depend_target = NULL;
157static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300158struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000159
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700160static bool want_usage;
161static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800162bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000163
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700164static char *quote_for_pmake(const char *str);
165static char *quote_for_wmake(const char *str);
166static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700167
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700168/*
169 * Execution limits that can be set via a command-line option or %pragma
170 */
171
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700172#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700173
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700174int64_t nasm_limit[LIMIT_MAX+1] =
175{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700176
177struct limit_info {
178 const char *name;
179 const char *help;
180};
181static const struct limit_info limit_info[LIMIT_MAX+1] = {
182 { "passes", "total number of passes" },
183 { "stalled-passes", "number of passes without forward progress" },
184 { "macro-levels", "levels of macro expansion"},
185 { "rep", "%rep count" },
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700186 { "eval", "expression evaluation descent"},
187 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188};
189
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700190enum directive_result
191nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700192{
193 int i;
194 int64_t val;
195 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700196 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700197
198 for (i = 0; i <= LIMIT_MAX; i++) {
199 if (!nasm_stricmp(limit, limit_info[i].name))
200 break;
201 }
202 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800203 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800204 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700205 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700206 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700207 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700208 return DIRR_ERROR;
209 }
210
211 if (!nasm_stricmp(valstr, "unlimited")) {
212 val = LIMIT_MAX_VAL;
213 } else {
214 val = readnum(valstr, &rn_error);
215 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800216 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800217 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700218 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700219 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700220 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700221 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700222 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700223 if (val > LIMIT_MAX_VAL)
224 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700225 }
226
227 nasm_limit[i] = val;
228 return DIRR_OK;
229}
230
H. Peter Anvin892c4812018-05-30 14:43:46 -0700231int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400232{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700233 location.segment = segment;
234 if (segment == NO_SEG) {
235 location.offset = absolute.offset;
236 in_absolute = true;
237 } else {
238 location.offset = raa_read(offsets, segment);
239 in_absolute = false;
240 }
241 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400242}
243
244static void set_curr_offs(int64_t l_off)
245{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800246 if (in_absolute)
247 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400248 else
249 offsets = raa_write(offsets, location.segment, l_off);
250}
251
H. Peter Anvin892c4812018-05-30 14:43:46 -0700252static void increment_offset(int64_t delta)
253{
254 if (unlikely(delta == 0))
255 return;
256
257 location.offset += delta;
258 set_curr_offs(location.offset);
259}
260
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000261static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000262{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000263 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000264 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800265 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000266 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000267 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000268}
269
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800270/*
271 * Define system-defined macros that are not part of
272 * macros/standard.mac.
273 */
274static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800275{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700276 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800277 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800278
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700279 if (oct->have_local) {
280 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
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, "__DATE_NUM__=%Y%m%d", &oct->local);
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, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400285 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700286 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400287 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800288 }
289
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700290 if (oct->have_gm) {
291 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400292 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700293 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400294 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700295 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400296 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700297 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400298 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800299 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700300
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700301 if (oct->have_posix) {
302 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400303 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800304 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800305
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400306 /*
307 * In case if output format is defined by alias
308 * we have to put shortname of the alias itself here
309 * otherwise ABI backward compatibility gets broken.
310 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400311 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400312 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400313 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800314
315 /*
316 * Output-format specific macros.
317 */
318 if (ofmt->stdmac)
319 preproc->extra_stdmac(ofmt->stdmac);
320
321 /*
322 * Debug format, if any
323 */
324 if (dfmt != &null_debug_form) {
325 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
326 preproc->pre_define(temp);
327 }
328}
329
330/*
331 * Initialize the preprocessor, set up the include path, and define
332 * the system-included macros. This is called between passes 1 and 2
333 * of parsing the command options; ofmt and dfmt are defined at this
334 * point.
335 *
336 * Command-line specified preprocessor directives (-p, -d, -u,
337 * --pragma, --before) are processed after this function.
338 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300339static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800340{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800341 preproc->init();
342 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300343 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800344}
345
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300346static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700347{
348 FILE *deps;
349 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700350 bool wmake = (quote_for_make == quote_for_wmake);
351 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800352 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700353
354 if (!list)
355 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700356
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700357 wrapstr = wmake ? " &\n " : " \\\n ";
358 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700359
360 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700361 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400362 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800363 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400364 return;
365 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700366 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400367 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700368 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700369
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700370 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800371 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700372 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400373 len = strlen(file);
374 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700375 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400376 linepos = 1;
377 }
378 fprintf(deps, " %s", file);
379 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700380 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700381 }
382 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700383
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800384 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700385 if (depend_emit_phony) {
386 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700387 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700388 nasm_free(file);
389 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390 }
391
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800392 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700393
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700394 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400395 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700396}
397
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700398/* Convert a struct tm to a POSIX-style time constant */
399static int64_t make_posix_time(const struct tm *tm)
400{
401 int64_t t;
402 int64_t y = tm->tm_year;
403
404 /* See IEEE 1003.1:2004, section 4.14 */
405
406 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
407 t += tm->tm_yday;
408 t *= 24;
409 t += tm->tm_hour;
410 t *= 60;
411 t += tm->tm_min;
412 t *= 60;
413 t += tm->tm_sec;
414
415 return t;
416}
417
418static void timestamp(void)
419{
420 struct compile_time * const oct = &official_compile_time;
421 const struct tm *tp, *best_gm;
422
423 time(&oct->t);
424
425 best_gm = NULL;
426
427 tp = localtime(&oct->t);
428 if (tp) {
429 oct->local = *tp;
430 best_gm = &oct->local;
431 oct->have_local = true;
432 }
433
434 tp = gmtime(&oct->t);
435 if (tp) {
436 oct->gm = *tp;
437 best_gm = &oct->gm;
438 oct->have_gm = true;
439 if (!oct->have_local)
440 oct->local = oct->gm;
441 } else {
442 oct->gm = oct->local;
443 }
444
445 if (best_gm) {
446 oct->posix = make_posix_time(best_gm);
447 oct->have_posix = true;
448 }
449}
450
H. Peter Anvin038d8612007-04-12 16:54:50 +0000451int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000452{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700453 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800454
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800455 error_file = stderr;
456
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800457 iflag_set_default_cpu(&cpu);
458 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400459
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800460 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300461
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800462 _pass_type = PASS_INIT;
463 _passn = 0;
464
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700465 want_usage = terminate_after_phase = false;
H. Peter Anvin77016c82018-12-10 22:29:49 -0800466 nasm_set_verror(nasm_verror_asm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467
H. Peter Anvin13506202018-11-28 14:55:58 -0800468 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700469 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700470
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700471 /*
472 * We must call init_labels() before the command line parsing,
473 * because we may be setting prefixes/suffixes from the command
474 * line.
475 */
476 init_labels();
477
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000478 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000479 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000480
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000481 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400482 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000483
H. Peter Anvin55568c12016-10-03 19:46:49 -0700484 parse_cmdline(argc, argv, 1);
485 if (terminate_after_phase) {
486 if (want_usage)
487 usage();
488 return 1;
489 }
490
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800491 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800492 if (!using_debug_info) {
493 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800494 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800495 } else if (!debug_format) {
496 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300497 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800498 } else {
499 dfmt = dfmt_find(ofmt, debug_format);
500 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800501 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800502 debug_format, ofmt->shortname);
503 }
504 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000505
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300506 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800507
508 parse_cmdline(argc, argv, 2);
509 if (terminate_after_phase) {
510 if (want_usage)
511 usage();
512 return 1;
513 }
514
515 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800516 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000517
H. Peter Anvin34754622018-11-28 12:36:53 -0800518 /* Dependency filename if we are also doing other things */
519 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
520 if (outname)
521 depend_file = nasm_strcat(outname, ".d");
522 else
523 depend_file = filename_set_extension(inname, ".d");
524 }
525
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300526 /*
527 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800528 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300529 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300530 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800531 if (!outname && !(operating_mode & OP_PREPROCESS)) {
532 outname = filename_set_extension(inname, ofmt->extension);
533 if (!strcmp(outname, inname)) {
534 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800535 nasm_warn(WARN_OTHER, "default output file same as input, using `%s' for output\n", outname);
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800536 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300537 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800538
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800539 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700540
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700541 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400542 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700543
H. Peter Anvin34754622018-11-28 12:36:53 -0800544 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000545 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700546
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400547 if (depend_missing_ok)
548 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700549
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800550 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 while ((line = preproc->getline()))
553 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800554 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800555 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400556 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000557 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700558 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000559 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000561
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800562 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700563 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800565 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 } else
567 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000568
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700569 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000570
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800571 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
572 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800573
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 while ((line = preproc->getline())) {
575 /*
576 * We generate %line directives if needed for later programs
577 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000578 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 int altline = src_get(&linnum, &file_name);
580 if (altline) {
581 if (altline == 1 && lineinc == 1)
582 nasm_fputs("", ofile);
583 else {
584 lineinc = (altline != -1 || lineinc != 1);
585 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000586 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 file_name);
588 }
589 prior_linnum = linnum;
590 }
591 nasm_fputs(line, ofile);
592 nasm_free(line);
593 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800594 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800595 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 if (ofile)
597 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700598 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400600 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400601 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400603 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700604 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400605 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800606 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400608 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400609 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700611 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200612
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400613 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800614 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400615 cleanup_labels();
616 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800617 if (ferror(ofile))
618 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400619 }
620
621 if (ofile) {
622 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700623 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400624 remove(outname);
625 ofile = NULL;
626 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000627 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000628
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800629 preproc->cleanup_session();
630
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700631 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400632 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700633
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000634 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000636
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 raa_free(offsets);
638 saa_free(forwrefs);
639 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000640 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700641 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800642 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000643
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700644 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645}
646
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647/*
648 * Get a parameter for a command line option.
649 * First arg must be in the form of e.g. -f...
650 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800651static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000652{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800653 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400654 if (p[2]) /* the parameter's in the option */
655 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800657 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800660 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661 return NULL;
662}
663
H. Peter Anvindc242712007-11-18 11:55:10 -0800664/*
665 * Copy a filename
666 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800667static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800668{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800669 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700670 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800671
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800672 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800673}
674
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700675/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700676 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700677 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700678static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700679{
680 const char *p;
681 char *os, *q;
682
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400683 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700684 size_t nbs = 0;
685
686 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400687 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700688
689 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400690 switch (*p) {
691 case ' ':
692 case '\t':
693 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
694 n += nbs + 2;
695 nbs = 0;
696 break;
697 case '$':
698 case '#':
699 nbs = 0;
700 n += 2;
701 break;
702 case '\\':
703 nbs++;
704 n++;
705 break;
706 default:
707 nbs = 0;
708 n++;
709 break;
710 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700711 }
712
713 /* Convert N backslashes at the end of filename to 2N backslashes */
714 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400715 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700716
717 os = q = nasm_malloc(n);
718
719 nbs = 0;
720 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400721 switch (*p) {
722 case ' ':
723 case '\t':
724 while (nbs--)
725 *q++ = '\\';
726 *q++ = '\\';
727 *q++ = *p;
728 break;
729 case '$':
730 *q++ = *p;
731 *q++ = *p;
732 nbs = 0;
733 break;
734 case '#':
735 *q++ = '\\';
736 *q++ = *p;
737 nbs = 0;
738 break;
739 case '\\':
740 *q++ = *p;
741 nbs++;
742 break;
743 default:
744 *q++ = *p;
745 nbs = 0;
746 break;
747 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700748 }
749 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400750 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700751
752 *q = '\0';
753
754 return os;
755}
756
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700757/*
758 * Convert a string to a Watcom make-safe form
759 */
760static char *quote_for_wmake(const char *str)
761{
762 const char *p;
763 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700764 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700765
766 size_t n = 1; /* Terminating zero */
767
768 if (!str)
769 return NULL;
770
771 for (p = str; *p; p++) {
772 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700773 case ' ':
774 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700775 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700776 quote = true;
777 n++;
778 break;
779 case '\"':
780 quote = true;
781 n += 2;
782 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700783 case '$':
784 case '#':
785 n += 2;
786 break;
787 default:
788 n++;
789 break;
790 }
791 }
792
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700793 if (quote)
794 n += 2;
795
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700796 os = q = nasm_malloc(n);
797
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700798 if (quote)
799 *q++ = '\"';
800
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700801 for (p = str; *p; p++) {
802 switch (*p) {
803 case '$':
804 case '#':
805 *q++ = '$';
806 *q++ = *p;
807 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700808 case '\"':
809 *q++ = *p;
810 *q++ = *p;
811 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700812 default:
813 *q++ = *p;
814 break;
815 }
816 }
817
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700818 if (quote)
819 *q++ = '\"';
820
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700821 *q = '\0';
822
823 return os;
824}
825
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100826enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800827 OPT_BOGUS,
828 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700829 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800830 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700831 OPT_MANGLE,
832 OPT_INCLUDE,
833 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700834 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700835 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800836 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700837 OPT_NO_LINE,
838 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100839};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800840struct textargs {
841 const char *label;
842 enum text_options opt;
843 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700844 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800845};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800846static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700847 {"v", OPT_VERSION, false, 0},
848 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700849 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700850 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
851 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
852 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
853 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
854 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
855 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
856 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700857 {"include", OPT_INCLUDE, true, 0},
858 {"pragma", OPT_PRAGMA, true, 0},
859 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700860 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700861 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800862 {"no-line", OPT_NO_LINE, false, 0},
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700863 {"debug", OPT_DEBUG, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700864 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000865};
866
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400867static void show_version(void)
868{
869 printf("NASM version %s compiled on %s%s\n",
870 nasm_version, nasm_date, nasm_compile_options);
871 exit(0);
872}
873
H. Peter Anvin423e3812007-11-15 17:12:29 -0800874static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700875static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000876{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000877 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800878 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000879
880 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800881 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000882
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400884 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
885 /* These parameters take values */
886 if (!(param = get_param(p, q, &advance)))
887 return advance;
888 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800889
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 switch (p[1]) {
891 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700892 if (pass == 1)
893 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700895
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400896 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700897 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800898 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400899 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700900
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400901 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700902 if (pass == 1) {
903 ofmt = ofmt_find(param, &ofmt_alias);
904 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800905 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700906 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400907 }
908 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700909
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400910 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800911 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700912 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700913
H. Peter Anvin55568c12016-10-03 19:46:49 -0700914 if (!*param) {
915 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300916 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700917 } else {
918 while (*param) {
919 switch (*param) {
920 case '0': case '1': case '2': case '3': case '4':
921 case '5': case '6': case '7': case '8': case '9':
922 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700923
Chang S. Baea5786342018-08-15 23:22:21 +0300924 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
925 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700926 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300927 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700928 else
Chang S. Baea5786342018-08-15 23:22:21 +0300929 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700930 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700931
H. Peter Anvin55568c12016-10-03 19:46:49 -0700932 case 'v':
933 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400934 param++;
935 opt_verbose_info = true;
936 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700937
H. Peter Anvin55568c12016-10-03 19:46:49 -0700938 case 'x':
939 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300940 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700942
H. Peter Anvin55568c12016-10-03 19:46:49 -0700943 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700944 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700945 *param);
946 break;
947 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400948 }
Chang S. Baea5786342018-08-15 23:22:21 +0300949 if (optimizing.level > MAX_OPTIMIZE)
950 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400952 }
953 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800954
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400955 case 'p': /* pre-include */
956 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 if (pass == 2)
958 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400959 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800960
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400961 case 'd': /* pre-define */
962 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700963 if (pass == 2)
964 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400965 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800966
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400967 case 'u': /* un-define */
968 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700969 if (pass == 2)
970 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400971 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800972
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400973 case 'i': /* include search path */
974 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800975 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300976 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400977 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800978
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400979 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700980 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800981 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400982 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800983
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400984 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700985 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800986 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800988
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400989 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800990 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700991 using_debug_info = true;
992 debug_format = param;
993 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400994 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800995
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400996 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700997 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800998 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
999 errfmt = &errfmt_msvc;
1000 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1001 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001002 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001003 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001004 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001008 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001009 using_debug_info = true;
1010 if (p[2])
1011 debug_format = nasm_skip_spaces(p + 2);
1012 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001014
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001016 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001017 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001019
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 case 'y':
1021 printf("\nvalid debug formats for '%s' output format are"
1022 " ('*' denotes default):\n", ofmt->shortname);
1023 dfmt_list(ofmt, stdout);
1024 exit(0);
1025 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001028 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001029 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001030 nasm_ctype_tasm_mode();
1031 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001033
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001035 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001037
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001038 case 'e': /* preprocess only */
1039 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001040 if (pass == 1)
1041 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001043
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001044 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 if (pass == 1)
1046 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001048
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001049 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001050 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001051 if (pass == 2)
1052 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001053 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001056 if (pass == 1) {
1057 switch (p[2]) {
1058 case 'W':
1059 quote_for_make = quote_for_wmake;
1060 break;
1061 case 'D':
1062 case 'F':
1063 case 'T':
1064 case 'Q':
1065 advance = true;
1066 break;
1067 default:
1068 break;
1069 }
1070 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001071 switch (p[2]) {
1072 case 0:
1073 operating_mode = OP_DEPEND;
1074 break;
1075 case 'G':
1076 operating_mode = OP_DEPEND;
1077 depend_missing_ok = true;
1078 break;
1079 case 'P':
1080 depend_emit_phony = true;
1081 break;
1082 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001083 operating_mode |= OP_DEPEND;
1084 if (q && (q[0] != '-' || q[1] == '\0')) {
1085 depend_file = q;
1086 advance = true;
1087 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001088 break;
1089 case 'F':
1090 depend_file = q;
1091 advance = true;
1092 break;
1093 case 'T':
1094 depend_target = q;
1095 advance = true;
1096 break;
1097 case 'Q':
1098 depend_target = quote_for_make(q);
1099 advance = true;
1100 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001101 case 'W':
1102 /* handled in pass 1 */
1103 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001104 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001105 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001106 break;
1107 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001108 }
1109 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001110 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001111 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001112 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 case '-':
1116 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001117 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001118 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001119 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001120
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001121 p += 2;
1122
1123 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001124 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 break;
1126 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001127
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001128 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001129 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001130 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001131 olen = strlen(tx->label);
1132
1133 if (olen > plen)
1134 continue;
1135
1136 if (nasm_memicmp(p, tx->label, olen))
1137 continue;
1138
1139 if (tx->label[olen-1] == '-')
1140 break; /* Incomplete option */
1141
1142 if (!p[olen] || p[olen] == '=')
1143 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001145
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001146 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001147 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001148 }
1149
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001150 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001151 if (param)
1152 *param++ = '\0';
1153
H. Peter Anvin3366e312018-02-07 14:14:36 -08001154 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001155 if (!param) {
1156 param = q;
1157 advance = true;
1158 }
1159
1160 /* Note: a null string is a valid parameter */
1161 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001162 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 break;
1164 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001165 } else {
1166 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001167 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001168 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001169 }
1170
1171 switch (tx->opt) {
1172 case OPT_VERSION:
1173 show_version();
1174 break;
1175 case OPT_ABORT_ON_PANIC:
1176 abort_on_panic = true;
1177 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001178 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001179 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001180 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001181 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001182 case OPT_INCLUDE:
1183 if (pass == 2)
1184 preproc->pre_include(q);
1185 break;
1186 case OPT_PRAGMA:
1187 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001188 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001189 break;
1190 case OPT_BEFORE:
1191 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001192 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001193 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001194 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001195 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001196 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001197 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001198 case OPT_KEEP_ALL:
1199 keep_all = true;
1200 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001201 case OPT_NO_LINE:
1202 pp_noline = true;
1203 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001204 case OPT_DEBUG:
1205 debug_nasm = param ? strtoul(param, NULL, 10) : 1;
1206 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001207 case OPT_HELP:
1208 help(0);
1209 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001210 default:
1211 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001213
1214 if (eqsave)
1215 *eqsave = '='; /* Restore = argument separator */
1216
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 break;
1218 }
1219
1220 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001221 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 break;
1223 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001224 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001225 /* In theory we could allow multiple input files... */
1226 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001227 }
1228
1229 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001230}
1231
H. Peter Anvineba20a72002-04-30 20:53:55 +00001232#define ARG_BUF_DELTA 128
1233
H. Peter Anvin55568c12016-10-03 19:46:49 -07001234static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001235{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001236 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001237 int bufsize, prevargsize;
1238
1239 bufsize = prevargsize = ARG_BUF_DELTA;
1240 buffer = nasm_malloc(ARG_BUF_DELTA);
1241 prevarg = nasm_malloc(ARG_BUF_DELTA);
1242 prevarg[0] = '\0';
1243
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 p = buffer;
1246 while (1) { /* Loop to handle long lines */
1247 q = fgets(p, bufsize - (p - buffer), rfile);
1248 if (!q)
1249 break;
1250 p += strlen(p);
1251 if (p > buffer && p[-1] == '\n')
1252 break;
1253 if (p - buffer > bufsize - 10) {
1254 int offset;
1255 offset = p - buffer;
1256 bufsize += ARG_BUF_DELTA;
1257 buffer = nasm_realloc(buffer, bufsize);
1258 p = buffer + offset;
1259 }
1260 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001261
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 if (!q && p == buffer) {
1263 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001264 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 nasm_free(buffer);
1266 nasm_free(prevarg);
1267 return;
1268 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001269
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 /*
1271 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1272 * them are present at the end of the line.
1273 */
1274 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001275
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001276 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001278
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001279 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001280
H. Peter Anvin55568c12016-10-03 19:46:49 -07001281 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283
Charles Crayne192d5b52007-10-18 19:02:42 -07001284 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 prevargsize += ARG_BUF_DELTA;
1286 prevarg = nasm_realloc(prevarg, prevargsize);
1287 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001288 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001289 }
1290}
1291
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001292/* Function to process args from a string of args, rather than the
1293 * argv array. Used by the environment variable and response file
1294 * processing.
1295 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001296static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001298 char *p, *q, *arg, *prevarg;
1299 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001300
1301 p = args;
1302 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001304 arg = NULL;
1305 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 q = p;
1307 while (*p && *p != separator)
1308 p++;
1309 while (*p == separator)
1310 *p++ = '\0';
1311 prevarg = arg;
1312 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001313 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001315 }
1316 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001317 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001318}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001319
H. Peter Anvin55568c12016-10-03 19:46:49 -07001320static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001321{
1322 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001323 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001324 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001325 perror(file);
1326 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001327 }
1328 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001329 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001330 }
1331 fclose(f);
1332}
1333
H. Peter Anvin55568c12016-10-03 19:46:49 -07001334static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001335{
1336 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001337 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001338
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001339 /*
1340 * Initialize all the warnings to their default state, including
1341 * warning index 0 used for "always on".
1342 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001343 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001344
1345 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001346 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001347 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001348 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001349 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001351 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001353 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001354
1355 /*
1356 * Now process the actual command line.
1357 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001359 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 argv++;
1361 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001362 /*
1363 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 * arguments like the environment variable. This allows us
1365 * to have multiple arguments on a single line, which is
1366 * different to the -@resp file processing below for regular
1367 * NASM.
1368 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001369 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 argc--;
1371 argv++;
1372 }
1373 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001374 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001375 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001376 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001377 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001378 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001380 } else {
1381 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1382 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 }
1384 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001385 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001386 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001387 }
1388
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001389 /*
1390 * Look for basic command line typos. This definitely doesn't
1391 * catch all errors, but it might help cases of fumbled fingers.
1392 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001393 if (pass != 2)
1394 return;
1395
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001396 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001397 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001398 else if ((errname && !strcmp(inname, errname)) ||
1399 (outname && !strcmp(inname, outname)) ||
1400 (listname && !strcmp(inname, listname)) ||
1401 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001402 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001403
1404 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001405 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001406 if (!error_file) {
1407 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001408 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001409 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001410 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001411}
1412
H. Peter Anvin29651542018-12-18 19:14:40 -08001413static void forward_refs(insn *instruction)
1414{
1415 int i;
1416 struct forwrefinfo *fwinf;
1417
1418 instruction->forw_ref = false;
1419
1420 if (!optimizing.level)
1421 return; /* For -O0 don't bother */
1422
1423 if (!forwref)
1424 return;
1425
1426 if (forwref->lineno != globallineno)
1427 return;
1428
1429 instruction->forw_ref = true;
1430 do {
1431 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1432 forwref = saa_rstruct(forwrefs);
1433 } while (forwref && forwref->lineno == globallineno);
1434
1435 if (!pass_first())
1436 return;
1437
1438 for (i = 0; i < instruction->operands; i++) {
1439 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1440 fwinf = saa_wstruct(forwrefs);
1441 fwinf->lineno = globallineno;
1442 fwinf->operand = i;
1443 }
1444 }
1445}
1446
1447static void process_insn(insn *instruction)
1448{
1449 int32_t n;
1450 int64_t l;
1451
1452 if (!instruction->times)
1453 return; /* Nothing to do... */
1454
1455 nasm_assert(instruction->times > 0);
1456
1457 /*
1458 * NOTE: insn_size() can change instruction->times
1459 * (usually to 1) when called.
1460 */
1461 if (!pass_final()) {
1462 for (n = 1; n <= instruction->times; n++) {
1463 l = insn_size(location.segment, location.offset,
1464 globalbits, instruction);
1465 if (l != -1) /* l == -1 -> invalid instruction */
1466 increment_offset(l);
1467 }
1468 } else {
1469 l = assemble(location.segment, location.offset,
1470 globalbits, instruction);
1471 /* We can't get an invalid instruction here */
1472 increment_offset(l);
1473
1474 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001475 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001476 for (n = 2; n <= instruction->times; n++) {
1477 l = assemble(location.segment, location.offset,
1478 globalbits, instruction);
1479 increment_offset(l);
1480 }
1481 lfmt->downlevel(LIST_TIMES);
1482 }
1483 }
1484}
1485
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001486static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001487{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001488 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001490 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001491 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001492
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001493 switch (cmd_sb) {
1494 case 16:
1495 break;
1496 case 32:
1497 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001498 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001499 break;
1500 case 64:
1501 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001502 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001503 break;
1504 default:
1505 panic();
1506 break;
1507 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001508
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001509 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001510
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001511 if (listname && !keep_all) {
1512 /* Remove the list file in case we die before the output pass */
1513 remove(listname);
1514 }
1515
1516 while (!terminate_after_phase && !pass_final()) {
1517 _passn++;
1518 if (pass_type() != PASS_OPT || !global_offset_changed)
1519 _pass_type++;
1520 global_offset_changed = 0;
1521
1522 /*
1523 * Create a warning buffer list unless we are in
1524 * pass 2 (everything will be emitted immediately in pass 2.)
1525 */
1526 if (warn_list) {
1527 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001528 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001529 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001530
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001531 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001532 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001533
H. Peter Anvincac0b192017-03-28 16:12:30 -07001534 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 cpu = cmd_cpu;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001536 if (pass_final())
1537 lfmt->init(listname);
1538
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001539 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001540 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 saa_rewind(forwrefs);
1542 forwref = saa_rstruct(forwrefs);
1543 raa_free(offsets);
1544 offsets = raa_init();
1545 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001546 location.segment = NO_SEG;
1547 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001548 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001549 location.known = true;
1550 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001551 switch_segment(ofmt->section(NULL, &globalbits));
1552 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001553
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001555
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001557 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001558 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001559 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001560
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001561 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001562 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001563 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001564 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001565 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001566 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001567
H. Peter Anvinc7131682017-03-07 17:45:01 -08001568 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001569 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001570 forward_refs(&output_ins);
1571 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001572 cleanup_insn(&output_ins);
1573
1574 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001577
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001578 preproc->cleanup_pass();
1579
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001580 if (global_offset_changed) {
1581 switch (pass_type()) {
1582 case PASS_OPT:
1583 /*
1584 * This is the only pass type that can be executed more
1585 * than once, and therefore has the ability to stall.
1586 */
1587 if (global_offset_changed < prev_offset_changed) {
1588 prev_offset_changed = global_offset_changed;
1589 stall_count = 0;
1590 } else {
1591 stall_count++;
1592 }
1593
1594 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1595 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1596 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001597 nasm_nonfatalf(ERR_UNDEAD,
1598 "unable to find valid values for all labels "
1599 "after %"PRId64" passes; "
1600 "stalled for %"PRId64", giving up.",
1601 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001602 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001603 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001604 }
1605 break;
1606
1607 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001608 /*!
1609 *!phase [off] phase error during stabilization
1610 *! warns about symbols having changed values during
1611 *! the second-to-last assembly pass. This is not
1612 *! inherently fatal, but may be a source of bugs.
1613 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001614 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1615 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001616 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001617 break;
1618
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001619 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001620 nasm_nonfatalf(ERR_UNDEAD,
1621 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001622 break;
1623
1624 default:
1625 /* This is normal, we'll keep going... */
1626 break;
1627 }
1628 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001629
1630 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001631 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001632
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001633 if (opt_verbose_info && pass_final()) {
1634 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001635 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001636 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001637
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001638 strlist_free(&warn_list);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001639 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001640}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001641
Ed Berosetfa771012002-06-09 20:56:40 +00001642/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001643 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001644 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001645static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001646{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001647 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001648
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001649 if ((severity & ERR_MASK) >= ERR_FATAL)
1650 return 0; /* Fatal errors are never suppressible */
1651
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001652 /* Warnings MUST HAVE a warning category specifier! */
1653 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001654
1655 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001656 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001657
1658 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001659}
1660
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001661static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001662{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001663 errflags type = severity & ERR_MASK;
1664
H. Peter Anvin8f622462017-04-02 19:02:29 -07001665 /*
1666 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001667 * We can never skip fatal errors as by definition they cannot be
1668 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001669 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001670 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001671 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001672
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001673 /*
1674 * ERR_LISTMSG messages are always skipped; the list file
1675 * receives them anyway as this function is not consulted
1676 * for sending to the list file.
1677 */
1678 if (type == ERR_LISTMSG)
1679 return true;
1680
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001681 /* This message not applicable unless pass_final */
1682 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001683}
1684
Ed Berosetfa771012002-06-09 20:56:40 +00001685/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001686 * check for suppressed message (usually warnings or notes)
1687 *
1688 * @param severity the severity of the warning or error
1689 * @return true if we should abort error/warning printing
1690 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001691static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001692{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001693 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001694 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001695 return false;
1696
1697 /* This error/warning is pointless if we are dead anyway */
1698 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1699 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001700
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001701 return !(warning_state[warn_index(severity)] & WARN_ST_ENABLED);
1702}
1703
1704/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001705 * Return the true error type (the ERR_MASK part) of the given
1706 * severity, accounting for warnings that may need to be promoted to
1707 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001708 *
1709 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001710 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001711 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001712static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001713{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001714 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1715 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001716
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001717 type = severity & ERR_MASK;
1718
1719 /* Promote warning to error? */
1720 if (type == ERR_WARNING) {
1721 uint8_t state = warning_state[warn_index(severity)];
1722 if ((state & warn_is_err) == warn_is_err)
1723 type = ERR_NONFATAL;
1724 }
1725
1726 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001727}
1728
1729/**
Ed Berosetfa771012002-06-09 20:56:40 +00001730 * common error reporting
1731 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001732 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001733 * specific error message to error_file and may or may not return. It
1734 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001735 *
1736 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001737 * @param fmt the printf style format string
1738 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001739static void nasm_verror_asm(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001740{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001741 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001742 char warnsuf[64];
1743 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001744 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001745 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001746 const char *currentfile = NULL;
1747 int32_t lineno = 0;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001748 static const char * const pfx_table[ERR_MASK+1] = {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001749 ";;; ", "debug: ", "info: ", "warning: ",
1750 "error: ", "", "fatal: ", "panic: "
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001751 };
H. Peter Anvin77016c82018-12-10 22:29:49 -08001752
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001753 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001754 return;
1755
1756 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001757 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001758 if (!currentfile) {
1759 currentfile = currentfile ? currentfile :
1760 inname && inname[0] ? inname :
1761 outname && outname[0] ? outname :
1762 NULL;
1763 lineno = 0;
1764 }
1765 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001766
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001767 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001768 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001769 else
1770 pfx = pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001771
1772 vsnprintf(msg, sizeof msg, fmt, args);
1773 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001774 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1775 /*
1776 * It's a warning without ERR_HERE defined, and we are not already
1777 * unwinding the macros that led us here.
1778 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001779 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001780 (true_type >= ERR_NONFATAL) ? "error=" : "",
1781 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001782 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001783
H. Peter Anvinddb29062018-12-11 00:06:29 -08001784 *linestr = 0;
1785 if (lineno) {
1786 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1787 errfmt->beforeline, lineno, errfmt->afterline);
1788 }
1789
H. Peter Anvin77016c82018-12-10 22:29:49 -08001790 if (!skip_this_pass(severity)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001791 const char *file = currentfile ? currentfile : "nasm";
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001792 const char *here = "";
1793
1794 if (severity & ERR_HERE)
1795 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001796
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001797 if (warn_list && true_type < ERR_NONFATAL &&
1798 !(pass_first() && (severity & ERR_PASS1)))
1799 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001800 /*
1801 * Buffer up warnings until we either get an error
1802 * or we are on the code-generation pass.
1803 */
1804 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1805 file, linestr, errfmt->beforemsg,
1806 pfx, msg, here, warnsuf);
1807 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001808 /*
1809 * If we have buffered warnings, and this is a non-warning,
1810 * output them now.
1811 */
1812 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001813 strlist_write(warn_list, "\n", error_file);
1814 strlist_free(&warn_list);
1815 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001816
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001817 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1818 file, linestr, errfmt->beforemsg,
1819 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001820
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001821 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001822 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001823
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001824 /* Are we recursing from error_list_macros? */
1825 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001826 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001827
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001828 /*
1829 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001830 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001831 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001832 if (severity & ERR_HERE) {
1833 if (lineno)
1834 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
1835 pfx, msg, currentfile, lineno, warnsuf);
1836 else if (currentfile)
1837 lfmt->error(severity, "%s%s in file %s%s",
1838 pfx, msg, currentfile, warnsuf);
1839 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001840 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08001841 pfx, msg, warnsuf);
1842 } else {
1843 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
1844 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001845
H. Peter Anvin8f622462017-04-02 19:02:29 -07001846 if (skip_this_pass(severity))
1847 return;
1848
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001849 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001850 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001851
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001852 /* error_list_macros can for obvious reasons not work with ERR_HERE */
1853 if (!(severity & ERR_HERE))
1854 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001855
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001856 switch (true_type) {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001857 case ERR_LISTMSG:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 case ERR_DEBUG:
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001859 case ERR_INFO:
H. Peter Anvinb030c922007-11-13 11:31:15 -08001860 case ERR_WARNING:
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001861 /* no further action, by definition */
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001862 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001864 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 break;
1866 case ERR_FATAL:
1867 if (ofile) {
1868 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001869 if (!keep_all)
1870 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001871 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 }
1873 if (want_usage)
1874 usage();
1875 exit(1); /* instantly die */
1876 break; /* placate silly compilers */
1877 case ERR_PANIC:
1878 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001879
1880 if (abort_on_panic)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001881 abort(); /* halt, catch fire, dump core/stop debugger */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001882
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001883 if (ofile) {
1884 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001885 if (!keep_all)
1886 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001887 ofile = NULL;
1888 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 exit(3);
1890 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001891 default:
1892 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001893 }
1894}
1895
H. Peter Anvin734b1882002-04-30 21:01:08 +00001896static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001897{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001898 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001899}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001900
1901static void help(const char xopt)
1902{
1903 int i;
1904
1905 printf
1906 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1907 "[-l listfile]\n"
1908 " [options...] [--] filename\n"
1909 " or nasm -v (or --v) for version info\n\n"
1910 "\n"
1911 "Response files should contain command line parameters,\n"
1912 "one per line.\n"
1913 "\n"
1914 " -t assemble in SciTech TASM compatible mode\n");
1915 printf
1916 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1917 " -a don't preprocess (assemble only)\n"
1918 " -M generate Makefile dependencies on stdout\n"
1919 " -MG d:o, missing files assumed generated\n"
1920 " -MF file set Makefile dependency file\n"
1921 " -MD file assemble and generate dependencies\n"
1922 " -MT file dependency target name\n"
1923 " -MQ file dependency target name (quoted)\n"
1924 " -MP emit phony target\n\n"
1925 " -Zfile redirect error messages to file\n"
1926 " -s redirect error messages to stdout\n\n"
1927 " -g generate debugging information\n\n"
1928 " -F format select a debugging format\n\n"
1929 " -gformat same as -g -F format\n\n"
1930 " -o outfile write output to an outfile\n\n"
1931 " -f format select an output format\n\n"
1932 " -l listfile write listing to a listfile\n\n"
1933 " -Ipath add a pathname to the include file path\n");
1934 printf
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001935 (" -Oflags... optimize opcodes, immediates and branch offsets\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001936 " -O0 no optimization\n"
1937 " -O1 minimal optimization\n"
1938 " -Ox multipass optimization (default)\n"
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001939 " -Ov display the number of passes executed at the end\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001940 " -Pfile pre-include a file (also --include)\n"
1941 " -Dmacro[=str] pre-define a macro\n"
1942 " -Umacro undefine a macro\n"
1943 " -Xformat specifiy error reporting format (gnu or vc)\n"
1944 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1945 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1946 " -w[+-]error[=foo]\n"
1947 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001948 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001949 " --pragma str pre-executes a specific %%pragma\n"
1950 " --before str add line (usually a preprocessor statement) before the input\n"
1951 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001952 " to all extern, common and global symbols (also --gprefix)\n"
1953 " --postfix str append the given string to all the given string\n"
1954 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001955 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001956 " --lpostfix str append the given string to all other symbols\n"
1957 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001958 " --no-line ignore %%line directives in input\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001959 " --limit-X val set execution limit X\n");
1960
1961 for (i = 0; i <= LIMIT_MAX; i++) {
1962 printf(" %-15s %s (default ",
1963 limit_info[i].name, limit_info[i].help);
1964 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001965 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001966 } else {
1967 printf("unlimited)\n");
1968 }
1969 }
1970
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001971 printf("\nWarnings for the -W/-w options: (defaults in brackets)\n");
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001972
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001973 for (i = 1; i <= WARN_IDX_ALL; i++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001974 printf(" %-23s %s%s\n",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001975 warning_name[i], warning_help[i],
1976 i == WARN_IDX_ALL ? "\n" :
1977 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
1978 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
1979 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001980
1981 if (xopt == 'f') {
1982 printf("valid output formats for -f are"
1983 " (`*' denotes default):\n");
1984 ofmt_list(ofmt, stdout);
1985 } else {
1986 printf("For a list of valid output formats, use -hf.\n");
1987 printf("For a list of debug formats, use -f <format> -y.\n");
1988 }
1989}