blob: 01e0628ca845e7060c7317c4d7d69e497481092c [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 Anvin130736c2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080082static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040083static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
84static void nasm_verror_vc(int severity, const char *fmt, va_list args);
85static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086static void usage(void);
87
John Coffman0efaec92002-05-26 19:20:08 +000088static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070089bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080090int pass0, passn;
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 Anvin338656c2016-02-17 20:59:22 -0800102const struct ofmt *ofmt = &OF_DEFAULT;
103const struct 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 Anvincd7893d2016-02-18 01:25:46 -0800119struct 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 Anvinecc9e0e2016-02-11 20:29:34 -0800171 {"zext-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 Anvin130736c2016-02-17 20:27:41 -0800176bool user_nolist = false;
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 Anvin6b18bcc2008-02-16 14:54:10 -0800347 /* Define some macros dependent on the runtime, but not
348 on the command line. */
349 define_macros_early();
350
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000351 parse_cmdline(argc, argv);
352
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 if (terminate_after_phase) {
354 if (want_usage)
355 usage();
356 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000357 }
358
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000359 /* If debugging info is disabled, suppress any debug calls */
360 if (!using_debug_info)
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800361 dfmt = &null_debug_form;
362 else if (!dfmt)
363 dfmt = ofmt->default_dfmt;
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000364
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 Anvin76690a12002-04-30 20:52:49 +0000367
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000368 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800369 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000370
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400371 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700372 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400373 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700374
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400375 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000376 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700377
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400378 if (depend_missing_ok)
379 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700380
H. Peter Anvin130736c2016-02-17 20:27:41 -0800381 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400383 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 while ((line = preproc->getline()))
386 nasm_free(line);
387 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400388 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000389 char *line;
390 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000391 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000393
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 if (*outname) {
395 ofile = fopen(outname, "w");
396 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400397 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 "unable to open output file `%s'",
399 outname);
400 } else
401 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000402
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700403 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000404
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400405 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800406 preproc->reset(inname, 3, depend_ptr);
407 memcpy(warning_on, warning_on_global,
408 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700409
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 while ((line = preproc->getline())) {
411 /*
412 * We generate %line directives if needed for later programs
413 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000414 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 int altline = src_get(&linnum, &file_name);
416 if (altline) {
417 if (altline == 1 && lineinc == 1)
418 nasm_fputs("", ofile);
419 else {
420 lineinc = (altline != -1 || lineinc != 1);
421 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000422 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 file_name);
424 }
425 prior_linnum = linnum;
426 }
427 nasm_fputs(line, ofile);
428 nasm_free(line);
429 }
430 nasm_free(file_name);
431 preproc->cleanup(0);
432 if (ofile)
433 fclose(ofile);
434 if (ofile && terminate_after_phase)
435 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400436 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400437 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400439 if (operating_mode & OP_NORMAL) {
440 /*
441 * We must call ofmt->filename _anyway_, even if the user
442 * has specified their own output file, because some
443 * formats (eg OBJ and COFF) use ofmt->filename to find out
444 * the name of the input file and then put that inside the
445 * file.
446 */
447 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400449 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
450 if (!ofile)
451 nasm_error(ERR_FATAL | ERR_NOFILE,
452 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400454 /*
455 * We must call init_labels() before ofmt->init() since
456 * some object formats will want to define labels in their
457 * init routines. (eg OS/2 defines the FLAT group)
458 */
459 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400461 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400462 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400464 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200465
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 if (!terminate_after_phase) {
467 ofmt->cleanup(using_debug_info);
468 cleanup_labels();
469 fflush(ofile);
470 if (ferror(ofile))
471 nasm_error(ERR_NONFATAL|ERR_NOFILE,
472 "write error on output file `%s'", outname);
473 }
474
475 if (ofile) {
476 fclose(ofile);
477 if (terminate_after_phase)
478 remove(outname);
479 ofile = NULL;
480 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000481 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000482
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700483 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400484 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700485
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000486 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000488
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 raa_free(offsets);
490 saa_free(forwrefs);
491 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000492 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700494 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000495}
496
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497/*
498 * Get a parameter for a command line option.
499 * First arg must be in the form of e.g. -f...
500 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800501static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000502{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800503 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400504 if (p[2]) /* the parameter's in the option */
505 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800507 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000509 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400510 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000512 return NULL;
513}
514
H. Peter Anvindc242712007-11-18 11:55:10 -0800515/*
516 * Copy a filename
517 */
518static void copy_filename(char *dst, const char *src)
519{
520 size_t len = strlen(src);
521
522 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400523 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
524 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800525 }
526 strncpy(dst, src, FILENAME_MAX);
527}
528
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700529/*
530 * Convert a string to Make-safe form
531 */
532static char *quote_for_make(const char *str)
533{
534 const char *p;
535 char *os, *q;
536
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400537 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700538 size_t nbs = 0;
539
540 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400541 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700542
543 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400544 switch (*p) {
545 case ' ':
546 case '\t':
547 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
548 n += nbs + 2;
549 nbs = 0;
550 break;
551 case '$':
552 case '#':
553 nbs = 0;
554 n += 2;
555 break;
556 case '\\':
557 nbs++;
558 n++;
559 break;
560 default:
561 nbs = 0;
562 n++;
563 break;
564 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700565 }
566
567 /* Convert N backslashes at the end of filename to 2N backslashes */
568 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400569 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700570
571 os = q = nasm_malloc(n);
572
573 nbs = 0;
574 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400575 switch (*p) {
576 case ' ':
577 case '\t':
578 while (nbs--)
579 *q++ = '\\';
580 *q++ = '\\';
581 *q++ = *p;
582 break;
583 case '$':
584 *q++ = *p;
585 *q++ = *p;
586 nbs = 0;
587 break;
588 case '#':
589 *q++ = '\\';
590 *q++ = *p;
591 nbs = 0;
592 break;
593 case '\\':
594 *q++ = *p;
595 nbs++;
596 break;
597 default:
598 *q++ = *p;
599 nbs = 0;
600 break;
601 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700602 }
603 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400604 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700605
606 *q = '\0';
607
608 return os;
609}
610
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000612 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000614};
615
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100616enum text_options {
617 OPT_PREFIX,
618 OPT_POSTFIX,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100619};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620struct textargs textopts[] = {
621 {"prefix", OPT_PREFIX},
622 {"postfix", OPT_POSTFIX},
623 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000624};
625
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400626static void show_version(void)
627{
628 printf("NASM version %s compiled on %s%s\n",
629 nasm_version, nasm_date, nasm_compile_options);
630 exit(0);
631}
632
H. Peter Anvin423e3812007-11-15 17:12:29 -0800633static bool stopoptions = false;
634static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000635{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000636 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800637 int i;
638 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700639 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000640
641 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800642 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000643
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400645 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
646 /* These parameters take values */
647 if (!(param = get_param(p, q, &advance)))
648 return advance;
649 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800650
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 switch (p[1]) {
652 case 's':
653 error_file = stdout;
654 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700655
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400656 case 'o': /* output file */
657 copy_filename(outname, param);
658 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700659
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400660 case 'f': /* output format */
661 ofmt = ofmt_find(param, &ofmt_alias);
662 if (!ofmt) {
663 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
664 "unrecognised output format `%s' - "
665 "use -hf for a list", param);
666 }
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800667 dfmt = NULL;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400668 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700669
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400670 case 'O': /* Optimization level */
671 {
672 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700673
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400674 if (!*param) {
675 /* Naked -O == -Ox */
676 optimizing = MAX_OPTIMIZE;
677 } else {
678 while (*param) {
679 switch (*param) {
680 case '0': case '1': case '2': case '3': case '4':
681 case '5': case '6': case '7': case '8': case '9':
682 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700683
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400684 /* -O0 -> optimizing == -1, 0.98 behaviour */
685 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
686 if (opt < 2)
687 optimizing = opt - 1;
688 else
689 optimizing = opt;
690 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700691
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400692 case 'v':
693 case '+':
694 param++;
695 opt_verbose_info = true;
696 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700697
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400698 case 'x':
699 param++;
700 optimizing = MAX_OPTIMIZE;
701 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700702
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400703 default:
704 nasm_error(ERR_FATAL,
705 "unknown optimization option -O%c\n",
706 *param);
707 break;
708 }
709 }
710 if (optimizing > MAX_OPTIMIZE)
711 optimizing = MAX_OPTIMIZE;
712 }
713 break;
714 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800715
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400716 case 'p': /* pre-include */
717 case 'P':
718 preproc->pre_include(param);
719 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800720
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400721 case 'd': /* pre-define */
722 case 'D':
723 preproc->pre_define(param);
724 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800725
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400726 case 'u': /* un-define */
727 case 'U':
728 preproc->pre_undefine(param);
729 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800730
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400731 case 'i': /* include search path */
732 case 'I':
733 preproc->include_path(param);
734 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800735
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400736 case 'l': /* listing file */
737 copy_filename(listname, param);
738 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800739
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400740 case 'Z': /* error messages file */
741 copy_filename(errname, param);
742 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800743
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400744 case 'F': /* specify debug format */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800745 dfmt = dfmt_find(ofmt, param);
746 if (!dfmt) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400747 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
748 "unrecognized debug format `%s' for"
749 " output format `%s'",
750 param, ofmt->shortname);
751 }
752 using_debug_info = true;
753 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800754
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400755 case 'X': /* specify error reporting format */
756 if (nasm_stricmp("vc", param) == 0)
757 nasm_set_verror(nasm_verror_vc);
758 else if (nasm_stricmp("gnu", param) == 0)
759 nasm_set_verror(nasm_verror_gnu);
760 else
761 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
762 "unrecognized error reporting format `%s'",
763 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800765
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700767 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800769
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 case 'h':
771 printf
772 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
773 "[-l listfile]\n"
774 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400775 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400777 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700779 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700781 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400782 " -MG d:o, missing files assumed generated\n"
783 " -MF <file> set Makefile dependency file\n"
784 " -MD <file> assemble and generate dependencies\n"
785 " -MT <file> dependency target name\n"
786 " -MQ <file> dependency target name (quoted)\n"
787 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700788 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 " -s redirect error messages to stdout\n\n"
790 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400791 " -o outfile write output to an outfile\n\n"
792 " -f format select an output format\n\n"
793 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 " -I<path> adds a pathname to the include file path\n");
795 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700796 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400797 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400798 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400799 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 " -P<file> pre-includes a file\n"
801 " -D<macro>[=<value>] pre-defines a macro\n"
802 " -U<macro> undefines a macro\n"
803 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800804 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400805 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400806 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400807 "--prefix,--postfix\n"
808 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100809 " extern and global variables\n"
810 "--allow-64-bit\n"
811 " do not restrict 64-bit code to 64-bit capable output\n"
812 " formats (use with care, no complaining)\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800813 "Warnings:\n");
814 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700816 warnings[i].name, warnings[i].help,
817 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 printf
819 ("\nresponse files should contain command line parameters"
820 ", one per line.\n");
821 if (p[2] == 'f') {
822 printf("\nvalid output formats for -f are"
823 " (`*' denotes default):\n");
824 ofmt_list(ofmt, stdout);
825 } else {
826 printf("\nFor a list of valid output formats, use -hf.\n");
827 printf("For a list of debug formats, use -f <form> -y.\n");
828 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400829 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800831
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 case 'y':
833 printf("\nvalid debug formats for '%s' output format are"
834 " ('*' denotes default):\n", ofmt->shortname);
835 dfmt_list(ofmt, stdout);
836 exit(0);
837 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700840 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800842
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400844 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800846
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400847 case 'e': /* preprocess only */
848 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400849 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400853 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800855
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400856 case 'W':
857 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
858 do_warn = false;
859 param += 3;
860 } else {
861 do_warn = true;
862 }
863 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800864
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800866 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400867 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400869 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400871 do_warn = (param[0] == '+');
872 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400873
874set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400875 for (i = 0; i <= ERR_WARN_MAX; i++) {
876 if (!nasm_stricmp(param, warnings[i].name))
877 break;
878 }
879 if (i <= ERR_WARN_MAX) {
880 warning_on_global[i] = do_warn;
881 } else if (!nasm_stricmp(param, "all")) {
882 for (i = 1; i <= ERR_WARN_MAX; i++)
883 warning_on_global[i] = do_warn;
884 } else if (!nasm_stricmp(param, "none")) {
885 for (i = 1; i <= ERR_WARN_MAX; i++)
886 warning_on_global[i] = !do_warn;
887 } else {
888 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
889 "invalid warning `%s'", param);
890 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400894 switch (p[2]) {
895 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400896 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400897 break;
898 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400899 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400900 depend_missing_ok = true;
901 break;
902 case 'P':
903 depend_emit_phony = true;
904 break;
905 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300906 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400907 depend_file = q;
908 advance = true;
909 break;
910 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400911 depend_file = q;
912 advance = true;
913 break;
914 case 'T':
915 depend_target = q;
916 advance = true;
917 break;
918 case 'Q':
919 depend_target = quote_for_make(q);
920 advance = true;
921 break;
922 default:
923 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
924 "unknown dependency option `-M%c'", p[2]);
925 break;
926 }
927 if (advance && (!q || !q[0])) {
928 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
929 "option `-M%c' requires a parameter", p[2]);
930 break;
931 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000933
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 case '-':
935 {
936 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000937
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 if (p[2] == 0) { /* -- => stop processing options */
939 stopoptions = 1;
940 break;
941 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400942
943 if (!nasm_stricmp(p, "--v"))
944 show_version();
945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 for (s = 0; textopts[s].label; s++) {
947 if (!nasm_stricmp(p + 2, textopts[s].label)) {
948 break;
949 }
950 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000951
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 switch (s) {
953
954 case OPT_PREFIX:
955 case OPT_POSTFIX:
956 {
957 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400958 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 ERR_USAGE,
960 "option `--%s' requires an argument",
961 p + 2);
962 break;
963 } else {
964 advance = 1, param = q;
965 }
966
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100967 switch (s) {
968 case OPT_PREFIX:
969 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100971 case OPT_POSTFIX:
972 strlcpy(lpostfix, param, POSTFIX_MAX);
973 break;
974 default:
975 nasm_error(ERR_PANIC | ERR_NOFILE,
976 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 break;
978 }
979 break;
980 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800981
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 default:
983 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400984 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 "unrecognised option `--%s'", p + 2);
986 break;
987 }
988 }
989 break;
990 }
991
992 default:
993 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400994 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 "unrecognised option `-%c'", p[1]);
996 break;
997 }
998 } else {
999 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001000 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001002 } else {
1003 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001004 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001005 }
1006
1007 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001008}
1009
H. Peter Anvineba20a72002-04-30 20:53:55 +00001010#define ARG_BUF_DELTA 128
1011
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001013{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001014 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001015 int bufsize, prevargsize;
1016
1017 bufsize = prevargsize = ARG_BUF_DELTA;
1018 buffer = nasm_malloc(ARG_BUF_DELTA);
1019 prevarg = nasm_malloc(ARG_BUF_DELTA);
1020 prevarg[0] = '\0';
1021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 p = buffer;
1024 while (1) { /* Loop to handle long lines */
1025 q = fgets(p, bufsize - (p - buffer), rfile);
1026 if (!q)
1027 break;
1028 p += strlen(p);
1029 if (p > buffer && p[-1] == '\n')
1030 break;
1031 if (p - buffer > bufsize - 10) {
1032 int offset;
1033 offset = p - buffer;
1034 bufsize += ARG_BUF_DELTA;
1035 buffer = nasm_realloc(buffer, bufsize);
1036 p = buffer + offset;
1037 }
1038 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 if (!q && p == buffer) {
1041 if (prevarg[0])
1042 process_arg(prevarg, NULL);
1043 nasm_free(buffer);
1044 nasm_free(prevarg);
1045 return;
1046 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001047
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 /*
1049 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1050 * them are present at the end of the line.
1051 */
1052 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001053
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001054 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001057 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 if (process_arg(prevarg, p))
1060 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061
Charles Crayne192d5b52007-10-18 19:02:42 -07001062 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 prevargsize += ARG_BUF_DELTA;
1064 prevarg = nasm_realloc(prevarg, prevargsize);
1065 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001066 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001067 }
1068}
1069
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001070/* Function to process args from a string of args, rather than the
1071 * argv array. Used by the environment variable and response file
1072 * processing.
1073 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001074static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001076 char *p, *q, *arg, *prevarg;
1077 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001078
1079 p = args;
1080 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001082 arg = NULL;
1083 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 q = p;
1085 while (*p && *p != separator)
1086 p++;
1087 while (*p == separator)
1088 *p++ = '\0';
1089 prevarg = arg;
1090 arg = q;
1091 if (process_arg(prevarg, arg))
1092 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001093 }
1094 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001096}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001097
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001098static void process_response_file(const char *file)
1099{
1100 char str[2048];
1101 FILE *f = fopen(file, "r");
1102 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001103 perror(file);
1104 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001105 }
1106 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001107 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001108 }
1109 fclose(f);
1110}
1111
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001112static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001113{
1114 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001115 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001116 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001117
Charles Craynefcce07f2007-09-30 22:15:36 -07001118 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001119
H. Peter Anvin972079f2008-09-30 17:01:23 -07001120 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001121 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001122
1123 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001124 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001125 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001126 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001127 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 envcopy = nasm_strdup(envreal);
1129 process_args(envcopy);
1130 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001131 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001132
1133 /*
1134 * Now process the actual command line.
1135 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001137 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 argv++;
1139 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001140 /*
1141 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 * arguments like the environment variable. This allows us
1143 * to have multiple arguments on a single line, which is
1144 * different to the -@resp file processing below for regular
1145 * NASM.
1146 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001147 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 argc--;
1149 argv++;
1150 }
1151 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001152 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001153 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001154 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001155 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 process_respfile(rfile);
1157 fclose(rfile);
1158 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001159 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 "unable to open response file `%s'", p);
1161 }
1162 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001163 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1164 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001165 }
1166
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001167 /*
1168 * Look for basic command line typos. This definitely doesn't
1169 * catch all errors, but it might help cases of fumbled fingers.
1170 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001171 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001172 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001173 "no input file specified");
1174 else if (!strcmp(inname, errname) ||
1175 !strcmp(inname, outname) ||
1176 !strcmp(inname, listname) ||
1177 (depend_file && !strcmp(inname, depend_file)))
1178 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1179 "file `%s' is both input and output file",
1180 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001181
H. Peter Anvin70653092007-10-19 14:42:29 -07001182 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001183 error_file = fopen(errname, "w");
1184 if (!error_file) {
1185 error_file = stderr; /* Revert to default! */
1186 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1187 "cannot open file `%s' for error messages",
1188 errname);
1189 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001190 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001191}
1192
H. Peter Anvin70055962007-10-11 00:05:31 -07001193static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001194
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001195static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001196{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001197 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001199 int i, validid;
1200 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001201 int32_t seg;
1202 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001203 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001205 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001206
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001207 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001208 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001209
Charles Craynec1905c22008-09-11 18:54:06 -07001210 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001211 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 int pass1, pass2;
1213 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001215 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001216 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1217 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001219 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001220
Keith Kaniosb7a89542007-04-12 02:40:54 +00001221 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 cpu = cmd_cpu;
1223 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001224 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001226 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001227 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001229 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001230 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 saa_rewind(forwrefs);
1232 forwref = saa_rstruct(forwrefs);
1233 raa_free(offsets);
1234 offsets = raa_init();
1235 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001236 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001237 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001238
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001240 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001241 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001242 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001243
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001245 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001247
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001248 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001249 * Here we parse our directives; this is not handled by the
1250 * 'real' parser. This really should be a separate function.
1251 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001253 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001254 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001255 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001256
H. Peter Anvin70055962007-10-11 00:05:31 -07001257 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001258 case D_SEGMENT: /* [SEGMENT n] */
1259 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 seg = ofmt->section(value, pass2, &sb);
1261 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001262 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001263 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 value);
1265 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001266 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 location.segment = seg;
1268 }
1269 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001270 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001271 if (*value) {
1272 stdscan_reset();
1273 stdscan_set(value);
1274 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001275 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001276 if (e) {
1277 unsigned int align = (unsigned int)e->value;
1278 if ((uint64_t)e->value > 0x7fffffff) {
1279 /*
1280 * FIXME: Please make some sane message here
1281 * ofmt should have some 'check' method which
1282 * would report segment alignment bounds.
1283 */
1284 nasm_error(ERR_FATAL,
1285 "incorrect segment alignment `%s'", value);
1286 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001287 nasm_error(ERR_NONFATAL,
1288 "segment alignment `%s' is not power of two",
1289 value);
1290 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001291
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001292 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001293 if (location.segment != NO_SEG)
1294 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001295 }
1296 }
1297 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001298 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 if (*value == '$')
1300 value++; /* skip initial $ if present */
1301 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001302 q = value;
1303 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001305 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 *q++ = '\0';
1307 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001309 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001311 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001313 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001316 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 }
1319 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001320 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 "identifier expected after EXTERN");
1322 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 }
1324 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 *q++ = '\0';
1326 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 special = NULL;
1329 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1330 int temp = pass0;
1331 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001332 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001334 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001336 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 } /* else pass0 == 1 */
1338 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001339 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001340 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001342 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 if (*value == '$')
1344 value++; /* skip initial $ if present */
1345 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001346 q = value;
1347 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001349 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 *q++ = '\0';
1351 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001355 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001357 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001360 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001362 }
1363 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001364 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 "identifier expected after GLOBAL");
1366 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367 }
1368 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 *q++ = '\0';
1370 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001371 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001373 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 } /* pass == 1 */
1375 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001376 case D_COMMON: /* [COMMON symbol size:special] */
1377 {
1378 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001379
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 if (*value == '$')
1381 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001382 p = value;
1383 validid = true;
1384 if (!isidstart(*p))
1385 validid = false;
1386 while (*p && !nasm_isspace(*p)) {
1387 if (!isidchar(*p))
1388 validid = false;
1389 p++;
1390 }
1391 if (!validid) {
1392 nasm_error(ERR_NONFATAL,
1393 "identifier expected after COMMON");
1394 break;
1395 }
1396 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001397 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001398 q = p;
1399 while (*q && *q != ':')
1400 q++;
1401 if (*q == ':') {
1402 *q++ = '\0';
1403 special = q;
1404 } else {
1405 special = NULL;
1406 }
1407 size = readnum(p, &rn_error);
1408 if (rn_error) {
1409 nasm_error(ERR_NONFATAL,
1410 "invalid size specified"
1411 " in COMMON declaration");
1412 break;
1413 }
1414 } else {
1415 nasm_error(ERR_NONFATAL,
1416 "no size specified in"
1417 " COMMON declaration");
1418 break;
1419 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001420
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001421 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001422 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001423 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001424 if (special)
1425 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 }
1427 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001428 }
1429 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001431 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001433 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 if (e) {
1435 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001436 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 1 ? ERR_NONFATAL : ERR_PANIC,
1438 "cannot use non-relocatable expression as "
1439 "ABSOLUTE address");
1440 else {
1441 abs_seg = reloc_seg(e);
1442 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001443 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001444 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 abs_offset = 0x100; /* don't go near zero in case of / */
1446 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001447 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001449 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 location.segment = NO_SEG;
1451 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001452 case D_DEBUG: /* [DEBUG] */
1453 {
1454 char debugid[128];
1455 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001456
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 p = value;
1458 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001459 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001460 if (!isidstart(*p)) {
1461 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001462 } else {
1463 while (*p && !nasm_isspace(*p)) {
1464 if (q >= debugid + sizeof debugid - 1) {
1465 overlong = true;
1466 break;
1467 }
1468 if (!isidchar(*p))
1469 badid = true;
1470 *q++ = *p++;
1471 }
1472 *q = 0;
1473 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001474 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001475 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1476 "identifier expected after DEBUG");
1477 break;
1478 }
1479 if (overlong) {
1480 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1481 "DEBUG identifier too long");
1482 break;
1483 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001484 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001485 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001486 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001488 }
1489 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001490 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001491 switch(*value) {
1492 case '-': validid = 0; value++; break;
1493 case '+': validid = 1; value++; break;
1494 case '*': validid = 2; value++; break;
1495 default: validid = 1; break;
1496 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001498 for (i = 1; i <= ERR_WARN_MAX; i++)
1499 if (!nasm_stricmp(value, warnings[i].name))
1500 break;
1501 if (i <= ERR_WARN_MAX) {
1502 switch(validid) {
1503 case 0:
1504 warning_on[i] = false;
1505 break;
1506 case 1:
1507 warning_on[i] = true;
1508 break;
1509 case 2:
1510 warning_on[i] = warning_on_global[i];
1511 break;
1512 }
1513 } else
1514 nasm_error(ERR_NONFATAL,
1515 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001517 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 cpu = get_cpu(value);
1519 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001520 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001521 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 if (*value == '+') {
1523 user_nolist = 0;
1524 } else {
1525 if (*value == '-') {
1526 user_nolist = 1;
1527 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001528 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 }
1530 }
1531 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001532 case D_DEFAULT: /* [DEFAULT] */
1533 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001534 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001535 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001536 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001537 switch ((int)tokval.t_integer) {
1538 case S_REL:
1539 globalrel = 1;
1540 break;
1541 case S_ABS:
1542 globalrel = 0;
1543 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001544 case P_BND:
1545 globalbnd = 1;
1546 break;
1547 case P_NOBND:
1548 globalbnd = 0;
1549 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001550 default:
1551 err = 1;
1552 break;
1553 }
1554 } else {
1555 err = 1;
1556 }
1557 break;
1558 case D_FLOAT:
1559 if (float_option(value)) {
1560 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1561 "unknown 'float' directive: %s",
1562 value);
1563 }
1564 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001566 if (ofmt->directive(d, value, pass2))
1567 break;
1568 /* else fall through */
1569 case D_unknown:
1570 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1571 "unrecognised directive [%s]",
1572 directive);
1573 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001575 if (err) {
1576 nasm_error(ERR_NONFATAL,
1577 "invalid parameter to [%s] directive",
1578 directive);
1579 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001581 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582
Charles Crayne2581c862008-09-10 19:21:52 -07001583 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001585 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001587 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 forwref = saa_rstruct(forwrefs);
1589 } while (forwref != NULL
1590 && forwref->lineno == globallineno);
1591 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001593
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001594 if (output_ins.forw_ref) {
1595 if (passn == 1) {
1596 for (i = 0; i < output_ins.operands; i++) {
1597 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1598 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1599 fwinf->lineno = globallineno;
1600 fwinf->operand = i;
1601 }
1602 }
1603 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001604 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001606
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 /* forw_ref */
1608 if (output_ins.opcode == I_EQU) {
1609 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 * Special `..' EQUs get processed in pass two,
1612 * except `..@' macro-processor EQUs which are done
1613 * in the normal place.
1614 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001615 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001616 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001618
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619 else if (output_ins.label[0] != '.' ||
1620 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 output_ins.label[2] == '@') {
1622 if (output_ins.operands == 1 &&
1623 (output_ins.oprs[0].type & IMMEDIATE) &&
1624 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001625 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 def_label(output_ins.label,
1627 output_ins.oprs[0].segment,
1628 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001629 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001631 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001633 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001635 && (output_ins.oprs[1].type & IMMEDIATE)
1636 && output_ins.oprs[1].segment == NO_SEG
1637 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001639 output_ins.oprs[0].offset | SEG_ABS,
1640 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001641 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001643 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001645 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001646 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 * Special `..' EQUs get processed here, except
1649 * `..@' macro processor EQUs which are done above.
1650 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001651 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 output_ins.label[1] == '.' &&
1653 output_ins.label[2] != '@') {
1654 if (output_ins.operands == 1 &&
1655 (output_ins.oprs[0].type & IMMEDIATE)) {
1656 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001657 output_ins.oprs[0].segment,
1658 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001659 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001661 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001663 && output_ins.oprs[0].segment == NO_SEG
1664 && (output_ins.oprs[1].type & IMMEDIATE)
1665 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001667 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001668 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001669 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001671 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001673 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001674 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001676
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001678
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001679 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001680 &output_ins);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001681
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 /* if (using_debug_info) && output_ins.opcode != -1) */
1683 if (using_debug_info)
1684 { /* fbk 03/25/01 */
1685 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001686 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 TYS_ELEMENTS(output_ins.operands);
1688 switch (output_ins.opcode) {
1689 case I_RESB:
1690 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001691 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 break;
1693 case I_RESW:
1694 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001695 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 break;
1697 case I_RESD:
1698 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001699 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 break;
1701 case I_RESQ:
1702 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001703 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 break;
1705 case I_REST:
1706 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001707 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001709 case I_RESO:
1710 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001711 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001712 break;
1713 case I_RESY:
1714 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001715 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001716 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 case I_DB:
1718 typeinfo |= TY_BYTE;
1719 break;
1720 case I_DW:
1721 typeinfo |= TY_WORD;
1722 break;
1723 case I_DD:
1724 if (output_ins.eops_float)
1725 typeinfo |= TY_FLOAT;
1726 else
1727 typeinfo |= TY_DWORD;
1728 break;
1729 case I_DQ:
1730 typeinfo |= TY_QWORD;
1731 break;
1732 case I_DT:
1733 typeinfo |= TY_TBYTE;
1734 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001735 case I_DO:
1736 typeinfo |= TY_OWORD;
1737 break;
1738 case I_DY:
1739 typeinfo |= TY_YWORD;
1740 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 default:
1742 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001745
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001746 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001747 }
1748 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001750 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001751 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001752 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 * else l == -1 => invalid instruction, which will be
1754 * flagged as an error on pass 2
1755 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001756
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001757 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001759 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001760 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001761
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 }
1763 } /* not an EQU */
1764 cleanup_insn(&output_ins);
1765 }
1766 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001767 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001769
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001770 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001771 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001772 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001773
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 if (pass1 == 1)
1775 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001776
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001777 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001779 } else if (global_offset_changed &&
1780 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001781 prev_offset_changed = global_offset_changed;
1782 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001783 } else {
1784 stall_count++;
1785 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001786
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001787 if (terminate_after_phase)
1788 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001789
1790 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001791 /* We get here if the labels don't converge
1792 * Example: FOO equ FOO + 1
1793 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001794 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001795 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001796 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001797 nasm_error(ERR_NONFATAL,
1798 "Possible causes: recursive EQUs, macro abuse.");
1799 break;
1800 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001801 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001802
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001804 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001805 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001806 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001807 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1808 }
1809}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001810
H. Peter Anvin70055962007-10-11 00:05:31 -07001811static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001812{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001813 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001815 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001816
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001817 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001819 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001820 q = strchr(buf, ']');
1821 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001822 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001823
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001824 /* stip off the comments */
1825 p = strchr(buf, ';');
1826 if (p) {
1827 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001828 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001829 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001830 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001831
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001832 /* no brace, no trailing spaces */
1833 *q = '\0';
1834 nasm_zap_spaces_rev(--q);
1835
1836 /* directive */
1837 p = nasm_skip_spaces(++buf);
1838 q = nasm_skip_word(p);
1839 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001840 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001841 *q = '\0';
1842 *directive = p;
1843
1844 /* and value finally */
1845 p = nasm_skip_spaces(++q);
1846 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001847
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001848 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001849}
1850
Ed Berosetfa771012002-06-09 20:56:40 +00001851/**
1852 * gnu style error reporting
1853 * This function prints an error message to error_file in the
1854 * style used by GNU. An example would be:
1855 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001856 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001857 * which the error occurs (or is detected) and "error:" is one of
1858 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001859 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001860 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001861 *
1862 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001863 * @param fmt the printf style format string
1864 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001865static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001866{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001867 char *currentfile = NULL;
1868 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001869
Ed Berosetfa771012002-06-09 20:56:40 +00001870 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001872
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001873 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001875
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001876 if (!skip_this_pass(severity)) {
1877 if (currentfile) {
1878 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1879 nasm_free(currentfile);
1880 } else {
1881 fputs("nasm: ", error_file);
1882 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001883 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001884
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001885 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001886}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001887
Ed Berosetfa771012002-06-09 20:56:40 +00001888/**
1889 * MS style error reporting
1890 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001891 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001892 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001893 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001894 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001895 * which the error occurs (or is detected) and "error:" is one of
1896 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001897 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001898 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001899 *
1900 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001901 * @param fmt the printf style format string
1902 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001903static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001904{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001905 char *currentfile = NULL;
1906 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001907
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 if (is_suppressed_warning(severity))
1909 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001910
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001911 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001913
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001914 if (!skip_this_pass(severity)) {
1915 if (currentfile) {
1916 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1917 nasm_free(currentfile);
1918 } else {
1919 fputs("nasm: ", error_file);
1920 }
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
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001940 /* Might be a warning but suppresed explicitly */
1941 if (severity & ERR_WARN_MASK)
1942 return !warning_on[WARN_IDX(severity)];
1943 else
1944 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001945}
1946
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001947static bool skip_this_pass(int severity)
1948{
1949 /* See if it's a pass-one only warning and we're not in pass one. */
1950 if ((severity & ERR_MASK) > ERR_WARNING)
1951 return false;
1952
1953 if (((severity & ERR_PASS1) && pass0 != 1) ||
1954 ((severity & ERR_PASS2) && pass0 != 2))
1955 return true;
1956
1957 return false;
1958}
1959
Ed Berosetfa771012002-06-09 20:56:40 +00001960/**
1961 * common error reporting
1962 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001963 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001964 * specific error message to error_file and may or may not return. It
1965 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001966 *
1967 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001968 * @param fmt the printf style format string
1969 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001970static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001971{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001972 char msg[1024];
1973 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001974
H. Peter Anvin7df04172008-06-10 18:27:38 -07001975 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001977 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 break;
1979 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001980 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 break;
1982 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001983 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 break;
1985 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001986 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 break;
1988 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001989 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001991 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001992 pfx = "";
1993 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001994 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001995
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001996 vsnprintf(msg, sizeof msg, fmt, args);
1997
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001998 if (!skip_this_pass(severity))
1999 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002000
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002001 /*
2002 * Don't suppress this with skip_this_pass(), or we don't get
2003 * preprocessor warnings in the list file
2004 */
2005 if ((severity & ERR_MASK) >= ERR_WARNING)
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002006 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002007
2008 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002009 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002010
2011 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002012 case ERR_DEBUG:
2013 /* no further action, by definition */
2014 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002015 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002016 /* Treat warnings as errors */
2017 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2018 terminate_after_phase = true;
2019 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002021 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 break;
2023 case ERR_FATAL:
2024 if (ofile) {
2025 fclose(ofile);
2026 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002027 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 }
2029 if (want_usage)
2030 usage();
2031 exit(1); /* instantly die */
2032 break; /* placate silly compilers */
2033 case ERR_PANIC:
2034 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002035 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002036 if (ofile) {
2037 fclose(ofile);
2038 remove(outname);
2039 ofile = NULL;
2040 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 exit(3);
2042 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002043 }
2044}
2045
H. Peter Anvin734b1882002-04-30 21:01:08 +00002046static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002047{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002048 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002049}
2050
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002051static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002052{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002053 iflag_t r;
2054
2055 iflag_clear_all(&r);
2056
H. Peter Anvine2c80182005-01-15 22:15:51 +00002057 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002058 iflag_set(&r, IF_8086);
2059 else if (!strcmp(value, "186"))
2060 iflag_set(&r, IF_186);
2061 else if (!strcmp(value, "286"))
2062 iflag_set(&r, IF_286);
2063 else if (!strcmp(value, "386"))
2064 iflag_set(&r, IF_386);
2065 else if (!strcmp(value, "486"))
2066 iflag_set(&r, IF_486);
2067 else if (!strcmp(value, "586") ||
2068 !nasm_stricmp(value, "pentium"))
2069 iflag_set(&r, IF_PENT);
2070 else if (!strcmp(value, "686") ||
2071 !nasm_stricmp(value, "ppro") ||
2072 !nasm_stricmp(value, "pentiumpro") ||
2073 !nasm_stricmp(value, "p2"))
2074 iflag_set(&r, IF_P6);
2075 else if (!nasm_stricmp(value, "p3") ||
2076 !nasm_stricmp(value, "katmai"))
2077 iflag_set(&r, IF_KATMAI);
2078 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2079 !nasm_stricmp(value, "willamette"))
2080 iflag_set(&r, IF_WILLAMETTE);
2081 else if (!nasm_stricmp(value, "prescott"))
2082 iflag_set(&r, IF_PRESCOTT);
2083 else if (!nasm_stricmp(value, "x64") ||
2084 !nasm_stricmp(value, "x86-64"))
2085 iflag_set(&r, IF_X86_64);
2086 else if (!nasm_stricmp(value, "ia64") ||
2087 !nasm_stricmp(value, "ia-64") ||
2088 !nasm_stricmp(value, "itanium")||
2089 !nasm_stricmp(value, "itanic") ||
2090 !nasm_stricmp(value, "merced"))
2091 iflag_set(&r, IF_IA64);
2092 else {
2093 iflag_set(&r, IF_PLEVEL);
2094 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2095 "unknown 'cpu' type");
2096 }
2097 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002098}
2099
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002100static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002101{
2102 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002103
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 if ((i = atoi(value)) == 16)
2105 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002106 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002107 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002108 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 "cannot specify 32-bit segment on processor below a 386");
2110 i = 16;
2111 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002112 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002113 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002114 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002115 "cannot specify 64-bit segment on processor below an x86-64");
2116 i = 16;
2117 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002118 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002119 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002120 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002121 value);
2122 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002123 }
2124 return i;
2125}