Bring CHANGES up to date.
[nasm/avx512.git] / nasm.c
blobeb94a01d0b5e14e43f74a640c760aaae5f8da0d4
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 parse_cmdline (int, char **);
34 static void assemble_file (char *);
35 static int getkw (char *buf, char **value);
36 static void register_output_formats(void);
37 static void report_error (int severity, const char *fmt, ...);
38 static void usage(void);
40 static int using_debug_info, opt_verbose_info;
41 int tasm_compatible_mode = FALSE;
42 int pass0;
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 */
62 long abs_seg; /* ABSOLUTE segment basis */
63 long abs_offset; /* ABSOLUTE offset */
65 static struct RAA *offsets;
67 static struct SAA *forwrefs; /* keep track of forward references */
68 static struct forwrefinfo *forwref;
70 static Preproc *preproc;
71 enum op_type {
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
88 * zero does nothing.
90 static const char *suppressed_names[1+ERR_WARN_MAX] = {
91 NULL, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
92 "gnu-elf-extensions"
96 * The explanations for the suppressible warnings. As before, entry
97 * zero does nothing.
99 static const 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",
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 = {
118 no_pp_reset,
119 no_pp_getline,
120 no_pp_cleanup
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(const char *line, FILE *outfile)
137 if (outfile) {
138 fputs(line, outfile);
139 fputc('\n', outfile);
140 } else
141 puts(line);
144 int main(int argc, char **argv)
146 pass0 = 1;
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));
153 preproc = &nasmpp;
154 operating_mode = op_normal;
156 error_file = stderr;
158 seg_init();
160 register_output_formats();
162 parse_cmdline(argc, argv);
164 if (terminate_after_phase)
166 if (want_usage)
167 usage();
168 return 1;
171 if (ofmt->stdmac)
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 */
178 char temp [64];
179 sprintf (temp, "__OUTPUT_FORMAT__=%s\n", ofmt->shortname);
180 pp_pre_define (temp);
183 switch ( operating_mode ) {
184 case op_depend:
186 char *line;
187 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
188 if (outname[0] == '\0')
189 ofmt->filename (inname, outname, report_error);
190 ofile = NULL;
191 fprintf(stdout, "%s: %s", outname, inname);
192 while ( (line = preproc->getline()) )
193 nasm_free (line);
194 preproc->cleanup(0);
195 putc('\n', stdout);
197 break;
199 case op_preprocess:
201 char *line;
202 char *file_name = NULL;
203 long prior_linnum=0;
204 int lineinc=0;
206 if (*outname) {
207 ofile = fopen(outname, "w");
208 if (!ofile)
209 report_error (ERR_FATAL | ERR_NOFILE,
210 "unable to open output file `%s'", outname);
211 } else
212 ofile = NULL;
214 location.known = FALSE;
216 /* pass = 1; */
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);
224 if (altline) {
225 if (altline==1 && lineinc==1)
226 nasm_fputs("", ofile);
227 else {
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);
235 nasm_free (line);
237 nasm_free(file_name);
238 preproc->cleanup(0);
239 if (ofile)
240 fclose(ofile);
241 if (ofile && terminate_after_phase)
242 remove(outname);
244 break;
246 case op_normal:
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
253 * file.
255 ofmt->filename (inname, outname, report_error);
257 ofile = fopen(outname, "wb");
258 if (!ofile) {
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)
268 init_labels ();
270 ofmt->init (ofile, report_error, define_label, evaluate);
272 assemble_file (inname);
274 if (!terminate_after_phase) {
275 ofmt->cleanup (using_debug_info);
276 cleanup_labels ();
277 } else {
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.
282 * 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 const 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 'P':
368 case 'd':
369 case 'D':
370 case 'i':
371 case 'I':
372 case 'l':
373 case 'E':
374 case 'F':
375 if ( !(param = get_param (p, q, &advance)) )
376 break;
377 if (p[1]=='o') { /* output file */
378 strcpy (outname, param);
379 } else if (p[1]=='f') { /* output format */
380 ofmt = ofmt_find(param);
381 if (!ofmt) {
382 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
383 "unrecognised output format `%s' - "
384 "use -hf for a list",
385 param);
387 else
388 ofmt->current_dfmt = ofmt->debug_formats[0];
389 } else if (p[1]=='O') { /* Optimization level */
390 int opt;
391 opt = -99;
392 while (*param) {
393 if (isdigit(*param)) {
394 opt = atoi(param);
395 while(isdigit(*++param)) ;
396 if (opt<=0) optimizing = -1; /* 0.98 behaviour */
397 else if (opt==1) optimizing = 0; /* Two passes, 0.98.09 behavior */
398 else if (opt<=3) optimizing = opt*5; /* Multiple passes */
399 else optimizing = opt; /* Multiple passes */
400 } else {
401 if (*param == 'v' || *param == '+') {
402 ++param;
403 opt_verbose_info = TRUE;
404 opt = 0;
407 } /* while (*param) */
408 if (opt == -99) report_error(ERR_FATAL,
409 "command line optimization level must be 'v', 0..3 or <nn>");
410 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
411 pp_pre_include (param);
412 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
413 pp_pre_define (param);
414 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
415 pp_pre_undefine (param);
416 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
417 pp_include_path (param);
418 } else if (p[1]=='l') { /* listing file */
419 strcpy (listname, param);
420 } else if (p[1]=='E') { /* error messages file */
421 error_file = fopen(param, "w");
422 if ( !error_file ) {
423 error_file = stderr; /* Revert to default! */
424 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
425 "cannot open file `%s' for error messages",
426 param);
428 } else if (p[1] == 'F') { /* specify debug format */
429 ofmt->current_dfmt = dfmt_find(ofmt, param);
430 if (!ofmt->current_dfmt) {
431 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
432 "unrecognized debug format `%s' for"
433 " output format `%s'",
434 param, ofmt->shortname);
437 break;
438 case 'g':
439 using_debug_info = TRUE;
440 break;
441 case 'h':
442 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
443 "[-l listfile]\n"
444 " [options...] [--] filename\n"
445 " or nasm -r for version info (obsolete)\n"
446 " or nasm -v for version info (preferred)\n\n"
447 " -t Assemble in SciTech TASM compatible mode\n"
448 " -g Generate debug information in selected format.\n");
449 printf(" -e preprocess only (writes output to stdout by default)\n"
450 " -a don't preprocess (assemble only)\n"
451 " -M generate Makefile dependencies on stdout\n\n"
452 " -E<file> redirect error messages to file\n"
453 " -s redirect error messages to stdout\n\n"
454 " -F format select a debugging format\n\n"
455 " -I<path> adds a pathname to the include file path\n");
456 printf(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
457 " -P<file> pre-includes a file\n"
458 " -D<macro>[=<value>] pre-defines a macro\n"
459 " -U<macro> undefines a macro\n"
460 " -w+foo enables warnings about foo; -w-foo disables them\n"
461 "where foo can be:\n");
462 for (i=1; i<=ERR_WARN_MAX; i++)
463 printf(" %-23s %s (default %s)\n",
464 suppressed_names[i], suppressed_what[i],
465 suppressed[i] ? "off" : "on");
466 printf ("\nresponse files should contain command line parameters"
467 ", one per line.\n");
468 if (p[2] == 'f') {
469 printf("\nvalid output formats for -f are"
470 " (`*' denotes default):\n");
471 ofmt_list(ofmt, stdout);
473 else {
474 printf ("\nFor a list of valid output formats, use -hf.\n");
475 printf ("For a list of debug formats, use -f <form> -y.\n");
477 exit (0); /* never need usage message here */
478 break;
479 case 'y':
480 printf("\nvalid debug formats for '%s' output format are"
481 " ('*' denotes default):\n",
482 ofmt->shortname);
483 dfmt_list(ofmt, stdout);
484 exit(0);
485 break;
486 case 't':
487 tasm_compatible_mode = TRUE;
488 break;
489 case 'r':
490 case 'v':
492 const char *nasm_version_string =
493 "NASM version " NASM_VER " compiled on " __DATE__
494 #ifdef DEBUG
495 " with -DDEBUG"
496 #endif
498 puts(nasm_version_string);
499 exit (0); /* never need usage message here */
501 break;
502 case 'e': /* preprocess only */
503 operating_mode = op_preprocess;
504 break;
505 case 'a': /* assemble only - don't preprocess */
506 preproc = &no_pp;
507 break;
508 case 'w':
509 if (p[2] != '+' && p[2] != '-') {
510 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
511 "invalid option to `-w'");
512 } else {
513 for (i=1; i<=ERR_WARN_MAX; i++)
514 if (!nasm_stricmp(p+3, suppressed_names[i]))
515 break;
516 if (i <= ERR_WARN_MAX)
517 suppressed[i] = (p[2] == '-');
518 else
519 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
520 "invalid option to `-w'");
522 break;
523 case 'M':
524 operating_mode = op_depend;
525 break;
527 case '-':
529 int s;
531 if (p[2]==0) { /* -- => stop processing options */
532 stopoptions = 1;
533 break;
535 for(s=0; textopts[s].label; s++)
537 if(!nasm_stricmp(p+2, textopts[s].label))
539 break;
543 switch(s)
546 case OPT_PREFIX:
547 case OPT_POSTFIX:
549 if (!q)
551 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
552 "option `--%s' requires an argument",
553 p+2);
554 break;
556 else
558 advance = 1, param = q;
561 if(s == OPT_PREFIX)
563 strncpy(lprefix,param,PREFIX_MAX-1);
564 lprefix[PREFIX_MAX-1]=0;
565 break;
567 if(s == OPT_POSTFIX)
569 strncpy(lpostfix,param,POSTFIX_MAX-1);
570 lpostfix[POSTFIX_MAX-1]=0;
571 break;
573 break;
575 default:
577 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
578 "unrecognised option `--%s'",
579 p+2);
580 break;
583 break;
586 default:
587 if (!ofmt->setinfo(GI_SWITCH,&p))
588 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
589 "unrecognised option `-%c'",
590 p[1]);
591 break;
594 else
596 if (*inname) {
597 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
598 "more than one input file specified");
599 } else
600 strcpy(inname, p);
603 return advance;
606 #define ARG_BUF_DELTA 128
608 static void process_respfile (FILE *rfile)
610 char *buffer, *p, *q, *prevarg;
611 int bufsize, prevargsize;
613 bufsize = prevargsize = ARG_BUF_DELTA;
614 buffer = nasm_malloc(ARG_BUF_DELTA);
615 prevarg = nasm_malloc(ARG_BUF_DELTA);
616 prevarg[0] = '\0';
618 while (1) { /* Loop to handle all lines in file */
620 p = buffer;
621 while (1) { /* Loop to handle long lines */
622 q = fgets(p, bufsize-(p-buffer), rfile);
623 if (!q)
624 break;
625 p += strlen(p);
626 if (p > buffer && p[-1] == '\n')
627 break;
628 if (p-buffer > bufsize-10) {
629 int offset;
630 offset = p - buffer;
631 bufsize += ARG_BUF_DELTA;
632 buffer = nasm_realloc(buffer, bufsize);
633 p = buffer + offset;
637 if (!q && p == buffer) {
638 if (prevarg[0])
639 process_arg (prevarg, NULL);
640 nasm_free (buffer);
641 nasm_free (prevarg);
642 return;
646 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
647 * them are present at the end of the line.
649 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
651 while (p > buffer && isspace(p[-1]))
652 *--p = '\0';
654 p = buffer;
655 while (isspace(*p))
656 p++;
658 if (process_arg (prevarg, p))
659 *p = '\0';
661 if (strlen(p) > prevargsize-10) {
662 prevargsize += ARG_BUF_DELTA;
663 prevarg = nasm_realloc(prevarg, prevargsize);
665 strcpy (prevarg, p);
669 /* Function to process args from a string of args, rather than the
670 * argv array. Used by the environment variable and response file
671 * processing.
673 static void process_args (char *args) {
674 char *p, *q, *arg, *prevarg;
675 char separator = ' ';
677 p = args;
678 if (*p && *p != '-')
679 separator = *p++;
680 arg = NULL;
681 while (*p) {
682 q = p;
683 while (*p && *p != separator) p++;
684 while (*p == separator) *p++ = '\0';
685 prevarg = arg;
686 arg = q;
687 if (process_arg (prevarg, arg))
688 arg = NULL;
690 if (arg)
691 process_arg (arg, NULL);
694 static void parse_cmdline(int argc, char **argv)
696 FILE *rfile;
697 char *envreal, *envcopy=NULL, *p, *arg;
699 *inname = *outname = *listname = '\0';
702 * First, process the NASMENV environment variable.
704 envreal = getenv("NASMENV");
705 arg = NULL;
706 if (envreal) {
707 envcopy = nasm_strdup(envreal);
708 process_args(envcopy);
709 nasm_free (envcopy);
713 * Now process the actual command line.
715 while (--argc)
717 int i;
718 argv++;
719 if (argv[0][0] == '@') {
720 /* We have a response file, so process this as a set of
721 * arguments like the environment variable. This allows us
722 * to have multiple arguments on a single line, which is
723 * different to the -@resp file processing below for regular
724 * NASM.
726 char *str = malloc(2048);
727 FILE *f = fopen(&argv[0][1],"r");
728 if (!str) {
729 printf("out of memory");
730 exit(-1);
732 if (f) {
733 while (fgets(str,2048,f)) {
734 process_args(str);
736 fclose(f);
738 free(str);
739 argc--;
740 argv++;
742 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
743 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
744 if ((rfile = fopen(p, "r"))) {
745 process_respfile (rfile);
746 fclose(rfile);
747 } else
748 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
749 "unable to open response file `%s'", p);
751 } else
752 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
753 argv += i, argc -= i;
756 if (!*inname)
757 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
758 "no input file specified");
762 static void assemble_file (char *fname)
764 char * value, * p, * q, * special, * line, debugid[80];
765 insn output_ins;
766 int i, rn_error, validid;
767 long seg, offs;
768 struct tokenval tokval;
769 expr * e;
770 int pass, pass_max;
771 int pass_cnt = 0; /* count actual passes */
773 if (cmd_sb == 32 && cmd_cpu < IF_386)
774 report_error(ERR_FATAL, "command line: "
775 "32-bit segment size requires a higher cpu");
777 pass_max = (optimizing>0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
778 pass0 = !(optimizing>0); /* start at 1 if not optimizing */
779 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
780 int pass1, pass2;
781 ldfunc def_label;
783 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
784 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
785 /* pass0 seq is 0, 0, 0,..., 1, 2 */
787 def_label = pass > 1 ? redefine_label : define_label;
790 sb = cmd_sb; /* set 'bits' to command line default */
791 cpu = cmd_cpu;
792 if (pass0 == 2) {
793 if (*listname)
794 nasmlist.init(listname, report_error);
796 in_abs_seg = FALSE;
797 global_offset_changed = FALSE; /* set by redefine_label */
798 location.segment = ofmt->section(NULL, pass2, &sb);
799 if (pass > 1) {
800 saa_rewind (forwrefs);
801 forwref = saa_rstruct (forwrefs);
802 raa_free (offsets);
803 offsets = raa_init();
805 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
806 globallineno = 0;
807 if (pass == 1) location.known = TRUE;
808 location.offset = offs = GET_CURR_OFFS;
810 while ( (line = preproc->getline()) )
812 globallineno++;
814 /* here we parse our directives; this is not handled by the 'real'
815 * parser. */
816 if ( (i = getkw (line, &value)) )
818 switch (i) {
819 case 1: /* [SEGMENT n] */
820 seg = ofmt->section (value, pass2, &sb);
821 if (seg == NO_SEG) {
822 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
823 "segment name `%s' not recognised",
824 value);
825 } else {
826 in_abs_seg = FALSE;
827 location.segment = seg;
829 break;
830 case 2: /* [EXTERN label:special] */
831 if (*value == '$') value++; /* skip initial $ if present */
832 if (pass0 == 2) {
833 q = value;
834 while (*q && *q != ':')
835 q++;
836 if (*q == ':') {
837 *q++ = '\0';
838 ofmt->symdef(value, 0L, 0L, 3, q);
840 } else if (pass == 1) { /* pass == 1 */
841 q = value;
842 validid = TRUE;
843 if (!isidstart(*q))
844 validid = FALSE;
845 while (*q && *q != ':') {
846 if (!isidchar(*q))
847 validid = FALSE;
848 q++;
850 if (!validid) {
851 report_error (ERR_NONFATAL,
852 "identifier expected after EXTERN");
853 break;
855 if (*q == ':') {
856 *q++ = '\0';
857 special = q;
858 } else
859 special = NULL;
860 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
861 int temp = pass0;
862 pass0 = 1; /* fake pass 1 in labels.c */
863 declare_as_global (value, special, report_error);
864 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
865 ofmt, report_error);
866 pass0 = temp;
868 } /* else pass0 == 1 */
869 break;
870 case 3: /* [BITS bits] */
871 sb = get_bits(value);
872 break;
873 case 4: /* [GLOBAL symbol:special] */
874 if (*value == '$') value++; /* skip initial $ if present */
875 if (pass0 == 2) { /* pass 2 */
876 q = value;
877 while (*q && *q != ':')
878 q++;
879 if (*q == ':') {
880 *q++ = '\0';
881 ofmt->symdef(value, 0L, 0L, 3, q);
883 } else if (pass2 == 1) { /* pass == 1 */
884 q = value;
885 validid = TRUE;
886 if (!isidstart(*q))
887 validid = FALSE;
888 while (*q && *q != ':') {
889 if (!isidchar(*q))
890 validid = FALSE;
891 q++;
893 if (!validid) {
894 report_error (ERR_NONFATAL,
895 "identifier expected after GLOBAL");
896 break;
898 if (*q == ':') {
899 *q++ = '\0';
900 special = q;
901 } else
902 special = NULL;
903 declare_as_global (value, special, report_error);
904 } /* pass == 1 */
905 break;
906 case 5: /* [COMMON symbol size:special] */
907 if (*value == '$') value++; /* skip initial $ if present */
908 if (pass0 == 1) {
909 p = value;
910 validid = TRUE;
911 if (!isidstart(*p))
912 validid = FALSE;
913 while (*p && !isspace(*p)) {
914 if (!isidchar(*p))
915 validid = FALSE;
916 p++;
918 if (!validid) {
919 report_error (ERR_NONFATAL,
920 "identifier expected after COMMON");
921 break;
923 if (*p) {
924 long size;
926 while (*p && isspace(*p))
927 *p++ = '\0';
928 q = p;
929 while (*q && *q != ':')
930 q++;
931 if (*q == ':') {
932 *q++ = '\0';
933 special = q;
934 } else
935 special = NULL;
936 size = readnum (p, &rn_error);
937 if (rn_error)
938 report_error (ERR_NONFATAL, "invalid size specified"
939 " in COMMON declaration");
940 else
941 define_common (value, seg_alloc(), size,
942 special, ofmt, report_error);
943 } else
944 report_error (ERR_NONFATAL, "no size specified in"
945 " COMMON declaration");
946 } else if (pass0 == 2) { /* pass == 2 */
947 q = value;
948 while (*q && *q != ':') {
949 if (isspace(*q))
950 *q = '\0';
951 q++;
953 if (*q == ':') {
954 *q++ = '\0';
955 ofmt->symdef(value, 0L, 0L, 3, q);
958 break;
959 case 6: /* [ABSOLUTE address] */
960 stdscan_reset();
961 stdscan_bufptr = value;
962 tokval.t_type = TOKEN_INVALID;
963 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, report_error,
964 NULL);
965 if (e) {
966 if (!is_reloc(e))
967 report_error (pass0==1 ? ERR_NONFATAL : ERR_PANIC,
968 "cannot use non-relocatable expression as "
969 "ABSOLUTE address");
970 else {
971 abs_seg = reloc_seg(e);
972 abs_offset = reloc_value(e);
974 } else
975 if (pass==1) abs_offset = 0x100;/* don't go near zero in case of / */
976 else report_error (ERR_PANIC, "invalid ABSOLUTE address "
977 "in pass two");
978 in_abs_seg = TRUE;
979 location.segment = NO_SEG;
980 break;
981 case 7: /* DEBUG */
982 p = value;
983 q = debugid;
984 validid = TRUE;
985 if (!isidstart(*p))
986 validid = FALSE;
987 while (*p && !isspace(*p)) {
988 if (!isidchar(*p))
989 validid = FALSE;
990 *q++ = *p++;
992 *q++ = 0;
993 if (!validid) {
994 report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
995 "identifier expected after DEBUG");
996 break;
998 while (*p && isspace(*p)) p++;
999 if (pass==pass_max) ofmt->current_dfmt->debug_directive (debugid, p);
1000 break;
1001 case 8: /* [WARNING {+|-}warn-name] */
1002 if (pass1 == 1) {
1003 while (*value && isspace(*value))
1004 value++;
1006 if (*value == '+' || *value == '-') {
1007 validid = (*value == '-') ? TRUE : FALSE;
1008 value++;
1009 } else
1010 validid = FALSE;
1012 for (i=1; i<=ERR_WARN_MAX; i++)
1013 if (!nasm_stricmp(value, suppressed_names[i]))
1014 break;
1015 if (i <= ERR_WARN_MAX)
1016 suppressed[i] = validid;
1017 else
1018 report_error (ERR_NONFATAL, "invalid warning id in WARNING directive");
1020 break;
1021 case 9: /* cpu */
1022 cpu = get_cpu (value);
1023 break;
1024 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1025 while (*value && isspace(*value))
1026 value++;
1028 if (*value == '+') {
1029 user_nolist = 0;
1031 else {
1032 if (*value == '-') {
1033 user_nolist = 1;
1035 else {
1036 report_error (ERR_NONFATAL, "invalid parameter to \"list\" directive");
1039 break;
1040 default:
1041 if (!ofmt->directive (line+1, value, pass2))
1042 report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
1043 "unrecognised directive [%s]",
1044 line+1);
1047 else /* it isn't a directive */
1049 parse_line (pass1, line, &output_ins,
1050 report_error, evaluate,
1051 def_label);
1053 if (!(optimizing>0) && pass == 2) {
1054 if (forwref != NULL && globallineno == forwref->lineno) {
1055 output_ins.forw_ref = TRUE;
1056 do {
1057 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1058 forwref = saa_rstruct (forwrefs);
1059 } while (forwref != NULL && forwref->lineno == globallineno);
1060 } else
1061 output_ins.forw_ref = FALSE;
1065 if (!(optimizing>0) && output_ins.forw_ref)
1067 if (pass == 1) {
1068 for(i = 0; i < output_ins.operands; i++)
1070 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
1072 struct forwrefinfo *fwinf =
1073 (struct forwrefinfo *)saa_wstruct(forwrefs);
1074 fwinf->lineno = globallineno;
1075 fwinf->operand = i;
1078 } else { /* pass == 2 */
1080 * Hack to prevent phase error in the code
1081 * rol ax,x
1082 * x equ 1
1084 * If the second operand is a forward reference,
1085 * the UNITY property of the number 1 in that
1086 * operand is cancelled. Otherwise the above
1087 * sequence will cause a phase error.
1089 * This hack means that the above code will
1090 * generate 286+ code.
1092 * The forward reference will mean that the
1093 * operand will not have the UNITY property on
1094 * the first pass, so the pass behaviours will
1095 * be consistent.
1098 if (output_ins.operands >= 2 &&
1099 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1101 output_ins.oprs[1].type &= ~(ONENESS|BYTENESS);
1104 } /* pass == 2 */
1106 } /* forw_ref */
1109 if (output_ins.opcode == I_EQU) {
1110 if (pass1 == 1)
1113 * Special `..' EQUs get processed in pass two,
1114 * except `..@' macro-processor EQUs which are done
1115 * in the normal place.
1117 if (!output_ins.label)
1118 report_error (ERR_NONFATAL,
1119 "EQU not preceded by label");
1121 else if (output_ins.label[0] != '.' ||
1122 output_ins.label[1] != '.' ||
1123 output_ins.label[2] == '@')
1125 if (output_ins.operands == 1 &&
1126 (output_ins.oprs[0].type & IMMEDIATE) &&
1127 output_ins.oprs[0].wrt == NO_SEG)
1129 int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
1130 def_label (output_ins.label,
1131 output_ins.oprs[0].segment,
1132 output_ins.oprs[0].offset,
1133 NULL, FALSE, isext, ofmt, report_error);
1135 else if (output_ins.operands == 2 &&
1136 (output_ins.oprs[0].type & IMMEDIATE) &&
1137 (output_ins.oprs[0].type & COLON) &&
1138 output_ins.oprs[0].segment == NO_SEG &&
1139 output_ins.oprs[0].wrt == NO_SEG &&
1140 (output_ins.oprs[1].type & IMMEDIATE) &&
1141 output_ins.oprs[1].segment == NO_SEG &&
1142 output_ins.oprs[1].wrt == NO_SEG)
1144 def_label (output_ins.label,
1145 output_ins.oprs[0].offset | SEG_ABS,
1146 output_ins.oprs[1].offset,
1147 NULL, FALSE, FALSE, ofmt, report_error);
1149 else
1150 report_error(ERR_NONFATAL, "bad syntax for EQU");
1152 } else { /* pass == 2 */
1154 * Special `..' EQUs get processed here, except
1155 * `..@' macro processor EQUs which are done above.
1157 if (output_ins.label[0] == '.' &&
1158 output_ins.label[1] == '.' &&
1159 output_ins.label[2] != '@')
1161 if (output_ins.operands == 1 &&
1162 (output_ins.oprs[0].type & IMMEDIATE)) {
1163 define_label (output_ins.label,
1164 output_ins.oprs[0].segment,
1165 output_ins.oprs[0].offset,
1166 NULL, FALSE, FALSE, ofmt, report_error);
1168 else if (output_ins.operands == 2 &&
1169 (output_ins.oprs[0].type & IMMEDIATE) &&
1170 (output_ins.oprs[0].type & COLON) &&
1171 output_ins.oprs[0].segment == NO_SEG &&
1172 (output_ins.oprs[1].type & IMMEDIATE) &&
1173 output_ins.oprs[1].segment == NO_SEG)
1175 define_label (output_ins.label,
1176 output_ins.oprs[0].offset | SEG_ABS,
1177 output_ins.oprs[1].offset,
1178 NULL, FALSE, FALSE, ofmt, report_error);
1180 else
1181 report_error(ERR_NONFATAL, "bad syntax for EQU");
1183 } /* pass == 2 */
1184 } else { /* instruction isn't an EQU */
1186 if (pass1 == 1) {
1188 long l = insn_size (location.segment, offs, sb, cpu,
1189 &output_ins, report_error);
1191 /* if (using_debug_info) && output_ins.opcode != -1)*/
1192 if (using_debug_info) /* fbk 03/25/01 */
1195 /* this is done here so we can do debug type info */
1196 long typeinfo = TYS_ELEMENTS(output_ins.operands);
1197 switch (output_ins.opcode) {
1198 case I_RESB:
1199 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1200 break;
1201 case I_RESW:
1202 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1203 break;
1204 case I_RESD:
1205 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1206 break;
1207 case I_RESQ:
1208 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1209 break;
1210 case I_REST:
1211 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1212 break;
1213 case I_DB:
1214 typeinfo |= TY_BYTE;
1215 break;
1216 case I_DW:
1217 typeinfo |= TY_WORD;
1218 break;
1219 case I_DD:
1220 if (output_ins.eops_float)
1221 typeinfo |= TY_FLOAT;
1222 else
1223 typeinfo |= TY_DWORD;
1224 break;
1225 case I_DQ:
1226 typeinfo |= TY_QWORD;
1227 break;
1228 case I_DT:
1229 typeinfo |= TY_TBYTE;
1230 break;
1231 default:
1232 typeinfo = TY_LABEL;
1236 ofmt->current_dfmt->debug_typevalue(typeinfo);
1239 if (l != -1) {
1240 offs += l;
1241 SET_CURR_OFFS (offs);
1244 * else l == -1 => invalid instruction, which will be
1245 * flagged as an error on pass 2
1248 } else { /* pass == 2 */
1249 offs += assemble (location.segment, offs, sb, cpu,
1250 &output_ins, ofmt, report_error, &nasmlist);
1251 SET_CURR_OFFS (offs);
1254 } /* not an EQU */
1255 cleanup_insn (&output_ins);
1257 nasm_free (line);
1258 location.offset = offs = GET_CURR_OFFS;
1259 } /* end while (line = preproc->getline... */
1261 if (pass1==2 && global_offset_changed)
1262 report_error(ERR_NONFATAL, "phase error detected at end of assembly.");
1264 if (pass1 == 1) preproc->cleanup(1);
1266 if (pass1==1 && terminate_after_phase) {
1267 fclose(ofile);
1268 remove(outname);
1269 if (want_usage)
1270 usage();
1271 exit (1);
1273 pass_cnt++;
1274 if (pass>1 && !global_offset_changed) {
1275 pass0++;
1276 if (pass0==2) pass = pass_max - 1;
1277 } else if (!(optimizing>0)) pass0++;
1279 } /* for (pass=1; pass<=2; pass++) */
1281 preproc->cleanup(0);
1282 nasmlist.cleanup();
1283 #if 1
1284 if (optimizing>0 && opt_verbose_info) /* -On and -Ov switches */
1285 fprintf(stdout,
1286 "info:: assembly required 1+%d+1 passes\n", pass_cnt-2);
1287 #endif
1288 } /* exit from assemble_file (...) */
1291 static int getkw (char *buf, char **value)
1293 char *p, *q;
1295 /* allow leading spaces or tabs */
1296 while (*buf==' ' || *buf=='\t')
1297 buf++;
1299 if (*buf!='[')
1300 return 0;
1302 p = buf;
1304 while (*p && *p != ']') p++;
1306 if (!*p)
1307 return 0;
1309 q = p++;
1311 while (*p && *p != ';') {
1312 if (!isspace(*p))
1313 return 0;
1314 p++;
1316 q[1] = '\0';
1318 p = buf+1;
1319 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1320 buf++;
1321 if (*buf==']') {
1322 *buf = '\0';
1323 *value = buf;
1324 } else {
1325 *buf++ = '\0';
1326 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
1327 *value = buf;
1328 while (*buf!=']') buf++;
1329 *buf++ = '\0';
1331 #if 0
1332 for (q=p; *q; q++)
1333 *q = tolower(*q);
1334 #endif
1335 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1336 return 1;
1337 if (!nasm_stricmp(p, "extern"))
1338 return 2;
1339 if (!nasm_stricmp(p, "bits"))
1340 return 3;
1341 if (!nasm_stricmp(p, "global"))
1342 return 4;
1343 if (!nasm_stricmp(p, "common"))
1344 return 5;
1345 if (!nasm_stricmp(p, "absolute"))
1346 return 6;
1347 if (!nasm_stricmp(p, "debug"))
1348 return 7;
1349 if (!nasm_stricmp(p, "warning"))
1350 return 8;
1351 if (!nasm_stricmp(p, "cpu"))
1352 return 9;
1353 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1354 return 10;
1355 return -1;
1358 static void report_error (int severity, const char *fmt, ...)
1360 va_list ap;
1363 * See if it's a suppressed warning.
1365 if ((severity & ERR_MASK) == ERR_WARNING &&
1366 (severity & ERR_WARN_MASK) != 0 &&
1367 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1368 return; /* and bail out if so */
1371 * See if it's a pass-one only warning and we're not in pass one.
1373 if ((severity & ERR_PASS1) && pass0 == 2)
1374 return;
1376 if (severity & ERR_NOFILE)
1377 fputs ("nasm: ", error_file);
1378 else {
1379 char * currentfile = NULL;
1380 long lineno = 0;
1381 src_get (&lineno, &currentfile);
1382 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
1383 nasm_free (currentfile);
1386 switch (severity & ERR_MASK) {
1387 case ERR_WARNING:
1388 fputs ("warning: ", error_file); break;
1389 case ERR_NONFATAL:
1390 fputs ("error: ", error_file); break;
1391 case ERR_FATAL:
1392 fputs ("fatal: ", error_file); break;
1393 case ERR_PANIC:
1394 fputs ("panic: ", error_file); break;
1395 case ERR_DEBUG:
1396 fputs("debug: ", error_file); break;
1399 va_start (ap, fmt);
1400 vfprintf (error_file, fmt, ap);
1401 fputc ('\n', error_file);
1403 if (severity & ERR_USAGE)
1404 want_usage = TRUE;
1406 switch (severity & ERR_MASK) {
1407 case ERR_WARNING: case ERR_DEBUG:
1408 /* no further action, by definition */
1409 break;
1410 case ERR_NONFATAL:
1411 /* terminate_after_phase = TRUE; *//**//* hack enables listing(!) on errors */
1412 terminate_after_phase = TRUE;
1413 break;
1414 case ERR_FATAL:
1415 if (ofile) {
1416 fclose(ofile);
1417 remove(outname);
1419 if (want_usage)
1420 usage();
1421 exit(1); /* instantly die */
1422 break; /* placate silly compilers */
1423 case ERR_PANIC:
1424 fflush(NULL);
1425 /* abort(); */ /* halt, catch fire, and dump core */
1426 exit(3);
1427 break;
1431 static void usage(void)
1433 fputs("type `nasm -h' for help\n", error_file);
1436 static void register_output_formats(void)
1438 ofmt = ofmt_register (report_error);
1441 #define BUF_DELTA 512
1443 static FILE *no_pp_fp;
1444 static efunc no_pp_err;
1445 static ListGen *no_pp_list;
1446 static long no_pp_lineinc;
1448 static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
1449 ListGen *listgen)
1451 src_set_fname(nasm_strdup(file));
1452 src_set_linnum(0);
1453 no_pp_lineinc = 1;
1454 no_pp_err = error;
1455 no_pp_fp = fopen(file, "r");
1456 if (!no_pp_fp)
1457 no_pp_err (ERR_FATAL | ERR_NOFILE,
1458 "unable to open input file `%s'", file);
1459 no_pp_list = listgen;
1460 (void) pass; /* placate compilers */
1461 (void) eval; /* placate compilers */
1464 static char *no_pp_getline (void)
1466 char *buffer, *p, *q;
1467 int bufsize;
1469 bufsize = BUF_DELTA;
1470 buffer = nasm_malloc(BUF_DELTA);
1471 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1473 while (1) { /* Loop to handle %line */
1475 p = buffer;
1476 while (1) { /* Loop to handle long lines */
1477 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1478 if (!q)
1479 break;
1480 p += strlen(p);
1481 if (p > buffer && p[-1] == '\n')
1482 break;
1483 if (p-buffer > bufsize-10) {
1484 int offset;
1485 offset = p - buffer;
1486 bufsize += BUF_DELTA;
1487 buffer = nasm_realloc(buffer, bufsize);
1488 p = buffer + offset;
1492 if (!q && p == buffer) {
1493 nasm_free (buffer);
1494 return NULL;
1498 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1499 * them are present at the end of the line.
1501 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1503 if (!strncmp(buffer, "%line", 5)) {
1504 long ln;
1505 int li;
1506 char *nm = nasm_malloc(strlen(buffer));
1507 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1508 nasm_free( src_set_fname(nm) );
1509 src_set_linnum(ln);
1510 no_pp_lineinc = li;
1511 continue;
1513 nasm_free(nm);
1515 break;
1518 no_pp_list->line (LIST_READ, buffer);
1520 return buffer;
1523 static void no_pp_cleanup (int pass)
1525 fclose(no_pp_fp);
1528 static unsigned long get_cpu (char *value)
1531 if (!strcmp(value, "8086")) return IF_8086;
1532 if (!strcmp(value, "186")) return IF_186;
1533 if (!strcmp(value, "286")) return IF_286;
1534 if (!strcmp(value, "386")) return IF_386;
1535 if (!strcmp(value, "486")) return IF_486;
1536 if (!strcmp(value, "586") ||
1537 !nasm_stricmp(value, "pentium") ) return IF_PENT;
1538 if (!strcmp(value, "686") ||
1539 !nasm_stricmp(value, "ppro") ||
1540 !nasm_stricmp(value, "pentiumpro") ||
1541 !nasm_stricmp(value, "p2") ) return IF_P6;
1542 if (!nasm_stricmp(value, "p3") ||
1543 !nasm_stricmp(value, "katmai") ) return IF_KATMAI;
1544 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1545 !nasm_stricmp(value, "willamette") ) return IF_WILLAMETTE;
1546 if (!nasm_stricmp(value, "ia64") ||
1547 !nasm_stricmp(value, "ia-64") ||
1548 !nasm_stricmp(value, "itanium") ||
1549 !nasm_stricmp(value, "itanic") ||
1550 !nasm_stricmp(value, "merced") ) return IF_IA64;
1552 report_error (pass0<2 ? ERR_NONFATAL : ERR_FATAL, "unknown 'cpu' type");
1554 return IF_PLEVEL; /* the maximum level */
1558 static int get_bits (char *value)
1560 int i;
1562 if ((i = atoi(value)) == 16) return i; /* set for a 16-bit segment */
1563 else if (i == 32) {
1564 if (cpu < IF_386) {
1565 report_error(ERR_NONFATAL,
1566 "cannot specify 32-bit segment on processor below a 386");
1567 i = 16;
1569 } else {
1570 report_error(pass0<2 ? ERR_NONFATAL : ERR_FATAL,
1571 "`%s' is not a valid segment size; must be 16 or 32",
1572 value);
1573 i = 16;
1575 return i;
1578 /* end of nasm.c */