blob: 1123ee300967857d4313969fdf652b098fcf9042 [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;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000041#ifdef TASM_COMPAT
42int tasm_compatible_mode = FALSE;
43#endif
H. Peter Anvineba20a72002-04-30 20:53:55 +000044
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000045static char inname[FILENAME_MAX];
46static char outname[FILENAME_MAX];
H. Peter Anvin6768eb72002-04-30 20:52:26 +000047static char listname[FILENAME_MAX];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000048static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000049/* static int pass = 0; */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000050static struct ofmt *ofmt = NULL;
51
H. Peter Anvinef7468f2002-04-30 20:57:59 +000052static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +000053
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000054static FILE *ofile = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000055static int optimizing = 0; /* number of optimization passes to take */
H. Peter Anvinaf535c12002-04-30 20:59:21 +000056static int sb, cmd_sb = 16; /* by default */
57static unsigned long cmd_cpu = IF_PLEVEL; /* highest level by default */
58static unsigned long cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
59int global_offset_changed; /* referenced in labels.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060
H. Peter Anvineba20a72002-04-30 20:53:55 +000061static loc_t location;
62int in_abs_seg; /* Flag we are in ABSOLUTE seg */
63static long abs_seg;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000064
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065static struct RAA *offsets;
66static long abs_offset;
H. Peter Anvinea838272002-04-30 20:51:53 +000067
68static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvineba20a72002-04-30 20:53:55 +000069static struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000071static Preproc *preproc;
H. Peter Anvin620515a2002-04-30 20:57:38 +000072enum op_type {
73 op_normal, /* Preprocess and assemble */
74 op_preprocess, /* Preprocess only */
75 op_depend /* Generate dependencies */
76};
77static enum op_type operating_mode;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000079/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +000080 * Which of the suppressible warnings are suppressed. Entry zero
81 * doesn't do anything. Initial defaults are given here.
82 */
83static char suppressed[1+ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +000084 0, TRUE, TRUE, TRUE, FALSE
H. Peter Anvin6768eb72002-04-30 20:52:26 +000085};
86
87/*
88 * The option names for the suppressible warnings. As before, entry
89 * zero does nothing.
90 */
91static char *suppressed_names[1+ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +000092 NULL, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093};
94
95/*
96 * The explanations for the suppressible warnings. As before, entry
97 * zero does nothing.
98 */
99static char *suppressed_what[1+ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000100 NULL,
101 "macro calls with wrong no. of params",
102 "cyclic macro self-references",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000103 "labels alone on lines without trailing `:'",
104 "numeric constants greater than 0xFFFFFFFF"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000105};
106
107/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 * This is a null preprocessor which just copies lines from input
109 * to output. It's used when someone explicitly requests that NASM
110 * not preprocess their source file.
111 */
112
H. Peter Anvin76690a12002-04-30 20:52:49 +0000113static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114static char *no_pp_getline (void);
115static void no_pp_cleanup (void);
116static Preproc no_pp = {
117 no_pp_reset,
118 no_pp_getline,
119 no_pp_cleanup
120};
121
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000122/*
123 * get/set current offset...
124 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000125#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000126 raa_read(offsets,location.segment))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000127#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000128 (void)(offsets=raa_write(offsets,location.segment,(x))))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129
130static int want_usage;
131static int terminate_after_phase;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000132int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133
H. Peter Anvineba20a72002-04-30 20:53:55 +0000134static void nasm_fputs(char *line, FILE *ofile)
135{
136 if (ofile) {
137 fputs(line, ofile);
138 fputc('\n', ofile);
139 } else
140 puts(line);
141}
142
143int main(int argc, char **argv)
144{
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000145 pass0 = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000146 want_usage = terminate_after_phase = FALSE;
147
148 nasm_set_malloc_error (report_error);
149 offsets = raa_init();
H. Peter Anvineba20a72002-04-30 20:53:55 +0000150 forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000151
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000152 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000153 operating_mode = op_normal;
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000154
155 error_file = stderr;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000156
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000157 seg_init();
158
159 register_output_formats();
160
161 parse_cmdline(argc, argv);
162
H. Peter Anvineba20a72002-04-30 20:53:55 +0000163 if (terminate_after_phase)
164 {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000165 if (want_usage)
166 usage();
167 return 1;
168 }
169
H. Peter Anvin76690a12002-04-30 20:52:49 +0000170 if (ofmt->stdmac)
171 pp_extra_stdmac (ofmt->stdmac);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000172 parser_global_info (ofmt, &location);
173 eval_global_info (ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000174
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000175 /* define some macros dependent of command-line */
176 {
177 char temp [64];
178 sprintf (temp, "__OUTPUT_FORMAT__=%s\n", ofmt->shortname);
179 pp_pre_define (temp);
180 }
181
H. Peter Anvin620515a2002-04-30 20:57:38 +0000182 switch ( operating_mode ) {
183 case op_depend:
184 {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 char *line;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000186 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
187 if (outname[0] == '\0')
188 ofmt->filename (inname, outname, report_error);
189 ofile = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000190 fprintf(stdout, "%s: %s", outname, inname);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000191 while ( (line = preproc->getline()) )
192 nasm_free (line);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000193 preproc->cleanup();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000194 putc('\n', stdout);
195 }
196 break;
197
198 case op_preprocess:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000199 {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000200 char *line;
201 char *file_name = NULL;
202 long prior_linnum=0;
203 int lineinc=0;
204
205 if (*outname) {
206 ofile = fopen(outname, "w");
207 if (!ofile)
208 report_error (ERR_FATAL | ERR_NOFILE,
209 "unable to open output file `%s'", outname);
210 } else
211 ofile = NULL;
212
213 location.known = FALSE;
214
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000215/* pass = 1; */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000216 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
217 while ( (line = preproc->getline()) ) {
218 /*
219 * We generate %line directives if needed for later programs
220 */
221 long linnum = prior_linnum += lineinc;
222 int altline = src_get(&linnum, &file_name);
223 if (altline) {
224 if (altline==1 && lineinc==1)
225 nasm_fputs("", ofile);
226 else {
227 lineinc = (altline != -1 || lineinc!=1);
228 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
229 linnum, lineinc, file_name);
230 }
231 prior_linnum = linnum;
232 }
233 nasm_fputs(line, ofile);
234 nasm_free (line);
235 }
236 nasm_free(file_name);
237 preproc->cleanup();
238 if (ofile)
239 fclose(ofile);
240 if (ofile && terminate_after_phase)
241 remove(outname);
242 }
243 break;
244
245 case op_normal:
246 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000247 /*
248 * We must call ofmt->filename _anyway_, even if the user
249 * has specified their own output file, because some
250 * formats (eg OBJ and COFF) use ofmt->filename to find out
251 * the name of the input file and then put that inside the
252 * file.
253 */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000254 ofmt->filename (inname, outname, report_error);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000255
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000256 ofile = fopen(outname, "wb");
257 if (!ofile) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000258 report_error (ERR_FATAL | ERR_NOFILE,
259 "unable to open output file `%s'", outname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000260 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000261
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000262 /*
263 * We must call init_labels() before ofmt->init() since
264 * some object formats will want to define labels in their
265 * init routines. (eg OS/2 defines the FLAT group)
266 */
267 init_labels ();
H. Peter Anvin620515a2002-04-30 20:57:38 +0000268
H. Peter Anvin76690a12002-04-30 20:52:49 +0000269 ofmt->init (ofile, report_error, define_label, evaluate);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000270
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 assemble_file (inname);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000272
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000273 if (!terminate_after_phase) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000274 ofmt->cleanup (using_debug_info);
275 cleanup_labels ();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000276 } else {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000277 /*
278 * We had an fclose on the output file here, but we
279 * actually do that in all the object file drivers as well,
280 * so we're leaving out the one here.
281 * fclose (ofile);
282 */
283
284 remove(outname);
285 if (listname[0])
286 remove(listname);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000287 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000288 }
289 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000290 }
H. Peter Anvin620515a2002-04-30 20:57:38 +0000291
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000292 if (want_usage)
H. Peter Anvin620515a2002-04-30 20:57:38 +0000293 usage();
294
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000295 raa_free (offsets);
296 saa_free (forwrefs);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000297 eval_cleanup ();
298 nasmlib_cleanup ();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000299
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000300 if (terminate_after_phase)
301 return 1;
302 else
303 return 0;
304}
305
H. Peter Anvineba20a72002-04-30 20:53:55 +0000306
307/*
308 * Get a parameter for a command line option.
309 * First arg must be in the form of e.g. -f...
310 */
311static char *get_param (char *p, char *q, int *advance)
312{
313 *advance = 0;
314 if (p[2]) /* the parameter's in the option */
315 {
316 p += 2;
317 while (isspace(*p))
318 p++;
319 return p;
320 }
321 if (q && q[0])
322 {
323 *advance = 1;
324 return q;
325 }
326 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
327 "option `-%c' requires an argument",
328 p[1]);
329 return NULL;
330}
331
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000332struct textargs
333{
334 char *label;
335 int value;
336};
337
338#define OPT_PREFIX 0
339#define OPT_POSTFIX 1
340struct textargs textopts[] =
341{
342 {"prefix",OPT_PREFIX},
343 {"postfix",OPT_POSTFIX},
344 {NULL,0}
345};
346
347
H. Peter Anvineba20a72002-04-30 20:53:55 +0000348int stopoptions = 0;
349static int process_arg (char *p, char *q)
350{
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000351 char *param;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000352 int i, advance = 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000353
354 if (!p || !p[0])
355 return 0;
356
H. Peter Anvineba20a72002-04-30 20:53:55 +0000357 if (p[0]=='-' && ! stopoptions)
358 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000359 switch (p[1]) {
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000360 case 's':
361 error_file = stdout;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000362 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000363 case 'o': /* these parameters take values */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000364 case 'O':
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000365 case 'f':
366 case 'p':
367 case 'd':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000368 case 'D':
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000369 case 'i':
370 case 'l':
H. Peter Anvin620515a2002-04-30 20:57:38 +0000371 case 'E':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000372 case 'F':
373 if ( !(param = get_param (p, q, &advance)) )
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000374 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000375 if (p[1]=='o') { /* output file */
376 strcpy (outname, param);
377 } else if (p[1]=='f') { /* output format */
378 ofmt = ofmt_find(param);
379 if (!ofmt) {
380 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000381 "unrecognised output format `%s' - "
382 "use -hf for a list",
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000383 param);
384 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000385 else
386 ofmt->current_dfmt = ofmt->debug_formats[0];
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000387 } else if (p[1]=='O') { /* Optimization level */
388 if (!isdigit(*param)) report_error(ERR_FATAL,
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000389 "command line optimization level must be 0..3 or <nn>");
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000390 optimizing = atoi(param);
391 if (optimizing <= 0) optimizing = 0;
392 else if (optimizing <= 3) optimizing *= 5; /* 5 passes for each level */
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000393 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000394 pp_pre_include (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000395 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396 pp_pre_define (param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000397 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
398 pp_pre_undefine (param);
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000399 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000400 pp_include_path (param);
401 } else if (p[1]=='l') { /* listing file */
402 strcpy (listname, param);
H. Peter Anvin620515a2002-04-30 20:57:38 +0000403 } else if (p[1]=='E') { /* error messages file */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000404 error_file = fopen(param, "w");
H. Peter Anvin620515a2002-04-30 20:57:38 +0000405 if ( !error_file ) {
406 error_file = stderr; /* Revert to default! */
407 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
408 "cannot open file `%s' for error messages",
409 param);
410 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000411 } else if (p[1] == 'F') { /* specify debug format */
412 ofmt->current_dfmt = dfmt_find(ofmt, param);
413 if (!ofmt->current_dfmt) {
414 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
415 "unrecognized debug format `%s' for"
416 " output format `%s'",
417 param, ofmt->shortname);
418 }
419 }
420 break;
421 case 'g':
422 using_debug_info = TRUE;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000423 break;
424 case 'h':
H. Peter Anvineba20a72002-04-30 20:53:55 +0000425 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
426 "[-l listfile]\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000427 " [options...] [--] filename\n"
428 " or nasm -r for version info\n\n"
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000429#ifdef TASM_COMPAT
430 " -t Assemble in SciTech TASM compatible mode\n"
431 " -g Generate debug information in selected format.\n"
432#endif
H. Peter Anvin620515a2002-04-30 20:57:38 +0000433 " -e preprocess only (writes output to stdout by default)\n"
434 " -a don't preprocess (assemble only)\n"
435 " -M generate Makefile dependencies on stdout\n\n"
H. Peter Anvin41bf8002002-04-30 20:58:18 +0000436 " -E<file> redirect error messages to file\n"
437 " -s redirect error messages to stdout\n\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000438 " -g enable debug info\n"
439 " -F format select a debugging format\n\n"
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000440 " -I<path> adds a pathname to the include file path\n"
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000441 " -O<digit> optimize branch offsets -O0 disables, -O2 default\n"
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000442 " -P<file> pre-includes a file\n"
443 " -D<macro>[=<value>] pre-defines a macro\n"
444 " -U<macro> undefines a macro\n"
H. Peter Anvin620515a2002-04-30 20:57:38 +0000445 " -w+foo enables warnings about foo; -w-foo disables them\n"
446 "where foo can be:\n");
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000447 for (i=1; i<=ERR_WARN_MAX; i++)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000448 printf(" %-16s%s (default %s)\n",
449 suppressed_names[i], suppressed_what[i],
450 suppressed[i] ? "off" : "on");
451 printf ("\nresponse files should contain command line parameters"
452 ", one per line.\n");
453 if (p[2] == 'f') {
454 printf("\nvalid output formats for -f are"
455 " (`*' denotes default):\n");
456 ofmt_list(ofmt, stdout);
457 }
458 else {
459 printf ("\nFor a list of valid output formats, use -hf.\n");
460 printf ("For a list of debug formats, use -f <form> -y.\n");
461 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000462 exit (0); /* never need usage message here */
463 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000464 case 'y':
465 printf("\nvalid debug formats for '%s' output format are"
466 " ('*' denotes default):\n",
467 ofmt->shortname);
468 dfmt_list(ofmt, stdout);
469 exit(0);
470 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000471#ifdef TASM_COMPAT
472 case 't':
473 tasm_compatible_mode = TRUE;
474 break;
475#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000476 case 'r':
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477#ifdef TASM_COMPAT
478 printf("NASM version %s - SciTech TASM compatible additions\n", NASM_VER);
479#else
H. Peter Anvineba20a72002-04-30 20:53:55 +0000480 printf("NASM version %s\n", NASM_VER);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000481#endif
H. Peter Anvineba20a72002-04-30 20:53:55 +0000482#ifdef DEBUG
483 printf("Compiled with -DDEBUG on " __DATE__ "\n");
484#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000485 exit (0); /* never need usage message here */
486 break;
487 case 'e': /* preprocess only */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000488 operating_mode = op_preprocess;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000489 break;
490 case 'a': /* assemble only - don't preprocess */
491 preproc = &no_pp;
492 break;
493 case 'w':
494 if (p[2] != '+' && p[2] != '-') {
495 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
496 "invalid option to `-w'");
497 } else {
498 for (i=1; i<=ERR_WARN_MAX; i++)
499 if (!nasm_stricmp(p+3, suppressed_names[i]))
500 break;
501 if (i <= ERR_WARN_MAX)
502 suppressed[i] = (p[2] == '-');
503 else
504 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
505 "invalid option to `-w'");
506 }
507 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000508 case 'M':
509 operating_mode = op_depend;
510 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000511
512 case '-':
513 {
514 int s;
515
516 if (p[2]==0) { /* -- => stop processing options */
517 stopoptions = 1;
518 break;
519 }
520 for(s=0; textopts[s].label; s++)
521 {
522 if(!nasm_stricmp(p+2, textopts[s].label))
523 {
524 break;
525 }
526 }
527
528 switch(s)
529 {
530
531 case OPT_PREFIX:
532 case OPT_POSTFIX:
533 {
534 if (!q)
535 {
536 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
537 "option `--%s' requires an argument",
538 p+2);
539 break;
540 }
541 else
542 {
543 advance = 1, param = q;
544 }
545
546 if(s == OPT_PREFIX)
547 {
548 strncpy(lprefix,param,PREFIX_MAX-1);
549 lprefix[PREFIX_MAX-1]=0;
550 break;
551 }
552 if(s == OPT_POSTFIX)
553 {
554 strncpy(lpostfix,param,POSTFIX_MAX-1);
555 lpostfix[POSTFIX_MAX-1]=0;
556 break;
557 }
558 break;
559 }
560 default:
561 {
562 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
563 "unrecognised option `--%s'",
564 p+2);
565 break;
566 }
567 }
568 break;
569 }
570
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000571 default:
H. Peter Anvineba20a72002-04-30 20:53:55 +0000572 if (!ofmt->setinfo(GI_SWITCH,&p))
573 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000574 "unrecognised option `-%c'",
575 p[1]);
576 break;
577 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578 }
579 else
580 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000581 if (*inname) {
582 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
583 "more than one input file specified");
584 } else
585 strcpy(inname, p);
586 }
587
588 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000589}
590
H. Peter Anvineba20a72002-04-30 20:53:55 +0000591#define ARG_BUF_DELTA 128
592
593static void process_respfile (FILE *rfile)
594{
595 char *buffer, *p, *q, *prevarg;
596 int bufsize, prevargsize;
597
598 bufsize = prevargsize = ARG_BUF_DELTA;
599 buffer = nasm_malloc(ARG_BUF_DELTA);
600 prevarg = nasm_malloc(ARG_BUF_DELTA);
601 prevarg[0] = '\0';
602
603 while (1) { /* Loop to handle all lines in file */
604
605 p = buffer;
606 while (1) { /* Loop to handle long lines */
607 q = fgets(p, bufsize-(p-buffer), rfile);
608 if (!q)
609 break;
610 p += strlen(p);
611 if (p > buffer && p[-1] == '\n')
612 break;
613 if (p-buffer > bufsize-10) {
614 int offset;
615 offset = p - buffer;
616 bufsize += ARG_BUF_DELTA;
617 buffer = nasm_realloc(buffer, bufsize);
618 p = buffer + offset;
619 }
620 }
621
622 if (!q && p == buffer) {
623 if (prevarg[0])
624 process_arg (prevarg, NULL);
625 nasm_free (buffer);
626 nasm_free (prevarg);
627 return;
628 }
629
630 /*
631 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
632 * them are present at the end of the line.
633 */
634 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
635
636 while (p > buffer && isspace(p[-1]))
637 *--p = '\0';
638
639 p = buffer;
640 while (isspace(*p))
641 p++;
642
643 if (process_arg (prevarg, p))
644 *p = '\0';
645
646 if (strlen(p) > prevargsize-10) {
647 prevargsize += ARG_BUF_DELTA;
648 prevarg = nasm_realloc(prevarg, prevargsize);
649 }
650 strcpy (prevarg, p);
651 }
652}
653
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000654/* Function to process args from a string of args, rather than the
655 * argv array. Used by the environment variable and response file
656 * processing.
657 */
658#ifdef TASM_COMPAT
659static void process_args (char *args) {
660 char *p, *q, *arg, *prevarg;
661 char separator = ' ';
662
663 p = args;
664 if (*p && *p != '-')
665 separator = *p++;
666 arg = NULL;
667 while (*p) {
668 q = p;
669 while (*p && *p != separator) p++;
670 while (*p == separator) *p++ = '\0';
671 prevarg = arg;
672 arg = q;
673 if (process_arg (prevarg, arg))
674 arg = NULL;
675 }
676 if (arg)
677 process_arg (arg, NULL);
678}
679#endif
680
H. Peter Anvineba20a72002-04-30 20:53:55 +0000681static void parse_cmdline(int argc, char **argv)
682{
683 FILE *rfile;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000684 char *envreal, *envcopy=NULL, *p, *arg;
685#ifndef TASM_COMPAT
686 char *q, *prevarg;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000687 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000688#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000689
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000690 *inname = *outname = *listname = '\0';
691
692 /*
693 * First, process the NASM environment variable.
694 */
695 envreal = getenv("NASM");
696 arg = NULL;
697 if (envreal) {
698 envcopy = nasm_strdup(envreal);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000699#ifdef TASM_COMPAT
700 process_args(envcopy);
701#else
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000702 p = envcopy;
703 if (*p && *p != '-')
704 separator = *p++;
705 while (*p) {
706 q = p;
707 while (*p && *p != separator) p++;
708 while (*p == separator) *p++ = '\0';
709 prevarg = arg;
710 arg = q;
711 if (process_arg (prevarg, arg))
712 arg = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000713 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000714 if (arg)
715 process_arg (arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000716#endif
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000717 nasm_free (envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000719
720 /*
721 * Now process the actual command line.
722 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000723 while (--argc)
724 {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000725 int i;
726 argv++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000727#ifdef TASM_COMPAT
728 if (argv[0][0] == '@') {
729 /* We have a response file, so process this as a set of
730 * arguments like the environment variable. This allows us
731 * to have multiple arguments on a single line, which is
732 * different to the -@resp file processing below for regular
733 * NASM.
734 */
735 char *str = malloc(2048);
736 FILE *f = fopen(&argv[0][1],"r");
737 if (!str) {
738 printf("out of memory");
739 exit(-1);
740 }
741 if (f) {
742 while (fgets(str,2048,f)) {
743 process_args(str);
744 }
745 fclose(f);
746 }
747 free(str);
748 argc--;
749 argv++;
750 }
751#endif
H. Peter Anvineba20a72002-04-30 20:53:55 +0000752 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000753 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000754 if ((rfile = fopen(p, "r"))) {
755 process_respfile (rfile);
756 fclose(rfile);
757 } else
758 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
759 "unable to open response file `%s'", p);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000760 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000761 } else
762 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000763 argv += i, argc -= i;
764 }
765
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000766 if (!*inname)
767 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
768 "no input file specified");
769}
770
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000771
H. Peter Anvineba20a72002-04-30 20:53:55 +0000772static void assemble_file (char *fname)
773{
774 char * value, * p, * q, * special, * line, debugid[80];
775 insn output_ins;
776 int i, rn_error, validid;
777 long seg, offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000778 struct tokenval tokval;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000779 expr * e;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000780 int pass, pass_max;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000781 int pass_cnt = 0; /* count actual passes */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000782
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000783 if (cmd_sb == 32 && cmd_cpu < IF_386)
784 report_error(ERR_FATAL, "command line: "
785 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000786
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000787 pass_max = optimizing + 2; /* passes 1, optimizing, then 2 */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000788 pass0 = !optimizing; /* start at 1 if not optimizing */
789 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000790 int pass1, pass2;
791 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000792
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000793 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
794 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000795 /* pass0 seq is 0, 0, 0,..., 1, 2 */
796
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000797 def_label = pass > 1 ? redefine_label : define_label;
798
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000800 sb = cmd_sb; /* set 'bits' to command line default */
801 cpu = cmd_cpu;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000802 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000803 if (*listname)
804 nasmlist.init(listname, report_error);
805 }
806 in_abs_seg = FALSE;
807 global_offset_changed = FALSE; /* set by redefine_label */
808 location.segment = ofmt->section(NULL, pass2, &sb);
809 if (pass > 1) {
810 saa_rewind (forwrefs);
811 forwref = saa_rstruct (forwrefs);
812 raa_free (offsets);
813 offsets = raa_init();
814 }
815 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
816 globallineno = 0;
817 if (pass == 1) location.known = TRUE;
818 location.offset = offs = GET_CURR_OFFS;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000819
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000820 while ( (line = preproc->getline()) )
821 {
822 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000823
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000824 /* here we parse our directives; this is not handled by the 'real'
825 * parser. */
826 if ( (i = getkw (line, &value)) )
827 {
828 switch (i) {
829 case 1: /* [SEGMENT n] */
830 seg = ofmt->section (value, pass2, &sb);
831 if (seg == NO_SEG) {
832 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
833 "segment name `%s' not recognised",
834 value);
835 } else {
836 in_abs_seg = FALSE;
837 location.segment = seg;
838 }
839 break;
840 case 2: /* [EXTERN label:special] */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000841 if (*value == '$') value++; /* skip initial $ if present */
842 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000843 q = value;
844 while (*q && *q != ':')
845 q++;
846 if (*q == ':') {
847 *q++ = '\0';
848 ofmt->symdef(value, 0L, 0L, 3, q);
849 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000850 } else if (pass0 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000851 q = value;
852 validid = TRUE;
853 if (!isidstart(*q))
854 validid = FALSE;
855 while (*q && *q != ':') {
856 if (!isidchar(*q))
857 validid = FALSE;
858 q++;
859 }
860 if (!validid) {
861 report_error (ERR_NONFATAL,
862 "identifier expected after EXTERN");
863 break;
864 }
865 if (*q == ':') {
866 *q++ = '\0';
867 special = q;
868 } else
869 special = NULL;
870 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
871 declare_as_global (value, special, report_error);
872 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
873 ofmt, report_error);
874 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000875 } /* else pass0 == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000876 break;
877 case 3: /* [BITS bits] */
878 sb = get_bits(value);
879 break;
880 case 4: /* [GLOBAL symbol:special] */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000881 if (*value == '$') value++; /* skip initial $ if present */
882 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000883 q = value;
884 while (*q && *q != ':')
885 q++;
886 if (*q == ':') {
887 *q++ = '\0';
888 ofmt->symdef(value, 0L, 0L, 3, q);
889 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000890 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000891 q = value;
892 validid = TRUE;
893 if (!isidstart(*q))
894 validid = FALSE;
895 while (*q && *q != ':') {
896 if (!isidchar(*q))
897 validid = FALSE;
898 q++;
899 }
900 if (!validid) {
901 report_error (ERR_NONFATAL,
902 "identifier expected after GLOBAL");
903 break;
904 }
905 if (*q == ':') {
906 *q++ = '\0';
907 special = q;
908 } else
909 special = NULL;
910 declare_as_global (value, special, report_error);
911 } /* pass == 1 */
912 break;
913 case 5: /* [COMMON symbol size:special] */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000914 if (*value == '$') value++; /* skip initial $ if present */
915 if (pass0 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000916 p = value;
917 validid = TRUE;
918 if (!isidstart(*p))
919 validid = FALSE;
920 while (*p && !isspace(*p)) {
921 if (!isidchar(*p))
922 validid = FALSE;
923 p++;
924 }
925 if (!validid) {
926 report_error (ERR_NONFATAL,
927 "identifier expected after COMMON");
928 break;
929 }
930 if (*p) {
931 long size;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000932
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000933 while (*p && isspace(*p))
934 *p++ = '\0';
935 q = p;
936 while (*q && *q != ':')
937 q++;
938 if (*q == ':') {
939 *q++ = '\0';
940 special = q;
941 } else
942 special = NULL;
943 size = readnum (p, &rn_error);
944 if (rn_error)
945 report_error (ERR_NONFATAL, "invalid size specified"
946 " in COMMON declaration");
947 else
948 define_common (value, seg_alloc(), size,
949 special, ofmt, report_error);
950 } else
951 report_error (ERR_NONFATAL, "no size specified in"
952 " COMMON declaration");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000953 } else if (pass0 == 2) { /* pass == 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000954 q = value;
955 while (*q && *q != ':') {
956 if (isspace(*q))
957 *q = '\0';
958 q++;
959 }
960 if (*q == ':') {
961 *q++ = '\0';
962 ofmt->symdef(value, 0L, 0L, 3, q);
963 }
964 }
965 break;
966 case 6: /* [ABSOLUTE address] */
967 stdscan_reset();
968 stdscan_bufptr = value;
969 tokval.t_type = TOKEN_INVALID;
970 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, report_error,
971 NULL);
972 if (e) {
973 if (!is_reloc(e))
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000974 report_error (pass0==1 ? ERR_NONFATAL : ERR_PANIC,
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000975 "cannot use non-relocatable expression as "
976 "ABSOLUTE address");
977 else {
978 abs_seg = reloc_seg(e);
979 abs_offset = reloc_value(e);
980 }
981 } else
982 if (pass==1) abs_offset = 0x100;/* don't go near zero in case of / */
983 else report_error (ERR_PANIC, "invalid ABSOLUTE address "
984 "in pass two");
985 in_abs_seg = TRUE;
986 location.segment = abs_seg;
987 break;
988 case 7: /* DEBUG */
989 p = value;
990 q = debugid;
991 validid = TRUE;
992 if (!isidstart(*p))
993 validid = FALSE;
994 while (*p && !isspace(*p)) {
995 if (!isidchar(*p))
996 validid = FALSE;
997 *q++ = *p++;
998 }
999 *q++ = 0;
1000 if (!validid) {
1001 report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
1002 "identifier expected after DEBUG");
1003 break;
1004 }
1005 while (*p && isspace(*p)) p++;
1006 if (pass==pass_max) ofmt->current_dfmt->debug_directive (debugid, p);
1007 break;
1008 case 8: /* [WARNING {+|-}warn-name] */
1009 if (pass1 == 1) {
1010 while (*value && isspace(*value))
1011 value++;
1012
1013 if (*value == '+' || *value == '-') {
1014 validid = (*value == '-') ? TRUE : FALSE;
1015 value++;
1016 } else
1017 validid = FALSE;
1018
1019 for (i=1; i<=ERR_WARN_MAX; i++)
1020 if (!nasm_stricmp(value, suppressed_names[i]))
H. Peter Anvineba20a72002-04-30 20:53:55 +00001021 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001022 if (i <= ERR_WARN_MAX)
1023 suppressed[i] = validid;
1024 else
1025 report_error (ERR_NONFATAL, "invalid warning id in WARNING directive");
1026 }
1027 break;
1028 case 9: /* cpu */
1029 cpu = get_cpu (value);
1030 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001031 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1032 while (*value && isspace(*value))
1033 value++;
1034
1035 if (*value == '+') {
1036 user_nolist = 0;
1037 }
1038 else {
1039 if (*value == '-') {
1040 user_nolist = 1;
1041 }
1042 else {
1043 report_error (ERR_NONFATAL, "invalid parameter to \"list\" directive");
1044 }
1045 }
1046 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001047 default:
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001048 if (!ofmt->directive (line+1, value, pass2))
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001049 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
1050 "unrecognised directive [%s]",
1051 line+1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001052 }
1053 }
1054 else /* it isn't a directive */
1055 {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001056 parse_line (pass1, line, &output_ins,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001057 report_error, evaluate,
1058 def_label);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001060 if (!optimizing && pass == 2) {
1061 if (forwref != NULL && globallineno == forwref->lineno) {
1062 output_ins.forw_ref = TRUE;
1063 do {
1064 output_ins.oprs[forwref->operand].opflags|= OPFLAG_FORWARD;
1065 forwref = saa_rstruct (forwrefs);
1066 } while (forwref != NULL && forwref->lineno == globallineno);
1067 } else
1068 output_ins.forw_ref = FALSE;
1069 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001070
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001071
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001072 if (!optimizing && output_ins.forw_ref)
1073 {
1074 if (pass == 1) {
1075 for(i = 0; i < output_ins.operands; i++)
1076 {
1077 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
1078 {
1079 struct forwrefinfo *fwinf =
1080 (struct forwrefinfo *)saa_wstruct(forwrefs);
1081 fwinf->lineno = globallineno;
1082 fwinf->operand = i;
1083 }
1084 }
1085 } else { /* pass == 2 */
1086 /*
1087 * Hack to prevent phase error in the code
1088 * rol ax,x
1089 * x equ 1
1090 *
1091 * If the second operand is a forward reference,
1092 * the UNITY property of the number 1 in that
1093 * operand is cancelled. Otherwise the above
1094 * sequence will cause a phase error.
1095 *
1096 * This hack means that the above code will
1097 * generate 286+ code.
1098 *
1099 * The forward reference will mean that the
1100 * operand will not have the UNITY property on
1101 * the first pass, so the pass behaviours will
1102 * be consistent.
1103 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001104
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001105 if (output_ins.operands >= 2 &&
1106 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1107 {
1108 output_ins.oprs[1].type &= ~(ONENESS|BYTENESS);
1109 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001110
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001111 } /* pass == 2 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001112
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001113 } /* forw_ref */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001114
H. Peter Anvineba20a72002-04-30 20:53:55 +00001115
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001116 if (output_ins.opcode == I_EQU) {
1117 if (pass1 == 1)
1118 {
1119 /*
1120 * Special `..' EQUs get processed in pass two,
1121 * except `..@' macro-processor EQUs which are done
1122 * in the normal place.
1123 */
1124 if (!output_ins.label)
1125 report_error (ERR_NONFATAL,
1126 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001127
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001128 else if (output_ins.label[0] != '.' ||
1129 output_ins.label[1] != '.' ||
1130 output_ins.label[2] == '@')
1131 {
1132 if (output_ins.operands == 1 &&
1133 (output_ins.oprs[0].type & IMMEDIATE) &&
1134 output_ins.oprs[0].wrt == NO_SEG)
1135 {
1136 int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
1137 def_label (output_ins.label,
1138 output_ins.oprs[0].segment,
1139 output_ins.oprs[0].offset,
1140 NULL, FALSE, isext, ofmt, report_error);
1141 }
1142 else if (output_ins.operands == 2 &&
1143 (output_ins.oprs[0].type & IMMEDIATE) &&
1144 (output_ins.oprs[0].type & COLON) &&
1145 output_ins.oprs[0].segment == NO_SEG &&
1146 output_ins.oprs[0].wrt == NO_SEG &&
1147 (output_ins.oprs[1].type & IMMEDIATE) &&
1148 output_ins.oprs[1].segment == NO_SEG &&
1149 output_ins.oprs[1].wrt == NO_SEG)
1150 {
1151 def_label (output_ins.label,
1152 output_ins.oprs[0].offset | SEG_ABS,
1153 output_ins.oprs[1].offset,
1154 NULL, FALSE, FALSE, ofmt, report_error);
1155 }
1156 else
1157 report_error(ERR_NONFATAL, "bad syntax for EQU");
1158 }
1159 } else { /* pass == 2 */
1160 /*
1161 * Special `..' EQUs get processed here, except
1162 * `..@' macro processor EQUs which are done above.
1163 */
1164 if (output_ins.label[0] == '.' &&
1165 output_ins.label[1] == '.' &&
1166 output_ins.label[2] != '@')
1167 {
1168 if (output_ins.operands == 1 &&
1169 (output_ins.oprs[0].type & IMMEDIATE)) {
1170 define_label (output_ins.label,
1171 output_ins.oprs[0].segment,
1172 output_ins.oprs[0].offset,
1173 NULL, FALSE, FALSE, ofmt, report_error);
1174 }
1175 else if (output_ins.operands == 2 &&
1176 (output_ins.oprs[0].type & IMMEDIATE) &&
1177 (output_ins.oprs[0].type & COLON) &&
1178 output_ins.oprs[0].segment == NO_SEG &&
1179 (output_ins.oprs[1].type & IMMEDIATE) &&
1180 output_ins.oprs[1].segment == NO_SEG)
1181 {
1182 define_label (output_ins.label,
1183 output_ins.oprs[0].offset | SEG_ABS,
1184 output_ins.oprs[1].offset,
1185 NULL, FALSE, FALSE, ofmt, report_error);
1186 }
1187 else
1188 report_error(ERR_NONFATAL, "bad syntax for EQU");
1189 }
1190 } /* pass == 2 */
1191 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001192
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001193 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001194
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001195 long l = insn_size (location.segment, offs, sb, cpu,
1196 &output_ins, report_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001197
1198 /* if (using_debug_info) && output_ins.opcode != -1)*/
1199 if (using_debug_info); /* fbk 12/29/00 */
1200
1201 {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001202 /* this is done here so we can do debug type info */
1203 long typeinfo = TYS_ELEMENTS(output_ins.operands);
1204 switch (output_ins.opcode) {
1205 case I_RESB:
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001206 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1207 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001208 case I_RESW:
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001209 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1210 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001211 case I_RESD:
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001212 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1213 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001214 case I_RESQ:
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001215 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1216 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001217 case I_REST:
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001218 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1219 break;
1220 case I_DB:
1221 typeinfo |= TY_BYTE;
1222 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001223 case I_DW:
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001224 typeinfo |= TY_WORD;
1225 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001226 case I_DD:
1227 if (output_ins.eops_float)
1228 typeinfo |= TY_FLOAT;
1229 else
1230 typeinfo |= TY_DWORD;
1231 break;
1232 case I_DQ:
1233 typeinfo |= TY_QWORD;
1234 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001235 case I_DT:
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001236 typeinfo |= TY_TBYTE;
1237 break;
1238 default:
1239 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001240
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001241 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001242
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001243 ofmt->current_dfmt->debug_typevalue(typeinfo);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001244
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001245 }
1246 if (l != -1) {
1247 offs += l;
1248 SET_CURR_OFFS (offs);
1249 }
1250 /*
1251 * else l == -1 => invalid instruction, which will be
1252 * flagged as an error on pass 2
1253 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001254
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001255 } else { /* pass == 2 */
1256 offs += assemble (location.segment, offs, sb, cpu,
1257 &output_ins, ofmt, report_error, &nasmlist);
1258 SET_CURR_OFFS (offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001259
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001260 }
1261 } /* not an EQU */
1262 cleanup_insn (&output_ins);
1263 }
1264 nasm_free (line);
1265 location.offset = offs = GET_CURR_OFFS;
1266 } /* end while (line = preproc->getline... */
1267
1268 if (pass1==2 && global_offset_changed)
1269 report_error(ERR_NONFATAL, "phase error detected at end of assembly.");
1270
1271 if (pass1 == 1) preproc->cleanup();
1272
1273 if (pass1==1 && terminate_after_phase) {
1274 fclose(ofile);
1275 remove(outname);
1276 if (want_usage)
1277 usage();
1278 exit (1);
1279 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001280 pass_cnt++;
1281 if (pass>1 && !global_offset_changed) {
1282 pass0++;
1283 if (pass0==2) pass = pass_max - 1;
1284 } else if (!optimizing) pass0++;
1285
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001286 } /* for (pass=1; pass<=2; pass++) */
1287
1288 nasmlist.cleanup();
1289#if 1
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001290 if (optimizing && using_debug_info) /* -On and -g switches */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001291 fprintf(error_file,
1292 "info:: assembly required 1+%d+1 passes\n", pass_cnt-2);
1293#endif
1294} /* exit from assemble_file (...) */
1295
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001296
H. Peter Anvineba20a72002-04-30 20:53:55 +00001297static int getkw (char *buf, char **value)
1298{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001299 char *p, *q;
1300
1301 if (*buf!='[')
1302 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001303
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001304 p = buf;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001305
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001306 while (*p && *p != ']') p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001307
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001308 if (!*p)
1309 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001310
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001311 q = p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001312
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001313 while (*p && *p != ';') {
1314 if (!isspace(*p))
1315 return 0;
1316 p++;
1317 }
1318 q[1] = '\0';
1319
1320 p = buf+1;
1321 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1322 buf++;
1323 if (*buf==']') {
1324 *buf = '\0';
1325 *value = buf;
1326 } else {
1327 *buf++ = '\0';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001328 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001329 *value = buf;
1330 while (*buf!=']') buf++;
1331 *buf++ = '\0';
1332 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001333#if 0
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001334 for (q=p; *q; q++)
1335 *q = tolower(*q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001336#endif
1337 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001338 return 1;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001339 if (!nasm_stricmp(p, "extern"))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001340 return 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001341 if (!nasm_stricmp(p, "bits"))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001342 return 3;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001343 if (!nasm_stricmp(p, "global"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001344 return 4;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345 if (!nasm_stricmp(p, "common"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001346 return 5;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347 if (!nasm_stricmp(p, "absolute"))
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001348 return 6;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001349 if (!nasm_stricmp(p, "debug"))
H. Peter Anvineba20a72002-04-30 20:53:55 +00001350 return 7;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351 if (!nasm_stricmp(p, "warning"))
1352 return 8;
1353 if (!nasm_stricmp(p, "cpu"))
1354 return 9;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001355 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1356 return 10;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001357 return -1;
1358}
1359
H. Peter Anvineba20a72002-04-30 20:53:55 +00001360static void report_error (int severity, char *fmt, ...)
1361{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001362 va_list ap;
1363
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001364 /*
1365 * See if it's a suppressed warning.
1366 */
1367 if ((severity & ERR_MASK) == ERR_WARNING &&
1368 (severity & ERR_WARN_MASK) != 0 &&
1369 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1370 return; /* and bail out if so */
1371
H. Peter Anvin76690a12002-04-30 20:52:49 +00001372 /*
1373 * See if it's a pass-one only warning and we're not in pass one.
1374 */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001375 if ((severity & ERR_PASS1) && pass0 == 2)
H. Peter Anvin76690a12002-04-30 20:52:49 +00001376 return;
1377
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001378 if (severity & ERR_NOFILE)
H. Peter Anvin620515a2002-04-30 20:57:38 +00001379 fputs ("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001380 else {
1381 char * currentfile = NULL;
1382 long lineno = 0;
1383 src_get (&lineno, &currentfile);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001384 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001385 nasm_free (currentfile);
1386 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001387
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001388 switch (severity & ERR_MASK) {
1389 case ERR_WARNING:
1390 fputs ("warning: ", error_file); break;
1391 case ERR_NONFATAL:
1392 fputs ("error: ", error_file); break;
1393 case ERR_FATAL:
1394 fputs ("fatal: ", error_file); break;
1395 case ERR_PANIC:
1396 fputs ("panic: ", error_file); break;
1397 case ERR_DEBUG:
1398 fputs("debug: ", error_file); break;
1399 }
1400
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001401 va_start (ap, fmt);
H. Peter Anvin620515a2002-04-30 20:57:38 +00001402 vfprintf (error_file, fmt, ap);
1403 fputc ('\n', error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001404
1405 if (severity & ERR_USAGE)
1406 want_usage = TRUE;
1407
1408 switch (severity & ERR_MASK) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001409 case ERR_WARNING: case ERR_DEBUG:
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001410 /* no further action, by definition */
1411 break;
1412 case ERR_NONFATAL:
1413 terminate_after_phase = TRUE;
1414 break;
1415 case ERR_FATAL:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001416 if (ofile) {
1417 fclose(ofile);
1418 remove(outname);
1419 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001420 if (want_usage)
1421 usage();
1422 exit(1); /* instantly die */
1423 break; /* placate silly compilers */
1424 case ERR_PANIC:
H. Peter Anvin620515a2002-04-30 20:57:38 +00001425 fflush(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001426/* abort(); */ /* halt, catch fire, and dump core */
1427 exit(3);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001428 break;
1429 }
1430}
1431
H. Peter Anvineba20a72002-04-30 20:53:55 +00001432static void usage(void)
1433{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001434 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001435}
1436
H. Peter Anvineba20a72002-04-30 20:53:55 +00001437static void register_output_formats(void)
1438{
1439 ofmt = ofmt_register (report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001440}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001441
1442#define BUF_DELTA 512
1443
1444static FILE *no_pp_fp;
1445static efunc no_pp_err;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001446static ListGen *no_pp_list;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001447static long no_pp_lineinc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001448
H. Peter Anvin76690a12002-04-30 20:52:49 +00001449static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
H. Peter Anvineba20a72002-04-30 20:53:55 +00001450 ListGen *listgen)
1451{
1452 src_set_fname(nasm_strdup(file));
1453 src_set_linnum(0);
1454 no_pp_lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001455 no_pp_err = error;
1456 no_pp_fp = fopen(file, "r");
1457 if (!no_pp_fp)
1458 no_pp_err (ERR_FATAL | ERR_NOFILE,
1459 "unable to open input file `%s'", file);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001460 no_pp_list = listgen;
1461 (void) pass; /* placate compilers */
1462 (void) eval; /* placate compilers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463}
1464
H. Peter Anvineba20a72002-04-30 20:53:55 +00001465static char *no_pp_getline (void)
1466{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001467 char *buffer, *p, *q;
1468 int bufsize;
1469
1470 bufsize = BUF_DELTA;
1471 buffer = nasm_malloc(BUF_DELTA);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001472 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1473
1474 while (1) { /* Loop to handle %line */
1475
1476 p = buffer;
1477 while (1) { /* Loop to handle long lines */
1478 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1479 if (!q)
1480 break;
1481 p += strlen(p);
1482 if (p > buffer && p[-1] == '\n')
1483 break;
1484 if (p-buffer > bufsize-10) {
1485 int offset;
1486 offset = p - buffer;
1487 bufsize += BUF_DELTA;
1488 buffer = nasm_realloc(buffer, bufsize);
1489 p = buffer + offset;
1490 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001491 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001492
1493 if (!q && p == buffer) {
1494 nasm_free (buffer);
1495 return NULL;
1496 }
1497
1498 /*
1499 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1500 * them are present at the end of the line.
1501 */
1502 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1503
1504 if (!strncmp(buffer, "%line", 5)) {
1505 long ln;
1506 int li;
1507 char *nm = nasm_malloc(strlen(buffer));
1508 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1509 nasm_free( src_set_fname(nm) );
1510 src_set_linnum(ln);
1511 no_pp_lineinc = li;
1512 continue;
1513 }
1514 nasm_free(nm);
1515 }
1516 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001517 }
1518
H. Peter Anvin76690a12002-04-30 20:52:49 +00001519 no_pp_list->line (LIST_READ, buffer);
1520
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001521 return buffer;
1522}
1523
H. Peter Anvineba20a72002-04-30 20:53:55 +00001524static void no_pp_cleanup (void)
1525{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001526 fclose(no_pp_fp);
1527}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001528
1529static unsigned long get_cpu (char *value)
1530{
1531
1532 if (!strcmp(value, "8086")) return IF_8086;
1533 if (!strcmp(value, "186")) return IF_186;
1534 if (!strcmp(value, "286")) return IF_286;
1535 if (!strcmp(value, "386")) return IF_386;
1536 if (!strcmp(value, "486")) return IF_486;
1537 if (!strcmp(value, "586") ||
1538 !nasm_stricmp(value, "pentium") ) return IF_PENT;
1539 if (!strcmp(value, "686") ||
1540 !nasm_stricmp(value, "ppro") ||
1541 !nasm_stricmp(value, "p2") ) return IF_P6;
1542 if (!nasm_stricmp(value, "p3") ||
1543 !nasm_stricmp(value, "katmai") ) return IF_KATMAI;
1544
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001545 report_error (pass0<2 ? ERR_NONFATAL : ERR_FATAL, "unknown 'cpu' type");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001546
1547 return IF_PLEVEL; /* the maximum level */
1548}
1549
1550
1551static int get_bits (char *value)
1552{
1553 int i;
1554
1555 if ((i = atoi(value)) == 16) return i; /* set for a 16-bit segment */
1556 else if (i == 32) {
1557 if (cpu < IF_386) {
1558 report_error(ERR_NONFATAL,
1559 "cannot specify 32-bit segment on processor below a 386");
1560 i = 16;
1561 }
1562 } else {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001563 report_error(pass0<2 ? ERR_NONFATAL : ERR_FATAL,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001564 "`%s' is not a valid segment size; must be 16 or 32",
1565 value);
1566 i = 16;
1567 }
1568 return i;
1569}
1570
1571/* end of nasm.c */