Fix handling of XCHG in 64-bit mode
[nasm.git] / nasm.c
blob880d0a5a6f94dab7bed11800c7244c1cee1a9465
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 "compiler.h"
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
17 #include <limits.h>
19 #include "nasm.h"
20 #include "nasmlib.h"
21 #include "float.h"
22 #include "stdscan.h"
23 #include "insns.h"
24 #include "preproc.h"
25 #include "parser.h"
26 #include "eval.h"
27 #include "assemble.h"
28 #include "labels.h"
29 #include "outform.h"
30 #include "listing.h"
32 struct forwrefinfo { /* info held on forward refs. */
33 int lineno;
34 int operand;
37 static int get_bits(char *value);
38 static uint32_t get_cpu(char *cpu_str);
39 static void parse_cmdline(int, char **);
40 static void assemble_file(char *);
41 static void register_output_formats(void);
42 static void report_error_gnu(int severity, const char *fmt, ...);
43 static void report_error_vc(int severity, const char *fmt, ...);
44 static void report_error_common(int severity, const char *fmt,
45 va_list args);
46 static int is_suppressed_warning(int severity);
47 static void usage(void);
48 static efunc report_error;
50 static int using_debug_info, opt_verbose_info;
51 bool tasm_compatible_mode = false;
52 int pass0;
53 int maxbits = 0;
54 int globalrel = 0;
56 static char inname[FILENAME_MAX];
57 static char outname[FILENAME_MAX];
58 static char listname[FILENAME_MAX];
59 static char errname[FILENAME_MAX];
60 static int globallineno; /* for forward-reference tracking */
61 /* static int pass = 0; */
62 static struct ofmt *ofmt = NULL;
64 static FILE *error_file; /* Where to write error messages */
66 static FILE *ofile = NULL;
67 int optimizing = -1; /* number of optimization passes to take */
68 static int sb, cmd_sb = 16; /* by default */
69 static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
70 static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
71 bool global_offset_changed; /* referenced in labels.c */
73 static struct location location;
74 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
75 int32_t abs_seg; /* ABSOLUTE segment basis */
76 int32_t abs_offset; /* ABSOLUTE offset */
78 static struct RAA *offsets;
80 static struct SAA *forwrefs; /* keep track of forward references */
81 static const struct forwrefinfo *forwref;
83 static Preproc *preproc;
84 enum op_type {
85 op_normal, /* Preprocess and assemble */
86 op_preprocess, /* Preprocess only */
87 op_depend, /* Generate dependencies */
88 op_depend_missing_ok, /* Generate dependencies, missing OK */
90 static enum op_type operating_mode;
93 * Which of the suppressible warnings are suppressed. Entry zero
94 * doesn't do anything. Initial defaults are given here.
96 static bool suppressed[1 + ERR_WARN_MAX] = {
97 0, true, true, true, false, true, false, true, true, false
101 * The option names for the suppressible warnings. As before, entry
102 * zero does nothing.
104 static const char *suppressed_names[1 + ERR_WARN_MAX] = {
105 NULL, "macro-params", "macro-selfref", "orphan-labels",
106 "number-overflow", "gnu-elf-extensions", "float-overflow",
107 "float-denorm", "float-underflow", "float-toolong"
111 * The explanations for the suppressible warnings. As before, entry
112 * zero does nothing.
114 static const char *suppressed_what[1 + ERR_WARN_MAX] = {
115 NULL,
116 "macro calls with wrong no. of params",
117 "cyclic macro self-references",
118 "labels alone on lines without trailing `:'",
119 "numeric constants do not fit in 32 bits",
120 "using 8- or 16-bit relocation in ELF32, a GNU extension",
121 "floating point overflow",
122 "floating point denormal",
123 "floating point underflow",
124 "too many digits in floating-point number"
128 * This is a null preprocessor which just copies lines from input
129 * to output. It's used when someone explicitly requests that NASM
130 * not preprocess their source file.
133 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
134 static char *no_pp_getline(void);
135 static void no_pp_cleanup(int);
136 static Preproc no_pp = {
137 no_pp_reset,
138 no_pp_getline,
139 no_pp_cleanup
143 * get/set current offset...
145 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
146 raa_read(offsets,location.segment))
147 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
148 (void)(offsets=raa_write(offsets,location.segment,(x))))
150 static int want_usage;
151 static int terminate_after_phase;
152 int user_nolist = 0; /* fbk 9/2/00 */
154 static void nasm_fputs(const char *line, FILE * outfile)
156 if (outfile) {
157 fputs(line, outfile);
158 fputc('\n', outfile);
159 } else
160 puts(line);
163 int main(int argc, char **argv)
165 pass0 = 1;
166 want_usage = terminate_after_phase = false;
167 report_error = report_error_gnu;
169 error_file = stderr;
171 nasm_set_malloc_error(report_error);
172 offsets = raa_init();
173 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
175 preproc = &nasmpp;
176 operating_mode = op_normal;
178 seg_init();
180 register_output_formats();
182 parse_cmdline(argc, argv);
184 if (terminate_after_phase) {
185 if (want_usage)
186 usage();
187 return 1;
190 /* If debugging info is disabled, suppress any debug calls */
191 if (!using_debug_info)
192 ofmt->current_dfmt = &null_debug_form;
194 if (ofmt->stdmac)
195 pp_extra_stdmac(ofmt->stdmac);
196 parser_global_info(ofmt, &location);
197 eval_global_info(ofmt, lookup_label, &location);
199 /* define some macros dependent of command-line */
201 char temp[64];
202 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
203 ofmt->shortname);
204 pp_pre_define(temp);
207 switch (operating_mode) {
208 case op_depend_missing_ok:
209 pp_include_path(NULL); /* "assume generated" */
210 /* fall through */
211 case op_depend:
213 char *line;
214 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
215 if (outname[0] == '\0')
216 ofmt->filename(inname, outname, report_error);
217 ofile = NULL;
218 fprintf(stdout, "%s: %s", outname, inname);
219 while ((line = preproc->getline()))
220 nasm_free(line);
221 preproc->cleanup(0);
222 putc('\n', stdout);
224 break;
226 case op_preprocess:
228 char *line;
229 char *file_name = NULL;
230 int32_t prior_linnum = 0;
231 int lineinc = 0;
233 if (*outname) {
234 ofile = fopen(outname, "w");
235 if (!ofile)
236 report_error(ERR_FATAL | ERR_NOFILE,
237 "unable to open output file `%s'",
238 outname);
239 } else
240 ofile = NULL;
242 location.known = false;
244 /* pass = 1; */
245 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
246 while ((line = preproc->getline())) {
248 * We generate %line directives if needed for later programs
250 int32_t linnum = prior_linnum += lineinc;
251 int altline = src_get(&linnum, &file_name);
252 if (altline) {
253 if (altline == 1 && lineinc == 1)
254 nasm_fputs("", ofile);
255 else {
256 lineinc = (altline != -1 || lineinc != 1);
257 fprintf(ofile ? ofile : stdout,
258 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
259 file_name);
261 prior_linnum = linnum;
263 nasm_fputs(line, ofile);
264 nasm_free(line);
266 nasm_free(file_name);
267 preproc->cleanup(0);
268 if (ofile)
269 fclose(ofile);
270 if (ofile && terminate_after_phase)
271 remove(outname);
273 break;
275 case op_normal:
278 * We must call ofmt->filename _anyway_, even if the user
279 * has specified their own output file, because some
280 * formats (eg OBJ and COFF) use ofmt->filename to find out
281 * the name of the input file and then put that inside the
282 * file.
284 ofmt->filename(inname, outname, report_error);
286 ofile = fopen(outname, "wb");
287 if (!ofile) {
288 report_error(ERR_FATAL | ERR_NOFILE,
289 "unable to open output file `%s'", outname);
293 * We must call init_labels() before ofmt->init() since
294 * some object formats will want to define labels in their
295 * init routines. (eg OS/2 defines the FLAT group)
297 init_labels();
299 ofmt->init(ofile, report_error, define_label, evaluate);
301 assemble_file(inname);
303 if (!terminate_after_phase) {
304 ofmt->cleanup(using_debug_info);
305 cleanup_labels();
306 } else {
308 * We had an fclose on the output file here, but we
309 * actually do that in all the object file drivers as well,
310 * so we're leaving out the one here.
311 * fclose (ofile);
313 remove(outname);
314 if (listname[0])
315 remove(listname);
318 break;
321 if (want_usage)
322 usage();
324 raa_free(offsets);
325 saa_free(forwrefs);
326 eval_cleanup();
327 stdscan_cleanup();
329 if (terminate_after_phase)
330 return 1;
331 else
332 return 0;
336 * Get a parameter for a command line option.
337 * First arg must be in the form of e.g. -f...
339 static char *get_param(char *p, char *q, int *advance)
341 *advance = 0;
342 if (p[2]) { /* the parameter's in the option */
343 p += 2;
344 while (isspace(*p))
345 p++;
346 return p;
348 if (q && q[0]) {
349 *advance = 1;
350 return q;
352 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
353 "option `-%c' requires an argument", p[1]);
354 return NULL;
357 struct textargs {
358 const char *label;
359 int value;
362 #define OPT_PREFIX 0
363 #define OPT_POSTFIX 1
364 struct textargs textopts[] = {
365 {"prefix", OPT_PREFIX},
366 {"postfix", OPT_POSTFIX},
367 {NULL, 0}
370 int stopoptions = 0;
371 static int process_arg(char *p, char *q)
373 char *param;
374 int i, advance = 0;
376 if (!p || !p[0])
377 return 0;
379 if (p[0] == '-' && !stopoptions) {
380 switch (p[1]) {
381 case 's':
382 error_file = stdout;
383 break;
384 case 'o': /* these parameters take values */
385 case 'O':
386 case 'f':
387 case 'p':
388 case 'P':
389 case 'd':
390 case 'D':
391 case 'i':
392 case 'I':
393 case 'l':
394 case 'F':
395 case 'X':
396 case 'u':
397 case 'U':
398 case 'Z':
399 if (!(param = get_param(p, q, &advance)))
400 break;
401 if (p[1] == 'o') { /* output file */
402 strcpy(outname, param);
403 } else if (p[1] == 'f') { /* output format */
404 ofmt = ofmt_find(param);
405 if (!ofmt) {
406 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
407 "unrecognised output format `%s' - "
408 "use -hf for a list", param);
409 } else
410 ofmt->current_dfmt = ofmt->debug_formats[0];
411 } else if (p[1] == 'O') { /* Optimization level */
412 int opt;
414 if (!*param) {
415 /* Naked -O == -Ox */
416 optimizing = INT_MAX >> 1; /* Almost unlimited */
417 } else {
418 while (*param) {
419 switch (*param) {
420 case '0': case '1': case '2': case '3': case '4':
421 case '5': case '6': case '7': case '8': case '9':
422 opt = strtoul(param, &param, 10);
424 /* -O0 -> optimizing == -1, 0.98 behaviour */
425 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
426 if (opt < 2)
427 optimizing = opt - 1;
428 else if (opt <= 5)
429 /* The optimizer seems to have problems with
430 < 5 passes? Hidden bug? */
431 optimizing = 5; /* 5 passes */
432 else
433 optimizing = opt; /* More than 5 passes */
434 break;
436 case 'v':
437 case '+':
438 param++;
439 opt_verbose_info = true;
440 break;
442 case 'x':
443 param++;
444 optimizing = INT_MAX >> 1; /* Almost unlimited */
445 break;
447 default:
448 report_error(ERR_FATAL,
449 "unknown optimization option -O%c\n",
450 *param);
451 break;
455 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
456 pp_pre_include(param);
457 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
458 pp_pre_define(param);
459 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
460 pp_pre_undefine(param);
461 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
462 pp_include_path(param);
463 } else if (p[1] == 'l') { /* listing file */
464 strcpy(listname, param);
465 } else if (p[1] == 'Z') { /* error messages file */
466 strcpy(errname, param);
467 } else if (p[1] == 'F') { /* specify debug format */
468 ofmt->current_dfmt = dfmt_find(ofmt, param);
469 if (!ofmt->current_dfmt) {
470 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
471 "unrecognized debug format `%s' for"
472 " output format `%s'",
473 param, ofmt->shortname);
475 } else if (p[1] == 'X') { /* specify error reporting format */
476 if (nasm_stricmp("vc", param) == 0)
477 report_error = report_error_vc;
478 else if (nasm_stricmp("gnu", param) == 0)
479 report_error = report_error_gnu;
480 else
481 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
482 "unrecognized error reporting format `%s'",
483 param);
485 break;
486 case 'g':
487 using_debug_info = true;
488 break;
489 case 'h':
490 printf
491 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
492 "[-l listfile]\n"
493 " [options...] [--] filename\n"
494 " or nasm -v for version info\n\n"
495 " -t assemble in SciTech TASM compatible mode\n"
496 " -g generate debug information in selected format.\n");
497 printf
498 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
499 " -a don't preprocess (assemble only)\n"
500 " -M generate Makefile dependencies on stdout\n"
501 " -MG d:o, missing files assumed generated\n\n"
502 " -Z<file> redirect error messages to file\n"
503 " -s redirect error messages to stdout\n\n"
504 " -F format select a debugging format\n\n"
505 " -I<path> adds a pathname to the include file path\n");
506 printf
507 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
508 " -P<file> pre-includes a file\n"
509 " -D<macro>[=<value>] pre-defines a macro\n"
510 " -U<macro> undefines a macro\n"
511 " -X<format> specifies error reporting format (gnu or vc)\n"
512 " -w+foo enables warnings about foo; -w-foo disables them\n"
513 "where foo can be:\n");
514 for (i = 1; i <= ERR_WARN_MAX; i++)
515 printf(" %-23s %s (default %s)\n",
516 suppressed_names[i], suppressed_what[i],
517 suppressed[i] ? "off" : "on");
518 printf
519 ("\nresponse files should contain command line parameters"
520 ", one per line.\n");
521 if (p[2] == 'f') {
522 printf("\nvalid output formats for -f are"
523 " (`*' denotes default):\n");
524 ofmt_list(ofmt, stdout);
525 } else {
526 printf("\nFor a list of valid output formats, use -hf.\n");
527 printf("For a list of debug formats, use -f <form> -y.\n");
529 exit(0); /* never need usage message here */
530 break;
531 case 'y':
532 printf("\nvalid debug formats for '%s' output format are"
533 " ('*' denotes default):\n", ofmt->shortname);
534 dfmt_list(ofmt, stdout);
535 exit(0);
536 break;
537 case 't':
538 tasm_compatible_mode = true;
539 break;
540 case 'v':
542 const char *nasm_version_string =
543 "NASM version " NASM_VER " compiled on " __DATE__
544 #ifdef DEBUG
545 " with -DDEBUG"
546 #endif
548 puts(nasm_version_string);
549 exit(0); /* never need usage message here */
551 break;
552 case 'e': /* preprocess only */
553 case 'E':
554 operating_mode = op_preprocess;
555 break;
556 case 'a': /* assemble only - don't preprocess */
557 preproc = &no_pp;
558 break;
559 case 'w':
560 if (p[2] != '+' && p[2] != '-') {
561 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
562 "invalid option to `-w'");
563 } else {
564 for (i = 1; i <= ERR_WARN_MAX; i++)
565 if (!nasm_stricmp(p + 3, suppressed_names[i]))
566 break;
567 if (i <= ERR_WARN_MAX)
568 suppressed[i] = (p[2] == '-');
569 else
570 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
571 "invalid option to `-w'");
573 break;
574 case 'M':
575 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
576 break;
578 case '-':
580 int s;
582 if (p[2] == 0) { /* -- => stop processing options */
583 stopoptions = 1;
584 break;
586 for (s = 0; textopts[s].label; s++) {
587 if (!nasm_stricmp(p + 2, textopts[s].label)) {
588 break;
592 switch (s) {
594 case OPT_PREFIX:
595 case OPT_POSTFIX:
597 if (!q) {
598 report_error(ERR_NONFATAL | ERR_NOFILE |
599 ERR_USAGE,
600 "option `--%s' requires an argument",
601 p + 2);
602 break;
603 } else {
604 advance = 1, param = q;
607 if (s == OPT_PREFIX) {
608 strncpy(lprefix, param, PREFIX_MAX - 1);
609 lprefix[PREFIX_MAX - 1] = 0;
610 break;
612 if (s == OPT_POSTFIX) {
613 strncpy(lpostfix, param, POSTFIX_MAX - 1);
614 lpostfix[POSTFIX_MAX - 1] = 0;
615 break;
617 break;
619 default:
621 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
622 "unrecognised option `--%s'", p + 2);
623 break;
626 break;
629 default:
630 if (!ofmt->setinfo(GI_SWITCH, &p))
631 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
632 "unrecognised option `-%c'", p[1]);
633 break;
635 } else {
636 if (*inname) {
637 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
638 "more than one input file specified");
639 } else
640 strcpy(inname, p);
643 return advance;
646 #define ARG_BUF_DELTA 128
648 static void process_respfile(FILE * rfile)
650 char *buffer, *p, *q, *prevarg;
651 int bufsize, prevargsize;
653 bufsize = prevargsize = ARG_BUF_DELTA;
654 buffer = nasm_malloc(ARG_BUF_DELTA);
655 prevarg = nasm_malloc(ARG_BUF_DELTA);
656 prevarg[0] = '\0';
658 while (1) { /* Loop to handle all lines in file */
660 p = buffer;
661 while (1) { /* Loop to handle long lines */
662 q = fgets(p, bufsize - (p - buffer), rfile);
663 if (!q)
664 break;
665 p += strlen(p);
666 if (p > buffer && p[-1] == '\n')
667 break;
668 if (p - buffer > bufsize - 10) {
669 int offset;
670 offset = p - buffer;
671 bufsize += ARG_BUF_DELTA;
672 buffer = nasm_realloc(buffer, bufsize);
673 p = buffer + offset;
677 if (!q && p == buffer) {
678 if (prevarg[0])
679 process_arg(prevarg, NULL);
680 nasm_free(buffer);
681 nasm_free(prevarg);
682 return;
686 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
687 * them are present at the end of the line.
689 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
691 while (p > buffer && isspace(p[-1]))
692 *--p = '\0';
694 p = buffer;
695 while (isspace(*p))
696 p++;
698 if (process_arg(prevarg, p))
699 *p = '\0';
701 if ((int) strlen(p) > prevargsize - 10) {
702 prevargsize += ARG_BUF_DELTA;
703 prevarg = nasm_realloc(prevarg, prevargsize);
705 strcpy(prevarg, p);
709 /* Function to process args from a string of args, rather than the
710 * argv array. Used by the environment variable and response file
711 * processing.
713 static void process_args(char *args)
715 char *p, *q, *arg, *prevarg;
716 char separator = ' ';
718 p = args;
719 if (*p && *p != '-')
720 separator = *p++;
721 arg = NULL;
722 while (*p) {
723 q = p;
724 while (*p && *p != separator)
725 p++;
726 while (*p == separator)
727 *p++ = '\0';
728 prevarg = arg;
729 arg = q;
730 if (process_arg(prevarg, arg))
731 arg = NULL;
733 if (arg)
734 process_arg(arg, NULL);
737 static void parse_cmdline(int argc, char **argv)
739 FILE *rfile;
740 char *envreal, *envcopy = NULL, *p, *arg;
742 *inname = *outname = *listname = *errname = '\0';
745 * First, process the NASMENV environment variable.
747 envreal = getenv("NASMENV");
748 arg = NULL;
749 if (envreal) {
750 envcopy = nasm_strdup(envreal);
751 process_args(envcopy);
752 nasm_free(envcopy);
756 * Now process the actual command line.
758 while (--argc) {
759 int i;
760 argv++;
761 if (argv[0][0] == '@') {
762 /* We have a response file, so process this as a set of
763 * arguments like the environment variable. This allows us
764 * to have multiple arguments on a single line, which is
765 * different to the -@resp file processing below for regular
766 * NASM.
768 char *str = malloc(2048);
769 FILE *f = fopen(&argv[0][1], "r");
770 if (!str) {
771 printf("out of memory");
772 exit(-1);
774 if (f) {
775 while (fgets(str, 2048, f)) {
776 process_args(str);
778 fclose(f);
780 free(str);
781 argc--;
782 argv++;
784 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
785 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
786 if (p) {
787 rfile = fopen(p, "r");
788 if (rfile) {
789 process_respfile(rfile);
790 fclose(rfile);
791 } else
792 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
793 "unable to open response file `%s'", p);
795 } else
796 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
797 argv += i, argc -= i;
800 if (!*inname)
801 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
802 "no input file specified");
804 /* Look for basic command line typos. This definitely doesn't
805 catch all errors, but it might help cases of fumbled fingers. */
806 if (!strcmp(inname, errname) || !strcmp(inname, outname) ||
807 !strcmp(inname, listname))
808 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
809 "file `%s' is both input and output file",
810 inname);
812 if (*errname) {
813 error_file = fopen(errname, "w");
814 if (!error_file) {
815 error_file = stderr; /* Revert to default! */
816 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
817 "cannot open file `%s' for error messages",
818 errname);
823 /* List of directives */
824 enum directives {
825 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
826 D_EXTERN, D_FLOAT, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
828 static const char *directives[] = {
829 "", "absolute", "bits", "common", "cpu", "debug", "default",
830 "extern", "float", "global", "list", "section", "segment", "warning"
832 static enum directives getkw(char **directive, char **value);
834 static void assemble_file(char *fname)
836 char *directive, *value, *p, *q, *special, *line, debugid[80];
837 insn output_ins;
838 int i, validid;
839 bool rn_error;
840 int32_t seg;
841 int64_t offs;
842 struct tokenval tokval;
843 expr *e;
844 int pass, pass_max;
845 int pass_cnt = 0; /* count actual passes */
847 if (cmd_sb == 32 && cmd_cpu < IF_386)
848 report_error(ERR_FATAL, "command line: "
849 "32-bit segment size requires a higher cpu");
851 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
852 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
853 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
854 int pass1, pass2;
855 ldfunc def_label;
857 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
858 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
859 /* pass0 seq is 0, 0, 0,..., 1, 2 */
861 def_label = pass > 1 ? redefine_label : define_label;
863 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
864 cpu = cmd_cpu;
865 if (pass0 == 2) {
866 if (*listname)
867 nasmlist.init(listname, report_error);
869 in_abs_seg = false;
870 global_offset_changed = false; /* set by redefine_label */
871 location.segment = ofmt->section(NULL, pass2, &sb);
872 globalbits = sb;
873 if (pass > 1) {
874 saa_rewind(forwrefs);
875 forwref = saa_rstruct(forwrefs);
876 raa_free(offsets);
877 offsets = raa_init();
879 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
880 globallineno = 0;
881 if (pass == 1)
882 location.known = true;
883 location.offset = offs = GET_CURR_OFFS;
885 while ((line = preproc->getline())) {
886 enum directives d;
887 globallineno++;
889 /* here we parse our directives; this is not handled by the 'real'
890 * parser. */
891 directive = line;
892 d = getkw(&directive, &value);
893 if (d) {
894 int err = 0;
896 switch (d) {
897 case D_SEGMENT: /* [SEGMENT n] */
898 case D_SECTION:
899 seg = ofmt->section(value, pass2, &sb);
900 if (seg == NO_SEG) {
901 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
902 "segment name `%s' not recognized",
903 value);
904 } else {
905 in_abs_seg = false;
906 location.segment = seg;
908 break;
909 case D_EXTERN: /* [EXTERN label:special] */
910 if (*value == '$')
911 value++; /* skip initial $ if present */
912 if (pass0 == 2) {
913 q = value;
914 while (*q && *q != ':')
915 q++;
916 if (*q == ':') {
917 *q++ = '\0';
918 ofmt->symdef(value, 0L, 0L, 3, q);
920 } else if (pass == 1) { /* pass == 1 */
921 q = value;
922 validid = true;
923 if (!isidstart(*q))
924 validid = false;
925 while (*q && *q != ':') {
926 if (!isidchar(*q))
927 validid = false;
928 q++;
930 if (!validid) {
931 report_error(ERR_NONFATAL,
932 "identifier expected after EXTERN");
933 break;
935 if (*q == ':') {
936 *q++ = '\0';
937 special = q;
938 } else
939 special = NULL;
940 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
941 int temp = pass0;
942 pass0 = 1; /* fake pass 1 in labels.c */
943 declare_as_global(value, special,
944 report_error);
945 define_label(value, seg_alloc(), 0L, NULL,
946 false, true, ofmt, report_error);
947 pass0 = temp;
949 } /* else pass0 == 1 */
950 break;
951 case D_BITS: /* [BITS bits] */
952 globalbits = sb = get_bits(value);
953 break;
954 case D_GLOBAL: /* [GLOBAL symbol:special] */
955 if (*value == '$')
956 value++; /* skip initial $ if present */
957 if (pass0 == 2) { /* pass 2 */
958 q = value;
959 while (*q && *q != ':')
960 q++;
961 if (*q == ':') {
962 *q++ = '\0';
963 ofmt->symdef(value, 0L, 0L, 3, q);
965 } else if (pass2 == 1) { /* pass == 1 */
966 q = value;
967 validid = true;
968 if (!isidstart(*q))
969 validid = false;
970 while (*q && *q != ':') {
971 if (!isidchar(*q))
972 validid = false;
973 q++;
975 if (!validid) {
976 report_error(ERR_NONFATAL,
977 "identifier expected after GLOBAL");
978 break;
980 if (*q == ':') {
981 *q++ = '\0';
982 special = q;
983 } else
984 special = NULL;
985 declare_as_global(value, special, report_error);
986 } /* pass == 1 */
987 break;
988 case D_COMMON: /* [COMMON symbol size:special] */
989 if (*value == '$')
990 value++; /* skip initial $ if present */
991 if (pass0 == 1) {
992 p = value;
993 validid = true;
994 if (!isidstart(*p))
995 validid = false;
996 while (*p && !isspace(*p)) {
997 if (!isidchar(*p))
998 validid = false;
999 p++;
1001 if (!validid) {
1002 report_error(ERR_NONFATAL,
1003 "identifier expected after COMMON");
1004 break;
1006 if (*p) {
1007 int64_t size;
1009 while (*p && isspace(*p))
1010 *p++ = '\0';
1011 q = p;
1012 while (*q && *q != ':')
1013 q++;
1014 if (*q == ':') {
1015 *q++ = '\0';
1016 special = q;
1017 } else
1018 special = NULL;
1019 size = readnum(p, &rn_error);
1020 if (rn_error)
1021 report_error(ERR_NONFATAL,
1022 "invalid size specified"
1023 " in COMMON declaration");
1024 else
1025 define_common(value, seg_alloc(), size,
1026 special, ofmt, report_error);
1027 } else
1028 report_error(ERR_NONFATAL,
1029 "no size specified in"
1030 " COMMON declaration");
1031 } else if (pass0 == 2) { /* pass == 2 */
1032 q = value;
1033 while (*q && *q != ':') {
1034 if (isspace(*q))
1035 *q = '\0';
1036 q++;
1038 if (*q == ':') {
1039 *q++ = '\0';
1040 ofmt->symdef(value, 0L, 0L, 3, q);
1043 break;
1044 case D_ABSOLUTE: /* [ABSOLUTE address] */
1045 stdscan_reset();
1046 stdscan_bufptr = value;
1047 tokval.t_type = TOKEN_INVALID;
1048 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1049 report_error, NULL);
1050 if (e) {
1051 if (!is_reloc(e))
1052 report_error(pass0 ==
1053 1 ? ERR_NONFATAL : ERR_PANIC,
1054 "cannot use non-relocatable expression as "
1055 "ABSOLUTE address");
1056 else {
1057 abs_seg = reloc_seg(e);
1058 abs_offset = reloc_value(e);
1060 } else if (pass == 1)
1061 abs_offset = 0x100; /* don't go near zero in case of / */
1062 else
1063 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1064 "in pass two");
1065 in_abs_seg = true;
1066 location.segment = NO_SEG;
1067 break;
1068 case D_DEBUG: /* [DEBUG] */
1069 p = value;
1070 q = debugid;
1071 validid = true;
1072 if (!isidstart(*p))
1073 validid = false;
1074 while (*p && !isspace(*p)) {
1075 if (!isidchar(*p))
1076 validid = false;
1077 *q++ = *p++;
1079 *q++ = 0;
1080 if (!validid) {
1081 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1082 "identifier expected after DEBUG");
1083 break;
1085 while (*p && isspace(*p))
1086 p++;
1087 if (pass == pass_max)
1088 ofmt->current_dfmt->debug_directive(debugid, p);
1089 break;
1090 case D_WARNING: /* [WARNING {+|-}warn-name] */
1091 if (pass1 == 1) {
1092 while (*value && isspace(*value))
1093 value++;
1095 if (*value == '+' || *value == '-') {
1096 validid = (*value == '-') ? true : false;
1097 value++;
1098 } else
1099 validid = false;
1101 for (i = 1; i <= ERR_WARN_MAX; i++)
1102 if (!nasm_stricmp(value, suppressed_names[i]))
1103 break;
1104 if (i <= ERR_WARN_MAX)
1105 suppressed[i] = validid;
1106 else
1107 report_error(ERR_NONFATAL,
1108 "invalid warning id in WARNING directive");
1110 break;
1111 case D_CPU: /* [CPU] */
1112 cpu = get_cpu(value);
1113 break;
1114 case D_LIST: /* [LIST {+|-}] */
1115 while (*value && isspace(*value))
1116 value++;
1118 if (*value == '+') {
1119 user_nolist = 0;
1120 } else {
1121 if (*value == '-') {
1122 user_nolist = 1;
1123 } else {
1124 err = 1;
1127 break;
1128 case D_DEFAULT: /* [DEFAULT] */
1129 stdscan_reset();
1130 stdscan_bufptr = value;
1131 tokval.t_type = TOKEN_INVALID;
1132 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1133 switch ((int)tokval.t_integer) {
1134 case S_REL:
1135 globalrel = 1;
1136 break;
1137 case S_ABS:
1138 globalrel = 0;
1139 break;
1140 default:
1141 err = 1;
1142 break;
1144 } else {
1145 err = 1;
1147 break;
1148 case D_FLOAT:
1149 if (float_option(value)) {
1150 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1151 "unknown 'float' directive: %s",
1152 value);
1154 break;
1155 default:
1156 if (!ofmt->directive(directive, value, pass2))
1157 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1158 "unrecognised directive [%s]",
1159 directive);
1161 if (err) {
1162 report_error(ERR_NONFATAL,
1163 "invalid parameter to [%s] directive",
1164 directive);
1166 } else { /* it isn't a directive */
1168 parse_line(pass1, line, &output_ins,
1169 report_error, evaluate, def_label);
1171 if (!(optimizing > 0) && pass == 2) {
1172 if (forwref != NULL && globallineno == forwref->lineno) {
1173 output_ins.forw_ref = true;
1174 do {
1175 output_ins.oprs[forwref->operand].opflags |=
1176 OPFLAG_FORWARD;
1177 forwref = saa_rstruct(forwrefs);
1178 } while (forwref != NULL
1179 && forwref->lineno == globallineno);
1180 } else
1181 output_ins.forw_ref = false;
1184 if (!(optimizing > 0) && output_ins.forw_ref) {
1185 if (pass == 1) {
1186 for (i = 0; i < output_ins.operands; i++) {
1187 if (output_ins.oprs[i].
1188 opflags & OPFLAG_FORWARD) {
1189 struct forwrefinfo *fwinf =
1190 (struct forwrefinfo *)
1191 saa_wstruct(forwrefs);
1192 fwinf->lineno = globallineno;
1193 fwinf->operand = i;
1196 } else { /* pass == 2 */
1198 * Hack to prevent phase error in the code
1199 * rol ax,x
1200 * x equ 1
1202 * If the second operand is a forward reference,
1203 * the UNITY property of the number 1 in that
1204 * operand is cancelled. Otherwise the above
1205 * sequence will cause a phase error.
1207 * This hack means that the above code will
1208 * generate 286+ code.
1210 * The forward reference will mean that the
1211 * operand will not have the UNITY property on
1212 * the first pass, so the pass behaviours will
1213 * be consistent.
1216 if (output_ins.operands >= 2 &&
1217 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1218 !(IMMEDIATE & ~output_ins.oprs[1].type))
1220 /* Remove special properties bits */
1221 output_ins.oprs[1].type &= ~REG_SMASK;
1224 } /* pass == 2 */
1228 /* forw_ref */
1229 if (output_ins.opcode == I_EQU) {
1230 if (pass1 == 1) {
1232 * Special `..' EQUs get processed in pass two,
1233 * except `..@' macro-processor EQUs which are done
1234 * in the normal place.
1236 if (!output_ins.label)
1237 report_error(ERR_NONFATAL,
1238 "EQU not preceded by label");
1240 else if (output_ins.label[0] != '.' ||
1241 output_ins.label[1] != '.' ||
1242 output_ins.label[2] == '@') {
1243 if (output_ins.operands == 1 &&
1244 (output_ins.oprs[0].type & IMMEDIATE) &&
1245 output_ins.oprs[0].wrt == NO_SEG) {
1246 int isext =
1247 output_ins.oprs[0].
1248 opflags & OPFLAG_EXTERN;
1249 def_label(output_ins.label,
1250 output_ins.oprs[0].segment,
1251 output_ins.oprs[0].offset, NULL,
1252 false, isext, ofmt,
1253 report_error);
1254 } else if (output_ins.operands == 2
1255 && (output_ins.oprs[0].
1256 type & IMMEDIATE)
1257 && (output_ins.oprs[0].type & COLON)
1258 && output_ins.oprs[0].segment ==
1259 NO_SEG
1260 && output_ins.oprs[0].wrt == NO_SEG
1261 && (output_ins.oprs[1].
1262 type & IMMEDIATE)
1263 && output_ins.oprs[1].segment ==
1264 NO_SEG
1265 && output_ins.oprs[1].wrt ==
1266 NO_SEG) {
1267 def_label(output_ins.label,
1268 output_ins.oprs[0].
1269 offset | SEG_ABS,
1270 output_ins.oprs[1].offset, NULL,
1271 false, false, ofmt,
1272 report_error);
1273 } else
1274 report_error(ERR_NONFATAL,
1275 "bad syntax for EQU");
1277 } else { /* pass == 2 */
1279 * Special `..' EQUs get processed here, except
1280 * `..@' macro processor EQUs which are done above.
1282 if (output_ins.label[0] == '.' &&
1283 output_ins.label[1] == '.' &&
1284 output_ins.label[2] != '@') {
1285 if (output_ins.operands == 1 &&
1286 (output_ins.oprs[0].type & IMMEDIATE)) {
1287 define_label(output_ins.label,
1288 output_ins.oprs[0].segment,
1289 output_ins.oprs[0].offset,
1290 NULL, false, false, ofmt,
1291 report_error);
1292 } else if (output_ins.operands == 2
1293 && (output_ins.oprs[0].
1294 type & IMMEDIATE)
1295 && (output_ins.oprs[0].type & COLON)
1296 && output_ins.oprs[0].segment ==
1297 NO_SEG
1298 && (output_ins.oprs[1].
1299 type & IMMEDIATE)
1300 && output_ins.oprs[1].segment ==
1301 NO_SEG) {
1302 define_label(output_ins.label,
1303 output_ins.oprs[0].
1304 offset | SEG_ABS,
1305 output_ins.oprs[1].offset,
1306 NULL, false, false, ofmt,
1307 report_error);
1308 } else
1309 report_error(ERR_NONFATAL,
1310 "bad syntax for EQU");
1312 } /* pass == 2 */
1313 } else { /* instruction isn't an EQU */
1315 if (pass1 == 1) {
1317 int64_t l = insn_size(location.segment, offs, sb, cpu,
1318 &output_ins, report_error);
1320 /* if (using_debug_info) && output_ins.opcode != -1) */
1321 if (using_debug_info)
1322 { /* fbk 03/25/01 */
1323 /* this is done here so we can do debug type info */
1324 int32_t typeinfo =
1325 TYS_ELEMENTS(output_ins.operands);
1326 switch (output_ins.opcode) {
1327 case I_RESB:
1328 typeinfo =
1329 TYS_ELEMENTS(output_ins.oprs[0].
1330 offset) | TY_BYTE;
1331 break;
1332 case I_RESW:
1333 typeinfo =
1334 TYS_ELEMENTS(output_ins.oprs[0].
1335 offset) | TY_WORD;
1336 break;
1337 case I_RESD:
1338 typeinfo =
1339 TYS_ELEMENTS(output_ins.oprs[0].
1340 offset) | TY_DWORD;
1341 break;
1342 case I_RESQ:
1343 typeinfo =
1344 TYS_ELEMENTS(output_ins.oprs[0].
1345 offset) | TY_QWORD;
1346 break;
1347 case I_REST:
1348 typeinfo =
1349 TYS_ELEMENTS(output_ins.oprs[0].
1350 offset) | TY_TBYTE;
1351 break;
1352 case I_DB:
1353 typeinfo |= TY_BYTE;
1354 break;
1355 case I_DW:
1356 typeinfo |= TY_WORD;
1357 break;
1358 case I_DD:
1359 if (output_ins.eops_float)
1360 typeinfo |= TY_FLOAT;
1361 else
1362 typeinfo |= TY_DWORD;
1363 break;
1364 case I_DQ:
1365 typeinfo |= TY_QWORD;
1366 break;
1367 case I_DT:
1368 typeinfo |= TY_TBYTE;
1369 break;
1370 case I_DO:
1371 typeinfo |= TY_OWORD;
1372 break;
1373 default:
1374 typeinfo = TY_LABEL;
1378 ofmt->current_dfmt->debug_typevalue(typeinfo);
1381 if (l != -1) {
1382 offs += l;
1383 SET_CURR_OFFS(offs);
1386 * else l == -1 => invalid instruction, which will be
1387 * flagged as an error on pass 2
1390 } else { /* pass == 2 */
1391 offs += assemble(location.segment, offs, sb, cpu,
1392 &output_ins, ofmt, report_error,
1393 &nasmlist);
1394 SET_CURR_OFFS(offs);
1397 } /* not an EQU */
1398 cleanup_insn(&output_ins);
1400 nasm_free(line);
1401 location.offset = offs = GET_CURR_OFFS;
1402 } /* end while (line = preproc->getline... */
1404 if (pass1 == 2 && global_offset_changed)
1405 report_error(ERR_NONFATAL,
1406 "phase error detected at end of assembly.");
1408 if (pass1 == 1)
1409 preproc->cleanup(1);
1411 if (pass1 == 1 && terminate_after_phase) {
1412 fclose(ofile);
1413 remove(outname);
1414 if (want_usage)
1415 usage();
1416 exit(1);
1418 pass_cnt++;
1419 if (pass > 1 && !global_offset_changed) {
1420 pass0++;
1421 if (pass0 == 2)
1422 pass = pass_max - 1;
1423 } else if (!(optimizing > 0))
1424 pass0++;
1426 } /* for (pass=1; pass<=2; pass++) */
1428 preproc->cleanup(0);
1429 nasmlist.cleanup();
1430 #if 1
1431 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1432 fprintf(stdout,
1433 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1434 #endif
1435 } /* exit from assemble_file (...) */
1437 static enum directives getkw(char **directive, char **value)
1439 char *p, *q, *buf;
1441 buf = *directive;
1443 /* allow leading spaces or tabs */
1444 while (*buf == ' ' || *buf == '\t')
1445 buf++;
1447 if (*buf != '[')
1448 return 0;
1450 p = buf;
1452 while (*p && *p != ']')
1453 p++;
1455 if (!*p)
1456 return 0;
1458 q = p++;
1460 while (*p && *p != ';') {
1461 if (!isspace(*p))
1462 return 0;
1463 p++;
1465 q[1] = '\0';
1467 *directive = p = buf + 1;
1468 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1469 buf++;
1470 if (*buf == ']') {
1471 *buf = '\0';
1472 *value = buf;
1473 } else {
1474 *buf++ = '\0';
1475 while (isspace(*buf))
1476 buf++; /* beppu - skip leading whitespace */
1477 *value = buf;
1478 while (*buf != ']')
1479 buf++;
1480 *buf++ = '\0';
1483 return bsii(*directive, directives, elements(directives));
1487 * gnu style error reporting
1488 * This function prints an error message to error_file in the
1489 * style used by GNU. An example would be:
1490 * file.asm:50: error: blah blah blah
1491 * where file.asm is the name of the file, 50 is the line number on
1492 * which the error occurs (or is detected) and "error:" is one of
1493 * the possible optional diagnostics -- it can be "error" or "warning"
1494 * or something else. Finally the line terminates with the actual
1495 * error message.
1497 * @param severity the severity of the warning or error
1498 * @param fmt the printf style format string
1500 static void report_error_gnu(int severity, const char *fmt, ...)
1502 va_list ap;
1504 if (is_suppressed_warning(severity))
1505 return;
1507 if (severity & ERR_NOFILE)
1508 fputs("nasm: ", error_file);
1509 else {
1510 char *currentfile = NULL;
1511 int32_t lineno = 0;
1512 src_get(&lineno, &currentfile);
1513 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1514 nasm_free(currentfile);
1516 va_start(ap, fmt);
1517 report_error_common(severity, fmt, ap);
1518 va_end(ap);
1522 * MS style error reporting
1523 * This function prints an error message to error_file in the
1524 * style used by Visual C and some other Microsoft tools. An example
1525 * would be:
1526 * file.asm(50) : error: blah blah blah
1527 * where file.asm is the name of the file, 50 is the line number on
1528 * which the error occurs (or is detected) and "error:" is one of
1529 * the possible optional diagnostics -- it can be "error" or "warning"
1530 * or something else. Finally the line terminates with the actual
1531 * error message.
1533 * @param severity the severity of the warning or error
1534 * @param fmt the printf style format string
1536 static void report_error_vc(int severity, const char *fmt, ...)
1538 va_list ap;
1540 if (is_suppressed_warning(severity))
1541 return;
1543 if (severity & ERR_NOFILE)
1544 fputs("nasm: ", error_file);
1545 else {
1546 char *currentfile = NULL;
1547 int32_t lineno = 0;
1548 src_get(&lineno, &currentfile);
1549 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1550 nasm_free(currentfile);
1552 va_start(ap, fmt);
1553 report_error_common(severity, fmt, ap);
1554 va_end(ap);
1558 * check for supressed warning
1559 * checks for suppressed warning or pass one only warning and we're
1560 * not in pass 1
1562 * @param severity the severity of the warning or error
1563 * @return true if we should abort error/warning printing
1565 static int is_suppressed_warning(int severity)
1568 * See if it's a suppressed warning.
1570 return ((severity & ERR_MASK) == ERR_WARNING &&
1571 (severity & ERR_WARN_MASK) != 0 &&
1572 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1574 * See if it's a pass-one only warning and we're not in pass one.
1576 ((severity & ERR_PASS1) && pass0 == 2);
1580 * common error reporting
1581 * This is the common back end of the error reporting schemes currently
1582 * implemented. It prints the nature of the warning and then the
1583 * specific error message to error_file and may or may not return. It
1584 * doesn't return if the error severity is a "panic" or "debug" type.
1586 * @param severity the severity of the warning or error
1587 * @param fmt the printf style format string
1589 static void report_error_common(int severity, const char *fmt,
1590 va_list args)
1592 switch (severity & ERR_MASK) {
1593 case ERR_WARNING:
1594 fputs("warning: ", error_file);
1595 break;
1596 case ERR_NONFATAL:
1597 fputs("error: ", error_file);
1598 break;
1599 case ERR_FATAL:
1600 fputs("fatal: ", error_file);
1601 break;
1602 case ERR_PANIC:
1603 fputs("panic: ", error_file);
1604 break;
1605 case ERR_DEBUG:
1606 fputs("debug: ", error_file);
1607 break;
1610 vfprintf(error_file, fmt, args);
1611 fputc('\n', error_file);
1613 if (severity & ERR_USAGE)
1614 want_usage = true;
1616 switch (severity & ERR_MASK) {
1617 case ERR_WARNING:
1618 case ERR_DEBUG:
1619 /* no further action, by definition */
1620 break;
1621 case ERR_NONFATAL:
1622 /* hack enables listing(!) on errors */
1623 terminate_after_phase = true;
1624 break;
1625 case ERR_FATAL:
1626 if (ofile) {
1627 fclose(ofile);
1628 remove(outname);
1630 if (want_usage)
1631 usage();
1632 exit(1); /* instantly die */
1633 break; /* placate silly compilers */
1634 case ERR_PANIC:
1635 fflush(NULL);
1636 /* abort(); *//* halt, catch fire, and dump core */
1637 exit(3);
1638 break;
1642 static void usage(void)
1644 fputs("type `nasm -h' for help\n", error_file);
1647 static void register_output_formats(void)
1649 ofmt = ofmt_register(report_error);
1652 #define BUF_DELTA 512
1654 static FILE *no_pp_fp;
1655 static efunc no_pp_err;
1656 static ListGen *no_pp_list;
1657 static int32_t no_pp_lineinc;
1659 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1660 ListGen * listgen)
1662 src_set_fname(nasm_strdup(file));
1663 src_set_linnum(0);
1664 no_pp_lineinc = 1;
1665 no_pp_err = error;
1666 no_pp_fp = fopen(file, "r");
1667 if (!no_pp_fp)
1668 no_pp_err(ERR_FATAL | ERR_NOFILE,
1669 "unable to open input file `%s'", file);
1670 no_pp_list = listgen;
1671 (void)pass; /* placate compilers */
1672 (void)eval; /* placate compilers */
1675 static char *no_pp_getline(void)
1677 char *buffer, *p, *q;
1678 int bufsize;
1680 bufsize = BUF_DELTA;
1681 buffer = nasm_malloc(BUF_DELTA);
1682 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1684 while (1) { /* Loop to handle %line */
1686 p = buffer;
1687 while (1) { /* Loop to handle long lines */
1688 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1689 if (!q)
1690 break;
1691 p += strlen(p);
1692 if (p > buffer && p[-1] == '\n')
1693 break;
1694 if (p - buffer > bufsize - 10) {
1695 int offset;
1696 offset = p - buffer;
1697 bufsize += BUF_DELTA;
1698 buffer = nasm_realloc(buffer, bufsize);
1699 p = buffer + offset;
1703 if (!q && p == buffer) {
1704 nasm_free(buffer);
1705 return NULL;
1709 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1710 * them are present at the end of the line.
1712 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1714 if (!nasm_strnicmp(buffer, "%line", 5)) {
1715 int32_t ln;
1716 int li;
1717 char *nm = nasm_malloc(strlen(buffer));
1718 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1719 nasm_free(src_set_fname(nm));
1720 src_set_linnum(ln);
1721 no_pp_lineinc = li;
1722 continue;
1724 nasm_free(nm);
1726 break;
1729 no_pp_list->line(LIST_READ, buffer);
1731 return buffer;
1734 static void no_pp_cleanup(int pass)
1736 (void)pass; /* placate GCC */
1737 fclose(no_pp_fp);
1740 static uint32_t get_cpu(char *value)
1742 if (!strcmp(value, "8086"))
1743 return IF_8086;
1744 if (!strcmp(value, "186"))
1745 return IF_186;
1746 if (!strcmp(value, "286"))
1747 return IF_286;
1748 if (!strcmp(value, "386"))
1749 return IF_386;
1750 if (!strcmp(value, "486"))
1751 return IF_486;
1752 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1753 return IF_PENT;
1754 if (!strcmp(value, "686") ||
1755 !nasm_stricmp(value, "ppro") ||
1756 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1757 return IF_P6;
1758 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1759 return IF_KATMAI;
1760 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1761 !nasm_stricmp(value, "willamette"))
1762 return IF_WILLAMETTE;
1763 if (!nasm_stricmp(value, "prescott"))
1764 return IF_PRESCOTT;
1765 if (!nasm_stricmp(value, "x64") ||
1766 !nasm_stricmp(value, "x86-64"))
1767 return IF_X86_64;
1768 if (!nasm_stricmp(value, "ia64") ||
1769 !nasm_stricmp(value, "ia-64") ||
1770 !nasm_stricmp(value, "itanium") ||
1771 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1772 return IF_IA64;
1774 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1775 "unknown 'cpu' type");
1777 return IF_PLEVEL; /* the maximum level */
1780 static int get_bits(char *value)
1782 int i;
1784 if ((i = atoi(value)) == 16)
1785 return i; /* set for a 16-bit segment */
1786 else if (i == 32) {
1787 if (cpu < IF_386) {
1788 report_error(ERR_NONFATAL,
1789 "cannot specify 32-bit segment on processor below a 386");
1790 i = 16;
1792 } else if (i == 64) {
1793 if (cpu < IF_X86_64) {
1794 report_error(ERR_NONFATAL,
1795 "cannot specify 64-bit segment on processor below an x86-64");
1796 i = 16;
1798 if (i != maxbits) {
1799 report_error(ERR_NONFATAL,
1800 "%s output format does not support 64-bit code",
1801 ofmt->shortname);
1802 i = 16;
1804 } else {
1805 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1806 "`%s' is not a valid segment size; must be 16, 32 or 64",
1807 value);
1808 i = 16;
1810 return i;
1813 /* end of nasm.c */