Fix global variables without declarations
[nasm.git] / asm / nasm.c
blob9e3c2344d41ddbe9f2298e767e4e52a5ecbaad1a
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * The Netwide Assembler main program module
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <limits.h>
46 #include <time.h>
48 #include "nasm.h"
49 #include "nasmlib.h"
50 #include "error.h"
51 #include "saa.h"
52 #include "raa.h"
53 #include "float.h"
54 #include "stdscan.h"
55 #include "insns.h"
56 #include "preproc.h"
57 #include "parser.h"
58 #include "eval.h"
59 #include "assemble.h"
60 #include "labels.h"
61 #include "outform.h"
62 #include "listing.h"
63 #include "iflag.h"
64 #include "ver.h"
67 * This is the maximum number of optimization passes to do. If we ever
68 * find a case where the optimizer doesn't naturally converge, we might
69 * have to drop this value so the assembler doesn't appear to just hang.
71 #define MAX_OPTIMIZE (INT_MAX >> 1)
73 struct forwrefinfo { /* info held on forward refs. */
74 int lineno;
75 int operand;
78 static void parse_cmdline(int, char **, int);
79 static void assemble_file(char *, StrList **);
80 static bool is_suppressed_warning(int severity);
81 static bool skip_this_pass(int severity);
82 static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
83 static void nasm_verror_vc(int severity, const char *fmt, va_list args);
84 static void nasm_verror_common(int severity, const char *fmt, va_list args);
85 static void usage(void);
87 static bool using_debug_info, opt_verbose_info;
88 static const char *debug_format;
90 bool tasm_compatible_mode = false;
91 int pass0, passn;
92 static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
93 int globalrel = 0;
94 int globalbnd = 0;
96 static time_t official_compile_time;
98 static char inname[FILENAME_MAX];
99 static char outname[FILENAME_MAX];
100 static char listname[FILENAME_MAX];
101 static char errname[FILENAME_MAX];
102 static int globallineno; /* for forward-reference tracking */
103 /* static int pass = 0; */
104 const struct ofmt *ofmt = &OF_DEFAULT;
105 const struct ofmt_alias *ofmt_alias = NULL;
106 const struct dfmt *dfmt;
108 static FILE *error_file; /* Where to write error messages */
110 FILE *ofile = NULL;
111 int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
112 static int cmd_sb = 16; /* by default */
114 iflag_t cpu;
115 static iflag_t cmd_cpu;
117 struct location location;
118 bool in_absolute; /* Flag we are in ABSOLUTE seg */
119 struct location absolute; /* Segment/offset inside ABSOLUTE */
121 static struct RAA *offsets;
123 static struct SAA *forwrefs; /* keep track of forward references */
124 static const struct forwrefinfo *forwref;
126 static const struct preproc_ops *preproc;
128 #define OP_NORMAL (1u << 0)
129 #define OP_PREPROCESS (1u << 1)
130 #define OP_DEPEND (1u << 2)
132 static unsigned int operating_mode;
134 /* Dependency flags */
135 static bool depend_emit_phony = false;
136 static bool depend_missing_ok = false;
137 static const char *depend_target = NULL;
138 static const char *depend_file = NULL;
140 static bool want_usage;
141 static bool terminate_after_phase;
142 bool user_nolist = false;
144 static char *quote_for_make(const char *str);
146 static int64_t get_curr_offs(void)
148 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
151 static void set_curr_offs(int64_t l_off)
153 if (in_absolute)
154 absolute.offset = l_off;
155 else
156 offsets = raa_write(offsets, location.segment, l_off);
159 static void nasm_fputs(const char *line, FILE * outfile)
161 if (outfile) {
162 fputs(line, outfile);
163 putc('\n', outfile);
164 } else
165 puts(line);
168 /* Convert a struct tm to a POSIX-style time constant */
169 static int64_t make_posix_time(struct tm *tm)
171 int64_t t;
172 int64_t y = tm->tm_year;
174 /* See IEEE 1003.1:2004, section 4.14 */
176 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
177 t += tm->tm_yday;
178 t *= 24;
179 t += tm->tm_hour;
180 t *= 60;
181 t += tm->tm_min;
182 t *= 60;
183 t += tm->tm_sec;
185 return t;
188 static void define_macros_early(void)
190 char temp[128];
191 struct tm lt, *lt_p, gm, *gm_p;
192 int64_t posix_time;
194 lt_p = localtime(&official_compile_time);
195 if (lt_p) {
196 lt = *lt_p;
198 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
199 preproc->pre_define(temp);
200 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
201 preproc->pre_define(temp);
202 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
203 preproc->pre_define(temp);
204 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
205 preproc->pre_define(temp);
208 gm_p = gmtime(&official_compile_time);
209 if (gm_p) {
210 gm = *gm_p;
212 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
213 preproc->pre_define(temp);
214 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
215 preproc->pre_define(temp);
216 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
217 preproc->pre_define(temp);
218 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
219 preproc->pre_define(temp);
222 if (gm_p)
223 posix_time = make_posix_time(&gm);
224 else if (lt_p)
225 posix_time = make_posix_time(&lt);
226 else
227 posix_time = 0;
229 if (posix_time) {
230 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
231 preproc->pre_define(temp);
235 static void define_macros_late(void)
237 char temp[128];
240 * In case if output format is defined by alias
241 * we have to put shortname of the alias itself here
242 * otherwise ABI backward compatibility gets broken.
244 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
245 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
246 preproc->pre_define(temp);
249 static void emit_dependencies(StrList *list)
251 FILE *deps;
252 int linepos, len;
253 StrList *l, *nl;
255 if (depend_file && strcmp(depend_file, "-")) {
256 deps = nasm_open_write(depend_file, NF_TEXT);
257 if (!deps) {
258 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
259 "unable to write dependency file `%s'", depend_file);
260 return;
262 } else {
263 deps = stdout;
266 linepos = fprintf(deps, "%s:", depend_target);
267 list_for_each(l, list) {
268 char *file = quote_for_make(l->str);
269 len = strlen(file);
270 if (linepos + len > 62 && linepos > 1) {
271 fprintf(deps, " \\\n ");
272 linepos = 1;
274 fprintf(deps, " %s", file);
275 linepos += len+1;
276 nasm_free(file);
278 fprintf(deps, "\n\n");
280 list_for_each_safe(l, nl, list) {
281 if (depend_emit_phony)
282 fprintf(deps, "%s:\n\n", l->str);
283 nasm_free(l);
286 if (deps != stdout)
287 fclose(deps);
290 int main(int argc, char **argv)
292 StrList *depend_list = NULL, **depend_ptr;
294 time(&official_compile_time);
296 iflag_set(&cpu, IF_PLEVEL);
297 iflag_set(&cmd_cpu, IF_PLEVEL);
299 pass0 = 0;
300 want_usage = terminate_after_phase = false;
301 nasm_set_verror(nasm_verror_gnu);
303 error_file = stderr;
305 tolower_init();
306 src_init();
308 offsets = raa_init();
309 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
311 preproc = &nasmpp;
312 operating_mode = OP_NORMAL;
314 parse_cmdline(argc, argv, 1);
315 if (terminate_after_phase) {
316 if (want_usage)
317 usage();
318 return 1;
322 * Define some macros dependent on the runtime, but not
323 * on the command line (as those are scanned in cmdline pass 2.)
325 preproc->init();
326 define_macros_early();
328 parse_cmdline(argc, argv, 2);
329 if (terminate_after_phase) {
330 if (want_usage)
331 usage();
332 return 1;
335 if (!using_debug_info) {
336 /* No debug info, redirect to the null backend (empty stubs) */
337 dfmt = &null_debug_form;
338 } else if (!debug_format) {
339 /* Default debug format for this backend */
340 dfmt = ofmt->default_dfmt;
341 } else {
342 dfmt = dfmt_find(ofmt, debug_format);
343 if (!dfmt) {
344 nasm_fatal(ERR_NOFILE | ERR_USAGE,
345 "unrecognized debug format `%s' for"
346 " output format `%s'",
347 debug_format, ofmt->shortname);
351 if (ofmt->stdmac)
352 preproc->extra_stdmac(ofmt->stdmac);
354 /* define some macros dependent of command-line */
355 define_macros_late();
357 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
358 if (!depend_target)
359 depend_target = quote_for_make(outname);
361 if (operating_mode & OP_DEPEND) {
362 char *line;
364 if (depend_missing_ok)
365 preproc->include_path(NULL); /* "assume generated" */
367 preproc->reset(inname, 0, depend_ptr);
368 if (outname[0] == '\0')
369 ofmt->filename(inname, outname);
370 ofile = NULL;
371 while ((line = preproc->getline()))
372 nasm_free(line);
373 preproc->cleanup(0);
374 } else if (operating_mode & OP_PREPROCESS) {
375 char *line;
376 const char *file_name = NULL;
377 int32_t prior_linnum = 0;
378 int lineinc = 0;
380 if (*outname) {
381 ofile = nasm_open_write(outname, NF_TEXT);
382 if (!ofile)
383 nasm_fatal(ERR_NOFILE,
384 "unable to open output file `%s'",
385 outname);
386 } else
387 ofile = NULL;
389 location.known = false;
391 /* pass = 1; */
392 preproc->reset(inname, 3, depend_ptr);
393 memcpy(warning_on, warning_on_global,
394 (ERR_WARN_MAX+1) * sizeof(bool));
396 while ((line = preproc->getline())) {
398 * We generate %line directives if needed for later programs
400 int32_t linnum = prior_linnum += lineinc;
401 int altline = src_get(&linnum, &file_name);
402 if (altline) {
403 if (altline == 1 && lineinc == 1)
404 nasm_fputs("", ofile);
405 else {
406 lineinc = (altline != -1 || lineinc != 1);
407 fprintf(ofile ? ofile : stdout,
408 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
409 file_name);
411 prior_linnum = linnum;
413 nasm_fputs(line, ofile);
414 nasm_free(line);
416 preproc->cleanup(0);
417 if (ofile)
418 fclose(ofile);
419 if (ofile && terminate_after_phase)
420 remove(outname);
421 ofile = NULL;
424 if (operating_mode & OP_NORMAL) {
426 * We must call ofmt->filename _anyway_, even if the user
427 * has specified their own output file, because some
428 * formats (eg OBJ and COFF) use ofmt->filename to find out
429 * the name of the input file and then put that inside the
430 * file.
432 ofmt->filename(inname, outname);
434 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
435 if (!ofile)
436 nasm_fatal(ERR_NOFILE,
437 "unable to open output file `%s'", outname);
440 * We must call init_labels() before ofmt->init() since
441 * some object formats will want to define labels in their
442 * init routines. (eg OS/2 defines the FLAT group)
444 init_labels();
446 ofmt->init();
447 dfmt->init();
449 assemble_file(inname, depend_ptr);
451 if (!terminate_after_phase) {
452 ofmt->cleanup();
453 cleanup_labels();
454 fflush(ofile);
455 if (ferror(ofile)) {
456 nasm_error(ERR_NONFATAL|ERR_NOFILE,
457 "write error on output file `%s'", outname);
458 terminate_after_phase = true;
462 if (ofile) {
463 fclose(ofile);
464 if (terminate_after_phase)
465 remove(outname);
466 ofile = NULL;
470 if (depend_list && !terminate_after_phase)
471 emit_dependencies(depend_list);
473 if (want_usage)
474 usage();
476 raa_free(offsets);
477 saa_free(forwrefs);
478 eval_cleanup();
479 stdscan_cleanup();
480 src_free();
482 return terminate_after_phase;
486 * Get a parameter for a command line option.
487 * First arg must be in the form of e.g. -f...
489 static char *get_param(char *p, char *q, bool *advance)
491 *advance = false;
492 if (p[2]) /* the parameter's in the option */
493 return nasm_skip_spaces(p + 2);
494 if (q && q[0]) {
495 *advance = true;
496 return q;
498 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
499 "option `-%c' requires an argument", p[1]);
500 return NULL;
504 * Copy a filename
506 static void copy_filename(char *dst, const char *src)
508 size_t len = strlen(src);
510 if (len >= (size_t)FILENAME_MAX) {
511 nasm_fatal(ERR_NOFILE, "file name too long");
512 return;
514 strncpy(dst, src, FILENAME_MAX);
518 * Convert a string to Make-safe form
520 static char *quote_for_make(const char *str)
522 const char *p;
523 char *os, *q;
525 size_t n = 1; /* Terminating zero */
526 size_t nbs = 0;
528 if (!str)
529 return NULL;
531 for (p = str; *p; p++) {
532 switch (*p) {
533 case ' ':
534 case '\t':
535 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
536 n += nbs + 2;
537 nbs = 0;
538 break;
539 case '$':
540 case '#':
541 nbs = 0;
542 n += 2;
543 break;
544 case '\\':
545 nbs++;
546 n++;
547 break;
548 default:
549 nbs = 0;
550 n++;
551 break;
555 /* Convert N backslashes at the end of filename to 2N backslashes */
556 if (nbs)
557 n += nbs;
559 os = q = nasm_malloc(n);
561 nbs = 0;
562 for (p = str; *p; p++) {
563 switch (*p) {
564 case ' ':
565 case '\t':
566 while (nbs--)
567 *q++ = '\\';
568 *q++ = '\\';
569 *q++ = *p;
570 break;
571 case '$':
572 *q++ = *p;
573 *q++ = *p;
574 nbs = 0;
575 break;
576 case '#':
577 *q++ = '\\';
578 *q++ = *p;
579 nbs = 0;
580 break;
581 case '\\':
582 *q++ = *p;
583 nbs++;
584 break;
585 default:
586 *q++ = *p;
587 nbs = 0;
588 break;
591 while (nbs--)
592 *q++ = '\\';
594 *q = '\0';
596 return os;
599 struct textargs {
600 const char *label;
601 int value;
604 enum text_options {
605 OPT_PREFIX,
606 OPT_POSTFIX
608 static const struct textargs textopts[] = {
609 {"prefix", OPT_PREFIX},
610 {"postfix", OPT_POSTFIX},
611 {NULL, 0}
614 static void show_version(void)
616 printf("NASM version %s compiled on %s%s\n",
617 nasm_version, nasm_date, nasm_compile_options);
618 exit(0);
621 static bool stopoptions = false;
622 static bool process_arg(char *p, char *q, int pass)
624 char *param;
625 int i;
626 bool advance = false;
627 bool do_warn;
629 if (!p || !p[0])
630 return false;
632 if (p[0] == '-' && !stopoptions) {
633 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
634 /* These parameters take values */
635 if (!(param = get_param(p, q, &advance)))
636 return advance;
639 switch (p[1]) {
640 case 's':
641 if (pass == 1)
642 error_file = stdout;
643 break;
645 case 'o': /* output file */
646 if (pass == 2)
647 copy_filename(outname, param);
648 break;
650 case 'f': /* output format */
651 if (pass == 1) {
652 ofmt = ofmt_find(param, &ofmt_alias);
653 if (!ofmt) {
654 nasm_fatal(ERR_NOFILE | ERR_USAGE,
655 "unrecognised output format `%s' - "
656 "use -hf for a list", param);
659 break;
661 case 'O': /* Optimization level */
662 if (pass == 2) {
663 int opt;
665 if (!*param) {
666 /* Naked -O == -Ox */
667 optimizing = MAX_OPTIMIZE;
668 } else {
669 while (*param) {
670 switch (*param) {
671 case '0': case '1': case '2': case '3': case '4':
672 case '5': case '6': case '7': case '8': case '9':
673 opt = strtoul(param, &param, 10);
675 /* -O0 -> optimizing == -1, 0.98 behaviour */
676 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
677 if (opt < 2)
678 optimizing = opt - 1;
679 else
680 optimizing = opt;
681 break;
683 case 'v':
684 case '+':
685 param++;
686 opt_verbose_info = true;
687 break;
689 case 'x':
690 param++;
691 optimizing = MAX_OPTIMIZE;
692 break;
694 default:
695 nasm_fatal(0,
696 "unknown optimization option -O%c\n",
697 *param);
698 break;
701 if (optimizing > MAX_OPTIMIZE)
702 optimizing = MAX_OPTIMIZE;
705 break;
707 case 'p': /* pre-include */
708 case 'P':
709 if (pass == 2)
710 preproc->pre_include(param);
711 break;
713 case 'd': /* pre-define */
714 case 'D':
715 if (pass == 2)
716 preproc->pre_define(param);
717 break;
719 case 'u': /* un-define */
720 case 'U':
721 if (pass == 2)
722 preproc->pre_undefine(param);
723 break;
725 case 'i': /* include search path */
726 case 'I':
727 if (pass == 2)
728 preproc->include_path(param);
729 break;
731 case 'l': /* listing file */
732 if (pass == 2)
733 copy_filename(listname, param);
734 break;
736 case 'Z': /* error messages file */
737 if (pass == 1)
738 copy_filename(errname, param);
739 break;
741 case 'F': /* specify debug format */
742 if (pass == 2) {
743 using_debug_info = true;
744 debug_format = param;
746 break;
748 case 'X': /* specify error reporting format */
749 if (pass == 1) {
750 if (nasm_stricmp("vc", param) == 0)
751 nasm_set_verror(nasm_verror_vc);
752 else if (nasm_stricmp("gnu", param) == 0)
753 nasm_set_verror(nasm_verror_gnu);
754 else
755 nasm_fatal(ERR_NOFILE | ERR_USAGE,
756 "unrecognized error reporting format `%s'",
757 param);
759 break;
761 case 'g':
762 if (pass == 2) {
763 using_debug_info = true;
764 if (p[2])
765 debug_format = nasm_skip_spaces(p + 2);
767 break;
769 case 'h':
770 printf
771 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
772 "[-l listfile]\n"
773 " [options...] [--] filename\n"
774 " or nasm -v (or --v) for version info\n\n"
775 " -t assemble in SciTech TASM compatible mode\n");
776 printf
777 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
778 " -a don't preprocess (assemble only)\n"
779 " -M generate Makefile dependencies on stdout\n"
780 " -MG d:o, missing files assumed generated\n"
781 " -MF <file> set Makefile dependency file\n"
782 " -MD <file> assemble and generate dependencies\n"
783 " -MT <file> dependency target name\n"
784 " -MQ <file> dependency target name (quoted)\n"
785 " -MP emit phony target\n\n"
786 " -Z<file> redirect error messages to file\n"
787 " -s redirect error messages to stdout\n\n"
788 " -g generate debugging information\n\n"
789 " -F format select a debugging format\n\n"
790 " -gformat same as -g -F format\n\n"
791 " -o outfile write output to an outfile\n\n"
792 " -f format select an output format\n\n"
793 " -l listfile write listing to a listfile\n\n"
794 " -I<path> adds a pathname to the include file path\n");
795 printf
796 (" -O<digit> optimize branch offsets\n"
797 " -O0: No optimization\n"
798 " -O1: Minimal optimization\n"
799 " -Ox: Multipass optimization (default)\n\n"
800 " -P<file> pre-includes a file\n"
801 " -D<macro>[=<value>] pre-defines a macro\n"
802 " -U<macro> undefines a macro\n"
803 " -X<format> specifies error reporting format (gnu or vc)\n"
804 " -w+foo enables warning foo (equiv. -Wfoo)\n"
805 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
806 " -h show invocation summary and exit\n\n"
807 "--prefix,--postfix\n"
808 " this options prepend or append the given argument to all\n"
809 " extern and global variables\n"
810 "Warnings:\n");
811 for (i = 0; i <= ERR_WARN_MAX; i++)
812 printf(" %-23s %s (default %s)\n",
813 warnings[i].name, warnings[i].help,
814 warnings[i].enabled ? "on" : "off");
815 printf
816 ("\nresponse files should contain command line parameters"
817 ", one per line.\n");
818 if (p[2] == 'f') {
819 printf("\nvalid output formats for -f are"
820 " (`*' denotes default):\n");
821 ofmt_list(ofmt, stdout);
822 } else {
823 printf("\nFor a list of valid output formats, use -hf.\n");
824 printf("For a list of debug formats, use -f <form> -y.\n");
826 exit(0); /* never need usage message here */
827 break;
829 case 'y':
830 printf("\nvalid debug formats for '%s' output format are"
831 " ('*' denotes default):\n", ofmt->shortname);
832 dfmt_list(ofmt, stdout);
833 exit(0);
834 break;
836 case 't':
837 if (pass == 2)
838 tasm_compatible_mode = true;
839 break;
841 case 'v':
842 show_version();
843 break;
845 case 'e': /* preprocess only */
846 case 'E':
847 if (pass == 1)
848 operating_mode = OP_PREPROCESS;
849 break;
851 case 'a': /* assemble only - don't preprocess */
852 if (pass == 1)
853 preproc = &preproc_nop;
854 break;
856 case 'W':
857 if (pass == 2) {
858 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
859 do_warn = false;
860 param += 3;
861 } else {
862 do_warn = true;
864 goto set_warning;
866 break;
868 case 'w':
869 if (pass == 2) {
870 if (param[0] != '+' && param[0] != '-') {
871 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
872 "invalid option to `-w'");
873 break;
875 do_warn = (param[0] == '+');
876 param++;
877 goto set_warning;
879 break;
881 set_warning:
882 for (i = 0; i <= ERR_WARN_MAX; i++) {
883 if (!nasm_stricmp(param, warnings[i].name))
884 break;
886 if (i <= ERR_WARN_MAX) {
887 warning_on_global[i] = do_warn;
888 } else if (!nasm_stricmp(param, "all")) {
889 for (i = 1; i <= ERR_WARN_MAX; i++)
890 warning_on_global[i] = do_warn;
891 } else if (!nasm_stricmp(param, "none")) {
892 for (i = 1; i <= ERR_WARN_MAX; i++)
893 warning_on_global[i] = !do_warn;
894 } else {
895 /* Ignore invalid warning names; forward compatibility */
897 break;
899 case 'M':
900 if (pass == 2) {
901 switch (p[2]) {
902 case 0:
903 operating_mode = OP_DEPEND;
904 break;
905 case 'G':
906 operating_mode = OP_DEPEND;
907 depend_missing_ok = true;
908 break;
909 case 'P':
910 depend_emit_phony = true;
911 break;
912 case 'D':
913 operating_mode = OP_NORMAL;
914 depend_file = q;
915 advance = true;
916 break;
917 case 'F':
918 depend_file = q;
919 advance = true;
920 break;
921 case 'T':
922 depend_target = q;
923 advance = true;
924 break;
925 case 'Q':
926 depend_target = quote_for_make(q);
927 advance = true;
928 break;
929 default:
930 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
931 "unknown dependency option `-M%c'", p[2]);
932 break;
934 if (advance && (!q || !q[0])) {
935 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
936 "option `-M%c' requires a parameter", p[2]);
937 break;
940 break;
942 case '-':
944 int s;
946 if (p[2] == 0) { /* -- => stop processing options */
947 stopoptions = 1;
948 break;
951 if (!nasm_stricmp(p, "--v"))
952 show_version();
954 if (!nasm_stricmp(p, "--version"))
955 show_version();
957 for (s = 0; textopts[s].label; s++) {
958 if (!nasm_stricmp(p + 2, textopts[s].label)) {
959 break;
963 switch (s) {
965 case OPT_PREFIX:
966 case OPT_POSTFIX:
968 if (!q) {
969 nasm_error(ERR_NONFATAL | ERR_NOFILE |
970 ERR_USAGE,
971 "option `--%s' requires an argument",
972 p + 2);
973 break;
974 } else {
975 advance = 1, param = q;
978 switch (s) {
979 case OPT_PREFIX:
980 if (pass == 2)
981 strlcpy(lprefix, param, PREFIX_MAX);
982 break;
983 case OPT_POSTFIX:
984 if (pass == 2)
985 strlcpy(lpostfix, param, POSTFIX_MAX);
986 break;
987 default:
988 nasm_panic(ERR_NOFILE,
989 "internal error");
990 break;
992 break;
995 default:
997 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
998 "unrecognised option `--%s'", p + 2);
999 break;
1002 break;
1005 default:
1006 if (!ofmt->setinfo(GI_SWITCH, &p))
1007 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1008 "unrecognised option `-%c'", p[1]);
1009 break;
1011 } else if (pass == 2) {
1012 if (*inname) {
1013 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1014 "more than one input file specified");
1015 } else {
1016 copy_filename(inname, p);
1020 return advance;
1023 #define ARG_BUF_DELTA 128
1025 static void process_respfile(FILE * rfile, int pass)
1027 char *buffer, *p, *q, *prevarg;
1028 int bufsize, prevargsize;
1030 bufsize = prevargsize = ARG_BUF_DELTA;
1031 buffer = nasm_malloc(ARG_BUF_DELTA);
1032 prevarg = nasm_malloc(ARG_BUF_DELTA);
1033 prevarg[0] = '\0';
1035 while (1) { /* Loop to handle all lines in file */
1036 p = buffer;
1037 while (1) { /* Loop to handle long lines */
1038 q = fgets(p, bufsize - (p - buffer), rfile);
1039 if (!q)
1040 break;
1041 p += strlen(p);
1042 if (p > buffer && p[-1] == '\n')
1043 break;
1044 if (p - buffer > bufsize - 10) {
1045 int offset;
1046 offset = p - buffer;
1047 bufsize += ARG_BUF_DELTA;
1048 buffer = nasm_realloc(buffer, bufsize);
1049 p = buffer + offset;
1053 if (!q && p == buffer) {
1054 if (prevarg[0])
1055 process_arg(prevarg, NULL, pass);
1056 nasm_free(buffer);
1057 nasm_free(prevarg);
1058 return;
1062 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1063 * them are present at the end of the line.
1065 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
1067 while (p > buffer && nasm_isspace(p[-1]))
1068 *--p = '\0';
1070 p = nasm_skip_spaces(buffer);
1072 if (process_arg(prevarg, p, pass))
1073 *p = '\0';
1075 if ((int) strlen(p) > prevargsize - 10) {
1076 prevargsize += ARG_BUF_DELTA;
1077 prevarg = nasm_realloc(prevarg, prevargsize);
1079 strncpy(prevarg, p, prevargsize);
1083 /* Function to process args from a string of args, rather than the
1084 * argv array. Used by the environment variable and response file
1085 * processing.
1087 static void process_args(char *args, int pass)
1089 char *p, *q, *arg, *prevarg;
1090 char separator = ' ';
1092 p = args;
1093 if (*p && *p != '-')
1094 separator = *p++;
1095 arg = NULL;
1096 while (*p) {
1097 q = p;
1098 while (*p && *p != separator)
1099 p++;
1100 while (*p == separator)
1101 *p++ = '\0';
1102 prevarg = arg;
1103 arg = q;
1104 if (process_arg(prevarg, arg, pass))
1105 arg = NULL;
1107 if (arg)
1108 process_arg(arg, NULL, pass);
1111 static void process_response_file(const char *file, int pass)
1113 char str[2048];
1114 FILE *f = nasm_open_read(file, NF_TEXT);
1115 if (!f) {
1116 perror(file);
1117 exit(-1);
1119 while (fgets(str, sizeof str, f)) {
1120 process_args(str, pass);
1122 fclose(f);
1125 static void parse_cmdline(int argc, char **argv, int pass)
1127 FILE *rfile;
1128 char *envreal, *envcopy = NULL, *p;
1129 int i;
1131 *inname = *outname = *listname = *errname = '\0';
1133 for (i = 0; i <= ERR_WARN_MAX; i++)
1134 warning_on_global[i] = warnings[i].enabled;
1137 * First, process the NASMENV environment variable.
1139 envreal = getenv("NASMENV");
1140 if (envreal) {
1141 envcopy = nasm_strdup(envreal);
1142 process_args(envcopy, pass);
1143 nasm_free(envcopy);
1147 * Now process the actual command line.
1149 while (--argc) {
1150 bool advance;
1151 argv++;
1152 if (argv[0][0] == '@') {
1154 * We have a response file, so process this as a set of
1155 * arguments like the environment variable. This allows us
1156 * to have multiple arguments on a single line, which is
1157 * different to the -@resp file processing below for regular
1158 * NASM.
1160 process_response_file(argv[0]+1, pass);
1161 argc--;
1162 argv++;
1164 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
1165 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
1166 if (p) {
1167 rfile = nasm_open_read(p, NF_TEXT);
1168 if (rfile) {
1169 process_respfile(rfile, pass);
1170 fclose(rfile);
1171 } else
1172 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1173 "unable to open response file `%s'", p);
1175 } else
1176 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
1177 argv += advance, argc -= advance;
1181 * Look for basic command line typos. This definitely doesn't
1182 * catch all errors, but it might help cases of fumbled fingers.
1184 if (pass != 2)
1185 return;
1187 if (!*inname)
1188 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1189 "no input file specified");
1190 else if (!strcmp(inname, errname) ||
1191 !strcmp(inname, outname) ||
1192 !strcmp(inname, listname) ||
1193 (depend_file && !strcmp(inname, depend_file)))
1194 nasm_fatal(ERR_NOFILE | ERR_USAGE,
1195 "file `%s' is both input and output file",
1196 inname);
1198 if (*errname) {
1199 error_file = nasm_open_write(errname, NF_TEXT);
1200 if (!error_file) {
1201 error_file = stderr; /* Revert to default! */
1202 nasm_fatal(ERR_NOFILE | ERR_USAGE,
1203 "cannot open file `%s' for error messages",
1204 errname);
1209 static void assemble_file(char *fname, StrList **depend_ptr)
1211 char *line;
1212 insn output_ins;
1213 int i;
1214 int64_t offs;
1215 int pass_max;
1216 int sb;
1217 uint64_t prev_offset_changed;
1218 unsigned int stall_count = 0; /* Make sure we make forward progress... */
1220 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
1221 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1223 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
1224 for (passn = 1; pass0 <= 2; passn++) {
1225 ldfunc def_label;
1227 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
1228 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1229 /* pass0 0, 0, 0, ..., 1, 2 */
1231 def_label = passn > 1 ? redefine_label : define_label;
1233 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
1234 cpu = cmd_cpu;
1235 if (pass0 == 2) {
1236 lfmt->init(listname);
1238 in_absolute = false;
1239 global_offset_changed = 0; /* set by redefine_label */
1240 location.segment = ofmt->section(NULL, pass2, &sb);
1241 globalbits = sb;
1242 if (passn > 1) {
1243 saa_rewind(forwrefs);
1244 forwref = saa_rstruct(forwrefs);
1245 raa_free(offsets);
1246 offsets = raa_init();
1248 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
1249 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
1251 globallineno = 0;
1252 if (passn == 1)
1253 location.known = true;
1254 location.offset = offs = get_curr_offs();
1256 while ((line = preproc->getline())) {
1257 globallineno++;
1260 * Here we parse our directives; this is not handled by the
1261 * main parser.
1263 if (process_directives(line) != D_none)
1264 goto end_of_line; /* Just do final cleanup */
1266 /* Not a directive, or even something that starts with [ */
1268 parse_line(pass1, line, &output_ins, def_label);
1270 if (optimizing > 0) {
1271 if (forwref != NULL && globallineno == forwref->lineno) {
1272 output_ins.forw_ref = true;
1273 do {
1274 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1275 forwref = saa_rstruct(forwrefs);
1276 } while (forwref != NULL
1277 && forwref->lineno == globallineno);
1278 } else
1279 output_ins.forw_ref = false;
1281 if (output_ins.forw_ref) {
1282 if (passn == 1) {
1283 for (i = 0; i < output_ins.operands; i++) {
1284 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1285 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1286 fwinf->lineno = globallineno;
1287 fwinf->operand = i;
1294 /* forw_ref */
1295 if (output_ins.opcode == I_EQU) {
1296 if (pass1 == 1) {
1298 * Special `..' EQUs get processed in pass two,
1299 * except `..@' macro-processor EQUs which are done
1300 * in the normal place.
1302 if (!output_ins.label)
1303 nasm_error(ERR_NONFATAL,
1304 "EQU not preceded by label");
1306 else if (output_ins.label[0] != '.' ||
1307 output_ins.label[1] != '.' ||
1308 output_ins.label[2] == '@') {
1309 if (output_ins.operands == 1 &&
1310 (output_ins.oprs[0].type & IMMEDIATE) &&
1311 output_ins.oprs[0].wrt == NO_SEG) {
1312 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1313 def_label(output_ins.label,
1314 output_ins.oprs[0].segment,
1315 output_ins.oprs[0].offset, NULL,
1316 false, isext);
1317 } else if (output_ins.operands == 2
1318 && (output_ins.oprs[0].type & IMMEDIATE)
1319 && (output_ins.oprs[0].type & COLON)
1320 && output_ins.oprs[0].segment == NO_SEG
1321 && output_ins.oprs[0].wrt == NO_SEG
1322 && (output_ins.oprs[1].type & IMMEDIATE)
1323 && output_ins.oprs[1].segment == NO_SEG
1324 && output_ins.oprs[1].wrt == NO_SEG) {
1325 def_label(output_ins.label,
1326 output_ins.oprs[0].offset | SEG_ABS,
1327 output_ins.oprs[1].offset,
1328 NULL, false, false);
1329 } else
1330 nasm_error(ERR_NONFATAL,
1331 "bad syntax for EQU");
1333 } else {
1335 * Special `..' EQUs get processed here, except
1336 * `..@' macro processor EQUs which are done above.
1338 if (output_ins.label[0] == '.' &&
1339 output_ins.label[1] == '.' &&
1340 output_ins.label[2] != '@') {
1341 if (output_ins.operands == 1 &&
1342 (output_ins.oprs[0].type & IMMEDIATE)) {
1343 define_label(output_ins.label,
1344 output_ins.oprs[0].segment,
1345 output_ins.oprs[0].offset,
1346 NULL, false, false);
1347 } else if (output_ins.operands == 2
1348 && (output_ins.oprs[0].type & IMMEDIATE)
1349 && (output_ins.oprs[0].type & COLON)
1350 && output_ins.oprs[0].segment == NO_SEG
1351 && (output_ins.oprs[1].type & IMMEDIATE)
1352 && output_ins.oprs[1].segment == NO_SEG) {
1353 define_label(output_ins.label,
1354 output_ins.oprs[0].offset | SEG_ABS,
1355 output_ins.oprs[1].offset,
1356 NULL, false, false);
1357 } else
1358 nasm_error(ERR_NONFATAL,
1359 "bad syntax for EQU");
1362 } else { /* instruction isn't an EQU */
1364 if (pass1 == 1) {
1365 int64_t l = insn_size(location.segment, offs, sb,
1366 &output_ins);
1367 l *= output_ins.times;
1369 /* if (using_debug_info) && output_ins.opcode != -1) */
1370 if (using_debug_info)
1371 { /* fbk 03/25/01 */
1372 /* this is done here so we can do debug type info */
1373 int32_t typeinfo =
1374 TYS_ELEMENTS(output_ins.operands);
1375 switch (output_ins.opcode) {
1376 case I_RESB:
1377 typeinfo =
1378 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1379 break;
1380 case I_RESW:
1381 typeinfo =
1382 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1383 break;
1384 case I_RESD:
1385 typeinfo =
1386 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1387 break;
1388 case I_RESQ:
1389 typeinfo =
1390 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1391 break;
1392 case I_REST:
1393 typeinfo =
1394 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1395 break;
1396 case I_RESO:
1397 typeinfo =
1398 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1399 break;
1400 case I_RESY:
1401 typeinfo =
1402 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1403 break;
1404 case I_DB:
1405 typeinfo |= TY_BYTE;
1406 break;
1407 case I_DW:
1408 typeinfo |= TY_WORD;
1409 break;
1410 case I_DD:
1411 if (output_ins.eops_float)
1412 typeinfo |= TY_FLOAT;
1413 else
1414 typeinfo |= TY_DWORD;
1415 break;
1416 case I_DQ:
1417 typeinfo |= TY_QWORD;
1418 break;
1419 case I_DT:
1420 typeinfo |= TY_TBYTE;
1421 break;
1422 case I_DO:
1423 typeinfo |= TY_OWORD;
1424 break;
1425 case I_DY:
1426 typeinfo |= TY_YWORD;
1427 break;
1428 default:
1429 typeinfo = TY_LABEL;
1433 dfmt->debug_typevalue(typeinfo);
1435 if (l != -1) {
1436 offs += l;
1437 set_curr_offs(offs);
1440 * else l == -1 => invalid instruction, which will be
1441 * flagged as an error on pass 2
1444 } else {
1445 offs += assemble(location.segment, offs, sb, &output_ins);
1446 set_curr_offs(offs);
1449 } /* not an EQU */
1450 cleanup_insn(&output_ins);
1452 end_of_line:
1453 nasm_free(line);
1454 location.offset = offs = get_curr_offs();
1455 } /* end while (line = preproc->getline... */
1457 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
1458 nasm_error(ERR_NONFATAL,
1459 "phase error detected at end of assembly.");
1461 if (pass1 == 1)
1462 preproc->cleanup(1);
1464 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
1465 pass0++;
1466 } else if (global_offset_changed &&
1467 global_offset_changed < prev_offset_changed) {
1468 prev_offset_changed = global_offset_changed;
1469 stall_count = 0;
1470 } else {
1471 stall_count++;
1474 if (terminate_after_phase)
1475 break;
1477 if ((stall_count > 997U) || (passn >= pass_max)) {
1478 /* We get here if the labels don't converge
1479 * Example: FOO equ FOO + 1
1481 nasm_error(ERR_NONFATAL,
1482 "Can't find valid values for all labels "
1483 "after %d passes, giving up.", passn);
1484 nasm_error(ERR_NONFATAL,
1485 "Possible causes: recursive EQUs, macro abuse.");
1486 break;
1490 preproc->cleanup(0);
1491 lfmt->cleanup();
1492 if (!terminate_after_phase && opt_verbose_info) {
1493 /* -On and -Ov switches */
1494 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1499 * gnu style error reporting
1500 * This function prints an error message to error_file in the
1501 * style used by GNU. An example would be:
1502 * file.asm:50: error: blah blah blah
1503 * where file.asm is the name of the file, 50 is the line number on
1504 * which the error occurs (or is detected) and "error:" is one of
1505 * the possible optional diagnostics -- it can be "error" or "warning"
1506 * or something else. Finally the line terminates with the actual
1507 * error message.
1509 * @param severity the severity of the warning or error
1510 * @param fmt the printf style format string
1512 static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
1514 const char *currentfile = NULL;
1515 int32_t lineno = 0;
1517 if (is_suppressed_warning(severity))
1518 return;
1520 if (!(severity & ERR_NOFILE))
1521 src_get(&lineno, &currentfile);
1523 if (!skip_this_pass(severity)) {
1524 if (currentfile) {
1525 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1526 } else {
1527 fputs("nasm: ", error_file);
1531 nasm_verror_common(severity, fmt, ap);
1535 * MS style error reporting
1536 * This function prints an error message to error_file in the
1537 * style used by Visual C and some other Microsoft tools. An example
1538 * would be:
1539 * file.asm(50) : error: blah blah blah
1540 * where file.asm is the name of the file, 50 is the line number on
1541 * which the error occurs (or is detected) and "error:" is one of
1542 * the possible optional diagnostics -- it can be "error" or "warning"
1543 * or something else. Finally the line terminates with the actual
1544 * error message.
1546 * @param severity the severity of the warning or error
1547 * @param fmt the printf style format string
1549 static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
1551 const char *currentfile = NULL;
1552 int32_t lineno = 0;
1554 if (is_suppressed_warning(severity))
1555 return;
1557 if (!(severity & ERR_NOFILE))
1558 src_get(&lineno, &currentfile);
1560 if (!skip_this_pass(severity)) {
1561 if (currentfile) {
1562 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1563 } else {
1564 fputs("nasm: ", error_file);
1568 nasm_verror_common(severity, fmt, ap);
1572 * check to see if this is a suppressable warning
1574 static inline bool is_suppressable_warning(int severity)
1576 int index;
1578 /* Not a warning at all */
1579 if ((severity & ERR_MASK) != ERR_WARNING)
1580 return false;
1582 index = WARN_IDX(severity);
1584 return index && index <= ERR_WARN_MAX;
1588 * check for suppressed warning
1589 * checks for suppressed warning or pass one only warning and we're
1590 * not in pass 1
1592 * @param severity the severity of the warning or error
1593 * @return true if we should abort error/warning printing
1595 static bool is_suppressed_warning(int severity)
1597 /* Might be a warning but suppresed explicitly */
1598 if (is_suppressable_warning(severity))
1599 return !warning_on[WARN_IDX(severity)];
1600 else
1601 return false;
1604 static bool skip_this_pass(int severity)
1606 /* See if it's a pass-specific warning which should be skipped. */
1608 if ((severity & ERR_MASK) > ERR_WARNING)
1609 return false;
1612 * passn is 1 on the very first pass only.
1613 * pass0 is 2 on the code-generation (final) pass only.
1614 * These are the passes we care about in this case.
1616 return (((severity & ERR_PASS1) && passn != 1) ||
1617 ((severity & ERR_PASS2) && pass0 != 2));
1621 * common error reporting
1622 * This is the common back end of the error reporting schemes currently
1623 * implemented. It prints the nature of the warning and then the
1624 * specific error message to error_file and may or may not return. It
1625 * doesn't return if the error severity is a "panic" or "debug" type.
1627 * @param severity the severity of the warning or error
1628 * @param fmt the printf style format string
1630 static void nasm_verror_common(int severity, const char *fmt, va_list args)
1632 char msg[1024];
1633 const char *pfx;
1635 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
1636 case ERR_WARNING:
1637 pfx = "warning: ";
1638 break;
1639 case ERR_NONFATAL:
1640 pfx = "error: ";
1641 break;
1642 case ERR_FATAL:
1643 pfx = "fatal: ";
1644 break;
1645 case ERR_PANIC:
1646 pfx = "panic: ";
1647 break;
1648 case ERR_DEBUG:
1649 pfx = "debug: ";
1650 break;
1651 default:
1652 pfx = "";
1653 break;
1656 vsnprintf(msg, sizeof msg - 64, fmt, args);
1657 if (is_suppressable_warning(severity)) {
1658 char *p = strchr(msg, '\0');
1659 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1662 if (!skip_this_pass(severity))
1663 fprintf(error_file, "%s%s\n", pfx, msg);
1665 /* Are we recursing from error_list_macros? */
1666 if (severity & ERR_PP_LISTMACRO)
1667 return;
1670 * Don't suppress this with skip_this_pass(), or we don't get
1671 * pass1 or preprocessor warnings in the list file
1673 lfmt->error(severity, pfx, msg);
1675 if (severity & ERR_USAGE)
1676 want_usage = true;
1678 preproc->error_list_macros(severity);
1680 switch (severity & ERR_MASK) {
1681 case ERR_DEBUG:
1682 /* no further action, by definition */
1683 break;
1684 case ERR_WARNING:
1685 /* Treat warnings as errors */
1686 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
1687 terminate_after_phase = true;
1688 break;
1689 case ERR_NONFATAL:
1690 terminate_after_phase = true;
1691 break;
1692 case ERR_FATAL:
1693 if (ofile) {
1694 fclose(ofile);
1695 remove(outname);
1696 ofile = NULL;
1698 if (want_usage)
1699 usage();
1700 exit(1); /* instantly die */
1701 break; /* placate silly compilers */
1702 case ERR_PANIC:
1703 fflush(NULL);
1704 /* abort(); */ /* halt, catch fire, and dump core */
1705 if (ofile) {
1706 fclose(ofile);
1707 remove(outname);
1708 ofile = NULL;
1710 exit(3);
1711 break;
1715 static void usage(void)
1717 fputs("type `nasm -h' for help\n", error_file);