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 report_error (int, char *, ...);
34 static void parse_cmdline (int, char **);
35 static void assemble_file (char *);
36 static int getkw (char *buf, char **value);
37 static void register_output_formats(void);
38 static void usage(void);
40 static int using_debug_info;
41 int tasm_compatible_mode = FALSE;
44 static char inname[FILENAME_MAX];
45 static char outname[FILENAME_MAX];
46 static char listname[FILENAME_MAX];
47 static int globallineno; /* for forward-reference tracking */
48 /* static int pass = 0; */
49 static struct ofmt *ofmt = NULL;
51 static FILE *error_file; /* Where to write error messages */
53 static FILE *ofile = NULL;
54 int optimizing = -1; /* number of optimization passes to take */
55 static int sb, cmd_sb = 16; /* by default */
56 static unsigned long cmd_cpu = IF_PLEVEL; /* highest level by default */
57 static unsigned long cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
58 int global_offset_changed; /* referenced in labels.c */
60 static loc_t location;
61 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
64 static struct RAA *offsets;
65 static long abs_offset;
67 static struct SAA *forwrefs; /* keep track of forward references */
68 static struct forwrefinfo *forwref;
70 static Preproc *preproc;
72 op_normal, /* Preprocess and assemble */
73 op_preprocess, /* Preprocess only */
74 op_depend /* Generate dependencies */
76 static enum op_type operating_mode;
79 * Which of the suppressible warnings are suppressed. Entry zero
80 * doesn't do anything. Initial defaults are given here.
82 static char suppressed[1+ERR_WARN_MAX] = {
83 0, TRUE, TRUE, TRUE, FALSE, TRUE
87 * The option names for the suppressible warnings. As before, entry
90 static char *suppressed_names[1+ERR_WARN_MAX] = {
91 NULL, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
96 * The explanations for the suppressible warnings. As before, entry
99 static char *suppressed_what[1+ERR_WARN_MAX] = {
101 "macro calls with wrong no. of params",
102 "cyclic macro self-references",
103 "labels alone on lines without trailing `:'",
104 "numeric constants greater than 0xFFFFFFFF",
105 "using 8- or 16-bit relocation in ELF, a GNU extension"
109 * This is a null preprocessor which just copies lines from input
110 * to output. It's used when someone explicitly requests that NASM
111 * not preprocess their source file.
114 static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
115 static char *no_pp_getline (void);
116 static void no_pp_cleanup (int);
117 static Preproc no_pp = {
124 * get/set current offset...
126 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
127 raa_read(offsets,location.segment))
128 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
129 (void)(offsets=raa_write(offsets,location.segment,(x))))
131 static int want_usage;
132 static int terminate_after_phase;
133 int user_nolist = 0; /* fbk 9/2/00 */
135 static void nasm_fputs(char *line, FILE *ofile)
144 int main(int argc, char **argv)
147 want_usage = terminate_after_phase = FALSE;
149 nasm_set_malloc_error (report_error);
150 offsets = raa_init();
151 forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
154 operating_mode = op_normal;
160 register_output_formats();
162 parse_cmdline(argc, argv);
164 if (terminate_after_phase)
172 pp_extra_stdmac (ofmt->stdmac);
173 parser_global_info (ofmt, &location);
174 eval_global_info (ofmt, lookup_label, &location);
176 /* define some macros dependent of command-line */
179 sprintf (temp, "__OUTPUT_FORMAT__=%s\n", ofmt->shortname);
180 pp_pre_define (temp);
183 switch ( operating_mode ) {
187 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
188 if (outname[0] == '\0')
189 ofmt->filename (inname, outname, report_error);
191 fprintf(stdout, "%s: %s", outname, inname);
192 while ( (line = preproc->getline()) )
202 char *file_name = NULL;
207 ofile = fopen(outname, "w");
209 report_error (ERR_FATAL | ERR_NOFILE,
210 "unable to open output file `%s'", outname);
214 location.known = FALSE;
217 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
218 while ( (line = preproc->getline()) ) {
220 * We generate %line directives if needed for later programs
222 long linnum = prior_linnum += lineinc;
223 int altline = src_get(&linnum, &file_name);
225 if (altline==1 && lineinc==1)
226 nasm_fputs("", ofile);
228 lineinc = (altline != -1 || lineinc!=1);
229 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
230 linnum, lineinc, file_name);
232 prior_linnum = linnum;
234 nasm_fputs(line, ofile);
237 nasm_free(file_name);
241 if (ofile && terminate_after_phase)
249 * We must call ofmt->filename _anyway_, even if the user
250 * has specified their own output file, because some
251 * formats (eg OBJ and COFF) use ofmt->filename to find out
252 * the name of the input file and then put that inside the
255 ofmt->filename (inname, outname, report_error);
257 ofile = fopen(outname, "wb");
259 report_error (ERR_FATAL | ERR_NOFILE,
260 "unable to open output file `%s'", outname);
264 * We must call init_labels() before ofmt->init() since
265 * some object formats will want to define labels in their
266 * init routines. (eg OS/2 defines the FLAT group)
270 ofmt->init (ofile, report_error, define_label, evaluate);
272 assemble_file (inname);
274 if (!terminate_after_phase) {
275 ofmt->cleanup (using_debug_info);
279 * We had an fclose on the output file here, but we
280 * actually do that in all the object file drivers as well,
281 * so we're leaving out the one here.
300 if (terminate_after_phase)
308 * Get a parameter for a command line option.
309 * First arg must be in the form of e.g. -f...
311 static char *get_param (char *p, char *q, int *advance)
314 if (p[2]) /* the parameter's in the option */
326 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
327 "option `-%c' requires an argument",
339 #define OPT_POSTFIX 1
340 struct textargs textopts[] =
342 {"prefix",OPT_PREFIX},
343 {"postfix",OPT_POSTFIX},
349 static int process_arg (char *p, char *q)
357 if (p[0]=='-' && ! stopoptions)
363 case 'o': /* these parameters take values */
375 if ( !(param = get_param (p, q, &advance)) )
377 if (p[1]=='o') { /* output file */
378 strcpy (outname, param);
379 } else if (p[1]=='f') { /* output format */
380 ofmt = ofmt_find(param);
382 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
383 "unrecognised output format `%s' - "
384 "use -hf for a list",
388 ofmt->current_dfmt = ofmt->debug_formats[0];
389 } else if (p[1]=='O') { /* Optimization level */
391 if (!isdigit(*param)) report_error(ERR_FATAL,
392 "command line optimization level must be 0..3 or <nn>");
394 if (opt<=0) optimizing = -1; /* 0.98 behaviour */
395 else if (opt==1) optimizing = 0; /* Two passes, 0.98.09 behavior */
396 else if (opt<=3) optimizing = opt*5; /* Multiple passes */
397 else optimizing = opt; /* Multiple passes */
398 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
399 pp_pre_include (param);
400 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
401 pp_pre_define (param);
402 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
403 pp_pre_undefine (param);
404 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
405 pp_include_path (param);
406 } else if (p[1]=='l') { /* listing file */
407 strcpy (listname, param);
408 } else if (p[1]=='E') { /* error messages file */
409 error_file = fopen(param, "w");
411 error_file = stderr; /* Revert to default! */
412 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
413 "cannot open file `%s' for error messages",
416 } else if (p[1] == 'F') { /* specify debug format */
417 ofmt->current_dfmt = dfmt_find(ofmt, param);
418 if (!ofmt->current_dfmt) {
419 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
420 "unrecognized debug format `%s' for"
421 " output format `%s'",
422 param, ofmt->shortname);
427 using_debug_info = TRUE;
430 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
432 " [options...] [--] filename\n"
433 " or nasm -r for version info (obsolete)\n"
434 " or nasm -v for version info (preferred)\n\n"
435 " -t Assemble in SciTech TASM compatible mode\n"
436 " -g Generate debug information in selected format.\n");
437 printf(" -e preprocess only (writes output to stdout by default)\n"
438 " -a don't preprocess (assemble only)\n"
439 " -M generate Makefile dependencies on stdout\n\n"
440 " -E<file> redirect error messages to file\n"
441 " -s redirect error messages to stdout\n\n"
442 " -F format select a debugging format\n\n"
443 " -I<path> adds a pathname to the include file path\n");
444 printf(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
445 " -P<file> pre-includes a file\n"
446 " -D<macro>[=<value>] pre-defines a macro\n"
447 " -U<macro> undefines a macro\n"
448 " -w+foo enables warnings about foo; -w-foo disables them\n"
449 "where foo can be:\n");
450 for (i=1; i<=ERR_WARN_MAX; i++)
451 printf(" %-23s %s (default %s)\n",
452 suppressed_names[i], suppressed_what[i],
453 suppressed[i] ? "off" : "on");
454 printf ("\nresponse files should contain command line parameters"
455 ", one per line.\n");
457 printf("\nvalid output formats for -f are"
458 " (`*' denotes default):\n");
459 ofmt_list(ofmt, stdout);
462 printf ("\nFor a list of valid output formats, use -hf.\n");
463 printf ("For a list of debug formats, use -f <form> -y.\n");
465 exit (0); /* never need usage message here */
468 printf("\nvalid debug formats for '%s' output format are"
469 " ('*' denotes default):\n",
471 dfmt_list(ofmt, stdout);
475 tasm_compatible_mode = TRUE;
479 printf("NASM version %s compiled "
483 "on " __DATE__ "\n", NASM_VER);
484 exit (0); /* never need usage message here */
486 case 'e': /* preprocess only */
487 operating_mode = op_preprocess;
489 case 'a': /* assemble only - don't preprocess */
493 if (p[2] != '+' && p[2] != '-') {
494 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
495 "invalid option to `-w'");
497 for (i=1; i<=ERR_WARN_MAX; i++)
498 if (!nasm_stricmp(p+3, suppressed_names[i]))
500 if (i <= ERR_WARN_MAX)
501 suppressed[i] = (p[2] == '-');
503 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
504 "invalid option to `-w'");
508 operating_mode = op_depend;
515 if (p[2]==0) { /* -- => stop processing options */
519 for(s=0; textopts[s].label; s++)
521 if(!nasm_stricmp(p+2, textopts[s].label))
535 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
536 "option `--%s' requires an argument",
542 advance = 1, param = q;
547 strncpy(lprefix,param,PREFIX_MAX-1);
548 lprefix[PREFIX_MAX-1]=0;
553 strncpy(lpostfix,param,POSTFIX_MAX-1);
554 lpostfix[POSTFIX_MAX-1]=0;
561 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
562 "unrecognised option `--%s'",
571 if (!ofmt->setinfo(GI_SWITCH,&p))
572 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
573 "unrecognised option `-%c'",
581 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
582 "more than one input file specified");
590 #define ARG_BUF_DELTA 128
592 static void process_respfile (FILE *rfile)
594 char *buffer, *p, *q, *prevarg;
595 int bufsize, prevargsize;
597 bufsize = prevargsize = ARG_BUF_DELTA;
598 buffer = nasm_malloc(ARG_BUF_DELTA);
599 prevarg = nasm_malloc(ARG_BUF_DELTA);
602 while (1) { /* Loop to handle all lines in file */
605 while (1) { /* Loop to handle long lines */
606 q = fgets(p, bufsize-(p-buffer), rfile);
610 if (p > buffer && p[-1] == '\n')
612 if (p-buffer > bufsize-10) {
615 bufsize += ARG_BUF_DELTA;
616 buffer = nasm_realloc(buffer, bufsize);
621 if (!q && p == buffer) {
623 process_arg (prevarg, NULL);
630 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
631 * them are present at the end of the line.
633 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
635 while (p > buffer && isspace(p[-1]))
642 if (process_arg (prevarg, p))
645 if (strlen(p) > prevargsize-10) {
646 prevargsize += ARG_BUF_DELTA;
647 prevarg = nasm_realloc(prevarg, prevargsize);
653 /* Function to process args from a string of args, rather than the
654 * argv array. Used by the environment variable and response file
657 static void process_args (char *args) {
658 char *p, *q, *arg, *prevarg;
659 char separator = ' ';
667 while (*p && *p != separator) p++;
668 while (*p == separator) *p++ = '\0';
671 if (process_arg (prevarg, arg))
675 process_arg (arg, NULL);
678 static void parse_cmdline(int argc, char **argv)
681 char *envreal, *envcopy=NULL, *p, *arg;
683 *inname = *outname = *listname = '\0';
686 * First, process the NASM environment variable.
688 envreal = getenv("NASM");
691 envcopy = nasm_strdup(envreal);
692 process_args(envcopy);
697 * Now process the actual command line.
703 if (argv[0][0] == '@') {
704 /* We have a response file, so process this as a set of
705 * arguments like the environment variable. This allows us
706 * to have multiple arguments on a single line, which is
707 * different to the -@resp file processing below for regular
710 char *str = malloc(2048);
711 FILE *f = fopen(&argv[0][1],"r");
713 printf("out of memory");
717 while (fgets(str,2048,f)) {
726 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
727 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
728 if ((rfile = fopen(p, "r"))) {
729 process_respfile (rfile);
732 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
733 "unable to open response file `%s'", p);
736 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
737 argv += i, argc -= i;
741 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
742 "no input file specified");
746 static void assemble_file (char *fname)
748 char * value, * p, * q, * special, * line, debugid[80];
750 int i, rn_error, validid;
752 struct tokenval tokval;
755 int pass_cnt = 0; /* count actual passes */
757 if (cmd_sb == 32 && cmd_cpu < IF_386)
758 report_error(ERR_FATAL, "command line: "
759 "32-bit segment size requires a higher cpu");
761 pass_max = (optimizing>0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
762 pass0 = !(optimizing>0); /* start at 1 if not optimizing */
763 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
767 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
768 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
769 /* pass0 seq is 0, 0, 0,..., 1, 2 */
771 def_label = pass > 1 ? redefine_label : define_label;
774 sb = cmd_sb; /* set 'bits' to command line default */
778 nasmlist.init(listname, report_error);
781 global_offset_changed = FALSE; /* set by redefine_label */
782 location.segment = ofmt->section(NULL, pass2, &sb);
784 saa_rewind (forwrefs);
785 forwref = saa_rstruct (forwrefs);
787 offsets = raa_init();
789 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
791 if (pass == 1) location.known = TRUE;
792 location.offset = offs = GET_CURR_OFFS;
794 while ( (line = preproc->getline()) )
798 /* here we parse our directives; this is not handled by the 'real'
800 if ( (i = getkw (line, &value)) )
803 case 1: /* [SEGMENT n] */
804 seg = ofmt->section (value, pass2, &sb);
806 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
807 "segment name `%s' not recognised",
811 location.segment = seg;
814 case 2: /* [EXTERN label:special] */
815 if (*value == '$') value++; /* skip initial $ if present */
818 while (*q && *q != ':')
822 ofmt->symdef(value, 0L, 0L, 3, q);
824 } else if (pass == 1) { /* pass == 1 */
829 while (*q && *q != ':') {
835 report_error (ERR_NONFATAL,
836 "identifier expected after EXTERN");
844 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
846 pass0 = 1; /* fake pass 1 in labels.c */
847 declare_as_global (value, special, report_error);
848 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
852 } /* else pass0 == 1 */
854 case 3: /* [BITS bits] */
855 sb = get_bits(value);
857 case 4: /* [GLOBAL symbol:special] */
858 if (*value == '$') value++; /* skip initial $ if present */
859 if (pass0 == 2) { /* pass 2 */
861 while (*q && *q != ':')
865 ofmt->symdef(value, 0L, 0L, 3, q);
867 } else if (pass2 == 1) { /* pass == 1 */
872 while (*q && *q != ':') {
878 report_error (ERR_NONFATAL,
879 "identifier expected after GLOBAL");
887 declare_as_global (value, special, report_error);
890 case 5: /* [COMMON symbol size:special] */
891 if (*value == '$') value++; /* skip initial $ if present */
897 while (*p && !isspace(*p)) {
903 report_error (ERR_NONFATAL,
904 "identifier expected after COMMON");
910 while (*p && isspace(*p))
913 while (*q && *q != ':')
920 size = readnum (p, &rn_error);
922 report_error (ERR_NONFATAL, "invalid size specified"
923 " in COMMON declaration");
925 define_common (value, seg_alloc(), size,
926 special, ofmt, report_error);
928 report_error (ERR_NONFATAL, "no size specified in"
929 " COMMON declaration");
930 } else if (pass0 == 2) { /* pass == 2 */
932 while (*q && *q != ':') {
939 ofmt->symdef(value, 0L, 0L, 3, q);
943 case 6: /* [ABSOLUTE address] */
945 stdscan_bufptr = value;
946 tokval.t_type = TOKEN_INVALID;
947 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, report_error,
951 report_error (pass0==1 ? ERR_NONFATAL : ERR_PANIC,
952 "cannot use non-relocatable expression as "
955 abs_seg = reloc_seg(e);
956 abs_offset = reloc_value(e);
959 if (pass==1) abs_offset = 0x100;/* don't go near zero in case of / */
960 else report_error (ERR_PANIC, "invalid ABSOLUTE address "
963 location.segment = abs_seg;
971 while (*p && !isspace(*p)) {
978 report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
979 "identifier expected after DEBUG");
982 while (*p && isspace(*p)) p++;
983 if (pass==pass_max) ofmt->current_dfmt->debug_directive (debugid, p);
985 case 8: /* [WARNING {+|-}warn-name] */
987 while (*value && isspace(*value))
990 if (*value == '+' || *value == '-') {
991 validid = (*value == '-') ? TRUE : FALSE;
996 for (i=1; i<=ERR_WARN_MAX; i++)
997 if (!nasm_stricmp(value, suppressed_names[i]))
999 if (i <= ERR_WARN_MAX)
1000 suppressed[i] = validid;
1002 report_error (ERR_NONFATAL, "invalid warning id in WARNING directive");
1006 cpu = get_cpu (value);
1008 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1009 while (*value && isspace(*value))
1012 if (*value == '+') {
1016 if (*value == '-') {
1020 report_error (ERR_NONFATAL, "invalid parameter to \"list\" directive");
1025 if (!ofmt->directive (line+1, value, pass2))
1026 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
1027 "unrecognised directive [%s]",
1031 else /* it isn't a directive */
1033 parse_line (pass1, line, &output_ins,
1034 report_error, evaluate,
1037 if (!(optimizing>0) && pass == 2) {
1038 if (forwref != NULL && globallineno == forwref->lineno) {
1039 output_ins.forw_ref = TRUE;
1041 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1042 forwref = saa_rstruct (forwrefs);
1043 } while (forwref != NULL && forwref->lineno == globallineno);
1045 output_ins.forw_ref = FALSE;
1049 if (!(optimizing>0) && output_ins.forw_ref)
1052 for(i = 0; i < output_ins.operands; i++)
1054 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
1056 struct forwrefinfo *fwinf =
1057 (struct forwrefinfo *)saa_wstruct(forwrefs);
1058 fwinf->lineno = globallineno;
1062 } else { /* pass == 2 */
1064 * Hack to prevent phase error in the code
1068 * If the second operand is a forward reference,
1069 * the UNITY property of the number 1 in that
1070 * operand is cancelled. Otherwise the above
1071 * sequence will cause a phase error.
1073 * This hack means that the above code will
1074 * generate 286+ code.
1076 * The forward reference will mean that the
1077 * operand will not have the UNITY property on
1078 * the first pass, so the pass behaviours will
1082 if (output_ins.operands >= 2 &&
1083 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1085 output_ins.oprs[1].type &= ~(ONENESS|BYTENESS);
1093 if (output_ins.opcode == I_EQU) {
1097 * Special `..' EQUs get processed in pass two,
1098 * except `..@' macro-processor EQUs which are done
1099 * in the normal place.
1101 if (!output_ins.label)
1102 report_error (ERR_NONFATAL,
1103 "EQU not preceded by label");
1105 else if (output_ins.label[0] != '.' ||
1106 output_ins.label[1] != '.' ||
1107 output_ins.label[2] == '@')
1109 if (output_ins.operands == 1 &&
1110 (output_ins.oprs[0].type & IMMEDIATE) &&
1111 output_ins.oprs[0].wrt == NO_SEG)
1113 int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
1114 def_label (output_ins.label,
1115 output_ins.oprs[0].segment,
1116 output_ins.oprs[0].offset,
1117 NULL, FALSE, isext, ofmt, report_error);
1119 else if (output_ins.operands == 2 &&
1120 (output_ins.oprs[0].type & IMMEDIATE) &&
1121 (output_ins.oprs[0].type & COLON) &&
1122 output_ins.oprs[0].segment == NO_SEG &&
1123 output_ins.oprs[0].wrt == NO_SEG &&
1124 (output_ins.oprs[1].type & IMMEDIATE) &&
1125 output_ins.oprs[1].segment == NO_SEG &&
1126 output_ins.oprs[1].wrt == NO_SEG)
1128 def_label (output_ins.label,
1129 output_ins.oprs[0].offset | SEG_ABS,
1130 output_ins.oprs[1].offset,
1131 NULL, FALSE, FALSE, ofmt, report_error);
1134 report_error(ERR_NONFATAL, "bad syntax for EQU");
1136 } else { /* pass == 2 */
1138 * Special `..' EQUs get processed here, except
1139 * `..@' macro processor EQUs which are done above.
1141 if (output_ins.label[0] == '.' &&
1142 output_ins.label[1] == '.' &&
1143 output_ins.label[2] != '@')
1145 if (output_ins.operands == 1 &&
1146 (output_ins.oprs[0].type & IMMEDIATE)) {
1147 define_label (output_ins.label,
1148 output_ins.oprs[0].segment,
1149 output_ins.oprs[0].offset,
1150 NULL, FALSE, FALSE, ofmt, report_error);
1152 else if (output_ins.operands == 2 &&
1153 (output_ins.oprs[0].type & IMMEDIATE) &&
1154 (output_ins.oprs[0].type & COLON) &&
1155 output_ins.oprs[0].segment == NO_SEG &&
1156 (output_ins.oprs[1].type & IMMEDIATE) &&
1157 output_ins.oprs[1].segment == NO_SEG)
1159 define_label (output_ins.label,
1160 output_ins.oprs[0].offset | SEG_ABS,
1161 output_ins.oprs[1].offset,
1162 NULL, FALSE, FALSE, ofmt, report_error);
1165 report_error(ERR_NONFATAL, "bad syntax for EQU");
1168 } else { /* instruction isn't an EQU */
1172 long l = insn_size (location.segment, offs, sb, cpu,
1173 &output_ins, report_error);
1175 /* if (using_debug_info) && output_ins.opcode != -1)*/
1176 if (using_debug_info) /* fbk 03/25/01 */
1179 /* this is done here so we can do debug type info */
1180 long typeinfo = TYS_ELEMENTS(output_ins.operands);
1181 switch (output_ins.opcode) {
1183 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1186 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1189 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1192 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1195 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1198 typeinfo |= TY_BYTE;
1201 typeinfo |= TY_WORD;
1204 if (output_ins.eops_float)
1205 typeinfo |= TY_FLOAT;
1207 typeinfo |= TY_DWORD;
1210 typeinfo |= TY_QWORD;
1213 typeinfo |= TY_TBYTE;
1216 typeinfo = TY_LABEL;
1220 ofmt->current_dfmt->debug_typevalue(typeinfo);
1225 SET_CURR_OFFS (offs);
1228 * else l == -1 => invalid instruction, which will be
1229 * flagged as an error on pass 2
1232 } else { /* pass == 2 */
1233 offs += assemble (location.segment, offs, sb, cpu,
1234 &output_ins, ofmt, report_error, &nasmlist);
1235 SET_CURR_OFFS (offs);
1239 cleanup_insn (&output_ins);
1242 location.offset = offs = GET_CURR_OFFS;
1243 } /* end while (line = preproc->getline... */
1245 if (pass1==2 && global_offset_changed)
1246 report_error(ERR_NONFATAL, "phase error detected at end of assembly.");
1248 if (pass1 == 1) preproc->cleanup(1);
1250 if (pass1==1 && terminate_after_phase) {
1258 if (pass>1 && !global_offset_changed) {
1260 if (pass0==2) pass = pass_max - 1;
1261 } else if (!(optimizing>0)) pass0++;
1263 } /* for (pass=1; pass<=2; pass++) */
1265 preproc->cleanup(0);
1268 if (optimizing>0 && using_debug_info) /* -On and -g switches */
1270 "info:: assembly required 1+%d+1 passes\n", pass_cnt-2);
1272 } /* exit from assemble_file (...) */
1275 static int getkw (char *buf, char **value)
1279 /* allow leading spaces or tabs */
1280 while (*buf==' ' || *buf=='\t')
1288 while (*p && *p != ']') p++;
1295 while (*p && *p != ';') {
1303 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1310 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
1312 while (*buf!=']') buf++;
1319 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1321 if (!nasm_stricmp(p, "extern"))
1323 if (!nasm_stricmp(p, "bits"))
1325 if (!nasm_stricmp(p, "global"))
1327 if (!nasm_stricmp(p, "common"))
1329 if (!nasm_stricmp(p, "absolute"))
1331 if (!nasm_stricmp(p, "debug"))
1333 if (!nasm_stricmp(p, "warning"))
1335 if (!nasm_stricmp(p, "cpu"))
1337 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1342 static void report_error (int severity, char *fmt, ...)
1347 * See if it's a suppressed warning.
1349 if ((severity & ERR_MASK) == ERR_WARNING &&
1350 (severity & ERR_WARN_MASK) != 0 &&
1351 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1352 return; /* and bail out if so */
1355 * See if it's a pass-one only warning and we're not in pass one.
1357 if ((severity & ERR_PASS1) && pass0 == 2)
1360 if (severity & ERR_NOFILE)
1361 fputs ("nasm: ", error_file);
1363 char * currentfile = NULL;
1365 src_get (&lineno, ¤tfile);
1366 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
1367 nasm_free (currentfile);
1370 switch (severity & ERR_MASK) {
1372 fputs ("warning: ", error_file); break;
1374 fputs ("error: ", error_file); break;
1376 fputs ("fatal: ", error_file); break;
1378 fputs ("panic: ", error_file); break;
1380 fputs("debug: ", error_file); break;
1384 vfprintf (error_file, fmt, ap);
1385 fputc ('\n', error_file);
1387 if (severity & ERR_USAGE)
1390 switch (severity & ERR_MASK) {
1391 case ERR_WARNING: case ERR_DEBUG:
1392 /* no further action, by definition */
1395 /* terminate_after_phase = TRUE; *//**//* hack enables listing(!) on errors */
1396 terminate_after_phase = TRUE;
1405 exit(1); /* instantly die */
1406 break; /* placate silly compilers */
1409 /* abort(); */ /* halt, catch fire, and dump core */
1415 static void usage(void)
1417 fputs("type `nasm -h' for help\n", error_file);
1420 static void register_output_formats(void)
1422 ofmt = ofmt_register (report_error);
1425 #define BUF_DELTA 512
1427 static FILE *no_pp_fp;
1428 static efunc no_pp_err;
1429 static ListGen *no_pp_list;
1430 static long no_pp_lineinc;
1432 static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
1435 src_set_fname(nasm_strdup(file));
1439 no_pp_fp = fopen(file, "r");
1441 no_pp_err (ERR_FATAL | ERR_NOFILE,
1442 "unable to open input file `%s'", file);
1443 no_pp_list = listgen;
1444 (void) pass; /* placate compilers */
1445 (void) eval; /* placate compilers */
1448 static char *no_pp_getline (void)
1450 char *buffer, *p, *q;
1453 bufsize = BUF_DELTA;
1454 buffer = nasm_malloc(BUF_DELTA);
1455 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1457 while (1) { /* Loop to handle %line */
1460 while (1) { /* Loop to handle long lines */
1461 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1465 if (p > buffer && p[-1] == '\n')
1467 if (p-buffer > bufsize-10) {
1469 offset = p - buffer;
1470 bufsize += BUF_DELTA;
1471 buffer = nasm_realloc(buffer, bufsize);
1472 p = buffer + offset;
1476 if (!q && p == buffer) {
1482 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1483 * them are present at the end of the line.
1485 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1487 if (!strncmp(buffer, "%line", 5)) {
1490 char *nm = nasm_malloc(strlen(buffer));
1491 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1492 nasm_free( src_set_fname(nm) );
1502 no_pp_list->line (LIST_READ, buffer);
1507 static void no_pp_cleanup (int pass)
1512 static unsigned long get_cpu (char *value)
1515 if (!strcmp(value, "8086")) return IF_8086;
1516 if (!strcmp(value, "186")) return IF_186;
1517 if (!strcmp(value, "286")) return IF_286;
1518 if (!strcmp(value, "386")) return IF_386;
1519 if (!strcmp(value, "486")) return IF_486;
1520 if (!strcmp(value, "586") ||
1521 !nasm_stricmp(value, "pentium") ) return IF_PENT;
1522 if (!strcmp(value, "686") ||
1523 !nasm_stricmp(value, "ppro") ||
1524 !nasm_stricmp(value, "p2") ) return IF_P6;
1525 if (!nasm_stricmp(value, "p3") ||
1526 !nasm_stricmp(value, "katmai") ) return IF_KATMAI;
1527 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1528 !nasm_stricmp(value, "willamette") ) return IF_WILLAMETTE;
1530 report_error (pass0<2 ? ERR_NONFATAL : ERR_FATAL, "unknown 'cpu' type");
1532 return IF_PLEVEL; /* the maximum level */
1536 static int get_bits (char *value)
1540 if ((i = atoi(value)) == 16) return i; /* set for a 16-bit segment */
1543 report_error(ERR_NONFATAL,
1544 "cannot specify 32-bit segment on processor below a 386");
1548 report_error(pass0<2 ? ERR_NONFATAL : ERR_FATAL,
1549 "`%s' is not a valid segment size; must be 16 or 32",