blob: 6b8dab1cbe3fc12bca56aa65aac0ee01f4641f44 [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"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000063
H. Peter Anvin31387b22010-07-15 18:28:52 -070064/*
65 * This is the maximum number of optimization passes to do. If we ever
66 * find a case where the optimizer doesn't naturally converge, we might
67 * have to drop this value so the assembler doesn't appear to just hang.
68 */
69#define MAX_OPTIMIZE (INT_MAX >> 1)
70
H. Peter Anvine2c80182005-01-15 22:15:51 +000071struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000072 int lineno;
73 int operand;
74};
75
Keith Kaniosa6dfa782007-04-13 16:47:53 +000076static int get_bits(char *value);
77static uint32_t get_cpu(char *cpu_str);
78static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070079static void assemble_file(char *, StrList **);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040080static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
81static void nasm_verror_vc(int severity, const char *fmt, va_list args);
82static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvin2b046cf2008-01-22 14:08:36 -080083static bool is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
85
John Coffman0efaec92002-05-26 19:20:08 +000086static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070087bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080088int pass0, passn;
Keith Kaniosb7a89542007-04-12 02:40:54 +000089int maxbits = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000090int globalrel = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000091
H. Peter Anvin9bd15062009-07-18 21:07:17 -040092static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080093
Keith Kaniosa6dfa782007-04-13 16:47:53 +000094static char inname[FILENAME_MAX];
95static char outname[FILENAME_MAX];
96static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -070097static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +000098static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000099/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400100struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400101struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400102const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000103
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000105
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400106FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700107int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
108static int sb, cmd_sb = 16; /* by default */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000109static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
110static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
Charles Craynec1905c22008-09-11 18:54:06 -0700111int64_t global_offset_changed; /* referenced in labels.c */
112int64_t prev_offset_changed;
113int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000114
H. Peter Anvin12e46512007-10-03 21:30:57 -0700115static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000117int32_t abs_seg; /* ABSOLUTE segment basis */
118int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000119
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000121
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700123static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400125static struct preproc_ops *preproc;
126
H. Peter Anvin620515a2002-04-30 20:57:38 +0000127enum op_type {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000128 op_normal, /* Preprocess and assemble */
129 op_preprocess, /* Preprocess only */
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700130 op_depend, /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000131};
132static enum op_type operating_mode;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700133/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700134static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700135static bool depend_missing_ok = false;
136static const char *depend_target = NULL;
137static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000138
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000139/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000140 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800141 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000142 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200143
H. Peter Anvin972079f2008-09-30 17:01:23 -0700144static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
145static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146
H. Peter Anvin972079f2008-09-30 17:01:23 -0700147static const struct warning {
148 const char *name;
149 const char *help;
150 bool enabled;
151} warnings[ERR_WARN_MAX+1] = {
152 {"error", "treat warnings as errors", false},
153 {"macro-params", "macro calls with wrong parameter count", true},
154 {"macro-selfref", "cyclic macro references", false},
155 {"macro-defaults", "macros with more default than optional parameters", true},
156 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700157 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700158 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
159 {"float-overflow", "floating point overflow", true},
160 {"float-denorm", "floating point denormal", false},
161 {"float-underflow", "floating point underflow", false},
162 {"float-toolong", "too many digits in floating-point number", true},
163 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800164 {"lock", "lock prefix on unlockable instructions", true},
165 {"hle", "invalid hle prefixes", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000166};
167
168/*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000169 * get/set current offset...
170 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000171#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000172 raa_read(offsets,location.segment))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000173#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000174 (void)(offsets=raa_write(offsets,location.segment,(x))))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000175
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700176static bool want_usage;
177static bool terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000178int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000179
H. Peter Anvin55340992012-09-09 17:09:00 -0700180static char *quote_for_make(const char *str);
181
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000182static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000183{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000184 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000185 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800186 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000187 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000188 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000189}
190
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800191/* Convert a struct tm to a POSIX-style time constant */
192static int64_t posix_mktime(struct tm *tm)
193{
194 int64_t t;
195 int64_t y = tm->tm_year;
196
197 /* See IEEE 1003.1:2004, section 4.14 */
198
199 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
200 t += tm->tm_yday;
201 t *= 24;
202 t += tm->tm_hour;
203 t *= 60;
204 t += tm->tm_min;
205 t *= 60;
206 t += tm->tm_sec;
207
208 return t;
209}
210
211static void define_macros_early(void)
212{
213 char temp[128];
214 struct tm lt, *lt_p, gm, *gm_p;
215 int64_t posix_time;
216
217 lt_p = localtime(&official_compile_time);
218 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400219 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800220
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400221 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
222 preproc->pre_define(temp);
223 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
224 preproc->pre_define(temp);
225 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
226 preproc->pre_define(temp);
227 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
228 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800229 }
230
231 gm_p = gmtime(&official_compile_time);
232 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400233 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800234
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400235 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
238 preproc->pre_define(temp);
239 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
240 preproc->pre_define(temp);
241 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
242 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800243 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700244
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800245 if (gm_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400246 posix_time = posix_mktime(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800247 else if (lt_p)
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400248 posix_time = posix_mktime(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800249 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400250 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800251
252 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400253 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
254 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800255 }
256}
257
258static void define_macros_late(void)
259{
260 char temp[128];
261
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400262 /*
263 * In case if output format is defined by alias
264 * we have to put shortname of the alias itself here
265 * otherwise ABI backward compatibility gets broken.
266 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400267 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400268 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400269 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800270}
271
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700272static void emit_dependencies(StrList *list)
273{
274 FILE *deps;
275 int linepos, len;
276 StrList *l, *nl;
277
278 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400279 deps = fopen(depend_file, "w");
280 if (!deps) {
281 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
282 "unable to write dependency file `%s'", depend_file);
283 return;
284 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700285 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400286 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700287 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700288
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700289 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400290 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700291 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400292 len = strlen(file);
293 if (linepos + len > 62 && linepos > 1) {
294 fprintf(deps, " \\\n ");
295 linepos = 1;
296 }
297 fprintf(deps, " %s", file);
298 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700299 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700300 }
301 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700302
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400303 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400304 if (depend_emit_phony)
305 fprintf(deps, "%s:\n\n", l->str);
306 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700307 }
308
309 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400310 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700311}
312
H. Peter Anvin038d8612007-04-12 16:54:50 +0000313int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000314{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700315 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700316
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800317 time(&official_compile_time);
318
Charles Crayne2581c862008-09-10 19:21:52 -0700319 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700320 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400321 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000322
H. Peter Anvin97e15752007-09-25 08:47:47 -0700323 error_file = stderr;
324
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700325 tolower_init();
326
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400327 nasm_init_malloc_error();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000328 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000329 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000330
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000332 operating_mode = op_normal;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000333
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000334 seg_init();
335
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800336 /* Define some macros dependent on the runtime, but not
337 on the command line. */
338 define_macros_early();
339
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000340 parse_cmdline(argc, argv);
341
H. Peter Anvine2c80182005-01-15 22:15:51 +0000342 if (terminate_after_phase) {
343 if (want_usage)
344 usage();
345 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346 }
347
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000348 /* If debugging info is disabled, suppress any debug calls */
349 if (!using_debug_info)
350 ofmt->current_dfmt = &null_debug_form;
351
H. Peter Anvin76690a12002-04-30 20:52:49 +0000352 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400353 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700354 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000356
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000357 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800358 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000359
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400360 depend_ptr = (depend_file || (operating_mode == op_depend)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700361 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400362 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700363
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 switch (operating_mode) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000365 case op_depend:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000367 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700368
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400369 if (depend_missing_ok)
370 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700371
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700372 preproc->reset(inname, 0, &nasmlist, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000373 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400374 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 while ((line = preproc->getline()))
377 nasm_free(line);
378 preproc->cleanup(0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 }
380 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000381
382 case op_preprocess:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000384 char *line;
385 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000386 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000388
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 if (*outname) {
390 ofile = fopen(outname, "w");
391 if (!ofile)
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400392 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 "unable to open output file `%s'",
394 outname);
395 } else
396 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000397
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700398 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000399
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400400 /* pass = 1; */
H. Peter Anvindbb640b2009-07-18 18:57:16 -0700401 preproc->reset(inname, 3, &nasmlist, depend_ptr);
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400402 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700403
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 while ((line = preproc->getline())) {
405 /*
406 * We generate %line directives if needed for later programs
407 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000408 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 int altline = src_get(&linnum, &file_name);
410 if (altline) {
411 if (altline == 1 && lineinc == 1)
412 nasm_fputs("", ofile);
413 else {
414 lineinc = (altline != -1 || lineinc != 1);
415 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000416 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 file_name);
418 }
419 prior_linnum = linnum;
420 }
421 nasm_fputs(line, ofile);
422 nasm_free(line);
423 }
424 nasm_free(file_name);
425 preproc->cleanup(0);
426 if (ofile)
427 fclose(ofile);
428 if (ofile && terminate_after_phase)
429 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400430 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 }
432 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000433
434 case op_normal:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 {
436 /*
437 * We must call ofmt->filename _anyway_, even if the user
438 * has specified their own output file, because some
439 * formats (eg OBJ and COFF) use ofmt->filename to find out
440 * the name of the input file and then put that inside the
441 * file.
442 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400443 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444
H. Peter Anvin0cba1072009-07-05 14:45:12 -0700445 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 if (!ofile) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400447 nasm_error(ERR_FATAL | ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 "unable to open output file `%s'", outname);
449 }
450
451 /*
452 * We must call init_labels() before ofmt->init() since
453 * some object formats will want to define labels in their
454 * init routines. (eg OS/2 defines the FLAT group)
455 */
456 init_labels();
457
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400458 ofmt->init();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400459 dfmt = ofmt->current_dfmt;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400460 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700462 assemble_file(inname, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463
464 if (!terminate_after_phase) {
465 ofmt->cleanup(using_debug_info);
466 cleanup_labels();
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400467 fflush(ofile);
468 if (ferror(ofile)) {
469 nasm_error(ERR_NONFATAL|ERR_NOFILE,
470 "write error on output file `%s'", outname);
471 }
472 }
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200473
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400474 if (ofile) {
475 fclose(ofile);
476 if (terminate_after_phase)
477 remove(outname);
478 ofile = NULL;
479 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 }
481 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000482 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000483
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700484 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400485 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700486
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000487 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000489
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 raa_free(offsets);
491 saa_free(forwrefs);
492 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000493 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000494
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700495 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000496}
497
H. Peter Anvineba20a72002-04-30 20:53:55 +0000498/*
499 * Get a parameter for a command line option.
500 * First arg must be in the form of e.g. -f...
501 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800502static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800504 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400505 if (p[2]) /* the parameter's in the option */
506 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800508 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000510 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400511 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000513 return NULL;
514}
515
H. Peter Anvindc242712007-11-18 11:55:10 -0800516/*
517 * Copy a filename
518 */
519static void copy_filename(char *dst, const char *src)
520{
521 size_t len = strlen(src);
522
523 if (len >= (size_t)FILENAME_MAX) {
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400524 nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
525 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800526 }
527 strncpy(dst, src, FILENAME_MAX);
528}
529
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700530/*
531 * Convert a string to Make-safe form
532 */
533static char *quote_for_make(const char *str)
534{
535 const char *p;
536 char *os, *q;
537
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400538 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700539 size_t nbs = 0;
540
541 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400542 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700543
544 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400545 switch (*p) {
546 case ' ':
547 case '\t':
548 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
549 n += nbs + 2;
550 nbs = 0;
551 break;
552 case '$':
553 case '#':
554 nbs = 0;
555 n += 2;
556 break;
557 case '\\':
558 nbs++;
559 n++;
560 break;
561 default:
562 nbs = 0;
563 n++;
564 break;
565 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700566 }
567
568 /* Convert N backslashes at the end of filename to 2N backslashes */
569 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400570 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700571
572 os = q = nasm_malloc(n);
573
574 nbs = 0;
575 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400576 switch (*p) {
577 case ' ':
578 case '\t':
579 while (nbs--)
580 *q++ = '\\';
581 *q++ = '\\';
582 *q++ = *p;
583 break;
584 case '$':
585 *q++ = *p;
586 *q++ = *p;
587 nbs = 0;
588 break;
589 case '#':
590 *q++ = '\\';
591 *q++ = *p;
592 nbs = 0;
593 break;
594 case '\\':
595 *q++ = *p;
596 nbs++;
597 break;
598 default:
599 *q++ = *p;
600 nbs = 0;
601 break;
602 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700603 }
604 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400605 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700606
607 *q = '\0';
608
609 return os;
610}
611
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000613 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000615};
616
617#define OPT_PREFIX 0
618#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619struct textargs textopts[] = {
620 {"prefix", OPT_PREFIX},
621 {"postfix", OPT_POSTFIX},
622 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000623};
624
H. Peter Anvin423e3812007-11-15 17:12:29 -0800625static bool stopoptions = false;
626static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000628 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800629 int i;
630 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700631 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000632
633 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800634 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000635
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400637 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
638 /* These parameters take values */
639 if (!(param = get_param(p, q, &advance)))
640 return advance;
641 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800642
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 switch (p[1]) {
644 case 's':
645 error_file = stdout;
646 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700647
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400648 case 'o': /* output file */
649 copy_filename(outname, param);
650 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700651
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400652 case 'f': /* output format */
653 ofmt = ofmt_find(param, &ofmt_alias);
654 if (!ofmt) {
655 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
656 "unrecognised output format `%s' - "
657 "use -hf for a list", param);
658 }
659 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700660
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400661 case 'O': /* Optimization level */
662 {
663 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700664
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400665 if (!*param) {
666 /* Naked -O == -Ox */
667 optimizing = MAX_OPTIMIZE;
668 } else {
669 while (*param) {
670 switch (*param) {
671 case '0': case '1': case '2': case '3': case '4':
672 case '5': case '6': case '7': case '8': case '9':
673 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700674
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400675 /* -O0 -> optimizing == -1, 0.98 behaviour */
676 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
677 if (opt < 2)
678 optimizing = opt - 1;
679 else
680 optimizing = opt;
681 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700682
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400683 case 'v':
684 case '+':
685 param++;
686 opt_verbose_info = true;
687 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700688
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400689 case 'x':
690 param++;
691 optimizing = MAX_OPTIMIZE;
692 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700693
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400694 default:
695 nasm_error(ERR_FATAL,
696 "unknown optimization option -O%c\n",
697 *param);
698 break;
699 }
700 }
701 if (optimizing > MAX_OPTIMIZE)
702 optimizing = MAX_OPTIMIZE;
703 }
704 break;
705 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800706
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400707 case 'p': /* pre-include */
708 case 'P':
709 preproc->pre_include(param);
710 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800711
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400712 case 'd': /* pre-define */
713 case 'D':
714 preproc->pre_define(param);
715 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800716
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400717 case 'u': /* un-define */
718 case 'U':
719 preproc->pre_undefine(param);
720 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800721
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400722 case 'i': /* include search path */
723 case 'I':
724 preproc->include_path(param);
725 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800726
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400727 case 'l': /* listing file */
728 copy_filename(listname, param);
729 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800730
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400731 case 'Z': /* error messages file */
732 copy_filename(errname, param);
733 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800734
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400735 case 'F': /* specify debug format */
736 ofmt->current_dfmt = dfmt_find(ofmt, param);
737 if (!ofmt->current_dfmt) {
738 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
739 "unrecognized debug format `%s' for"
740 " output format `%s'",
741 param, ofmt->shortname);
742 }
743 using_debug_info = true;
744 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800745
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400746 case 'X': /* specify error reporting format */
747 if (nasm_stricmp("vc", param) == 0)
748 nasm_set_verror(nasm_verror_vc);
749 else if (nasm_stricmp("gnu", param) == 0)
750 nasm_set_verror(nasm_verror_gnu);
751 else
752 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
753 "unrecognized error reporting format `%s'",
754 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800756
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700758 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800760
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 case 'h':
762 printf
763 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
764 "[-l listfile]\n"
765 " [options...] [--] filename\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700766 " or nasm -v for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400768 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700770 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700772 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400773 " -MG d:o, missing files assumed generated\n"
774 " -MF <file> set Makefile dependency file\n"
775 " -MD <file> assemble and generate dependencies\n"
776 " -MT <file> dependency target name\n"
777 " -MQ <file> dependency target name (quoted)\n"
778 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700779 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 " -s redirect error messages to stdout\n\n"
781 " -F format select a debugging format\n\n"
782 " -I<path> adds a pathname to the include file path\n");
783 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700784 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400785 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400786 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400787 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 " -P<file> pre-includes a file\n"
789 " -D<macro>[=<value>] pre-defines a macro\n"
790 " -U<macro> undefines a macro\n"
791 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800792 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400793 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
794 "--prefix,--postfix\n"
795 " this options prepend or append the given argument to all\n"
796 " extern and global variables\n\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800797 "Warnings:\n");
798 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700800 warnings[i].name, warnings[i].help,
801 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 printf
803 ("\nresponse files should contain command line parameters"
804 ", one per line.\n");
805 if (p[2] == 'f') {
806 printf("\nvalid output formats for -f are"
807 " (`*' denotes default):\n");
808 ofmt_list(ofmt, stdout);
809 } else {
810 printf("\nFor a list of valid output formats, use -hf.\n");
811 printf("For a list of debug formats, use -f <form> -y.\n");
812 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400813 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800815
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 case 'y':
817 printf("\nvalid debug formats for '%s' output format are"
818 " ('*' denotes default):\n", ofmt->shortname);
819 dfmt_list(ofmt, stdout);
820 exit(0);
821 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800822
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700824 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800826
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 case 'v':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400828 printf("NASM version %s compiled on %s%s\n",
829 nasm_version, nasm_date, nasm_compile_options);
830 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800832
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400833 case 'e': /* preprocess only */
834 case 'E':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 operating_mode = op_preprocess;
836 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800837
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400838 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400839 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400842 case 'W':
843 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
844 do_warn = false;
845 param += 3;
846 } else {
847 do_warn = true;
848 }
849 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800850
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800852 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400853 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400855 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400857 do_warn = (param[0] == '+');
858 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400859
860set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400861 for (i = 0; i <= ERR_WARN_MAX; i++) {
862 if (!nasm_stricmp(param, warnings[i].name))
863 break;
864 }
865 if (i <= ERR_WARN_MAX) {
866 warning_on_global[i] = do_warn;
867 } else if (!nasm_stricmp(param, "all")) {
868 for (i = 1; i <= ERR_WARN_MAX; i++)
869 warning_on_global[i] = do_warn;
870 } else if (!nasm_stricmp(param, "none")) {
871 for (i = 1; i <= ERR_WARN_MAX; i++)
872 warning_on_global[i] = !do_warn;
873 } else {
874 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
875 "invalid warning `%s'", param);
876 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800878
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400880 switch (p[2]) {
881 case 0:
882 operating_mode = op_depend;
883 break;
884 case 'G':
885 operating_mode = op_depend;
886 depend_missing_ok = true;
887 break;
888 case 'P':
889 depend_emit_phony = true;
890 break;
891 case 'D':
892 depend_file = q;
893 advance = true;
894 break;
895 case 'T':
896 depend_target = q;
897 advance = true;
898 break;
899 case 'Q':
900 depend_target = quote_for_make(q);
901 advance = true;
902 break;
903 default:
904 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
905 "unknown dependency option `-M%c'", p[2]);
906 break;
907 }
908 if (advance && (!q || !q[0])) {
909 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
910 "option `-M%c' requires a parameter", p[2]);
911 break;
912 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000914
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 case '-':
916 {
917 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000918
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 if (p[2] == 0) { /* -- => stop processing options */
920 stopoptions = 1;
921 break;
922 }
923 for (s = 0; textopts[s].label; s++) {
924 if (!nasm_stricmp(p + 2, textopts[s].label)) {
925 break;
926 }
927 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000928
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 switch (s) {
930
931 case OPT_PREFIX:
932 case OPT_POSTFIX:
933 {
934 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400935 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 ERR_USAGE,
937 "option `--%s' requires an argument",
938 p + 2);
939 break;
940 } else {
941 advance = 1, param = q;
942 }
943
944 if (s == OPT_PREFIX) {
945 strncpy(lprefix, param, PREFIX_MAX - 1);
946 lprefix[PREFIX_MAX - 1] = 0;
947 break;
948 }
949 if (s == OPT_POSTFIX) {
950 strncpy(lpostfix, param, POSTFIX_MAX - 1);
951 lpostfix[POSTFIX_MAX - 1] = 0;
952 break;
953 }
954 break;
955 }
956 default:
957 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400958 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 "unrecognised option `--%s'", p + 2);
960 break;
961 }
962 }
963 break;
964 }
965
966 default:
967 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400968 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 "unrecognised option `-%c'", p[1]);
970 break;
971 }
972 } else {
973 if (*inname) {
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 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -0800976 } else {
977 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400978 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000979 }
980
981 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000982}
983
H. Peter Anvineba20a72002-04-30 20:53:55 +0000984#define ARG_BUF_DELTA 128
985
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000987{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000988 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000989 int bufsize, prevargsize;
990
991 bufsize = prevargsize = ARG_BUF_DELTA;
992 buffer = nasm_malloc(ARG_BUF_DELTA);
993 prevarg = nasm_malloc(ARG_BUF_DELTA);
994 prevarg[0] = '\0';
995
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 p = buffer;
998 while (1) { /* Loop to handle long lines */
999 q = fgets(p, bufsize - (p - buffer), rfile);
1000 if (!q)
1001 break;
1002 p += strlen(p);
1003 if (p > buffer && p[-1] == '\n')
1004 break;
1005 if (p - buffer > bufsize - 10) {
1006 int offset;
1007 offset = p - buffer;
1008 bufsize += ARG_BUF_DELTA;
1009 buffer = nasm_realloc(buffer, bufsize);
1010 p = buffer + offset;
1011 }
1012 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 if (!q && p == buffer) {
1015 if (prevarg[0])
1016 process_arg(prevarg, NULL);
1017 nasm_free(buffer);
1018 nasm_free(prevarg);
1019 return;
1020 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 /*
1023 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1024 * them are present at the end of the line.
1025 */
1026 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001027
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001028 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001030
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001031 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001032
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 if (process_arg(prevarg, p))
1034 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001035
Charles Crayne192d5b52007-10-18 19:02:42 -07001036 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 prevargsize += ARG_BUF_DELTA;
1038 prevarg = nasm_realloc(prevarg, prevargsize);
1039 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001040 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001041 }
1042}
1043
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001044/* Function to process args from a string of args, rather than the
1045 * argv array. Used by the environment variable and response file
1046 * processing.
1047 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001048static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001050 char *p, *q, *arg, *prevarg;
1051 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001052
1053 p = args;
1054 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001056 arg = NULL;
1057 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 q = p;
1059 while (*p && *p != separator)
1060 p++;
1061 while (*p == separator)
1062 *p++ = '\0';
1063 prevarg = arg;
1064 arg = q;
1065 if (process_arg(prevarg, arg))
1066 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001067 }
1068 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001070}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001071
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001072static void process_response_file(const char *file)
1073{
1074 char str[2048];
1075 FILE *f = fopen(file, "r");
1076 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001077 perror(file);
1078 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001079 }
1080 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001081 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001082 }
1083 fclose(f);
1084}
1085
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001086static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001087{
1088 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001089 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001090 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001091
Charles Craynefcce07f2007-09-30 22:15:36 -07001092 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001093
H. Peter Anvin972079f2008-09-30 17:01:23 -07001094 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001095 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001096
1097 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001098 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001099 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001100 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001101 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 envcopy = nasm_strdup(envreal);
1103 process_args(envcopy);
1104 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001105 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001106
1107 /*
1108 * Now process the actual command line.
1109 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001111 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 argv++;
1113 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001114 /*
1115 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 * arguments like the environment variable. This allows us
1117 * to have multiple arguments on a single line, which is
1118 * different to the -@resp file processing below for regular
1119 * NASM.
1120 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001121 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 argc--;
1123 argv++;
1124 }
1125 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001126 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001127 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001128 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001129 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 process_respfile(rfile);
1131 fclose(rfile);
1132 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001133 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 "unable to open response file `%s'", p);
1135 }
1136 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001137 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1138 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001139 }
1140
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001141 /*
1142 * Look for basic command line typos. This definitely doesn't
1143 * catch all errors, but it might help cases of fumbled fingers.
1144 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001145 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001146 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001147 "no input file specified");
1148 else if (!strcmp(inname, errname) ||
1149 !strcmp(inname, outname) ||
1150 !strcmp(inname, listname) ||
1151 (depend_file && !strcmp(inname, depend_file)))
1152 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1153 "file `%s' is both input and output file",
1154 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001155
H. Peter Anvin70653092007-10-19 14:42:29 -07001156 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001157 error_file = fopen(errname, "w");
1158 if (!error_file) {
1159 error_file = stderr; /* Revert to default! */
1160 nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
1161 "cannot open file `%s' for error messages",
1162 errname);
1163 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001164 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001165}
1166
H. Peter Anvin70055962007-10-11 00:05:31 -07001167static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001168
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001169static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001170{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001171 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001173 int i, validid;
1174 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001175 int32_t seg;
1176 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001177 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001179 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001180
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001181 if (cmd_sb == 32 && cmd_cpu < IF_386)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001182 nasm_error(ERR_FATAL, "command line: "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001184
Charles Craynec1905c22008-09-11 18:54:06 -07001185 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001186 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 int pass1, pass2;
1188 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001189
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001190 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001191 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1192 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001193
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001194 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001195
Keith Kaniosb7a89542007-04-12 02:40:54 +00001196 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 cpu = cmd_cpu;
1198 if (pass0 == 2) {
1199 if (*listname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001200 nasmlist.init(listname, nasm_error);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001202 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001203 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001205 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001206 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 saa_rewind(forwrefs);
1208 forwref = saa_rstruct(forwrefs);
1209 raa_free(offsets);
1210 offsets = raa_init();
1211 }
H. Peter Anvindbb640b2009-07-18 18:57:16 -07001212 preproc->reset(fname, pass1, &nasmlist,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001213 pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001214 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001215
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001217 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001218 location.known = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 location.offset = offs = GET_CURR_OFFS;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001220
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001222 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001224
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001225 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001226 * Here we parse our directives; this is not handled by the
1227 * 'real' parser. This really should be a separate function.
1228 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001230 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001231 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001232 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001233
H. Peter Anvin70055962007-10-11 00:05:31 -07001234 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001235 case D_SEGMENT: /* [SEGMENT n] */
1236 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 seg = ofmt->section(value, pass2, &sb);
1238 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001239 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001240 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 value);
1242 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001243 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 location.segment = seg;
1245 }
1246 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001247 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001248 if (*value) {
1249 stdscan_reset();
1250 stdscan_set(value);
1251 tokval.t_type = TOKEN_INVALID;
1252 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, nasm_error, NULL);
1253 if (e) {
1254 unsigned int align = (unsigned int)e->value;
1255 if ((uint64_t)e->value > 0x7fffffff) {
1256 /*
1257 * FIXME: Please make some sane message here
1258 * ofmt should have some 'check' method which
1259 * would report segment alignment bounds.
1260 */
1261 nasm_error(ERR_FATAL,
1262 "incorrect segment alignment `%s'", value);
1263 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001264 nasm_error(ERR_NONFATAL,
1265 "segment alignment `%s' is not power of two",
1266 value);
1267 }
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001268 /* callee should be able to handle all details */
Cyrill Gorcunov2ef5c272010-04-21 13:45:32 +04001269 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001270 }
1271 }
1272 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001273 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 if (*value == '$')
1275 value++; /* skip initial $ if present */
1276 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001277 q = value;
1278 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001280 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 *q++ = '\0';
1282 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001283 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001284 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001285 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001286 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001287 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001288 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001289 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001291 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001293 }
1294 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001295 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 "identifier expected after EXTERN");
1297 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001298 }
1299 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 *q++ = '\0';
1301 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001302 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 special = NULL;
1304 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1305 int temp = pass0;
1306 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001307 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001309 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001311 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 } /* else pass0 == 1 */
1313 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001314 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001315 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001317 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 if (*value == '$')
1319 value++; /* skip initial $ if present */
1320 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001321 q = value;
1322 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001324 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 *q++ = '\0';
1326 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001330 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001331 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001332 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001333 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001335 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001337 }
1338 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001339 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 "identifier expected after GLOBAL");
1341 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342 }
1343 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 *q++ = '\0';
1345 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001346 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001348 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 } /* pass == 1 */
1350 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001351 case D_COMMON: /* [COMMON symbol size:special] */
1352 {
1353 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001354
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 if (*value == '$')
1356 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001357 p = value;
1358 validid = true;
1359 if (!isidstart(*p))
1360 validid = false;
1361 while (*p && !nasm_isspace(*p)) {
1362 if (!isidchar(*p))
1363 validid = false;
1364 p++;
1365 }
1366 if (!validid) {
1367 nasm_error(ERR_NONFATAL,
1368 "identifier expected after COMMON");
1369 break;
1370 }
1371 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001372 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001373 q = p;
1374 while (*q && *q != ':')
1375 q++;
1376 if (*q == ':') {
1377 *q++ = '\0';
1378 special = q;
1379 } else {
1380 special = NULL;
1381 }
1382 size = readnum(p, &rn_error);
1383 if (rn_error) {
1384 nasm_error(ERR_NONFATAL,
1385 "invalid size specified"
1386 " in COMMON declaration");
1387 break;
1388 }
1389 } else {
1390 nasm_error(ERR_NONFATAL,
1391 "no size specified in"
1392 " COMMON declaration");
1393 break;
1394 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001396 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001397 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001398 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001399 if (special)
1400 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 }
1402 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001403 }
1404 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001406 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 tokval.t_type = TOKEN_INVALID;
1408 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001409 nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 if (e) {
1411 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001412 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 1 ? ERR_NONFATAL : ERR_PANIC,
1414 "cannot use non-relocatable expression as "
1415 "ABSOLUTE address");
1416 else {
1417 abs_seg = reloc_seg(e);
1418 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001419 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001420 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 abs_offset = 0x100; /* don't go near zero in case of / */
1422 else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001423 nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001425 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 location.segment = NO_SEG;
1427 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001428 case D_DEBUG: /* [DEBUG] */
1429 {
1430 char debugid[128];
1431 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001432
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 p = value;
1434 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001435 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001436 if (!isidstart(*p)) {
1437 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001438 } else {
1439 while (*p && !nasm_isspace(*p)) {
1440 if (q >= debugid + sizeof debugid - 1) {
1441 overlong = true;
1442 break;
1443 }
1444 if (!isidchar(*p))
1445 badid = true;
1446 *q++ = *p++;
1447 }
1448 *q = 0;
1449 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001450 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001451 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1452 "identifier expected after DEBUG");
1453 break;
1454 }
1455 if (overlong) {
1456 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1457 "DEBUG identifier too long");
1458 break;
1459 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001460 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001461 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001462 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001464 }
1465 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001466 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001467 switch(*value) {
1468 case '-': validid = 0; value++; break;
1469 case '+': validid = 1; value++; break;
1470 case '*': validid = 2; value++; break;
1471 default: validid = 1; break;
1472 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001474 for (i = 1; i <= ERR_WARN_MAX; i++)
1475 if (!nasm_stricmp(value, warnings[i].name))
1476 break;
1477 if (i <= ERR_WARN_MAX) {
1478 switch(validid) {
1479 case 0:
1480 warning_on[i] = false;
1481 break;
1482 case 1:
1483 warning_on[i] = true;
1484 break;
1485 case 2:
1486 warning_on[i] = warning_on_global[i];
1487 break;
1488 }
1489 } else
1490 nasm_error(ERR_NONFATAL,
1491 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001493 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 cpu = get_cpu(value);
1495 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001496 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001497 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 if (*value == '+') {
1499 user_nolist = 0;
1500 } else {
1501 if (*value == '-') {
1502 user_nolist = 1;
1503 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001504 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 }
1506 }
1507 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001508 case D_DEFAULT: /* [DEFAULT] */
1509 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001510 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001511 tokval.t_type = TOKEN_INVALID;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001512 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1513 switch ((int)tokval.t_integer) {
1514 case S_REL:
1515 globalrel = 1;
1516 break;
1517 case S_ABS:
1518 globalrel = 0;
1519 break;
1520 default:
1521 err = 1;
1522 break;
1523 }
1524 } else {
1525 err = 1;
1526 }
1527 break;
1528 case D_FLOAT:
1529 if (float_option(value)) {
1530 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1531 "unknown 'float' directive: %s",
1532 value);
1533 }
1534 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001536 if (ofmt->directive(d, value, pass2))
1537 break;
1538 /* else fall through */
1539 case D_unknown:
1540 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1541 "unrecognised directive [%s]",
1542 directive);
1543 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001545 if (err) {
1546 nasm_error(ERR_NONFATAL,
1547 "invalid parameter to [%s] directive",
1548 directive);
1549 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001551 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552
Charles Crayne2581c862008-09-10 19:21:52 -07001553 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001555 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001557 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 forwref = saa_rstruct(forwrefs);
1559 } while (forwref != NULL
1560 && forwref->lineno == globallineno);
1561 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001562 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001563
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001564 if (output_ins.forw_ref) {
1565 if (passn == 1) {
1566 for (i = 0; i < output_ins.operands; i++) {
1567 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1568 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1569 fwinf->lineno = globallineno;
1570 fwinf->operand = i;
1571 }
1572 }
1573 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001574 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001576
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 /* forw_ref */
1578 if (output_ins.opcode == I_EQU) {
1579 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001580 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 * Special `..' EQUs get processed in pass two,
1582 * except `..@' macro-processor EQUs which are done
1583 * in the normal place.
1584 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001585 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001586 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001588
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001589 else if (output_ins.label[0] != '.' ||
1590 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 output_ins.label[2] == '@') {
1592 if (output_ins.operands == 1 &&
1593 (output_ins.oprs[0].type & IMMEDIATE) &&
1594 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001595 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 def_label(output_ins.label,
1597 output_ins.oprs[0].segment,
1598 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001599 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001601 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001603 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001605 && (output_ins.oprs[1].type & IMMEDIATE)
1606 && output_ins.oprs[1].segment == NO_SEG
1607 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001609 output_ins.oprs[0].offset | SEG_ABS,
1610 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001611 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001613 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001615 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001616 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001617 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 * Special `..' EQUs get processed here, except
1619 * `..@' macro processor EQUs which are done above.
1620 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 output_ins.label[1] == '.' &&
1623 output_ins.label[2] != '@') {
1624 if (output_ins.operands == 1 &&
1625 (output_ins.oprs[0].type & IMMEDIATE)) {
1626 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001627 output_ins.oprs[0].segment,
1628 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001629 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001631 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001633 && output_ins.oprs[0].segment == NO_SEG
1634 && (output_ins.oprs[1].type & IMMEDIATE)
1635 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001637 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001638 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001639 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001641 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001643 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001644 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001646
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001648
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001649 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001650 &output_ins, nasm_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001651
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 /* if (using_debug_info) && output_ins.opcode != -1) */
1653 if (using_debug_info)
1654 { /* fbk 03/25/01 */
1655 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001656 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 TYS_ELEMENTS(output_ins.operands);
1658 switch (output_ins.opcode) {
1659 case I_RESB:
1660 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001661 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 break;
1663 case I_RESW:
1664 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001665 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 break;
1667 case I_RESD:
1668 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001669 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 break;
1671 case I_RESQ:
1672 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001673 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 break;
1675 case I_REST:
1676 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001677 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001679 case I_RESO:
1680 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001681 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001682 break;
1683 case I_RESY:
1684 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001685 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001686 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 case I_DB:
1688 typeinfo |= TY_BYTE;
1689 break;
1690 case I_DW:
1691 typeinfo |= TY_WORD;
1692 break;
1693 case I_DD:
1694 if (output_ins.eops_float)
1695 typeinfo |= TY_FLOAT;
1696 else
1697 typeinfo |= TY_DWORD;
1698 break;
1699 case I_DQ:
1700 typeinfo |= TY_QWORD;
1701 break;
1702 case I_DT:
1703 typeinfo |= TY_TBYTE;
1704 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001705 case I_DO:
1706 typeinfo |= TY_OWORD;
1707 break;
1708 case I_DY:
1709 typeinfo |= TY_YWORD;
1710 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 default:
1712 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001713
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001715
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001716 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001717 }
1718 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 offs += l;
1720 SET_CURR_OFFS(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001721 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001722 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 * else l == -1 => invalid instruction, which will be
1724 * flagged as an error on pass 2
1725 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001726
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001727 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001729 &output_ins, ofmt, nasm_error,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 &nasmlist);
1731 SET_CURR_OFFS(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001732
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 }
1734 } /* not an EQU */
1735 cleanup_insn(&output_ins);
1736 }
1737 nasm_free(line);
1738 location.offset = offs = GET_CURR_OFFS;
1739 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001740
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001741 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001742 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001743 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001744
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 if (pass1 == 1)
1746 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001747
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001748 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001750 } else if (global_offset_changed &&
1751 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001752 prev_offset_changed = global_offset_changed;
1753 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001754 } else {
1755 stall_count++;
1756 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001757
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001758 if (terminate_after_phase)
1759 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001760
1761 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001762 /* We get here if the labels don't converge
1763 * Example: FOO equ FOO + 1
1764 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001765 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001766 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001767 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001768 nasm_error(ERR_NONFATAL,
1769 "Possible causes: recursive EQUs, macro abuse.");
1770 break;
1771 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001772 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001773
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 preproc->cleanup(0);
1775 nasmlist.cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001776 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001777 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001778 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1779 }
1780}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001781
H. Peter Anvin70055962007-10-11 00:05:31 -07001782static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001783{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001784 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001786 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001787
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001788 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001790 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001791 q = strchr(buf, ']');
1792 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001793 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001794
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001795 /* stip off the comments */
1796 p = strchr(buf, ';');
1797 if (p) {
1798 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001799 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001800 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001801 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001802
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001803 /* no brace, no trailing spaces */
1804 *q = '\0';
1805 nasm_zap_spaces_rev(--q);
1806
1807 /* directive */
1808 p = nasm_skip_spaces(++buf);
1809 q = nasm_skip_word(p);
1810 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001811 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001812 *q = '\0';
1813 *directive = p;
1814
1815 /* and value finally */
1816 p = nasm_skip_spaces(++q);
1817 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001818
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001819 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001820}
1821
Ed Berosetfa771012002-06-09 20:56:40 +00001822/**
1823 * gnu style error reporting
1824 * This function prints an error message to error_file in the
1825 * style used by GNU. An example would be:
1826 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001827 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001828 * which the error occurs (or is detected) and "error:" is one of
1829 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001830 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001831 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001832 *
1833 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001834 * @param fmt the printf style format string
1835 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001836static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001837{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001838 char *currentfile = NULL;
1839 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001840
Ed Berosetfa771012002-06-09 20:56:40 +00001841 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001843
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001844 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001846
1847 if (currentfile) {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001848 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1849 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001850 } else {
Cyrill Gorcunov04dba652013-02-15 02:21:07 +04001851 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001852 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001853
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001854 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001855}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001856
Ed Berosetfa771012002-06-09 20:56:40 +00001857/**
1858 * MS style error reporting
1859 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001860 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001861 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001862 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001863 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001864 * which the error occurs (or is detected) and "error:" is one of
1865 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001866 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001867 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001868 *
1869 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001870 * @param fmt the printf style format string
1871 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001872static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001873{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001874 char *currentfile = NULL;
1875 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001876
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 if (is_suppressed_warning(severity))
1878 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001879
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001880 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001882
1883 if (currentfile) {
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001884 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 nasm_free(currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001886 } else {
1887 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001888 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001889
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001890 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001891}
1892
1893/**
1894 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001895 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001896 * not in pass 1
1897 *
1898 * @param severity the severity of the warning or error
1899 * @return true if we should abort error/warning printing
1900 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001901static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001902{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001903 /* Not a warning at all */
1904 if ((severity & ERR_MASK) != ERR_WARNING)
1905 return false;
1906
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001907 /* See if it's a pass-one only warning and we're not in pass one. */
1908 if (((severity & ERR_PASS1) && pass0 != 1) ||
1909 ((severity & ERR_PASS2) && pass0 != 2))
1910 return true;
1911
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001912 /* Might be a warning but suppresed explicitly */
1913 if (severity & ERR_WARN_MASK)
1914 return !warning_on[WARN_IDX(severity)];
1915 else
1916 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001917}
1918
1919/**
1920 * common error reporting
1921 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001922 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001923 * specific error message to error_file and may or may not return. It
1924 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001925 *
1926 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001927 * @param fmt the printf style format string
1928 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001929static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001930{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001931 char msg[1024];
1932 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001933
H. Peter Anvin7df04172008-06-10 18:27:38 -07001934 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001936 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 break;
1938 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001939 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 break;
1941 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001942 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 break;
1944 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001945 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 break;
1947 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001948 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001950 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001951 pfx = "";
1952 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001953 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001954
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001955 vsnprintf(msg, sizeof msg, fmt, args);
1956
1957 fprintf(error_file, "%s%s\n", pfx, msg);
1958
1959 if (*listname)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001960 nasmlist.error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001961
1962 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001963 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001964
1965 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 case ERR_DEBUG:
1967 /* no further action, by definition */
1968 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001969 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001970 /* Treat warnings as errors */
1971 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1972 terminate_after_phase = true;
1973 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001975 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 break;
1977 case ERR_FATAL:
1978 if (ofile) {
1979 fclose(ofile);
1980 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001981 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 }
1983 if (want_usage)
1984 usage();
1985 exit(1); /* instantly die */
1986 break; /* placate silly compilers */
1987 case ERR_PANIC:
1988 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001989 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 exit(3);
1991 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001992 }
1993}
1994
H. Peter Anvin734b1882002-04-30 21:01:08 +00001995static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001996{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001997 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001998}
1999
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002000static uint32_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002001{
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 if (!strcmp(value, "8086"))
2003 return IF_8086;
2004 if (!strcmp(value, "186"))
2005 return IF_186;
2006 if (!strcmp(value, "286"))
2007 return IF_286;
2008 if (!strcmp(value, "386"))
2009 return IF_386;
2010 if (!strcmp(value, "486"))
2011 return IF_486;
2012 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
2013 return IF_PENT;
2014 if (!strcmp(value, "686") ||
2015 !nasm_stricmp(value, "ppro") ||
2016 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
2017 return IF_P6;
2018 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
2019 return IF_KATMAI;
2020 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2021 !nasm_stricmp(value, "willamette"))
2022 return IF_WILLAMETTE;
2023 if (!nasm_stricmp(value, "prescott"))
2024 return IF_PRESCOTT;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002025 if (!nasm_stricmp(value, "x64") ||
2026 !nasm_stricmp(value, "x86-64"))
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002027 return IF_X86_64;
H. Peter Anvin3ab8de62002-05-28 01:25:06 +00002028 if (!nasm_stricmp(value, "ia64") ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 !nasm_stricmp(value, "ia-64") ||
2030 !nasm_stricmp(value, "itanium") ||
2031 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
2032 return IF_IA64;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002033
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002034 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 "unknown 'cpu' type");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002036
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 return IF_PLEVEL; /* the maximum level */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002038}
2039
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002040static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002041{
2042 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002043
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 if ((i = atoi(value)) == 16)
2045 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002046 else if (i == 32) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 if (cpu < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002048 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002049 "cannot specify 32-bit segment on processor below a 386");
2050 i = 16;
2051 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002052 } else if (i == 64) {
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002053 if (cpu < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002054 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002055 "cannot specify 64-bit segment on processor below an x86-64");
2056 i = 16;
2057 }
2058 if (i != maxbits) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002059 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002060 "%s output format does not support 64-bit code",
2061 ofmt->shortname);
2062 i = 16;
2063 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002064 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002065 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002066 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002067 value);
2068 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002069 }
2070 return i;
2071}