Enable a few warnings by default; clean up warning descriptions
[nasm.git] / nasm.c
blob560678eaf16b0354af8519c7407aec538d591538
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 "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;
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, int *advance)
341 *advance = 0;
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 = 1;
350 return q;
352 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
353 "option `-%c' requires an argument", p[1]);
354 return NULL;
357 struct textargs {
358 const char *label;
359 int value;
362 #define OPT_PREFIX 0
363 #define OPT_POSTFIX 1
364 struct textargs textopts[] = {
365 {"prefix", OPT_PREFIX},
366 {"postfix", OPT_POSTFIX},
367 {NULL, 0}
370 int stopoptions = 0;
371 static int process_arg(char *p, char *q)
373 char *param;
374 int i, advance = 0;
375 bool suppress;
377 if (!p || !p[0])
378 return 0;
380 if (p[0] == '-' && !stopoptions) {
381 switch (p[1]) {
382 case 's':
383 error_file = stdout;
384 break;
385 case 'o': /* these parameters take values */
386 case 'O':
387 case 'f':
388 case 'p':
389 case 'P':
390 case 'd':
391 case 'D':
392 case 'i':
393 case 'I':
394 case 'l':
395 case 'F':
396 case 'X':
397 case 'u':
398 case 'U':
399 case 'Z':
400 if (!(param = get_param(p, q, &advance)))
401 break;
402 if (p[1] == 'o') { /* output file */
403 strcpy(outname, param);
404 } else if (p[1] == 'f') { /* output format */
405 ofmt = ofmt_find(param);
406 if (!ofmt) {
407 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
408 "unrecognised output format `%s' - "
409 "use -hf for a list", param);
410 } else
411 ofmt->current_dfmt = ofmt->debug_formats[0];
412 } else if (p[1] == 'O') { /* Optimization level */
413 int opt;
415 if (!*param) {
416 /* Naked -O == -Ox */
417 optimizing = INT_MAX >> 1; /* Almost unlimited */
418 } else {
419 while (*param) {
420 switch (*param) {
421 case '0': case '1': case '2': case '3': case '4':
422 case '5': case '6': case '7': case '8': case '9':
423 opt = strtoul(param, &param, 10);
425 /* -O0 -> optimizing == -1, 0.98 behaviour */
426 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
427 if (opt < 2)
428 optimizing = opt - 1;
429 else if (opt <= 5)
430 /* The optimizer seems to have problems with
431 < 5 passes? Hidden bug? */
432 optimizing = 5; /* 5 passes */
433 else
434 optimizing = opt; /* More than 5 passes */
435 break;
437 case 'v':
438 case '+':
439 param++;
440 opt_verbose_info = true;
441 break;
443 case 'x':
444 param++;
445 optimizing = INT_MAX >> 1; /* Almost unlimited */
446 break;
448 default:
449 report_error(ERR_FATAL,
450 "unknown optimization option -O%c\n",
451 *param);
452 break;
456 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
457 pp_pre_include(param);
458 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
459 pp_pre_define(param);
460 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
461 pp_pre_undefine(param);
462 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
463 pp_include_path(param);
464 } else if (p[1] == 'l') { /* listing file */
465 strcpy(listname, param);
466 } else if (p[1] == 'Z') { /* error messages file */
467 strcpy(errname, param);
468 } else if (p[1] == 'F') { /* specify debug format */
469 ofmt->current_dfmt = dfmt_find(ofmt, param);
470 if (!ofmt->current_dfmt) {
471 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
472 "unrecognized debug format `%s' for"
473 " output format `%s'",
474 param, ofmt->shortname);
476 } else if (p[1] == 'X') { /* specify error reporting format */
477 if (nasm_stricmp("vc", param) == 0)
478 report_error = report_error_vc;
479 else if (nasm_stricmp("gnu", param) == 0)
480 report_error = report_error_gnu;
481 else
482 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
483 "unrecognized error reporting format `%s'",
484 param);
486 break;
487 case 'g':
488 using_debug_info = true;
489 break;
490 case 'h':
491 printf
492 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
493 "[-l listfile]\n"
494 " [options...] [--] filename\n"
495 " or nasm -v for version info\n\n"
496 " -t assemble in SciTech TASM compatible mode\n"
497 " -g generate debug information in selected format.\n");
498 printf
499 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
500 " -a don't preprocess (assemble only)\n"
501 " -M generate Makefile dependencies on stdout\n"
502 " -MG d:o, missing files assumed generated\n\n"
503 " -Z<file> redirect error messages to file\n"
504 " -s redirect error messages to stdout\n\n"
505 " -F format select a debugging format\n\n"
506 " -I<path> adds a pathname to the include file path\n");
507 printf
508 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
509 " -P<file> pre-includes a file\n"
510 " -D<macro>[=<value>] pre-defines a macro\n"
511 " -U<macro> undefines a macro\n"
512 " -X<format> specifies error reporting format (gnu or vc)\n"
513 " -w+foo enables warning foo (equiv. -Wfoo)\n"
514 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
515 "Warnings:\n");
516 for (i = 0; i <= ERR_WARN_MAX; i++)
517 printf(" %-23s %s (default %s)\n",
518 suppressed_names[i], suppressed_what[i],
519 suppressed[i] ? "off" : "on");
520 printf
521 ("\nresponse files should contain command line parameters"
522 ", one per line.\n");
523 if (p[2] == 'f') {
524 printf("\nvalid output formats for -f are"
525 " (`*' denotes default):\n");
526 ofmt_list(ofmt, stdout);
527 } else {
528 printf("\nFor a list of valid output formats, use -hf.\n");
529 printf("For a list of debug formats, use -f <form> -y.\n");
531 exit(0); /* never need usage message here */
532 break;
533 case 'y':
534 printf("\nvalid debug formats for '%s' output format are"
535 " ('*' denotes default):\n", ofmt->shortname);
536 dfmt_list(ofmt, stdout);
537 exit(0);
538 break;
539 case 't':
540 tasm_compatible_mode = true;
541 break;
542 case 'v':
544 const char *nasm_version_string =
545 "NASM version " NASM_VER " compiled on " __DATE__
546 #ifdef DEBUG
547 " with -DDEBUG"
548 #endif
550 puts(nasm_version_string);
551 exit(0); /* never need usage message here */
553 break;
554 case 'e': /* preprocess only */
555 case 'E':
556 operating_mode = op_preprocess;
557 break;
558 case 'a': /* assemble only - don't preprocess */
559 preproc = &no_pp;
560 break;
561 case 'W':
562 if (p[2] == 'n' && p[3] == 'o' && p[4] == '-') {
563 suppress = true;
564 p += 5;
565 } else {
566 suppress = false;
567 p += 2;
569 goto set_warning;
570 case 'w':
571 if (p[2] != '+' && p[2] != '-') {
572 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
573 "invalid option to `-w'");
574 break;
576 suppress = (p[2] == '-');
577 p += 3;
578 goto set_warning;
579 set_warning:
580 for (i = 0; i <= ERR_WARN_MAX; i++)
581 if (!nasm_stricmp(p, suppressed_names[i]))
582 break;
583 if (i <= ERR_WARN_MAX)
584 suppressed[i] = suppress;
585 else if (!nasm_stricmp(p, "all"))
586 for (i = 1; i <= ERR_WARN_MAX; i++)
587 suppressed[i] = suppress;
588 else
589 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
590 "invalid warning `%s'", p);
591 break;
592 case 'M':
593 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
594 break;
596 case '-':
598 int s;
600 if (p[2] == 0) { /* -- => stop processing options */
601 stopoptions = 1;
602 break;
604 for (s = 0; textopts[s].label; s++) {
605 if (!nasm_stricmp(p + 2, textopts[s].label)) {
606 break;
610 switch (s) {
612 case OPT_PREFIX:
613 case OPT_POSTFIX:
615 if (!q) {
616 report_error(ERR_NONFATAL | ERR_NOFILE |
617 ERR_USAGE,
618 "option `--%s' requires an argument",
619 p + 2);
620 break;
621 } else {
622 advance = 1, param = q;
625 if (s == OPT_PREFIX) {
626 strncpy(lprefix, param, PREFIX_MAX - 1);
627 lprefix[PREFIX_MAX - 1] = 0;
628 break;
630 if (s == OPT_POSTFIX) {
631 strncpy(lpostfix, param, POSTFIX_MAX - 1);
632 lpostfix[POSTFIX_MAX - 1] = 0;
633 break;
635 break;
637 default:
639 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
640 "unrecognised option `--%s'", p + 2);
641 break;
644 break;
647 default:
648 if (!ofmt->setinfo(GI_SWITCH, &p))
649 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
650 "unrecognised option `-%c'", p[1]);
651 break;
653 } else {
654 if (*inname) {
655 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
656 "more than one input file specified");
657 } else
658 strcpy(inname, p);
661 return advance;
664 #define ARG_BUF_DELTA 128
666 static void process_respfile(FILE * rfile)
668 char *buffer, *p, *q, *prevarg;
669 int bufsize, prevargsize;
671 bufsize = prevargsize = ARG_BUF_DELTA;
672 buffer = nasm_malloc(ARG_BUF_DELTA);
673 prevarg = nasm_malloc(ARG_BUF_DELTA);
674 prevarg[0] = '\0';
676 while (1) { /* Loop to handle all lines in file */
678 p = buffer;
679 while (1) { /* Loop to handle long lines */
680 q = fgets(p, bufsize - (p - buffer), rfile);
681 if (!q)
682 break;
683 p += strlen(p);
684 if (p > buffer && p[-1] == '\n')
685 break;
686 if (p - buffer > bufsize - 10) {
687 int offset;
688 offset = p - buffer;
689 bufsize += ARG_BUF_DELTA;
690 buffer = nasm_realloc(buffer, bufsize);
691 p = buffer + offset;
695 if (!q && p == buffer) {
696 if (prevarg[0])
697 process_arg(prevarg, NULL);
698 nasm_free(buffer);
699 nasm_free(prevarg);
700 return;
704 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
705 * them are present at the end of the line.
707 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
709 while (p > buffer && isspace(p[-1]))
710 *--p = '\0';
712 p = buffer;
713 while (isspace(*p))
714 p++;
716 if (process_arg(prevarg, p))
717 *p = '\0';
719 if ((int) strlen(p) > prevargsize - 10) {
720 prevargsize += ARG_BUF_DELTA;
721 prevarg = nasm_realloc(prevarg, prevargsize);
723 strcpy(prevarg, p);
727 /* Function to process args from a string of args, rather than the
728 * argv array. Used by the environment variable and response file
729 * processing.
731 static void process_args(char *args)
733 char *p, *q, *arg, *prevarg;
734 char separator = ' ';
736 p = args;
737 if (*p && *p != '-')
738 separator = *p++;
739 arg = NULL;
740 while (*p) {
741 q = p;
742 while (*p && *p != separator)
743 p++;
744 while (*p == separator)
745 *p++ = '\0';
746 prevarg = arg;
747 arg = q;
748 if (process_arg(prevarg, arg))
749 arg = NULL;
751 if (arg)
752 process_arg(arg, NULL);
755 static void parse_cmdline(int argc, char **argv)
757 FILE *rfile;
758 char *envreal, *envcopy = NULL, *p, *arg;
760 *inname = *outname = *listname = *errname = '\0';
763 * First, process the NASMENV environment variable.
765 envreal = getenv("NASMENV");
766 arg = NULL;
767 if (envreal) {
768 envcopy = nasm_strdup(envreal);
769 process_args(envcopy);
770 nasm_free(envcopy);
774 * Now process the actual command line.
776 while (--argc) {
777 int i;
778 argv++;
779 if (argv[0][0] == '@') {
780 /* We have a response file, so process this as a set of
781 * arguments like the environment variable. This allows us
782 * to have multiple arguments on a single line, which is
783 * different to the -@resp file processing below for regular
784 * NASM.
786 char *str = malloc(2048);
787 FILE *f = fopen(&argv[0][1], "r");
788 if (!str) {
789 printf("out of memory");
790 exit(-1);
792 if (f) {
793 while (fgets(str, 2048, f)) {
794 process_args(str);
796 fclose(f);
798 free(str);
799 argc--;
800 argv++;
802 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
803 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
804 if (p) {
805 rfile = fopen(p, "r");
806 if (rfile) {
807 process_respfile(rfile);
808 fclose(rfile);
809 } else
810 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
811 "unable to open response file `%s'", p);
813 } else
814 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
815 argv += i, argc -= i;
818 if (!*inname)
819 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
820 "no input file specified");
822 /* Look for basic command line typos. This definitely doesn't
823 catch all errors, but it might help cases of fumbled fingers. */
824 if (!strcmp(inname, errname) || !strcmp(inname, outname) ||
825 !strcmp(inname, listname))
826 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
827 "file `%s' is both input and output file",
828 inname);
830 if (*errname) {
831 error_file = fopen(errname, "w");
832 if (!error_file) {
833 error_file = stderr; /* Revert to default! */
834 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
835 "cannot open file `%s' for error messages",
836 errname);
841 /* List of directives */
842 enum directives {
843 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
844 D_EXTERN, D_FLOAT, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
846 static const char *directives[] = {
847 "", "absolute", "bits", "common", "cpu", "debug", "default",
848 "extern", "float", "global", "list", "section", "segment", "warning"
850 static enum directives getkw(char **directive, char **value);
852 static void assemble_file(char *fname)
854 char *directive, *value, *p, *q, *special, *line, debugid[80];
855 insn output_ins;
856 int i, validid;
857 bool rn_error;
858 int32_t seg;
859 int64_t offs;
860 struct tokenval tokval;
861 expr *e;
862 int pass, pass_max;
863 int pass_cnt = 0; /* count actual passes */
865 if (cmd_sb == 32 && cmd_cpu < IF_386)
866 report_error(ERR_FATAL, "command line: "
867 "32-bit segment size requires a higher cpu");
869 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
870 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
871 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
872 int pass1, pass2;
873 ldfunc def_label;
875 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
876 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
877 /* pass0 seq is 0, 0, 0,..., 1, 2 */
879 def_label = pass > 1 ? redefine_label : define_label;
881 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
882 cpu = cmd_cpu;
883 if (pass0 == 2) {
884 if (*listname)
885 nasmlist.init(listname, report_error);
887 in_abs_seg = false;
888 global_offset_changed = false; /* set by redefine_label */
889 location.segment = ofmt->section(NULL, pass2, &sb);
890 globalbits = sb;
891 if (pass > 1) {
892 saa_rewind(forwrefs);
893 forwref = saa_rstruct(forwrefs);
894 raa_free(offsets);
895 offsets = raa_init();
897 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
898 globallineno = 0;
899 if (pass == 1)
900 location.known = true;
901 location.offset = offs = GET_CURR_OFFS;
903 while ((line = preproc->getline())) {
904 enum directives d;
905 globallineno++;
907 /* here we parse our directives; this is not handled by the 'real'
908 * parser. */
909 directive = line;
910 d = getkw(&directive, &value);
911 if (d) {
912 int err = 0;
914 switch (d) {
915 case D_SEGMENT: /* [SEGMENT n] */
916 case D_SECTION:
917 seg = ofmt->section(value, pass2, &sb);
918 if (seg == NO_SEG) {
919 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
920 "segment name `%s' not recognized",
921 value);
922 } else {
923 in_abs_seg = false;
924 location.segment = seg;
926 break;
927 case D_EXTERN: /* [EXTERN label:special] */
928 if (*value == '$')
929 value++; /* skip initial $ if present */
930 if (pass0 == 2) {
931 q = value;
932 while (*q && *q != ':')
933 q++;
934 if (*q == ':') {
935 *q++ = '\0';
936 ofmt->symdef(value, 0L, 0L, 3, q);
938 } else if (pass == 1) { /* pass == 1 */
939 q = value;
940 validid = true;
941 if (!isidstart(*q))
942 validid = false;
943 while (*q && *q != ':') {
944 if (!isidchar(*q))
945 validid = false;
946 q++;
948 if (!validid) {
949 report_error(ERR_NONFATAL,
950 "identifier expected after EXTERN");
951 break;
953 if (*q == ':') {
954 *q++ = '\0';
955 special = q;
956 } else
957 special = NULL;
958 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
959 int temp = pass0;
960 pass0 = 1; /* fake pass 1 in labels.c */
961 declare_as_global(value, special,
962 report_error);
963 define_label(value, seg_alloc(), 0L, NULL,
964 false, true, ofmt, report_error);
965 pass0 = temp;
967 } /* else pass0 == 1 */
968 break;
969 case D_BITS: /* [BITS bits] */
970 globalbits = sb = get_bits(value);
971 break;
972 case D_GLOBAL: /* [GLOBAL symbol:special] */
973 if (*value == '$')
974 value++; /* skip initial $ if present */
975 if (pass0 == 2) { /* pass 2 */
976 q = value;
977 while (*q && *q != ':')
978 q++;
979 if (*q == ':') {
980 *q++ = '\0';
981 ofmt->symdef(value, 0L, 0L, 3, q);
983 } else if (pass2 == 1) { /* pass == 1 */
984 q = value;
985 validid = true;
986 if (!isidstart(*q))
987 validid = false;
988 while (*q && *q != ':') {
989 if (!isidchar(*q))
990 validid = false;
991 q++;
993 if (!validid) {
994 report_error(ERR_NONFATAL,
995 "identifier expected after GLOBAL");
996 break;
998 if (*q == ':') {
999 *q++ = '\0';
1000 special = q;
1001 } else
1002 special = NULL;
1003 declare_as_global(value, special, report_error);
1004 } /* pass == 1 */
1005 break;
1006 case D_COMMON: /* [COMMON symbol size:special] */
1007 if (*value == '$')
1008 value++; /* skip initial $ if present */
1009 if (pass0 == 1) {
1010 p = value;
1011 validid = true;
1012 if (!isidstart(*p))
1013 validid = false;
1014 while (*p && !isspace(*p)) {
1015 if (!isidchar(*p))
1016 validid = false;
1017 p++;
1019 if (!validid) {
1020 report_error(ERR_NONFATAL,
1021 "identifier expected after COMMON");
1022 break;
1024 if (*p) {
1025 int64_t size;
1027 while (*p && isspace(*p))
1028 *p++ = '\0';
1029 q = p;
1030 while (*q && *q != ':')
1031 q++;
1032 if (*q == ':') {
1033 *q++ = '\0';
1034 special = q;
1035 } else
1036 special = NULL;
1037 size = readnum(p, &rn_error);
1038 if (rn_error)
1039 report_error(ERR_NONFATAL,
1040 "invalid size specified"
1041 " in COMMON declaration");
1042 else
1043 define_common(value, seg_alloc(), size,
1044 special, ofmt, report_error);
1045 } else
1046 report_error(ERR_NONFATAL,
1047 "no size specified in"
1048 " COMMON declaration");
1049 } else if (pass0 == 2) { /* pass == 2 */
1050 q = value;
1051 while (*q && *q != ':') {
1052 if (isspace(*q))
1053 *q = '\0';
1054 q++;
1056 if (*q == ':') {
1057 *q++ = '\0';
1058 ofmt->symdef(value, 0L, 0L, 3, q);
1061 break;
1062 case D_ABSOLUTE: /* [ABSOLUTE address] */
1063 stdscan_reset();
1064 stdscan_bufptr = value;
1065 tokval.t_type = TOKEN_INVALID;
1066 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1067 report_error, NULL);
1068 if (e) {
1069 if (!is_reloc(e))
1070 report_error(pass0 ==
1071 1 ? ERR_NONFATAL : ERR_PANIC,
1072 "cannot use non-relocatable expression as "
1073 "ABSOLUTE address");
1074 else {
1075 abs_seg = reloc_seg(e);
1076 abs_offset = reloc_value(e);
1078 } else if (pass == 1)
1079 abs_offset = 0x100; /* don't go near zero in case of / */
1080 else
1081 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1082 "in pass two");
1083 in_abs_seg = true;
1084 location.segment = NO_SEG;
1085 break;
1086 case D_DEBUG: /* [DEBUG] */
1087 p = value;
1088 q = debugid;
1089 validid = true;
1090 if (!isidstart(*p))
1091 validid = false;
1092 while (*p && !isspace(*p)) {
1093 if (!isidchar(*p))
1094 validid = false;
1095 *q++ = *p++;
1097 *q++ = 0;
1098 if (!validid) {
1099 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1100 "identifier expected after DEBUG");
1101 break;
1103 while (*p && isspace(*p))
1104 p++;
1105 if (pass == pass_max)
1106 ofmt->current_dfmt->debug_directive(debugid, p);
1107 break;
1108 case D_WARNING: /* [WARNING {+|-}warn-name] */
1109 if (pass1 == 1) {
1110 while (*value && isspace(*value))
1111 value++;
1113 if (*value == '+' || *value == '-') {
1114 validid = (*value == '-') ? true : false;
1115 value++;
1116 } else
1117 validid = false;
1119 for (i = 1; i <= ERR_WARN_MAX; i++)
1120 if (!nasm_stricmp(value, suppressed_names[i]))
1121 break;
1122 if (i <= ERR_WARN_MAX)
1123 suppressed[i] = validid;
1124 else
1125 report_error(ERR_NONFATAL,
1126 "invalid warning id in WARNING directive");
1128 break;
1129 case D_CPU: /* [CPU] */
1130 cpu = get_cpu(value);
1131 break;
1132 case D_LIST: /* [LIST {+|-}] */
1133 while (*value && isspace(*value))
1134 value++;
1136 if (*value == '+') {
1137 user_nolist = 0;
1138 } else {
1139 if (*value == '-') {
1140 user_nolist = 1;
1141 } else {
1142 err = 1;
1145 break;
1146 case D_DEFAULT: /* [DEFAULT] */
1147 stdscan_reset();
1148 stdscan_bufptr = value;
1149 tokval.t_type = TOKEN_INVALID;
1150 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1151 switch ((int)tokval.t_integer) {
1152 case S_REL:
1153 globalrel = 1;
1154 break;
1155 case S_ABS:
1156 globalrel = 0;
1157 break;
1158 default:
1159 err = 1;
1160 break;
1162 } else {
1163 err = 1;
1165 break;
1166 case D_FLOAT:
1167 if (float_option(value)) {
1168 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1169 "unknown 'float' directive: %s",
1170 value);
1172 break;
1173 default:
1174 if (!ofmt->directive(directive, value, pass2))
1175 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1176 "unrecognised directive [%s]",
1177 directive);
1179 if (err) {
1180 report_error(ERR_NONFATAL,
1181 "invalid parameter to [%s] directive",
1182 directive);
1184 } else { /* it isn't a directive */
1186 parse_line(pass1, line, &output_ins,
1187 report_error, evaluate, def_label);
1189 if (!(optimizing > 0) && pass == 2) {
1190 if (forwref != NULL && globallineno == forwref->lineno) {
1191 output_ins.forw_ref = true;
1192 do {
1193 output_ins.oprs[forwref->operand].opflags |=
1194 OPFLAG_FORWARD;
1195 forwref = saa_rstruct(forwrefs);
1196 } while (forwref != NULL
1197 && forwref->lineno == globallineno);
1198 } else
1199 output_ins.forw_ref = false;
1202 if (!(optimizing > 0) && output_ins.forw_ref) {
1203 if (pass == 1) {
1204 for (i = 0; i < output_ins.operands; i++) {
1205 if (output_ins.oprs[i].
1206 opflags & OPFLAG_FORWARD) {
1207 struct forwrefinfo *fwinf =
1208 (struct forwrefinfo *)
1209 saa_wstruct(forwrefs);
1210 fwinf->lineno = globallineno;
1211 fwinf->operand = i;
1214 } else { /* pass == 2 */
1216 * Hack to prevent phase error in the code
1217 * rol ax,x
1218 * x equ 1
1220 * If the second operand is a forward reference,
1221 * the UNITY property of the number 1 in that
1222 * operand is cancelled. Otherwise the above
1223 * sequence will cause a phase error.
1225 * This hack means that the above code will
1226 * generate 286+ code.
1228 * The forward reference will mean that the
1229 * operand will not have the UNITY property on
1230 * the first pass, so the pass behaviours will
1231 * be consistent.
1234 if (output_ins.operands >= 2 &&
1235 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1236 !(IMMEDIATE & ~output_ins.oprs[1].type))
1238 /* Remove special properties bits */
1239 output_ins.oprs[1].type &= ~REG_SMASK;
1242 } /* pass == 2 */
1246 /* forw_ref */
1247 if (output_ins.opcode == I_EQU) {
1248 if (pass1 == 1) {
1250 * Special `..' EQUs get processed in pass two,
1251 * except `..@' macro-processor EQUs which are done
1252 * in the normal place.
1254 if (!output_ins.label)
1255 report_error(ERR_NONFATAL,
1256 "EQU not preceded by label");
1258 else if (output_ins.label[0] != '.' ||
1259 output_ins.label[1] != '.' ||
1260 output_ins.label[2] == '@') {
1261 if (output_ins.operands == 1 &&
1262 (output_ins.oprs[0].type & IMMEDIATE) &&
1263 output_ins.oprs[0].wrt == NO_SEG) {
1264 int isext =
1265 output_ins.oprs[0].
1266 opflags & OPFLAG_EXTERN;
1267 def_label(output_ins.label,
1268 output_ins.oprs[0].segment,
1269 output_ins.oprs[0].offset, NULL,
1270 false, isext, ofmt,
1271 report_error);
1272 } else if (output_ins.operands == 2
1273 && (output_ins.oprs[0].
1274 type & IMMEDIATE)
1275 && (output_ins.oprs[0].type & COLON)
1276 && output_ins.oprs[0].segment ==
1277 NO_SEG
1278 && output_ins.oprs[0].wrt == NO_SEG
1279 && (output_ins.oprs[1].
1280 type & IMMEDIATE)
1281 && output_ins.oprs[1].segment ==
1282 NO_SEG
1283 && output_ins.oprs[1].wrt ==
1284 NO_SEG) {
1285 def_label(output_ins.label,
1286 output_ins.oprs[0].
1287 offset | SEG_ABS,
1288 output_ins.oprs[1].offset, NULL,
1289 false, false, ofmt,
1290 report_error);
1291 } else
1292 report_error(ERR_NONFATAL,
1293 "bad syntax for EQU");
1295 } else { /* pass == 2 */
1297 * Special `..' EQUs get processed here, except
1298 * `..@' macro processor EQUs which are done above.
1300 if (output_ins.label[0] == '.' &&
1301 output_ins.label[1] == '.' &&
1302 output_ins.label[2] != '@') {
1303 if (output_ins.operands == 1 &&
1304 (output_ins.oprs[0].type & IMMEDIATE)) {
1305 define_label(output_ins.label,
1306 output_ins.oprs[0].segment,
1307 output_ins.oprs[0].offset,
1308 NULL, false, false, ofmt,
1309 report_error);
1310 } else if (output_ins.operands == 2
1311 && (output_ins.oprs[0].
1312 type & IMMEDIATE)
1313 && (output_ins.oprs[0].type & COLON)
1314 && output_ins.oprs[0].segment ==
1315 NO_SEG
1316 && (output_ins.oprs[1].
1317 type & IMMEDIATE)
1318 && output_ins.oprs[1].segment ==
1319 NO_SEG) {
1320 define_label(output_ins.label,
1321 output_ins.oprs[0].
1322 offset | SEG_ABS,
1323 output_ins.oprs[1].offset,
1324 NULL, false, false, ofmt,
1325 report_error);
1326 } else
1327 report_error(ERR_NONFATAL,
1328 "bad syntax for EQU");
1330 } /* pass == 2 */
1331 } else { /* instruction isn't an EQU */
1333 if (pass1 == 1) {
1335 int64_t l = insn_size(location.segment, offs, sb, cpu,
1336 &output_ins, report_error);
1338 /* if (using_debug_info) && output_ins.opcode != -1) */
1339 if (using_debug_info)
1340 { /* fbk 03/25/01 */
1341 /* this is done here so we can do debug type info */
1342 int32_t typeinfo =
1343 TYS_ELEMENTS(output_ins.operands);
1344 switch (output_ins.opcode) {
1345 case I_RESB:
1346 typeinfo =
1347 TYS_ELEMENTS(output_ins.oprs[0].
1348 offset) | TY_BYTE;
1349 break;
1350 case I_RESW:
1351 typeinfo =
1352 TYS_ELEMENTS(output_ins.oprs[0].
1353 offset) | TY_WORD;
1354 break;
1355 case I_RESD:
1356 typeinfo =
1357 TYS_ELEMENTS(output_ins.oprs[0].
1358 offset) | TY_DWORD;
1359 break;
1360 case I_RESQ:
1361 typeinfo =
1362 TYS_ELEMENTS(output_ins.oprs[0].
1363 offset) | TY_QWORD;
1364 break;
1365 case I_REST:
1366 typeinfo =
1367 TYS_ELEMENTS(output_ins.oprs[0].
1368 offset) | TY_TBYTE;
1369 break;
1370 case I_DB:
1371 typeinfo |= TY_BYTE;
1372 break;
1373 case I_DW:
1374 typeinfo |= TY_WORD;
1375 break;
1376 case I_DD:
1377 if (output_ins.eops_float)
1378 typeinfo |= TY_FLOAT;
1379 else
1380 typeinfo |= TY_DWORD;
1381 break;
1382 case I_DQ:
1383 typeinfo |= TY_QWORD;
1384 break;
1385 case I_DT:
1386 typeinfo |= TY_TBYTE;
1387 break;
1388 case I_DO:
1389 typeinfo |= TY_OWORD;
1390 break;
1391 default:
1392 typeinfo = TY_LABEL;
1396 ofmt->current_dfmt->debug_typevalue(typeinfo);
1399 if (l != -1) {
1400 offs += l;
1401 SET_CURR_OFFS(offs);
1404 * else l == -1 => invalid instruction, which will be
1405 * flagged as an error on pass 2
1408 } else { /* pass == 2 */
1409 offs += assemble(location.segment, offs, sb, cpu,
1410 &output_ins, ofmt, report_error,
1411 &nasmlist);
1412 SET_CURR_OFFS(offs);
1415 } /* not an EQU */
1416 cleanup_insn(&output_ins);
1418 nasm_free(line);
1419 location.offset = offs = GET_CURR_OFFS;
1420 } /* end while (line = preproc->getline... */
1422 if (pass1 == 2 && global_offset_changed)
1423 report_error(ERR_NONFATAL,
1424 "phase error detected at end of assembly.");
1426 if (pass1 == 1)
1427 preproc->cleanup(1);
1429 if (pass1 == 1 && terminate_after_phase) {
1430 fclose(ofile);
1431 remove(outname);
1432 if (want_usage)
1433 usage();
1434 exit(1);
1436 pass_cnt++;
1437 if (pass > 1 && !global_offset_changed) {
1438 pass0++;
1439 if (pass0 == 2)
1440 pass = pass_max - 1;
1441 } else if (!(optimizing > 0))
1442 pass0++;
1444 } /* for (pass=1; pass<=2; pass++) */
1446 preproc->cleanup(0);
1447 nasmlist.cleanup();
1448 #if 1
1449 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1450 fprintf(stdout,
1451 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1452 #endif
1453 } /* exit from assemble_file (...) */
1455 static enum directives getkw(char **directive, char **value)
1457 char *p, *q, *buf;
1459 buf = *directive;
1461 /* allow leading spaces or tabs */
1462 while (*buf == ' ' || *buf == '\t')
1463 buf++;
1465 if (*buf != '[')
1466 return 0;
1468 p = buf;
1470 while (*p && *p != ']')
1471 p++;
1473 if (!*p)
1474 return 0;
1476 q = p++;
1478 while (*p && *p != ';') {
1479 if (!isspace(*p))
1480 return 0;
1481 p++;
1483 q[1] = '\0';
1485 *directive = p = buf + 1;
1486 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1487 buf++;
1488 if (*buf == ']') {
1489 *buf = '\0';
1490 *value = buf;
1491 } else {
1492 *buf++ = '\0';
1493 while (isspace(*buf))
1494 buf++; /* beppu - skip leading whitespace */
1495 *value = buf;
1496 while (*buf != ']')
1497 buf++;
1498 *buf++ = '\0';
1501 return bsii(*directive, directives, elements(directives));
1505 * gnu style error reporting
1506 * This function prints an error message to error_file in the
1507 * style used by GNU. An example would be:
1508 * file.asm:50: error: blah blah blah
1509 * where file.asm is the name of the file, 50 is the line number on
1510 * which the error occurs (or is detected) and "error:" is one of
1511 * the possible optional diagnostics -- it can be "error" or "warning"
1512 * or something else. Finally the line terminates with the actual
1513 * error message.
1515 * @param severity the severity of the warning or error
1516 * @param fmt the printf style format string
1518 static void report_error_gnu(int severity, const char *fmt, ...)
1520 va_list ap;
1522 if (is_suppressed_warning(severity))
1523 return;
1525 if (severity & ERR_NOFILE)
1526 fputs("nasm: ", error_file);
1527 else {
1528 char *currentfile = NULL;
1529 int32_t lineno = 0;
1530 src_get(&lineno, &currentfile);
1531 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1532 nasm_free(currentfile);
1534 va_start(ap, fmt);
1535 report_error_common(severity, fmt, ap);
1536 va_end(ap);
1540 * MS style error reporting
1541 * This function prints an error message to error_file in the
1542 * style used by Visual C and some other Microsoft tools. An example
1543 * would be:
1544 * file.asm(50) : error: blah blah blah
1545 * where file.asm is the name of the file, 50 is the line number on
1546 * which the error occurs (or is detected) and "error:" is one of
1547 * the possible optional diagnostics -- it can be "error" or "warning"
1548 * or something else. Finally the line terminates with the actual
1549 * error message.
1551 * @param severity the severity of the warning or error
1552 * @param fmt the printf style format string
1554 static void report_error_vc(int severity, const char *fmt, ...)
1556 va_list ap;
1558 if (is_suppressed_warning(severity))
1559 return;
1561 if (severity & ERR_NOFILE)
1562 fputs("nasm: ", error_file);
1563 else {
1564 char *currentfile = NULL;
1565 int32_t lineno = 0;
1566 src_get(&lineno, &currentfile);
1567 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1568 nasm_free(currentfile);
1570 va_start(ap, fmt);
1571 report_error_common(severity, fmt, ap);
1572 va_end(ap);
1576 * check for supressed warning
1577 * checks for suppressed warning or pass one only warning and we're
1578 * not in pass 1
1580 * @param severity the severity of the warning or error
1581 * @return true if we should abort error/warning printing
1583 static int is_suppressed_warning(int severity)
1586 * See if it's a suppressed warning.
1588 return ((severity & ERR_MASK) == ERR_WARNING &&
1589 (severity & ERR_WARN_MASK) != 0 &&
1590 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1592 * See if it's a pass-one only warning and we're not in pass one.
1594 ((severity & ERR_PASS1) && pass0 == 2);
1598 * common error reporting
1599 * This is the common back end of the error reporting schemes currently
1600 * implemented. It prints the nature of the warning and then the
1601 * specific error message to error_file and may or may not return. It
1602 * doesn't return if the error severity is a "panic" or "debug" type.
1604 * @param severity the severity of the warning or error
1605 * @param fmt the printf style format string
1607 static void report_error_common(int severity, const char *fmt,
1608 va_list args)
1610 switch (severity & ERR_MASK) {
1611 case ERR_WARNING:
1612 fputs("warning: ", error_file);
1613 break;
1614 case ERR_NONFATAL:
1615 fputs("error: ", error_file);
1616 break;
1617 case ERR_FATAL:
1618 fputs("fatal: ", error_file);
1619 break;
1620 case ERR_PANIC:
1621 fputs("panic: ", error_file);
1622 break;
1623 case ERR_DEBUG:
1624 fputs("debug: ", error_file);
1625 break;
1628 vfprintf(error_file, fmt, args);
1629 putc('\n', error_file);
1631 if (severity & ERR_USAGE)
1632 want_usage = true;
1634 switch (severity & ERR_MASK) {
1635 case ERR_DEBUG:
1636 /* no further action, by definition */
1637 break;
1638 case ERR_WARNING:
1639 if (!suppressed[0]) /* Treat warnings as errors */
1640 terminate_after_phase = true;
1641 break;
1642 case ERR_NONFATAL:
1643 terminate_after_phase = true;
1644 break;
1645 case ERR_FATAL:
1646 if (ofile) {
1647 fclose(ofile);
1648 remove(outname);
1650 if (want_usage)
1651 usage();
1652 exit(1); /* instantly die */
1653 break; /* placate silly compilers */
1654 case ERR_PANIC:
1655 fflush(NULL);
1656 /* abort(); *//* halt, catch fire, and dump core */
1657 exit(3);
1658 break;
1662 static void usage(void)
1664 fputs("type `nasm -h' for help\n", error_file);
1667 static void register_output_formats(void)
1669 ofmt = ofmt_register(report_error);
1672 #define BUF_DELTA 512
1674 static FILE *no_pp_fp;
1675 static efunc no_pp_err;
1676 static ListGen *no_pp_list;
1677 static int32_t no_pp_lineinc;
1679 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1680 ListGen * listgen)
1682 src_set_fname(nasm_strdup(file));
1683 src_set_linnum(0);
1684 no_pp_lineinc = 1;
1685 no_pp_err = error;
1686 no_pp_fp = fopen(file, "r");
1687 if (!no_pp_fp)
1688 no_pp_err(ERR_FATAL | ERR_NOFILE,
1689 "unable to open input file `%s'", file);
1690 no_pp_list = listgen;
1691 (void)pass; /* placate compilers */
1692 (void)eval; /* placate compilers */
1695 static char *no_pp_getline(void)
1697 char *buffer, *p, *q;
1698 int bufsize;
1700 bufsize = BUF_DELTA;
1701 buffer = nasm_malloc(BUF_DELTA);
1702 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1704 while (1) { /* Loop to handle %line */
1706 p = buffer;
1707 while (1) { /* Loop to handle long lines */
1708 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1709 if (!q)
1710 break;
1711 p += strlen(p);
1712 if (p > buffer && p[-1] == '\n')
1713 break;
1714 if (p - buffer > bufsize - 10) {
1715 int offset;
1716 offset = p - buffer;
1717 bufsize += BUF_DELTA;
1718 buffer = nasm_realloc(buffer, bufsize);
1719 p = buffer + offset;
1723 if (!q && p == buffer) {
1724 nasm_free(buffer);
1725 return NULL;
1729 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1730 * them are present at the end of the line.
1732 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1734 if (!nasm_strnicmp(buffer, "%line", 5)) {
1735 int32_t ln;
1736 int li;
1737 char *nm = nasm_malloc(strlen(buffer));
1738 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1739 nasm_free(src_set_fname(nm));
1740 src_set_linnum(ln);
1741 no_pp_lineinc = li;
1742 continue;
1744 nasm_free(nm);
1746 break;
1749 no_pp_list->line(LIST_READ, buffer);
1751 return buffer;
1754 static void no_pp_cleanup(int pass)
1756 (void)pass; /* placate GCC */
1757 fclose(no_pp_fp);
1760 static uint32_t get_cpu(char *value)
1762 if (!strcmp(value, "8086"))
1763 return IF_8086;
1764 if (!strcmp(value, "186"))
1765 return IF_186;
1766 if (!strcmp(value, "286"))
1767 return IF_286;
1768 if (!strcmp(value, "386"))
1769 return IF_386;
1770 if (!strcmp(value, "486"))
1771 return IF_486;
1772 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1773 return IF_PENT;
1774 if (!strcmp(value, "686") ||
1775 !nasm_stricmp(value, "ppro") ||
1776 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1777 return IF_P6;
1778 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1779 return IF_KATMAI;
1780 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1781 !nasm_stricmp(value, "willamette"))
1782 return IF_WILLAMETTE;
1783 if (!nasm_stricmp(value, "prescott"))
1784 return IF_PRESCOTT;
1785 if (!nasm_stricmp(value, "x64") ||
1786 !nasm_stricmp(value, "x86-64"))
1787 return IF_X86_64;
1788 if (!nasm_stricmp(value, "ia64") ||
1789 !nasm_stricmp(value, "ia-64") ||
1790 !nasm_stricmp(value, "itanium") ||
1791 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1792 return IF_IA64;
1794 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1795 "unknown 'cpu' type");
1797 return IF_PLEVEL; /* the maximum level */
1800 static int get_bits(char *value)
1802 int i;
1804 if ((i = atoi(value)) == 16)
1805 return i; /* set for a 16-bit segment */
1806 else if (i == 32) {
1807 if (cpu < IF_386) {
1808 report_error(ERR_NONFATAL,
1809 "cannot specify 32-bit segment on processor below a 386");
1810 i = 16;
1812 } else if (i == 64) {
1813 if (cpu < IF_X86_64) {
1814 report_error(ERR_NONFATAL,
1815 "cannot specify 64-bit segment on processor below an x86-64");
1816 i = 16;
1818 if (i != maxbits) {
1819 report_error(ERR_NONFATAL,
1820 "%s output format does not support 64-bit code",
1821 ofmt->shortname);
1822 i = 16;
1824 } else {
1825 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1826 "`%s' is not a valid segment size; must be 16, 32 or 64",
1827 value);
1828 i = 16;
1830 return i;
1833 /* end of nasm.c */