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.
32 struct forwrefinfo { /* info held on forward refs. */
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,
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;
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;
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
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
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 = {
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)
157 fputs(line, outfile);
163 int main(int argc, char **argv)
166 want_usage = terminate_after_phase = false;
167 report_error = report_error_gnu;
171 nasm_set_malloc_error(report_error);
172 offsets = raa_init();
173 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
176 operating_mode = op_normal;
180 register_output_formats();
182 parse_cmdline(argc, argv);
184 if (terminate_after_phase) {
190 /* If debugging info is disabled, suppress any debug calls */
191 if (!using_debug_info)
192 ofmt->current_dfmt = &null_debug_form;
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 */
202 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
207 switch (operating_mode) {
208 case op_depend_missing_ok:
209 pp_include_path(NULL); /* "assume generated" */
214 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
215 if (outname[0] == '\0')
216 ofmt->filename(inname, outname, report_error);
218 fprintf(stdout, "%s: %s", outname, inname);
219 while ((line = preproc->getline()))
229 char *file_name = NULL;
230 int32_t prior_linnum = 0;
234 ofile = fopen(outname, "w");
236 report_error(ERR_FATAL | ERR_NOFILE,
237 "unable to open output file `%s'",
242 location.known = false;
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);
253 if (altline == 1 && lineinc == 1)
254 nasm_fputs("", ofile);
256 lineinc = (altline != -1 || lineinc != 1);
257 fprintf(ofile ? ofile : stdout,
258 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
261 prior_linnum = linnum;
263 nasm_fputs(line, ofile);
266 nasm_free(file_name);
270 if (ofile && terminate_after_phase)
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
284 ofmt->filename(inname, outname, report_error);
286 ofile = fopen(outname, "wb");
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)
299 ofmt->init(ofile, report_error, define_label, evaluate);
301 assemble_file(inname);
303 if (!terminate_after_phase) {
304 ofmt->cleanup(using_debug_info);
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.
329 if (terminate_after_phase)
336 * Get a parameter for a command line option.
337 * First arg must be in the form of e.g. -f...
339 static char *get_param(char *p, char *q, bool *advance)
342 if (p[2]) { /* the parameter's in the option */
352 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
353 "option `-%c' requires an argument", p[1]);
360 static void copy_filename(char *dst, const char *src)
362 size_t len = strlen(src);
364 if (len >= (size_t)FILENAME_MAX) {
365 report_error(ERR_FATAL | ERR_NOFILE, "file name too long");
368 strncpy(dst, src, FILENAME_MAX);
377 #define OPT_POSTFIX 1
378 struct textargs textopts[] = {
379 {"prefix", OPT_PREFIX},
380 {"postfix", OPT_POSTFIX},
384 static bool stopoptions = false;
385 static bool process_arg(char *p, char *q)
389 bool advance = false;
395 if (p[0] == '-' && !stopoptions) {
396 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
397 /* These parameters take values */
398 if (!(param = get_param(p, q, &advance)))
407 case 'o': /* output file */
408 copy_filename(outname, param);
411 case 'f': /* output format */
412 ofmt = ofmt_find(param);
414 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
415 "unrecognised output format `%s' - "
416 "use -hf for a list", param);
418 ofmt->current_dfmt = ofmt->debug_formats[0];
422 case 'O': /* Optimization level */
427 /* Naked -O == -Ox */
428 optimizing = INT_MAX >> 1; /* Almost unlimited */
432 case '0': case '1': case '2': case '3': case '4':
433 case '5': case '6': case '7': case '8': case '9':
434 opt = strtoul(param, ¶m, 10);
436 /* -O0 -> optimizing == -1, 0.98 behaviour */
437 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
439 optimizing = opt - 1;
441 /* The optimizer seems to have problems with
442 < 5 passes? Hidden bug? */
443 optimizing = 5; /* 5 passes */
445 optimizing = opt; /* More than 5 passes */
451 opt_verbose_info = true;
456 optimizing = INT_MAX >> 1; /* Almost unlimited */
460 report_error(ERR_FATAL,
461 "unknown optimization option -O%c\n",
470 case 'p': /* pre-include */
472 pp_pre_include(param);
475 case 'd': /* pre-define */
477 pp_pre_define(param);
480 case 'u': /* un-define */
482 pp_pre_undefine(param);
485 case 'i': /* include search path */
487 pp_include_path(param);
490 case 'l': /* listing file */
491 copy_filename(listname, param);
494 case 'Z': /* error messages file */
495 strcpy(errname, param);
498 case 'F': /* specify debug format */
499 ofmt->current_dfmt = dfmt_find(ofmt, param);
500 if (!ofmt->current_dfmt) {
501 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
502 "unrecognized debug format `%s' for"
503 " output format `%s'",
504 param, ofmt->shortname);
508 case 'X': /* specify error reporting format */
509 if (nasm_stricmp("vc", param) == 0)
510 report_error = report_error_vc;
511 else if (nasm_stricmp("gnu", param) == 0)
512 report_error = report_error_gnu;
514 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
515 "unrecognized error reporting format `%s'",
520 using_debug_info = true;
525 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
527 " [options...] [--] filename\n"
528 " or nasm -v for version info\n\n"
529 " -t assemble in SciTech TASM compatible mode\n"
530 " -g generate debug information in selected format.\n");
532 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
533 " -a don't preprocess (assemble only)\n"
534 " -M generate Makefile dependencies on stdout\n"
535 " -MG d:o, missing files assumed generated\n\n"
536 " -Z<file> redirect error messages to file\n"
537 " -s redirect error messages to stdout\n\n"
538 " -F format select a debugging format\n\n"
539 " -I<path> adds a pathname to the include file path\n");
541 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
542 " -P<file> pre-includes a file\n"
543 " -D<macro>[=<value>] pre-defines a macro\n"
544 " -U<macro> undefines a macro\n"
545 " -X<format> specifies error reporting format (gnu or vc)\n"
546 " -w+foo enables warning foo (equiv. -Wfoo)\n"
547 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
549 for (i = 0; i <= ERR_WARN_MAX; i++)
550 printf(" %-23s %s (default %s)\n",
551 suppressed_names[i], suppressed_what[i],
552 suppressed[i] ? "off" : "on");
554 ("\nresponse files should contain command line parameters"
555 ", one per line.\n");
557 printf("\nvalid output formats for -f are"
558 " (`*' denotes default):\n");
559 ofmt_list(ofmt, stdout);
561 printf("\nFor a list of valid output formats, use -hf.\n");
562 printf("For a list of debug formats, use -f <form> -y.\n");
564 exit(0); /* never need usage message here */
568 printf("\nvalid debug formats for '%s' output format are"
569 " ('*' denotes default):\n", ofmt->shortname);
570 dfmt_list(ofmt, stdout);
575 tasm_compatible_mode = true;
580 const char *nasm_version_string =
581 "NASM version " NASM_VER " compiled on " __DATE__
586 puts(nasm_version_string);
587 exit(0); /* never need usage message here */
591 case 'e': /* preprocess only */
593 operating_mode = op_preprocess;
596 case 'a': /* assemble only - don't preprocess */
601 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
610 if (param[0] != '+' && param[0] != '-') {
611 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
612 "invalid option to `-w'");
615 suppress = (param[0] == '-');
619 for (i = 0; i <= ERR_WARN_MAX; i++)
620 if (!nasm_stricmp(param, suppressed_names[i]))
622 if (i <= ERR_WARN_MAX)
623 suppressed[i] = suppress;
624 else if (!nasm_stricmp(param, "all"))
625 for (i = 1; i <= ERR_WARN_MAX; i++)
626 suppressed[i] = suppress;
627 else if (!nasm_stricmp(param, "none"))
628 for (i = 1; i <= ERR_WARN_MAX; i++)
629 suppressed[i] = !suppress;
631 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
632 "invalid warning `%s'", param);
636 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
643 if (p[2] == 0) { /* -- => stop processing options */
647 for (s = 0; textopts[s].label; s++) {
648 if (!nasm_stricmp(p + 2, textopts[s].label)) {
659 report_error(ERR_NONFATAL | ERR_NOFILE |
661 "option `--%s' requires an argument",
665 advance = 1, param = q;
668 if (s == OPT_PREFIX) {
669 strncpy(lprefix, param, PREFIX_MAX - 1);
670 lprefix[PREFIX_MAX - 1] = 0;
673 if (s == OPT_POSTFIX) {
674 strncpy(lpostfix, param, POSTFIX_MAX - 1);
675 lpostfix[POSTFIX_MAX - 1] = 0;
682 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
683 "unrecognised option `--%s'", p + 2);
691 if (!ofmt->setinfo(GI_SWITCH, &p))
692 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
693 "unrecognised option `-%c'", p[1]);
698 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
699 "more than one input file specified");
701 copy_filename(inname, p);
708 #define ARG_BUF_DELTA 128
710 static void process_respfile(FILE * rfile)
712 char *buffer, *p, *q, *prevarg;
713 int bufsize, prevargsize;
715 bufsize = prevargsize = ARG_BUF_DELTA;
716 buffer = nasm_malloc(ARG_BUF_DELTA);
717 prevarg = nasm_malloc(ARG_BUF_DELTA);
720 while (1) { /* Loop to handle all lines in file */
722 while (1) { /* Loop to handle long lines */
723 q = fgets(p, bufsize - (p - buffer), rfile);
727 if (p > buffer && p[-1] == '\n')
729 if (p - buffer > bufsize - 10) {
732 bufsize += ARG_BUF_DELTA;
733 buffer = nasm_realloc(buffer, bufsize);
738 if (!q && p == buffer) {
740 process_arg(prevarg, NULL);
747 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
748 * them are present at the end of the line.
750 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
752 while (p > buffer && isspace(p[-1]))
759 if (process_arg(prevarg, p))
762 if ((int) strlen(p) > prevargsize - 10) {
763 prevargsize += ARG_BUF_DELTA;
764 prevarg = nasm_realloc(prevarg, prevargsize);
766 strncpy(prevarg, p, prevargsize);
770 /* Function to process args from a string of args, rather than the
771 * argv array. Used by the environment variable and response file
774 static void process_args(char *args)
776 char *p, *q, *arg, *prevarg;
777 char separator = ' ';
785 while (*p && *p != separator)
787 while (*p == separator)
791 if (process_arg(prevarg, arg))
795 process_arg(arg, NULL);
798 static void parse_cmdline(int argc, char **argv)
801 char *envreal, *envcopy = NULL, *p, *arg;
803 *inname = *outname = *listname = *errname = '\0';
806 * First, process the NASMENV environment variable.
808 envreal = getenv("NASMENV");
811 envcopy = nasm_strdup(envreal);
812 process_args(envcopy);
817 * Now process the actual command line.
822 if (argv[0][0] == '@') {
823 /* We have a response file, so process this as a set of
824 * arguments like the environment variable. This allows us
825 * to have multiple arguments on a single line, which is
826 * different to the -@resp file processing below for regular
829 char *str = malloc(2048);
830 FILE *f = fopen(&argv[0][1], "r");
832 printf("out of memory");
836 while (fgets(str, 2048, f)) {
845 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
846 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
848 rfile = fopen(p, "r");
850 process_respfile(rfile);
853 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
854 "unable to open response file `%s'", p);
857 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
858 argv += advance, argc -= advance;
862 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
863 "no input file specified");
865 /* Look for basic command line typos. This definitely doesn't
866 catch all errors, but it might help cases of fumbled fingers. */
867 if (!strcmp(inname, errname) || !strcmp(inname, outname) ||
868 !strcmp(inname, listname))
869 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
870 "file `%s' is both input and output file",
874 error_file = fopen(errname, "w");
876 error_file = stderr; /* Revert to default! */
877 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
878 "cannot open file `%s' for error messages",
884 /* List of directives */
886 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
887 D_EXTERN, D_FLOAT, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
889 static const char *directives[] = {
890 "", "absolute", "bits", "common", "cpu", "debug", "default",
891 "extern", "float", "global", "list", "section", "segment", "warning"
893 static enum directives getkw(char **directive, char **value);
895 static void assemble_file(char *fname)
897 char *directive, *value, *p, *q, *special, *line, debugid[80];
903 struct tokenval tokval;
906 int pass_cnt = 0; /* count actual passes */
908 if (cmd_sb == 32 && cmd_cpu < IF_386)
909 report_error(ERR_FATAL, "command line: "
910 "32-bit segment size requires a higher cpu");
912 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
913 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
914 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
918 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
919 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
920 /* pass0 seq is 0, 0, 0,..., 1, 2 */
922 def_label = pass > 1 ? redefine_label : define_label;
924 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
928 nasmlist.init(listname, report_error);
931 global_offset_changed = false; /* set by redefine_label */
932 location.segment = ofmt->section(NULL, pass2, &sb);
935 saa_rewind(forwrefs);
936 forwref = saa_rstruct(forwrefs);
938 offsets = raa_init();
940 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
943 location.known = true;
944 location.offset = offs = GET_CURR_OFFS;
946 while ((line = preproc->getline())) {
950 /* here we parse our directives; this is not handled by the 'real'
953 d = getkw(&directive, &value);
958 case D_SEGMENT: /* [SEGMENT n] */
960 seg = ofmt->section(value, pass2, &sb);
962 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
963 "segment name `%s' not recognized",
967 location.segment = seg;
970 case D_EXTERN: /* [EXTERN label:special] */
972 value++; /* skip initial $ if present */
975 while (*q && *q != ':')
979 ofmt->symdef(value, 0L, 0L, 3, q);
981 } else if (pass == 1) { /* pass == 1 */
986 while (*q && *q != ':') {
992 report_error(ERR_NONFATAL,
993 "identifier expected after EXTERN");
1001 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1003 pass0 = 1; /* fake pass 1 in labels.c */
1004 declare_as_global(value, special,
1006 define_label(value, seg_alloc(), 0L, NULL,
1007 false, true, ofmt, report_error);
1010 } /* else pass0 == 1 */
1012 case D_BITS: /* [BITS bits] */
1013 globalbits = sb = get_bits(value);
1015 case D_GLOBAL: /* [GLOBAL symbol:special] */
1017 value++; /* skip initial $ if present */
1018 if (pass0 == 2) { /* pass 2 */
1020 while (*q && *q != ':')
1024 ofmt->symdef(value, 0L, 0L, 3, q);
1026 } else if (pass2 == 1) { /* pass == 1 */
1031 while (*q && *q != ':') {
1037 report_error(ERR_NONFATAL,
1038 "identifier expected after GLOBAL");
1046 declare_as_global(value, special, report_error);
1049 case D_COMMON: /* [COMMON symbol size:special] */
1051 value++; /* skip initial $ if present */
1057 while (*p && !isspace(*p)) {
1063 report_error(ERR_NONFATAL,
1064 "identifier expected after COMMON");
1070 while (*p && isspace(*p))
1073 while (*q && *q != ':')
1080 size = readnum(p, &rn_error);
1082 report_error(ERR_NONFATAL,
1083 "invalid size specified"
1084 " in COMMON declaration");
1086 define_common(value, seg_alloc(), size,
1087 special, ofmt, report_error);
1089 report_error(ERR_NONFATAL,
1090 "no size specified in"
1091 " COMMON declaration");
1092 } else if (pass0 == 2) { /* pass == 2 */
1094 while (*q && *q != ':') {
1101 ofmt->symdef(value, 0L, 0L, 3, q);
1105 case D_ABSOLUTE: /* [ABSOLUTE address] */
1107 stdscan_bufptr = value;
1108 tokval.t_type = TOKEN_INVALID;
1109 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1110 report_error, NULL);
1113 report_error(pass0 ==
1114 1 ? ERR_NONFATAL : ERR_PANIC,
1115 "cannot use non-relocatable expression as "
1116 "ABSOLUTE address");
1118 abs_seg = reloc_seg(e);
1119 abs_offset = reloc_value(e);
1121 } else if (pass == 1)
1122 abs_offset = 0x100; /* don't go near zero in case of / */
1124 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1127 location.segment = NO_SEG;
1129 case D_DEBUG: /* [DEBUG] */
1135 while (*p && !isspace(*p)) {
1142 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1143 "identifier expected after DEBUG");
1146 while (*p && isspace(*p))
1148 if (pass == pass_max)
1149 ofmt->current_dfmt->debug_directive(debugid, p);
1151 case D_WARNING: /* [WARNING {+|-}warn-name] */
1153 while (*value && isspace(*value))
1156 if (*value == '+' || *value == '-') {
1157 validid = (*value == '-') ? true : false;
1162 for (i = 1; i <= ERR_WARN_MAX; i++)
1163 if (!nasm_stricmp(value, suppressed_names[i]))
1165 if (i <= ERR_WARN_MAX)
1166 suppressed[i] = validid;
1168 report_error(ERR_NONFATAL,
1169 "invalid warning id in WARNING directive");
1172 case D_CPU: /* [CPU] */
1173 cpu = get_cpu(value);
1175 case D_LIST: /* [LIST {+|-}] */
1176 while (*value && isspace(*value))
1179 if (*value == '+') {
1182 if (*value == '-') {
1189 case D_DEFAULT: /* [DEFAULT] */
1191 stdscan_bufptr = value;
1192 tokval.t_type = TOKEN_INVALID;
1193 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1194 switch ((int)tokval.t_integer) {
1210 if (float_option(value)) {
1211 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1212 "unknown 'float' directive: %s",
1217 if (!ofmt->directive(directive, value, pass2))
1218 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1219 "unrecognised directive [%s]",
1223 report_error(ERR_NONFATAL,
1224 "invalid parameter to [%s] directive",
1227 } else { /* it isn't a directive */
1229 parse_line(pass1, line, &output_ins,
1230 report_error, evaluate, def_label);
1232 if (!(optimizing > 0) && pass == 2) {
1233 if (forwref != NULL && globallineno == forwref->lineno) {
1234 output_ins.forw_ref = true;
1236 output_ins.oprs[forwref->operand].opflags |=
1238 forwref = saa_rstruct(forwrefs);
1239 } while (forwref != NULL
1240 && forwref->lineno == globallineno);
1242 output_ins.forw_ref = false;
1245 if (!(optimizing > 0) && output_ins.forw_ref) {
1247 for (i = 0; i < output_ins.operands; i++) {
1248 if (output_ins.oprs[i].
1249 opflags & OPFLAG_FORWARD) {
1250 struct forwrefinfo *fwinf =
1251 (struct forwrefinfo *)
1252 saa_wstruct(forwrefs);
1253 fwinf->lineno = globallineno;
1257 } else { /* pass == 2 */
1259 * Hack to prevent phase error in the code
1263 * If the second operand is a forward reference,
1264 * the UNITY property of the number 1 in that
1265 * operand is cancelled. Otherwise the above
1266 * sequence will cause a phase error.
1268 * This hack means that the above code will
1269 * generate 286+ code.
1271 * The forward reference will mean that the
1272 * operand will not have the UNITY property on
1273 * the first pass, so the pass behaviours will
1277 if (output_ins.operands >= 2 &&
1278 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1279 !(IMMEDIATE & ~output_ins.oprs[1].type))
1281 /* Remove special properties bits */
1282 output_ins.oprs[1].type &= ~REG_SMASK;
1290 if (output_ins.opcode == I_EQU) {
1293 * Special `..' EQUs get processed in pass two,
1294 * except `..@' macro-processor EQUs which are done
1295 * in the normal place.
1297 if (!output_ins.label)
1298 report_error(ERR_NONFATAL,
1299 "EQU not preceded by label");
1301 else if (output_ins.label[0] != '.' ||
1302 output_ins.label[1] != '.' ||
1303 output_ins.label[2] == '@') {
1304 if (output_ins.operands == 1 &&
1305 (output_ins.oprs[0].type & IMMEDIATE) &&
1306 output_ins.oprs[0].wrt == NO_SEG) {
1309 opflags & OPFLAG_EXTERN;
1310 def_label(output_ins.label,
1311 output_ins.oprs[0].segment,
1312 output_ins.oprs[0].offset, NULL,
1315 } else if (output_ins.operands == 2
1316 && (output_ins.oprs[0].
1318 && (output_ins.oprs[0].type & COLON)
1319 && output_ins.oprs[0].segment ==
1321 && output_ins.oprs[0].wrt == NO_SEG
1322 && (output_ins.oprs[1].
1324 && output_ins.oprs[1].segment ==
1326 && output_ins.oprs[1].wrt ==
1328 def_label(output_ins.label,
1331 output_ins.oprs[1].offset, NULL,
1335 report_error(ERR_NONFATAL,
1336 "bad syntax for EQU");
1338 } else { /* pass == 2 */
1340 * Special `..' EQUs get processed here, except
1341 * `..@' macro processor EQUs which are done above.
1343 if (output_ins.label[0] == '.' &&
1344 output_ins.label[1] == '.' &&
1345 output_ins.label[2] != '@') {
1346 if (output_ins.operands == 1 &&
1347 (output_ins.oprs[0].type & IMMEDIATE)) {
1348 define_label(output_ins.label,
1349 output_ins.oprs[0].segment,
1350 output_ins.oprs[0].offset,
1351 NULL, false, false, ofmt,
1353 } else if (output_ins.operands == 2
1354 && (output_ins.oprs[0].
1356 && (output_ins.oprs[0].type & COLON)
1357 && output_ins.oprs[0].segment ==
1359 && (output_ins.oprs[1].
1361 && output_ins.oprs[1].segment ==
1363 define_label(output_ins.label,
1366 output_ins.oprs[1].offset,
1367 NULL, false, false, ofmt,
1370 report_error(ERR_NONFATAL,
1371 "bad syntax for EQU");
1374 } else { /* instruction isn't an EQU */
1378 int64_t l = insn_size(location.segment, offs, sb, cpu,
1379 &output_ins, report_error);
1381 /* if (using_debug_info) && output_ins.opcode != -1) */
1382 if (using_debug_info)
1383 { /* fbk 03/25/01 */
1384 /* this is done here so we can do debug type info */
1386 TYS_ELEMENTS(output_ins.operands);
1387 switch (output_ins.opcode) {
1390 TYS_ELEMENTS(output_ins.oprs[0].
1395 TYS_ELEMENTS(output_ins.oprs[0].
1400 TYS_ELEMENTS(output_ins.oprs[0].
1405 TYS_ELEMENTS(output_ins.oprs[0].
1410 TYS_ELEMENTS(output_ins.oprs[0].
1414 typeinfo |= TY_BYTE;
1417 typeinfo |= TY_WORD;
1420 if (output_ins.eops_float)
1421 typeinfo |= TY_FLOAT;
1423 typeinfo |= TY_DWORD;
1426 typeinfo |= TY_QWORD;
1429 typeinfo |= TY_TBYTE;
1432 typeinfo |= TY_OWORD;
1435 typeinfo = TY_LABEL;
1439 ofmt->current_dfmt->debug_typevalue(typeinfo);
1444 SET_CURR_OFFS(offs);
1447 * else l == -1 => invalid instruction, which will be
1448 * flagged as an error on pass 2
1451 } else { /* pass == 2 */
1452 offs += assemble(location.segment, offs, sb, cpu,
1453 &output_ins, ofmt, report_error,
1455 SET_CURR_OFFS(offs);
1459 cleanup_insn(&output_ins);
1462 location.offset = offs = GET_CURR_OFFS;
1463 } /* end while (line = preproc->getline... */
1465 if (pass1 == 2 && global_offset_changed)
1466 report_error(ERR_NONFATAL,
1467 "phase error detected at end of assembly.");
1470 preproc->cleanup(1);
1472 if (pass1 == 1 && terminate_after_phase) {
1480 if (pass > 1 && !global_offset_changed) {
1483 pass = pass_max - 1;
1484 } else if (!(optimizing > 0))
1487 } /* for (pass=1; pass<=2; pass++) */
1489 preproc->cleanup(0);
1492 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1494 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1496 } /* exit from assemble_file (...) */
1498 static enum directives getkw(char **directive, char **value)
1504 /* allow leading spaces or tabs */
1505 while (*buf == ' ' || *buf == '\t')
1513 while (*p && *p != ']')
1521 while (*p && *p != ';') {
1528 *directive = p = buf + 1;
1529 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1536 while (isspace(*buf))
1537 buf++; /* beppu - skip leading whitespace */
1544 return bsii(*directive, directives, elements(directives));
1548 * gnu style error reporting
1549 * This function prints an error message to error_file in the
1550 * style used by GNU. An example would be:
1551 * file.asm:50: error: blah blah blah
1552 * where file.asm is the name of the file, 50 is the line number on
1553 * which the error occurs (or is detected) and "error:" is one of
1554 * the possible optional diagnostics -- it can be "error" or "warning"
1555 * or something else. Finally the line terminates with the actual
1558 * @param severity the severity of the warning or error
1559 * @param fmt the printf style format string
1561 static void report_error_gnu(int severity, const char *fmt, ...)
1565 if (is_suppressed_warning(severity))
1568 if (severity & ERR_NOFILE)
1569 fputs("nasm: ", error_file);
1571 char *currentfile = NULL;
1573 src_get(&lineno, ¤tfile);
1574 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1575 nasm_free(currentfile);
1578 report_error_common(severity, fmt, ap);
1583 * MS style error reporting
1584 * This function prints an error message to error_file in the
1585 * style used by Visual C and some other Microsoft tools. An example
1587 * file.asm(50) : error: blah blah blah
1588 * where file.asm is the name of the file, 50 is the line number on
1589 * which the error occurs (or is detected) and "error:" is one of
1590 * the possible optional diagnostics -- it can be "error" or "warning"
1591 * or something else. Finally the line terminates with the actual
1594 * @param severity the severity of the warning or error
1595 * @param fmt the printf style format string
1597 static void report_error_vc(int severity, const char *fmt, ...)
1601 if (is_suppressed_warning(severity))
1604 if (severity & ERR_NOFILE)
1605 fputs("nasm: ", error_file);
1607 char *currentfile = NULL;
1609 src_get(&lineno, ¤tfile);
1610 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1611 nasm_free(currentfile);
1614 report_error_common(severity, fmt, ap);
1619 * check for supressed warning
1620 * checks for suppressed warning or pass one only warning and we're
1623 * @param severity the severity of the warning or error
1624 * @return true if we should abort error/warning printing
1626 static int is_suppressed_warning(int severity)
1629 * See if it's a suppressed warning.
1631 return ((severity & ERR_MASK) == ERR_WARNING &&
1632 (severity & ERR_WARN_MASK) != 0 &&
1633 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1635 * See if it's a pass-one only warning and we're not in pass one.
1637 ((severity & ERR_PASS1) && pass0 == 2);
1641 * common error reporting
1642 * This is the common back end of the error reporting schemes currently
1643 * implemented. It prints the nature of the warning and then the
1644 * specific error message to error_file and may or may not return. It
1645 * doesn't return if the error severity is a "panic" or "debug" type.
1647 * @param severity the severity of the warning or error
1648 * @param fmt the printf style format string
1650 static void report_error_common(int severity, const char *fmt,
1653 switch (severity & ERR_MASK) {
1655 fputs("warning: ", error_file);
1658 fputs("error: ", error_file);
1661 fputs("fatal: ", error_file);
1664 fputs("panic: ", error_file);
1667 fputs("debug: ", error_file);
1671 vfprintf(error_file, fmt, args);
1672 putc('\n', error_file);
1674 if (severity & ERR_USAGE)
1677 switch (severity & ERR_MASK) {
1679 /* no further action, by definition */
1682 if (!suppressed[0]) /* Treat warnings as errors */
1683 terminate_after_phase = true;
1686 terminate_after_phase = true;
1695 exit(1); /* instantly die */
1696 break; /* placate silly compilers */
1699 /* abort(); *//* halt, catch fire, and dump core */
1705 static void usage(void)
1707 fputs("type `nasm -h' for help\n", error_file);
1710 static void register_output_formats(void)
1712 ofmt = ofmt_register(report_error);
1715 #define BUF_DELTA 512
1717 static FILE *no_pp_fp;
1718 static efunc no_pp_err;
1719 static ListGen *no_pp_list;
1720 static int32_t no_pp_lineinc;
1722 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1725 src_set_fname(nasm_strdup(file));
1729 no_pp_fp = fopen(file, "r");
1731 no_pp_err(ERR_FATAL | ERR_NOFILE,
1732 "unable to open input file `%s'", file);
1733 no_pp_list = listgen;
1734 (void)pass; /* placate compilers */
1735 (void)eval; /* placate compilers */
1738 static char *no_pp_getline(void)
1740 char *buffer, *p, *q;
1743 bufsize = BUF_DELTA;
1744 buffer = nasm_malloc(BUF_DELTA);
1745 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1747 while (1) { /* Loop to handle %line */
1750 while (1) { /* Loop to handle long lines */
1751 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1755 if (p > buffer && p[-1] == '\n')
1757 if (p - buffer > bufsize - 10) {
1759 offset = p - buffer;
1760 bufsize += BUF_DELTA;
1761 buffer = nasm_realloc(buffer, bufsize);
1762 p = buffer + offset;
1766 if (!q && p == buffer) {
1772 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1773 * them are present at the end of the line.
1775 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1777 if (!nasm_strnicmp(buffer, "%line", 5)) {
1780 char *nm = nasm_malloc(strlen(buffer));
1781 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1782 nasm_free(src_set_fname(nm));
1792 no_pp_list->line(LIST_READ, buffer);
1797 static void no_pp_cleanup(int pass)
1799 (void)pass; /* placate GCC */
1803 static uint32_t get_cpu(char *value)
1805 if (!strcmp(value, "8086"))
1807 if (!strcmp(value, "186"))
1809 if (!strcmp(value, "286"))
1811 if (!strcmp(value, "386"))
1813 if (!strcmp(value, "486"))
1815 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1817 if (!strcmp(value, "686") ||
1818 !nasm_stricmp(value, "ppro") ||
1819 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1821 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1823 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1824 !nasm_stricmp(value, "willamette"))
1825 return IF_WILLAMETTE;
1826 if (!nasm_stricmp(value, "prescott"))
1828 if (!nasm_stricmp(value, "x64") ||
1829 !nasm_stricmp(value, "x86-64"))
1831 if (!nasm_stricmp(value, "ia64") ||
1832 !nasm_stricmp(value, "ia-64") ||
1833 !nasm_stricmp(value, "itanium") ||
1834 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1837 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1838 "unknown 'cpu' type");
1840 return IF_PLEVEL; /* the maximum level */
1843 static int get_bits(char *value)
1847 if ((i = atoi(value)) == 16)
1848 return i; /* set for a 16-bit segment */
1851 report_error(ERR_NONFATAL,
1852 "cannot specify 32-bit segment on processor below a 386");
1855 } else if (i == 64) {
1856 if (cpu < IF_X86_64) {
1857 report_error(ERR_NONFATAL,
1858 "cannot specify 64-bit segment on processor below an x86-64");
1862 report_error(ERR_NONFATAL,
1863 "%s output format does not support 64-bit code",
1868 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1869 "`%s' is not a valid segment size; must be 16, 32 or 64",