blob: b83810d247e5ea69a92311c9bb618f131e7b4fc4 [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;
H. Peter Anvineba20a72002-04-30 20:53:55 +000092
H. Peter Anvin9bd15062009-07-18 21:07:17 -040093static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080094
Keith Kaniosa6dfa782007-04-13 16:47:53 +000095static char inname[FILENAME_MAX];
96static char outname[FILENAME_MAX];
97static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -070098static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +000099static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000100/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400101struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400102struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400103const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000104
H. Peter Anvine2c80182005-01-15 22:15:51 +0000105static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000106
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400107FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700108int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
109static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400110
111static iflag_t cpu;
112static iflag_t cmd_cpu;
113
Charles Craynec1905c22008-09-11 18:54:06 -0700114int64_t global_offset_changed; /* referenced in labels.c */
115int64_t prev_offset_changed;
116int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000117
H. Peter Anvin12e46512007-10-03 21:30:57 -0700118static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000120int32_t abs_seg; /* ABSOLUTE segment basis */
121int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000122
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000123static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000124
H. Peter Anvine2c80182005-01-15 22:15:51 +0000125static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700126static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000127
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400128static struct preproc_ops *preproc;
129
H. Peter Anvin620515a2002-04-30 20:57:38 +0000130enum op_type {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000131 op_normal, /* Preprocess and assemble */
132 op_preprocess, /* Preprocess only */
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700133 op_depend, /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000134};
135static enum op_type operating_mode;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700136/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700137static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700138static bool depend_missing_ok = false;
139static const char *depend_target = NULL;
140static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000142/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000143 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800144 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000145 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200146
H. Peter Anvin972079f2008-09-30 17:01:23 -0700147static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
148static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000149
H. Peter Anvin972079f2008-09-30 17:01:23 -0700150static const struct warning {
151 const char *name;
152 const char *help;
153 bool enabled;
154} warnings[ERR_WARN_MAX+1] = {
155 {"error", "treat warnings as errors", false},
156 {"macro-params", "macro calls with wrong parameter count", true},
157 {"macro-selfref", "cyclic macro references", false},
158 {"macro-defaults", "macros with more default than optional parameters", true},
159 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700160 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700161 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
162 {"float-overflow", "floating point overflow", true},
163 {"float-denorm", "floating point denormal", false},
164 {"float-underflow", "floating point underflow", false},
165 {"float-toolong", "too many digits in floating-point number", true},
166 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800167 {"lock", "lock prefix on unlockable instructions", true},
168 {"hle", "invalid hle prefixes", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000169};
170
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700171static bool want_usage;
172static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000173int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174
H. Peter Anvin55340992012-09-09 17:09:00 -0700175static char *quote_for_make(const char *str);
176
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400177static int64_t get_curr_offs(void)
178{
179 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
180}
181
182static void set_curr_offs(int64_t l_off)
183{
184 if (in_abs_seg)
185 abs_offset = l_off;
186 else
187 offsets = raa_write(offsets, location.segment, l_off);
188}
189
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000190static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000191{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000192 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800194 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000195 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000196 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197}
198
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800199/* Convert a struct tm to a POSIX-style time constant */
200static int64_t posix_mktime(struct tm *tm)
201{
202 int64_t t;
203 int64_t y = tm->tm_year;
204
205 /* See IEEE 1003.1:2004, section 4.14 */
206
207 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
208 t += tm->tm_yday;
209 t *= 24;
210 t += tm->tm_hour;
211 t *= 60;
212 t += tm->tm_min;
213 t *= 60;
214 t += tm->tm_sec;
215
216 return t;
217}
218
219static void define_macros_early(void)
220{
221 char temp[128];
222 struct tm lt, *lt_p, gm, *gm_p;
223 int64_t posix_time;
224
225 lt_p = localtime(&official_compile_time);
226 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400227 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800228
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400229 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
230 preproc->pre_define(temp);
231 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
232 preproc->pre_define(temp);
233 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
234 preproc->pre_define(temp);
235 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
236 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800237 }
238
239 gm_p = gmtime(&official_compile_time);
240 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400241 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800242
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400243 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
244 preproc->pre_define(temp);
245 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
246 preproc->pre_define(temp);
247 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
248 preproc->pre_define(temp);
249 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
250 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800251 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700252
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800253 if (gm_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400254 posix_time = posix_mktime(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800255 else if (lt_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400256 posix_time = posix_mktime(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400258 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259
260 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400261 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
262 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800263 }
264}
265
266static void define_macros_late(void)
267{
268 char temp[128];
269
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400270 /*
271 * In case if output format is defined by alias
272 * we have to put shortname of the alias itself here
273 * otherwise ABI backward compatibility gets broken.
274 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400275 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400276 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400277 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800278}
279
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700280static void emit_dependencies(StrList *list)
281{
282 FILE *deps;
283 int linepos, len;
284 StrList *l, *nl;
285
286 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400287 deps = fopen(depend_file, "w");
288 if (!deps) {
289 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
290 "unable to write dependency file `%s'", depend_file);
291 return;
292 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700293 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400294 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700295 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700296
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700297 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400298 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700299 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400300 len = strlen(file);
301 if (linepos + len > 62 && linepos > 1) {
302 fprintf(deps, " \\\n ");
303 linepos = 1;
304 }
305 fprintf(deps, " %s", file);
306 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700307 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700308 }
309 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700310
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400311 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400312 if (depend_emit_phony)
313 fprintf(deps, "%s:\n\n", l->str);
314 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700315 }
316
317 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400318 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700319}
320
H. Peter Anvin038d8612007-04-12 16:54:50 +0000321int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000322{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700323 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700324
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800325 time(&official_compile_time);
326
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400327 iflag_set(&cpu, IF_PLEVEL);
328 iflag_set(&cmd_cpu, IF_PLEVEL);
329
Charles Crayne2581c862008-09-10 19:21:52 -0700330 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700331 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400332 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000333
H. Peter Anvin97e15752007-09-25 08:47:47 -0700334 error_file = stderr;
335
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700336 tolower_init();
337
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400338 nasm_init_malloc_error();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000340 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000341
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000343 operating_mode = op_normal;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000344
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000345 seg_init();
346
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800347 /* Define some macros dependent on the runtime, but not
348 on the command line. */
349 define_macros_early();
350
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000351 parse_cmdline(argc, argv);
352
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 if (terminate_after_phase) {
354 if (want_usage)
355 usage();
356 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000357 }
358
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000359 /* If debugging info is disabled, suppress any debug calls */
360 if (!using_debug_info)
361 ofmt->current_dfmt = &null_debug_form;
362
H. Peter Anvin76690a12002-04-30 20:52:49 +0000363 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400364 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700365 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000367
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000368 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800369 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000370
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400371 depend_ptr = (depend_file || (operating_mode == op_depend)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700372 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400373 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700374
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 switch (operating_mode) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000376 case op_depend:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700379
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400380 if (depend_missing_ok)
381 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700382
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700383 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400385 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 while ((line = preproc->getline()))
388 nasm_free(line);
389 preproc->cleanup(0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000390 }
391 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000392
393 case op_preprocess:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000395 char *line;
396 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000397 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000399
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 if (*outname) {
401 ofile = fopen(outname, "w");
402 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400403 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 "unable to open output file `%s'",
405 outname);
406 } else
407 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000408
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700409 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000410
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400411 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700412 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400413 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700414
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 while ((line = preproc->getline())) {
416 /*
417 * We generate %line directives if needed for later programs
418 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000419 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 int altline = src_get(&linnum, &file_name);
421 if (altline) {
422 if (altline == 1 && lineinc == 1)
423 nasm_fputs("", ofile);
424 else {
425 lineinc = (altline != -1 || lineinc != 1);
426 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000427 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 file_name);
429 }
430 prior_linnum = linnum;
431 }
432 nasm_fputs(line, ofile);
433 nasm_free(line);
434 }
435 nasm_free(file_name);
436 preproc->cleanup(0);
437 if (ofile)
438 fclose(ofile);
439 if (ofile && terminate_after_phase)
440 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400441 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 }
443 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000444
445 case op_normal:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 {
447 /*
448 * We must call ofmt->filename _anyway_, even if the user
449 * has specified their own output file, because some
450 * formats (eg OBJ and COFF) use ofmt->filename to find out
451 * the name of the input file and then put that inside the
452 * file.
453 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400454 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455
H. Peter Anvin0cba1072009-07-05 14:45:12 -0700456 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 if (!ofile) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400458 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 "unable to open output file `%s'", outname);
460 }
461
462 /*
463 * We must call init_labels() before ofmt->init() since
464 * some object formats will want to define labels in their
465 * init routines. (eg OS/2 defines the FLAT group)
466 */
467 init_labels();
468
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400469 ofmt->init();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400470 dfmt = ofmt->current_dfmt;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400471 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700473 assemble_file(inname, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474
475 if (!terminate_after_phase) {
476 ofmt->cleanup(using_debug_info);
477 cleanup_labels();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400478 fflush(ofile);
479 if (ferror(ofile)) {
480 nasm_error(ERR_NONFATAL|ERR_NOFILE,
481 "write error on output file `%s'", outname);
482 }
483 }
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200484
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400485 if (ofile) {
486 fclose(ofile);
487 if (terminate_after_phase)
488 remove(outname);
489 ofile = NULL;
490 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 }
492 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000494
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700495 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400496 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700497
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000498 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000500
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 raa_free(offsets);
502 saa_free(forwrefs);
503 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000504 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700506 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000507}
508
H. Peter Anvineba20a72002-04-30 20:53:55 +0000509/*
510 * Get a parameter for a command line option.
511 * First arg must be in the form of e.g. -f...
512 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800513static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800515 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400516 if (p[2]) /* the parameter's in the option */
517 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800519 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000521 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400522 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000524 return NULL;
525}
526
H. Peter Anvindc242712007-11-18 11:55:10 -0800527/*
528 * Copy a filename
529 */
530static void copy_filename(char *dst, const char *src)
531{
532 size_t len = strlen(src);
533
534 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400535 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
536 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800537 }
538 strncpy(dst, src, FILENAME_MAX);
539}
540
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700541/*
542 * Convert a string to Make-safe form
543 */
544static char *quote_for_make(const char *str)
545{
546 const char *p;
547 char *os, *q;
548
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400549 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700550 size_t nbs = 0;
551
552 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400553 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700554
555 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400556 switch (*p) {
557 case ' ':
558 case '\t':
559 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
560 n += nbs + 2;
561 nbs = 0;
562 break;
563 case '$':
564 case '#':
565 nbs = 0;
566 n += 2;
567 break;
568 case '\\':
569 nbs++;
570 n++;
571 break;
572 default:
573 nbs = 0;
574 n++;
575 break;
576 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700577 }
578
579 /* Convert N backslashes at the end of filename to 2N backslashes */
580 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400581 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700582
583 os = q = nasm_malloc(n);
584
585 nbs = 0;
586 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400587 switch (*p) {
588 case ' ':
589 case '\t':
590 while (nbs--)
591 *q++ = '\\';
592 *q++ = '\\';
593 *q++ = *p;
594 break;
595 case '$':
596 *q++ = *p;
597 *q++ = *p;
598 nbs = 0;
599 break;
600 case '#':
601 *q++ = '\\';
602 *q++ = *p;
603 nbs = 0;
604 break;
605 case '\\':
606 *q++ = *p;
607 nbs++;
608 break;
609 default:
610 *q++ = *p;
611 nbs = 0;
612 break;
613 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700614 }
615 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400616 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700617
618 *q = '\0';
619
620 return os;
621}
622
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000624 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000626};
627
628#define OPT_PREFIX 0
629#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630struct textargs textopts[] = {
631 {"prefix", OPT_PREFIX},
632 {"postfix", OPT_POSTFIX},
633 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000634};
635
H. Peter Anvin423e3812007-11-15 17:12:29 -0800636static bool stopoptions = false;
637static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000638{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000639 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800640 int i;
641 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700642 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000643
644 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800645 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000646
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400648 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
649 /* These parameters take values */
650 if (!(param = get_param(p, q, &advance)))
651 return advance;
652 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800653
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 switch (p[1]) {
655 case 's':
656 error_file = stdout;
657 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700658
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400659 case 'o': /* output file */
660 copy_filename(outname, param);
661 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700662
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400663 case 'f': /* output format */
664 ofmt = ofmt_find(param, &ofmt_alias);
665 if (!ofmt) {
666 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
667 "unrecognised output format `%s' - "
668 "use -hf for a list", param);
669 }
670 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700671
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400672 case 'O': /* Optimization level */
673 {
674 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700675
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400676 if (!*param) {
677 /* Naked -O == -Ox */
678 optimizing = MAX_OPTIMIZE;
679 } else {
680 while (*param) {
681 switch (*param) {
682 case '0': case '1': case '2': case '3': case '4':
683 case '5': case '6': case '7': case '8': case '9':
684 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700685
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400686 /* -O0 -> optimizing == -1, 0.98 behaviour */
687 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
688 if (opt < 2)
689 optimizing = opt - 1;
690 else
691 optimizing = opt;
692 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700693
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400694 case 'v':
695 case '+':
696 param++;
697 opt_verbose_info = true;
698 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700699
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400700 case 'x':
701 param++;
702 optimizing = MAX_OPTIMIZE;
703 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700704
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400705 default:
706 nasm_error(ERR_FATAL,
707 "unknown optimization option -O%c\n",
708 *param);
709 break;
710 }
711 }
712 if (optimizing > MAX_OPTIMIZE)
713 optimizing = MAX_OPTIMIZE;
714 }
715 break;
716 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800717
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400718 case 'p': /* pre-include */
719 case 'P':
720 preproc->pre_include(param);
721 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800722
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400723 case 'd': /* pre-define */
724 case 'D':
725 preproc->pre_define(param);
726 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800727
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400728 case 'u': /* un-define */
729 case 'U':
730 preproc->pre_undefine(param);
731 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800732
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400733 case 'i': /* include search path */
734 case 'I':
735 preproc->include_path(param);
736 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800737
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400738 case 'l': /* listing file */
739 copy_filename(listname, param);
740 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800741
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400742 case 'Z': /* error messages file */
743 copy_filename(errname, param);
744 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800745
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400746 case 'F': /* specify debug format */
747 ofmt->current_dfmt = dfmt_find(ofmt, param);
748 if (!ofmt->current_dfmt) {
749 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
750 "unrecognized debug format `%s' for"
751 " output format `%s'",
752 param, ofmt->shortname);
753 }
754 using_debug_info = true;
755 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800756
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400757 case 'X': /* specify error reporting format */
758 if (nasm_stricmp("vc", param) == 0)
759 nasm_set_verror(nasm_verror_vc);
760 else if (nasm_stricmp("gnu", param) == 0)
761 nasm_set_verror(nasm_verror_gnu);
762 else
763 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
764 "unrecognized error reporting format `%s'",
765 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800767
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700769 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800771
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 case 'h':
773 printf
774 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
775 "[-l listfile]\n"
776 " [options...] [--] filename\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700777 " or nasm -v for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400779 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700781 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700783 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400784 " -MG d:o, missing files assumed generated\n"
785 " -MF <file> set Makefile dependency file\n"
786 " -MD <file> assemble and generate dependencies\n"
787 " -MT <file> dependency target name\n"
788 " -MQ <file> dependency target name (quoted)\n"
789 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700790 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 " -s redirect error messages to stdout\n\n"
792 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400793 " -o outfile write output to an outfile\n\n"
794 " -f format select an output format\n\n"
795 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 " -I<path> adds a pathname to the include file path\n");
797 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700798 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400799 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400800 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400801 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 " -P<file> pre-includes a file\n"
803 " -D<macro>[=<value>] pre-defines a macro\n"
804 " -U<macro> undefines a macro\n"
805 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800806 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400807 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400808 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400809 "--prefix,--postfix\n"
810 " this options prepend or append the given argument to all\n"
811 " extern and global variables\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800812 "Warnings:\n");
813 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700815 warnings[i].name, warnings[i].help,
816 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 printf
818 ("\nresponse files should contain command line parameters"
819 ", one per line.\n");
820 if (p[2] == 'f') {
821 printf("\nvalid output formats for -f are"
822 " (`*' denotes default):\n");
823 ofmt_list(ofmt, stdout);
824 } else {
825 printf("\nFor a list of valid output formats, use -hf.\n");
826 printf("For a list of debug formats, use -f <form> -y.\n");
827 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400828 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800830
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 case 'y':
832 printf("\nvalid debug formats for '%s' output format are"
833 " ('*' denotes default):\n", ofmt->shortname);
834 dfmt_list(ofmt, stdout);
835 exit(0);
836 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700839 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 case 'v':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400843 printf("NASM version %s compiled on %s%s\n",
844 nasm_version, nasm_date, nasm_compile_options);
845 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800847
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400848 case 'e': /* preprocess only */
849 case 'E':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 operating_mode = op_preprocess;
851 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800852
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400853 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400854 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800856
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400857 case 'W':
858 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
859 do_warn = false;
860 param += 3;
861 } else {
862 do_warn = true;
863 }
864 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800865
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800867 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400868 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400870 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400872 do_warn = (param[0] == '+');
873 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400874
875set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400876 for (i = 0; i <= ERR_WARN_MAX; i++) {
877 if (!nasm_stricmp(param, warnings[i].name))
878 break;
879 }
880 if (i <= ERR_WARN_MAX) {
881 warning_on_global[i] = do_warn;
882 } else if (!nasm_stricmp(param, "all")) {
883 for (i = 1; i <= ERR_WARN_MAX; i++)
884 warning_on_global[i] = do_warn;
885 } else if (!nasm_stricmp(param, "none")) {
886 for (i = 1; i <= ERR_WARN_MAX; i++)
887 warning_on_global[i] = !do_warn;
888 } else {
889 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
890 "invalid warning `%s'", param);
891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400895 switch (p[2]) {
896 case 0:
897 operating_mode = op_depend;
898 break;
899 case 'G':
900 operating_mode = op_depend;
901 depend_missing_ok = true;
902 break;
903 case 'P':
904 depend_emit_phony = true;
905 break;
906 case 'D':
907 depend_file = q;
908 advance = true;
909 break;
910 case 'T':
911 depend_target = q;
912 advance = true;
913 break;
914 case 'Q':
915 depend_target = quote_for_make(q);
916 advance = true;
917 break;
918 default:
919 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
920 "unknown dependency option `-M%c'", p[2]);
921 break;
922 }
923 if (advance && (!q || !q[0])) {
924 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
925 "option `-M%c' requires a parameter", p[2]);
926 break;
927 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000929
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 case '-':
931 {
932 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000933
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 if (p[2] == 0) { /* -- => stop processing options */
935 stopoptions = 1;
936 break;
937 }
938 for (s = 0; textopts[s].label; s++) {
939 if (!nasm_stricmp(p + 2, textopts[s].label)) {
940 break;
941 }
942 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 switch (s) {
945
946 case OPT_PREFIX:
947 case OPT_POSTFIX:
948 {
949 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400950 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 ERR_USAGE,
952 "option `--%s' requires an argument",
953 p + 2);
954 break;
955 } else {
956 advance = 1, param = q;
957 }
958
959 if (s == OPT_PREFIX) {
960 strncpy(lprefix, param, PREFIX_MAX - 1);
961 lprefix[PREFIX_MAX - 1] = 0;
962 break;
963 }
964 if (s == OPT_POSTFIX) {
965 strncpy(lpostfix, param, POSTFIX_MAX - 1);
966 lpostfix[POSTFIX_MAX - 1] = 0;
967 break;
968 }
969 break;
970 }
971 default:
972 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400973 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 "unrecognised option `--%s'", p + 2);
975 break;
976 }
977 }
978 break;
979 }
980
981 default:
982 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400983 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 "unrecognised option `-%c'", p[1]);
985 break;
986 }
987 } else {
988 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400989 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800991 } else {
992 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000994 }
995
996 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000997}
998
H. Peter Anvineba20a72002-04-30 20:53:55 +0000999#define ARG_BUF_DELTA 128
1000
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001002{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001003 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001004 int bufsize, prevargsize;
1005
1006 bufsize = prevargsize = ARG_BUF_DELTA;
1007 buffer = nasm_malloc(ARG_BUF_DELTA);
1008 prevarg = nasm_malloc(ARG_BUF_DELTA);
1009 prevarg[0] = '\0';
1010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 p = buffer;
1013 while (1) { /* Loop to handle long lines */
1014 q = fgets(p, bufsize - (p - buffer), rfile);
1015 if (!q)
1016 break;
1017 p += strlen(p);
1018 if (p > buffer && p[-1] == '\n')
1019 break;
1020 if (p - buffer > bufsize - 10) {
1021 int offset;
1022 offset = p - buffer;
1023 bufsize += ARG_BUF_DELTA;
1024 buffer = nasm_realloc(buffer, bufsize);
1025 p = buffer + offset;
1026 }
1027 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 if (!q && p == buffer) {
1030 if (prevarg[0])
1031 process_arg(prevarg, NULL);
1032 nasm_free(buffer);
1033 nasm_free(prevarg);
1034 return;
1035 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001036
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 /*
1038 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1039 * them are present at the end of the line.
1040 */
1041 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001042
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001043 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001045
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001046 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001047
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 if (process_arg(prevarg, p))
1049 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001050
Charles Crayne192d5b52007-10-18 19:02:42 -07001051 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 prevargsize += ARG_BUF_DELTA;
1053 prevarg = nasm_realloc(prevarg, prevargsize);
1054 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001055 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056 }
1057}
1058
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001059/* Function to process args from a string of args, rather than the
1060 * argv array. Used by the environment variable and response file
1061 * processing.
1062 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001063static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001065 char *p, *q, *arg, *prevarg;
1066 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001067
1068 p = args;
1069 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001071 arg = NULL;
1072 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 q = p;
1074 while (*p && *p != separator)
1075 p++;
1076 while (*p == separator)
1077 *p++ = '\0';
1078 prevarg = arg;
1079 arg = q;
1080 if (process_arg(prevarg, arg))
1081 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001082 }
1083 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001085}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001086
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001087static void process_response_file(const char *file)
1088{
1089 char str[2048];
1090 FILE *f = fopen(file, "r");
1091 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001092 perror(file);
1093 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001094 }
1095 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001096 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001097 }
1098 fclose(f);
1099}
1100
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001101static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001102{
1103 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001104 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001105 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001106
Charles Craynefcce07f2007-09-30 22:15:36 -07001107 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001108
H. Peter Anvin972079f2008-09-30 17:01:23 -07001109 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001110 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001111
1112 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001113 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001114 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001115 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001116 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 envcopy = nasm_strdup(envreal);
1118 process_args(envcopy);
1119 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001120 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001121
1122 /*
1123 * Now process the actual command line.
1124 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001126 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 argv++;
1128 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001129 /*
1130 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 * arguments like the environment variable. This allows us
1132 * to have multiple arguments on a single line, which is
1133 * different to the -@resp file processing below for regular
1134 * NASM.
1135 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001136 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 argc--;
1138 argv++;
1139 }
1140 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001141 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001142 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001143 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001144 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 process_respfile(rfile);
1146 fclose(rfile);
1147 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001148 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 "unable to open response file `%s'", p);
1150 }
1151 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001152 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1153 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001154 }
1155
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001156 /*
1157 * Look for basic command line typos. This definitely doesn't
1158 * catch all errors, but it might help cases of fumbled fingers.
1159 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001160 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001161 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001162 "no input file specified");
1163 else if (!strcmp(inname, errname) ||
1164 !strcmp(inname, outname) ||
1165 !strcmp(inname, listname) ||
1166 (depend_file && !strcmp(inname, depend_file)))
1167 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1168 "file `%s' is both input and output file",
1169 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001170
H. Peter Anvin70653092007-10-19 14:42:29 -07001171 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001172 error_file = fopen(errname, "w");
1173 if (!error_file) {
1174 error_file = stderr; /* Revert to default! */
1175 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1176 "cannot open file `%s' for error messages",
1177 errname);
1178 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001179 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001180}
1181
H. Peter Anvin70055962007-10-11 00:05:31 -07001182static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001183
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001184static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001185{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001186 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001188 int i, validid;
1189 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001190 int32_t seg;
1191 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001192 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001194 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001195
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001196 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001197 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001199
Charles Craynec1905c22008-09-11 18:54:06 -07001200 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001201 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001202 int pass1, pass2;
1203 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001204
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001205 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001206 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1207 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001208
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001209 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210
Keith Kaniosb7a89542007-04-12 02:40:54 +00001211 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 cpu = cmd_cpu;
1213 if (pass0 == 2) {
1214 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001215 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001217 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001218 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001220 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001221 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 saa_rewind(forwrefs);
1223 forwref = saa_rstruct(forwrefs);
1224 raa_free(offsets);
1225 offsets = raa_init();
1226 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001227 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001228 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001229 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001232 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001233 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001234 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001235
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001237 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001239
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001240 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001241 * Here we parse our directives; this is not handled by the
1242 * 'real' parser. This really should be a separate function.
1243 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001245 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001246 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001247 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001248
H. Peter Anvin70055962007-10-11 00:05:31 -07001249 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001250 case D_SEGMENT: /* [SEGMENT n] */
1251 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 seg = ofmt->section(value, pass2, &sb);
1253 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001254 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001255 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 value);
1257 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001258 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 location.segment = seg;
1260 }
1261 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001262 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001263 if (*value) {
1264 stdscan_reset();
1265 stdscan_set(value);
1266 tokval.t_type = TOKEN_INVALID;
1267 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1268 if (e) {
1269 unsigned int align = (unsigned int)e->value;
1270 if ((uint64_t)e->value > 0x7fffffff) {
1271 /*
1272 * FIXME: Please make some sane message here
1273 * ofmt should have some 'check' method which
1274 * would report segment alignment bounds.
1275 */
1276 nasm_error(ERR_FATAL,
1277 "incorrect segment alignment `%s'", value);
1278 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001279 nasm_error(ERR_NONFATAL,
1280 "segment alignment `%s' is not power of two",
1281 value);
1282 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001283 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001284 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001285 }
1286 }
1287 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001288 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 if (*value == '$')
1290 value++; /* skip initial $ if present */
1291 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001292 q = value;
1293 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001295 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 *q++ = '\0';
1297 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001298 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001299 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001300 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001301 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001302 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001303 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001306 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308 }
1309 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001310 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 "identifier expected after EXTERN");
1312 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001313 }
1314 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 *q++ = '\0';
1316 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001317 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 special = NULL;
1319 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1320 int temp = pass0;
1321 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001322 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001324 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001326 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 } /* else pass0 == 1 */
1328 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001329 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001330 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001332 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 if (*value == '$')
1334 value++; /* skip initial $ if present */
1335 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001336 q = value;
1337 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001339 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 *q++ = '\0';
1341 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001344 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001345 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001346 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001347 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001348 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001350 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 }
1353 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001354 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 "identifier expected after GLOBAL");
1356 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001357 }
1358 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 *q++ = '\0';
1360 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001361 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001363 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 } /* pass == 1 */
1365 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001366 case D_COMMON: /* [COMMON symbol size:special] */
1367 {
1368 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001369
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (*value == '$')
1371 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001372 p = value;
1373 validid = true;
1374 if (!isidstart(*p))
1375 validid = false;
1376 while (*p && !nasm_isspace(*p)) {
1377 if (!isidchar(*p))
1378 validid = false;
1379 p++;
1380 }
1381 if (!validid) {
1382 nasm_error(ERR_NONFATAL,
1383 "identifier expected after COMMON");
1384 break;
1385 }
1386 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001387 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001388 q = p;
1389 while (*q && *q != ':')
1390 q++;
1391 if (*q == ':') {
1392 *q++ = '\0';
1393 special = q;
1394 } else {
1395 special = NULL;
1396 }
1397 size = readnum(p, &rn_error);
1398 if (rn_error) {
1399 nasm_error(ERR_NONFATAL,
1400 "invalid size specified"
1401 " in COMMON declaration");
1402 break;
1403 }
1404 } else {
1405 nasm_error(ERR_NONFATAL,
1406 "no size specified in"
1407 " COMMON declaration");
1408 break;
1409 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001410
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001411 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001412 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001413 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001414 if (special)
1415 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 }
1417 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001418 }
1419 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001421 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 tokval.t_type = TOKEN_INVALID;
1423 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001424 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 if (e) {
1426 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001427 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 1 ? ERR_NONFATAL : ERR_PANIC,
1429 "cannot use non-relocatable expression as "
1430 "ABSOLUTE address");
1431 else {
1432 abs_seg = reloc_seg(e);
1433 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001434 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001435 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 abs_offset = 0x100; /* don't go near zero in case of / */
1437 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001438 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001440 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 location.segment = NO_SEG;
1442 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001443 case D_DEBUG: /* [DEBUG] */
1444 {
1445 char debugid[128];
1446 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001447
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 p = value;
1449 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001450 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001451 if (!isidstart(*p)) {
1452 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001453 } else {
1454 while (*p && !nasm_isspace(*p)) {
1455 if (q >= debugid + sizeof debugid - 1) {
1456 overlong = true;
1457 break;
1458 }
1459 if (!isidchar(*p))
1460 badid = true;
1461 *q++ = *p++;
1462 }
1463 *q = 0;
1464 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001465 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001466 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1467 "identifier expected after DEBUG");
1468 break;
1469 }
1470 if (overlong) {
1471 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1472 "DEBUG identifier too long");
1473 break;
1474 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001475 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001476 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001477 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001479 }
1480 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001481 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001482 switch(*value) {
1483 case '-': validid = 0; value++; break;
1484 case '+': validid = 1; value++; break;
1485 case '*': validid = 2; value++; break;
1486 default: validid = 1; break;
1487 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001489 for (i = 1; i <= ERR_WARN_MAX; i++)
1490 if (!nasm_stricmp(value, warnings[i].name))
1491 break;
1492 if (i <= ERR_WARN_MAX) {
1493 switch(validid) {
1494 case 0:
1495 warning_on[i] = false;
1496 break;
1497 case 1:
1498 warning_on[i] = true;
1499 break;
1500 case 2:
1501 warning_on[i] = warning_on_global[i];
1502 break;
1503 }
1504 } else
1505 nasm_error(ERR_NONFATAL,
1506 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001508 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 cpu = get_cpu(value);
1510 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001511 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001512 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 if (*value == '+') {
1514 user_nolist = 0;
1515 } else {
1516 if (*value == '-') {
1517 user_nolist = 1;
1518 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001519 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 }
1521 }
1522 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001523 case D_DEFAULT: /* [DEFAULT] */
1524 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001525 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001526 tokval.t_type = TOKEN_INVALID;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001527 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1528 switch ((int)tokval.t_integer) {
1529 case S_REL:
1530 globalrel = 1;
1531 break;
1532 case S_ABS:
1533 globalrel = 0;
1534 break;
1535 default:
1536 err = 1;
1537 break;
1538 }
1539 } else {
1540 err = 1;
1541 }
1542 break;
1543 case D_FLOAT:
1544 if (float_option(value)) {
1545 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1546 "unknown 'float' directive: %s",
1547 value);
1548 }
1549 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001551 if (ofmt->directive(d, value, pass2))
1552 break;
1553 /* else fall through */
1554 case D_unknown:
1555 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1556 "unrecognised directive [%s]",
1557 directive);
1558 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001560 if (err) {
1561 nasm_error(ERR_NONFATAL,
1562 "invalid parameter to [%s] directive",
1563 directive);
1564 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001566 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567
Charles Crayne2581c862008-09-10 19:21:52 -07001568 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001570 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001572 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 forwref = saa_rstruct(forwrefs);
1574 } while (forwref != NULL
1575 && forwref->lineno == globallineno);
1576 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001577 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001578
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001579 if (output_ins.forw_ref) {
1580 if (passn == 1) {
1581 for (i = 0; i < output_ins.operands; i++) {
1582 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1583 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1584 fwinf->lineno = globallineno;
1585 fwinf->operand = i;
1586 }
1587 }
1588 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001589 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001591
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 /* forw_ref */
1593 if (output_ins.opcode == I_EQU) {
1594 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001595 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 * Special `..' EQUs get processed in pass two,
1597 * except `..@' macro-processor EQUs which are done
1598 * in the normal place.
1599 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001600 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001601 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001604 else if (output_ins.label[0] != '.' ||
1605 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 output_ins.label[2] == '@') {
1607 if (output_ins.operands == 1 &&
1608 (output_ins.oprs[0].type & IMMEDIATE) &&
1609 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001610 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 def_label(output_ins.label,
1612 output_ins.oprs[0].segment,
1613 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001614 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001616 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001618 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001620 && (output_ins.oprs[1].type & IMMEDIATE)
1621 && output_ins.oprs[1].segment == NO_SEG
1622 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001624 output_ins.oprs[0].offset | SEG_ABS,
1625 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001626 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001628 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001630 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001631 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001632 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 * Special `..' EQUs get processed here, except
1634 * `..@' macro processor EQUs which are done above.
1635 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001636 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 output_ins.label[1] == '.' &&
1638 output_ins.label[2] != '@') {
1639 if (output_ins.operands == 1 &&
1640 (output_ins.oprs[0].type & IMMEDIATE)) {
1641 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001642 output_ins.oprs[0].segment,
1643 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001644 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001646 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001648 && output_ins.oprs[0].segment == NO_SEG
1649 && (output_ins.oprs[1].type & IMMEDIATE)
1650 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001652 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001654 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001656 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001659 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001661
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001663
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001664 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001665 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001666
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 /* if (using_debug_info) && output_ins.opcode != -1) */
1668 if (using_debug_info)
1669 { /* fbk 03/25/01 */
1670 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001671 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 TYS_ELEMENTS(output_ins.operands);
1673 switch (output_ins.opcode) {
1674 case I_RESB:
1675 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001676 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 break;
1678 case I_RESW:
1679 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001680 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 break;
1682 case I_RESD:
1683 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001684 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 break;
1686 case I_RESQ:
1687 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001688 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 break;
1690 case I_REST:
1691 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001692 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001694 case I_RESO:
1695 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001696 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001697 break;
1698 case I_RESY:
1699 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001700 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001701 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 case I_DB:
1703 typeinfo |= TY_BYTE;
1704 break;
1705 case I_DW:
1706 typeinfo |= TY_WORD;
1707 break;
1708 case I_DD:
1709 if (output_ins.eops_float)
1710 typeinfo |= TY_FLOAT;
1711 else
1712 typeinfo |= TY_DWORD;
1713 break;
1714 case I_DQ:
1715 typeinfo |= TY_QWORD;
1716 break;
1717 case I_DT:
1718 typeinfo |= TY_TBYTE;
1719 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001720 case I_DO:
1721 typeinfo |= TY_OWORD;
1722 break;
1723 case I_DY:
1724 typeinfo |= TY_YWORD;
1725 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 default:
1727 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001728
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001730
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001731 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001732 }
1733 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001735 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001736 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001737 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 * else l == -1 => invalid instruction, which will be
1739 * flagged as an error on pass 2
1740 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001741
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001742 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001744 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001746 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001747
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 }
1749 } /* not an EQU */
1750 cleanup_insn(&output_ins);
1751 }
1752 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001753 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001755
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001756 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001757 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001758 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001759
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 if (pass1 == 1)
1761 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001762
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001763 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001765 } else if (global_offset_changed &&
1766 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001767 prev_offset_changed = global_offset_changed;
1768 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001769 } else {
1770 stall_count++;
1771 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001772
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001773 if (terminate_after_phase)
1774 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001775
1776 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001777 /* We get here if the labels don't converge
1778 * Example: FOO equ FOO + 1
1779 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001780 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001781 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001782 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001783 nasm_error(ERR_NONFATAL,
1784 "Possible causes: recursive EQUs, macro abuse.");
1785 break;
1786 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001787 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001788
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 preproc->cleanup(0);
1790 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001791 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001792 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001793 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1794 }
1795}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001796
H. Peter Anvin70055962007-10-11 00:05:31 -07001797static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001798{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001799 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001801 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001802
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001803 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001805 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001806 q = strchr(buf, ']');
1807 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001808 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001809
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001810 /* stip off the comments */
1811 p = strchr(buf, ';');
1812 if (p) {
1813 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001814 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001815 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001816 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001817
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001818 /* no brace, no trailing spaces */
1819 *q = '\0';
1820 nasm_zap_spaces_rev(--q);
1821
1822 /* directive */
1823 p = nasm_skip_spaces(++buf);
1824 q = nasm_skip_word(p);
1825 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001826 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001827 *q = '\0';
1828 *directive = p;
1829
1830 /* and value finally */
1831 p = nasm_skip_spaces(++q);
1832 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001833
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001834 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001835}
1836
Ed Berosetfa771012002-06-09 20:56:40 +00001837/**
1838 * gnu style error reporting
1839 * This function prints an error message to error_file in the
1840 * style used by GNU. An example would be:
1841 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001842 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001843 * which the error occurs (or is detected) and "error:" is one of
1844 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001845 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001846 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001847 *
1848 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001849 * @param fmt the printf style format string
1850 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001851static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001852{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001853 char *currentfile = NULL;
1854 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001855
Ed Berosetfa771012002-06-09 20:56:40 +00001856 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001858
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001859 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001861
1862 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001863 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1864 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001865 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001866 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001867 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001868
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001869 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001870}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001871
Ed Berosetfa771012002-06-09 20:56:40 +00001872/**
1873 * MS style error reporting
1874 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001875 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001876 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001877 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001878 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001879 * which the error occurs (or is detected) and "error:" is one of
1880 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001881 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001882 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001883 *
1884 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001885 * @param fmt the printf style format string
1886 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001887static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001888{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001889 char *currentfile = NULL;
1890 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001891
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 if (is_suppressed_warning(severity))
1893 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001894
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001895 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001897
1898 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001899 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001901 } else {
1902 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001903 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001904
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001905 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001906}
1907
1908/**
1909 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001910 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001911 * not in pass 1
1912 *
1913 * @param severity the severity of the warning or error
1914 * @return true if we should abort error/warning printing
1915 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001916static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001917{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001918 /* Not a warning at all */
1919 if ((severity & ERR_MASK) != ERR_WARNING)
1920 return false;
1921
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001922 /* See if it's a pass-one only warning and we're not in pass one. */
1923 if (((severity & ERR_PASS1) && pass0 != 1) ||
1924 ((severity & ERR_PASS2) && pass0 != 2))
1925 return true;
1926
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001927 /* Might be a warning but suppresed explicitly */
1928 if (severity & ERR_WARN_MASK)
1929 return !warning_on[WARN_IDX(severity)];
1930 else
1931 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001932}
1933
1934/**
1935 * common error reporting
1936 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001937 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001938 * specific error message to error_file and may or may not return. It
1939 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001940 *
1941 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001942 * @param fmt the printf style format string
1943 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001944static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001945{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001946 char msg[1024];
1947 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001948
H. Peter Anvin7df04172008-06-10 18:27:38 -07001949 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001951 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 break;
1953 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001954 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 break;
1956 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001957 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 break;
1959 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001960 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001961 break;
1962 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001963 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001964 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001965 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001966 pfx = "";
1967 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001968 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001969
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001970 vsnprintf(msg, sizeof msg, fmt, args);
1971
1972 fprintf(error_file, "%s%s\n", pfx, msg);
1973
1974 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001975 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001976
1977 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001978 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001979
1980 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 case ERR_DEBUG:
1982 /* no further action, by definition */
1983 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001984 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001985 /* Treat warnings as errors */
1986 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1987 terminate_after_phase = true;
1988 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001990 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 break;
1992 case ERR_FATAL:
1993 if (ofile) {
1994 fclose(ofile);
1995 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001996 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 }
1998 if (want_usage)
1999 usage();
2000 exit(1); /* instantly die */
2001 break; /* placate silly compilers */
2002 case ERR_PANIC:
2003 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002004 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 exit(3);
2006 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002007 }
2008}
2009
H. Peter Anvin734b1882002-04-30 21:01:08 +00002010static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002011{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002012 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002013}
2014
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002015static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002016{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002017 iflag_t r;
2018
2019 iflag_clear_all(&r);
2020
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002022 iflag_set(&r, IF_8086);
2023 else if (!strcmp(value, "186"))
2024 iflag_set(&r, IF_186);
2025 else if (!strcmp(value, "286"))
2026 iflag_set(&r, IF_286);
2027 else if (!strcmp(value, "386"))
2028 iflag_set(&r, IF_386);
2029 else if (!strcmp(value, "486"))
2030 iflag_set(&r, IF_486);
2031 else if (!strcmp(value, "586") ||
2032 !nasm_stricmp(value, "pentium"))
2033 iflag_set(&r, IF_PENT);
2034 else if (!strcmp(value, "686") ||
2035 !nasm_stricmp(value, "ppro") ||
2036 !nasm_stricmp(value, "pentiumpro") ||
2037 !nasm_stricmp(value, "p2"))
2038 iflag_set(&r, IF_P6);
2039 else if (!nasm_stricmp(value, "p3") ||
2040 !nasm_stricmp(value, "katmai"))
2041 iflag_set(&r, IF_KATMAI);
2042 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2043 !nasm_stricmp(value, "willamette"))
2044 iflag_set(&r, IF_WILLAMETTE);
2045 else if (!nasm_stricmp(value, "prescott"))
2046 iflag_set(&r, IF_PRESCOTT);
2047 else if (!nasm_stricmp(value, "x64") ||
2048 !nasm_stricmp(value, "x86-64"))
2049 iflag_set(&r, IF_X86_64);
2050 else if (!nasm_stricmp(value, "ia64") ||
2051 !nasm_stricmp(value, "ia-64") ||
2052 !nasm_stricmp(value, "itanium")||
2053 !nasm_stricmp(value, "itanic") ||
2054 !nasm_stricmp(value, "merced"))
2055 iflag_set(&r, IF_IA64);
2056 else {
2057 iflag_set(&r, IF_PLEVEL);
2058 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2059 "unknown 'cpu' type");
2060 }
2061 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002062}
2063
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002064static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002065{
2066 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002067
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 if ((i = atoi(value)) == 16)
2069 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002070 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002071 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002072 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002073 "cannot specify 32-bit segment on processor below a 386");
2074 i = 16;
2075 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002076 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002077 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002078 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002079 "cannot specify 64-bit segment on processor below an x86-64");
2080 i = 16;
2081 }
2082 if (i != maxbits) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002083 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002084 "%s output format does not support 64-bit code",
2085 ofmt->shortname);
2086 i = 16;
2087 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002088 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002089 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002090 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 value);
2092 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002093 }
2094 return i;
2095}