blob: 2bb30292164b788615d2257b91177e8b101ac75d [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 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000132 op_normal, /* Preprocess and assemble */
133 op_preprocess, /* Preprocess only */
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700134 op_depend, /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000135};
136static enum op_type operating_mode;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700137/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700138static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700139static bool depend_missing_ok = false;
140static const char *depend_target = NULL;
141static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000142
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000144 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800145 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200147
H. Peter Anvin972079f2008-09-30 17:01:23 -0700148static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
149static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000150
H. Peter Anvin972079f2008-09-30 17:01:23 -0700151static const struct warning {
152 const char *name;
153 const char *help;
154 bool enabled;
155} warnings[ERR_WARN_MAX+1] = {
156 {"error", "treat warnings as errors", false},
157 {"macro-params", "macro calls with wrong parameter count", true},
158 {"macro-selfref", "cyclic macro references", false},
159 {"macro-defaults", "macros with more default than optional parameters", true},
160 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700161 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700162 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
163 {"float-overflow", "floating point overflow", true},
164 {"float-denorm", "floating point denormal", false},
165 {"float-underflow", "floating point underflow", false},
166 {"float-toolong", "too many digits in floating-point number", true},
167 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800168 {"lock", "lock prefix on unlockable instructions", true},
169 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800170 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000171};
172
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700173static bool want_usage;
174static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000175int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176
H. Peter Anvin55340992012-09-09 17:09:00 -0700177static char *quote_for_make(const char *str);
178
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400179static int64_t get_curr_offs(void)
180{
181 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
182}
183
184static void set_curr_offs(int64_t l_off)
185{
186 if (in_abs_seg)
187 abs_offset = l_off;
188 else
189 offsets = raa_write(offsets, location.segment, l_off);
190}
191
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000192static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000194 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800196 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000199}
200
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800201/* Convert a struct tm to a POSIX-style time constant */
202static int64_t posix_mktime(struct tm *tm)
203{
204 int64_t t;
205 int64_t y = tm->tm_year;
206
207 /* See IEEE 1003.1:2004, section 4.14 */
208
209 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
210 t += tm->tm_yday;
211 t *= 24;
212 t += tm->tm_hour;
213 t *= 60;
214 t += tm->tm_min;
215 t *= 60;
216 t += tm->tm_sec;
217
218 return t;
219}
220
221static void define_macros_early(void)
222{
223 char temp[128];
224 struct tm lt, *lt_p, gm, *gm_p;
225 int64_t posix_time;
226
227 lt_p = localtime(&official_compile_time);
228 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400229 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800230
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400231 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
232 preproc->pre_define(temp);
233 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
234 preproc->pre_define(temp);
235 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
238 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800239 }
240
241 gm_p = gmtime(&official_compile_time);
242 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400243 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800244
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400245 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
246 preproc->pre_define(temp);
247 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
248 preproc->pre_define(temp);
249 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
250 preproc->pre_define(temp);
251 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
252 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800253 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700254
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800255 if (gm_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400256 posix_time = posix_mktime(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 else if (lt_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400258 posix_time = posix_mktime(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400260 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261
262 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400263 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
264 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265 }
266}
267
268static void define_macros_late(void)
269{
270 char temp[128];
271
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400272 /*
273 * In case if output format is defined by alias
274 * we have to put shortname of the alias itself here
275 * otherwise ABI backward compatibility gets broken.
276 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400277 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400278 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400279 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280}
281
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700282static void emit_dependencies(StrList *list)
283{
284 FILE *deps;
285 int linepos, len;
286 StrList *l, *nl;
287
288 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400289 deps = fopen(depend_file, "w");
290 if (!deps) {
291 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
292 "unable to write dependency file `%s'", depend_file);
293 return;
294 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700295 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400296 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700297 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700298
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700299 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400300 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700301 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400302 len = strlen(file);
303 if (linepos + len > 62 && linepos > 1) {
304 fprintf(deps, " \\\n ");
305 linepos = 1;
306 }
307 fprintf(deps, " %s", file);
308 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700309 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700310 }
311 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700312
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400313 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400314 if (depend_emit_phony)
315 fprintf(deps, "%s:\n\n", l->str);
316 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700317 }
318
319 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400320 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321}
322
H. Peter Anvin038d8612007-04-12 16:54:50 +0000323int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000324{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700326
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800327 time(&official_compile_time);
328
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400329 iflag_set(&cpu, IF_PLEVEL);
330 iflag_set(&cmd_cpu, IF_PLEVEL);
331
Charles Crayne2581c862008-09-10 19:21:52 -0700332 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700333 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400334 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000335
H. Peter Anvin97e15752007-09-25 08:47:47 -0700336 error_file = stderr;
337
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700338 tolower_init();
339
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400340 nasm_init_malloc_error();
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;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000345 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 Gorcunov9f563692013-02-15 12:24:11 +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) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000378 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
395 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
447 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
H. Peter Anvin423e3812007-11-15 17:12:29 -0800638static bool stopoptions = false;
639static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000640{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000641 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800642 int i;
643 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700644 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645
646 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800647 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000648
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400650 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
651 /* These parameters take values */
652 if (!(param = get_param(p, q, &advance)))
653 return advance;
654 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800655
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 switch (p[1]) {
657 case 's':
658 error_file = stdout;
659 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700660
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400661 case 'o': /* output file */
662 copy_filename(outname, param);
663 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700664
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400665 case 'f': /* output format */
666 ofmt = ofmt_find(param, &ofmt_alias);
667 if (!ofmt) {
668 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
669 "unrecognised output format `%s' - "
670 "use -hf for a list", param);
671 }
672 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700673
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400674 case 'O': /* Optimization level */
675 {
676 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700677
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400678 if (!*param) {
679 /* Naked -O == -Ox */
680 optimizing = MAX_OPTIMIZE;
681 } else {
682 while (*param) {
683 switch (*param) {
684 case '0': case '1': case '2': case '3': case '4':
685 case '5': case '6': case '7': case '8': case '9':
686 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700687
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400688 /* -O0 -> optimizing == -1, 0.98 behaviour */
689 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
690 if (opt < 2)
691 optimizing = opt - 1;
692 else
693 optimizing = opt;
694 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700695
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400696 case 'v':
697 case '+':
698 param++;
699 opt_verbose_info = true;
700 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700701
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400702 case 'x':
703 param++;
704 optimizing = MAX_OPTIMIZE;
705 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700706
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400707 default:
708 nasm_error(ERR_FATAL,
709 "unknown optimization option -O%c\n",
710 *param);
711 break;
712 }
713 }
714 if (optimizing > MAX_OPTIMIZE)
715 optimizing = MAX_OPTIMIZE;
716 }
717 break;
718 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800719
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400720 case 'p': /* pre-include */
721 case 'P':
722 preproc->pre_include(param);
723 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800724
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400725 case 'd': /* pre-define */
726 case 'D':
727 preproc->pre_define(param);
728 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800729
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400730 case 'u': /* un-define */
731 case 'U':
732 preproc->pre_undefine(param);
733 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800734
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400735 case 'i': /* include search path */
736 case 'I':
737 preproc->include_path(param);
738 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800739
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400740 case 'l': /* listing file */
741 copy_filename(listname, param);
742 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800743
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400744 case 'Z': /* error messages file */
745 copy_filename(errname, param);
746 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800747
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400748 case 'F': /* specify debug format */
749 ofmt->current_dfmt = dfmt_find(ofmt, param);
750 if (!ofmt->current_dfmt) {
751 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
752 "unrecognized debug format `%s' for"
753 " output format `%s'",
754 param, ofmt->shortname);
755 }
756 using_debug_info = true;
757 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800758
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400759 case 'X': /* specify error reporting format */
760 if (nasm_stricmp("vc", param) == 0)
761 nasm_set_verror(nasm_verror_vc);
762 else if (nasm_stricmp("gnu", param) == 0)
763 nasm_set_verror(nasm_verror_gnu);
764 else
765 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
766 "unrecognized error reporting format `%s'",
767 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800769
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700771 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800773
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 case 'h':
775 printf
776 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
777 "[-l listfile]\n"
778 " [options...] [--] filename\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700779 " or nasm -v for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400781 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700783 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700785 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400786 " -MG d:o, missing files assumed generated\n"
787 " -MF <file> set Makefile dependency file\n"
788 " -MD <file> assemble and generate dependencies\n"
789 " -MT <file> dependency target name\n"
790 " -MQ <file> dependency target name (quoted)\n"
791 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700792 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 " -s redirect error messages to stdout\n\n"
794 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400795 " -o outfile write output to an outfile\n\n"
796 " -f format select an output format\n\n"
797 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 " -I<path> adds a pathname to the include file path\n");
799 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700800 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400801 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400802 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400803 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 " -P<file> pre-includes a file\n"
805 " -D<macro>[=<value>] pre-defines a macro\n"
806 " -U<macro> undefines a macro\n"
807 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800808 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400809 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400810 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400811 "--prefix,--postfix\n"
812 " this options prepend or append the given argument to all\n"
813 " extern and global variables\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800814 "Warnings:\n");
815 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700817 warnings[i].name, warnings[i].help,
818 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 printf
820 ("\nresponse files should contain command line parameters"
821 ", one per line.\n");
822 if (p[2] == 'f') {
823 printf("\nvalid output formats for -f are"
824 " (`*' denotes default):\n");
825 ofmt_list(ofmt, stdout);
826 } else {
827 printf("\nFor a list of valid output formats, use -hf.\n");
828 printf("For a list of debug formats, use -f <form> -y.\n");
829 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400830 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800832
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 case 'y':
834 printf("\nvalid debug formats for '%s' output format are"
835 " ('*' denotes default):\n", ofmt->shortname);
836 dfmt_list(ofmt, stdout);
837 exit(0);
838 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800839
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700841 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800843
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 case 'v':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400845 printf("NASM version %s compiled on %s%s\n",
846 nasm_version, nasm_date, nasm_compile_options);
847 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800849
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400850 case 'e': /* preprocess only */
851 case 'E':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 operating_mode = op_preprocess;
853 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800854
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400855 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400856 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800858
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400859 case 'W':
860 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
861 do_warn = false;
862 param += 3;
863 } else {
864 do_warn = true;
865 }
866 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800867
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800869 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400870 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400872 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400874 do_warn = (param[0] == '+');
875 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400876
877set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400878 for (i = 0; i <= ERR_WARN_MAX; i++) {
879 if (!nasm_stricmp(param, warnings[i].name))
880 break;
881 }
882 if (i <= ERR_WARN_MAX) {
883 warning_on_global[i] = do_warn;
884 } else if (!nasm_stricmp(param, "all")) {
885 for (i = 1; i <= ERR_WARN_MAX; i++)
886 warning_on_global[i] = do_warn;
887 } else if (!nasm_stricmp(param, "none")) {
888 for (i = 1; i <= ERR_WARN_MAX; i++)
889 warning_on_global[i] = !do_warn;
890 } else {
891 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
892 "invalid warning `%s'", param);
893 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800895
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400897 switch (p[2]) {
898 case 0:
899 operating_mode = op_depend;
900 break;
901 case 'G':
902 operating_mode = op_depend;
903 depend_missing_ok = true;
904 break;
905 case 'P':
906 depend_emit_phony = true;
907 break;
908 case 'D':
909 depend_file = q;
910 advance = true;
911 break;
912 case 'T':
913 depend_target = q;
914 advance = true;
915 break;
916 case 'Q':
917 depend_target = quote_for_make(q);
918 advance = true;
919 break;
920 default:
921 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
922 "unknown dependency option `-M%c'", p[2]);
923 break;
924 }
925 if (advance && (!q || !q[0])) {
926 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
927 "option `-M%c' requires a parameter", p[2]);
928 break;
929 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000931
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 case '-':
933 {
934 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000935
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 if (p[2] == 0) { /* -- => stop processing options */
937 stopoptions = 1;
938 break;
939 }
940 for (s = 0; textopts[s].label; s++) {
941 if (!nasm_stricmp(p + 2, textopts[s].label)) {
942 break;
943 }
944 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 switch (s) {
947
948 case OPT_PREFIX:
949 case OPT_POSTFIX:
950 {
951 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400952 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 ERR_USAGE,
954 "option `--%s' requires an argument",
955 p + 2);
956 break;
957 } else {
958 advance = 1, param = q;
959 }
960
961 if (s == OPT_PREFIX) {
962 strncpy(lprefix, param, PREFIX_MAX - 1);
963 lprefix[PREFIX_MAX - 1] = 0;
964 break;
965 }
966 if (s == OPT_POSTFIX) {
967 strncpy(lpostfix, param, POSTFIX_MAX - 1);
968 lpostfix[POSTFIX_MAX - 1] = 0;
969 break;
970 }
971 break;
972 }
973 default:
974 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400975 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 "unrecognised option `--%s'", p + 2);
977 break;
978 }
979 }
980 break;
981 }
982
983 default:
984 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400985 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 "unrecognised option `-%c'", p[1]);
987 break;
988 }
989 } else {
990 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400991 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800993 } else {
994 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400995 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000996 }
997
998 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000999}
1000
H. Peter Anvineba20a72002-04-30 20:53:55 +00001001#define ARG_BUF_DELTA 128
1002
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001004{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001005 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001006 int bufsize, prevargsize;
1007
1008 bufsize = prevargsize = ARG_BUF_DELTA;
1009 buffer = nasm_malloc(ARG_BUF_DELTA);
1010 prevarg = nasm_malloc(ARG_BUF_DELTA);
1011 prevarg[0] = '\0';
1012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 p = buffer;
1015 while (1) { /* Loop to handle long lines */
1016 q = fgets(p, bufsize - (p - buffer), rfile);
1017 if (!q)
1018 break;
1019 p += strlen(p);
1020 if (p > buffer && p[-1] == '\n')
1021 break;
1022 if (p - buffer > bufsize - 10) {
1023 int offset;
1024 offset = p - buffer;
1025 bufsize += ARG_BUF_DELTA;
1026 buffer = nasm_realloc(buffer, bufsize);
1027 p = buffer + offset;
1028 }
1029 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 if (!q && p == buffer) {
1032 if (prevarg[0])
1033 process_arg(prevarg, NULL);
1034 nasm_free(buffer);
1035 nasm_free(prevarg);
1036 return;
1037 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 /*
1040 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1041 * them are present at the end of the line.
1042 */
1043 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001044
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001045 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001047
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001048 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001049
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 if (process_arg(prevarg, p))
1051 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052
Charles Crayne192d5b52007-10-18 19:02:42 -07001053 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 prevargsize += ARG_BUF_DELTA;
1055 prevarg = nasm_realloc(prevarg, prevargsize);
1056 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001057 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058 }
1059}
1060
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001061/* Function to process args from a string of args, rather than the
1062 * argv array. Used by the environment variable and response file
1063 * processing.
1064 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001065static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001067 char *p, *q, *arg, *prevarg;
1068 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001069
1070 p = args;
1071 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001073 arg = NULL;
1074 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 q = p;
1076 while (*p && *p != separator)
1077 p++;
1078 while (*p == separator)
1079 *p++ = '\0';
1080 prevarg = arg;
1081 arg = q;
1082 if (process_arg(prevarg, arg))
1083 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001084 }
1085 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001087}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001088
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001089static void process_response_file(const char *file)
1090{
1091 char str[2048];
1092 FILE *f = fopen(file, "r");
1093 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001094 perror(file);
1095 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001096 }
1097 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001098 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001099 }
1100 fclose(f);
1101}
1102
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001103static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001104{
1105 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001106 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001107 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001108
Charles Craynefcce07f2007-09-30 22:15:36 -07001109 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001110
H. Peter Anvin972079f2008-09-30 17:01:23 -07001111 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001112 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001113
1114 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001115 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001116 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001117 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001118 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 envcopy = nasm_strdup(envreal);
1120 process_args(envcopy);
1121 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001122 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001123
1124 /*
1125 * Now process the actual command line.
1126 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001128 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 argv++;
1130 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001131 /*
1132 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 * arguments like the environment variable. This allows us
1134 * to have multiple arguments on a single line, which is
1135 * different to the -@resp file processing below for regular
1136 * NASM.
1137 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001138 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 argc--;
1140 argv++;
1141 }
1142 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001143 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001144 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001145 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001146 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 process_respfile(rfile);
1148 fclose(rfile);
1149 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001150 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 "unable to open response file `%s'", p);
1152 }
1153 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001154 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1155 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001156 }
1157
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001158 /*
1159 * Look for basic command line typos. This definitely doesn't
1160 * catch all errors, but it might help cases of fumbled fingers.
1161 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001162 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001163 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001164 "no input file specified");
1165 else if (!strcmp(inname, errname) ||
1166 !strcmp(inname, outname) ||
1167 !strcmp(inname, listname) ||
1168 (depend_file && !strcmp(inname, depend_file)))
1169 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1170 "file `%s' is both input and output file",
1171 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001172
H. Peter Anvin70653092007-10-19 14:42:29 -07001173 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001174 error_file = fopen(errname, "w");
1175 if (!error_file) {
1176 error_file = stderr; /* Revert to default! */
1177 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1178 "cannot open file `%s' for error messages",
1179 errname);
1180 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001181 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001182}
1183
H. Peter Anvin70055962007-10-11 00:05:31 -07001184static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001185
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001186static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001187{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001188 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001190 int i, validid;
1191 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001192 int32_t seg;
1193 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001194 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001196 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001197
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001198 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001199 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001201
Charles Craynec1905c22008-09-11 18:54:06 -07001202 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001203 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 int pass1, pass2;
1205 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001206
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001207 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001208 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1209 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001211 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001212
Keith Kaniosb7a89542007-04-12 02:40:54 +00001213 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 cpu = cmd_cpu;
1215 if (pass0 == 2) {
1216 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001217 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001219 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001220 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001222 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001223 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 saa_rewind(forwrefs);
1225 forwref = saa_rstruct(forwrefs);
1226 raa_free(offsets);
1227 offsets = raa_init();
1228 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001229 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001230 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001231 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001232
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001234 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001235 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001236 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001237
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001239 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001241
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001242 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001243 * Here we parse our directives; this is not handled by the
1244 * 'real' parser. This really should be a separate function.
1245 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001247 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001248 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001249 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001250
H. Peter Anvin70055962007-10-11 00:05:31 -07001251 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001252 case D_SEGMENT: /* [SEGMENT n] */
1253 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 seg = ofmt->section(value, pass2, &sb);
1255 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001256 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001257 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 value);
1259 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001260 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 location.segment = seg;
1262 }
1263 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001264 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001265 if (*value) {
1266 stdscan_reset();
1267 stdscan_set(value);
1268 tokval.t_type = TOKEN_INVALID;
1269 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1270 if (e) {
1271 unsigned int align = (unsigned int)e->value;
1272 if ((uint64_t)e->value > 0x7fffffff) {
1273 /*
1274 * FIXME: Please make some sane message here
1275 * ofmt should have some 'check' method which
1276 * would report segment alignment bounds.
1277 */
1278 nasm_error(ERR_FATAL,
1279 "incorrect segment alignment `%s'", value);
1280 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001281 nasm_error(ERR_NONFATAL,
1282 "segment alignment `%s' is not power of two",
1283 value);
1284 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001285 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001286 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001287 }
1288 }
1289 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001290 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 if (*value == '$')
1292 value++; /* skip initial $ if present */
1293 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001294 q = value;
1295 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001297 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 *q++ = '\0';
1299 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001300 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001301 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001302 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001303 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001305 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001306 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001308 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 }
1311 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001312 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 "identifier expected after EXTERN");
1314 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001315 }
1316 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 *q++ = '\0';
1318 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 special = NULL;
1321 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1322 int temp = pass0;
1323 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001324 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001326 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001328 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 } /* else pass0 == 1 */
1330 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001331 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001332 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001334 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (*value == '$')
1336 value++; /* skip initial $ if present */
1337 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001338 q = value;
1339 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001341 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 *q++ = '\0';
1343 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001344 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001346 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001347 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001348 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001349 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001350 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001352 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 }
1355 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001356 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 "identifier expected after GLOBAL");
1358 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001359 }
1360 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 *q++ = '\0';
1362 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001365 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 } /* pass == 1 */
1367 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001368 case D_COMMON: /* [COMMON symbol size:special] */
1369 {
1370 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001371
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 if (*value == '$')
1373 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001374 p = value;
1375 validid = true;
1376 if (!isidstart(*p))
1377 validid = false;
1378 while (*p && !nasm_isspace(*p)) {
1379 if (!isidchar(*p))
1380 validid = false;
1381 p++;
1382 }
1383 if (!validid) {
1384 nasm_error(ERR_NONFATAL,
1385 "identifier expected after COMMON");
1386 break;
1387 }
1388 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001389 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001390 q = p;
1391 while (*q && *q != ':')
1392 q++;
1393 if (*q == ':') {
1394 *q++ = '\0';
1395 special = q;
1396 } else {
1397 special = NULL;
1398 }
1399 size = readnum(p, &rn_error);
1400 if (rn_error) {
1401 nasm_error(ERR_NONFATAL,
1402 "invalid size specified"
1403 " in COMMON declaration");
1404 break;
1405 }
1406 } else {
1407 nasm_error(ERR_NONFATAL,
1408 "no size specified in"
1409 " COMMON declaration");
1410 break;
1411 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001412
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001413 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001414 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001415 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001416 if (special)
1417 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 }
1419 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001420 }
1421 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001423 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 tokval.t_type = TOKEN_INVALID;
1425 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001426 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 if (e) {
1428 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001429 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 1 ? ERR_NONFATAL : ERR_PANIC,
1431 "cannot use non-relocatable expression as "
1432 "ABSOLUTE address");
1433 else {
1434 abs_seg = reloc_seg(e);
1435 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001436 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001437 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 abs_offset = 0x100; /* don't go near zero in case of / */
1439 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001440 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001442 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 location.segment = NO_SEG;
1444 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001445 case D_DEBUG: /* [DEBUG] */
1446 {
1447 char debugid[128];
1448 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001449
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 p = value;
1451 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001452 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001453 if (!isidstart(*p)) {
1454 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001455 } else {
1456 while (*p && !nasm_isspace(*p)) {
1457 if (q >= debugid + sizeof debugid - 1) {
1458 overlong = true;
1459 break;
1460 }
1461 if (!isidchar(*p))
1462 badid = true;
1463 *q++ = *p++;
1464 }
1465 *q = 0;
1466 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001467 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001468 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1469 "identifier expected after DEBUG");
1470 break;
1471 }
1472 if (overlong) {
1473 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1474 "DEBUG identifier too long");
1475 break;
1476 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001477 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001478 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001479 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001481 }
1482 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001483 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001484 switch(*value) {
1485 case '-': validid = 0; value++; break;
1486 case '+': validid = 1; value++; break;
1487 case '*': validid = 2; value++; break;
1488 default: validid = 1; break;
1489 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001491 for (i = 1; i <= ERR_WARN_MAX; i++)
1492 if (!nasm_stricmp(value, warnings[i].name))
1493 break;
1494 if (i <= ERR_WARN_MAX) {
1495 switch(validid) {
1496 case 0:
1497 warning_on[i] = false;
1498 break;
1499 case 1:
1500 warning_on[i] = true;
1501 break;
1502 case 2:
1503 warning_on[i] = warning_on_global[i];
1504 break;
1505 }
1506 } else
1507 nasm_error(ERR_NONFATAL,
1508 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001510 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 cpu = get_cpu(value);
1512 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001513 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001514 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 if (*value == '+') {
1516 user_nolist = 0;
1517 } else {
1518 if (*value == '-') {
1519 user_nolist = 1;
1520 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001521 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 }
1523 }
1524 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001525 case D_DEFAULT: /* [DEFAULT] */
1526 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001527 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001528 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001529 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001530 switch ((int)tokval.t_integer) {
1531 case S_REL:
1532 globalrel = 1;
1533 break;
1534 case S_ABS:
1535 globalrel = 0;
1536 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001537 case P_BND:
1538 globalbnd = 1;
1539 break;
1540 case P_NOBND:
1541 globalbnd = 0;
1542 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001543 default:
1544 err = 1;
1545 break;
1546 }
1547 } else {
1548 err = 1;
1549 }
1550 break;
1551 case D_FLOAT:
1552 if (float_option(value)) {
1553 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1554 "unknown 'float' directive: %s",
1555 value);
1556 }
1557 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001559 if (ofmt->directive(d, value, pass2))
1560 break;
1561 /* else fall through */
1562 case D_unknown:
1563 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1564 "unrecognised directive [%s]",
1565 directive);
1566 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001568 if (err) {
1569 nasm_error(ERR_NONFATAL,
1570 "invalid parameter to [%s] directive",
1571 directive);
1572 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001574 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575
Charles Crayne2581c862008-09-10 19:21:52 -07001576 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001578 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001580 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 forwref = saa_rstruct(forwrefs);
1582 } while (forwref != NULL
1583 && forwref->lineno == globallineno);
1584 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001585 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001586
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001587 if (output_ins.forw_ref) {
1588 if (passn == 1) {
1589 for (i = 0; i < output_ins.operands; i++) {
1590 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1591 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1592 fwinf->lineno = globallineno;
1593 fwinf->operand = i;
1594 }
1595 }
1596 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001597 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 /* forw_ref */
1601 if (output_ins.opcode == I_EQU) {
1602 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001603 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 * Special `..' EQUs get processed in pass two,
1605 * except `..@' macro-processor EQUs which are done
1606 * in the normal place.
1607 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001608 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001609 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001612 else if (output_ins.label[0] != '.' ||
1613 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 output_ins.label[2] == '@') {
1615 if (output_ins.operands == 1 &&
1616 (output_ins.oprs[0].type & IMMEDIATE) &&
1617 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001618 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 def_label(output_ins.label,
1620 output_ins.oprs[0].segment,
1621 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001622 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001624 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001626 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001628 && (output_ins.oprs[1].type & IMMEDIATE)
1629 && output_ins.oprs[1].segment == NO_SEG
1630 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001632 output_ins.oprs[0].offset | SEG_ABS,
1633 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001634 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001636 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001638 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001639 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001640 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 * Special `..' EQUs get processed here, except
1642 * `..@' macro processor EQUs which are done above.
1643 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 output_ins.label[1] == '.' &&
1646 output_ins.label[2] != '@') {
1647 if (output_ins.operands == 1 &&
1648 (output_ins.oprs[0].type & IMMEDIATE)) {
1649 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 output_ins.oprs[0].segment,
1651 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001652 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001654 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001656 && output_ins.oprs[0].segment == NO_SEG
1657 && (output_ins.oprs[1].type & IMMEDIATE)
1658 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001660 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001662 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001664 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001666 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001667 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001669
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001671
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001672 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001673 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001674
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 /* if (using_debug_info) && output_ins.opcode != -1) */
1676 if (using_debug_info)
1677 { /* fbk 03/25/01 */
1678 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001679 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 TYS_ELEMENTS(output_ins.operands);
1681 switch (output_ins.opcode) {
1682 case I_RESB:
1683 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001684 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 break;
1686 case I_RESW:
1687 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001688 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 break;
1690 case I_RESD:
1691 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001692 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 break;
1694 case I_RESQ:
1695 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001696 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 break;
1698 case I_REST:
1699 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001700 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001702 case I_RESO:
1703 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001704 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001705 break;
1706 case I_RESY:
1707 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001708 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001709 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 case I_DB:
1711 typeinfo |= TY_BYTE;
1712 break;
1713 case I_DW:
1714 typeinfo |= TY_WORD;
1715 break;
1716 case I_DD:
1717 if (output_ins.eops_float)
1718 typeinfo |= TY_FLOAT;
1719 else
1720 typeinfo |= TY_DWORD;
1721 break;
1722 case I_DQ:
1723 typeinfo |= TY_QWORD;
1724 break;
1725 case I_DT:
1726 typeinfo |= TY_TBYTE;
1727 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001728 case I_DO:
1729 typeinfo |= TY_OWORD;
1730 break;
1731 case I_DY:
1732 typeinfo |= TY_YWORD;
1733 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 default:
1735 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001736
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001738
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001739 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001740 }
1741 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001743 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001744 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001745 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 * else l == -1 => invalid instruction, which will be
1747 * flagged as an error on pass 2
1748 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001749
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001750 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001752 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001754 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 }
1757 } /* not an EQU */
1758 cleanup_insn(&output_ins);
1759 }
1760 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001761 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001763
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001764 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001765 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001766 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001767
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 if (pass1 == 1)
1769 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001770
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001771 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001773 } else if (global_offset_changed &&
1774 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001775 prev_offset_changed = global_offset_changed;
1776 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001777 } else {
1778 stall_count++;
1779 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001780
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001781 if (terminate_after_phase)
1782 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001783
1784 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001785 /* We get here if the labels don't converge
1786 * Example: FOO equ FOO + 1
1787 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001788 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001789 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001790 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001791 nasm_error(ERR_NONFATAL,
1792 "Possible causes: recursive EQUs, macro abuse.");
1793 break;
1794 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001795 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001796
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 preproc->cleanup(0);
1798 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001799 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001800 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001801 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1802 }
1803}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001804
H. Peter Anvin70055962007-10-11 00:05:31 -07001805static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001806{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001807 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001809 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001810
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001811 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001813 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001814 q = strchr(buf, ']');
1815 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001816 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001817
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001818 /* stip off the comments */
1819 p = strchr(buf, ';');
1820 if (p) {
1821 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001822 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001823 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001824 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001825
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001826 /* no brace, no trailing spaces */
1827 *q = '\0';
1828 nasm_zap_spaces_rev(--q);
1829
1830 /* directive */
1831 p = nasm_skip_spaces(++buf);
1832 q = nasm_skip_word(p);
1833 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001834 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001835 *q = '\0';
1836 *directive = p;
1837
1838 /* and value finally */
1839 p = nasm_skip_spaces(++q);
1840 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001841
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001842 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001843}
1844
Ed Berosetfa771012002-06-09 20:56:40 +00001845/**
1846 * gnu style error reporting
1847 * This function prints an error message to error_file in the
1848 * style used by GNU. An example would be:
1849 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001850 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001851 * which the error occurs (or is detected) and "error:" is one of
1852 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001853 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001854 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001855 *
1856 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001857 * @param fmt the printf style format string
1858 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001859static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001860{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001861 char *currentfile = NULL;
1862 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001863
Ed Berosetfa771012002-06-09 20:56:40 +00001864 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001866
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001867 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001869
1870 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001871 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1872 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001873 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001874 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001875 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001876
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001877 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001878}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001879
Ed Berosetfa771012002-06-09 20:56:40 +00001880/**
1881 * MS style error reporting
1882 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001883 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001884 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001885 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001886 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001887 * which the error occurs (or is detected) and "error:" is one of
1888 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001889 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001890 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001891 *
1892 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001893 * @param fmt the printf style format string
1894 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001895static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001896{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001897 char *currentfile = NULL;
1898 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001899
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 if (is_suppressed_warning(severity))
1901 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001902
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001903 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001905
1906 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001907 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001909 } else {
1910 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001911 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001912
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001913 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001914}
1915
1916/**
1917 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001918 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001919 * not in pass 1
1920 *
1921 * @param severity the severity of the warning or error
1922 * @return true if we should abort error/warning printing
1923 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001924static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001925{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001926 /* Not a warning at all */
1927 if ((severity & ERR_MASK) != ERR_WARNING)
1928 return false;
1929
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001930 /* See if it's a pass-one only warning and we're not in pass one. */
1931 if (((severity & ERR_PASS1) && pass0 != 1) ||
1932 ((severity & ERR_PASS2) && pass0 != 2))
1933 return true;
1934
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001935 /* Might be a warning but suppresed explicitly */
1936 if (severity & ERR_WARN_MASK)
1937 return !warning_on[WARN_IDX(severity)];
1938 else
1939 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001940}
1941
1942/**
1943 * common error reporting
1944 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001945 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001946 * specific error message to error_file and may or may not return. It
1947 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001948 *
1949 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001950 * @param fmt the printf style format string
1951 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001952static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001953{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001954 char msg[1024];
1955 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001956
H. Peter Anvin7df04172008-06-10 18:27:38 -07001957 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001959 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 break;
1961 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001962 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 break;
1964 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001965 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 break;
1967 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001968 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 break;
1970 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001971 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001973 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001974 pfx = "";
1975 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001976 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001977
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001978 vsnprintf(msg, sizeof msg, fmt, args);
1979
1980 fprintf(error_file, "%s%s\n", pfx, msg);
1981
1982 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001983 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001984
1985 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001986 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001987
1988 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 case ERR_DEBUG:
1990 /* no further action, by definition */
1991 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001992 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001993 /* Treat warnings as errors */
1994 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1995 terminate_after_phase = true;
1996 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001998 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 break;
2000 case ERR_FATAL:
2001 if (ofile) {
2002 fclose(ofile);
2003 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002004 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 }
2006 if (want_usage)
2007 usage();
2008 exit(1); /* instantly die */
2009 break; /* placate silly compilers */
2010 case ERR_PANIC:
2011 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002012 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 exit(3);
2014 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002015 }
2016}
2017
H. Peter Anvin734b1882002-04-30 21:01:08 +00002018static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002019{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002020 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002021}
2022
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002023static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002024{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002025 iflag_t r;
2026
2027 iflag_clear_all(&r);
2028
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002030 iflag_set(&r, IF_8086);
2031 else if (!strcmp(value, "186"))
2032 iflag_set(&r, IF_186);
2033 else if (!strcmp(value, "286"))
2034 iflag_set(&r, IF_286);
2035 else if (!strcmp(value, "386"))
2036 iflag_set(&r, IF_386);
2037 else if (!strcmp(value, "486"))
2038 iflag_set(&r, IF_486);
2039 else if (!strcmp(value, "586") ||
2040 !nasm_stricmp(value, "pentium"))
2041 iflag_set(&r, IF_PENT);
2042 else if (!strcmp(value, "686") ||
2043 !nasm_stricmp(value, "ppro") ||
2044 !nasm_stricmp(value, "pentiumpro") ||
2045 !nasm_stricmp(value, "p2"))
2046 iflag_set(&r, IF_P6);
2047 else if (!nasm_stricmp(value, "p3") ||
2048 !nasm_stricmp(value, "katmai"))
2049 iflag_set(&r, IF_KATMAI);
2050 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2051 !nasm_stricmp(value, "willamette"))
2052 iflag_set(&r, IF_WILLAMETTE);
2053 else if (!nasm_stricmp(value, "prescott"))
2054 iflag_set(&r, IF_PRESCOTT);
2055 else if (!nasm_stricmp(value, "x64") ||
2056 !nasm_stricmp(value, "x86-64"))
2057 iflag_set(&r, IF_X86_64);
2058 else if (!nasm_stricmp(value, "ia64") ||
2059 !nasm_stricmp(value, "ia-64") ||
2060 !nasm_stricmp(value, "itanium")||
2061 !nasm_stricmp(value, "itanic") ||
2062 !nasm_stricmp(value, "merced"))
2063 iflag_set(&r, IF_IA64);
2064 else {
2065 iflag_set(&r, IF_PLEVEL);
2066 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2067 "unknown 'cpu' type");
2068 }
2069 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002070}
2071
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002072static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002073{
2074 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002075
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 if ((i = atoi(value)) == 16)
2077 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002078 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002079 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002080 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 "cannot specify 32-bit segment on processor below a 386");
2082 i = 16;
2083 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002084 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002085 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002086 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002087 "cannot specify 64-bit segment on processor below an x86-64");
2088 i = 16;
2089 }
2090 if (i != maxbits) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002091 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002092 "%s output format does not support 64-bit code",
2093 ofmt->shortname);
2094 i = 16;
2095 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002096 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002097 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002098 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 value);
2100 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002101 }
2102 return i;
2103}