blob: 126f2712fc08c26062978ebce45a73f211dcdcdc [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"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000063
H. Peter Anvin31387b22010-07-15 18:28:52 -070064/*
65 * This is the maximum number of optimization passes to do. If we ever
66 * find a case where the optimizer doesn't naturally converge, we might
67 * have to drop this value so the assembler doesn't appear to just hang.
68 */
69#define MAX_OPTIMIZE (INT_MAX >> 1)
70
H. Peter Anvine2c80182005-01-15 22:15:51 +000071struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000072 int lineno;
73 int operand;
74};
75
Keith Kaniosa6dfa782007-04-13 16:47:53 +000076static int get_bits(char *value);
77static uint32_t get_cpu(char *cpu_str);
78static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070079static void assemble_file(char *, StrList **);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040080static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
81static void nasm_verror_vc(int severity, const char *fmt, va_list args);
82static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvin2b046cf2008-01-22 14:08:36 -080083static bool is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
85
John Coffman0efaec92002-05-26 19:20:08 +000086static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070087bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080088int pass0, passn;
Keith Kaniosb7a89542007-04-12 02:40:54 +000089int maxbits = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000090int globalrel = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000091
H. Peter Anvin9bd15062009-07-18 21:07:17 -040092static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080093
Keith Kaniosa6dfa782007-04-13 16:47:53 +000094static char inname[FILENAME_MAX];
95static char outname[FILENAME_MAX];
96static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -070097static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +000098static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000099/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400100struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400101struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400102const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000103
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000105
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400106FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700107int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
108static int sb, cmd_sb = 16; /* by default */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000109static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
110static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
Charles Craynec1905c22008-09-11 18:54:06 -0700111int64_t global_offset_changed; /* referenced in labels.c */
112int64_t prev_offset_changed;
113int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000114
H. Peter Anvin12e46512007-10-03 21:30:57 -0700115static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000117int32_t abs_seg; /* ABSOLUTE segment basis */
118int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000119
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000121
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700123static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400125static struct preproc_ops *preproc;
126
H. Peter Anvin620515a2002-04-30 20:57:38 +0000127enum op_type {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000128 op_normal, /* Preprocess and assemble */
129 op_preprocess, /* Preprocess only */
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700130 op_depend, /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000131};
132static enum op_type operating_mode;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700133/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700134static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700135static bool depend_missing_ok = false;
136static const char *depend_target = NULL;
137static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000138
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000139/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000140 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800141 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000142 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200143
H. Peter Anvin972079f2008-09-30 17:01:23 -0700144static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
145static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146
H. Peter Anvin972079f2008-09-30 17:01:23 -0700147static const struct warning {
148 const char *name;
149 const char *help;
150 bool enabled;
151} warnings[ERR_WARN_MAX+1] = {
152 {"error", "treat warnings as errors", false},
153 {"macro-params", "macro calls with wrong parameter count", true},
154 {"macro-selfref", "cyclic macro references", false},
155 {"macro-defaults", "macros with more default than optional parameters", true},
156 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700157 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700158 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
159 {"float-overflow", "floating point overflow", true},
160 {"float-denorm", "floating point denormal", false},
161 {"float-underflow", "floating point underflow", false},
162 {"float-toolong", "too many digits in floating-point number", true},
163 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800164 {"lock", "lock prefix on unlockable instructions", true},
165 {"hle", "invalid hle prefixes", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000166};
167
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700168static bool want_usage;
169static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000170int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000171
H. Peter Anvin55340992012-09-09 17:09:00 -0700172static char *quote_for_make(const char *str);
173
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400174static int64_t get_curr_offs(void)
175{
176 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
177}
178
179static void set_curr_offs(int64_t l_off)
180{
181 if (in_abs_seg)
182 abs_offset = l_off;
183 else
184 offsets = raa_write(offsets, location.segment, l_off);
185}
186
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000187static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000188{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000189 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800191 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000192 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194}
195
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800196/* Convert a struct tm to a POSIX-style time constant */
197static int64_t posix_mktime(struct tm *tm)
198{
199 int64_t t;
200 int64_t y = tm->tm_year;
201
202 /* See IEEE 1003.1:2004, section 4.14 */
203
204 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
205 t += tm->tm_yday;
206 t *= 24;
207 t += tm->tm_hour;
208 t *= 60;
209 t += tm->tm_min;
210 t *= 60;
211 t += tm->tm_sec;
212
213 return t;
214}
215
216static void define_macros_early(void)
217{
218 char temp[128];
219 struct tm lt, *lt_p, gm, *gm_p;
220 int64_t posix_time;
221
222 lt_p = localtime(&official_compile_time);
223 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400224 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800225
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400226 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
227 preproc->pre_define(temp);
228 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
229 preproc->pre_define(temp);
230 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
231 preproc->pre_define(temp);
232 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
233 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800234 }
235
236 gm_p = gmtime(&official_compile_time);
237 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400238 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800239
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400240 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
241 preproc->pre_define(temp);
242 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
243 preproc->pre_define(temp);
244 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
245 preproc->pre_define(temp);
246 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
247 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800248 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700249
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800250 if (gm_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400251 posix_time = posix_mktime(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800252 else if (lt_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400253 posix_time = posix_mktime(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800254 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400255 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800256
257 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400258 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
259 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260 }
261}
262
263static void define_macros_late(void)
264{
265 char temp[128];
266
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400267 /*
268 * In case if output format is defined by alias
269 * we have to put shortname of the alias itself here
270 * otherwise ABI backward compatibility gets broken.
271 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400272 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400273 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400274 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800275}
276
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700277static void emit_dependencies(StrList *list)
278{
279 FILE *deps;
280 int linepos, len;
281 StrList *l, *nl;
282
283 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400284 deps = fopen(depend_file, "w");
285 if (!deps) {
286 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
287 "unable to write dependency file `%s'", depend_file);
288 return;
289 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700290 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400291 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700292 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700293
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700294 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400295 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700296 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400297 len = strlen(file);
298 if (linepos + len > 62 && linepos > 1) {
299 fprintf(deps, " \\\n ");
300 linepos = 1;
301 }
302 fprintf(deps, " %s", file);
303 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700304 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700305 }
306 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700307
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400308 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400309 if (depend_emit_phony)
310 fprintf(deps, "%s:\n\n", l->str);
311 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700312 }
313
314 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400315 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700316}
317
H. Peter Anvin038d8612007-04-12 16:54:50 +0000318int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000319{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700320 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700321
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800322 time(&official_compile_time);
323
Charles Crayne2581c862008-09-10 19:21:52 -0700324 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700325 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400326 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000327
H. Peter Anvin97e15752007-09-25 08:47:47 -0700328 error_file = stderr;
329
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700330 tolower_init();
331
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400332 nasm_init_malloc_error();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000333 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000334 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000335
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000337 operating_mode = op_normal;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000338
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339 seg_init();
340
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800341 /* Define some macros dependent on the runtime, but not
342 on the command line. */
343 define_macros_early();
344
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000345 parse_cmdline(argc, argv);
346
H. Peter Anvine2c80182005-01-15 22:15:51 +0000347 if (terminate_after_phase) {
348 if (want_usage)
349 usage();
350 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000351 }
352
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000353 /* If debugging info is disabled, suppress any debug calls */
354 if (!using_debug_info)
355 ofmt->current_dfmt = &null_debug_form;
356
H. Peter Anvin76690a12002-04-30 20:52:49 +0000357 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400358 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700359 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000361
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000362 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800363 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000364
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400365 depend_ptr = (depend_file || (operating_mode == op_depend)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700366 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400367 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700368
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369 switch (operating_mode) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000370 case op_depend:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000372 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700373
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400374 if (depend_missing_ok)
375 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700376
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700377 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400379 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381 while ((line = preproc->getline()))
382 nasm_free(line);
383 preproc->cleanup(0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 }
385 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000386
387 case op_preprocess:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000389 char *line;
390 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000391 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000393
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 if (*outname) {
395 ofile = fopen(outname, "w");
396 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400397 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 "unable to open output file `%s'",
399 outname);
400 } else
401 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000402
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700403 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000404
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400405 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700406 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400407 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700408
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 while ((line = preproc->getline())) {
410 /*
411 * We generate %line directives if needed for later programs
412 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000413 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 int altline = src_get(&linnum, &file_name);
415 if (altline) {
416 if (altline == 1 && lineinc == 1)
417 nasm_fputs("", ofile);
418 else {
419 lineinc = (altline != -1 || lineinc != 1);
420 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000421 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 file_name);
423 }
424 prior_linnum = linnum;
425 }
426 nasm_fputs(line, ofile);
427 nasm_free(line);
428 }
429 nasm_free(file_name);
430 preproc->cleanup(0);
431 if (ofile)
432 fclose(ofile);
433 if (ofile && terminate_after_phase)
434 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400435 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 }
437 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000438
439 case op_normal:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 {
441 /*
442 * We must call ofmt->filename _anyway_, even if the user
443 * has specified their own output file, because some
444 * formats (eg OBJ and COFF) use ofmt->filename to find out
445 * the name of the input file and then put that inside the
446 * file.
447 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400448 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449
H. Peter Anvin0cba1072009-07-05 14:45:12 -0700450 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 if (!ofile) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400452 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 "unable to open output file `%s'", outname);
454 }
455
456 /*
457 * We must call init_labels() before ofmt->init() since
458 * some object formats will want to define labels in their
459 * init routines. (eg OS/2 defines the FLAT group)
460 */
461 init_labels();
462
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400463 ofmt->init();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400464 dfmt = ofmt->current_dfmt;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400465 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700467 assemble_file(inname, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468
469 if (!terminate_after_phase) {
470 ofmt->cleanup(using_debug_info);
471 cleanup_labels();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400472 fflush(ofile);
473 if (ferror(ofile)) {
474 nasm_error(ERR_NONFATAL|ERR_NOFILE,
475 "write error on output file `%s'", outname);
476 }
477 }
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200478
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400479 if (ofile) {
480 fclose(ofile);
481 if (terminate_after_phase)
482 remove(outname);
483 ofile = NULL;
484 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 }
486 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000487 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000488
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700489 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400490 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700491
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000494
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495 raa_free(offsets);
496 saa_free(forwrefs);
497 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000498 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000499
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700500 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000501}
502
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503/*
504 * Get a parameter for a command line option.
505 * First arg must be in the form of e.g. -f...
506 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800507static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000508{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800509 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400510 if (p[2]) /* the parameter's in the option */
511 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800513 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400516 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518 return NULL;
519}
520
H. Peter Anvindc242712007-11-18 11:55:10 -0800521/*
522 * Copy a filename
523 */
524static void copy_filename(char *dst, const char *src)
525{
526 size_t len = strlen(src);
527
528 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400529 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
530 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800531 }
532 strncpy(dst, src, FILENAME_MAX);
533}
534
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700535/*
536 * Convert a string to Make-safe form
537 */
538static char *quote_for_make(const char *str)
539{
540 const char *p;
541 char *os, *q;
542
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400543 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700544 size_t nbs = 0;
545
546 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400547 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700548
549 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400550 switch (*p) {
551 case ' ':
552 case '\t':
553 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
554 n += nbs + 2;
555 nbs = 0;
556 break;
557 case '$':
558 case '#':
559 nbs = 0;
560 n += 2;
561 break;
562 case '\\':
563 nbs++;
564 n++;
565 break;
566 default:
567 nbs = 0;
568 n++;
569 break;
570 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700571 }
572
573 /* Convert N backslashes at the end of filename to 2N backslashes */
574 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400575 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700576
577 os = q = nasm_malloc(n);
578
579 nbs = 0;
580 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400581 switch (*p) {
582 case ' ':
583 case '\t':
584 while (nbs--)
585 *q++ = '\\';
586 *q++ = '\\';
587 *q++ = *p;
588 break;
589 case '$':
590 *q++ = *p;
591 *q++ = *p;
592 nbs = 0;
593 break;
594 case '#':
595 *q++ = '\\';
596 *q++ = *p;
597 nbs = 0;
598 break;
599 case '\\':
600 *q++ = *p;
601 nbs++;
602 break;
603 default:
604 *q++ = *p;
605 nbs = 0;
606 break;
607 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700608 }
609 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400610 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700611
612 *q = '\0';
613
614 return os;
615}
616
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000618 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000620};
621
622#define OPT_PREFIX 0
623#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624struct textargs textopts[] = {
625 {"prefix", OPT_PREFIX},
626 {"postfix", OPT_POSTFIX},
627 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000628};
629
H. Peter Anvin423e3812007-11-15 17:12:29 -0800630static bool stopoptions = false;
631static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000633 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800634 int i;
635 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700636 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000637
638 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800639 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000640
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400642 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
643 /* These parameters take values */
644 if (!(param = get_param(p, q, &advance)))
645 return advance;
646 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800647
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 switch (p[1]) {
649 case 's':
650 error_file = stdout;
651 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700652
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400653 case 'o': /* output file */
654 copy_filename(outname, param);
655 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700656
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400657 case 'f': /* output format */
658 ofmt = ofmt_find(param, &ofmt_alias);
659 if (!ofmt) {
660 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
661 "unrecognised output format `%s' - "
662 "use -hf for a list", param);
663 }
664 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700665
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400666 case 'O': /* Optimization level */
667 {
668 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700669
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400670 if (!*param) {
671 /* Naked -O == -Ox */
672 optimizing = MAX_OPTIMIZE;
673 } else {
674 while (*param) {
675 switch (*param) {
676 case '0': case '1': case '2': case '3': case '4':
677 case '5': case '6': case '7': case '8': case '9':
678 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700679
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400680 /* -O0 -> optimizing == -1, 0.98 behaviour */
681 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
682 if (opt < 2)
683 optimizing = opt - 1;
684 else
685 optimizing = opt;
686 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700687
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400688 case 'v':
689 case '+':
690 param++;
691 opt_verbose_info = true;
692 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700693
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400694 case 'x':
695 param++;
696 optimizing = MAX_OPTIMIZE;
697 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700698
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400699 default:
700 nasm_error(ERR_FATAL,
701 "unknown optimization option -O%c\n",
702 *param);
703 break;
704 }
705 }
706 if (optimizing > MAX_OPTIMIZE)
707 optimizing = MAX_OPTIMIZE;
708 }
709 break;
710 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800711
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400712 case 'p': /* pre-include */
713 case 'P':
714 preproc->pre_include(param);
715 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800716
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400717 case 'd': /* pre-define */
718 case 'D':
719 preproc->pre_define(param);
720 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800721
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400722 case 'u': /* un-define */
723 case 'U':
724 preproc->pre_undefine(param);
725 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800726
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400727 case 'i': /* include search path */
728 case 'I':
729 preproc->include_path(param);
730 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800731
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400732 case 'l': /* listing file */
733 copy_filename(listname, param);
734 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800735
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400736 case 'Z': /* error messages file */
737 copy_filename(errname, param);
738 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800739
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400740 case 'F': /* specify debug format */
741 ofmt->current_dfmt = dfmt_find(ofmt, param);
742 if (!ofmt->current_dfmt) {
743 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
744 "unrecognized debug format `%s' for"
745 " output format `%s'",
746 param, ofmt->shortname);
747 }
748 using_debug_info = true;
749 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800750
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 case 'X': /* specify error reporting format */
752 if (nasm_stricmp("vc", param) == 0)
753 nasm_set_verror(nasm_verror_vc);
754 else if (nasm_stricmp("gnu", param) == 0)
755 nasm_set_verror(nasm_verror_gnu);
756 else
757 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
758 "unrecognized error reporting format `%s'",
759 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800761
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700763 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800765
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 case 'h':
767 printf
768 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
769 "[-l listfile]\n"
770 " [options...] [--] filename\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700771 " or nasm -v for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400773 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700775 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700777 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400778 " -MG d:o, missing files assumed generated\n"
779 " -MF <file> set Makefile dependency file\n"
780 " -MD <file> assemble and generate dependencies\n"
781 " -MT <file> dependency target name\n"
782 " -MQ <file> dependency target name (quoted)\n"
783 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700784 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 " -s redirect error messages to stdout\n\n"
786 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400787 " -o outfile write output to an outfile\n\n"
788 " -f format select an output format\n\n"
789 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 " -I<path> adds a pathname to the include file path\n");
791 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700792 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400793 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400794 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400795 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 " -P<file> pre-includes a file\n"
797 " -D<macro>[=<value>] pre-defines a macro\n"
798 " -U<macro> undefines a macro\n"
799 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800800 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400801 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400802 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400803 "--prefix,--postfix\n"
804 " this options prepend or append the given argument to all\n"
805 " extern and global variables\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800806 "Warnings:\n");
807 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700809 warnings[i].name, warnings[i].help,
810 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 printf
812 ("\nresponse files should contain command line parameters"
813 ", one per line.\n");
814 if (p[2] == 'f') {
815 printf("\nvalid output formats for -f are"
816 " (`*' denotes default):\n");
817 ofmt_list(ofmt, stdout);
818 } else {
819 printf("\nFor a list of valid output formats, use -hf.\n");
820 printf("For a list of debug formats, use -f <form> -y.\n");
821 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400822 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800824
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 case 'y':
826 printf("\nvalid debug formats for '%s' output format are"
827 " ('*' denotes default):\n", ofmt->shortname);
828 dfmt_list(ofmt, stdout);
829 exit(0);
830 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800831
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700833 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800835
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 case 'v':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400837 printf("NASM version %s compiled on %s%s\n",
838 nasm_version, nasm_date, nasm_compile_options);
839 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400842 case 'e': /* preprocess only */
843 case 'E':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 operating_mode = op_preprocess;
845 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800846
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400847 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400848 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800850
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400851 case 'W':
852 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
853 do_warn = false;
854 param += 3;
855 } else {
856 do_warn = true;
857 }
858 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800859
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800861 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400862 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400866 do_warn = (param[0] == '+');
867 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400868
869set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400870 for (i = 0; i <= ERR_WARN_MAX; i++) {
871 if (!nasm_stricmp(param, warnings[i].name))
872 break;
873 }
874 if (i <= ERR_WARN_MAX) {
875 warning_on_global[i] = do_warn;
876 } else if (!nasm_stricmp(param, "all")) {
877 for (i = 1; i <= ERR_WARN_MAX; i++)
878 warning_on_global[i] = do_warn;
879 } else if (!nasm_stricmp(param, "none")) {
880 for (i = 1; i <= ERR_WARN_MAX; i++)
881 warning_on_global[i] = !do_warn;
882 } else {
883 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
884 "invalid warning `%s'", param);
885 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800887
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400889 switch (p[2]) {
890 case 0:
891 operating_mode = op_depend;
892 break;
893 case 'G':
894 operating_mode = op_depend;
895 depend_missing_ok = true;
896 break;
897 case 'P':
898 depend_emit_phony = true;
899 break;
900 case 'D':
901 depend_file = q;
902 advance = true;
903 break;
904 case 'T':
905 depend_target = q;
906 advance = true;
907 break;
908 case 'Q':
909 depend_target = quote_for_make(q);
910 advance = true;
911 break;
912 default:
913 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
914 "unknown dependency option `-M%c'", p[2]);
915 break;
916 }
917 if (advance && (!q || !q[0])) {
918 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
919 "option `-M%c' requires a parameter", p[2]);
920 break;
921 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000923
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 case '-':
925 {
926 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000927
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 if (p[2] == 0) { /* -- => stop processing options */
929 stopoptions = 1;
930 break;
931 }
932 for (s = 0; textopts[s].label; s++) {
933 if (!nasm_stricmp(p + 2, textopts[s].label)) {
934 break;
935 }
936 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000937
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 switch (s) {
939
940 case OPT_PREFIX:
941 case OPT_POSTFIX:
942 {
943 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400944 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 ERR_USAGE,
946 "option `--%s' requires an argument",
947 p + 2);
948 break;
949 } else {
950 advance = 1, param = q;
951 }
952
953 if (s == OPT_PREFIX) {
954 strncpy(lprefix, param, PREFIX_MAX - 1);
955 lprefix[PREFIX_MAX - 1] = 0;
956 break;
957 }
958 if (s == OPT_POSTFIX) {
959 strncpy(lpostfix, param, POSTFIX_MAX - 1);
960 lpostfix[POSTFIX_MAX - 1] = 0;
961 break;
962 }
963 break;
964 }
965 default:
966 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400967 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 "unrecognised option `--%s'", p + 2);
969 break;
970 }
971 }
972 break;
973 }
974
975 default:
976 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400977 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 "unrecognised option `-%c'", p[1]);
979 break;
980 }
981 } else {
982 if (*inname) {
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 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800985 } else {
986 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000988 }
989
990 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000991}
992
H. Peter Anvineba20a72002-04-30 20:53:55 +0000993#define ARG_BUF_DELTA 128
994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000996{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000997 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000998 int bufsize, prevargsize;
999
1000 bufsize = prevargsize = ARG_BUF_DELTA;
1001 buffer = nasm_malloc(ARG_BUF_DELTA);
1002 prevarg = nasm_malloc(ARG_BUF_DELTA);
1003 prevarg[0] = '\0';
1004
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 p = buffer;
1007 while (1) { /* Loop to handle long lines */
1008 q = fgets(p, bufsize - (p - buffer), rfile);
1009 if (!q)
1010 break;
1011 p += strlen(p);
1012 if (p > buffer && p[-1] == '\n')
1013 break;
1014 if (p - buffer > bufsize - 10) {
1015 int offset;
1016 offset = p - buffer;
1017 bufsize += ARG_BUF_DELTA;
1018 buffer = nasm_realloc(buffer, bufsize);
1019 p = buffer + offset;
1020 }
1021 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 if (!q && p == buffer) {
1024 if (prevarg[0])
1025 process_arg(prevarg, NULL);
1026 nasm_free(buffer);
1027 nasm_free(prevarg);
1028 return;
1029 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 /*
1032 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1033 * them are present at the end of the line.
1034 */
1035 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001036
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001037 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001039
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001040 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001041
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 if (process_arg(prevarg, p))
1043 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001044
Charles Crayne192d5b52007-10-18 19:02:42 -07001045 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 prevargsize += ARG_BUF_DELTA;
1047 prevarg = nasm_realloc(prevarg, prevargsize);
1048 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001049 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001050 }
1051}
1052
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001053/* Function to process args from a string of args, rather than the
1054 * argv array. Used by the environment variable and response file
1055 * processing.
1056 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001057static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001059 char *p, *q, *arg, *prevarg;
1060 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001061
1062 p = args;
1063 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001065 arg = NULL;
1066 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 q = p;
1068 while (*p && *p != separator)
1069 p++;
1070 while (*p == separator)
1071 *p++ = '\0';
1072 prevarg = arg;
1073 arg = q;
1074 if (process_arg(prevarg, arg))
1075 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001076 }
1077 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001079}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001080
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001081static void process_response_file(const char *file)
1082{
1083 char str[2048];
1084 FILE *f = fopen(file, "r");
1085 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001086 perror(file);
1087 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001088 }
1089 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001090 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001091 }
1092 fclose(f);
1093}
1094
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001095static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001096{
1097 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001098 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001099 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001100
Charles Craynefcce07f2007-09-30 22:15:36 -07001101 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001102
H. Peter Anvin972079f2008-09-30 17:01:23 -07001103 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001104 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001105
1106 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001107 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001108 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001109 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001110 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 envcopy = nasm_strdup(envreal);
1112 process_args(envcopy);
1113 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001114 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001115
1116 /*
1117 * Now process the actual command line.
1118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001120 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 argv++;
1122 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001123 /*
1124 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 * arguments like the environment variable. This allows us
1126 * to have multiple arguments on a single line, which is
1127 * different to the -@resp file processing below for regular
1128 * NASM.
1129 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001130 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 argc--;
1132 argv++;
1133 }
1134 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001135 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001136 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001137 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001138 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 process_respfile(rfile);
1140 fclose(rfile);
1141 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001142 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 "unable to open response file `%s'", p);
1144 }
1145 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001146 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1147 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001148 }
1149
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001150 /*
1151 * Look for basic command line typos. This definitely doesn't
1152 * catch all errors, but it might help cases of fumbled fingers.
1153 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001154 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001155 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001156 "no input file specified");
1157 else if (!strcmp(inname, errname) ||
1158 !strcmp(inname, outname) ||
1159 !strcmp(inname, listname) ||
1160 (depend_file && !strcmp(inname, depend_file)))
1161 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1162 "file `%s' is both input and output file",
1163 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001164
H. Peter Anvin70653092007-10-19 14:42:29 -07001165 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001166 error_file = fopen(errname, "w");
1167 if (!error_file) {
1168 error_file = stderr; /* Revert to default! */
1169 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1170 "cannot open file `%s' for error messages",
1171 errname);
1172 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001173 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001174}
1175
H. Peter Anvin70055962007-10-11 00:05:31 -07001176static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001177
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001178static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001179{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001180 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001182 int i, validid;
1183 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001184 int32_t seg;
1185 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001186 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001188 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001189
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001190 if (cmd_sb == 32 && cmd_cpu < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001191 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001193
Charles Craynec1905c22008-09-11 18:54:06 -07001194 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001195 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 int pass1, pass2;
1197 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001198
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001199 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001200 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1201 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001203 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204
Keith Kaniosb7a89542007-04-12 02:40:54 +00001205 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 cpu = cmd_cpu;
1207 if (pass0 == 2) {
1208 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001209 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001211 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001212 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001214 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001215 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 saa_rewind(forwrefs);
1217 forwref = saa_rstruct(forwrefs);
1218 raa_free(offsets);
1219 offsets = raa_init();
1220 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001221 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001222 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001223 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001224
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001226 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001227 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001228 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001229
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001231 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001233
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001234 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001235 * Here we parse our directives; this is not handled by the
1236 * 'real' parser. This really should be a separate function.
1237 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001239 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001240 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001241 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001242
H. Peter Anvin70055962007-10-11 00:05:31 -07001243 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001244 case D_SEGMENT: /* [SEGMENT n] */
1245 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 seg = ofmt->section(value, pass2, &sb);
1247 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001248 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001249 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 value);
1251 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001252 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 location.segment = seg;
1254 }
1255 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001256 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001257 if (*value) {
1258 stdscan_reset();
1259 stdscan_set(value);
1260 tokval.t_type = TOKEN_INVALID;
1261 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1262 if (e) {
1263 unsigned int align = (unsigned int)e->value;
1264 if ((uint64_t)e->value > 0x7fffffff) {
1265 /*
1266 * FIXME: Please make some sane message here
1267 * ofmt should have some 'check' method which
1268 * would report segment alignment bounds.
1269 */
1270 nasm_error(ERR_FATAL,
1271 "incorrect segment alignment `%s'", value);
1272 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001273 nasm_error(ERR_NONFATAL,
1274 "segment alignment `%s' is not power of two",
1275 value);
1276 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001277 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001278 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001279 }
1280 }
1281 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001282 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 if (*value == '$')
1284 value++; /* skip initial $ if present */
1285 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001286 q = value;
1287 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001289 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 *q++ = '\0';
1291 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001292 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001293 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001294 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001295 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001296 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001297 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001298 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001300 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001302 }
1303 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001304 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 "identifier expected after EXTERN");
1306 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001307 }
1308 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 *q++ = '\0';
1310 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001311 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 special = NULL;
1313 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1314 int temp = pass0;
1315 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001316 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001318 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001320 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 } /* else pass0 == 1 */
1322 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001323 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001324 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001326 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 if (*value == '$')
1328 value++; /* skip initial $ if present */
1329 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001330 q = value;
1331 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001333 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 *q++ = '\0';
1335 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001336 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001338 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001339 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001340 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001341 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001344 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001346 }
1347 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001348 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 "identifier expected after GLOBAL");
1350 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351 }
1352 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 *q++ = '\0';
1354 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001355 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001357 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 } /* pass == 1 */
1359 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001360 case D_COMMON: /* [COMMON symbol size:special] */
1361 {
1362 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001363
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 if (*value == '$')
1365 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001366 p = value;
1367 validid = true;
1368 if (!isidstart(*p))
1369 validid = false;
1370 while (*p && !nasm_isspace(*p)) {
1371 if (!isidchar(*p))
1372 validid = false;
1373 p++;
1374 }
1375 if (!validid) {
1376 nasm_error(ERR_NONFATAL,
1377 "identifier expected after COMMON");
1378 break;
1379 }
1380 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001381 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001382 q = p;
1383 while (*q && *q != ':')
1384 q++;
1385 if (*q == ':') {
1386 *q++ = '\0';
1387 special = q;
1388 } else {
1389 special = NULL;
1390 }
1391 size = readnum(p, &rn_error);
1392 if (rn_error) {
1393 nasm_error(ERR_NONFATAL,
1394 "invalid size specified"
1395 " in COMMON declaration");
1396 break;
1397 }
1398 } else {
1399 nasm_error(ERR_NONFATAL,
1400 "no size specified in"
1401 " COMMON declaration");
1402 break;
1403 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001404
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001405 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001406 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001407 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001408 if (special)
1409 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 }
1411 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001412 }
1413 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001415 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 tokval.t_type = TOKEN_INVALID;
1417 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001418 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 if (e) {
1420 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001421 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 1 ? ERR_NONFATAL : ERR_PANIC,
1423 "cannot use non-relocatable expression as "
1424 "ABSOLUTE address");
1425 else {
1426 abs_seg = reloc_seg(e);
1427 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001428 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001429 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 abs_offset = 0x100; /* don't go near zero in case of / */
1431 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001432 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001434 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 location.segment = NO_SEG;
1436 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001437 case D_DEBUG: /* [DEBUG] */
1438 {
1439 char debugid[128];
1440 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001441
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 p = value;
1443 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001444 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001445 if (!isidstart(*p)) {
1446 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001447 } else {
1448 while (*p && !nasm_isspace(*p)) {
1449 if (q >= debugid + sizeof debugid - 1) {
1450 overlong = true;
1451 break;
1452 }
1453 if (!isidchar(*p))
1454 badid = true;
1455 *q++ = *p++;
1456 }
1457 *q = 0;
1458 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001459 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001460 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1461 "identifier expected after DEBUG");
1462 break;
1463 }
1464 if (overlong) {
1465 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1466 "DEBUG identifier too long");
1467 break;
1468 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001469 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001470 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001471 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001473 }
1474 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001475 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001476 switch(*value) {
1477 case '-': validid = 0; value++; break;
1478 case '+': validid = 1; value++; break;
1479 case '*': validid = 2; value++; break;
1480 default: validid = 1; break;
1481 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001483 for (i = 1; i <= ERR_WARN_MAX; i++)
1484 if (!nasm_stricmp(value, warnings[i].name))
1485 break;
1486 if (i <= ERR_WARN_MAX) {
1487 switch(validid) {
1488 case 0:
1489 warning_on[i] = false;
1490 break;
1491 case 1:
1492 warning_on[i] = true;
1493 break;
1494 case 2:
1495 warning_on[i] = warning_on_global[i];
1496 break;
1497 }
1498 } else
1499 nasm_error(ERR_NONFATAL,
1500 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001502 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 cpu = get_cpu(value);
1504 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001505 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001506 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 if (*value == '+') {
1508 user_nolist = 0;
1509 } else {
1510 if (*value == '-') {
1511 user_nolist = 1;
1512 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001513 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 }
1515 }
1516 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001517 case D_DEFAULT: /* [DEFAULT] */
1518 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001519 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001520 tokval.t_type = TOKEN_INVALID;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001521 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1522 switch ((int)tokval.t_integer) {
1523 case S_REL:
1524 globalrel = 1;
1525 break;
1526 case S_ABS:
1527 globalrel = 0;
1528 break;
1529 default:
1530 err = 1;
1531 break;
1532 }
1533 } else {
1534 err = 1;
1535 }
1536 break;
1537 case D_FLOAT:
1538 if (float_option(value)) {
1539 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1540 "unknown 'float' directive: %s",
1541 value);
1542 }
1543 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001545 if (ofmt->directive(d, value, pass2))
1546 break;
1547 /* else fall through */
1548 case D_unknown:
1549 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1550 "unrecognised directive [%s]",
1551 directive);
1552 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001554 if (err) {
1555 nasm_error(ERR_NONFATAL,
1556 "invalid parameter to [%s] directive",
1557 directive);
1558 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001560 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561
Charles Crayne2581c862008-09-10 19:21:52 -07001562 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001564 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001566 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 forwref = saa_rstruct(forwrefs);
1568 } while (forwref != NULL
1569 && forwref->lineno == globallineno);
1570 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001571 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001572
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001573 if (output_ins.forw_ref) {
1574 if (passn == 1) {
1575 for (i = 0; i < output_ins.operands; i++) {
1576 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1577 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1578 fwinf->lineno = globallineno;
1579 fwinf->operand = i;
1580 }
1581 }
1582 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001583 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001585
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 /* forw_ref */
1587 if (output_ins.opcode == I_EQU) {
1588 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001589 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 * Special `..' EQUs get processed in pass two,
1591 * except `..@' macro-processor EQUs which are done
1592 * in the normal place.
1593 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001594 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001595 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001597
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001598 else if (output_ins.label[0] != '.' ||
1599 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 output_ins.label[2] == '@') {
1601 if (output_ins.operands == 1 &&
1602 (output_ins.oprs[0].type & IMMEDIATE) &&
1603 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001604 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 def_label(output_ins.label,
1606 output_ins.oprs[0].segment,
1607 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001608 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001610 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001612 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001614 && (output_ins.oprs[1].type & IMMEDIATE)
1615 && output_ins.oprs[1].segment == NO_SEG
1616 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001618 output_ins.oprs[0].offset | SEG_ABS,
1619 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001620 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001622 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001624 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001625 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001626 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 * Special `..' EQUs get processed here, except
1628 * `..@' macro processor EQUs which are done above.
1629 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001630 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 output_ins.label[1] == '.' &&
1632 output_ins.label[2] != '@') {
1633 if (output_ins.operands == 1 &&
1634 (output_ins.oprs[0].type & IMMEDIATE)) {
1635 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001636 output_ins.oprs[0].segment,
1637 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001638 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001640 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001642 && output_ins.oprs[0].segment == NO_SEG
1643 && (output_ins.oprs[1].type & IMMEDIATE)
1644 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001646 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001648 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001650 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001653 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001655
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001657
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001658 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001659 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001660
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 /* if (using_debug_info) && output_ins.opcode != -1) */
1662 if (using_debug_info)
1663 { /* fbk 03/25/01 */
1664 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001665 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 TYS_ELEMENTS(output_ins.operands);
1667 switch (output_ins.opcode) {
1668 case I_RESB:
1669 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001670 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 break;
1672 case I_RESW:
1673 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001674 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 break;
1676 case I_RESD:
1677 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001678 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 break;
1680 case I_RESQ:
1681 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001682 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 break;
1684 case I_REST:
1685 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001686 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001688 case I_RESO:
1689 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001690 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001691 break;
1692 case I_RESY:
1693 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001694 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001695 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 case I_DB:
1697 typeinfo |= TY_BYTE;
1698 break;
1699 case I_DW:
1700 typeinfo |= TY_WORD;
1701 break;
1702 case I_DD:
1703 if (output_ins.eops_float)
1704 typeinfo |= TY_FLOAT;
1705 else
1706 typeinfo |= TY_DWORD;
1707 break;
1708 case I_DQ:
1709 typeinfo |= TY_QWORD;
1710 break;
1711 case I_DT:
1712 typeinfo |= TY_TBYTE;
1713 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001714 case I_DO:
1715 typeinfo |= TY_OWORD;
1716 break;
1717 case I_DY:
1718 typeinfo |= TY_YWORD;
1719 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 default:
1721 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001722
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001724
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001725 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001726 }
1727 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001729 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001730 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001731 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 * else l == -1 => invalid instruction, which will be
1733 * flagged as an error on pass 2
1734 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001735
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001736 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001738 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001740 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001741
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 }
1743 } /* not an EQU */
1744 cleanup_insn(&output_ins);
1745 }
1746 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001747 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001749
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001750 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001751 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001752 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001753
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 if (pass1 == 1)
1755 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001756
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001757 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001759 } else if (global_offset_changed &&
1760 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001761 prev_offset_changed = global_offset_changed;
1762 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001763 } else {
1764 stall_count++;
1765 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001766
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001767 if (terminate_after_phase)
1768 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001769
1770 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001771 /* We get here if the labels don't converge
1772 * Example: FOO equ FOO + 1
1773 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001774 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001775 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001776 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001777 nasm_error(ERR_NONFATAL,
1778 "Possible causes: recursive EQUs, macro abuse.");
1779 break;
1780 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001781 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 preproc->cleanup(0);
1784 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001785 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001786 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001787 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1788 }
1789}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001790
H. Peter Anvin70055962007-10-11 00:05:31 -07001791static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001793 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001795 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001796
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001797 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001799 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001800 q = strchr(buf, ']');
1801 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001802 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001803
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001804 /* stip off the comments */
1805 p = strchr(buf, ';');
1806 if (p) {
1807 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001808 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001809 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001810 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001811
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001812 /* no brace, no trailing spaces */
1813 *q = '\0';
1814 nasm_zap_spaces_rev(--q);
1815
1816 /* directive */
1817 p = nasm_skip_spaces(++buf);
1818 q = nasm_skip_word(p);
1819 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001820 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001821 *q = '\0';
1822 *directive = p;
1823
1824 /* and value finally */
1825 p = nasm_skip_spaces(++q);
1826 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001827
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001828 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001829}
1830
Ed Berosetfa771012002-06-09 20:56:40 +00001831/**
1832 * gnu style error reporting
1833 * This function prints an error message to error_file in the
1834 * style used by GNU. An example would be:
1835 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001836 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001837 * which the error occurs (or is detected) and "error:" is one of
1838 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001839 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001840 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001841 *
1842 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001843 * @param fmt the printf style format string
1844 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001845static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001846{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001847 char *currentfile = NULL;
1848 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001849
Ed Berosetfa771012002-06-09 20:56:40 +00001850 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001852
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001853 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001855
1856 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001857 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1858 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001859 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001860 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001861 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001862
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001863 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001864}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001865
Ed Berosetfa771012002-06-09 20:56:40 +00001866/**
1867 * MS style error reporting
1868 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001869 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001870 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001871 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001872 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001873 * which the error occurs (or is detected) and "error:" is one of
1874 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001875 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001876 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001877 *
1878 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001879 * @param fmt the printf style format string
1880 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001881static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001882{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001883 char *currentfile = NULL;
1884 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001885
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 if (is_suppressed_warning(severity))
1887 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001888
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001889 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001891
1892 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001893 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001895 } else {
1896 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001897 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001898
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001899 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001900}
1901
1902/**
1903 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001904 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001905 * not in pass 1
1906 *
1907 * @param severity the severity of the warning or error
1908 * @return true if we should abort error/warning printing
1909 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001910static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001911{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001912 /* Not a warning at all */
1913 if ((severity & ERR_MASK) != ERR_WARNING)
1914 return false;
1915
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001916 /* See if it's a pass-one only warning and we're not in pass one. */
1917 if (((severity & ERR_PASS1) && pass0 != 1) ||
1918 ((severity & ERR_PASS2) && pass0 != 2))
1919 return true;
1920
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001921 /* Might be a warning but suppresed explicitly */
1922 if (severity & ERR_WARN_MASK)
1923 return !warning_on[WARN_IDX(severity)];
1924 else
1925 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001926}
1927
1928/**
1929 * common error reporting
1930 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001931 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001932 * specific error message to error_file and may or may not return. It
1933 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001934 *
1935 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001936 * @param fmt the printf style format string
1937 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001938static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001939{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001940 char msg[1024];
1941 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001942
H. Peter Anvin7df04172008-06-10 18:27:38 -07001943 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001945 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 break;
1947 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001948 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 break;
1950 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001951 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 break;
1953 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001954 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 break;
1956 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001957 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001959 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001960 pfx = "";
1961 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001962 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001963
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001964 vsnprintf(msg, sizeof msg, fmt, args);
1965
1966 fprintf(error_file, "%s%s\n", pfx, msg);
1967
1968 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001969 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001970
1971 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001972 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001973
1974 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 case ERR_DEBUG:
1976 /* no further action, by definition */
1977 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001978 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001979 /* Treat warnings as errors */
1980 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1981 terminate_after_phase = true;
1982 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001984 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 break;
1986 case ERR_FATAL:
1987 if (ofile) {
1988 fclose(ofile);
1989 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001990 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 }
1992 if (want_usage)
1993 usage();
1994 exit(1); /* instantly die */
1995 break; /* placate silly compilers */
1996 case ERR_PANIC:
1997 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001998 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 exit(3);
2000 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002001 }
2002}
2003
H. Peter Anvin734b1882002-04-30 21:01:08 +00002004static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002005{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002006 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002007}
2008
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002009static uint32_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002010{
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 if (!strcmp(value, "8086"))
2012 return IF_8086;
2013 if (!strcmp(value, "186"))
2014 return IF_186;
2015 if (!strcmp(value, "286"))
2016 return IF_286;
2017 if (!strcmp(value, "386"))
2018 return IF_386;
2019 if (!strcmp(value, "486"))
2020 return IF_486;
2021 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
2022 return IF_PENT;
2023 if (!strcmp(value, "686") ||
2024 !nasm_stricmp(value, "ppro") ||
2025 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
2026 return IF_P6;
2027 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
2028 return IF_KATMAI;
2029 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2030 !nasm_stricmp(value, "willamette"))
2031 return IF_WILLAMETTE;
2032 if (!nasm_stricmp(value, "prescott"))
2033 return IF_PRESCOTT;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002034 if (!nasm_stricmp(value, "x64") ||
2035 !nasm_stricmp(value, "x86-64"))
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002036 return IF_X86_64;
H. Peter Anvin3ab8de62002-05-28 01:25:06 +00002037 if (!nasm_stricmp(value, "ia64") ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002038 !nasm_stricmp(value, "ia-64") ||
2039 !nasm_stricmp(value, "itanium") ||
2040 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
2041 return IF_IA64;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002042
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002043 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 "unknown 'cpu' type");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002045
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 return IF_PLEVEL; /* the maximum level */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002047}
2048
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002049static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002050{
2051 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002052
H. Peter Anvine2c80182005-01-15 22:15:51 +00002053 if ((i = atoi(value)) == 16)
2054 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002055 else if (i == 32) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002056 if (cpu < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002057 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 "cannot specify 32-bit segment on processor below a 386");
2059 i = 16;
2060 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002061 } else if (i == 64) {
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002062 if (cpu < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002063 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002064 "cannot specify 64-bit segment on processor below an x86-64");
2065 i = 16;
2066 }
2067 if (i != maxbits) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002068 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002069 "%s output format does not support 64-bit code",
2070 ofmt->shortname);
2071 i = 16;
2072 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002073 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002074 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002075 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 value);
2077 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002078 }
2079 return i;
2080}