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.
28 struct forwrefinfo { /* info held on forward refs. */
33 static int get_bits(char *value);
34 static uint32_t get_cpu(char *cpu_str);
35 static void parse_cmdline(int, char **);
36 static void assemble_file(char *);
37 static int getkw(char **directive, char **value);
38 static void register_output_formats(void);
39 static void report_error_gnu(int severity, const char *fmt, ...);
40 static void report_error_vc(int severity, const char *fmt, ...);
41 static void report_error_common(int severity, const char *fmt,
43 static int is_suppressed_warning(int severity);
44 static void usage(void);
45 static efunc report_error;
47 static int using_debug_info, opt_verbose_info;
48 int tasm_compatible_mode = FALSE;
53 static char inname[FILENAME_MAX];
54 static char outname[FILENAME_MAX];
55 static char listname[FILENAME_MAX];
56 static int globallineno; /* for forward-reference tracking */
57 /* static int pass = 0; */
58 static struct ofmt *ofmt = NULL;
60 static FILE *error_file; /* Where to write error messages */
62 static FILE *ofile = NULL;
63 int optimizing = -1; /* number of optimization passes to take */
64 static int sb, cmd_sb = 16; /* by default */
65 static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
66 static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
67 int global_offset_changed; /* referenced in labels.c */
69 static loc_t location;
70 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
71 int32_t abs_seg; /* ABSOLUTE segment basis */
72 int32_t abs_offset; /* ABSOLUTE offset */
74 static struct RAA *offsets;
76 static struct SAA *forwrefs; /* keep track of forward references */
77 static struct forwrefinfo *forwref;
79 static Preproc *preproc;
81 op_normal, /* Preprocess and assemble */
82 op_preprocess, /* Preprocess only */
83 op_depend, /* Generate dependencies */
84 op_depend_missing_ok, /* Generate dependencies, missing OK */
86 static enum op_type operating_mode;
89 * Which of the suppressible warnings are suppressed. Entry zero
90 * doesn't do anything. Initial defaults are given here.
92 static char suppressed[1 + ERR_WARN_MAX] = {
93 0, TRUE, TRUE, TRUE, FALSE, TRUE
97 * The option names for the suppressible warnings. As before, entry
100 static const char *suppressed_names[1 + ERR_WARN_MAX] = {
101 NULL, "macro-params", "macro-selfref", "orphan-labels",
107 * The explanations for the suppressible warnings. As before, entry
110 static const char *suppressed_what[1 + ERR_WARN_MAX] = {
112 "macro calls with wrong no. of params",
113 "cyclic macro self-references",
114 "labels alone on lines without trailing `:'",
115 "numeric constants greater than 0xFFFFFFFF",
116 "using 8- or 16-bit relocation in ELF, a GNU extension"
120 * This is a null preprocessor which just copies lines from input
121 * to output. It's used when someone explicitly requests that NASM
122 * not preprocess their source file.
125 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
126 static char *no_pp_getline(void);
127 static void no_pp_cleanup(int);
128 static Preproc no_pp = {
135 * get/set current offset...
137 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
138 raa_read(offsets,location.segment))
139 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
140 (void)(offsets=raa_write(offsets,location.segment,(x))))
142 static int want_usage;
143 static int terminate_after_phase;
144 int user_nolist = 0; /* fbk 9/2/00 */
146 static void nasm_fputs(const char *line, FILE * outfile)
149 fputs(line, outfile);
150 fputc('\n', outfile);
155 int main(int argc, char **argv)
158 want_usage = terminate_after_phase = FALSE;
159 report_error = report_error_gnu;
163 nasm_set_malloc_error(report_error);
164 offsets = raa_init();
165 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
168 operating_mode = op_normal;
172 register_output_formats();
174 parse_cmdline(argc, argv);
176 if (terminate_after_phase) {
182 /* If debugging info is disabled, suppress any debug calls */
183 if (!using_debug_info)
184 ofmt->current_dfmt = &null_debug_form;
187 pp_extra_stdmac(ofmt->stdmac);
188 parser_global_info(ofmt, &location);
189 eval_global_info(ofmt, lookup_label, &location);
191 /* define some macros dependent of command-line */
194 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
199 switch (operating_mode) {
200 case op_depend_missing_ok:
201 pp_include_path(NULL); /* "assume generated" */
206 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
207 if (outname[0] == '\0')
208 ofmt->filename(inname, outname, report_error);
210 fprintf(stdout, "%s: %s", outname, inname);
211 while ((line = preproc->getline()))
221 char *file_name = NULL;
222 int32_t prior_linnum = 0;
226 ofile = fopen(outname, "w");
228 report_error(ERR_FATAL | ERR_NOFILE,
229 "unable to open output file `%s'",
234 location.known = FALSE;
237 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
238 while ((line = preproc->getline())) {
240 * We generate %line directives if needed for later programs
242 int32_t linnum = prior_linnum += lineinc;
243 int altline = src_get(&linnum, &file_name);
245 if (altline == 1 && lineinc == 1)
246 nasm_fputs("", ofile);
248 lineinc = (altline != -1 || lineinc != 1);
249 fprintf(ofile ? ofile : stdout,
250 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
253 prior_linnum = linnum;
255 nasm_fputs(line, ofile);
258 nasm_free(file_name);
262 if (ofile && terminate_after_phase)
270 * We must call ofmt->filename _anyway_, even if the user
271 * has specified their own output file, because some
272 * formats (eg OBJ and COFF) use ofmt->filename to find out
273 * the name of the input file and then put that inside the
276 ofmt->filename(inname, outname, report_error);
278 ofile = fopen(outname, "wb");
280 report_error(ERR_FATAL | ERR_NOFILE,
281 "unable to open output file `%s'", outname);
285 * We must call init_labels() before ofmt->init() since
286 * some object formats will want to define labels in their
287 * init routines. (eg OS/2 defines the FLAT group)
291 ofmt->init(ofile, report_error, define_label, evaluate);
293 assemble_file(inname);
295 if (!terminate_after_phase) {
296 ofmt->cleanup(using_debug_info);
300 * We had an fclose on the output file here, but we
301 * actually do that in all the object file drivers as well,
302 * so we're leaving out the one here.
321 if (terminate_after_phase)
328 * Get a parameter for a command line option.
329 * First arg must be in the form of e.g. -f...
331 static char *get_param(char *p, char *q, int *advance)
334 if (p[2]) { /* the parameter's in the option */
344 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
345 "option `-%c' requires an argument", p[1]);
355 #define OPT_POSTFIX 1
356 struct textargs textopts[] = {
357 {"prefix", OPT_PREFIX},
358 {"postfix", OPT_POSTFIX},
363 static int process_arg(char *p, char *q)
371 if (p[0] == '-' && !stopoptions) {
376 case 'o': /* these parameters take values */
391 if (!(param = get_param(p, q, &advance)))
393 if (p[1] == 'o') { /* output file */
394 strcpy(outname, param);
395 } else if (p[1] == 'f') { /* output format */
396 ofmt = ofmt_find(param);
398 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
399 "unrecognised output format `%s' - "
400 "use -hf for a list", param);
402 ofmt->current_dfmt = ofmt->debug_formats[0];
403 } else if (p[1] == 'O') { /* Optimization level */
407 if (isdigit(*param)) {
409 while (isdigit(*++param)) ;
411 optimizing = -1; /* 0.98 behaviour */
413 optimizing = 0; /* Two passes, 0.98.09 behavior */
415 /* The optimizer seems to have problems with
416 < 5 passes? Hidden bug? */
417 optimizing = 5; /* 5 passes */
419 optimizing = opt; /* More than 5 passes */
421 if (*param == 'v' || *param == '+') {
423 opt_verbose_info = TRUE;
425 } else { /* garbage */
430 } /* while (*param) */
432 report_error(ERR_FATAL,
433 "command line optimization level must be 'v', 0..3 or <nn>");
434 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
435 pp_pre_include(param);
436 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
437 pp_pre_define(param);
438 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
439 pp_pre_undefine(param);
440 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
441 pp_include_path(param);
442 } else if (p[1] == 'l') { /* listing file */
443 strcpy(listname, param);
444 } else if (p[1] == 'Z') { /* error messages file */
445 error_file = fopen(param, "w");
447 error_file = stderr; /* Revert to default! */
448 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
449 "cannot open file `%s' for error messages",
452 } else if (p[1] == 'F') { /* specify debug format */
453 ofmt->current_dfmt = dfmt_find(ofmt, param);
454 if (!ofmt->current_dfmt) {
455 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
456 "unrecognized debug format `%s' for"
457 " output format `%s'",
458 param, ofmt->shortname);
460 } else if (p[1] == 'X') { /* specify error reporting format */
461 if (nasm_stricmp("vc", param) == 0)
462 report_error = report_error_vc;
463 else if (nasm_stricmp("gnu", param) == 0)
464 report_error = report_error_gnu;
466 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
467 "unrecognized error reporting format `%s'",
472 using_debug_info = TRUE;
476 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
478 " [options...] [--] filename\n"
479 " or nasm -v for version info\n\n"
480 " -t assemble in SciTech TASM compatible mode\n"
481 " -g generate debug information in selected format.\n");
483 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
484 " -a don't preprocess (assemble only)\n"
485 " -M generate Makefile dependencies on stdout\n"
486 " -MG d:o, missing files assumed generated\n\n"
487 " -Z<file> redirect error messages to file\n"
488 " -s redirect error messages to stdout\n\n"
489 " -F format select a debugging format\n\n"
490 " -I<path> adds a pathname to the include file path\n");
492 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
493 " -P<file> pre-includes a file\n"
494 " -D<macro>[=<value>] pre-defines a macro\n"
495 " -U<macro> undefines a macro\n"
496 " -X<format> specifies error reporting format (gnu or vc)\n"
497 " -w+foo enables warnings about foo; -w-foo disables them\n"
498 "where foo can be:\n");
499 for (i = 1; i <= ERR_WARN_MAX; i++)
500 printf(" %-23s %s (default %s)\n",
501 suppressed_names[i], suppressed_what[i],
502 suppressed[i] ? "off" : "on");
504 ("\nresponse files should contain command line parameters"
505 ", one per line.\n");
507 printf("\nvalid output formats for -f are"
508 " (`*' denotes default):\n");
509 ofmt_list(ofmt, stdout);
511 printf("\nFor a list of valid output formats, use -hf.\n");
512 printf("For a list of debug formats, use -f <form> -y.\n");
514 exit(0); /* never need usage message here */
517 printf("\nvalid debug formats for '%s' output format are"
518 " ('*' denotes default):\n", ofmt->shortname);
519 dfmt_list(ofmt, stdout);
523 tasm_compatible_mode = TRUE;
527 const char *nasm_version_string =
528 "NASM version " NASM_VER " compiled on " __DATE__
533 puts(nasm_version_string);
534 exit(0); /* never need usage message here */
537 case 'e': /* preprocess only */
539 operating_mode = op_preprocess;
541 case 'a': /* assemble only - don't preprocess */
545 if (p[2] != '+' && p[2] != '-') {
546 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
547 "invalid option to `-w'");
549 for (i = 1; i <= ERR_WARN_MAX; i++)
550 if (!nasm_stricmp(p + 3, suppressed_names[i]))
552 if (i <= ERR_WARN_MAX)
553 suppressed[i] = (p[2] == '-');
555 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
556 "invalid option to `-w'");
560 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
567 if (p[2] == 0) { /* -- => stop processing options */
571 for (s = 0; textopts[s].label; s++) {
572 if (!nasm_stricmp(p + 2, textopts[s].label)) {
583 report_error(ERR_NONFATAL | ERR_NOFILE |
585 "option `--%s' requires an argument",
589 advance = 1, param = q;
592 if (s == OPT_PREFIX) {
593 strncpy(lprefix, param, PREFIX_MAX - 1);
594 lprefix[PREFIX_MAX - 1] = 0;
597 if (s == OPT_POSTFIX) {
598 strncpy(lpostfix, param, POSTFIX_MAX - 1);
599 lpostfix[POSTFIX_MAX - 1] = 0;
606 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
607 "unrecognised option `--%s'", p + 2);
615 if (!ofmt->setinfo(GI_SWITCH, &p))
616 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
617 "unrecognised option `-%c'", p[1]);
622 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
623 "more than one input file specified");
631 #define ARG_BUF_DELTA 128
633 static void process_respfile(FILE * rfile)
635 char *buffer, *p, *q, *prevarg;
636 int bufsize, prevargsize;
638 bufsize = prevargsize = ARG_BUF_DELTA;
639 buffer = nasm_malloc(ARG_BUF_DELTA);
640 prevarg = nasm_malloc(ARG_BUF_DELTA);
643 while (1) { /* Loop to handle all lines in file */
646 while (1) { /* Loop to handle long lines */
647 q = fgets(p, bufsize - (p - buffer), rfile);
651 if (p > buffer && p[-1] == '\n')
653 if (p - buffer > bufsize - 10) {
656 bufsize += ARG_BUF_DELTA;
657 buffer = nasm_realloc(buffer, bufsize);
662 if (!q && p == buffer) {
664 process_arg(prevarg, NULL);
671 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
672 * them are present at the end of the line.
674 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
676 while (p > buffer && isspace(p[-1]))
683 if (process_arg(prevarg, p))
686 if (strlen(p) > prevargsize - 10) {
687 prevargsize += ARG_BUF_DELTA;
688 prevarg = nasm_realloc(prevarg, prevargsize);
694 /* Function to process args from a string of args, rather than the
695 * argv array. Used by the environment variable and response file
698 static void process_args(char *args)
700 char *p, *q, *arg, *prevarg;
701 char separator = ' ';
709 while (*p && *p != separator)
711 while (*p == separator)
715 if (process_arg(prevarg, arg))
719 process_arg(arg, NULL);
722 static void parse_cmdline(int argc, char **argv)
725 char *envreal, *envcopy = NULL, *p, *arg;
727 *inname = *outname = *listname = '\0';
730 * First, process the NASMENV environment variable.
732 envreal = getenv("NASMENV");
735 envcopy = nasm_strdup(envreal);
736 process_args(envcopy);
741 * Now process the actual command line.
746 if (argv[0][0] == '@') {
747 /* We have a response file, so process this as a set of
748 * arguments like the environment variable. This allows us
749 * to have multiple arguments on a single line, which is
750 * different to the -@resp file processing below for regular
753 char *str = malloc(2048);
754 FILE *f = fopen(&argv[0][1], "r");
756 printf("out of memory");
760 while (fgets(str, 2048, f)) {
769 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
770 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
772 rfile = fopen(p, "r");
774 process_respfile(rfile);
777 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
778 "unable to open response file `%s'", p);
781 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
782 argv += i, argc -= i;
786 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
787 "no input file specified");
790 /* List of directives */
792 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
793 D_EXTERN, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
795 static const char *directives[] = {
796 "", "absolute", "bits", "common", "cpu", "debug", "default",
797 "extern", "global", "list", "section", "segment", "warning"
800 static void assemble_file(char *fname)
802 char *directive, *value, *p, *q, *special, *line, debugid[80];
804 int i, rn_error, validid;
806 struct tokenval tokval;
809 int pass_cnt = 0; /* count actual passes */
811 if (cmd_sb == 32 && cmd_cpu < IF_386)
812 report_error(ERR_FATAL, "command line: "
813 "32-bit segment size requires a higher cpu");
815 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
816 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
817 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
821 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
822 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
823 /* pass0 seq is 0, 0, 0,..., 1, 2 */
825 def_label = pass > 1 ? redefine_label : define_label;
827 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
831 nasmlist.init(listname, report_error);
834 global_offset_changed = FALSE; /* set by redefine_label */
835 location.segment = ofmt->section(NULL, pass2, &sb);
838 saa_rewind(forwrefs);
839 forwref = saa_rstruct(forwrefs);
841 offsets = raa_init();
843 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
846 location.known = TRUE;
847 location.offset = offs = GET_CURR_OFFS;
849 while ((line = preproc->getline())) {
852 /* here we parse our directives; this is not handled by the 'real'
855 i = getkw(&directive, &value);
860 case D_SEGMENT: /* [SEGMENT n] */
862 seg = ofmt->section(value, pass2, &sb);
864 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
865 "segment name `%s' not recognized",
869 location.segment = seg;
872 case D_EXTERN: /* [EXTERN label:special] */
874 value++; /* skip initial $ if present */
877 while (*q && *q != ':')
881 ofmt->symdef(value, 0L, 0L, 3, q);
883 } else if (pass == 1) { /* pass == 1 */
888 while (*q && *q != ':') {
894 report_error(ERR_NONFATAL,
895 "identifier expected after EXTERN");
903 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
905 pass0 = 1; /* fake pass 1 in labels.c */
906 declare_as_global(value, special,
908 define_label(value, seg_alloc(), 0L, NULL,
909 FALSE, TRUE, ofmt, report_error);
912 } /* else pass0 == 1 */
914 case D_BITS: /* [BITS bits] */
915 globalbits = sb = get_bits(value);
917 case D_GLOBAL: /* [GLOBAL symbol:special] */
919 value++; /* skip initial $ if present */
920 if (pass0 == 2) { /* pass 2 */
922 while (*q && *q != ':')
926 ofmt->symdef(value, 0L, 0L, 3, q);
928 } else if (pass2 == 1) { /* pass == 1 */
933 while (*q && *q != ':') {
939 report_error(ERR_NONFATAL,
940 "identifier expected after GLOBAL");
948 declare_as_global(value, special, report_error);
951 case D_COMMON: /* [COMMON symbol size:special] */
953 value++; /* skip initial $ if present */
959 while (*p && !isspace(*p)) {
965 report_error(ERR_NONFATAL,
966 "identifier expected after COMMON");
972 while (*p && isspace(*p))
975 while (*q && *q != ':')
982 size = readnum(p, &rn_error);
984 report_error(ERR_NONFATAL,
985 "invalid size specified"
986 " in COMMON declaration");
988 define_common(value, seg_alloc(), size,
989 special, ofmt, report_error);
991 report_error(ERR_NONFATAL,
992 "no size specified in"
993 " COMMON declaration");
994 } else if (pass0 == 2) { /* pass == 2 */
996 while (*q && *q != ':') {
1003 ofmt->symdef(value, 0L, 0L, 3, q);
1007 case D_ABSOLUTE: /* [ABSOLUTE address] */
1009 stdscan_bufptr = value;
1010 tokval.t_type = TOKEN_INVALID;
1011 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1012 report_error, NULL);
1015 report_error(pass0 ==
1016 1 ? ERR_NONFATAL : ERR_PANIC,
1017 "cannot use non-relocatable expression as "
1018 "ABSOLUTE address");
1020 abs_seg = reloc_seg(e);
1021 abs_offset = reloc_value(e);
1023 } else if (pass == 1)
1024 abs_offset = 0x100; /* don't go near zero in case of / */
1026 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1029 location.segment = NO_SEG;
1031 case D_DEBUG: /* [DEBUG] */
1037 while (*p && !isspace(*p)) {
1044 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1045 "identifier expected after DEBUG");
1048 while (*p && isspace(*p))
1050 if (pass == pass_max)
1051 ofmt->current_dfmt->debug_directive(debugid, p);
1053 case D_WARNING: /* [WARNING {+|-}warn-name] */
1055 while (*value && isspace(*value))
1058 if (*value == '+' || *value == '-') {
1059 validid = (*value == '-') ? TRUE : FALSE;
1064 for (i = 1; i <= ERR_WARN_MAX; i++)
1065 if (!nasm_stricmp(value, suppressed_names[i]))
1067 if (i <= ERR_WARN_MAX)
1068 suppressed[i] = validid;
1070 report_error(ERR_NONFATAL,
1071 "invalid warning id in WARNING directive");
1074 case D_CPU: /* [CPU] */
1075 cpu = get_cpu(value);
1077 case D_LIST: /* [LIST {+|-}] */
1078 while (*value && isspace(*value))
1081 if (*value == '+') {
1084 if (*value == '-') {
1091 case D_DEFAULT: /* [DEFAULT] */
1093 stdscan_bufptr = value;
1094 tokval.t_type = TOKEN_INVALID;
1095 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1096 switch ((int)tokval.t_integer) {
1112 if (!ofmt->directive(directive, value, pass2))
1113 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1114 "unrecognised directive [%s]",
1118 report_error(ERR_NONFATAL,
1119 "invalid parameter to [%s] directive",
1122 } else { /* it isn't a directive */
1124 parse_line(pass1, line, &output_ins,
1125 report_error, evaluate, def_label);
1127 if (!(optimizing > 0) && pass == 2) {
1128 if (forwref != NULL && globallineno == forwref->lineno) {
1129 output_ins.forw_ref = TRUE;
1131 output_ins.oprs[forwref->operand].opflags |=
1133 forwref = saa_rstruct(forwrefs);
1134 } while (forwref != NULL
1135 && forwref->lineno == globallineno);
1137 output_ins.forw_ref = FALSE;
1140 if (!(optimizing > 0) && output_ins.forw_ref) {
1142 for (i = 0; i < output_ins.operands; i++) {
1143 if (output_ins.oprs[i].
1144 opflags & OPFLAG_FORWARD) {
1145 struct forwrefinfo *fwinf =
1146 (struct forwrefinfo *)
1147 saa_wstruct(forwrefs);
1148 fwinf->lineno = globallineno;
1152 } else { /* pass == 2 */
1154 * Hack to prevent phase error in the code
1158 * If the second operand is a forward reference,
1159 * the UNITY property of the number 1 in that
1160 * operand is cancelled. Otherwise the above
1161 * sequence will cause a phase error.
1163 * This hack means that the above code will
1164 * generate 286+ code.
1166 * The forward reference will mean that the
1167 * operand will not have the UNITY property on
1168 * the first pass, so the pass behaviours will
1172 if (output_ins.operands >= 2 &&
1173 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1174 !(IMMEDIATE & ~output_ins.oprs[1].type))
1176 /* Remove special properties bits */
1177 output_ins.oprs[1].type &= ~REG_SMASK;
1185 if (output_ins.opcode == I_EQU) {
1188 * Special `..' EQUs get processed in pass two,
1189 * except `..@' macro-processor EQUs which are done
1190 * in the normal place.
1192 if (!output_ins.label)
1193 report_error(ERR_NONFATAL,
1194 "EQU not preceded by label");
1196 else if (output_ins.label[0] != '.' ||
1197 output_ins.label[1] != '.' ||
1198 output_ins.label[2] == '@') {
1199 if (output_ins.operands == 1 &&
1200 (output_ins.oprs[0].type & IMMEDIATE) &&
1201 output_ins.oprs[0].wrt == NO_SEG) {
1204 opflags & OPFLAG_EXTERN;
1205 def_label(output_ins.label,
1206 output_ins.oprs[0].segment,
1207 output_ins.oprs[0].offset, NULL,
1210 } else if (output_ins.operands == 2
1211 && (output_ins.oprs[0].
1213 && (output_ins.oprs[0].type & COLON)
1214 && output_ins.oprs[0].segment ==
1216 && output_ins.oprs[0].wrt == NO_SEG
1217 && (output_ins.oprs[1].
1219 && output_ins.oprs[1].segment ==
1221 && output_ins.oprs[1].wrt ==
1223 def_label(output_ins.label,
1226 output_ins.oprs[1].offset, NULL,
1230 report_error(ERR_NONFATAL,
1231 "bad syntax for EQU");
1233 } else { /* pass == 2 */
1235 * Special `..' EQUs get processed here, except
1236 * `..@' macro processor EQUs which are done above.
1238 if (output_ins.label[0] == '.' &&
1239 output_ins.label[1] == '.' &&
1240 output_ins.label[2] != '@') {
1241 if (output_ins.operands == 1 &&
1242 (output_ins.oprs[0].type & IMMEDIATE)) {
1243 define_label(output_ins.label,
1244 output_ins.oprs[0].segment,
1245 output_ins.oprs[0].offset,
1246 NULL, FALSE, FALSE, ofmt,
1248 } else if (output_ins.operands == 2
1249 && (output_ins.oprs[0].
1251 && (output_ins.oprs[0].type & COLON)
1252 && output_ins.oprs[0].segment ==
1254 && (output_ins.oprs[1].
1256 && output_ins.oprs[1].segment ==
1258 define_label(output_ins.label,
1261 output_ins.oprs[1].offset,
1262 NULL, FALSE, FALSE, ofmt,
1265 report_error(ERR_NONFATAL,
1266 "bad syntax for EQU");
1269 } else { /* instruction isn't an EQU */
1273 int32_t l = insn_size(location.segment, offs, sb, cpu,
1274 &output_ins, report_error);
1276 /* if (using_debug_info) && output_ins.opcode != -1) */
1277 if (using_debug_info)
1278 { /* fbk 03/25/01 */
1279 /* this is done here so we can do debug type info */
1281 TYS_ELEMENTS(output_ins.operands);
1282 switch (output_ins.opcode) {
1285 TYS_ELEMENTS(output_ins.oprs[0].
1290 TYS_ELEMENTS(output_ins.oprs[0].
1295 TYS_ELEMENTS(output_ins.oprs[0].
1300 TYS_ELEMENTS(output_ins.oprs[0].
1305 TYS_ELEMENTS(output_ins.oprs[0].
1309 typeinfo |= TY_BYTE;
1312 typeinfo |= TY_WORD;
1315 if (output_ins.eops_float)
1316 typeinfo |= TY_FLOAT;
1318 typeinfo |= TY_DWORD;
1321 typeinfo |= TY_QWORD;
1324 typeinfo |= TY_TBYTE;
1327 typeinfo |= TY_OWORD;
1330 typeinfo = TY_LABEL;
1334 ofmt->current_dfmt->debug_typevalue(typeinfo);
1339 SET_CURR_OFFS(offs);
1342 * else l == -1 => invalid instruction, which will be
1343 * flagged as an error on pass 2
1346 } else { /* pass == 2 */
1347 offs += assemble(location.segment, offs, sb, cpu,
1348 &output_ins, ofmt, report_error,
1350 SET_CURR_OFFS(offs);
1354 cleanup_insn(&output_ins);
1357 location.offset = offs = GET_CURR_OFFS;
1358 } /* end while (line = preproc->getline... */
1360 if (pass1 == 2 && global_offset_changed)
1361 report_error(ERR_NONFATAL,
1362 "phase error detected at end of assembly.");
1365 preproc->cleanup(1);
1367 if (pass1 == 1 && terminate_after_phase) {
1375 if (pass > 1 && !global_offset_changed) {
1378 pass = pass_max - 1;
1379 } else if (!(optimizing > 0))
1382 } /* for (pass=1; pass<=2; pass++) */
1384 preproc->cleanup(0);
1387 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1389 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1391 } /* exit from assemble_file (...) */
1393 static int getkw(char **directive, char **value)
1399 /* allow leading spaces or tabs */
1400 while (*buf == ' ' || *buf == '\t')
1408 while (*p && *p != ']')
1416 while (*p && *p != ';') {
1423 *directive = p = buf + 1;
1424 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1431 while (isspace(*buf))
1432 buf++; /* beppu - skip leading whitespace */
1439 return bsii(*directive, directives, elements(directives));
1443 * gnu style error reporting
1444 * This function prints an error message to error_file in the
1445 * style used by GNU. An example would be:
1446 * file.asm:50: error: blah blah blah
1447 * where file.asm is the name of the file, 50 is the line number on
1448 * which the error occurs (or is detected) and "error:" is one of
1449 * the possible optional diagnostics -- it can be "error" or "warning"
1450 * or something else. Finally the line terminates with the actual
1453 * @param severity the severity of the warning or error
1454 * @param fmt the printf style format string
1456 static void report_error_gnu(int severity, const char *fmt, ...)
1460 if (is_suppressed_warning(severity))
1463 if (severity & ERR_NOFILE)
1464 fputs("nasm: ", error_file);
1466 char *currentfile = NULL;
1468 src_get(&lineno, ¤tfile);
1469 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1470 nasm_free(currentfile);
1473 report_error_common(severity, fmt, ap);
1478 * MS style error reporting
1479 * This function prints an error message to error_file in the
1480 * style used by Visual C and some other Microsoft tools. An example
1482 * file.asm(50) : error: blah blah blah
1483 * where file.asm is the name of the file, 50 is the line number on
1484 * which the error occurs (or is detected) and "error:" is one of
1485 * the possible optional diagnostics -- it can be "error" or "warning"
1486 * or something else. Finally the line terminates with the actual
1489 * @param severity the severity of the warning or error
1490 * @param fmt the printf style format string
1492 static void report_error_vc(int severity, const char *fmt, ...)
1496 if (is_suppressed_warning(severity))
1499 if (severity & ERR_NOFILE)
1500 fputs("nasm: ", error_file);
1502 char *currentfile = NULL;
1504 src_get(&lineno, ¤tfile);
1505 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1506 nasm_free(currentfile);
1509 report_error_common(severity, fmt, ap);
1514 * check for supressed warning
1515 * checks for suppressed warning or pass one only warning and we're
1518 * @param severity the severity of the warning or error
1519 * @return true if we should abort error/warning printing
1521 static int is_suppressed_warning(int severity)
1524 * See if it's a suppressed warning.
1526 return ((severity & ERR_MASK) == ERR_WARNING &&
1527 (severity & ERR_WARN_MASK) != 0 &&
1528 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1530 * See if it's a pass-one only warning and we're not in pass one.
1532 ((severity & ERR_PASS1) && pass0 == 2);
1536 * common error reporting
1537 * This is the common back end of the error reporting schemes currently
1538 * implemented. It prints the nature of the warning and then the
1539 * specific error message to error_file and may or may not return. It
1540 * doesn't return if the error severity is a "panic" or "debug" type.
1542 * @param severity the severity of the warning or error
1543 * @param fmt the printf style format string
1545 static void report_error_common(int severity, const char *fmt,
1548 switch (severity & ERR_MASK) {
1550 fputs("warning: ", error_file);
1553 fputs("error: ", error_file);
1556 fputs("fatal: ", error_file);
1559 fputs("panic: ", error_file);
1562 fputs("debug: ", error_file);
1566 vfprintf(error_file, fmt, args);
1567 fputc('\n', error_file);
1569 if (severity & ERR_USAGE)
1572 switch (severity & ERR_MASK) {
1575 /* no further action, by definition */
1578 /* hack enables listing(!) on errors */
1579 terminate_after_phase = TRUE;
1588 exit(1); /* instantly die */
1589 break; /* placate silly compilers */
1592 /* abort(); *//* halt, catch fire, and dump core */
1598 static void usage(void)
1600 fputs("type `nasm -h' for help\n", error_file);
1603 static void register_output_formats(void)
1605 ofmt = ofmt_register(report_error);
1608 #define BUF_DELTA 512
1610 static FILE *no_pp_fp;
1611 static efunc no_pp_err;
1612 static ListGen *no_pp_list;
1613 static int32_t no_pp_lineinc;
1615 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1618 src_set_fname(nasm_strdup(file));
1622 no_pp_fp = fopen(file, "r");
1624 no_pp_err(ERR_FATAL | ERR_NOFILE,
1625 "unable to open input file `%s'", file);
1626 no_pp_list = listgen;
1627 (void)pass; /* placate compilers */
1628 (void)eval; /* placate compilers */
1631 static char *no_pp_getline(void)
1633 char *buffer, *p, *q;
1636 bufsize = BUF_DELTA;
1637 buffer = nasm_malloc(BUF_DELTA);
1638 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1640 while (1) { /* Loop to handle %line */
1643 while (1) { /* Loop to handle long lines */
1644 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1648 if (p > buffer && p[-1] == '\n')
1650 if (p - buffer > bufsize - 10) {
1652 offset = p - buffer;
1653 bufsize += BUF_DELTA;
1654 buffer = nasm_realloc(buffer, bufsize);
1655 p = buffer + offset;
1659 if (!q && p == buffer) {
1665 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1666 * them are present at the end of the line.
1668 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1670 if (!strncmp(buffer, "%line", 5)) {
1673 char *nm = nasm_malloc(strlen(buffer));
1674 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1675 nasm_free(src_set_fname(nm));
1685 no_pp_list->line(LIST_READ, buffer);
1690 static void no_pp_cleanup(int pass)
1692 (void)pass; /* placate GCC */
1696 static uint32_t get_cpu(char *value)
1699 if (!strcmp(value, "8086"))
1701 if (!strcmp(value, "186"))
1703 if (!strcmp(value, "286"))
1705 if (!strcmp(value, "386"))
1707 if (!strcmp(value, "486"))
1709 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1711 if (!strcmp(value, "686") ||
1712 !nasm_stricmp(value, "ppro") ||
1713 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1715 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1717 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1718 !nasm_stricmp(value, "willamette"))
1719 return IF_WILLAMETTE;
1720 if (!nasm_stricmp(value, "prescott"))
1722 if (!nasm_stricmp(value, "x64") ||
1723 !nasm_stricmp(value, "x86-64"))
1725 if (!nasm_stricmp(value, "ia64") ||
1726 !nasm_stricmp(value, "ia-64") ||
1727 !nasm_stricmp(value, "itanium") ||
1728 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1731 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1732 "unknown 'cpu' type");
1734 return IF_PLEVEL; /* the maximum level */
1737 static int get_bits(char *value)
1741 if ((i = atoi(value)) == 16)
1742 return i; /* set for a 16-bit segment */
1745 report_error(ERR_NONFATAL,
1746 "cannot specify 32-bit segment on processor below a 386");
1749 } else if (i == 64) {
1750 if (cpu < IF_X86_64) {
1751 report_error(ERR_NONFATAL,
1752 "cannot specify 64-bit segment on processor below an x86-64");
1756 report_error(ERR_NONFATAL,
1757 "%s output format does not support 64-bit code",
1762 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1763 "`%s' is not a valid segment size; must be 16, 32 or 64",