blob: 17ee5538de18c249228320e8c0b3db756ce9da98 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070046#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080047#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49#include "nasm.h"
50#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvin31b707b2009-06-27 22:07:33 -070061#include "output/outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvin2b046cf2008-01-22 14:08:36 -080084static bool is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085static void usage(void);
86
John Coffman0efaec92002-05-26 19:20:08 +000087static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070088bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080089int pass0, passn;
Keith Kaniosb7a89542007-04-12 02:40:54 +000090int maxbits = 0;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +010091static bool allow_64_bit = false;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000092int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080093int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000094
H. Peter Anvin9bd15062009-07-18 21:07:17 -040095static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080096
Keith Kaniosa6dfa782007-04-13 16:47:53 +000097static char inname[FILENAME_MAX];
98static char outname[FILENAME_MAX];
99static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700100static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000102/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400103struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400104struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400105const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000106
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000108
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400109FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700110int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
111static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400112
113static iflag_t cpu;
114static iflag_t cmd_cpu;
115
Charles Craynec1905c22008-09-11 18:54:06 -0700116int64_t global_offset_changed; /* referenced in labels.c */
117int64_t prev_offset_changed;
118int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000119
H. Peter Anvin12e46512007-10-03 21:30:57 -0700120static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000121int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000122int32_t abs_seg; /* ABSOLUTE segment basis */
123int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000124
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000126
H. Peter Anvine2c80182005-01-15 22:15:51 +0000127static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700128static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400130static struct preproc_ops *preproc;
131
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400132#define OP_NORMAL (1u << 0)
133#define OP_PREPROCESS (1u << 1)
134#define OP_DEPEND (1u << 2)
135
136static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400137
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700138/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700139static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700140static bool depend_missing_ok = false;
141static const char *depend_target = NULL;
142static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000145 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800146 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000147 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200148
H. Peter Anvin972079f2008-09-30 17:01:23 -0700149static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
150static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000151
H. Peter Anvin972079f2008-09-30 17:01:23 -0700152static const struct warning {
153 const char *name;
154 const char *help;
155 bool enabled;
156} warnings[ERR_WARN_MAX+1] = {
157 {"error", "treat warnings as errors", false},
158 {"macro-params", "macro calls with wrong parameter count", true},
159 {"macro-selfref", "cyclic macro references", false},
160 {"macro-defaults", "macros with more default than optional parameters", true},
161 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700162 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700163 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
164 {"float-overflow", "floating point overflow", true},
165 {"float-denorm", "floating point denormal", false},
166 {"float-underflow", "floating point underflow", false},
167 {"float-toolong", "too many digits in floating-point number", true},
168 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800169 {"lock", "lock prefix on unlockable instructions", true},
170 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800171 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000172};
173
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700174static bool want_usage;
175static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000176int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
H. Peter Anvin55340992012-09-09 17:09:00 -0700178static char *quote_for_make(const char *str);
179
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400180static int64_t get_curr_offs(void)
181{
182 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
183}
184
185static void set_curr_offs(int64_t l_off)
186{
187 if (in_abs_seg)
188 abs_offset = l_off;
189 else
190 offsets = raa_write(offsets, location.segment, l_off);
191}
192
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000193static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000195 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000196 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800197 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000200}
201
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800202/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800203static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800204{
205 int64_t t;
206 int64_t y = tm->tm_year;
207
208 /* See IEEE 1003.1:2004, section 4.14 */
209
210 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
211 t += tm->tm_yday;
212 t *= 24;
213 t += tm->tm_hour;
214 t *= 60;
215 t += tm->tm_min;
216 t *= 60;
217 t += tm->tm_sec;
218
219 return t;
220}
221
222static void define_macros_early(void)
223{
224 char temp[128];
225 struct tm lt, *lt_p, gm, *gm_p;
226 int64_t posix_time;
227
228 lt_p = localtime(&official_compile_time);
229 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400230 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800231
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400232 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
233 preproc->pre_define(temp);
234 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
235 preproc->pre_define(temp);
236 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
237 preproc->pre_define(temp);
238 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
239 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800240 }
241
242 gm_p = gmtime(&official_compile_time);
243 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400244 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800245
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400246 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
247 preproc->pre_define(temp);
248 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
249 preproc->pre_define(temp);
250 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
251 preproc->pre_define(temp);
252 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
253 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800254 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700255
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800256 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800257 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800259 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400261 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262
263 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
265 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800266 }
267}
268
269static void define_macros_late(void)
270{
271 char temp[128];
272
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400273 /*
274 * In case if output format is defined by alias
275 * we have to put shortname of the alias itself here
276 * otherwise ABI backward compatibility gets broken.
277 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400278 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400279 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400280 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800281}
282
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700283static void emit_dependencies(StrList *list)
284{
285 FILE *deps;
286 int linepos, len;
287 StrList *l, *nl;
288
289 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400290 deps = fopen(depend_file, "w");
291 if (!deps) {
292 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
293 "unable to write dependency file `%s'", depend_file);
294 return;
295 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700296 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400297 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700298 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700299
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700300 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400301 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700302 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400303 len = strlen(file);
304 if (linepos + len > 62 && linepos > 1) {
305 fprintf(deps, " \\\n ");
306 linepos = 1;
307 }
308 fprintf(deps, " %s", file);
309 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700310 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700311 }
312 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700313
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400314 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400315 if (depend_emit_phony)
316 fprintf(deps, "%s:\n\n", l->str);
317 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700318 }
319
320 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400321 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700322}
323
H. Peter Anvin038d8612007-04-12 16:54:50 +0000324int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000325{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700327
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800328 time(&official_compile_time);
329
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400330 iflag_set(&cpu, IF_PLEVEL);
331 iflag_set(&cmd_cpu, IF_PLEVEL);
332
Charles Crayne2581c862008-09-10 19:21:52 -0700333 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700334 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400335 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000336
H. Peter Anvin97e15752007-09-25 08:47:47 -0700337 error_file = stderr;
338
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700339 tolower_init();
340
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000341 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000342 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000343
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400345 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000346
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000347 seg_init();
348
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800349 /* Define some macros dependent on the runtime, but not
350 on the command line. */
351 define_macros_early();
352
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353 parse_cmdline(argc, argv);
354
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 if (terminate_after_phase) {
356 if (want_usage)
357 usage();
358 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359 }
360
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000361 /* If debugging info is disabled, suppress any debug calls */
362 if (!using_debug_info)
363 ofmt->current_dfmt = &null_debug_form;
364
H. Peter Anvin76690a12002-04-30 20:52:49 +0000365 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400366 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700367 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000370 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800371 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000372
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400373 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700374 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400375 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700376
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400377 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700379
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400380 if (depend_missing_ok)
381 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700382
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700383 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400385 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 while ((line = preproc->getline()))
388 nasm_free(line);
389 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400390 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000391 char *line;
392 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000393 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000395
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 if (*outname) {
397 ofile = fopen(outname, "w");
398 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400399 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 "unable to open output file `%s'",
401 outname);
402 } else
403 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000404
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700405 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000406
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400407 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700408 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400409 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700410
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 while ((line = preproc->getline())) {
412 /*
413 * We generate %line directives if needed for later programs
414 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000415 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 int altline = src_get(&linnum, &file_name);
417 if (altline) {
418 if (altline == 1 && lineinc == 1)
419 nasm_fputs("", ofile);
420 else {
421 lineinc = (altline != -1 || lineinc != 1);
422 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000423 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 file_name);
425 }
426 prior_linnum = linnum;
427 }
428 nasm_fputs(line, ofile);
429 nasm_free(line);
430 }
431 nasm_free(file_name);
432 preproc->cleanup(0);
433 if (ofile)
434 fclose(ofile);
435 if (ofile && terminate_after_phase)
436 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400437 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400438 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400440 if (operating_mode & OP_NORMAL) {
441 /*
442 * We must call ofmt->filename _anyway_, even if the user
443 * has specified their own output file, because some
444 * formats (eg OBJ and COFF) use ofmt->filename to find out
445 * the name of the input file and then put that inside the
446 * file.
447 */
448 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400450 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
451 if (!ofile)
452 nasm_error(ERR_FATAL | ERR_NOFILE,
453 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400455 /*
456 * We must call init_labels() before ofmt->init() since
457 * some object formats will want to define labels in their
458 * init routines. (eg OS/2 defines the FLAT group)
459 */
460 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400462 ofmt->init();
463 dfmt = ofmt->current_dfmt;
464 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200467
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400468 if (!terminate_after_phase) {
469 ofmt->cleanup(using_debug_info);
470 cleanup_labels();
471 fflush(ofile);
472 if (ferror(ofile))
473 nasm_error(ERR_NONFATAL|ERR_NOFILE,
474 "write error on output file `%s'", outname);
475 }
476
477 if (ofile) {
478 fclose(ofile);
479 if (terminate_after_phase)
480 remove(outname);
481 ofile = NULL;
482 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000484
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700485 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400486 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700487
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 raa_free(offsets);
492 saa_free(forwrefs);
493 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000494 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700496 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000497}
498
H. Peter Anvineba20a72002-04-30 20:53:55 +0000499/*
500 * Get a parameter for a command line option.
501 * First arg must be in the form of e.g. -f...
502 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800503static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800505 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400506 if (p[2]) /* the parameter's in the option */
507 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800509 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400512 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514 return NULL;
515}
516
H. Peter Anvindc242712007-11-18 11:55:10 -0800517/*
518 * Copy a filename
519 */
520static void copy_filename(char *dst, const char *src)
521{
522 size_t len = strlen(src);
523
524 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400525 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
526 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800527 }
528 strncpy(dst, src, FILENAME_MAX);
529}
530
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700531/*
532 * Convert a string to Make-safe form
533 */
534static char *quote_for_make(const char *str)
535{
536 const char *p;
537 char *os, *q;
538
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400539 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700540 size_t nbs = 0;
541
542 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400543 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700544
545 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400546 switch (*p) {
547 case ' ':
548 case '\t':
549 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
550 n += nbs + 2;
551 nbs = 0;
552 break;
553 case '$':
554 case '#':
555 nbs = 0;
556 n += 2;
557 break;
558 case '\\':
559 nbs++;
560 n++;
561 break;
562 default:
563 nbs = 0;
564 n++;
565 break;
566 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700567 }
568
569 /* Convert N backslashes at the end of filename to 2N backslashes */
570 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400571 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700572
573 os = q = nasm_malloc(n);
574
575 nbs = 0;
576 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400577 switch (*p) {
578 case ' ':
579 case '\t':
580 while (nbs--)
581 *q++ = '\\';
582 *q++ = '\\';
583 *q++ = *p;
584 break;
585 case '$':
586 *q++ = *p;
587 *q++ = *p;
588 nbs = 0;
589 break;
590 case '#':
591 *q++ = '\\';
592 *q++ = *p;
593 nbs = 0;
594 break;
595 case '\\':
596 *q++ = *p;
597 nbs++;
598 break;
599 default:
600 *q++ = *p;
601 nbs = 0;
602 break;
603 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700604 }
605 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400606 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700607
608 *q = '\0';
609
610 return os;
611}
612
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000614 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000616};
617
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100618enum text_options {
619 OPT_PREFIX,
620 OPT_POSTFIX,
621 OPT_ALLOW_64_BIT
622};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623struct textargs textopts[] = {
624 {"prefix", OPT_PREFIX},
625 {"postfix", OPT_POSTFIX},
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100626 {"allow-64-bit", OPT_ALLOW_64_BIT},
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000628};
629
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400630static void show_version(void)
631{
632 printf("NASM version %s compiled on %s%s\n",
633 nasm_version, nasm_date, nasm_compile_options);
634 exit(0);
635}
636
H. Peter Anvin423e3812007-11-15 17:12:29 -0800637static bool stopoptions = false;
638static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000639{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000640 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800641 int i;
642 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700643 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000644
645 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800646 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000647
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400649 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
650 /* These parameters take values */
651 if (!(param = get_param(p, q, &advance)))
652 return advance;
653 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 switch (p[1]) {
656 case 's':
657 error_file = stdout;
658 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700659
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400660 case 'o': /* output file */
661 copy_filename(outname, param);
662 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700663
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400664 case 'f': /* output format */
665 ofmt = ofmt_find(param, &ofmt_alias);
666 if (!ofmt) {
667 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
668 "unrecognised output format `%s' - "
669 "use -hf for a list", param);
670 }
671 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700672
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400673 case 'O': /* Optimization level */
674 {
675 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700676
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400677 if (!*param) {
678 /* Naked -O == -Ox */
679 optimizing = MAX_OPTIMIZE;
680 } else {
681 while (*param) {
682 switch (*param) {
683 case '0': case '1': case '2': case '3': case '4':
684 case '5': case '6': case '7': case '8': case '9':
685 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700686
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400687 /* -O0 -> optimizing == -1, 0.98 behaviour */
688 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
689 if (opt < 2)
690 optimizing = opt - 1;
691 else
692 optimizing = opt;
693 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700694
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400695 case 'v':
696 case '+':
697 param++;
698 opt_verbose_info = true;
699 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700700
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400701 case 'x':
702 param++;
703 optimizing = MAX_OPTIMIZE;
704 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700705
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400706 default:
707 nasm_error(ERR_FATAL,
708 "unknown optimization option -O%c\n",
709 *param);
710 break;
711 }
712 }
713 if (optimizing > MAX_OPTIMIZE)
714 optimizing = MAX_OPTIMIZE;
715 }
716 break;
717 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800718
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400719 case 'p': /* pre-include */
720 case 'P':
721 preproc->pre_include(param);
722 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800723
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400724 case 'd': /* pre-define */
725 case 'D':
726 preproc->pre_define(param);
727 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800728
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400729 case 'u': /* un-define */
730 case 'U':
731 preproc->pre_undefine(param);
732 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800733
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400734 case 'i': /* include search path */
735 case 'I':
736 preproc->include_path(param);
737 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800738
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 case 'l': /* listing file */
740 copy_filename(listname, param);
741 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800742
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400743 case 'Z': /* error messages file */
744 copy_filename(errname, param);
745 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800746
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400747 case 'F': /* specify debug format */
748 ofmt->current_dfmt = dfmt_find(ofmt, param);
749 if (!ofmt->current_dfmt) {
750 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
751 "unrecognized debug format `%s' for"
752 " output format `%s'",
753 param, ofmt->shortname);
754 }
755 using_debug_info = true;
756 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800757
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400758 case 'X': /* specify error reporting format */
759 if (nasm_stricmp("vc", param) == 0)
760 nasm_set_verror(nasm_verror_vc);
761 else if (nasm_stricmp("gnu", param) == 0)
762 nasm_set_verror(nasm_verror_gnu);
763 else
764 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
765 "unrecognized error reporting format `%s'",
766 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800768
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700770 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800772
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 case 'h':
774 printf
775 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
776 "[-l listfile]\n"
777 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400778 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400780 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700782 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700784 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400785 " -MG d:o, missing files assumed generated\n"
786 " -MF <file> set Makefile dependency file\n"
787 " -MD <file> assemble and generate dependencies\n"
788 " -MT <file> dependency target name\n"
789 " -MQ <file> dependency target name (quoted)\n"
790 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700791 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 " -s redirect error messages to stdout\n\n"
793 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400794 " -o outfile write output to an outfile\n\n"
795 " -f format select an output format\n\n"
796 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 " -I<path> adds a pathname to the include file path\n");
798 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700799 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400800 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400801 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400802 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 " -P<file> pre-includes a file\n"
804 " -D<macro>[=<value>] pre-defines a macro\n"
805 " -U<macro> undefines a macro\n"
806 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800807 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400808 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400809 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400810 "--prefix,--postfix\n"
811 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100812 " extern and global variables\n"
813 "--allow-64-bit\n"
814 " do not restrict 64-bit code to 64-bit capable output\n"
815 " formats (use with care, no complaining)\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800816 "Warnings:\n");
817 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700819 warnings[i].name, warnings[i].help,
820 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 printf
822 ("\nresponse files should contain command line parameters"
823 ", one per line.\n");
824 if (p[2] == 'f') {
825 printf("\nvalid output formats for -f are"
826 " (`*' denotes default):\n");
827 ofmt_list(ofmt, stdout);
828 } else {
829 printf("\nFor a list of valid output formats, use -hf.\n");
830 printf("For a list of debug formats, use -f <form> -y.\n");
831 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400832 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800834
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 case 'y':
836 printf("\nvalid debug formats for '%s' output format are"
837 " ('*' denotes default):\n", ofmt->shortname);
838 dfmt_list(ofmt, stdout);
839 exit(0);
840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700843 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800845
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400847 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800849
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400850 case 'e': /* preprocess only */
851 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400852 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800854
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400855 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400856 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800858
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400859 case 'W':
860 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
861 do_warn = false;
862 param += 3;
863 } else {
864 do_warn = true;
865 }
866 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800867
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800869 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400870 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400872 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400874 do_warn = (param[0] == '+');
875 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400876
877set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400878 for (i = 0; i <= ERR_WARN_MAX; i++) {
879 if (!nasm_stricmp(param, warnings[i].name))
880 break;
881 }
882 if (i <= ERR_WARN_MAX) {
883 warning_on_global[i] = do_warn;
884 } else if (!nasm_stricmp(param, "all")) {
885 for (i = 1; i <= ERR_WARN_MAX; i++)
886 warning_on_global[i] = do_warn;
887 } else if (!nasm_stricmp(param, "none")) {
888 for (i = 1; i <= ERR_WARN_MAX; i++)
889 warning_on_global[i] = !do_warn;
890 } else {
891 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
892 "invalid warning `%s'", param);
893 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800895
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400897 switch (p[2]) {
898 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400899 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400900 break;
901 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400902 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400903 depend_missing_ok = true;
904 break;
905 case 'P':
906 depend_emit_phony = true;
907 break;
908 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300909 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400910 depend_file = q;
911 advance = true;
912 break;
913 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400914 depend_file = q;
915 advance = true;
916 break;
917 case 'T':
918 depend_target = q;
919 advance = true;
920 break;
921 case 'Q':
922 depend_target = quote_for_make(q);
923 advance = true;
924 break;
925 default:
926 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
927 "unknown dependency option `-M%c'", p[2]);
928 break;
929 }
930 if (advance && (!q || !q[0])) {
931 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
932 "option `-M%c' requires a parameter", p[2]);
933 break;
934 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000936
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 case '-':
938 {
939 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 if (p[2] == 0) { /* -- => stop processing options */
942 stopoptions = 1;
943 break;
944 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400945
946 if (!nasm_stricmp(p, "--v"))
947 show_version();
948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 for (s = 0; textopts[s].label; s++) {
950 if (!nasm_stricmp(p + 2, textopts[s].label)) {
951 break;
952 }
953 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000954
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 switch (s) {
956
957 case OPT_PREFIX:
958 case OPT_POSTFIX:
959 {
960 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400961 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 ERR_USAGE,
963 "option `--%s' requires an argument",
964 p + 2);
965 break;
966 } else {
967 advance = 1, param = q;
968 }
969
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100970 switch (s) {
971 case OPT_PREFIX:
972 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100974 case OPT_POSTFIX:
975 strlcpy(lpostfix, param, POSTFIX_MAX);
976 break;
977 default:
978 nasm_error(ERR_PANIC | ERR_NOFILE,
979 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 break;
981 }
982 break;
983 }
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100984 case OPT_ALLOW_64_BIT:
985 allow_64_bit = true;
986 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 default:
988 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400989 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 "unrecognised option `--%s'", p + 2);
991 break;
992 }
993 }
994 break;
995 }
996
997 default:
998 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400999 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 "unrecognised option `-%c'", p[1]);
1001 break;
1002 }
1003 } else {
1004 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001005 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001007 } else {
1008 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001009 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001010 }
1011
1012 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001013}
1014
H. Peter Anvineba20a72002-04-30 20:53:55 +00001015#define ARG_BUF_DELTA 128
1016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001018{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001019 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001020 int bufsize, prevargsize;
1021
1022 bufsize = prevargsize = ARG_BUF_DELTA;
1023 buffer = nasm_malloc(ARG_BUF_DELTA);
1024 prevarg = nasm_malloc(ARG_BUF_DELTA);
1025 prevarg[0] = '\0';
1026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 p = buffer;
1029 while (1) { /* Loop to handle long lines */
1030 q = fgets(p, bufsize - (p - buffer), rfile);
1031 if (!q)
1032 break;
1033 p += strlen(p);
1034 if (p > buffer && p[-1] == '\n')
1035 break;
1036 if (p - buffer > bufsize - 10) {
1037 int offset;
1038 offset = p - buffer;
1039 bufsize += ARG_BUF_DELTA;
1040 buffer = nasm_realloc(buffer, bufsize);
1041 p = buffer + offset;
1042 }
1043 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 if (!q && p == buffer) {
1046 if (prevarg[0])
1047 process_arg(prevarg, NULL);
1048 nasm_free(buffer);
1049 nasm_free(prevarg);
1050 return;
1051 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 /*
1054 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1055 * them are present at the end of the line.
1056 */
1057 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001059 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001062 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 if (process_arg(prevarg, p))
1065 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001066
Charles Crayne192d5b52007-10-18 19:02:42 -07001067 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 prevargsize += ARG_BUF_DELTA;
1069 prevarg = nasm_realloc(prevarg, prevargsize);
1070 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001071 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001072 }
1073}
1074
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001075/* Function to process args from a string of args, rather than the
1076 * argv array. Used by the environment variable and response file
1077 * processing.
1078 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001079static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001081 char *p, *q, *arg, *prevarg;
1082 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001083
1084 p = args;
1085 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001087 arg = NULL;
1088 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 q = p;
1090 while (*p && *p != separator)
1091 p++;
1092 while (*p == separator)
1093 *p++ = '\0';
1094 prevarg = arg;
1095 arg = q;
1096 if (process_arg(prevarg, arg))
1097 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001098 }
1099 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001101}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001102
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001103static void process_response_file(const char *file)
1104{
1105 char str[2048];
1106 FILE *f = fopen(file, "r");
1107 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001108 perror(file);
1109 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001110 }
1111 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001112 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001113 }
1114 fclose(f);
1115}
1116
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001117static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001118{
1119 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001120 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001121 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001122
Charles Craynefcce07f2007-09-30 22:15:36 -07001123 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001124
H. Peter Anvin972079f2008-09-30 17:01:23 -07001125 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001126 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001127
1128 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001129 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001130 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001131 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001132 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 envcopy = nasm_strdup(envreal);
1134 process_args(envcopy);
1135 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001136 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001137
1138 /*
1139 * Now process the actual command line.
1140 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001142 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 argv++;
1144 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001145 /*
1146 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 * arguments like the environment variable. This allows us
1148 * to have multiple arguments on a single line, which is
1149 * different to the -@resp file processing below for regular
1150 * NASM.
1151 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001152 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 argc--;
1154 argv++;
1155 }
1156 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001157 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001158 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001159 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001160 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 process_respfile(rfile);
1162 fclose(rfile);
1163 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001164 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 "unable to open response file `%s'", p);
1166 }
1167 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001168 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1169 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001170 }
1171
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001172 /*
1173 * Look for basic command line typos. This definitely doesn't
1174 * catch all errors, but it might help cases of fumbled fingers.
1175 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001176 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001177 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001178 "no input file specified");
1179 else if (!strcmp(inname, errname) ||
1180 !strcmp(inname, outname) ||
1181 !strcmp(inname, listname) ||
1182 (depend_file && !strcmp(inname, depend_file)))
1183 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1184 "file `%s' is both input and output file",
1185 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001186
H. Peter Anvin70653092007-10-19 14:42:29 -07001187 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001188 error_file = fopen(errname, "w");
1189 if (!error_file) {
1190 error_file = stderr; /* Revert to default! */
1191 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1192 "cannot open file `%s' for error messages",
1193 errname);
1194 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001195 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001196}
1197
H. Peter Anvin70055962007-10-11 00:05:31 -07001198static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001199
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001200static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001201{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001202 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001204 int i, validid;
1205 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001206 int32_t seg;
1207 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001208 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001210 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001211
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001212 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001213 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001215
Charles Craynec1905c22008-09-11 18:54:06 -07001216 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001217 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 int pass1, pass2;
1219 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001220
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001221 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001222 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1223 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001225 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001226
Keith Kaniosb7a89542007-04-12 02:40:54 +00001227 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 cpu = cmd_cpu;
1229 if (pass0 == 2) {
1230 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001231 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001233 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001234 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001236 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001237 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 saa_rewind(forwrefs);
1239 forwref = saa_rstruct(forwrefs);
1240 raa_free(offsets);
1241 offsets = raa_init();
1242 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001243 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001244 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001245 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001246
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001248 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001249 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001250 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001251
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001253 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001255
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001256 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001257 * Here we parse our directives; this is not handled by the
1258 * 'real' parser. This really should be a separate function.
1259 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001261 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001262 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001263 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001264
H. Peter Anvin70055962007-10-11 00:05:31 -07001265 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001266 case D_SEGMENT: /* [SEGMENT n] */
1267 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 seg = ofmt->section(value, pass2, &sb);
1269 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001270 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001271 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 value);
1273 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001274 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 location.segment = seg;
1276 }
1277 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001278 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001279 if (*value) {
1280 stdscan_reset();
1281 stdscan_set(value);
1282 tokval.t_type = TOKEN_INVALID;
1283 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1284 if (e) {
1285 unsigned int align = (unsigned int)e->value;
1286 if ((uint64_t)e->value > 0x7fffffff) {
1287 /*
1288 * FIXME: Please make some sane message here
1289 * ofmt should have some 'check' method which
1290 * would report segment alignment bounds.
1291 */
1292 nasm_error(ERR_FATAL,
1293 "incorrect segment alignment `%s'", value);
1294 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001295 nasm_error(ERR_NONFATAL,
1296 "segment alignment `%s' is not power of two",
1297 value);
1298 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001299 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001300 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001301 }
1302 }
1303 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001304 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 if (*value == '$')
1306 value++; /* skip initial $ if present */
1307 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308 q = value;
1309 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001311 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 *q++ = '\0';
1313 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001315 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001316 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001317 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001319 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001320 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001322 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001324 }
1325 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001326 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 "identifier expected after EXTERN");
1328 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329 }
1330 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 *q++ = '\0';
1332 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001333 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 special = NULL;
1335 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1336 int temp = pass0;
1337 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001338 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001340 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 } /* else pass0 == 1 */
1344 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001345 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001346 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001348 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 if (*value == '$')
1350 value++; /* skip initial $ if present */
1351 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 q = value;
1353 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001355 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 *q++ = '\0';
1357 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001361 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001362 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001363 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001366 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001368 }
1369 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001370 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 "identifier expected after GLOBAL");
1372 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 }
1374 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 *q++ = '\0';
1376 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001377 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001379 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 } /* pass == 1 */
1381 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001382 case D_COMMON: /* [COMMON symbol size:special] */
1383 {
1384 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001385
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (*value == '$')
1387 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001388 p = value;
1389 validid = true;
1390 if (!isidstart(*p))
1391 validid = false;
1392 while (*p && !nasm_isspace(*p)) {
1393 if (!isidchar(*p))
1394 validid = false;
1395 p++;
1396 }
1397 if (!validid) {
1398 nasm_error(ERR_NONFATAL,
1399 "identifier expected after COMMON");
1400 break;
1401 }
1402 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001403 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001404 q = p;
1405 while (*q && *q != ':')
1406 q++;
1407 if (*q == ':') {
1408 *q++ = '\0';
1409 special = q;
1410 } else {
1411 special = NULL;
1412 }
1413 size = readnum(p, &rn_error);
1414 if (rn_error) {
1415 nasm_error(ERR_NONFATAL,
1416 "invalid size specified"
1417 " in COMMON declaration");
1418 break;
1419 }
1420 } else {
1421 nasm_error(ERR_NONFATAL,
1422 "no size specified in"
1423 " COMMON declaration");
1424 break;
1425 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001426
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001427 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001428 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001429 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001430 if (special)
1431 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 }
1433 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001434 }
1435 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001437 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 tokval.t_type = TOKEN_INVALID;
1439 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001440 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 if (e) {
1442 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001443 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 1 ? ERR_NONFATAL : ERR_PANIC,
1445 "cannot use non-relocatable expression as "
1446 "ABSOLUTE address");
1447 else {
1448 abs_seg = reloc_seg(e);
1449 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001450 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001451 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 abs_offset = 0x100; /* don't go near zero in case of / */
1453 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001454 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001456 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 location.segment = NO_SEG;
1458 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001459 case D_DEBUG: /* [DEBUG] */
1460 {
1461 char debugid[128];
1462 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001463
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 p = value;
1465 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001466 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001467 if (!isidstart(*p)) {
1468 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001469 } else {
1470 while (*p && !nasm_isspace(*p)) {
1471 if (q >= debugid + sizeof debugid - 1) {
1472 overlong = true;
1473 break;
1474 }
1475 if (!isidchar(*p))
1476 badid = true;
1477 *q++ = *p++;
1478 }
1479 *q = 0;
1480 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001481 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001482 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1483 "identifier expected after DEBUG");
1484 break;
1485 }
1486 if (overlong) {
1487 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1488 "DEBUG identifier too long");
1489 break;
1490 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001491 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001492 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001493 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001495 }
1496 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001497 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001498 switch(*value) {
1499 case '-': validid = 0; value++; break;
1500 case '+': validid = 1; value++; break;
1501 case '*': validid = 2; value++; break;
1502 default: validid = 1; break;
1503 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001505 for (i = 1; i <= ERR_WARN_MAX; i++)
1506 if (!nasm_stricmp(value, warnings[i].name))
1507 break;
1508 if (i <= ERR_WARN_MAX) {
1509 switch(validid) {
1510 case 0:
1511 warning_on[i] = false;
1512 break;
1513 case 1:
1514 warning_on[i] = true;
1515 break;
1516 case 2:
1517 warning_on[i] = warning_on_global[i];
1518 break;
1519 }
1520 } else
1521 nasm_error(ERR_NONFATAL,
1522 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001524 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 cpu = get_cpu(value);
1526 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001527 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001528 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 if (*value == '+') {
1530 user_nolist = 0;
1531 } else {
1532 if (*value == '-') {
1533 user_nolist = 1;
1534 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001535 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 }
1537 }
1538 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001539 case D_DEFAULT: /* [DEFAULT] */
1540 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001541 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001542 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001543 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001544 switch ((int)tokval.t_integer) {
1545 case S_REL:
1546 globalrel = 1;
1547 break;
1548 case S_ABS:
1549 globalrel = 0;
1550 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001551 case P_BND:
1552 globalbnd = 1;
1553 break;
1554 case P_NOBND:
1555 globalbnd = 0;
1556 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001557 default:
1558 err = 1;
1559 break;
1560 }
1561 } else {
1562 err = 1;
1563 }
1564 break;
1565 case D_FLOAT:
1566 if (float_option(value)) {
1567 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1568 "unknown 'float' directive: %s",
1569 value);
1570 }
1571 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001573 if (ofmt->directive(d, value, pass2))
1574 break;
1575 /* else fall through */
1576 case D_unknown:
1577 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1578 "unrecognised directive [%s]",
1579 directive);
1580 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001582 if (err) {
1583 nasm_error(ERR_NONFATAL,
1584 "invalid parameter to [%s] directive",
1585 directive);
1586 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001588 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589
Charles Crayne2581c862008-09-10 19:21:52 -07001590 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001594 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 forwref = saa_rstruct(forwrefs);
1596 } while (forwref != NULL
1597 && forwref->lineno == globallineno);
1598 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001599 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001600
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001601 if (output_ins.forw_ref) {
1602 if (passn == 1) {
1603 for (i = 0; i < output_ins.operands; i++) {
1604 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1605 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1606 fwinf->lineno = globallineno;
1607 fwinf->operand = i;
1608 }
1609 }
1610 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001611 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001613
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 /* forw_ref */
1615 if (output_ins.opcode == I_EQU) {
1616 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001617 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 * Special `..' EQUs get processed in pass two,
1619 * except `..@' macro-processor EQUs which are done
1620 * in the normal place.
1621 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001622 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001623 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001625
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001626 else if (output_ins.label[0] != '.' ||
1627 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 output_ins.label[2] == '@') {
1629 if (output_ins.operands == 1 &&
1630 (output_ins.oprs[0].type & IMMEDIATE) &&
1631 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001632 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 def_label(output_ins.label,
1634 output_ins.oprs[0].segment,
1635 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001636 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001638 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001640 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001642 && (output_ins.oprs[1].type & IMMEDIATE)
1643 && output_ins.oprs[1].segment == NO_SEG
1644 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001646 output_ins.oprs[0].offset | SEG_ABS,
1647 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001648 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001650 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001653 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 * Special `..' EQUs get processed here, except
1656 * `..@' macro processor EQUs which are done above.
1657 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 output_ins.label[1] == '.' &&
1660 output_ins.label[2] != '@') {
1661 if (output_ins.operands == 1 &&
1662 (output_ins.oprs[0].type & IMMEDIATE)) {
1663 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001664 output_ins.oprs[0].segment,
1665 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001666 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001668 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001670 && output_ins.oprs[0].segment == NO_SEG
1671 && (output_ins.oprs[1].type & IMMEDIATE)
1672 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001674 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001675 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001676 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001678 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001680 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001681 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001683
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001685
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001686 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001687 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001688
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 /* if (using_debug_info) && output_ins.opcode != -1) */
1690 if (using_debug_info)
1691 { /* fbk 03/25/01 */
1692 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001693 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 TYS_ELEMENTS(output_ins.operands);
1695 switch (output_ins.opcode) {
1696 case I_RESB:
1697 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001698 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 break;
1700 case I_RESW:
1701 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001702 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 break;
1704 case I_RESD:
1705 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001706 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 break;
1708 case I_RESQ:
1709 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001710 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 break;
1712 case I_REST:
1713 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001714 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001716 case I_RESO:
1717 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001718 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001719 break;
1720 case I_RESY:
1721 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001722 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001723 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 case I_DB:
1725 typeinfo |= TY_BYTE;
1726 break;
1727 case I_DW:
1728 typeinfo |= TY_WORD;
1729 break;
1730 case I_DD:
1731 if (output_ins.eops_float)
1732 typeinfo |= TY_FLOAT;
1733 else
1734 typeinfo |= TY_DWORD;
1735 break;
1736 case I_DQ:
1737 typeinfo |= TY_QWORD;
1738 break;
1739 case I_DT:
1740 typeinfo |= TY_TBYTE;
1741 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001742 case I_DO:
1743 typeinfo |= TY_OWORD;
1744 break;
1745 case I_DY:
1746 typeinfo |= TY_YWORD;
1747 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 default:
1749 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001750
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001752
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001753 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001754 }
1755 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001757 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001758 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001759 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 * else l == -1 => invalid instruction, which will be
1761 * flagged as an error on pass 2
1762 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001763
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001764 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001766 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001768 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 }
1771 } /* not an EQU */
1772 cleanup_insn(&output_ins);
1773 }
1774 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001775 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001777
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001778 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001779 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001780 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001781
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 if (pass1 == 1)
1783 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001784
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001785 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001787 } else if (global_offset_changed &&
1788 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001789 prev_offset_changed = global_offset_changed;
1790 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001791 } else {
1792 stall_count++;
1793 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001794
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001795 if (terminate_after_phase)
1796 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001797
1798 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001799 /* We get here if the labels don't converge
1800 * Example: FOO equ FOO + 1
1801 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001802 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001803 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001804 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001805 nasm_error(ERR_NONFATAL,
1806 "Possible causes: recursive EQUs, macro abuse.");
1807 break;
1808 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001809 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001810
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 preproc->cleanup(0);
1812 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001813 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001814 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001815 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1816 }
1817}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001818
H. Peter Anvin70055962007-10-11 00:05:31 -07001819static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001820{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001821 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001823 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001824
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001825 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001827 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001828 q = strchr(buf, ']');
1829 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001830 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001831
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001832 /* stip off the comments */
1833 p = strchr(buf, ';');
1834 if (p) {
1835 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001836 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001837 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001838 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001839
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001840 /* no brace, no trailing spaces */
1841 *q = '\0';
1842 nasm_zap_spaces_rev(--q);
1843
1844 /* directive */
1845 p = nasm_skip_spaces(++buf);
1846 q = nasm_skip_word(p);
1847 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001848 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001849 *q = '\0';
1850 *directive = p;
1851
1852 /* and value finally */
1853 p = nasm_skip_spaces(++q);
1854 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001855
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001856 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001857}
1858
Ed Berosetfa771012002-06-09 20:56:40 +00001859/**
1860 * gnu style error reporting
1861 * This function prints an error message to error_file in the
1862 * style used by GNU. An example would be:
1863 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001864 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001865 * which the error occurs (or is detected) and "error:" is one of
1866 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001867 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001868 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001869 *
1870 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001871 * @param fmt the printf style format string
1872 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001873static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001874{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001875 char *currentfile = NULL;
1876 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001877
Ed Berosetfa771012002-06-09 20:56:40 +00001878 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001880
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001881 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001883
1884 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001885 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1886 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001887 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001888 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001889 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001890
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001891 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001892}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001893
Ed Berosetfa771012002-06-09 20:56:40 +00001894/**
1895 * MS style error reporting
1896 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001897 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001898 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001899 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001900 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001901 * which the error occurs (or is detected) and "error:" is one of
1902 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001903 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001904 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001905 *
1906 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001907 * @param fmt the printf style format string
1908 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001909static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001910{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001911 char *currentfile = NULL;
1912 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001913
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 if (is_suppressed_warning(severity))
1915 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001916
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001917 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001919
1920 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001921 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001923 } else {
1924 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001925 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001926
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001927 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001928}
1929
1930/**
1931 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001932 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001933 * not in pass 1
1934 *
1935 * @param severity the severity of the warning or error
1936 * @return true if we should abort error/warning printing
1937 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001938static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001939{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001940 /* Not a warning at all */
1941 if ((severity & ERR_MASK) != ERR_WARNING)
1942 return false;
1943
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001944 /* See if it's a pass-one only warning and we're not in pass one. */
1945 if (((severity & ERR_PASS1) && pass0 != 1) ||
1946 ((severity & ERR_PASS2) && pass0 != 2))
1947 return true;
1948
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001949 /* Might be a warning but suppresed explicitly */
1950 if (severity & ERR_WARN_MASK)
1951 return !warning_on[WARN_IDX(severity)];
1952 else
1953 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001954}
1955
1956/**
1957 * common error reporting
1958 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001959 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001960 * specific error message to error_file and may or may not return. It
1961 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001962 *
1963 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001964 * @param fmt the printf style format string
1965 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001966static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001967{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001968 char msg[1024];
1969 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001970
H. Peter Anvin7df04172008-06-10 18:27:38 -07001971 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001973 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 break;
1975 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001976 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 break;
1978 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001979 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 break;
1981 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001982 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 break;
1984 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001985 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001987 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001988 pfx = "";
1989 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001990 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001991
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001992 vsnprintf(msg, sizeof msg, fmt, args);
1993
1994 fprintf(error_file, "%s%s\n", pfx, msg);
1995
1996 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001997 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001998
1999 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002000 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002001
2002 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 case ERR_DEBUG:
2004 /* no further action, by definition */
2005 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002006 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002007 /* Treat warnings as errors */
2008 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2009 terminate_after_phase = true;
2010 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002012 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 break;
2014 case ERR_FATAL:
2015 if (ofile) {
2016 fclose(ofile);
2017 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002018 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 }
2020 if (want_usage)
2021 usage();
2022 exit(1); /* instantly die */
2023 break; /* placate silly compilers */
2024 case ERR_PANIC:
2025 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002026 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 exit(3);
2028 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002029 }
2030}
2031
H. Peter Anvin734b1882002-04-30 21:01:08 +00002032static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002033{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002034 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002035}
2036
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002037static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002038{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002039 iflag_t r;
2040
2041 iflag_clear_all(&r);
2042
H. Peter Anvine2c80182005-01-15 22:15:51 +00002043 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002044 iflag_set(&r, IF_8086);
2045 else if (!strcmp(value, "186"))
2046 iflag_set(&r, IF_186);
2047 else if (!strcmp(value, "286"))
2048 iflag_set(&r, IF_286);
2049 else if (!strcmp(value, "386"))
2050 iflag_set(&r, IF_386);
2051 else if (!strcmp(value, "486"))
2052 iflag_set(&r, IF_486);
2053 else if (!strcmp(value, "586") ||
2054 !nasm_stricmp(value, "pentium"))
2055 iflag_set(&r, IF_PENT);
2056 else if (!strcmp(value, "686") ||
2057 !nasm_stricmp(value, "ppro") ||
2058 !nasm_stricmp(value, "pentiumpro") ||
2059 !nasm_stricmp(value, "p2"))
2060 iflag_set(&r, IF_P6);
2061 else if (!nasm_stricmp(value, "p3") ||
2062 !nasm_stricmp(value, "katmai"))
2063 iflag_set(&r, IF_KATMAI);
2064 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2065 !nasm_stricmp(value, "willamette"))
2066 iflag_set(&r, IF_WILLAMETTE);
2067 else if (!nasm_stricmp(value, "prescott"))
2068 iflag_set(&r, IF_PRESCOTT);
2069 else if (!nasm_stricmp(value, "x64") ||
2070 !nasm_stricmp(value, "x86-64"))
2071 iflag_set(&r, IF_X86_64);
2072 else if (!nasm_stricmp(value, "ia64") ||
2073 !nasm_stricmp(value, "ia-64") ||
2074 !nasm_stricmp(value, "itanium")||
2075 !nasm_stricmp(value, "itanic") ||
2076 !nasm_stricmp(value, "merced"))
2077 iflag_set(&r, IF_IA64);
2078 else {
2079 iflag_set(&r, IF_PLEVEL);
2080 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2081 "unknown 'cpu' type");
2082 }
2083 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002084}
2085
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002086static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002087{
2088 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002089
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 if ((i = atoi(value)) == 16)
2091 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002092 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002093 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002094 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 "cannot specify 32-bit segment on processor below a 386");
2096 i = 16;
2097 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002098 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002099 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002100 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002101 "cannot specify 64-bit segment on processor below an x86-64");
2102 i = 16;
2103 }
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01002104 if (i != maxbits && !allow_64_bit) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002105 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002106 "%s output format does not support 64-bit code",
2107 ofmt->shortname);
2108 i = 16;
2109 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002110 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002111 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002112 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 value);
2114 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002115 }
2116 return i;
2117}