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.
26 struct forwrefinfo { /* info held on forward refs. */
31 static int get_bits (char *value);
32 static unsigned long get_cpu (char *cpu_str);
33 static void parse_cmdline (int, char **);
34 static void assemble_file (char *);
35 static int getkw (char **directive, char **value);
36 static void register_output_formats(void);
37 static void report_error_gnu (int severity, const char *fmt, ...);
38 static void report_error_vc (int severity, const char *fmt, ...);
39 static void report_error_common (int severity, const char *fmt, va_list args);
40 static int is_suppressed_warning (int severity);
41 static void usage(void);
42 static efunc report_error;
44 static int using_debug_info, opt_verbose_info;
45 int tasm_compatible_mode = FALSE;
48 static char inname[FILENAME_MAX];
49 static char outname[FILENAME_MAX];
50 static char listname[FILENAME_MAX];
51 static int globallineno; /* for forward-reference tracking */
52 /* static int pass = 0; */
53 static struct ofmt *ofmt = NULL;
55 static FILE *error_file; /* Where to write error messages */
57 static FILE *ofile = NULL;
58 int optimizing = -1; /* number of optimization passes to take */
59 static int sb, cmd_sb = 16; /* by default */
60 static unsigned long cmd_cpu = IF_PLEVEL; /* highest level by default */
61 static unsigned long cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
62 int global_offset_changed; /* referenced in labels.c */
64 static loc_t location;
65 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
66 long abs_seg; /* ABSOLUTE segment basis */
67 long abs_offset; /* ABSOLUTE offset */
69 static struct RAA *offsets;
71 static struct SAA *forwrefs; /* keep track of forward references */
72 static struct forwrefinfo *forwref;
74 static Preproc *preproc;
76 op_normal, /* Preprocess and assemble */
77 op_preprocess, /* Preprocess only */
78 op_depend /* Generate dependencies */
80 static enum op_type operating_mode;
83 * Which of the suppressible warnings are suppressed. Entry zero
84 * doesn't do anything. Initial defaults are given here.
86 static char suppressed[1+ERR_WARN_MAX] = {
87 0, TRUE, TRUE, TRUE, FALSE, TRUE
91 * The option names for the suppressible warnings. As before, entry
94 static const char *suppressed_names[1+ERR_WARN_MAX] = {
95 NULL, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
100 * The explanations for the suppressible warnings. As before, entry
103 static const char *suppressed_what[1+ERR_WARN_MAX] = {
105 "macro calls with wrong no. of params",
106 "cyclic macro self-references",
107 "labels alone on lines without trailing `:'",
108 "numeric constants greater than 0xFFFFFFFF",
109 "using 8- or 16-bit relocation in ELF, a GNU extension"
113 * This is a null preprocessor which just copies lines from input
114 * to output. It's used when someone explicitly requests that NASM
115 * not preprocess their source file.
118 static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
119 static char *no_pp_getline (void);
120 static void no_pp_cleanup (int);
121 static Preproc no_pp = {
128 * get/set current offset...
130 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
131 raa_read(offsets,location.segment))
132 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
133 (void)(offsets=raa_write(offsets,location.segment,(x))))
135 static int want_usage;
136 static int terminate_after_phase;
137 int user_nolist = 0; /* fbk 9/2/00 */
139 static void nasm_fputs(const char *line, FILE *outfile)
142 fputs(line, outfile);
143 fputc('\n', outfile);
148 int main(int argc, char **argv)
151 want_usage = terminate_after_phase = FALSE;
152 report_error = report_error_gnu;
154 nasm_set_malloc_error (report_error);
155 offsets = raa_init();
156 forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
159 operating_mode = op_normal;
165 register_output_formats();
167 parse_cmdline(argc, argv);
169 if (terminate_after_phase)
176 /* If debugging info is disabled, suppress any debug calls */
177 if (!using_debug_info)
178 ofmt->current_dfmt = &null_debug_form;
181 pp_extra_stdmac (ofmt->stdmac);
182 parser_global_info (ofmt, &location);
183 eval_global_info (ofmt, lookup_label, &location);
185 /* define some macros dependent of command-line */
188 sprintf (temp, "__OUTPUT_FORMAT__=%s\n", ofmt->shortname);
189 pp_pre_define (temp);
192 switch ( operating_mode ) {
196 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
197 if (outname[0] == '\0')
198 ofmt->filename (inname, outname, report_error);
200 fprintf(stdout, "%s: %s", outname, inname);
201 while ( (line = preproc->getline()) )
211 char *file_name = NULL;
216 ofile = fopen(outname, "w");
218 report_error (ERR_FATAL | ERR_NOFILE,
219 "unable to open output file `%s'", outname);
223 location.known = FALSE;
226 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
227 while ( (line = preproc->getline()) ) {
229 * We generate %line directives if needed for later programs
231 long linnum = prior_linnum += lineinc;
232 int altline = src_get(&linnum, &file_name);
234 if (altline==1 && lineinc==1)
235 nasm_fputs("", ofile);
237 lineinc = (altline != -1 || lineinc!=1);
238 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
239 linnum, lineinc, file_name);
241 prior_linnum = linnum;
243 nasm_fputs(line, ofile);
246 nasm_free(file_name);
250 if (ofile && terminate_after_phase)
258 * We must call ofmt->filename _anyway_, even if the user
259 * has specified their own output file, because some
260 * formats (eg OBJ and COFF) use ofmt->filename to find out
261 * the name of the input file and then put that inside the
264 ofmt->filename (inname, outname, report_error);
266 ofile = fopen(outname, "wb");
268 report_error (ERR_FATAL | ERR_NOFILE,
269 "unable to open output file `%s'", outname);
273 * We must call init_labels() before ofmt->init() since
274 * some object formats will want to define labels in their
275 * init routines. (eg OS/2 defines the FLAT group)
279 ofmt->init (ofile, report_error, define_label, evaluate);
281 assemble_file (inname);
283 if (!terminate_after_phase) {
284 ofmt->cleanup (using_debug_info);
288 * We had an fclose on the output file here, but we
289 * actually do that in all the object file drivers as well,
290 * so we're leaving out the one here.
309 if (terminate_after_phase)
317 * Get a parameter for a command line option.
318 * First arg must be in the form of e.g. -f...
320 static char *get_param (char *p, char *q, int *advance)
323 if (p[2]) /* the parameter's in the option */
335 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
336 "option `-%c' requires an argument",
348 #define OPT_POSTFIX 1
349 struct textargs textopts[] =
351 {"prefix",OPT_PREFIX},
352 {"postfix",OPT_POSTFIX},
358 static int process_arg (char *p, char *q)
366 if (p[0]=='-' && ! stopoptions)
372 case 'o': /* these parameters take values */
387 if ( !(param = get_param (p, q, &advance)) )
389 if (p[1]=='o') { /* output file */
390 strcpy (outname, param);
391 } else if (p[1]=='f') { /* output format */
392 ofmt = ofmt_find(param);
394 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
395 "unrecognised output format `%s' - "
396 "use -hf for a list",
400 ofmt->current_dfmt = ofmt->debug_formats[0];
401 } else if (p[1]=='O') { /* Optimization level */
405 if (isdigit(*param)) {
407 while(isdigit(*++param)) ;
408 if (opt<=0) optimizing = -1; /* 0.98 behaviour */
409 else if (opt==1) optimizing = 0; /* Two passes, 0.98.09 behavior */
410 else optimizing = opt; /* Multiple passes */
412 if (*param == 'v' || *param == '+') {
414 opt_verbose_info = TRUE;
416 } else { /* garbage */
421 } /* while (*param) */
422 if (opt == -99) report_error(ERR_FATAL,
423 "command line optimization level must be 'v', 0..3 or <nn>");
424 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
425 pp_pre_include (param);
426 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
427 pp_pre_define (param);
428 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
429 pp_pre_undefine (param);
430 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
431 pp_include_path (param);
432 } else if (p[1]=='l') { /* listing file */
433 strcpy (listname, param);
434 } else if (p[1]=='E') { /* error messages file */
435 error_file = fopen(param, "w");
437 error_file = stderr; /* Revert to default! */
438 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
439 "cannot open file `%s' for error messages",
442 } else if (p[1] == 'F') { /* specify debug format */
443 ofmt->current_dfmt = dfmt_find(ofmt, param);
444 if (!ofmt->current_dfmt) {
445 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
446 "unrecognized debug format `%s' for"
447 " output format `%s'",
448 param, ofmt->shortname);
450 } else if (p[1] == 'X') { /* specify error reporting format */
451 if (nasm_stricmp("vc", param) == 0)
452 report_error = report_error_vc;
453 else if (nasm_stricmp("gnu", param) == 0)
454 report_error = report_error_gnu;
456 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
457 "unrecognized error reporting format `%s'",
462 using_debug_info = TRUE;
465 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
467 " [options...] [--] filename\n"
468 " or nasm -r for version info (obsolete)\n"
469 " or nasm -v for version info (preferred)\n\n"
470 " -t Assemble in SciTech TASM compatible mode\n"
471 " -g Generate debug information in selected format.\n");
472 printf(" -e preprocess only (writes output to stdout by default)\n"
473 " -a don't preprocess (assemble only)\n"
474 " -M generate Makefile dependencies on stdout\n\n"
475 " -E<file> redirect error messages to file\n"
476 " -s redirect error messages to stdout\n\n"
477 " -F format select a debugging format\n\n"
478 " -I<path> adds a pathname to the include file path\n");
479 printf(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
480 " -P<file> pre-includes a file\n"
481 " -D<macro>[=<value>] pre-defines a macro\n"
482 " -U<macro> undefines a macro\n"
483 " -X<format> specifies error reporting format (gnu or vc)\n"
484 " -w+foo enables warnings about foo; -w-foo disables them\n"
485 "where foo can be:\n");
486 for (i=1; i<=ERR_WARN_MAX; i++)
487 printf(" %-23s %s (default %s)\n",
488 suppressed_names[i], suppressed_what[i],
489 suppressed[i] ? "off" : "on");
490 printf ("\nresponse files should contain command line parameters"
491 ", one per line.\n");
493 printf("\nvalid output formats for -f are"
494 " (`*' denotes default):\n");
495 ofmt_list(ofmt, stdout);
498 printf ("\nFor a list of valid output formats, use -hf.\n");
499 printf ("For a list of debug formats, use -f <form> -y.\n");
501 exit (0); /* never need usage message here */
504 printf("\nvalid debug formats for '%s' output format are"
505 " ('*' denotes default):\n",
507 dfmt_list(ofmt, stdout);
511 tasm_compatible_mode = TRUE;
516 const char *nasm_version_string =
517 "NASM version " NASM_VER " compiled on " __DATE__
522 puts(nasm_version_string);
523 exit (0); /* never need usage message here */
526 case 'e': /* preprocess only */
527 operating_mode = op_preprocess;
529 case 'a': /* assemble only - don't preprocess */
533 if (p[2] != '+' && p[2] != '-') {
534 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
535 "invalid option to `-w'");
537 for (i=1; i<=ERR_WARN_MAX; i++)
538 if (!nasm_stricmp(p+3, suppressed_names[i]))
540 if (i <= ERR_WARN_MAX)
541 suppressed[i] = (p[2] == '-');
543 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
544 "invalid option to `-w'");
548 operating_mode = op_depend;
555 if (p[2]==0) { /* -- => stop processing options */
559 for(s=0; textopts[s].label; s++)
561 if(!nasm_stricmp(p+2, textopts[s].label))
575 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
576 "option `--%s' requires an argument",
582 advance = 1, param = q;
587 strncpy(lprefix,param,PREFIX_MAX-1);
588 lprefix[PREFIX_MAX-1]=0;
593 strncpy(lpostfix,param,POSTFIX_MAX-1);
594 lpostfix[POSTFIX_MAX-1]=0;
601 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
602 "unrecognised option `--%s'",
611 if (!ofmt->setinfo(GI_SWITCH,&p))
612 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
613 "unrecognised option `-%c'",
621 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
622 "more than one input file specified");
630 #define ARG_BUF_DELTA 128
632 static void process_respfile (FILE *rfile)
634 char *buffer, *p, *q, *prevarg;
635 int bufsize, prevargsize;
637 bufsize = prevargsize = ARG_BUF_DELTA;
638 buffer = nasm_malloc(ARG_BUF_DELTA);
639 prevarg = nasm_malloc(ARG_BUF_DELTA);
642 while (1) { /* Loop to handle all lines in file */
645 while (1) { /* Loop to handle long lines */
646 q = fgets(p, bufsize-(p-buffer), rfile);
650 if (p > buffer && p[-1] == '\n')
652 if (p-buffer > bufsize-10) {
655 bufsize += ARG_BUF_DELTA;
656 buffer = nasm_realloc(buffer, bufsize);
661 if (!q && p == buffer) {
663 process_arg (prevarg, NULL);
670 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
671 * them are present at the end of the line.
673 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
675 while (p > buffer && isspace(p[-1]))
682 if (process_arg (prevarg, p))
685 if (strlen(p) > prevargsize-10) {
686 prevargsize += ARG_BUF_DELTA;
687 prevarg = nasm_realloc(prevarg, prevargsize);
693 /* Function to process args from a string of args, rather than the
694 * argv array. Used by the environment variable and response file
697 static void process_args (char *args) {
698 char *p, *q, *arg, *prevarg;
699 char separator = ' ';
707 while (*p && *p != separator) p++;
708 while (*p == separator) *p++ = '\0';
711 if (process_arg (prevarg, arg))
715 process_arg (arg, NULL);
718 static void parse_cmdline(int argc, char **argv)
721 char *envreal, *envcopy=NULL, *p, *arg;
723 *inname = *outname = *listname = '\0';
726 * First, process the NASMENV environment variable.
728 envreal = getenv("NASMENV");
731 envcopy = nasm_strdup(envreal);
732 process_args(envcopy);
737 * Now process the actual command line.
743 if (argv[0][0] == '@') {
744 /* We have a response file, so process this as a set of
745 * arguments like the environment variable. This allows us
746 * to have multiple arguments on a single line, which is
747 * different to the -@resp file processing below for regular
750 char *str = malloc(2048);
751 FILE *f = fopen(&argv[0][1],"r");
753 printf("out of memory");
757 while (fgets(str,2048,f)) {
766 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
767 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
768 if ((rfile = fopen(p, "r"))) {
769 process_respfile (rfile);
772 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
773 "unable to open response file `%s'", p);
776 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
777 argv += i, argc -= i;
781 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
782 "no input file specified");
786 static void assemble_file (char *fname)
788 char * directive, * value, * p, * q, * special, * line, debugid[80];
790 int i, rn_error, validid;
792 struct tokenval tokval;
795 int pass_cnt = 0; /* count actual passes */
797 if (cmd_sb == 32 && cmd_cpu < IF_386)
798 report_error(ERR_FATAL, "command line: "
799 "32-bit segment size requires a higher cpu");
801 pass_max = (optimizing>0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
802 pass0 = !(optimizing>0); /* start at 1 if not optimizing */
803 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
807 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
808 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
809 /* pass0 seq is 0, 0, 0,..., 1, 2 */
811 def_label = pass > 1 ? redefine_label : define_label;
814 sb = cmd_sb; /* set 'bits' to command line default */
818 nasmlist.init(listname, report_error);
821 global_offset_changed = FALSE; /* set by redefine_label */
822 location.segment = ofmt->section(NULL, pass2, &sb);
824 saa_rewind (forwrefs);
825 forwref = saa_rstruct (forwrefs);
827 offsets = raa_init();
829 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
831 if (pass == 1) location.known = TRUE;
832 location.offset = offs = GET_CURR_OFFS;
834 while ( (line = preproc->getline()) )
838 /* here we parse our directives; this is not handled by the 'real'
841 if ( (i = getkw (&directive, &value)) )
844 case 1: /* [SEGMENT n] */
845 seg = ofmt->section (value, pass2, &sb);
847 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
848 "segment name `%s' not recognised",
852 location.segment = seg;
855 case 2: /* [EXTERN label:special] */
856 if (*value == '$') value++; /* skip initial $ if present */
859 while (*q && *q != ':')
863 ofmt->symdef(value, 0L, 0L, 3, q);
865 } else if (pass == 1) { /* pass == 1 */
870 while (*q && *q != ':') {
876 report_error (ERR_NONFATAL,
877 "identifier expected after EXTERN");
885 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
887 pass0 = 1; /* fake pass 1 in labels.c */
888 declare_as_global (value, special, report_error);
889 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
893 } /* else pass0 == 1 */
895 case 3: /* [BITS bits] */
896 sb = get_bits(value);
898 case 4: /* [GLOBAL symbol:special] */
899 if (*value == '$') value++; /* skip initial $ if present */
900 if (pass0 == 2) { /* pass 2 */
902 while (*q && *q != ':')
906 ofmt->symdef(value, 0L, 0L, 3, q);
908 } else if (pass2 == 1) { /* pass == 1 */
913 while (*q && *q != ':') {
919 report_error (ERR_NONFATAL,
920 "identifier expected after GLOBAL");
928 declare_as_global (value, special, report_error);
931 case 5: /* [COMMON symbol size:special] */
932 if (*value == '$') value++; /* skip initial $ if present */
938 while (*p && !isspace(*p)) {
944 report_error (ERR_NONFATAL,
945 "identifier expected after COMMON");
951 while (*p && isspace(*p))
954 while (*q && *q != ':')
961 size = readnum (p, &rn_error);
963 report_error (ERR_NONFATAL, "invalid size specified"
964 " in COMMON declaration");
966 define_common (value, seg_alloc(), size,
967 special, ofmt, report_error);
969 report_error (ERR_NONFATAL, "no size specified in"
970 " COMMON declaration");
971 } else if (pass0 == 2) { /* pass == 2 */
973 while (*q && *q != ':') {
980 ofmt->symdef(value, 0L, 0L, 3, q);
984 case 6: /* [ABSOLUTE address] */
986 stdscan_bufptr = value;
987 tokval.t_type = TOKEN_INVALID;
988 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, report_error,
992 report_error (pass0==1 ? ERR_NONFATAL : ERR_PANIC,
993 "cannot use non-relocatable expression as "
996 abs_seg = reloc_seg(e);
997 abs_offset = reloc_value(e);
1000 if (pass==1) abs_offset = 0x100;/* don't go near zero in case of / */
1001 else report_error (ERR_PANIC, "invalid ABSOLUTE address "
1004 location.segment = NO_SEG;
1012 while (*p && !isspace(*p)) {
1019 report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
1020 "identifier expected after DEBUG");
1023 while (*p && isspace(*p)) p++;
1024 if (pass==pass_max) ofmt->current_dfmt->debug_directive (debugid, p);
1026 case 8: /* [WARNING {+|-}warn-name] */
1028 while (*value && isspace(*value))
1031 if (*value == '+' || *value == '-') {
1032 validid = (*value == '-') ? TRUE : FALSE;
1037 for (i=1; i<=ERR_WARN_MAX; i++)
1038 if (!nasm_stricmp(value, suppressed_names[i]))
1040 if (i <= ERR_WARN_MAX)
1041 suppressed[i] = validid;
1043 report_error (ERR_NONFATAL, "invalid warning id in WARNING directive");
1047 cpu = get_cpu (value);
1049 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1050 while (*value && isspace(*value))
1053 if (*value == '+') {
1057 if (*value == '-') {
1061 report_error (ERR_NONFATAL, "invalid parameter to \"list\" directive");
1066 if (!ofmt->directive (directive, value, pass2))
1067 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
1068 "unrecognised directive [%s]",
1072 else /* it isn't a directive */
1074 parse_line (pass1, line, &output_ins,
1075 report_error, evaluate,
1078 if (!(optimizing>0) && pass == 2) {
1079 if (forwref != NULL && globallineno == forwref->lineno) {
1080 output_ins.forw_ref = TRUE;
1082 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1083 forwref = saa_rstruct (forwrefs);
1084 } while (forwref != NULL && forwref->lineno == globallineno);
1086 output_ins.forw_ref = FALSE;
1090 if (!(optimizing>0) && output_ins.forw_ref)
1093 for(i = 0; i < output_ins.operands; i++)
1095 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
1097 struct forwrefinfo *fwinf =
1098 (struct forwrefinfo *)saa_wstruct(forwrefs);
1099 fwinf->lineno = globallineno;
1103 } else { /* pass == 2 */
1105 * Hack to prevent phase error in the code
1109 * If the second operand is a forward reference,
1110 * the UNITY property of the number 1 in that
1111 * operand is cancelled. Otherwise the above
1112 * sequence will cause a phase error.
1114 * This hack means that the above code will
1115 * generate 286+ code.
1117 * The forward reference will mean that the
1118 * operand will not have the UNITY property on
1119 * the first pass, so the pass behaviours will
1123 if (output_ins.operands >= 2 &&
1124 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1126 output_ins.oprs[1].type &= ~(ONENESS|BYTENESS);
1134 if (output_ins.opcode == I_EQU) {
1138 * Special `..' EQUs get processed in pass two,
1139 * except `..@' macro-processor EQUs which are done
1140 * in the normal place.
1142 if (!output_ins.label)
1143 report_error (ERR_NONFATAL,
1144 "EQU not preceded by label");
1146 else if (output_ins.label[0] != '.' ||
1147 output_ins.label[1] != '.' ||
1148 output_ins.label[2] == '@')
1150 if (output_ins.operands == 1 &&
1151 (output_ins.oprs[0].type & IMMEDIATE) &&
1152 output_ins.oprs[0].wrt == NO_SEG)
1154 int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
1155 def_label (output_ins.label,
1156 output_ins.oprs[0].segment,
1157 output_ins.oprs[0].offset,
1158 NULL, FALSE, isext, ofmt, report_error);
1160 else if (output_ins.operands == 2 &&
1161 (output_ins.oprs[0].type & IMMEDIATE) &&
1162 (output_ins.oprs[0].type & COLON) &&
1163 output_ins.oprs[0].segment == NO_SEG &&
1164 output_ins.oprs[0].wrt == NO_SEG &&
1165 (output_ins.oprs[1].type & IMMEDIATE) &&
1166 output_ins.oprs[1].segment == NO_SEG &&
1167 output_ins.oprs[1].wrt == NO_SEG)
1169 def_label (output_ins.label,
1170 output_ins.oprs[0].offset | SEG_ABS,
1171 output_ins.oprs[1].offset,
1172 NULL, FALSE, FALSE, ofmt, report_error);
1175 report_error(ERR_NONFATAL, "bad syntax for EQU");
1177 } else { /* pass == 2 */
1179 * Special `..' EQUs get processed here, except
1180 * `..@' macro processor EQUs which are done above.
1182 if (output_ins.label[0] == '.' &&
1183 output_ins.label[1] == '.' &&
1184 output_ins.label[2] != '@')
1186 if (output_ins.operands == 1 &&
1187 (output_ins.oprs[0].type & IMMEDIATE)) {
1188 define_label (output_ins.label,
1189 output_ins.oprs[0].segment,
1190 output_ins.oprs[0].offset,
1191 NULL, FALSE, FALSE, ofmt, report_error);
1193 else if (output_ins.operands == 2 &&
1194 (output_ins.oprs[0].type & IMMEDIATE) &&
1195 (output_ins.oprs[0].type & COLON) &&
1196 output_ins.oprs[0].segment == NO_SEG &&
1197 (output_ins.oprs[1].type & IMMEDIATE) &&
1198 output_ins.oprs[1].segment == NO_SEG)
1200 define_label (output_ins.label,
1201 output_ins.oprs[0].offset | SEG_ABS,
1202 output_ins.oprs[1].offset,
1203 NULL, FALSE, FALSE, ofmt, report_error);
1206 report_error(ERR_NONFATAL, "bad syntax for EQU");
1209 } else { /* instruction isn't an EQU */
1213 long l = insn_size (location.segment, offs, sb, cpu,
1214 &output_ins, report_error);
1216 /* if (using_debug_info) && output_ins.opcode != -1)*/
1217 if (using_debug_info) /* fbk 03/25/01 */
1220 /* this is done here so we can do debug type info */
1221 long typeinfo = TYS_ELEMENTS(output_ins.operands);
1222 switch (output_ins.opcode) {
1224 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1227 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1230 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1233 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1236 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1239 typeinfo |= TY_BYTE;
1242 typeinfo |= TY_WORD;
1245 if (output_ins.eops_float)
1246 typeinfo |= TY_FLOAT;
1248 typeinfo |= TY_DWORD;
1251 typeinfo |= TY_QWORD;
1254 typeinfo |= TY_TBYTE;
1257 typeinfo = TY_LABEL;
1261 ofmt->current_dfmt->debug_typevalue(typeinfo);
1266 SET_CURR_OFFS (offs);
1269 * else l == -1 => invalid instruction, which will be
1270 * flagged as an error on pass 2
1273 } else { /* pass == 2 */
1274 offs += assemble (location.segment, offs, sb, cpu,
1275 &output_ins, ofmt, report_error, &nasmlist);
1276 SET_CURR_OFFS (offs);
1280 cleanup_insn (&output_ins);
1283 location.offset = offs = GET_CURR_OFFS;
1284 } /* end while (line = preproc->getline... */
1286 if (pass1==2 && global_offset_changed)
1287 report_error(ERR_NONFATAL, "phase error detected at end of assembly.");
1289 if (pass1 == 1) preproc->cleanup(1);
1291 if (pass1==1 && terminate_after_phase) {
1299 if (pass>1 && !global_offset_changed) {
1301 if (pass0==2) pass = pass_max - 1;
1302 } else if (!(optimizing>0)) pass0++;
1304 } /* for (pass=1; pass<=2; pass++) */
1306 preproc->cleanup(0);
1309 if (optimizing>0 && opt_verbose_info) /* -On and -Ov switches */
1311 "info:: assembly required 1+%d+1 passes\n", pass_cnt-2);
1313 } /* exit from assemble_file (...) */
1316 static int getkw (char **directive, char **value)
1322 /* allow leading spaces or tabs */
1323 while (*buf==' ' || *buf=='\t')
1331 while (*p && *p != ']') p++;
1338 while (*p && *p != ';') {
1345 *directive = p = buf+1;
1346 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1353 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
1355 while (*buf!=']') buf++;
1362 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1364 if (!nasm_stricmp(p, "extern"))
1366 if (!nasm_stricmp(p, "bits"))
1368 if (!nasm_stricmp(p, "global"))
1370 if (!nasm_stricmp(p, "common"))
1372 if (!nasm_stricmp(p, "absolute"))
1374 if (!nasm_stricmp(p, "debug"))
1376 if (!nasm_stricmp(p, "warning"))
1378 if (!nasm_stricmp(p, "cpu"))
1380 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1386 * gnu style error reporting
1387 * This function prints an error message to error_file in the
1388 * style used by GNU. An example would be:
1389 * file.asm:50: error: blah blah blah
1390 * where file.asm is the name of the file, 50 is the line number on
1391 * which the error occurs (or is detected) and "error:" is one of
1392 * the possible optional diagnostics -- it can be "error" or "warning"
1393 * or something else. Finally the line terminates with the actual
1396 * @param severity the severity of the warning or error
1397 * @param fmt the printf style format string
1399 static void report_error_gnu (int severity, const char *fmt, ...)
1403 if (is_suppressed_warning(severity))
1406 if (severity & ERR_NOFILE)
1407 fputs ("nasm: ", error_file);
1409 char * currentfile = NULL;
1411 src_get (&lineno, ¤tfile);
1412 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
1413 nasm_free (currentfile);
1416 report_error_common (severity, fmt, ap);
1421 * MS style error reporting
1422 * This function prints an error message to error_file in the
1423 * style used by Visual C and some other Microsoft tools. An example
1425 * file.asm(50) : error: blah blah blah
1426 * where file.asm is the name of the file, 50 is the line number on
1427 * which the error occurs (or is detected) and "error:" is one of
1428 * the possible optional diagnostics -- it can be "error" or "warning"
1429 * or something else. Finally the line terminates with the actual
1432 * @param severity the severity of the warning or error
1433 * @param fmt the printf style format string
1435 static void report_error_vc (int severity, const char *fmt, ...)
1439 if (is_suppressed_warning (severity))
1442 if (severity & ERR_NOFILE)
1443 fputs ("nasm: ", error_file);
1445 char * currentfile = NULL;
1447 src_get (&lineno, ¤tfile);
1448 fprintf (error_file, "%s(%ld) : ", currentfile, lineno);
1449 nasm_free (currentfile);
1452 report_error_common (severity, fmt, ap);
1457 * check for supressed warning
1458 * checks for suppressed warning or pass one only warning and we're
1461 * @param severity the severity of the warning or error
1462 * @return true if we should abort error/warning printing
1464 static int is_suppressed_warning (int severity)
1467 * See if it's a suppressed warning.
1469 return ((severity & ERR_MASK) == ERR_WARNING &&
1470 (severity & ERR_WARN_MASK) != 0 &&
1471 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ]) ||
1473 * See if it's a pass-one only warning and we're not in pass one.
1475 ((severity & ERR_PASS1) && pass0 == 2);
1479 * common error reporting
1480 * This is the common back end of the error reporting schemes currently
1481 * implemented. It prints the nature of the warning and then the
1482 * specific error message to error_file and may or may not return. It
1483 * doesn't return if the error severity is a "panic" or "debug" type.
1485 * @param severity the severity of the warning or error
1486 * @param fmt the printf style format string
1488 static void report_error_common (int severity, const char *fmt, va_list args)
1490 switch (severity & ERR_MASK) {
1492 fputs ("warning: ", error_file); break;
1494 fputs ("error: ", error_file); break;
1496 fputs ("fatal: ", error_file); break;
1498 fputs ("panic: ", error_file); break;
1500 fputs("debug: ", error_file); break;
1503 vfprintf (error_file, fmt, args);
1504 fputc ('\n', error_file);
1506 if (severity & ERR_USAGE)
1509 switch (severity & ERR_MASK) {
1510 case ERR_WARNING: case ERR_DEBUG:
1511 /* no further action, by definition */
1514 /* hack enables listing(!) on errors */
1515 terminate_after_phase = TRUE;
1524 exit(1); /* instantly die */
1525 break; /* placate silly compilers */
1528 /* abort(); */ /* halt, catch fire, and dump core */
1534 static void usage(void)
1536 fputs("type `nasm -h' for help\n", error_file);
1539 static void register_output_formats(void)
1541 ofmt = ofmt_register (report_error);
1544 #define BUF_DELTA 512
1546 static FILE *no_pp_fp;
1547 static efunc no_pp_err;
1548 static ListGen *no_pp_list;
1549 static long no_pp_lineinc;
1551 static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
1554 src_set_fname(nasm_strdup(file));
1558 no_pp_fp = fopen(file, "r");
1560 no_pp_err (ERR_FATAL | ERR_NOFILE,
1561 "unable to open input file `%s'", file);
1562 no_pp_list = listgen;
1563 (void) pass; /* placate compilers */
1564 (void) eval; /* placate compilers */
1567 static char *no_pp_getline (void)
1569 char *buffer, *p, *q;
1572 bufsize = BUF_DELTA;
1573 buffer = nasm_malloc(BUF_DELTA);
1574 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1576 while (1) { /* Loop to handle %line */
1579 while (1) { /* Loop to handle long lines */
1580 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1584 if (p > buffer && p[-1] == '\n')
1586 if (p-buffer > bufsize-10) {
1588 offset = p - buffer;
1589 bufsize += BUF_DELTA;
1590 buffer = nasm_realloc(buffer, bufsize);
1591 p = buffer + offset;
1595 if (!q && p == buffer) {
1601 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1602 * them are present at the end of the line.
1604 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1606 if (!strncmp(buffer, "%line", 5)) {
1609 char *nm = nasm_malloc(strlen(buffer));
1610 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1611 nasm_free( src_set_fname(nm) );
1621 no_pp_list->line (LIST_READ, buffer);
1626 static void no_pp_cleanup (int pass)
1631 static unsigned long get_cpu (char *value)
1634 if (!strcmp(value, "8086")) return IF_8086;
1635 if (!strcmp(value, "186")) return IF_186;
1636 if (!strcmp(value, "286")) return IF_286;
1637 if (!strcmp(value, "386")) return IF_386;
1638 if (!strcmp(value, "486")) return IF_486;
1639 if (!strcmp(value, "586") ||
1640 !nasm_stricmp(value, "pentium") ) return IF_PENT;
1641 if (!strcmp(value, "686") ||
1642 !nasm_stricmp(value, "ppro") ||
1643 !nasm_stricmp(value, "pentiumpro") ||
1644 !nasm_stricmp(value, "p2") ) return IF_P6;
1645 if (!nasm_stricmp(value, "p3") ||
1646 !nasm_stricmp(value, "katmai") ) return IF_KATMAI;
1647 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1648 !nasm_stricmp(value, "willamette") ) return IF_WILLAMETTE;
1649 if (!nasm_stricmp(value, "prescott") ) return IF_PRESCOTT;
1650 if (!nasm_stricmp(value, "ia64") ||
1651 !nasm_stricmp(value, "ia-64") ||
1652 !nasm_stricmp(value, "itanium") ||
1653 !nasm_stricmp(value, "itanic") ||
1654 !nasm_stricmp(value, "merced") ) return IF_IA64;
1656 report_error (pass0<2 ? ERR_NONFATAL : ERR_FATAL, "unknown 'cpu' type");
1658 return IF_PLEVEL; /* the maximum level */
1662 static int get_bits (char *value)
1666 if ((i = atoi(value)) == 16) return i; /* set for a 16-bit segment */
1669 report_error(ERR_NONFATAL,
1670 "cannot specify 32-bit segment on processor below a 386");
1674 report_error(pass0<2 ? ERR_NONFATAL : ERR_FATAL,
1675 "`%s' is not a valid segment size; must be 16 or 32",