blob: 42f1e89463c05d3244768211cbd0bee5a41d4e80 [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 Anvin215186f2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040082static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
83static void nasm_verror_vc(int severity, const char *fmt, va_list args);
84static void nasm_verror_common(int severity, const char *fmt, va_list args);
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;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000090int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080091int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000092
H. Peter Anvin9bd15062009-07-18 21:07:17 -040093static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080094
Keith Kaniosa6dfa782007-04-13 16:47:53 +000095static char inname[FILENAME_MAX];
96static char outname[FILENAME_MAX];
97static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -070098static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +000099static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000100/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400101struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400102struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400103const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000104
H. Peter Anvine2c80182005-01-15 22:15:51 +0000105static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000106
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400107FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700108int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
109static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400110
111static iflag_t cpu;
112static iflag_t cmd_cpu;
113
Charles Craynec1905c22008-09-11 18:54:06 -0700114int64_t global_offset_changed; /* referenced in labels.c */
115int64_t prev_offset_changed;
116int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000117
H. Peter Anvin12e46512007-10-03 21:30:57 -0700118static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000120int32_t abs_seg; /* ABSOLUTE segment basis */
121int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000122
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000123static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000124
H. Peter Anvine2c80182005-01-15 22:15:51 +0000125static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700126static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000127
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400128static struct preproc_ops *preproc;
129
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400130#define OP_NORMAL (1u << 0)
131#define OP_PREPROCESS (1u << 1)
132#define OP_DEPEND (1u << 2)
133
134static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400135
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700136/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700137static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700138static bool depend_missing_ok = false;
139static const char *depend_target = NULL;
140static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000142/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000143 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800144 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000145 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200146
H. Peter Anvin972079f2008-09-30 17:01:23 -0700147static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
148static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000149
H. Peter Anvin972079f2008-09-30 17:01:23 -0700150static const struct warning {
151 const char *name;
152 const char *help;
153 bool enabled;
154} warnings[ERR_WARN_MAX+1] = {
155 {"error", "treat warnings as errors", false},
156 {"macro-params", "macro calls with wrong parameter count", true},
157 {"macro-selfref", "cyclic macro references", false},
158 {"macro-defaults", "macros with more default than optional parameters", true},
159 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700160 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700161 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
162 {"float-overflow", "floating point overflow", true},
163 {"float-denorm", "floating point denormal", false},
164 {"float-underflow", "floating point underflow", false},
165 {"float-toolong", "too many digits in floating-point number", true},
166 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800167 {"lock", "lock prefix on unlockable instructions", true},
168 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800169 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800170 {"zext-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000171};
172
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700173static bool want_usage;
174static bool terminate_after_phase;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800175bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176
H. Peter Anvin55340992012-09-09 17:09:00 -0700177static char *quote_for_make(const char *str);
178
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400179static int64_t get_curr_offs(void)
180{
181 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
182}
183
184static void set_curr_offs(int64_t l_off)
185{
186 if (in_abs_seg)
187 abs_offset = l_off;
188 else
189 offsets = raa_write(offsets, location.segment, l_off);
190}
191
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000192static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000194 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800196 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000199}
200
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800201/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800202static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800203{
204 int64_t t;
205 int64_t y = tm->tm_year;
206
207 /* See IEEE 1003.1:2004, section 4.14 */
208
209 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
210 t += tm->tm_yday;
211 t *= 24;
212 t += tm->tm_hour;
213 t *= 60;
214 t += tm->tm_min;
215 t *= 60;
216 t += tm->tm_sec;
217
218 return t;
219}
220
221static void define_macros_early(void)
222{
223 char temp[128];
224 struct tm lt, *lt_p, gm, *gm_p;
225 int64_t posix_time;
226
227 lt_p = localtime(&official_compile_time);
228 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400229 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800230
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400231 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
232 preproc->pre_define(temp);
233 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
234 preproc->pre_define(temp);
235 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
238 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800239 }
240
241 gm_p = gmtime(&official_compile_time);
242 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400243 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800244
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400245 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
246 preproc->pre_define(temp);
247 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
248 preproc->pre_define(temp);
249 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
250 preproc->pre_define(temp);
251 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
252 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800253 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700254
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800255 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800256 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800258 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400260 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261
262 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400263 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
264 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265 }
266}
267
268static void define_macros_late(void)
269{
270 char temp[128];
271
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400272 /*
273 * In case if output format is defined by alias
274 * we have to put shortname of the alias itself here
275 * otherwise ABI backward compatibility gets broken.
276 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400277 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400278 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400279 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280}
281
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700282static void emit_dependencies(StrList *list)
283{
284 FILE *deps;
285 int linepos, len;
286 StrList *l, *nl;
287
288 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400289 deps = fopen(depend_file, "w");
290 if (!deps) {
291 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
292 "unable to write dependency file `%s'", depend_file);
293 return;
294 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700295 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400296 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700297 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700298
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700299 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400300 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700301 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400302 len = strlen(file);
303 if (linepos + len > 62 && linepos > 1) {
304 fprintf(deps, " \\\n ");
305 linepos = 1;
306 }
307 fprintf(deps, " %s", file);
308 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700309 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700310 }
311 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700312
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400313 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400314 if (depend_emit_phony)
315 fprintf(deps, "%s:\n\n", l->str);
316 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700317 }
318
319 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400320 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321}
322
H. Peter Anvin038d8612007-04-12 16:54:50 +0000323int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000324{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700326
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800327 time(&official_compile_time);
328
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400329 iflag_set(&cpu, IF_PLEVEL);
330 iflag_set(&cmd_cpu, IF_PLEVEL);
331
Charles Crayne2581c862008-09-10 19:21:52 -0700332 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700333 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400334 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000335
H. Peter Anvin97e15752007-09-25 08:47:47 -0700336 error_file = stderr;
337
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700338 tolower_init();
339
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000340 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000341 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000342
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400344 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000345
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800346 /* Define some macros dependent on the runtime, but not
347 on the command line. */
348 define_macros_early();
349
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000350 parse_cmdline(argc, argv);
351
H. Peter Anvine2c80182005-01-15 22:15:51 +0000352 if (terminate_after_phase) {
353 if (want_usage)
354 usage();
355 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356 }
357
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000358 /* If debugging info is disabled, suppress any debug calls */
359 if (!using_debug_info)
360 ofmt->current_dfmt = &null_debug_form;
361
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400363 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700364 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000366
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000367 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800368 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000369
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400370 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700371 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400372 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700373
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400374 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000375 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700376
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400377 if (depend_missing_ok)
378 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700379
H. Peter Anvin215186f2016-02-17 20:27:41 -0800380 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400382 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 while ((line = preproc->getline()))
385 nasm_free(line);
386 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400387 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000388 char *line;
389 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000390 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000392
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 if (*outname) {
394 ofile = fopen(outname, "w");
395 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800396 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 "unable to open output file `%s'",
398 outname);
399 } else
400 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000401
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700402 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000403
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400404 /* pass = 1; */
H. Peter Anvin215186f2016-02-17 20:27:41 -0800405 preproc->reset(inname, 3, depend_ptr);
406 memcpy(warning_on, warning_on_global,
407 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700408
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 while ((line = preproc->getline())) {
410 /*
411 * We generate %line directives if needed for later programs
412 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000413 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 int altline = src_get(&linnum, &file_name);
415 if (altline) {
416 if (altline == 1 && lineinc == 1)
417 nasm_fputs("", ofile);
418 else {
419 lineinc = (altline != -1 || lineinc != 1);
420 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000421 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 file_name);
423 }
424 prior_linnum = linnum;
425 }
426 nasm_fputs(line, ofile);
427 nasm_free(line);
428 }
429 nasm_free(file_name);
430 preproc->cleanup(0);
431 if (ofile)
432 fclose(ofile);
433 if (ofile && terminate_after_phase)
434 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400435 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400436 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400438 if (operating_mode & OP_NORMAL) {
439 /*
440 * We must call ofmt->filename _anyway_, even if the user
441 * has specified their own output file, because some
442 * formats (eg OBJ and COFF) use ofmt->filename to find out
443 * the name of the input file and then put that inside the
444 * file.
445 */
446 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400448 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
449 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800450 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400451 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400453 /*
454 * We must call init_labels() before ofmt->init() since
455 * some object formats will want to define labels in their
456 * init routines. (eg OS/2 defines the FLAT group)
457 */
458 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400460 ofmt->init();
461 dfmt = ofmt->current_dfmt;
462 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400464 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200465
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 if (!terminate_after_phase) {
467 ofmt->cleanup(using_debug_info);
468 cleanup_labels();
469 fflush(ofile);
470 if (ferror(ofile))
471 nasm_error(ERR_NONFATAL|ERR_NOFILE,
472 "write error on output file `%s'", outname);
473 }
474
475 if (ofile) {
476 fclose(ofile);
477 if (terminate_after_phase)
478 remove(outname);
479 ofile = NULL;
480 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000481 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000482
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700483 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400484 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700485
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000486 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000488
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 raa_free(offsets);
490 saa_free(forwrefs);
491 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000492 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700494 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000495}
496
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497/*
498 * Get a parameter for a command line option.
499 * First arg must be in the form of e.g. -f...
500 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800501static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000502{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800503 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400504 if (p[2]) /* the parameter's in the option */
505 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800507 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000509 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400510 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000512 return NULL;
513}
514
H. Peter Anvindc242712007-11-18 11:55:10 -0800515/*
516 * Copy a filename
517 */
518static void copy_filename(char *dst, const char *src)
519{
520 size_t len = strlen(src);
521
522 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800523 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400524 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800525 }
526 strncpy(dst, src, FILENAME_MAX);
527}
528
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700529/*
530 * Convert a string to Make-safe form
531 */
532static char *quote_for_make(const char *str)
533{
534 const char *p;
535 char *os, *q;
536
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400537 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700538 size_t nbs = 0;
539
540 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400541 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700542
543 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400544 switch (*p) {
545 case ' ':
546 case '\t':
547 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
548 n += nbs + 2;
549 nbs = 0;
550 break;
551 case '$':
552 case '#':
553 nbs = 0;
554 n += 2;
555 break;
556 case '\\':
557 nbs++;
558 n++;
559 break;
560 default:
561 nbs = 0;
562 n++;
563 break;
564 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700565 }
566
567 /* Convert N backslashes at the end of filename to 2N backslashes */
568 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400569 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700570
571 os = q = nasm_malloc(n);
572
573 nbs = 0;
574 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400575 switch (*p) {
576 case ' ':
577 case '\t':
578 while (nbs--)
579 *q++ = '\\';
580 *q++ = '\\';
581 *q++ = *p;
582 break;
583 case '$':
584 *q++ = *p;
585 *q++ = *p;
586 nbs = 0;
587 break;
588 case '#':
589 *q++ = '\\';
590 *q++ = *p;
591 nbs = 0;
592 break;
593 case '\\':
594 *q++ = *p;
595 nbs++;
596 break;
597 default:
598 *q++ = *p;
599 nbs = 0;
600 break;
601 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700602 }
603 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400604 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700605
606 *q = '\0';
607
608 return os;
609}
610
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000612 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000614};
615
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100616enum text_options {
617 OPT_PREFIX,
H. Peter Anvin53f15592016-03-01 22:43:51 -0800618 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100619};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620struct textargs textopts[] = {
621 {"prefix", OPT_PREFIX},
622 {"postfix", OPT_POSTFIX},
623 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000624};
625
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400626static void show_version(void)
627{
628 printf("NASM version %s compiled on %s%s\n",
629 nasm_version, nasm_date, nasm_compile_options);
630 exit(0);
631}
632
H. Peter Anvin423e3812007-11-15 17:12:29 -0800633static bool stopoptions = false;
634static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000635{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000636 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800637 int i;
638 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700639 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000640
641 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800642 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000643
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400645 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
646 /* These parameters take values */
647 if (!(param = get_param(p, q, &advance)))
648 return advance;
649 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800650
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 switch (p[1]) {
652 case 's':
653 error_file = stdout;
654 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700655
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400656 case 'o': /* output file */
657 copy_filename(outname, param);
658 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700659
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400660 case 'f': /* output format */
661 ofmt = ofmt_find(param, &ofmt_alias);
662 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800663 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400664 "unrecognised output format `%s' - "
665 "use -hf for a list", param);
666 }
667 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700668
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400669 case 'O': /* Optimization level */
670 {
671 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700672
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400673 if (!*param) {
674 /* Naked -O == -Ox */
675 optimizing = MAX_OPTIMIZE;
676 } else {
677 while (*param) {
678 switch (*param) {
679 case '0': case '1': case '2': case '3': case '4':
680 case '5': case '6': case '7': case '8': case '9':
681 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700682
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400683 /* -O0 -> optimizing == -1, 0.98 behaviour */
684 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
685 if (opt < 2)
686 optimizing = opt - 1;
687 else
688 optimizing = opt;
689 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700690
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400691 case 'v':
692 case '+':
693 param++;
694 opt_verbose_info = true;
695 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700696
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400697 case 'x':
698 param++;
699 optimizing = MAX_OPTIMIZE;
700 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700701
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400702 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800703 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400704 "unknown optimization option -O%c\n",
705 *param);
706 break;
707 }
708 }
709 if (optimizing > MAX_OPTIMIZE)
710 optimizing = MAX_OPTIMIZE;
711 }
712 break;
713 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800714
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400715 case 'p': /* pre-include */
716 case 'P':
717 preproc->pre_include(param);
718 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800719
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400720 case 'd': /* pre-define */
721 case 'D':
722 preproc->pre_define(param);
723 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800724
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400725 case 'u': /* un-define */
726 case 'U':
727 preproc->pre_undefine(param);
728 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800729
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400730 case 'i': /* include search path */
731 case 'I':
732 preproc->include_path(param);
733 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800734
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400735 case 'l': /* listing file */
736 copy_filename(listname, param);
737 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800738
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 case 'Z': /* error messages file */
740 copy_filename(errname, param);
741 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800742
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400743 case 'F': /* specify debug format */
744 ofmt->current_dfmt = dfmt_find(ofmt, param);
745 if (!ofmt->current_dfmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800746 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400747 "unrecognized debug format `%s' for"
748 " output format `%s'",
749 param, ofmt->shortname);
750 }
751 using_debug_info = true;
752 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800753
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400754 case 'X': /* specify error reporting format */
755 if (nasm_stricmp("vc", param) == 0)
756 nasm_set_verror(nasm_verror_vc);
757 else if (nasm_stricmp("gnu", param) == 0)
758 nasm_set_verror(nasm_verror_gnu);
759 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800760 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400761 "unrecognized error reporting format `%s'",
762 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800764
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700766 using_debug_info = true;
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 'h':
770 printf
771 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
772 "[-l listfile]\n"
773 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400774 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400776 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700778 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700780 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400781 " -MG d:o, missing files assumed generated\n"
782 " -MF <file> set Makefile dependency file\n"
783 " -MD <file> assemble and generate dependencies\n"
784 " -MT <file> dependency target name\n"
785 " -MQ <file> dependency target name (quoted)\n"
786 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700787 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 " -s redirect error messages to stdout\n\n"
789 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400790 " -o outfile write output to an outfile\n\n"
791 " -f format select an output format\n\n"
792 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 " -I<path> adds a pathname to the include file path\n");
794 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700795 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400796 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400797 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400798 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 " -P<file> pre-includes a file\n"
800 " -D<macro>[=<value>] pre-defines a macro\n"
801 " -U<macro> undefines a macro\n"
802 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800803 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400804 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400805 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400806 "--prefix,--postfix\n"
807 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100808 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800809 "Warnings:\n");
810 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700812 warnings[i].name, warnings[i].help,
813 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 printf
815 ("\nresponse files should contain command line parameters"
816 ", one per line.\n");
817 if (p[2] == 'f') {
818 printf("\nvalid output formats for -f are"
819 " (`*' denotes default):\n");
820 ofmt_list(ofmt, stdout);
821 } else {
822 printf("\nFor a list of valid output formats, use -hf.\n");
823 printf("For a list of debug formats, use -f <form> -y.\n");
824 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400825 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800827
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 case 'y':
829 printf("\nvalid debug formats for '%s' output format are"
830 " ('*' denotes default):\n", ofmt->shortname);
831 dfmt_list(ofmt, stdout);
832 exit(0);
833 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800834
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700836 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400840 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800842
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400843 case 'e': /* preprocess only */
844 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400845 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800847
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400848 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400849 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 case 'W':
853 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
854 do_warn = false;
855 param += 3;
856 } else {
857 do_warn = true;
858 }
859 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800860
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800862 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400863 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400865 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400867 do_warn = (param[0] == '+');
868 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400869
870set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400871 for (i = 0; i <= ERR_WARN_MAX; i++) {
872 if (!nasm_stricmp(param, warnings[i].name))
873 break;
874 }
875 if (i <= ERR_WARN_MAX) {
876 warning_on_global[i] = do_warn;
877 } else if (!nasm_stricmp(param, "all")) {
878 for (i = 1; i <= ERR_WARN_MAX; i++)
879 warning_on_global[i] = do_warn;
880 } else if (!nasm_stricmp(param, "none")) {
881 for (i = 1; i <= ERR_WARN_MAX; i++)
882 warning_on_global[i] = !do_warn;
883 } else {
884 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
885 "invalid warning `%s'", param);
886 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800888
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400890 switch (p[2]) {
891 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400892 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400893 break;
894 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400895 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400896 depend_missing_ok = true;
897 break;
898 case 'P':
899 depend_emit_phony = true;
900 break;
901 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300902 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400903 depend_file = q;
904 advance = true;
905 break;
906 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400907 depend_file = q;
908 advance = true;
909 break;
910 case 'T':
911 depend_target = q;
912 advance = true;
913 break;
914 case 'Q':
915 depend_target = quote_for_make(q);
916 advance = true;
917 break;
918 default:
919 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
920 "unknown dependency option `-M%c'", p[2]);
921 break;
922 }
923 if (advance && (!q || !q[0])) {
924 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
925 "option `-M%c' requires a parameter", p[2]);
926 break;
927 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000929
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 case '-':
931 {
932 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000933
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 if (p[2] == 0) { /* -- => stop processing options */
935 stopoptions = 1;
936 break;
937 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400938
939 if (!nasm_stricmp(p, "--v"))
940 show_version();
941
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 for (s = 0; textopts[s].label; s++) {
943 if (!nasm_stricmp(p + 2, textopts[s].label)) {
944 break;
945 }
946 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000947
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 switch (s) {
949
950 case OPT_PREFIX:
951 case OPT_POSTFIX:
952 {
953 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400954 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 ERR_USAGE,
956 "option `--%s' requires an argument",
957 p + 2);
958 break;
959 } else {
960 advance = 1, param = q;
961 }
962
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100963 switch (s) {
964 case OPT_PREFIX:
965 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100967 case OPT_POSTFIX:
968 strlcpy(lpostfix, param, POSTFIX_MAX);
969 break;
970 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800971 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100972 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 break;
974 }
975 break;
976 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 default:
979 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400980 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 "unrecognised option `--%s'", p + 2);
982 break;
983 }
984 }
985 break;
986 }
987
988 default:
989 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400990 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 "unrecognised option `-%c'", p[1]);
992 break;
993 }
994 } else {
995 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400996 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800998 } else {
999 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001000 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001001 }
1002
1003 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001004}
1005
H. Peter Anvineba20a72002-04-30 20:53:55 +00001006#define ARG_BUF_DELTA 128
1007
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001009{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001010 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001011 int bufsize, prevargsize;
1012
1013 bufsize = prevargsize = ARG_BUF_DELTA;
1014 buffer = nasm_malloc(ARG_BUF_DELTA);
1015 prevarg = nasm_malloc(ARG_BUF_DELTA);
1016 prevarg[0] = '\0';
1017
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 p = buffer;
1020 while (1) { /* Loop to handle long lines */
1021 q = fgets(p, bufsize - (p - buffer), rfile);
1022 if (!q)
1023 break;
1024 p += strlen(p);
1025 if (p > buffer && p[-1] == '\n')
1026 break;
1027 if (p - buffer > bufsize - 10) {
1028 int offset;
1029 offset = p - buffer;
1030 bufsize += ARG_BUF_DELTA;
1031 buffer = nasm_realloc(buffer, bufsize);
1032 p = buffer + offset;
1033 }
1034 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001035
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 if (!q && p == buffer) {
1037 if (prevarg[0])
1038 process_arg(prevarg, NULL);
1039 nasm_free(buffer);
1040 nasm_free(prevarg);
1041 return;
1042 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001043
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 /*
1045 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1046 * them are present at the end of the line.
1047 */
1048 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001049
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001050 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001053 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 if (process_arg(prevarg, p))
1056 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001057
Charles Crayne192d5b52007-10-18 19:02:42 -07001058 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 prevargsize += ARG_BUF_DELTA;
1060 prevarg = nasm_realloc(prevarg, prevargsize);
1061 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001062 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063 }
1064}
1065
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001066/* Function to process args from a string of args, rather than the
1067 * argv array. Used by the environment variable and response file
1068 * processing.
1069 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001070static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001072 char *p, *q, *arg, *prevarg;
1073 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001074
1075 p = args;
1076 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001078 arg = NULL;
1079 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 q = p;
1081 while (*p && *p != separator)
1082 p++;
1083 while (*p == separator)
1084 *p++ = '\0';
1085 prevarg = arg;
1086 arg = q;
1087 if (process_arg(prevarg, arg))
1088 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001089 }
1090 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001092}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001093
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001094static void process_response_file(const char *file)
1095{
1096 char str[2048];
1097 FILE *f = fopen(file, "r");
1098 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001099 perror(file);
1100 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001101 }
1102 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001103 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001104 }
1105 fclose(f);
1106}
1107
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001108static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001109{
1110 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001111 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001112 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001113
Charles Craynefcce07f2007-09-30 22:15:36 -07001114 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001115
H. Peter Anvin972079f2008-09-30 17:01:23 -07001116 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001117 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001118
1119 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001120 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001121 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001122 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001123 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 envcopy = nasm_strdup(envreal);
1125 process_args(envcopy);
1126 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001127 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001128
1129 /*
1130 * Now process the actual command line.
1131 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001133 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 argv++;
1135 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001136 /*
1137 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 * arguments like the environment variable. This allows us
1139 * to have multiple arguments on a single line, which is
1140 * different to the -@resp file processing below for regular
1141 * NASM.
1142 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001143 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 argc--;
1145 argv++;
1146 }
1147 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001148 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001149 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001150 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001151 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 process_respfile(rfile);
1153 fclose(rfile);
1154 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001155 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 "unable to open response file `%s'", p);
1157 }
1158 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001159 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1160 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001161 }
1162
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001163 /*
1164 * Look for basic command line typos. This definitely doesn't
1165 * catch all errors, but it might help cases of fumbled fingers.
1166 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001167 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001168 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001169 "no input file specified");
1170 else if (!strcmp(inname, errname) ||
1171 !strcmp(inname, outname) ||
1172 !strcmp(inname, listname) ||
1173 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001174 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001175 "file `%s' is both input and output file",
1176 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001177
H. Peter Anvin70653092007-10-19 14:42:29 -07001178 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001179 error_file = fopen(errname, "w");
1180 if (!error_file) {
1181 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001182 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001183 "cannot open file `%s' for error messages",
1184 errname);
1185 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001186 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001187}
1188
H. Peter Anvin70055962007-10-11 00:05:31 -07001189static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001190
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001191static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001192{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001193 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001195 int i, validid;
1196 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001197 int32_t seg;
1198 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001199 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001201 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001202
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001203 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001204 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001205
Charles Craynec1905c22008-09-11 18:54:06 -07001206 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001207 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 int pass1, pass2;
1209 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001210
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001211 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001212 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1213 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001214
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001215 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216
Keith Kaniosb7a89542007-04-12 02:40:54 +00001217 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 cpu = cmd_cpu;
1219 if (pass0 == 2) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001220 nasmlist->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001222 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001223 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001225 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001226 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 saa_rewind(forwrefs);
1228 forwref = saa_rstruct(forwrefs);
1229 raa_free(offsets);
1230 offsets = raa_init();
1231 }
H. Peter Anvin215186f2016-02-17 20:27:41 -08001232 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001233 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001234
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001236 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001237 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001238 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001239
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001241 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001244 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001245 * Here we parse our directives; this is not handled by the
1246 * 'real' parser. This really should be a separate function.
1247 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001249 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001250 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001251 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001252
H. Peter Anvin70055962007-10-11 00:05:31 -07001253 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001254 case D_SEGMENT: /* [SEGMENT n] */
1255 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 seg = ofmt->section(value, pass2, &sb);
1257 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001258 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001259 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 value);
1261 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001262 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 location.segment = seg;
1264 }
1265 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001266 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001267 if (*value) {
1268 stdscan_reset();
1269 stdscan_set(value);
1270 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08001271 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001272 if (e) {
1273 unsigned int align = (unsigned int)e->value;
1274 if ((uint64_t)e->value > 0x7fffffff) {
1275 /*
1276 * FIXME: Please make some sane message here
1277 * ofmt should have some 'check' method which
1278 * would report segment alignment bounds.
1279 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001280 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001281 "incorrect segment alignment `%s'", value);
1282 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001283 nasm_error(ERR_NONFATAL,
1284 "segment alignment `%s' is not power of two",
1285 value);
1286 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001287
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001288 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001289 if (location.segment != NO_SEG)
1290 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001291 }
1292 }
1293 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001294 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 if (*value == '$')
1296 value++; /* skip initial $ if present */
1297 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001298 q = value;
1299 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001301 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 *q++ = '\0';
1303 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001305 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001306 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001307 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001309 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001312 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 }
1315 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001316 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 "identifier expected after EXTERN");
1318 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319 }
1320 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 *q++ = '\0';
1322 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 special = NULL;
1325 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1326 int temp = pass0;
1327 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001328 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001330 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001332 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 } /* else pass0 == 1 */
1334 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001335 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001336 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001338 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 if (*value == '$')
1340 value++; /* skip initial $ if present */
1341 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342 q = value;
1343 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 *q++ = '\0';
1347 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001348 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001350 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001351 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001353 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001356 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 }
1359 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001360 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 "identifier expected after GLOBAL");
1362 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363 }
1364 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 *q++ = '\0';
1366 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001369 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 } /* pass == 1 */
1371 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001372 case D_COMMON: /* [COMMON symbol size:special] */
1373 {
1374 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001375
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 if (*value == '$')
1377 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001378 p = value;
1379 validid = true;
1380 if (!isidstart(*p))
1381 validid = false;
1382 while (*p && !nasm_isspace(*p)) {
1383 if (!isidchar(*p))
1384 validid = false;
1385 p++;
1386 }
1387 if (!validid) {
1388 nasm_error(ERR_NONFATAL,
1389 "identifier expected after COMMON");
1390 break;
1391 }
1392 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001393 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001394 q = p;
1395 while (*q && *q != ':')
1396 q++;
1397 if (*q == ':') {
1398 *q++ = '\0';
1399 special = q;
1400 } else {
1401 special = NULL;
1402 }
1403 size = readnum(p, &rn_error);
1404 if (rn_error) {
1405 nasm_error(ERR_NONFATAL,
1406 "invalid size specified"
1407 " in COMMON declaration");
1408 break;
1409 }
1410 } else {
1411 nasm_error(ERR_NONFATAL,
1412 "no size specified in"
1413 " COMMON declaration");
1414 break;
1415 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001416
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001417 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001418 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001419 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001420 if (special)
1421 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 }
1423 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001424 }
1425 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001427 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08001429 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 if (e) {
1431 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001432 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 1 ? ERR_NONFATAL : ERR_PANIC,
1434 "cannot use non-relocatable expression as "
1435 "ABSOLUTE address");
1436 else {
1437 abs_seg = reloc_seg(e);
1438 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001440 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 abs_offset = 0x100; /* don't go near zero in case of / */
1442 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001443 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001445 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 location.segment = NO_SEG;
1447 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001448 case D_DEBUG: /* [DEBUG] */
1449 {
1450 char debugid[128];
1451 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001452
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 p = value;
1454 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001455 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001456 if (!isidstart(*p)) {
1457 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001458 } else {
1459 while (*p && !nasm_isspace(*p)) {
1460 if (q >= debugid + sizeof debugid - 1) {
1461 overlong = true;
1462 break;
1463 }
1464 if (!isidchar(*p))
1465 badid = true;
1466 *q++ = *p++;
1467 }
1468 *q = 0;
1469 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001470 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001471 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1472 "identifier expected after DEBUG");
1473 break;
1474 }
1475 if (overlong) {
1476 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1477 "DEBUG identifier too long");
1478 break;
1479 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001480 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001481 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001482 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001484 }
1485 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001486 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001487 switch(*value) {
1488 case '-': validid = 0; value++; break;
1489 case '+': validid = 1; value++; break;
1490 case '*': validid = 2; value++; break;
1491 default: validid = 1; break;
1492 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001494 for (i = 1; i <= ERR_WARN_MAX; i++)
1495 if (!nasm_stricmp(value, warnings[i].name))
1496 break;
1497 if (i <= ERR_WARN_MAX) {
1498 switch(validid) {
1499 case 0:
1500 warning_on[i] = false;
1501 break;
1502 case 1:
1503 warning_on[i] = true;
1504 break;
1505 case 2:
1506 warning_on[i] = warning_on_global[i];
1507 break;
1508 }
1509 } else
1510 nasm_error(ERR_NONFATAL,
1511 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001513 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 cpu = get_cpu(value);
1515 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001516 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001517 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 if (*value == '+') {
1519 user_nolist = 0;
1520 } else {
1521 if (*value == '-') {
1522 user_nolist = 1;
1523 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001524 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 }
1526 }
1527 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001528 case D_DEFAULT: /* [DEFAULT] */
1529 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001530 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001531 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001532 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001533 switch ((int)tokval.t_integer) {
1534 case S_REL:
1535 globalrel = 1;
1536 break;
1537 case S_ABS:
1538 globalrel = 0;
1539 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001540 case P_BND:
1541 globalbnd = 1;
1542 break;
1543 case P_NOBND:
1544 globalbnd = 0;
1545 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001546 default:
1547 err = 1;
1548 break;
1549 }
1550 } else {
1551 err = 1;
1552 }
1553 break;
1554 case D_FLOAT:
1555 if (float_option(value)) {
1556 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1557 "unknown 'float' directive: %s",
1558 value);
1559 }
1560 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001562 if (ofmt->directive(d, value, pass2))
1563 break;
1564 /* else fall through */
1565 case D_unknown:
1566 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1567 "unrecognised directive [%s]",
1568 directive);
1569 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001571 if (err) {
1572 nasm_error(ERR_NONFATAL,
1573 "invalid parameter to [%s] directive",
1574 directive);
1575 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001577 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578
Charles Crayne2581c862008-09-10 19:21:52 -07001579 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001581 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001583 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 forwref = saa_rstruct(forwrefs);
1585 } while (forwref != NULL
1586 && forwref->lineno == globallineno);
1587 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001588 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001589
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001590 if (output_ins.forw_ref) {
1591 if (passn == 1) {
1592 for (i = 0; i < output_ins.operands; i++) {
1593 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1594 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1595 fwinf->lineno = globallineno;
1596 fwinf->operand = i;
1597 }
1598 }
1599 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001600 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001602
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 /* forw_ref */
1604 if (output_ins.opcode == I_EQU) {
1605 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001606 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 * Special `..' EQUs get processed in pass two,
1608 * except `..@' macro-processor EQUs which are done
1609 * in the normal place.
1610 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001611 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001612 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001614
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001615 else if (output_ins.label[0] != '.' ||
1616 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 output_ins.label[2] == '@') {
1618 if (output_ins.operands == 1 &&
1619 (output_ins.oprs[0].type & IMMEDIATE) &&
1620 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001621 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 def_label(output_ins.label,
1623 output_ins.oprs[0].segment,
1624 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001625 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001627 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001629 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001631 && (output_ins.oprs[1].type & IMMEDIATE)
1632 && output_ins.oprs[1].segment == NO_SEG
1633 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001635 output_ins.oprs[0].offset | SEG_ABS,
1636 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001637 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001639 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001641 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001642 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001643 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 * Special `..' EQUs get processed here, except
1645 * `..@' macro processor EQUs which are done above.
1646 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 output_ins.label[1] == '.' &&
1649 output_ins.label[2] != '@') {
1650 if (output_ins.operands == 1 &&
1651 (output_ins.oprs[0].type & IMMEDIATE)) {
1652 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653 output_ins.oprs[0].segment,
1654 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001655 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001657 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001659 && output_ins.oprs[0].segment == NO_SEG
1660 && (output_ins.oprs[1].type & IMMEDIATE)
1661 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001663 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001664 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001665 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001667 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001669 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001670 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001672
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001674
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001675 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001676 &output_ins);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001677
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 /* if (using_debug_info) && output_ins.opcode != -1) */
1679 if (using_debug_info)
1680 { /* fbk 03/25/01 */
1681 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001682 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 TYS_ELEMENTS(output_ins.operands);
1684 switch (output_ins.opcode) {
1685 case I_RESB:
1686 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001687 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 break;
1689 case I_RESW:
1690 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001691 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 break;
1693 case I_RESD:
1694 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001695 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 break;
1697 case I_RESQ:
1698 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001699 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 break;
1701 case I_REST:
1702 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001703 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001705 case I_RESO:
1706 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001707 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001708 break;
1709 case I_RESY:
1710 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001711 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001712 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 case I_DB:
1714 typeinfo |= TY_BYTE;
1715 break;
1716 case I_DW:
1717 typeinfo |= TY_WORD;
1718 break;
1719 case I_DD:
1720 if (output_ins.eops_float)
1721 typeinfo |= TY_FLOAT;
1722 else
1723 typeinfo |= TY_DWORD;
1724 break;
1725 case I_DQ:
1726 typeinfo |= TY_QWORD;
1727 break;
1728 case I_DT:
1729 typeinfo |= TY_TBYTE;
1730 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001731 case I_DO:
1732 typeinfo |= TY_OWORD;
1733 break;
1734 case I_DY:
1735 typeinfo |= TY_YWORD;
1736 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 default:
1738 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001739
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001741
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001742 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001743 }
1744 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001746 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001747 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001748 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 * else l == -1 => invalid instruction, which will be
1750 * flagged as an error on pass 2
1751 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001752
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001753 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001755 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001756 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 }
1759 } /* not an EQU */
1760 cleanup_insn(&output_ins);
1761 }
1762 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001763 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001765
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001766 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001767 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001768 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 if (pass1 == 1)
1771 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001772
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001773 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001775 } else if (global_offset_changed &&
1776 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001777 prev_offset_changed = global_offset_changed;
1778 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001779 } else {
1780 stall_count++;
1781 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001782
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001783 if (terminate_after_phase)
1784 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001785
1786 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001787 /* We get here if the labels don't converge
1788 * Example: FOO equ FOO + 1
1789 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001790 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001791 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001792 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001793 nasm_error(ERR_NONFATAL,
1794 "Possible causes: recursive EQUs, macro abuse.");
1795 break;
1796 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001797 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001798
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 preproc->cleanup(0);
H. Peter Anvin215186f2016-02-17 20:27:41 -08001800 nasmlist->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001801 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001802 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001803 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1804 }
1805}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001806
H. Peter Anvin70055962007-10-11 00:05:31 -07001807static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001808{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001809 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001811 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001812
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001813 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001815 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001816 q = strchr(buf, ']');
1817 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001818 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001819
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001820 /* stip off the comments */
1821 p = strchr(buf, ';');
1822 if (p) {
1823 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001824 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001825 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001826 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001827
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001828 /* no brace, no trailing spaces */
1829 *q = '\0';
1830 nasm_zap_spaces_rev(--q);
1831
1832 /* directive */
1833 p = nasm_skip_spaces(++buf);
1834 q = nasm_skip_word(p);
1835 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001836 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001837 *q = '\0';
1838 *directive = p;
1839
1840 /* and value finally */
1841 p = nasm_skip_spaces(++q);
1842 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001843
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001844 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001845}
1846
Ed Berosetfa771012002-06-09 20:56:40 +00001847/**
1848 * gnu style error reporting
1849 * This function prints an error message to error_file in the
1850 * style used by GNU. An example would be:
1851 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001852 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001853 * which the error occurs (or is detected) and "error:" is one of
1854 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001855 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001856 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001857 *
1858 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001859 * @param fmt the printf style format string
1860 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001861static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001862{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001863 char *currentfile = NULL;
1864 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001865
Ed Berosetfa771012002-06-09 20:56:40 +00001866 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001868
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001869 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001871
1872 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001873 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1874 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001875 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001876 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001877 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001878
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001879 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001880}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001881
Ed Berosetfa771012002-06-09 20:56:40 +00001882/**
1883 * MS style error reporting
1884 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001885 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001886 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001887 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001888 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001889 * which the error occurs (or is detected) and "error:" is one of
1890 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001891 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001892 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001893 *
1894 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001895 * @param fmt the printf style format string
1896 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001897static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001898{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001899 char *currentfile = NULL;
1900 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001901
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 if (is_suppressed_warning(severity))
1903 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001904
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001905 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001907
1908 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001909 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001911 } else {
1912 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001913 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001914
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001915 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001916}
1917
1918/**
1919 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001920 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001921 * not in pass 1
1922 *
1923 * @param severity the severity of the warning or error
1924 * @return true if we should abort error/warning printing
1925 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001926static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001927{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001928 /* Not a warning at all */
1929 if ((severity & ERR_MASK) != ERR_WARNING)
1930 return false;
1931
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001932 /* See if it's a pass-one only warning and we're not in pass one. */
1933 if (((severity & ERR_PASS1) && pass0 != 1) ||
1934 ((severity & ERR_PASS2) && pass0 != 2))
1935 return true;
1936
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001937 /* Might be a warning but suppresed explicitly */
1938 if (severity & ERR_WARN_MASK)
1939 return !warning_on[WARN_IDX(severity)];
1940 else
1941 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001942}
1943
1944/**
1945 * common error reporting
1946 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001947 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001948 * specific error message to error_file and may or may not return. It
1949 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001950 *
1951 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001952 * @param fmt the printf style format string
1953 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001954static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001955{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001956 char msg[1024];
1957 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001958
H. Peter Anvin7df04172008-06-10 18:27:38 -07001959 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001961 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 break;
1963 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001964 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 break;
1966 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001967 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 break;
1969 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001970 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 break;
1972 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001973 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001975 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001976 pfx = "";
1977 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001978 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001979
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001980 vsnprintf(msg, sizeof msg, fmt, args);
1981
1982 fprintf(error_file, "%s%s\n", pfx, msg);
1983
H. Peter Anvin215186f2016-02-17 20:27:41 -08001984 nasmlist->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001985
1986 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001987 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001988
1989 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 case ERR_DEBUG:
1991 /* no further action, by definition */
1992 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001993 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001994 /* Treat warnings as errors */
1995 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1996 terminate_after_phase = true;
1997 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001999 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 break;
2001 case ERR_FATAL:
2002 if (ofile) {
2003 fclose(ofile);
2004 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002005 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002006 }
2007 if (want_usage)
2008 usage();
2009 exit(1); /* instantly die */
2010 break; /* placate silly compilers */
2011 case ERR_PANIC:
2012 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002013 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 exit(3);
2015 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002016 }
2017}
2018
H. Peter Anvin734b1882002-04-30 21:01:08 +00002019static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002020{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002021 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002022}
2023
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002024static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002025{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002026 iflag_t r;
2027
2028 iflag_clear_all(&r);
2029
H. Peter Anvine2c80182005-01-15 22:15:51 +00002030 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002031 iflag_set(&r, IF_8086);
2032 else if (!strcmp(value, "186"))
2033 iflag_set(&r, IF_186);
2034 else if (!strcmp(value, "286"))
2035 iflag_set(&r, IF_286);
2036 else if (!strcmp(value, "386"))
2037 iflag_set(&r, IF_386);
2038 else if (!strcmp(value, "486"))
2039 iflag_set(&r, IF_486);
2040 else if (!strcmp(value, "586") ||
2041 !nasm_stricmp(value, "pentium"))
2042 iflag_set(&r, IF_PENT);
2043 else if (!strcmp(value, "686") ||
2044 !nasm_stricmp(value, "ppro") ||
2045 !nasm_stricmp(value, "pentiumpro") ||
2046 !nasm_stricmp(value, "p2"))
2047 iflag_set(&r, IF_P6);
2048 else if (!nasm_stricmp(value, "p3") ||
2049 !nasm_stricmp(value, "katmai"))
2050 iflag_set(&r, IF_KATMAI);
2051 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2052 !nasm_stricmp(value, "willamette"))
2053 iflag_set(&r, IF_WILLAMETTE);
2054 else if (!nasm_stricmp(value, "prescott"))
2055 iflag_set(&r, IF_PRESCOTT);
2056 else if (!nasm_stricmp(value, "x64") ||
2057 !nasm_stricmp(value, "x86-64"))
2058 iflag_set(&r, IF_X86_64);
2059 else if (!nasm_stricmp(value, "ia64") ||
2060 !nasm_stricmp(value, "ia-64") ||
2061 !nasm_stricmp(value, "itanium")||
2062 !nasm_stricmp(value, "itanic") ||
2063 !nasm_stricmp(value, "merced"))
2064 iflag_set(&r, IF_IA64);
2065 else {
2066 iflag_set(&r, IF_PLEVEL);
2067 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2068 "unknown 'cpu' type");
2069 }
2070 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002071}
2072
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002073static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002074{
2075 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002076
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 if ((i = atoi(value)) == 16)
2078 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002079 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002080 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002081 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 "cannot specify 32-bit segment on processor below a 386");
2083 i = 16;
2084 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002085 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002086 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002087 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002088 "cannot specify 64-bit segment on processor below an x86-64");
2089 i = 16;
2090 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002091 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002092 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002093 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 value);
2095 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002096 }
2097 return i;
2098}