blob: 0ce0ba2a6d98f5d8acd54ffd3704b35c4cea8d6e [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Cyrill Gorcunovf187eb72013-02-15 12:25:33 +04003 * Copyright 1996-2013 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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070046#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080047#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49#include "nasm.h"
50#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvin31b707b2009-06-27 22:07:33 -070061#include "output/outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.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);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvin2b046cf2008-01-22 14:08:36 -080084static bool is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085static void usage(void);
86
John Coffman0efaec92002-05-26 19:20:08 +000087static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070088bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080089int pass0, passn;
Keith Kaniosb7a89542007-04-12 02:40:54 +000090int maxbits = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000091int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080092int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000093
H. Peter Anvin9bd15062009-07-18 21:07:17 -040094static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080095
Keith Kaniosa6dfa782007-04-13 16:47:53 +000096static char inname[FILENAME_MAX];
97static char outname[FILENAME_MAX];
98static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -070099static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000100static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000101/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400102struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400103struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400104const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000107
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400108FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700109int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
110static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400111
112static iflag_t cpu;
113static iflag_t cmd_cpu;
114
Charles Craynec1905c22008-09-11 18:54:06 -0700115int64_t global_offset_changed; /* referenced in labels.c */
116int64_t prev_offset_changed;
117int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000118
H. Peter Anvin12e46512007-10-03 21:30:57 -0700119static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000121int32_t abs_seg; /* ABSOLUTE segment basis */
122int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000123
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000125
H. Peter Anvine2c80182005-01-15 22:15:51 +0000126static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700127static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400129static struct preproc_ops *preproc;
130
H. Peter Anvin620515a2002-04-30 20:57:38 +0000131enum op_type {
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400132 OP_NORMAL, /* Preprocess and assemble */
133 OP_PREPROCESS, /* Preprocess only */
134 OP_DEPEND, /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000135};
136static enum op_type operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400137
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700138/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700139static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700140static bool depend_missing_ok = false;
141static const char *depend_target = NULL;
142static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000145 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800146 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000147 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200148
H. Peter Anvin972079f2008-09-30 17:01:23 -0700149static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
150static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000151
H. Peter Anvin972079f2008-09-30 17:01:23 -0700152static const struct warning {
153 const char *name;
154 const char *help;
155 bool enabled;
156} warnings[ERR_WARN_MAX+1] = {
157 {"error", "treat warnings as errors", false},
158 {"macro-params", "macro calls with wrong parameter count", true},
159 {"macro-selfref", "cyclic macro references", false},
160 {"macro-defaults", "macros with more default than optional parameters", true},
161 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700162 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700163 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
164 {"float-overflow", "floating point overflow", true},
165 {"float-denorm", "floating point denormal", false},
166 {"float-underflow", "floating point underflow", false},
167 {"float-toolong", "too many digits in floating-point number", true},
168 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800169 {"lock", "lock prefix on unlockable instructions", true},
170 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800171 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000172};
173
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700174static bool want_usage;
175static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000176int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
H. Peter Anvin55340992012-09-09 17:09:00 -0700178static char *quote_for_make(const char *str);
179
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400180static int64_t get_curr_offs(void)
181{
182 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
183}
184
185static void set_curr_offs(int64_t l_off)
186{
187 if (in_abs_seg)
188 abs_offset = l_off;
189 else
190 offsets = raa_write(offsets, location.segment, l_off);
191}
192
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000193static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000195 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000196 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800197 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000200}
201
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800202/* Convert a struct tm to a POSIX-style time constant */
203static int64_t posix_mktime(struct tm *tm)
204{
205 int64_t t;
206 int64_t y = tm->tm_year;
207
208 /* See IEEE 1003.1:2004, section 4.14 */
209
210 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
211 t += tm->tm_yday;
212 t *= 24;
213 t += tm->tm_hour;
214 t *= 60;
215 t += tm->tm_min;
216 t *= 60;
217 t += tm->tm_sec;
218
219 return t;
220}
221
222static void define_macros_early(void)
223{
224 char temp[128];
225 struct tm lt, *lt_p, gm, *gm_p;
226 int64_t posix_time;
227
228 lt_p = localtime(&official_compile_time);
229 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400230 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800231
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400232 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
233 preproc->pre_define(temp);
234 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
235 preproc->pre_define(temp);
236 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
237 preproc->pre_define(temp);
238 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
239 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800240 }
241
242 gm_p = gmtime(&official_compile_time);
243 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400244 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800245
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400246 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
247 preproc->pre_define(temp);
248 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
249 preproc->pre_define(temp);
250 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
251 preproc->pre_define(temp);
252 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
253 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800254 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700255
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800256 if (gm_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400257 posix_time = posix_mktime(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258 else if (lt_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400259 posix_time = posix_mktime(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400261 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262
263 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
265 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800266 }
267}
268
269static void define_macros_late(void)
270{
271 char temp[128];
272
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400273 /*
274 * In case if output format is defined by alias
275 * we have to put shortname of the alias itself here
276 * otherwise ABI backward compatibility gets broken.
277 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400278 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400279 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400280 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800281}
282
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700283static void emit_dependencies(StrList *list)
284{
285 FILE *deps;
286 int linepos, len;
287 StrList *l, *nl;
288
289 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400290 deps = fopen(depend_file, "w");
291 if (!deps) {
292 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
293 "unable to write dependency file `%s'", depend_file);
294 return;
295 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700296 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400297 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700298 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700299
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700300 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400301 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700302 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400303 len = strlen(file);
304 if (linepos + len > 62 && linepos > 1) {
305 fprintf(deps, " \\\n ");
306 linepos = 1;
307 }
308 fprintf(deps, " %s", file);
309 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700310 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700311 }
312 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700313
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400314 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400315 if (depend_emit_phony)
316 fprintf(deps, "%s:\n\n", l->str);
317 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700318 }
319
320 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400321 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700322}
323
H. Peter Anvin038d8612007-04-12 16:54:50 +0000324int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000325{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700327
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800328 time(&official_compile_time);
329
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400330 iflag_set(&cpu, IF_PLEVEL);
331 iflag_set(&cmd_cpu, IF_PLEVEL);
332
Charles Crayne2581c862008-09-10 19:21:52 -0700333 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700334 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400335 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000336
H. Peter Anvin97e15752007-09-25 08:47:47 -0700337 error_file = stderr;
338
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700339 tolower_init();
340
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000341 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000342 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000343
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400345 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000346
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000347 seg_init();
348
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800349 /* Define some macros dependent on the runtime, but not
350 on the command line. */
351 define_macros_early();
352
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353 parse_cmdline(argc, argv);
354
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 if (terminate_after_phase) {
356 if (want_usage)
357 usage();
358 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359 }
360
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000361 /* If debugging info is disabled, suppress any debug calls */
362 if (!using_debug_info)
363 ofmt->current_dfmt = &null_debug_form;
364
H. Peter Anvin76690a12002-04-30 20:52:49 +0000365 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400366 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700367 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000370 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800371 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000372
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400373 depend_ptr = (depend_file || (operating_mode == OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700374 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400375 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700376
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377 switch (operating_mode) {
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400378 case OP_DEPEND:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000380 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700381
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400382 if (depend_missing_ok)
383 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700384
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700385 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400387 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 while ((line = preproc->getline()))
390 nasm_free(line);
391 preproc->cleanup(0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 }
393 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000394
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400395 case OP_PREPROCESS:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000397 char *line;
398 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000399 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000401
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 if (*outname) {
403 ofile = fopen(outname, "w");
404 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400405 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 "unable to open output file `%s'",
407 outname);
408 } else
409 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000410
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700411 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000412
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400413 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700414 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400415 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700416
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 while ((line = preproc->getline())) {
418 /*
419 * We generate %line directives if needed for later programs
420 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000421 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 int altline = src_get(&linnum, &file_name);
423 if (altline) {
424 if (altline == 1 && lineinc == 1)
425 nasm_fputs("", ofile);
426 else {
427 lineinc = (altline != -1 || lineinc != 1);
428 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000429 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 file_name);
431 }
432 prior_linnum = linnum;
433 }
434 nasm_fputs(line, ofile);
435 nasm_free(line);
436 }
437 nasm_free(file_name);
438 preproc->cleanup(0);
439 if (ofile)
440 fclose(ofile);
441 if (ofile && terminate_after_phase)
442 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400443 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 }
445 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000446
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400447 case OP_NORMAL:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 {
449 /*
450 * We must call ofmt->filename _anyway_, even if the user
451 * has specified their own output file, because some
452 * formats (eg OBJ and COFF) use ofmt->filename to find out
453 * the name of the input file and then put that inside the
454 * file.
455 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400456 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457
H. Peter Anvin0cba1072009-07-05 14:45:12 -0700458 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 if (!ofile) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400460 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 "unable to open output file `%s'", outname);
462 }
463
464 /*
465 * We must call init_labels() before ofmt->init() since
466 * some object formats will want to define labels in their
467 * init routines. (eg OS/2 defines the FLAT group)
468 */
469 init_labels();
470
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400471 ofmt->init();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400472 dfmt = ofmt->current_dfmt;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400473 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700475 assemble_file(inname, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476
477 if (!terminate_after_phase) {
478 ofmt->cleanup(using_debug_info);
479 cleanup_labels();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400480 fflush(ofile);
481 if (ferror(ofile)) {
482 nasm_error(ERR_NONFATAL|ERR_NOFILE,
483 "write error on output file `%s'", outname);
484 }
485 }
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200486
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400487 if (ofile) {
488 fclose(ofile);
489 if (terminate_after_phase)
490 remove(outname);
491 ofile = NULL;
492 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 }
494 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000496
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700497 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400498 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700499
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000502
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 raa_free(offsets);
504 saa_free(forwrefs);
505 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000506 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000507
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700508 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000509}
510
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511/*
512 * Get a parameter for a command line option.
513 * First arg must be in the form of e.g. -f...
514 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800515static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000516{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800517 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400518 if (p[2]) /* the parameter's in the option */
519 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800521 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400524 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526 return NULL;
527}
528
H. Peter Anvindc242712007-11-18 11:55:10 -0800529/*
530 * Copy a filename
531 */
532static void copy_filename(char *dst, const char *src)
533{
534 size_t len = strlen(src);
535
536 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400537 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
538 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800539 }
540 strncpy(dst, src, FILENAME_MAX);
541}
542
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700543/*
544 * Convert a string to Make-safe form
545 */
546static char *quote_for_make(const char *str)
547{
548 const char *p;
549 char *os, *q;
550
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400551 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700552 size_t nbs = 0;
553
554 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400555 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700556
557 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400558 switch (*p) {
559 case ' ':
560 case '\t':
561 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
562 n += nbs + 2;
563 nbs = 0;
564 break;
565 case '$':
566 case '#':
567 nbs = 0;
568 n += 2;
569 break;
570 case '\\':
571 nbs++;
572 n++;
573 break;
574 default:
575 nbs = 0;
576 n++;
577 break;
578 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700579 }
580
581 /* Convert N backslashes at the end of filename to 2N backslashes */
582 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400583 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700584
585 os = q = nasm_malloc(n);
586
587 nbs = 0;
588 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400589 switch (*p) {
590 case ' ':
591 case '\t':
592 while (nbs--)
593 *q++ = '\\';
594 *q++ = '\\';
595 *q++ = *p;
596 break;
597 case '$':
598 *q++ = *p;
599 *q++ = *p;
600 nbs = 0;
601 break;
602 case '#':
603 *q++ = '\\';
604 *q++ = *p;
605 nbs = 0;
606 break;
607 case '\\':
608 *q++ = *p;
609 nbs++;
610 break;
611 default:
612 *q++ = *p;
613 nbs = 0;
614 break;
615 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700616 }
617 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400618 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700619
620 *q = '\0';
621
622 return os;
623}
624
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000626 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000628};
629
630#define OPT_PREFIX 0
631#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632struct textargs textopts[] = {
633 {"prefix", OPT_PREFIX},
634 {"postfix", OPT_POSTFIX},
635 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000636};
637
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400638static void show_version(void)
639{
640 printf("NASM version %s compiled on %s%s\n",
641 nasm_version, nasm_date, nasm_compile_options);
642 exit(0);
643}
644
H. Peter Anvin423e3812007-11-15 17:12:29 -0800645static bool stopoptions = false;
646static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000648 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800649 int i;
650 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700651 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000652
653 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800654 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000655
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400657 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
658 /* These parameters take values */
659 if (!(param = get_param(p, q, &advance)))
660 return advance;
661 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800662
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 switch (p[1]) {
664 case 's':
665 error_file = stdout;
666 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700667
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400668 case 'o': /* output file */
669 copy_filename(outname, param);
670 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700671
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400672 case 'f': /* output format */
673 ofmt = ofmt_find(param, &ofmt_alias);
674 if (!ofmt) {
675 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
676 "unrecognised output format `%s' - "
677 "use -hf for a list", param);
678 }
679 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700680
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400681 case 'O': /* Optimization level */
682 {
683 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700684
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400685 if (!*param) {
686 /* Naked -O == -Ox */
687 optimizing = MAX_OPTIMIZE;
688 } else {
689 while (*param) {
690 switch (*param) {
691 case '0': case '1': case '2': case '3': case '4':
692 case '5': case '6': case '7': case '8': case '9':
693 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700694
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400695 /* -O0 -> optimizing == -1, 0.98 behaviour */
696 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
697 if (opt < 2)
698 optimizing = opt - 1;
699 else
700 optimizing = opt;
701 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700702
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400703 case 'v':
704 case '+':
705 param++;
706 opt_verbose_info = true;
707 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700708
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400709 case 'x':
710 param++;
711 optimizing = MAX_OPTIMIZE;
712 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700713
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400714 default:
715 nasm_error(ERR_FATAL,
716 "unknown optimization option -O%c\n",
717 *param);
718 break;
719 }
720 }
721 if (optimizing > MAX_OPTIMIZE)
722 optimizing = MAX_OPTIMIZE;
723 }
724 break;
725 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800726
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400727 case 'p': /* pre-include */
728 case 'P':
729 preproc->pre_include(param);
730 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800731
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400732 case 'd': /* pre-define */
733 case 'D':
734 preproc->pre_define(param);
735 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800736
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400737 case 'u': /* un-define */
738 case 'U':
739 preproc->pre_undefine(param);
740 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800741
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400742 case 'i': /* include search path */
743 case 'I':
744 preproc->include_path(param);
745 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800746
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400747 case 'l': /* listing file */
748 copy_filename(listname, param);
749 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800750
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 case 'Z': /* error messages file */
752 copy_filename(errname, param);
753 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800754
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400755 case 'F': /* specify debug format */
756 ofmt->current_dfmt = dfmt_find(ofmt, param);
757 if (!ofmt->current_dfmt) {
758 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
759 "unrecognized debug format `%s' for"
760 " output format `%s'",
761 param, ofmt->shortname);
762 }
763 using_debug_info = true;
764 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800765
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400766 case 'X': /* specify error reporting format */
767 if (nasm_stricmp("vc", param) == 0)
768 nasm_set_verror(nasm_verror_vc);
769 else if (nasm_stricmp("gnu", param) == 0)
770 nasm_set_verror(nasm_verror_gnu);
771 else
772 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
773 "unrecognized error reporting format `%s'",
774 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800776
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700778 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800780
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 case 'h':
782 printf
783 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
784 "[-l listfile]\n"
785 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400786 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400788 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700790 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700792 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400793 " -MG d:o, missing files assumed generated\n"
794 " -MF <file> set Makefile dependency file\n"
795 " -MD <file> assemble and generate dependencies\n"
796 " -MT <file> dependency target name\n"
797 " -MQ <file> dependency target name (quoted)\n"
798 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700799 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 " -s redirect error messages to stdout\n\n"
801 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400802 " -o outfile write output to an outfile\n\n"
803 " -f format select an output format\n\n"
804 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 " -I<path> adds a pathname to the include file path\n");
806 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700807 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400808 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400809 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400810 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 " -P<file> pre-includes a file\n"
812 " -D<macro>[=<value>] pre-defines a macro\n"
813 " -U<macro> undefines a macro\n"
814 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800815 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400816 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400817 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400818 "--prefix,--postfix\n"
819 " this options prepend or append the given argument to all\n"
820 " extern and global variables\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800821 "Warnings:\n");
822 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700824 warnings[i].name, warnings[i].help,
825 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 printf
827 ("\nresponse files should contain command line parameters"
828 ", one per line.\n");
829 if (p[2] == 'f') {
830 printf("\nvalid output formats for -f are"
831 " (`*' denotes default):\n");
832 ofmt_list(ofmt, stdout);
833 } else {
834 printf("\nFor a list of valid output formats, use -hf.\n");
835 printf("For a list of debug formats, use -f <form> -y.\n");
836 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400837 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800839
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 case 'y':
841 printf("\nvalid debug formats for '%s' output format are"
842 " ('*' denotes default):\n", ofmt->shortname);
843 dfmt_list(ofmt, stdout);
844 exit(0);
845 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800846
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700848 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800850
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400852 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800854
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400855 case 'e': /* preprocess only */
856 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400857 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800859
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400860 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400861 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800863
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 case 'W':
865 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
866 do_warn = false;
867 param += 3;
868 } else {
869 do_warn = true;
870 }
871 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800872
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800874 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400875 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400877 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400879 do_warn = (param[0] == '+');
880 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400881
882set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400883 for (i = 0; i <= ERR_WARN_MAX; i++) {
884 if (!nasm_stricmp(param, warnings[i].name))
885 break;
886 }
887 if (i <= ERR_WARN_MAX) {
888 warning_on_global[i] = do_warn;
889 } else if (!nasm_stricmp(param, "all")) {
890 for (i = 1; i <= ERR_WARN_MAX; i++)
891 warning_on_global[i] = do_warn;
892 } else if (!nasm_stricmp(param, "none")) {
893 for (i = 1; i <= ERR_WARN_MAX; i++)
894 warning_on_global[i] = !do_warn;
895 } else {
896 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
897 "invalid warning `%s'", param);
898 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800900
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 switch (p[2]) {
903 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400904 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400905 break;
906 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400907 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400908 depend_missing_ok = true;
909 break;
910 case 'P':
911 depend_emit_phony = true;
912 break;
913 case 'D':
914 depend_file = q;
915 advance = true;
916 break;
917 case 'T':
918 depend_target = q;
919 advance = true;
920 break;
921 case 'Q':
922 depend_target = quote_for_make(q);
923 advance = true;
924 break;
925 default:
926 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
927 "unknown dependency option `-M%c'", p[2]);
928 break;
929 }
930 if (advance && (!q || !q[0])) {
931 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
932 "option `-M%c' requires a parameter", p[2]);
933 break;
934 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000936
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 case '-':
938 {
939 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 if (p[2] == 0) { /* -- => stop processing options */
942 stopoptions = 1;
943 break;
944 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400945
946 if (!nasm_stricmp(p, "--v"))
947 show_version();
948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 for (s = 0; textopts[s].label; s++) {
950 if (!nasm_stricmp(p + 2, textopts[s].label)) {
951 break;
952 }
953 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000954
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 switch (s) {
956
957 case OPT_PREFIX:
958 case OPT_POSTFIX:
959 {
960 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400961 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 ERR_USAGE,
963 "option `--%s' requires an argument",
964 p + 2);
965 break;
966 } else {
967 advance = 1, param = q;
968 }
969
970 if (s == OPT_PREFIX) {
971 strncpy(lprefix, param, PREFIX_MAX - 1);
972 lprefix[PREFIX_MAX - 1] = 0;
973 break;
974 }
975 if (s == OPT_POSTFIX) {
976 strncpy(lpostfix, param, POSTFIX_MAX - 1);
977 lpostfix[POSTFIX_MAX - 1] = 0;
978 break;
979 }
980 break;
981 }
982 default:
983 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400984 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 "unrecognised option `--%s'", p + 2);
986 break;
987 }
988 }
989 break;
990 }
991
992 default:
993 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400994 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 "unrecognised option `-%c'", p[1]);
996 break;
997 }
998 } else {
999 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001000 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001002 } else {
1003 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001004 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001005 }
1006
1007 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001008}
1009
H. Peter Anvineba20a72002-04-30 20:53:55 +00001010#define ARG_BUF_DELTA 128
1011
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001013{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001014 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001015 int bufsize, prevargsize;
1016
1017 bufsize = prevargsize = ARG_BUF_DELTA;
1018 buffer = nasm_malloc(ARG_BUF_DELTA);
1019 prevarg = nasm_malloc(ARG_BUF_DELTA);
1020 prevarg[0] = '\0';
1021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 p = buffer;
1024 while (1) { /* Loop to handle long lines */
1025 q = fgets(p, bufsize - (p - buffer), rfile);
1026 if (!q)
1027 break;
1028 p += strlen(p);
1029 if (p > buffer && p[-1] == '\n')
1030 break;
1031 if (p - buffer > bufsize - 10) {
1032 int offset;
1033 offset = p - buffer;
1034 bufsize += ARG_BUF_DELTA;
1035 buffer = nasm_realloc(buffer, bufsize);
1036 p = buffer + offset;
1037 }
1038 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 if (!q && p == buffer) {
1041 if (prevarg[0])
1042 process_arg(prevarg, NULL);
1043 nasm_free(buffer);
1044 nasm_free(prevarg);
1045 return;
1046 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001047
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 /*
1049 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1050 * them are present at the end of the line.
1051 */
1052 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001053
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001054 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001057 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 if (process_arg(prevarg, p))
1060 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061
Charles Crayne192d5b52007-10-18 19:02:42 -07001062 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 prevargsize += ARG_BUF_DELTA;
1064 prevarg = nasm_realloc(prevarg, prevargsize);
1065 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001066 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001067 }
1068}
1069
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001070/* Function to process args from a string of args, rather than the
1071 * argv array. Used by the environment variable and response file
1072 * processing.
1073 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001074static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001076 char *p, *q, *arg, *prevarg;
1077 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001078
1079 p = args;
1080 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001082 arg = NULL;
1083 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 q = p;
1085 while (*p && *p != separator)
1086 p++;
1087 while (*p == separator)
1088 *p++ = '\0';
1089 prevarg = arg;
1090 arg = q;
1091 if (process_arg(prevarg, arg))
1092 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001093 }
1094 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001096}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001097
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001098static void process_response_file(const char *file)
1099{
1100 char str[2048];
1101 FILE *f = fopen(file, "r");
1102 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001103 perror(file);
1104 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001105 }
1106 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001107 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001108 }
1109 fclose(f);
1110}
1111
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001112static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001113{
1114 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001115 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001116 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001117
Charles Craynefcce07f2007-09-30 22:15:36 -07001118 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001119
H. Peter Anvin972079f2008-09-30 17:01:23 -07001120 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001121 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001122
1123 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001124 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001125 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001126 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001127 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 envcopy = nasm_strdup(envreal);
1129 process_args(envcopy);
1130 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001131 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001132
1133 /*
1134 * Now process the actual command line.
1135 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001137 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 argv++;
1139 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001140 /*
1141 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 * arguments like the environment variable. This allows us
1143 * to have multiple arguments on a single line, which is
1144 * different to the -@resp file processing below for regular
1145 * NASM.
1146 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001147 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 argc--;
1149 argv++;
1150 }
1151 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001152 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001153 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001154 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001155 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 process_respfile(rfile);
1157 fclose(rfile);
1158 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001159 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 "unable to open response file `%s'", p);
1161 }
1162 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001163 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1164 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001165 }
1166
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001167 /*
1168 * Look for basic command line typos. This definitely doesn't
1169 * catch all errors, but it might help cases of fumbled fingers.
1170 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001171 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001172 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001173 "no input file specified");
1174 else if (!strcmp(inname, errname) ||
1175 !strcmp(inname, outname) ||
1176 !strcmp(inname, listname) ||
1177 (depend_file && !strcmp(inname, depend_file)))
1178 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1179 "file `%s' is both input and output file",
1180 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001181
H. Peter Anvin70653092007-10-19 14:42:29 -07001182 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001183 error_file = fopen(errname, "w");
1184 if (!error_file) {
1185 error_file = stderr; /* Revert to default! */
1186 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1187 "cannot open file `%s' for error messages",
1188 errname);
1189 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001190 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001191}
1192
H. Peter Anvin70055962007-10-11 00:05:31 -07001193static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001194
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001195static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001196{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001197 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001199 int i, validid;
1200 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001201 int32_t seg;
1202 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001203 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001205 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001206
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001207 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001208 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001210
Charles Craynec1905c22008-09-11 18:54:06 -07001211 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001212 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 int pass1, pass2;
1214 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001215
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001216 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001217 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1218 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001219
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001220 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001221
Keith Kaniosb7a89542007-04-12 02:40:54 +00001222 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 cpu = cmd_cpu;
1224 if (pass0 == 2) {
1225 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001226 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001228 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001229 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001231 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001232 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 saa_rewind(forwrefs);
1234 forwref = saa_rstruct(forwrefs);
1235 raa_free(offsets);
1236 offsets = raa_init();
1237 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001238 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001239 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001240 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001241
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001243 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001244 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001245 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001246
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001248 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001250
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001251 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001252 * Here we parse our directives; this is not handled by the
1253 * 'real' parser. This really should be a separate function.
1254 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001256 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001257 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001258 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001259
H. Peter Anvin70055962007-10-11 00:05:31 -07001260 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001261 case D_SEGMENT: /* [SEGMENT n] */
1262 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 seg = ofmt->section(value, pass2, &sb);
1264 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001265 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001266 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 value);
1268 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001269 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 location.segment = seg;
1271 }
1272 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001273 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001274 if (*value) {
1275 stdscan_reset();
1276 stdscan_set(value);
1277 tokval.t_type = TOKEN_INVALID;
1278 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1279 if (e) {
1280 unsigned int align = (unsigned int)e->value;
1281 if ((uint64_t)e->value > 0x7fffffff) {
1282 /*
1283 * FIXME: Please make some sane message here
1284 * ofmt should have some 'check' method which
1285 * would report segment alignment bounds.
1286 */
1287 nasm_error(ERR_FATAL,
1288 "incorrect segment alignment `%s'", value);
1289 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001290 nasm_error(ERR_NONFATAL,
1291 "segment alignment `%s' is not power of two",
1292 value);
1293 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001294 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001295 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001296 }
1297 }
1298 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001299 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 if (*value == '$')
1301 value++; /* skip initial $ if present */
1302 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001303 q = value;
1304 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001306 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 *q++ = '\0';
1308 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001309 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001310 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001311 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001312 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001313 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001314 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001315 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001317 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319 }
1320 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001321 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 "identifier expected after EXTERN");
1323 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001324 }
1325 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 *q++ = '\0';
1327 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001328 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 special = NULL;
1330 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1331 int temp = pass0;
1332 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001333 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001335 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001337 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 } /* else pass0 == 1 */
1339 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001340 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001341 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001343 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 if (*value == '$')
1345 value++; /* skip initial $ if present */
1346 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347 q = value;
1348 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001350 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 *q++ = '\0';
1352 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001353 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001355 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001356 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001357 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001358 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001359 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001361 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363 }
1364 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001365 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 "identifier expected after GLOBAL");
1367 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001368 }
1369 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 *q++ = '\0';
1371 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001372 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001374 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 } /* pass == 1 */
1376 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001377 case D_COMMON: /* [COMMON symbol size:special] */
1378 {
1379 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001380
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 if (*value == '$')
1382 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001383 p = value;
1384 validid = true;
1385 if (!isidstart(*p))
1386 validid = false;
1387 while (*p && !nasm_isspace(*p)) {
1388 if (!isidchar(*p))
1389 validid = false;
1390 p++;
1391 }
1392 if (!validid) {
1393 nasm_error(ERR_NONFATAL,
1394 "identifier expected after COMMON");
1395 break;
1396 }
1397 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001398 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001399 q = p;
1400 while (*q && *q != ':')
1401 q++;
1402 if (*q == ':') {
1403 *q++ = '\0';
1404 special = q;
1405 } else {
1406 special = NULL;
1407 }
1408 size = readnum(p, &rn_error);
1409 if (rn_error) {
1410 nasm_error(ERR_NONFATAL,
1411 "invalid size specified"
1412 " in COMMON declaration");
1413 break;
1414 }
1415 } else {
1416 nasm_error(ERR_NONFATAL,
1417 "no size specified in"
1418 " COMMON declaration");
1419 break;
1420 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001421
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001422 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001423 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001424 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001425 if (special)
1426 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 }
1428 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001429 }
1430 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001432 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 tokval.t_type = TOKEN_INVALID;
1434 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001435 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 if (e) {
1437 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001438 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 1 ? ERR_NONFATAL : ERR_PANIC,
1440 "cannot use non-relocatable expression as "
1441 "ABSOLUTE address");
1442 else {
1443 abs_seg = reloc_seg(e);
1444 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001445 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001446 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 abs_offset = 0x100; /* don't go near zero in case of / */
1448 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001449 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001451 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 location.segment = NO_SEG;
1453 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001454 case D_DEBUG: /* [DEBUG] */
1455 {
1456 char debugid[128];
1457 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001458
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 p = value;
1460 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001461 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001462 if (!isidstart(*p)) {
1463 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001464 } else {
1465 while (*p && !nasm_isspace(*p)) {
1466 if (q >= debugid + sizeof debugid - 1) {
1467 overlong = true;
1468 break;
1469 }
1470 if (!isidchar(*p))
1471 badid = true;
1472 *q++ = *p++;
1473 }
1474 *q = 0;
1475 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001476 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001477 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1478 "identifier expected after DEBUG");
1479 break;
1480 }
1481 if (overlong) {
1482 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1483 "DEBUG identifier too long");
1484 break;
1485 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001486 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001487 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001488 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001490 }
1491 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001492 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001493 switch(*value) {
1494 case '-': validid = 0; value++; break;
1495 case '+': validid = 1; value++; break;
1496 case '*': validid = 2; value++; break;
1497 default: validid = 1; break;
1498 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001500 for (i = 1; i <= ERR_WARN_MAX; i++)
1501 if (!nasm_stricmp(value, warnings[i].name))
1502 break;
1503 if (i <= ERR_WARN_MAX) {
1504 switch(validid) {
1505 case 0:
1506 warning_on[i] = false;
1507 break;
1508 case 1:
1509 warning_on[i] = true;
1510 break;
1511 case 2:
1512 warning_on[i] = warning_on_global[i];
1513 break;
1514 }
1515 } else
1516 nasm_error(ERR_NONFATAL,
1517 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001519 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 cpu = get_cpu(value);
1521 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001522 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001523 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 if (*value == '+') {
1525 user_nolist = 0;
1526 } else {
1527 if (*value == '-') {
1528 user_nolist = 1;
1529 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001530 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 }
1532 }
1533 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001534 case D_DEFAULT: /* [DEFAULT] */
1535 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001536 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001537 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001538 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001539 switch ((int)tokval.t_integer) {
1540 case S_REL:
1541 globalrel = 1;
1542 break;
1543 case S_ABS:
1544 globalrel = 0;
1545 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001546 case P_BND:
1547 globalbnd = 1;
1548 break;
1549 case P_NOBND:
1550 globalbnd = 0;
1551 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001552 default:
1553 err = 1;
1554 break;
1555 }
1556 } else {
1557 err = 1;
1558 }
1559 break;
1560 case D_FLOAT:
1561 if (float_option(value)) {
1562 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1563 "unknown 'float' directive: %s",
1564 value);
1565 }
1566 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001568 if (ofmt->directive(d, value, pass2))
1569 break;
1570 /* else fall through */
1571 case D_unknown:
1572 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1573 "unrecognised directive [%s]",
1574 directive);
1575 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001577 if (err) {
1578 nasm_error(ERR_NONFATAL,
1579 "invalid parameter to [%s] directive",
1580 directive);
1581 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001583 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584
Charles Crayne2581c862008-09-10 19:21:52 -07001585 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001587 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001589 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 forwref = saa_rstruct(forwrefs);
1591 } while (forwref != NULL
1592 && forwref->lineno == globallineno);
1593 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001594 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001595
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001596 if (output_ins.forw_ref) {
1597 if (passn == 1) {
1598 for (i = 0; i < output_ins.operands; i++) {
1599 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1600 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1601 fwinf->lineno = globallineno;
1602 fwinf->operand = i;
1603 }
1604 }
1605 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001606 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 /* forw_ref */
1610 if (output_ins.opcode == I_EQU) {
1611 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001612 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 * Special `..' EQUs get processed in pass two,
1614 * except `..@' macro-processor EQUs which are done
1615 * in the normal place.
1616 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001617 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001618 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001620
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621 else if (output_ins.label[0] != '.' ||
1622 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 output_ins.label[2] == '@') {
1624 if (output_ins.operands == 1 &&
1625 (output_ins.oprs[0].type & IMMEDIATE) &&
1626 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001627 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 def_label(output_ins.label,
1629 output_ins.oprs[0].segment,
1630 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001631 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001633 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001635 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001637 && (output_ins.oprs[1].type & IMMEDIATE)
1638 && output_ins.oprs[1].segment == NO_SEG
1639 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001641 output_ins.oprs[0].offset | SEG_ABS,
1642 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001643 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001645 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001648 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001649 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 * Special `..' EQUs get processed here, except
1651 * `..@' macro processor EQUs which are done above.
1652 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 output_ins.label[1] == '.' &&
1655 output_ins.label[2] != '@') {
1656 if (output_ins.operands == 1 &&
1657 (output_ins.oprs[0].type & IMMEDIATE)) {
1658 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001659 output_ins.oprs[0].segment,
1660 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001661 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001663 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001665 && output_ins.oprs[0].segment == NO_SEG
1666 && (output_ins.oprs[1].type & IMMEDIATE)
1667 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001669 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001671 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001673 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001675 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001676 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001678
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001680
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001681 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001682 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001683
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 /* if (using_debug_info) && output_ins.opcode != -1) */
1685 if (using_debug_info)
1686 { /* fbk 03/25/01 */
1687 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001688 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 TYS_ELEMENTS(output_ins.operands);
1690 switch (output_ins.opcode) {
1691 case I_RESB:
1692 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001693 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 break;
1695 case I_RESW:
1696 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001697 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 break;
1699 case I_RESD:
1700 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001701 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 break;
1703 case I_RESQ:
1704 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001705 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 break;
1707 case I_REST:
1708 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001709 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001711 case I_RESO:
1712 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001713 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001714 break;
1715 case I_RESY:
1716 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001717 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001718 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 case I_DB:
1720 typeinfo |= TY_BYTE;
1721 break;
1722 case I_DW:
1723 typeinfo |= TY_WORD;
1724 break;
1725 case I_DD:
1726 if (output_ins.eops_float)
1727 typeinfo |= TY_FLOAT;
1728 else
1729 typeinfo |= TY_DWORD;
1730 break;
1731 case I_DQ:
1732 typeinfo |= TY_QWORD;
1733 break;
1734 case I_DT:
1735 typeinfo |= TY_TBYTE;
1736 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001737 case I_DO:
1738 typeinfo |= TY_OWORD;
1739 break;
1740 case I_DY:
1741 typeinfo |= TY_YWORD;
1742 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 default:
1744 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001745
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001747
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001748 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001749 }
1750 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001752 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001753 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001754 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 * else l == -1 => invalid instruction, which will be
1756 * flagged as an error on pass 2
1757 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001758
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001759 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001761 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001763 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001764
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 }
1766 } /* not an EQU */
1767 cleanup_insn(&output_ins);
1768 }
1769 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001770 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001772
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001773 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001774 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001775 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001776
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 if (pass1 == 1)
1778 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001779
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001780 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001782 } else if (global_offset_changed &&
1783 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001784 prev_offset_changed = global_offset_changed;
1785 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001786 } else {
1787 stall_count++;
1788 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001789
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001790 if (terminate_after_phase)
1791 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001792
1793 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001794 /* We get here if the labels don't converge
1795 * Example: FOO equ FOO + 1
1796 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001797 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001798 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001799 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001800 nasm_error(ERR_NONFATAL,
1801 "Possible causes: recursive EQUs, macro abuse.");
1802 break;
1803 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001804 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001805
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 preproc->cleanup(0);
1807 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001808 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001809 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001810 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1811 }
1812}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001813
H. Peter Anvin70055962007-10-11 00:05:31 -07001814static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001815{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001816 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001818 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001819
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001820 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001822 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001823 q = strchr(buf, ']');
1824 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001825 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001826
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001827 /* stip off the comments */
1828 p = strchr(buf, ';');
1829 if (p) {
1830 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001831 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001832 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001833 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001834
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001835 /* no brace, no trailing spaces */
1836 *q = '\0';
1837 nasm_zap_spaces_rev(--q);
1838
1839 /* directive */
1840 p = nasm_skip_spaces(++buf);
1841 q = nasm_skip_word(p);
1842 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001843 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001844 *q = '\0';
1845 *directive = p;
1846
1847 /* and value finally */
1848 p = nasm_skip_spaces(++q);
1849 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001850
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001851 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001852}
1853
Ed Berosetfa771012002-06-09 20:56:40 +00001854/**
1855 * gnu style error reporting
1856 * This function prints an error message to error_file in the
1857 * style used by GNU. An example would be:
1858 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001859 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001860 * which the error occurs (or is detected) and "error:" is one of
1861 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001862 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001863 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001864 *
1865 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001866 * @param fmt the printf style format string
1867 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001868static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001869{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001870 char *currentfile = NULL;
1871 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001872
Ed Berosetfa771012002-06-09 20:56:40 +00001873 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001875
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001876 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001878
1879 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001880 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1881 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001882 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001883 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001884 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001885
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001886 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001887}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001888
Ed Berosetfa771012002-06-09 20:56:40 +00001889/**
1890 * MS style error reporting
1891 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001892 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001893 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001894 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001895 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001896 * which the error occurs (or is detected) and "error:" is one of
1897 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001898 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001899 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001900 *
1901 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001902 * @param fmt the printf style format string
1903 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001904static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001905{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001906 char *currentfile = NULL;
1907 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001908
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 if (is_suppressed_warning(severity))
1910 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001911
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001912 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001914
1915 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001916 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001918 } else {
1919 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001920 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001921
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001922 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001923}
1924
1925/**
1926 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001927 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001928 * not in pass 1
1929 *
1930 * @param severity the severity of the warning or error
1931 * @return true if we should abort error/warning printing
1932 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001933static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001934{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001935 /* Not a warning at all */
1936 if ((severity & ERR_MASK) != ERR_WARNING)
1937 return false;
1938
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001939 /* See if it's a pass-one only warning and we're not in pass one. */
1940 if (((severity & ERR_PASS1) && pass0 != 1) ||
1941 ((severity & ERR_PASS2) && pass0 != 2))
1942 return true;
1943
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001944 /* Might be a warning but suppresed explicitly */
1945 if (severity & ERR_WARN_MASK)
1946 return !warning_on[WARN_IDX(severity)];
1947 else
1948 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001949}
1950
1951/**
1952 * common error reporting
1953 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001954 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001955 * specific error message to error_file and may or may not return. It
1956 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001957 *
1958 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001959 * @param fmt the printf style format string
1960 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001961static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001962{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001963 char msg[1024];
1964 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001965
H. Peter Anvin7df04172008-06-10 18:27:38 -07001966 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001968 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 break;
1970 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001971 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 break;
1973 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001974 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 break;
1976 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001977 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 break;
1979 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001980 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001982 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001983 pfx = "";
1984 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001985 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001986
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001987 vsnprintf(msg, sizeof msg, fmt, args);
1988
1989 fprintf(error_file, "%s%s\n", pfx, msg);
1990
1991 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001992 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001993
1994 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001995 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001996
1997 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 case ERR_DEBUG:
1999 /* no further action, by definition */
2000 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002001 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002002 /* Treat warnings as errors */
2003 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2004 terminate_after_phase = true;
2005 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002006 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002007 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002008 break;
2009 case ERR_FATAL:
2010 if (ofile) {
2011 fclose(ofile);
2012 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002013 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 }
2015 if (want_usage)
2016 usage();
2017 exit(1); /* instantly die */
2018 break; /* placate silly compilers */
2019 case ERR_PANIC:
2020 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002021 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 exit(3);
2023 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002024 }
2025}
2026
H. Peter Anvin734b1882002-04-30 21:01:08 +00002027static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002028{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002029 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002030}
2031
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002032static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002033{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002034 iflag_t r;
2035
2036 iflag_clear_all(&r);
2037
H. Peter Anvine2c80182005-01-15 22:15:51 +00002038 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002039 iflag_set(&r, IF_8086);
2040 else if (!strcmp(value, "186"))
2041 iflag_set(&r, IF_186);
2042 else if (!strcmp(value, "286"))
2043 iflag_set(&r, IF_286);
2044 else if (!strcmp(value, "386"))
2045 iflag_set(&r, IF_386);
2046 else if (!strcmp(value, "486"))
2047 iflag_set(&r, IF_486);
2048 else if (!strcmp(value, "586") ||
2049 !nasm_stricmp(value, "pentium"))
2050 iflag_set(&r, IF_PENT);
2051 else if (!strcmp(value, "686") ||
2052 !nasm_stricmp(value, "ppro") ||
2053 !nasm_stricmp(value, "pentiumpro") ||
2054 !nasm_stricmp(value, "p2"))
2055 iflag_set(&r, IF_P6);
2056 else if (!nasm_stricmp(value, "p3") ||
2057 !nasm_stricmp(value, "katmai"))
2058 iflag_set(&r, IF_KATMAI);
2059 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2060 !nasm_stricmp(value, "willamette"))
2061 iflag_set(&r, IF_WILLAMETTE);
2062 else if (!nasm_stricmp(value, "prescott"))
2063 iflag_set(&r, IF_PRESCOTT);
2064 else if (!nasm_stricmp(value, "x64") ||
2065 !nasm_stricmp(value, "x86-64"))
2066 iflag_set(&r, IF_X86_64);
2067 else if (!nasm_stricmp(value, "ia64") ||
2068 !nasm_stricmp(value, "ia-64") ||
2069 !nasm_stricmp(value, "itanium")||
2070 !nasm_stricmp(value, "itanic") ||
2071 !nasm_stricmp(value, "merced"))
2072 iflag_set(&r, IF_IA64);
2073 else {
2074 iflag_set(&r, IF_PLEVEL);
2075 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2076 "unknown 'cpu' type");
2077 }
2078 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002079}
2080
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002081static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002082{
2083 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002084
H. Peter Anvine2c80182005-01-15 22:15:51 +00002085 if ((i = atoi(value)) == 16)
2086 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002087 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002088 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002089 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 "cannot specify 32-bit segment on processor below a 386");
2091 i = 16;
2092 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002093 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002094 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002095 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002096 "cannot specify 64-bit segment on processor below an x86-64");
2097 i = 16;
2098 }
2099 if (i != maxbits) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002100 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002101 "%s output format does not support 64-bit code",
2102 ofmt->shortname);
2103 i = 16;
2104 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002105 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002106 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002107 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 value);
2109 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002110 }
2111 return i;
2112}