blob: 0a03c7b9549561c2934f081d41530e7a4b94fbd0 [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 Anvin620515a2002-04-30 20:57:38 +000046static FILE *error_file = stderr; /* Where to write error messages */
47
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;
142
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000144 seg_init();
145
146 register_output_formats();
147
148 parse_cmdline(argc, argv);
149
H. Peter Anvineba20a72002-04-30 20:53:55 +0000150 if (terminate_after_phase)
151 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000152 if (want_usage)
153 usage();
154 return 1;
155 }
156
H. Peter Anvin76690a12002-04-30 20:52:49 +0000157 if (ofmt->stdmac)
158 pp_extra_stdmac (ofmt->stdmac);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000159 parser_global_info (ofmt, &location);
160 eval_global_info (ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000161
H. Peter Anvin620515a2002-04-30 20:57:38 +0000162 switch ( operating_mode ) {
163 case op_depend:
164 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 char *line;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000166 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
167 if (outname[0] == '\0')
168 ofmt->filename (inname, outname, report_error);
169 ofile = NULL;
170 printf("%s: %s", outname, inname);
171 while ( (line = preproc->getline()) )
172 nasm_free (line);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000173 preproc->cleanup();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000174 putc('\n', stdout);
175 }
176 break;
177
178 case op_preprocess:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000179 {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000180 char *line;
181 char *file_name = NULL;
182 long prior_linnum=0;
183 int lineinc=0;
184
185 if (*outname) {
186 ofile = fopen(outname, "w");
187 if (!ofile)
188 report_error (ERR_FATAL | ERR_NOFILE,
189 "unable to open output file `%s'", outname);
190 } else
191 ofile = NULL;
192
193 location.known = FALSE;
194
195 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
196 while ( (line = preproc->getline()) ) {
197 /*
198 * We generate %line directives if needed for later programs
199 */
200 long linnum = prior_linnum += lineinc;
201 int altline = src_get(&linnum, &file_name);
202 if (altline) {
203 if (altline==1 && lineinc==1)
204 nasm_fputs("", ofile);
205 else {
206 lineinc = (altline != -1 || lineinc!=1);
207 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
208 linnum, lineinc, file_name);
209 }
210 prior_linnum = linnum;
211 }
212 nasm_fputs(line, ofile);
213 nasm_free (line);
214 }
215 nasm_free(file_name);
216 preproc->cleanup();
217 if (ofile)
218 fclose(ofile);
219 if (ofile && terminate_after_phase)
220 remove(outname);
221 }
222 break;
223
224 case op_normal:
225 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000226 /*
227 * We must call ofmt->filename _anyway_, even if the user
228 * has specified their own output file, because some
229 * formats (eg OBJ and COFF) use ofmt->filename to find out
230 * the name of the input file and then put that inside the
231 * file.
232 */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000233 ofmt->filename (inname, outname, report_error);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000234
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000235 ofile = fopen(outname, "wb");
236 if (!ofile) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000237 report_error (ERR_FATAL | ERR_NOFILE,
238 "unable to open output file `%s'", outname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000239 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000240
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000241 /*
242 * We must call init_labels() before ofmt->init() since
243 * some object formats will want to define labels in their
244 * init routines. (eg OS/2 defines the FLAT group)
245 */
246 init_labels ();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000247
H. Peter Anvin76690a12002-04-30 20:52:49 +0000248 ofmt->init (ofile, report_error, define_label, evaluate);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000249
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000250 assemble_file (inname);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000251
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000252 if (!terminate_after_phase) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000253 ofmt->cleanup (using_debug_info);
254 cleanup_labels ();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000255 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000256 else {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000257
258 /*
259 * We had an fclose on the output file here, but we
260 * actually do that in all the object file drivers as well,
261 * so we're leaving out the one here.
262 * fclose (ofile);
263 */
264
265 remove(outname);
266 if (listname[0])
267 remove(listname);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000268 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000269 }
270 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000271 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000272
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000273 if (want_usage)
H. Peter Anvin620515a2002-04-30 20:57:38 +0000274 usage();
275
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 raa_free (offsets);
277 saa_free (forwrefs);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000278 eval_cleanup ();
279 nasmlib_cleanup ();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000280
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000281 if (terminate_after_phase)
282 return 1;
283 else
284 return 0;
285}
286
H. Peter Anvineba20a72002-04-30 20:53:55 +0000287
288/*
289 * Get a parameter for a command line option.
290 * First arg must be in the form of e.g. -f...
291 */
292static char *get_param (char *p, char *q, int *advance)
293{
294 *advance = 0;
295 if (p[2]) /* the parameter's in the option */
296 {
297 p += 2;
298 while (isspace(*p))
299 p++;
300 return p;
301 }
302 if (q && q[0])
303 {
304 *advance = 1;
305 return q;
306 }
307 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
308 "option `-%c' requires an argument",
309 p[1]);
310 return NULL;
311}
312
313int stopoptions = 0;
314static int process_arg (char *p, char *q)
315{
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000316 char *param;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000317 int i, advance = 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000318
319 if (!p || !p[0])
320 return 0;
321
H. Peter Anvineba20a72002-04-30 20:53:55 +0000322 if (p[0]=='-' && ! stopoptions)
323 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000324 switch (p[1]) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000325 case '-': /* -- => stop processing options */
326 stopoptions = 1;
327 break;
328 case 's': /* silently ignored for compatibility */
329 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330 case 'o': /* these parameters take values */
331 case 'f':
332 case 'p':
333 case 'd':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000334 case 'D':
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000335 case 'i':
336 case 'l':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000337 case 'E':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000338 case 'F':
339 if ( !(param = get_param (p, q, &advance)) )
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000341 if (p[1]=='o') { /* output file */
342 strcpy (outname, param);
343 } else if (p[1]=='f') { /* output format */
344 ofmt = ofmt_find(param);
345 if (!ofmt) {
346 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000347 "unrecognised output format `%s' - "
348 "use -hf for a list",
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000349 param);
350 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000351 else
352 ofmt->current_dfmt = ofmt->debug_formats[0];
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000353 } else if (p[1]=='p') { /* pre-include */
354 pp_pre_include (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000355 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000356 pp_pre_define (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000357 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
358 pp_pre_undefine (param);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000359 } else if (p[1]=='i') { /* include search path */
360 pp_include_path (param);
361 } else if (p[1]=='l') { /* listing file */
362 strcpy (listname, param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000363 } else if (p[1]=='E') { /* error messages file */
364 error_file = fopen(param, "wt");
365 if ( !error_file ) {
366 error_file = stderr; /* Revert to default! */
367 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
368 "cannot open file `%s' for error messages",
369 param);
370 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000371 } else if (p[1] == 'F') { /* specify debug format */
372 ofmt->current_dfmt = dfmt_find(ofmt, param);
373 if (!ofmt->current_dfmt) {
374 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
375 "unrecognized debug format `%s' for"
376 " output format `%s'",
377 param, ofmt->shortname);
378 }
379 }
380 break;
381 case 'g':
382 using_debug_info = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000383 break;
384 case 'h':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000385 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
386 "[-l listfile]\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000387 " [options...] [--] filename\n"
388 " or nasm -r for version info\n\n"
389 " -e preprocess only (writes output to stdout by default)\n"
390 " -a don't preprocess (assemble only)\n"
391 " -M generate Makefile dependencies on stdout\n\n"
392 " -E<file> redirect error messages to file\n\n"
393 " -g enable debug info\n"
394 " -F format select a debugging format\n\n"
395 " -i<path> adds a pathname to the include file path\n"
H. Peter Anvineba20a72002-04-30 20:53:55 +0000396 " -p<file> pre-includes a file\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000397 " -d<macro>[=<value>] pre-defines a macro\n"
398 " -u<macro> undefines a macro\n"
399 " -w+foo enables warnings about foo; -w-foo disables them\n"
400 "where foo can be:\n");
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000401 for (i=1; i<=ERR_WARN_MAX; i++)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000402 printf(" %-16s%s (default %s)\n",
403 suppressed_names[i], suppressed_what[i],
404 suppressed[i] ? "off" : "on");
405 printf ("\nresponse files should contain command line parameters"
406 ", one per line.\n");
407 if (p[2] == 'f') {
408 printf("\nvalid output formats for -f are"
409 " (`*' denotes default):\n");
410 ofmt_list(ofmt, stdout);
411 }
412 else {
413 printf ("\nFor a list of valid output formats, use -hf.\n");
414 printf ("For a list of debug formats, use -f <form> -y.\n");
415 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000416 exit (0); /* never need usage message here */
417 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000418 case 'y':
419 printf("\nvalid debug formats for '%s' output format are"
420 " ('*' denotes default):\n",
421 ofmt->shortname);
422 dfmt_list(ofmt, stdout);
423 exit(0);
424 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000425 case 'r':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000426 printf("NASM version %s\n", NASM_VER);
427#ifdef DEBUG
428 printf("Compiled with -DDEBUG on " __DATE__ "\n");
429#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000430 exit (0); /* never need usage message here */
431 break;
432 case 'e': /* preprocess only */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000433 operating_mode = op_preprocess;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000434 break;
435 case 'a': /* assemble only - don't preprocess */
436 preproc = &no_pp;
437 break;
438 case 'w':
439 if (p[2] != '+' && p[2] != '-') {
440 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
441 "invalid option to `-w'");
442 } else {
443 for (i=1; i<=ERR_WARN_MAX; i++)
444 if (!nasm_stricmp(p+3, suppressed_names[i]))
445 break;
446 if (i <= ERR_WARN_MAX)
447 suppressed[i] = (p[2] == '-');
448 else
449 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
450 "invalid option to `-w'");
451 }
452 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000453 case 'M':
454 operating_mode = op_depend;
455 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000456 default:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000457 if (!ofmt->setinfo(GI_SWITCH,&p))
458 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000459 "unrecognised option `-%c'",
460 p[1]);
461 break;
462 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000463 }
464 else
465 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000466 if (*inname) {
467 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
468 "more than one input file specified");
469 } else
470 strcpy(inname, p);
471 }
472
473 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000474}
475
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476#define ARG_BUF_DELTA 128
477
478static void process_respfile (FILE *rfile)
479{
480 char *buffer, *p, *q, *prevarg;
481 int bufsize, prevargsize;
482
483 bufsize = prevargsize = ARG_BUF_DELTA;
484 buffer = nasm_malloc(ARG_BUF_DELTA);
485 prevarg = nasm_malloc(ARG_BUF_DELTA);
486 prevarg[0] = '\0';
487
488 while (1) { /* Loop to handle all lines in file */
489
490 p = buffer;
491 while (1) { /* Loop to handle long lines */
492 q = fgets(p, bufsize-(p-buffer), rfile);
493 if (!q)
494 break;
495 p += strlen(p);
496 if (p > buffer && p[-1] == '\n')
497 break;
498 if (p-buffer > bufsize-10) {
499 int offset;
500 offset = p - buffer;
501 bufsize += ARG_BUF_DELTA;
502 buffer = nasm_realloc(buffer, bufsize);
503 p = buffer + offset;
504 }
505 }
506
507 if (!q && p == buffer) {
508 if (prevarg[0])
509 process_arg (prevarg, NULL);
510 nasm_free (buffer);
511 nasm_free (prevarg);
512 return;
513 }
514
515 /*
516 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
517 * them are present at the end of the line.
518 */
519 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
520
521 while (p > buffer && isspace(p[-1]))
522 *--p = '\0';
523
524 p = buffer;
525 while (isspace(*p))
526 p++;
527
528 if (process_arg (prevarg, p))
529 *p = '\0';
530
531 if (strlen(p) > prevargsize-10) {
532 prevargsize += ARG_BUF_DELTA;
533 prevarg = nasm_realloc(prevarg, prevargsize);
534 }
535 strcpy (prevarg, p);
536 }
537}
538
539static void parse_cmdline(int argc, char **argv)
540{
541 FILE *rfile;
542 char *envreal, *envcopy=NULL, *p, *q, *arg, *prevarg;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000543 char separator = ' ';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000544
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000545 *inname = *outname = *listname = '\0';
546
547 /*
548 * First, process the NASM environment variable.
549 */
550 envreal = getenv("NASM");
551 arg = NULL;
552 if (envreal) {
553 envcopy = nasm_strdup(envreal);
554 p = envcopy;
555 if (*p && *p != '-')
556 separator = *p++;
557 while (*p) {
558 q = p;
559 while (*p && *p != separator) p++;
560 while (*p == separator) *p++ = '\0';
561 prevarg = arg;
562 arg = q;
563 if (process_arg (prevarg, arg))
564 arg = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000565 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000566 if (arg)
567 process_arg (arg, NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000568 nasm_free (envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000570
571 /*
572 * Now process the actual command line.
573 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000574 while (--argc)
575 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000576 int i;
577 argv++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
579 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i)))
580 if ((rfile = fopen(p, "r"))) {
581 process_respfile (rfile);
582 fclose(rfile);
583 } else
584 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
585 "unable to open response file `%s'", p);
586 } else
587 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000588 argv += i, argc -= i;
589 }
590
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000591 if (!*inname)
592 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
593 "no input file specified");
594}
595
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596static void assemble_file (char *fname)
597{
598 char * value, * p, * q, * special, * line, debugid[80];
599 insn output_ins;
600 int i, rn_error, validid;
601 long seg, offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000602 struct tokenval tokval;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000603 expr * e;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000604
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605 /*
606 * pass one
607 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000608 pass = 1;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000609 in_abs_seg = FALSE;
610 location.segment = ofmt->section(NULL, pass, &sb);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000611 preproc->reset(fname, 1, report_error, evaluate, &nasmlist);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612 globallineno = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613 location.known = TRUE;
614 location.offset = offs = get_curr_ofs;
615
616 while ( (line = preproc->getline()) )
617 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618 globallineno++;
619
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000620 /* here we parse our directives; this is not handled by the 'real'
621 * parser. */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622 if ( (i = getkw (line, &value)) )
623 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000624 switch (i) {
625 case 1: /* [SEGMENT n] */
626 seg = ofmt->section (value, pass, &sb);
627 if (seg == NO_SEG) {
628 report_error (ERR_NONFATAL,
629 "segment name `%s' not recognised",
630 value);
631 } else {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632 in_abs_seg = FALSE;
633 location.segment = seg;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000634 }
635 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000636 case 2: /* [EXTERN label:special] */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000637 if (*value == '$')
638 value++; /* skip initial $ if present */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000639 q = value;
640 validid = TRUE;
641 if (!isidstart(*q))
642 validid = FALSE;
643 while (*q && *q != ':') {
644 if (!isidchar(*q))
645 validid = FALSE;
646 q++;
647 }
648 if (!validid) {
649 report_error (ERR_NONFATAL,
650 "identifier expected after EXTERN");
651 break;
652 }
653 if (*q == ':') {
654 *q++ = '\0';
655 special = q;
656 } else
657 special = NULL;
658 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
659 declare_as_global (value, special, report_error);
660 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
661 ofmt, report_error);
662 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000663 break;
664 case 3: /* [BITS bits] */
665 switch (atoi(value)) {
666 case 16:
667 case 32:
668 sb = atoi(value);
669 break;
670 default:
671 report_error(ERR_NONFATAL,
672 "`%s' is not a valid argument to [BITS]",
673 value);
674 break;
675 }
676 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000677 case 4: /* [GLOBAL symbol:special] */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000678 if (*value == '$')
679 value++; /* skip initial $ if present */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000680 q = value;
681 validid = TRUE;
682 if (!isidstart(*q))
683 validid = FALSE;
684 while (*q && *q != ':') {
685 if (!isidchar(*q))
686 validid = FALSE;
687 q++;
688 }
689 if (!validid) {
690 report_error (ERR_NONFATAL,
691 "identifier expected after GLOBAL");
692 break;
693 }
694 if (*q == ':') {
695 *q++ = '\0';
696 special = q;
697 } else
698 special = NULL;
699 declare_as_global (value, special, report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000700 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000701 case 5: /* [COMMON symbol size:special] */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000702 p = value;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000703 validid = TRUE;
704 if (!isidstart(*p))
705 validid = FALSE;
706 while (*p && !isspace(*p)) {
707 if (!isidchar(*p))
708 validid = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000709 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000710 }
711 if (!validid) {
712 report_error (ERR_NONFATAL,
713 "identifier expected after COMMON");
714 break;
715 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000716 if (*p) {
717 long size;
718
719 while (*p && isspace(*p))
720 *p++ = '\0';
H. Peter Anvin76690a12002-04-30 20:52:49 +0000721 q = p;
722 while (*q && *q != ':')
723 q++;
724 if (*q == ':') {
725 *q++ = '\0';
726 special = q;
727 } else
728 special = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000729 size = readnum (p, &rn_error);
730 if (rn_error)
731 report_error (ERR_NONFATAL, "invalid size specified"
732 " in COMMON declaration");
733 else
734 define_common (value, seg_alloc(), size,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000735 special, ofmt, report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000736 } else
737 report_error (ERR_NONFATAL, "no size specified in"
738 " COMMON declaration");
739 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000740 case 6: /* [ABSOLUTE address] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000741 stdscan_reset();
742 stdscan_bufptr = value;
743 tokval.t_type = TOKEN_INVALID;
744 e = evaluate(stdscan, NULL, &tokval, NULL, 1, report_error,
745 NULL);
746 if (e) {
747 if (!is_reloc(e))
748 report_error (ERR_NONFATAL, "cannot use non-"
749 "relocatable expression as ABSOLUTE"
750 " address");
751 else {
752 abs_seg = reloc_seg(e);
753 abs_offset = reloc_value(e);
754 }
755 } else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000756 abs_offset = 0x100;/* don't go near zero in case of / */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000757 in_abs_seg = TRUE;
758 location.segment = abs_seg;
759 break;
760 case 7:
761 p = value;
762 validid = TRUE;
763 if (!isidstart(*p))
764 validid = FALSE;
765 while (*p && !isspace(*p)) {
766 if (!isidchar(*p))
767 validid = FALSE;
768 p++;
769 }
770 if (!validid) {
771 report_error (ERR_NONFATAL,
772 "identifier expected after DEBUG");
773 break;
774 }
775 while (*p && isspace(*p)) p++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776 break;
777 default:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000778 if (!ofmt->directive (line+1, value, 1))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000779 report_error (ERR_NONFATAL, "unrecognised directive [%s]",
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000780 line+1);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000781 break;
782 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000783 }
784 else /* it isn't a directive */
785 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000786 parse_line (1, line, &output_ins,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000787 report_error, evaluate, define_label);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000788
H. Peter Anvineba20a72002-04-30 20:53:55 +0000789 if (output_ins.forw_ref)
790 {
791 for(i = 0; i < output_ins.operands; i++)
792 {
793 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
794 {
795 struct forwrefinfo *fwinf =
796 (struct forwrefinfo *)saa_wstruct(forwrefs);
797 fwinf->lineno = globallineno;
798 fwinf->operand = i;
799 }
800 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000801 }
802
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803 if (output_ins.opcode == I_EQU)
804 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805 /*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000806 * Special `..' EQUs get processed in pass two,
807 * except `..@' macro-processor EQUs which are done
808 * in the normal place.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000809 */
810 if (!output_ins.label)
811 report_error (ERR_NONFATAL,
812 "EQU not preceded by label");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000813
814 /*
815 * EQU cannot be used to declare a label relative to
816 * an external symbol.
817 */
818 else if ((output_ins.oprs[0].opflags & OPFLAG_EXTERN)
819 || (output_ins.operands > 1
820 && (output_ins.oprs[1].opflags & OPFLAG_EXTERN)))
821 {
822 report_error (ERR_NONFATAL,
823 "EQU used relative to external symbol");
824 }
825
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826 else if (output_ins.label[0] != '.' ||
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000827 output_ins.label[1] != '.' ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000828 output_ins.label[2] == '@')
829 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000830 if (output_ins.operands == 1 &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000831 (output_ins.oprs[0].type & IMMEDIATE) &&
H. Peter Anvineba20a72002-04-30 20:53:55 +0000832 output_ins.oprs[0].wrt == NO_SEG)
833 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000834 define_label (output_ins.label,
835 output_ins.oprs[0].segment,
836 output_ins.oprs[0].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000837 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000838 }
839 else if (output_ins.operands == 2 &&
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000840 (output_ins.oprs[0].type & IMMEDIATE) &&
841 (output_ins.oprs[0].type & COLON) &&
842 output_ins.oprs[0].segment == NO_SEG &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000843 output_ins.oprs[0].wrt == NO_SEG &&
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000844 (output_ins.oprs[1].type & IMMEDIATE) &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000845 output_ins.oprs[1].segment == NO_SEG &&
H. Peter Anvineba20a72002-04-30 20:53:55 +0000846 output_ins.oprs[1].wrt == NO_SEG)
847 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000848 define_label (output_ins.label,
849 output_ins.oprs[0].offset | SEG_ABS,
850 output_ins.oprs[1].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000851 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000852 }
853 else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000854 report_error(ERR_NONFATAL, "bad syntax for EQU");
855 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000856 }
857 else /* instruction isn't an EQU */
858 {
859 long l = insn_size (location.segment, offs, sb,
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000860 &output_ins, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000861 if (using_debug_info && output_ins.opcode != -1) {
862 /* this is done here so we can do debug type info */
863 long typeinfo = TYS_ELEMENTS(output_ins.operands);
864 switch (output_ins.opcode) {
865 case I_RESB:
866 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
867 break;
868 case I_RESW:
869 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
870 break;
871 case I_RESD:
872 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
873 break;
874 case I_RESQ:
875 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
876 break;
877 case I_REST:
878 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
879 break;
880 case I_DB:
881 typeinfo |= TY_BYTE;
882 break;
883 case I_DW:
884 typeinfo |= TY_WORD;
885 break;
886 case I_DD:
887 if (output_ins.eops_float)
888 typeinfo |= TY_FLOAT;
889 else
890 typeinfo |= TY_DWORD;
891 break;
892 case I_DQ:
893 typeinfo |= TY_QWORD;
894 break;
895 case I_DT:
896 typeinfo |= TY_TBYTE;
897 break;
898 default:
899 typeinfo = TY_LABEL;
900 }
901 ofmt->current_dfmt->debug_typevalue(typeinfo);
902 }
903 if (l != -1) {
904 offs += l;
905 set_curr_ofs (offs);
906 }
907 /*
908 * else l == -1 => invalid instruction, which will be
909 * flagged as an error on pass 2
910 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000911 }
912 cleanup_insn (&output_ins);
913 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000914 nasm_free (line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000915 location.offset = offs = get_curr_ofs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000916 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000917
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000918 preproc->cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000919
920 if (terminate_after_phase) {
921 fclose(ofile);
922 remove(outname);
923 if (want_usage)
924 usage();
925 exit (1);
926 }
927
H. Peter Anvineba20a72002-04-30 20:53:55 +0000928 /*
929 * pass two
930 */
931
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000932 pass = 2;
H. Peter Anvinea838272002-04-30 20:51:53 +0000933 saa_rewind (forwrefs);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000934 if (*listname)
935 nasmlist.init(listname, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000936 forwref = saa_rstruct (forwrefs);
937 in_abs_seg = FALSE;
938 location.segment = ofmt->section(NULL, pass, &sb);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000939 raa_free (offsets);
940 offsets = raa_init();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000941 preproc->reset(fname, 2, report_error, evaluate, &nasmlist);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000942 globallineno = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000943 location.offset = offs = get_curr_ofs;
944
945 while ( (line = preproc->getline()) )
946 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947 globallineno++;
948
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000949 /* here we parse our directives; this is not handled by
950 * the 'real' parser. */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000951 if ( (i = getkw (line, &value)) ) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000952 switch (i) {
953 case 1: /* [SEGMENT n] */
954 seg = ofmt->section (value, pass, &sb);
955 if (seg == NO_SEG) {
956 report_error (ERR_PANIC,
957 "invalid segment name on pass two");
958 } else
H. Peter Anvineba20a72002-04-30 20:53:55 +0000959 in_abs_seg = FALSE;
960 location.segment = seg;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000961 break;
962 case 2: /* [EXTERN label] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000963 q = value;
964 while (*q && *q != ':')
965 q++;
966 if (*q == ':') {
967 *q++ = '\0';
968 ofmt->symdef(value, 0L, 0L, 3, q);
969 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000970 break;
971 case 3: /* [BITS bits] */
972 switch (atoi(value)) {
973 case 16:
974 case 32:
975 sb = atoi(value);
976 break;
977 default:
978 report_error(ERR_PANIC,
979 "invalid [BITS] value on pass two",
980 value);
981 break;
982 }
983 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000984 case 4: /* [GLOBAL symbol] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000985 q = value;
986 while (*q && *q != ':')
987 q++;
988 if (*q == ':') {
989 *q++ = '\0';
990 ofmt->symdef(value, 0L, 0L, 3, q);
991 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000992 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000993 case 5: /* [COMMON symbol size] */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000994 q = value;
995 while (*q && *q != ':') {
996 if (isspace(*q))
997 *q = '\0';
998 q++;
999 }
1000 if (*q == ':') {
1001 *q++ = '\0';
1002 ofmt->symdef(value, 0L, 0L, 3, q);
1003 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001004 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001005 case 6: /* [ABSOLUTE addr] */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001006 stdscan_reset();
1007 stdscan_bufptr = value;
1008 tokval.t_type = TOKEN_INVALID;
1009 e = evaluate(stdscan, NULL, &tokval, NULL, 2, report_error,
1010 NULL);
1011 if (e) {
1012 if (!is_reloc(e))
1013 report_error (ERR_PANIC, "non-reloc ABSOLUTE address"
1014 " in pass two");
1015 else {
1016 abs_seg = reloc_seg(e);
1017 abs_offset = reloc_value(e);
1018 }
1019 } else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001020 report_error (ERR_PANIC, "invalid ABSOLUTE address "
1021 "in pass two");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001022 in_abs_seg = TRUE;
1023 location.segment = abs_seg;
1024 break;
1025 case 7:
1026 p = value;
1027 q = debugid;
1028 validid = TRUE;
1029 if (!isidstart(*p))
1030 validid = FALSE;
1031 while (*p && !isspace(*p)) {
1032 if (!isidchar(*p))
1033 validid = FALSE;
1034 *q++ = *p++;
1035 }
1036 *q++ = 0;
1037 if (!validid) {
1038 report_error (ERR_PANIC,
1039 "identifier expected after DEBUG in pass 2");
1040 break;
1041 }
1042 while (*p && isspace(*p))
1043 p++;
1044 ofmt->current_dfmt->debug_directive (debugid, p);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001045 break;
1046 default:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001047 if (!ofmt->directive (line+1, value, 2))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001048 report_error (ERR_PANIC, "invalid directive on pass two");
1049 break;
1050 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001051 }
1052 else /* not a directive */
1053 {
H. Peter Anvin76690a12002-04-30 20:52:49 +00001054 parse_line (2, line, &output_ins,
H. Peter Anvineba20a72002-04-30 20:53:55 +00001055 report_error, evaluate, redefine_label);
1056 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvinea838272002-04-30 20:51:53 +00001057 output_ins.forw_ref = TRUE;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058 do {
1059 output_ins.oprs[forwref->operand].opflags|= OPFLAG_FORWARD;
1060 forwref = saa_rstruct (forwrefs);
1061 } while (forwref != NULL && forwref->lineno == globallineno);
H. Peter Anvinea838272002-04-30 20:51:53 +00001062 } else
1063 output_ins.forw_ref = FALSE;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001064
1065 /*
1066 * Hack to prevent phase error in the code
1067 * rol ax,x
1068 * x equ 1
H. Peter Anvineba20a72002-04-30 20:53:55 +00001069 *
1070 * If the second operand is a forward reference,
1071 * the UNITY property of the number 1 in that
1072 * operand is cancelled. Otherwise the above
1073 * sequence will cause a phase error.
1074 *
1075 * This hack means that the above code will
1076 * generate 286+ code.
1077 *
1078 * The forward reference will mean that the
1079 * operand will not have the UNITY property on
1080 * the first pass, so the pass behaviours will
1081 * be consistent.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001082 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001083
1084 if (output_ins.forw_ref &&
1085 output_ins.operands >= 2 &&
1086 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1087 {
1088 output_ins.oprs[1].type &= ~ONENESS;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001089 }
1090
H. Peter Anvineba20a72002-04-30 20:53:55 +00001091 if (output_ins.opcode == I_EQU)
1092 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001093 /*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001094 * Special `..' EQUs get processed here, except
1095 * `..@' macro processor EQUs which are done above.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001096 */
1097 if (output_ins.label[0] == '.' &&
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001098 output_ins.label[1] == '.' &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00001099 output_ins.label[2] != '@')
1100 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001101 if (output_ins.operands == 1 &&
1102 (output_ins.oprs[0].type & IMMEDIATE)) {
1103 define_label (output_ins.label,
1104 output_ins.oprs[0].segment,
1105 output_ins.oprs[0].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001106 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001107 }
1108 else if (output_ins.operands == 2 &&
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001109 (output_ins.oprs[0].type & IMMEDIATE) &&
1110 (output_ins.oprs[0].type & COLON) &&
1111 output_ins.oprs[0].segment == NO_SEG &&
1112 (output_ins.oprs[1].type & IMMEDIATE) &&
H. Peter Anvineba20a72002-04-30 20:53:55 +00001113 output_ins.oprs[1].segment == NO_SEG)
1114 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001115 define_label (output_ins.label,
1116 output_ins.oprs[0].offset | SEG_ABS,
1117 output_ins.oprs[1].offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001118 NULL, FALSE, FALSE, ofmt, report_error);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001119 }
1120 else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001121 report_error(ERR_NONFATAL, "bad syntax for EQU");
1122 }
1123 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001124 offs += assemble (location.segment, offs, sb,
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001125 &output_ins, ofmt, report_error, &nasmlist);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001126 cleanup_insn (&output_ins);
1127 set_curr_ofs (offs);
1128 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001129
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001130 nasm_free (line);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001131
H. Peter Anvineba20a72002-04-30 20:53:55 +00001132 location.offset = offs = get_curr_ofs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001133 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001134
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001135 preproc->cleanup();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001136 nasmlist.cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001137}
1138
H. Peter Anvineba20a72002-04-30 20:53:55 +00001139static int getkw (char *buf, char **value)
1140{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001141 char *p, *q;
1142
1143 if (*buf!='[')
1144 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001145
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001146 p = buf;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001147
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001148 while (*p && *p != ']') p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001149
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001150 if (!*p)
1151 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001152
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001153 q = p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001154
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001155 while (*p && *p != ';') {
1156 if (!isspace(*p))
1157 return 0;
1158 p++;
1159 }
1160 q[1] = '\0';
1161
1162 p = buf+1;
1163 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1164 buf++;
1165 if (*buf==']') {
1166 *buf = '\0';
1167 *value = buf;
1168 } else {
1169 *buf++ = '\0';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001170 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001171 *value = buf;
1172 while (*buf!=']') buf++;
1173 *buf++ = '\0';
1174 }
1175 for (q=p; *q; q++)
1176 *q = tolower(*q);
1177 if (!strcmp(p, "segment") || !strcmp(p, "section"))
1178 return 1;
1179 if (!strcmp(p, "extern"))
1180 return 2;
1181 if (!strcmp(p, "bits"))
1182 return 3;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001183 if (!strcmp(p, "global"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001184 return 4;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001185 if (!strcmp(p, "common"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001186 return 5;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001187 if (!strcmp(p, "absolute"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001188 return 6;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001189 if (!strcmp(p, "debug"))
1190 return 7;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001191 return -1;
1192}
1193
H. Peter Anvineba20a72002-04-30 20:53:55 +00001194static void report_error (int severity, char *fmt, ...)
1195{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001196 va_list ap;
1197
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001198 /*
1199 * See if it's a suppressed warning.
1200 */
1201 if ((severity & ERR_MASK) == ERR_WARNING &&
1202 (severity & ERR_WARN_MASK) != 0 &&
1203 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1204 return; /* and bail out if so */
1205
H. Peter Anvin76690a12002-04-30 20:52:49 +00001206 /*
1207 * See if it's a pass-one only warning and we're not in pass one.
1208 */
1209 if ((severity & ERR_PASS1) && pass != 1)
1210 return;
1211
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001212 if (severity & ERR_NOFILE)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001213 fputs ("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001214 else {
1215 char * currentfile = NULL;
1216 long lineno = 0;
1217 src_get (&lineno, &currentfile);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001218 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001219 nasm_free (currentfile);
1220 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001221
1222 if ( (severity & ERR_MASK) == ERR_WARNING)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001223 fputs ("warning: ", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001224 else if ( (severity & ERR_MASK) == ERR_PANIC)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001225 fputs ("panic: ", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001226
1227 va_start (ap, fmt);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001228 vfprintf (error_file, fmt, ap);
1229 fputc ('\n', error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001230
1231 if (severity & ERR_USAGE)
1232 want_usage = TRUE;
1233
1234 switch (severity & ERR_MASK) {
1235 case ERR_WARNING:
1236 /* no further action, by definition */
1237 break;
1238 case ERR_NONFATAL:
1239 terminate_after_phase = TRUE;
1240 break;
1241 case ERR_FATAL:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242 if (ofile) {
1243 fclose(ofile);
1244 remove(outname);
1245 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001246 if (want_usage)
1247 usage();
1248 exit(1); /* instantly die */
1249 break; /* placate silly compilers */
1250 case ERR_PANIC:
H. Peter Anvin620515a2002-04-30 20:57:38 +00001251 fflush(NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001252 abort(); /* halt, catch fire, and dump core */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001253 break;
1254 }
1255}
1256
H. Peter Anvineba20a72002-04-30 20:53:55 +00001257static void usage(void)
1258{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001259 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001260}
1261
H. Peter Anvineba20a72002-04-30 20:53:55 +00001262static void register_output_formats(void)
1263{
1264 ofmt = ofmt_register (report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001265}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001266
1267#define BUF_DELTA 512
1268
1269static FILE *no_pp_fp;
1270static efunc no_pp_err;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001271static ListGen *no_pp_list;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001272static long no_pp_lineinc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001273
H. Peter Anvin76690a12002-04-30 20:52:49 +00001274static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
H. Peter Anvineba20a72002-04-30 20:53:55 +00001275 ListGen *listgen)
1276{
1277 src_set_fname(nasm_strdup(file));
1278 src_set_linnum(0);
1279 no_pp_lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001280 no_pp_err = error;
1281 no_pp_fp = fopen(file, "r");
1282 if (!no_pp_fp)
1283 no_pp_err (ERR_FATAL | ERR_NOFILE,
1284 "unable to open input file `%s'", file);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001285 no_pp_list = listgen;
1286 (void) pass; /* placate compilers */
1287 (void) eval; /* placate compilers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288}
1289
H. Peter Anvineba20a72002-04-30 20:53:55 +00001290static char *no_pp_getline (void)
1291{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001292 char *buffer, *p, *q;
1293 int bufsize;
1294
1295 bufsize = BUF_DELTA;
1296 buffer = nasm_malloc(BUF_DELTA);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001297 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1298
1299 while (1) { /* Loop to handle %line */
1300
1301 p = buffer;
1302 while (1) { /* Loop to handle long lines */
1303 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1304 if (!q)
1305 break;
1306 p += strlen(p);
1307 if (p > buffer && p[-1] == '\n')
1308 break;
1309 if (p-buffer > bufsize-10) {
1310 int offset;
1311 offset = p - buffer;
1312 bufsize += BUF_DELTA;
1313 buffer = nasm_realloc(buffer, bufsize);
1314 p = buffer + offset;
1315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001317
1318 if (!q && p == buffer) {
1319 nasm_free (buffer);
1320 return NULL;
1321 }
1322
1323 /*
1324 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1325 * them are present at the end of the line.
1326 */
1327 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1328
1329 if (!strncmp(buffer, "%line", 5)) {
1330 long ln;
1331 int li;
1332 char *nm = nasm_malloc(strlen(buffer));
1333 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1334 nasm_free( src_set_fname(nm) );
1335 src_set_linnum(ln);
1336 no_pp_lineinc = li;
1337 continue;
1338 }
1339 nasm_free(nm);
1340 }
1341 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 }
1343
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344 no_pp_list->line (LIST_READ, buffer);
1345
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001346 return buffer;
1347}
1348
H. Peter Anvineba20a72002-04-30 20:53:55 +00001349static void no_pp_cleanup (void)
1350{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001351 fclose(no_pp_fp);
1352}