blob: e48f0a99d1bd347e17bf64b4b7680dc023d5b017 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01003 * Copyright 1996-2016 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
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400131#define OP_NORMAL (1u << 0)
132#define OP_PREPROCESS (1u << 1)
133#define OP_DEPEND (1u << 2)
134
135static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400136
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 Anvind24dd5f2016-02-08 10:32:13 -0800171 {"zero-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000172};
173
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700174static bool want_usage;
175static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000176int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
H. Peter Anvin55340992012-09-09 17:09:00 -0700178static char *quote_for_make(const char *str);
179
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400180static int64_t get_curr_offs(void)
181{
182 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
183}
184
185static void set_curr_offs(int64_t l_off)
186{
187 if (in_abs_seg)
188 abs_offset = l_off;
189 else
190 offsets = raa_write(offsets, location.segment, l_off);
191}
192
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000193static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000195 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000196 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800197 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000200}
201
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800202/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800203static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800204{
205 int64_t t;
206 int64_t y = tm->tm_year;
207
208 /* See IEEE 1003.1:2004, section 4.14 */
209
210 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
211 t += tm->tm_yday;
212 t *= 24;
213 t += tm->tm_hour;
214 t *= 60;
215 t += tm->tm_min;
216 t *= 60;
217 t += tm->tm_sec;
218
219 return t;
220}
221
222static void define_macros_early(void)
223{
224 char temp[128];
225 struct tm lt, *lt_p, gm, *gm_p;
226 int64_t posix_time;
227
228 lt_p = localtime(&official_compile_time);
229 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400230 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800231
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400232 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
233 preproc->pre_define(temp);
234 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
235 preproc->pre_define(temp);
236 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
237 preproc->pre_define(temp);
238 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
239 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800240 }
241
242 gm_p = gmtime(&official_compile_time);
243 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400244 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800245
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400246 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
247 preproc->pre_define(temp);
248 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
249 preproc->pre_define(temp);
250 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
251 preproc->pre_define(temp);
252 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
253 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800254 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700255
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800256 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800257 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800259 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400261 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262
263 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
265 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800266 }
267}
268
269static void define_macros_late(void)
270{
271 char temp[128];
272
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400273 /*
274 * In case if output format is defined by alias
275 * we have to put shortname of the alias itself here
276 * otherwise ABI backward compatibility gets broken.
277 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400278 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400279 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400280 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800281}
282
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700283static void emit_dependencies(StrList *list)
284{
285 FILE *deps;
286 int linepos, len;
287 StrList *l, *nl;
288
289 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400290 deps = fopen(depend_file, "w");
291 if (!deps) {
292 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
293 "unable to write dependency file `%s'", depend_file);
294 return;
295 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700296 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400297 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700298 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700299
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700300 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400301 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700302 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400303 len = strlen(file);
304 if (linepos + len > 62 && linepos > 1) {
305 fprintf(deps, " \\\n ");
306 linepos = 1;
307 }
308 fprintf(deps, " %s", file);
309 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700310 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700311 }
312 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700313
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400314 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400315 if (depend_emit_phony)
316 fprintf(deps, "%s:\n\n", l->str);
317 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700318 }
319
320 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400321 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700322}
323
H. Peter Anvin038d8612007-04-12 16:54:50 +0000324int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000325{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700327
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800328 time(&official_compile_time);
329
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400330 iflag_set(&cpu, IF_PLEVEL);
331 iflag_set(&cmd_cpu, IF_PLEVEL);
332
Charles Crayne2581c862008-09-10 19:21:52 -0700333 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700334 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400335 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000336
H. Peter Anvin97e15752007-09-25 08:47:47 -0700337 error_file = stderr;
338
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700339 tolower_init();
340
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000341 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000342 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000343
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400345 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000346
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000347 seg_init();
348
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800349 /* Define some macros dependent on the runtime, but not
350 on the command line. */
351 define_macros_early();
352
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353 parse_cmdline(argc, argv);
354
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 if (terminate_after_phase) {
356 if (want_usage)
357 usage();
358 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359 }
360
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000361 /* If debugging info is disabled, suppress any debug calls */
362 if (!using_debug_info)
363 ofmt->current_dfmt = &null_debug_form;
364
H. Peter Anvin76690a12002-04-30 20:52:49 +0000365 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400366 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700367 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000370 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800371 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000372
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400373 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700374 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400375 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700376
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400377 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700379
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400380 if (depend_missing_ok)
381 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700382
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700383 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400385 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 while ((line = preproc->getline()))
388 nasm_free(line);
389 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400390 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000391 char *line;
392 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000393 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000395
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 if (*outname) {
397 ofile = fopen(outname, "w");
398 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400399 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 "unable to open output file `%s'",
401 outname);
402 } else
403 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000404
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700405 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000406
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400407 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700408 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400409 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700410
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 while ((line = preproc->getline())) {
412 /*
413 * We generate %line directives if needed for later programs
414 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000415 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 int altline = src_get(&linnum, &file_name);
417 if (altline) {
418 if (altline == 1 && lineinc == 1)
419 nasm_fputs("", ofile);
420 else {
421 lineinc = (altline != -1 || lineinc != 1);
422 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000423 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 file_name);
425 }
426 prior_linnum = linnum;
427 }
428 nasm_fputs(line, ofile);
429 nasm_free(line);
430 }
431 nasm_free(file_name);
432 preproc->cleanup(0);
433 if (ofile)
434 fclose(ofile);
435 if (ofile && terminate_after_phase)
436 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400437 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400438 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400440 if (operating_mode & OP_NORMAL) {
441 /*
442 * We must call ofmt->filename _anyway_, even if the user
443 * has specified their own output file, because some
444 * formats (eg OBJ and COFF) use ofmt->filename to find out
445 * the name of the input file and then put that inside the
446 * file.
447 */
448 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400450 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
451 if (!ofile)
452 nasm_error(ERR_FATAL | ERR_NOFILE,
453 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400455 /*
456 * We must call init_labels() before ofmt->init() since
457 * some object formats will want to define labels in their
458 * init routines. (eg OS/2 defines the FLAT group)
459 */
460 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400462 ofmt->init();
463 dfmt = ofmt->current_dfmt;
464 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200467
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400468 if (!terminate_after_phase) {
469 ofmt->cleanup(using_debug_info);
470 cleanup_labels();
471 fflush(ofile);
472 if (ferror(ofile))
473 nasm_error(ERR_NONFATAL|ERR_NOFILE,
474 "write error on output file `%s'", outname);
475 }
476
477 if (ofile) {
478 fclose(ofile);
479 if (terminate_after_phase)
480 remove(outname);
481 ofile = NULL;
482 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000484
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700485 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400486 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700487
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 raa_free(offsets);
492 saa_free(forwrefs);
493 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000494 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700496 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000497}
498
H. Peter Anvineba20a72002-04-30 20:53:55 +0000499/*
500 * Get a parameter for a command line option.
501 * First arg must be in the form of e.g. -f...
502 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800503static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800505 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400506 if (p[2]) /* the parameter's in the option */
507 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800509 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400512 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514 return NULL;
515}
516
H. Peter Anvindc242712007-11-18 11:55:10 -0800517/*
518 * Copy a filename
519 */
520static void copy_filename(char *dst, const char *src)
521{
522 size_t len = strlen(src);
523
524 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400525 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
526 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800527 }
528 strncpy(dst, src, FILENAME_MAX);
529}
530
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700531/*
532 * Convert a string to Make-safe form
533 */
534static char *quote_for_make(const char *str)
535{
536 const char *p;
537 char *os, *q;
538
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400539 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700540 size_t nbs = 0;
541
542 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400543 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700544
545 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400546 switch (*p) {
547 case ' ':
548 case '\t':
549 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
550 n += nbs + 2;
551 nbs = 0;
552 break;
553 case '$':
554 case '#':
555 nbs = 0;
556 n += 2;
557 break;
558 case '\\':
559 nbs++;
560 n++;
561 break;
562 default:
563 nbs = 0;
564 n++;
565 break;
566 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700567 }
568
569 /* Convert N backslashes at the end of filename to 2N backslashes */
570 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400571 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700572
573 os = q = nasm_malloc(n);
574
575 nbs = 0;
576 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400577 switch (*p) {
578 case ' ':
579 case '\t':
580 while (nbs--)
581 *q++ = '\\';
582 *q++ = '\\';
583 *q++ = *p;
584 break;
585 case '$':
586 *q++ = *p;
587 *q++ = *p;
588 nbs = 0;
589 break;
590 case '#':
591 *q++ = '\\';
592 *q++ = *p;
593 nbs = 0;
594 break;
595 case '\\':
596 *q++ = *p;
597 nbs++;
598 break;
599 default:
600 *q++ = *p;
601 nbs = 0;
602 break;
603 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700604 }
605 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400606 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700607
608 *q = '\0';
609
610 return os;
611}
612
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000614 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000616};
617
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100618enum text_options {
619 OPT_PREFIX,
620 OPT_POSTFIX,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100621};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622struct textargs textopts[] = {
623 {"prefix", OPT_PREFIX},
624 {"postfix", OPT_POSTFIX},
625 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000626};
627
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400628static void show_version(void)
629{
630 printf("NASM version %s compiled on %s%s\n",
631 nasm_version, nasm_date, nasm_compile_options);
632 exit(0);
633}
634
H. Peter Anvin423e3812007-11-15 17:12:29 -0800635static bool stopoptions = false;
636static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000638 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800639 int i;
640 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700641 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000642
643 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800644 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400647 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
648 /* These parameters take values */
649 if (!(param = get_param(p, q, &advance)))
650 return advance;
651 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800652
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 switch (p[1]) {
654 case 's':
655 error_file = stdout;
656 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700657
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400658 case 'o': /* output file */
659 copy_filename(outname, param);
660 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700661
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400662 case 'f': /* output format */
663 ofmt = ofmt_find(param, &ofmt_alias);
664 if (!ofmt) {
665 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
666 "unrecognised output format `%s' - "
667 "use -hf for a list", param);
668 }
669 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700670
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400671 case 'O': /* Optimization level */
672 {
673 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700674
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400675 if (!*param) {
676 /* Naked -O == -Ox */
677 optimizing = MAX_OPTIMIZE;
678 } else {
679 while (*param) {
680 switch (*param) {
681 case '0': case '1': case '2': case '3': case '4':
682 case '5': case '6': case '7': case '8': case '9':
683 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700684
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400685 /* -O0 -> optimizing == -1, 0.98 behaviour */
686 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
687 if (opt < 2)
688 optimizing = opt - 1;
689 else
690 optimizing = opt;
691 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700692
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400693 case 'v':
694 case '+':
695 param++;
696 opt_verbose_info = true;
697 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700698
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400699 case 'x':
700 param++;
701 optimizing = MAX_OPTIMIZE;
702 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700703
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400704 default:
705 nasm_error(ERR_FATAL,
706 "unknown optimization option -O%c\n",
707 *param);
708 break;
709 }
710 }
711 if (optimizing > MAX_OPTIMIZE)
712 optimizing = MAX_OPTIMIZE;
713 }
714 break;
715 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800716
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400717 case 'p': /* pre-include */
718 case 'P':
719 preproc->pre_include(param);
720 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800721
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400722 case 'd': /* pre-define */
723 case 'D':
724 preproc->pre_define(param);
725 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800726
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400727 case 'u': /* un-define */
728 case 'U':
729 preproc->pre_undefine(param);
730 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800731
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400732 case 'i': /* include search path */
733 case 'I':
734 preproc->include_path(param);
735 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800736
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400737 case 'l': /* listing file */
738 copy_filename(listname, param);
739 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800740
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400741 case 'Z': /* error messages file */
742 copy_filename(errname, param);
743 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800744
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400745 case 'F': /* specify debug format */
746 ofmt->current_dfmt = dfmt_find(ofmt, param);
747 if (!ofmt->current_dfmt) {
748 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
749 "unrecognized debug format `%s' for"
750 " output format `%s'",
751 param, ofmt->shortname);
752 }
753 using_debug_info = true;
754 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800755
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400756 case 'X': /* specify error reporting format */
757 if (nasm_stricmp("vc", param) == 0)
758 nasm_set_verror(nasm_verror_vc);
759 else if (nasm_stricmp("gnu", param) == 0)
760 nasm_set_verror(nasm_verror_gnu);
761 else
762 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
763 "unrecognized error reporting format `%s'",
764 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800766
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700768 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800770
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 case 'h':
772 printf
773 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
774 "[-l listfile]\n"
775 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400776 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400778 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700780 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700782 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400783 " -MG d:o, missing files assumed generated\n"
784 " -MF <file> set Makefile dependency file\n"
785 " -MD <file> assemble and generate dependencies\n"
786 " -MT <file> dependency target name\n"
787 " -MQ <file> dependency target name (quoted)\n"
788 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700789 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 " -s redirect error messages to stdout\n\n"
791 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400792 " -o outfile write output to an outfile\n\n"
793 " -f format select an output format\n\n"
794 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 " -I<path> adds a pathname to the include file path\n");
796 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700797 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400798 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400799 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400800 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 " -P<file> pre-includes a file\n"
802 " -D<macro>[=<value>] pre-defines a macro\n"
803 " -U<macro> undefines a macro\n"
804 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800805 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400806 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400807 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400808 "--prefix,--postfix\n"
809 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100810 " extern and global variables\n"
811 "--allow-64-bit\n"
812 " do not restrict 64-bit code to 64-bit capable output\n"
813 " formats (use with care, no complaining)\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800814 "Warnings:\n");
815 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700817 warnings[i].name, warnings[i].help,
818 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 printf
820 ("\nresponse files should contain command line parameters"
821 ", one per line.\n");
822 if (p[2] == 'f') {
823 printf("\nvalid output formats for -f are"
824 " (`*' denotes default):\n");
825 ofmt_list(ofmt, stdout);
826 } else {
827 printf("\nFor a list of valid output formats, use -hf.\n");
828 printf("For a list of debug formats, use -f <form> -y.\n");
829 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400830 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800832
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 case 'y':
834 printf("\nvalid debug formats for '%s' output format are"
835 " ('*' denotes default):\n", ofmt->shortname);
836 dfmt_list(ofmt, stdout);
837 exit(0);
838 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800839
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700841 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800843
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400845 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800847
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400848 case 'e': /* preprocess only */
849 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400850 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800852
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400853 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400854 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800856
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400857 case 'W':
858 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
859 do_warn = false;
860 param += 3;
861 } else {
862 do_warn = true;
863 }
864 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800865
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800867 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400868 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400870 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400872 do_warn = (param[0] == '+');
873 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400874
875set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400876 for (i = 0; i <= ERR_WARN_MAX; i++) {
877 if (!nasm_stricmp(param, warnings[i].name))
878 break;
879 }
880 if (i <= ERR_WARN_MAX) {
881 warning_on_global[i] = do_warn;
882 } else if (!nasm_stricmp(param, "all")) {
883 for (i = 1; i <= ERR_WARN_MAX; i++)
884 warning_on_global[i] = do_warn;
885 } else if (!nasm_stricmp(param, "none")) {
886 for (i = 1; i <= ERR_WARN_MAX; i++)
887 warning_on_global[i] = !do_warn;
888 } else {
889 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
890 "invalid warning `%s'", param);
891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400895 switch (p[2]) {
896 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400897 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400898 break;
899 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400900 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400901 depend_missing_ok = true;
902 break;
903 case 'P':
904 depend_emit_phony = true;
905 break;
906 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300907 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400908 depend_file = q;
909 advance = true;
910 break;
911 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400912 depend_file = q;
913 advance = true;
914 break;
915 case 'T':
916 depend_target = q;
917 advance = true;
918 break;
919 case 'Q':
920 depend_target = quote_for_make(q);
921 advance = true;
922 break;
923 default:
924 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
925 "unknown dependency option `-M%c'", p[2]);
926 break;
927 }
928 if (advance && (!q || !q[0])) {
929 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
930 "option `-M%c' requires a parameter", p[2]);
931 break;
932 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000934
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 case '-':
936 {
937 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000938
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 if (p[2] == 0) { /* -- => stop processing options */
940 stopoptions = 1;
941 break;
942 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400943
944 if (!nasm_stricmp(p, "--v"))
945 show_version();
946
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 for (s = 0; textopts[s].label; s++) {
948 if (!nasm_stricmp(p + 2, textopts[s].label)) {
949 break;
950 }
951 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000952
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 switch (s) {
954
955 case OPT_PREFIX:
956 case OPT_POSTFIX:
957 {
958 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400959 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 ERR_USAGE,
961 "option `--%s' requires an argument",
962 p + 2);
963 break;
964 } else {
965 advance = 1, param = q;
966 }
967
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100968 switch (s) {
969 case OPT_PREFIX:
970 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100972 case OPT_POSTFIX:
973 strlcpy(lpostfix, param, POSTFIX_MAX);
974 break;
975 default:
976 nasm_error(ERR_PANIC | ERR_NOFILE,
977 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 break;
979 }
980 break;
981 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800982
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 default:
984 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400985 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 "unrecognised option `--%s'", p + 2);
987 break;
988 }
989 }
990 break;
991 }
992
993 default:
994 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400995 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 "unrecognised option `-%c'", p[1]);
997 break;
998 }
999 } else {
1000 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001001 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001003 } else {
1004 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001006 }
1007
1008 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001009}
1010
H. Peter Anvineba20a72002-04-30 20:53:55 +00001011#define ARG_BUF_DELTA 128
1012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001014{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001015 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001016 int bufsize, prevargsize;
1017
1018 bufsize = prevargsize = ARG_BUF_DELTA;
1019 buffer = nasm_malloc(ARG_BUF_DELTA);
1020 prevarg = nasm_malloc(ARG_BUF_DELTA);
1021 prevarg[0] = '\0';
1022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 p = buffer;
1025 while (1) { /* Loop to handle long lines */
1026 q = fgets(p, bufsize - (p - buffer), rfile);
1027 if (!q)
1028 break;
1029 p += strlen(p);
1030 if (p > buffer && p[-1] == '\n')
1031 break;
1032 if (p - buffer > bufsize - 10) {
1033 int offset;
1034 offset = p - buffer;
1035 bufsize += ARG_BUF_DELTA;
1036 buffer = nasm_realloc(buffer, bufsize);
1037 p = buffer + offset;
1038 }
1039 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 if (!q && p == buffer) {
1042 if (prevarg[0])
1043 process_arg(prevarg, NULL);
1044 nasm_free(buffer);
1045 nasm_free(prevarg);
1046 return;
1047 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /*
1050 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1051 * them are present at the end of the line.
1052 */
1053 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001054
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001055 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001057
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001058 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 if (process_arg(prevarg, p))
1061 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001062
Charles Crayne192d5b52007-10-18 19:02:42 -07001063 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 prevargsize += ARG_BUF_DELTA;
1065 prevarg = nasm_realloc(prevarg, prevargsize);
1066 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001067 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001068 }
1069}
1070
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001071/* Function to process args from a string of args, rather than the
1072 * argv array. Used by the environment variable and response file
1073 * processing.
1074 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001075static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001077 char *p, *q, *arg, *prevarg;
1078 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001079
1080 p = args;
1081 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001083 arg = NULL;
1084 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 q = p;
1086 while (*p && *p != separator)
1087 p++;
1088 while (*p == separator)
1089 *p++ = '\0';
1090 prevarg = arg;
1091 arg = q;
1092 if (process_arg(prevarg, arg))
1093 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001094 }
1095 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001097}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001098
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001099static void process_response_file(const char *file)
1100{
1101 char str[2048];
1102 FILE *f = fopen(file, "r");
1103 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001104 perror(file);
1105 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001106 }
1107 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001108 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001109 }
1110 fclose(f);
1111}
1112
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001113static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001114{
1115 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001116 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001117 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001118
Charles Craynefcce07f2007-09-30 22:15:36 -07001119 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001120
H. Peter Anvin972079f2008-09-30 17:01:23 -07001121 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001122 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001123
1124 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001125 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001126 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001127 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001128 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 envcopy = nasm_strdup(envreal);
1130 process_args(envcopy);
1131 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001132 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001133
1134 /*
1135 * Now process the actual command line.
1136 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001138 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 argv++;
1140 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001141 /*
1142 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 * arguments like the environment variable. This allows us
1144 * to have multiple arguments on a single line, which is
1145 * different to the -@resp file processing below for regular
1146 * NASM.
1147 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001148 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 argc--;
1150 argv++;
1151 }
1152 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001153 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001154 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001155 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001156 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 process_respfile(rfile);
1158 fclose(rfile);
1159 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001160 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 "unable to open response file `%s'", p);
1162 }
1163 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001164 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1165 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001166 }
1167
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001168 /*
1169 * Look for basic command line typos. This definitely doesn't
1170 * catch all errors, but it might help cases of fumbled fingers.
1171 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001172 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001173 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001174 "no input file specified");
1175 else if (!strcmp(inname, errname) ||
1176 !strcmp(inname, outname) ||
1177 !strcmp(inname, listname) ||
1178 (depend_file && !strcmp(inname, depend_file)))
1179 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1180 "file `%s' is both input and output file",
1181 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001182
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001184 error_file = fopen(errname, "w");
1185 if (!error_file) {
1186 error_file = stderr; /* Revert to default! */
1187 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1188 "cannot open file `%s' for error messages",
1189 errname);
1190 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001191 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001192}
1193
H. Peter Anvin70055962007-10-11 00:05:31 -07001194static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001195
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001196static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001197{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001198 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001200 int i, validid;
1201 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001202 int32_t seg;
1203 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001204 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001206 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001207
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001208 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001209 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001211
Charles Craynec1905c22008-09-11 18:54:06 -07001212 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001213 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 int pass1, pass2;
1215 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001216
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001217 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001218 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1219 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001220
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001221 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001222
Keith Kaniosb7a89542007-04-12 02:40:54 +00001223 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 cpu = cmd_cpu;
1225 if (pass0 == 2) {
1226 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001227 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001229 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001230 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001232 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001233 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 saa_rewind(forwrefs);
1235 forwref = saa_rstruct(forwrefs);
1236 raa_free(offsets);
1237 offsets = raa_init();
1238 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001239 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001240 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001241 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001242
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001244 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001245 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001246 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001247
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001249 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001252 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001253 * Here we parse our directives; this is not handled by the
1254 * 'real' parser. This really should be a separate function.
1255 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001257 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001258 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001259 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001260
H. Peter Anvin70055962007-10-11 00:05:31 -07001261 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001262 case D_SEGMENT: /* [SEGMENT n] */
1263 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 seg = ofmt->section(value, pass2, &sb);
1265 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001266 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001267 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 value);
1269 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001270 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 location.segment = seg;
1272 }
1273 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001274 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001275 if (*value) {
1276 stdscan_reset();
1277 stdscan_set(value);
1278 tokval.t_type = TOKEN_INVALID;
1279 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1280 if (e) {
1281 unsigned int align = (unsigned int)e->value;
1282 if ((uint64_t)e->value > 0x7fffffff) {
1283 /*
1284 * FIXME: Please make some sane message here
1285 * ofmt should have some 'check' method which
1286 * would report segment alignment bounds.
1287 */
1288 nasm_error(ERR_FATAL,
1289 "incorrect segment alignment `%s'", value);
1290 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001291 nasm_error(ERR_NONFATAL,
1292 "segment alignment `%s' is not power of two",
1293 value);
1294 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001295 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001296 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001297 }
1298 }
1299 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001300 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 if (*value == '$')
1302 value++; /* skip initial $ if present */
1303 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 q = value;
1305 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001307 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 *q++ = '\0';
1309 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001311 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001313 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001315 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001316 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001318 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001320 }
1321 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001322 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 "identifier expected after EXTERN");
1324 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001325 }
1326 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 *q++ = '\0';
1328 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 special = NULL;
1331 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1332 int temp = pass0;
1333 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001334 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001336 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001338 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 } /* else pass0 == 1 */
1340 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001341 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001342 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001344 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 if (*value == '$')
1346 value++; /* skip initial $ if present */
1347 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001348 q = value;
1349 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 *q++ = '\0';
1353 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001357 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001359 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001362 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364 }
1365 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001366 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 "identifier expected after GLOBAL");
1368 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 }
1370 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 *q++ = '\0';
1372 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001375 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 } /* pass == 1 */
1377 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001378 case D_COMMON: /* [COMMON symbol size:special] */
1379 {
1380 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001381
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 if (*value == '$')
1383 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001384 p = value;
1385 validid = true;
1386 if (!isidstart(*p))
1387 validid = false;
1388 while (*p && !nasm_isspace(*p)) {
1389 if (!isidchar(*p))
1390 validid = false;
1391 p++;
1392 }
1393 if (!validid) {
1394 nasm_error(ERR_NONFATAL,
1395 "identifier expected after COMMON");
1396 break;
1397 }
1398 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001399 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001400 q = p;
1401 while (*q && *q != ':')
1402 q++;
1403 if (*q == ':') {
1404 *q++ = '\0';
1405 special = q;
1406 } else {
1407 special = NULL;
1408 }
1409 size = readnum(p, &rn_error);
1410 if (rn_error) {
1411 nasm_error(ERR_NONFATAL,
1412 "invalid size specified"
1413 " in COMMON declaration");
1414 break;
1415 }
1416 } else {
1417 nasm_error(ERR_NONFATAL,
1418 "no size specified in"
1419 " COMMON declaration");
1420 break;
1421 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001423 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001424 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001425 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001426 if (special)
1427 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 }
1429 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001430 }
1431 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001433 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 tokval.t_type = TOKEN_INVALID;
1435 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001436 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 if (e) {
1438 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001439 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 1 ? ERR_NONFATAL : ERR_PANIC,
1441 "cannot use non-relocatable expression as "
1442 "ABSOLUTE address");
1443 else {
1444 abs_seg = reloc_seg(e);
1445 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001446 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001447 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 abs_offset = 0x100; /* don't go near zero in case of / */
1449 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001450 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001452 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 location.segment = NO_SEG;
1454 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001455 case D_DEBUG: /* [DEBUG] */
1456 {
1457 char debugid[128];
1458 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001459
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 p = value;
1461 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001462 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001463 if (!isidstart(*p)) {
1464 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001465 } else {
1466 while (*p && !nasm_isspace(*p)) {
1467 if (q >= debugid + sizeof debugid - 1) {
1468 overlong = true;
1469 break;
1470 }
1471 if (!isidchar(*p))
1472 badid = true;
1473 *q++ = *p++;
1474 }
1475 *q = 0;
1476 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001477 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001478 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1479 "identifier expected after DEBUG");
1480 break;
1481 }
1482 if (overlong) {
1483 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1484 "DEBUG identifier too long");
1485 break;
1486 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001487 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001488 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001489 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001491 }
1492 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001493 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001494 switch(*value) {
1495 case '-': validid = 0; value++; break;
1496 case '+': validid = 1; value++; break;
1497 case '*': validid = 2; value++; break;
1498 default: validid = 1; break;
1499 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001501 for (i = 1; i <= ERR_WARN_MAX; i++)
1502 if (!nasm_stricmp(value, warnings[i].name))
1503 break;
1504 if (i <= ERR_WARN_MAX) {
1505 switch(validid) {
1506 case 0:
1507 warning_on[i] = false;
1508 break;
1509 case 1:
1510 warning_on[i] = true;
1511 break;
1512 case 2:
1513 warning_on[i] = warning_on_global[i];
1514 break;
1515 }
1516 } else
1517 nasm_error(ERR_NONFATAL,
1518 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001520 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 cpu = get_cpu(value);
1522 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001523 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001524 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 if (*value == '+') {
1526 user_nolist = 0;
1527 } else {
1528 if (*value == '-') {
1529 user_nolist = 1;
1530 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001531 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 }
1533 }
1534 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001535 case D_DEFAULT: /* [DEFAULT] */
1536 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001537 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001538 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001539 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001540 switch ((int)tokval.t_integer) {
1541 case S_REL:
1542 globalrel = 1;
1543 break;
1544 case S_ABS:
1545 globalrel = 0;
1546 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001547 case P_BND:
1548 globalbnd = 1;
1549 break;
1550 case P_NOBND:
1551 globalbnd = 0;
1552 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001553 default:
1554 err = 1;
1555 break;
1556 }
1557 } else {
1558 err = 1;
1559 }
1560 break;
1561 case D_FLOAT:
1562 if (float_option(value)) {
1563 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1564 "unknown 'float' directive: %s",
1565 value);
1566 }
1567 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001569 if (ofmt->directive(d, value, pass2))
1570 break;
1571 /* else fall through */
1572 case D_unknown:
1573 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1574 "unrecognised directive [%s]",
1575 directive);
1576 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001578 if (err) {
1579 nasm_error(ERR_NONFATAL,
1580 "invalid parameter to [%s] directive",
1581 directive);
1582 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001584 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585
Charles Crayne2581c862008-09-10 19:21:52 -07001586 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001588 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001590 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 forwref = saa_rstruct(forwrefs);
1592 } while (forwref != NULL
1593 && forwref->lineno == globallineno);
1594 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001595 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001596
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001597 if (output_ins.forw_ref) {
1598 if (passn == 1) {
1599 for (i = 0; i < output_ins.operands; i++) {
1600 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1601 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1602 fwinf->lineno = globallineno;
1603 fwinf->operand = i;
1604 }
1605 }
1606 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001607 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001609
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 /* forw_ref */
1611 if (output_ins.opcode == I_EQU) {
1612 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001613 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 * Special `..' EQUs get processed in pass two,
1615 * except `..@' macro-processor EQUs which are done
1616 * in the normal place.
1617 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001619 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001621
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001622 else if (output_ins.label[0] != '.' ||
1623 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 output_ins.label[2] == '@') {
1625 if (output_ins.operands == 1 &&
1626 (output_ins.oprs[0].type & IMMEDIATE) &&
1627 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001628 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 def_label(output_ins.label,
1630 output_ins.oprs[0].segment,
1631 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001632 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001634 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001636 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001638 && (output_ins.oprs[1].type & IMMEDIATE)
1639 && output_ins.oprs[1].segment == NO_SEG
1640 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001642 output_ins.oprs[0].offset | SEG_ABS,
1643 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001644 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001646 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001648 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001649 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 * Special `..' EQUs get processed here, except
1652 * `..@' macro processor EQUs which are done above.
1653 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 output_ins.label[1] == '.' &&
1656 output_ins.label[2] != '@') {
1657 if (output_ins.operands == 1 &&
1658 (output_ins.oprs[0].type & IMMEDIATE)) {
1659 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001660 output_ins.oprs[0].segment,
1661 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001662 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001664 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001666 && output_ins.oprs[0].segment == NO_SEG
1667 && (output_ins.oprs[1].type & IMMEDIATE)
1668 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001670 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001671 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001672 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001674 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001676 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001677 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001679
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001681
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001682 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001683 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001684
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 /* if (using_debug_info) && output_ins.opcode != -1) */
1686 if (using_debug_info)
1687 { /* fbk 03/25/01 */
1688 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001689 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 TYS_ELEMENTS(output_ins.operands);
1691 switch (output_ins.opcode) {
1692 case I_RESB:
1693 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001694 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 break;
1696 case I_RESW:
1697 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001698 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 break;
1700 case I_RESD:
1701 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001702 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 break;
1704 case I_RESQ:
1705 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001706 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 break;
1708 case I_REST:
1709 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001710 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001712 case I_RESO:
1713 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001714 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001715 break;
1716 case I_RESY:
1717 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001718 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001719 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 case I_DB:
1721 typeinfo |= TY_BYTE;
1722 break;
1723 case I_DW:
1724 typeinfo |= TY_WORD;
1725 break;
1726 case I_DD:
1727 if (output_ins.eops_float)
1728 typeinfo |= TY_FLOAT;
1729 else
1730 typeinfo |= TY_DWORD;
1731 break;
1732 case I_DQ:
1733 typeinfo |= TY_QWORD;
1734 break;
1735 case I_DT:
1736 typeinfo |= TY_TBYTE;
1737 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001738 case I_DO:
1739 typeinfo |= TY_OWORD;
1740 break;
1741 case I_DY:
1742 typeinfo |= TY_YWORD;
1743 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 default:
1745 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001746
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001748
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001749 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001750 }
1751 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001753 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001754 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001755 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 * else l == -1 => invalid instruction, which will be
1757 * flagged as an error on pass 2
1758 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001759
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001760 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001762 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001764 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001765
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 }
1767 } /* not an EQU */
1768 cleanup_insn(&output_ins);
1769 }
1770 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001771 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001773
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001774 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001775 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001776 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001777
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 if (pass1 == 1)
1779 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001780
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001781 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001783 } else if (global_offset_changed &&
1784 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001785 prev_offset_changed = global_offset_changed;
1786 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001787 } else {
1788 stall_count++;
1789 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001790
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001791 if (terminate_after_phase)
1792 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001793
1794 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001795 /* We get here if the labels don't converge
1796 * Example: FOO equ FOO + 1
1797 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001798 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001799 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001800 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001801 nasm_error(ERR_NONFATAL,
1802 "Possible causes: recursive EQUs, macro abuse.");
1803 break;
1804 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001805 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001806
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 preproc->cleanup(0);
1808 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001809 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001810 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001811 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1812 }
1813}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001814
H. Peter Anvin70055962007-10-11 00:05:31 -07001815static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001816{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001817 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001819 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001820
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001821 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001823 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001824 q = strchr(buf, ']');
1825 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001826 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001827
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001828 /* stip off the comments */
1829 p = strchr(buf, ';');
1830 if (p) {
1831 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001832 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001833 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001834 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001835
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001836 /* no brace, no trailing spaces */
1837 *q = '\0';
1838 nasm_zap_spaces_rev(--q);
1839
1840 /* directive */
1841 p = nasm_skip_spaces(++buf);
1842 q = nasm_skip_word(p);
1843 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001844 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001845 *q = '\0';
1846 *directive = p;
1847
1848 /* and value finally */
1849 p = nasm_skip_spaces(++q);
1850 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001851
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001852 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001853}
1854
Ed Berosetfa771012002-06-09 20:56:40 +00001855/**
1856 * gnu style error reporting
1857 * This function prints an error message to error_file in the
1858 * style used by GNU. An example would be:
1859 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001860 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001861 * which the error occurs (or is detected) and "error:" is one of
1862 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001863 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001864 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001865 *
1866 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001867 * @param fmt the printf style format string
1868 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001869static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001870{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001871 char *currentfile = NULL;
1872 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001873
Ed Berosetfa771012002-06-09 20:56:40 +00001874 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001876
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001877 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001879
1880 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001881 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1882 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001883 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001884 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001885 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001886
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001887 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001888}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001889
Ed Berosetfa771012002-06-09 20:56:40 +00001890/**
1891 * MS style error reporting
1892 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001893 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001894 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001895 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001896 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001897 * which the error occurs (or is detected) and "error:" is one of
1898 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001899 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001900 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001901 *
1902 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001903 * @param fmt the printf style format string
1904 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001905static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001906{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001907 char *currentfile = NULL;
1908 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001909
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 if (is_suppressed_warning(severity))
1911 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001912
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001913 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001915
1916 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001917 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001919 } else {
1920 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001921 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001922
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001923 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001924}
1925
1926/**
1927 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001928 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001929 * not in pass 1
1930 *
1931 * @param severity the severity of the warning or error
1932 * @return true if we should abort error/warning printing
1933 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001934static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001935{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001936 /* Not a warning at all */
1937 if ((severity & ERR_MASK) != ERR_WARNING)
1938 return false;
1939
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001940 /* See if it's a pass-one only warning and we're not in pass one. */
1941 if (((severity & ERR_PASS1) && pass0 != 1) ||
1942 ((severity & ERR_PASS2) && pass0 != 2))
1943 return true;
1944
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001945 /* Might be a warning but suppresed explicitly */
1946 if (severity & ERR_WARN_MASK)
1947 return !warning_on[WARN_IDX(severity)];
1948 else
1949 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001950}
1951
1952/**
1953 * common error reporting
1954 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001955 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001956 * specific error message to error_file and may or may not return. It
1957 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001958 *
1959 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001960 * @param fmt the printf style format string
1961 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001962static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001963{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001964 char msg[1024];
1965 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001966
H. Peter Anvin7df04172008-06-10 18:27:38 -07001967 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001969 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 break;
1971 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001972 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 break;
1974 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001975 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 break;
1977 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001978 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 break;
1980 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001981 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001983 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001984 pfx = "";
1985 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001986 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001987
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001988 vsnprintf(msg, sizeof msg, fmt, args);
1989
1990 fprintf(error_file, "%s%s\n", pfx, msg);
1991
1992 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001993 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001994
1995 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001996 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001997
1998 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 case ERR_DEBUG:
2000 /* no further action, by definition */
2001 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002002 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002003 /* Treat warnings as errors */
2004 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2005 terminate_after_phase = true;
2006 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002008 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 break;
2010 case ERR_FATAL:
2011 if (ofile) {
2012 fclose(ofile);
2013 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002014 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 }
2016 if (want_usage)
2017 usage();
2018 exit(1); /* instantly die */
2019 break; /* placate silly compilers */
2020 case ERR_PANIC:
2021 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002022 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 exit(3);
2024 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002025 }
2026}
2027
H. Peter Anvin734b1882002-04-30 21:01:08 +00002028static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002029{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002030 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002031}
2032
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002033static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002034{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002035 iflag_t r;
2036
2037 iflag_clear_all(&r);
2038
H. Peter Anvine2c80182005-01-15 22:15:51 +00002039 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002040 iflag_set(&r, IF_8086);
2041 else if (!strcmp(value, "186"))
2042 iflag_set(&r, IF_186);
2043 else if (!strcmp(value, "286"))
2044 iflag_set(&r, IF_286);
2045 else if (!strcmp(value, "386"))
2046 iflag_set(&r, IF_386);
2047 else if (!strcmp(value, "486"))
2048 iflag_set(&r, IF_486);
2049 else if (!strcmp(value, "586") ||
2050 !nasm_stricmp(value, "pentium"))
2051 iflag_set(&r, IF_PENT);
2052 else if (!strcmp(value, "686") ||
2053 !nasm_stricmp(value, "ppro") ||
2054 !nasm_stricmp(value, "pentiumpro") ||
2055 !nasm_stricmp(value, "p2"))
2056 iflag_set(&r, IF_P6);
2057 else if (!nasm_stricmp(value, "p3") ||
2058 !nasm_stricmp(value, "katmai"))
2059 iflag_set(&r, IF_KATMAI);
2060 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2061 !nasm_stricmp(value, "willamette"))
2062 iflag_set(&r, IF_WILLAMETTE);
2063 else if (!nasm_stricmp(value, "prescott"))
2064 iflag_set(&r, IF_PRESCOTT);
2065 else if (!nasm_stricmp(value, "x64") ||
2066 !nasm_stricmp(value, "x86-64"))
2067 iflag_set(&r, IF_X86_64);
2068 else if (!nasm_stricmp(value, "ia64") ||
2069 !nasm_stricmp(value, "ia-64") ||
2070 !nasm_stricmp(value, "itanium")||
2071 !nasm_stricmp(value, "itanic") ||
2072 !nasm_stricmp(value, "merced"))
2073 iflag_set(&r, IF_IA64);
2074 else {
2075 iflag_set(&r, IF_PLEVEL);
2076 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2077 "unknown 'cpu' type");
2078 }
2079 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002080}
2081
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002082static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002083{
2084 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002085
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 if ((i = atoi(value)) == 16)
2087 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002088 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002089 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002090 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 "cannot specify 32-bit segment on processor below a 386");
2092 i = 16;
2093 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002094 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002095 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002096 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002097 "cannot specify 64-bit segment on processor below an x86-64");
2098 i = 16;
2099 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002100 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002101 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002102 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 value);
2104 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002105 }
2106 return i;
2107}