NASM 0.98.08
[nasm/avx512.git] / nasm.c
blob1123ee300967857d4313969fdf652b098fcf9042
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.
7 */
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "insns.h"
18 #include "preproc.h"
19 #include "parser.h"
20 #include "eval.h"
21 #include "assemble.h"
22 #include "labels.h"
23 #include "outform.h"
24 #include "listing.h"
26 struct forwrefinfo { /* info held on forward refs. */
27 int lineno;
28 int operand;
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 #ifdef TASM_COMPAT
42 int tasm_compatible_mode = FALSE;
43 #endif
45 static char inname[FILENAME_MAX];
46 static char outname[FILENAME_MAX];
47 static char listname[FILENAME_MAX];
48 static int globallineno; /* for forward-reference tracking */
49 /* static int pass = 0; */
50 static struct ofmt *ofmt = NULL;
52 static FILE *error_file; /* Where to write error messages */
54 static FILE *ofile = NULL;
55 static int optimizing = 0; /* number of optimization passes to take */
56 static int sb, cmd_sb = 16; /* by default */
57 static unsigned long cmd_cpu = IF_PLEVEL; /* highest level by default */
58 static unsigned long cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
59 int global_offset_changed; /* referenced in labels.c */
61 static loc_t location;
62 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
63 static long abs_seg;
65 static struct RAA *offsets;
66 static long abs_offset;
68 static struct SAA *forwrefs; /* keep track of forward references */
69 static struct forwrefinfo *forwref;
71 static Preproc *preproc;
72 enum op_type {
73 op_normal, /* Preprocess and assemble */
74 op_preprocess, /* Preprocess only */
75 op_depend /* Generate dependencies */
77 static enum op_type operating_mode;
80 * Which of the suppressible warnings are suppressed. Entry zero
81 * doesn't do anything. Initial defaults are given here.
83 static char suppressed[1+ERR_WARN_MAX] = {
84 0, TRUE, TRUE, TRUE, FALSE
88 * The option names for the suppressible warnings. As before, entry
89 * zero does nothing.
91 static char *suppressed_names[1+ERR_WARN_MAX] = {
92 NULL, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
96 * The explanations for the suppressible warnings. As before, entry
97 * zero does nothing.
99 static char *suppressed_what[1+ERR_WARN_MAX] = {
100 NULL,
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"
108 * This is a null preprocessor which just copies lines from input
109 * to output. It's used when someone explicitly requests that NASM
110 * not preprocess their source file.
113 static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
114 static char *no_pp_getline (void);
115 static void no_pp_cleanup (void);
116 static Preproc no_pp = {
117 no_pp_reset,
118 no_pp_getline,
119 no_pp_cleanup
123 * get/set current offset...
125 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
126 raa_read(offsets,location.segment))
127 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
128 (void)(offsets=raa_write(offsets,location.segment,(x))))
130 static int want_usage;
131 static int terminate_after_phase;
132 int user_nolist = 0; /* fbk 9/2/00 */
134 static void nasm_fputs(char *line, FILE *ofile)
136 if (ofile) {
137 fputs(line, ofile);
138 fputc('\n', ofile);
139 } else
140 puts(line);
143 int main(int argc, char **argv)
145 pass0 = 1;
146 want_usage = terminate_after_phase = FALSE;
148 nasm_set_malloc_error (report_error);
149 offsets = raa_init();
150 forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
152 preproc = &nasmpp;
153 operating_mode = op_normal;
155 error_file = stderr;
157 seg_init();
159 register_output_formats();
161 parse_cmdline(argc, argv);
163 if (terminate_after_phase)
165 if (want_usage)
166 usage();
167 return 1;
170 if (ofmt->stdmac)
171 pp_extra_stdmac (ofmt->stdmac);
172 parser_global_info (ofmt, &location);
173 eval_global_info (ofmt, lookup_label, &location);
175 /* define some macros dependent of command-line */
177 char temp [64];
178 sprintf (temp, "__OUTPUT_FORMAT__=%s\n", ofmt->shortname);
179 pp_pre_define (temp);
182 switch ( operating_mode ) {
183 case op_depend:
185 char *line;
186 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
187 if (outname[0] == '\0')
188 ofmt->filename (inname, outname, report_error);
189 ofile = NULL;
190 fprintf(stdout, "%s: %s", outname, inname);
191 while ( (line = preproc->getline()) )
192 nasm_free (line);
193 preproc->cleanup();
194 putc('\n', stdout);
196 break;
198 case op_preprocess:
200 char *line;
201 char *file_name = NULL;
202 long prior_linnum=0;
203 int lineinc=0;
205 if (*outname) {
206 ofile = fopen(outname, "w");
207 if (!ofile)
208 report_error (ERR_FATAL | ERR_NOFILE,
209 "unable to open output file `%s'", outname);
210 } else
211 ofile = NULL;
213 location.known = FALSE;
215 /* pass = 1; */
216 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
217 while ( (line = preproc->getline()) ) {
219 * We generate %line directives if needed for later programs
221 long linnum = prior_linnum += lineinc;
222 int altline = src_get(&linnum, &file_name);
223 if (altline) {
224 if (altline==1 && lineinc==1)
225 nasm_fputs("", ofile);
226 else {
227 lineinc = (altline != -1 || lineinc!=1);
228 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
229 linnum, lineinc, file_name);
231 prior_linnum = linnum;
233 nasm_fputs(line, ofile);
234 nasm_free (line);
236 nasm_free(file_name);
237 preproc->cleanup();
238 if (ofile)
239 fclose(ofile);
240 if (ofile && terminate_after_phase)
241 remove(outname);
243 break;
245 case op_normal:
248 * We must call ofmt->filename _anyway_, even if the user
249 * has specified their own output file, because some
250 * formats (eg OBJ and COFF) use ofmt->filename to find out
251 * the name of the input file and then put that inside the
252 * file.
254 ofmt->filename (inname, outname, report_error);
256 ofile = fopen(outname, "wb");
257 if (!ofile) {
258 report_error (ERR_FATAL | ERR_NOFILE,
259 "unable to open output file `%s'", outname);
263 * We must call init_labels() before ofmt->init() since
264 * some object formats will want to define labels in their
265 * init routines. (eg OS/2 defines the FLAT group)
267 init_labels ();
269 ofmt->init (ofile, report_error, define_label, evaluate);
271 assemble_file (inname);
273 if (!terminate_after_phase) {
274 ofmt->cleanup (using_debug_info);
275 cleanup_labels ();
276 } else {
278 * We had an fclose on the output file here, but we
279 * actually do that in all the object file drivers as well,
280 * so we're leaving out the one here.
281 * fclose (ofile);
284 remove(outname);
285 if (listname[0])
286 remove(listname);
289 break;
292 if (want_usage)
293 usage();
295 raa_free (offsets);
296 saa_free (forwrefs);
297 eval_cleanup ();
298 nasmlib_cleanup ();
300 if (terminate_after_phase)
301 return 1;
302 else
303 return 0;
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)
313 *advance = 0;
314 if (p[2]) /* the parameter's in the option */
316 p += 2;
317 while (isspace(*p))
318 p++;
319 return p;
321 if (q && q[0])
323 *advance = 1;
324 return q;
326 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
327 "option `-%c' requires an argument",
328 p[1]);
329 return NULL;
332 struct textargs
334 char *label;
335 int value;
338 #define OPT_PREFIX 0
339 #define OPT_POSTFIX 1
340 struct textargs textopts[] =
342 {"prefix",OPT_PREFIX},
343 {"postfix",OPT_POSTFIX},
344 {NULL,0}
348 int stopoptions = 0;
349 static int process_arg (char *p, char *q)
351 char *param;
352 int i, advance = 0;
354 if (!p || !p[0])
355 return 0;
357 if (p[0]=='-' && ! stopoptions)
359 switch (p[1]) {
360 case 's':
361 error_file = stdout;
362 break;
363 case 'o': /* these parameters take values */
364 case 'O':
365 case 'f':
366 case 'p':
367 case 'd':
368 case 'D':
369 case 'i':
370 case 'l':
371 case 'E':
372 case 'F':
373 if ( !(param = get_param (p, q, &advance)) )
374 break;
375 if (p[1]=='o') { /* output file */
376 strcpy (outname, param);
377 } else if (p[1]=='f') { /* output format */
378 ofmt = ofmt_find(param);
379 if (!ofmt) {
380 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
381 "unrecognised output format `%s' - "
382 "use -hf for a list",
383 param);
385 else
386 ofmt->current_dfmt = ofmt->debug_formats[0];
387 } else if (p[1]=='O') { /* Optimization level */
388 if (!isdigit(*param)) report_error(ERR_FATAL,
389 "command line optimization level must be 0..3 or <nn>");
390 optimizing = atoi(param);
391 if (optimizing <= 0) optimizing = 0;
392 else if (optimizing <= 3) optimizing *= 5; /* 5 passes for each level */
393 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
394 pp_pre_include (param);
395 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
396 pp_pre_define (param);
397 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
398 pp_pre_undefine (param);
399 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
400 pp_include_path (param);
401 } else if (p[1]=='l') { /* listing file */
402 strcpy (listname, param);
403 } else if (p[1]=='E') { /* error messages file */
404 error_file = fopen(param, "w");
405 if ( !error_file ) {
406 error_file = stderr; /* Revert to default! */
407 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
408 "cannot open file `%s' for error messages",
409 param);
411 } else if (p[1] == 'F') { /* specify debug format */
412 ofmt->current_dfmt = dfmt_find(ofmt, param);
413 if (!ofmt->current_dfmt) {
414 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
415 "unrecognized debug format `%s' for"
416 " output format `%s'",
417 param, ofmt->shortname);
420 break;
421 case 'g':
422 using_debug_info = TRUE;
423 break;
424 case 'h':
425 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
426 "[-l listfile]\n"
427 " [options...] [--] filename\n"
428 " or nasm -r for version info\n\n"
429 #ifdef TASM_COMPAT
430 " -t Assemble in SciTech TASM compatible mode\n"
431 " -g Generate debug information in selected format.\n"
432 #endif
433 " -e preprocess only (writes output to stdout by default)\n"
434 " -a don't preprocess (assemble only)\n"
435 " -M generate Makefile dependencies on stdout\n\n"
436 " -E<file> redirect error messages to file\n"
437 " -s redirect error messages to stdout\n\n"
438 " -g enable debug info\n"
439 " -F format select a debugging format\n\n"
440 " -I<path> adds a pathname to the include file path\n"
441 " -O<digit> optimize branch offsets -O0 disables, -O2 default\n"
442 " -P<file> pre-includes a file\n"
443 " -D<macro>[=<value>] pre-defines a macro\n"
444 " -U<macro> undefines a macro\n"
445 " -w+foo enables warnings about foo; -w-foo disables them\n"
446 "where foo can be:\n");
447 for (i=1; i<=ERR_WARN_MAX; i++)
448 printf(" %-16s%s (default %s)\n",
449 suppressed_names[i], suppressed_what[i],
450 suppressed[i] ? "off" : "on");
451 printf ("\nresponse files should contain command line parameters"
452 ", one per line.\n");
453 if (p[2] == 'f') {
454 printf("\nvalid output formats for -f are"
455 " (`*' denotes default):\n");
456 ofmt_list(ofmt, stdout);
458 else {
459 printf ("\nFor a list of valid output formats, use -hf.\n");
460 printf ("For a list of debug formats, use -f <form> -y.\n");
462 exit (0); /* never need usage message here */
463 break;
464 case 'y':
465 printf("\nvalid debug formats for '%s' output format are"
466 " ('*' denotes default):\n",
467 ofmt->shortname);
468 dfmt_list(ofmt, stdout);
469 exit(0);
470 break;
471 #ifdef TASM_COMPAT
472 case 't':
473 tasm_compatible_mode = TRUE;
474 break;
475 #endif
476 case 'r':
477 #ifdef TASM_COMPAT
478 printf("NASM version %s - SciTech TASM compatible additions\n", NASM_VER);
479 #else
480 printf("NASM version %s\n", NASM_VER);
481 #endif
482 #ifdef DEBUG
483 printf("Compiled with -DDEBUG on " __DATE__ "\n");
484 #endif
485 exit (0); /* never need usage message here */
486 break;
487 case 'e': /* preprocess only */
488 operating_mode = op_preprocess;
489 break;
490 case 'a': /* assemble only - don't preprocess */
491 preproc = &no_pp;
492 break;
493 case 'w':
494 if (p[2] != '+' && p[2] != '-') {
495 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
496 "invalid option to `-w'");
497 } else {
498 for (i=1; i<=ERR_WARN_MAX; i++)
499 if (!nasm_stricmp(p+3, suppressed_names[i]))
500 break;
501 if (i <= ERR_WARN_MAX)
502 suppressed[i] = (p[2] == '-');
503 else
504 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
505 "invalid option to `-w'");
507 break;
508 case 'M':
509 operating_mode = op_depend;
510 break;
512 case '-':
514 int s;
516 if (p[2]==0) { /* -- => stop processing options */
517 stopoptions = 1;
518 break;
520 for(s=0; textopts[s].label; s++)
522 if(!nasm_stricmp(p+2, textopts[s].label))
524 break;
528 switch(s)
531 case OPT_PREFIX:
532 case OPT_POSTFIX:
534 if (!q)
536 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
537 "option `--%s' requires an argument",
538 p+2);
539 break;
541 else
543 advance = 1, param = q;
546 if(s == OPT_PREFIX)
548 strncpy(lprefix,param,PREFIX_MAX-1);
549 lprefix[PREFIX_MAX-1]=0;
550 break;
552 if(s == OPT_POSTFIX)
554 strncpy(lpostfix,param,POSTFIX_MAX-1);
555 lpostfix[POSTFIX_MAX-1]=0;
556 break;
558 break;
560 default:
562 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
563 "unrecognised option `--%s'",
564 p+2);
565 break;
568 break;
571 default:
572 if (!ofmt->setinfo(GI_SWITCH,&p))
573 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
574 "unrecognised option `-%c'",
575 p[1]);
576 break;
579 else
581 if (*inname) {
582 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
583 "more than one input file specified");
584 } else
585 strcpy(inname, p);
588 return advance;
591 #define ARG_BUF_DELTA 128
593 static void process_respfile (FILE *rfile)
595 char *buffer, *p, *q, *prevarg;
596 int bufsize, prevargsize;
598 bufsize = prevargsize = ARG_BUF_DELTA;
599 buffer = nasm_malloc(ARG_BUF_DELTA);
600 prevarg = nasm_malloc(ARG_BUF_DELTA);
601 prevarg[0] = '\0';
603 while (1) { /* Loop to handle all lines in file */
605 p = buffer;
606 while (1) { /* Loop to handle long lines */
607 q = fgets(p, bufsize-(p-buffer), rfile);
608 if (!q)
609 break;
610 p += strlen(p);
611 if (p > buffer && p[-1] == '\n')
612 break;
613 if (p-buffer > bufsize-10) {
614 int offset;
615 offset = p - buffer;
616 bufsize += ARG_BUF_DELTA;
617 buffer = nasm_realloc(buffer, bufsize);
618 p = buffer + offset;
622 if (!q && p == buffer) {
623 if (prevarg[0])
624 process_arg (prevarg, NULL);
625 nasm_free (buffer);
626 nasm_free (prevarg);
627 return;
631 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
632 * them are present at the end of the line.
634 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
636 while (p > buffer && isspace(p[-1]))
637 *--p = '\0';
639 p = buffer;
640 while (isspace(*p))
641 p++;
643 if (process_arg (prevarg, p))
644 *p = '\0';
646 if (strlen(p) > prevargsize-10) {
647 prevargsize += ARG_BUF_DELTA;
648 prevarg = nasm_realloc(prevarg, prevargsize);
650 strcpy (prevarg, p);
654 /* Function to process args from a string of args, rather than the
655 * argv array. Used by the environment variable and response file
656 * processing.
658 #ifdef TASM_COMPAT
659 static void process_args (char *args) {
660 char *p, *q, *arg, *prevarg;
661 char separator = ' ';
663 p = args;
664 if (*p && *p != '-')
665 separator = *p++;
666 arg = NULL;
667 while (*p) {
668 q = p;
669 while (*p && *p != separator) p++;
670 while (*p == separator) *p++ = '\0';
671 prevarg = arg;
672 arg = q;
673 if (process_arg (prevarg, arg))
674 arg = NULL;
676 if (arg)
677 process_arg (arg, NULL);
679 #endif
681 static void parse_cmdline(int argc, char **argv)
683 FILE *rfile;
684 char *envreal, *envcopy=NULL, *p, *arg;
685 #ifndef TASM_COMPAT
686 char *q, *prevarg;
687 char separator = ' ';
688 #endif
690 *inname = *outname = *listname = '\0';
693 * First, process the NASM environment variable.
695 envreal = getenv("NASM");
696 arg = NULL;
697 if (envreal) {
698 envcopy = nasm_strdup(envreal);
699 #ifdef TASM_COMPAT
700 process_args(envcopy);
701 #else
702 p = envcopy;
703 if (*p && *p != '-')
704 separator = *p++;
705 while (*p) {
706 q = p;
707 while (*p && *p != separator) p++;
708 while (*p == separator) *p++ = '\0';
709 prevarg = arg;
710 arg = q;
711 if (process_arg (prevarg, arg))
712 arg = NULL;
714 if (arg)
715 process_arg (arg, NULL);
716 #endif
717 nasm_free (envcopy);
721 * Now process the actual command line.
723 while (--argc)
725 int i;
726 argv++;
727 #ifdef TASM_COMPAT
728 if (argv[0][0] == '@') {
729 /* We have a response file, so process this as a set of
730 * arguments like the environment variable. This allows us
731 * to have multiple arguments on a single line, which is
732 * different to the -@resp file processing below for regular
733 * NASM.
735 char *str = malloc(2048);
736 FILE *f = fopen(&argv[0][1],"r");
737 if (!str) {
738 printf("out of memory");
739 exit(-1);
741 if (f) {
742 while (fgets(str,2048,f)) {
743 process_args(str);
745 fclose(f);
747 free(str);
748 argc--;
749 argv++;
751 #endif
752 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
753 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
754 if ((rfile = fopen(p, "r"))) {
755 process_respfile (rfile);
756 fclose(rfile);
757 } else
758 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
759 "unable to open response file `%s'", p);
761 } else
762 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
763 argv += i, argc -= i;
766 if (!*inname)
767 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
768 "no input file specified");
772 static void assemble_file (char *fname)
774 char * value, * p, * q, * special, * line, debugid[80];
775 insn output_ins;
776 int i, rn_error, validid;
777 long seg, offs;
778 struct tokenval tokval;
779 expr * e;
780 int pass, pass_max;
781 int pass_cnt = 0; /* count actual passes */
783 if (cmd_sb == 32 && cmd_cpu < IF_386)
784 report_error(ERR_FATAL, "command line: "
785 "32-bit segment size requires a higher cpu");
787 pass_max = optimizing + 2; /* passes 1, optimizing, then 2 */
788 pass0 = !optimizing; /* start at 1 if not optimizing */
789 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
790 int pass1, pass2;
791 ldfunc def_label;
793 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
794 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
795 /* pass0 seq is 0, 0, 0,..., 1, 2 */
797 def_label = pass > 1 ? redefine_label : define_label;
800 sb = cmd_sb; /* set 'bits' to command line default */
801 cpu = cmd_cpu;
802 if (pass0 == 2) {
803 if (*listname)
804 nasmlist.init(listname, report_error);
806 in_abs_seg = FALSE;
807 global_offset_changed = FALSE; /* set by redefine_label */
808 location.segment = ofmt->section(NULL, pass2, &sb);
809 if (pass > 1) {
810 saa_rewind (forwrefs);
811 forwref = saa_rstruct (forwrefs);
812 raa_free (offsets);
813 offsets = raa_init();
815 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
816 globallineno = 0;
817 if (pass == 1) location.known = TRUE;
818 location.offset = offs = GET_CURR_OFFS;
820 while ( (line = preproc->getline()) )
822 globallineno++;
824 /* here we parse our directives; this is not handled by the 'real'
825 * parser. */
826 if ( (i = getkw (line, &value)) )
828 switch (i) {
829 case 1: /* [SEGMENT n] */
830 seg = ofmt->section (value, pass2, &sb);
831 if (seg == NO_SEG) {
832 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
833 "segment name `%s' not recognised",
834 value);
835 } else {
836 in_abs_seg = FALSE;
837 location.segment = seg;
839 break;
840 case 2: /* [EXTERN label:special] */
841 if (*value == '$') value++; /* skip initial $ if present */
842 if (pass0 == 2) {
843 q = value;
844 while (*q && *q != ':')
845 q++;
846 if (*q == ':') {
847 *q++ = '\0';
848 ofmt->symdef(value, 0L, 0L, 3, q);
850 } else if (pass0 == 1) { /* pass == 1 */
851 q = value;
852 validid = TRUE;
853 if (!isidstart(*q))
854 validid = FALSE;
855 while (*q && *q != ':') {
856 if (!isidchar(*q))
857 validid = FALSE;
858 q++;
860 if (!validid) {
861 report_error (ERR_NONFATAL,
862 "identifier expected after EXTERN");
863 break;
865 if (*q == ':') {
866 *q++ = '\0';
867 special = q;
868 } else
869 special = NULL;
870 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
871 declare_as_global (value, special, report_error);
872 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
873 ofmt, report_error);
875 } /* else pass0 == 1 */
876 break;
877 case 3: /* [BITS bits] */
878 sb = get_bits(value);
879 break;
880 case 4: /* [GLOBAL symbol:special] */
881 if (*value == '$') value++; /* skip initial $ if present */
882 if (pass0 == 2) { /* pass 2 */
883 q = value;
884 while (*q && *q != ':')
885 q++;
886 if (*q == ':') {
887 *q++ = '\0';
888 ofmt->symdef(value, 0L, 0L, 3, q);
890 } else if (pass2 == 1) { /* pass == 1 */
891 q = value;
892 validid = TRUE;
893 if (!isidstart(*q))
894 validid = FALSE;
895 while (*q && *q != ':') {
896 if (!isidchar(*q))
897 validid = FALSE;
898 q++;
900 if (!validid) {
901 report_error (ERR_NONFATAL,
902 "identifier expected after GLOBAL");
903 break;
905 if (*q == ':') {
906 *q++ = '\0';
907 special = q;
908 } else
909 special = NULL;
910 declare_as_global (value, special, report_error);
911 } /* pass == 1 */
912 break;
913 case 5: /* [COMMON symbol size:special] */
914 if (*value == '$') value++; /* skip initial $ if present */
915 if (pass0 == 1) {
916 p = value;
917 validid = TRUE;
918 if (!isidstart(*p))
919 validid = FALSE;
920 while (*p && !isspace(*p)) {
921 if (!isidchar(*p))
922 validid = FALSE;
923 p++;
925 if (!validid) {
926 report_error (ERR_NONFATAL,
927 "identifier expected after COMMON");
928 break;
930 if (*p) {
931 long size;
933 while (*p && isspace(*p))
934 *p++ = '\0';
935 q = p;
936 while (*q && *q != ':')
937 q++;
938 if (*q == ':') {
939 *q++ = '\0';
940 special = q;
941 } else
942 special = NULL;
943 size = readnum (p, &rn_error);
944 if (rn_error)
945 report_error (ERR_NONFATAL, "invalid size specified"
946 " in COMMON declaration");
947 else
948 define_common (value, seg_alloc(), size,
949 special, ofmt, report_error);
950 } else
951 report_error (ERR_NONFATAL, "no size specified in"
952 " COMMON declaration");
953 } else if (pass0 == 2) { /* pass == 2 */
954 q = value;
955 while (*q && *q != ':') {
956 if (isspace(*q))
957 *q = '\0';
958 q++;
960 if (*q == ':') {
961 *q++ = '\0';
962 ofmt->symdef(value, 0L, 0L, 3, q);
965 break;
966 case 6: /* [ABSOLUTE address] */
967 stdscan_reset();
968 stdscan_bufptr = value;
969 tokval.t_type = TOKEN_INVALID;
970 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, report_error,
971 NULL);
972 if (e) {
973 if (!is_reloc(e))
974 report_error (pass0==1 ? ERR_NONFATAL : ERR_PANIC,
975 "cannot use non-relocatable expression as "
976 "ABSOLUTE address");
977 else {
978 abs_seg = reloc_seg(e);
979 abs_offset = reloc_value(e);
981 } else
982 if (pass==1) abs_offset = 0x100;/* don't go near zero in case of / */
983 else report_error (ERR_PANIC, "invalid ABSOLUTE address "
984 "in pass two");
985 in_abs_seg = TRUE;
986 location.segment = abs_seg;
987 break;
988 case 7: /* DEBUG */
989 p = value;
990 q = debugid;
991 validid = TRUE;
992 if (!isidstart(*p))
993 validid = FALSE;
994 while (*p && !isspace(*p)) {
995 if (!isidchar(*p))
996 validid = FALSE;
997 *q++ = *p++;
999 *q++ = 0;
1000 if (!validid) {
1001 report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
1002 "identifier expected after DEBUG");
1003 break;
1005 while (*p && isspace(*p)) p++;
1006 if (pass==pass_max) ofmt->current_dfmt->debug_directive (debugid, p);
1007 break;
1008 case 8: /* [WARNING {+|-}warn-name] */
1009 if (pass1 == 1) {
1010 while (*value && isspace(*value))
1011 value++;
1013 if (*value == '+' || *value == '-') {
1014 validid = (*value == '-') ? TRUE : FALSE;
1015 value++;
1016 } else
1017 validid = FALSE;
1019 for (i=1; i<=ERR_WARN_MAX; i++)
1020 if (!nasm_stricmp(value, suppressed_names[i]))
1021 break;
1022 if (i <= ERR_WARN_MAX)
1023 suppressed[i] = validid;
1024 else
1025 report_error (ERR_NONFATAL, "invalid warning id in WARNING directive");
1027 break;
1028 case 9: /* cpu */
1029 cpu = get_cpu (value);
1030 break;
1031 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1032 while (*value && isspace(*value))
1033 value++;
1035 if (*value == '+') {
1036 user_nolist = 0;
1038 else {
1039 if (*value == '-') {
1040 user_nolist = 1;
1042 else {
1043 report_error (ERR_NONFATAL, "invalid parameter to \"list\" directive");
1046 break;
1047 default:
1048 if (!ofmt->directive (line+1, value, pass2))
1049 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
1050 "unrecognised directive [%s]",
1051 line+1);
1054 else /* it isn't a directive */
1056 parse_line (pass1, line, &output_ins,
1057 report_error, evaluate,
1058 def_label);
1060 if (!optimizing && pass == 2) {
1061 if (forwref != NULL && globallineno == forwref->lineno) {
1062 output_ins.forw_ref = TRUE;
1063 do {
1064 output_ins.oprs[forwref->operand].opflags|= OPFLAG_FORWARD;
1065 forwref = saa_rstruct (forwrefs);
1066 } while (forwref != NULL && forwref->lineno == globallineno);
1067 } else
1068 output_ins.forw_ref = FALSE;
1072 if (!optimizing && output_ins.forw_ref)
1074 if (pass == 1) {
1075 for(i = 0; i < output_ins.operands; i++)
1077 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
1079 struct forwrefinfo *fwinf =
1080 (struct forwrefinfo *)saa_wstruct(forwrefs);
1081 fwinf->lineno = globallineno;
1082 fwinf->operand = i;
1085 } else { /* pass == 2 */
1087 * Hack to prevent phase error in the code
1088 * rol ax,x
1089 * x equ 1
1091 * If the second operand is a forward reference,
1092 * the UNITY property of the number 1 in that
1093 * operand is cancelled. Otherwise the above
1094 * sequence will cause a phase error.
1096 * This hack means that the above code will
1097 * generate 286+ code.
1099 * The forward reference will mean that the
1100 * operand will not have the UNITY property on
1101 * the first pass, so the pass behaviours will
1102 * be consistent.
1105 if (output_ins.operands >= 2 &&
1106 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1108 output_ins.oprs[1].type &= ~(ONENESS|BYTENESS);
1111 } /* pass == 2 */
1113 } /* forw_ref */
1116 if (output_ins.opcode == I_EQU) {
1117 if (pass1 == 1)
1120 * Special `..' EQUs get processed in pass two,
1121 * except `..@' macro-processor EQUs which are done
1122 * in the normal place.
1124 if (!output_ins.label)
1125 report_error (ERR_NONFATAL,
1126 "EQU not preceded by label");
1128 else if (output_ins.label[0] != '.' ||
1129 output_ins.label[1] != '.' ||
1130 output_ins.label[2] == '@')
1132 if (output_ins.operands == 1 &&
1133 (output_ins.oprs[0].type & IMMEDIATE) &&
1134 output_ins.oprs[0].wrt == NO_SEG)
1136 int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
1137 def_label (output_ins.label,
1138 output_ins.oprs[0].segment,
1139 output_ins.oprs[0].offset,
1140 NULL, FALSE, isext, ofmt, report_error);
1142 else if (output_ins.operands == 2 &&
1143 (output_ins.oprs[0].type & IMMEDIATE) &&
1144 (output_ins.oprs[0].type & COLON) &&
1145 output_ins.oprs[0].segment == NO_SEG &&
1146 output_ins.oprs[0].wrt == NO_SEG &&
1147 (output_ins.oprs[1].type & IMMEDIATE) &&
1148 output_ins.oprs[1].segment == NO_SEG &&
1149 output_ins.oprs[1].wrt == NO_SEG)
1151 def_label (output_ins.label,
1152 output_ins.oprs[0].offset | SEG_ABS,
1153 output_ins.oprs[1].offset,
1154 NULL, FALSE, FALSE, ofmt, report_error);
1156 else
1157 report_error(ERR_NONFATAL, "bad syntax for EQU");
1159 } else { /* pass == 2 */
1161 * Special `..' EQUs get processed here, except
1162 * `..@' macro processor EQUs which are done above.
1164 if (output_ins.label[0] == '.' &&
1165 output_ins.label[1] == '.' &&
1166 output_ins.label[2] != '@')
1168 if (output_ins.operands == 1 &&
1169 (output_ins.oprs[0].type & IMMEDIATE)) {
1170 define_label (output_ins.label,
1171 output_ins.oprs[0].segment,
1172 output_ins.oprs[0].offset,
1173 NULL, FALSE, FALSE, ofmt, report_error);
1175 else if (output_ins.operands == 2 &&
1176 (output_ins.oprs[0].type & IMMEDIATE) &&
1177 (output_ins.oprs[0].type & COLON) &&
1178 output_ins.oprs[0].segment == NO_SEG &&
1179 (output_ins.oprs[1].type & IMMEDIATE) &&
1180 output_ins.oprs[1].segment == NO_SEG)
1182 define_label (output_ins.label,
1183 output_ins.oprs[0].offset | SEG_ABS,
1184 output_ins.oprs[1].offset,
1185 NULL, FALSE, FALSE, ofmt, report_error);
1187 else
1188 report_error(ERR_NONFATAL, "bad syntax for EQU");
1190 } /* pass == 2 */
1191 } else { /* instruction isn't an EQU */
1193 if (pass1 == 1) {
1195 long l = insn_size (location.segment, offs, sb, cpu,
1196 &output_ins, report_error);
1198 /* if (using_debug_info) && output_ins.opcode != -1)*/
1199 if (using_debug_info); /* fbk 12/29/00 */
1202 /* this is done here so we can do debug type info */
1203 long typeinfo = TYS_ELEMENTS(output_ins.operands);
1204 switch (output_ins.opcode) {
1205 case I_RESB:
1206 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1207 break;
1208 case I_RESW:
1209 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1210 break;
1211 case I_RESD:
1212 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1213 break;
1214 case I_RESQ:
1215 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1216 break;
1217 case I_REST:
1218 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1219 break;
1220 case I_DB:
1221 typeinfo |= TY_BYTE;
1222 break;
1223 case I_DW:
1224 typeinfo |= TY_WORD;
1225 break;
1226 case I_DD:
1227 if (output_ins.eops_float)
1228 typeinfo |= TY_FLOAT;
1229 else
1230 typeinfo |= TY_DWORD;
1231 break;
1232 case I_DQ:
1233 typeinfo |= TY_QWORD;
1234 break;
1235 case I_DT:
1236 typeinfo |= TY_TBYTE;
1237 break;
1238 default:
1239 typeinfo = TY_LABEL;
1243 ofmt->current_dfmt->debug_typevalue(typeinfo);
1246 if (l != -1) {
1247 offs += l;
1248 SET_CURR_OFFS (offs);
1251 * else l == -1 => invalid instruction, which will be
1252 * flagged as an error on pass 2
1255 } else { /* pass == 2 */
1256 offs += assemble (location.segment, offs, sb, cpu,
1257 &output_ins, ofmt, report_error, &nasmlist);
1258 SET_CURR_OFFS (offs);
1261 } /* not an EQU */
1262 cleanup_insn (&output_ins);
1264 nasm_free (line);
1265 location.offset = offs = GET_CURR_OFFS;
1266 } /* end while (line = preproc->getline... */
1268 if (pass1==2 && global_offset_changed)
1269 report_error(ERR_NONFATAL, "phase error detected at end of assembly.");
1271 if (pass1 == 1) preproc->cleanup();
1273 if (pass1==1 && terminate_after_phase) {
1274 fclose(ofile);
1275 remove(outname);
1276 if (want_usage)
1277 usage();
1278 exit (1);
1280 pass_cnt++;
1281 if (pass>1 && !global_offset_changed) {
1282 pass0++;
1283 if (pass0==2) pass = pass_max - 1;
1284 } else if (!optimizing) pass0++;
1286 } /* for (pass=1; pass<=2; pass++) */
1288 nasmlist.cleanup();
1289 #if 1
1290 if (optimizing && using_debug_info) /* -On and -g switches */
1291 fprintf(error_file,
1292 "info:: assembly required 1+%d+1 passes\n", pass_cnt-2);
1293 #endif
1294 } /* exit from assemble_file (...) */
1297 static int getkw (char *buf, char **value)
1299 char *p, *q;
1301 if (*buf!='[')
1302 return 0;
1304 p = buf;
1306 while (*p && *p != ']') p++;
1308 if (!*p)
1309 return 0;
1311 q = p++;
1313 while (*p && *p != ';') {
1314 if (!isspace(*p))
1315 return 0;
1316 p++;
1318 q[1] = '\0';
1320 p = buf+1;
1321 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1322 buf++;
1323 if (*buf==']') {
1324 *buf = '\0';
1325 *value = buf;
1326 } else {
1327 *buf++ = '\0';
1328 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
1329 *value = buf;
1330 while (*buf!=']') buf++;
1331 *buf++ = '\0';
1333 #if 0
1334 for (q=p; *q; q++)
1335 *q = tolower(*q);
1336 #endif
1337 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1338 return 1;
1339 if (!nasm_stricmp(p, "extern"))
1340 return 2;
1341 if (!nasm_stricmp(p, "bits"))
1342 return 3;
1343 if (!nasm_stricmp(p, "global"))
1344 return 4;
1345 if (!nasm_stricmp(p, "common"))
1346 return 5;
1347 if (!nasm_stricmp(p, "absolute"))
1348 return 6;
1349 if (!nasm_stricmp(p, "debug"))
1350 return 7;
1351 if (!nasm_stricmp(p, "warning"))
1352 return 8;
1353 if (!nasm_stricmp(p, "cpu"))
1354 return 9;
1355 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1356 return 10;
1357 return -1;
1360 static void report_error (int severity, char *fmt, ...)
1362 va_list ap;
1365 * See if it's a suppressed warning.
1367 if ((severity & ERR_MASK) == ERR_WARNING &&
1368 (severity & ERR_WARN_MASK) != 0 &&
1369 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1370 return; /* and bail out if so */
1373 * See if it's a pass-one only warning and we're not in pass one.
1375 if ((severity & ERR_PASS1) && pass0 == 2)
1376 return;
1378 if (severity & ERR_NOFILE)
1379 fputs ("nasm: ", error_file);
1380 else {
1381 char * currentfile = NULL;
1382 long lineno = 0;
1383 src_get (&lineno, &currentfile);
1384 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
1385 nasm_free (currentfile);
1388 switch (severity & ERR_MASK) {
1389 case ERR_WARNING:
1390 fputs ("warning: ", error_file); break;
1391 case ERR_NONFATAL:
1392 fputs ("error: ", error_file); break;
1393 case ERR_FATAL:
1394 fputs ("fatal: ", error_file); break;
1395 case ERR_PANIC:
1396 fputs ("panic: ", error_file); break;
1397 case ERR_DEBUG:
1398 fputs("debug: ", error_file); break;
1401 va_start (ap, fmt);
1402 vfprintf (error_file, fmt, ap);
1403 fputc ('\n', error_file);
1405 if (severity & ERR_USAGE)
1406 want_usage = TRUE;
1408 switch (severity & ERR_MASK) {
1409 case ERR_WARNING: case ERR_DEBUG:
1410 /* no further action, by definition */
1411 break;
1412 case ERR_NONFATAL:
1413 terminate_after_phase = TRUE;
1414 break;
1415 case ERR_FATAL:
1416 if (ofile) {
1417 fclose(ofile);
1418 remove(outname);
1420 if (want_usage)
1421 usage();
1422 exit(1); /* instantly die */
1423 break; /* placate silly compilers */
1424 case ERR_PANIC:
1425 fflush(NULL);
1426 /* abort(); */ /* halt, catch fire, and dump core */
1427 exit(3);
1428 break;
1432 static void usage(void)
1434 fputs("type `nasm -h' for help\n", error_file);
1437 static void register_output_formats(void)
1439 ofmt = ofmt_register (report_error);
1442 #define BUF_DELTA 512
1444 static FILE *no_pp_fp;
1445 static efunc no_pp_err;
1446 static ListGen *no_pp_list;
1447 static long no_pp_lineinc;
1449 static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
1450 ListGen *listgen)
1452 src_set_fname(nasm_strdup(file));
1453 src_set_linnum(0);
1454 no_pp_lineinc = 1;
1455 no_pp_err = error;
1456 no_pp_fp = fopen(file, "r");
1457 if (!no_pp_fp)
1458 no_pp_err (ERR_FATAL | ERR_NOFILE,
1459 "unable to open input file `%s'", file);
1460 no_pp_list = listgen;
1461 (void) pass; /* placate compilers */
1462 (void) eval; /* placate compilers */
1465 static char *no_pp_getline (void)
1467 char *buffer, *p, *q;
1468 int bufsize;
1470 bufsize = BUF_DELTA;
1471 buffer = nasm_malloc(BUF_DELTA);
1472 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1474 while (1) { /* Loop to handle %line */
1476 p = buffer;
1477 while (1) { /* Loop to handle long lines */
1478 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1479 if (!q)
1480 break;
1481 p += strlen(p);
1482 if (p > buffer && p[-1] == '\n')
1483 break;
1484 if (p-buffer > bufsize-10) {
1485 int offset;
1486 offset = p - buffer;
1487 bufsize += BUF_DELTA;
1488 buffer = nasm_realloc(buffer, bufsize);
1489 p = buffer + offset;
1493 if (!q && p == buffer) {
1494 nasm_free (buffer);
1495 return NULL;
1499 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1500 * them are present at the end of the line.
1502 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1504 if (!strncmp(buffer, "%line", 5)) {
1505 long ln;
1506 int li;
1507 char *nm = nasm_malloc(strlen(buffer));
1508 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1509 nasm_free( src_set_fname(nm) );
1510 src_set_linnum(ln);
1511 no_pp_lineinc = li;
1512 continue;
1514 nasm_free(nm);
1516 break;
1519 no_pp_list->line (LIST_READ, buffer);
1521 return buffer;
1524 static void no_pp_cleanup (void)
1526 fclose(no_pp_fp);
1529 static unsigned long get_cpu (char *value)
1532 if (!strcmp(value, "8086")) return IF_8086;
1533 if (!strcmp(value, "186")) return IF_186;
1534 if (!strcmp(value, "286")) return IF_286;
1535 if (!strcmp(value, "386")) return IF_386;
1536 if (!strcmp(value, "486")) return IF_486;
1537 if (!strcmp(value, "586") ||
1538 !nasm_stricmp(value, "pentium") ) return IF_PENT;
1539 if (!strcmp(value, "686") ||
1540 !nasm_stricmp(value, "ppro") ||
1541 !nasm_stricmp(value, "p2") ) return IF_P6;
1542 if (!nasm_stricmp(value, "p3") ||
1543 !nasm_stricmp(value, "katmai") ) return IF_KATMAI;
1545 report_error (pass0<2 ? ERR_NONFATAL : ERR_FATAL, "unknown 'cpu' type");
1547 return IF_PLEVEL; /* the maximum level */
1551 static int get_bits (char *value)
1553 int i;
1555 if ((i = atoi(value)) == 16) return i; /* set for a 16-bit segment */
1556 else if (i == 32) {
1557 if (cpu < IF_386) {
1558 report_error(ERR_NONFATAL,
1559 "cannot specify 32-bit segment on processor below a 386");
1560 i = 16;
1562 } else {
1563 report_error(pass0<2 ? ERR_NONFATAL : ERR_FATAL,
1564 "`%s' is not a valid segment size; must be 16 or 32",
1565 value);
1566 i = 16;
1568 return i;
1571 /* end of nasm.c */