blob: 74fcc08a684fb47c80f06f32407fe3bdbe5f062e [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>
14
15#include "nasm.h"
16#include "nasmlib.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000017#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000018#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000019#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000020#include "assemble.h"
21#include "labels.h"
22#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000023#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000024
H. Peter Anvineba20a72002-04-30 20:53:55 +000025struct forwrefinfo { /* info held on forward refs. */
26 int lineno;
27 int operand;
28};
29
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000030static void report_error (int, char *, ...);
31static void parse_cmdline (int, char **);
32static void assemble_file (char *);
33static int getkw (char *buf, char **value);
34static void register_output_formats(void);
35static void usage(void);
36
H. Peter Anvineba20a72002-04-30 20:53:55 +000037static int using_debug_info;
38
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000039static char inname[FILENAME_MAX];
40static char outname[FILENAME_MAX];
H. Peter Anvin6768eb72002-04-30 20:52:26 +000041static char listname[FILENAME_MAX];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000042static int globallineno; /* for forward-reference tracking */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000043static int pass;
44static struct ofmt *ofmt = NULL;
45
H. Peter Anvinef7468f2002-04-30 20:57:59 +000046static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +000047
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048static FILE *ofile = NULL;
49static int sb = 16; /* by default */
50
H. Peter Anvineba20a72002-04-30 20:53:55 +000051static loc_t location;
52int in_abs_seg; /* Flag we are in ABSOLUTE seg */
53static long abs_seg;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000054
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055static struct RAA *offsets;
56static long abs_offset;
H. Peter Anvinea838272002-04-30 20:51:53 +000057
58static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvineba20a72002-04-30 20:53:55 +000059static struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000061static Preproc *preproc;
H. Peter Anvin620515a2002-04-30 20:57:38 +000062enum op_type {
63 op_normal, /* Preprocess and assemble */
64 op_preprocess, /* Preprocess only */
65 op_depend /* Generate dependencies */
66};
67static enum op_type operating_mode;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000068
69/* used by error function to report location */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000070
71/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +000072 * Which of the suppressible warnings are suppressed. Entry zero
73 * doesn't do anything. Initial defaults are given here.
74 */
75static char suppressed[1+ERR_WARN_MAX] = {
H. Peter Anvineba20a72002-04-30 20:53:55 +000076 0, TRUE, TRUE, FALSE
H. Peter Anvin6768eb72002-04-30 20:52:26 +000077};
78
79/*
80 * The option names for the suppressible warnings. As before, entry
81 * zero does nothing.
82 */
83static char *suppressed_names[1+ERR_WARN_MAX] = {
H. Peter Anvin76690a12002-04-30 20:52:49 +000084 NULL, "macro-params", "orphan-labels", "number-overflow"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000085};
86
87/*
88 * The explanations for the suppressible warnings. As before, entry
89 * zero does nothing.
90 */
91static char *suppressed_what[1+ERR_WARN_MAX] = {
92 NULL, "macro calls with wrong no. of params",
H. Peter Anvin76690a12002-04-30 20:52:49 +000093 "labels alone on lines without trailing `:'",
94 "numeric constants greater than 0xFFFFFFFF"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000095};
96
97/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000098 * This is a null preprocessor which just copies lines from input
99 * to output. It's used when someone explicitly requests that NASM
100 * not preprocess their source file.
101 */
102
H. Peter Anvin76690a12002-04-30 20:52:49 +0000103static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104static char *no_pp_getline (void);
105static void no_pp_cleanup (void);
106static Preproc no_pp = {
107 no_pp_reset,
108 no_pp_getline,
109 no_pp_cleanup
110};
111
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000112/*
113 * get/set current offset...
114 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000115#define get_curr_ofs (in_abs_seg?abs_offset:\
116 raa_read(offsets,location.segment))
117#define set_curr_ofs(x) (in_abs_seg?(void)(abs_offset=(x)):\
118 (void)(offsets=raa_write(offsets,location.segment,(x))))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000119
120static int want_usage;
121static int terminate_after_phase;
122
H. Peter Anvineba20a72002-04-30 20:53:55 +0000123static void nasm_fputs(char *line, FILE *ofile)
124{
125 if (ofile) {
126 fputs(line, ofile);
127 fputc('\n', ofile);
128 } else
129 puts(line);
130}
131
132int main(int argc, char **argv)
133{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000134 want_usage = terminate_after_phase = FALSE;
135
136 nasm_set_malloc_error (report_error);
137 offsets = raa_init();
H. Peter Anvineba20a72002-04-30 20:53:55 +0000138 forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000139
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000140 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000141 operating_mode = op_normal;
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000142
143 error_file = stderr;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000145 seg_init();
146
147 register_output_formats();
148
149 parse_cmdline(argc, argv);
150
H. Peter Anvineba20a72002-04-30 20:53:55 +0000151 if (terminate_after_phase)
152 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000153 if (want_usage)
154 usage();
155 return 1;
156 }
157
H. Peter Anvin76690a12002-04-30 20:52:49 +0000158 if (ofmt->stdmac)
159 pp_extra_stdmac (ofmt->stdmac);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000160 parser_global_info (ofmt, &location);
161 eval_global_info (ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000162
H. Peter Anvin620515a2002-04-30 20:57:38 +0000163 switch ( operating_mode ) {
164 case op_depend:
165 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000166 char *line;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000167 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
168 if (outname[0] == '\0')
169 ofmt->filename (inname, outname, report_error);
170 ofile = NULL;
171 printf("%s: %s", outname, inname);
172 while ( (line = preproc->getline()) )
173 nasm_free (line);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174 preproc->cleanup();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000175 putc('\n', stdout);
176 }
177 break;
178
179 case op_preprocess:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000180 {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000181 char *line;
182 char *file_name = NULL;
183 long prior_linnum=0;
184 int lineinc=0;
185
186 if (*outname) {
187 ofile = fopen(outname, "w");
188 if (!ofile)
189 report_error (ERR_FATAL | ERR_NOFILE,
190 "unable to open output file `%s'", outname);
191 } else
192 ofile = NULL;
193
194 location.known = FALSE;
195
196 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
197 while ( (line = preproc->getline()) ) {
198 /*
199 * We generate %line directives if needed for later programs
200 */
201 long linnum = prior_linnum += lineinc;
202 int altline = src_get(&linnum, &file_name);
203 if (altline) {
204 if (altline==1 && lineinc==1)
205 nasm_fputs("", ofile);
206 else {
207 lineinc = (altline != -1 || lineinc!=1);
208 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
209 linnum, lineinc, file_name);
210 }
211 prior_linnum = linnum;
212 }
213 nasm_fputs(line, ofile);
214 nasm_free (line);
215 }
216 nasm_free(file_name);
217 preproc->cleanup();
218 if (ofile)
219 fclose(ofile);
220 if (ofile && terminate_after_phase)
221 remove(outname);
222 }
223 break;
224
225 case op_normal:
226 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000227 /*
228 * We must call ofmt->filename _anyway_, even if the user
229 * has specified their own output file, because some
230 * formats (eg OBJ and COFF) use ofmt->filename to find out
231 * the name of the input file and then put that inside the
232 * file.
233 */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000234 ofmt->filename (inname, outname, report_error);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000235
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000236 ofile = fopen(outname, "wb");
237 if (!ofile) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000238 report_error (ERR_FATAL | ERR_NOFILE,
239 "unable to open output file `%s'", outname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000240 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000241
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000242 /*
243 * We must call init_labels() before ofmt->init() since
244 * some object formats will want to define labels in their
245 * init routines. (eg OS/2 defines the FLAT group)
246 */
247 init_labels ();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000248
H. Peter Anvin76690a12002-04-30 20:52:49 +0000249 ofmt->init (ofile, report_error, define_label, evaluate);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000250
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000251 assemble_file (inname);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000252
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000253 if (!terminate_after_phase) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000254 ofmt->cleanup (using_debug_info);
255 cleanup_labels ();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000256 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000257 else {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000258
259 /*
260 * We had an fclose on the output file here, but we
261 * actually do that in all the object file drivers as well,
262 * so we're leaving out the one here.
263 * fclose (ofile);
264 */
265
266 remove(outname);
267 if (listname[0])
268 remove(listname);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000269 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000270 }
271 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000272 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000273
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000274 if (want_usage)
H. Peter Anvin620515a2002-04-30 20:57:38 +0000275 usage();
276
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277 raa_free (offsets);
278 saa_free (forwrefs);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000279 eval_cleanup ();
280 nasmlib_cleanup ();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000281
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000282 if (terminate_after_phase)
283 return 1;
284 else
285 return 0;
286}
287
H. Peter Anvineba20a72002-04-30 20:53:55 +0000288
289/*
290 * Get a parameter for a command line option.
291 * First arg must be in the form of e.g. -f...
292 */
293static char *get_param (char *p, char *q, int *advance)
294{
295 *advance = 0;
296 if (p[2]) /* the parameter's in the option */
297 {
298 p += 2;
299 while (isspace(*p))
300 p++;
301 return p;
302 }
303 if (q && q[0])
304 {
305 *advance = 1;
306 return q;
307 }
308 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
309 "option `-%c' requires an argument",
310 p[1]);
311 return NULL;
312}
313
314int stopoptions = 0;
315static int process_arg (char *p, char *q)
316{
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000317 char *param;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000318 int i, advance = 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000319
320 if (!p || !p[0])
321 return 0;
322
H. Peter Anvineba20a72002-04-30 20:53:55 +0000323 if (p[0]=='-' && ! stopoptions)
324 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000325 switch (p[1]) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000326 case '-': /* -- => stop processing options */
327 stopoptions = 1;
328 break;
329 case 's': /* silently ignored for compatibility */
330 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 case 'o': /* these parameters take values */
332 case 'f':
333 case 'p':
334 case 'd':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000335 case 'D':
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000336 case 'i':
337 case 'l':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000338 case 'E':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000339 case 'F':
340 if ( !(param = get_param (p, q, &advance)) )
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000341 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000342 if (p[1]=='o') { /* output file */
343 strcpy (outname, param);
344 } else if (p[1]=='f') { /* output format */
345 ofmt = ofmt_find(param);
346 if (!ofmt) {
347 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000348 "unrecognised output format `%s' - "
349 "use -hf for a list",
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000350 param);
351 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000352 else
353 ofmt->current_dfmt = ofmt->debug_formats[0];
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000354 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000355 pp_pre_include (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000356 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000357 pp_pre_define (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000358 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
359 pp_pre_undefine (param);
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000360 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000361 pp_include_path (param);
362 } else if (p[1]=='l') { /* listing file */
363 strcpy (listname, param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000364 } else if (p[1]=='E') { /* error messages file */
365 error_file = fopen(param, "wt");
366 if ( !error_file ) {
367 error_file = stderr; /* Revert to default! */
368 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
369 "cannot open file `%s' for error messages",
370 param);
371 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000372 } else if (p[1] == 'F') { /* specify debug format */
373 ofmt->current_dfmt = dfmt_find(ofmt, param);
374 if (!ofmt->current_dfmt) {
375 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
376 "unrecognized debug format `%s' for"
377 " output format `%s'",
378 param, ofmt->shortname);
379 }
380 }
381 break;
382 case 'g':
383 using_debug_info = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000384 break;
385 case 'h':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000386 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
387 "[-l listfile]\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000388 " [options...] [--] filename\n"
389 " or nasm -r for version info\n\n"
390 " -e preprocess only (writes output to stdout by default)\n"
391 " -a don't preprocess (assemble only)\n"
392 " -M generate Makefile dependencies on stdout\n\n"
393 " -E<file> redirect error messages to file\n\n"
394 " -g enable debug info\n"
395 " -F format select a debugging format\n\n"
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000396 " -I<path> adds a pathname to the include file path\n"
397 " -P<file> pre-includes a file\n"
398 " -D<macro>[=<value>] pre-defines a macro\n"
399 " -U<macro> undefines a macro\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000400 " -w+foo enables warnings about foo; -w-foo disables them\n"
401 "where foo can be:\n");
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000402 for (i=1; i<=ERR_WARN_MAX; i++)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000403 printf(" %-16s%s (default %s)\n",
404 suppressed_names[i], suppressed_what[i],
405 suppressed[i] ? "off" : "on");
406 printf ("\nresponse files should contain command line parameters"
407 ", one per line.\n");
408 if (p[2] == 'f') {
409 printf("\nvalid output formats for -f are"
410 " (`*' denotes default):\n");
411 ofmt_list(ofmt, stdout);
412 }
413 else {
414 printf ("\nFor a list of valid output formats, use -hf.\n");
415 printf ("For a list of debug formats, use -f <form> -y.\n");
416 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000417 exit (0); /* never need usage message here */
418 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000419 case 'y':
420 printf("\nvalid debug formats for '%s' output format are"
421 " ('*' denotes default):\n",
422 ofmt->shortname);
423 dfmt_list(ofmt, stdout);
424 exit(0);
425 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000426 case 'r':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000427 printf("NASM version %s\n", NASM_VER);
428#ifdef DEBUG
429 printf("Compiled with -DDEBUG on " __DATE__ "\n");
430#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000431 exit (0); /* never need usage message here */
432 break;
433 case 'e': /* preprocess only */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000434 operating_mode = op_preprocess;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000435 break;
436 case 'a': /* assemble only - don't preprocess */
437 preproc = &no_pp;
438 break;
439 case 'w':
440 if (p[2] != '+' && p[2] != '-') {
441 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
442 "invalid option to `-w'");
443 } else {
444 for (i=1; i<=ERR_WARN_MAX; i++)
445 if (!nasm_stricmp(p+3, suppressed_names[i]))
446 break;
447 if (i <= ERR_WARN_MAX)
448 suppressed[i] = (p[2] == '-');
449 else
450 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
451 "invalid option to `-w'");
452 }
453 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000454 case 'M':
455 operating_mode = op_depend;
456 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000457 default:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000458 if (!ofmt->setinfo(GI_SWITCH,&p))
459 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000460 "unrecognised option `-%c'",
461 p[1]);
462 break;
463 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000464 }
465 else
466 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000467 if (*inname) {
468 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
469 "more than one input file specified");
470 } else
471 strcpy(inname, p);
472 }
473
474 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000475}
476
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477#define ARG_BUF_DELTA 128
478
479static void process_respfile (FILE *rfile)
480{
481 char *buffer, *p, *q, *prevarg;
482 int bufsize, prevargsize;
483
484 bufsize = prevargsize = ARG_BUF_DELTA;
485 buffer = nasm_malloc(ARG_BUF_DELTA);
486 prevarg = nasm_malloc(ARG_BUF_DELTA);
487 prevarg[0] = '\0';
488
489 while (1) { /* Loop to handle all lines in file */
490
491 p = buffer;
492 while (1) { /* Loop to handle long lines */
493 q = fgets(p, bufsize-(p-buffer), rfile);
494 if (!q)
495 break;
496 p += strlen(p);
497 if (p > buffer && p[-1] == '\n')
498 break;
499 if (p-buffer > bufsize-10) {
500 int offset;
501 offset = p - buffer;
502 bufsize += ARG_BUF_DELTA;
503 buffer = nasm_realloc(buffer, bufsize);
504 p = buffer + offset;
505 }
506 }
507
508 if (!q && p == buffer) {
509 if (prevarg[0])
510 process_arg (prevarg, NULL);
511 nasm_free (buffer);
512 nasm_free (prevarg);
513 return;
514 }
515
516 /*
517 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
518 * them are present at the end of the line.
519 */
520 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
521
522 while (p > buffer && isspace(p[-1]))
523 *--p = '\0';
524
525 p = buffer;
526 while (isspace(*p))
527 p++;
528
529 if (process_arg (prevarg, p))
530 *p = '\0';
531
532 if (strlen(p) > prevargsize-10) {
533 prevargsize += ARG_BUF_DELTA;
534 prevarg = nasm_realloc(prevarg, prevargsize);
535 }
536 strcpy (prevarg, p);
537 }
538}
539
540static void parse_cmdline(int argc, char **argv)
541{
542 FILE *rfile;
543 char *envreal, *envcopy=NULL, *p, *q, *arg, *prevarg;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000544 char separator = ' ';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000545
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000546 *inname = *outname = *listname = '\0';
547
548 /*
549 * First, process the NASM environment variable.
550 */
551 envreal = getenv("NASM");
552 arg = NULL;
553 if (envreal) {
554 envcopy = nasm_strdup(envreal);
555 p = envcopy;
556 if (*p && *p != '-')
557 separator = *p++;
558 while (*p) {
559 q = p;
560 while (*p && *p != separator) p++;
561 while (*p == separator) *p++ = '\0';
562 prevarg = arg;
563 arg = q;
564 if (process_arg (prevarg, arg))
565 arg = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000566 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000567 if (arg)
568 process_arg (arg, NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000569 nasm_free (envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000570 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000571
572 /*
573 * Now process the actual command line.
574 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575 while (--argc)
576 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000577 int i;
578 argv++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
580 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i)))
581 if ((rfile = fopen(p, "r"))) {
582 process_respfile (rfile);
583 fclose(rfile);
584 } else
585 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
586 "unable to open response file `%s'", p);
587 } else
588 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000589 argv += i, argc -= i;
590 }
591
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000592 if (!*inname)
593 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
594 "no input file specified");
595}
596
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597static void assemble_file (char *fname)
598{
599 char * value, * p, * q, * special, * line, debugid[80];
600 insn output_ins;
601 int i, rn_error, validid;
602 long seg, offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000603 struct tokenval tokval;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604 expr * e;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000605
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606 /*
607 * pass one
608 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000609 pass = 1;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610 in_abs_seg = FALSE;
611 location.segment = ofmt->section(NULL, pass, &sb);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000612 preproc->reset(fname, 1, report_error, evaluate, &nasmlist);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613 globallineno = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614 location.known = TRUE;
615 location.offset = offs = get_curr_ofs;
616
617 while ( (line = preproc->getline()) )
618 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000619 globallineno++;
620
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000621 /* here we parse our directives; this is not handled by the 'real'
622 * parser. */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623 if ( (i = getkw (line, &value)) )
624 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000625 switch (i) {
626 case 1: /* [SEGMENT n] */
627 seg = ofmt->section (value, pass, &sb);
628 if (seg == NO_SEG) {
629 report_error (ERR_NONFATAL,
630 "segment name `%s' not recognised",
631 value);
632 } else {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000633 in_abs_seg = FALSE;
634 location.segment = seg;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000635 }
636 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000637 case 2: /* [EXTERN label:special] */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000638 if (*value == '$')
639 value++; /* skip initial $ if present */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000640 q = value;
641 validid = TRUE;
642 if (!isidstart(*q))
643 validid = FALSE;
644 while (*q && *q != ':') {
645 if (!isidchar(*q))
646 validid = FALSE;
647 q++;
648 }
649 if (!validid) {
650 report_error (ERR_NONFATAL,
651 "identifier expected after EXTERN");
652 break;
653 }
654 if (*q == ':') {
655 *q++ = '\0';
656 special = q;
657 } else
658 special = NULL;
659 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
660 declare_as_global (value, special, report_error);
661 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
662 ofmt, report_error);
663 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000664 break;
665 case 3: /* [BITS bits] */
666 switch (atoi(value)) {
667 case 16:
668 case 32:
669 sb = atoi(value);
670 break;
671 default:
672 report_error(ERR_NONFATAL,
673 "`%s' is not a valid argument to [BITS]",
674 value);
675 break;
676 }
677 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000678 case 4: /* [GLOBAL symbol:special] */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000679 if (*value == '$')
680 value++; /* skip initial $ if present */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000681 q = value;
682 validid = TRUE;
683 if (!isidstart(*q))
684 validid = FALSE;
685 while (*q && *q != ':') {
686 if (!isidchar(*q))
687 validid = FALSE;
688 q++;
689 }
690 if (!validid) {
691 report_error (ERR_NONFATAL,
692 "identifier expected after GLOBAL");
693 break;
694 }
695 if (*q == ':') {
696 *q++ = '\0';
697 special = q;
698 } else
699 special = NULL;
700 declare_as_global (value, special, report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000701 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000702 case 5: /* [COMMON symbol size:special] */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000703 p = value;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000704 validid = TRUE;
705 if (!isidstart(*p))
706 validid = FALSE;
707 while (*p && !isspace(*p)) {
708 if (!isidchar(*p))
709 validid = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000710 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000711 }
712 if (!validid) {
713 report_error (ERR_NONFATAL,
714 "identifier expected after COMMON");
715 break;
716 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000717 if (*p) {
718 long size;
719
720 while (*p && isspace(*p))
721 *p++ = '\0';
H. Peter Anvin76690a12002-04-30 20:52:49 +0000722 q = p;
723 while (*q && *q != ':')
724 q++;
725 if (*q == ':') {
726 *q++ = '\0';
727 special = q;
728 } else
729 special = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000730 size = readnum (p, &rn_error);
731 if (rn_error)
732 report_error (ERR_NONFATAL, "invalid size specified"
733 " in COMMON declaration");
734 else
735 define_common (value, seg_alloc(), size,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000736 special, ofmt, report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000737 } else
738 report_error (ERR_NONFATAL, "no size specified in"
739 " COMMON declaration");
740 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000741 case 6: /* [ABSOLUTE address] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000742 stdscan_reset();
743 stdscan_bufptr = value;
744 tokval.t_type = TOKEN_INVALID;
745 e = evaluate(stdscan, NULL, &tokval, NULL, 1, report_error,
746 NULL);
747 if (e) {
748 if (!is_reloc(e))
749 report_error (ERR_NONFATAL, "cannot use non-"
750 "relocatable expression as ABSOLUTE"
751 " address");
752 else {
753 abs_seg = reloc_seg(e);
754 abs_offset = reloc_value(e);
755 }
756 } else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000757 abs_offset = 0x100;/* don't go near zero in case of / */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000758 in_abs_seg = TRUE;
759 location.segment = abs_seg;
760 break;
761 case 7:
762 p = value;
763 validid = TRUE;
764 if (!isidstart(*p))
765 validid = FALSE;
766 while (*p && !isspace(*p)) {
767 if (!isidchar(*p))
768 validid = FALSE;
769 p++;
770 }
771 if (!validid) {
772 report_error (ERR_NONFATAL,
773 "identifier expected after DEBUG");
774 break;
775 }
776 while (*p && isspace(*p)) p++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000777 break;
778 default:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000779 if (!ofmt->directive (line+1, value, 1))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000780 report_error (ERR_NONFATAL, "unrecognised directive [%s]",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000781 line+1);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000782 break;
783 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000784 }
785 else /* it isn't a directive */
786 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000787 parse_line (1, line, &output_ins,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000788 report_error, evaluate, define_label);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000789
H. Peter Anvineba20a72002-04-30 20:53:55 +0000790 if (output_ins.forw_ref)
791 {
792 for(i = 0; i < output_ins.operands; i++)
793 {
794 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
795 {
796 struct forwrefinfo *fwinf =
797 (struct forwrefinfo *)saa_wstruct(forwrefs);
798 fwinf->lineno = globallineno;
799 fwinf->operand = i;
800 }
801 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000802 }
803
H. Peter Anvineba20a72002-04-30 20:53:55 +0000804 if (output_ins.opcode == I_EQU)
805 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000806 /*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000807 * Special `..' EQUs get processed in pass two,
808 * except `..@' macro-processor EQUs which are done
809 * in the normal place.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810 */
811 if (!output_ins.label)
812 report_error (ERR_NONFATAL,
813 "EQU not preceded by label");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000814
815 /*
816 * EQU cannot be used to declare a label relative to
817 * an external symbol.
818 */
819 else if ((output_ins.oprs[0].opflags & OPFLAG_EXTERN)
820 || (output_ins.operands > 1
821 && (output_ins.oprs[1].opflags & OPFLAG_EXTERN)))
822 {
823 report_error (ERR_NONFATAL,
824 "EQU used relative to external symbol");
825 }
826
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000827 else if (output_ins.label[0] != '.' ||
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000828 output_ins.label[1] != '.' ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000829 output_ins.label[2] == '@')
830 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000831 if (output_ins.operands == 1 &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000832 (output_ins.oprs[0].type & IMMEDIATE) &&
H. Peter Anvineba20a72002-04-30 20:53:55 +0000833 output_ins.oprs[0].wrt == NO_SEG)
834 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000835 define_label (output_ins.label,
836 output_ins.oprs[0].segment,
837 output_ins.oprs[0].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000838 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000839 }
840 else if (output_ins.operands == 2 &&
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000841 (output_ins.oprs[0].type & IMMEDIATE) &&
842 (output_ins.oprs[0].type & COLON) &&
843 output_ins.oprs[0].segment == NO_SEG &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000844 output_ins.oprs[0].wrt == NO_SEG &&
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000845 (output_ins.oprs[1].type & IMMEDIATE) &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000846 output_ins.oprs[1].segment == NO_SEG &&
H. Peter Anvineba20a72002-04-30 20:53:55 +0000847 output_ins.oprs[1].wrt == NO_SEG)
848 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000849 define_label (output_ins.label,
850 output_ins.oprs[0].offset | SEG_ABS,
851 output_ins.oprs[1].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000852 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000853 }
854 else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000855 report_error(ERR_NONFATAL, "bad syntax for EQU");
856 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000857 }
858 else /* instruction isn't an EQU */
859 {
860 long l = insn_size (location.segment, offs, sb,
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000861 &output_ins, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000862 if (using_debug_info && output_ins.opcode != -1) {
863 /* this is done here so we can do debug type info */
864 long typeinfo = TYS_ELEMENTS(output_ins.operands);
865 switch (output_ins.opcode) {
866 case I_RESB:
867 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
868 break;
869 case I_RESW:
870 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
871 break;
872 case I_RESD:
873 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
874 break;
875 case I_RESQ:
876 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
877 break;
878 case I_REST:
879 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
880 break;
881 case I_DB:
882 typeinfo |= TY_BYTE;
883 break;
884 case I_DW:
885 typeinfo |= TY_WORD;
886 break;
887 case I_DD:
888 if (output_ins.eops_float)
889 typeinfo |= TY_FLOAT;
890 else
891 typeinfo |= TY_DWORD;
892 break;
893 case I_DQ:
894 typeinfo |= TY_QWORD;
895 break;
896 case I_DT:
897 typeinfo |= TY_TBYTE;
898 break;
899 default:
900 typeinfo = TY_LABEL;
901 }
902 ofmt->current_dfmt->debug_typevalue(typeinfo);
903 }
904 if (l != -1) {
905 offs += l;
906 set_curr_ofs (offs);
907 }
908 /*
909 * else l == -1 => invalid instruction, which will be
910 * flagged as an error on pass 2
911 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000912 }
913 cleanup_insn (&output_ins);
914 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000915 nasm_free (line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000916 location.offset = offs = get_curr_ofs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000917 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000918
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000919 preproc->cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000920
921 if (terminate_after_phase) {
922 fclose(ofile);
923 remove(outname);
924 if (want_usage)
925 usage();
926 exit (1);
927 }
928
H. Peter Anvineba20a72002-04-30 20:53:55 +0000929 /*
930 * pass two
931 */
932
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000933 pass = 2;
H. Peter Anvinea838272002-04-30 20:51:53 +0000934 saa_rewind (forwrefs);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000935 if (*listname)
936 nasmlist.init(listname, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000937 forwref = saa_rstruct (forwrefs);
938 in_abs_seg = FALSE;
939 location.segment = ofmt->section(NULL, pass, &sb);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000940 raa_free (offsets);
941 offsets = raa_init();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000942 preproc->reset(fname, 2, report_error, evaluate, &nasmlist);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000943 globallineno = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000944 location.offset = offs = get_curr_ofs;
945
946 while ( (line = preproc->getline()) )
947 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000948 globallineno++;
949
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000950 /* here we parse our directives; this is not handled by
951 * the 'real' parser. */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000952 if ( (i = getkw (line, &value)) ) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000953 switch (i) {
954 case 1: /* [SEGMENT n] */
955 seg = ofmt->section (value, pass, &sb);
956 if (seg == NO_SEG) {
957 report_error (ERR_PANIC,
958 "invalid segment name on pass two");
959 } else
H. Peter Anvineba20a72002-04-30 20:53:55 +0000960 in_abs_seg = FALSE;
961 location.segment = seg;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000962 break;
963 case 2: /* [EXTERN label] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000964 q = value;
965 while (*q && *q != ':')
966 q++;
967 if (*q == ':') {
968 *q++ = '\0';
969 ofmt->symdef(value, 0L, 0L, 3, q);
970 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000971 break;
972 case 3: /* [BITS bits] */
973 switch (atoi(value)) {
974 case 16:
975 case 32:
976 sb = atoi(value);
977 break;
978 default:
979 report_error(ERR_PANIC,
980 "invalid [BITS] value on pass two",
981 value);
982 break;
983 }
984 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000985 case 4: /* [GLOBAL symbol] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000986 q = value;
987 while (*q && *q != ':')
988 q++;
989 if (*q == ':') {
990 *q++ = '\0';
991 ofmt->symdef(value, 0L, 0L, 3, q);
992 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000993 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000994 case 5: /* [COMMON symbol size] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000995 q = value;
996 while (*q && *q != ':') {
997 if (isspace(*q))
998 *q = '\0';
999 q++;
1000 }
1001 if (*q == ':') {
1002 *q++ = '\0';
1003 ofmt->symdef(value, 0L, 0L, 3, q);
1004 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001005 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001006 case 6: /* [ABSOLUTE addr] */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001007 stdscan_reset();
1008 stdscan_bufptr = value;
1009 tokval.t_type = TOKEN_INVALID;
1010 e = evaluate(stdscan, NULL, &tokval, NULL, 2, report_error,
1011 NULL);
1012 if (e) {
1013 if (!is_reloc(e))
1014 report_error (ERR_PANIC, "non-reloc ABSOLUTE address"
1015 " in pass two");
1016 else {
1017 abs_seg = reloc_seg(e);
1018 abs_offset = reloc_value(e);
1019 }
1020 } else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001021 report_error (ERR_PANIC, "invalid ABSOLUTE address "
1022 "in pass two");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001023 in_abs_seg = TRUE;
1024 location.segment = abs_seg;
1025 break;
1026 case 7:
1027 p = value;
1028 q = debugid;
1029 validid = TRUE;
1030 if (!isidstart(*p))
1031 validid = FALSE;
1032 while (*p && !isspace(*p)) {
1033 if (!isidchar(*p))
1034 validid = FALSE;
1035 *q++ = *p++;
1036 }
1037 *q++ = 0;
1038 if (!validid) {
1039 report_error (ERR_PANIC,
1040 "identifier expected after DEBUG in pass 2");
1041 break;
1042 }
1043 while (*p && isspace(*p))
1044 p++;
1045 ofmt->current_dfmt->debug_directive (debugid, p);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001046 break;
1047 default:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001048 if (!ofmt->directive (line+1, value, 2))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001049 report_error (ERR_PANIC, "invalid directive on pass two");
1050 break;
1051 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052 }
1053 else /* not a directive */
1054 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001055 parse_line (2, line, &output_ins,
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056 report_error, evaluate, redefine_label);
1057 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvinea838272002-04-30 20:51:53 +00001058 output_ins.forw_ref = TRUE;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059 do {
1060 output_ins.oprs[forwref->operand].opflags|= OPFLAG_FORWARD;
1061 forwref = saa_rstruct (forwrefs);
1062 } while (forwref != NULL && forwref->lineno == globallineno);
H. Peter Anvinea838272002-04-30 20:51:53 +00001063 } else
1064 output_ins.forw_ref = FALSE;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001065
1066 /*
1067 * Hack to prevent phase error in the code
1068 * rol ax,x
1069 * x equ 1
H. Peter Anvineba20a72002-04-30 20:53:55 +00001070 *
1071 * If the second operand is a forward reference,
1072 * the UNITY property of the number 1 in that
1073 * operand is cancelled. Otherwise the above
1074 * sequence will cause a phase error.
1075 *
1076 * This hack means that the above code will
1077 * generate 286+ code.
1078 *
1079 * The forward reference will mean that the
1080 * operand will not have the UNITY property on
1081 * the first pass, so the pass behaviours will
1082 * be consistent.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001083 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001084
1085 if (output_ins.forw_ref &&
1086 output_ins.operands >= 2 &&
1087 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1088 {
1089 output_ins.oprs[1].type &= ~ONENESS;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001090 }
1091
H. Peter Anvineba20a72002-04-30 20:53:55 +00001092 if (output_ins.opcode == I_EQU)
1093 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001094 /*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001095 * Special `..' EQUs get processed here, except
1096 * `..@' macro processor EQUs which are done above.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001097 */
1098 if (output_ins.label[0] == '.' &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001099 output_ins.label[1] == '.' &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00001100 output_ins.label[2] != '@')
1101 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001102 if (output_ins.operands == 1 &&
1103 (output_ins.oprs[0].type & IMMEDIATE)) {
1104 define_label (output_ins.label,
1105 output_ins.oprs[0].segment,
1106 output_ins.oprs[0].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001107 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001108 }
1109 else if (output_ins.operands == 2 &&
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001110 (output_ins.oprs[0].type & IMMEDIATE) &&
1111 (output_ins.oprs[0].type & COLON) &&
1112 output_ins.oprs[0].segment == NO_SEG &&
1113 (output_ins.oprs[1].type & IMMEDIATE) &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00001114 output_ins.oprs[1].segment == NO_SEG)
1115 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001116 define_label (output_ins.label,
1117 output_ins.oprs[0].offset | SEG_ABS,
1118 output_ins.oprs[1].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001119 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001120 }
1121 else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001122 report_error(ERR_NONFATAL, "bad syntax for EQU");
1123 }
1124 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001125 offs += assemble (location.segment, offs, sb,
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001126 &output_ins, ofmt, report_error, &nasmlist);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001127 cleanup_insn (&output_ins);
1128 set_curr_ofs (offs);
1129 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001130
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001131 nasm_free (line);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001132
H. Peter Anvineba20a72002-04-30 20:53:55 +00001133 location.offset = offs = get_curr_ofs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001134 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001135
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001136 preproc->cleanup();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001137 nasmlist.cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001138}
1139
H. Peter Anvineba20a72002-04-30 20:53:55 +00001140static int getkw (char *buf, char **value)
1141{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001142 char *p, *q;
1143
1144 if (*buf!='[')
1145 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001146
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001147 p = buf;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001148
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001149 while (*p && *p != ']') p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001150
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001151 if (!*p)
1152 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001153
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001154 q = p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001155
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001156 while (*p && *p != ';') {
1157 if (!isspace(*p))
1158 return 0;
1159 p++;
1160 }
1161 q[1] = '\0';
1162
1163 p = buf+1;
1164 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1165 buf++;
1166 if (*buf==']') {
1167 *buf = '\0';
1168 *value = buf;
1169 } else {
1170 *buf++ = '\0';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001171 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001172 *value = buf;
1173 while (*buf!=']') buf++;
1174 *buf++ = '\0';
1175 }
1176 for (q=p; *q; q++)
1177 *q = tolower(*q);
1178 if (!strcmp(p, "segment") || !strcmp(p, "section"))
1179 return 1;
1180 if (!strcmp(p, "extern"))
1181 return 2;
1182 if (!strcmp(p, "bits"))
1183 return 3;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001184 if (!strcmp(p, "global"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001185 return 4;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001186 if (!strcmp(p, "common"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001187 return 5;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001188 if (!strcmp(p, "absolute"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001189 return 6;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001190 if (!strcmp(p, "debug"))
1191 return 7;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001192 return -1;
1193}
1194
H. Peter Anvineba20a72002-04-30 20:53:55 +00001195static void report_error (int severity, char *fmt, ...)
1196{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001197 va_list ap;
1198
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001199 /*
1200 * See if it's a suppressed warning.
1201 */
1202 if ((severity & ERR_MASK) == ERR_WARNING &&
1203 (severity & ERR_WARN_MASK) != 0 &&
1204 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1205 return; /* and bail out if so */
1206
H. Peter Anvin76690a12002-04-30 20:52:49 +00001207 /*
1208 * See if it's a pass-one only warning and we're not in pass one.
1209 */
1210 if ((severity & ERR_PASS1) && pass != 1)
1211 return;
1212
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001213 if (severity & ERR_NOFILE)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001214 fputs ("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001215 else {
1216 char * currentfile = NULL;
1217 long lineno = 0;
1218 src_get (&lineno, &currentfile);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001219 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220 nasm_free (currentfile);
1221 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001222
1223 if ( (severity & ERR_MASK) == ERR_WARNING)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001224 fputs ("warning: ", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001225 else if ( (severity & ERR_MASK) == ERR_PANIC)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001226 fputs ("panic: ", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001227
1228 va_start (ap, fmt);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001229 vfprintf (error_file, fmt, ap);
1230 fputc ('\n', error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001231
1232 if (severity & ERR_USAGE)
1233 want_usage = TRUE;
1234
1235 switch (severity & ERR_MASK) {
1236 case ERR_WARNING:
1237 /* no further action, by definition */
1238 break;
1239 case ERR_NONFATAL:
1240 terminate_after_phase = TRUE;
1241 break;
1242 case ERR_FATAL:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243 if (ofile) {
1244 fclose(ofile);
1245 remove(outname);
1246 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001247 if (want_usage)
1248 usage();
1249 exit(1); /* instantly die */
1250 break; /* placate silly compilers */
1251 case ERR_PANIC:
H. Peter Anvin620515a2002-04-30 20:57:38 +00001252 fflush(NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001253 abort(); /* halt, catch fire, and dump core */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001254 break;
1255 }
1256}
1257
H. Peter Anvineba20a72002-04-30 20:53:55 +00001258static void usage(void)
1259{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001260 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001261}
1262
H. Peter Anvineba20a72002-04-30 20:53:55 +00001263static void register_output_formats(void)
1264{
1265 ofmt = ofmt_register (report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001266}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267
1268#define BUF_DELTA 512
1269
1270static FILE *no_pp_fp;
1271static efunc no_pp_err;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001272static ListGen *no_pp_list;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001273static long no_pp_lineinc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001274
H. Peter Anvin76690a12002-04-30 20:52:49 +00001275static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276 ListGen *listgen)
1277{
1278 src_set_fname(nasm_strdup(file));
1279 src_set_linnum(0);
1280 no_pp_lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001281 no_pp_err = error;
1282 no_pp_fp = fopen(file, "r");
1283 if (!no_pp_fp)
1284 no_pp_err (ERR_FATAL | ERR_NOFILE,
1285 "unable to open input file `%s'", file);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001286 no_pp_list = listgen;
1287 (void) pass; /* placate compilers */
1288 (void) eval; /* placate compilers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289}
1290
H. Peter Anvineba20a72002-04-30 20:53:55 +00001291static char *no_pp_getline (void)
1292{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293 char *buffer, *p, *q;
1294 int bufsize;
1295
1296 bufsize = BUF_DELTA;
1297 buffer = nasm_malloc(BUF_DELTA);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001298 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1299
1300 while (1) { /* Loop to handle %line */
1301
1302 p = buffer;
1303 while (1) { /* Loop to handle long lines */
1304 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1305 if (!q)
1306 break;
1307 p += strlen(p);
1308 if (p > buffer && p[-1] == '\n')
1309 break;
1310 if (p-buffer > bufsize-10) {
1311 int offset;
1312 offset = p - buffer;
1313 bufsize += BUF_DELTA;
1314 buffer = nasm_realloc(buffer, bufsize);
1315 p = buffer + offset;
1316 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001317 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001318
1319 if (!q && p == buffer) {
1320 nasm_free (buffer);
1321 return NULL;
1322 }
1323
1324 /*
1325 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1326 * them are present at the end of the line.
1327 */
1328 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1329
1330 if (!strncmp(buffer, "%line", 5)) {
1331 long ln;
1332 int li;
1333 char *nm = nasm_malloc(strlen(buffer));
1334 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1335 nasm_free( src_set_fname(nm) );
1336 src_set_linnum(ln);
1337 no_pp_lineinc = li;
1338 continue;
1339 }
1340 nasm_free(nm);
1341 }
1342 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343 }
1344
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345 no_pp_list->line (LIST_READ, buffer);
1346
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347 return buffer;
1348}
1349
H. Peter Anvineba20a72002-04-30 20:53:55 +00001350static void no_pp_cleanup (void)
1351{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001352 fclose(no_pp_fp);
1353}