BR 3392421: consider mode decorators in instruction matching
[nasm/externdefs2.git] / asm / nasm.c
blob60cd6ea58b859e30a87f00bc3160ad4ef33ff18e
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>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "error.h"
50 #include "saa.h"
51 #include "raa.h"
52 #include "float.h"
53 #include "stdscan.h"
54 #include "insns.h"
55 #include "preproc.h"
56 #include "parser.h"
57 #include "eval.h"
58 #include "assemble.h"
59 #include "labels.h"
60 #include "outform.h"
61 #include "listing.h"
62 #include "iflag.h"
63 #include "ver.h"
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
70 #define MAX_OPTIMIZE (INT_MAX >> 1)
72 struct forwrefinfo { /* info held on forward refs. */
73 int lineno;
74 int operand;
77 static void parse_cmdline(int, char **, int);
78 static void assemble_file(char *, StrList **);
79 static bool is_suppressed_warning(int severity);
80 static bool skip_this_pass(int severity);
81 static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82 static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83 static void nasm_verror_common(int severity, const char *fmt, va_list args);
84 static void usage(void);
86 static bool using_debug_info, opt_verbose_info;
87 static const char *debug_format;
89 bool tasm_compatible_mode = false;
90 int pass0, passn;
91 static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
92 int globalrel = 0;
93 int globalbnd = 0;
95 struct compile_time official_compile_time;
97 static char inname[FILENAME_MAX];
98 static char outname[FILENAME_MAX];
99 static char listname[FILENAME_MAX];
100 static char errname[FILENAME_MAX];
101 static int globallineno; /* for forward-reference tracking */
102 /* static int pass = 0; */
103 const struct ofmt *ofmt = &OF_DEFAULT;
104 const struct ofmt_alias *ofmt_alias = NULL;
105 const struct dfmt *dfmt;
107 static FILE *error_file; /* Where to write error messages */
109 FILE *ofile = NULL;
110 int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
111 static int cmd_sb = 16; /* by default */
113 iflag_t cpu;
114 static iflag_t cmd_cpu;
116 struct location location;
117 bool in_absolute; /* Flag we are in ABSOLUTE seg */
118 struct location absolute; /* Segment/offset inside ABSOLUTE */
120 static struct RAA *offsets;
122 static struct SAA *forwrefs; /* keep track of forward references */
123 static const struct forwrefinfo *forwref;
125 static const struct preproc_ops *preproc;
127 #define OP_NORMAL (1u << 0)
128 #define OP_PREPROCESS (1u << 1)
129 #define OP_DEPEND (1u << 2)
131 static unsigned int operating_mode;
133 /* Dependency flags */
134 static bool depend_emit_phony = false;
135 static bool depend_missing_ok = false;
136 static const char *depend_target = NULL;
137 static const char *depend_file = NULL;
138 StrList *depend_list;
140 static bool want_usage;
141 static bool terminate_after_phase;
142 bool user_nolist = false;
144 static char *quote_for_pmake(const char *str);
145 static char *quote_for_wmake(const char *str);
146 static char *(*quote_for_make)(const char *) = quote_for_pmake;
148 static int64_t get_curr_offs(void)
150 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
153 static void set_curr_offs(int64_t l_off)
155 if (in_absolute)
156 absolute.offset = l_off;
157 else
158 offsets = raa_write(offsets, location.segment, l_off);
161 static void nasm_fputs(const char *line, FILE * outfile)
163 if (outfile) {
164 fputs(line, outfile);
165 putc('\n', outfile);
166 } else
167 puts(line);
170 static void define_macros_early(void)
172 const struct compile_time * const oct = &official_compile_time;
173 char temp[128];
175 if (oct->have_local) {
176 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
177 preproc->pre_define(temp);
178 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
179 preproc->pre_define(temp);
180 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
181 preproc->pre_define(temp);
182 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
183 preproc->pre_define(temp);
186 if (oct->have_gm) {
187 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
188 preproc->pre_define(temp);
189 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
190 preproc->pre_define(temp);
191 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
192 preproc->pre_define(temp);
193 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
194 preproc->pre_define(temp);
197 if (oct->have_posix) {
198 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
199 preproc->pre_define(temp);
203 static void define_macros_late(void)
205 char temp[128];
208 * In case if output format is defined by alias
209 * we have to put shortname of the alias itself here
210 * otherwise ABI backward compatibility gets broken.
212 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
213 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
214 preproc->pre_define(temp);
217 static void emit_dependencies(StrList *list)
219 FILE *deps;
220 int linepos, len;
221 StrList *l, *nl;
222 char wrapstr[] = " \\\n ";
224 wrapstr[1] = (quote_for_make == quote_for_wmake) ? '&' : '\\';
226 if (depend_file && strcmp(depend_file, "-")) {
227 deps = nasm_open_write(depend_file, NF_TEXT);
228 if (!deps) {
229 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
230 "unable to write dependency file `%s'", depend_file);
231 return;
233 } else {
234 deps = stdout;
237 linepos = fprintf(deps, "%s :", depend_target);
238 list_for_each(l, list) {
239 char *file = quote_for_make(l->str);
240 len = strlen(file);
241 if (linepos + len > 62 && linepos > 1) {
242 fwrite(wrapstr, 1, sizeof wrapstr-1, deps);
243 linepos = 1;
245 fprintf(deps, " %s", file);
246 linepos += len+1;
247 nasm_free(file);
249 fprintf(deps, "\n\n");
251 list_for_each_safe(l, nl, list) {
252 if (depend_emit_phony) {
253 char *file = quote_for_make(l->str);
254 fprintf(deps, "%s :\n\n", file);
255 nasm_free(file);
257 nasm_free(l);
260 if (deps != stdout)
261 fclose(deps);
264 /* Convert a struct tm to a POSIX-style time constant */
265 static int64_t make_posix_time(const struct tm *tm)
267 int64_t t;
268 int64_t y = tm->tm_year;
270 /* See IEEE 1003.1:2004, section 4.14 */
272 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
273 t += tm->tm_yday;
274 t *= 24;
275 t += tm->tm_hour;
276 t *= 60;
277 t += tm->tm_min;
278 t *= 60;
279 t += tm->tm_sec;
281 return t;
284 static void timestamp(void)
286 struct compile_time * const oct = &official_compile_time;
287 const struct tm *tp, *best_gm;
289 time(&oct->t);
291 best_gm = NULL;
293 tp = localtime(&oct->t);
294 if (tp) {
295 oct->local = *tp;
296 best_gm = &oct->local;
297 oct->have_local = true;
300 tp = gmtime(&oct->t);
301 if (tp) {
302 oct->gm = *tp;
303 best_gm = &oct->gm;
304 oct->have_gm = true;
305 if (!oct->have_local)
306 oct->local = oct->gm;
307 } else {
308 oct->gm = oct->local;
311 if (best_gm) {
312 oct->posix = make_posix_time(best_gm);
313 oct->have_posix = true;
317 int main(int argc, char **argv)
319 StrList **depend_ptr;
321 timestamp();
323 iflag_set(&cpu, IF_PLEVEL);
324 iflag_set(&cmd_cpu, IF_PLEVEL);
326 pass0 = 0;
327 want_usage = terminate_after_phase = false;
328 nasm_set_verror(nasm_verror_gnu);
330 error_file = stderr;
332 tolower_init();
333 src_init();
335 offsets = raa_init();
336 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
338 preproc = &nasmpp;
339 operating_mode = OP_NORMAL;
341 parse_cmdline(argc, argv, 1);
342 if (terminate_after_phase) {
343 if (want_usage)
344 usage();
345 return 1;
349 * Define some macros dependent on the runtime, but not
350 * on the command line (as those are scanned in cmdline pass 2.)
352 preproc->init();
353 define_macros_early();
355 parse_cmdline(argc, argv, 2);
356 if (terminate_after_phase) {
357 if (want_usage)
358 usage();
359 return 1;
362 /* Save away the default state of warnings */
363 memcpy(warning_state_init, warning_state, sizeof warning_state);
365 if (!using_debug_info) {
366 /* No debug info, redirect to the null backend (empty stubs) */
367 dfmt = &null_debug_form;
368 } else if (!debug_format) {
369 /* Default debug format for this backend */
370 dfmt = ofmt->default_dfmt;
371 } else {
372 dfmt = dfmt_find(ofmt, debug_format);
373 if (!dfmt) {
374 nasm_fatal(ERR_NOFILE | ERR_USAGE,
375 "unrecognized debug format `%s' for"
376 " output format `%s'",
377 debug_format, ofmt->shortname);
381 if (ofmt->stdmac)
382 preproc->extra_stdmac(ofmt->stdmac);
384 /* define some macros dependent of command-line */
385 define_macros_late();
387 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
389 if (!depend_target)
390 depend_target = quote_for_make(outname);
392 if (operating_mode & OP_DEPEND) {
393 char *line;
395 if (depend_missing_ok)
396 preproc->include_path(NULL); /* "assume generated" */
398 preproc->reset(inname, 0, depend_ptr);
399 if (outname[0] == '\0')
400 ofmt->filename(inname, outname);
401 ofile = NULL;
402 while ((line = preproc->getline()))
403 nasm_free(line);
404 preproc->cleanup(0);
405 } else if (operating_mode & OP_PREPROCESS) {
406 char *line;
407 const char *file_name = NULL;
408 int32_t prior_linnum = 0;
409 int lineinc = 0;
411 if (*outname) {
412 ofile = nasm_open_write(outname, NF_TEXT);
413 if (!ofile)
414 nasm_fatal(ERR_NOFILE,
415 "unable to open output file `%s'",
416 outname);
417 } else
418 ofile = NULL;
420 location.known = false;
422 /* pass = 1; */
423 preproc->reset(inname, 3, depend_ptr);
425 /* Revert all warnings to the default state */
426 memcpy(warning_state, warning_state_init, sizeof warning_state);
428 while ((line = preproc->getline())) {
430 * We generate %line directives if needed for later programs
432 int32_t linnum = prior_linnum += lineinc;
433 int altline = src_get(&linnum, &file_name);
434 if (altline) {
435 if (altline == 1 && lineinc == 1)
436 nasm_fputs("", ofile);
437 else {
438 lineinc = (altline != -1 || lineinc != 1);
439 fprintf(ofile ? ofile : stdout,
440 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
441 file_name);
443 prior_linnum = linnum;
445 nasm_fputs(line, ofile);
446 nasm_free(line);
448 preproc->cleanup(0);
449 if (ofile)
450 fclose(ofile);
451 if (ofile && terminate_after_phase)
452 remove(outname);
453 ofile = NULL;
456 if (operating_mode & OP_NORMAL) {
458 * We must call ofmt->filename _anyway_, even if the user
459 * has specified their own output file, because some
460 * formats (eg OBJ and COFF) use ofmt->filename to find out
461 * the name of the input file and then put that inside the
462 * file.
464 ofmt->filename(inname, outname);
466 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
467 if (!ofile)
468 nasm_fatal(ERR_NOFILE,
469 "unable to open output file `%s'", outname);
472 * We must call init_labels() before ofmt->init() since
473 * some object formats will want to define labels in their
474 * init routines. (eg OS/2 defines the FLAT group)
476 init_labels();
478 ofmt->init();
479 dfmt->init();
481 assemble_file(inname, depend_ptr);
483 if (!terminate_after_phase) {
484 ofmt->cleanup();
485 cleanup_labels();
486 fflush(ofile);
487 if (ferror(ofile)) {
488 nasm_error(ERR_NONFATAL|ERR_NOFILE,
489 "write error on output file `%s'", outname);
490 terminate_after_phase = true;
494 if (ofile) {
495 fclose(ofile);
496 if (terminate_after_phase)
497 remove(outname);
498 ofile = NULL;
502 if (depend_list && !terminate_after_phase)
503 emit_dependencies(depend_list);
505 if (want_usage)
506 usage();
508 raa_free(offsets);
509 saa_free(forwrefs);
510 eval_cleanup();
511 stdscan_cleanup();
512 src_free();
514 return terminate_after_phase;
518 * Get a parameter for a command line option.
519 * First arg must be in the form of e.g. -f...
521 static char *get_param(char *p, char *q, bool *advance)
523 *advance = false;
524 if (p[2]) /* the parameter's in the option */
525 return nasm_skip_spaces(p + 2);
526 if (q && q[0]) {
527 *advance = true;
528 return q;
530 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
531 "option `-%c' requires an argument", p[1]);
532 return NULL;
536 * Copy a filename
538 static void copy_filename(char *dst, const char *src)
540 size_t len = strlen(src);
542 if (len >= (size_t)FILENAME_MAX) {
543 nasm_fatal(ERR_NOFILE, "file name too long");
544 return;
546 strncpy(dst, src, FILENAME_MAX);
550 * Convert a string to a POSIX make-safe form
552 static char *quote_for_pmake(const char *str)
554 const char *p;
555 char *os, *q;
557 size_t n = 1; /* Terminating zero */
558 size_t nbs = 0;
560 if (!str)
561 return NULL;
563 for (p = str; *p; p++) {
564 switch (*p) {
565 case ' ':
566 case '\t':
567 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
568 n += nbs + 2;
569 nbs = 0;
570 break;
571 case '$':
572 case '#':
573 nbs = 0;
574 n += 2;
575 break;
576 case '\\':
577 nbs++;
578 n++;
579 break;
580 default:
581 nbs = 0;
582 n++;
583 break;
587 /* Convert N backslashes at the end of filename to 2N backslashes */
588 if (nbs)
589 n += nbs;
591 os = q = nasm_malloc(n);
593 nbs = 0;
594 for (p = str; *p; p++) {
595 switch (*p) {
596 case ' ':
597 case '\t':
598 while (nbs--)
599 *q++ = '\\';
600 *q++ = '\\';
601 *q++ = *p;
602 break;
603 case '$':
604 *q++ = *p;
605 *q++ = *p;
606 nbs = 0;
607 break;
608 case '#':
609 *q++ = '\\';
610 *q++ = *p;
611 nbs = 0;
612 break;
613 case '\\':
614 *q++ = *p;
615 nbs++;
616 break;
617 default:
618 *q++ = *p;
619 nbs = 0;
620 break;
623 while (nbs--)
624 *q++ = '\\';
626 *q = '\0';
628 return os;
632 * Convert a string to a Watcom make-safe form
634 static char *quote_for_wmake(const char *str)
636 const char *p;
637 char *os, *q;
639 size_t n = 1; /* Terminating zero */
641 if (!str)
642 return NULL;
644 for (p = str; *p; p++) {
645 switch (*p) {
646 case '$':
647 case '#':
648 n += 2;
649 break;
650 default:
651 n++;
652 break;
656 os = q = nasm_malloc(n);
658 for (p = str; *p; p++) {
659 switch (*p) {
660 case '$':
661 case '#':
662 *q++ = '$';
663 *q++ = *p;
664 break;
665 default:
666 *q++ = *p;
667 break;
671 *q = '\0';
673 return os;
676 struct textargs {
677 const char *label;
678 int value;
681 enum text_options {
682 OPT_PREFIX,
683 OPT_POSTFIX
685 static const struct textargs textopts[] = {
686 {"prefix", OPT_PREFIX},
687 {"postfix", OPT_POSTFIX},
688 {NULL, 0}
691 static void show_version(void)
693 printf("NASM version %s compiled on %s%s\n",
694 nasm_version, nasm_date, nasm_compile_options);
695 exit(0);
698 static bool stopoptions = false;
699 static bool process_arg(char *p, char *q, int pass)
701 char *param;
702 int i;
703 bool advance = false;
705 if (!p || !p[0])
706 return false;
708 if (p[0] == '-' && !stopoptions) {
709 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
710 /* These parameters take values */
711 if (!(param = get_param(p, q, &advance)))
712 return advance;
715 switch (p[1]) {
716 case 's':
717 if (pass == 1)
718 error_file = stdout;
719 break;
721 case 'o': /* output file */
722 if (pass == 2)
723 copy_filename(outname, param);
724 break;
726 case 'f': /* output format */
727 if (pass == 1) {
728 ofmt = ofmt_find(param, &ofmt_alias);
729 if (!ofmt) {
730 nasm_fatal(ERR_NOFILE | ERR_USAGE,
731 "unrecognised output format `%s' - "
732 "use -hf for a list", param);
735 break;
737 case 'O': /* Optimization level */
738 if (pass == 2) {
739 int opt;
741 if (!*param) {
742 /* Naked -O == -Ox */
743 optimizing = MAX_OPTIMIZE;
744 } else {
745 while (*param) {
746 switch (*param) {
747 case '0': case '1': case '2': case '3': case '4':
748 case '5': case '6': case '7': case '8': case '9':
749 opt = strtoul(param, &param, 10);
751 /* -O0 -> optimizing == -1, 0.98 behaviour */
752 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
753 if (opt < 2)
754 optimizing = opt - 1;
755 else
756 optimizing = opt;
757 break;
759 case 'v':
760 case '+':
761 param++;
762 opt_verbose_info = true;
763 break;
765 case 'x':
766 param++;
767 optimizing = MAX_OPTIMIZE;
768 break;
770 default:
771 nasm_fatal(0,
772 "unknown optimization option -O%c\n",
773 *param);
774 break;
777 if (optimizing > MAX_OPTIMIZE)
778 optimizing = MAX_OPTIMIZE;
781 break;
783 case 'p': /* pre-include */
784 case 'P':
785 if (pass == 2)
786 preproc->pre_include(param);
787 break;
789 case 'd': /* pre-define */
790 case 'D':
791 if (pass == 2)
792 preproc->pre_define(param);
793 break;
795 case 'u': /* un-define */
796 case 'U':
797 if (pass == 2)
798 preproc->pre_undefine(param);
799 break;
801 case 'i': /* include search path */
802 case 'I':
803 if (pass == 2)
804 preproc->include_path(param);
805 break;
807 case 'l': /* listing file */
808 if (pass == 2)
809 copy_filename(listname, param);
810 break;
812 case 'Z': /* error messages file */
813 if (pass == 1)
814 copy_filename(errname, param);
815 break;
817 case 'F': /* specify debug format */
818 if (pass == 2) {
819 using_debug_info = true;
820 debug_format = param;
822 break;
824 case 'X': /* specify error reporting format */
825 if (pass == 1) {
826 if (nasm_stricmp("vc", param) == 0)
827 nasm_set_verror(nasm_verror_vc);
828 else if (nasm_stricmp("gnu", param) == 0)
829 nasm_set_verror(nasm_verror_gnu);
830 else
831 nasm_fatal(ERR_NOFILE | ERR_USAGE,
832 "unrecognized error reporting format `%s'",
833 param);
835 break;
837 case 'g':
838 if (pass == 2) {
839 using_debug_info = true;
840 if (p[2])
841 debug_format = nasm_skip_spaces(p + 2);
843 break;
845 case 'h':
846 printf
847 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
848 "[-l listfile]\n"
849 " [options...] [--] filename\n"
850 " or nasm -v (or --v) for version info\n\n"
851 " -t assemble in SciTech TASM compatible mode\n");
852 printf
853 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
854 " -a don't preprocess (assemble only)\n"
855 " -M generate Makefile dependencies on stdout\n"
856 " -MG d:o, missing files assumed generated\n"
857 " -MF <file> set Makefile dependency file\n"
858 " -MD <file> assemble and generate dependencies\n"
859 " -MT <file> dependency target name\n"
860 " -MQ <file> dependency target name (quoted)\n"
861 " -MP emit phony target\n\n"
862 " -Z<file> redirect error messages to file\n"
863 " -s redirect error messages to stdout\n\n"
864 " -g generate debugging information\n\n"
865 " -F format select a debugging format\n\n"
866 " -gformat same as -g -F format\n\n"
867 " -o outfile write output to an outfile\n\n"
868 " -f format select an output format\n\n"
869 " -l listfile write listing to a listfile\n\n"
870 " -I<path> adds a pathname to the include file path\n");
871 printf
872 (" -O<digit> optimize branch offsets\n"
873 " -O0: No optimization\n"
874 " -O1: Minimal optimization\n"
875 " -Ox: Multipass optimization (default)\n\n"
876 " -P<file> pre-includes a file\n"
877 " -D<macro>[=<value>] pre-defines a macro\n"
878 " -U<macro> undefines a macro\n"
879 " -X<format> specifies error reporting format (gnu or vc)\n"
880 " -w+foo enables warning foo (equiv. -Wfoo)\n"
881 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
882 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
883 " -h show invocation summary and exit\n\n"
884 "--prefix,--postfix\n"
885 " these options prepend or append the given string\n"
886 " to all extern and global variables\n"
887 "\n"
888 "Response files should contain command line parameters,\n"
889 "one per line.\n"
890 "\n"
891 "Warnings for the -W/-w options:\n");
892 for (i = 0; i <= ERR_WARN_ALL; i++)
893 printf(" %-23s %s%s\n",
894 warnings[i].name, warnings[i].help,
895 i == ERR_WARN_ALL ? "\n" :
896 warnings[i].enabled ? " (default on)" :
897 " (default off)");
898 if (p[2] == 'f') {
899 printf("valid output formats for -f are"
900 " (`*' denotes default):\n");
901 ofmt_list(ofmt, stdout);
902 } else {
903 printf("For a list of valid output formats, use -hf.\n");
904 printf("For a list of debug formats, use -f <form> -y.\n");
906 exit(0); /* never need usage message here */
907 break;
909 case 'y':
910 printf("\nvalid debug formats for '%s' output format are"
911 " ('*' denotes default):\n", ofmt->shortname);
912 dfmt_list(ofmt, stdout);
913 exit(0);
914 break;
916 case 't':
917 if (pass == 2)
918 tasm_compatible_mode = true;
919 break;
921 case 'v':
922 show_version();
923 break;
925 case 'e': /* preprocess only */
926 case 'E':
927 if (pass == 1)
928 operating_mode = OP_PREPROCESS;
929 break;
931 case 'a': /* assemble only - don't preprocess */
932 if (pass == 1)
933 preproc = &preproc_nop;
934 break;
936 case 'w':
937 case 'W':
938 if (pass == 2) {
939 if (!set_warning_status(param)) {
940 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
941 "unknown warning option: %s", param);
944 break;
946 case 'M':
947 if (pass == 1) {
948 switch (p[2]) {
949 case 'W':
950 quote_for_make = quote_for_wmake;
951 break;
952 case 'D':
953 case 'F':
954 case 'T':
955 case 'Q':
956 advance = true;
957 break;
958 default:
959 break;
961 } else {
962 switch (p[2]) {
963 case 0:
964 operating_mode = OP_DEPEND;
965 break;
966 case 'G':
967 operating_mode = OP_DEPEND;
968 depend_missing_ok = true;
969 break;
970 case 'P':
971 depend_emit_phony = true;
972 break;
973 case 'D':
974 operating_mode = OP_NORMAL;
975 depend_file = q;
976 advance = true;
977 break;
978 case 'F':
979 depend_file = q;
980 advance = true;
981 break;
982 case 'T':
983 depend_target = q;
984 advance = true;
985 break;
986 case 'Q':
987 depend_target = quote_for_make(q);
988 advance = true;
989 break;
990 case 'W':
991 /* handled in pass 1 */
992 break;
993 default:
994 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
995 "unknown dependency option `-M%c'", p[2]);
996 break;
999 if (advance && (!q || !q[0])) {
1000 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1001 "option `-M%c' requires a parameter", p[2]);
1002 break;
1004 break;
1006 case '-':
1008 int s;
1010 if (p[2] == 0) { /* -- => stop processing options */
1011 stopoptions = 1;
1012 break;
1015 if (!nasm_stricmp(p, "--v"))
1016 show_version();
1018 if (!nasm_stricmp(p, "--version"))
1019 show_version();
1021 for (s = 0; textopts[s].label; s++) {
1022 if (!nasm_stricmp(p + 2, textopts[s].label)) {
1023 break;
1027 switch (s) {
1028 case OPT_PREFIX:
1029 case OPT_POSTFIX:
1031 if (!q) {
1032 nasm_error(ERR_NONFATAL | ERR_NOFILE |
1033 ERR_USAGE,
1034 "option `--%s' requires an argument",
1035 p + 2);
1036 break;
1037 } else {
1038 advance = 1, param = q;
1041 switch (s) {
1042 case OPT_PREFIX:
1043 if (pass == 2)
1044 strlcpy(lprefix, param, PREFIX_MAX);
1045 break;
1046 case OPT_POSTFIX:
1047 if (pass == 2)
1048 strlcpy(lpostfix, param, POSTFIX_MAX);
1049 break;
1050 default:
1051 panic();
1052 break;
1054 break;
1057 default:
1059 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1060 "unrecognised option `--%s'", p + 2);
1061 break;
1064 break;
1067 default:
1068 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1069 "unrecognised option `-%c'", p[1]);
1070 break;
1072 } else if (pass == 2) {
1073 if (*inname) {
1074 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1075 "more than one input file specified");
1076 } else {
1077 copy_filename(inname, p);
1081 return advance;
1084 #define ARG_BUF_DELTA 128
1086 static void process_respfile(FILE * rfile, int pass)
1088 char *buffer, *p, *q, *prevarg;
1089 int bufsize, prevargsize;
1091 bufsize = prevargsize = ARG_BUF_DELTA;
1092 buffer = nasm_malloc(ARG_BUF_DELTA);
1093 prevarg = nasm_malloc(ARG_BUF_DELTA);
1094 prevarg[0] = '\0';
1096 while (1) { /* Loop to handle all lines in file */
1097 p = buffer;
1098 while (1) { /* Loop to handle long lines */
1099 q = fgets(p, bufsize - (p - buffer), rfile);
1100 if (!q)
1101 break;
1102 p += strlen(p);
1103 if (p > buffer && p[-1] == '\n')
1104 break;
1105 if (p - buffer > bufsize - 10) {
1106 int offset;
1107 offset = p - buffer;
1108 bufsize += ARG_BUF_DELTA;
1109 buffer = nasm_realloc(buffer, bufsize);
1110 p = buffer + offset;
1114 if (!q && p == buffer) {
1115 if (prevarg[0])
1116 process_arg(prevarg, NULL, pass);
1117 nasm_free(buffer);
1118 nasm_free(prevarg);
1119 return;
1123 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1124 * them are present at the end of the line.
1126 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
1128 while (p > buffer && nasm_isspace(p[-1]))
1129 *--p = '\0';
1131 p = nasm_skip_spaces(buffer);
1133 if (process_arg(prevarg, p, pass))
1134 *p = '\0';
1136 if ((int) strlen(p) > prevargsize - 10) {
1137 prevargsize += ARG_BUF_DELTA;
1138 prevarg = nasm_realloc(prevarg, prevargsize);
1140 strncpy(prevarg, p, prevargsize);
1144 /* Function to process args from a string of args, rather than the
1145 * argv array. Used by the environment variable and response file
1146 * processing.
1148 static void process_args(char *args, int pass)
1150 char *p, *q, *arg, *prevarg;
1151 char separator = ' ';
1153 p = args;
1154 if (*p && *p != '-')
1155 separator = *p++;
1156 arg = NULL;
1157 while (*p) {
1158 q = p;
1159 while (*p && *p != separator)
1160 p++;
1161 while (*p == separator)
1162 *p++ = '\0';
1163 prevarg = arg;
1164 arg = q;
1165 if (process_arg(prevarg, arg, pass))
1166 arg = NULL;
1168 if (arg)
1169 process_arg(arg, NULL, pass);
1172 static void process_response_file(const char *file, int pass)
1174 char str[2048];
1175 FILE *f = nasm_open_read(file, NF_TEXT);
1176 if (!f) {
1177 perror(file);
1178 exit(-1);
1180 while (fgets(str, sizeof str, f)) {
1181 process_args(str, pass);
1183 fclose(f);
1186 static void parse_cmdline(int argc, char **argv, int pass)
1188 FILE *rfile;
1189 char *envreal, *envcopy = NULL, *p;
1190 int i;
1192 *inname = *outname = *listname = *errname = '\0';
1194 /* Initialize all the warnings to their default state */
1195 for (i = 0; i < ERR_WARN_ALL; i++) {
1196 warning_state_init[i] = warning_state[i] =
1197 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1201 * First, process the NASMENV environment variable.
1203 envreal = getenv("NASMENV");
1204 if (envreal) {
1205 envcopy = nasm_strdup(envreal);
1206 process_args(envcopy, pass);
1207 nasm_free(envcopy);
1211 * Now process the actual command line.
1213 while (--argc) {
1214 bool advance;
1215 argv++;
1216 if (argv[0][0] == '@') {
1218 * We have a response file, so process this as a set of
1219 * arguments like the environment variable. This allows us
1220 * to have multiple arguments on a single line, which is
1221 * different to the -@resp file processing below for regular
1222 * NASM.
1224 process_response_file(argv[0]+1, pass);
1225 argc--;
1226 argv++;
1228 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
1229 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
1230 if (p) {
1231 rfile = nasm_open_read(p, NF_TEXT);
1232 if (rfile) {
1233 process_respfile(rfile, pass);
1234 fclose(rfile);
1235 } else
1236 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1237 "unable to open response file `%s'", p);
1239 } else
1240 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
1241 argv += advance, argc -= advance;
1245 * Look for basic command line typos. This definitely doesn't
1246 * catch all errors, but it might help cases of fumbled fingers.
1248 if (pass != 2)
1249 return;
1251 if (!*inname)
1252 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1253 "no input file specified");
1254 else if (!strcmp(inname, errname) ||
1255 !strcmp(inname, outname) ||
1256 !strcmp(inname, listname) ||
1257 (depend_file && !strcmp(inname, depend_file)))
1258 nasm_fatal(ERR_NOFILE | ERR_USAGE,
1259 "file `%s' is both input and output file",
1260 inname);
1262 if (*errname) {
1263 error_file = nasm_open_write(errname, NF_TEXT);
1264 if (!error_file) {
1265 error_file = stderr; /* Revert to default! */
1266 nasm_fatal(ERR_NOFILE | ERR_USAGE,
1267 "cannot open file `%s' for error messages",
1268 errname);
1273 static void assemble_file(char *fname, StrList **depend_ptr)
1275 char *line;
1276 insn output_ins;
1277 int i;
1278 int64_t offs;
1279 int pass_max;
1280 uint64_t prev_offset_changed;
1281 unsigned int stall_count = 0; /* Make sure we make forward progress... */
1283 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
1284 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1286 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
1287 for (passn = 1; pass0 <= 2; passn++) {
1288 ldfunc def_label;
1290 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
1291 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1292 /* pass0 0, 0, 0, ..., 1, 2 */
1294 def_label = passn > 1 ? redefine_label : define_label;
1296 globalbits = cmd_sb; /* set 'bits' to command line default */
1297 cpu = cmd_cpu;
1298 if (pass0 == 2) {
1299 lfmt->init(listname);
1300 } else if (passn == 1 && *listname) {
1301 /* Remove the list file in case we die before the output pass */
1302 remove(listname);
1304 in_absolute = false;
1305 global_offset_changed = 0; /* set by redefine_label */
1306 location.segment = ofmt->section(NULL, pass2, &globalbits);
1307 if (passn > 1) {
1308 saa_rewind(forwrefs);
1309 forwref = saa_rstruct(forwrefs);
1310 raa_free(offsets);
1311 offsets = raa_init();
1313 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
1315 /* Revert all warnings to the default state */
1316 memcpy(warning_state, warning_state_init, sizeof warning_state);
1318 globallineno = 0;
1319 if (passn == 1)
1320 location.known = true;
1321 location.offset = offs = get_curr_offs();
1323 while ((line = preproc->getline())) {
1324 globallineno++;
1327 * Here we parse our directives; this is not handled by the
1328 * main parser.
1330 if (process_directives(line))
1331 goto end_of_line; /* Just do final cleanup */
1333 /* Not a directive, or even something that starts with [ */
1335 parse_line(pass1, line, &output_ins, def_label);
1337 if (optimizing > 0) {
1338 if (forwref != NULL && globallineno == forwref->lineno) {
1339 output_ins.forw_ref = true;
1340 do {
1341 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1342 forwref = saa_rstruct(forwrefs);
1343 } while (forwref != NULL
1344 && forwref->lineno == globallineno);
1345 } else
1346 output_ins.forw_ref = false;
1348 if (output_ins.forw_ref) {
1349 if (passn == 1) {
1350 for (i = 0; i < output_ins.operands; i++) {
1351 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1352 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1353 fwinf->lineno = globallineno;
1354 fwinf->operand = i;
1361 /* forw_ref */
1362 if (output_ins.opcode == I_EQU) {
1363 if (pass1 == 1) {
1365 * Special `..' EQUs get processed in pass two,
1366 * except `..@' macro-processor EQUs which are done
1367 * in the normal place.
1369 if (!output_ins.label)
1370 nasm_error(ERR_NONFATAL,
1371 "EQU not preceded by label");
1373 else if (output_ins.label[0] != '.' ||
1374 output_ins.label[1] != '.' ||
1375 output_ins.label[2] == '@') {
1376 if (output_ins.operands == 1 &&
1377 (output_ins.oprs[0].type & IMMEDIATE) &&
1378 output_ins.oprs[0].wrt == NO_SEG) {
1379 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1380 def_label(output_ins.label,
1381 output_ins.oprs[0].segment,
1382 output_ins.oprs[0].offset, NULL,
1383 false, isext);
1384 } else if (output_ins.operands == 2
1385 && (output_ins.oprs[0].type & IMMEDIATE)
1386 && (output_ins.oprs[0].type & COLON)
1387 && output_ins.oprs[0].segment == NO_SEG
1388 && output_ins.oprs[0].wrt == NO_SEG
1389 && (output_ins.oprs[1].type & IMMEDIATE)
1390 && output_ins.oprs[1].segment == NO_SEG
1391 && output_ins.oprs[1].wrt == NO_SEG) {
1392 def_label(output_ins.label,
1393 output_ins.oprs[0].offset | SEG_ABS,
1394 output_ins.oprs[1].offset,
1395 NULL, false, false);
1396 } else
1397 nasm_error(ERR_NONFATAL,
1398 "bad syntax for EQU");
1400 } else {
1402 * Special `..' EQUs get processed here, except
1403 * `..@' macro processor EQUs which are done above.
1405 if (output_ins.label[0] == '.' &&
1406 output_ins.label[1] == '.' &&
1407 output_ins.label[2] != '@') {
1408 if (output_ins.operands == 1 &&
1409 (output_ins.oprs[0].type & IMMEDIATE)) {
1410 define_label(output_ins.label,
1411 output_ins.oprs[0].segment,
1412 output_ins.oprs[0].offset,
1413 NULL, false, false);
1414 } else if (output_ins.operands == 2
1415 && (output_ins.oprs[0].type & IMMEDIATE)
1416 && (output_ins.oprs[0].type & COLON)
1417 && output_ins.oprs[0].segment == NO_SEG
1418 && (output_ins.oprs[1].type & IMMEDIATE)
1419 && output_ins.oprs[1].segment == NO_SEG) {
1420 define_label(output_ins.label,
1421 output_ins.oprs[0].offset | SEG_ABS,
1422 output_ins.oprs[1].offset,
1423 NULL, false, false);
1424 } else
1425 nasm_error(ERR_NONFATAL,
1426 "bad syntax for EQU");
1429 } else { /* instruction isn't an EQU */
1430 int32_t n;
1432 nasm_assert(output_ins.times >= 0);
1434 for (n = 1; n <= output_ins.times; n++) {
1435 if (pass1 == 1) {
1436 int64_t l = insn_size(location.segment, offs,
1437 globalbits, &output_ins);
1439 /* if (using_debug_info) && output_ins.opcode != -1) */
1440 if (using_debug_info)
1441 { /* fbk 03/25/01 */
1442 /* this is done here so we can do debug type info */
1443 int32_t typeinfo =
1444 TYS_ELEMENTS(output_ins.operands);
1445 switch (output_ins.opcode) {
1446 case I_RESB:
1447 typeinfo =
1448 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1449 break;
1450 case I_RESW:
1451 typeinfo =
1452 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1453 break;
1454 case I_RESD:
1455 typeinfo =
1456 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1457 break;
1458 case I_RESQ:
1459 typeinfo =
1460 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1461 break;
1462 case I_REST:
1463 typeinfo =
1464 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1465 break;
1466 case I_RESO:
1467 typeinfo =
1468 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1469 break;
1470 case I_RESY:
1471 typeinfo =
1472 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1473 break;
1474 case I_RESZ:
1475 typeinfo =
1476 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1477 break;
1478 case I_DB:
1479 typeinfo |= TY_BYTE;
1480 break;
1481 case I_DW:
1482 typeinfo |= TY_WORD;
1483 break;
1484 case I_DD:
1485 if (output_ins.eops_float)
1486 typeinfo |= TY_FLOAT;
1487 else
1488 typeinfo |= TY_DWORD;
1489 break;
1490 case I_DQ:
1491 typeinfo |= TY_QWORD;
1492 break;
1493 case I_DT:
1494 typeinfo |= TY_TBYTE;
1495 break;
1496 case I_DO:
1497 typeinfo |= TY_OWORD;
1498 break;
1499 case I_DY:
1500 typeinfo |= TY_YWORD;
1501 break;
1502 case I_DZ:
1503 typeinfo |= TY_ZWORD;
1504 break;
1505 default:
1506 typeinfo = TY_LABEL;
1507 break;
1510 dfmt->debug_typevalue(typeinfo);
1514 * For INCBIN, let the code in assemble
1515 * handle TIMES, so we don't have to read the
1516 * input file over and over.
1518 if (l != -1) {
1519 offs += l;
1520 set_curr_offs(offs);
1523 * else l == -1 => invalid instruction, which will be
1524 * flagged as an error on pass 2
1526 } else {
1527 if (n == 2)
1528 lfmt->uplevel(LIST_TIMES);
1529 offs += assemble(location.segment, offs,
1530 globalbits, &output_ins);
1531 set_curr_offs(offs);
1533 } /* not an EQU */
1535 if (output_ins.times > 1)
1536 lfmt->downlevel(LIST_TIMES);
1538 cleanup_insn(&output_ins);
1540 end_of_line:
1541 nasm_free(line);
1542 location.offset = offs = get_curr_offs();
1543 } /* end while (line = preproc->getline... */
1545 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
1546 nasm_error(ERR_NONFATAL,
1547 "phase error detected at end of assembly.");
1549 if (pass1 == 1)
1550 preproc->cleanup(1);
1552 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
1553 pass0++;
1554 } else if (global_offset_changed &&
1555 global_offset_changed < prev_offset_changed) {
1556 prev_offset_changed = global_offset_changed;
1557 stall_count = 0;
1558 } else {
1559 stall_count++;
1562 if (terminate_after_phase)
1563 break;
1565 if ((stall_count > 997U) || (passn >= pass_max)) {
1566 /* We get here if the labels don't converge
1567 * Example: FOO equ FOO + 1
1569 nasm_error(ERR_NONFATAL,
1570 "Can't find valid values for all labels "
1571 "after %d passes, giving up.", passn);
1572 nasm_error(ERR_NONFATAL,
1573 "Possible causes: recursive EQUs, macro abuse.");
1574 break;
1578 preproc->cleanup(0);
1579 lfmt->cleanup();
1580 if (!terminate_after_phase && opt_verbose_info) {
1581 /* -On and -Ov switches */
1582 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1587 * gnu style error reporting
1588 * This function prints an error message to error_file in the
1589 * style used by GNU. An example would be:
1590 * file.asm:50: error: blah blah blah
1591 * where file.asm is the name of the file, 50 is the line number on
1592 * which the error occurs (or is detected) and "error:" is one of
1593 * the possible optional diagnostics -- it can be "error" or "warning"
1594 * or something else. Finally the line terminates with the actual
1595 * error message.
1597 * @param severity the severity of the warning or error
1598 * @param fmt the printf style format string
1600 static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
1602 const char *currentfile = NULL;
1603 int32_t lineno = 0;
1605 if (is_suppressed_warning(severity))
1606 return;
1608 if (!(severity & ERR_NOFILE))
1609 src_get(&lineno, &currentfile);
1611 if (!skip_this_pass(severity)) {
1612 if (currentfile) {
1613 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1614 } else {
1615 fputs("nasm: ", error_file);
1619 nasm_verror_common(severity, fmt, ap);
1623 * MS style error reporting
1624 * This function prints an error message to error_file in the
1625 * style used by Visual C and some other Microsoft tools. An example
1626 * would be:
1627 * file.asm(50) : error: blah blah blah
1628 * where file.asm is the name of the file, 50 is the line number on
1629 * which the error occurs (or is detected) and "error:" is one of
1630 * the possible optional diagnostics -- it can be "error" or "warning"
1631 * or something else. Finally the line terminates with the actual
1632 * error message.
1634 * @param severity the severity of the warning or error
1635 * @param fmt the printf style format string
1637 static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
1639 const char *currentfile = NULL;
1640 int32_t lineno = 0;
1642 if (is_suppressed_warning(severity))
1643 return;
1645 if (!(severity & ERR_NOFILE))
1646 src_get(&lineno, &currentfile);
1648 if (!skip_this_pass(severity)) {
1649 if (currentfile) {
1650 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1651 } else {
1652 fputs("nasm: ", error_file);
1656 nasm_verror_common(severity, fmt, ap);
1660 * check to see if this is a suppressable warning
1662 static inline bool is_valid_warning(int severity)
1664 /* Not a warning at all */
1665 if ((severity & ERR_MASK) != ERR_WARNING)
1666 return false;
1668 return WARN_IDX(severity) < ERR_WARN_ALL;
1672 * check for suppressed warning
1673 * checks for suppressed warning or pass one only warning and we're
1674 * not in pass 1
1676 * @param severity the severity of the warning or error
1677 * @return true if we should abort error/warning printing
1679 static bool is_suppressed_warning(int severity)
1681 /* Might be a warning but suppresed explicitly */
1682 if (is_valid_warning(severity))
1683 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1684 else
1685 return false;
1688 static bool warning_is_error(int severity)
1690 if (is_valid_warning(severity))
1691 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
1692 else
1693 return false;
1696 static bool skip_this_pass(int severity)
1699 * See if it's a pass-specific error or warning which should be skipped.
1700 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1701 * they cannot be resumed from.
1703 if ((severity & ERR_MASK) > ERR_NONFATAL)
1704 return false;
1707 * passn is 1 on the very first pass only.
1708 * pass0 is 2 on the code-generation (final) pass only.
1709 * These are the passes we care about in this case.
1711 return (((severity & ERR_PASS1) && passn != 1) ||
1712 ((severity & ERR_PASS2) && pass0 != 2));
1716 * common error reporting
1717 * This is the common back end of the error reporting schemes currently
1718 * implemented. It prints the nature of the warning and then the
1719 * specific error message to error_file and may or may not return. It
1720 * doesn't return if the error severity is a "panic" or "debug" type.
1722 * @param severity the severity of the warning or error
1723 * @param fmt the printf style format string
1725 static void nasm_verror_common(int severity, const char *fmt, va_list args)
1727 char msg[1024];
1728 const char *pfx;
1730 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
1731 case ERR_WARNING:
1732 pfx = "warning: ";
1733 break;
1734 case ERR_NONFATAL:
1735 pfx = "error: ";
1736 break;
1737 case ERR_FATAL:
1738 pfx = "fatal: ";
1739 break;
1740 case ERR_PANIC:
1741 pfx = "panic: ";
1742 break;
1743 case ERR_DEBUG:
1744 pfx = "debug: ";
1745 break;
1746 default:
1747 pfx = "";
1748 break;
1751 vsnprintf(msg, sizeof msg - 64, fmt, args);
1752 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
1753 char *p = strchr(msg, '\0');
1754 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1757 if (!skip_this_pass(severity))
1758 fprintf(error_file, "%s%s\n", pfx, msg);
1760 /* Are we recursing from error_list_macros? */
1761 if (severity & ERR_PP_LISTMACRO)
1762 return;
1765 * Don't suppress this with skip_this_pass(), or we don't get
1766 * pass1 or preprocessor warnings in the list file
1768 lfmt->error(severity, pfx, msg);
1770 if (skip_this_pass(severity))
1771 return;
1773 if (severity & ERR_USAGE)
1774 want_usage = true;
1776 preproc->error_list_macros(severity);
1778 switch (severity & ERR_MASK) {
1779 case ERR_DEBUG:
1780 /* no further action, by definition */
1781 break;
1782 case ERR_WARNING:
1783 /* Treat warnings as errors */
1784 if (warning_is_error(severity))
1785 terminate_after_phase = true;
1786 break;
1787 case ERR_NONFATAL:
1788 terminate_after_phase = true;
1789 break;
1790 case ERR_FATAL:
1791 if (ofile) {
1792 fclose(ofile);
1793 remove(outname);
1794 ofile = NULL;
1796 if (want_usage)
1797 usage();
1798 exit(1); /* instantly die */
1799 break; /* placate silly compilers */
1800 case ERR_PANIC:
1801 fflush(NULL);
1802 /* abort(); */ /* halt, catch fire, and dump core */
1803 if (ofile) {
1804 fclose(ofile);
1805 remove(outname);
1806 ofile = NULL;
1808 exit(3);
1809 break;
1813 static void usage(void)
1815 fputs("type `nasm -h' for help\n", error_file);