Sanitize the pass logic, and only issue PASS1 warnings on pass0 == 1
[nasm.git] / nasm.c
blob9267a9f8703a465442a8da6ba9ff505e2d9c216e
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 license given in the file "LICENSE"
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 "float.h"
22 #include "stdscan.h"
23 #include "insns.h"
24 #include "preproc.h"
25 #include "parser.h"
26 #include "eval.h"
27 #include "assemble.h"
28 #include "labels.h"
29 #include "outform.h"
30 #include "listing.h"
32 struct forwrefinfo { /* info held on forward refs. */
33 int lineno;
34 int operand;
37 static int get_bits(char *value);
38 static uint32_t get_cpu(char *cpu_str);
39 static void parse_cmdline(int, char **);
40 static void assemble_file(char *);
41 static void register_output_formats(void);
42 static void report_error_gnu(int severity, const char *fmt, ...);
43 static void report_error_vc(int severity, const char *fmt, ...);
44 static void report_error_common(int severity, const char *fmt,
45 va_list args);
46 static int is_suppressed_warning(int severity);
47 static void usage(void);
48 static efunc report_error;
50 static int using_debug_info, opt_verbose_info;
51 bool tasm_compatible_mode = false;
52 int pass0, passn;
53 int maxbits = 0;
54 int globalrel = 0;
56 static char inname[FILENAME_MAX];
57 static char outname[FILENAME_MAX];
58 static char listname[FILENAME_MAX];
59 static char errname[FILENAME_MAX];
60 static int globallineno; /* for forward-reference tracking */
61 /* static int pass = 0; */
62 static struct ofmt *ofmt = NULL;
64 static FILE *error_file; /* Where to write error messages */
66 static FILE *ofile = NULL;
67 int optimizing = -1; /* number of optimization passes to take */
68 static int sb, cmd_sb = 16; /* by default */
69 static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
70 static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
71 bool global_offset_changed; /* referenced in labels.c */
73 static struct location location;
74 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
75 int32_t abs_seg; /* ABSOLUTE segment basis */
76 int32_t abs_offset; /* ABSOLUTE offset */
78 static struct RAA *offsets;
80 static struct SAA *forwrefs; /* keep track of forward references */
81 static const struct forwrefinfo *forwref;
83 static Preproc *preproc;
84 enum op_type {
85 op_normal, /* Preprocess and assemble */
86 op_preprocess, /* Preprocess only */
87 op_depend, /* Generate dependencies */
88 op_depend_missing_ok, /* Generate dependencies, missing OK */
90 static enum op_type operating_mode;
93 * Which of the suppressible warnings are suppressed. Entry zero
94 * isn't an actual warning, but it used for -w+error/-Werror.
96 static bool suppressed[ERR_WARN_MAX+1] = {
97 true, false, true, false, false, true, false, true, true, false
101 * The option names for the suppressible warnings. As before, entry
102 * zero does nothing.
104 static const char *suppressed_names[ERR_WARN_MAX+1] = {
105 "error", "macro-params", "macro-selfref", "orphan-labels",
106 "number-overflow", "gnu-elf-extensions", "float-overflow",
107 "float-denorm", "float-underflow", "float-toolong"
111 * The explanations for the suppressible warnings. As before, entry
112 * zero does nothing.
114 static const char *suppressed_what[ERR_WARN_MAX+1] = {
115 "treat warnings as errors",
116 "macro calls with wrong parameter count",
117 "cyclic macro references",
118 "labels alone on lines without trailing `:'",
119 "numeric constants does not fit in 64 bits",
120 "using 8- or 16-bit relocation in ELF32, a GNU extension",
121 "floating point overflow",
122 "floating point denormal",
123 "floating point underflow",
124 "too many digits in floating-point number"
128 * This is a null preprocessor which just copies lines from input
129 * to output. It's used when someone explicitly requests that NASM
130 * not preprocess their source file.
133 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
134 static char *no_pp_getline(void);
135 static void no_pp_cleanup(int);
136 static Preproc no_pp = {
137 no_pp_reset,
138 no_pp_getline,
139 no_pp_cleanup
143 * get/set current offset...
145 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
146 raa_read(offsets,location.segment))
147 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
148 (void)(offsets=raa_write(offsets,location.segment,(x))))
150 static int want_usage;
151 static int terminate_after_phase;
152 int user_nolist = 0; /* fbk 9/2/00 */
154 static void nasm_fputs(const char *line, FILE * outfile)
156 if (outfile) {
157 fputs(line, outfile);
158 putc('\n', outfile);
159 } else
160 puts(line);
163 int main(int argc, char **argv)
165 pass0 = 1;
166 want_usage = terminate_after_phase = false;
167 report_error = report_error_gnu;
169 error_file = stderr;
171 nasm_set_malloc_error(report_error);
172 offsets = raa_init();
173 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
175 preproc = &nasmpp;
176 operating_mode = op_normal;
178 seg_init();
180 register_output_formats();
182 parse_cmdline(argc, argv);
184 if (terminate_after_phase) {
185 if (want_usage)
186 usage();
187 return 1;
190 /* If debugging info is disabled, suppress any debug calls */
191 if (!using_debug_info)
192 ofmt->current_dfmt = &null_debug_form;
194 if (ofmt->stdmac)
195 pp_extra_stdmac(ofmt->stdmac);
196 parser_global_info(ofmt, &location);
197 eval_global_info(ofmt, lookup_label, &location);
199 /* define some macros dependent of command-line */
201 char temp[64];
202 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
203 ofmt->shortname);
204 pp_pre_define(temp);
207 switch (operating_mode) {
208 case op_depend_missing_ok:
209 pp_include_path(NULL); /* "assume generated" */
210 /* fall through */
211 case op_depend:
213 char *line;
214 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
215 if (outname[0] == '\0')
216 ofmt->filename(inname, outname, report_error);
217 ofile = NULL;
218 fprintf(stdout, "%s: %s", outname, inname);
219 while ((line = preproc->getline()))
220 nasm_free(line);
221 preproc->cleanup(0);
222 putc('\n', stdout);
224 break;
226 case op_preprocess:
228 char *line;
229 char *file_name = NULL;
230 int32_t prior_linnum = 0;
231 int lineinc = 0;
233 if (*outname) {
234 ofile = fopen(outname, "w");
235 if (!ofile)
236 report_error(ERR_FATAL | ERR_NOFILE,
237 "unable to open output file `%s'",
238 outname);
239 } else
240 ofile = NULL;
242 location.known = false;
244 /* pass = 1; */
245 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
246 while ((line = preproc->getline())) {
248 * We generate %line directives if needed for later programs
250 int32_t linnum = prior_linnum += lineinc;
251 int altline = src_get(&linnum, &file_name);
252 if (altline) {
253 if (altline == 1 && lineinc == 1)
254 nasm_fputs("", ofile);
255 else {
256 lineinc = (altline != -1 || lineinc != 1);
257 fprintf(ofile ? ofile : stdout,
258 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
259 file_name);
261 prior_linnum = linnum;
263 nasm_fputs(line, ofile);
264 nasm_free(line);
266 nasm_free(file_name);
267 preproc->cleanup(0);
268 if (ofile)
269 fclose(ofile);
270 if (ofile && terminate_after_phase)
271 remove(outname);
273 break;
275 case op_normal:
278 * We must call ofmt->filename _anyway_, even if the user
279 * has specified their own output file, because some
280 * formats (eg OBJ and COFF) use ofmt->filename to find out
281 * the name of the input file and then put that inside the
282 * file.
284 ofmt->filename(inname, outname, report_error);
286 ofile = fopen(outname, "wb");
287 if (!ofile) {
288 report_error(ERR_FATAL | ERR_NOFILE,
289 "unable to open output file `%s'", outname);
293 * We must call init_labels() before ofmt->init() since
294 * some object formats will want to define labels in their
295 * init routines. (eg OS/2 defines the FLAT group)
297 init_labels();
299 ofmt->init(ofile, report_error, define_label, evaluate);
301 assemble_file(inname);
303 if (!terminate_after_phase) {
304 ofmt->cleanup(using_debug_info);
305 cleanup_labels();
306 } else {
308 * We had an fclose on the output file here, but we
309 * actually do that in all the object file drivers as well,
310 * so we're leaving out the one here.
311 * fclose (ofile);
313 remove(outname);
314 if (listname[0])
315 remove(listname);
318 break;
321 if (want_usage)
322 usage();
324 raa_free(offsets);
325 saa_free(forwrefs);
326 eval_cleanup();
327 stdscan_cleanup();
329 if (terminate_after_phase)
330 return 1;
331 else
332 return 0;
336 * Get a parameter for a command line option.
337 * First arg must be in the form of e.g. -f...
339 static char *get_param(char *p, char *q, bool *advance)
341 *advance = false;
342 if (p[2]) { /* the parameter's in the option */
343 p += 2;
344 while (isspace(*p))
345 p++;
346 return p;
348 if (q && q[0]) {
349 *advance = true;
350 return q;
352 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
353 "option `-%c' requires an argument", p[1]);
354 return NULL;
358 * Copy a filename
360 static void copy_filename(char *dst, const char *src)
362 size_t len = strlen(src);
364 if (len >= (size_t)FILENAME_MAX) {
365 report_error(ERR_FATAL | ERR_NOFILE, "file name too long");
366 return;
368 strncpy(dst, src, FILENAME_MAX);
371 struct textargs {
372 const char *label;
373 int value;
376 #define OPT_PREFIX 0
377 #define OPT_POSTFIX 1
378 struct textargs textopts[] = {
379 {"prefix", OPT_PREFIX},
380 {"postfix", OPT_POSTFIX},
381 {NULL, 0}
384 static bool stopoptions = false;
385 static bool process_arg(char *p, char *q)
387 char *param;
388 int i;
389 bool advance = false;
390 bool suppress;
392 if (!p || !p[0])
393 return false;
395 if (p[0] == '-' && !stopoptions) {
396 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
397 /* These parameters take values */
398 if (!(param = get_param(p, q, &advance)))
399 return advance;
402 switch (p[1]) {
403 case 's':
404 error_file = stdout;
405 break;
407 case 'o': /* output file */
408 copy_filename(outname, param);
409 break;
411 case 'f': /* output format */
412 ofmt = ofmt_find(param);
413 if (!ofmt) {
414 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
415 "unrecognised output format `%s' - "
416 "use -hf for a list", param);
417 } else {
418 ofmt->current_dfmt = ofmt->debug_formats[0];
420 break;
422 case 'O': /* Optimization level */
424 int opt;
426 if (!*param) {
427 /* Naked -O == -Ox */
428 optimizing = INT_MAX >> 1; /* Almost unlimited */
429 } else {
430 while (*param) {
431 switch (*param) {
432 case '0': case '1': case '2': case '3': case '4':
433 case '5': case '6': case '7': case '8': case '9':
434 opt = strtoul(param, &param, 10);
436 /* -O0 -> optimizing == -1, 0.98 behaviour */
437 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
438 if (opt < 2)
439 optimizing = opt - 1;
440 else
441 optimizing = opt;
442 break;
444 case 'v':
445 case '+':
446 param++;
447 opt_verbose_info = true;
448 break;
450 case 'x':
451 param++;
452 optimizing = INT_MAX >> 1; /* Almost unlimited */
453 break;
455 default:
456 report_error(ERR_FATAL,
457 "unknown optimization option -O%c\n",
458 *param);
459 break;
463 break;
466 case 'p': /* pre-include */
467 case 'P':
468 pp_pre_include(param);
469 break;
471 case 'd': /* pre-define */
472 case 'D':
473 pp_pre_define(param);
474 break;
476 case 'u': /* un-define */
477 case 'U':
478 pp_pre_undefine(param);
479 break;
481 case 'i': /* include search path */
482 case 'I':
483 pp_include_path(param);
484 break;
486 case 'l': /* listing file */
487 copy_filename(listname, param);
488 break;
490 case 'Z': /* error messages file */
491 strcpy(errname, param);
492 break;
494 case 'F': /* specify debug format */
495 ofmt->current_dfmt = dfmt_find(ofmt, param);
496 if (!ofmt->current_dfmt) {
497 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
498 "unrecognized debug format `%s' for"
499 " output format `%s'",
500 param, ofmt->shortname);
502 break;
504 case 'X': /* specify error reporting format */
505 if (nasm_stricmp("vc", param) == 0)
506 report_error = report_error_vc;
507 else if (nasm_stricmp("gnu", param) == 0)
508 report_error = report_error_gnu;
509 else
510 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
511 "unrecognized error reporting format `%s'",
512 param);
513 break;
515 case 'g':
516 using_debug_info = true;
517 break;
519 case 'h':
520 printf
521 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
522 "[-l listfile]\n"
523 " [options...] [--] filename\n"
524 " or nasm -v for version info\n\n"
525 " -t assemble in SciTech TASM compatible mode\n"
526 " -g generate debug information in selected format.\n");
527 printf
528 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
529 " -a don't preprocess (assemble only)\n"
530 " -M generate Makefile dependencies on stdout\n"
531 " -MG d:o, missing files assumed generated\n\n"
532 " -Z<file> redirect error messages to file\n"
533 " -s redirect error messages to stdout\n\n"
534 " -F format select a debugging format\n\n"
535 " -I<path> adds a pathname to the include file path\n");
536 printf
537 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
538 " -P<file> pre-includes a file\n"
539 " -D<macro>[=<value>] pre-defines a macro\n"
540 " -U<macro> undefines a macro\n"
541 " -X<format> specifies error reporting format (gnu or vc)\n"
542 " -w+foo enables warning foo (equiv. -Wfoo)\n"
543 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
544 "Warnings:\n");
545 for (i = 0; i <= ERR_WARN_MAX; i++)
546 printf(" %-23s %s (default %s)\n",
547 suppressed_names[i], suppressed_what[i],
548 suppressed[i] ? "off" : "on");
549 printf
550 ("\nresponse files should contain command line parameters"
551 ", one per line.\n");
552 if (p[2] == 'f') {
553 printf("\nvalid output formats for -f are"
554 " (`*' denotes default):\n");
555 ofmt_list(ofmt, stdout);
556 } else {
557 printf("\nFor a list of valid output formats, use -hf.\n");
558 printf("For a list of debug formats, use -f <form> -y.\n");
560 exit(0); /* never need usage message here */
561 break;
563 case 'y':
564 printf("\nvalid debug formats for '%s' output format are"
565 " ('*' denotes default):\n", ofmt->shortname);
566 dfmt_list(ofmt, stdout);
567 exit(0);
568 break;
570 case 't':
571 tasm_compatible_mode = true;
572 break;
574 case 'v':
576 const char *nasm_version_string =
577 "NASM version " NASM_VER " compiled on " __DATE__
578 #ifdef DEBUG
579 " with -DDEBUG"
580 #endif
582 puts(nasm_version_string);
583 exit(0); /* never need usage message here */
585 break;
587 case 'e': /* preprocess only */
588 case 'E':
589 operating_mode = op_preprocess;
590 break;
592 case 'a': /* assemble only - don't preprocess */
593 preproc = &no_pp;
594 break;
596 case 'W':
597 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
598 suppress = true;
599 param += 3;
600 } else {
601 suppress = false;
603 goto set_warning;
605 case 'w':
606 if (param[0] != '+' && param[0] != '-') {
607 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
608 "invalid option to `-w'");
609 break;
611 suppress = (param[0] == '-');
612 param++;
613 goto set_warning;
614 set_warning:
615 for (i = 0; i <= ERR_WARN_MAX; i++)
616 if (!nasm_stricmp(param, suppressed_names[i]))
617 break;
618 if (i <= ERR_WARN_MAX)
619 suppressed[i] = suppress;
620 else if (!nasm_stricmp(param, "all"))
621 for (i = 1; i <= ERR_WARN_MAX; i++)
622 suppressed[i] = suppress;
623 else if (!nasm_stricmp(param, "none"))
624 for (i = 1; i <= ERR_WARN_MAX; i++)
625 suppressed[i] = !suppress;
626 else
627 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
628 "invalid warning `%s'", param);
629 break;
631 case 'M':
632 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
633 break;
635 case '-':
637 int s;
639 if (p[2] == 0) { /* -- => stop processing options */
640 stopoptions = 1;
641 break;
643 for (s = 0; textopts[s].label; s++) {
644 if (!nasm_stricmp(p + 2, textopts[s].label)) {
645 break;
649 switch (s) {
651 case OPT_PREFIX:
652 case OPT_POSTFIX:
654 if (!q) {
655 report_error(ERR_NONFATAL | ERR_NOFILE |
656 ERR_USAGE,
657 "option `--%s' requires an argument",
658 p + 2);
659 break;
660 } else {
661 advance = 1, param = q;
664 if (s == OPT_PREFIX) {
665 strncpy(lprefix, param, PREFIX_MAX - 1);
666 lprefix[PREFIX_MAX - 1] = 0;
667 break;
669 if (s == OPT_POSTFIX) {
670 strncpy(lpostfix, param, POSTFIX_MAX - 1);
671 lpostfix[POSTFIX_MAX - 1] = 0;
672 break;
674 break;
676 default:
678 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
679 "unrecognised option `--%s'", p + 2);
680 break;
683 break;
686 default:
687 if (!ofmt->setinfo(GI_SWITCH, &p))
688 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
689 "unrecognised option `-%c'", p[1]);
690 break;
692 } else {
693 if (*inname) {
694 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
695 "more than one input file specified");
696 } else {
697 copy_filename(inname, p);
701 return advance;
704 #define ARG_BUF_DELTA 128
706 static void process_respfile(FILE * rfile)
708 char *buffer, *p, *q, *prevarg;
709 int bufsize, prevargsize;
711 bufsize = prevargsize = ARG_BUF_DELTA;
712 buffer = nasm_malloc(ARG_BUF_DELTA);
713 prevarg = nasm_malloc(ARG_BUF_DELTA);
714 prevarg[0] = '\0';
716 while (1) { /* Loop to handle all lines in file */
717 p = buffer;
718 while (1) { /* Loop to handle long lines */
719 q = fgets(p, bufsize - (p - buffer), rfile);
720 if (!q)
721 break;
722 p += strlen(p);
723 if (p > buffer && p[-1] == '\n')
724 break;
725 if (p - buffer > bufsize - 10) {
726 int offset;
727 offset = p - buffer;
728 bufsize += ARG_BUF_DELTA;
729 buffer = nasm_realloc(buffer, bufsize);
730 p = buffer + offset;
734 if (!q && p == buffer) {
735 if (prevarg[0])
736 process_arg(prevarg, NULL);
737 nasm_free(buffer);
738 nasm_free(prevarg);
739 return;
743 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
744 * them are present at the end of the line.
746 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
748 while (p > buffer && isspace(p[-1]))
749 *--p = '\0';
751 p = buffer;
752 while (isspace(*p))
753 p++;
755 if (process_arg(prevarg, p))
756 *p = '\0';
758 if ((int) strlen(p) > prevargsize - 10) {
759 prevargsize += ARG_BUF_DELTA;
760 prevarg = nasm_realloc(prevarg, prevargsize);
762 strncpy(prevarg, p, prevargsize);
766 /* Function to process args from a string of args, rather than the
767 * argv array. Used by the environment variable and response file
768 * processing.
770 static void process_args(char *args)
772 char *p, *q, *arg, *prevarg;
773 char separator = ' ';
775 p = args;
776 if (*p && *p != '-')
777 separator = *p++;
778 arg = NULL;
779 while (*p) {
780 q = p;
781 while (*p && *p != separator)
782 p++;
783 while (*p == separator)
784 *p++ = '\0';
785 prevarg = arg;
786 arg = q;
787 if (process_arg(prevarg, arg))
788 arg = NULL;
790 if (arg)
791 process_arg(arg, NULL);
794 static void parse_cmdline(int argc, char **argv)
796 FILE *rfile;
797 char *envreal, *envcopy = NULL, *p, *arg;
799 *inname = *outname = *listname = *errname = '\0';
802 * First, process the NASMENV environment variable.
804 envreal = getenv("NASMENV");
805 arg = NULL;
806 if (envreal) {
807 envcopy = nasm_strdup(envreal);
808 process_args(envcopy);
809 nasm_free(envcopy);
813 * Now process the actual command line.
815 while (--argc) {
816 bool advance;
817 argv++;
818 if (argv[0][0] == '@') {
819 /* We have a response file, so process this as a set of
820 * arguments like the environment variable. This allows us
821 * to have multiple arguments on a single line, which is
822 * different to the -@resp file processing below for regular
823 * NASM.
825 char *str = malloc(2048);
826 FILE *f = fopen(&argv[0][1], "r");
827 if (!str) {
828 printf("out of memory");
829 exit(-1);
831 if (f) {
832 while (fgets(str, 2048, f)) {
833 process_args(str);
835 fclose(f);
837 free(str);
838 argc--;
839 argv++;
841 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
842 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
843 if (p) {
844 rfile = fopen(p, "r");
845 if (rfile) {
846 process_respfile(rfile);
847 fclose(rfile);
848 } else
849 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
850 "unable to open response file `%s'", p);
852 } else
853 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
854 argv += advance, argc -= advance;
857 /* Look for basic command line typos. This definitely doesn't
858 catch all errors, but it might help cases of fumbled fingers. */
859 if (!*inname)
860 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
861 "no input file specified");
862 else if (!strcmp(inname, errname) || !strcmp(inname, outname) ||
863 !strcmp(inname, listname))
864 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
865 "file `%s' is both input and output file",
866 inname);
868 if (*errname) {
869 error_file = fopen(errname, "w");
870 if (!error_file) {
871 error_file = stderr; /* Revert to default! */
872 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
873 "cannot open file `%s' for error messages",
874 errname);
879 /* List of directives */
880 enum directives {
881 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
882 D_EXTERN, D_FLOAT, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
884 static const char *directives[] = {
885 "", "absolute", "bits", "common", "cpu", "debug", "default",
886 "extern", "float", "global", "list", "section", "segment", "warning"
888 static enum directives getkw(char **directive, char **value);
890 static void assemble_file(char *fname)
892 char *directive, *value, *p, *q, *special, *line, debugid[80];
893 insn output_ins;
894 int i, validid;
895 bool rn_error;
896 int32_t seg;
897 int64_t offs;
898 struct tokenval tokval;
899 expr *e;
900 int pass_max;
902 if (cmd_sb == 32 && cmd_cpu < IF_386)
903 report_error(ERR_FATAL, "command line: "
904 "32-bit segment size requires a higher cpu");
906 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
907 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
908 for (passn = 1; pass0 <= 2; passn++) {
909 int pass1, pass2;
910 ldfunc def_label;
912 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
913 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
914 /* pass0 0, 0, 0, ..., 1, 2 */
916 printf("pass = %d (%d,%d,%d)\n", passn, pass0, pass1, pass2);
918 def_label = passn > 1 ? redefine_label : define_label;
920 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
921 cpu = cmd_cpu;
922 if (pass0 == 2) {
923 if (*listname)
924 nasmlist.init(listname, report_error);
926 in_abs_seg = false;
927 global_offset_changed = false; /* set by redefine_label */
928 location.segment = ofmt->section(NULL, pass2, &sb);
929 globalbits = sb;
930 if (passn > 1) {
931 saa_rewind(forwrefs);
932 forwref = saa_rstruct(forwrefs);
933 raa_free(offsets);
934 offsets = raa_init();
936 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
937 globallineno = 0;
938 if (passn == 1)
939 location.known = true;
940 location.offset = offs = GET_CURR_OFFS;
942 while ((line = preproc->getline())) {
943 enum directives d;
944 globallineno++;
946 /* here we parse our directives; this is not handled by the 'real'
947 * parser. */
948 directive = line;
949 d = getkw(&directive, &value);
950 if (d) {
951 int err = 0;
953 switch (d) {
954 case D_SEGMENT: /* [SEGMENT n] */
955 case D_SECTION:
956 seg = ofmt->section(value, pass2, &sb);
957 if (seg == NO_SEG) {
958 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
959 "segment name `%s' not recognized",
960 value);
961 } else {
962 in_abs_seg = false;
963 location.segment = seg;
965 break;
966 case D_EXTERN: /* [EXTERN label:special] */
967 if (*value == '$')
968 value++; /* skip initial $ if present */
969 if (pass0 == 2) {
970 q = value;
971 while (*q && *q != ':')
972 q++;
973 if (*q == ':') {
974 *q++ = '\0';
975 ofmt->symdef(value, 0L, 0L, 3, q);
977 } else if (passn == 1) {
978 q = value;
979 validid = true;
980 if (!isidstart(*q))
981 validid = false;
982 while (*q && *q != ':') {
983 if (!isidchar(*q))
984 validid = false;
985 q++;
987 if (!validid) {
988 report_error(ERR_NONFATAL,
989 "identifier expected after EXTERN");
990 break;
992 if (*q == ':') {
993 *q++ = '\0';
994 special = q;
995 } else
996 special = NULL;
997 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
998 int temp = pass0;
999 pass0 = 1; /* fake pass 1 in labels.c */
1000 declare_as_global(value, special,
1001 report_error);
1002 define_label(value, seg_alloc(), 0L, NULL,
1003 false, true, ofmt, report_error);
1004 pass0 = temp;
1006 } /* else pass0 == 1 */
1007 break;
1008 case D_BITS: /* [BITS bits] */
1009 globalbits = sb = get_bits(value);
1010 break;
1011 case D_GLOBAL: /* [GLOBAL symbol:special] */
1012 if (*value == '$')
1013 value++; /* skip initial $ if present */
1014 if (pass0 == 2) { /* pass 2 */
1015 q = value;
1016 while (*q && *q != ':')
1017 q++;
1018 if (*q == ':') {
1019 *q++ = '\0';
1020 ofmt->symdef(value, 0L, 0L, 3, q);
1022 } else if (pass2 == 1) { /* pass == 1 */
1023 q = value;
1024 validid = true;
1025 if (!isidstart(*q))
1026 validid = false;
1027 while (*q && *q != ':') {
1028 if (!isidchar(*q))
1029 validid = false;
1030 q++;
1032 if (!validid) {
1033 report_error(ERR_NONFATAL,
1034 "identifier expected after GLOBAL");
1035 break;
1037 if (*q == ':') {
1038 *q++ = '\0';
1039 special = q;
1040 } else
1041 special = NULL;
1042 declare_as_global(value, special, report_error);
1043 } /* pass == 1 */
1044 break;
1045 case D_COMMON: /* [COMMON symbol size:special] */
1046 if (*value == '$')
1047 value++; /* skip initial $ if present */
1048 if (pass0 == 1) {
1049 p = value;
1050 validid = true;
1051 if (!isidstart(*p))
1052 validid = false;
1053 while (*p && !isspace(*p)) {
1054 if (!isidchar(*p))
1055 validid = false;
1056 p++;
1058 if (!validid) {
1059 report_error(ERR_NONFATAL,
1060 "identifier expected after COMMON");
1061 break;
1063 if (*p) {
1064 int64_t size;
1066 while (*p && isspace(*p))
1067 *p++ = '\0';
1068 q = p;
1069 while (*q && *q != ':')
1070 q++;
1071 if (*q == ':') {
1072 *q++ = '\0';
1073 special = q;
1074 } else
1075 special = NULL;
1076 size = readnum(p, &rn_error);
1077 if (rn_error)
1078 report_error(ERR_NONFATAL,
1079 "invalid size specified"
1080 " in COMMON declaration");
1081 else
1082 define_common(value, seg_alloc(), size,
1083 special, ofmt, report_error);
1084 } else
1085 report_error(ERR_NONFATAL,
1086 "no size specified in"
1087 " COMMON declaration");
1088 } else if (pass0 == 2) { /* pass == 2 */
1089 q = value;
1090 while (*q && *q != ':') {
1091 if (isspace(*q))
1092 *q = '\0';
1093 q++;
1095 if (*q == ':') {
1096 *q++ = '\0';
1097 ofmt->symdef(value, 0L, 0L, 3, q);
1100 break;
1101 case D_ABSOLUTE: /* [ABSOLUTE address] */
1102 stdscan_reset();
1103 stdscan_bufptr = value;
1104 tokval.t_type = TOKEN_INVALID;
1105 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1106 report_error, NULL);
1107 if (e) {
1108 if (!is_reloc(e))
1109 report_error(pass0 ==
1110 1 ? ERR_NONFATAL : ERR_PANIC,
1111 "cannot use non-relocatable expression as "
1112 "ABSOLUTE address");
1113 else {
1114 abs_seg = reloc_seg(e);
1115 abs_offset = reloc_value(e);
1117 } else if (passn == 1)
1118 abs_offset = 0x100; /* don't go near zero in case of / */
1119 else
1120 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1121 "in pass two");
1122 in_abs_seg = true;
1123 location.segment = NO_SEG;
1124 break;
1125 case D_DEBUG: /* [DEBUG] */
1126 p = value;
1127 q = debugid;
1128 validid = true;
1129 if (!isidstart(*p))
1130 validid = false;
1131 while (*p && !isspace(*p)) {
1132 if (!isidchar(*p))
1133 validid = false;
1134 *q++ = *p++;
1136 *q++ = 0;
1137 if (!validid) {
1138 report_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1139 "identifier expected after DEBUG");
1140 break;
1142 while (*p && isspace(*p))
1143 p++;
1144 if (pass0 == 2)
1145 ofmt->current_dfmt->debug_directive(debugid, p);
1146 break;
1147 case D_WARNING: /* [WARNING {+|-}warn-name] */
1148 if (pass1 == 1) {
1149 while (*value && isspace(*value))
1150 value++;
1152 if (*value == '+' || *value == '-') {
1153 validid = (*value == '-') ? true : false;
1154 value++;
1155 } else
1156 validid = false;
1158 for (i = 1; i <= ERR_WARN_MAX; i++)
1159 if (!nasm_stricmp(value, suppressed_names[i]))
1160 break;
1161 if (i <= ERR_WARN_MAX)
1162 suppressed[i] = validid;
1163 else
1164 report_error(ERR_NONFATAL,
1165 "invalid warning id in WARNING directive");
1167 break;
1168 case D_CPU: /* [CPU] */
1169 cpu = get_cpu(value);
1170 break;
1171 case D_LIST: /* [LIST {+|-}] */
1172 while (*value && isspace(*value))
1173 value++;
1175 if (*value == '+') {
1176 user_nolist = 0;
1177 } else {
1178 if (*value == '-') {
1179 user_nolist = 1;
1180 } else {
1181 err = 1;
1184 break;
1185 case D_DEFAULT: /* [DEFAULT] */
1186 stdscan_reset();
1187 stdscan_bufptr = value;
1188 tokval.t_type = TOKEN_INVALID;
1189 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1190 switch ((int)tokval.t_integer) {
1191 case S_REL:
1192 globalrel = 1;
1193 break;
1194 case S_ABS:
1195 globalrel = 0;
1196 break;
1197 default:
1198 err = 1;
1199 break;
1201 } else {
1202 err = 1;
1204 break;
1205 case D_FLOAT:
1206 if (float_option(value)) {
1207 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1208 "unknown 'float' directive: %s",
1209 value);
1211 break;
1212 default:
1213 if (!ofmt->directive(directive, value, pass2))
1214 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1215 "unrecognised directive [%s]",
1216 directive);
1218 if (err) {
1219 report_error(ERR_NONFATAL,
1220 "invalid parameter to [%s] directive",
1221 directive);
1223 } else { /* it isn't a directive */
1225 parse_line(pass1, line, &output_ins,
1226 report_error, evaluate, def_label);
1228 if (!(optimizing > 0) && pass0 == 2) {
1229 if (forwref != NULL && globallineno == forwref->lineno) {
1230 output_ins.forw_ref = true;
1231 do {
1232 output_ins.oprs[forwref->operand].opflags |=
1233 OPFLAG_FORWARD;
1234 forwref = saa_rstruct(forwrefs);
1235 } while (forwref != NULL
1236 && forwref->lineno == globallineno);
1237 } else
1238 output_ins.forw_ref = false;
1241 if (!(optimizing > 0) && output_ins.forw_ref) {
1242 if (passn == 1) {
1243 for (i = 0; i < output_ins.operands; i++) {
1244 if (output_ins.oprs[i].
1245 opflags & OPFLAG_FORWARD) {
1246 struct forwrefinfo *fwinf =
1247 (struct forwrefinfo *)
1248 saa_wstruct(forwrefs);
1249 fwinf->lineno = globallineno;
1250 fwinf->operand = i;
1253 } else { /* passn > 1 */
1255 * Hack to prevent phase error in the code
1256 * rol ax,x
1257 * x equ 1
1259 * If the second operand is a forward reference,
1260 * the UNITY property of the number 1 in that
1261 * operand is cancelled. Otherwise the above
1262 * sequence will cause a phase error.
1264 * This hack means that the above code will
1265 * generate 286+ code.
1267 * The forward reference will mean that the
1268 * operand will not have the UNITY property on
1269 * the first pass, so the pass behaviours will
1270 * be consistent.
1273 if (output_ins.operands >= 2 &&
1274 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1275 !(IMMEDIATE & ~output_ins.oprs[1].type))
1277 /* Remove special properties bits */
1278 output_ins.oprs[1].type &= ~REG_SMASK;
1285 /* forw_ref */
1286 if (output_ins.opcode == I_EQU) {
1287 if (pass1 == 1) {
1289 * Special `..' EQUs get processed in pass two,
1290 * except `..@' macro-processor EQUs which are done
1291 * in the normal place.
1293 if (!output_ins.label)
1294 report_error(ERR_NONFATAL,
1295 "EQU not preceded by label");
1297 else if (output_ins.label[0] != '.' ||
1298 output_ins.label[1] != '.' ||
1299 output_ins.label[2] == '@') {
1300 if (output_ins.operands == 1 &&
1301 (output_ins.oprs[0].type & IMMEDIATE) &&
1302 output_ins.oprs[0].wrt == NO_SEG) {
1303 int isext =
1304 output_ins.oprs[0].
1305 opflags & OPFLAG_EXTERN;
1306 def_label(output_ins.label,
1307 output_ins.oprs[0].segment,
1308 output_ins.oprs[0].offset, NULL,
1309 false, isext, ofmt,
1310 report_error);
1311 } else if (output_ins.operands == 2
1312 && (output_ins.oprs[0].
1313 type & IMMEDIATE)
1314 && (output_ins.oprs[0].type & COLON)
1315 && output_ins.oprs[0].segment ==
1316 NO_SEG
1317 && output_ins.oprs[0].wrt == NO_SEG
1318 && (output_ins.oprs[1].
1319 type & IMMEDIATE)
1320 && output_ins.oprs[1].segment ==
1321 NO_SEG
1322 && output_ins.oprs[1].wrt ==
1323 NO_SEG) {
1324 def_label(output_ins.label,
1325 output_ins.oprs[0].
1326 offset | SEG_ABS,
1327 output_ins.oprs[1].offset, NULL,
1328 false, false, ofmt,
1329 report_error);
1330 } else
1331 report_error(ERR_NONFATAL,
1332 "bad syntax for EQU");
1334 } else {
1336 * Special `..' EQUs get processed here, except
1337 * `..@' macro processor EQUs which are done above.
1339 if (output_ins.label[0] == '.' &&
1340 output_ins.label[1] == '.' &&
1341 output_ins.label[2] != '@') {
1342 if (output_ins.operands == 1 &&
1343 (output_ins.oprs[0].type & IMMEDIATE)) {
1344 define_label(output_ins.label,
1345 output_ins.oprs[0].segment,
1346 output_ins.oprs[0].offset,
1347 NULL, false, false, ofmt,
1348 report_error);
1349 } else if (output_ins.operands == 2
1350 && (output_ins.oprs[0].
1351 type & IMMEDIATE)
1352 && (output_ins.oprs[0].type & COLON)
1353 && output_ins.oprs[0].segment ==
1354 NO_SEG
1355 && (output_ins.oprs[1].
1356 type & IMMEDIATE)
1357 && output_ins.oprs[1].segment ==
1358 NO_SEG) {
1359 define_label(output_ins.label,
1360 output_ins.oprs[0].
1361 offset | SEG_ABS,
1362 output_ins.oprs[1].offset,
1363 NULL, false, false, ofmt,
1364 report_error);
1365 } else
1366 report_error(ERR_NONFATAL,
1367 "bad syntax for EQU");
1370 } else { /* instruction isn't an EQU */
1372 if (pass1 == 1) {
1374 int64_t l = insn_size(location.segment, offs, sb, cpu,
1375 &output_ins, report_error);
1377 /* if (using_debug_info) && output_ins.opcode != -1) */
1378 if (using_debug_info)
1379 { /* fbk 03/25/01 */
1380 /* this is done here so we can do debug type info */
1381 int32_t typeinfo =
1382 TYS_ELEMENTS(output_ins.operands);
1383 switch (output_ins.opcode) {
1384 case I_RESB:
1385 typeinfo =
1386 TYS_ELEMENTS(output_ins.oprs[0].
1387 offset) | TY_BYTE;
1388 break;
1389 case I_RESW:
1390 typeinfo =
1391 TYS_ELEMENTS(output_ins.oprs[0].
1392 offset) | TY_WORD;
1393 break;
1394 case I_RESD:
1395 typeinfo =
1396 TYS_ELEMENTS(output_ins.oprs[0].
1397 offset) | TY_DWORD;
1398 break;
1399 case I_RESQ:
1400 typeinfo =
1401 TYS_ELEMENTS(output_ins.oprs[0].
1402 offset) | TY_QWORD;
1403 break;
1404 case I_REST:
1405 typeinfo =
1406 TYS_ELEMENTS(output_ins.oprs[0].
1407 offset) | TY_TBYTE;
1408 break;
1409 case I_DB:
1410 typeinfo |= TY_BYTE;
1411 break;
1412 case I_DW:
1413 typeinfo |= TY_WORD;
1414 break;
1415 case I_DD:
1416 if (output_ins.eops_float)
1417 typeinfo |= TY_FLOAT;
1418 else
1419 typeinfo |= TY_DWORD;
1420 break;
1421 case I_DQ:
1422 typeinfo |= TY_QWORD;
1423 break;
1424 case I_DT:
1425 typeinfo |= TY_TBYTE;
1426 break;
1427 case I_DO:
1428 typeinfo |= TY_OWORD;
1429 break;
1430 default:
1431 typeinfo = TY_LABEL;
1435 ofmt->current_dfmt->debug_typevalue(typeinfo);
1438 if (l != -1) {
1439 offs += l;
1440 SET_CURR_OFFS(offs);
1443 * else l == -1 => invalid instruction, which will be
1444 * flagged as an error on pass 2
1447 } else {
1448 offs += assemble(location.segment, offs, sb, cpu,
1449 &output_ins, ofmt, report_error,
1450 &nasmlist);
1451 SET_CURR_OFFS(offs);
1454 } /* not an EQU */
1455 cleanup_insn(&output_ins);
1457 nasm_free(line);
1458 location.offset = offs = GET_CURR_OFFS;
1459 } /* end while (line = preproc->getline... */
1461 if (pass1 == 2 && global_offset_changed)
1462 report_error(ERR_NONFATAL,
1463 "phase error detected at end of assembly.");
1465 if (pass1 == 1)
1466 preproc->cleanup(1);
1468 if (pass1 == 1 && terminate_after_phase) {
1469 fclose(ofile);
1470 remove(outname);
1471 if (want_usage)
1472 usage();
1473 exit(1);
1475 if (passn >= pass_max - 2 ||
1476 (passn > 1 && !global_offset_changed))
1477 pass0++;
1480 preproc->cleanup(0);
1481 nasmlist.cleanup();
1482 #if 1
1483 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1484 fprintf(stdout,
1485 "info:: assembly required 1+%d+1 passes\n", passn-3);
1486 #endif
1487 } /* exit from assemble_file (...) */
1489 static enum directives getkw(char **directive, char **value)
1491 char *p, *q, *buf;
1493 buf = *directive;
1495 /* allow leading spaces or tabs */
1496 while (*buf == ' ' || *buf == '\t')
1497 buf++;
1499 if (*buf != '[')
1500 return 0;
1502 p = buf;
1504 while (*p && *p != ']')
1505 p++;
1507 if (!*p)
1508 return 0;
1510 q = p++;
1512 while (*p && *p != ';') {
1513 if (!isspace(*p))
1514 return 0;
1515 p++;
1517 q[1] = '\0';
1519 *directive = p = buf + 1;
1520 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1521 buf++;
1522 if (*buf == ']') {
1523 *buf = '\0';
1524 *value = buf;
1525 } else {
1526 *buf++ = '\0';
1527 while (isspace(*buf))
1528 buf++; /* beppu - skip leading whitespace */
1529 *value = buf;
1530 while (*buf != ']')
1531 buf++;
1532 *buf++ = '\0';
1535 return bsii(*directive, directives, elements(directives));
1539 * gnu style error reporting
1540 * This function prints an error message to error_file in the
1541 * style used by GNU. An example would be:
1542 * file.asm:50: error: blah blah blah
1543 * where file.asm is the name of the file, 50 is the line number on
1544 * which the error occurs (or is detected) and "error:" is one of
1545 * the possible optional diagnostics -- it can be "error" or "warning"
1546 * or something else. Finally the line terminates with the actual
1547 * error message.
1549 * @param severity the severity of the warning or error
1550 * @param fmt the printf style format string
1552 static void report_error_gnu(int severity, const char *fmt, ...)
1554 va_list ap;
1556 if (is_suppressed_warning(severity))
1557 return;
1559 if (severity & ERR_NOFILE)
1560 fputs("nasm: ", error_file);
1561 else {
1562 char *currentfile = NULL;
1563 int32_t lineno = 0;
1564 src_get(&lineno, &currentfile);
1565 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1566 nasm_free(currentfile);
1568 va_start(ap, fmt);
1569 report_error_common(severity, fmt, ap);
1570 va_end(ap);
1574 * MS style error reporting
1575 * This function prints an error message to error_file in the
1576 * style used by Visual C and some other Microsoft tools. An example
1577 * would be:
1578 * file.asm(50) : error: blah blah blah
1579 * where file.asm is the name of the file, 50 is the line number on
1580 * which the error occurs (or is detected) and "error:" is one of
1581 * the possible optional diagnostics -- it can be "error" or "warning"
1582 * or something else. Finally the line terminates with the actual
1583 * error message.
1585 * @param severity the severity of the warning or error
1586 * @param fmt the printf style format string
1588 static void report_error_vc(int severity, const char *fmt, ...)
1590 va_list ap;
1592 if (is_suppressed_warning(severity))
1593 return;
1595 if (severity & ERR_NOFILE)
1596 fputs("nasm: ", error_file);
1597 else {
1598 char *currentfile = NULL;
1599 int32_t lineno = 0;
1600 src_get(&lineno, &currentfile);
1601 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1602 nasm_free(currentfile);
1604 va_start(ap, fmt);
1605 report_error_common(severity, fmt, ap);
1606 va_end(ap);
1610 * check for supressed warning
1611 * checks for suppressed warning or pass one only warning and we're
1612 * not in pass 1
1614 * @param severity the severity of the warning or error
1615 * @return true if we should abort error/warning printing
1617 static int is_suppressed_warning(int severity)
1620 * See if it's a suppressed warning.
1622 return ((severity & ERR_MASK) == ERR_WARNING &&
1623 (severity & ERR_WARN_MASK) != 0 &&
1624 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1626 * See if it's a pass-one only warning and we're not in pass
1627 * zero or one.
1629 ((severity & ERR_PASS1) && pass0 != 1);
1633 * common error reporting
1634 * This is the common back end of the error reporting schemes currently
1635 * implemented. It prints the nature of the warning and then the
1636 * specific error message to error_file and may or may not return. It
1637 * doesn't return if the error severity is a "panic" or "debug" type.
1639 * @param severity the severity of the warning or error
1640 * @param fmt the printf style format string
1642 static void report_error_common(int severity, const char *fmt,
1643 va_list args)
1645 switch (severity & ERR_MASK) {
1646 case ERR_WARNING:
1647 fputs("warning: ", error_file);
1648 break;
1649 case ERR_NONFATAL:
1650 fputs("error: ", error_file);
1651 break;
1652 case ERR_FATAL:
1653 fputs("fatal: ", error_file);
1654 break;
1655 case ERR_PANIC:
1656 fputs("panic: ", error_file);
1657 break;
1658 case ERR_DEBUG:
1659 fputs("debug: ", error_file);
1660 break;
1663 vfprintf(error_file, fmt, args);
1664 putc('\n', error_file);
1666 if (severity & ERR_USAGE)
1667 want_usage = true;
1669 switch (severity & ERR_MASK) {
1670 case ERR_DEBUG:
1671 /* no further action, by definition */
1672 break;
1673 case ERR_WARNING:
1674 if (!suppressed[0]) /* Treat warnings as errors */
1675 terminate_after_phase = true;
1676 break;
1677 case ERR_NONFATAL:
1678 terminate_after_phase = true;
1679 break;
1680 case ERR_FATAL:
1681 if (ofile) {
1682 fclose(ofile);
1683 remove(outname);
1685 if (want_usage)
1686 usage();
1687 exit(1); /* instantly die */
1688 break; /* placate silly compilers */
1689 case ERR_PANIC:
1690 fflush(NULL);
1691 /* abort(); *//* halt, catch fire, and dump core */
1692 exit(3);
1693 break;
1697 static void usage(void)
1699 fputs("type `nasm -h' for help\n", error_file);
1702 static void register_output_formats(void)
1704 ofmt = ofmt_register(report_error);
1707 #define BUF_DELTA 512
1709 static FILE *no_pp_fp;
1710 static efunc no_pp_err;
1711 static ListGen *no_pp_list;
1712 static int32_t no_pp_lineinc;
1714 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1715 ListGen * listgen)
1717 src_set_fname(nasm_strdup(file));
1718 src_set_linnum(0);
1719 no_pp_lineinc = 1;
1720 no_pp_err = error;
1721 no_pp_fp = fopen(file, "r");
1722 if (!no_pp_fp)
1723 no_pp_err(ERR_FATAL | ERR_NOFILE,
1724 "unable to open input file `%s'", file);
1725 no_pp_list = listgen;
1726 (void)pass; /* placate compilers */
1727 (void)eval; /* placate compilers */
1730 static char *no_pp_getline(void)
1732 char *buffer, *p, *q;
1733 int bufsize;
1735 bufsize = BUF_DELTA;
1736 buffer = nasm_malloc(BUF_DELTA);
1737 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1739 while (1) { /* Loop to handle %line */
1741 p = buffer;
1742 while (1) { /* Loop to handle long lines */
1743 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1744 if (!q)
1745 break;
1746 p += strlen(p);
1747 if (p > buffer && p[-1] == '\n')
1748 break;
1749 if (p - buffer > bufsize - 10) {
1750 int offset;
1751 offset = p - buffer;
1752 bufsize += BUF_DELTA;
1753 buffer = nasm_realloc(buffer, bufsize);
1754 p = buffer + offset;
1758 if (!q && p == buffer) {
1759 nasm_free(buffer);
1760 return NULL;
1764 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1765 * them are present at the end of the line.
1767 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1769 if (!nasm_strnicmp(buffer, "%line", 5)) {
1770 int32_t ln;
1771 int li;
1772 char *nm = nasm_malloc(strlen(buffer));
1773 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1774 nasm_free(src_set_fname(nm));
1775 src_set_linnum(ln);
1776 no_pp_lineinc = li;
1777 continue;
1779 nasm_free(nm);
1781 break;
1784 no_pp_list->line(LIST_READ, buffer);
1786 return buffer;
1789 static void no_pp_cleanup(int pass)
1791 (void)pass; /* placate GCC */
1792 fclose(no_pp_fp);
1795 static uint32_t get_cpu(char *value)
1797 if (!strcmp(value, "8086"))
1798 return IF_8086;
1799 if (!strcmp(value, "186"))
1800 return IF_186;
1801 if (!strcmp(value, "286"))
1802 return IF_286;
1803 if (!strcmp(value, "386"))
1804 return IF_386;
1805 if (!strcmp(value, "486"))
1806 return IF_486;
1807 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1808 return IF_PENT;
1809 if (!strcmp(value, "686") ||
1810 !nasm_stricmp(value, "ppro") ||
1811 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1812 return IF_P6;
1813 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1814 return IF_KATMAI;
1815 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1816 !nasm_stricmp(value, "willamette"))
1817 return IF_WILLAMETTE;
1818 if (!nasm_stricmp(value, "prescott"))
1819 return IF_PRESCOTT;
1820 if (!nasm_stricmp(value, "x64") ||
1821 !nasm_stricmp(value, "x86-64"))
1822 return IF_X86_64;
1823 if (!nasm_stricmp(value, "ia64") ||
1824 !nasm_stricmp(value, "ia-64") ||
1825 !nasm_stricmp(value, "itanium") ||
1826 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1827 return IF_IA64;
1829 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1830 "unknown 'cpu' type");
1832 return IF_PLEVEL; /* the maximum level */
1835 static int get_bits(char *value)
1837 int i;
1839 if ((i = atoi(value)) == 16)
1840 return i; /* set for a 16-bit segment */
1841 else if (i == 32) {
1842 if (cpu < IF_386) {
1843 report_error(ERR_NONFATAL,
1844 "cannot specify 32-bit segment on processor below a 386");
1845 i = 16;
1847 } else if (i == 64) {
1848 if (cpu < IF_X86_64) {
1849 report_error(ERR_NONFATAL,
1850 "cannot specify 64-bit segment on processor below an x86-64");
1851 i = 16;
1853 if (i != maxbits) {
1854 report_error(ERR_NONFATAL,
1855 "%s output format does not support 64-bit code",
1856 ofmt->shortname);
1857 i = 16;
1859 } else {
1860 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1861 "`%s' is not a valid segment size; must be 16, 32 or 64",
1862 value);
1863 i = 16;
1865 return i;
1868 /* end of nasm.c */