blob: 5cf04a9807d28b3ddc9b32dd7b322968f1a4d1c1 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Cyrill Gorcunovf187eb72013-02-15 12:25:33 +04003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070046#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080047#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49#include "nasm.h"
50#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvin31b707b2009-06-27 22:07:33 -070061#include "output/outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvin2b046cf2008-01-22 14:08:36 -080084static bool is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085static void usage(void);
86
John Coffman0efaec92002-05-26 19:20:08 +000087static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070088bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080089int pass0, passn;
Keith Kaniosb7a89542007-04-12 02:40:54 +000090int maxbits = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000091int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080092int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000093
H. Peter Anvin9bd15062009-07-18 21:07:17 -040094static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080095
Keith Kaniosa6dfa782007-04-13 16:47:53 +000096static char inname[FILENAME_MAX];
97static char outname[FILENAME_MAX];
98static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -070099static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000100static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000101/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400102struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400103struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400104const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000107
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400108FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700109int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
110static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400111
112static iflag_t cpu;
113static iflag_t cmd_cpu;
114
Charles Craynec1905c22008-09-11 18:54:06 -0700115int64_t global_offset_changed; /* referenced in labels.c */
116int64_t prev_offset_changed;
117int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000118
H. Peter Anvin12e46512007-10-03 21:30:57 -0700119static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000121int32_t abs_seg; /* ABSOLUTE segment basis */
122int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000123
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000125
H. Peter Anvine2c80182005-01-15 22:15:51 +0000126static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700127static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400129static struct preproc_ops *preproc;
130
H. Peter Anvin620515a2002-04-30 20:57:38 +0000131enum op_type {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000132 op_normal, /* Preprocess and assemble */
133 op_preprocess, /* Preprocess only */
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700134 op_depend, /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000135};
136static enum op_type operating_mode;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700137/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700138static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700139static bool depend_missing_ok = false;
140static const char *depend_target = NULL;
141static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000142
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000144 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800145 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200147
H. Peter Anvin972079f2008-09-30 17:01:23 -0700148static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
149static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000150
H. Peter Anvin972079f2008-09-30 17:01:23 -0700151static const struct warning {
152 const char *name;
153 const char *help;
154 bool enabled;
155} warnings[ERR_WARN_MAX+1] = {
156 {"error", "treat warnings as errors", false},
157 {"macro-params", "macro calls with wrong parameter count", true},
158 {"macro-selfref", "cyclic macro references", false},
159 {"macro-defaults", "macros with more default than optional parameters", true},
160 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700161 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700162 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
163 {"float-overflow", "floating point overflow", true},
164 {"float-denorm", "floating point denormal", false},
165 {"float-underflow", "floating point underflow", false},
166 {"float-toolong", "too many digits in floating-point number", true},
167 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800168 {"lock", "lock prefix on unlockable instructions", true},
169 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800170 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000171};
172
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700173static bool want_usage;
174static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000175int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176
H. Peter Anvin55340992012-09-09 17:09:00 -0700177static char *quote_for_make(const char *str);
178
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400179static int64_t get_curr_offs(void)
180{
181 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
182}
183
184static void set_curr_offs(int64_t l_off)
185{
186 if (in_abs_seg)
187 abs_offset = l_off;
188 else
189 offsets = raa_write(offsets, location.segment, l_off);
190}
191
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000192static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000194 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800196 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000199}
200
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800201/* Convert a struct tm to a POSIX-style time constant */
202static int64_t posix_mktime(struct tm *tm)
203{
204 int64_t t;
205 int64_t y = tm->tm_year;
206
207 /* See IEEE 1003.1:2004, section 4.14 */
208
209 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
210 t += tm->tm_yday;
211 t *= 24;
212 t += tm->tm_hour;
213 t *= 60;
214 t += tm->tm_min;
215 t *= 60;
216 t += tm->tm_sec;
217
218 return t;
219}
220
221static void define_macros_early(void)
222{
223 char temp[128];
224 struct tm lt, *lt_p, gm, *gm_p;
225 int64_t posix_time;
226
227 lt_p = localtime(&official_compile_time);
228 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400229 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800230
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400231 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
232 preproc->pre_define(temp);
233 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
234 preproc->pre_define(temp);
235 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
238 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800239 }
240
241 gm_p = gmtime(&official_compile_time);
242 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400243 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800244
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400245 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
246 preproc->pre_define(temp);
247 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
248 preproc->pre_define(temp);
249 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
250 preproc->pre_define(temp);
251 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
252 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800253 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700254
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800255 if (gm_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400256 posix_time = posix_mktime(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 else if (lt_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400258 posix_time = posix_mktime(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400260 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261
262 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400263 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
264 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265 }
266}
267
268static void define_macros_late(void)
269{
270 char temp[128];
271
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400272 /*
273 * In case if output format is defined by alias
274 * we have to put shortname of the alias itself here
275 * otherwise ABI backward compatibility gets broken.
276 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400277 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400278 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400279 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280}
281
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700282static void emit_dependencies(StrList *list)
283{
284 FILE *deps;
285 int linepos, len;
286 StrList *l, *nl;
287
288 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400289 deps = fopen(depend_file, "w");
290 if (!deps) {
291 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
292 "unable to write dependency file `%s'", depend_file);
293 return;
294 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700295 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400296 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700297 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700298
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700299 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400300 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700301 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400302 len = strlen(file);
303 if (linepos + len > 62 && linepos > 1) {
304 fprintf(deps, " \\\n ");
305 linepos = 1;
306 }
307 fprintf(deps, " %s", file);
308 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700309 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700310 }
311 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700312
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400313 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400314 if (depend_emit_phony)
315 fprintf(deps, "%s:\n\n", l->str);
316 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700317 }
318
319 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400320 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321}
322
H. Peter Anvin038d8612007-04-12 16:54:50 +0000323int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000324{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700326
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800327 time(&official_compile_time);
328
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400329 iflag_set(&cpu, IF_PLEVEL);
330 iflag_set(&cmd_cpu, IF_PLEVEL);
331
Charles Crayne2581c862008-09-10 19:21:52 -0700332 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700333 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400334 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000335
H. Peter Anvin97e15752007-09-25 08:47:47 -0700336 error_file = stderr;
337
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700338 tolower_init();
339
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000340 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000341 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000342
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000344 operating_mode = op_normal;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000345
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346 seg_init();
347
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800348 /* Define some macros dependent on the runtime, but not
349 on the command line. */
350 define_macros_early();
351
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000352 parse_cmdline(argc, argv);
353
H. Peter Anvine2c80182005-01-15 22:15:51 +0000354 if (terminate_after_phase) {
355 if (want_usage)
356 usage();
357 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000358 }
359
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000360 /* If debugging info is disabled, suppress any debug calls */
361 if (!using_debug_info)
362 ofmt->current_dfmt = &null_debug_form;
363
H. Peter Anvin76690a12002-04-30 20:52:49 +0000364 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400365 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700366 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000369 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800370 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000371
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400372 depend_ptr = (depend_file || (operating_mode == op_depend)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700373 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400374 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700375
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 switch (operating_mode) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000377 case op_depend:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000379 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700380
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400381 if (depend_missing_ok)
382 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700383
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700384 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400386 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 while ((line = preproc->getline()))
389 nasm_free(line);
390 preproc->cleanup(0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391 }
392 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000393
394 case op_preprocess:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000396 char *line;
397 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000398 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000400
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 if (*outname) {
402 ofile = fopen(outname, "w");
403 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400404 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 "unable to open output file `%s'",
406 outname);
407 } else
408 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000409
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700410 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000411
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400412 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700413 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400414 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700415
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 while ((line = preproc->getline())) {
417 /*
418 * We generate %line directives if needed for later programs
419 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000420 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 int altline = src_get(&linnum, &file_name);
422 if (altline) {
423 if (altline == 1 && lineinc == 1)
424 nasm_fputs("", ofile);
425 else {
426 lineinc = (altline != -1 || lineinc != 1);
427 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000428 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 file_name);
430 }
431 prior_linnum = linnum;
432 }
433 nasm_fputs(line, ofile);
434 nasm_free(line);
435 }
436 nasm_free(file_name);
437 preproc->cleanup(0);
438 if (ofile)
439 fclose(ofile);
440 if (ofile && terminate_after_phase)
441 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400442 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 }
444 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000445
446 case op_normal:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 {
448 /*
449 * We must call ofmt->filename _anyway_, even if the user
450 * has specified their own output file, because some
451 * formats (eg OBJ and COFF) use ofmt->filename to find out
452 * the name of the input file and then put that inside the
453 * file.
454 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400455 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456
H. Peter Anvin0cba1072009-07-05 14:45:12 -0700457 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 if (!ofile) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400459 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 "unable to open output file `%s'", outname);
461 }
462
463 /*
464 * We must call init_labels() before ofmt->init() since
465 * some object formats will want to define labels in their
466 * init routines. (eg OS/2 defines the FLAT group)
467 */
468 init_labels();
469
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400470 ofmt->init();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400471 dfmt = ofmt->current_dfmt;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400472 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700474 assemble_file(inname, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475
476 if (!terminate_after_phase) {
477 ofmt->cleanup(using_debug_info);
478 cleanup_labels();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400479 fflush(ofile);
480 if (ferror(ofile)) {
481 nasm_error(ERR_NONFATAL|ERR_NOFILE,
482 "write error on output file `%s'", outname);
483 }
484 }
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200485
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400486 if (ofile) {
487 fclose(ofile);
488 if (terminate_after_phase)
489 remove(outname);
490 ofile = NULL;
491 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 }
493 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000495
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700496 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400497 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700498
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000499 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000501
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 raa_free(offsets);
503 saa_free(forwrefs);
504 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000505 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700507 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000508}
509
H. Peter Anvineba20a72002-04-30 20:53:55 +0000510/*
511 * Get a parameter for a command line option.
512 * First arg must be in the form of e.g. -f...
513 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800514static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800516 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400517 if (p[2]) /* the parameter's in the option */
518 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800520 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400523 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525 return NULL;
526}
527
H. Peter Anvindc242712007-11-18 11:55:10 -0800528/*
529 * Copy a filename
530 */
531static void copy_filename(char *dst, const char *src)
532{
533 size_t len = strlen(src);
534
535 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400536 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
537 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800538 }
539 strncpy(dst, src, FILENAME_MAX);
540}
541
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700542/*
543 * Convert a string to Make-safe form
544 */
545static char *quote_for_make(const char *str)
546{
547 const char *p;
548 char *os, *q;
549
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400550 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700551 size_t nbs = 0;
552
553 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400554 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700555
556 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400557 switch (*p) {
558 case ' ':
559 case '\t':
560 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
561 n += nbs + 2;
562 nbs = 0;
563 break;
564 case '$':
565 case '#':
566 nbs = 0;
567 n += 2;
568 break;
569 case '\\':
570 nbs++;
571 n++;
572 break;
573 default:
574 nbs = 0;
575 n++;
576 break;
577 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700578 }
579
580 /* Convert N backslashes at the end of filename to 2N backslashes */
581 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400582 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700583
584 os = q = nasm_malloc(n);
585
586 nbs = 0;
587 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400588 switch (*p) {
589 case ' ':
590 case '\t':
591 while (nbs--)
592 *q++ = '\\';
593 *q++ = '\\';
594 *q++ = *p;
595 break;
596 case '$':
597 *q++ = *p;
598 *q++ = *p;
599 nbs = 0;
600 break;
601 case '#':
602 *q++ = '\\';
603 *q++ = *p;
604 nbs = 0;
605 break;
606 case '\\':
607 *q++ = *p;
608 nbs++;
609 break;
610 default:
611 *q++ = *p;
612 nbs = 0;
613 break;
614 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700615 }
616 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400617 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700618
619 *q = '\0';
620
621 return os;
622}
623
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000625 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000627};
628
629#define OPT_PREFIX 0
630#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631struct textargs textopts[] = {
632 {"prefix", OPT_PREFIX},
633 {"postfix", OPT_POSTFIX},
634 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000635};
636
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400637static void show_version(void)
638{
639 printf("NASM version %s compiled on %s%s\n",
640 nasm_version, nasm_date, nasm_compile_options);
641 exit(0);
642}
643
H. Peter Anvin423e3812007-11-15 17:12:29 -0800644static bool stopoptions = false;
645static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000646{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000647 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800648 int i;
649 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700650 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000651
652 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800653 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400656 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
657 /* These parameters take values */
658 if (!(param = get_param(p, q, &advance)))
659 return advance;
660 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800661
H. Peter Anvine2c80182005-01-15 22:15:51 +0000662 switch (p[1]) {
663 case 's':
664 error_file = stdout;
665 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700666
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400667 case 'o': /* output file */
668 copy_filename(outname, param);
669 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700670
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400671 case 'f': /* output format */
672 ofmt = ofmt_find(param, &ofmt_alias);
673 if (!ofmt) {
674 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
675 "unrecognised output format `%s' - "
676 "use -hf for a list", param);
677 }
678 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700679
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400680 case 'O': /* Optimization level */
681 {
682 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700683
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400684 if (!*param) {
685 /* Naked -O == -Ox */
686 optimizing = MAX_OPTIMIZE;
687 } else {
688 while (*param) {
689 switch (*param) {
690 case '0': case '1': case '2': case '3': case '4':
691 case '5': case '6': case '7': case '8': case '9':
692 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700693
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400694 /* -O0 -> optimizing == -1, 0.98 behaviour */
695 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
696 if (opt < 2)
697 optimizing = opt - 1;
698 else
699 optimizing = opt;
700 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700701
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400702 case 'v':
703 case '+':
704 param++;
705 opt_verbose_info = true;
706 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700707
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400708 case 'x':
709 param++;
710 optimizing = MAX_OPTIMIZE;
711 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700712
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400713 default:
714 nasm_error(ERR_FATAL,
715 "unknown optimization option -O%c\n",
716 *param);
717 break;
718 }
719 }
720 if (optimizing > MAX_OPTIMIZE)
721 optimizing = MAX_OPTIMIZE;
722 }
723 break;
724 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800725
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400726 case 'p': /* pre-include */
727 case 'P':
728 preproc->pre_include(param);
729 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800730
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400731 case 'd': /* pre-define */
732 case 'D':
733 preproc->pre_define(param);
734 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800735
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400736 case 'u': /* un-define */
737 case 'U':
738 preproc->pre_undefine(param);
739 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800740
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400741 case 'i': /* include search path */
742 case 'I':
743 preproc->include_path(param);
744 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800745
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400746 case 'l': /* listing file */
747 copy_filename(listname, param);
748 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800749
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400750 case 'Z': /* error messages file */
751 copy_filename(errname, param);
752 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800753
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400754 case 'F': /* specify debug format */
755 ofmt->current_dfmt = dfmt_find(ofmt, param);
756 if (!ofmt->current_dfmt) {
757 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
758 "unrecognized debug format `%s' for"
759 " output format `%s'",
760 param, ofmt->shortname);
761 }
762 using_debug_info = true;
763 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800764
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400765 case 'X': /* specify error reporting format */
766 if (nasm_stricmp("vc", param) == 0)
767 nasm_set_verror(nasm_verror_vc);
768 else if (nasm_stricmp("gnu", param) == 0)
769 nasm_set_verror(nasm_verror_gnu);
770 else
771 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
772 "unrecognized error reporting format `%s'",
773 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800775
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700777 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800779
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 case 'h':
781 printf
782 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
783 "[-l listfile]\n"
784 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400785 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400787 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700789 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700791 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400792 " -MG d:o, missing files assumed generated\n"
793 " -MF <file> set Makefile dependency file\n"
794 " -MD <file> assemble and generate dependencies\n"
795 " -MT <file> dependency target name\n"
796 " -MQ <file> dependency target name (quoted)\n"
797 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700798 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 " -s redirect error messages to stdout\n\n"
800 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400801 " -o outfile write output to an outfile\n\n"
802 " -f format select an output format\n\n"
803 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 " -I<path> adds a pathname to the include file path\n");
805 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700806 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400807 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400808 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400809 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 " -P<file> pre-includes a file\n"
811 " -D<macro>[=<value>] pre-defines a macro\n"
812 " -U<macro> undefines a macro\n"
813 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800814 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400815 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400816 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400817 "--prefix,--postfix\n"
818 " this options prepend or append the given argument to all\n"
819 " extern and global variables\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800820 "Warnings:\n");
821 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700823 warnings[i].name, warnings[i].help,
824 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 printf
826 ("\nresponse files should contain command line parameters"
827 ", one per line.\n");
828 if (p[2] == 'f') {
829 printf("\nvalid output formats for -f are"
830 " (`*' denotes default):\n");
831 ofmt_list(ofmt, stdout);
832 } else {
833 printf("\nFor a list of valid output formats, use -hf.\n");
834 printf("For a list of debug formats, use -f <form> -y.\n");
835 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400836 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 case 'y':
840 printf("\nvalid debug formats for '%s' output format are"
841 " ('*' denotes default):\n", ofmt->shortname);
842 dfmt_list(ofmt, stdout);
843 exit(0);
844 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800845
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700847 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800849
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400851 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800853
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400854 case 'e': /* preprocess only */
855 case 'E':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 operating_mode = op_preprocess;
857 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800858
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400859 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400860 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800862
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400863 case 'W':
864 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
865 do_warn = false;
866 param += 3;
867 } else {
868 do_warn = true;
869 }
870 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800871
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800873 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400874 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400876 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400878 do_warn = (param[0] == '+');
879 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400880
881set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400882 for (i = 0; i <= ERR_WARN_MAX; i++) {
883 if (!nasm_stricmp(param, warnings[i].name))
884 break;
885 }
886 if (i <= ERR_WARN_MAX) {
887 warning_on_global[i] = do_warn;
888 } else if (!nasm_stricmp(param, "all")) {
889 for (i = 1; i <= ERR_WARN_MAX; i++)
890 warning_on_global[i] = do_warn;
891 } else if (!nasm_stricmp(param, "none")) {
892 for (i = 1; i <= ERR_WARN_MAX; i++)
893 warning_on_global[i] = !do_warn;
894 } else {
895 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
896 "invalid warning `%s'", param);
897 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800899
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400901 switch (p[2]) {
902 case 0:
903 operating_mode = op_depend;
904 break;
905 case 'G':
906 operating_mode = op_depend;
907 depend_missing_ok = true;
908 break;
909 case 'P':
910 depend_emit_phony = true;
911 break;
912 case 'D':
913 depend_file = q;
914 advance = true;
915 break;
916 case 'T':
917 depend_target = q;
918 advance = true;
919 break;
920 case 'Q':
921 depend_target = quote_for_make(q);
922 advance = true;
923 break;
924 default:
925 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
926 "unknown dependency option `-M%c'", p[2]);
927 break;
928 }
929 if (advance && (!q || !q[0])) {
930 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
931 "option `-M%c' requires a parameter", p[2]);
932 break;
933 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000935
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 case '-':
937 {
938 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000939
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 if (p[2] == 0) { /* -- => stop processing options */
941 stopoptions = 1;
942 break;
943 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400944
945 if (!nasm_stricmp(p, "--v"))
946 show_version();
947
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 for (s = 0; textopts[s].label; s++) {
949 if (!nasm_stricmp(p + 2, textopts[s].label)) {
950 break;
951 }
952 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000953
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 switch (s) {
955
956 case OPT_PREFIX:
957 case OPT_POSTFIX:
958 {
959 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400960 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 ERR_USAGE,
962 "option `--%s' requires an argument",
963 p + 2);
964 break;
965 } else {
966 advance = 1, param = q;
967 }
968
969 if (s == OPT_PREFIX) {
970 strncpy(lprefix, param, PREFIX_MAX - 1);
971 lprefix[PREFIX_MAX - 1] = 0;
972 break;
973 }
974 if (s == OPT_POSTFIX) {
975 strncpy(lpostfix, param, POSTFIX_MAX - 1);
976 lpostfix[POSTFIX_MAX - 1] = 0;
977 break;
978 }
979 break;
980 }
981 default:
982 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400983 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 "unrecognised option `--%s'", p + 2);
985 break;
986 }
987 }
988 break;
989 }
990
991 default:
992 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400993 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 "unrecognised option `-%c'", p[1]);
995 break;
996 }
997 } else {
998 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400999 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001001 } else {
1002 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001004 }
1005
1006 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001007}
1008
H. Peter Anvineba20a72002-04-30 20:53:55 +00001009#define ARG_BUF_DELTA 128
1010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001012{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001013 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001014 int bufsize, prevargsize;
1015
1016 bufsize = prevargsize = ARG_BUF_DELTA;
1017 buffer = nasm_malloc(ARG_BUF_DELTA);
1018 prevarg = nasm_malloc(ARG_BUF_DELTA);
1019 prevarg[0] = '\0';
1020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 p = buffer;
1023 while (1) { /* Loop to handle long lines */
1024 q = fgets(p, bufsize - (p - buffer), rfile);
1025 if (!q)
1026 break;
1027 p += strlen(p);
1028 if (p > buffer && p[-1] == '\n')
1029 break;
1030 if (p - buffer > bufsize - 10) {
1031 int offset;
1032 offset = p - buffer;
1033 bufsize += ARG_BUF_DELTA;
1034 buffer = nasm_realloc(buffer, bufsize);
1035 p = buffer + offset;
1036 }
1037 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 if (!q && p == buffer) {
1040 if (prevarg[0])
1041 process_arg(prevarg, NULL);
1042 nasm_free(buffer);
1043 nasm_free(prevarg);
1044 return;
1045 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001046
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 /*
1048 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1049 * them are present at the end of the line.
1050 */
1051 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001053 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001055
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001056 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 if (process_arg(prevarg, p))
1059 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060
Charles Crayne192d5b52007-10-18 19:02:42 -07001061 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 prevargsize += ARG_BUF_DELTA;
1063 prevarg = nasm_realloc(prevarg, prevargsize);
1064 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001065 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001066 }
1067}
1068
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001069/* Function to process args from a string of args, rather than the
1070 * argv array. Used by the environment variable and response file
1071 * processing.
1072 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001073static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001075 char *p, *q, *arg, *prevarg;
1076 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001077
1078 p = args;
1079 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001081 arg = NULL;
1082 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 q = p;
1084 while (*p && *p != separator)
1085 p++;
1086 while (*p == separator)
1087 *p++ = '\0';
1088 prevarg = arg;
1089 arg = q;
1090 if (process_arg(prevarg, arg))
1091 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001092 }
1093 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001095}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001096
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001097static void process_response_file(const char *file)
1098{
1099 char str[2048];
1100 FILE *f = fopen(file, "r");
1101 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001102 perror(file);
1103 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001104 }
1105 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001106 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001107 }
1108 fclose(f);
1109}
1110
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001111static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001112{
1113 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001114 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001115 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001116
Charles Craynefcce07f2007-09-30 22:15:36 -07001117 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001118
H. Peter Anvin972079f2008-09-30 17:01:23 -07001119 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001120 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001121
1122 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001123 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001124 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001125 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001126 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 envcopy = nasm_strdup(envreal);
1128 process_args(envcopy);
1129 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001130 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001131
1132 /*
1133 * Now process the actual command line.
1134 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001136 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 argv++;
1138 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001139 /*
1140 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 * arguments like the environment variable. This allows us
1142 * to have multiple arguments on a single line, which is
1143 * different to the -@resp file processing below for regular
1144 * NASM.
1145 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001146 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 argc--;
1148 argv++;
1149 }
1150 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001151 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001152 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001153 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001154 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 process_respfile(rfile);
1156 fclose(rfile);
1157 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001158 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 "unable to open response file `%s'", p);
1160 }
1161 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001162 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1163 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001164 }
1165
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001166 /*
1167 * Look for basic command line typos. This definitely doesn't
1168 * catch all errors, but it might help cases of fumbled fingers.
1169 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001170 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001171 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001172 "no input file specified");
1173 else if (!strcmp(inname, errname) ||
1174 !strcmp(inname, outname) ||
1175 !strcmp(inname, listname) ||
1176 (depend_file && !strcmp(inname, depend_file)))
1177 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1178 "file `%s' is both input and output file",
1179 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001180
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001182 error_file = fopen(errname, "w");
1183 if (!error_file) {
1184 error_file = stderr; /* Revert to default! */
1185 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1186 "cannot open file `%s' for error messages",
1187 errname);
1188 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001189 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001190}
1191
H. Peter Anvin70055962007-10-11 00:05:31 -07001192static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001193
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001194static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001195{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001196 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001198 int i, validid;
1199 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001200 int32_t seg;
1201 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001202 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001204 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001205
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001206 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001207 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001209
Charles Craynec1905c22008-09-11 18:54:06 -07001210 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001211 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 int pass1, pass2;
1213 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001215 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001216 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1217 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001219 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001220
Keith Kaniosb7a89542007-04-12 02:40:54 +00001221 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 cpu = cmd_cpu;
1223 if (pass0 == 2) {
1224 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001225 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001227 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001228 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001230 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001231 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 saa_rewind(forwrefs);
1233 forwref = saa_rstruct(forwrefs);
1234 raa_free(offsets);
1235 offsets = raa_init();
1236 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001237 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001238 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001239 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001240
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001242 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001243 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001244 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001245
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001247 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001249
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001250 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001251 * Here we parse our directives; this is not handled by the
1252 * 'real' parser. This really should be a separate function.
1253 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001255 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001256 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001257 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001258
H. Peter Anvin70055962007-10-11 00:05:31 -07001259 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001260 case D_SEGMENT: /* [SEGMENT n] */
1261 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 seg = ofmt->section(value, pass2, &sb);
1263 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001264 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001265 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 value);
1267 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001268 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 location.segment = seg;
1270 }
1271 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001272 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001273 if (*value) {
1274 stdscan_reset();
1275 stdscan_set(value);
1276 tokval.t_type = TOKEN_INVALID;
1277 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1278 if (e) {
1279 unsigned int align = (unsigned int)e->value;
1280 if ((uint64_t)e->value > 0x7fffffff) {
1281 /*
1282 * FIXME: Please make some sane message here
1283 * ofmt should have some 'check' method which
1284 * would report segment alignment bounds.
1285 */
1286 nasm_error(ERR_FATAL,
1287 "incorrect segment alignment `%s'", value);
1288 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001289 nasm_error(ERR_NONFATAL,
1290 "segment alignment `%s' is not power of two",
1291 value);
1292 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001293 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001294 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001295 }
1296 }
1297 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001298 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 if (*value == '$')
1300 value++; /* skip initial $ if present */
1301 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001302 q = value;
1303 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001305 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 *q++ = '\0';
1307 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001309 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001311 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001313 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001316 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 }
1319 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001320 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 "identifier expected after EXTERN");
1322 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 }
1324 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 *q++ = '\0';
1326 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 special = NULL;
1329 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1330 int temp = pass0;
1331 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001332 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001334 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001336 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 } /* else pass0 == 1 */
1338 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001339 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001340 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001342 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 if (*value == '$')
1344 value++; /* skip initial $ if present */
1345 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001346 q = value;
1347 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001349 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 *q++ = '\0';
1351 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001355 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001357 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001360 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001362 }
1363 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001364 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 "identifier expected after GLOBAL");
1366 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367 }
1368 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 *q++ = '\0';
1370 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001371 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001373 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 } /* pass == 1 */
1375 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001376 case D_COMMON: /* [COMMON symbol size:special] */
1377 {
1378 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001379
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 if (*value == '$')
1381 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001382 p = value;
1383 validid = true;
1384 if (!isidstart(*p))
1385 validid = false;
1386 while (*p && !nasm_isspace(*p)) {
1387 if (!isidchar(*p))
1388 validid = false;
1389 p++;
1390 }
1391 if (!validid) {
1392 nasm_error(ERR_NONFATAL,
1393 "identifier expected after COMMON");
1394 break;
1395 }
1396 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001397 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001398 q = p;
1399 while (*q && *q != ':')
1400 q++;
1401 if (*q == ':') {
1402 *q++ = '\0';
1403 special = q;
1404 } else {
1405 special = NULL;
1406 }
1407 size = readnum(p, &rn_error);
1408 if (rn_error) {
1409 nasm_error(ERR_NONFATAL,
1410 "invalid size specified"
1411 " in COMMON declaration");
1412 break;
1413 }
1414 } else {
1415 nasm_error(ERR_NONFATAL,
1416 "no size specified in"
1417 " COMMON declaration");
1418 break;
1419 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001420
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001421 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001422 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001423 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001424 if (special)
1425 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 }
1427 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001428 }
1429 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001431 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 tokval.t_type = TOKEN_INVALID;
1433 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001434 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 if (e) {
1436 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001437 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 1 ? ERR_NONFATAL : ERR_PANIC,
1439 "cannot use non-relocatable expression as "
1440 "ABSOLUTE address");
1441 else {
1442 abs_seg = reloc_seg(e);
1443 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001444 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001445 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 abs_offset = 0x100; /* don't go near zero in case of / */
1447 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001448 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001450 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 location.segment = NO_SEG;
1452 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001453 case D_DEBUG: /* [DEBUG] */
1454 {
1455 char debugid[128];
1456 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001457
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 p = value;
1459 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001460 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001461 if (!isidstart(*p)) {
1462 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001463 } else {
1464 while (*p && !nasm_isspace(*p)) {
1465 if (q >= debugid + sizeof debugid - 1) {
1466 overlong = true;
1467 break;
1468 }
1469 if (!isidchar(*p))
1470 badid = true;
1471 *q++ = *p++;
1472 }
1473 *q = 0;
1474 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001475 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001476 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1477 "identifier expected after DEBUG");
1478 break;
1479 }
1480 if (overlong) {
1481 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1482 "DEBUG identifier too long");
1483 break;
1484 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001485 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001486 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001487 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001489 }
1490 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001491 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001492 switch(*value) {
1493 case '-': validid = 0; value++; break;
1494 case '+': validid = 1; value++; break;
1495 case '*': validid = 2; value++; break;
1496 default: validid = 1; break;
1497 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001499 for (i = 1; i <= ERR_WARN_MAX; i++)
1500 if (!nasm_stricmp(value, warnings[i].name))
1501 break;
1502 if (i <= ERR_WARN_MAX) {
1503 switch(validid) {
1504 case 0:
1505 warning_on[i] = false;
1506 break;
1507 case 1:
1508 warning_on[i] = true;
1509 break;
1510 case 2:
1511 warning_on[i] = warning_on_global[i];
1512 break;
1513 }
1514 } else
1515 nasm_error(ERR_NONFATAL,
1516 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001518 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 cpu = get_cpu(value);
1520 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001521 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001522 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 if (*value == '+') {
1524 user_nolist = 0;
1525 } else {
1526 if (*value == '-') {
1527 user_nolist = 1;
1528 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001529 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 }
1531 }
1532 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001533 case D_DEFAULT: /* [DEFAULT] */
1534 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001535 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001536 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001537 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001538 switch ((int)tokval.t_integer) {
1539 case S_REL:
1540 globalrel = 1;
1541 break;
1542 case S_ABS:
1543 globalrel = 0;
1544 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001545 case P_BND:
1546 globalbnd = 1;
1547 break;
1548 case P_NOBND:
1549 globalbnd = 0;
1550 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001551 default:
1552 err = 1;
1553 break;
1554 }
1555 } else {
1556 err = 1;
1557 }
1558 break;
1559 case D_FLOAT:
1560 if (float_option(value)) {
1561 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1562 "unknown 'float' directive: %s",
1563 value);
1564 }
1565 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001567 if (ofmt->directive(d, value, pass2))
1568 break;
1569 /* else fall through */
1570 case D_unknown:
1571 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1572 "unrecognised directive [%s]",
1573 directive);
1574 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001576 if (err) {
1577 nasm_error(ERR_NONFATAL,
1578 "invalid parameter to [%s] directive",
1579 directive);
1580 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001582 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583
Charles Crayne2581c862008-09-10 19:21:52 -07001584 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001586 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001588 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 forwref = saa_rstruct(forwrefs);
1590 } while (forwref != NULL
1591 && forwref->lineno == globallineno);
1592 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001593 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001594
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001595 if (output_ins.forw_ref) {
1596 if (passn == 1) {
1597 for (i = 0; i < output_ins.operands; i++) {
1598 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1599 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1600 fwinf->lineno = globallineno;
1601 fwinf->operand = i;
1602 }
1603 }
1604 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001605 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001607
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 /* forw_ref */
1609 if (output_ins.opcode == I_EQU) {
1610 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001611 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 * Special `..' EQUs get processed in pass two,
1613 * except `..@' macro-processor EQUs which are done
1614 * in the normal place.
1615 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001617 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001619
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001620 else if (output_ins.label[0] != '.' ||
1621 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 output_ins.label[2] == '@') {
1623 if (output_ins.operands == 1 &&
1624 (output_ins.oprs[0].type & IMMEDIATE) &&
1625 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001626 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 def_label(output_ins.label,
1628 output_ins.oprs[0].segment,
1629 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001630 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001632 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001634 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001636 && (output_ins.oprs[1].type & IMMEDIATE)
1637 && output_ins.oprs[1].segment == NO_SEG
1638 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001640 output_ins.oprs[0].offset | SEG_ABS,
1641 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001642 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001644 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001646 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001647 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001648 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 * Special `..' EQUs get processed here, except
1650 * `..@' macro processor EQUs which are done above.
1651 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 output_ins.label[1] == '.' &&
1654 output_ins.label[2] != '@') {
1655 if (output_ins.operands == 1 &&
1656 (output_ins.oprs[0].type & IMMEDIATE)) {
1657 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658 output_ins.oprs[0].segment,
1659 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001660 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001662 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001664 && output_ins.oprs[0].segment == NO_SEG
1665 && (output_ins.oprs[1].type & IMMEDIATE)
1666 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001668 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001669 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001670 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001672 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001674 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001675 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001677
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001679
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001680 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001681 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001682
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 /* if (using_debug_info) && output_ins.opcode != -1) */
1684 if (using_debug_info)
1685 { /* fbk 03/25/01 */
1686 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001687 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 TYS_ELEMENTS(output_ins.operands);
1689 switch (output_ins.opcode) {
1690 case I_RESB:
1691 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001692 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 break;
1694 case I_RESW:
1695 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001696 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 break;
1698 case I_RESD:
1699 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001700 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 break;
1702 case I_RESQ:
1703 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001704 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 break;
1706 case I_REST:
1707 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001708 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001710 case I_RESO:
1711 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001712 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001713 break;
1714 case I_RESY:
1715 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001716 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001717 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 case I_DB:
1719 typeinfo |= TY_BYTE;
1720 break;
1721 case I_DW:
1722 typeinfo |= TY_WORD;
1723 break;
1724 case I_DD:
1725 if (output_ins.eops_float)
1726 typeinfo |= TY_FLOAT;
1727 else
1728 typeinfo |= TY_DWORD;
1729 break;
1730 case I_DQ:
1731 typeinfo |= TY_QWORD;
1732 break;
1733 case I_DT:
1734 typeinfo |= TY_TBYTE;
1735 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001736 case I_DO:
1737 typeinfo |= TY_OWORD;
1738 break;
1739 case I_DY:
1740 typeinfo |= TY_YWORD;
1741 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 default:
1743 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001744
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001746
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001747 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001748 }
1749 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001751 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001752 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001753 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 * else l == -1 => invalid instruction, which will be
1755 * flagged as an error on pass 2
1756 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001757
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001758 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001760 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001762 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001763
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 }
1765 } /* not an EQU */
1766 cleanup_insn(&output_ins);
1767 }
1768 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001769 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001771
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001772 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001773 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001774 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001775
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 if (pass1 == 1)
1777 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001778
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001779 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001781 } else if (global_offset_changed &&
1782 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001783 prev_offset_changed = global_offset_changed;
1784 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001785 } else {
1786 stall_count++;
1787 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001788
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001789 if (terminate_after_phase)
1790 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001791
1792 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001793 /* We get here if the labels don't converge
1794 * Example: FOO equ FOO + 1
1795 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001796 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001797 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001798 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001799 nasm_error(ERR_NONFATAL,
1800 "Possible causes: recursive EQUs, macro abuse.");
1801 break;
1802 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001803 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001804
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 preproc->cleanup(0);
1806 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001807 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001808 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001809 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1810 }
1811}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001812
H. Peter Anvin70055962007-10-11 00:05:31 -07001813static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001814{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001815 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001817 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001818
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001819 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001821 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001822 q = strchr(buf, ']');
1823 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001824 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001825
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001826 /* stip off the comments */
1827 p = strchr(buf, ';');
1828 if (p) {
1829 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001830 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001831 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001832 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001833
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001834 /* no brace, no trailing spaces */
1835 *q = '\0';
1836 nasm_zap_spaces_rev(--q);
1837
1838 /* directive */
1839 p = nasm_skip_spaces(++buf);
1840 q = nasm_skip_word(p);
1841 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001842 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001843 *q = '\0';
1844 *directive = p;
1845
1846 /* and value finally */
1847 p = nasm_skip_spaces(++q);
1848 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001849
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001850 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001851}
1852
Ed Berosetfa771012002-06-09 20:56:40 +00001853/**
1854 * gnu style error reporting
1855 * This function prints an error message to error_file in the
1856 * style used by GNU. An example would be:
1857 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001858 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001859 * which the error occurs (or is detected) and "error:" is one of
1860 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001861 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001862 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001863 *
1864 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001865 * @param fmt the printf style format string
1866 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001867static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001868{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001869 char *currentfile = NULL;
1870 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001871
Ed Berosetfa771012002-06-09 20:56:40 +00001872 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001874
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001875 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001877
1878 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001879 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1880 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001881 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001882 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001883 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001884
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001885 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001886}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001887
Ed Berosetfa771012002-06-09 20:56:40 +00001888/**
1889 * MS style error reporting
1890 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001891 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001892 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001893 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001894 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001895 * which the error occurs (or is detected) and "error:" is one of
1896 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001897 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001898 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001899 *
1900 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001901 * @param fmt the printf style format string
1902 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001903static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001904{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001905 char *currentfile = NULL;
1906 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001907
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 if (is_suppressed_warning(severity))
1909 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001910
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001911 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001913
1914 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001915 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001917 } else {
1918 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001919 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001920
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001921 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001922}
1923
1924/**
1925 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001926 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001927 * not in pass 1
1928 *
1929 * @param severity the severity of the warning or error
1930 * @return true if we should abort error/warning printing
1931 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001932static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001933{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001934 /* Not a warning at all */
1935 if ((severity & ERR_MASK) != ERR_WARNING)
1936 return false;
1937
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001938 /* See if it's a pass-one only warning and we're not in pass one. */
1939 if (((severity & ERR_PASS1) && pass0 != 1) ||
1940 ((severity & ERR_PASS2) && pass0 != 2))
1941 return true;
1942
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001943 /* Might be a warning but suppresed explicitly */
1944 if (severity & ERR_WARN_MASK)
1945 return !warning_on[WARN_IDX(severity)];
1946 else
1947 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001948}
1949
1950/**
1951 * common error reporting
1952 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001953 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001954 * specific error message to error_file and may or may not return. It
1955 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001956 *
1957 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001958 * @param fmt the printf style format string
1959 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001960static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001961{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001962 char msg[1024];
1963 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001964
H. Peter Anvin7df04172008-06-10 18:27:38 -07001965 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001967 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 break;
1969 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001970 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 break;
1972 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001973 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 break;
1975 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001976 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 break;
1978 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001979 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001981 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001982 pfx = "";
1983 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001984 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001985
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001986 vsnprintf(msg, sizeof msg, fmt, args);
1987
1988 fprintf(error_file, "%s%s\n", pfx, msg);
1989
1990 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001991 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001992
1993 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001994 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001995
1996 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 case ERR_DEBUG:
1998 /* no further action, by definition */
1999 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002000 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002001 /* Treat warnings as errors */
2002 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2003 terminate_after_phase = true;
2004 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002006 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 break;
2008 case ERR_FATAL:
2009 if (ofile) {
2010 fclose(ofile);
2011 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002012 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 }
2014 if (want_usage)
2015 usage();
2016 exit(1); /* instantly die */
2017 break; /* placate silly compilers */
2018 case ERR_PANIC:
2019 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002020 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 exit(3);
2022 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002023 }
2024}
2025
H. Peter Anvin734b1882002-04-30 21:01:08 +00002026static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002027{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002028 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002029}
2030
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002031static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002032{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002033 iflag_t r;
2034
2035 iflag_clear_all(&r);
2036
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002038 iflag_set(&r, IF_8086);
2039 else if (!strcmp(value, "186"))
2040 iflag_set(&r, IF_186);
2041 else if (!strcmp(value, "286"))
2042 iflag_set(&r, IF_286);
2043 else if (!strcmp(value, "386"))
2044 iflag_set(&r, IF_386);
2045 else if (!strcmp(value, "486"))
2046 iflag_set(&r, IF_486);
2047 else if (!strcmp(value, "586") ||
2048 !nasm_stricmp(value, "pentium"))
2049 iflag_set(&r, IF_PENT);
2050 else if (!strcmp(value, "686") ||
2051 !nasm_stricmp(value, "ppro") ||
2052 !nasm_stricmp(value, "pentiumpro") ||
2053 !nasm_stricmp(value, "p2"))
2054 iflag_set(&r, IF_P6);
2055 else if (!nasm_stricmp(value, "p3") ||
2056 !nasm_stricmp(value, "katmai"))
2057 iflag_set(&r, IF_KATMAI);
2058 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2059 !nasm_stricmp(value, "willamette"))
2060 iflag_set(&r, IF_WILLAMETTE);
2061 else if (!nasm_stricmp(value, "prescott"))
2062 iflag_set(&r, IF_PRESCOTT);
2063 else if (!nasm_stricmp(value, "x64") ||
2064 !nasm_stricmp(value, "x86-64"))
2065 iflag_set(&r, IF_X86_64);
2066 else if (!nasm_stricmp(value, "ia64") ||
2067 !nasm_stricmp(value, "ia-64") ||
2068 !nasm_stricmp(value, "itanium")||
2069 !nasm_stricmp(value, "itanic") ||
2070 !nasm_stricmp(value, "merced"))
2071 iflag_set(&r, IF_IA64);
2072 else {
2073 iflag_set(&r, IF_PLEVEL);
2074 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2075 "unknown 'cpu' type");
2076 }
2077 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002078}
2079
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002080static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002081{
2082 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002083
H. Peter Anvine2c80182005-01-15 22:15:51 +00002084 if ((i = atoi(value)) == 16)
2085 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002086 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002087 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002088 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002089 "cannot specify 32-bit segment on processor below a 386");
2090 i = 16;
2091 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002092 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002093 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002094 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002095 "cannot specify 64-bit segment on processor below an x86-64");
2096 i = 16;
2097 }
2098 if (i != maxbits) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002099 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002100 "%s output format does not support 64-bit code",
2101 ofmt->shortname);
2102 i = 16;
2103 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002104 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002105 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002106 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 value);
2108 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002109 }
2110 return i;
2111}