blob: 841640cdd5e910172987f504e6f988e4143518e1 [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 Anvinaf535c12002-04-30 20:59:21 +000017#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000018#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000019#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000020#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000021#include "assemble.h"
22#include "labels.h"
23#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000024#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000025
H. Peter Anvineba20a72002-04-30 20:53:55 +000026struct forwrefinfo { /* info held on forward refs. */
27 int lineno;
28 int operand;
29};
30
H. Peter Anvinaf535c12002-04-30 20:59:21 +000031static int get_bits (char *value);
32static unsigned long get_cpu (char *cpu_str);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000033static void report_error (int, char *, ...);
34static void parse_cmdline (int, char **);
35static void assemble_file (char *);
36static int getkw (char *buf, char **value);
37static void register_output_formats(void);
38static void usage(void);
39
H. Peter Anvineba20a72002-04-30 20:53:55 +000040static int using_debug_info;
41
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000042static char inname[FILENAME_MAX];
43static char outname[FILENAME_MAX];
H. Peter Anvin6768eb72002-04-30 20:52:26 +000044static char listname[FILENAME_MAX];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000045static int globallineno; /* for forward-reference tracking */
H. Peter Anvinaf535c12002-04-30 20:59:21 +000046static int pass = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047static struct ofmt *ofmt = NULL;
48
H. Peter Anvinef7468f2002-04-30 20:57:59 +000049static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +000050
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051static FILE *ofile = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +000052static int optimizing = 10; /* number of optimization passes to take */
53static int sb, cmd_sb = 16; /* by default */
54static unsigned long cmd_cpu = IF_PLEVEL; /* highest level by default */
55static unsigned long cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
56int global_offset_changed; /* referenced in labels.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057
H. Peter Anvineba20a72002-04-30 20:53:55 +000058static loc_t location;
59int in_abs_seg; /* Flag we are in ABSOLUTE seg */
60static long abs_seg;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062static struct RAA *offsets;
63static long abs_offset;
H. Peter Anvinea838272002-04-30 20:51:53 +000064
65static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvineba20a72002-04-30 20:53:55 +000066static struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000067
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000068static Preproc *preproc;
H. Peter Anvin620515a2002-04-30 20:57:38 +000069enum op_type {
70 op_normal, /* Preprocess and assemble */
71 op_preprocess, /* Preprocess only */
72 op_depend /* Generate dependencies */
73};
74static enum op_type operating_mode;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000075
76/* used by error function to report location */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000077
78/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +000079 * Which of the suppressible warnings are suppressed. Entry zero
80 * doesn't do anything. Initial defaults are given here.
81 */
82static char suppressed[1+ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +000083 0, TRUE, TRUE, TRUE, FALSE
H. Peter Anvin6768eb72002-04-30 20:52:26 +000084};
85
86/*
87 * The option names for the suppressible warnings. As before, entry
88 * zero does nothing.
89 */
90static char *suppressed_names[1+ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +000091 NULL, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
H. Peter Anvin6768eb72002-04-30 20:52:26 +000092};
93
94/*
95 * The explanations for the suppressible warnings. As before, entry
96 * zero does nothing.
97 */
98static char *suppressed_what[1+ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +000099 NULL,
100 "macro calls with wrong no. of params",
101 "cyclic macro self-references",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000102 "labels alone on lines without trailing `:'",
103 "numeric constants greater than 0xFFFFFFFF"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000104};
105
106/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000107 * This is a null preprocessor which just copies lines from input
108 * to output. It's used when someone explicitly requests that NASM
109 * not preprocess their source file.
110 */
111
H. Peter Anvin76690a12002-04-30 20:52:49 +0000112static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113static char *no_pp_getline (void);
114static void no_pp_cleanup (void);
115static Preproc no_pp = {
116 no_pp_reset,
117 no_pp_getline,
118 no_pp_cleanup
119};
120
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000121/*
122 * get/set current offset...
123 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000124#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000125 raa_read(offsets,location.segment))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000126#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000127 (void)(offsets=raa_write(offsets,location.segment,(x))))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128
129static int want_usage;
130static int terminate_after_phase;
131
H. Peter Anvineba20a72002-04-30 20:53:55 +0000132static void nasm_fputs(char *line, FILE *ofile)
133{
134 if (ofile) {
135 fputs(line, ofile);
136 fputc('\n', ofile);
137 } else
138 puts(line);
139}
140
141int main(int argc, char **argv)
142{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143 want_usage = terminate_after_phase = FALSE;
144
145 nasm_set_malloc_error (report_error);
146 offsets = raa_init();
H. Peter Anvineba20a72002-04-30 20:53:55 +0000147 forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000148
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000149 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000150 operating_mode = op_normal;
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000151
152 error_file = stderr;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000153
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154 seg_init();
155
156 register_output_formats();
157
158 parse_cmdline(argc, argv);
159
H. Peter Anvineba20a72002-04-30 20:53:55 +0000160 if (terminate_after_phase)
161 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000162 if (want_usage)
163 usage();
164 return 1;
165 }
166
H. Peter Anvin76690a12002-04-30 20:52:49 +0000167 if (ofmt->stdmac)
168 pp_extra_stdmac (ofmt->stdmac);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000169 parser_global_info (ofmt, &location);
170 eval_global_info (ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000171
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000172 /* define some macros dependent of command-line */
173 {
174 char temp [64];
175 sprintf (temp, "__OUTPUT_FORMAT__=%s\n", ofmt->shortname);
176 pp_pre_define (temp);
177 }
178
H. Peter Anvin620515a2002-04-30 20:57:38 +0000179 switch ( operating_mode ) {
180 case op_depend:
181 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182 char *line;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000183 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
184 if (outname[0] == '\0')
185 ofmt->filename (inname, outname, report_error);
186 ofile = NULL;
187 printf("%s: %s", outname, inname);
188 while ( (line = preproc->getline()) )
189 nasm_free (line);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190 preproc->cleanup();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000191 putc('\n', stdout);
192 }
193 break;
194
195 case op_preprocess:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000196 {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000197 char *line;
198 char *file_name = NULL;
199 long prior_linnum=0;
200 int lineinc=0;
201
202 if (*outname) {
203 ofile = fopen(outname, "w");
204 if (!ofile)
205 report_error (ERR_FATAL | ERR_NOFILE,
206 "unable to open output file `%s'", outname);
207 } else
208 ofile = NULL;
209
210 location.known = FALSE;
211
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000212 pass = 1;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000213 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
214 while ( (line = preproc->getline()) ) {
215 /*
216 * We generate %line directives if needed for later programs
217 */
218 long linnum = prior_linnum += lineinc;
219 int altline = src_get(&linnum, &file_name);
220 if (altline) {
221 if (altline==1 && lineinc==1)
222 nasm_fputs("", ofile);
223 else {
224 lineinc = (altline != -1 || lineinc!=1);
225 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
226 linnum, lineinc, file_name);
227 }
228 prior_linnum = linnum;
229 }
230 nasm_fputs(line, ofile);
231 nasm_free (line);
232 }
233 nasm_free(file_name);
234 preproc->cleanup();
235 if (ofile)
236 fclose(ofile);
237 if (ofile && terminate_after_phase)
238 remove(outname);
239 }
240 break;
241
242 case op_normal:
243 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000244 /*
245 * We must call ofmt->filename _anyway_, even if the user
246 * has specified their own output file, because some
247 * formats (eg OBJ and COFF) use ofmt->filename to find out
248 * the name of the input file and then put that inside the
249 * file.
250 */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000251 ofmt->filename (inname, outname, report_error);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000252
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000253 ofile = fopen(outname, "wb");
254 if (!ofile) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000255 report_error (ERR_FATAL | ERR_NOFILE,
256 "unable to open output file `%s'", outname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000257 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000258
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000259 /*
260 * We must call init_labels() before ofmt->init() since
261 * some object formats will want to define labels in their
262 * init routines. (eg OS/2 defines the FLAT group)
263 */
264 init_labels ();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000265
H. Peter Anvin76690a12002-04-30 20:52:49 +0000266 ofmt->init (ofile, report_error, define_label, evaluate);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000267
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000268 assemble_file (inname);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000269
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 if (!terminate_after_phase) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000271 ofmt->cleanup (using_debug_info);
272 cleanup_labels ();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000273 } else {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000274 /*
275 * We had an fclose on the output file here, but we
276 * actually do that in all the object file drivers as well,
277 * so we're leaving out the one here.
278 * fclose (ofile);
279 */
280
281 remove(outname);
282 if (listname[0])
283 remove(listname);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000284 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000285 }
286 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000287 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000288
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000289 if (want_usage)
H. Peter Anvin620515a2002-04-30 20:57:38 +0000290 usage();
291
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000292 raa_free (offsets);
293 saa_free (forwrefs);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000294 eval_cleanup ();
295 nasmlib_cleanup ();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000296
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000297 if (terminate_after_phase)
298 return 1;
299 else
300 return 0;
301}
302
H. Peter Anvineba20a72002-04-30 20:53:55 +0000303
304/*
305 * Get a parameter for a command line option.
306 * First arg must be in the form of e.g. -f...
307 */
308static char *get_param (char *p, char *q, int *advance)
309{
310 *advance = 0;
311 if (p[2]) /* the parameter's in the option */
312 {
313 p += 2;
314 while (isspace(*p))
315 p++;
316 return p;
317 }
318 if (q && q[0])
319 {
320 *advance = 1;
321 return q;
322 }
323 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
324 "option `-%c' requires an argument",
325 p[1]);
326 return NULL;
327}
328
329int stopoptions = 0;
330static int process_arg (char *p, char *q)
331{
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000332 char *param;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000333 int i, advance = 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000334
335 if (!p || !p[0])
336 return 0;
337
H. Peter Anvineba20a72002-04-30 20:53:55 +0000338 if (p[0]=='-' && ! stopoptions)
339 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340 switch (p[1]) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000341 case '-': /* -- => stop processing options */
342 stopoptions = 1;
343 break;
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000344 case 's':
345 error_file = stdout;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000346 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000347 case 'o': /* these parameters take values */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000348 case 'O':
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000349 case 'f':
350 case 'p':
351 case 'd':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000352 case 'D':
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000353 case 'i':
354 case 'l':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000355 case 'E':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000356 case 'F':
357 if ( !(param = get_param (p, q, &advance)) )
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000358 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000359 if (p[1]=='o') { /* output file */
360 strcpy (outname, param);
361 } else if (p[1]=='f') { /* output format */
362 ofmt = ofmt_find(param);
363 if (!ofmt) {
364 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000365 "unrecognised output format `%s' - "
366 "use -hf for a list",
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000367 param);
368 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000369 else
370 ofmt->current_dfmt = ofmt->debug_formats[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000371 } else if (p[1]=='O') { /* Optimization level */
372 if (!isdigit(*param)) report_error(ERR_FATAL,
373 "command line optimization level must be 0..3");
374 optimizing = atoi(param);
375 if (optimizing <= 0) optimizing = 0;
376 else if (optimizing <= 3) optimizing *= 5; /* 5 passes for each level */
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000377 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000378 pp_pre_include (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000379 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000380 pp_pre_define (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000381 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
382 pp_pre_undefine (param);
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000383 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000384 pp_include_path (param);
385 } else if (p[1]=='l') { /* listing file */
386 strcpy (listname, param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000387 } else if (p[1]=='E') { /* error messages file */
388 error_file = fopen(param, "wt");
389 if ( !error_file ) {
390 error_file = stderr; /* Revert to default! */
391 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
392 "cannot open file `%s' for error messages",
393 param);
394 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000395 } else if (p[1] == 'F') { /* specify debug format */
396 ofmt->current_dfmt = dfmt_find(ofmt, param);
397 if (!ofmt->current_dfmt) {
398 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
399 "unrecognized debug format `%s' for"
400 " output format `%s'",
401 param, ofmt->shortname);
402 }
403 }
404 break;
405 case 'g':
406 using_debug_info = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000407 break;
408 case 'h':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000409 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
410 "[-l listfile]\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000411 " [options...] [--] filename\n"
412 " or nasm -r for version info\n\n"
413 " -e preprocess only (writes output to stdout by default)\n"
414 " -a don't preprocess (assemble only)\n"
415 " -M generate Makefile dependencies on stdout\n\n"
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000416 " -E<file> redirect error messages to file\n"
417 " -s redirect error messages to stdout\n\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000418 " -g enable debug info\n"
419 " -F format select a debugging format\n\n"
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000420 " -I<path> adds a pathname to the include file path\n"
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000421 " -O<digit> optimize branch offsets -O0 disables, -O2 default\n"
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000422 " -P<file> pre-includes a file\n"
423 " -D<macro>[=<value>] pre-defines a macro\n"
424 " -U<macro> undefines a macro\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000425 " -w+foo enables warnings about foo; -w-foo disables them\n"
426 "where foo can be:\n");
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000427 for (i=1; i<=ERR_WARN_MAX; i++)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000428 printf(" %-16s%s (default %s)\n",
429 suppressed_names[i], suppressed_what[i],
430 suppressed[i] ? "off" : "on");
431 printf ("\nresponse files should contain command line parameters"
432 ", one per line.\n");
433 if (p[2] == 'f') {
434 printf("\nvalid output formats for -f are"
435 " (`*' denotes default):\n");
436 ofmt_list(ofmt, stdout);
437 }
438 else {
439 printf ("\nFor a list of valid output formats, use -hf.\n");
440 printf ("For a list of debug formats, use -f <form> -y.\n");
441 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000442 exit (0); /* never need usage message here */
443 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000444 case 'y':
445 printf("\nvalid debug formats for '%s' output format are"
446 " ('*' denotes default):\n",
447 ofmt->shortname);
448 dfmt_list(ofmt, stdout);
449 exit(0);
450 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000451 case 'r':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000452 printf("NASM version %s\n", NASM_VER);
453#ifdef DEBUG
454 printf("Compiled with -DDEBUG on " __DATE__ "\n");
455#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000456 exit (0); /* never need usage message here */
457 break;
458 case 'e': /* preprocess only */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000459 operating_mode = op_preprocess;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000460 break;
461 case 'a': /* assemble only - don't preprocess */
462 preproc = &no_pp;
463 break;
464 case 'w':
465 if (p[2] != '+' && p[2] != '-') {
466 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
467 "invalid option to `-w'");
468 } else {
469 for (i=1; i<=ERR_WARN_MAX; i++)
470 if (!nasm_stricmp(p+3, suppressed_names[i]))
471 break;
472 if (i <= ERR_WARN_MAX)
473 suppressed[i] = (p[2] == '-');
474 else
475 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
476 "invalid option to `-w'");
477 }
478 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000479 case 'M':
480 operating_mode = op_depend;
481 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000482 default:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000483 if (!ofmt->setinfo(GI_SWITCH,&p))
484 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000485 "unrecognised option `-%c'",
486 p[1]);
487 break;
488 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000489 }
490 else
491 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000492 if (*inname) {
493 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
494 "more than one input file specified");
495 } else
496 strcpy(inname, p);
497 }
498
499 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500}
501
H. Peter Anvineba20a72002-04-30 20:53:55 +0000502#define ARG_BUF_DELTA 128
503
504static void process_respfile (FILE *rfile)
505{
506 char *buffer, *p, *q, *prevarg;
507 int bufsize, prevargsize;
508
509 bufsize = prevargsize = ARG_BUF_DELTA;
510 buffer = nasm_malloc(ARG_BUF_DELTA);
511 prevarg = nasm_malloc(ARG_BUF_DELTA);
512 prevarg[0] = '\0';
513
514 while (1) { /* Loop to handle all lines in file */
515
516 p = buffer;
517 while (1) { /* Loop to handle long lines */
518 q = fgets(p, bufsize-(p-buffer), rfile);
519 if (!q)
520 break;
521 p += strlen(p);
522 if (p > buffer && p[-1] == '\n')
523 break;
524 if (p-buffer > bufsize-10) {
525 int offset;
526 offset = p - buffer;
527 bufsize += ARG_BUF_DELTA;
528 buffer = nasm_realloc(buffer, bufsize);
529 p = buffer + offset;
530 }
531 }
532
533 if (!q && p == buffer) {
534 if (prevarg[0])
535 process_arg (prevarg, NULL);
536 nasm_free (buffer);
537 nasm_free (prevarg);
538 return;
539 }
540
541 /*
542 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
543 * them are present at the end of the line.
544 */
545 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
546
547 while (p > buffer && isspace(p[-1]))
548 *--p = '\0';
549
550 p = buffer;
551 while (isspace(*p))
552 p++;
553
554 if (process_arg (prevarg, p))
555 *p = '\0';
556
557 if (strlen(p) > prevargsize-10) {
558 prevargsize += ARG_BUF_DELTA;
559 prevarg = nasm_realloc(prevarg, prevargsize);
560 }
561 strcpy (prevarg, p);
562 }
563}
564
565static void parse_cmdline(int argc, char **argv)
566{
567 FILE *rfile;
568 char *envreal, *envcopy=NULL, *p, *q, *arg, *prevarg;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000569 char separator = ' ';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000570
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000571 *inname = *outname = *listname = '\0';
572
573 /*
574 * First, process the NASM environment variable.
575 */
576 envreal = getenv("NASM");
577 arg = NULL;
578 if (envreal) {
579 envcopy = nasm_strdup(envreal);
580 p = envcopy;
581 if (*p && *p != '-')
582 separator = *p++;
583 while (*p) {
584 q = p;
585 while (*p && *p != separator) p++;
586 while (*p == separator) *p++ = '\0';
587 prevarg = arg;
588 arg = q;
589 if (process_arg (prevarg, arg))
590 arg = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000591 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000592 if (arg)
593 process_arg (arg, NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000594 nasm_free (envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000596
597 /*
598 * Now process the actual command line.
599 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600 while (--argc)
601 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000602 int i;
603 argv++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000605 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606 if ((rfile = fopen(p, "r"))) {
607 process_respfile (rfile);
608 fclose(rfile);
609 } else
610 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
611 "unable to open response file `%s'", p);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000612 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613 } else
614 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000615 argv += i, argc -= i;
616 }
617
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000618 if (!*inname)
619 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
620 "no input file specified");
621}
622
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000623
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624static void assemble_file (char *fname)
625{
626 char * value, * p, * q, * special, * line, debugid[80];
627 insn output_ins;
628 int i, rn_error, validid;
629 long seg, offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000630 struct tokenval tokval;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000631 expr * e;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000632 int pass_max;
633 int pass_cnt = 0; /* count actual passes */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000634
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000635 if (cmd_sb == 32 && cmd_cpu < IF_386)
636 report_error(ERR_FATAL, "command line: "
637 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000638
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000639 pass_max = optimizing + 2; /* passes 1, optimizing, then 2 */
640 for (pass = 1; pass <= pass_max; pass++) {
641 int pass1, pass2;
642 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000643
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000644 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
645 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
646 def_label = pass > 1 ? redefine_label : define_label;
647
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000648
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000649 sb = cmd_sb; /* set 'bits' to command line default */
650 cpu = cmd_cpu;
651 if (pass == pass_max) {
652 if (*listname)
653 nasmlist.init(listname, report_error);
654 }
655 in_abs_seg = FALSE;
656 global_offset_changed = FALSE; /* set by redefine_label */
657 location.segment = ofmt->section(NULL, pass2, &sb);
658 if (pass > 1) {
659 saa_rewind (forwrefs);
660 forwref = saa_rstruct (forwrefs);
661 raa_free (offsets);
662 offsets = raa_init();
663 }
664 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
665 globallineno = 0;
666 if (pass == 1) location.known = TRUE;
667 location.offset = offs = GET_CURR_OFFS;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000668
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000669 while ( (line = preproc->getline()) )
670 {
671 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000672
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000673 /* here we parse our directives; this is not handled by the 'real'
674 * parser. */
675 if ( (i = getkw (line, &value)) )
676 {
677 switch (i) {
678 case 1: /* [SEGMENT n] */
679 seg = ofmt->section (value, pass2, &sb);
680 if (seg == NO_SEG) {
681 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
682 "segment name `%s' not recognised",
683 value);
684 } else {
685 in_abs_seg = FALSE;
686 location.segment = seg;
687 }
688 break;
689 case 2: /* [EXTERN label:special] */
690 if (pass == pass_max) {
691 q = value;
692 while (*q && *q != ':')
693 q++;
694 if (*q == ':') {
695 *q++ = '\0';
696 ofmt->symdef(value, 0L, 0L, 3, q);
697 }
698 } else if (pass == 1) { /* pass == 1 */
699 if (*value == '$')
700 value++; /* skip initial $ if present */
701 q = value;
702 validid = TRUE;
703 if (!isidstart(*q))
704 validid = FALSE;
705 while (*q && *q != ':') {
706 if (!isidchar(*q))
707 validid = FALSE;
708 q++;
709 }
710 if (!validid) {
711 report_error (ERR_NONFATAL,
712 "identifier expected after EXTERN");
713 break;
714 }
715 if (*q == ':') {
716 *q++ = '\0';
717 special = q;
718 } else
719 special = NULL;
720 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
721 declare_as_global (value, special, report_error);
722 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
723 ofmt, report_error);
724 }
725 } /* else pass == 1 */
726 break;
727 case 3: /* [BITS bits] */
728 sb = get_bits(value);
729 break;
730 case 4: /* [GLOBAL symbol:special] */
731 if (pass == pass_max) { /* pass 2 */
732 q = value;
733 while (*q && *q != ':')
734 q++;
735 if (*q == ':') {
736 *q++ = '\0';
737 ofmt->symdef(value, 0L, 0L, 3, q);
738 }
739 } else if (pass == 1) { /* pass == 1 */
740 if (*value == '$')
741 value++; /* skip initial $ if present */
742 q = value;
743 validid = TRUE;
744 if (!isidstart(*q))
745 validid = FALSE;
746 while (*q && *q != ':') {
747 if (!isidchar(*q))
748 validid = FALSE;
749 q++;
750 }
751 if (!validid) {
752 report_error (ERR_NONFATAL,
753 "identifier expected after GLOBAL");
754 break;
755 }
756 if (*q == ':') {
757 *q++ = '\0';
758 special = q;
759 } else
760 special = NULL;
761 declare_as_global (value, special, report_error);
762 } /* pass == 1 */
763 break;
764 case 5: /* [COMMON symbol size:special] */
765 if (pass == 1) {
766 p = value;
767 validid = TRUE;
768 if (!isidstart(*p))
769 validid = FALSE;
770 while (*p && !isspace(*p)) {
771 if (!isidchar(*p))
772 validid = FALSE;
773 p++;
774 }
775 if (!validid) {
776 report_error (ERR_NONFATAL,
777 "identifier expected after COMMON");
778 break;
779 }
780 if (*p) {
781 long size;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000782
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000783 while (*p && isspace(*p))
784 *p++ = '\0';
785 q = p;
786 while (*q && *q != ':')
787 q++;
788 if (*q == ':') {
789 *q++ = '\0';
790 special = q;
791 } else
792 special = NULL;
793 size = readnum (p, &rn_error);
794 if (rn_error)
795 report_error (ERR_NONFATAL, "invalid size specified"
796 " in COMMON declaration");
797 else
798 define_common (value, seg_alloc(), size,
799 special, ofmt, report_error);
800 } else
801 report_error (ERR_NONFATAL, "no size specified in"
802 " COMMON declaration");
803 } else if (pass == pass_max) { /* pass == 2 */
804 q = value;
805 while (*q && *q != ':') {
806 if (isspace(*q))
807 *q = '\0';
808 q++;
809 }
810 if (*q == ':') {
811 *q++ = '\0';
812 ofmt->symdef(value, 0L, 0L, 3, q);
813 }
814 }
815 break;
816 case 6: /* [ABSOLUTE address] */
817 stdscan_reset();
818 stdscan_bufptr = value;
819 tokval.t_type = TOKEN_INVALID;
820 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, report_error,
821 NULL);
822 if (e) {
823 if (!is_reloc(e))
824 report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
825 "cannot use non-relocatable expression as "
826 "ABSOLUTE address");
827 else {
828 abs_seg = reloc_seg(e);
829 abs_offset = reloc_value(e);
830 }
831 } else
832 if (pass==1) abs_offset = 0x100;/* don't go near zero in case of / */
833 else report_error (ERR_PANIC, "invalid ABSOLUTE address "
834 "in pass two");
835 in_abs_seg = TRUE;
836 location.segment = abs_seg;
837 break;
838 case 7: /* DEBUG */
839 p = value;
840 q = debugid;
841 validid = TRUE;
842 if (!isidstart(*p))
843 validid = FALSE;
844 while (*p && !isspace(*p)) {
845 if (!isidchar(*p))
846 validid = FALSE;
847 *q++ = *p++;
848 }
849 *q++ = 0;
850 if (!validid) {
851 report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
852 "identifier expected after DEBUG");
853 break;
854 }
855 while (*p && isspace(*p)) p++;
856 if (pass==pass_max) ofmt->current_dfmt->debug_directive (debugid, p);
857 break;
858 case 8: /* [WARNING {+|-}warn-name] */
859 if (pass1 == 1) {
860 while (*value && isspace(*value))
861 value++;
862
863 if (*value == '+' || *value == '-') {
864 validid = (*value == '-') ? TRUE : FALSE;
865 value++;
866 } else
867 validid = FALSE;
868
869 for (i=1; i<=ERR_WARN_MAX; i++)
870 if (!nasm_stricmp(value, suppressed_names[i]))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000871 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000872 if (i <= ERR_WARN_MAX)
873 suppressed[i] = validid;
874 else
875 report_error (ERR_NONFATAL, "invalid warning id in WARNING directive");
876 }
877 break;
878 case 9: /* cpu */
879 cpu = get_cpu (value);
880 break;
881 default:
882 if (!ofmt->directive (line+1, value, pass1))
883 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
884 "unrecognised directive [%s]",
885 line+1);
886 break;
887 }
888 }
889 else /* it isn't a directive */
890 {
891 parse_line (pass2, line, &output_ins,
892 report_error, evaluate,
893 def_label);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000894
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000895 if (!optimizing && pass == 2) {
896 if (forwref != NULL && globallineno == forwref->lineno) {
897 output_ins.forw_ref = TRUE;
898 do {
899 output_ins.oprs[forwref->operand].opflags|= OPFLAG_FORWARD;
900 forwref = saa_rstruct (forwrefs);
901 } while (forwref != NULL && forwref->lineno == globallineno);
902 } else
903 output_ins.forw_ref = FALSE;
904 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000905
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000906
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000907 if (!optimizing && output_ins.forw_ref)
908 {
909 if (pass == 1) {
910 for(i = 0; i < output_ins.operands; i++)
911 {
912 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
913 {
914 struct forwrefinfo *fwinf =
915 (struct forwrefinfo *)saa_wstruct(forwrefs);
916 fwinf->lineno = globallineno;
917 fwinf->operand = i;
918 }
919 }
920 } else { /* pass == 2 */
921 /*
922 * Hack to prevent phase error in the code
923 * rol ax,x
924 * x equ 1
925 *
926 * If the second operand is a forward reference,
927 * the UNITY property of the number 1 in that
928 * operand is cancelled. Otherwise the above
929 * sequence will cause a phase error.
930 *
931 * This hack means that the above code will
932 * generate 286+ code.
933 *
934 * The forward reference will mean that the
935 * operand will not have the UNITY property on
936 * the first pass, so the pass behaviours will
937 * be consistent.
938 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000939
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000940 if (output_ins.operands >= 2 &&
941 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
942 {
943 output_ins.oprs[1].type &= ~(ONENESS|BYTENESS);
944 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000945
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000946 } /* pass == 2 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000948 } /* forw_ref */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000949
H. Peter Anvineba20a72002-04-30 20:53:55 +0000950
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000951 if (output_ins.opcode == I_EQU) {
952 if (pass1 == 1)
953 {
954 /*
955 * Special `..' EQUs get processed in pass two,
956 * except `..@' macro-processor EQUs which are done
957 * in the normal place.
958 */
959 if (!output_ins.label)
960 report_error (ERR_NONFATAL,
961 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000962
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000963 else if (output_ins.label[0] != '.' ||
964 output_ins.label[1] != '.' ||
965 output_ins.label[2] == '@')
966 {
967 if (output_ins.operands == 1 &&
968 (output_ins.oprs[0].type & IMMEDIATE) &&
969 output_ins.oprs[0].wrt == NO_SEG)
970 {
971 int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
972 def_label (output_ins.label,
973 output_ins.oprs[0].segment,
974 output_ins.oprs[0].offset,
975 NULL, FALSE, isext, ofmt, report_error);
976 }
977 else if (output_ins.operands == 2 &&
978 (output_ins.oprs[0].type & IMMEDIATE) &&
979 (output_ins.oprs[0].type & COLON) &&
980 output_ins.oprs[0].segment == NO_SEG &&
981 output_ins.oprs[0].wrt == NO_SEG &&
982 (output_ins.oprs[1].type & IMMEDIATE) &&
983 output_ins.oprs[1].segment == NO_SEG &&
984 output_ins.oprs[1].wrt == NO_SEG)
985 {
986 def_label (output_ins.label,
987 output_ins.oprs[0].offset | SEG_ABS,
988 output_ins.oprs[1].offset,
989 NULL, FALSE, FALSE, ofmt, report_error);
990 }
991 else
992 report_error(ERR_NONFATAL, "bad syntax for EQU");
993 }
994 } else { /* pass == 2 */
995 /*
996 * Special `..' EQUs get processed here, except
997 * `..@' macro processor EQUs which are done above.
998 */
999 if (output_ins.label[0] == '.' &&
1000 output_ins.label[1] == '.' &&
1001 output_ins.label[2] != '@')
1002 {
1003 if (output_ins.operands == 1 &&
1004 (output_ins.oprs[0].type & IMMEDIATE)) {
1005 define_label (output_ins.label,
1006 output_ins.oprs[0].segment,
1007 output_ins.oprs[0].offset,
1008 NULL, FALSE, FALSE, ofmt, report_error);
1009 }
1010 else if (output_ins.operands == 2 &&
1011 (output_ins.oprs[0].type & IMMEDIATE) &&
1012 (output_ins.oprs[0].type & COLON) &&
1013 output_ins.oprs[0].segment == NO_SEG &&
1014 (output_ins.oprs[1].type & IMMEDIATE) &&
1015 output_ins.oprs[1].segment == NO_SEG)
1016 {
1017 define_label (output_ins.label,
1018 output_ins.oprs[0].offset | SEG_ABS,
1019 output_ins.oprs[1].offset,
1020 NULL, FALSE, FALSE, ofmt, report_error);
1021 }
1022 else
1023 report_error(ERR_NONFATAL, "bad syntax for EQU");
1024 }
1025 } /* pass == 2 */
1026 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001027
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001028 if (pass1 == 1) {
1029 long l = insn_size (location.segment, offs, sb, cpu,
1030 &output_ins, report_error);
1031 if (using_debug_info && output_ins.opcode != -1) {
1032 /* this is done here so we can do debug type info */
1033 long typeinfo = TYS_ELEMENTS(output_ins.operands);
1034 switch (output_ins.opcode) {
1035 case I_RESB:
1036 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1037 break;
1038 case I_RESW:
1039 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1040 break;
1041 case I_RESD:
1042 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1043 break;
1044 case I_RESQ:
1045 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1046 break;
1047 case I_REST:
1048 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1049 break;
1050 case I_DB:
1051 typeinfo |= TY_BYTE;
1052 break;
1053 case I_DW:
1054 typeinfo |= TY_WORD;
1055 break;
1056 case I_DD:
1057 if (output_ins.eops_float)
1058 typeinfo |= TY_FLOAT;
1059 else
1060 typeinfo |= TY_DWORD;
1061 break;
1062 case I_DQ:
1063 typeinfo |= TY_QWORD;
1064 break;
1065 case I_DT:
1066 typeinfo |= TY_TBYTE;
1067 break;
1068 default:
1069 typeinfo = TY_LABEL;
1070 }
1071 ofmt->current_dfmt->debug_typevalue(typeinfo);
1072 }
1073 if (l != -1) {
1074 offs += l;
1075 SET_CURR_OFFS (offs);
1076 }
1077 /*
1078 * else l == -1 => invalid instruction, which will be
1079 * flagged as an error on pass 2
1080 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001081
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001082 } else { /* pass == 2 */
1083 offs += assemble (location.segment, offs, sb, cpu,
1084 &output_ins, ofmt, report_error, &nasmlist);
1085 SET_CURR_OFFS (offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001086
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001087 }
1088 } /* not an EQU */
1089 cleanup_insn (&output_ins);
1090 }
1091 nasm_free (line);
1092 location.offset = offs = GET_CURR_OFFS;
1093 } /* end while (line = preproc->getline... */
1094
1095 if (pass1==2 && global_offset_changed)
1096 report_error(ERR_NONFATAL, "phase error detected at end of assembly.");
1097
1098 if (pass1 == 1) preproc->cleanup();
1099
1100 if (pass1==1 && terminate_after_phase) {
1101 fclose(ofile);
1102 remove(outname);
1103 if (want_usage)
1104 usage();
1105 exit (1);
1106 }
1107 pass_cnt++;
1108 if (pass>1 && !global_offset_changed && pass<pass_max) pass = pass_max-1;
1109 } /* for (pass=1; pass<=2; pass++) */
1110
1111 nasmlist.cleanup();
1112#if 1
1113 if (optimizing)
1114 fprintf(error_file,
1115 "info:: assembly required 1+%d+1 passes\n", pass_cnt-2);
1116#endif
1117} /* exit from assemble_file (...) */
1118
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001119
H. Peter Anvineba20a72002-04-30 20:53:55 +00001120static int getkw (char *buf, char **value)
1121{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001122 char *p, *q;
1123
1124 if (*buf!='[')
1125 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001126
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001127 p = buf;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001128
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001129 while (*p && *p != ']') p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001130
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001131 if (!*p)
1132 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001133
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001134 q = p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001135
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001136 while (*p && *p != ';') {
1137 if (!isspace(*p))
1138 return 0;
1139 p++;
1140 }
1141 q[1] = '\0';
1142
1143 p = buf+1;
1144 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1145 buf++;
1146 if (*buf==']') {
1147 *buf = '\0';
1148 *value = buf;
1149 } else {
1150 *buf++ = '\0';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001151 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001152 *value = buf;
1153 while (*buf!=']') buf++;
1154 *buf++ = '\0';
1155 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001156#if 0
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001157 for (q=p; *q; q++)
1158 *q = tolower(*q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001159#endif
1160 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001161 return 1;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001162 if (!nasm_stricmp(p, "extern"))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001163 return 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001164 if (!nasm_stricmp(p, "bits"))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001165 return 3;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001166 if (!nasm_stricmp(p, "global"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001167 return 4;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001168 if (!nasm_stricmp(p, "common"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001169 return 5;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001170 if (!nasm_stricmp(p, "absolute"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001171 return 6;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001172 if (!nasm_stricmp(p, "debug"))
H. Peter Anvineba20a72002-04-30 20:53:55 +00001173 return 7;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001174 if (!nasm_stricmp(p, "warning"))
1175 return 8;
1176 if (!nasm_stricmp(p, "cpu"))
1177 return 9;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001178 return -1;
1179}
1180
H. Peter Anvineba20a72002-04-30 20:53:55 +00001181static void report_error (int severity, char *fmt, ...)
1182{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001183 va_list ap;
1184
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001185 /*
1186 * See if it's a suppressed warning.
1187 */
1188 if ((severity & ERR_MASK) == ERR_WARNING &&
1189 (severity & ERR_WARN_MASK) != 0 &&
1190 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1191 return; /* and bail out if so */
1192
H. Peter Anvin76690a12002-04-30 20:52:49 +00001193 /*
1194 * See if it's a pass-one only warning and we're not in pass one.
1195 */
1196 if ((severity & ERR_PASS1) && pass != 1)
1197 return;
1198
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001199 if (severity & ERR_NOFILE)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001200 fputs ("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001201 else {
1202 char * currentfile = NULL;
1203 long lineno = 0;
1204 src_get (&lineno, &currentfile);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001205 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001206 nasm_free (currentfile);
1207 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001208
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001209 switch (severity & ERR_MASK) {
1210 case ERR_WARNING:
1211 fputs ("warning: ", error_file); break;
1212 case ERR_NONFATAL:
1213 fputs ("error: ", error_file); break;
1214 case ERR_FATAL:
1215 fputs ("fatal: ", error_file); break;
1216 case ERR_PANIC:
1217 fputs ("panic: ", error_file); break;
1218 case ERR_DEBUG:
1219 fputs("debug: ", error_file); break;
1220 }
1221
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001222 va_start (ap, fmt);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001223 vfprintf (error_file, fmt, ap);
1224 fputc ('\n', error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001225
1226 if (severity & ERR_USAGE)
1227 want_usage = TRUE;
1228
1229 switch (severity & ERR_MASK) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001230 case ERR_WARNING: case ERR_DEBUG:
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001231 /* no further action, by definition */
1232 break;
1233 case ERR_NONFATAL:
1234 terminate_after_phase = TRUE;
1235 break;
1236 case ERR_FATAL:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001237 if (ofile) {
1238 fclose(ofile);
1239 remove(outname);
1240 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001241 if (want_usage)
1242 usage();
1243 exit(1); /* instantly die */
1244 break; /* placate silly compilers */
1245 case ERR_PANIC:
H. Peter Anvin620515a2002-04-30 20:57:38 +00001246 fflush(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001247/* abort(); */ /* halt, catch fire, and dump core */
1248 exit(3);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001249 break;
1250 }
1251}
1252
H. Peter Anvineba20a72002-04-30 20:53:55 +00001253static void usage(void)
1254{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001255 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001256}
1257
H. Peter Anvineba20a72002-04-30 20:53:55 +00001258static void register_output_formats(void)
1259{
1260 ofmt = ofmt_register (report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001261}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001262
1263#define BUF_DELTA 512
1264
1265static FILE *no_pp_fp;
1266static efunc no_pp_err;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001267static ListGen *no_pp_list;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001268static long no_pp_lineinc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001269
H. Peter Anvin76690a12002-04-30 20:52:49 +00001270static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
H. Peter Anvineba20a72002-04-30 20:53:55 +00001271 ListGen *listgen)
1272{
1273 src_set_fname(nasm_strdup(file));
1274 src_set_linnum(0);
1275 no_pp_lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001276 no_pp_err = error;
1277 no_pp_fp = fopen(file, "r");
1278 if (!no_pp_fp)
1279 no_pp_err (ERR_FATAL | ERR_NOFILE,
1280 "unable to open input file `%s'", file);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001281 no_pp_list = listgen;
1282 (void) pass; /* placate compilers */
1283 (void) eval; /* placate compilers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284}
1285
H. Peter Anvineba20a72002-04-30 20:53:55 +00001286static char *no_pp_getline (void)
1287{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288 char *buffer, *p, *q;
1289 int bufsize;
1290
1291 bufsize = BUF_DELTA;
1292 buffer = nasm_malloc(BUF_DELTA);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001293 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1294
1295 while (1) { /* Loop to handle %line */
1296
1297 p = buffer;
1298 while (1) { /* Loop to handle long lines */
1299 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1300 if (!q)
1301 break;
1302 p += strlen(p);
1303 if (p > buffer && p[-1] == '\n')
1304 break;
1305 if (p-buffer > bufsize-10) {
1306 int offset;
1307 offset = p - buffer;
1308 bufsize += BUF_DELTA;
1309 buffer = nasm_realloc(buffer, bufsize);
1310 p = buffer + offset;
1311 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001313
1314 if (!q && p == buffer) {
1315 nasm_free (buffer);
1316 return NULL;
1317 }
1318
1319 /*
1320 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1321 * them are present at the end of the line.
1322 */
1323 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1324
1325 if (!strncmp(buffer, "%line", 5)) {
1326 long ln;
1327 int li;
1328 char *nm = nasm_malloc(strlen(buffer));
1329 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1330 nasm_free( src_set_fname(nm) );
1331 src_set_linnum(ln);
1332 no_pp_lineinc = li;
1333 continue;
1334 }
1335 nasm_free(nm);
1336 }
1337 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001338 }
1339
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340 no_pp_list->line (LIST_READ, buffer);
1341
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 return buffer;
1343}
1344
H. Peter Anvineba20a72002-04-30 20:53:55 +00001345static void no_pp_cleanup (void)
1346{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347 fclose(no_pp_fp);
1348}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001349
1350static unsigned long get_cpu (char *value)
1351{
1352
1353 if (!strcmp(value, "8086")) return IF_8086;
1354 if (!strcmp(value, "186")) return IF_186;
1355 if (!strcmp(value, "286")) return IF_286;
1356 if (!strcmp(value, "386")) return IF_386;
1357 if (!strcmp(value, "486")) return IF_486;
1358 if (!strcmp(value, "586") ||
1359 !nasm_stricmp(value, "pentium") ) return IF_PENT;
1360 if (!strcmp(value, "686") ||
1361 !nasm_stricmp(value, "ppro") ||
1362 !nasm_stricmp(value, "p2") ) return IF_P6;
1363 if (!nasm_stricmp(value, "p3") ||
1364 !nasm_stricmp(value, "katmai") ) return IF_KATMAI;
1365
1366 report_error (pass ? ERR_NONFATAL : ERR_FATAL, "unknown 'cpu' type");
1367
1368 return IF_PLEVEL; /* the maximum level */
1369}
1370
1371
1372static int get_bits (char *value)
1373{
1374 int i;
1375
1376 if ((i = atoi(value)) == 16) return i; /* set for a 16-bit segment */
1377 else if (i == 32) {
1378 if (cpu < IF_386) {
1379 report_error(ERR_NONFATAL,
1380 "cannot specify 32-bit segment on processor below a 386");
1381 i = 16;
1382 }
1383 } else {
1384 report_error(pass ? ERR_NONFATAL : ERR_FATAL,
1385 "`%s' is not a valid segment size; must be 16 or 32",
1386 value);
1387 i = 16;
1388 }
1389 return i;
1390}
1391
1392/* end of nasm.c */