Remove FIXME from documentation
[nasm.git] / nasm.c
blobb8de19843f4f49709da51698816ed8a446d4bbaf
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 * isn't an actual warning, but it used for -w+error/-Werror.
96 static bool suppressed[ERR_WARN_MAX+1] = {
97 true, false, true, false, 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[ERR_WARN_MAX+1] = {
105 "error", "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[ERR_WARN_MAX+1] = {
115 "treat warnings as errors",
116 "macro calls with wrong parameter count",
117 "cyclic macro references",
118 "labels alone on lines without trailing `:'",
119 "numeric constants does not fit in 64 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 putc('\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, bool *advance)
341 *advance = false;
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 = true;
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 static bool stopoptions = false;
371 static bool process_arg(char *p, char *q)
373 char *param;
374 int i;
375 bool advance = false;
376 bool suppress;
378 if (!p || !p[0])
379 return false;
381 if (p[0] == '-' && !stopoptions) {
382 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
383 /* These parameters take values */
384 if (!(param = get_param(p, q, &advance)))
385 return advance;
388 switch (p[1]) {
389 case 's':
390 error_file = stdout;
391 break;
393 case 'o': /* output file */
394 strcpy(outname, param);
395 break;
397 case 'f': /* output format */
398 ofmt = ofmt_find(param);
399 if (!ofmt) {
400 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
401 "unrecognised output format `%s' - "
402 "use -hf for a list", param);
403 } else {
404 ofmt->current_dfmt = ofmt->debug_formats[0];
406 break;
408 case 'O': /* Optimization level */
410 int opt;
412 if (!*param) {
413 /* Naked -O == -Ox */
414 optimizing = INT_MAX >> 1; /* Almost unlimited */
415 } else {
416 while (*param) {
417 switch (*param) {
418 case '0': case '1': case '2': case '3': case '4':
419 case '5': case '6': case '7': case '8': case '9':
420 opt = strtoul(param, &param, 10);
422 /* -O0 -> optimizing == -1, 0.98 behaviour */
423 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
424 if (opt < 2)
425 optimizing = opt - 1;
426 else if (opt <= 5)
427 /* The optimizer seems to have problems with
428 < 5 passes? Hidden bug? */
429 optimizing = 5; /* 5 passes */
430 else
431 optimizing = opt; /* More than 5 passes */
432 break;
434 case 'v':
435 case '+':
436 param++;
437 opt_verbose_info = true;
438 break;
440 case 'x':
441 param++;
442 optimizing = INT_MAX >> 1; /* Almost unlimited */
443 break;
445 default:
446 report_error(ERR_FATAL,
447 "unknown optimization option -O%c\n",
448 *param);
449 break;
453 break;
456 case 'p': /* pre-include */
457 case 'P':
458 pp_pre_include(param);
459 break;
461 case 'd': /* pre-define */
462 case 'D':
463 pp_pre_define(param);
464 break;
466 case 'u': /* un-define */
467 case 'U':
468 pp_pre_undefine(param);
469 break;
471 case 'i': /* include search path */
472 case 'I':
473 pp_include_path(param);
474 break;
476 case 'l': /* listing file */
477 strcpy(listname, param);
478 break;
480 case 'Z': /* error messages file */
481 strcpy(errname, param);
482 break;
484 case 'F': /* specify debug format */
485 ofmt->current_dfmt = dfmt_find(ofmt, param);
486 if (!ofmt->current_dfmt) {
487 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
488 "unrecognized debug format `%s' for"
489 " output format `%s'",
490 param, ofmt->shortname);
492 break;
494 case 'X': /* specify error reporting format */
495 if (nasm_stricmp("vc", param) == 0)
496 report_error = report_error_vc;
497 else if (nasm_stricmp("gnu", param) == 0)
498 report_error = report_error_gnu;
499 else
500 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
501 "unrecognized error reporting format `%s'",
502 param);
503 break;
505 case 'g':
506 using_debug_info = true;
507 break;
509 case 'h':
510 printf
511 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
512 "[-l listfile]\n"
513 " [options...] [--] filename\n"
514 " or nasm -v for version info\n\n"
515 " -t assemble in SciTech TASM compatible mode\n"
516 " -g generate debug information in selected format.\n");
517 printf
518 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
519 " -a don't preprocess (assemble only)\n"
520 " -M generate Makefile dependencies on stdout\n"
521 " -MG d:o, missing files assumed generated\n\n"
522 " -Z<file> redirect error messages to file\n"
523 " -s redirect error messages to stdout\n\n"
524 " -F format select a debugging format\n\n"
525 " -I<path> adds a pathname to the include file path\n");
526 printf
527 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
528 " -P<file> pre-includes a file\n"
529 " -D<macro>[=<value>] pre-defines a macro\n"
530 " -U<macro> undefines a macro\n"
531 " -X<format> specifies error reporting format (gnu or vc)\n"
532 " -w+foo enables warning foo (equiv. -Wfoo)\n"
533 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
534 "Warnings:\n");
535 for (i = 0; i <= ERR_WARN_MAX; i++)
536 printf(" %-23s %s (default %s)\n",
537 suppressed_names[i], suppressed_what[i],
538 suppressed[i] ? "off" : "on");
539 printf
540 ("\nresponse files should contain command line parameters"
541 ", one per line.\n");
542 if (p[2] == 'f') {
543 printf("\nvalid output formats for -f are"
544 " (`*' denotes default):\n");
545 ofmt_list(ofmt, stdout);
546 } else {
547 printf("\nFor a list of valid output formats, use -hf.\n");
548 printf("For a list of debug formats, use -f <form> -y.\n");
550 exit(0); /* never need usage message here */
551 break;
553 case 'y':
554 printf("\nvalid debug formats for '%s' output format are"
555 " ('*' denotes default):\n", ofmt->shortname);
556 dfmt_list(ofmt, stdout);
557 exit(0);
558 break;
560 case 't':
561 tasm_compatible_mode = true;
562 break;
564 case 'v':
566 const char *nasm_version_string =
567 "NASM version " NASM_VER " compiled on " __DATE__
568 #ifdef DEBUG
569 " with -DDEBUG"
570 #endif
572 puts(nasm_version_string);
573 exit(0); /* never need usage message here */
575 break;
577 case 'e': /* preprocess only */
578 case 'E':
579 operating_mode = op_preprocess;
580 break;
582 case 'a': /* assemble only - don't preprocess */
583 preproc = &no_pp;
584 break;
586 case 'W':
587 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
588 suppress = true;
589 param += 3;
590 } else {
591 suppress = false;
593 goto set_warning;
595 case 'w':
596 if (param[0] != '+' && param[0] != '-') {
597 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
598 "invalid option to `-w'");
599 break;
601 suppress = (param[0] == '-');
602 param++;
603 goto set_warning;
604 set_warning:
605 for (i = 0; i <= ERR_WARN_MAX; i++)
606 if (!nasm_stricmp(param, suppressed_names[i]))
607 break;
608 if (i <= ERR_WARN_MAX)
609 suppressed[i] = suppress;
610 else if (!nasm_stricmp(param, "all"))
611 for (i = 1; i <= ERR_WARN_MAX; i++)
612 suppressed[i] = suppress;
613 else if (!nasm_stricmp(param, "none"))
614 for (i = 1; i <= ERR_WARN_MAX; i++)
615 suppressed[i] = !suppress;
616 else
617 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
618 "invalid warning `%s'", param);
619 break;
621 case 'M':
622 operating_mode = p[2] == 'G' ? op_depend_missing_ok : op_depend;
623 break;
625 case '-':
627 int s;
629 if (p[2] == 0) { /* -- => stop processing options */
630 stopoptions = 1;
631 break;
633 for (s = 0; textopts[s].label; s++) {
634 if (!nasm_stricmp(p + 2, textopts[s].label)) {
635 break;
639 switch (s) {
641 case OPT_PREFIX:
642 case OPT_POSTFIX:
644 if (!q) {
645 report_error(ERR_NONFATAL | ERR_NOFILE |
646 ERR_USAGE,
647 "option `--%s' requires an argument",
648 p + 2);
649 break;
650 } else {
651 advance = 1, param = q;
654 if (s == OPT_PREFIX) {
655 strncpy(lprefix, param, PREFIX_MAX - 1);
656 lprefix[PREFIX_MAX - 1] = 0;
657 break;
659 if (s == OPT_POSTFIX) {
660 strncpy(lpostfix, param, POSTFIX_MAX - 1);
661 lpostfix[POSTFIX_MAX - 1] = 0;
662 break;
664 break;
666 default:
668 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
669 "unrecognised option `--%s'", p + 2);
670 break;
673 break;
676 default:
677 if (!ofmt->setinfo(GI_SWITCH, &p))
678 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
679 "unrecognised option `-%c'", p[1]);
680 break;
682 } else {
683 if (*inname) {
684 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
685 "more than one input file specified");
686 } else
687 strcpy(inname, p);
690 return advance;
693 #define ARG_BUF_DELTA 128
695 static void process_respfile(FILE * rfile)
697 char *buffer, *p, *q, *prevarg;
698 int bufsize, prevargsize;
700 bufsize = prevargsize = ARG_BUF_DELTA;
701 buffer = nasm_malloc(ARG_BUF_DELTA);
702 prevarg = nasm_malloc(ARG_BUF_DELTA);
703 prevarg[0] = '\0';
705 while (1) { /* Loop to handle all lines in file */
707 p = buffer;
708 while (1) { /* Loop to handle long lines */
709 q = fgets(p, bufsize - (p - buffer), rfile);
710 if (!q)
711 break;
712 p += strlen(p);
713 if (p > buffer && p[-1] == '\n')
714 break;
715 if (p - buffer > bufsize - 10) {
716 int offset;
717 offset = p - buffer;
718 bufsize += ARG_BUF_DELTA;
719 buffer = nasm_realloc(buffer, bufsize);
720 p = buffer + offset;
724 if (!q && p == buffer) {
725 if (prevarg[0])
726 process_arg(prevarg, NULL);
727 nasm_free(buffer);
728 nasm_free(prevarg);
729 return;
733 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
734 * them are present at the end of the line.
736 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
738 while (p > buffer && isspace(p[-1]))
739 *--p = '\0';
741 p = buffer;
742 while (isspace(*p))
743 p++;
745 if (process_arg(prevarg, p))
746 *p = '\0';
748 if ((int) strlen(p) > prevargsize - 10) {
749 prevargsize += ARG_BUF_DELTA;
750 prevarg = nasm_realloc(prevarg, prevargsize);
752 strcpy(prevarg, p);
756 /* Function to process args from a string of args, rather than the
757 * argv array. Used by the environment variable and response file
758 * processing.
760 static void process_args(char *args)
762 char *p, *q, *arg, *prevarg;
763 char separator = ' ';
765 p = args;
766 if (*p && *p != '-')
767 separator = *p++;
768 arg = NULL;
769 while (*p) {
770 q = p;
771 while (*p && *p != separator)
772 p++;
773 while (*p == separator)
774 *p++ = '\0';
775 prevarg = arg;
776 arg = q;
777 if (process_arg(prevarg, arg))
778 arg = NULL;
780 if (arg)
781 process_arg(arg, NULL);
784 static void parse_cmdline(int argc, char **argv)
786 FILE *rfile;
787 char *envreal, *envcopy = NULL, *p, *arg;
789 *inname = *outname = *listname = *errname = '\0';
792 * First, process the NASMENV environment variable.
794 envreal = getenv("NASMENV");
795 arg = NULL;
796 if (envreal) {
797 envcopy = nasm_strdup(envreal);
798 process_args(envcopy);
799 nasm_free(envcopy);
803 * Now process the actual command line.
805 while (--argc) {
806 bool advance;
807 argv++;
808 if (argv[0][0] == '@') {
809 /* We have a response file, so process this as a set of
810 * arguments like the environment variable. This allows us
811 * to have multiple arguments on a single line, which is
812 * different to the -@resp file processing below for regular
813 * NASM.
815 char *str = malloc(2048);
816 FILE *f = fopen(&argv[0][1], "r");
817 if (!str) {
818 printf("out of memory");
819 exit(-1);
821 if (f) {
822 while (fgets(str, 2048, f)) {
823 process_args(str);
825 fclose(f);
827 free(str);
828 argc--;
829 argv++;
831 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
832 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
833 if (p) {
834 rfile = fopen(p, "r");
835 if (rfile) {
836 process_respfile(rfile);
837 fclose(rfile);
838 } else
839 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
840 "unable to open response file `%s'", p);
842 } else
843 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
844 argv += advance, argc -= advance;
847 if (!*inname)
848 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
849 "no input file specified");
851 /* Look for basic command line typos. This definitely doesn't
852 catch all errors, but it might help cases of fumbled fingers. */
853 if (!strcmp(inname, errname) || !strcmp(inname, outname) ||
854 !strcmp(inname, listname))
855 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
856 "file `%s' is both input and output file",
857 inname);
859 if (*errname) {
860 error_file = fopen(errname, "w");
861 if (!error_file) {
862 error_file = stderr; /* Revert to default! */
863 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
864 "cannot open file `%s' for error messages",
865 errname);
870 /* List of directives */
871 enum directives {
872 D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
873 D_EXTERN, D_FLOAT, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
875 static const char *directives[] = {
876 "", "absolute", "bits", "common", "cpu", "debug", "default",
877 "extern", "float", "global", "list", "section", "segment", "warning"
879 static enum directives getkw(char **directive, char **value);
881 static void assemble_file(char *fname)
883 char *directive, *value, *p, *q, *special, *line, debugid[80];
884 insn output_ins;
885 int i, validid;
886 bool rn_error;
887 int32_t seg;
888 int64_t offs;
889 struct tokenval tokval;
890 expr *e;
891 int pass, pass_max;
892 int pass_cnt = 0; /* count actual passes */
894 if (cmd_sb == 32 && cmd_cpu < IF_386)
895 report_error(ERR_FATAL, "command line: "
896 "32-bit segment size requires a higher cpu");
898 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
899 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
900 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
901 int pass1, pass2;
902 ldfunc def_label;
904 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
905 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
906 /* pass0 seq is 0, 0, 0,..., 1, 2 */
908 def_label = pass > 1 ? redefine_label : define_label;
910 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
911 cpu = cmd_cpu;
912 if (pass0 == 2) {
913 if (*listname)
914 nasmlist.init(listname, report_error);
916 in_abs_seg = false;
917 global_offset_changed = false; /* set by redefine_label */
918 location.segment = ofmt->section(NULL, pass2, &sb);
919 globalbits = sb;
920 if (pass > 1) {
921 saa_rewind(forwrefs);
922 forwref = saa_rstruct(forwrefs);
923 raa_free(offsets);
924 offsets = raa_init();
926 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
927 globallineno = 0;
928 if (pass == 1)
929 location.known = true;
930 location.offset = offs = GET_CURR_OFFS;
932 while ((line = preproc->getline())) {
933 enum directives d;
934 globallineno++;
936 /* here we parse our directives; this is not handled by the 'real'
937 * parser. */
938 directive = line;
939 d = getkw(&directive, &value);
940 if (d) {
941 int err = 0;
943 switch (d) {
944 case D_SEGMENT: /* [SEGMENT n] */
945 case D_SECTION:
946 seg = ofmt->section(value, pass2, &sb);
947 if (seg == NO_SEG) {
948 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
949 "segment name `%s' not recognized",
950 value);
951 } else {
952 in_abs_seg = false;
953 location.segment = seg;
955 break;
956 case D_EXTERN: /* [EXTERN label:special] */
957 if (*value == '$')
958 value++; /* skip initial $ if present */
959 if (pass0 == 2) {
960 q = value;
961 while (*q && *q != ':')
962 q++;
963 if (*q == ':') {
964 *q++ = '\0';
965 ofmt->symdef(value, 0L, 0L, 3, q);
967 } else if (pass == 1) { /* pass == 1 */
968 q = value;
969 validid = true;
970 if (!isidstart(*q))
971 validid = false;
972 while (*q && *q != ':') {
973 if (!isidchar(*q))
974 validid = false;
975 q++;
977 if (!validid) {
978 report_error(ERR_NONFATAL,
979 "identifier expected after EXTERN");
980 break;
982 if (*q == ':') {
983 *q++ = '\0';
984 special = q;
985 } else
986 special = NULL;
987 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
988 int temp = pass0;
989 pass0 = 1; /* fake pass 1 in labels.c */
990 declare_as_global(value, special,
991 report_error);
992 define_label(value, seg_alloc(), 0L, NULL,
993 false, true, ofmt, report_error);
994 pass0 = temp;
996 } /* else pass0 == 1 */
997 break;
998 case D_BITS: /* [BITS bits] */
999 globalbits = sb = get_bits(value);
1000 break;
1001 case D_GLOBAL: /* [GLOBAL symbol:special] */
1002 if (*value == '$')
1003 value++; /* skip initial $ if present */
1004 if (pass0 == 2) { /* pass 2 */
1005 q = value;
1006 while (*q && *q != ':')
1007 q++;
1008 if (*q == ':') {
1009 *q++ = '\0';
1010 ofmt->symdef(value, 0L, 0L, 3, q);
1012 } else if (pass2 == 1) { /* pass == 1 */
1013 q = value;
1014 validid = true;
1015 if (!isidstart(*q))
1016 validid = false;
1017 while (*q && *q != ':') {
1018 if (!isidchar(*q))
1019 validid = false;
1020 q++;
1022 if (!validid) {
1023 report_error(ERR_NONFATAL,
1024 "identifier expected after GLOBAL");
1025 break;
1027 if (*q == ':') {
1028 *q++ = '\0';
1029 special = q;
1030 } else
1031 special = NULL;
1032 declare_as_global(value, special, report_error);
1033 } /* pass == 1 */
1034 break;
1035 case D_COMMON: /* [COMMON symbol size:special] */
1036 if (*value == '$')
1037 value++; /* skip initial $ if present */
1038 if (pass0 == 1) {
1039 p = value;
1040 validid = true;
1041 if (!isidstart(*p))
1042 validid = false;
1043 while (*p && !isspace(*p)) {
1044 if (!isidchar(*p))
1045 validid = false;
1046 p++;
1048 if (!validid) {
1049 report_error(ERR_NONFATAL,
1050 "identifier expected after COMMON");
1051 break;
1053 if (*p) {
1054 int64_t size;
1056 while (*p && isspace(*p))
1057 *p++ = '\0';
1058 q = p;
1059 while (*q && *q != ':')
1060 q++;
1061 if (*q == ':') {
1062 *q++ = '\0';
1063 special = q;
1064 } else
1065 special = NULL;
1066 size = readnum(p, &rn_error);
1067 if (rn_error)
1068 report_error(ERR_NONFATAL,
1069 "invalid size specified"
1070 " in COMMON declaration");
1071 else
1072 define_common(value, seg_alloc(), size,
1073 special, ofmt, report_error);
1074 } else
1075 report_error(ERR_NONFATAL,
1076 "no size specified in"
1077 " COMMON declaration");
1078 } else if (pass0 == 2) { /* pass == 2 */
1079 q = value;
1080 while (*q && *q != ':') {
1081 if (isspace(*q))
1082 *q = '\0';
1083 q++;
1085 if (*q == ':') {
1086 *q++ = '\0';
1087 ofmt->symdef(value, 0L, 0L, 3, q);
1090 break;
1091 case D_ABSOLUTE: /* [ABSOLUTE address] */
1092 stdscan_reset();
1093 stdscan_bufptr = value;
1094 tokval.t_type = TOKEN_INVALID;
1095 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
1096 report_error, NULL);
1097 if (e) {
1098 if (!is_reloc(e))
1099 report_error(pass0 ==
1100 1 ? ERR_NONFATAL : ERR_PANIC,
1101 "cannot use non-relocatable expression as "
1102 "ABSOLUTE address");
1103 else {
1104 abs_seg = reloc_seg(e);
1105 abs_offset = reloc_value(e);
1107 } else if (pass == 1)
1108 abs_offset = 0x100; /* don't go near zero in case of / */
1109 else
1110 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1111 "in pass two");
1112 in_abs_seg = true;
1113 location.segment = NO_SEG;
1114 break;
1115 case D_DEBUG: /* [DEBUG] */
1116 p = value;
1117 q = debugid;
1118 validid = true;
1119 if (!isidstart(*p))
1120 validid = false;
1121 while (*p && !isspace(*p)) {
1122 if (!isidchar(*p))
1123 validid = false;
1124 *q++ = *p++;
1126 *q++ = 0;
1127 if (!validid) {
1128 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1129 "identifier expected after DEBUG");
1130 break;
1132 while (*p && isspace(*p))
1133 p++;
1134 if (pass == pass_max)
1135 ofmt->current_dfmt->debug_directive(debugid, p);
1136 break;
1137 case D_WARNING: /* [WARNING {+|-}warn-name] */
1138 if (pass1 == 1) {
1139 while (*value && isspace(*value))
1140 value++;
1142 if (*value == '+' || *value == '-') {
1143 validid = (*value == '-') ? true : false;
1144 value++;
1145 } else
1146 validid = false;
1148 for (i = 1; i <= ERR_WARN_MAX; i++)
1149 if (!nasm_stricmp(value, suppressed_names[i]))
1150 break;
1151 if (i <= ERR_WARN_MAX)
1152 suppressed[i] = validid;
1153 else
1154 report_error(ERR_NONFATAL,
1155 "invalid warning id in WARNING directive");
1157 break;
1158 case D_CPU: /* [CPU] */
1159 cpu = get_cpu(value);
1160 break;
1161 case D_LIST: /* [LIST {+|-}] */
1162 while (*value && isspace(*value))
1163 value++;
1165 if (*value == '+') {
1166 user_nolist = 0;
1167 } else {
1168 if (*value == '-') {
1169 user_nolist = 1;
1170 } else {
1171 err = 1;
1174 break;
1175 case D_DEFAULT: /* [DEFAULT] */
1176 stdscan_reset();
1177 stdscan_bufptr = value;
1178 tokval.t_type = TOKEN_INVALID;
1179 if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
1180 switch ((int)tokval.t_integer) {
1181 case S_REL:
1182 globalrel = 1;
1183 break;
1184 case S_ABS:
1185 globalrel = 0;
1186 break;
1187 default:
1188 err = 1;
1189 break;
1191 } else {
1192 err = 1;
1194 break;
1195 case D_FLOAT:
1196 if (float_option(value)) {
1197 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1198 "unknown 'float' directive: %s",
1199 value);
1201 break;
1202 default:
1203 if (!ofmt->directive(directive, value, pass2))
1204 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1205 "unrecognised directive [%s]",
1206 directive);
1208 if (err) {
1209 report_error(ERR_NONFATAL,
1210 "invalid parameter to [%s] directive",
1211 directive);
1213 } else { /* it isn't a directive */
1215 parse_line(pass1, line, &output_ins,
1216 report_error, evaluate, def_label);
1218 if (!(optimizing > 0) && pass == 2) {
1219 if (forwref != NULL && globallineno == forwref->lineno) {
1220 output_ins.forw_ref = true;
1221 do {
1222 output_ins.oprs[forwref->operand].opflags |=
1223 OPFLAG_FORWARD;
1224 forwref = saa_rstruct(forwrefs);
1225 } while (forwref != NULL
1226 && forwref->lineno == globallineno);
1227 } else
1228 output_ins.forw_ref = false;
1231 if (!(optimizing > 0) && output_ins.forw_ref) {
1232 if (pass == 1) {
1233 for (i = 0; i < output_ins.operands; i++) {
1234 if (output_ins.oprs[i].
1235 opflags & OPFLAG_FORWARD) {
1236 struct forwrefinfo *fwinf =
1237 (struct forwrefinfo *)
1238 saa_wstruct(forwrefs);
1239 fwinf->lineno = globallineno;
1240 fwinf->operand = i;
1243 } else { /* pass == 2 */
1245 * Hack to prevent phase error in the code
1246 * rol ax,x
1247 * x equ 1
1249 * If the second operand is a forward reference,
1250 * the UNITY property of the number 1 in that
1251 * operand is cancelled. Otherwise the above
1252 * sequence will cause a phase error.
1254 * This hack means that the above code will
1255 * generate 286+ code.
1257 * The forward reference will mean that the
1258 * operand will not have the UNITY property on
1259 * the first pass, so the pass behaviours will
1260 * be consistent.
1263 if (output_ins.operands >= 2 &&
1264 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1265 !(IMMEDIATE & ~output_ins.oprs[1].type))
1267 /* Remove special properties bits */
1268 output_ins.oprs[1].type &= ~REG_SMASK;
1271 } /* pass == 2 */
1275 /* forw_ref */
1276 if (output_ins.opcode == I_EQU) {
1277 if (pass1 == 1) {
1279 * Special `..' EQUs get processed in pass two,
1280 * except `..@' macro-processor EQUs which are done
1281 * in the normal place.
1283 if (!output_ins.label)
1284 report_error(ERR_NONFATAL,
1285 "EQU not preceded by label");
1287 else if (output_ins.label[0] != '.' ||
1288 output_ins.label[1] != '.' ||
1289 output_ins.label[2] == '@') {
1290 if (output_ins.operands == 1 &&
1291 (output_ins.oprs[0].type & IMMEDIATE) &&
1292 output_ins.oprs[0].wrt == NO_SEG) {
1293 int isext =
1294 output_ins.oprs[0].
1295 opflags & OPFLAG_EXTERN;
1296 def_label(output_ins.label,
1297 output_ins.oprs[0].segment,
1298 output_ins.oprs[0].offset, NULL,
1299 false, isext, ofmt,
1300 report_error);
1301 } else if (output_ins.operands == 2
1302 && (output_ins.oprs[0].
1303 type & IMMEDIATE)
1304 && (output_ins.oprs[0].type & COLON)
1305 && output_ins.oprs[0].segment ==
1306 NO_SEG
1307 && output_ins.oprs[0].wrt == NO_SEG
1308 && (output_ins.oprs[1].
1309 type & IMMEDIATE)
1310 && output_ins.oprs[1].segment ==
1311 NO_SEG
1312 && output_ins.oprs[1].wrt ==
1313 NO_SEG) {
1314 def_label(output_ins.label,
1315 output_ins.oprs[0].
1316 offset | SEG_ABS,
1317 output_ins.oprs[1].offset, NULL,
1318 false, false, ofmt,
1319 report_error);
1320 } else
1321 report_error(ERR_NONFATAL,
1322 "bad syntax for EQU");
1324 } else { /* pass == 2 */
1326 * Special `..' EQUs get processed here, except
1327 * `..@' macro processor EQUs which are done above.
1329 if (output_ins.label[0] == '.' &&
1330 output_ins.label[1] == '.' &&
1331 output_ins.label[2] != '@') {
1332 if (output_ins.operands == 1 &&
1333 (output_ins.oprs[0].type & IMMEDIATE)) {
1334 define_label(output_ins.label,
1335 output_ins.oprs[0].segment,
1336 output_ins.oprs[0].offset,
1337 NULL, false, false, ofmt,
1338 report_error);
1339 } else if (output_ins.operands == 2
1340 && (output_ins.oprs[0].
1341 type & IMMEDIATE)
1342 && (output_ins.oprs[0].type & COLON)
1343 && output_ins.oprs[0].segment ==
1344 NO_SEG
1345 && (output_ins.oprs[1].
1346 type & IMMEDIATE)
1347 && output_ins.oprs[1].segment ==
1348 NO_SEG) {
1349 define_label(output_ins.label,
1350 output_ins.oprs[0].
1351 offset | SEG_ABS,
1352 output_ins.oprs[1].offset,
1353 NULL, false, false, ofmt,
1354 report_error);
1355 } else
1356 report_error(ERR_NONFATAL,
1357 "bad syntax for EQU");
1359 } /* pass == 2 */
1360 } else { /* instruction isn't an EQU */
1362 if (pass1 == 1) {
1364 int64_t l = insn_size(location.segment, offs, sb, cpu,
1365 &output_ins, report_error);
1367 /* if (using_debug_info) && output_ins.opcode != -1) */
1368 if (using_debug_info)
1369 { /* fbk 03/25/01 */
1370 /* this is done here so we can do debug type info */
1371 int32_t typeinfo =
1372 TYS_ELEMENTS(output_ins.operands);
1373 switch (output_ins.opcode) {
1374 case I_RESB:
1375 typeinfo =
1376 TYS_ELEMENTS(output_ins.oprs[0].
1377 offset) | TY_BYTE;
1378 break;
1379 case I_RESW:
1380 typeinfo =
1381 TYS_ELEMENTS(output_ins.oprs[0].
1382 offset) | TY_WORD;
1383 break;
1384 case I_RESD:
1385 typeinfo =
1386 TYS_ELEMENTS(output_ins.oprs[0].
1387 offset) | TY_DWORD;
1388 break;
1389 case I_RESQ:
1390 typeinfo =
1391 TYS_ELEMENTS(output_ins.oprs[0].
1392 offset) | TY_QWORD;
1393 break;
1394 case I_REST:
1395 typeinfo =
1396 TYS_ELEMENTS(output_ins.oprs[0].
1397 offset) | TY_TBYTE;
1398 break;
1399 case I_DB:
1400 typeinfo |= TY_BYTE;
1401 break;
1402 case I_DW:
1403 typeinfo |= TY_WORD;
1404 break;
1405 case I_DD:
1406 if (output_ins.eops_float)
1407 typeinfo |= TY_FLOAT;
1408 else
1409 typeinfo |= TY_DWORD;
1410 break;
1411 case I_DQ:
1412 typeinfo |= TY_QWORD;
1413 break;
1414 case I_DT:
1415 typeinfo |= TY_TBYTE;
1416 break;
1417 case I_DO:
1418 typeinfo |= TY_OWORD;
1419 break;
1420 default:
1421 typeinfo = TY_LABEL;
1425 ofmt->current_dfmt->debug_typevalue(typeinfo);
1428 if (l != -1) {
1429 offs += l;
1430 SET_CURR_OFFS(offs);
1433 * else l == -1 => invalid instruction, which will be
1434 * flagged as an error on pass 2
1437 } else { /* pass == 2 */
1438 offs += assemble(location.segment, offs, sb, cpu,
1439 &output_ins, ofmt, report_error,
1440 &nasmlist);
1441 SET_CURR_OFFS(offs);
1444 } /* not an EQU */
1445 cleanup_insn(&output_ins);
1447 nasm_free(line);
1448 location.offset = offs = GET_CURR_OFFS;
1449 } /* end while (line = preproc->getline... */
1451 if (pass1 == 2 && global_offset_changed)
1452 report_error(ERR_NONFATAL,
1453 "phase error detected at end of assembly.");
1455 if (pass1 == 1)
1456 preproc->cleanup(1);
1458 if (pass1 == 1 && terminate_after_phase) {
1459 fclose(ofile);
1460 remove(outname);
1461 if (want_usage)
1462 usage();
1463 exit(1);
1465 pass_cnt++;
1466 if (pass > 1 && !global_offset_changed) {
1467 pass0++;
1468 if (pass0 == 2)
1469 pass = pass_max - 1;
1470 } else if (!(optimizing > 0))
1471 pass0++;
1473 } /* for (pass=1; pass<=2; pass++) */
1475 preproc->cleanup(0);
1476 nasmlist.cleanup();
1477 #if 1
1478 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1479 fprintf(stdout,
1480 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1481 #endif
1482 } /* exit from assemble_file (...) */
1484 static enum directives getkw(char **directive, char **value)
1486 char *p, *q, *buf;
1488 buf = *directive;
1490 /* allow leading spaces or tabs */
1491 while (*buf == ' ' || *buf == '\t')
1492 buf++;
1494 if (*buf != '[')
1495 return 0;
1497 p = buf;
1499 while (*p && *p != ']')
1500 p++;
1502 if (!*p)
1503 return 0;
1505 q = p++;
1507 while (*p && *p != ';') {
1508 if (!isspace(*p))
1509 return 0;
1510 p++;
1512 q[1] = '\0';
1514 *directive = p = buf + 1;
1515 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1516 buf++;
1517 if (*buf == ']') {
1518 *buf = '\0';
1519 *value = buf;
1520 } else {
1521 *buf++ = '\0';
1522 while (isspace(*buf))
1523 buf++; /* beppu - skip leading whitespace */
1524 *value = buf;
1525 while (*buf != ']')
1526 buf++;
1527 *buf++ = '\0';
1530 return bsii(*directive, directives, elements(directives));
1534 * gnu style error reporting
1535 * This function prints an error message to error_file in the
1536 * style used by GNU. An example would be:
1537 * file.asm:50: error: blah blah blah
1538 * where file.asm is the name of the file, 50 is the line number on
1539 * which the error occurs (or is detected) and "error:" is one of
1540 * the possible optional diagnostics -- it can be "error" or "warning"
1541 * or something else. Finally the line terminates with the actual
1542 * error message.
1544 * @param severity the severity of the warning or error
1545 * @param fmt the printf style format string
1547 static void report_error_gnu(int severity, const char *fmt, ...)
1549 va_list ap;
1551 if (is_suppressed_warning(severity))
1552 return;
1554 if (severity & ERR_NOFILE)
1555 fputs("nasm: ", error_file);
1556 else {
1557 char *currentfile = NULL;
1558 int32_t lineno = 0;
1559 src_get(&lineno, &currentfile);
1560 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1561 nasm_free(currentfile);
1563 va_start(ap, fmt);
1564 report_error_common(severity, fmt, ap);
1565 va_end(ap);
1569 * MS style error reporting
1570 * This function prints an error message to error_file in the
1571 * style used by Visual C and some other Microsoft tools. An example
1572 * would be:
1573 * file.asm(50) : error: blah blah blah
1574 * where file.asm is the name of the file, 50 is the line number on
1575 * which the error occurs (or is detected) and "error:" is one of
1576 * the possible optional diagnostics -- it can be "error" or "warning"
1577 * or something else. Finally the line terminates with the actual
1578 * error message.
1580 * @param severity the severity of the warning or error
1581 * @param fmt the printf style format string
1583 static void report_error_vc(int severity, const char *fmt, ...)
1585 va_list ap;
1587 if (is_suppressed_warning(severity))
1588 return;
1590 if (severity & ERR_NOFILE)
1591 fputs("nasm: ", error_file);
1592 else {
1593 char *currentfile = NULL;
1594 int32_t lineno = 0;
1595 src_get(&lineno, &currentfile);
1596 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1597 nasm_free(currentfile);
1599 va_start(ap, fmt);
1600 report_error_common(severity, fmt, ap);
1601 va_end(ap);
1605 * check for supressed warning
1606 * checks for suppressed warning or pass one only warning and we're
1607 * not in pass 1
1609 * @param severity the severity of the warning or error
1610 * @return true if we should abort error/warning printing
1612 static int is_suppressed_warning(int severity)
1615 * See if it's a suppressed warning.
1617 return ((severity & ERR_MASK) == ERR_WARNING &&
1618 (severity & ERR_WARN_MASK) != 0 &&
1619 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1621 * See if it's a pass-one only warning and we're not in pass one.
1623 ((severity & ERR_PASS1) && pass0 == 2);
1627 * common error reporting
1628 * This is the common back end of the error reporting schemes currently
1629 * implemented. It prints the nature of the warning and then the
1630 * specific error message to error_file and may or may not return. It
1631 * doesn't return if the error severity is a "panic" or "debug" type.
1633 * @param severity the severity of the warning or error
1634 * @param fmt the printf style format string
1636 static void report_error_common(int severity, const char *fmt,
1637 va_list args)
1639 switch (severity & ERR_MASK) {
1640 case ERR_WARNING:
1641 fputs("warning: ", error_file);
1642 break;
1643 case ERR_NONFATAL:
1644 fputs("error: ", error_file);
1645 break;
1646 case ERR_FATAL:
1647 fputs("fatal: ", error_file);
1648 break;
1649 case ERR_PANIC:
1650 fputs("panic: ", error_file);
1651 break;
1652 case ERR_DEBUG:
1653 fputs("debug: ", error_file);
1654 break;
1657 vfprintf(error_file, fmt, args);
1658 putc('\n', error_file);
1660 if (severity & ERR_USAGE)
1661 want_usage = true;
1663 switch (severity & ERR_MASK) {
1664 case ERR_DEBUG:
1665 /* no further action, by definition */
1666 break;
1667 case ERR_WARNING:
1668 if (!suppressed[0]) /* Treat warnings as errors */
1669 terminate_after_phase = true;
1670 break;
1671 case ERR_NONFATAL:
1672 terminate_after_phase = true;
1673 break;
1674 case ERR_FATAL:
1675 if (ofile) {
1676 fclose(ofile);
1677 remove(outname);
1679 if (want_usage)
1680 usage();
1681 exit(1); /* instantly die */
1682 break; /* placate silly compilers */
1683 case ERR_PANIC:
1684 fflush(NULL);
1685 /* abort(); *//* halt, catch fire, and dump core */
1686 exit(3);
1687 break;
1691 static void usage(void)
1693 fputs("type `nasm -h' for help\n", error_file);
1696 static void register_output_formats(void)
1698 ofmt = ofmt_register(report_error);
1701 #define BUF_DELTA 512
1703 static FILE *no_pp_fp;
1704 static efunc no_pp_err;
1705 static ListGen *no_pp_list;
1706 static int32_t no_pp_lineinc;
1708 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1709 ListGen * listgen)
1711 src_set_fname(nasm_strdup(file));
1712 src_set_linnum(0);
1713 no_pp_lineinc = 1;
1714 no_pp_err = error;
1715 no_pp_fp = fopen(file, "r");
1716 if (!no_pp_fp)
1717 no_pp_err(ERR_FATAL | ERR_NOFILE,
1718 "unable to open input file `%s'", file);
1719 no_pp_list = listgen;
1720 (void)pass; /* placate compilers */
1721 (void)eval; /* placate compilers */
1724 static char *no_pp_getline(void)
1726 char *buffer, *p, *q;
1727 int bufsize;
1729 bufsize = BUF_DELTA;
1730 buffer = nasm_malloc(BUF_DELTA);
1731 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1733 while (1) { /* Loop to handle %line */
1735 p = buffer;
1736 while (1) { /* Loop to handle long lines */
1737 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1738 if (!q)
1739 break;
1740 p += strlen(p);
1741 if (p > buffer && p[-1] == '\n')
1742 break;
1743 if (p - buffer > bufsize - 10) {
1744 int offset;
1745 offset = p - buffer;
1746 bufsize += BUF_DELTA;
1747 buffer = nasm_realloc(buffer, bufsize);
1748 p = buffer + offset;
1752 if (!q && p == buffer) {
1753 nasm_free(buffer);
1754 return NULL;
1758 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1759 * them are present at the end of the line.
1761 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1763 if (!nasm_strnicmp(buffer, "%line", 5)) {
1764 int32_t ln;
1765 int li;
1766 char *nm = nasm_malloc(strlen(buffer));
1767 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1768 nasm_free(src_set_fname(nm));
1769 src_set_linnum(ln);
1770 no_pp_lineinc = li;
1771 continue;
1773 nasm_free(nm);
1775 break;
1778 no_pp_list->line(LIST_READ, buffer);
1780 return buffer;
1783 static void no_pp_cleanup(int pass)
1785 (void)pass; /* placate GCC */
1786 fclose(no_pp_fp);
1789 static uint32_t get_cpu(char *value)
1791 if (!strcmp(value, "8086"))
1792 return IF_8086;
1793 if (!strcmp(value, "186"))
1794 return IF_186;
1795 if (!strcmp(value, "286"))
1796 return IF_286;
1797 if (!strcmp(value, "386"))
1798 return IF_386;
1799 if (!strcmp(value, "486"))
1800 return IF_486;
1801 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1802 return IF_PENT;
1803 if (!strcmp(value, "686") ||
1804 !nasm_stricmp(value, "ppro") ||
1805 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1806 return IF_P6;
1807 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1808 return IF_KATMAI;
1809 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1810 !nasm_stricmp(value, "willamette"))
1811 return IF_WILLAMETTE;
1812 if (!nasm_stricmp(value, "prescott"))
1813 return IF_PRESCOTT;
1814 if (!nasm_stricmp(value, "x64") ||
1815 !nasm_stricmp(value, "x86-64"))
1816 return IF_X86_64;
1817 if (!nasm_stricmp(value, "ia64") ||
1818 !nasm_stricmp(value, "ia-64") ||
1819 !nasm_stricmp(value, "itanium") ||
1820 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1821 return IF_IA64;
1823 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1824 "unknown 'cpu' type");
1826 return IF_PLEVEL; /* the maximum level */
1829 static int get_bits(char *value)
1831 int i;
1833 if ((i = atoi(value)) == 16)
1834 return i; /* set for a 16-bit segment */
1835 else if (i == 32) {
1836 if (cpu < IF_386) {
1837 report_error(ERR_NONFATAL,
1838 "cannot specify 32-bit segment on processor below a 386");
1839 i = 16;
1841 } else if (i == 64) {
1842 if (cpu < IF_X86_64) {
1843 report_error(ERR_NONFATAL,
1844 "cannot specify 64-bit segment on processor below an x86-64");
1845 i = 16;
1847 if (i != maxbits) {
1848 report_error(ERR_NONFATAL,
1849 "%s output format does not support 64-bit code",
1850 ofmt->shortname);
1851 i = 16;
1853 } else {
1854 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1855 "`%s' is not a valid segment size; must be 16, 32 or 64",
1856 value);
1857 i = 16;
1859 return i;
1862 /* end of nasm.c */