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.
31 struct forwrefinfo { /* info held on forward refs. */
36 static int get_bits(char *value);
37 static uint32_t get_cpu(char *cpu_str);
38 static void parse_cmdline(int, char **);
39 static void assemble_file(char *);
40 static int getkw(char **directive, char **value);
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 int 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 int 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 * doesn't do anything. Initial defaults are given here.
96 static char suppressed[1 + ERR_WARN_MAX] = {
97 0, TRUE, TRUE, TRUE, FALSE, TRUE
101 * The option names for the suppressible warnings. As before, entry
104 static const char *suppressed_names[1 + ERR_WARN_MAX] = {
105 NULL, "macro-params", "macro-selfref", "orphan-labels",
111 * The explanations for the suppressible warnings. As before, entry
114 static const char *suppressed_what[1 + ERR_WARN_MAX] = {
116 "macro calls with wrong no. of params",
117 "cyclic macro self-references",
118 "labels alone on lines without trailing `:'",
119 "numeric constants greater than 0xFFFFFFFF",
120 "using 8- or 16-bit relocation in ELF, a GNU extension"
124 * This is a null preprocessor which just copies lines from input
125 * to output. It's used when someone explicitly requests that NASM
126 * not preprocess their source file.
129 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
130 static char *no_pp_getline(void);
131 static void no_pp_cleanup(int);
132 static Preproc no_pp = {
139 * get/set current offset...
141 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
142 raa_read(offsets,location.segment))
143 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
144 (void)(offsets=raa_write(offsets,location.segment,(x))))
146 static int want_usage;
147 static int terminate_after_phase;
148 int user_nolist = 0; /* fbk 9/2/00 */
150 static void nasm_fputs(const char *line, FILE * outfile)
153 fputs(line, outfile);
154 fputc('\n', outfile);
159 int main(int argc, char **argv)
162 want_usage = terminate_after_phase = FALSE;
163 report_error = report_error_gnu;
167 nasm_set_malloc_error(report_error);
168 offsets = raa_init();
169 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
172 operating_mode = op_normal;
176 register_output_formats();
178 parse_cmdline(argc, argv);
180 if (terminate_after_phase) {
186 /* If debugging info is disabled, suppress any debug calls */
187 if (!using_debug_info)
188 ofmt->current_dfmt = &null_debug_form;
191 pp_extra_stdmac(ofmt->stdmac);
192 parser_global_info(ofmt, &location);
193 eval_global_info(ofmt, lookup_label, &location);
195 /* define some macros dependent of command-line */
198 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
203 switch (operating_mode) {
204 case op_depend_missing_ok:
205 pp_include_path(NULL); /* "assume generated" */
210 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
211 if (outname[0] == '\0')
212 ofmt->filename(inname, outname, report_error);
214 fprintf(stdout, "%s: %s", outname, inname);
215 while ((line = preproc->getline()))
225 char *file_name = NULL;
226 int32_t prior_linnum = 0;
230 ofile = fopen(outname, "w");
232 report_error(ERR_FATAL | ERR_NOFILE,
233 "unable to open output file `%s'",
238 location.known = FALSE;
241 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
242 while ((line = preproc->getline())) {
244 * We generate %line directives if needed for later programs
246 int32_t linnum = prior_linnum += lineinc;
247 int altline = src_get(&linnum, &file_name);
249 if (altline == 1 && lineinc == 1)
250 nasm_fputs("", ofile);
252 lineinc = (altline != -1 || lineinc != 1);
253 fprintf(ofile ? ofile : stdout,
254 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
257 prior_linnum = linnum;
259 nasm_fputs(line, ofile);
262 nasm_free(file_name);
266 if (ofile && terminate_after_phase)
274 * We must call ofmt->filename _anyway_, even if the user
275 * has specified their own output file, because some
276 * formats (eg OBJ and COFF) use ofmt->filename to find out
277 * the name of the input file and then put that inside the
280 ofmt->filename(inname, outname, report_error);
282 ofile = fopen(outname, "wb");
284 report_error(ERR_FATAL | ERR_NOFILE,
285 "unable to open output file `%s'", outname);
289 * We must call init_labels() before ofmt->init() since
290 * some object formats will want to define labels in their
291 * init routines. (eg OS/2 defines the FLAT group)
295 ofmt->init(ofile, report_error, define_label, evaluate);
297 assemble_file(inname);
299 if (!terminate_after_phase) {
300 ofmt->cleanup(using_debug_info);
304 * We had an fclose on the output file here, but we
305 * actually do that in all the object file drivers as well,
306 * so we're leaving out the one here.
325 if (terminate_after_phase)
332 * Get a parameter for a command line option.
333 * First arg must be in the form of e.g. -f...
335 static char *get_param(char *p, char *q, int *advance)
338 if (p[2]) { /* the parameter's in the option */
348 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
349 "option `-%c' requires an argument", p[1]);
359 #define OPT_POSTFIX 1
360 struct textargs textopts[] = {
361 {"prefix", OPT_PREFIX},
362 {"postfix", OPT_POSTFIX},
367 static int process_arg(char *p, char *q)
375 if (p[0] == '-' && !stopoptions) {
380 case 'o': /* these parameters take values */
395 if (!(param = get_param(p, q, &advance)))
397 if (p[1] == 'o') { /* output file */
398 strcpy(outname, param);
399 } else if (p[1] == 'f') { /* output format */
400 ofmt = ofmt_find(param);
402 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
403 "unrecognised output format `%s' - "
404 "use -hf for a list", param);
406 ofmt->current_dfmt = ofmt->debug_formats[0];
407 } else if (p[1] == 'O') { /* Optimization level */
411 /* Naked -O == -Ox */
412 optimizing = INT_MAX >> 1; /* Almost unlimited */
416 case '0': case '1': case '2': case '3': case '4':
417 case '5': case '6': case '7': case '8': case '9':
418 opt = strtoul(param, ¶m, 10);
420 /* -O0 -> optimizing == -1, 0.98 behaviour */
421 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
423 optimizing = opt - 1;
425 /* The optimizer seems to have problems with
426 < 5 passes? Hidden bug? */
427 optimizing = 5; /* 5 passes */
429 optimizing = opt; /* More than 5 passes */
435 opt_verbose_info = TRUE;
440 optimizing = INT_MAX >> 1; /* Almost unlimited */
444 report_error(ERR_FATAL,
445 "unknown optimization option -O%c\n",
451 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
452 pp_pre_include(param);
453 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
454 pp_pre_define(param);
455 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
456 pp_pre_undefine(param);
457 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
458 pp_include_path(param);
459 } else if (p[1] == 'l') { /* listing file */
460 strcpy(listname, param);
461 } else if (p[1] == 'Z') { /* error messages file */
462 strcpy(errname, param);
463 } else if (p[1] == 'F') { /* specify debug format */
464 ofmt->current_dfmt = dfmt_find(ofmt, param);
465 if (!ofmt->current_dfmt) {
466 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
467 "unrecognized debug format `%s' for"
468 " output format `%s'",
469 param, ofmt->shortname);
471 } else if (p[1] == 'X') { /* specify error reporting format */
472 if (nasm_stricmp("vc", param) == 0)
473 report_error = report_error_vc;
474 else if (nasm_stricmp("gnu", param) == 0)
475 report_error = report_error_gnu;
477 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
478 "unrecognized error reporting format `%s'",
483 using_debug_info = TRUE;
487 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
489 " [options...] [--] filename\n"
490 " or nasm -v for version info\n\n"
491 " -t assemble in SciTech TASM compatible mode\n"
492 " -g generate debug information in selected format.\n");
494 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
495 " -a don't preprocess (assemble only)\n"
496 " -M generate Makefile dependencies on stdout\n"
497 " -MG d:o, missing files assumed generated\n\n"
498 " -Z<file> redirect error messages to file\n"
499 " -s redirect error messages to stdout\n\n"
500 " -F format select a debugging format\n\n"
501 " -I<path> adds a pathname to the include file path\n");
503 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
504 " -P<file> pre-includes a file\n"
505 " -D<macro>[=<value>] pre-defines a macro\n"
506 " -U<macro> undefines a macro\n"
507 " -X<format> specifies error reporting format (gnu or vc)\n"
508 " -w+foo enables warnings about foo; -w-foo disables them\n"
509 "where foo can be:\n");
510 for (i = 1; i <= ERR_WARN_MAX; i++)
511 printf(" %-23s %s (default %s)\n",
512 suppressed_names[i], suppressed_what[i],
513 suppressed[i] ? "off" : "on");
515 ("\nresponse files should contain command line parameters"
516 ", one per line.\n");
518 printf("\nvalid output formats for -f are"
519 " (`*' denotes default):\n");
520 ofmt_list(ofmt, stdout);
522 printf("\nFor a list of valid output formats, use -hf.\n");
523 printf("For a list of debug formats, use -f <form> -y.\n");
525 exit(0); /* never need usage message here */
528 printf("\nvalid debug formats for '%s' output format are"
529 " ('*' denotes default):\n", ofmt->shortname);
530 dfmt_list(ofmt, stdout);
534 tasm_compatible_mode = TRUE;
538 const char *nasm_version_string =
539 "NASM version " NASM_VER " compiled on " __DATE__
544 puts(nasm_version_string);
545 exit(0); /* never need usage message here */
548 case 'e': /* preprocess only */
550 operating_mode = op_preprocess;
552 case 'a': /* assemble only - don't preprocess */
556 if (p[2] != '+' && p[2] != '-') {
557 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
558 "invalid option to `-w'");
560 for (i = 1; i <= ERR_WARN_MAX; i++)
561 if (!nasm_stricmp(p + 3, suppressed_names[i]))
563 if (i <= ERR_WARN_MAX)
564 suppressed[i] = (p[2] == '-');
566 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
567 "invalid option to `-w'");
571 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
578 if (p[2] == 0) { /* -- => stop processing options */
582 for (s = 0; textopts[s].label; s++) {
583 if (!nasm_stricmp(p + 2, textopts[s].label)) {
594 report_error(ERR_NONFATAL | ERR_NOFILE |
596 "option `--%s' requires an argument",
600 advance = 1, param = q;
603 if (s == OPT_PREFIX) {
604 strncpy(lprefix, param, PREFIX_MAX - 1);
605 lprefix[PREFIX_MAX - 1] = 0;
608 if (s == OPT_POSTFIX) {
609 strncpy(lpostfix, param, POSTFIX_MAX - 1);
610 lpostfix[POSTFIX_MAX - 1] = 0;
617 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
618 "unrecognised option `--%s'", p + 2);
626 if (!ofmt->setinfo(GI_SWITCH, &p))
627 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
628 "unrecognised option `-%c'", p[1]);
633 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
634 "more than one input file specified");
642 #define ARG_BUF_DELTA 128
644 static void process_respfile(FILE * rfile)
646 char *buffer, *p, *q, *prevarg;
647 int bufsize, prevargsize;
649 bufsize = prevargsize = ARG_BUF_DELTA;
650 buffer = nasm_malloc(ARG_BUF_DELTA);
651 prevarg = nasm_malloc(ARG_BUF_DELTA);
654 while (1) { /* Loop to handle all lines in file */
657 while (1) { /* Loop to handle long lines */
658 q = fgets(p, bufsize - (p - buffer), rfile);
662 if (p > buffer && p[-1] == '\n')
664 if (p - buffer > bufsize - 10) {
667 bufsize += ARG_BUF_DELTA;
668 buffer = nasm_realloc(buffer, bufsize);
673 if (!q && p == buffer) {
675 process_arg(prevarg, NULL);
682 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
683 * them are present at the end of the line.
685 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
687 while (p > buffer && isspace(p[-1]))
694 if (process_arg(prevarg, p))
697 if (strlen(p) > prevargsize - 10) {
698 prevargsize += ARG_BUF_DELTA;
699 prevarg = nasm_realloc(prevarg, prevargsize);
705 /* Function to process args from a string of args, rather than the
706 * argv array. Used by the environment variable and response file
709 static void process_args(char *args)
711 char *p, *q, *arg, *prevarg;
712 char separator = ' ';
720 while (*p && *p != separator)
722 while (*p == separator)
726 if (process_arg(prevarg, arg))
730 process_arg(arg, NULL);
733 static void parse_cmdline(int argc, char **argv)
736 char *envreal, *envcopy = NULL, *p, *arg;
738 *inname = *outname = *listname = *errname = '\0';
741 * First, process the NASMENV environment variable.
743 envreal = getenv("NASMENV");
746 envcopy = nasm_strdup(envreal);
747 process_args(envcopy);
752 * Now process the actual command line.
757 if (argv[0][0] == '@') {
758 /* We have a response file, so process this as a set of
759 * arguments like the environment variable. This allows us
760 * to have multiple arguments on a single line, which is
761 * different to the -@resp file processing below for regular
764 char *str = malloc(2048);
765 FILE *f = fopen(&argv[0][1], "r");
767 printf("out of memory");
771 while (fgets(str, 2048, f)) {
780 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
781 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
783 rfile = fopen(p, "r");
785 process_respfile(rfile);
788 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
789 "unable to open response file `%s'", p);
792 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
793 argv += i, argc -= i;
797 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
798 "no input file specified");
800 /* Look for basic command line typos. This definitely doesn't
801 catch all errors, but it might help cases of fumbled fingers. */
802 if (!strcmp(inname, errname) || !strcmp(inname, outname) ||
803 !strcmp(inname, listname))
804 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
805 "file `%s' is both input and output file",
809 error_file = fopen(errname, "w");
811 error_file = stderr; /* Revert to default! */
812 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
813 "cannot open file `%s' for error messages",
819 /* List of directives */
821 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
822 D_EXTERN, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
824 static const char *directives[] = {
825 "", "absolute", "bits", "common", "cpu", "debug", "default",
826 "extern", "global", "list", "section", "segment", "warning"
829 static void assemble_file(char *fname)
831 char *directive, *value, *p, *q, *special, *line, debugid[80];
833 int i, rn_error, validid;
835 struct tokenval tokval;
838 int pass_cnt = 0; /* count actual passes */
840 if (cmd_sb == 32 && cmd_cpu < IF_386)
841 report_error(ERR_FATAL, "command line: "
842 "32-bit segment size requires a higher cpu");
844 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
845 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
846 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
850 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
851 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
852 /* pass0 seq is 0, 0, 0,..., 1, 2 */
854 def_label = pass > 1 ? redefine_label : define_label;
856 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
860 nasmlist.init(listname, report_error);
863 global_offset_changed = FALSE; /* set by redefine_label */
864 location.segment = ofmt->section(NULL, pass2, &sb);
867 saa_rewind(forwrefs);
868 forwref = saa_rstruct(forwrefs);
870 offsets = raa_init();
872 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
875 location.known = TRUE;
876 location.offset = offs = GET_CURR_OFFS;
878 while ((line = preproc->getline())) {
881 /* here we parse our directives; this is not handled by the 'real'
884 i = getkw(&directive, &value);
889 case D_SEGMENT: /* [SEGMENT n] */
891 seg = ofmt->section(value, pass2, &sb);
893 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
894 "segment name `%s' not recognized",
898 location.segment = seg;
901 case D_EXTERN: /* [EXTERN label:special] */
903 value++; /* skip initial $ if present */
906 while (*q && *q != ':')
910 ofmt->symdef(value, 0L, 0L, 3, q);
912 } else if (pass == 1) { /* pass == 1 */
917 while (*q && *q != ':') {
923 report_error(ERR_NONFATAL,
924 "identifier expected after EXTERN");
932 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
934 pass0 = 1; /* fake pass 1 in labels.c */
935 declare_as_global(value, special,
937 define_label(value, seg_alloc(), 0L, NULL,
938 FALSE, TRUE, ofmt, report_error);
941 } /* else pass0 == 1 */
943 case D_BITS: /* [BITS bits] */
944 globalbits = sb = get_bits(value);
946 case D_GLOBAL: /* [GLOBAL symbol:special] */
948 value++; /* skip initial $ if present */
949 if (pass0 == 2) { /* pass 2 */
951 while (*q && *q != ':')
955 ofmt->symdef(value, 0L, 0L, 3, q);
957 } else if (pass2 == 1) { /* pass == 1 */
962 while (*q && *q != ':') {
968 report_error(ERR_NONFATAL,
969 "identifier expected after GLOBAL");
977 declare_as_global(value, special, report_error);
980 case D_COMMON: /* [COMMON symbol size:special] */
982 value++; /* skip initial $ if present */
988 while (*p && !isspace(*p)) {
994 report_error(ERR_NONFATAL,
995 "identifier expected after COMMON");
1001 while (*p && isspace(*p))
1004 while (*q && *q != ':')
1011 size = readnum(p, &rn_error);
1013 report_error(ERR_NONFATAL,
1014 "invalid size specified"
1015 " in COMMON declaration");
1017 define_common(value, seg_alloc(), size,
1018 special, ofmt, report_error);
1020 report_error(ERR_NONFATAL,
1021 "no size specified in"
1022 " COMMON declaration");
1023 } else if (pass0 == 2) { /* pass == 2 */
1025 while (*q && *q != ':') {
1032 ofmt->symdef(value, 0L, 0L, 3, q);
1036 case D_ABSOLUTE: /* [ABSOLUTE address] */
1038 stdscan_bufptr = value;
1039 tokval.t_type = TOKEN_INVALID;
1040 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1041 report_error, NULL);
1044 report_error(pass0 ==
1045 1 ? ERR_NONFATAL : ERR_PANIC,
1046 "cannot use non-relocatable expression as "
1047 "ABSOLUTE address");
1049 abs_seg = reloc_seg(e);
1050 abs_offset = reloc_value(e);
1052 } else if (pass == 1)
1053 abs_offset = 0x100; /* don't go near zero in case of / */
1055 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1058 location.segment = NO_SEG;
1060 case D_DEBUG: /* [DEBUG] */
1066 while (*p && !isspace(*p)) {
1073 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1074 "identifier expected after DEBUG");
1077 while (*p && isspace(*p))
1079 if (pass == pass_max)
1080 ofmt->current_dfmt->debug_directive(debugid, p);
1082 case D_WARNING: /* [WARNING {+|-}warn-name] */
1084 while (*value && isspace(*value))
1087 if (*value == '+' || *value == '-') {
1088 validid = (*value == '-') ? TRUE : FALSE;
1093 for (i = 1; i <= ERR_WARN_MAX; i++)
1094 if (!nasm_stricmp(value, suppressed_names[i]))
1096 if (i <= ERR_WARN_MAX)
1097 suppressed[i] = validid;
1099 report_error(ERR_NONFATAL,
1100 "invalid warning id in WARNING directive");
1103 case D_CPU: /* [CPU] */
1104 cpu = get_cpu(value);
1106 case D_LIST: /* [LIST {+|-}] */
1107 while (*value && isspace(*value))
1110 if (*value == '+') {
1113 if (*value == '-') {
1120 case D_DEFAULT: /* [DEFAULT] */
1122 stdscan_bufptr = value;
1123 tokval.t_type = TOKEN_INVALID;
1124 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1125 switch ((int)tokval.t_integer) {
1141 if (!ofmt->directive(directive, value, pass2))
1142 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1143 "unrecognised directive [%s]",
1147 report_error(ERR_NONFATAL,
1148 "invalid parameter to [%s] directive",
1151 } else { /* it isn't a directive */
1153 parse_line(pass1, line, &output_ins,
1154 report_error, evaluate, def_label);
1156 if (!(optimizing > 0) && pass == 2) {
1157 if (forwref != NULL && globallineno == forwref->lineno) {
1158 output_ins.forw_ref = TRUE;
1160 output_ins.oprs[forwref->operand].opflags |=
1162 forwref = saa_rstruct(forwrefs);
1163 } while (forwref != NULL
1164 && forwref->lineno == globallineno);
1166 output_ins.forw_ref = FALSE;
1169 if (!(optimizing > 0) && output_ins.forw_ref) {
1171 for (i = 0; i < output_ins.operands; i++) {
1172 if (output_ins.oprs[i].
1173 opflags & OPFLAG_FORWARD) {
1174 struct forwrefinfo *fwinf =
1175 (struct forwrefinfo *)
1176 saa_wstruct(forwrefs);
1177 fwinf->lineno = globallineno;
1181 } else { /* pass == 2 */
1183 * Hack to prevent phase error in the code
1187 * If the second operand is a forward reference,
1188 * the UNITY property of the number 1 in that
1189 * operand is cancelled. Otherwise the above
1190 * sequence will cause a phase error.
1192 * This hack means that the above code will
1193 * generate 286+ code.
1195 * The forward reference will mean that the
1196 * operand will not have the UNITY property on
1197 * the first pass, so the pass behaviours will
1201 if (output_ins.operands >= 2 &&
1202 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1203 !(IMMEDIATE & ~output_ins.oprs[1].type))
1205 /* Remove special properties bits */
1206 output_ins.oprs[1].type &= ~REG_SMASK;
1214 if (output_ins.opcode == I_EQU) {
1217 * Special `..' EQUs get processed in pass two,
1218 * except `..@' macro-processor EQUs which are done
1219 * in the normal place.
1221 if (!output_ins.label)
1222 report_error(ERR_NONFATAL,
1223 "EQU not preceded by label");
1225 else if (output_ins.label[0] != '.' ||
1226 output_ins.label[1] != '.' ||
1227 output_ins.label[2] == '@') {
1228 if (output_ins.operands == 1 &&
1229 (output_ins.oprs[0].type & IMMEDIATE) &&
1230 output_ins.oprs[0].wrt == NO_SEG) {
1233 opflags & OPFLAG_EXTERN;
1234 def_label(output_ins.label,
1235 output_ins.oprs[0].segment,
1236 output_ins.oprs[0].offset, NULL,
1239 } else if (output_ins.operands == 2
1240 && (output_ins.oprs[0].
1242 && (output_ins.oprs[0].type & COLON)
1243 && output_ins.oprs[0].segment ==
1245 && output_ins.oprs[0].wrt == NO_SEG
1246 && (output_ins.oprs[1].
1248 && output_ins.oprs[1].segment ==
1250 && output_ins.oprs[1].wrt ==
1252 def_label(output_ins.label,
1255 output_ins.oprs[1].offset, NULL,
1259 report_error(ERR_NONFATAL,
1260 "bad syntax for EQU");
1262 } else { /* pass == 2 */
1264 * Special `..' EQUs get processed here, except
1265 * `..@' macro processor EQUs which are done above.
1267 if (output_ins.label[0] == '.' &&
1268 output_ins.label[1] == '.' &&
1269 output_ins.label[2] != '@') {
1270 if (output_ins.operands == 1 &&
1271 (output_ins.oprs[0].type & IMMEDIATE)) {
1272 define_label(output_ins.label,
1273 output_ins.oprs[0].segment,
1274 output_ins.oprs[0].offset,
1275 NULL, FALSE, FALSE, ofmt,
1277 } else if (output_ins.operands == 2
1278 && (output_ins.oprs[0].
1280 && (output_ins.oprs[0].type & COLON)
1281 && output_ins.oprs[0].segment ==
1283 && (output_ins.oprs[1].
1285 && output_ins.oprs[1].segment ==
1287 define_label(output_ins.label,
1290 output_ins.oprs[1].offset,
1291 NULL, FALSE, FALSE, ofmt,
1294 report_error(ERR_NONFATAL,
1295 "bad syntax for EQU");
1298 } else { /* instruction isn't an EQU */
1302 int32_t l = insn_size(location.segment, offs, sb, cpu,
1303 &output_ins, report_error);
1305 /* if (using_debug_info) && output_ins.opcode != -1) */
1306 if (using_debug_info)
1307 { /* fbk 03/25/01 */
1308 /* this is done here so we can do debug type info */
1310 TYS_ELEMENTS(output_ins.operands);
1311 switch (output_ins.opcode) {
1314 TYS_ELEMENTS(output_ins.oprs[0].
1319 TYS_ELEMENTS(output_ins.oprs[0].
1324 TYS_ELEMENTS(output_ins.oprs[0].
1329 TYS_ELEMENTS(output_ins.oprs[0].
1334 TYS_ELEMENTS(output_ins.oprs[0].
1338 typeinfo |= TY_BYTE;
1341 typeinfo |= TY_WORD;
1344 if (output_ins.eops_float)
1345 typeinfo |= TY_FLOAT;
1347 typeinfo |= TY_DWORD;
1350 typeinfo |= TY_QWORD;
1353 typeinfo |= TY_TBYTE;
1356 typeinfo |= TY_OWORD;
1359 typeinfo = TY_LABEL;
1363 ofmt->current_dfmt->debug_typevalue(typeinfo);
1368 SET_CURR_OFFS(offs);
1371 * else l == -1 => invalid instruction, which will be
1372 * flagged as an error on pass 2
1375 } else { /* pass == 2 */
1376 offs += assemble(location.segment, offs, sb, cpu,
1377 &output_ins, ofmt, report_error,
1379 SET_CURR_OFFS(offs);
1383 cleanup_insn(&output_ins);
1386 location.offset = offs = GET_CURR_OFFS;
1387 } /* end while (line = preproc->getline... */
1389 if (pass1 == 2 && global_offset_changed)
1390 report_error(ERR_NONFATAL,
1391 "phase error detected at end of assembly.");
1394 preproc->cleanup(1);
1396 if (pass1 == 1 && terminate_after_phase) {
1404 if (pass > 1 && !global_offset_changed) {
1407 pass = pass_max - 1;
1408 } else if (!(optimizing > 0))
1411 } /* for (pass=1; pass<=2; pass++) */
1413 preproc->cleanup(0);
1416 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1418 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1420 } /* exit from assemble_file (...) */
1422 static int getkw(char **directive, char **value)
1428 /* allow leading spaces or tabs */
1429 while (*buf == ' ' || *buf == '\t')
1437 while (*p && *p != ']')
1445 while (*p && *p != ';') {
1452 *directive = p = buf + 1;
1453 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1460 while (isspace(*buf))
1461 buf++; /* beppu - skip leading whitespace */
1468 return bsii(*directive, directives, elements(directives));
1472 * gnu style error reporting
1473 * This function prints an error message to error_file in the
1474 * style used by GNU. An example would be:
1475 * file.asm:50: error: blah blah blah
1476 * where file.asm is the name of the file, 50 is the line number on
1477 * which the error occurs (or is detected) and "error:" is one of
1478 * the possible optional diagnostics -- it can be "error" or "warning"
1479 * or something else. Finally the line terminates with the actual
1482 * @param severity the severity of the warning or error
1483 * @param fmt the printf style format string
1485 static void report_error_gnu(int severity, const char *fmt, ...)
1489 if (is_suppressed_warning(severity))
1492 if (severity & ERR_NOFILE)
1493 fputs("nasm: ", error_file);
1495 char *currentfile = NULL;
1497 src_get(&lineno, ¤tfile);
1498 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1499 nasm_free(currentfile);
1502 report_error_common(severity, fmt, ap);
1507 * MS style error reporting
1508 * This function prints an error message to error_file in the
1509 * style used by Visual C and some other Microsoft tools. An example
1511 * file.asm(50) : error: blah blah blah
1512 * where file.asm is the name of the file, 50 is the line number on
1513 * which the error occurs (or is detected) and "error:" is one of
1514 * the possible optional diagnostics -- it can be "error" or "warning"
1515 * or something else. Finally the line terminates with the actual
1518 * @param severity the severity of the warning or error
1519 * @param fmt the printf style format string
1521 static void report_error_vc(int severity, const char *fmt, ...)
1525 if (is_suppressed_warning(severity))
1528 if (severity & ERR_NOFILE)
1529 fputs("nasm: ", error_file);
1531 char *currentfile = NULL;
1533 src_get(&lineno, ¤tfile);
1534 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1535 nasm_free(currentfile);
1538 report_error_common(severity, fmt, ap);
1543 * check for supressed warning
1544 * checks for suppressed warning or pass one only warning and we're
1547 * @param severity the severity of the warning or error
1548 * @return true if we should abort error/warning printing
1550 static int is_suppressed_warning(int severity)
1553 * See if it's a suppressed warning.
1555 return ((severity & ERR_MASK) == ERR_WARNING &&
1556 (severity & ERR_WARN_MASK) != 0 &&
1557 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1559 * See if it's a pass-one only warning and we're not in pass one.
1561 ((severity & ERR_PASS1) && pass0 == 2);
1565 * common error reporting
1566 * This is the common back end of the error reporting schemes currently
1567 * implemented. It prints the nature of the warning and then the
1568 * specific error message to error_file and may or may not return. It
1569 * doesn't return if the error severity is a "panic" or "debug" type.
1571 * @param severity the severity of the warning or error
1572 * @param fmt the printf style format string
1574 static void report_error_common(int severity, const char *fmt,
1577 switch (severity & ERR_MASK) {
1579 fputs("warning: ", error_file);
1582 fputs("error: ", error_file);
1585 fputs("fatal: ", error_file);
1588 fputs("panic: ", error_file);
1591 fputs("debug: ", error_file);
1595 vfprintf(error_file, fmt, args);
1596 fputc('\n', error_file);
1598 if (severity & ERR_USAGE)
1601 switch (severity & ERR_MASK) {
1604 /* no further action, by definition */
1607 /* hack enables listing(!) on errors */
1608 terminate_after_phase = TRUE;
1617 exit(1); /* instantly die */
1618 break; /* placate silly compilers */
1621 /* abort(); *//* halt, catch fire, and dump core */
1627 static void usage(void)
1629 fputs("type `nasm -h' for help\n", error_file);
1632 static void register_output_formats(void)
1634 ofmt = ofmt_register(report_error);
1637 #define BUF_DELTA 512
1639 static FILE *no_pp_fp;
1640 static efunc no_pp_err;
1641 static ListGen *no_pp_list;
1642 static int32_t no_pp_lineinc;
1644 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1647 src_set_fname(nasm_strdup(file));
1651 no_pp_fp = fopen(file, "r");
1653 no_pp_err(ERR_FATAL | ERR_NOFILE,
1654 "unable to open input file `%s'", file);
1655 no_pp_list = listgen;
1656 (void)pass; /* placate compilers */
1657 (void)eval; /* placate compilers */
1660 static char *no_pp_getline(void)
1662 char *buffer, *p, *q;
1665 bufsize = BUF_DELTA;
1666 buffer = nasm_malloc(BUF_DELTA);
1667 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1669 while (1) { /* Loop to handle %line */
1672 while (1) { /* Loop to handle long lines */
1673 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1677 if (p > buffer && p[-1] == '\n')
1679 if (p - buffer > bufsize - 10) {
1681 offset = p - buffer;
1682 bufsize += BUF_DELTA;
1683 buffer = nasm_realloc(buffer, bufsize);
1684 p = buffer + offset;
1688 if (!q && p == buffer) {
1694 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1695 * them are present at the end of the line.
1697 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1699 if (!nasm_strnicmp(buffer, "%line", 5)) {
1702 char *nm = nasm_malloc(strlen(buffer));
1703 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1704 nasm_free(src_set_fname(nm));
1714 no_pp_list->line(LIST_READ, buffer);
1719 static void no_pp_cleanup(int pass)
1721 (void)pass; /* placate GCC */
1725 static uint32_t get_cpu(char *value)
1728 if (!strcmp(value, "8086"))
1730 if (!strcmp(value, "186"))
1732 if (!strcmp(value, "286"))
1734 if (!strcmp(value, "386"))
1736 if (!strcmp(value, "486"))
1738 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1740 if (!strcmp(value, "686") ||
1741 !nasm_stricmp(value, "ppro") ||
1742 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1744 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1746 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1747 !nasm_stricmp(value, "willamette"))
1748 return IF_WILLAMETTE;
1749 if (!nasm_stricmp(value, "prescott"))
1751 if (!nasm_stricmp(value, "x64") ||
1752 !nasm_stricmp(value, "x86-64"))
1754 if (!nasm_stricmp(value, "ia64") ||
1755 !nasm_stricmp(value, "ia-64") ||
1756 !nasm_stricmp(value, "itanium") ||
1757 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1760 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1761 "unknown 'cpu' type");
1763 return IF_PLEVEL; /* the maximum level */
1766 static int get_bits(char *value)
1770 if ((i = atoi(value)) == 16)
1771 return i; /* set for a 16-bit segment */
1774 report_error(ERR_NONFATAL,
1775 "cannot specify 32-bit segment on processor below a 386");
1778 } else if (i == 64) {
1779 if (cpu < IF_X86_64) {
1780 report_error(ERR_NONFATAL,
1781 "cannot specify 64-bit segment on processor below an x86-64");
1785 report_error(ERR_NONFATAL,
1786 "%s output format does not support 64-bit code",
1791 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1792 "`%s' is not a valid segment size; must be 16, 32 or 64",