NASM 0.98bf
[nasm/avx512.git] / nasm.c
blob843164640e5e0913185eca8927718c7ad5ca2ad3
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 "preproc.h"
18 #include "parser.h"
19 #include "eval.h"
20 #include "assemble.h"
21 #include "labels.h"
22 #include "outform.h"
23 #include "listing.h"
25 struct forwrefinfo { /* info held on forward refs. */
26 int lineno;
27 int operand;
30 static void report_error (int, char *, ...);
31 static void parse_cmdline (int, char **);
32 static void assemble_file (char *);
33 static int getkw (char *buf, char **value);
34 static void register_output_formats(void);
35 static void usage(void);
37 static int using_debug_info;
39 static char inname[FILENAME_MAX];
40 static char outname[FILENAME_MAX];
41 static char listname[FILENAME_MAX];
42 static int globallineno; /* for forward-reference tracking */
43 static int pass;
44 static struct ofmt *ofmt = NULL;
46 static FILE *error_file; /* Where to write error messages */
48 static FILE *ofile = NULL;
49 static int sb = 16; /* by default */
51 static loc_t location;
52 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
53 static long abs_seg;
55 static struct RAA *offsets;
56 static long abs_offset;
58 static struct SAA *forwrefs; /* keep track of forward references */
59 static struct forwrefinfo *forwref;
61 static Preproc *preproc;
62 enum op_type {
63 op_normal, /* Preprocess and assemble */
64 op_preprocess, /* Preprocess only */
65 op_depend /* Generate dependencies */
67 static enum op_type operating_mode;
69 /* used by error function to report location */
72 * Which of the suppressible warnings are suppressed. Entry zero
73 * doesn't do anything. Initial defaults are given here.
75 static char suppressed[1+ERR_WARN_MAX] = {
76 0, TRUE, TRUE, FALSE
80 * The option names for the suppressible warnings. As before, entry
81 * zero does nothing.
83 static char *suppressed_names[1+ERR_WARN_MAX] = {
84 NULL, "macro-params", "orphan-labels", "number-overflow"
88 * The explanations for the suppressible warnings. As before, entry
89 * zero does nothing.
91 static char *suppressed_what[1+ERR_WARN_MAX] = {
92 NULL, "macro calls with wrong no. of params",
93 "labels alone on lines without trailing `:'",
94 "numeric constants greater than 0xFFFFFFFF"
98 * This is a null preprocessor which just copies lines from input
99 * to output. It's used when someone explicitly requests that NASM
100 * not preprocess their source file.
103 static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
104 static char *no_pp_getline (void);
105 static void no_pp_cleanup (void);
106 static Preproc no_pp = {
107 no_pp_reset,
108 no_pp_getline,
109 no_pp_cleanup
113 * get/set current offset...
115 #define get_curr_ofs (in_abs_seg?abs_offset:\
116 raa_read(offsets,location.segment))
117 #define set_curr_ofs(x) (in_abs_seg?(void)(abs_offset=(x)):\
118 (void)(offsets=raa_write(offsets,location.segment,(x))))
120 static int want_usage;
121 static int terminate_after_phase;
123 static void nasm_fputs(char *line, FILE *ofile)
125 if (ofile) {
126 fputs(line, ofile);
127 fputc('\n', ofile);
128 } else
129 puts(line);
132 int main(int argc, char **argv)
134 want_usage = terminate_after_phase = FALSE;
136 nasm_set_malloc_error (report_error);
137 offsets = raa_init();
138 forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
140 preproc = &nasmpp;
141 operating_mode = op_normal;
143 error_file = stderr;
145 seg_init();
147 register_output_formats();
149 parse_cmdline(argc, argv);
151 if (terminate_after_phase)
153 if (want_usage)
154 usage();
155 return 1;
158 if (ofmt->stdmac)
159 pp_extra_stdmac (ofmt->stdmac);
160 parser_global_info (ofmt, &location);
161 eval_global_info (ofmt, lookup_label, &location);
163 switch ( operating_mode ) {
164 case op_depend:
166 char *line;
167 preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
168 if (outname[0] == '\0')
169 ofmt->filename (inname, outname, report_error);
170 ofile = NULL;
171 printf("%s: %s", outname, inname);
172 while ( (line = preproc->getline()) != NULL )
173 nasm_free (line);
174 preproc->cleanup();
175 putc('\n', stdout);
177 break;
179 case op_preprocess:
181 char *line;
182 char *file_name = NULL;
183 long prior_linnum=0;
184 int lineinc=0;
186 if (*outname) {
187 ofile = fopen(outname, "w");
188 if (!ofile)
189 report_error (ERR_FATAL | ERR_NOFILE,
190 "unable to open output file `%s'", outname);
191 } else
192 ofile = NULL;
194 location.known = FALSE;
196 preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
197 while ( (line = preproc->getline()) != NULL ) {
199 * We generate %line directives if needed for later programs
201 long linnum = prior_linnum += lineinc;
202 int altline = src_get(&linnum, &file_name);
203 if (altline) {
204 if (altline==1 && lineinc==1)
205 nasm_fputs("", ofile);
206 else {
207 lineinc = (altline != -1 || lineinc!=1);
208 fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
209 linnum, lineinc, file_name);
211 prior_linnum = linnum;
213 nasm_fputs(line, ofile);
214 nasm_free (line);
216 nasm_free(file_name);
217 preproc->cleanup();
218 if (ofile)
219 fclose(ofile);
220 if (ofile && terminate_after_phase)
221 remove(outname);
223 break;
225 case op_normal:
228 * We must call ofmt->filename _anyway_, even if the user
229 * has specified their own output file, because some
230 * formats (eg OBJ and COFF) use ofmt->filename to find out
231 * the name of the input file and then put that inside the
232 * file.
234 ofmt->filename (inname, outname, report_error);
236 ofile = fopen(outname, "wb");
237 if (!ofile) {
238 report_error (ERR_FATAL | ERR_NOFILE,
239 "unable to open output file `%s'", outname);
243 * We must call init_labels() before ofmt->init() since
244 * some object formats will want to define labels in their
245 * init routines. (eg OS/2 defines the FLAT group)
247 init_labels ();
249 ofmt->init (ofile, report_error, define_label, evaluate);
251 assemble_file (inname);
253 if (!terminate_after_phase) {
254 ofmt->cleanup (using_debug_info);
255 cleanup_labels ();
257 else {
260 * We had an fclose on the output file here, but we
261 * actually do that in all the object file drivers as well,
262 * so we're leaving out the one here.
263 * fclose (ofile);
266 remove(outname);
267 if (listname[0])
268 remove(listname);
271 break;
274 if (want_usage)
275 usage();
277 raa_free (offsets);
278 saa_free (forwrefs);
279 eval_cleanup ();
280 nasmlib_cleanup ();
282 if (terminate_after_phase)
283 return 1;
284 else
285 return 0;
290 * Get a parameter for a command line option.
291 * First arg must be in the form of e.g. -f...
293 static char *get_param (char *p, char *q, int *advance)
295 *advance = 0;
296 if (p[2]) /* the parameter's in the option */
298 p += 2;
299 while (isspace(*p))
300 p++;
301 return p;
303 if (q && q[0])
305 *advance = 1;
306 return q;
308 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
309 "option `-%c' requires an argument",
310 p[1]);
311 return NULL;
314 int stopoptions = 0;
315 static int process_arg (char *p, char *q)
317 char *param;
318 int i, advance = 0;
320 if (!p || !p[0])
321 return 0;
323 if (p[0]=='-' && ! stopoptions)
325 switch (p[1]) {
326 case '-': /* -- => stop processing options */
327 stopoptions = 1;
328 break;
329 case 's':
330 error_file = stdout;
331 break;
332 case 'o': /* these parameters take values */
333 case 'f':
334 case 'p':
335 case 'd':
336 case 'D':
337 case 'U':
338 case 'i':
339 case 'l':
340 case 'E':
341 case 'F':
342 if ( !(param = get_param (p, q, &advance)) )
343 break;
344 if (p[1]=='o') { /* output file */
345 strcpy (outname, param);
346 } else if (p[1]=='f') { /* output format */
347 ofmt = ofmt_find(param);
348 if (!ofmt) {
349 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
350 "unrecognised output format `%s' - "
351 "use -hf for a list",
352 param);
354 else
355 ofmt->current_dfmt = ofmt->debug_formats[0];
356 } else if (p[1]=='P' || p[1]=='p') { /* pre-include */
357 pp_pre_include (param);
358 } else if (p[1]=='D' || p[1]=='d') { /* pre-define */
359 pp_pre_define (param);
360 } else if (p[1]=='U' || p[1]=='u') { /* un-define */
361 pp_pre_undefine (param);
362 } else if (p[1]=='I' || p[1]=='i') { /* include search path */
363 pp_include_path (param);
364 } else if (p[1]=='l') { /* listing file */
365 strcpy (listname, param);
366 } else if (p[1]=='E') { /* error messages file */
367 error_file = fopen(param, "wt");
368 if ( !error_file ) {
369 error_file = stderr; /* Revert to default! */
370 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
371 "cannot open file `%s' for error messages",
372 param);
374 } else if (p[1] == 'F') { /* specify debug format */
375 ofmt->current_dfmt = dfmt_find(ofmt, param);
376 if (!ofmt->current_dfmt) {
377 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
378 "unrecognized debug format `%s' for"
379 " output format `%s'",
380 param, ofmt->shortname);
383 break;
384 case 'g':
385 using_debug_info = TRUE;
386 break;
387 case 'h':
388 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
389 "[-l listfile]\n"
390 " [options...] [--] filename\n"
391 " or nasm -r for version info\n\n"
392 " -e preprocess only (writes output to stdout by default)\n"
393 " -a don't preprocess (assemble only)\n"
394 " -M generate Makefile dependencies on stdout\n\n"
395 " -E<file> redirect error messages to file\n"
396 " -s redirect error messages to stdout\n\n"
397 " -g enable debug info\n"
398 " -F format select a debugging format\n\n"
399 " -I<path> adds a pathname to the include file path\n"
400 " -P<file> pre-includes a file\n"
401 " -D<macro>[=<value>] pre-defines a macro\n"
402 " -U<macro> undefines a macro\n"
403 " -w+foo enables warnings about foo; -w-foo disables them\n"
404 "where foo can be:\n");
405 for (i=1; i<=ERR_WARN_MAX; i++)
406 printf(" %-16s%s (default %s)\n",
407 suppressed_names[i], suppressed_what[i],
408 suppressed[i] ? "off" : "on");
409 printf ("\nresponse files should contain command line parameters"
410 ", one per line.\n");
411 if (p[2] == 'f') {
412 printf("\nvalid output formats for -f are"
413 " (`*' denotes default):\n");
414 ofmt_list(ofmt, stdout);
416 else {
417 printf ("\nFor a list of valid output formats, use -hf.\n");
418 printf ("For a list of debug formats, use -f <form> -y.\n");
420 exit (0); /* never need usage message here */
421 break;
422 case 'y':
423 printf("\nvalid debug formats for '%s' output format are"
424 " ('*' denotes default):\n",
425 ofmt->shortname);
426 dfmt_list(ofmt, stdout);
427 exit(0);
428 break;
429 case 'r':
430 printf("NASM version %s\n", NASM_VER);
431 #ifdef DEBUG
432 printf("Compiled with -DDEBUG on " __DATE__ "\n");
433 #endif
434 exit (0); /* never need usage message here */
435 break;
436 case 'e': /* preprocess only */
437 operating_mode = op_preprocess;
438 break;
439 case 'a': /* assemble only - don't preprocess */
440 preproc = &no_pp;
441 break;
442 case 'w':
443 if (p[2] != '+' && p[2] != '-') {
444 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
445 "invalid option to `-w'");
446 } else {
447 for (i=1; i<=ERR_WARN_MAX; i++)
448 if (!nasm_stricmp(p+3, suppressed_names[i]))
449 break;
450 if (i <= ERR_WARN_MAX)
451 suppressed[i] = (p[2] == '-');
452 else
453 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
454 "invalid option to `-w'");
456 break;
457 case 'M':
458 operating_mode = op_depend;
459 break;
460 default:
461 if (!ofmt->setinfo(GI_SWITCH,&p))
462 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
463 "unrecognised option `-%c'",
464 p[1]);
465 break;
468 else
470 if (*inname) {
471 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
472 "more than one input file specified");
473 } else
474 strcpy(inname, p);
477 return advance;
480 #define ARG_BUF_DELTA 128
482 static void process_respfile (FILE *rfile)
484 char *buffer, *p, *q, *prevarg;
485 int bufsize, prevargsize;
487 bufsize = prevargsize = ARG_BUF_DELTA;
488 buffer = nasm_malloc(ARG_BUF_DELTA);
489 prevarg = nasm_malloc(ARG_BUF_DELTA);
490 prevarg[0] = '\0';
492 while (1) { /* Loop to handle all lines in file */
494 p = buffer;
495 while (1) { /* Loop to handle long lines */
496 q = fgets(p, bufsize-(p-buffer), rfile);
497 if (!q)
498 break;
499 p += strlen(p);
500 if (p > buffer && p[-1] == '\n')
501 break;
502 if (p-buffer > bufsize-10) {
503 int offset;
504 offset = p - buffer;
505 bufsize += ARG_BUF_DELTA;
506 buffer = nasm_realloc(buffer, bufsize);
507 p = buffer + offset;
511 if (!q && p == buffer) {
512 if (prevarg[0])
513 process_arg (prevarg, NULL);
514 nasm_free (buffer);
515 nasm_free (prevarg);
516 return;
520 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
521 * them are present at the end of the line.
523 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
525 while (p > buffer && isspace(p[-1]))
526 *--p = '\0';
528 p = buffer;
529 while (isspace(*p))
530 p++;
532 if (process_arg (prevarg, p))
533 *p = '\0';
535 if ((int)strlen(p) > prevargsize-10) {
536 prevargsize += ARG_BUF_DELTA;
537 prevarg = nasm_realloc(prevarg, prevargsize);
539 strcpy (prevarg, p);
543 static void parse_cmdline(int argc, char **argv)
545 FILE *rfile;
546 char *envreal, *envcopy, *p, *q, *arg, *prevarg;
547 char separator = ' ';
549 *inname = *outname = *listname = '\0';
552 * First, process the NASM environment variable.
554 envreal = getenv("NASM");
555 arg = NULL;
556 if (envreal) {
557 envcopy = nasm_strdup(envreal);
558 p = envcopy;
559 if (*p && *p != '-')
560 separator = *p++;
561 while (*p) {
562 q = p;
563 while (*p && *p != separator) p++;
564 while (*p == separator) *p++ = '\0';
565 prevarg = arg;
566 arg = q;
567 if (process_arg (prevarg, arg))
568 arg = NULL;
570 if (arg)
571 process_arg (arg, NULL);
572 nasm_free (envcopy);
576 * Now process the actual command line.
578 while (--argc)
580 int i;
581 argv++;
582 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
583 if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
584 if ((rfile = fopen(p, "r"))) {
585 process_respfile (rfile);
586 fclose(rfile);
587 } else
588 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
589 "unable to open response file `%s'", p);
591 } else
592 i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
593 argv += i, argc -= i;
596 if (!*inname)
597 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
598 "no input file specified");
601 static void assemble_file (char *fname)
603 char * value, * p, * q, * special, * line, debugid[80];
604 insn output_ins;
605 int i, rn_error, validid;
606 long seg, offs;
607 struct tokenval tokval;
608 expr * e;
611 * pass one
613 pass = 1;
614 in_abs_seg = FALSE;
615 location.segment = ofmt->section(NULL, pass, &sb);
616 preproc->reset(fname, 1, report_error, evaluate, &nasmlist);
617 globallineno = 0;
618 location.known = TRUE;
619 location.offset = offs = get_curr_ofs;
621 while ( (line = preproc->getline()) != NULL )
623 globallineno++;
625 /* here we parse our directives; this is not handled by the 'real'
626 * parser. */
627 if ( (i = getkw (line, &value)) )
629 switch (i) {
630 case 1: /* [SEGMENT n] */
631 seg = ofmt->section (value, pass, &sb);
632 if (seg == NO_SEG) {
633 report_error (ERR_NONFATAL,
634 "segment name `%s' not recognised",
635 value);
636 } else {
637 in_abs_seg = FALSE;
638 location.segment = seg;
640 break;
641 case 2: /* [EXTERN label:special] */
642 if (*value == '$')
643 value++; /* skip initial $ if present */
644 q = value;
645 validid = TRUE;
646 if (!isidstart(*q))
647 validid = FALSE;
648 while (*q && *q != ':') {
649 if (!isidchar(*q))
650 validid = FALSE;
651 q++;
653 if (!validid) {
654 report_error (ERR_NONFATAL,
655 "identifier expected after EXTERN");
656 break;
658 if (*q == ':') {
659 *q++ = '\0';
660 special = q;
661 } else
662 special = NULL;
663 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
664 declare_as_global (value, special, report_error);
665 define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
666 ofmt, report_error);
668 break;
669 case 3: /* [BITS bits] */
670 switch (atoi(value)) {
671 case 16:
672 case 32:
673 sb = atoi(value);
674 break;
675 default:
676 report_error(ERR_NONFATAL,
677 "`%s' is not a valid argument to [BITS]",
678 value);
679 break;
681 break;
682 case 4: /* [GLOBAL symbol:special] */
683 if (*value == '$')
684 value++; /* skip initial $ if present */
685 q = value;
686 validid = TRUE;
687 if (!isidstart(*q))
688 validid = FALSE;
689 while (*q && *q != ':') {
690 if (!isidchar(*q))
691 validid = FALSE;
692 q++;
694 if (!validid) {
695 report_error (ERR_NONFATAL,
696 "identifier expected after GLOBAL");
697 break;
699 if (*q == ':') {
700 *q++ = '\0';
701 special = q;
702 } else
703 special = NULL;
704 declare_as_global (value, special, report_error);
705 break;
706 case 5: /* [COMMON symbol size:special] */
707 p = value;
708 validid = TRUE;
709 if (!isidstart(*p))
710 validid = FALSE;
711 while (*p && !isspace(*p)) {
712 if (!isidchar(*p))
713 validid = FALSE;
714 p++;
716 if (!validid) {
717 report_error (ERR_NONFATAL,
718 "identifier expected after COMMON");
719 break;
721 if (*p) {
722 long size;
724 while (*p && isspace(*p))
725 *p++ = '\0';
726 q = p;
727 while (*q && *q != ':')
728 q++;
729 if (*q == ':') {
730 *q++ = '\0';
731 special = q;
732 } else
733 special = NULL;
734 size = readnum (p, &rn_error);
735 if (rn_error)
736 report_error (ERR_NONFATAL, "invalid size specified"
737 " in COMMON declaration");
738 else
739 define_common (value, seg_alloc(), size,
740 special, ofmt, report_error);
741 } else
742 report_error (ERR_NONFATAL, "no size specified in"
743 " COMMON declaration");
744 break;
745 case 6: /* [ABSOLUTE address] */
746 stdscan_reset();
747 stdscan_bufptr = value;
748 tokval.t_type = TOKEN_INVALID;
749 e = evaluate(stdscan, NULL, &tokval, NULL, 1, report_error,
750 NULL);
751 if (e) {
752 if (!is_reloc(e))
753 report_error (ERR_NONFATAL, "cannot use non-"
754 "relocatable expression as ABSOLUTE"
755 " address");
756 else {
757 abs_seg = reloc_seg(e);
758 abs_offset = reloc_value(e);
760 } else
761 abs_offset = 0x100;/* don't go near zero in case of / */
762 in_abs_seg = TRUE;
763 location.segment = abs_seg;
764 break;
765 case 7:
766 p = value;
767 validid = TRUE;
768 if (!isidstart(*p))
769 validid = FALSE;
770 while (*p && !isspace(*p)) {
771 if (!isidchar(*p))
772 validid = FALSE;
773 p++;
775 if (!validid) {
776 report_error (ERR_NONFATAL,
777 "identifier expected after DEBUG");
778 break;
780 while (*p && isspace(*p)) p++;
781 break;
782 default:
783 if (!ofmt->directive (line+1, value, 1))
784 report_error (ERR_NONFATAL, "unrecognised directive [%s]",
785 line+1);
786 break;
789 else /* it isn't a directive */
791 parse_line (1, line, &output_ins,
792 report_error, evaluate, define_label);
794 if (output_ins.forw_ref)
796 for(i = 0; i < output_ins.operands; i++)
798 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
800 struct forwrefinfo *fwinf =
801 (struct forwrefinfo *)saa_wstruct(forwrefs);
802 fwinf->lineno = globallineno;
803 fwinf->operand = i;
808 if (output_ins.opcode == I_EQU)
811 * Special `..' EQUs get processed in pass two,
812 * except `..@' macro-processor EQUs which are done
813 * in the normal place.
815 if (!output_ins.label)
816 report_error (ERR_NONFATAL,
817 "EQU not preceded by label");
819 else if (output_ins.label[0] != '.' ||
820 output_ins.label[1] != '.' ||
821 output_ins.label[2] == '@')
823 if (output_ins.operands == 1 &&
824 (output_ins.oprs[0].type & IMMEDIATE) &&
825 output_ins.oprs[0].wrt == NO_SEG)
827 int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
828 define_label (output_ins.label,
829 output_ins.oprs[0].segment,
830 output_ins.oprs[0].offset,
831 NULL, FALSE, isext, ofmt, report_error);
833 else if (output_ins.operands == 2 &&
834 (output_ins.oprs[0].type & IMMEDIATE) &&
835 (output_ins.oprs[0].type & COLON) &&
836 output_ins.oprs[0].segment == NO_SEG &&
837 output_ins.oprs[0].wrt == NO_SEG &&
838 (output_ins.oprs[1].type & IMMEDIATE) &&
839 output_ins.oprs[1].segment == NO_SEG &&
840 output_ins.oprs[1].wrt == NO_SEG)
842 define_label (output_ins.label,
843 output_ins.oprs[0].offset | SEG_ABS,
844 output_ins.oprs[1].offset,
845 NULL, FALSE, FALSE, ofmt, report_error);
847 else
848 report_error(ERR_NONFATAL, "bad syntax for EQU");
851 else /* instruction isn't an EQU */
853 long l = insn_size (location.segment, offs, sb,
854 &output_ins, report_error);
855 if (using_debug_info && output_ins.opcode != -1) {
856 /* this is done here so we can do debug type info */
857 long typeinfo = TYS_ELEMENTS(output_ins.operands);
858 switch (output_ins.opcode) {
859 case I_RESB:
860 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
861 break;
862 case I_RESW:
863 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
864 break;
865 case I_RESD:
866 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
867 break;
868 case I_RESQ:
869 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
870 break;
871 case I_REST:
872 typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
873 break;
874 case I_DB:
875 typeinfo |= TY_BYTE;
876 break;
877 case I_DW:
878 typeinfo |= TY_WORD;
879 break;
880 case I_DD:
881 if (output_ins.eops_float)
882 typeinfo |= TY_FLOAT;
883 else
884 typeinfo |= TY_DWORD;
885 break;
886 case I_DQ:
887 typeinfo |= TY_QWORD;
888 break;
889 case I_DT:
890 typeinfo |= TY_TBYTE;
891 break;
892 default:
893 typeinfo = TY_LABEL;
895 ofmt->current_dfmt->debug_typevalue(typeinfo);
897 if (l != -1) {
898 offs += l;
899 set_curr_ofs (offs);
902 * else l == -1 => invalid instruction, which will be
903 * flagged as an error on pass 2
906 cleanup_insn (&output_ins);
908 nasm_free (line);
909 location.offset = offs = get_curr_ofs;
912 preproc->cleanup();
914 if (terminate_after_phase) {
915 fclose(ofile);
916 remove(outname);
917 if (want_usage)
918 usage();
919 exit (1);
923 * pass two
926 pass = 2;
927 saa_rewind (forwrefs);
928 if (*listname)
929 nasmlist.init(listname, report_error);
930 forwref = saa_rstruct (forwrefs);
931 in_abs_seg = FALSE;
932 location.segment = ofmt->section(NULL, pass, &sb);
933 raa_free (offsets);
934 offsets = raa_init();
935 preproc->reset(fname, 2, report_error, evaluate, &nasmlist);
936 globallineno = 0;
937 location.offset = offs = get_curr_ofs;
939 while ( (line = preproc->getline()) != NULL )
941 globallineno++;
943 /* here we parse our directives; this is not handled by
944 * the 'real' parser. */
945 if ( (i = getkw (line, &value)) ) {
946 switch (i) {
947 case 1: /* [SEGMENT n] */
948 seg = ofmt->section (value, pass, &sb);
949 if (seg == NO_SEG) {
950 report_error (ERR_PANIC,
951 "invalid segment name on pass two");
952 } else
953 in_abs_seg = FALSE;
954 location.segment = seg;
955 break;
956 case 2: /* [EXTERN label] */
957 q = value;
958 while (*q && *q != ':')
959 q++;
960 if (*q == ':') {
961 *q++ = '\0';
962 ofmt->symdef(value, 0L, 0L, 3, q);
964 break;
965 case 3: /* [BITS bits] */
966 switch (atoi(value)) {
967 case 16:
968 case 32:
969 sb = atoi(value);
970 break;
971 default:
972 report_error(ERR_PANIC,
973 "invalid [BITS] value on pass two",
974 value);
975 break;
977 break;
978 case 4: /* [GLOBAL symbol] */
979 q = value;
980 while (*q && *q != ':')
981 q++;
982 if (*q == ':') {
983 *q++ = '\0';
984 ofmt->symdef(value, 0L, 0L, 3, q);
986 break;
987 case 5: /* [COMMON symbol size] */
988 q = value;
989 while (*q && *q != ':') {
990 if (isspace(*q))
991 *q = '\0';
992 q++;
994 if (*q == ':') {
995 *q++ = '\0';
996 ofmt->symdef(value, 0L, 0L, 3, q);
998 break;
999 case 6: /* [ABSOLUTE addr] */
1000 stdscan_reset();
1001 stdscan_bufptr = value;
1002 tokval.t_type = TOKEN_INVALID;
1003 e = evaluate(stdscan, NULL, &tokval, NULL, 2, report_error,
1004 NULL);
1005 if (e) {
1006 if (!is_reloc(e))
1007 report_error (ERR_PANIC, "non-reloc ABSOLUTE address"
1008 " in pass two");
1009 else {
1010 abs_seg = reloc_seg(e);
1011 abs_offset = reloc_value(e);
1013 } else
1014 report_error (ERR_PANIC, "invalid ABSOLUTE address "
1015 "in pass two");
1016 in_abs_seg = TRUE;
1017 location.segment = abs_seg;
1018 break;
1019 case 7:
1020 p = value;
1021 q = debugid;
1022 validid = TRUE;
1023 if (!isidstart(*p))
1024 validid = FALSE;
1025 while (*p && !isspace(*p)) {
1026 if (!isidchar(*p))
1027 validid = FALSE;
1028 *q++ = *p++;
1030 *q++ = 0;
1031 if (!validid) {
1032 report_error (ERR_PANIC,
1033 "identifier expected after DEBUG in pass 2");
1034 break;
1036 while (*p && isspace(*p))
1037 p++;
1038 ofmt->current_dfmt->debug_directive (debugid, p);
1039 break;
1040 default:
1041 if (!ofmt->directive (line+1, value, 2))
1042 report_error (ERR_PANIC, "invalid directive on pass two");
1043 break;
1046 else /* not a directive */
1048 parse_line (2, line, &output_ins,
1049 report_error, evaluate, redefine_label);
1050 if (forwref != NULL && globallineno == forwref->lineno) {
1051 output_ins.forw_ref = TRUE;
1052 do {
1053 output_ins.oprs[forwref->operand].opflags|= OPFLAG_FORWARD;
1054 forwref = saa_rstruct (forwrefs);
1055 } while (forwref != NULL && forwref->lineno == globallineno);
1056 } else
1057 output_ins.forw_ref = FALSE;
1060 * Hack to prevent phase error in the code
1061 * rol ax,x
1062 * x equ 1
1064 * If the second operand is a forward reference,
1065 * the UNITY property of the number 1 in that
1066 * operand is cancelled. Otherwise the above
1067 * sequence will cause a phase error.
1069 * This hack means that the above code will
1070 * generate 286+ code.
1072 * The forward reference will mean that the
1073 * operand will not have the UNITY property on
1074 * the first pass, so the pass behaviours will
1075 * be consistent.
1078 if (output_ins.forw_ref &&
1079 output_ins.operands >= 2 &&
1080 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1082 output_ins.oprs[1].type &= ~ONENESS;
1085 if (output_ins.opcode == I_EQU)
1088 * Special `..' EQUs get processed here, except
1089 * `..@' macro processor EQUs which are done above.
1091 if (output_ins.label[0] == '.' &&
1092 output_ins.label[1] == '.' &&
1093 output_ins.label[2] != '@')
1095 if (output_ins.operands == 1 &&
1096 (output_ins.oprs[0].type & IMMEDIATE)) {
1097 define_label (output_ins.label,
1098 output_ins.oprs[0].segment,
1099 output_ins.oprs[0].offset,
1100 NULL, FALSE, FALSE, ofmt, report_error);
1102 else if (output_ins.operands == 2 &&
1103 (output_ins.oprs[0].type & IMMEDIATE) &&
1104 (output_ins.oprs[0].type & COLON) &&
1105 output_ins.oprs[0].segment == NO_SEG &&
1106 (output_ins.oprs[1].type & IMMEDIATE) &&
1107 output_ins.oprs[1].segment == NO_SEG)
1109 define_label (output_ins.label,
1110 output_ins.oprs[0].offset | SEG_ABS,
1111 output_ins.oprs[1].offset,
1112 NULL, FALSE, FALSE, ofmt, report_error);
1114 else
1115 report_error(ERR_NONFATAL, "bad syntax for EQU");
1118 offs += assemble (location.segment, offs, sb,
1119 &output_ins, ofmt, report_error, &nasmlist);
1120 cleanup_insn (&output_ins);
1121 set_curr_ofs (offs);
1124 nasm_free (line);
1126 location.offset = offs = get_curr_ofs;
1129 preproc->cleanup();
1130 nasmlist.cleanup();
1133 static int getkw (char *buf, char **value)
1135 char *p, *q;
1137 if (*buf!='[')
1138 return 0;
1140 p = buf;
1142 while (*p && *p != ']') p++;
1144 if (!*p)
1145 return 0;
1147 q = p++;
1149 while (*p && *p != ';') {
1150 if (!isspace(*p))
1151 return 0;
1152 p++;
1154 q[1] = '\0';
1156 p = buf+1;
1157 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1158 buf++;
1159 if (*buf==']') {
1160 *buf = '\0';
1161 *value = buf;
1162 } else {
1163 *buf++ = '\0';
1164 while (isspace(*buf)) buf++; /* beppu - skip leading whitespace */
1165 *value = buf;
1166 while (*buf!=']') buf++;
1167 *buf++ = '\0';
1169 for (q=p; *q; q++)
1170 *q = tolower(*q);
1171 if (!strcmp(p, "segment") || !strcmp(p, "section"))
1172 return 1;
1173 if (!strcmp(p, "extern"))
1174 return 2;
1175 if (!strcmp(p, "bits"))
1176 return 3;
1177 if (!strcmp(p, "global"))
1178 return 4;
1179 if (!strcmp(p, "common"))
1180 return 5;
1181 if (!strcmp(p, "absolute"))
1182 return 6;
1183 if (!strcmp(p, "debug"))
1184 return 7;
1185 return -1;
1188 static void report_error (int severity, char *fmt, ...)
1190 va_list ap;
1193 * See if it's a suppressed warning.
1195 if ((severity & ERR_MASK) == ERR_WARNING &&
1196 (severity & ERR_WARN_MASK) != 0 &&
1197 suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1198 return; /* and bail out if so */
1201 * See if it's a pass-one only warning and we're not in pass one.
1203 if ((severity & ERR_PASS1) && pass != 1)
1204 return;
1206 if (severity & ERR_NOFILE)
1207 fputs ("nasm: ", error_file);
1208 else {
1209 char * currentfile = NULL;
1210 long lineno = 0;
1211 src_get (&lineno, &currentfile);
1212 fprintf (error_file, "%s:%ld: ", currentfile, lineno);
1213 nasm_free (currentfile);
1216 if ( (severity & ERR_MASK) == ERR_WARNING)
1217 fputs ("warning: ", error_file);
1218 else if ( (severity & ERR_MASK) == ERR_PANIC)
1219 fputs ("panic: ", error_file);
1221 va_start (ap, fmt);
1222 vfprintf (error_file, fmt, ap);
1223 fputc ('\n', error_file);
1225 if (severity & ERR_USAGE)
1226 want_usage = TRUE;
1228 switch (severity & ERR_MASK) {
1229 case ERR_WARNING:
1230 /* no further action, by definition */
1231 break;
1232 case ERR_NONFATAL:
1233 terminate_after_phase = TRUE;
1234 break;
1235 case ERR_FATAL:
1236 if (ofile) {
1237 fclose(ofile);
1238 remove(outname);
1240 if (want_usage)
1241 usage();
1242 exit(1); /* instantly die */
1243 break; /* placate silly compilers */
1244 case ERR_PANIC:
1245 fflush(NULL);
1246 abort(); /* halt, catch fire, and dump core */
1247 break;
1251 static void usage(void)
1253 fputs("type `nasm -h' for help\n", error_file);
1256 static void register_output_formats(void)
1258 ofmt = ofmt_register (report_error);
1261 #define BUF_DELTA 512
1263 static FILE *no_pp_fp;
1264 static efunc no_pp_err;
1265 static ListGen *no_pp_list;
1266 static long no_pp_lineinc;
1268 static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
1269 ListGen *listgen)
1271 src_set_fname(nasm_strdup(file));
1272 src_set_linnum(0);
1273 no_pp_lineinc = 1;
1274 no_pp_err = error;
1275 no_pp_fp = fopen(file, "r");
1276 if (!no_pp_fp)
1277 no_pp_err (ERR_FATAL | ERR_NOFILE,
1278 "unable to open input file `%s'", file);
1279 no_pp_list = listgen;
1280 (void) pass; /* placate compilers */
1281 (void) eval; /* placate compilers */
1284 static char *no_pp_getline (void)
1286 char *buffer, *p, *q;
1287 int bufsize;
1289 bufsize = BUF_DELTA;
1290 buffer = nasm_malloc(BUF_DELTA);
1291 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1293 while (1) { /* Loop to handle %line */
1295 p = buffer;
1296 while (1) { /* Loop to handle long lines */
1297 q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1298 if (!q)
1299 break;
1300 p += strlen(p);
1301 if (p > buffer && p[-1] == '\n')
1302 break;
1303 if (p-buffer > bufsize-10) {
1304 int offset;
1305 offset = p - buffer;
1306 bufsize += BUF_DELTA;
1307 buffer = nasm_realloc(buffer, bufsize);
1308 p = buffer + offset;
1312 if (!q && p == buffer) {
1313 nasm_free (buffer);
1314 return NULL;
1318 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1319 * them are present at the end of the line.
1321 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1323 if (!strncmp(buffer, "%line", 5)) {
1324 long ln;
1325 int li;
1326 char *nm = nasm_malloc(strlen(buffer));
1327 if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1328 nasm_free( src_set_fname(nm) );
1329 src_set_linnum(ln);
1330 no_pp_lineinc = li;
1331 continue;
1333 nasm_free(nm);
1335 break;
1338 no_pp_list->line (LIST_READ, buffer);
1340 return buffer;
1343 static void no_pp_cleanup (void)
1345 fclose(no_pp_fp);