blob: ba42aa889545830af31fb190b61b4c80865138d8 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070046#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080047#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49#include "nasm.h"
50#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvin31b707b2009-06-27 22:07:33 -070061#include "output/outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvin2b046cf2008-01-22 14:08:36 -080084static bool is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085static void usage(void);
86
John Coffman0efaec92002-05-26 19:20:08 +000087static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070088bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080089int pass0, passn;
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 Anvine2c80182005-01-15 22:15:51 +0000175int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176
H. Peter Anvin55340992012-09-09 17:09:00 -0700177static char *quote_for_make(const char *str);
178
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400179static int64_t get_curr_offs(void)
180{
181 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
182}
183
184static void set_curr_offs(int64_t l_off)
185{
186 if (in_abs_seg)
187 abs_offset = l_off;
188 else
189 offsets = raa_write(offsets, location.segment, l_off);
190}
191
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000192static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000194 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800196 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000199}
200
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800201/* Convert a struct tm to a POSIX-style time constant */
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 Anvindbb640b2009-07-18 18:57:16 -0700380 preproc->reset(inname, 0, &nasmlist, 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 Anvindbb640b2009-07-18 18:57:16 -0700405 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400406 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 while ((line = preproc->getline())) {
409 /*
410 * We generate %line directives if needed for later programs
411 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000412 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 int altline = src_get(&linnum, &file_name);
414 if (altline) {
415 if (altline == 1 && lineinc == 1)
416 nasm_fputs("", ofile);
417 else {
418 lineinc = (altline != -1 || lineinc != 1);
419 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000420 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 file_name);
422 }
423 prior_linnum = linnum;
424 }
425 nasm_fputs(line, ofile);
426 nasm_free(line);
427 }
428 nasm_free(file_name);
429 preproc->cleanup(0);
430 if (ofile)
431 fclose(ofile);
432 if (ofile && terminate_after_phase)
433 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400434 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400435 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400437 if (operating_mode & OP_NORMAL) {
438 /*
439 * We must call ofmt->filename _anyway_, even if the user
440 * has specified their own output file, because some
441 * formats (eg OBJ and COFF) use ofmt->filename to find out
442 * the name of the input file and then put that inside the
443 * file.
444 */
445 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400447 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
448 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800449 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400450 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400452 /*
453 * We must call init_labels() before ofmt->init() since
454 * some object formats will want to define labels in their
455 * init routines. (eg OS/2 defines the FLAT group)
456 */
457 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400459 ofmt->init();
460 dfmt = ofmt->current_dfmt;
461 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400463 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200464
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400465 if (!terminate_after_phase) {
466 ofmt->cleanup(using_debug_info);
467 cleanup_labels();
468 fflush(ofile);
469 if (ferror(ofile))
470 nasm_error(ERR_NONFATAL|ERR_NOFILE,
471 "write error on output file `%s'", outname);
472 }
473
474 if (ofile) {
475 fclose(ofile);
476 if (terminate_after_phase)
477 remove(outname);
478 ofile = NULL;
479 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000480 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000481
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700482 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400483 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700484
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000485 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000487
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 raa_free(offsets);
489 saa_free(forwrefs);
490 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000491 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700493 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000494}
495
H. Peter Anvineba20a72002-04-30 20:53:55 +0000496/*
497 * Get a parameter for a command line option.
498 * First arg must be in the form of e.g. -f...
499 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800500static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000501{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800502 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400503 if (p[2]) /* the parameter's in the option */
504 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800506 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000508 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400509 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511 return NULL;
512}
513
H. Peter Anvindc242712007-11-18 11:55:10 -0800514/*
515 * Copy a filename
516 */
517static void copy_filename(char *dst, const char *src)
518{
519 size_t len = strlen(src);
520
521 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800522 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400523 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800524 }
525 strncpy(dst, src, FILENAME_MAX);
526}
527
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700528/*
529 * Convert a string to Make-safe form
530 */
531static char *quote_for_make(const char *str)
532{
533 const char *p;
534 char *os, *q;
535
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400536 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700537 size_t nbs = 0;
538
539 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400540 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700541
542 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400543 switch (*p) {
544 case ' ':
545 case '\t':
546 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
547 n += nbs + 2;
548 nbs = 0;
549 break;
550 case '$':
551 case '#':
552 nbs = 0;
553 n += 2;
554 break;
555 case '\\':
556 nbs++;
557 n++;
558 break;
559 default:
560 nbs = 0;
561 n++;
562 break;
563 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700564 }
565
566 /* Convert N backslashes at the end of filename to 2N backslashes */
567 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400568 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700569
570 os = q = nasm_malloc(n);
571
572 nbs = 0;
573 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400574 switch (*p) {
575 case ' ':
576 case '\t':
577 while (nbs--)
578 *q++ = '\\';
579 *q++ = '\\';
580 *q++ = *p;
581 break;
582 case '$':
583 *q++ = *p;
584 *q++ = *p;
585 nbs = 0;
586 break;
587 case '#':
588 *q++ = '\\';
589 *q++ = *p;
590 nbs = 0;
591 break;
592 case '\\':
593 *q++ = *p;
594 nbs++;
595 break;
596 default:
597 *q++ = *p;
598 nbs = 0;
599 break;
600 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700601 }
602 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400603 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700604
605 *q = '\0';
606
607 return os;
608}
609
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000611 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000613};
614
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100615enum text_options {
616 OPT_PREFIX,
H. Peter Anvin53f15592016-03-01 22:43:51 -0800617 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100618};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619struct textargs textopts[] = {
620 {"prefix", OPT_PREFIX},
621 {"postfix", OPT_POSTFIX},
622 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000623};
624
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400625static void show_version(void)
626{
627 printf("NASM version %s compiled on %s%s\n",
628 nasm_version, nasm_date, nasm_compile_options);
629 exit(0);
630}
631
H. Peter Anvin423e3812007-11-15 17:12:29 -0800632static bool stopoptions = false;
633static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000635 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800636 int i;
637 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700638 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000639
640 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800641 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000642
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400644 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
645 /* These parameters take values */
646 if (!(param = get_param(p, q, &advance)))
647 return advance;
648 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800649
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 switch (p[1]) {
651 case 's':
652 error_file = stdout;
653 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700654
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400655 case 'o': /* output file */
656 copy_filename(outname, param);
657 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700658
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400659 case 'f': /* output format */
660 ofmt = ofmt_find(param, &ofmt_alias);
661 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800662 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400663 "unrecognised output format `%s' - "
664 "use -hf for a list", param);
665 }
666 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700667
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400668 case 'O': /* Optimization level */
669 {
670 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700671
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400672 if (!*param) {
673 /* Naked -O == -Ox */
674 optimizing = MAX_OPTIMIZE;
675 } else {
676 while (*param) {
677 switch (*param) {
678 case '0': case '1': case '2': case '3': case '4':
679 case '5': case '6': case '7': case '8': case '9':
680 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700681
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400682 /* -O0 -> optimizing == -1, 0.98 behaviour */
683 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
684 if (opt < 2)
685 optimizing = opt - 1;
686 else
687 optimizing = opt;
688 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700689
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400690 case 'v':
691 case '+':
692 param++;
693 opt_verbose_info = true;
694 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700695
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400696 case 'x':
697 param++;
698 optimizing = MAX_OPTIMIZE;
699 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700700
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400701 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800702 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400703 "unknown optimization option -O%c\n",
704 *param);
705 break;
706 }
707 }
708 if (optimizing > MAX_OPTIMIZE)
709 optimizing = MAX_OPTIMIZE;
710 }
711 break;
712 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800713
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400714 case 'p': /* pre-include */
715 case 'P':
716 preproc->pre_include(param);
717 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800718
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400719 case 'd': /* pre-define */
720 case 'D':
721 preproc->pre_define(param);
722 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800723
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400724 case 'u': /* un-define */
725 case 'U':
726 preproc->pre_undefine(param);
727 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800728
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400729 case 'i': /* include search path */
730 case 'I':
731 preproc->include_path(param);
732 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800733
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400734 case 'l': /* listing file */
735 copy_filename(listname, param);
736 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800737
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400738 case 'Z': /* error messages file */
739 copy_filename(errname, param);
740 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800741
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400742 case 'F': /* specify debug format */
743 ofmt->current_dfmt = dfmt_find(ofmt, param);
744 if (!ofmt->current_dfmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800745 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400746 "unrecognized debug format `%s' for"
747 " output format `%s'",
748 param, ofmt->shortname);
749 }
750 using_debug_info = true;
751 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800752
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400753 case 'X': /* specify error reporting format */
754 if (nasm_stricmp("vc", param) == 0)
755 nasm_set_verror(nasm_verror_vc);
756 else if (nasm_stricmp("gnu", param) == 0)
757 nasm_set_verror(nasm_verror_gnu);
758 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800759 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400760 "unrecognized error reporting format `%s'",
761 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800763
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700765 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800767
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 case 'h':
769 printf
770 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
771 "[-l listfile]\n"
772 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400773 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400775 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700777 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700779 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400780 " -MG d:o, missing files assumed generated\n"
781 " -MF <file> set Makefile dependency file\n"
782 " -MD <file> assemble and generate dependencies\n"
783 " -MT <file> dependency target name\n"
784 " -MQ <file> dependency target name (quoted)\n"
785 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700786 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 " -s redirect error messages to stdout\n\n"
788 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400789 " -o outfile write output to an outfile\n\n"
790 " -f format select an output format\n\n"
791 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 " -I<path> adds a pathname to the include file path\n");
793 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700794 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400795 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400796 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400797 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 " -P<file> pre-includes a file\n"
799 " -D<macro>[=<value>] pre-defines a macro\n"
800 " -U<macro> undefines a macro\n"
801 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800802 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400803 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400804 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400805 "--prefix,--postfix\n"
806 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100807 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800808 "Warnings:\n");
809 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700811 warnings[i].name, warnings[i].help,
812 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 printf
814 ("\nresponse files should contain command line parameters"
815 ", one per line.\n");
816 if (p[2] == 'f') {
817 printf("\nvalid output formats for -f are"
818 " (`*' denotes default):\n");
819 ofmt_list(ofmt, stdout);
820 } else {
821 printf("\nFor a list of valid output formats, use -hf.\n");
822 printf("For a list of debug formats, use -f <form> -y.\n");
823 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400824 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800826
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 case 'y':
828 printf("\nvalid debug formats for '%s' output format are"
829 " ('*' denotes default):\n", ofmt->shortname);
830 dfmt_list(ofmt, stdout);
831 exit(0);
832 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800833
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700835 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400839 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400842 case 'e': /* preprocess only */
843 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400844 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800846
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400847 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400848 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800850
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400851 case 'W':
852 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
853 do_warn = false;
854 param += 3;
855 } else {
856 do_warn = true;
857 }
858 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800859
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800861 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400862 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400866 do_warn = (param[0] == '+');
867 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400868
869set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400870 for (i = 0; i <= ERR_WARN_MAX; i++) {
871 if (!nasm_stricmp(param, warnings[i].name))
872 break;
873 }
874 if (i <= ERR_WARN_MAX) {
875 warning_on_global[i] = do_warn;
876 } else if (!nasm_stricmp(param, "all")) {
877 for (i = 1; i <= ERR_WARN_MAX; i++)
878 warning_on_global[i] = do_warn;
879 } else if (!nasm_stricmp(param, "none")) {
880 for (i = 1; i <= ERR_WARN_MAX; i++)
881 warning_on_global[i] = !do_warn;
882 } else {
883 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
884 "invalid warning `%s'", param);
885 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800887
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400889 switch (p[2]) {
890 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400891 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400892 break;
893 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400894 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400895 depend_missing_ok = true;
896 break;
897 case 'P':
898 depend_emit_phony = true;
899 break;
900 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300901 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400902 depend_file = q;
903 advance = true;
904 break;
905 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 depend_file = q;
907 advance = true;
908 break;
909 case 'T':
910 depend_target = q;
911 advance = true;
912 break;
913 case 'Q':
914 depend_target = quote_for_make(q);
915 advance = true;
916 break;
917 default:
918 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
919 "unknown dependency option `-M%c'", p[2]);
920 break;
921 }
922 if (advance && (!q || !q[0])) {
923 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
924 "option `-M%c' requires a parameter", p[2]);
925 break;
926 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000928
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 case '-':
930 {
931 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000932
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 if (p[2] == 0) { /* -- => stop processing options */
934 stopoptions = 1;
935 break;
936 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400937
938 if (!nasm_stricmp(p, "--v"))
939 show_version();
940
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 for (s = 0; textopts[s].label; s++) {
942 if (!nasm_stricmp(p + 2, textopts[s].label)) {
943 break;
944 }
945 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000946
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 switch (s) {
948
949 case OPT_PREFIX:
950 case OPT_POSTFIX:
951 {
952 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400953 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 ERR_USAGE,
955 "option `--%s' requires an argument",
956 p + 2);
957 break;
958 } else {
959 advance = 1, param = q;
960 }
961
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100962 switch (s) {
963 case OPT_PREFIX:
964 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100966 case OPT_POSTFIX:
967 strlcpy(lpostfix, param, POSTFIX_MAX);
968 break;
969 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800970 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100971 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 break;
973 }
974 break;
975 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800976
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 default:
978 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400979 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 "unrecognised option `--%s'", p + 2);
981 break;
982 }
983 }
984 break;
985 }
986
987 default:
988 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400989 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 "unrecognised option `-%c'", p[1]);
991 break;
992 }
993 } else {
994 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400995 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800997 } else {
998 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001000 }
1001
1002 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001003}
1004
H. Peter Anvineba20a72002-04-30 20:53:55 +00001005#define ARG_BUF_DELTA 128
1006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001008{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001009 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001010 int bufsize, prevargsize;
1011
1012 bufsize = prevargsize = ARG_BUF_DELTA;
1013 buffer = nasm_malloc(ARG_BUF_DELTA);
1014 prevarg = nasm_malloc(ARG_BUF_DELTA);
1015 prevarg[0] = '\0';
1016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 p = buffer;
1019 while (1) { /* Loop to handle long lines */
1020 q = fgets(p, bufsize - (p - buffer), rfile);
1021 if (!q)
1022 break;
1023 p += strlen(p);
1024 if (p > buffer && p[-1] == '\n')
1025 break;
1026 if (p - buffer > bufsize - 10) {
1027 int offset;
1028 offset = p - buffer;
1029 bufsize += ARG_BUF_DELTA;
1030 buffer = nasm_realloc(buffer, bufsize);
1031 p = buffer + offset;
1032 }
1033 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001034
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 if (!q && p == buffer) {
1036 if (prevarg[0])
1037 process_arg(prevarg, NULL);
1038 nasm_free(buffer);
1039 nasm_free(prevarg);
1040 return;
1041 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 /*
1044 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1045 * them are present at the end of the line.
1046 */
1047 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001048
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001049 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001051
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001052 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 if (process_arg(prevarg, p))
1055 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056
Charles Crayne192d5b52007-10-18 19:02:42 -07001057 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 prevargsize += ARG_BUF_DELTA;
1059 prevarg = nasm_realloc(prevarg, prevargsize);
1060 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001061 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001062 }
1063}
1064
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001065/* Function to process args from a string of args, rather than the
1066 * argv array. Used by the environment variable and response file
1067 * processing.
1068 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001069static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001071 char *p, *q, *arg, *prevarg;
1072 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001073
1074 p = args;
1075 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001077 arg = NULL;
1078 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 q = p;
1080 while (*p && *p != separator)
1081 p++;
1082 while (*p == separator)
1083 *p++ = '\0';
1084 prevarg = arg;
1085 arg = q;
1086 if (process_arg(prevarg, arg))
1087 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001088 }
1089 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001091}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001092
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001093static void process_response_file(const char *file)
1094{
1095 char str[2048];
1096 FILE *f = fopen(file, "r");
1097 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001098 perror(file);
1099 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001100 }
1101 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001102 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001103 }
1104 fclose(f);
1105}
1106
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001107static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001108{
1109 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001110 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001111 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001112
Charles Craynefcce07f2007-09-30 22:15:36 -07001113 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001114
H. Peter Anvin972079f2008-09-30 17:01:23 -07001115 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001116 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001117
1118 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001119 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001120 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001121 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001122 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 envcopy = nasm_strdup(envreal);
1124 process_args(envcopy);
1125 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001126 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001127
1128 /*
1129 * Now process the actual command line.
1130 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001132 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 argv++;
1134 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001135 /*
1136 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 * arguments like the environment variable. This allows us
1138 * to have multiple arguments on a single line, which is
1139 * different to the -@resp file processing below for regular
1140 * NASM.
1141 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001142 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 argc--;
1144 argv++;
1145 }
1146 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001147 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001148 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001149 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001150 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 process_respfile(rfile);
1152 fclose(rfile);
1153 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001154 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 "unable to open response file `%s'", p);
1156 }
1157 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001158 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1159 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001160 }
1161
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001162 /*
1163 * Look for basic command line typos. This definitely doesn't
1164 * catch all errors, but it might help cases of fumbled fingers.
1165 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001166 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001167 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001168 "no input file specified");
1169 else if (!strcmp(inname, errname) ||
1170 !strcmp(inname, outname) ||
1171 !strcmp(inname, listname) ||
1172 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001173 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001174 "file `%s' is both input and output file",
1175 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001176
H. Peter Anvin70653092007-10-19 14:42:29 -07001177 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001178 error_file = fopen(errname, "w");
1179 if (!error_file) {
1180 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001181 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001182 "cannot open file `%s' for error messages",
1183 errname);
1184 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001185 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001186}
1187
H. Peter Anvin70055962007-10-11 00:05:31 -07001188static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001189
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001190static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001191{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001192 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001194 int i, validid;
1195 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001196 int32_t seg;
1197 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001198 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001200 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001201
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001202 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin41087062016-03-03 14:27:34 -08001203 nasm_fatal(0, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 "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) {
1220 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001221 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001223 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001224 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001226 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001227 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 saa_rewind(forwrefs);
1229 forwref = saa_rstruct(forwrefs);
1230 raa_free(offsets);
1231 offsets = raa_init();
1232 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001233 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001234 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001235 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001236
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001238 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001239 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001240 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001241
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001243 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001246 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001247 * Here we parse our directives; this is not handled by the
1248 * 'real' parser. This really should be a separate function.
1249 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001251 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001252 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001253 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001254
H. Peter Anvin70055962007-10-11 00:05:31 -07001255 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001256 case D_SEGMENT: /* [SEGMENT n] */
1257 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 seg = ofmt->section(value, pass2, &sb);
1259 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001260 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001261 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 value);
1263 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001264 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 location.segment = seg;
1266 }
1267 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001268 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001269 if (*value) {
1270 stdscan_reset();
1271 stdscan_set(value);
1272 tokval.t_type = TOKEN_INVALID;
1273 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1274 if (e) {
1275 unsigned int align = (unsigned int)e->value;
1276 if ((uint64_t)e->value > 0x7fffffff) {
1277 /*
1278 * FIXME: Please make some sane message here
1279 * ofmt should have some 'check' method which
1280 * would report segment alignment bounds.
1281 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001282 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001283 "incorrect segment alignment `%s'", value);
1284 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001285 nasm_error(ERR_NONFATAL,
1286 "segment alignment `%s' is not power of two",
1287 value);
1288 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001289
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001290 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001291 if (location.segment != NO_SEG)
1292 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001293 }
1294 }
1295 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001296 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 if (*value == '$')
1298 value++; /* skip initial $ if present */
1299 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001300 q = value;
1301 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001303 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 *q++ = '\0';
1305 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001306 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001307 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001309 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001311 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001314 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001316 }
1317 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001318 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 "identifier expected after EXTERN");
1320 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001321 }
1322 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 *q++ = '\0';
1324 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001325 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 special = NULL;
1327 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1328 int temp = pass0;
1329 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001330 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001332 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001334 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 } /* else pass0 == 1 */
1336 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001337 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001338 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001340 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 if (*value == '$')
1342 value++; /* skip initial $ if present */
1343 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001344 q = value;
1345 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 *q++ = '\0';
1349 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001350 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001353 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001355 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001358 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 }
1361 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001362 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 "identifier expected after GLOBAL");
1364 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001365 }
1366 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 *q++ = '\0';
1368 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001371 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 } /* pass == 1 */
1373 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001374 case D_COMMON: /* [COMMON symbol size:special] */
1375 {
1376 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001377
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 if (*value == '$')
1379 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001380 p = value;
1381 validid = true;
1382 if (!isidstart(*p))
1383 validid = false;
1384 while (*p && !nasm_isspace(*p)) {
1385 if (!isidchar(*p))
1386 validid = false;
1387 p++;
1388 }
1389 if (!validid) {
1390 nasm_error(ERR_NONFATAL,
1391 "identifier expected after COMMON");
1392 break;
1393 }
1394 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001395 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001396 q = p;
1397 while (*q && *q != ':')
1398 q++;
1399 if (*q == ':') {
1400 *q++ = '\0';
1401 special = q;
1402 } else {
1403 special = NULL;
1404 }
1405 size = readnum(p, &rn_error);
1406 if (rn_error) {
1407 nasm_error(ERR_NONFATAL,
1408 "invalid size specified"
1409 " in COMMON declaration");
1410 break;
1411 }
1412 } else {
1413 nasm_error(ERR_NONFATAL,
1414 "no size specified in"
1415 " COMMON declaration");
1416 break;
1417 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001418
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001419 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001420 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001421 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001422 if (special)
1423 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 }
1425 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001426 }
1427 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001429 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 tokval.t_type = TOKEN_INVALID;
1431 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001432 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 if (e) {
1434 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001435 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 1 ? ERR_NONFATAL : ERR_PANIC,
1437 "cannot use non-relocatable expression as "
1438 "ABSOLUTE address");
1439 else {
1440 abs_seg = reloc_seg(e);
1441 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001442 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001443 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 abs_offset = 0x100; /* don't go near zero in case of / */
1445 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001446 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001448 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 location.segment = NO_SEG;
1450 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001451 case D_DEBUG: /* [DEBUG] */
1452 {
1453 char debugid[128];
1454 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001455
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 p = value;
1457 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001458 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001459 if (!isidstart(*p)) {
1460 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001461 } else {
1462 while (*p && !nasm_isspace(*p)) {
1463 if (q >= debugid + sizeof debugid - 1) {
1464 overlong = true;
1465 break;
1466 }
1467 if (!isidchar(*p))
1468 badid = true;
1469 *q++ = *p++;
1470 }
1471 *q = 0;
1472 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001473 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001474 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1475 "identifier expected after DEBUG");
1476 break;
1477 }
1478 if (overlong) {
1479 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1480 "DEBUG identifier too long");
1481 break;
1482 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001483 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001484 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001485 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001487 }
1488 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001489 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001490 switch(*value) {
1491 case '-': validid = 0; value++; break;
1492 case '+': validid = 1; value++; break;
1493 case '*': validid = 2; value++; break;
1494 default: validid = 1; break;
1495 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001497 for (i = 1; i <= ERR_WARN_MAX; i++)
1498 if (!nasm_stricmp(value, warnings[i].name))
1499 break;
1500 if (i <= ERR_WARN_MAX) {
1501 switch(validid) {
1502 case 0:
1503 warning_on[i] = false;
1504 break;
1505 case 1:
1506 warning_on[i] = true;
1507 break;
1508 case 2:
1509 warning_on[i] = warning_on_global[i];
1510 break;
1511 }
1512 } else
1513 nasm_error(ERR_NONFATAL,
1514 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001516 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 cpu = get_cpu(value);
1518 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001519 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001520 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 if (*value == '+') {
1522 user_nolist = 0;
1523 } else {
1524 if (*value == '-') {
1525 user_nolist = 1;
1526 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001527 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 }
1529 }
1530 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001531 case D_DEFAULT: /* [DEFAULT] */
1532 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001533 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001534 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001535 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001536 switch ((int)tokval.t_integer) {
1537 case S_REL:
1538 globalrel = 1;
1539 break;
1540 case S_ABS:
1541 globalrel = 0;
1542 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001543 case P_BND:
1544 globalbnd = 1;
1545 break;
1546 case P_NOBND:
1547 globalbnd = 0;
1548 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001549 default:
1550 err = 1;
1551 break;
1552 }
1553 } else {
1554 err = 1;
1555 }
1556 break;
1557 case D_FLOAT:
1558 if (float_option(value)) {
1559 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1560 "unknown 'float' directive: %s",
1561 value);
1562 }
1563 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001565 if (ofmt->directive(d, value, pass2))
1566 break;
1567 /* else fall through */
1568 case D_unknown:
1569 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1570 "unrecognised directive [%s]",
1571 directive);
1572 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001574 if (err) {
1575 nasm_error(ERR_NONFATAL,
1576 "invalid parameter to [%s] directive",
1577 directive);
1578 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001580 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581
Charles Crayne2581c862008-09-10 19:21:52 -07001582 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001584 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001586 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 forwref = saa_rstruct(forwrefs);
1588 } while (forwref != NULL
1589 && forwref->lineno == globallineno);
1590 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001591 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001592
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001593 if (output_ins.forw_ref) {
1594 if (passn == 1) {
1595 for (i = 0; i < output_ins.operands; i++) {
1596 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1597 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1598 fwinf->lineno = globallineno;
1599 fwinf->operand = i;
1600 }
1601 }
1602 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001603 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 /* forw_ref */
1607 if (output_ins.opcode == I_EQU) {
1608 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001609 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 * Special `..' EQUs get processed in pass two,
1611 * except `..@' macro-processor EQUs which are done
1612 * in the normal place.
1613 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001614 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001615 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001617
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618 else if (output_ins.label[0] != '.' ||
1619 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 output_ins.label[2] == '@') {
1621 if (output_ins.operands == 1 &&
1622 (output_ins.oprs[0].type & IMMEDIATE) &&
1623 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001624 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 def_label(output_ins.label,
1626 output_ins.oprs[0].segment,
1627 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001628 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001630 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001632 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001634 && (output_ins.oprs[1].type & IMMEDIATE)
1635 && output_ins.oprs[1].segment == NO_SEG
1636 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001638 output_ins.oprs[0].offset | SEG_ABS,
1639 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001640 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001642 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001645 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001646 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 * Special `..' EQUs get processed here, except
1648 * `..@' macro processor EQUs which are done above.
1649 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 output_ins.label[1] == '.' &&
1652 output_ins.label[2] != '@') {
1653 if (output_ins.operands == 1 &&
1654 (output_ins.oprs[0].type & IMMEDIATE)) {
1655 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 output_ins.oprs[0].segment,
1657 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001658 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001660 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001662 && output_ins.oprs[0].segment == NO_SEG
1663 && (output_ins.oprs[1].type & IMMEDIATE)
1664 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001666 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001667 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001668 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001670 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001672 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001673 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001675
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001677
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001678 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001679 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001680
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 /* if (using_debug_info) && output_ins.opcode != -1) */
1682 if (using_debug_info)
1683 { /* fbk 03/25/01 */
1684 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001685 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 TYS_ELEMENTS(output_ins.operands);
1687 switch (output_ins.opcode) {
1688 case I_RESB:
1689 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001690 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 break;
1692 case I_RESW:
1693 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001694 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 break;
1696 case I_RESD:
1697 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001698 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 break;
1700 case I_RESQ:
1701 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001702 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 break;
1704 case I_REST:
1705 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001706 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001708 case I_RESO:
1709 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001710 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001711 break;
1712 case I_RESY:
1713 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001714 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001715 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 case I_DB:
1717 typeinfo |= TY_BYTE;
1718 break;
1719 case I_DW:
1720 typeinfo |= TY_WORD;
1721 break;
1722 case I_DD:
1723 if (output_ins.eops_float)
1724 typeinfo |= TY_FLOAT;
1725 else
1726 typeinfo |= TY_DWORD;
1727 break;
1728 case I_DQ:
1729 typeinfo |= TY_QWORD;
1730 break;
1731 case I_DT:
1732 typeinfo |= TY_TBYTE;
1733 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001734 case I_DO:
1735 typeinfo |= TY_OWORD;
1736 break;
1737 case I_DY:
1738 typeinfo |= TY_YWORD;
1739 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 default:
1741 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001742
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001744
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001745 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001746 }
1747 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001749 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001750 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001751 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 * else l == -1 => invalid instruction, which will be
1753 * flagged as an error on pass 2
1754 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001755
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001756 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001758 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001760 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001761
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 }
1763 } /* not an EQU */
1764 cleanup_insn(&output_ins);
1765 }
1766 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001767 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001769
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001770 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001771 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001772 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001773
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 if (pass1 == 1)
1775 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001776
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001777 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001779 } else if (global_offset_changed &&
1780 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001781 prev_offset_changed = global_offset_changed;
1782 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001783 } else {
1784 stall_count++;
1785 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001786
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001787 if (terminate_after_phase)
1788 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001789
1790 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001791 /* We get here if the labels don't converge
1792 * Example: FOO equ FOO + 1
1793 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001794 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001795 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001796 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001797 nasm_error(ERR_NONFATAL,
1798 "Possible causes: recursive EQUs, macro abuse.");
1799 break;
1800 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001801 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001802
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 preproc->cleanup(0);
1804 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001805 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001806 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001807 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1808 }
1809}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001810
H. Peter Anvin70055962007-10-11 00:05:31 -07001811static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001812{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001813 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001815 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001816
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001817 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001819 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001820 q = strchr(buf, ']');
1821 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001822 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001823
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001824 /* stip off the comments */
1825 p = strchr(buf, ';');
1826 if (p) {
1827 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001828 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001829 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001830 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001831
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001832 /* no brace, no trailing spaces */
1833 *q = '\0';
1834 nasm_zap_spaces_rev(--q);
1835
1836 /* directive */
1837 p = nasm_skip_spaces(++buf);
1838 q = nasm_skip_word(p);
1839 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001840 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001841 *q = '\0';
1842 *directive = p;
1843
1844 /* and value finally */
1845 p = nasm_skip_spaces(++q);
1846 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001847
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001848 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001849}
1850
Ed Berosetfa771012002-06-09 20:56:40 +00001851/**
1852 * gnu style error reporting
1853 * This function prints an error message to error_file in the
1854 * style used by GNU. An example would be:
1855 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001856 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001857 * which the error occurs (or is detected) and "error:" is one of
1858 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001859 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001860 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001861 *
1862 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001863 * @param fmt the printf style format string
1864 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001865static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001866{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001867 char *currentfile = NULL;
1868 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001869
Ed Berosetfa771012002-06-09 20:56:40 +00001870 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001872
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001873 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001875
1876 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001877 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1878 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001879 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001880 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001881 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001882
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001883 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001884}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001885
Ed Berosetfa771012002-06-09 20:56:40 +00001886/**
1887 * MS style error reporting
1888 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001889 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001890 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001891 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001892 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001893 * which the error occurs (or is detected) and "error:" is one of
1894 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001895 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001896 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001897 *
1898 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001899 * @param fmt the printf style format string
1900 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001901static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001902{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001903 char *currentfile = NULL;
1904 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001905
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 if (is_suppressed_warning(severity))
1907 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001908
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001909 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001911
1912 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001913 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001915 } else {
1916 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001917 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001918
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001919 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001920}
1921
1922/**
1923 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001924 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001925 * not in pass 1
1926 *
1927 * @param severity the severity of the warning or error
1928 * @return true if we should abort error/warning printing
1929 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001930static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001931{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001932 /* Not a warning at all */
1933 if ((severity & ERR_MASK) != ERR_WARNING)
1934 return false;
1935
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001936 /* See if it's a pass-one only warning and we're not in pass one. */
1937 if (((severity & ERR_PASS1) && pass0 != 1) ||
1938 ((severity & ERR_PASS2) && pass0 != 2))
1939 return true;
1940
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001941 /* Might be a warning but suppresed explicitly */
1942 if (severity & ERR_WARN_MASK)
1943 return !warning_on[WARN_IDX(severity)];
1944 else
1945 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001946}
1947
1948/**
1949 * common error reporting
1950 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001951 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001952 * specific error message to error_file and may or may not return. It
1953 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001954 *
1955 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001956 * @param fmt the printf style format string
1957 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001958static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001959{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001960 char msg[1024];
1961 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001962
H. Peter Anvin7df04172008-06-10 18:27:38 -07001963 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001964 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001965 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 break;
1967 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001968 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 break;
1970 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001971 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 break;
1973 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001974 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 break;
1976 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001977 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001979 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001980 pfx = "";
1981 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001982 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001983
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001984 vsnprintf(msg, sizeof msg, fmt, args);
1985
1986 fprintf(error_file, "%s%s\n", pfx, msg);
1987
1988 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001989 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001990
1991 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001992 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001993
1994 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 case ERR_DEBUG:
1996 /* no further action, by definition */
1997 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001998 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001999 /* Treat warnings as errors */
2000 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2001 terminate_after_phase = true;
2002 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002004 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 break;
2006 case ERR_FATAL:
2007 if (ofile) {
2008 fclose(ofile);
2009 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002010 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 }
2012 if (want_usage)
2013 usage();
2014 exit(1); /* instantly die */
2015 break; /* placate silly compilers */
2016 case ERR_PANIC:
2017 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002018 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 exit(3);
2020 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002021 }
2022}
2023
H. Peter Anvin734b1882002-04-30 21:01:08 +00002024static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002025{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002026 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002027}
2028
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002029static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002030{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002031 iflag_t r;
2032
2033 iflag_clear_all(&r);
2034
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002036 iflag_set(&r, IF_8086);
2037 else if (!strcmp(value, "186"))
2038 iflag_set(&r, IF_186);
2039 else if (!strcmp(value, "286"))
2040 iflag_set(&r, IF_286);
2041 else if (!strcmp(value, "386"))
2042 iflag_set(&r, IF_386);
2043 else if (!strcmp(value, "486"))
2044 iflag_set(&r, IF_486);
2045 else if (!strcmp(value, "586") ||
2046 !nasm_stricmp(value, "pentium"))
2047 iflag_set(&r, IF_PENT);
2048 else if (!strcmp(value, "686") ||
2049 !nasm_stricmp(value, "ppro") ||
2050 !nasm_stricmp(value, "pentiumpro") ||
2051 !nasm_stricmp(value, "p2"))
2052 iflag_set(&r, IF_P6);
2053 else if (!nasm_stricmp(value, "p3") ||
2054 !nasm_stricmp(value, "katmai"))
2055 iflag_set(&r, IF_KATMAI);
2056 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2057 !nasm_stricmp(value, "willamette"))
2058 iflag_set(&r, IF_WILLAMETTE);
2059 else if (!nasm_stricmp(value, "prescott"))
2060 iflag_set(&r, IF_PRESCOTT);
2061 else if (!nasm_stricmp(value, "x64") ||
2062 !nasm_stricmp(value, "x86-64"))
2063 iflag_set(&r, IF_X86_64);
2064 else if (!nasm_stricmp(value, "ia64") ||
2065 !nasm_stricmp(value, "ia-64") ||
2066 !nasm_stricmp(value, "itanium")||
2067 !nasm_stricmp(value, "itanic") ||
2068 !nasm_stricmp(value, "merced"))
2069 iflag_set(&r, IF_IA64);
2070 else {
2071 iflag_set(&r, IF_PLEVEL);
2072 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2073 "unknown 'cpu' type");
2074 }
2075 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002076}
2077
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002078static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002079{
2080 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002081
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 if ((i = atoi(value)) == 16)
2083 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002084 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002085 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002086 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 "cannot specify 32-bit segment on processor below a 386");
2088 i = 16;
2089 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002090 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002091 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002092 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002093 "cannot specify 64-bit segment on processor below an x86-64");
2094 i = 16;
2095 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002096 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002097 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002098 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 value);
2100 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002101 }
2102 return i;
2103}