blob: 398ad3de52e1055eca3139e74c8e00effd0d5414 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvinc7131682017-03-07 17:45:01 -08003 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080046#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
48#include "nasm.h"
49#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
H. Peter Anvin55568c12016-10-03 19:46:49 -070079static void parse_cmdline(int, char **, int);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080082static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040083static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
84static void nasm_verror_vc(int severity, const char *fmt, va_list args);
85static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086static void usage(void);
87
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080088static bool using_debug_info, opt_verbose_info;
89static const char *debug_format;
90
H. Peter Anvin6867acc2007-10-10 14:58:45 -070091bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080092int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080093static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000094int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080095int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000096
H. Peter Anvin9bd15062009-07-18 21:07:17 -040097static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080098
Keith Kaniosa6dfa782007-04-13 16:47:53 +000099static char inname[FILENAME_MAX];
100static char outname[FILENAME_MAX];
101static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700102static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000104/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800105const struct ofmt *ofmt = &OF_DEFAULT;
106const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400107const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000108
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000110
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400111FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700112int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100113static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400114
115static iflag_t cpu;
116static iflag_t cmd_cpu;
117
Charles Craynec1905c22008-09-11 18:54:06 -0700118int64_t global_offset_changed; /* referenced in labels.c */
119int64_t prev_offset_changed;
120int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000121
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800122struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000124int32_t abs_seg; /* ABSOLUTE segment basis */
125int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000126
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000127static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000128
H. Peter Anvine2c80182005-01-15 22:15:51 +0000129static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700130static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131
H. Peter Anvine7469712016-02-18 02:20:59 -0800132static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400133
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400134#define OP_NORMAL (1u << 0)
135#define OP_PREPROCESS (1u << 1)
136#define OP_DEPEND (1u << 2)
137
138static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400139
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700140/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700141static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700142static bool depend_missing_ok = false;
143static const char *depend_target = NULL;
144static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000145
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000147 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800148 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000149 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200150
H. Peter Anvin972079f2008-09-30 17:01:23 -0700151static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
152static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000153
H. Peter Anvin972079f2008-09-30 17:01:23 -0700154static const struct warning {
155 const char *name;
156 const char *help;
157 bool enabled;
158} warnings[ERR_WARN_MAX+1] = {
159 {"error", "treat warnings as errors", false},
160 {"macro-params", "macro calls with wrong parameter count", true},
161 {"macro-selfref", "cyclic macro references", false},
162 {"macro-defaults", "macros with more default than optional parameters", true},
163 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700164 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700165 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
166 {"float-overflow", "floating point overflow", true},
167 {"float-denorm", "floating point denormal", false},
168 {"float-underflow", "floating point underflow", false},
169 {"float-toolong", "too many digits in floating-point number", true},
170 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800171 {"lock", "lock prefix on unlockable instructions", true},
172 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800173 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800174 {"zext-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin69550ea2016-05-09 12:05:56 -0700175 {"ptr", "non-NASM keyword used in other assemblers", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000176};
177
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700178static bool want_usage;
179static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800180bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000181
H. Peter Anvin55340992012-09-09 17:09:00 -0700182static char *quote_for_make(const char *str);
183
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400184static int64_t get_curr_offs(void)
185{
186 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
187}
188
189static void set_curr_offs(int64_t l_off)
190{
191 if (in_abs_seg)
192 abs_offset = l_off;
193 else
194 offsets = raa_write(offsets, location.segment, l_off);
195}
196
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000197static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000199 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000200 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800201 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000202 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000203 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000204}
205
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800206/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800207static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800208{
209 int64_t t;
210 int64_t y = tm->tm_year;
211
212 /* See IEEE 1003.1:2004, section 4.14 */
213
214 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
215 t += tm->tm_yday;
216 t *= 24;
217 t += tm->tm_hour;
218 t *= 60;
219 t += tm->tm_min;
220 t *= 60;
221 t += tm->tm_sec;
222
223 return t;
224}
225
226static void define_macros_early(void)
227{
228 char temp[128];
229 struct tm lt, *lt_p, gm, *gm_p;
230 int64_t posix_time;
231
232 lt_p = localtime(&official_compile_time);
233 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400234 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800235
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400236 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
237 preproc->pre_define(temp);
238 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
239 preproc->pre_define(temp);
240 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
241 preproc->pre_define(temp);
242 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
243 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800244 }
245
246 gm_p = gmtime(&official_compile_time);
247 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400248 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800249
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400250 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
251 preproc->pre_define(temp);
252 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
253 preproc->pre_define(temp);
254 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
255 preproc->pre_define(temp);
256 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
257 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700259
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800261 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800263 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400265 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800266
267 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400268 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
269 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800270 }
271}
272
273static void define_macros_late(void)
274{
275 char temp[128];
276
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400277 /*
278 * In case if output format is defined by alias
279 * we have to put shortname of the alias itself here
280 * otherwise ABI backward compatibility gets broken.
281 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400282 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400283 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400284 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800285}
286
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700287static void emit_dependencies(StrList *list)
288{
289 FILE *deps;
290 int linepos, len;
291 StrList *l, *nl;
292
293 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700294 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400295 if (!deps) {
296 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
297 "unable to write dependency file `%s'", depend_file);
298 return;
299 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700300 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400301 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700302 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700303
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700304 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400305 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700306 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400307 len = strlen(file);
308 if (linepos + len > 62 && linepos > 1) {
309 fprintf(deps, " \\\n ");
310 linepos = 1;
311 }
312 fprintf(deps, " %s", file);
313 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700314 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700315 }
316 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700317
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400318 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400319 if (depend_emit_phony)
320 fprintf(deps, "%s:\n\n", l->str);
321 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700322 }
323
324 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400325 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326}
327
H. Peter Anvin038d8612007-04-12 16:54:50 +0000328int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000329{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700330 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700331
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800332 time(&official_compile_time);
333
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400334 iflag_set(&cpu, IF_PLEVEL);
335 iflag_set(&cmd_cpu, IF_PLEVEL);
336
Charles Crayne2581c862008-09-10 19:21:52 -0700337 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700338 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400339 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000340
H. Peter Anvin97e15752007-09-25 08:47:47 -0700341 error_file = stderr;
342
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700343 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700344 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700345
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000347 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000348
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400350 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000351
H. Peter Anvin55568c12016-10-03 19:46:49 -0700352 parse_cmdline(argc, argv, 1);
353 if (terminate_after_phase) {
354 if (want_usage)
355 usage();
356 return 1;
357 }
358
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700359 /*
360 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700361 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700362 */
363 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800364 define_macros_early();
365
H. Peter Anvin55568c12016-10-03 19:46:49 -0700366 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367 if (terminate_after_phase) {
368 if (want_usage)
369 usage();
370 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000371 }
372
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800373 if (!using_debug_info) {
374 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800375 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800376 } else if (!debug_format) {
377 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800378 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800379 } else {
380 dfmt = dfmt_find(ofmt, debug_format);
381 if (!dfmt) {
382 nasm_fatal(ERR_NOFILE | ERR_USAGE,
383 "unrecognized debug format `%s' for"
384 " output format `%s'",
385 debug_format, ofmt->shortname);
386 }
387 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000388
H. Peter Anvin76690a12002-04-30 20:52:49 +0000389 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400390 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000391
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000392 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800393 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000394
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400395 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700396 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400397 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700398
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400399 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000400 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700401
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400402 if (depend_missing_ok)
403 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700404
H. Peter Anvin130736c2016-02-17 20:27:41 -0800405 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400407 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 while ((line = preproc->getline()))
410 nasm_free(line);
411 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400412 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000413 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700414 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000415 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000417
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700419 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800421 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 "unable to open output file `%s'",
423 outname);
424 } else
425 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000426
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700427 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000428
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400429 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800430 preproc->reset(inname, 3, depend_ptr);
431 memcpy(warning_on, warning_on_global,
432 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700433
H. Peter Anvine2c80182005-01-15 22:15:51 +0000434 while ((line = preproc->getline())) {
435 /*
436 * We generate %line directives if needed for later programs
437 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000438 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 int altline = src_get(&linnum, &file_name);
440 if (altline) {
441 if (altline == 1 && lineinc == 1)
442 nasm_fputs("", ofile);
443 else {
444 lineinc = (altline != -1 || lineinc != 1);
445 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000446 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 file_name);
448 }
449 prior_linnum = linnum;
450 }
451 nasm_fputs(line, ofile);
452 nasm_free(line);
453 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 preproc->cleanup(0);
455 if (ofile)
456 fclose(ofile);
457 if (ofile && terminate_after_phase)
458 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400459 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400460 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400462 if (operating_mode & OP_NORMAL) {
463 /*
464 * We must call ofmt->filename _anyway_, even if the user
465 * has specified their own output file, because some
466 * formats (eg OBJ and COFF) use ofmt->filename to find out
467 * the name of the input file and then put that inside the
468 * file.
469 */
470 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700472 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400473 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800474 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400475 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400477 /*
478 * We must call init_labels() before ofmt->init() since
479 * some object formats will want to define labels in their
480 * init routines. (eg OS/2 defines the FLAT group)
481 */
482 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400484 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400485 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400487 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200488
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400489 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800490 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400491 cleanup_labels();
492 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700493 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400494 nasm_error(ERR_NONFATAL|ERR_NOFILE,
495 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700496 terminate_after_phase = true;
497 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400498 }
499
500 if (ofile) {
501 fclose(ofile);
502 if (terminate_after_phase)
503 remove(outname);
504 ofile = NULL;
505 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000507
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700508 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400509 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700510
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000511 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000513
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 raa_free(offsets);
515 saa_free(forwrefs);
516 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000517 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700518 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000519
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700520 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000521}
522
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523/*
524 * Get a parameter for a command line option.
525 * First arg must be in the form of e.g. -f...
526 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800527static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000528{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800529 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400530 if (p[2]) /* the parameter's in the option */
531 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800533 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000535 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400536 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000538 return NULL;
539}
540
H. Peter Anvindc242712007-11-18 11:55:10 -0800541/*
542 * Copy a filename
543 */
544static void copy_filename(char *dst, const char *src)
545{
546 size_t len = strlen(src);
547
548 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800549 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400550 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800551 }
552 strncpy(dst, src, FILENAME_MAX);
553}
554
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700555/*
556 * Convert a string to Make-safe form
557 */
558static char *quote_for_make(const char *str)
559{
560 const char *p;
561 char *os, *q;
562
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400563 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700564 size_t nbs = 0;
565
566 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400567 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700568
569 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400570 switch (*p) {
571 case ' ':
572 case '\t':
573 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
574 n += nbs + 2;
575 nbs = 0;
576 break;
577 case '$':
578 case '#':
579 nbs = 0;
580 n += 2;
581 break;
582 case '\\':
583 nbs++;
584 n++;
585 break;
586 default:
587 nbs = 0;
588 n++;
589 break;
590 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700591 }
592
593 /* Convert N backslashes at the end of filename to 2N backslashes */
594 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400595 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700596
597 os = q = nasm_malloc(n);
598
599 nbs = 0;
600 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400601 switch (*p) {
602 case ' ':
603 case '\t':
604 while (nbs--)
605 *q++ = '\\';
606 *q++ = '\\';
607 *q++ = *p;
608 break;
609 case '$':
610 *q++ = *p;
611 *q++ = *p;
612 nbs = 0;
613 break;
614 case '#':
615 *q++ = '\\';
616 *q++ = *p;
617 nbs = 0;
618 break;
619 case '\\':
620 *q++ = *p;
621 nbs++;
622 break;
623 default:
624 *q++ = *p;
625 nbs = 0;
626 break;
627 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700628 }
629 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400630 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700631
632 *q = '\0';
633
634 return os;
635}
636
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000638 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000640};
641
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100642enum text_options {
643 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800644 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100645};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800646static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 {"prefix", OPT_PREFIX},
648 {"postfix", OPT_POSTFIX},
649 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000650};
651
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400652static void show_version(void)
653{
654 printf("NASM version %s compiled on %s%s\n",
655 nasm_version, nasm_date, nasm_compile_options);
656 exit(0);
657}
658
H. Peter Anvin423e3812007-11-15 17:12:29 -0800659static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700660static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000662 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800663 int i;
664 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700665 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000666
667 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800668 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000669
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400671 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
672 /* These parameters take values */
673 if (!(param = get_param(p, q, &advance)))
674 return advance;
675 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800676
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 switch (p[1]) {
678 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700679 if (pass == 1)
680 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700682
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400683 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700684 if (pass == 2)
685 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400686 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700687
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400688 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700689 if (pass == 1) {
690 ofmt = ofmt_find(param, &ofmt_alias);
691 if (!ofmt) {
692 nasm_fatal(ERR_NOFILE | ERR_USAGE,
693 "unrecognised output format `%s' - "
694 "use -hf for a list", param);
695 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400696 }
697 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700698
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400699 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700700 if (pass == 2) {
701 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700702
H. Peter Anvin55568c12016-10-03 19:46:49 -0700703 if (!*param) {
704 /* Naked -O == -Ox */
705 optimizing = MAX_OPTIMIZE;
706 } else {
707 while (*param) {
708 switch (*param) {
709 case '0': case '1': case '2': case '3': case '4':
710 case '5': case '6': case '7': case '8': case '9':
711 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700712
H. Peter Anvin55568c12016-10-03 19:46:49 -0700713 /* -O0 -> optimizing == -1, 0.98 behaviour */
714 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
715 if (opt < 2)
716 optimizing = opt - 1;
717 else
718 optimizing = opt;
719 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700720
H. Peter Anvin55568c12016-10-03 19:46:49 -0700721 case 'v':
722 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400723 param++;
724 opt_verbose_info = true;
725 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700726
H. Peter Anvin55568c12016-10-03 19:46:49 -0700727 case 'x':
728 param++;
729 optimizing = MAX_OPTIMIZE;
730 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700731
H. Peter Anvin55568c12016-10-03 19:46:49 -0700732 default:
733 nasm_fatal(0,
734 "unknown optimization option -O%c\n",
735 *param);
736 break;
737 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400738 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700739 if (optimizing > MAX_OPTIMIZE)
740 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400741 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400742 }
743 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800744
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400745 case 'p': /* pre-include */
746 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700747 if (pass == 2)
748 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400749 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800750
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 case 'd': /* pre-define */
752 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700753 if (pass == 2)
754 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400755 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800756
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400757 case 'u': /* un-define */
758 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700759 if (pass == 2)
760 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400761 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800762
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400763 case 'i': /* include search path */
764 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700765 if (pass == 2)
766 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400767 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800768
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400769 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700770 if (pass == 2)
771 copy_filename(listname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400772 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800773
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400774 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700775 if (pass == 1)
776 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400777 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800778
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400779 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700780 if (pass == 2) {
781 using_debug_info = true;
782 debug_format = param;
783 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400784 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800785
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400786 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700787 if (pass == 1) {
788 if (nasm_stricmp("vc", param) == 0)
789 nasm_set_verror(nasm_verror_vc);
790 else if (nasm_stricmp("gnu", param) == 0)
791 nasm_set_verror(nasm_verror_gnu);
792 else
793 nasm_fatal(ERR_NOFILE | ERR_USAGE,
794 "unrecognized error reporting format `%s'",
795 param);
796 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800798
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700800 if (pass == 2) {
801 using_debug_info = true;
802 if (p[2])
803 debug_format = nasm_skip_spaces(p + 2);
804 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800806
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 case 'h':
808 printf
809 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
810 "[-l listfile]\n"
811 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400812 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800813 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700815 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700817 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400818 " -MG d:o, missing files assumed generated\n"
819 " -MF <file> set Makefile dependency file\n"
820 " -MD <file> assemble and generate dependencies\n"
821 " -MT <file> dependency target name\n"
822 " -MQ <file> dependency target name (quoted)\n"
823 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700824 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800826 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800828 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400829 " -o outfile write output to an outfile\n\n"
830 " -f format select an output format\n\n"
831 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 " -I<path> adds a pathname to the include file path\n");
833 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700834 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400835 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400836 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400837 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 " -P<file> pre-includes a file\n"
839 " -D<macro>[=<value>] pre-defines a macro\n"
840 " -U<macro> undefines a macro\n"
841 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800842 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400843 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400844 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400845 "--prefix,--postfix\n"
846 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100847 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800848 "Warnings:\n");
849 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700851 warnings[i].name, warnings[i].help,
852 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 printf
854 ("\nresponse files should contain command line parameters"
855 ", one per line.\n");
856 if (p[2] == 'f') {
857 printf("\nvalid output formats for -f are"
858 " (`*' denotes default):\n");
859 ofmt_list(ofmt, stdout);
860 } else {
861 printf("\nFor a list of valid output formats, use -hf.\n");
862 printf("For a list of debug formats, use -f <form> -y.\n");
863 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800866
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 case 'y':
868 printf("\nvalid debug formats for '%s' output format are"
869 " ('*' denotes default):\n", ofmt->shortname);
870 dfmt_list(ofmt, stdout);
871 exit(0);
872 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800873
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700875 if (pass == 2)
876 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800878
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400880 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800882
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400883 case 'e': /* preprocess only */
884 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700885 if (pass == 1)
886 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800888
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400889 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700890 if (pass == 1)
891 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800893
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400894 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700895 if (pass == 2) {
896 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
897 do_warn = false;
898 param += 3;
899 } else {
900 do_warn = true;
901 }
902 goto set_warning;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400903 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700904 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800905
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 case 'w':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700907 if (pass == 2) {
908 if (param[0] != '+' && param[0] != '-') {
909 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
910 "invalid option to `-w'");
911 break;
912 }
913 do_warn = (param[0] == '+');
914 param++;
915 goto set_warning;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700917 break;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400918
919set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400920 for (i = 0; i <= ERR_WARN_MAX; i++) {
921 if (!nasm_stricmp(param, warnings[i].name))
922 break;
923 }
924 if (i <= ERR_WARN_MAX) {
925 warning_on_global[i] = do_warn;
926 } else if (!nasm_stricmp(param, "all")) {
927 for (i = 1; i <= ERR_WARN_MAX; i++)
928 warning_on_global[i] = do_warn;
929 } else if (!nasm_stricmp(param, "none")) {
930 for (i = 1; i <= ERR_WARN_MAX; i++)
931 warning_on_global[i] = !do_warn;
932 } else {
H. Peter Anvinb4f734f2016-05-09 12:13:08 -0700933 /* Ignore invalid warning names; forward compatibility */
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400934 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800936
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 case 'M':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700938 if (pass == 2) {
939 switch (p[2]) {
940 case 0:
941 operating_mode = OP_DEPEND;
942 break;
943 case 'G':
944 operating_mode = OP_DEPEND;
945 depend_missing_ok = true;
946 break;
947 case 'P':
948 depend_emit_phony = true;
949 break;
950 case 'D':
951 operating_mode = OP_NORMAL;
952 depend_file = q;
953 advance = true;
954 break;
955 case 'F':
956 depend_file = q;
957 advance = true;
958 break;
959 case 'T':
960 depend_target = q;
961 advance = true;
962 break;
963 case 'Q':
964 depend_target = quote_for_make(q);
965 advance = true;
966 break;
967 default:
968 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
969 "unknown dependency option `-M%c'", p[2]);
970 break;
971 }
972 if (advance && (!q || !q[0])) {
973 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
974 "option `-M%c' requires a parameter", p[2]);
975 break;
976 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400977 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000979
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 case '-':
981 {
982 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000983
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 if (p[2] == 0) { /* -- => stop processing options */
985 stopoptions = 1;
986 break;
987 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400988
989 if (!nasm_stricmp(p, "--v"))
990 show_version();
991
Andy Willis3f546032016-09-13 00:02:21 +0300992 if (!nasm_stricmp(p, "--version"))
993 show_version();
994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 for (s = 0; textopts[s].label; s++) {
996 if (!nasm_stricmp(p + 2, textopts[s].label)) {
997 break;
998 }
999 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001000
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 switch (s) {
1002
1003 case OPT_PREFIX:
1004 case OPT_POSTFIX:
1005 {
1006 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001007 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 ERR_USAGE,
1009 "option `--%s' requires an argument",
1010 p + 2);
1011 break;
1012 } else {
1013 advance = 1, param = q;
1014 }
1015
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001016 switch (s) {
1017 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001018 if (pass == 2)
1019 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001021 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001022 if (pass == 2)
1023 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001024 break;
1025 default:
H. Peter Anvin41087062016-03-03 14:27:34 -08001026 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001027 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 break;
1029 }
1030 break;
1031 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08001032
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 default:
1034 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001035 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 "unrecognised option `--%s'", p + 2);
1037 break;
1038 }
1039 }
1040 break;
1041 }
1042
1043 default:
1044 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001045 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 "unrecognised option `-%c'", p[1]);
1047 break;
1048 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001049 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001051 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001053 } else {
1054 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001055 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001056 }
1057
1058 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001059}
1060
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061#define ARG_BUF_DELTA 128
1062
H. Peter Anvin55568c12016-10-03 19:46:49 -07001063static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001065 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001066 int bufsize, prevargsize;
1067
1068 bufsize = prevargsize = ARG_BUF_DELTA;
1069 buffer = nasm_malloc(ARG_BUF_DELTA);
1070 prevarg = nasm_malloc(ARG_BUF_DELTA);
1071 prevarg[0] = '\0';
1072
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 p = buffer;
1075 while (1) { /* Loop to handle long lines */
1076 q = fgets(p, bufsize - (p - buffer), rfile);
1077 if (!q)
1078 break;
1079 p += strlen(p);
1080 if (p > buffer && p[-1] == '\n')
1081 break;
1082 if (p - buffer > bufsize - 10) {
1083 int offset;
1084 offset = p - buffer;
1085 bufsize += ARG_BUF_DELTA;
1086 buffer = nasm_realloc(buffer, bufsize);
1087 p = buffer + offset;
1088 }
1089 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 if (!q && p == buffer) {
1092 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001093 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 nasm_free(buffer);
1095 nasm_free(prevarg);
1096 return;
1097 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /*
1100 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1101 * them are present at the end of the line.
1102 */
1103 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001104
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001105 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001107
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001108 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001109
H. Peter Anvin55568c12016-10-03 19:46:49 -07001110 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001112
Charles Crayne192d5b52007-10-18 19:02:42 -07001113 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 prevargsize += ARG_BUF_DELTA;
1115 prevarg = nasm_realloc(prevarg, prevargsize);
1116 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001117 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001118 }
1119}
1120
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001121/* Function to process args from a string of args, rather than the
1122 * argv array. Used by the environment variable and response file
1123 * processing.
1124 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001125static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001127 char *p, *q, *arg, *prevarg;
1128 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001129
1130 p = args;
1131 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001133 arg = NULL;
1134 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 q = p;
1136 while (*p && *p != separator)
1137 p++;
1138 while (*p == separator)
1139 *p++ = '\0';
1140 prevarg = arg;
1141 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001142 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001144 }
1145 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001146 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001147}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001148
H. Peter Anvin55568c12016-10-03 19:46:49 -07001149static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001150{
1151 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001152 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001153 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001154 perror(file);
1155 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001156 }
1157 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001158 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001159 }
1160 fclose(f);
1161}
1162
H. Peter Anvin55568c12016-10-03 19:46:49 -07001163static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001164{
1165 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001166 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001167 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001168
Charles Craynefcce07f2007-09-30 22:15:36 -07001169 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001170
H. Peter Anvin972079f2008-09-30 17:01:23 -07001171 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001172 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001173
1174 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001175 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001176 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001177 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001178 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001180 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001182 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001183
1184 /*
1185 * Now process the actual command line.
1186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001188 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 argv++;
1190 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001191 /*
1192 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 * arguments like the environment variable. This allows us
1194 * to have multiple arguments on a single line, which is
1195 * different to the -@resp file processing below for regular
1196 * NASM.
1197 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001198 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 argc--;
1200 argv++;
1201 }
1202 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001203 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001204 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001205 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001206 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001207 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 fclose(rfile);
1209 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001210 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 "unable to open response file `%s'", p);
1212 }
1213 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001214 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001215 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001216 }
1217
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001218 /*
1219 * Look for basic command line typos. This definitely doesn't
1220 * catch all errors, but it might help cases of fumbled fingers.
1221 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001222 if (pass != 2)
1223 return;
1224
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001225 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001226 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001227 "no input file specified");
1228 else if (!strcmp(inname, errname) ||
1229 !strcmp(inname, outname) ||
1230 !strcmp(inname, listname) ||
1231 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001232 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001233 "file `%s' is both input and output file",
1234 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001235
H. Peter Anvin70653092007-10-19 14:42:29 -07001236 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001237 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001238 if (!error_file) {
1239 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001240 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001241 "cannot open file `%s' for error messages",
1242 errname);
1243 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001244 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001245}
1246
H. Peter Anvinc7131682017-03-07 17:45:01 -08001247static enum directives parse_directive_line(char **directive, char **value)
1248{
1249 char *p, *q, *buf;
1250
1251 buf = nasm_skip_spaces(*directive);
1252
1253 /*
1254 * It should be enclosed in [ ].
1255 * XXX: we don't check there is nothing else on the remainder of the
1256 * line, except a possible comment.
1257 */
1258 if (*buf != '[')
1259 return D_none;
1260 q = strchr(buf, ']');
1261 if (!q)
1262 return D_corrupt;
1263
1264 /*
1265 * Strip off the comments. XXX: this doesn't account for quoted
1266 * strings inside a directive. We should really strip the
1267 * comments in generic code, not here. While we're at it, it
1268 * would be better to pass the backend a series of tokens instead
1269 * of a raw string, and actually process quoted strings for it,
1270 * like of like argv is handled in C.
1271 */
1272 p = strchr(buf, ';');
1273 if (p) {
1274 if (p < q) /* ouch! somewhere inside */
1275 return D_corrupt;
1276 *p = '\0';
1277 }
1278
1279 /* no brace, no trailing spaces */
1280 *q = '\0';
1281 nasm_zap_spaces_rev(--q);
1282
1283 /* directive */
1284 p = nasm_skip_spaces(++buf);
1285 q = nasm_skip_word(p);
1286 if (!q)
1287 return D_corrupt; /* sigh... no value there */
1288 *q = '\0';
1289 *directive = p;
1290
1291 /* and value finally */
1292 p = nasm_skip_spaces(++q);
1293 *value = p;
1294
1295 return find_directive(*directive);
1296}
1297
1298static void process_pragma(char *str)
1299{
1300 (void)str;
1301}
1302
1303static enum directives process_directives(char *directive)
1304{
1305 enum directives d;
1306 char *value, *p, *q, *special;
1307 struct tokenval tokval;
1308 bool bad_param = false;
1309
1310 d = parse_directive_line(&directive, &value);
1311
1312 switch (d) {
1313 case D_none:
1314 return D_none; /* Not a directive */
1315
1316 case D_corrupt:
1317 nasm_error(ERR_NONFATAL, "invalid directive line");
1318 break;
1319
1320 default: /* It's a backend-specific directive */
1321 if (ofmt->directive(d, value, pass2))
1322 break;
1323 /* else fall through */
1324 case D_unknown:
1325 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1326 "unrecognised directive [%s]", directive);
1327 break;
1328
1329 case D_SEGMENT: /* [SEGMENT n] */
1330 case D_SECTION:
1331 {
1332 int sb;
1333 int32_t seg = ofmt->section(value, pass2, &sb);
1334
1335 if (seg == NO_SEG) {
1336 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1337 "segment name `%s' not recognized", value);
1338 } else {
1339 in_abs_seg = false;
1340 location.segment = seg;
1341 }
1342 break;
1343 }
1344
1345 case D_SECTALIGN: /* [SECTALIGN n] */
1346 {
1347 expr *e;
1348
1349 if (*value) {
1350 stdscan_reset();
1351 stdscan_set(value);
1352 tokval.t_type = TOKEN_INVALID;
1353 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
1354 if (e) {
1355 uint64_t align = e->value;
1356
1357 if (!is_power2(e->value)) {
1358 nasm_error(ERR_NONFATAL,
1359 "segment alignment `%s' is not power of two",
1360 value);
1361 } else if (align > UINT64_C(0x7fffffff)) {
1362 /*
1363 * FIXME: Please make some sane message here
1364 * ofmt should have some 'check' method which
1365 * would report segment alignment bounds.
1366 */
1367 nasm_error(ERR_NONFATAL,
1368 "absurdly large segment alignment `%s' (2^%d)",
1369 value, ilog2_64(align));
1370 }
1371
1372 /* callee should be able to handle all details */
1373 if (location.segment != NO_SEG)
1374 ofmt->sectalign(location.segment, align);
1375 }
1376 }
1377 break;
1378 }
1379
1380 case D_EXTERN: /* [EXTERN label:special] */
1381 if (*value == '$')
1382 value++; /* skip initial $ if present */
1383 if (pass0 == 2) {
1384 q = value;
1385 while (*q && *q != ':')
1386 q++;
1387 if (*q == ':') {
1388 *q++ = '\0';
1389 ofmt->symdef(value, 0L, 0L, 3, q);
1390 }
1391 } else if (passn == 1) {
1392 bool validid = true;
1393 q = value;
1394 if (!isidstart(*q))
1395 validid = false;
1396 while (*q && *q != ':') {
1397 if (!isidchar(*q))
1398 validid = false;
1399 q++;
1400 }
1401 if (!validid) {
1402 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
1403 break;
1404 }
1405 if (*q == ':') {
1406 *q++ = '\0';
1407 special = q;
1408 } else
1409 special = NULL;
1410 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1411 int temp = pass0;
1412 pass0 = 1; /* fake pass 1 in labels.c */
1413 declare_as_global(value, special);
1414 define_label(value, seg_alloc(), 0L, NULL,
1415 false, true);
1416 pass0 = temp;
1417 }
1418 } /* else pass0 == 1 */
1419 break;
1420
1421 case D_BITS: /* [BITS bits] */
1422 globalbits = get_bits(value);
1423 break;
1424
1425 case D_GLOBAL: /* [GLOBAL symbol:special] */
1426 if (*value == '$')
1427 value++; /* skip initial $ if present */
1428 if (pass0 == 2) { /* pass 2 */
1429 q = value;
1430 while (*q && *q != ':')
1431 q++;
1432 if (*q == ':') {
1433 *q++ = '\0';
1434 ofmt->symdef(value, 0L, 0L, 3, q);
1435 }
1436 } else if (pass2 == 1) { /* pass == 1 */
1437 bool validid = true;
1438
1439 q = value;
1440 if (!isidstart(*q))
1441 validid = false;
1442 while (*q && *q != ':') {
1443 if (!isidchar(*q))
1444 validid = false;
1445 q++;
1446 }
1447 if (!validid) {
1448 nasm_error(ERR_NONFATAL,
1449 "identifier expected after GLOBAL");
1450 break;
1451 }
1452 if (*q == ':') {
1453 *q++ = '\0';
1454 special = q;
1455 } else
1456 special = NULL;
1457 declare_as_global(value, special);
1458 } /* pass == 1 */
1459 break;
1460
1461 case D_COMMON: /* [COMMON symbol size:special] */
1462 {
1463 int64_t size;
1464 bool rn_error;
1465 bool validid;
1466
1467 if (*value == '$')
1468 value++; /* skip initial $ if present */
1469 p = value;
1470 validid = true;
1471 if (!isidstart(*p))
1472 validid = false;
1473 while (*p && !nasm_isspace(*p)) {
1474 if (!isidchar(*p))
1475 validid = false;
1476 p++;
1477 }
1478 if (!validid) {
1479 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
1480 break;
1481 }
1482 if (*p) {
1483 p = nasm_zap_spaces_fwd(p);
1484 q = p;
1485 while (*q && *q != ':')
1486 q++;
1487 if (*q == ':') {
1488 *q++ = '\0';
1489 special = q;
1490 } else {
1491 special = NULL;
1492 }
1493 size = readnum(p, &rn_error);
1494 if (rn_error) {
1495 nasm_error(ERR_NONFATAL,
1496 "invalid size specified"
1497 " in COMMON declaration");
1498 break;
1499 }
1500 } else {
1501 nasm_error(ERR_NONFATAL,
1502 "no size specified in"
1503 " COMMON declaration");
1504 break;
1505 }
1506
1507 if (pass0 < 2) {
1508 define_common(value, seg_alloc(), size, special);
1509 } else if (pass0 == 2) {
1510 if (special)
1511 ofmt->symdef(value, 0L, 0L, 3, special);
1512 }
1513 break;
1514 }
1515
1516 case D_ABSOLUTE: /* [ABSOLUTE address] */
1517 {
1518 expr *e;
1519
1520 stdscan_reset();
1521 stdscan_set(value);
1522 tokval.t_type = TOKEN_INVALID;
1523 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
1524 if (e) {
1525 if (!is_reloc(e))
1526 nasm_error(pass0 ==
1527 1 ? ERR_NONFATAL : ERR_PANIC,
1528 "cannot use non-relocatable expression as "
1529 "ABSOLUTE address");
1530 else {
1531 abs_seg = reloc_seg(e);
1532 abs_offset = reloc_value(e);
1533 }
1534 } else if (passn == 1)
1535 abs_offset = 0x100; /* don't go near zero in case of / */
1536 else
1537 nasm_panic(0, "invalid ABSOLUTE address "
1538 "in pass two");
1539 in_abs_seg = true;
1540 location.segment = NO_SEG;
1541 break;
1542 }
1543
1544 case D_DEBUG: /* [DEBUG] */
1545 {
1546 bool badid, overlong;
1547 char debugid[128];
1548
1549 p = value;
1550 q = debugid;
1551 badid = overlong = false;
1552 if (!isidstart(*p)) {
1553 badid = true;
1554 } else {
1555 while (*p && !nasm_isspace(*p)) {
1556 if (q >= debugid + sizeof debugid - 1) {
1557 overlong = true;
1558 break;
1559 }
1560 if (!isidchar(*p))
1561 badid = true;
1562 *q++ = *p++;
1563 }
1564 *q = 0;
1565 }
1566 if (badid) {
1567 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1568 "identifier expected after DEBUG");
1569 break;
1570 }
1571 if (overlong) {
1572 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1573 "DEBUG identifier too long");
1574 break;
1575 }
1576 p = nasm_skip_spaces(p);
1577 if (pass0 == 2)
1578 dfmt->debug_directive(debugid, p);
1579 break;
1580 }
1581
1582 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
1583 {
1584 enum warn_action { WID_OFF, WID_ON, WID_RESET };
1585 enum warn_action action;
1586 int i;
1587
1588 value = nasm_skip_spaces(value);
1589 switch(*value) {
1590 case '-': action = WID_OFF; value++; break;
1591 case '+': action = WID_ON; value++; break;
1592 case '*': action = WID_RESET; value++; break;
1593 default: action = WID_ON; break;
1594 }
1595
1596 for (i = 1; i <= ERR_WARN_MAX; i++)
1597 if (!nasm_stricmp(value, warnings[i].name))
1598 break;
1599 if (i <= ERR_WARN_MAX) {
1600 switch (action) {
1601 case WID_OFF:
1602 warning_on[i] = false;
1603 break;
1604 case WID_ON:
1605 warning_on[i] = true;
1606 break;
1607 case WID_RESET:
1608 warning_on[i] = warning_on_global[i];
1609 break;
1610 }
1611 }
1612 break;
1613 }
1614
1615 case D_CPU: /* [CPU] */
1616 cpu = get_cpu(value);
1617 break;
1618
1619 case D_LIST: /* [LIST {+|-}] */
1620 value = nasm_skip_spaces(value);
1621 if (*value == '+') {
1622 user_nolist = 0;
1623 } else {
1624 if (*value == '-') {
1625 user_nolist = 1;
1626 } else {
1627 bad_param = true;
1628 }
1629 }
1630 break;
1631
1632 case D_DEFAULT: /* [DEFAULT] */
1633 stdscan_reset();
1634 stdscan_set(value);
1635 tokval.t_type = TOKEN_INVALID;
1636 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
1637 switch (tokval.t_integer) {
1638 case S_REL:
1639 globalrel = 1;
1640 break;
1641 case S_ABS:
1642 globalrel = 0;
1643 break;
1644 case P_BND:
1645 globalbnd = 1;
1646 break;
1647 case P_NOBND:
1648 globalbnd = 0;
1649 break;
1650 default:
1651 bad_param = true;
1652 break;
1653 }
1654 } else {
1655 bad_param = true;
1656 }
1657 break;
1658
1659 case D_FLOAT:
1660 if (float_option(value)) {
1661 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1662 "unknown 'float' directive: %s", value);
1663 }
1664 break;
1665
1666 case D_PRAGMA:
1667 process_pragma(value);
1668 break;
1669 }
1670
1671
1672 /* A common error message */
1673 if (bad_param) {
1674 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
1675 directive);
1676 }
1677
1678 return d;
1679}
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001680
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001681static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001682{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001683 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001685 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001686 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001687 int pass_max;
Martin Lindhe8cc93f52016-11-16 16:48:13 +01001688 int sb;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001689
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001690 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001691 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001692
Charles Craynec1905c22008-09-11 18:54:06 -07001693 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001694 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001697 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001698 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1699 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001700
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001701 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001702
Keith Kaniosb7a89542007-04-12 02:40:54 +00001703 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 cpu = cmd_cpu;
1705 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001706 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001708 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001709 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001711 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001712 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 saa_rewind(forwrefs);
1714 forwref = saa_rstruct(forwrefs);
1715 raa_free(offsets);
1716 offsets = raa_init();
1717 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001718 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001719 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001720
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001722 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001723 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001724 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001725
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 while ((line = preproc->getline())) {
1727 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001728
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001729 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001730 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001731 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001732 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001733 if (process_directives(line) != D_none)
1734 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001735
H. Peter Anvinc7131682017-03-07 17:45:01 -08001736 /* Not a directive, or even something that starts with [ */
1737
1738 parse_line(pass1, line, &output_ins, def_label);
1739
1740 if (optimizing > 0) {
1741 if (forwref != NULL && globallineno == forwref->lineno) {
1742 output_ins.forw_ref = true;
1743 do {
1744 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1745 forwref = saa_rstruct(forwrefs);
1746 } while (forwref != NULL
1747 && forwref->lineno == globallineno);
1748 } else
1749 output_ins.forw_ref = false;
1750
1751 if (output_ins.forw_ref) {
1752 if (passn == 1) {
1753 for (i = 0; i < output_ins.operands; i++) {
1754 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1755 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1756 fwinf->lineno = globallineno;
1757 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001758 }
1759 }
1760 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001761 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001762 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001763
H. Peter Anvinc7131682017-03-07 17:45:01 -08001764 /* forw_ref */
1765 if (output_ins.opcode == I_EQU) {
1766 if (pass1 == 1) {
1767 /*
1768 * Special `..' EQUs get processed in pass two,
1769 * except `..@' macro-processor EQUs which are done
1770 * in the normal place.
1771 */
1772 if (!output_ins.label)
1773 nasm_error(ERR_NONFATAL,
1774 "EQU not preceded by label");
1775
1776 else if (output_ins.label[0] != '.' ||
1777 output_ins.label[1] != '.' ||
1778 output_ins.label[2] == '@') {
1779 if (output_ins.operands == 1 &&
1780 (output_ins.oprs[0].type & IMMEDIATE) &&
1781 output_ins.oprs[0].wrt == NO_SEG) {
1782 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1783 def_label(output_ins.label,
1784 output_ins.oprs[0].segment,
1785 output_ins.oprs[0].offset, NULL,
1786 false, isext);
1787 } else if (output_ins.operands == 2
1788 && (output_ins.oprs[0].type & IMMEDIATE)
1789 && (output_ins.oprs[0].type & COLON)
1790 && output_ins.oprs[0].segment == NO_SEG
1791 && output_ins.oprs[0].wrt == NO_SEG
1792 && (output_ins.oprs[1].type & IMMEDIATE)
1793 && output_ins.oprs[1].segment == NO_SEG
1794 && output_ins.oprs[1].wrt == NO_SEG) {
1795 def_label(output_ins.label,
1796 output_ins.oprs[0].offset | SEG_ABS,
1797 output_ins.oprs[1].offset,
1798 NULL, false, false);
1799 } else
1800 nasm_error(ERR_NONFATAL,
1801 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001802 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001803 } else {
1804 /*
1805 * Special `..' EQUs get processed here, except
1806 * `..@' macro processor EQUs which are done above.
1807 */
1808 if (output_ins.label[0] == '.' &&
1809 output_ins.label[1] == '.' &&
1810 output_ins.label[2] != '@') {
1811 if (output_ins.operands == 1 &&
1812 (output_ins.oprs[0].type & IMMEDIATE)) {
1813 define_label(output_ins.label,
1814 output_ins.oprs[0].segment,
1815 output_ins.oprs[0].offset,
1816 NULL, false, false);
1817 } else if (output_ins.operands == 2
1818 && (output_ins.oprs[0].type & IMMEDIATE)
1819 && (output_ins.oprs[0].type & COLON)
1820 && output_ins.oprs[0].segment == NO_SEG
1821 && (output_ins.oprs[1].type & IMMEDIATE)
1822 && output_ins.oprs[1].segment == NO_SEG) {
1823 define_label(output_ins.label,
1824 output_ins.oprs[0].offset | SEG_ABS,
1825 output_ins.oprs[1].offset,
1826 NULL, false, false);
1827 } else
1828 nasm_error(ERR_NONFATAL,
1829 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001830 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001831 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001832 } else { /* instruction isn't an EQU */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833
H. Peter Anvinc7131682017-03-07 17:45:01 -08001834 if (pass1 == 1) {
1835
1836 int64_t l = insn_size(location.segment, offs, sb, cpu,
1837 &output_ins);
1838 l *= output_ins.times;
1839
1840 /* if (using_debug_info) && output_ins.opcode != -1) */
1841 if (using_debug_info)
1842 { /* fbk 03/25/01 */
1843 /* this is done here so we can do debug type info */
1844 int32_t typeinfo =
1845 TYS_ELEMENTS(output_ins.operands);
1846 switch (output_ins.opcode) {
1847 case I_RESB:
1848 typeinfo =
1849 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001850 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001851 case I_RESW:
1852 typeinfo =
1853 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001854 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001855 case I_RESD:
1856 typeinfo =
1857 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001858 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001859 case I_RESQ:
1860 typeinfo =
1861 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001862 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001863 case I_REST:
1864 typeinfo =
1865 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001866 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001867 case I_RESO:
1868 typeinfo =
1869 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001870 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001871 case I_RESY:
1872 typeinfo =
1873 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001874 break;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001875 case I_DB:
1876 typeinfo |= TY_BYTE;
1877 break;
1878 case I_DW:
1879 typeinfo |= TY_WORD;
1880 break;
1881 case I_DD:
1882 if (output_ins.eops_float)
1883 typeinfo |= TY_FLOAT;
1884 else
1885 typeinfo |= TY_DWORD;
1886 break;
1887 case I_DQ:
1888 typeinfo |= TY_QWORD;
1889 break;
1890 case I_DT:
1891 typeinfo |= TY_TBYTE;
1892 break;
1893 case I_DO:
1894 typeinfo |= TY_OWORD;
1895 break;
1896 case I_DY:
1897 typeinfo |= TY_YWORD;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001898 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001899 default:
H. Peter Anvinc7131682017-03-07 17:45:01 -08001900 typeinfo = TY_LABEL;
1901
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001902 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001903
1904 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001905 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001906 if (l != -1) {
1907 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001908 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001910 /*
1911 * else l == -1 => invalid instruction, which will be
1912 * flagged as an error on pass 2
1913 */
1914
1915 } else {
1916 offs += assemble(location.segment, offs, sb, cpu,
1917 &output_ins);
1918 set_curr_offs(offs);
1919
1920 }
1921 } /* not an EQU */
1922 cleanup_insn(&output_ins);
1923
1924 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001926 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001928
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001929 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001930 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001931 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001932
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 if (pass1 == 1)
1934 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001935
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001936 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001938 } else if (global_offset_changed &&
1939 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001940 prev_offset_changed = global_offset_changed;
1941 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001942 } else {
1943 stall_count++;
1944 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001945
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001946 if (terminate_after_phase)
1947 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001948
1949 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001950 /* We get here if the labels don't converge
1951 * Example: FOO equ FOO + 1
1952 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001953 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001954 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001955 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001956 nasm_error(ERR_NONFATAL,
1957 "Possible causes: recursive EQUs, macro abuse.");
1958 break;
1959 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001960 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001961
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001963 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001964 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001965 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001966 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1967 }
1968}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001969
Ed Berosetfa771012002-06-09 20:56:40 +00001970/**
1971 * gnu style error reporting
1972 * This function prints an error message to error_file in the
1973 * style used by GNU. An example would be:
1974 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001975 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001976 * which the error occurs (or is detected) and "error:" is one of
1977 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001978 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001979 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001980 *
1981 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001982 * @param fmt the printf style format string
1983 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001984static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001985{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001986 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001987 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001988
Ed Berosetfa771012002-06-09 20:56:40 +00001989 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001991
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001992 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001993 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001994
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001995 if (!skip_this_pass(severity)) {
1996 if (currentfile) {
1997 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001998 } else {
1999 fputs("nasm: ", error_file);
2000 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002001 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07002002
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002003 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00002004}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002005
Ed Berosetfa771012002-06-09 20:56:40 +00002006/**
2007 * MS style error reporting
2008 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07002009 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00002010 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00002011 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07002012 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00002013 * which the error occurs (or is detected) and "error:" is one of
2014 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07002015 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00002016 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07002017 *
2018 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002019 * @param fmt the printf style format string
2020 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002021static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00002022{
H. Peter Anvin274cda82016-05-10 02:56:29 -07002023 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07002024 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00002025
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 if (is_suppressed_warning(severity))
2027 return;
Ed Berosetfa771012002-06-09 20:56:40 +00002028
H. Peter Anvin48ef4192009-07-01 22:12:59 -07002029 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002030 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07002031
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002032 if (!skip_this_pass(severity)) {
2033 if (currentfile) {
2034 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002035 } else {
2036 fputs("nasm: ", error_file);
2037 }
Ed Berosetfa771012002-06-09 20:56:40 +00002038 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07002039
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002040 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00002041}
2042
2043/**
2044 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07002045 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00002046 * not in pass 1
2047 *
2048 * @param severity the severity of the warning or error
2049 * @return true if we should abort error/warning printing
2050 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08002051static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00002052{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04002053 /* Not a warning at all */
2054 if ((severity & ERR_MASK) != ERR_WARNING)
2055 return false;
2056
H. Peter Anvin442a05a2012-02-24 21:50:53 -08002057 /* Might be a warning but suppresed explicitly */
2058 if (severity & ERR_WARN_MASK)
2059 return !warning_on[WARN_IDX(severity)];
2060 else
2061 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00002062}
2063
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002064static bool skip_this_pass(int severity)
2065{
H. Peter Anvin934f0472016-05-09 12:00:19 -07002066 /* See if it's a pass-specific warning which should be skipped. */
2067
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002068 if ((severity & ERR_MASK) > ERR_WARNING)
2069 return false;
2070
H. Peter Anvin934f0472016-05-09 12:00:19 -07002071 /*
2072 * passn is 1 on the very first pass only.
2073 * pass0 is 2 on the code-generation (final) pass only.
2074 * These are the passes we care about in this case.
2075 */
2076 return (((severity & ERR_PASS1) && passn != 1) ||
2077 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002078}
2079
Ed Berosetfa771012002-06-09 20:56:40 +00002080/**
2081 * common error reporting
2082 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07002083 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00002084 * specific error message to error_file and may or may not return. It
2085 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07002086 *
2087 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00002088 * @param fmt the printf style format string
2089 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002090static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00002091{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002092 char msg[1024];
2093 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002094
H. Peter Anvin7df04172008-06-10 18:27:38 -07002095 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002097 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 break;
2099 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002100 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 break;
2102 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002103 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 break;
2105 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002106 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 break;
2108 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002109 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002110 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002111 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002112 pfx = "";
2113 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002114 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002115
H. Peter Anvin934f0472016-05-09 12:00:19 -07002116 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002117 if ((severity & (ERR_WARN_MASK|ERR_PP_LISTMACRO)) == ERR_WARN_MASK) {
H. Peter Anvin934f0472016-05-09 12:00:19 -07002118 char *p = strchr(msg, '\0');
2119 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
2120 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002121
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002122 if (!skip_this_pass(severity))
2123 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002124
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002125 /* Are we recursing from error_list_macros? */
2126 if (severity & ERR_PP_LISTMACRO)
2127 return;
2128
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002129 /*
2130 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002131 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002132 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07002133 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002134
2135 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002136 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002137
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002138 preproc->error_list_macros(severity);
2139
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002140 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 case ERR_DEBUG:
2142 /* no further action, by definition */
2143 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002144 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002145 /* Treat warnings as errors */
2146 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2147 terminate_after_phase = true;
2148 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002150 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002151 break;
2152 case ERR_FATAL:
2153 if (ofile) {
2154 fclose(ofile);
2155 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002156 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 }
2158 if (want_usage)
2159 usage();
2160 exit(1); /* instantly die */
2161 break; /* placate silly compilers */
2162 case ERR_PANIC:
2163 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002164 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002165 if (ofile) {
2166 fclose(ofile);
2167 remove(outname);
2168 ofile = NULL;
2169 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002170 exit(3);
2171 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002172 }
2173}
2174
H. Peter Anvin734b1882002-04-30 21:01:08 +00002175static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002176{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002177 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002178}
2179
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002180static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002181{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002182 iflag_t r;
2183
2184 iflag_clear_all(&r);
2185
H. Peter Anvine2c80182005-01-15 22:15:51 +00002186 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002187 iflag_set(&r, IF_8086);
2188 else if (!strcmp(value, "186"))
2189 iflag_set(&r, IF_186);
2190 else if (!strcmp(value, "286"))
2191 iflag_set(&r, IF_286);
2192 else if (!strcmp(value, "386"))
2193 iflag_set(&r, IF_386);
2194 else if (!strcmp(value, "486"))
2195 iflag_set(&r, IF_486);
2196 else if (!strcmp(value, "586") ||
2197 !nasm_stricmp(value, "pentium"))
2198 iflag_set(&r, IF_PENT);
2199 else if (!strcmp(value, "686") ||
2200 !nasm_stricmp(value, "ppro") ||
2201 !nasm_stricmp(value, "pentiumpro") ||
2202 !nasm_stricmp(value, "p2"))
2203 iflag_set(&r, IF_P6);
2204 else if (!nasm_stricmp(value, "p3") ||
2205 !nasm_stricmp(value, "katmai"))
2206 iflag_set(&r, IF_KATMAI);
2207 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2208 !nasm_stricmp(value, "willamette"))
2209 iflag_set(&r, IF_WILLAMETTE);
2210 else if (!nasm_stricmp(value, "prescott"))
2211 iflag_set(&r, IF_PRESCOTT);
2212 else if (!nasm_stricmp(value, "x64") ||
2213 !nasm_stricmp(value, "x86-64"))
2214 iflag_set(&r, IF_X86_64);
2215 else if (!nasm_stricmp(value, "ia64") ||
2216 !nasm_stricmp(value, "ia-64") ||
2217 !nasm_stricmp(value, "itanium")||
2218 !nasm_stricmp(value, "itanic") ||
2219 !nasm_stricmp(value, "merced"))
2220 iflag_set(&r, IF_IA64);
2221 else {
2222 iflag_set(&r, IF_PLEVEL);
2223 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2224 "unknown 'cpu' type");
2225 }
2226 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002227}
2228
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002229static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002230{
2231 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002232
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 if ((i = atoi(value)) == 16)
2234 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002235 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002237 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 "cannot specify 32-bit segment on processor below a 386");
2239 i = 16;
2240 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002241 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002242 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002243 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002244 "cannot specify 64-bit segment on processor below an x86-64");
2245 i = 16;
2246 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002247 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002248 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002249 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250 value);
2251 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002252 }
2253 return i;
2254}