blob: 6b7ca4c3c7861f644fc6eef838cfda32827308d2 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* The Netwide Assembler main program module
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 */
8
9#include <stdio.h>
10#include <stdarg.h>
11#include <stdlib.h>
12#include <string.h>
13#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000014#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000015
16#include "nasm.h"
17#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000018#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000019#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000020#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000021#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000022#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000023#include "assemble.h"
24#include "labels.h"
25#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000026#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000027
H. Peter Anvine2c80182005-01-15 22:15:51 +000028struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000029 int lineno;
30 int operand;
31};
32
Keith Kaniosa6dfa782007-04-13 16:47:53 +000033static int get_bits(char *value);
34static uint32_t get_cpu(char *cpu_str);
35static void parse_cmdline(int, char **);
36static void assemble_file(char *);
37static int getkw(char **directive, char **value);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000038static void register_output_formats(void);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000039static void report_error_gnu(int severity, const char *fmt, ...);
40static void report_error_vc(int severity, const char *fmt, ...);
41static void report_error_common(int severity, const char *fmt,
H. Peter Anvine2c80182005-01-15 22:15:51 +000042 va_list args);
43static int is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044static void usage(void);
Ed Berosetfa771012002-06-09 20:56:40 +000045static efunc report_error;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
John Coffman0efaec92002-05-26 19:20:08 +000047static int using_debug_info, opt_verbose_info;
H. Peter Anvine2c80182005-01-15 22:15:51 +000048int tasm_compatible_mode = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +000049int pass0;
Keith Kaniosb7a89542007-04-12 02:40:54 +000050int maxbits = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000051int globalrel = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000052
Keith Kaniosa6dfa782007-04-13 16:47:53 +000053static char inname[FILENAME_MAX];
54static char outname[FILENAME_MAX];
55static char listname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +000056static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000057/* static int pass = 0; */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058static struct ofmt *ofmt = NULL;
59
H. Peter Anvine2c80182005-01-15 22:15:51 +000060static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +000061
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062static FILE *ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000063int optimizing = -1; /* number of optimization passes to take */
64static int sb, cmd_sb = 16; /* by default */
Keith Kaniosb7a89542007-04-12 02:40:54 +000065static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
66static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
H. Peter Anvine2c80182005-01-15 22:15:51 +000067int global_offset_changed; /* referenced in labels.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000068
H. Peter Anvineba20a72002-04-30 20:53:55 +000069static loc_t location;
H. Peter Anvine2c80182005-01-15 22:15:51 +000070int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +000071int32_t abs_seg; /* ABSOLUTE segment basis */
72int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +000073
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000074static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +000075
H. Peter Anvine2c80182005-01-15 22:15:51 +000076static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvineba20a72002-04-30 20:53:55 +000077static struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000079static Preproc *preproc;
H. Peter Anvin620515a2002-04-30 20:57:38 +000080enum op_type {
H. Peter Anvine2c80182005-01-15 22:15:51 +000081 op_normal, /* Preprocess and assemble */
82 op_preprocess, /* Preprocess only */
H. Peter Anvin37a321f2007-09-24 13:41:58 -070083 op_depend, /* Generate dependencies */
84 op_depend_missing_ok, /* Generate dependencies, missing OK */
H. Peter Anvin620515a2002-04-30 20:57:38 +000085};
86static enum op_type operating_mode;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +000089 * Which of the suppressible warnings are suppressed. Entry zero
90 * doesn't do anything. Initial defaults are given here.
91 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +000092static char suppressed[1 + ERR_WARN_MAX] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +000093 0, TRUE, TRUE, TRUE, FALSE, TRUE
H. Peter Anvin6768eb72002-04-30 20:52:26 +000094};
95
96/*
97 * The option names for the suppressible warnings. As before, entry
98 * zero does nothing.
99 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000100static const char *suppressed_names[1 + ERR_WARN_MAX] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101 NULL, "macro-params", "macro-selfref", "orphan-labels",
102 "number-overflow",
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000103 "gnu-elf-extensions"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000104};
105
106/*
107 * The explanations for the suppressible warnings. As before, entry
108 * zero does nothing.
109 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000110static const char *suppressed_what[1 + ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000111 NULL,
112 "macro calls with wrong no. of params",
113 "cyclic macro self-references",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000114 "labels alone on lines without trailing `:'",
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000115 "numeric constants greater than 0xFFFFFFFF",
116 "using 8- or 16-bit relocation in ELF, a GNU extension"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000117};
118
119/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120 * This is a null preprocessor which just copies lines from input
121 * to output. It's used when someone explicitly requests that NASM
122 * not preprocess their source file.
123 */
124
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000125static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
126static char *no_pp_getline(void);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000127static void no_pp_cleanup(int);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000128static Preproc no_pp = {
129 no_pp_reset,
130 no_pp_getline,
131 no_pp_cleanup
132};
133
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000134/*
135 * get/set current offset...
136 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000137#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000138 raa_read(offsets,location.segment))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000139#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000140 (void)(offsets=raa_write(offsets,location.segment,(x))))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000141
142static int want_usage;
143static int terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000144int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000145
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000146static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000147{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000148 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 fputs(line, outfile);
150 fputc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000151 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000152 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000153}
154
H. Peter Anvin038d8612007-04-12 16:54:50 +0000155int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000156{
Ed Berosetfa771012002-06-09 20:56:40 +0000157 pass0 = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000158 want_usage = terminate_after_phase = FALSE;
Ed Berosetfa771012002-06-09 20:56:40 +0000159 report_error = report_error_gnu;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000160
H. Peter Anvin97e15752007-09-25 08:47:47 -0700161 error_file = stderr;
162
H. Peter Anvine2c80182005-01-15 22:15:51 +0000163 nasm_set_malloc_error(report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000164 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000165 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000166
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000167 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000168 operating_mode = op_normal;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000169
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 seg_init();
171
172 register_output_formats();
173
174 parse_cmdline(argc, argv);
175
H. Peter Anvine2c80182005-01-15 22:15:51 +0000176 if (terminate_after_phase) {
177 if (want_usage)
178 usage();
179 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180 }
181
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000182 /* If debugging info is disabled, suppress any debug calls */
183 if (!using_debug_info)
184 ofmt->current_dfmt = &null_debug_form;
185
H. Peter Anvin76690a12002-04-30 20:52:49 +0000186 if (ofmt->stdmac)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000187 pp_extra_stdmac(ofmt->stdmac);
188 parser_global_info(ofmt, &location);
189 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000190
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000191 /* define some macros dependent of command-line */
192 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000193 char temp[64];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000194 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
195 ofmt->shortname);
196 pp_pre_define(temp);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000197 }
198
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 switch (operating_mode) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700200 case op_depend_missing_ok:
201 pp_include_path(NULL); /* "assume generated" */
202 /* fall through */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000203 case op_depend:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000204 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000205 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
207 if (outname[0] == '\0')
208 ofmt->filename(inname, outname, report_error);
209 ofile = NULL;
210 fprintf(stdout, "%s: %s", outname, inname);
211 while ((line = preproc->getline()))
212 nasm_free(line);
213 preproc->cleanup(0);
214 putc('\n', stdout);
215 }
216 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000217
218 case op_preprocess:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000220 char *line;
221 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000222 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000223 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000224
H. Peter Anvine2c80182005-01-15 22:15:51 +0000225 if (*outname) {
226 ofile = fopen(outname, "w");
227 if (!ofile)
228 report_error(ERR_FATAL | ERR_NOFILE,
229 "unable to open output file `%s'",
230 outname);
231 } else
232 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000233
H. Peter Anvine2c80182005-01-15 22:15:51 +0000234 location.known = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000235
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000236/* pass = 1; */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000237 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
238 while ((line = preproc->getline())) {
239 /*
240 * We generate %line directives if needed for later programs
241 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000242 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000243 int altline = src_get(&linnum, &file_name);
244 if (altline) {
245 if (altline == 1 && lineinc == 1)
246 nasm_fputs("", ofile);
247 else {
248 lineinc = (altline != -1 || lineinc != 1);
249 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000250 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000251 file_name);
252 }
253 prior_linnum = linnum;
254 }
255 nasm_fputs(line, ofile);
256 nasm_free(line);
257 }
258 nasm_free(file_name);
259 preproc->cleanup(0);
260 if (ofile)
261 fclose(ofile);
262 if (ofile && terminate_after_phase)
263 remove(outname);
264 }
265 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000266
267 case op_normal:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000268 {
269 /*
270 * We must call ofmt->filename _anyway_, even if the user
271 * has specified their own output file, because some
272 * formats (eg OBJ and COFF) use ofmt->filename to find out
273 * the name of the input file and then put that inside the
274 * file.
275 */
276 ofmt->filename(inname, outname, report_error);
277
278 ofile = fopen(outname, "wb");
279 if (!ofile) {
280 report_error(ERR_FATAL | ERR_NOFILE,
281 "unable to open output file `%s'", outname);
282 }
283
284 /*
285 * We must call init_labels() before ofmt->init() since
286 * some object formats will want to define labels in their
287 * init routines. (eg OS/2 defines the FLAT group)
288 */
289 init_labels();
290
291 ofmt->init(ofile, report_error, define_label, evaluate);
292
293 assemble_file(inname);
294
295 if (!terminate_after_phase) {
296 ofmt->cleanup(using_debug_info);
297 cleanup_labels();
298 } else {
299 /*
300 * We had an fclose on the output file here, but we
301 * actually do that in all the object file drivers as well,
302 * so we're leaving out the one here.
303 * fclose (ofile);
304 */
305 remove(outname);
306 if (listname[0])
307 remove(listname);
308 }
309 }
310 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000311 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000312
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000313 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000315
H. Peter Anvine2c80182005-01-15 22:15:51 +0000316 raa_free(offsets);
317 saa_free(forwrefs);
318 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000319 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000320
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000321 if (terminate_after_phase)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 return 1;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000323 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000324 return 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000325}
326
H. Peter Anvineba20a72002-04-30 20:53:55 +0000327/*
328 * Get a parameter for a command line option.
329 * First arg must be in the form of e.g. -f...
330 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000331static char *get_param(char *p, char *q, int *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000332{
333 *advance = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000334 if (p[2]) { /* the parameter's in the option */
335 p += 2;
336 while (isspace(*p))
337 p++;
338 return p;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000339 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000340 if (q && q[0]) {
341 *advance = 1;
342 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000344 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
345 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000346 return NULL;
347}
348
H. Peter Anvine2c80182005-01-15 22:15:51 +0000349struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000350 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000352};
353
354#define OPT_PREFIX 0
355#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000356struct textargs textopts[] = {
357 {"prefix", OPT_PREFIX},
358 {"postfix", OPT_POSTFIX},
359 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000360};
361
H. Peter Anvineba20a72002-04-30 20:53:55 +0000362int stopoptions = 0;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000363static int process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000364{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000365 char *param;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 int i, advance = 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000367
368 if (!p || !p[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369 return 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000370
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371 if (p[0] == '-' && !stopoptions) {
372 switch (p[1]) {
373 case 's':
374 error_file = stdout;
375 break;
376 case 'o': /* these parameters take values */
377 case 'O':
378 case 'f':
379 case 'p':
380 case 'P':
381 case 'd':
382 case 'D':
383 case 'i':
384 case 'I':
385 case 'l':
386 case 'E':
387 case 'F':
388 case 'X':
389 case 'u':
390 case 'U':
391 if (!(param = get_param(p, q, &advance)))
392 break;
393 if (p[1] == 'o') { /* output file */
394 strcpy(outname, param);
395 } else if (p[1] == 'f') { /* output format */
396 ofmt = ofmt_find(param);
397 if (!ofmt) {
398 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
399 "unrecognised output format `%s' - "
400 "use -hf for a list", param);
401 } else
402 ofmt->current_dfmt = ofmt->debug_formats[0];
403 } else if (p[1] == 'O') { /* Optimization level */
404 int opt;
405 opt = -99;
406 while (*param) {
407 if (isdigit(*param)) {
408 opt = atoi(param);
409 while (isdigit(*++param)) ;
410 if (opt <= 0)
411 optimizing = -1; /* 0.98 behaviour */
412 else if (opt == 1)
413 optimizing = 0; /* Two passes, 0.98.09 behavior */
H. Peter Anvin26970092007-09-22 18:16:52 -0700414 else if (opt <= 5)
415 /* The optimizer seems to have problems with
416 < 5 passes? Hidden bug? */
417 optimizing = 5; /* 5 passes */
418 else
419 optimizing = opt; /* More than 5 passes */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 } else {
421 if (*param == 'v' || *param == '+') {
422 ++param;
423 opt_verbose_info = TRUE;
424 opt = 0;
425 } else { /* garbage */
426 opt = -99;
427 break;
428 }
429 }
430 } /* while (*param) */
431 if (opt == -99)
432 report_error(ERR_FATAL,
433 "command line optimization level must be 'v', 0..3 or <nn>");
434 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
435 pp_pre_include(param);
436 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
437 pp_pre_define(param);
438 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
439 pp_pre_undefine(param);
440 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
441 pp_include_path(param);
442 } else if (p[1] == 'l') { /* listing file */
443 strcpy(listname, param);
444 } else if (p[1] == 'E') { /* error messages file */
445 error_file = fopen(param, "w");
446 if (!error_file) {
447 error_file = stderr; /* Revert to default! */
448 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
449 "cannot open file `%s' for error messages",
450 param);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000451 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 } else if (p[1] == 'F') { /* specify debug format */
453 ofmt->current_dfmt = dfmt_find(ofmt, param);
454 if (!ofmt->current_dfmt) {
455 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
456 "unrecognized debug format `%s' for"
457 " output format `%s'",
458 param, ofmt->shortname);
459 }
460 } else if (p[1] == 'X') { /* specify error reporting format */
461 if (nasm_stricmp("vc", param) == 0)
462 report_error = report_error_vc;
463 else if (nasm_stricmp("gnu", param) == 0)
464 report_error = report_error_gnu;
465 else
466 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
467 "unrecognized error reporting format `%s'",
468 param);
469 }
470 break;
471 case 'g':
472 using_debug_info = TRUE;
473 break;
474 case 'h':
475 printf
476 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
477 "[-l listfile]\n"
478 " [options...] [--] filename\n"
479 " or nasm -r for version info (obsolete)\n"
480 " or nasm -v for version info (preferred)\n\n"
481 " -t assemble in SciTech TASM compatible mode\n"
482 " -g generate debug information in selected format.\n");
483 printf
484 (" -e preprocess only (writes output to stdout by default)\n"
485 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700486 " -M generate Makefile dependencies on stdout\n"
487 " -MG d:o, missing files assumed generated\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 " -E<file> redirect error messages to file\n"
489 " -s redirect error messages to stdout\n\n"
490 " -F format select a debugging format\n\n"
491 " -I<path> adds a pathname to the include file path\n");
492 printf
493 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
494 " -P<file> pre-includes a file\n"
495 " -D<macro>[=<value>] pre-defines a macro\n"
496 " -U<macro> undefines a macro\n"
497 " -X<format> specifies error reporting format (gnu or vc)\n"
498 " -w+foo enables warnings about foo; -w-foo disables them\n"
499 "where foo can be:\n");
500 for (i = 1; i <= ERR_WARN_MAX; i++)
501 printf(" %-23s %s (default %s)\n",
502 suppressed_names[i], suppressed_what[i],
503 suppressed[i] ? "off" : "on");
504 printf
505 ("\nresponse files should contain command line parameters"
506 ", one per line.\n");
507 if (p[2] == 'f') {
508 printf("\nvalid output formats for -f are"
509 " (`*' denotes default):\n");
510 ofmt_list(ofmt, stdout);
511 } else {
512 printf("\nFor a list of valid output formats, use -hf.\n");
513 printf("For a list of debug formats, use -f <form> -y.\n");
514 }
515 exit(0); /* never need usage message here */
516 break;
517 case 'y':
518 printf("\nvalid debug formats for '%s' output format are"
519 " ('*' denotes default):\n", ofmt->shortname);
520 dfmt_list(ofmt, stdout);
521 exit(0);
522 break;
523 case 't':
524 tasm_compatible_mode = TRUE;
525 break;
526 case 'r':
527 case 'v':
528 {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000529 const char *nasm_version_string =
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 "NASM version " NASM_VER " compiled on " __DATE__
H. Peter Anvineba20a72002-04-30 20:53:55 +0000531#ifdef DEBUG
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 " with -DDEBUG"
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 ;
535 puts(nasm_version_string);
536 exit(0); /* never need usage message here */
537 }
538 break;
539 case 'e': /* preprocess only */
540 operating_mode = op_preprocess;
541 break;
542 case 'a': /* assemble only - don't preprocess */
543 preproc = &no_pp;
544 break;
545 case 'w':
546 if (p[2] != '+' && p[2] != '-') {
547 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
548 "invalid option to `-w'");
549 } else {
550 for (i = 1; i <= ERR_WARN_MAX; i++)
551 if (!nasm_stricmp(p + 3, suppressed_names[i]))
552 break;
553 if (i <= ERR_WARN_MAX)
554 suppressed[i] = (p[2] == '-');
555 else
556 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
557 "invalid option to `-w'");
558 }
559 break;
560 case 'M':
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700561 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000563
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 case '-':
565 {
566 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000567
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 if (p[2] == 0) { /* -- => stop processing options */
569 stopoptions = 1;
570 break;
571 }
572 for (s = 0; textopts[s].label; s++) {
573 if (!nasm_stricmp(p + 2, textopts[s].label)) {
574 break;
575 }
576 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 switch (s) {
579
580 case OPT_PREFIX:
581 case OPT_POSTFIX:
582 {
583 if (!q) {
584 report_error(ERR_NONFATAL | ERR_NOFILE |
585 ERR_USAGE,
586 "option `--%s' requires an argument",
587 p + 2);
588 break;
589 } else {
590 advance = 1, param = q;
591 }
592
593 if (s == OPT_PREFIX) {
594 strncpy(lprefix, param, PREFIX_MAX - 1);
595 lprefix[PREFIX_MAX - 1] = 0;
596 break;
597 }
598 if (s == OPT_POSTFIX) {
599 strncpy(lpostfix, param, POSTFIX_MAX - 1);
600 lpostfix[POSTFIX_MAX - 1] = 0;
601 break;
602 }
603 break;
604 }
605 default:
606 {
607 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
608 "unrecognised option `--%s'", p + 2);
609 break;
610 }
611 }
612 break;
613 }
614
615 default:
616 if (!ofmt->setinfo(GI_SWITCH, &p))
617 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
618 "unrecognised option `-%c'", p[1]);
619 break;
620 }
621 } else {
622 if (*inname) {
623 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
624 "more than one input file specified");
625 } else
626 strcpy(inname, p);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000627 }
628
629 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000630}
631
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632#define ARG_BUF_DELTA 128
633
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000635{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000636 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637 int bufsize, prevargsize;
638
639 bufsize = prevargsize = ARG_BUF_DELTA;
640 buffer = nasm_malloc(ARG_BUF_DELTA);
641 prevarg = nasm_malloc(ARG_BUF_DELTA);
642 prevarg[0] = '\0';
643
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 while (1) { /* Loop to handle all lines in file */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000645
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 p = buffer;
647 while (1) { /* Loop to handle long lines */
648 q = fgets(p, bufsize - (p - buffer), rfile);
649 if (!q)
650 break;
651 p += strlen(p);
652 if (p > buffer && p[-1] == '\n')
653 break;
654 if (p - buffer > bufsize - 10) {
655 int offset;
656 offset = p - buffer;
657 bufsize += ARG_BUF_DELTA;
658 buffer = nasm_realloc(buffer, bufsize);
659 p = buffer + offset;
660 }
661 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000662
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 if (!q && p == buffer) {
664 if (prevarg[0])
665 process_arg(prevarg, NULL);
666 nasm_free(buffer);
667 nasm_free(prevarg);
668 return;
669 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000670
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 /*
672 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
673 * them are present at the end of the line.
674 */
675 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +0000676
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 while (p > buffer && isspace(p[-1]))
678 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +0000679
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 p = buffer;
681 while (isspace(*p))
682 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000683
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 if (process_arg(prevarg, p))
685 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 if (strlen(p) > prevargsize - 10) {
688 prevargsize += ARG_BUF_DELTA;
689 prevarg = nasm_realloc(prevarg, prevargsize);
690 }
691 strcpy(prevarg, p);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000692 }
693}
694
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000695/* Function to process args from a string of args, rather than the
696 * argv array. Used by the environment variable and response file
697 * processing.
698 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000699static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000701 char *p, *q, *arg, *prevarg;
702 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000703
704 p = args;
705 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000707 arg = NULL;
708 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 q = p;
710 while (*p && *p != separator)
711 p++;
712 while (*p == separator)
713 *p++ = '\0';
714 prevarg = arg;
715 arg = q;
716 if (process_arg(prevarg, arg))
717 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000718 }
719 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000721}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000722
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000723static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724{
725 FILE *rfile;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000726 char *envreal, *envcopy = NULL, *p, *arg;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000728 *inname = *outname = *listname = '\0';
729
730 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +0000731 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000732 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +0000733 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000734 arg = NULL;
735 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 envcopy = nasm_strdup(envreal);
737 process_args(envcopy);
738 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000740
741 /*
742 * Now process the actual command line.
743 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 while (--argc) {
745 int i;
746 argv++;
747 if (argv[0][0] == '@') {
748 /* We have a response file, so process this as a set of
749 * arguments like the environment variable. This allows us
750 * to have multiple arguments on a single line, which is
751 * different to the -@resp file processing below for regular
752 * NASM.
753 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000754 char *str = malloc(2048);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 FILE *f = fopen(&argv[0][1], "r");
756 if (!str) {
757 printf("out of memory");
758 exit(-1);
759 }
760 if (f) {
761 while (fgets(str, 2048, f)) {
762 process_args(str);
763 }
764 fclose(f);
765 }
766 free(str);
767 argc--;
768 argv++;
769 }
770 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000771 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
772 if (p) {
773 rfile = fopen(p, "r");
774 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 process_respfile(rfile);
776 fclose(rfile);
777 } else
778 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
779 "unable to open response file `%s'", p);
780 }
781 } else
782 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
783 argv += i, argc -= i;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000784 }
785
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000786 if (!*inname)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
788 "no input file specified");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000789}
790
H. Peter Anvin62b24d72007-08-29 16:38:05 +0000791/* List of directives */
792enum {
793 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
794 D_EXTERN, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
795};
796static const char *directives[] = {
797 "", "absolute", "bits", "common", "cpu", "debug", "default",
798 "extern", "global", "list", "section", "segment", "warning"
799};
800
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000801static void assemble_file(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000802{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000803 char *directive, *value, *p, *q, *special, *line, debugid[80];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 insn output_ins;
805 int i, rn_error, validid;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000806 int32_t seg, offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000807 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 expr *e;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000809 int pass, pass_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 int pass_cnt = 0; /* count actual passes */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000812 if (cmd_sb == 32 && cmd_cpu < IF_386)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 report_error(ERR_FATAL, "command line: "
814 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000815
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
817 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
818 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
819 int pass1, pass2;
820 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000821
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
823 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
824 /* pass0 seq is 0, 0, 0,..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000825
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 def_label = pass > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000827
Keith Kaniosb7a89542007-04-12 02:40:54 +0000828 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 cpu = cmd_cpu;
830 if (pass0 == 2) {
831 if (*listname)
832 nasmlist.init(listname, report_error);
833 }
834 in_abs_seg = FALSE;
835 global_offset_changed = FALSE; /* set by redefine_label */
836 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000837 globalbits = sb;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 if (pass > 1) {
839 saa_rewind(forwrefs);
840 forwref = saa_rstruct(forwrefs);
841 raa_free(offsets);
842 offsets = raa_init();
843 }
844 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
845 globallineno = 0;
846 if (pass == 1)
847 location.known = TRUE;
848 location.offset = offs = GET_CURR_OFFS;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000849
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 while ((line = preproc->getline())) {
851 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000852
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 /* here we parse our directives; this is not handled by the 'real'
854 * parser. */
855 directive = line;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000856 i = getkw(&directive, &value);
857 if (i) {
H. Peter Anvin62b24d72007-08-29 16:38:05 +0000858 int err = 0;
859
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 switch (i) {
H. Peter Anvin62b24d72007-08-29 16:38:05 +0000861 case D_SEGMENT: /* [SEGMENT n] */
862 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 seg = ofmt->section(value, pass2, &sb);
864 if (seg == NO_SEG) {
865 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000866 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 value);
868 } else {
869 in_abs_seg = FALSE;
870 location.segment = seg;
871 }
872 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +0000873 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 if (*value == '$')
875 value++; /* skip initial $ if present */
876 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000877 q = value;
878 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000880 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 *q++ = '\0';
882 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000883 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 } else if (pass == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000885 q = value;
886 validid = TRUE;
887 if (!isidstart(*q))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 validid = FALSE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000889 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 if (!isidchar(*q))
891 validid = FALSE;
892 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000893 }
894 if (!validid) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 report_error(ERR_NONFATAL,
896 "identifier expected after EXTERN");
897 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000898 }
899 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 *q++ = '\0';
901 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000902 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 special = NULL;
904 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
905 int temp = pass0;
906 pass0 = 1; /* fake pass 1 in labels.c */
907 declare_as_global(value, special,
908 report_error);
909 define_label(value, seg_alloc(), 0L, NULL,
910 FALSE, TRUE, ofmt, report_error);
911 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000912 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 } /* else pass0 == 1 */
914 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +0000915 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000916 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +0000918 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 if (*value == '$')
920 value++; /* skip initial $ if present */
921 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000922 q = value;
923 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000925 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 *q++ = '\0';
927 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000928 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000930 q = value;
931 validid = TRUE;
932 if (!isidstart(*q))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 validid = FALSE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000934 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 if (!isidchar(*q))
936 validid = FALSE;
937 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000938 }
939 if (!validid) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 report_error(ERR_NONFATAL,
941 "identifier expected after GLOBAL");
942 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000943 }
944 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 *q++ = '\0';
946 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000947 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 special = NULL;
949 declare_as_global(value, special, report_error);
950 } /* pass == 1 */
951 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +0000952 case D_COMMON: /* [COMMON symbol size:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 if (*value == '$')
954 value++; /* skip initial $ if present */
955 if (pass0 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000956 p = value;
957 validid = TRUE;
958 if (!isidstart(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 validid = FALSE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000960 while (*p && !isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 if (!isidchar(*p))
962 validid = FALSE;
963 p++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000964 }
965 if (!validid) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 report_error(ERR_NONFATAL,
967 "identifier expected after COMMON");
968 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000969 }
970 if (*p) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000971 int64_t size;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000972
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 while (*p && isspace(*p))
974 *p++ = '\0';
975 q = p;
976 while (*q && *q != ':')
977 q++;
978 if (*q == ':') {
979 *q++ = '\0';
980 special = q;
981 } else
982 special = NULL;
983 size = readnum(p, &rn_error);
984 if (rn_error)
985 report_error(ERR_NONFATAL,
986 "invalid size specified"
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000987 " in COMMON declaration");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 else
989 define_common(value, seg_alloc(), size,
990 special, ofmt, report_error);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000991 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 report_error(ERR_NONFATAL,
993 "no size specified in"
994 " COMMON declaration");
995 } else if (pass0 == 2) { /* pass == 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000996 q = value;
997 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 if (isspace(*q))
999 *q = '\0';
1000 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001001 }
1002 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 *q++ = '\0';
1004 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001005 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 }
1007 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001008 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 stdscan_reset();
1010 stdscan_bufptr = value;
1011 tokval.t_type = TOKEN_INVALID;
1012 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1013 report_error, NULL);
1014 if (e) {
1015 if (!is_reloc(e))
1016 report_error(pass0 ==
1017 1 ? ERR_NONFATAL : ERR_PANIC,
1018 "cannot use non-relocatable expression as "
1019 "ABSOLUTE address");
1020 else {
1021 abs_seg = reloc_seg(e);
1022 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001023 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 } else if (pass == 1)
1025 abs_offset = 0x100; /* don't go near zero in case of / */
1026 else
1027 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1028 "in pass two");
1029 in_abs_seg = TRUE;
1030 location.segment = NO_SEG;
1031 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001032 case D_DEBUG: /* [DEBUG] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 p = value;
1034 q = debugid;
1035 validid = TRUE;
1036 if (!isidstart(*p))
1037 validid = FALSE;
1038 while (*p && !isspace(*p)) {
1039 if (!isidchar(*p))
1040 validid = FALSE;
1041 *q++ = *p++;
1042 }
1043 *q++ = 0;
1044 if (!validid) {
1045 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1046 "identifier expected after DEBUG");
1047 break;
1048 }
1049 while (*p && isspace(*p))
1050 p++;
1051 if (pass == pass_max)
1052 ofmt->current_dfmt->debug_directive(debugid, p);
1053 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001054 case D_WARNING: /* [WARNING {+|-}warn-name] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 if (pass1 == 1) {
1056 while (*value && isspace(*value))
1057 value++;
1058
1059 if (*value == '+' || *value == '-') {
1060 validid = (*value == '-') ? TRUE : FALSE;
1061 value++;
1062 } else
1063 validid = FALSE;
1064
1065 for (i = 1; i <= ERR_WARN_MAX; i++)
1066 if (!nasm_stricmp(value, suppressed_names[i]))
1067 break;
1068 if (i <= ERR_WARN_MAX)
1069 suppressed[i] = validid;
1070 else
1071 report_error(ERR_NONFATAL,
1072 "invalid warning id in WARNING directive");
1073 }
1074 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001075 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 cpu = get_cpu(value);
1077 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001078 case D_LIST: /* [LIST {+|-}] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 while (*value && isspace(*value))
1080 value++;
1081
1082 if (*value == '+') {
1083 user_nolist = 0;
1084 } else {
1085 if (*value == '-') {
1086 user_nolist = 1;
1087 } else {
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001088 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 }
1090 }
1091 break;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001092 case D_DEFAULT: /* [DEFAULT] */
1093 stdscan_reset();
1094 stdscan_bufptr = value;
1095 tokval.t_type = TOKEN_INVALID;
1096 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1097 switch ((int)tokval.t_integer) {
1098 case S_REL:
1099 globalrel = 1;
1100 break;
1101 case S_ABS:
1102 globalrel = 0;
1103 break;
1104 default:
1105 err = 1;
1106 break;
1107 }
1108 } else {
1109 err = 1;
1110 }
1111 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 default:
1113 if (!ofmt->directive(directive, value, pass2))
1114 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1115 "unrecognised directive [%s]",
1116 directive);
1117 }
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001118 if (err) {
1119 report_error(ERR_NONFATAL,
1120 "invalid parameter to [%s] directive",
1121 directive);
1122 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 } else { /* it isn't a directive */
1124
1125 parse_line(pass1, line, &output_ins,
1126 report_error, evaluate, def_label);
1127
1128 if (!(optimizing > 0) && pass == 2) {
1129 if (forwref != NULL && globallineno == forwref->lineno) {
1130 output_ins.forw_ref = TRUE;
1131 do {
1132 output_ins.oprs[forwref->operand].opflags |=
1133 OPFLAG_FORWARD;
1134 forwref = saa_rstruct(forwrefs);
1135 } while (forwref != NULL
1136 && forwref->lineno == globallineno);
1137 } else
1138 output_ins.forw_ref = FALSE;
1139 }
1140
1141 if (!(optimizing > 0) && output_ins.forw_ref) {
1142 if (pass == 1) {
1143 for (i = 0; i < output_ins.operands; i++) {
1144 if (output_ins.oprs[i].
1145 opflags & OPFLAG_FORWARD) {
1146 struct forwrefinfo *fwinf =
1147 (struct forwrefinfo *)
1148 saa_wstruct(forwrefs);
1149 fwinf->lineno = globallineno;
1150 fwinf->operand = i;
1151 }
1152 }
1153 } else { /* pass == 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001154 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 * Hack to prevent phase error in the code
1156 * rol ax,x
1157 * x equ 1
1158 *
1159 * If the second operand is a forward reference,
1160 * the UNITY property of the number 1 in that
1161 * operand is cancelled. Otherwise the above
1162 * sequence will cause a phase error.
1163 *
1164 * This hack means that the above code will
1165 * generate 286+ code.
1166 *
1167 * The forward reference will mean that the
1168 * operand will not have the UNITY property on
1169 * the first pass, so the pass behaviours will
1170 * be consistent.
1171 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001172
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001173 if (output_ins.operands >= 2 &&
H. Peter Anvin5a640e12007-05-29 23:57:12 +00001174 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1175 !(IMMEDIATE & ~output_ins.oprs[1].type))
1176 {
1177 /* Remove special properties bits */
1178 output_ins.oprs[1].type &= ~REG_SMASK;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001179 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001180
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 } /* pass == 2 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001182
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001184
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 /* forw_ref */
1186 if (output_ins.opcode == I_EQU) {
1187 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001188 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 * Special `..' EQUs get processed in pass two,
1190 * except `..@' macro-processor EQUs which are done
1191 * in the normal place.
1192 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001193 if (!output_ins.label)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 report_error(ERR_NONFATAL,
1195 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001196
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001197 else if (output_ins.label[0] != '.' ||
1198 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 output_ins.label[2] == '@') {
1200 if (output_ins.operands == 1 &&
1201 (output_ins.oprs[0].type & IMMEDIATE) &&
1202 output_ins.oprs[0].wrt == NO_SEG) {
1203 int isext =
1204 output_ins.oprs[0].
1205 opflags & OPFLAG_EXTERN;
1206 def_label(output_ins.label,
1207 output_ins.oprs[0].segment,
1208 output_ins.oprs[0].offset, NULL,
1209 FALSE, isext, ofmt,
1210 report_error);
1211 } else if (output_ins.operands == 2
1212 && (output_ins.oprs[0].
1213 type & IMMEDIATE)
1214 && (output_ins.oprs[0].type & COLON)
1215 && output_ins.oprs[0].segment ==
1216 NO_SEG
1217 && output_ins.oprs[0].wrt == NO_SEG
1218 && (output_ins.oprs[1].
1219 type & IMMEDIATE)
1220 && output_ins.oprs[1].segment ==
1221 NO_SEG
1222 && output_ins.oprs[1].wrt ==
1223 NO_SEG) {
1224 def_label(output_ins.label,
1225 output_ins.oprs[0].
1226 offset | SEG_ABS,
1227 output_ins.oprs[1].offset, NULL,
1228 FALSE, FALSE, ofmt,
1229 report_error);
1230 } else
1231 report_error(ERR_NONFATAL,
1232 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001233 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 } else { /* pass == 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001235 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 * Special `..' EQUs get processed here, except
1237 * `..@' macro processor EQUs which are done above.
1238 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001239 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 output_ins.label[1] == '.' &&
1241 output_ins.label[2] != '@') {
1242 if (output_ins.operands == 1 &&
1243 (output_ins.oprs[0].type & IMMEDIATE)) {
1244 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001245 output_ins.oprs[0].segment,
1246 output_ins.oprs[0].offset,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 NULL, FALSE, FALSE, ofmt,
1248 report_error);
1249 } else if (output_ins.operands == 2
1250 && (output_ins.oprs[0].
1251 type & IMMEDIATE)
1252 && (output_ins.oprs[0].type & COLON)
1253 && output_ins.oprs[0].segment ==
1254 NO_SEG
1255 && (output_ins.oprs[1].
1256 type & IMMEDIATE)
1257 && output_ins.oprs[1].segment ==
1258 NO_SEG) {
1259 define_label(output_ins.label,
1260 output_ins.oprs[0].
1261 offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001262 output_ins.oprs[1].offset,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 NULL, FALSE, FALSE, ofmt,
1264 report_error);
1265 } else
1266 report_error(ERR_NONFATAL,
1267 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001268 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 } /* pass == 2 */
1270 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001271
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001273
Keith Kaniosb7a89542007-04-12 02:40:54 +00001274 int32_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 &output_ins, report_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001276
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 /* if (using_debug_info) && output_ins.opcode != -1) */
1278 if (using_debug_info)
1279 { /* fbk 03/25/01 */
1280 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001281 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 TYS_ELEMENTS(output_ins.operands);
1283 switch (output_ins.opcode) {
1284 case I_RESB:
1285 typeinfo =
1286 TYS_ELEMENTS(output_ins.oprs[0].
1287 offset) | TY_BYTE;
1288 break;
1289 case I_RESW:
1290 typeinfo =
1291 TYS_ELEMENTS(output_ins.oprs[0].
1292 offset) | TY_WORD;
1293 break;
1294 case I_RESD:
1295 typeinfo =
1296 TYS_ELEMENTS(output_ins.oprs[0].
1297 offset) | TY_DWORD;
1298 break;
1299 case I_RESQ:
1300 typeinfo =
1301 TYS_ELEMENTS(output_ins.oprs[0].
1302 offset) | TY_QWORD;
1303 break;
1304 case I_REST:
1305 typeinfo =
1306 TYS_ELEMENTS(output_ins.oprs[0].
1307 offset) | TY_TBYTE;
1308 break;
1309 case I_DB:
1310 typeinfo |= TY_BYTE;
1311 break;
1312 case I_DW:
1313 typeinfo |= TY_WORD;
1314 break;
1315 case I_DD:
1316 if (output_ins.eops_float)
1317 typeinfo |= TY_FLOAT;
1318 else
1319 typeinfo |= TY_DWORD;
1320 break;
1321 case I_DQ:
1322 typeinfo |= TY_QWORD;
1323 break;
1324 case I_DT:
1325 typeinfo |= TY_TBYTE;
1326 break;
H. Peter Anvin4408b622007-09-22 21:29:41 -07001327 case I_DO:
1328 typeinfo |= TY_OWORD;
1329 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 default:
1331 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001332
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001334
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 ofmt->current_dfmt->debug_typevalue(typeinfo);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001336
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001337 }
1338 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 offs += l;
1340 SET_CURR_OFFS(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001341 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001342 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 * else l == -1 => invalid instruction, which will be
1344 * flagged as an error on pass 2
1345 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001346
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 } else { /* pass == 2 */
1348 offs += assemble(location.segment, offs, sb, cpu,
1349 &output_ins, ofmt, report_error,
1350 &nasmlist);
1351 SET_CURR_OFFS(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001352
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 }
1354 } /* not an EQU */
1355 cleanup_insn(&output_ins);
1356 }
1357 nasm_free(line);
1358 location.offset = offs = GET_CURR_OFFS;
1359 } /* end while (line = preproc->getline... */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 if (pass1 == 2 && global_offset_changed)
1362 report_error(ERR_NONFATAL,
1363 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 if (pass1 == 1)
1366 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 if (pass1 == 1 && terminate_after_phase) {
1369 fclose(ofile);
1370 remove(outname);
1371 if (want_usage)
1372 usage();
1373 exit(1);
1374 }
1375 pass_cnt++;
1376 if (pass > 1 && !global_offset_changed) {
1377 pass0++;
1378 if (pass0 == 2)
1379 pass = pass_max - 1;
1380 } else if (!(optimizing > 0))
1381 pass0++;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001382
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 } /* for (pass=1; pass<=2; pass++) */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 preproc->cleanup(0);
1386 nasmlist.cleanup();
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001387#if 1
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1389 fprintf(stdout,
1390 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001391#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392} /* exit from assemble_file (...) */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001393
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001394static int getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001396 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397
Michael K. Ter Louw1d392362003-08-15 22:25:53 +00001398 buf = *directive;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001399
H. Peter Anvincaa82a12002-04-30 21:03:11 +00001400 /* allow leading spaces or tabs */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 while (*buf == ' ' || *buf == '\t')
1402 buf++;
H. Peter Anvincaa82a12002-04-30 21:03:11 +00001403
H. Peter Anvine2c80182005-01-15 22:15:51 +00001404 if (*buf != '[')
1405 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001406
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001407 p = buf;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001408
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 while (*p && *p != ']')
1410 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001411
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001412 if (!*p)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001414
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001415 q = p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001416
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001417 while (*p && *p != ';') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 if (!isspace(*p))
1419 return 0;
1420 p++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001421 }
1422 q[1] = '\0';
1423
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 *directive = p = buf + 1;
1425 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1426 buf++;
1427 if (*buf == ']') {
1428 *buf = '\0';
1429 *value = buf;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001430 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 *buf++ = '\0';
1432 while (isspace(*buf))
1433 buf++; /* beppu - skip leading whitespace */
1434 *value = buf;
1435 while (*buf != ']')
1436 buf++;
1437 *buf++ = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001438 }
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001439
1440 return bsii(*directive, directives, elements(directives));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001441}
1442
Ed Berosetfa771012002-06-09 20:56:40 +00001443/**
1444 * gnu style error reporting
1445 * This function prints an error message to error_file in the
1446 * style used by GNU. An example would be:
1447 * file.asm:50: error: blah blah blah
1448 * where file.asm is the name of the file, 50 is the line number on
1449 * which the error occurs (or is detected) and "error:" is one of
1450 * the possible optional diagnostics -- it can be "error" or "warning"
1451 * or something else. Finally the line terminates with the actual
1452 * error message.
1453 *
1454 * @param severity the severity of the warning or error
1455 * @param fmt the printf style format string
1456 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001457static void report_error_gnu(int severity, const char *fmt, ...)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001458{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001459 va_list ap;
1460
Ed Berosetfa771012002-06-09 20:56:40 +00001461 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001463
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001464 if (severity & ERR_NOFILE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001466 else {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001467 char *currentfile = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001468 int32_t lineno = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 src_get(&lineno, &currentfile);
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001470 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 nasm_free(currentfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001472 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 va_start(ap, fmt);
1474 report_error_common(severity, fmt, ap);
1475 va_end(ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001476}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001477
Ed Berosetfa771012002-06-09 20:56:40 +00001478/**
1479 * MS style error reporting
1480 * This function prints an error message to error_file in the
1481 * style used by Visual C and some other Microsoft tools. An example
1482 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001483 * file.asm(50) : error: blah blah blah
1484 * where file.asm is the name of the file, 50 is the line number on
1485 * which the error occurs (or is detected) and "error:" is one of
1486 * the possible optional diagnostics -- it can be "error" or "warning"
1487 * or something else. Finally the line terminates with the actual
1488 * error message.
Ed Berosetfa771012002-06-09 20:56:40 +00001489 *
1490 * @param severity the severity of the warning or error
1491 * @param fmt the printf style format string
1492 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001493static void report_error_vc(int severity, const char *fmt, ...)
Ed Berosetfa771012002-06-09 20:56:40 +00001494{
1495 va_list ap;
1496
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 if (is_suppressed_warning(severity))
1498 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001499
1500 if (severity & ERR_NOFILE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001502 else {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001503 char *currentfile = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001504 int32_t lineno = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 src_get(&lineno, &currentfile);
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001506 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 nasm_free(currentfile);
Ed Berosetfa771012002-06-09 20:56:40 +00001508 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 va_start(ap, fmt);
1510 report_error_common(severity, fmt, ap);
1511 va_end(ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001512}
1513
1514/**
1515 * check for supressed warning
1516 * checks for suppressed warning or pass one only warning and we're
1517 * not in pass 1
1518 *
1519 * @param severity the severity of the warning or error
1520 * @return true if we should abort error/warning printing
1521 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522static int is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001523{
1524 /*
1525 * See if it's a suppressed warning.
1526 */
1527 return ((severity & ERR_MASK) == ERR_WARNING &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 (severity & ERR_WARN_MASK) != 0 &&
1529 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1530 /*
1531 * See if it's a pass-one only warning and we're not in pass one.
1532 */
1533 ((severity & ERR_PASS1) && pass0 == 2);
Ed Berosetfa771012002-06-09 20:56:40 +00001534}
1535
1536/**
1537 * common error reporting
1538 * This is the common back end of the error reporting schemes currently
1539 * implemented. It prints the nature of the warning and then the
1540 * specific error message to error_file and may or may not return. It
1541 * doesn't return if the error severity is a "panic" or "debug" type.
1542 *
1543 * @param severity the severity of the warning or error
1544 * @param fmt the printf style format string
1545 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001546static void report_error_common(int severity, const char *fmt,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001548{
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001549 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 case ERR_WARNING:
1551 fputs("warning: ", error_file);
1552 break;
1553 case ERR_NONFATAL:
1554 fputs("error: ", error_file);
1555 break;
1556 case ERR_FATAL:
1557 fputs("fatal: ", error_file);
1558 break;
1559 case ERR_PANIC:
1560 fputs("panic: ", error_file);
1561 break;
1562 case ERR_DEBUG:
1563 fputs("debug: ", error_file);
1564 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001565 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001566
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 vfprintf(error_file, fmt, args);
1568 fputc('\n', error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001569
1570 if (severity & ERR_USAGE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 want_usage = TRUE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001572
1573 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 case ERR_WARNING:
1575 case ERR_DEBUG:
1576 /* no further action, by definition */
1577 break;
1578 case ERR_NONFATAL:
1579 /* hack enables listing(!) on errors */
H. Peter Anvincaa82a12002-04-30 21:03:11 +00001580 terminate_after_phase = TRUE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 break;
1582 case ERR_FATAL:
1583 if (ofile) {
1584 fclose(ofile);
1585 remove(outname);
1586 }
1587 if (want_usage)
1588 usage();
1589 exit(1); /* instantly die */
1590 break; /* placate silly compilers */
1591 case ERR_PANIC:
1592 fflush(NULL);
1593 /* abort(); *//* halt, catch fire, and dump core */
1594 exit(3);
1595 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001596 }
1597}
1598
H. Peter Anvin734b1882002-04-30 21:01:08 +00001599static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001600{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001601 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001602}
1603
H. Peter Anvin734b1882002-04-30 21:01:08 +00001604static void register_output_formats(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001605{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 ofmt = ofmt_register(report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001607}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608
1609#define BUF_DELTA 512
1610
1611static FILE *no_pp_fp;
1612static efunc no_pp_err;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001613static ListGen *no_pp_list;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001614static int32_t no_pp_lineinc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001616static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001618{
1619 src_set_fname(nasm_strdup(file));
1620 src_set_linnum(0);
1621 no_pp_lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001622 no_pp_err = error;
1623 no_pp_fp = fopen(file, "r");
1624 if (!no_pp_fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 no_pp_err(ERR_FATAL | ERR_NOFILE,
1626 "unable to open input file `%s'", file);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001627 no_pp_list = listgen;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 (void)pass; /* placate compilers */
1629 (void)eval; /* placate compilers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001630}
1631
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001632static char *no_pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001633{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001634 char *buffer, *p, *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001635 int bufsize;
1636
1637 bufsize = BUF_DELTA;
1638 buffer = nasm_malloc(BUF_DELTA);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001639 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1640
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 while (1) { /* Loop to handle %line */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001642
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 p = buffer;
1644 while (1) { /* Loop to handle long lines */
1645 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1646 if (!q)
1647 break;
1648 p += strlen(p);
1649 if (p > buffer && p[-1] == '\n')
1650 break;
1651 if (p - buffer > bufsize - 10) {
1652 int offset;
1653 offset = p - buffer;
1654 bufsize += BUF_DELTA;
1655 buffer = nasm_realloc(buffer, bufsize);
1656 p = buffer + offset;
1657 }
1658 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001659
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 if (!q && p == buffer) {
1661 nasm_free(buffer);
1662 return NULL;
1663 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001664
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 /*
1666 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1667 * them are present at the end of the line.
1668 */
1669 buffer[strcspn(buffer, "\r\n\032")] = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 if (!strncmp(buffer, "%line", 5)) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001672 int32_t ln;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 int li;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001674 char *nm = nasm_malloc(strlen(buffer));
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001675 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 nasm_free(src_set_fname(nm));
1677 src_set_linnum(ln);
1678 no_pp_lineinc = li;
1679 continue;
1680 }
1681 nasm_free(nm);
1682 }
1683 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001684 }
1685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 no_pp_list->line(LIST_READ, buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001687
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001688 return buffer;
1689}
1690
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691static void no_pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001692{
Keith Kaniosc7ae18d2007-04-14 00:46:25 +00001693 (void)pass; /* placate GCC */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001694 fclose(no_pp_fp);
1695}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001696
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001697static uint32_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001698{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001699
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 if (!strcmp(value, "8086"))
1701 return IF_8086;
1702 if (!strcmp(value, "186"))
1703 return IF_186;
1704 if (!strcmp(value, "286"))
1705 return IF_286;
1706 if (!strcmp(value, "386"))
1707 return IF_386;
1708 if (!strcmp(value, "486"))
1709 return IF_486;
1710 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1711 return IF_PENT;
1712 if (!strcmp(value, "686") ||
1713 !nasm_stricmp(value, "ppro") ||
1714 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1715 return IF_P6;
1716 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1717 return IF_KATMAI;
1718 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1719 !nasm_stricmp(value, "willamette"))
1720 return IF_WILLAMETTE;
1721 if (!nasm_stricmp(value, "prescott"))
1722 return IF_PRESCOTT;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001723 if (!nasm_stricmp(value, "x64") ||
1724 !nasm_stricmp(value, "x86-64"))
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001725 return IF_X86_64;
H. Peter Anvin3ab8de62002-05-28 01:25:06 +00001726 if (!nasm_stricmp(value, "ia64") ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 !nasm_stricmp(value, "ia-64") ||
1728 !nasm_stricmp(value, "itanium") ||
1729 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1730 return IF_IA64;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001731
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1733 "unknown 'cpu' type");
H. Peter Anvin734b1882002-04-30 21:01:08 +00001734
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 return IF_PLEVEL; /* the maximum level */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001736}
1737
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001738static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001739{
1740 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001741
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 if ((i = atoi(value)) == 16)
1743 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001744 else if (i == 32) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 if (cpu < IF_386) {
1746 report_error(ERR_NONFATAL,
1747 "cannot specify 32-bit segment on processor below a 386");
1748 i = 16;
1749 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001750 } else if (i == 64) {
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001751 if (cpu < IF_X86_64) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001752 report_error(ERR_NONFATAL,
1753 "cannot specify 64-bit segment on processor below an x86-64");
1754 i = 16;
1755 }
1756 if (i != maxbits) {
1757 report_error(ERR_NONFATAL,
1758 "%s output format does not support 64-bit code",
1759 ofmt->shortname);
1760 i = 16;
1761 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001762 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001764 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 value);
1766 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001767 }
1768 return i;
1769}
1770
1771/* end of nasm.c */