.gitignore file doesn't need to be in the release file
[nasm.git] / nasm.c
blobfa13460aea9f93ad3072ea97c968d7da21472bc5
1 /* The Netwide Assembler main program module
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 */
9 #include "compiler.h"
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
17 #include <limits.h>
19 #include "nasm.h"
20 #include "nasmlib.h"
21 #include "stdscan.h"
22 #include "insns.h"
23 #include "preproc.h"
24 #include "parser.h"
25 #include "eval.h"
26 #include "assemble.h"
27 #include "labels.h"
28 #include "outform.h"
29 #include "listing.h"
31 struct forwrefinfo { /* info held on forward refs. */
32 int lineno;
33 int operand;
36 static int get_bits(char *value);
37 static uint32_t get_cpu(char *cpu_str);
38 static void parse_cmdline(int, char **);
39 static void assemble_file(char *);
40 static void register_output_formats(void);
41 static void report_error_gnu(int severity, const char *fmt, ...);
42 static void report_error_vc(int severity, const char *fmt, ...);
43 static void report_error_common(int severity, const char *fmt,
44 va_list args);
45 static int is_suppressed_warning(int severity);
46 static void usage(void);
47 static efunc report_error;
49 static int using_debug_info, opt_verbose_info;
50 bool tasm_compatible_mode = false;
51 int pass0;
52 int maxbits = 0;
53 int globalrel = 0;
55 static char inname[FILENAME_MAX];
56 static char outname[FILENAME_MAX];
57 static char listname[FILENAME_MAX];
58 static char errname[FILENAME_MAX];
59 static int globallineno; /* for forward-reference tracking */
60 /* static int pass = 0; */
61 static struct ofmt *ofmt = NULL;
63 static FILE *error_file; /* Where to write error messages */
65 static FILE *ofile = NULL;
66 int optimizing = -1; /* number of optimization passes to take */
67 static int sb, cmd_sb = 16; /* by default */
68 static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
69 static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
70 bool global_offset_changed; /* referenced in labels.c */
72 static struct location location;
73 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
74 int32_t abs_seg; /* ABSOLUTE segment basis */
75 int32_t abs_offset; /* ABSOLUTE offset */
77 static struct RAA *offsets;
79 static struct SAA *forwrefs; /* keep track of forward references */
80 static const struct forwrefinfo *forwref;
82 static Preproc *preproc;
83 enum op_type {
84 op_normal, /* Preprocess and assemble */
85 op_preprocess, /* Preprocess only */
86 op_depend, /* Generate dependencies */
87 op_depend_missing_ok, /* Generate dependencies, missing OK */
89 static enum op_type operating_mode;
92 * Which of the suppressible warnings are suppressed. Entry zero
93 * doesn't do anything. Initial defaults are given here.
95 static char suppressed[1 + ERR_WARN_MAX] = {
96 0, true, true, true, false, true
100 * The option names for the suppressible warnings. As before, entry
101 * zero does nothing.
103 static const char *suppressed_names[1 + ERR_WARN_MAX] = {
104 NULL, "macro-params", "macro-selfref", "orphan-labels",
105 "number-overflow",
106 "gnu-elf-extensions"
110 * The explanations for the suppressible warnings. As before, entry
111 * zero does nothing.
113 static const char *suppressed_what[1 + ERR_WARN_MAX] = {
114 NULL,
115 "macro calls with wrong no. of params",
116 "cyclic macro self-references",
117 "labels alone on lines without trailing `:'",
118 "numeric constants greater than 0xFFFFFFFF",
119 "using 8- or 16-bit relocation in ELF, a GNU extension"
123 * This is a null preprocessor which just copies lines from input
124 * to output. It's used when someone explicitly requests that NASM
125 * not preprocess their source file.
128 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
129 static char *no_pp_getline(void);
130 static void no_pp_cleanup(int);
131 static Preproc no_pp = {
132 no_pp_reset,
133 no_pp_getline,
134 no_pp_cleanup
138 * get/set current offset...
140 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
141 raa_read(offsets,location.segment))
142 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
143 (void)(offsets=raa_write(offsets,location.segment,(x))))
145 static int want_usage;
146 static int terminate_after_phase;
147 int user_nolist = 0; /* fbk 9/2/00 */
149 static void nasm_fputs(const char *line, FILE * outfile)
151 if (outfile) {
152 fputs(line, outfile);
153 fputc('\n', outfile);
154 } else
155 puts(line);
158 int main(int argc, char **argv)
160 pass0 = 1;
161 want_usage = terminate_after_phase = false;
162 report_error = report_error_gnu;
164 error_file = stderr;
166 nasm_set_malloc_error(report_error);
167 offsets = raa_init();
168 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
170 preproc = &nasmpp;
171 operating_mode = op_normal;
173 seg_init();
175 register_output_formats();
177 parse_cmdline(argc, argv);
179 if (terminate_after_phase) {
180 if (want_usage)
181 usage();
182 return 1;
185 /* If debugging info is disabled, suppress any debug calls */
186 if (!using_debug_info)
187 ofmt->current_dfmt = &null_debug_form;
189 if (ofmt->stdmac)
190 pp_extra_stdmac(ofmt->stdmac);
191 parser_global_info(ofmt, &location);
192 eval_global_info(ofmt, lookup_label, &location);
194 /* define some macros dependent of command-line */
196 char temp[64];
197 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
198 ofmt->shortname);
199 pp_pre_define(temp);
202 switch (operating_mode) {
203 case op_depend_missing_ok:
204 pp_include_path(NULL); /* "assume generated" */
205 /* fall through */
206 case op_depend:
208 char *line;
209 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
210 if (outname[0] == '\0')
211 ofmt->filename(inname, outname, report_error);
212 ofile = NULL;
213 fprintf(stdout, "%s: %s", outname, inname);
214 while ((line = preproc->getline()))
215 nasm_free(line);
216 preproc->cleanup(0);
217 putc('\n', stdout);
219 break;
221 case op_preprocess:
223 char *line;
224 char *file_name = NULL;
225 int32_t prior_linnum = 0;
226 int lineinc = 0;
228 if (*outname) {
229 ofile = fopen(outname, "w");
230 if (!ofile)
231 report_error(ERR_FATAL | ERR_NOFILE,
232 "unable to open output file `%s'",
233 outname);
234 } else
235 ofile = NULL;
237 location.known = false;
239 /* pass = 1; */
240 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
241 while ((line = preproc->getline())) {
243 * We generate %line directives if needed for later programs
245 int32_t linnum = prior_linnum += lineinc;
246 int altline = src_get(&linnum, &file_name);
247 if (altline) {
248 if (altline == 1 && lineinc == 1)
249 nasm_fputs("", ofile);
250 else {
251 lineinc = (altline != -1 || lineinc != 1);
252 fprintf(ofile ? ofile : stdout,
253 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
254 file_name);
256 prior_linnum = linnum;
258 nasm_fputs(line, ofile);
259 nasm_free(line);
261 nasm_free(file_name);
262 preproc->cleanup(0);
263 if (ofile)
264 fclose(ofile);
265 if (ofile && terminate_after_phase)
266 remove(outname);
268 break;
270 case op_normal:
273 * We must call ofmt->filename _anyway_, even if the user
274 * has specified their own output file, because some
275 * formats (eg OBJ and COFF) use ofmt->filename to find out
276 * the name of the input file and then put that inside the
277 * file.
279 ofmt->filename(inname, outname, report_error);
281 ofile = fopen(outname, "wb");
282 if (!ofile) {
283 report_error(ERR_FATAL | ERR_NOFILE,
284 "unable to open output file `%s'", outname);
288 * We must call init_labels() before ofmt->init() since
289 * some object formats will want to define labels in their
290 * init routines. (eg OS/2 defines the FLAT group)
292 init_labels();
294 ofmt->init(ofile, report_error, define_label, evaluate);
296 assemble_file(inname);
298 if (!terminate_after_phase) {
299 ofmt->cleanup(using_debug_info);
300 cleanup_labels();
301 } else {
303 * We had an fclose on the output file here, but we
304 * actually do that in all the object file drivers as well,
305 * so we're leaving out the one here.
306 * fclose (ofile);
308 remove(outname);
309 if (listname[0])
310 remove(listname);
313 break;
316 if (want_usage)
317 usage();
319 raa_free(offsets);
320 saa_free(forwrefs);
321 eval_cleanup();
322 stdscan_cleanup();
324 if (terminate_after_phase)
325 return 1;
326 else
327 return 0;
331 * Get a parameter for a command line option.
332 * First arg must be in the form of e.g. -f...
334 static char *get_param(char *p, char *q, int *advance)
336 *advance = 0;
337 if (p[2]) { /* the parameter's in the option */
338 p += 2;
339 while (isspace(*p))
340 p++;
341 return p;
343 if (q && q[0]) {
344 *advance = 1;
345 return q;
347 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
348 "option `-%c' requires an argument", p[1]);
349 return NULL;
352 struct textargs {
353 const char *label;
354 int value;
357 #define OPT_PREFIX 0
358 #define OPT_POSTFIX 1
359 struct textargs textopts[] = {
360 {"prefix", OPT_PREFIX},
361 {"postfix", OPT_POSTFIX},
362 {NULL, 0}
365 int stopoptions = 0;
366 static int process_arg(char *p, char *q)
368 char *param;
369 int i, advance = 0;
371 if (!p || !p[0])
372 return 0;
374 if (p[0] == '-' && !stopoptions) {
375 switch (p[1]) {
376 case 's':
377 error_file = stdout;
378 break;
379 case 'o': /* these parameters take values */
380 case 'O':
381 case 'f':
382 case 'p':
383 case 'P':
384 case 'd':
385 case 'D':
386 case 'i':
387 case 'I':
388 case 'l':
389 case 'F':
390 case 'X':
391 case 'u':
392 case 'U':
393 case 'Z':
394 if (!(param = get_param(p, q, &advance)))
395 break;
396 if (p[1] == 'o') { /* output file */
397 strcpy(outname, param);
398 } else if (p[1] == 'f') { /* output format */
399 ofmt = ofmt_find(param);
400 if (!ofmt) {
401 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
402 "unrecognised output format `%s' - "
403 "use -hf for a list", param);
404 } else
405 ofmt->current_dfmt = ofmt->debug_formats[0];
406 } else if (p[1] == 'O') { /* Optimization level */
407 int opt;
409 if (!*param) {
410 /* Naked -O == -Ox */
411 optimizing = INT_MAX >> 1; /* Almost unlimited */
412 } else {
413 while (*param) {
414 switch (*param) {
415 case '0': case '1': case '2': case '3': case '4':
416 case '5': case '6': case '7': case '8': case '9':
417 opt = strtoul(param, &param, 10);
419 /* -O0 -> optimizing == -1, 0.98 behaviour */
420 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
421 if (opt < 2)
422 optimizing = opt - 1;
423 else if (opt <= 5)
424 /* The optimizer seems to have problems with
425 < 5 passes? Hidden bug? */
426 optimizing = 5; /* 5 passes */
427 else
428 optimizing = opt; /* More than 5 passes */
429 break;
431 case 'v':
432 case '+':
433 param++;
434 opt_verbose_info = true;
435 break;
437 case 'x':
438 param++;
439 optimizing = INT_MAX >> 1; /* Almost unlimited */
440 break;
442 default:
443 report_error(ERR_FATAL,
444 "unknown optimization option -O%c\n",
445 *param);
446 break;
450 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
451 pp_pre_include(param);
452 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
453 pp_pre_define(param);
454 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
455 pp_pre_undefine(param);
456 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
457 pp_include_path(param);
458 } else if (p[1] == 'l') { /* listing file */
459 strcpy(listname, param);
460 } else if (p[1] == 'Z') { /* error messages file */
461 strcpy(errname, param);
462 } else if (p[1] == 'F') { /* specify debug format */
463 ofmt->current_dfmt = dfmt_find(ofmt, param);
464 if (!ofmt->current_dfmt) {
465 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
466 "unrecognized debug format `%s' for"
467 " output format `%s'",
468 param, ofmt->shortname);
470 } else if (p[1] == 'X') { /* specify error reporting format */
471 if (nasm_stricmp("vc", param) == 0)
472 report_error = report_error_vc;
473 else if (nasm_stricmp("gnu", param) == 0)
474 report_error = report_error_gnu;
475 else
476 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
477 "unrecognized error reporting format `%s'",
478 param);
480 break;
481 case 'g':
482 using_debug_info = true;
483 break;
484 case 'h':
485 printf
486 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
487 "[-l listfile]\n"
488 " [options...] [--] filename\n"
489 " or nasm -v for version info\n\n"
490 " -t assemble in SciTech TASM compatible mode\n"
491 " -g generate debug information in selected format.\n");
492 printf
493 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
494 " -a don't preprocess (assemble only)\n"
495 " -M generate Makefile dependencies on stdout\n"
496 " -MG d:o, missing files assumed generated\n\n"
497 " -Z<file> redirect error messages to file\n"
498 " -s redirect error messages to stdout\n\n"
499 " -F format select a debugging format\n\n"
500 " -I<path> adds a pathname to the include file path\n");
501 printf
502 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
503 " -P<file> pre-includes a file\n"
504 " -D<macro>[=<value>] pre-defines a macro\n"
505 " -U<macro> undefines a macro\n"
506 " -X<format> specifies error reporting format (gnu or vc)\n"
507 " -w+foo enables warnings about foo; -w-foo disables them\n"
508 "where foo can be:\n");
509 for (i = 1; i <= ERR_WARN_MAX; i++)
510 printf(" %-23s %s (default %s)\n",
511 suppressed_names[i], suppressed_what[i],
512 suppressed[i] ? "off" : "on");
513 printf
514 ("\nresponse files should contain command line parameters"
515 ", one per line.\n");
516 if (p[2] == 'f') {
517 printf("\nvalid output formats for -f are"
518 " (`*' denotes default):\n");
519 ofmt_list(ofmt, stdout);
520 } else {
521 printf("\nFor a list of valid output formats, use -hf.\n");
522 printf("For a list of debug formats, use -f <form> -y.\n");
524 exit(0); /* never need usage message here */
525 break;
526 case 'y':
527 printf("\nvalid debug formats for '%s' output format are"
528 " ('*' denotes default):\n", ofmt->shortname);
529 dfmt_list(ofmt, stdout);
530 exit(0);
531 break;
532 case 't':
533 tasm_compatible_mode = true;
534 break;
535 case 'v':
537 const char *nasm_version_string =
538 "NASM version " NASM_VER " compiled on " __DATE__
539 #ifdef DEBUG
540 " with -DDEBUG"
541 #endif
543 puts(nasm_version_string);
544 exit(0); /* never need usage message here */
546 break;
547 case 'e': /* preprocess only */
548 case 'E':
549 operating_mode = op_preprocess;
550 break;
551 case 'a': /* assemble only - don't preprocess */
552 preproc = &no_pp;
553 break;
554 case 'w':
555 if (p[2] != '+' && p[2] != '-') {
556 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
557 "invalid option to `-w'");
558 } else {
559 for (i = 1; i <= ERR_WARN_MAX; i++)
560 if (!nasm_stricmp(p + 3, suppressed_names[i]))
561 break;
562 if (i <= ERR_WARN_MAX)
563 suppressed[i] = (p[2] == '-');
564 else
565 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
566 "invalid option to `-w'");
568 break;
569 case 'M':
570 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
571 break;
573 case '-':
575 int s;
577 if (p[2] == 0) { /* -- => stop processing options */
578 stopoptions = 1;
579 break;
581 for (s = 0; textopts[s].label; s++) {
582 if (!nasm_stricmp(p + 2, textopts[s].label)) {
583 break;
587 switch (s) {
589 case OPT_PREFIX:
590 case OPT_POSTFIX:
592 if (!q) {
593 report_error(ERR_NONFATAL | ERR_NOFILE |
594 ERR_USAGE,
595 "option `--%s' requires an argument",
596 p + 2);
597 break;
598 } else {
599 advance = 1, param = q;
602 if (s == OPT_PREFIX) {
603 strncpy(lprefix, param, PREFIX_MAX - 1);
604 lprefix[PREFIX_MAX - 1] = 0;
605 break;
607 if (s == OPT_POSTFIX) {
608 strncpy(lpostfix, param, POSTFIX_MAX - 1);
609 lpostfix[POSTFIX_MAX - 1] = 0;
610 break;
612 break;
614 default:
616 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
617 "unrecognised option `--%s'", p + 2);
618 break;
621 break;
624 default:
625 if (!ofmt->setinfo(GI_SWITCH, &p))
626 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
627 "unrecognised option `-%c'", p[1]);
628 break;
630 } else {
631 if (*inname) {
632 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
633 "more than one input file specified");
634 } else
635 strcpy(inname, p);
638 return advance;
641 #define ARG_BUF_DELTA 128
643 static void process_respfile(FILE * rfile)
645 char *buffer, *p, *q, *prevarg;
646 int bufsize, prevargsize;
648 bufsize = prevargsize = ARG_BUF_DELTA;
649 buffer = nasm_malloc(ARG_BUF_DELTA);
650 prevarg = nasm_malloc(ARG_BUF_DELTA);
651 prevarg[0] = '\0';
653 while (1) { /* Loop to handle all lines in file */
655 p = buffer;
656 while (1) { /* Loop to handle long lines */
657 q = fgets(p, bufsize - (p - buffer), rfile);
658 if (!q)
659 break;
660 p += strlen(p);
661 if (p > buffer && p[-1] == '\n')
662 break;
663 if (p - buffer > bufsize - 10) {
664 int offset;
665 offset = p - buffer;
666 bufsize += ARG_BUF_DELTA;
667 buffer = nasm_realloc(buffer, bufsize);
668 p = buffer + offset;
672 if (!q && p == buffer) {
673 if (prevarg[0])
674 process_arg(prevarg, NULL);
675 nasm_free(buffer);
676 nasm_free(prevarg);
677 return;
681 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
682 * them are present at the end of the line.
684 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
686 while (p > buffer && isspace(p[-1]))
687 *--p = '\0';
689 p = buffer;
690 while (isspace(*p))
691 p++;
693 if (process_arg(prevarg, p))
694 *p = '\0';
696 if (strlen(p) > prevargsize - 10) {
697 prevargsize += ARG_BUF_DELTA;
698 prevarg = nasm_realloc(prevarg, prevargsize);
700 strcpy(prevarg, p);
704 /* Function to process args from a string of args, rather than the
705 * argv array. Used by the environment variable and response file
706 * processing.
708 static void process_args(char *args)
710 char *p, *q, *arg, *prevarg;
711 char separator = ' ';
713 p = args;
714 if (*p && *p != '-')
715 separator = *p++;
716 arg = NULL;
717 while (*p) {
718 q = p;
719 while (*p && *p != separator)
720 p++;
721 while (*p == separator)
722 *p++ = '\0';
723 prevarg = arg;
724 arg = q;
725 if (process_arg(prevarg, arg))
726 arg = NULL;
728 if (arg)
729 process_arg(arg, NULL);
732 static void parse_cmdline(int argc, char **argv)
734 FILE *rfile;
735 char *envreal, *envcopy = NULL, *p, *arg;
737 *inname = *outname = *listname = *errname = '\0';
740 * First, process the NASMENV environment variable.
742 envreal = getenv("NASMENV");
743 arg = NULL;
744 if (envreal) {
745 envcopy = nasm_strdup(envreal);
746 process_args(envcopy);
747 nasm_free(envcopy);
751 * Now process the actual command line.
753 while (--argc) {
754 int i;
755 argv++;
756 if (argv[0][0] == '@') {
757 /* We have a response file, so process this as a set of
758 * arguments like the environment variable. This allows us
759 * to have multiple arguments on a single line, which is
760 * different to the -@resp file processing below for regular
761 * NASM.
763 char *str = malloc(2048);
764 FILE *f = fopen(&argv[0][1], "r");
765 if (!str) {
766 printf("out of memory");
767 exit(-1);
769 if (f) {
770 while (fgets(str, 2048, f)) {
771 process_args(str);
773 fclose(f);
775 free(str);
776 argc--;
777 argv++;
779 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
780 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
781 if (p) {
782 rfile = fopen(p, "r");
783 if (rfile) {
784 process_respfile(rfile);
785 fclose(rfile);
786 } else
787 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
788 "unable to open response file `%s'", p);
790 } else
791 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
792 argv += i, argc -= i;
795 if (!*inname)
796 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
797 "no input file specified");
799 /* Look for basic command line typos. This definitely doesn't
800 catch all errors, but it might help cases of fumbled fingers. */
801 if (!strcmp(inname, errname) || !strcmp(inname, outname) ||
802 !strcmp(inname, listname))
803 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
804 "file `%s' is both input and output file",
805 inname);
807 if (*errname) {
808 error_file = fopen(errname, "w");
809 if (!error_file) {
810 error_file = stderr; /* Revert to default! */
811 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
812 "cannot open file `%s' for error messages",
813 errname);
818 /* List of directives */
819 enum directives {
820 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
821 D_EXTERN, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
823 static const char *directives[] = {
824 "", "absolute", "bits", "common", "cpu", "debug", "default",
825 "extern", "global", "list", "section", "segment", "warning"
827 static enum directives getkw(char **directive, char **value);
829 static void assemble_file(char *fname)
831 char *directive, *value, *p, *q, *special, *line, debugid[80];
832 insn output_ins;
833 int i, validid;
834 bool rn_error;
835 int32_t seg, offs;
836 struct tokenval tokval;
837 expr *e;
838 int pass, pass_max;
839 int pass_cnt = 0; /* count actual passes */
841 if (cmd_sb == 32 && cmd_cpu < IF_386)
842 report_error(ERR_FATAL, "command line: "
843 "32-bit segment size requires a higher cpu");
845 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
846 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
847 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
848 int pass1, pass2;
849 ldfunc def_label;
851 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
852 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
853 /* pass0 seq is 0, 0, 0,..., 1, 2 */
855 def_label = pass > 1 ? redefine_label : define_label;
857 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
858 cpu = cmd_cpu;
859 if (pass0 == 2) {
860 if (*listname)
861 nasmlist.init(listname, report_error);
863 in_abs_seg = false;
864 global_offset_changed = false; /* set by redefine_label */
865 location.segment = ofmt->section(NULL, pass2, &sb);
866 globalbits = sb;
867 if (pass > 1) {
868 saa_rewind(forwrefs);
869 forwref = saa_rstruct(forwrefs);
870 raa_free(offsets);
871 offsets = raa_init();
873 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
874 globallineno = 0;
875 if (pass == 1)
876 location.known = true;
877 location.offset = offs = GET_CURR_OFFS;
879 while ((line = preproc->getline())) {
880 enum directives d;
881 globallineno++;
883 /* here we parse our directives; this is not handled by the 'real'
884 * parser. */
885 directive = line;
886 d = getkw(&directive, &value);
887 if (d) {
888 int err = 0;
890 switch (d) {
891 case D_SEGMENT: /* [SEGMENT n] */
892 case D_SECTION:
893 seg = ofmt->section(value, pass2, &sb);
894 if (seg == NO_SEG) {
895 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
896 "segment name `%s' not recognized",
897 value);
898 } else {
899 in_abs_seg = false;
900 location.segment = seg;
902 break;
903 case D_EXTERN: /* [EXTERN label:special] */
904 if (*value == '$')
905 value++; /* skip initial $ if present */
906 if (pass0 == 2) {
907 q = value;
908 while (*q && *q != ':')
909 q++;
910 if (*q == ':') {
911 *q++ = '\0';
912 ofmt->symdef(value, 0L, 0L, 3, q);
914 } else if (pass == 1) { /* pass == 1 */
915 q = value;
916 validid = true;
917 if (!isidstart(*q))
918 validid = false;
919 while (*q && *q != ':') {
920 if (!isidchar(*q))
921 validid = false;
922 q++;
924 if (!validid) {
925 report_error(ERR_NONFATAL,
926 "identifier expected after EXTERN");
927 break;
929 if (*q == ':') {
930 *q++ = '\0';
931 special = q;
932 } else
933 special = NULL;
934 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
935 int temp = pass0;
936 pass0 = 1; /* fake pass 1 in labels.c */
937 declare_as_global(value, special,
938 report_error);
939 define_label(value, seg_alloc(), 0L, NULL,
940 false, true, ofmt, report_error);
941 pass0 = temp;
943 } /* else pass0 == 1 */
944 break;
945 case D_BITS: /* [BITS bits] */
946 globalbits = sb = get_bits(value);
947 break;
948 case D_GLOBAL: /* [GLOBAL symbol:special] */
949 if (*value == '$')
950 value++; /* skip initial $ if present */
951 if (pass0 == 2) { /* pass 2 */
952 q = value;
953 while (*q && *q != ':')
954 q++;
955 if (*q == ':') {
956 *q++ = '\0';
957 ofmt->symdef(value, 0L, 0L, 3, q);
959 } else if (pass2 == 1) { /* pass == 1 */
960 q = value;
961 validid = true;
962 if (!isidstart(*q))
963 validid = false;
964 while (*q && *q != ':') {
965 if (!isidchar(*q))
966 validid = false;
967 q++;
969 if (!validid) {
970 report_error(ERR_NONFATAL,
971 "identifier expected after GLOBAL");
972 break;
974 if (*q == ':') {
975 *q++ = '\0';
976 special = q;
977 } else
978 special = NULL;
979 declare_as_global(value, special, report_error);
980 } /* pass == 1 */
981 break;
982 case D_COMMON: /* [COMMON symbol size:special] */
983 if (*value == '$')
984 value++; /* skip initial $ if present */
985 if (pass0 == 1) {
986 p = value;
987 validid = true;
988 if (!isidstart(*p))
989 validid = false;
990 while (*p && !isspace(*p)) {
991 if (!isidchar(*p))
992 validid = false;
993 p++;
995 if (!validid) {
996 report_error(ERR_NONFATAL,
997 "identifier expected after COMMON");
998 break;
1000 if (*p) {
1001 int64_t size;
1003 while (*p && isspace(*p))
1004 *p++ = '\0';
1005 q = p;
1006 while (*q && *q != ':')
1007 q++;
1008 if (*q == ':') {
1009 *q++ = '\0';
1010 special = q;
1011 } else
1012 special = NULL;
1013 size = readnum(p, &rn_error);
1014 if (rn_error)
1015 report_error(ERR_NONFATAL,
1016 "invalid size specified"
1017 " in COMMON declaration");
1018 else
1019 define_common(value, seg_alloc(), size,
1020 special, ofmt, report_error);
1021 } else
1022 report_error(ERR_NONFATAL,
1023 "no size specified in"
1024 " COMMON declaration");
1025 } else if (pass0 == 2) { /* pass == 2 */
1026 q = value;
1027 while (*q && *q != ':') {
1028 if (isspace(*q))
1029 *q = '\0';
1030 q++;
1032 if (*q == ':') {
1033 *q++ = '\0';
1034 ofmt->symdef(value, 0L, 0L, 3, q);
1037 break;
1038 case D_ABSOLUTE: /* [ABSOLUTE address] */
1039 stdscan_reset();
1040 stdscan_bufptr = value;
1041 tokval.t_type = TOKEN_INVALID;
1042 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1043 report_error, NULL);
1044 if (e) {
1045 if (!is_reloc(e))
1046 report_error(pass0 ==
1047 1 ? ERR_NONFATAL : ERR_PANIC,
1048 "cannot use non-relocatable expression as "
1049 "ABSOLUTE address");
1050 else {
1051 abs_seg = reloc_seg(e);
1052 abs_offset = reloc_value(e);
1054 } else if (pass == 1)
1055 abs_offset = 0x100; /* don't go near zero in case of / */
1056 else
1057 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1058 "in pass two");
1059 in_abs_seg = true;
1060 location.segment = NO_SEG;
1061 break;
1062 case D_DEBUG: /* [DEBUG] */
1063 p = value;
1064 q = debugid;
1065 validid = true;
1066 if (!isidstart(*p))
1067 validid = false;
1068 while (*p && !isspace(*p)) {
1069 if (!isidchar(*p))
1070 validid = false;
1071 *q++ = *p++;
1073 *q++ = 0;
1074 if (!validid) {
1075 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1076 "identifier expected after DEBUG");
1077 break;
1079 while (*p && isspace(*p))
1080 p++;
1081 if (pass == pass_max)
1082 ofmt->current_dfmt->debug_directive(debugid, p);
1083 break;
1084 case D_WARNING: /* [WARNING {+|-}warn-name] */
1085 if (pass1 == 1) {
1086 while (*value && isspace(*value))
1087 value++;
1089 if (*value == '+' || *value == '-') {
1090 validid = (*value == '-') ? true : false;
1091 value++;
1092 } else
1093 validid = false;
1095 for (i = 1; i <= ERR_WARN_MAX; i++)
1096 if (!nasm_stricmp(value, suppressed_names[i]))
1097 break;
1098 if (i <= ERR_WARN_MAX)
1099 suppressed[i] = validid;
1100 else
1101 report_error(ERR_NONFATAL,
1102 "invalid warning id in WARNING directive");
1104 break;
1105 case D_CPU: /* [CPU] */
1106 cpu = get_cpu(value);
1107 break;
1108 case D_LIST: /* [LIST {+|-}] */
1109 while (*value && isspace(*value))
1110 value++;
1112 if (*value == '+') {
1113 user_nolist = 0;
1114 } else {
1115 if (*value == '-') {
1116 user_nolist = 1;
1117 } else {
1118 err = 1;
1121 break;
1122 case D_DEFAULT: /* [DEFAULT] */
1123 stdscan_reset();
1124 stdscan_bufptr = value;
1125 tokval.t_type = TOKEN_INVALID;
1126 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1127 switch ((int)tokval.t_integer) {
1128 case S_REL:
1129 globalrel = 1;
1130 break;
1131 case S_ABS:
1132 globalrel = 0;
1133 break;
1134 default:
1135 err = 1;
1136 break;
1138 } else {
1139 err = 1;
1141 break;
1142 default:
1143 if (!ofmt->directive(directive, value, pass2))
1144 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1145 "unrecognised directive [%s]",
1146 directive);
1148 if (err) {
1149 report_error(ERR_NONFATAL,
1150 "invalid parameter to [%s] directive",
1151 directive);
1153 } else { /* it isn't a directive */
1155 parse_line(pass1, line, &output_ins,
1156 report_error, evaluate, def_label);
1158 if (!(optimizing > 0) && pass == 2) {
1159 if (forwref != NULL && globallineno == forwref->lineno) {
1160 output_ins.forw_ref = true;
1161 do {
1162 output_ins.oprs[forwref->operand].opflags |=
1163 OPFLAG_FORWARD;
1164 forwref = saa_rstruct(forwrefs);
1165 } while (forwref != NULL
1166 && forwref->lineno == globallineno);
1167 } else
1168 output_ins.forw_ref = false;
1171 if (!(optimizing > 0) && output_ins.forw_ref) {
1172 if (pass == 1) {
1173 for (i = 0; i < output_ins.operands; i++) {
1174 if (output_ins.oprs[i].
1175 opflags & OPFLAG_FORWARD) {
1176 struct forwrefinfo *fwinf =
1177 (struct forwrefinfo *)
1178 saa_wstruct(forwrefs);
1179 fwinf->lineno = globallineno;
1180 fwinf->operand = i;
1183 } else { /* pass == 2 */
1185 * Hack to prevent phase error in the code
1186 * rol ax,x
1187 * x equ 1
1189 * If the second operand is a forward reference,
1190 * the UNITY property of the number 1 in that
1191 * operand is cancelled. Otherwise the above
1192 * sequence will cause a phase error.
1194 * This hack means that the above code will
1195 * generate 286+ code.
1197 * The forward reference will mean that the
1198 * operand will not have the UNITY property on
1199 * the first pass, so the pass behaviours will
1200 * be consistent.
1203 if (output_ins.operands >= 2 &&
1204 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1205 !(IMMEDIATE & ~output_ins.oprs[1].type))
1207 /* Remove special properties bits */
1208 output_ins.oprs[1].type &= ~REG_SMASK;
1211 } /* pass == 2 */
1215 /* forw_ref */
1216 if (output_ins.opcode == I_EQU) {
1217 if (pass1 == 1) {
1219 * Special `..' EQUs get processed in pass two,
1220 * except `..@' macro-processor EQUs which are done
1221 * in the normal place.
1223 if (!output_ins.label)
1224 report_error(ERR_NONFATAL,
1225 "EQU not preceded by label");
1227 else if (output_ins.label[0] != '.' ||
1228 output_ins.label[1] != '.' ||
1229 output_ins.label[2] == '@') {
1230 if (output_ins.operands == 1 &&
1231 (output_ins.oprs[0].type & IMMEDIATE) &&
1232 output_ins.oprs[0].wrt == NO_SEG) {
1233 int isext =
1234 output_ins.oprs[0].
1235 opflags & OPFLAG_EXTERN;
1236 def_label(output_ins.label,
1237 output_ins.oprs[0].segment,
1238 output_ins.oprs[0].offset, NULL,
1239 false, isext, ofmt,
1240 report_error);
1241 } else if (output_ins.operands == 2
1242 && (output_ins.oprs[0].
1243 type & IMMEDIATE)
1244 && (output_ins.oprs[0].type & COLON)
1245 && output_ins.oprs[0].segment ==
1246 NO_SEG
1247 && output_ins.oprs[0].wrt == NO_SEG
1248 && (output_ins.oprs[1].
1249 type & IMMEDIATE)
1250 && output_ins.oprs[1].segment ==
1251 NO_SEG
1252 && output_ins.oprs[1].wrt ==
1253 NO_SEG) {
1254 def_label(output_ins.label,
1255 output_ins.oprs[0].
1256 offset | SEG_ABS,
1257 output_ins.oprs[1].offset, NULL,
1258 false, false, ofmt,
1259 report_error);
1260 } else
1261 report_error(ERR_NONFATAL,
1262 "bad syntax for EQU");
1264 } else { /* pass == 2 */
1266 * Special `..' EQUs get processed here, except
1267 * `..@' macro processor EQUs which are done above.
1269 if (output_ins.label[0] == '.' &&
1270 output_ins.label[1] == '.' &&
1271 output_ins.label[2] != '@') {
1272 if (output_ins.operands == 1 &&
1273 (output_ins.oprs[0].type & IMMEDIATE)) {
1274 define_label(output_ins.label,
1275 output_ins.oprs[0].segment,
1276 output_ins.oprs[0].offset,
1277 NULL, false, false, ofmt,
1278 report_error);
1279 } else if (output_ins.operands == 2
1280 && (output_ins.oprs[0].
1281 type & IMMEDIATE)
1282 && (output_ins.oprs[0].type & COLON)
1283 && output_ins.oprs[0].segment ==
1284 NO_SEG
1285 && (output_ins.oprs[1].
1286 type & IMMEDIATE)
1287 && output_ins.oprs[1].segment ==
1288 NO_SEG) {
1289 define_label(output_ins.label,
1290 output_ins.oprs[0].
1291 offset | SEG_ABS,
1292 output_ins.oprs[1].offset,
1293 NULL, false, false, ofmt,
1294 report_error);
1295 } else
1296 report_error(ERR_NONFATAL,
1297 "bad syntax for EQU");
1299 } /* pass == 2 */
1300 } else { /* instruction isn't an EQU */
1302 if (pass1 == 1) {
1304 int32_t l = insn_size(location.segment, offs, sb, cpu,
1305 &output_ins, report_error);
1307 /* if (using_debug_info) && output_ins.opcode != -1) */
1308 if (using_debug_info)
1309 { /* fbk 03/25/01 */
1310 /* this is done here so we can do debug type info */
1311 int32_t typeinfo =
1312 TYS_ELEMENTS(output_ins.operands);
1313 switch (output_ins.opcode) {
1314 case I_RESB:
1315 typeinfo =
1316 TYS_ELEMENTS(output_ins.oprs[0].
1317 offset) | TY_BYTE;
1318 break;
1319 case I_RESW:
1320 typeinfo =
1321 TYS_ELEMENTS(output_ins.oprs[0].
1322 offset) | TY_WORD;
1323 break;
1324 case I_RESD:
1325 typeinfo =
1326 TYS_ELEMENTS(output_ins.oprs[0].
1327 offset) | TY_DWORD;
1328 break;
1329 case I_RESQ:
1330 typeinfo =
1331 TYS_ELEMENTS(output_ins.oprs[0].
1332 offset) | TY_QWORD;
1333 break;
1334 case I_REST:
1335 typeinfo =
1336 TYS_ELEMENTS(output_ins.oprs[0].
1337 offset) | TY_TBYTE;
1338 break;
1339 case I_DB:
1340 typeinfo |= TY_BYTE;
1341 break;
1342 case I_DW:
1343 typeinfo |= TY_WORD;
1344 break;
1345 case I_DD:
1346 if (output_ins.eops_float)
1347 typeinfo |= TY_FLOAT;
1348 else
1349 typeinfo |= TY_DWORD;
1350 break;
1351 case I_DQ:
1352 typeinfo |= TY_QWORD;
1353 break;
1354 case I_DT:
1355 typeinfo |= TY_TBYTE;
1356 break;
1357 case I_DO:
1358 typeinfo |= TY_OWORD;
1359 break;
1360 default:
1361 typeinfo = TY_LABEL;
1365 ofmt->current_dfmt->debug_typevalue(typeinfo);
1368 if (l != -1) {
1369 offs += l;
1370 SET_CURR_OFFS(offs);
1373 * else l == -1 => invalid instruction, which will be
1374 * flagged as an error on pass 2
1377 } else { /* pass == 2 */
1378 offs += assemble(location.segment, offs, sb, cpu,
1379 &output_ins, ofmt, report_error,
1380 &nasmlist);
1381 SET_CURR_OFFS(offs);
1384 } /* not an EQU */
1385 cleanup_insn(&output_ins);
1387 nasm_free(line);
1388 location.offset = offs = GET_CURR_OFFS;
1389 } /* end while (line = preproc->getline... */
1391 if (pass1 == 2 && global_offset_changed)
1392 report_error(ERR_NONFATAL,
1393 "phase error detected at end of assembly.");
1395 if (pass1 == 1)
1396 preproc->cleanup(1);
1398 if (pass1 == 1 && terminate_after_phase) {
1399 fclose(ofile);
1400 remove(outname);
1401 if (want_usage)
1402 usage();
1403 exit(1);
1405 pass_cnt++;
1406 if (pass > 1 && !global_offset_changed) {
1407 pass0++;
1408 if (pass0 == 2)
1409 pass = pass_max - 1;
1410 } else if (!(optimizing > 0))
1411 pass0++;
1413 } /* for (pass=1; pass<=2; pass++) */
1415 preproc->cleanup(0);
1416 nasmlist.cleanup();
1417 #if 1
1418 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1419 fprintf(stdout,
1420 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1421 #endif
1422 } /* exit from assemble_file (...) */
1424 static enum directives getkw(char **directive, char **value)
1426 char *p, *q, *buf;
1428 buf = *directive;
1430 /* allow leading spaces or tabs */
1431 while (*buf == ' ' || *buf == '\t')
1432 buf++;
1434 if (*buf != '[')
1435 return 0;
1437 p = buf;
1439 while (*p && *p != ']')
1440 p++;
1442 if (!*p)
1443 return 0;
1445 q = p++;
1447 while (*p && *p != ';') {
1448 if (!isspace(*p))
1449 return 0;
1450 p++;
1452 q[1] = '\0';
1454 *directive = p = buf + 1;
1455 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1456 buf++;
1457 if (*buf == ']') {
1458 *buf = '\0';
1459 *value = buf;
1460 } else {
1461 *buf++ = '\0';
1462 while (isspace(*buf))
1463 buf++; /* beppu - skip leading whitespace */
1464 *value = buf;
1465 while (*buf != ']')
1466 buf++;
1467 *buf++ = '\0';
1470 return bsii(*directive, directives, elements(directives));
1474 * gnu style error reporting
1475 * This function prints an error message to error_file in the
1476 * style used by GNU. An example would be:
1477 * file.asm:50: error: blah blah blah
1478 * where file.asm is the name of the file, 50 is the line number on
1479 * which the error occurs (or is detected) and "error:" is one of
1480 * the possible optional diagnostics -- it can be "error" or "warning"
1481 * or something else. Finally the line terminates with the actual
1482 * error message.
1484 * @param severity the severity of the warning or error
1485 * @param fmt the printf style format string
1487 static void report_error_gnu(int severity, const char *fmt, ...)
1489 va_list ap;
1491 if (is_suppressed_warning(severity))
1492 return;
1494 if (severity & ERR_NOFILE)
1495 fputs("nasm: ", error_file);
1496 else {
1497 char *currentfile = NULL;
1498 int32_t lineno = 0;
1499 src_get(&lineno, &currentfile);
1500 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1501 nasm_free(currentfile);
1503 va_start(ap, fmt);
1504 report_error_common(severity, fmt, ap);
1505 va_end(ap);
1509 * MS style error reporting
1510 * This function prints an error message to error_file in the
1511 * style used by Visual C and some other Microsoft tools. An example
1512 * would be:
1513 * file.asm(50) : error: blah blah blah
1514 * where file.asm is the name of the file, 50 is the line number on
1515 * which the error occurs (or is detected) and "error:" is one of
1516 * the possible optional diagnostics -- it can be "error" or "warning"
1517 * or something else. Finally the line terminates with the actual
1518 * error message.
1520 * @param severity the severity of the warning or error
1521 * @param fmt the printf style format string
1523 static void report_error_vc(int severity, const char *fmt, ...)
1525 va_list ap;
1527 if (is_suppressed_warning(severity))
1528 return;
1530 if (severity & ERR_NOFILE)
1531 fputs("nasm: ", error_file);
1532 else {
1533 char *currentfile = NULL;
1534 int32_t lineno = 0;
1535 src_get(&lineno, &currentfile);
1536 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1537 nasm_free(currentfile);
1539 va_start(ap, fmt);
1540 report_error_common(severity, fmt, ap);
1541 va_end(ap);
1545 * check for supressed warning
1546 * checks for suppressed warning or pass one only warning and we're
1547 * not in pass 1
1549 * @param severity the severity of the warning or error
1550 * @return true if we should abort error/warning printing
1552 static int is_suppressed_warning(int severity)
1555 * See if it's a suppressed warning.
1557 return ((severity & ERR_MASK) == ERR_WARNING &&
1558 (severity & ERR_WARN_MASK) != 0 &&
1559 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1561 * See if it's a pass-one only warning and we're not in pass one.
1563 ((severity & ERR_PASS1) && pass0 == 2);
1567 * common error reporting
1568 * This is the common back end of the error reporting schemes currently
1569 * implemented. It prints the nature of the warning and then the
1570 * specific error message to error_file and may or may not return. It
1571 * doesn't return if the error severity is a "panic" or "debug" type.
1573 * @param severity the severity of the warning or error
1574 * @param fmt the printf style format string
1576 static void report_error_common(int severity, const char *fmt,
1577 va_list args)
1579 switch (severity & ERR_MASK) {
1580 case ERR_WARNING:
1581 fputs("warning: ", error_file);
1582 break;
1583 case ERR_NONFATAL:
1584 fputs("error: ", error_file);
1585 break;
1586 case ERR_FATAL:
1587 fputs("fatal: ", error_file);
1588 break;
1589 case ERR_PANIC:
1590 fputs("panic: ", error_file);
1591 break;
1592 case ERR_DEBUG:
1593 fputs("debug: ", error_file);
1594 break;
1597 vfprintf(error_file, fmt, args);
1598 fputc('\n', error_file);
1600 if (severity & ERR_USAGE)
1601 want_usage = true;
1603 switch (severity & ERR_MASK) {
1604 case ERR_WARNING:
1605 case ERR_DEBUG:
1606 /* no further action, by definition */
1607 break;
1608 case ERR_NONFATAL:
1609 /* hack enables listing(!) on errors */
1610 terminate_after_phase = true;
1611 break;
1612 case ERR_FATAL:
1613 if (ofile) {
1614 fclose(ofile);
1615 remove(outname);
1617 if (want_usage)
1618 usage();
1619 exit(1); /* instantly die */
1620 break; /* placate silly compilers */
1621 case ERR_PANIC:
1622 fflush(NULL);
1623 /* abort(); *//* halt, catch fire, and dump core */
1624 exit(3);
1625 break;
1629 static void usage(void)
1631 fputs("type `nasm -h' for help\n", error_file);
1634 static void register_output_formats(void)
1636 ofmt = ofmt_register(report_error);
1639 #define BUF_DELTA 512
1641 static FILE *no_pp_fp;
1642 static efunc no_pp_err;
1643 static ListGen *no_pp_list;
1644 static int32_t no_pp_lineinc;
1646 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1647 ListGen * listgen)
1649 src_set_fname(nasm_strdup(file));
1650 src_set_linnum(0);
1651 no_pp_lineinc = 1;
1652 no_pp_err = error;
1653 no_pp_fp = fopen(file, "r");
1654 if (!no_pp_fp)
1655 no_pp_err(ERR_FATAL | ERR_NOFILE,
1656 "unable to open input file `%s'", file);
1657 no_pp_list = listgen;
1658 (void)pass; /* placate compilers */
1659 (void)eval; /* placate compilers */
1662 static char *no_pp_getline(void)
1664 char *buffer, *p, *q;
1665 int bufsize;
1667 bufsize = BUF_DELTA;
1668 buffer = nasm_malloc(BUF_DELTA);
1669 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1671 while (1) { /* Loop to handle %line */
1673 p = buffer;
1674 while (1) { /* Loop to handle long lines */
1675 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1676 if (!q)
1677 break;
1678 p += strlen(p);
1679 if (p > buffer && p[-1] == '\n')
1680 break;
1681 if (p - buffer > bufsize - 10) {
1682 int offset;
1683 offset = p - buffer;
1684 bufsize += BUF_DELTA;
1685 buffer = nasm_realloc(buffer, bufsize);
1686 p = buffer + offset;
1690 if (!q && p == buffer) {
1691 nasm_free(buffer);
1692 return NULL;
1696 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1697 * them are present at the end of the line.
1699 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1701 if (!nasm_strnicmp(buffer, "%line", 5)) {
1702 int32_t ln;
1703 int li;
1704 char *nm = nasm_malloc(strlen(buffer));
1705 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1706 nasm_free(src_set_fname(nm));
1707 src_set_linnum(ln);
1708 no_pp_lineinc = li;
1709 continue;
1711 nasm_free(nm);
1713 break;
1716 no_pp_list->line(LIST_READ, buffer);
1718 return buffer;
1721 static void no_pp_cleanup(int pass)
1723 (void)pass; /* placate GCC */
1724 fclose(no_pp_fp);
1727 static uint32_t get_cpu(char *value)
1730 if (!strcmp(value, "8086"))
1731 return IF_8086;
1732 if (!strcmp(value, "186"))
1733 return IF_186;
1734 if (!strcmp(value, "286"))
1735 return IF_286;
1736 if (!strcmp(value, "386"))
1737 return IF_386;
1738 if (!strcmp(value, "486"))
1739 return IF_486;
1740 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1741 return IF_PENT;
1742 if (!strcmp(value, "686") ||
1743 !nasm_stricmp(value, "ppro") ||
1744 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1745 return IF_P6;
1746 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1747 return IF_KATMAI;
1748 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1749 !nasm_stricmp(value, "willamette"))
1750 return IF_WILLAMETTE;
1751 if (!nasm_stricmp(value, "prescott"))
1752 return IF_PRESCOTT;
1753 if (!nasm_stricmp(value, "x64") ||
1754 !nasm_stricmp(value, "x86-64"))
1755 return IF_X86_64;
1756 if (!nasm_stricmp(value, "ia64") ||
1757 !nasm_stricmp(value, "ia-64") ||
1758 !nasm_stricmp(value, "itanium") ||
1759 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1760 return IF_IA64;
1762 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1763 "unknown 'cpu' type");
1765 return IF_PLEVEL; /* the maximum level */
1768 static int get_bits(char *value)
1770 int i;
1772 if ((i = atoi(value)) == 16)
1773 return i; /* set for a 16-bit segment */
1774 else if (i == 32) {
1775 if (cpu < IF_386) {
1776 report_error(ERR_NONFATAL,
1777 "cannot specify 32-bit segment on processor below a 386");
1778 i = 16;
1780 } else if (i == 64) {
1781 if (cpu < IF_X86_64) {
1782 report_error(ERR_NONFATAL,
1783 "cannot specify 64-bit segment on processor below an x86-64");
1784 i = 16;
1786 if (i != maxbits) {
1787 report_error(ERR_NONFATAL,
1788 "%s output format does not support 64-bit code",
1789 ofmt->shortname);
1790 i = 16;
1792 } else {
1793 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1794 "`%s' is not a valid segment size; must be 16, 32 or 64",
1795 value);
1796 i = 16;
1798 return i;
1801 /* end of nasm.c */