blob: d10ddbcb80ee03a4c96267bdf9de67494a453837 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Cyrill Gorcunovf187eb72013-02-15 12:25:33 +04003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070046#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080047#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49#include "nasm.h"
50#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvin31b707b2009-06-27 22:07:33 -070061#include "output/outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvin2b046cf2008-01-22 14:08:36 -080084static bool is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085static void usage(void);
86
John Coffman0efaec92002-05-26 19:20:08 +000087static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070088bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080089int pass0, passn;
Keith Kaniosb7a89542007-04-12 02:40:54 +000090int maxbits = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000091int globalrel = 0;
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
H. Peter Anvin620515a2002-04-30 20:57:38 +0000130enum op_type {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000131 op_normal, /* Preprocess and assemble */
132 op_preprocess, /* Preprocess only */
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700133 op_depend, /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000134};
135static enum op_type operating_mode;
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 Anvin6768eb72002-04-30 20:52:26 +0000170};
171
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700172static bool want_usage;
173static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000174int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000175
H. Peter Anvin55340992012-09-09 17:09:00 -0700176static char *quote_for_make(const char *str);
177
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400178static int64_t get_curr_offs(void)
179{
180 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
181}
182
183static void set_curr_offs(int64_t l_off)
184{
185 if (in_abs_seg)
186 abs_offset = l_off;
187 else
188 offsets = raa_write(offsets, location.segment, l_off);
189}
190
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000191static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000192{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000193 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000194 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800195 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000196 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198}
199
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800200/* Convert a struct tm to a POSIX-style time constant */
201static int64_t posix_mktime(struct tm *tm)
202{
203 int64_t t;
204 int64_t y = tm->tm_year;
205
206 /* See IEEE 1003.1:2004, section 4.14 */
207
208 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
209 t += tm->tm_yday;
210 t *= 24;
211 t += tm->tm_hour;
212 t *= 60;
213 t += tm->tm_min;
214 t *= 60;
215 t += tm->tm_sec;
216
217 return t;
218}
219
220static void define_macros_early(void)
221{
222 char temp[128];
223 struct tm lt, *lt_p, gm, *gm_p;
224 int64_t posix_time;
225
226 lt_p = localtime(&official_compile_time);
227 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400228 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800229
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400230 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
231 preproc->pre_define(temp);
232 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
233 preproc->pre_define(temp);
234 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
235 preproc->pre_define(temp);
236 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
237 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800238 }
239
240 gm_p = gmtime(&official_compile_time);
241 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400242 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800243
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400244 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
245 preproc->pre_define(temp);
246 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
247 preproc->pre_define(temp);
248 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
249 preproc->pre_define(temp);
250 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
251 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800252 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700253
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800254 if (gm_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400255 posix_time = posix_mktime(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800256 else if (lt_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400257 posix_time = posix_mktime(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400259 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260
261 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400262 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
263 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264 }
265}
266
267static void define_macros_late(void)
268{
269 char temp[128];
270
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400271 /*
272 * In case if output format is defined by alias
273 * we have to put shortname of the alias itself here
274 * otherwise ABI backward compatibility gets broken.
275 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400276 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400277 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400278 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800279}
280
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700281static void emit_dependencies(StrList *list)
282{
283 FILE *deps;
284 int linepos, len;
285 StrList *l, *nl;
286
287 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400288 deps = fopen(depend_file, "w");
289 if (!deps) {
290 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
291 "unable to write dependency file `%s'", depend_file);
292 return;
293 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700294 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400295 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700296 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700297
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700298 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400299 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700300 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400301 len = strlen(file);
302 if (linepos + len > 62 && linepos > 1) {
303 fprintf(deps, " \\\n ");
304 linepos = 1;
305 }
306 fprintf(deps, " %s", file);
307 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700308 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700309 }
310 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700311
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400312 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400313 if (depend_emit_phony)
314 fprintf(deps, "%s:\n\n", l->str);
315 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700316 }
317
318 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400319 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700320}
321
H. Peter Anvin038d8612007-04-12 16:54:50 +0000322int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000323{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700324 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700325
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800326 time(&official_compile_time);
327
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400328 iflag_set(&cpu, IF_PLEVEL);
329 iflag_set(&cmd_cpu, IF_PLEVEL);
330
Charles Crayne2581c862008-09-10 19:21:52 -0700331 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700332 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400333 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000334
H. Peter Anvin97e15752007-09-25 08:47:47 -0700335 error_file = stderr;
336
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700337 tolower_init();
338
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400339 nasm_init_malloc_error();
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;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000344 operating_mode = op_normal;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000345
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346 seg_init();
347
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800348 /* Define some macros dependent on the runtime, but not
349 on the command line. */
350 define_macros_early();
351
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000352 parse_cmdline(argc, argv);
353
H. Peter Anvine2c80182005-01-15 22:15:51 +0000354 if (terminate_after_phase) {
355 if (want_usage)
356 usage();
357 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000358 }
359
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000360 /* If debugging info is disabled, suppress any debug calls */
361 if (!using_debug_info)
362 ofmt->current_dfmt = &null_debug_form;
363
H. Peter Anvin76690a12002-04-30 20:52:49 +0000364 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400365 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700366 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000369 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800370 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000371
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400372 depend_ptr = (depend_file || (operating_mode == op_depend)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700373 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400374 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700375
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 switch (operating_mode) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000377 case op_depend:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000379 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700380
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400381 if (depend_missing_ok)
382 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700383
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700384 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400386 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 while ((line = preproc->getline()))
389 nasm_free(line);
390 preproc->cleanup(0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391 }
392 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000393
394 case op_preprocess:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000396 char *line;
397 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000398 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000400
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 if (*outname) {
402 ofile = fopen(outname, "w");
403 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400404 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 "unable to open output file `%s'",
406 outname);
407 } else
408 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000409
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700410 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000411
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400412 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700413 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400414 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700415
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 while ((line = preproc->getline())) {
417 /*
418 * We generate %line directives if needed for later programs
419 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000420 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 int altline = src_get(&linnum, &file_name);
422 if (altline) {
423 if (altline == 1 && lineinc == 1)
424 nasm_fputs("", ofile);
425 else {
426 lineinc = (altline != -1 || lineinc != 1);
427 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000428 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 file_name);
430 }
431 prior_linnum = linnum;
432 }
433 nasm_fputs(line, ofile);
434 nasm_free(line);
435 }
436 nasm_free(file_name);
437 preproc->cleanup(0);
438 if (ofile)
439 fclose(ofile);
440 if (ofile && terminate_after_phase)
441 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400442 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 }
444 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000445
446 case op_normal:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 {
448 /*
449 * We must call ofmt->filename _anyway_, even if the user
450 * has specified their own output file, because some
451 * formats (eg OBJ and COFF) use ofmt->filename to find out
452 * the name of the input file and then put that inside the
453 * file.
454 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400455 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456
H. Peter Anvin0cba1072009-07-05 14:45:12 -0700457 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 if (!ofile) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400459 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 "unable to open output file `%s'", outname);
461 }
462
463 /*
464 * We must call init_labels() before ofmt->init() since
465 * some object formats will want to define labels in their
466 * init routines. (eg OS/2 defines the FLAT group)
467 */
468 init_labels();
469
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400470 ofmt->init();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400471 dfmt = ofmt->current_dfmt;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400472 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700474 assemble_file(inname, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475
476 if (!terminate_after_phase) {
477 ofmt->cleanup(using_debug_info);
478 cleanup_labels();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400479 fflush(ofile);
480 if (ferror(ofile)) {
481 nasm_error(ERR_NONFATAL|ERR_NOFILE,
482 "write error on output file `%s'", outname);
483 }
484 }
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200485
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400486 if (ofile) {
487 fclose(ofile);
488 if (terminate_after_phase)
489 remove(outname);
490 ofile = NULL;
491 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 }
493 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000495
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700496 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400497 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700498
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000499 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000501
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 raa_free(offsets);
503 saa_free(forwrefs);
504 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000505 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700507 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000508}
509
H. Peter Anvineba20a72002-04-30 20:53:55 +0000510/*
511 * Get a parameter for a command line option.
512 * First arg must be in the form of e.g. -f...
513 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800514static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800516 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400517 if (p[2]) /* the parameter's in the option */
518 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800520 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400523 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525 return NULL;
526}
527
H. Peter Anvindc242712007-11-18 11:55:10 -0800528/*
529 * Copy a filename
530 */
531static void copy_filename(char *dst, const char *src)
532{
533 size_t len = strlen(src);
534
535 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400536 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
537 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800538 }
539 strncpy(dst, src, FILENAME_MAX);
540}
541
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700542/*
543 * Convert a string to Make-safe form
544 */
545static char *quote_for_make(const char *str)
546{
547 const char *p;
548 char *os, *q;
549
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400550 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700551 size_t nbs = 0;
552
553 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400554 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700555
556 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400557 switch (*p) {
558 case ' ':
559 case '\t':
560 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
561 n += nbs + 2;
562 nbs = 0;
563 break;
564 case '$':
565 case '#':
566 nbs = 0;
567 n += 2;
568 break;
569 case '\\':
570 nbs++;
571 n++;
572 break;
573 default:
574 nbs = 0;
575 n++;
576 break;
577 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700578 }
579
580 /* Convert N backslashes at the end of filename to 2N backslashes */
581 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400582 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700583
584 os = q = nasm_malloc(n);
585
586 nbs = 0;
587 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400588 switch (*p) {
589 case ' ':
590 case '\t':
591 while (nbs--)
592 *q++ = '\\';
593 *q++ = '\\';
594 *q++ = *p;
595 break;
596 case '$':
597 *q++ = *p;
598 *q++ = *p;
599 nbs = 0;
600 break;
601 case '#':
602 *q++ = '\\';
603 *q++ = *p;
604 nbs = 0;
605 break;
606 case '\\':
607 *q++ = *p;
608 nbs++;
609 break;
610 default:
611 *q++ = *p;
612 nbs = 0;
613 break;
614 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700615 }
616 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400617 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700618
619 *q = '\0';
620
621 return os;
622}
623
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000625 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000627};
628
629#define OPT_PREFIX 0
630#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631struct textargs textopts[] = {
632 {"prefix", OPT_PREFIX},
633 {"postfix", OPT_POSTFIX},
634 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000635};
636
H. Peter Anvin423e3812007-11-15 17:12:29 -0800637static bool stopoptions = false;
638static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000639{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000640 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800641 int i;
642 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700643 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000644
645 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800646 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000647
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400649 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
650 /* These parameters take values */
651 if (!(param = get_param(p, q, &advance)))
652 return advance;
653 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 switch (p[1]) {
656 case 's':
657 error_file = stdout;
658 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700659
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400660 case 'o': /* output file */
661 copy_filename(outname, param);
662 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700663
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400664 case 'f': /* output format */
665 ofmt = ofmt_find(param, &ofmt_alias);
666 if (!ofmt) {
667 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
668 "unrecognised output format `%s' - "
669 "use -hf for a list", param);
670 }
671 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700672
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400673 case 'O': /* Optimization level */
674 {
675 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700676
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400677 if (!*param) {
678 /* Naked -O == -Ox */
679 optimizing = MAX_OPTIMIZE;
680 } else {
681 while (*param) {
682 switch (*param) {
683 case '0': case '1': case '2': case '3': case '4':
684 case '5': case '6': case '7': case '8': case '9':
685 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700686
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400687 /* -O0 -> optimizing == -1, 0.98 behaviour */
688 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
689 if (opt < 2)
690 optimizing = opt - 1;
691 else
692 optimizing = opt;
693 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700694
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400695 case 'v':
696 case '+':
697 param++;
698 opt_verbose_info = true;
699 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700700
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400701 case 'x':
702 param++;
703 optimizing = MAX_OPTIMIZE;
704 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700705
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400706 default:
707 nasm_error(ERR_FATAL,
708 "unknown optimization option -O%c\n",
709 *param);
710 break;
711 }
712 }
713 if (optimizing > MAX_OPTIMIZE)
714 optimizing = MAX_OPTIMIZE;
715 }
716 break;
717 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800718
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400719 case 'p': /* pre-include */
720 case 'P':
721 preproc->pre_include(param);
722 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800723
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400724 case 'd': /* pre-define */
725 case 'D':
726 preproc->pre_define(param);
727 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800728
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400729 case 'u': /* un-define */
730 case 'U':
731 preproc->pre_undefine(param);
732 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800733
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400734 case 'i': /* include search path */
735 case 'I':
736 preproc->include_path(param);
737 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800738
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 case 'l': /* listing file */
740 copy_filename(listname, param);
741 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800742
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400743 case 'Z': /* error messages file */
744 copy_filename(errname, param);
745 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800746
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400747 case 'F': /* specify debug format */
748 ofmt->current_dfmt = dfmt_find(ofmt, param);
749 if (!ofmt->current_dfmt) {
750 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
751 "unrecognized debug format `%s' for"
752 " output format `%s'",
753 param, ofmt->shortname);
754 }
755 using_debug_info = true;
756 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800757
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400758 case 'X': /* specify error reporting format */
759 if (nasm_stricmp("vc", param) == 0)
760 nasm_set_verror(nasm_verror_vc);
761 else if (nasm_stricmp("gnu", param) == 0)
762 nasm_set_verror(nasm_verror_gnu);
763 else
764 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
765 "unrecognized error reporting format `%s'",
766 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800768
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700770 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800772
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 case 'h':
774 printf
775 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
776 "[-l listfile]\n"
777 " [options...] [--] filename\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700778 " or nasm -v for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400780 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700782 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700784 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400785 " -MG d:o, missing files assumed generated\n"
786 " -MF <file> set Makefile dependency file\n"
787 " -MD <file> assemble and generate dependencies\n"
788 " -MT <file> dependency target name\n"
789 " -MQ <file> dependency target name (quoted)\n"
790 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700791 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 " -s redirect error messages to stdout\n\n"
793 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400794 " -o outfile write output to an outfile\n\n"
795 " -f format select an output format\n\n"
796 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 " -I<path> adds a pathname to the include file path\n");
798 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700799 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400800 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400801 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400802 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 " -P<file> pre-includes a file\n"
804 " -D<macro>[=<value>] pre-defines a macro\n"
805 " -U<macro> undefines a macro\n"
806 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800807 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400808 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400809 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400810 "--prefix,--postfix\n"
811 " this options prepend or append the given argument to all\n"
812 " extern and global variables\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800813 "Warnings:\n");
814 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700816 warnings[i].name, warnings[i].help,
817 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 printf
819 ("\nresponse files should contain command line parameters"
820 ", one per line.\n");
821 if (p[2] == 'f') {
822 printf("\nvalid output formats for -f are"
823 " (`*' denotes default):\n");
824 ofmt_list(ofmt, stdout);
825 } else {
826 printf("\nFor a list of valid output formats, use -hf.\n");
827 printf("For a list of debug formats, use -f <form> -y.\n");
828 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400829 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800831
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 case 'y':
833 printf("\nvalid debug formats for '%s' output format are"
834 " ('*' denotes default):\n", ofmt->shortname);
835 dfmt_list(ofmt, stdout);
836 exit(0);
837 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700840 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800842
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 case 'v':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400844 printf("NASM version %s compiled on %s%s\n",
845 nasm_version, nasm_date, nasm_compile_options);
846 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800848
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400849 case 'e': /* preprocess only */
850 case 'E':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 operating_mode = op_preprocess;
852 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800853
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400854 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400855 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800857
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400858 case 'W':
859 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
860 do_warn = false;
861 param += 3;
862 } else {
863 do_warn = true;
864 }
865 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800866
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800868 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400869 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400871 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400873 do_warn = (param[0] == '+');
874 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400875
876set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400877 for (i = 0; i <= ERR_WARN_MAX; i++) {
878 if (!nasm_stricmp(param, warnings[i].name))
879 break;
880 }
881 if (i <= ERR_WARN_MAX) {
882 warning_on_global[i] = do_warn;
883 } else if (!nasm_stricmp(param, "all")) {
884 for (i = 1; i <= ERR_WARN_MAX; i++)
885 warning_on_global[i] = do_warn;
886 } else if (!nasm_stricmp(param, "none")) {
887 for (i = 1; i <= ERR_WARN_MAX; i++)
888 warning_on_global[i] = !do_warn;
889 } else {
890 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
891 "invalid warning `%s'", param);
892 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800894
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400896 switch (p[2]) {
897 case 0:
898 operating_mode = op_depend;
899 break;
900 case 'G':
901 operating_mode = op_depend;
902 depend_missing_ok = true;
903 break;
904 case 'P':
905 depend_emit_phony = true;
906 break;
907 case 'D':
908 depend_file = q;
909 advance = true;
910 break;
911 case 'T':
912 depend_target = q;
913 advance = true;
914 break;
915 case 'Q':
916 depend_target = quote_for_make(q);
917 advance = true;
918 break;
919 default:
920 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
921 "unknown dependency option `-M%c'", p[2]);
922 break;
923 }
924 if (advance && (!q || !q[0])) {
925 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
926 "option `-M%c' requires a parameter", p[2]);
927 break;
928 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000930
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 case '-':
932 {
933 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000934
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 if (p[2] == 0) { /* -- => stop processing options */
936 stopoptions = 1;
937 break;
938 }
939 for (s = 0; textopts[s].label; s++) {
940 if (!nasm_stricmp(p + 2, textopts[s].label)) {
941 break;
942 }
943 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000944
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 switch (s) {
946
947 case OPT_PREFIX:
948 case OPT_POSTFIX:
949 {
950 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400951 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 ERR_USAGE,
953 "option `--%s' requires an argument",
954 p + 2);
955 break;
956 } else {
957 advance = 1, param = q;
958 }
959
960 if (s == OPT_PREFIX) {
961 strncpy(lprefix, param, PREFIX_MAX - 1);
962 lprefix[PREFIX_MAX - 1] = 0;
963 break;
964 }
965 if (s == OPT_POSTFIX) {
966 strncpy(lpostfix, param, POSTFIX_MAX - 1);
967 lpostfix[POSTFIX_MAX - 1] = 0;
968 break;
969 }
970 break;
971 }
972 default:
973 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400974 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 "unrecognised option `--%s'", p + 2);
976 break;
977 }
978 }
979 break;
980 }
981
982 default:
983 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400984 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 "unrecognised option `-%c'", p[1]);
986 break;
987 }
988 } else {
989 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400990 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800992 } else {
993 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400994 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000995 }
996
997 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000998}
999
H. Peter Anvineba20a72002-04-30 20:53:55 +00001000#define ARG_BUF_DELTA 128
1001
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001003{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001004 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001005 int bufsize, prevargsize;
1006
1007 bufsize = prevargsize = ARG_BUF_DELTA;
1008 buffer = nasm_malloc(ARG_BUF_DELTA);
1009 prevarg = nasm_malloc(ARG_BUF_DELTA);
1010 prevarg[0] = '\0';
1011
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 p = buffer;
1014 while (1) { /* Loop to handle long lines */
1015 q = fgets(p, bufsize - (p - buffer), rfile);
1016 if (!q)
1017 break;
1018 p += strlen(p);
1019 if (p > buffer && p[-1] == '\n')
1020 break;
1021 if (p - buffer > bufsize - 10) {
1022 int offset;
1023 offset = p - buffer;
1024 bufsize += ARG_BUF_DELTA;
1025 buffer = nasm_realloc(buffer, bufsize);
1026 p = buffer + offset;
1027 }
1028 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001029
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 if (!q && p == buffer) {
1031 if (prevarg[0])
1032 process_arg(prevarg, NULL);
1033 nasm_free(buffer);
1034 nasm_free(prevarg);
1035 return;
1036 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001037
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 /*
1039 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1040 * them are present at the end of the line.
1041 */
1042 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001043
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001044 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001046
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001047 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001048
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 if (process_arg(prevarg, p))
1050 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001051
Charles Crayne192d5b52007-10-18 19:02:42 -07001052 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 prevargsize += ARG_BUF_DELTA;
1054 prevarg = nasm_realloc(prevarg, prevargsize);
1055 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001056 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001057 }
1058}
1059
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001060/* Function to process args from a string of args, rather than the
1061 * argv array. Used by the environment variable and response file
1062 * processing.
1063 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001064static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001066 char *p, *q, *arg, *prevarg;
1067 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001068
1069 p = args;
1070 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001072 arg = NULL;
1073 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 q = p;
1075 while (*p && *p != separator)
1076 p++;
1077 while (*p == separator)
1078 *p++ = '\0';
1079 prevarg = arg;
1080 arg = q;
1081 if (process_arg(prevarg, arg))
1082 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001083 }
1084 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001086}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001087
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001088static void process_response_file(const char *file)
1089{
1090 char str[2048];
1091 FILE *f = fopen(file, "r");
1092 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001093 perror(file);
1094 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001095 }
1096 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001097 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001098 }
1099 fclose(f);
1100}
1101
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001102static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001103{
1104 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001105 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001106 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001107
Charles Craynefcce07f2007-09-30 22:15:36 -07001108 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001109
H. Peter Anvin972079f2008-09-30 17:01:23 -07001110 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001111 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001112
1113 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001114 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001115 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001116 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001117 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 envcopy = nasm_strdup(envreal);
1119 process_args(envcopy);
1120 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001121 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001122
1123 /*
1124 * Now process the actual command line.
1125 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001127 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 argv++;
1129 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001130 /*
1131 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 * arguments like the environment variable. This allows us
1133 * to have multiple arguments on a single line, which is
1134 * different to the -@resp file processing below for regular
1135 * NASM.
1136 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001137 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 argc--;
1139 argv++;
1140 }
1141 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001142 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001143 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001144 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001145 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 process_respfile(rfile);
1147 fclose(rfile);
1148 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001149 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 "unable to open response file `%s'", p);
1151 }
1152 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001153 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1154 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001155 }
1156
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001157 /*
1158 * Look for basic command line typos. This definitely doesn't
1159 * catch all errors, but it might help cases of fumbled fingers.
1160 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001161 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001162 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001163 "no input file specified");
1164 else if (!strcmp(inname, errname) ||
1165 !strcmp(inname, outname) ||
1166 !strcmp(inname, listname) ||
1167 (depend_file && !strcmp(inname, depend_file)))
1168 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1169 "file `%s' is both input and output file",
1170 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001171
H. Peter Anvin70653092007-10-19 14:42:29 -07001172 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001173 error_file = fopen(errname, "w");
1174 if (!error_file) {
1175 error_file = stderr; /* Revert to default! */
1176 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1177 "cannot open file `%s' for error messages",
1178 errname);
1179 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001180 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001181}
1182
H. Peter Anvin70055962007-10-11 00:05:31 -07001183static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001184
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001185static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001186{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001187 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001189 int i, validid;
1190 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001191 int32_t seg;
1192 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001193 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001195 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001196
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001197 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001198 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001200
Charles Craynec1905c22008-09-11 18:54:06 -07001201 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001202 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 int pass1, pass2;
1204 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001205
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001206 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001207 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1208 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001209
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001210 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211
Keith Kaniosb7a89542007-04-12 02:40:54 +00001212 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 cpu = cmd_cpu;
1214 if (pass0 == 2) {
1215 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001216 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001218 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001219 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001221 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001222 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 saa_rewind(forwrefs);
1224 forwref = saa_rstruct(forwrefs);
1225 raa_free(offsets);
1226 offsets = raa_init();
1227 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001228 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001229 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001230 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001231
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001233 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001234 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001235 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001236
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001238 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001241 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001242 * Here we parse our directives; this is not handled by the
1243 * 'real' parser. This really should be a separate function.
1244 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001246 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001247 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001248 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001249
H. Peter Anvin70055962007-10-11 00:05:31 -07001250 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001251 case D_SEGMENT: /* [SEGMENT n] */
1252 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 seg = ofmt->section(value, pass2, &sb);
1254 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001255 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001256 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 value);
1258 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001259 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 location.segment = seg;
1261 }
1262 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001263 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001264 if (*value) {
1265 stdscan_reset();
1266 stdscan_set(value);
1267 tokval.t_type = TOKEN_INVALID;
1268 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1269 if (e) {
1270 unsigned int align = (unsigned int)e->value;
1271 if ((uint64_t)e->value > 0x7fffffff) {
1272 /*
1273 * FIXME: Please make some sane message here
1274 * ofmt should have some 'check' method which
1275 * would report segment alignment bounds.
1276 */
1277 nasm_error(ERR_FATAL,
1278 "incorrect segment alignment `%s'", value);
1279 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001280 nasm_error(ERR_NONFATAL,
1281 "segment alignment `%s' is not power of two",
1282 value);
1283 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001284 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001285 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001286 }
1287 }
1288 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001289 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 if (*value == '$')
1291 value++; /* skip initial $ if present */
1292 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001293 q = value;
1294 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001296 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 *q++ = '\0';
1298 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001299 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001300 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001301 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001302 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001303 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001304 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001305 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001307 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001309 }
1310 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001311 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 "identifier expected after EXTERN");
1313 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 }
1315 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 *q++ = '\0';
1317 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 special = NULL;
1320 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1321 int temp = pass0;
1322 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001323 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001325 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 } /* else pass0 == 1 */
1329 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001330 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001331 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001333 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 if (*value == '$')
1335 value++; /* skip initial $ if present */
1336 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001337 q = value;
1338 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001340 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 *q++ = '\0';
1342 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001343 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001346 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001348 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001349 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001351 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001353 }
1354 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001355 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 "identifier expected after GLOBAL");
1357 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 }
1359 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 *q++ = '\0';
1361 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001362 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001364 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 } /* pass == 1 */
1366 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001367 case D_COMMON: /* [COMMON symbol size:special] */
1368 {
1369 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001370
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 if (*value == '$')
1372 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001373 p = value;
1374 validid = true;
1375 if (!isidstart(*p))
1376 validid = false;
1377 while (*p && !nasm_isspace(*p)) {
1378 if (!isidchar(*p))
1379 validid = false;
1380 p++;
1381 }
1382 if (!validid) {
1383 nasm_error(ERR_NONFATAL,
1384 "identifier expected after COMMON");
1385 break;
1386 }
1387 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001388 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001389 q = p;
1390 while (*q && *q != ':')
1391 q++;
1392 if (*q == ':') {
1393 *q++ = '\0';
1394 special = q;
1395 } else {
1396 special = NULL;
1397 }
1398 size = readnum(p, &rn_error);
1399 if (rn_error) {
1400 nasm_error(ERR_NONFATAL,
1401 "invalid size specified"
1402 " in COMMON declaration");
1403 break;
1404 }
1405 } else {
1406 nasm_error(ERR_NONFATAL,
1407 "no size specified in"
1408 " COMMON declaration");
1409 break;
1410 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001411
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001412 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001413 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001414 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001415 if (special)
1416 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 }
1418 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001419 }
1420 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001422 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 tokval.t_type = TOKEN_INVALID;
1424 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001425 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 if (e) {
1427 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001428 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 1 ? ERR_NONFATAL : ERR_PANIC,
1430 "cannot use non-relocatable expression as "
1431 "ABSOLUTE address");
1432 else {
1433 abs_seg = reloc_seg(e);
1434 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001435 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001436 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 abs_offset = 0x100; /* don't go near zero in case of / */
1438 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001439 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001441 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 location.segment = NO_SEG;
1443 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001444 case D_DEBUG: /* [DEBUG] */
1445 {
1446 char debugid[128];
1447 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001448
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 p = value;
1450 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001451 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001452 if (!isidstart(*p)) {
1453 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001454 } else {
1455 while (*p && !nasm_isspace(*p)) {
1456 if (q >= debugid + sizeof debugid - 1) {
1457 overlong = true;
1458 break;
1459 }
1460 if (!isidchar(*p))
1461 badid = true;
1462 *q++ = *p++;
1463 }
1464 *q = 0;
1465 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001466 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001467 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1468 "identifier expected after DEBUG");
1469 break;
1470 }
1471 if (overlong) {
1472 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1473 "DEBUG identifier too long");
1474 break;
1475 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001476 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001477 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001478 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001480 }
1481 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001482 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001483 switch(*value) {
1484 case '-': validid = 0; value++; break;
1485 case '+': validid = 1; value++; break;
1486 case '*': validid = 2; value++; break;
1487 default: validid = 1; break;
1488 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001490 for (i = 1; i <= ERR_WARN_MAX; i++)
1491 if (!nasm_stricmp(value, warnings[i].name))
1492 break;
1493 if (i <= ERR_WARN_MAX) {
1494 switch(validid) {
1495 case 0:
1496 warning_on[i] = false;
1497 break;
1498 case 1:
1499 warning_on[i] = true;
1500 break;
1501 case 2:
1502 warning_on[i] = warning_on_global[i];
1503 break;
1504 }
1505 } else
1506 nasm_error(ERR_NONFATAL,
1507 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001509 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 cpu = get_cpu(value);
1511 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001512 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001513 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 if (*value == '+') {
1515 user_nolist = 0;
1516 } else {
1517 if (*value == '-') {
1518 user_nolist = 1;
1519 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001520 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 }
1522 }
1523 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001524 case D_DEFAULT: /* [DEFAULT] */
1525 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001526 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001527 tokval.t_type = TOKEN_INVALID;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001528 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1529 switch ((int)tokval.t_integer) {
1530 case S_REL:
1531 globalrel = 1;
1532 break;
1533 case S_ABS:
1534 globalrel = 0;
1535 break;
1536 default:
1537 err = 1;
1538 break;
1539 }
1540 } else {
1541 err = 1;
1542 }
1543 break;
1544 case D_FLOAT:
1545 if (float_option(value)) {
1546 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1547 "unknown 'float' directive: %s",
1548 value);
1549 }
1550 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001552 if (ofmt->directive(d, value, pass2))
1553 break;
1554 /* else fall through */
1555 case D_unknown:
1556 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1557 "unrecognised directive [%s]",
1558 directive);
1559 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001561 if (err) {
1562 nasm_error(ERR_NONFATAL,
1563 "invalid parameter to [%s] directive",
1564 directive);
1565 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001567 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568
Charles Crayne2581c862008-09-10 19:21:52 -07001569 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001571 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001573 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 forwref = saa_rstruct(forwrefs);
1575 } while (forwref != NULL
1576 && forwref->lineno == globallineno);
1577 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001578 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001579
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001580 if (output_ins.forw_ref) {
1581 if (passn == 1) {
1582 for (i = 0; i < output_ins.operands; i++) {
1583 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1584 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1585 fwinf->lineno = globallineno;
1586 fwinf->operand = i;
1587 }
1588 }
1589 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001590 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001592
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 /* forw_ref */
1594 if (output_ins.opcode == I_EQU) {
1595 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001596 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 * Special `..' EQUs get processed in pass two,
1598 * except `..@' macro-processor EQUs which are done
1599 * in the normal place.
1600 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001601 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001602 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001604
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001605 else if (output_ins.label[0] != '.' ||
1606 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 output_ins.label[2] == '@') {
1608 if (output_ins.operands == 1 &&
1609 (output_ins.oprs[0].type & IMMEDIATE) &&
1610 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001611 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 def_label(output_ins.label,
1613 output_ins.oprs[0].segment,
1614 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001615 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001617 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001619 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001621 && (output_ins.oprs[1].type & IMMEDIATE)
1622 && output_ins.oprs[1].segment == NO_SEG
1623 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001625 output_ins.oprs[0].offset | SEG_ABS,
1626 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001627 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001629 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001631 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001632 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001633 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 * Special `..' EQUs get processed here, except
1635 * `..@' macro processor EQUs which are done above.
1636 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001637 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 output_ins.label[1] == '.' &&
1639 output_ins.label[2] != '@') {
1640 if (output_ins.operands == 1 &&
1641 (output_ins.oprs[0].type & IMMEDIATE)) {
1642 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001643 output_ins.oprs[0].segment,
1644 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001645 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001647 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001649 && output_ins.oprs[0].segment == NO_SEG
1650 && (output_ins.oprs[1].type & IMMEDIATE)
1651 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001653 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001655 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001657 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001659 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001660 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001662
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001664
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001665 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001666 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001667
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 /* if (using_debug_info) && output_ins.opcode != -1) */
1669 if (using_debug_info)
1670 { /* fbk 03/25/01 */
1671 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001672 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 TYS_ELEMENTS(output_ins.operands);
1674 switch (output_ins.opcode) {
1675 case I_RESB:
1676 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001677 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 break;
1679 case I_RESW:
1680 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001681 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 break;
1683 case I_RESD:
1684 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001685 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 break;
1687 case I_RESQ:
1688 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001689 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 break;
1691 case I_REST:
1692 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001693 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001695 case I_RESO:
1696 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001697 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001698 break;
1699 case I_RESY:
1700 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001701 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001702 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 case I_DB:
1704 typeinfo |= TY_BYTE;
1705 break;
1706 case I_DW:
1707 typeinfo |= TY_WORD;
1708 break;
1709 case I_DD:
1710 if (output_ins.eops_float)
1711 typeinfo |= TY_FLOAT;
1712 else
1713 typeinfo |= TY_DWORD;
1714 break;
1715 case I_DQ:
1716 typeinfo |= TY_QWORD;
1717 break;
1718 case I_DT:
1719 typeinfo |= TY_TBYTE;
1720 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001721 case I_DO:
1722 typeinfo |= TY_OWORD;
1723 break;
1724 case I_DY:
1725 typeinfo |= TY_YWORD;
1726 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 default:
1728 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001729
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001731
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001732 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001733 }
1734 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001736 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001737 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001738 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 * else l == -1 => invalid instruction, which will be
1740 * flagged as an error on pass 2
1741 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001742
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001743 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001745 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 &nasmlist);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001747 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001748
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 }
1750 } /* not an EQU */
1751 cleanup_insn(&output_ins);
1752 }
1753 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001754 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001756
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001757 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001758 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001759 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001760
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 if (pass1 == 1)
1762 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001763
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001764 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001766 } else if (global_offset_changed &&
1767 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001768 prev_offset_changed = global_offset_changed;
1769 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001770 } else {
1771 stall_count++;
1772 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001773
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001774 if (terminate_after_phase)
1775 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001776
1777 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001778 /* We get here if the labels don't converge
1779 * Example: FOO equ FOO + 1
1780 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001781 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001782 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001783 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001784 nasm_error(ERR_NONFATAL,
1785 "Possible causes: recursive EQUs, macro abuse.");
1786 break;
1787 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001788 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001789
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 preproc->cleanup(0);
1791 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001792 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001793 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001794 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1795 }
1796}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001797
H. Peter Anvin70055962007-10-11 00:05:31 -07001798static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001799{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001800 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001802 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001803
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001804 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001806 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001807 q = strchr(buf, ']');
1808 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001809 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001811 /* stip off the comments */
1812 p = strchr(buf, ';');
1813 if (p) {
1814 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001815 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001816 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001817 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001818
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001819 /* no brace, no trailing spaces */
1820 *q = '\0';
1821 nasm_zap_spaces_rev(--q);
1822
1823 /* directive */
1824 p = nasm_skip_spaces(++buf);
1825 q = nasm_skip_word(p);
1826 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001827 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001828 *q = '\0';
1829 *directive = p;
1830
1831 /* and value finally */
1832 p = nasm_skip_spaces(++q);
1833 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001834
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001835 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001836}
1837
Ed Berosetfa771012002-06-09 20:56:40 +00001838/**
1839 * gnu style error reporting
1840 * This function prints an error message to error_file in the
1841 * style used by GNU. An example would be:
1842 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001843 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001844 * which the error occurs (or is detected) and "error:" is one of
1845 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001846 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001847 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001848 *
1849 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001850 * @param fmt the printf style format string
1851 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001852static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001853{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001854 char *currentfile = NULL;
1855 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001856
Ed Berosetfa771012002-06-09 20:56:40 +00001857 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001859
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001860 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001862
1863 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001864 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1865 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001866 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001867 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001868 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001869
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001870 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001871}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001872
Ed Berosetfa771012002-06-09 20:56:40 +00001873/**
1874 * MS style error reporting
1875 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001876 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001877 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001878 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001879 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001880 * which the error occurs (or is detected) and "error:" is one of
1881 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001882 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001883 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001884 *
1885 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001886 * @param fmt the printf style format string
1887 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001888static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001889{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001890 char *currentfile = NULL;
1891 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001892
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 if (is_suppressed_warning(severity))
1894 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001895
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001896 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001898
1899 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001900 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001902 } else {
1903 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001904 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001905
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001906 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001907}
1908
1909/**
1910 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001911 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001912 * not in pass 1
1913 *
1914 * @param severity the severity of the warning or error
1915 * @return true if we should abort error/warning printing
1916 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001917static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001918{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001919 /* Not a warning at all */
1920 if ((severity & ERR_MASK) != ERR_WARNING)
1921 return false;
1922
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001923 /* See if it's a pass-one only warning and we're not in pass one. */
1924 if (((severity & ERR_PASS1) && pass0 != 1) ||
1925 ((severity & ERR_PASS2) && pass0 != 2))
1926 return true;
1927
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001928 /* Might be a warning but suppresed explicitly */
1929 if (severity & ERR_WARN_MASK)
1930 return !warning_on[WARN_IDX(severity)];
1931 else
1932 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001933}
1934
1935/**
1936 * common error reporting
1937 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001938 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001939 * specific error message to error_file and may or may not return. It
1940 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001941 *
1942 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001943 * @param fmt the printf style format string
1944 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001945static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001946{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001947 char msg[1024];
1948 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001949
H. Peter Anvin7df04172008-06-10 18:27:38 -07001950 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001952 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953 break;
1954 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001955 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 break;
1957 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001958 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 break;
1960 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001961 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 break;
1963 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001964 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001966 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001967 pfx = "";
1968 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001969 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001970
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001971 vsnprintf(msg, sizeof msg, fmt, args);
1972
1973 fprintf(error_file, "%s%s\n", pfx, msg);
1974
1975 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001976 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001977
1978 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001979 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001980
1981 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 case ERR_DEBUG:
1983 /* no further action, by definition */
1984 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001985 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001986 /* Treat warnings as errors */
1987 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1988 terminate_after_phase = true;
1989 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001991 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 break;
1993 case ERR_FATAL:
1994 if (ofile) {
1995 fclose(ofile);
1996 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001997 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 }
1999 if (want_usage)
2000 usage();
2001 exit(1); /* instantly die */
2002 break; /* placate silly compilers */
2003 case ERR_PANIC:
2004 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002005 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002006 exit(3);
2007 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002008 }
2009}
2010
H. Peter Anvin734b1882002-04-30 21:01:08 +00002011static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002012{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002013 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002014}
2015
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002016static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002017{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002018 iflag_t r;
2019
2020 iflag_clear_all(&r);
2021
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002023 iflag_set(&r, IF_8086);
2024 else if (!strcmp(value, "186"))
2025 iflag_set(&r, IF_186);
2026 else if (!strcmp(value, "286"))
2027 iflag_set(&r, IF_286);
2028 else if (!strcmp(value, "386"))
2029 iflag_set(&r, IF_386);
2030 else if (!strcmp(value, "486"))
2031 iflag_set(&r, IF_486);
2032 else if (!strcmp(value, "586") ||
2033 !nasm_stricmp(value, "pentium"))
2034 iflag_set(&r, IF_PENT);
2035 else if (!strcmp(value, "686") ||
2036 !nasm_stricmp(value, "ppro") ||
2037 !nasm_stricmp(value, "pentiumpro") ||
2038 !nasm_stricmp(value, "p2"))
2039 iflag_set(&r, IF_P6);
2040 else if (!nasm_stricmp(value, "p3") ||
2041 !nasm_stricmp(value, "katmai"))
2042 iflag_set(&r, IF_KATMAI);
2043 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2044 !nasm_stricmp(value, "willamette"))
2045 iflag_set(&r, IF_WILLAMETTE);
2046 else if (!nasm_stricmp(value, "prescott"))
2047 iflag_set(&r, IF_PRESCOTT);
2048 else if (!nasm_stricmp(value, "x64") ||
2049 !nasm_stricmp(value, "x86-64"))
2050 iflag_set(&r, IF_X86_64);
2051 else if (!nasm_stricmp(value, "ia64") ||
2052 !nasm_stricmp(value, "ia-64") ||
2053 !nasm_stricmp(value, "itanium")||
2054 !nasm_stricmp(value, "itanic") ||
2055 !nasm_stricmp(value, "merced"))
2056 iflag_set(&r, IF_IA64);
2057 else {
2058 iflag_set(&r, IF_PLEVEL);
2059 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2060 "unknown 'cpu' type");
2061 }
2062 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002063}
2064
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002065static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002066{
2067 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002068
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 if ((i = atoi(value)) == 16)
2070 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002071 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002072 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002073 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 "cannot specify 32-bit segment on processor below a 386");
2075 i = 16;
2076 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002077 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002078 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002079 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002080 "cannot specify 64-bit segment on processor below an x86-64");
2081 i = 16;
2082 }
2083 if (i != maxbits) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002084 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002085 "%s output format does not support 64-bit code",
2086 ofmt->shortname);
2087 i = 16;
2088 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002089 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002090 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002091 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002092 value);
2093 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002094 }
2095 return i;
2096}