Apply Nindent to all .c and .h files
[nasm/avx512.git] / nasm.c
blob2157286879fbe35a797a53d193e4bd77c5cfa3fa
1 /* The Netwide Assembler main program module
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 */
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "insns.h"
18 #include "preproc.h"
19 #include "parser.h"
20 #include "eval.h"
21 #include "assemble.h"
22 #include "labels.h"
23 #include "outform.h"
24 #include "listing.h"
26 struct forwrefinfo { /* info held on forward refs. */
27 int lineno;
28 int operand;
31 static int get_bits(char *value);
32 static unsigned long get_cpu(char *cpu_str);
33 static void parse_cmdline(int, char **);
34 static void assemble_file(char *);
35 static int getkw(char **directive, char **value);
36 static void register_output_formats(void);
37 static void report_error_gnu(int severity, const char *fmt, ...);
38 static void report_error_vc(int severity, const char *fmt, ...);
39 static void report_error_common(int severity, const char *fmt,
40 va_list args);
41 static int is_suppressed_warning(int severity);
42 static void usage(void);
43 static efunc report_error;
45 static int using_debug_info, opt_verbose_info;
46 int tasm_compatible_mode = FALSE;
47 int pass0;
49 static char inname[FILENAME_MAX];
50 static char outname[FILENAME_MAX];
51 static char listname[FILENAME_MAX];
52 static int globallineno; /* for forward-reference tracking */
53 /* static int pass = 0; */
54 static struct ofmt *ofmt = NULL;
56 static FILE *error_file; /* Where to write error messages */
58 static FILE *ofile = NULL;
59 int optimizing = -1; /* number of optimization passes to take */
60 static int sb, cmd_sb = 16; /* by default */
61 static unsigned long cmd_cpu = IF_PLEVEL; /* highest level by default */
62 static unsigned long cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
63 int global_offset_changed; /* referenced in labels.c */
65 static loc_t location;
66 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
67 long abs_seg; /* ABSOLUTE segment basis */
68 long abs_offset; /* ABSOLUTE offset */
70 static struct RAA *offsets;
72 static struct SAA *forwrefs; /* keep track of forward references */
73 static struct forwrefinfo *forwref;
75 static Preproc *preproc;
76 enum op_type {
77 op_normal, /* Preprocess and assemble */
78 op_preprocess, /* Preprocess only */
79 op_depend /* Generate dependencies */
81 static enum op_type operating_mode;
84 * Which of the suppressible warnings are suppressed. Entry zero
85 * doesn't do anything. Initial defaults are given here.
87 static char suppressed[1 + ERR_WARN_MAX] = {
88 0, TRUE, TRUE, TRUE, FALSE, TRUE
92 * The option names for the suppressible warnings. As before, entry
93 * zero does nothing.
95 static const char *suppressed_names[1 + ERR_WARN_MAX] = {
96 NULL, "macro-params", "macro-selfref", "orphan-labels",
97 "number-overflow",
98 "gnu-elf-extensions"
102 * The explanations for the suppressible warnings. As before, entry
103 * zero does nothing.
105 static const char *suppressed_what[1 + ERR_WARN_MAX] = {
106 NULL,
107 "macro calls with wrong no. of params",
108 "cyclic macro self-references",
109 "labels alone on lines without trailing `:'",
110 "numeric constants greater than 0xFFFFFFFF",
111 "using 8- or 16-bit relocation in ELF, a GNU extension"
115 * This is a null preprocessor which just copies lines from input
116 * to output. It's used when someone explicitly requests that NASM
117 * not preprocess their source file.
120 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
121 static char *no_pp_getline(void);
122 static void no_pp_cleanup(int);
123 static Preproc no_pp = {
124 no_pp_reset,
125 no_pp_getline,
126 no_pp_cleanup
130 * get/set current offset...
132 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
133 raa_read(offsets,location.segment))
134 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
135 (void)(offsets=raa_write(offsets,location.segment,(x))))
137 static int want_usage;
138 static int terminate_after_phase;
139 int user_nolist = 0; /* fbk 9/2/00 */
141 static void nasm_fputs(const char *line, FILE * outfile)
143 if (outfile) {
144 fputs(line, outfile);
145 fputc('\n', outfile);
146 } else
147 puts(line);
150 int main(int argc, char **argv)
152 pass0 = 1;
153 want_usage = terminate_after_phase = FALSE;
154 report_error = report_error_gnu;
156 nasm_set_malloc_error(report_error);
157 offsets = raa_init();
158 forwrefs = saa_init((long)sizeof(struct forwrefinfo));
160 preproc = &nasmpp;
161 operating_mode = op_normal;
163 error_file = stderr;
165 seg_init();
167 register_output_formats();
169 parse_cmdline(argc, argv);
171 if (terminate_after_phase) {
172 if (want_usage)
173 usage();
174 return 1;
177 /* If debugging info is disabled, suppress any debug calls */
178 if (!using_debug_info)
179 ofmt->current_dfmt = &null_debug_form;
181 if (ofmt->stdmac)
182 pp_extra_stdmac(ofmt->stdmac);
183 parser_global_info(ofmt, &location);
184 eval_global_info(ofmt, lookup_label, &location);
186 /* define some macros dependent of command-line */
188 char temp[64];
189 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
190 ofmt->shortname);
191 pp_pre_define(temp);
194 switch (operating_mode) {
195 case op_depend:
197 char *line;
198 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
199 if (outname[0] == '\0')
200 ofmt->filename(inname, outname, report_error);
201 ofile = NULL;
202 fprintf(stdout, "%s: %s", outname, inname);
203 while ((line = preproc->getline()))
204 nasm_free(line);
205 preproc->cleanup(0);
206 putc('\n', stdout);
208 break;
210 case op_preprocess:
212 char *line;
213 char *file_name = NULL;
214 long prior_linnum = 0;
215 int lineinc = 0;
217 if (*outname) {
218 ofile = fopen(outname, "w");
219 if (!ofile)
220 report_error(ERR_FATAL | ERR_NOFILE,
221 "unable to open output file `%s'",
222 outname);
223 } else
224 ofile = NULL;
226 location.known = FALSE;
228 /* pass = 1; */
229 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
230 while ((line = preproc->getline())) {
232 * We generate %line directives if needed for later programs
234 long linnum = prior_linnum += lineinc;
235 int altline = src_get(&linnum, &file_name);
236 if (altline) {
237 if (altline == 1 && lineinc == 1)
238 nasm_fputs("", ofile);
239 else {
240 lineinc = (altline != -1 || lineinc != 1);
241 fprintf(ofile ? ofile : stdout,
242 "%%line %ld+%d %s\n", linnum, lineinc,
243 file_name);
245 prior_linnum = linnum;
247 nasm_fputs(line, ofile);
248 nasm_free(line);
250 nasm_free(file_name);
251 preproc->cleanup(0);
252 if (ofile)
253 fclose(ofile);
254 if (ofile && terminate_after_phase)
255 remove(outname);
257 break;
259 case op_normal:
262 * We must call ofmt->filename _anyway_, even if the user
263 * has specified their own output file, because some
264 * formats (eg OBJ and COFF) use ofmt->filename to find out
265 * the name of the input file and then put that inside the
266 * file.
268 ofmt->filename(inname, outname, report_error);
270 ofile = fopen(outname, "wb");
271 if (!ofile) {
272 report_error(ERR_FATAL | ERR_NOFILE,
273 "unable to open output file `%s'", outname);
277 * We must call init_labels() before ofmt->init() since
278 * some object formats will want to define labels in their
279 * init routines. (eg OS/2 defines the FLAT group)
281 init_labels();
283 ofmt->init(ofile, report_error, define_label, evaluate);
285 assemble_file(inname);
287 if (!terminate_after_phase) {
288 ofmt->cleanup(using_debug_info);
289 cleanup_labels();
290 } else {
292 * We had an fclose on the output file here, but we
293 * actually do that in all the object file drivers as well,
294 * so we're leaving out the one here.
295 * fclose (ofile);
297 remove(outname);
298 if (listname[0])
299 remove(listname);
302 break;
305 if (want_usage)
306 usage();
308 raa_free(offsets);
309 saa_free(forwrefs);
310 eval_cleanup();
311 nasmlib_cleanup();
313 if (terminate_after_phase)
314 return 1;
315 else
316 return 0;
320 * Get a parameter for a command line option.
321 * First arg must be in the form of e.g. -f...
323 static char *get_param(char *p, char *q, int *advance)
325 *advance = 0;
326 if (p[2]) { /* the parameter's in the option */
327 p += 2;
328 while (isspace(*p))
329 p++;
330 return p;
332 if (q && q[0]) {
333 *advance = 1;
334 return q;
336 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
337 "option `-%c' requires an argument", p[1]);
338 return NULL;
341 struct textargs {
342 const char *label;
343 int value;
346 #define OPT_PREFIX 0
347 #define OPT_POSTFIX 1
348 struct textargs textopts[] = {
349 {"prefix", OPT_PREFIX},
350 {"postfix", OPT_POSTFIX},
351 {NULL, 0}
354 int stopoptions = 0;
355 static int process_arg(char *p, char *q)
357 char *param;
358 int i, advance = 0;
360 if (!p || !p[0])
361 return 0;
363 if (p[0] == '-' && !stopoptions) {
364 switch (p[1]) {
365 case 's':
366 error_file = stdout;
367 break;
368 case 'o': /* these parameters take values */
369 case 'O':
370 case 'f':
371 case 'p':
372 case 'P':
373 case 'd':
374 case 'D':
375 case 'i':
376 case 'I':
377 case 'l':
378 case 'E':
379 case 'F':
380 case 'X':
381 case 'u':
382 case 'U':
383 if (!(param = get_param(p, q, &advance)))
384 break;
385 if (p[1] == 'o') { /* output file */
386 strcpy(outname, param);
387 } else if (p[1] == 'f') { /* output format */
388 ofmt = ofmt_find(param);
389 if (!ofmt) {
390 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
391 "unrecognised output format `%s' - "
392 "use -hf for a list", param);
393 } else
394 ofmt->current_dfmt = ofmt->debug_formats[0];
395 } else if (p[1] == 'O') { /* Optimization level */
396 int opt;
397 opt = -99;
398 while (*param) {
399 if (isdigit(*param)) {
400 opt = atoi(param);
401 while (isdigit(*++param)) ;
402 if (opt <= 0)
403 optimizing = -1; /* 0.98 behaviour */
404 else if (opt == 1)
405 optimizing = 0; /* Two passes, 0.98.09 behavior */
406 else
407 optimizing = opt; /* Multiple passes */
408 } else {
409 if (*param == 'v' || *param == '+') {
410 ++param;
411 opt_verbose_info = TRUE;
412 opt = 0;
413 } else { /* garbage */
414 opt = -99;
415 break;
418 } /* while (*param) */
419 if (opt == -99)
420 report_error(ERR_FATAL,
421 "command line optimization level must be 'v', 0..3 or <nn>");
422 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
423 pp_pre_include(param);
424 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
425 pp_pre_define(param);
426 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
427 pp_pre_undefine(param);
428 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
429 pp_include_path(param);
430 } else if (p[1] == 'l') { /* listing file */
431 strcpy(listname, param);
432 } else if (p[1] == 'E') { /* error messages file */
433 error_file = fopen(param, "w");
434 if (!error_file) {
435 error_file = stderr; /* Revert to default! */
436 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
437 "cannot open file `%s' for error messages",
438 param);
440 } else if (p[1] == 'F') { /* specify debug format */
441 ofmt->current_dfmt = dfmt_find(ofmt, param);
442 if (!ofmt->current_dfmt) {
443 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
444 "unrecognized debug format `%s' for"
445 " output format `%s'",
446 param, ofmt->shortname);
448 } else if (p[1] == 'X') { /* specify error reporting format */
449 if (nasm_stricmp("vc", param) == 0)
450 report_error = report_error_vc;
451 else if (nasm_stricmp("gnu", param) == 0)
452 report_error = report_error_gnu;
453 else
454 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
455 "unrecognized error reporting format `%s'",
456 param);
458 break;
459 case 'g':
460 using_debug_info = TRUE;
461 break;
462 case 'h':
463 printf
464 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
465 "[-l listfile]\n"
466 " [options...] [--] filename\n"
467 " or nasm -r for version info (obsolete)\n"
468 " or nasm -v for version info (preferred)\n\n"
469 " -t assemble in SciTech TASM compatible mode\n"
470 " -g generate debug information in selected format.\n");
471 printf
472 (" -e preprocess only (writes output to stdout by default)\n"
473 " -a don't preprocess (assemble only)\n"
474 " -M generate Makefile dependencies on stdout\n\n"
475 " -E<file> redirect error messages to file\n"
476 " -s redirect error messages to stdout\n\n"
477 " -F format select a debugging format\n\n"
478 " -I<path> adds a pathname to the include file path\n");
479 printf
480 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
481 " -P<file> pre-includes a file\n"
482 " -D<macro>[=<value>] pre-defines a macro\n"
483 " -U<macro> undefines a macro\n"
484 " -X<format> specifies error reporting format (gnu or vc)\n"
485 " -w+foo enables warnings about foo; -w-foo disables them\n"
486 "where foo can be:\n");
487 for (i = 1; i <= ERR_WARN_MAX; i++)
488 printf(" %-23s %s (default %s)\n",
489 suppressed_names[i], suppressed_what[i],
490 suppressed[i] ? "off" : "on");
491 printf
492 ("\nresponse files should contain command line parameters"
493 ", one per line.\n");
494 if (p[2] == 'f') {
495 printf("\nvalid output formats for -f are"
496 " (`*' denotes default):\n");
497 ofmt_list(ofmt, stdout);
498 } else {
499 printf("\nFor a list of valid output formats, use -hf.\n");
500 printf("For a list of debug formats, use -f <form> -y.\n");
502 exit(0); /* never need usage message here */
503 break;
504 case 'y':
505 printf("\nvalid debug formats for '%s' output format are"
506 " ('*' denotes default):\n", ofmt->shortname);
507 dfmt_list(ofmt, stdout);
508 exit(0);
509 break;
510 case 't':
511 tasm_compatible_mode = TRUE;
512 break;
513 case 'r':
514 case 'v':
516 const char *nasm_version_string =
517 "NASM version " NASM_VER " compiled on " __DATE__
518 #ifdef DEBUG
519 " with -DDEBUG"
520 #endif
522 puts(nasm_version_string);
523 exit(0); /* never need usage message here */
525 break;
526 case 'e': /* preprocess only */
527 operating_mode = op_preprocess;
528 break;
529 case 'a': /* assemble only - don't preprocess */
530 preproc = &no_pp;
531 break;
532 case 'w':
533 if (p[2] != '+' && p[2] != '-') {
534 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
535 "invalid option to `-w'");
536 } else {
537 for (i = 1; i <= ERR_WARN_MAX; i++)
538 if (!nasm_stricmp(p + 3, suppressed_names[i]))
539 break;
540 if (i <= ERR_WARN_MAX)
541 suppressed[i] = (p[2] == '-');
542 else
543 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
544 "invalid option to `-w'");
546 break;
547 case 'M':
548 operating_mode = op_depend;
549 break;
551 case '-':
553 int s;
555 if (p[2] == 0) { /* -- => stop processing options */
556 stopoptions = 1;
557 break;
559 for (s = 0; textopts[s].label; s++) {
560 if (!nasm_stricmp(p + 2, textopts[s].label)) {
561 break;
565 switch (s) {
567 case OPT_PREFIX:
568 case OPT_POSTFIX:
570 if (!q) {
571 report_error(ERR_NONFATAL | ERR_NOFILE |
572 ERR_USAGE,
573 "option `--%s' requires an argument",
574 p + 2);
575 break;
576 } else {
577 advance = 1, param = q;
580 if (s == OPT_PREFIX) {
581 strncpy(lprefix, param, PREFIX_MAX - 1);
582 lprefix[PREFIX_MAX - 1] = 0;
583 break;
585 if (s == OPT_POSTFIX) {
586 strncpy(lpostfix, param, POSTFIX_MAX - 1);
587 lpostfix[POSTFIX_MAX - 1] = 0;
588 break;
590 break;
592 default:
594 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
595 "unrecognised option `--%s'", p + 2);
596 break;
599 break;
602 default:
603 if (!ofmt->setinfo(GI_SWITCH, &p))
604 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
605 "unrecognised option `-%c'", p[1]);
606 break;
608 } else {
609 if (*inname) {
610 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
611 "more than one input file specified");
612 } else
613 strcpy(inname, p);
616 return advance;
619 #define ARG_BUF_DELTA 128
621 static void process_respfile(FILE * rfile)
623 char *buffer, *p, *q, *prevarg;
624 int bufsize, prevargsize;
626 bufsize = prevargsize = ARG_BUF_DELTA;
627 buffer = nasm_malloc(ARG_BUF_DELTA);
628 prevarg = nasm_malloc(ARG_BUF_DELTA);
629 prevarg[0] = '\0';
631 while (1) { /* Loop to handle all lines in file */
633 p = buffer;
634 while (1) { /* Loop to handle long lines */
635 q = fgets(p, bufsize - (p - buffer), rfile);
636 if (!q)
637 break;
638 p += strlen(p);
639 if (p > buffer && p[-1] == '\n')
640 break;
641 if (p - buffer > bufsize - 10) {
642 int offset;
643 offset = p - buffer;
644 bufsize += ARG_BUF_DELTA;
645 buffer = nasm_realloc(buffer, bufsize);
646 p = buffer + offset;
650 if (!q && p == buffer) {
651 if (prevarg[0])
652 process_arg(prevarg, NULL);
653 nasm_free(buffer);
654 nasm_free(prevarg);
655 return;
659 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
660 * them are present at the end of the line.
662 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
664 while (p > buffer && isspace(p[-1]))
665 *--p = '\0';
667 p = buffer;
668 while (isspace(*p))
669 p++;
671 if (process_arg(prevarg, p))
672 *p = '\0';
674 if (strlen(p) > prevargsize - 10) {
675 prevargsize += ARG_BUF_DELTA;
676 prevarg = nasm_realloc(prevarg, prevargsize);
678 strcpy(prevarg, p);
682 /* Function to process args from a string of args, rather than the
683 * argv array. Used by the environment variable and response file
684 * processing.
686 static void process_args(char *args)
688 char *p, *q, *arg, *prevarg;
689 char separator = ' ';
691 p = args;
692 if (*p && *p != '-')
693 separator = *p++;
694 arg = NULL;
695 while (*p) {
696 q = p;
697 while (*p && *p != separator)
698 p++;
699 while (*p == separator)
700 *p++ = '\0';
701 prevarg = arg;
702 arg = q;
703 if (process_arg(prevarg, arg))
704 arg = NULL;
706 if (arg)
707 process_arg(arg, NULL);
710 static void parse_cmdline(int argc, char **argv)
712 FILE *rfile;
713 char *envreal, *envcopy = NULL, *p, *arg;
715 *inname = *outname = *listname = '\0';
718 * First, process the NASMENV environment variable.
720 envreal = getenv("NASMENV");
721 arg = NULL;
722 if (envreal) {
723 envcopy = nasm_strdup(envreal);
724 process_args(envcopy);
725 nasm_free(envcopy);
729 * Now process the actual command line.
731 while (--argc) {
732 int i;
733 argv++;
734 if (argv[0][0] == '@') {
735 /* We have a response file, so process this as a set of
736 * arguments like the environment variable. This allows us
737 * to have multiple arguments on a single line, which is
738 * different to the -@resp file processing below for regular
739 * NASM.
741 char *str = malloc(2048);
742 FILE *f = fopen(&argv[0][1], "r");
743 if (!str) {
744 printf("out of memory");
745 exit(-1);
747 if (f) {
748 while (fgets(str, 2048, f)) {
749 process_args(str);
751 fclose(f);
753 free(str);
754 argc--;
755 argv++;
757 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
758 if ((p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i))) {
759 if ((rfile = fopen(p, "r"))) {
760 process_respfile(rfile);
761 fclose(rfile);
762 } else
763 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
764 "unable to open response file `%s'", p);
766 } else
767 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
768 argv += i, argc -= i;
771 if (!*inname)
772 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
773 "no input file specified");
776 static void assemble_file(char *fname)
778 char *directive, *value, *p, *q, *special, *line, debugid[80];
779 insn output_ins;
780 int i, rn_error, validid;
781 long seg, offs;
782 struct tokenval tokval;
783 expr *e;
784 int pass, pass_max;
785 int pass_cnt = 0; /* count actual passes */
787 if (cmd_sb == 32 && cmd_cpu < IF_386)
788 report_error(ERR_FATAL, "command line: "
789 "32-bit segment size requires a higher cpu");
791 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
792 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
793 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
794 int pass1, pass2;
795 ldfunc def_label;
797 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
798 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
799 /* pass0 seq is 0, 0, 0,..., 1, 2 */
801 def_label = pass > 1 ? redefine_label : define_label;
803 sb = cmd_sb; /* set 'bits' to command line default */
804 cpu = cmd_cpu;
805 if (pass0 == 2) {
806 if (*listname)
807 nasmlist.init(listname, report_error);
809 in_abs_seg = FALSE;
810 global_offset_changed = FALSE; /* set by redefine_label */
811 location.segment = ofmt->section(NULL, pass2, &sb);
812 if (pass > 1) {
813 saa_rewind(forwrefs);
814 forwref = saa_rstruct(forwrefs);
815 raa_free(offsets);
816 offsets = raa_init();
818 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
819 globallineno = 0;
820 if (pass == 1)
821 location.known = TRUE;
822 location.offset = offs = GET_CURR_OFFS;
824 while ((line = preproc->getline())) {
825 globallineno++;
827 /* here we parse our directives; this is not handled by the 'real'
828 * parser. */
829 directive = line;
830 if ((i = getkw(&directive, &value))) {
831 switch (i) {
832 case 1: /* [SEGMENT n] */
833 seg = ofmt->section(value, pass2, &sb);
834 if (seg == NO_SEG) {
835 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
836 "segment name `%s' not recognised",
837 value);
838 } else {
839 in_abs_seg = FALSE;
840 location.segment = seg;
842 break;
843 case 2: /* [EXTERN label:special] */
844 if (*value == '$')
845 value++; /* skip initial $ if present */
846 if (pass0 == 2) {
847 q = value;
848 while (*q && *q != ':')
849 q++;
850 if (*q == ':') {
851 *q++ = '\0';
852 ofmt->symdef(value, 0L, 0L, 3, q);
854 } else if (pass == 1) { /* pass == 1 */
855 q = value;
856 validid = TRUE;
857 if (!isidstart(*q))
858 validid = FALSE;
859 while (*q && *q != ':') {
860 if (!isidchar(*q))
861 validid = FALSE;
862 q++;
864 if (!validid) {
865 report_error(ERR_NONFATAL,
866 "identifier expected after EXTERN");
867 break;
869 if (*q == ':') {
870 *q++ = '\0';
871 special = q;
872 } else
873 special = NULL;
874 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
875 int temp = pass0;
876 pass0 = 1; /* fake pass 1 in labels.c */
877 declare_as_global(value, special,
878 report_error);
879 define_label(value, seg_alloc(), 0L, NULL,
880 FALSE, TRUE, ofmt, report_error);
881 pass0 = temp;
883 } /* else pass0 == 1 */
884 break;
885 case 3: /* [BITS bits] */
886 sb = get_bits(value);
887 break;
888 case 4: /* [GLOBAL symbol:special] */
889 if (*value == '$')
890 value++; /* skip initial $ if present */
891 if (pass0 == 2) { /* pass 2 */
892 q = value;
893 while (*q && *q != ':')
894 q++;
895 if (*q == ':') {
896 *q++ = '\0';
897 ofmt->symdef(value, 0L, 0L, 3, q);
899 } else if (pass2 == 1) { /* pass == 1 */
900 q = value;
901 validid = TRUE;
902 if (!isidstart(*q))
903 validid = FALSE;
904 while (*q && *q != ':') {
905 if (!isidchar(*q))
906 validid = FALSE;
907 q++;
909 if (!validid) {
910 report_error(ERR_NONFATAL,
911 "identifier expected after GLOBAL");
912 break;
914 if (*q == ':') {
915 *q++ = '\0';
916 special = q;
917 } else
918 special = NULL;
919 declare_as_global(value, special, report_error);
920 } /* pass == 1 */
921 break;
922 case 5: /* [COMMON symbol size:special] */
923 if (*value == '$')
924 value++; /* skip initial $ if present */
925 if (pass0 == 1) {
926 p = value;
927 validid = TRUE;
928 if (!isidstart(*p))
929 validid = FALSE;
930 while (*p && !isspace(*p)) {
931 if (!isidchar(*p))
932 validid = FALSE;
933 p++;
935 if (!validid) {
936 report_error(ERR_NONFATAL,
937 "identifier expected after COMMON");
938 break;
940 if (*p) {
941 long size;
943 while (*p && isspace(*p))
944 *p++ = '\0';
945 q = p;
946 while (*q && *q != ':')
947 q++;
948 if (*q == ':') {
949 *q++ = '\0';
950 special = q;
951 } else
952 special = NULL;
953 size = readnum(p, &rn_error);
954 if (rn_error)
955 report_error(ERR_NONFATAL,
956 "invalid size specified"
957 " in COMMON declaration");
958 else
959 define_common(value, seg_alloc(), size,
960 special, ofmt, report_error);
961 } else
962 report_error(ERR_NONFATAL,
963 "no size specified in"
964 " COMMON declaration");
965 } else if (pass0 == 2) { /* pass == 2 */
966 q = value;
967 while (*q && *q != ':') {
968 if (isspace(*q))
969 *q = '\0';
970 q++;
972 if (*q == ':') {
973 *q++ = '\0';
974 ofmt->symdef(value, 0L, 0L, 3, q);
977 break;
978 case 6: /* [ABSOLUTE address] */
979 stdscan_reset();
980 stdscan_bufptr = value;
981 tokval.t_type = TOKEN_INVALID;
982 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
983 report_error, NULL);
984 if (e) {
985 if (!is_reloc(e))
986 report_error(pass0 ==
987 1 ? ERR_NONFATAL : ERR_PANIC,
988 "cannot use non-relocatable expression as "
989 "ABSOLUTE address");
990 else {
991 abs_seg = reloc_seg(e);
992 abs_offset = reloc_value(e);
994 } else if (pass == 1)
995 abs_offset = 0x100; /* don't go near zero in case of / */
996 else
997 report_error(ERR_PANIC, "invalid ABSOLUTE address "
998 "in pass two");
999 in_abs_seg = TRUE;
1000 location.segment = NO_SEG;
1001 break;
1002 case 7: /* DEBUG */
1003 p = value;
1004 q = debugid;
1005 validid = TRUE;
1006 if (!isidstart(*p))
1007 validid = FALSE;
1008 while (*p && !isspace(*p)) {
1009 if (!isidchar(*p))
1010 validid = FALSE;
1011 *q++ = *p++;
1013 *q++ = 0;
1014 if (!validid) {
1015 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1016 "identifier expected after DEBUG");
1017 break;
1019 while (*p && isspace(*p))
1020 p++;
1021 if (pass == pass_max)
1022 ofmt->current_dfmt->debug_directive(debugid, p);
1023 break;
1024 case 8: /* [WARNING {+|-}warn-name] */
1025 if (pass1 == 1) {
1026 while (*value && isspace(*value))
1027 value++;
1029 if (*value == '+' || *value == '-') {
1030 validid = (*value == '-') ? TRUE : FALSE;
1031 value++;
1032 } else
1033 validid = FALSE;
1035 for (i = 1; i <= ERR_WARN_MAX; i++)
1036 if (!nasm_stricmp(value, suppressed_names[i]))
1037 break;
1038 if (i <= ERR_WARN_MAX)
1039 suppressed[i] = validid;
1040 else
1041 report_error(ERR_NONFATAL,
1042 "invalid warning id in WARNING directive");
1044 break;
1045 case 9: /* cpu */
1046 cpu = get_cpu(value);
1047 break;
1048 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1049 while (*value && isspace(*value))
1050 value++;
1052 if (*value == '+') {
1053 user_nolist = 0;
1054 } else {
1055 if (*value == '-') {
1056 user_nolist = 1;
1057 } else {
1058 report_error(ERR_NONFATAL,
1059 "invalid parameter to \"list\" directive");
1062 break;
1063 default:
1064 if (!ofmt->directive(directive, value, pass2))
1065 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1066 "unrecognised directive [%s]",
1067 directive);
1069 } else { /* it isn't a directive */
1071 parse_line(pass1, line, &output_ins,
1072 report_error, evaluate, def_label);
1074 if (!(optimizing > 0) && pass == 2) {
1075 if (forwref != NULL && globallineno == forwref->lineno) {
1076 output_ins.forw_ref = TRUE;
1077 do {
1078 output_ins.oprs[forwref->operand].opflags |=
1079 OPFLAG_FORWARD;
1080 forwref = saa_rstruct(forwrefs);
1081 } while (forwref != NULL
1082 && forwref->lineno == globallineno);
1083 } else
1084 output_ins.forw_ref = FALSE;
1087 if (!(optimizing > 0) && output_ins.forw_ref) {
1088 if (pass == 1) {
1089 for (i = 0; i < output_ins.operands; i++) {
1090 if (output_ins.oprs[i].
1091 opflags & OPFLAG_FORWARD) {
1092 struct forwrefinfo *fwinf =
1093 (struct forwrefinfo *)
1094 saa_wstruct(forwrefs);
1095 fwinf->lineno = globallineno;
1096 fwinf->operand = i;
1099 } else { /* pass == 2 */
1101 * Hack to prevent phase error in the code
1102 * rol ax,x
1103 * x equ 1
1105 * If the second operand is a forward reference,
1106 * the UNITY property of the number 1 in that
1107 * operand is cancelled. Otherwise the above
1108 * sequence will cause a phase error.
1110 * This hack means that the above code will
1111 * generate 286+ code.
1113 * The forward reference will mean that the
1114 * operand will not have the UNITY property on
1115 * the first pass, so the pass behaviours will
1116 * be consistent.
1119 if (output_ins.operands >= 2 &&
1120 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1122 output_ins.oprs[1].type &=
1123 ~(ONENESS | BYTENESS);
1126 } /* pass == 2 */
1130 /* forw_ref */
1131 if (output_ins.opcode == I_EQU) {
1132 if (pass1 == 1) {
1134 * Special `..' EQUs get processed in pass two,
1135 * except `..@' macro-processor EQUs which are done
1136 * in the normal place.
1138 if (!output_ins.label)
1139 report_error(ERR_NONFATAL,
1140 "EQU not preceded by label");
1142 else if (output_ins.label[0] != '.' ||
1143 output_ins.label[1] != '.' ||
1144 output_ins.label[2] == '@') {
1145 if (output_ins.operands == 1 &&
1146 (output_ins.oprs[0].type & IMMEDIATE) &&
1147 output_ins.oprs[0].wrt == NO_SEG) {
1148 int isext =
1149 output_ins.oprs[0].
1150 opflags & OPFLAG_EXTERN;
1151 def_label(output_ins.label,
1152 output_ins.oprs[0].segment,
1153 output_ins.oprs[0].offset, NULL,
1154 FALSE, isext, ofmt,
1155 report_error);
1156 } else if (output_ins.operands == 2
1157 && (output_ins.oprs[0].
1158 type & IMMEDIATE)
1159 && (output_ins.oprs[0].type & COLON)
1160 && output_ins.oprs[0].segment ==
1161 NO_SEG
1162 && output_ins.oprs[0].wrt == NO_SEG
1163 && (output_ins.oprs[1].
1164 type & IMMEDIATE)
1165 && output_ins.oprs[1].segment ==
1166 NO_SEG
1167 && output_ins.oprs[1].wrt ==
1168 NO_SEG) {
1169 def_label(output_ins.label,
1170 output_ins.oprs[0].
1171 offset | SEG_ABS,
1172 output_ins.oprs[1].offset, NULL,
1173 FALSE, FALSE, ofmt,
1174 report_error);
1175 } else
1176 report_error(ERR_NONFATAL,
1177 "bad syntax for EQU");
1179 } else { /* pass == 2 */
1181 * Special `..' EQUs get processed here, except
1182 * `..@' macro processor EQUs which are done above.
1184 if (output_ins.label[0] == '.' &&
1185 output_ins.label[1] == '.' &&
1186 output_ins.label[2] != '@') {
1187 if (output_ins.operands == 1 &&
1188 (output_ins.oprs[0].type & IMMEDIATE)) {
1189 define_label(output_ins.label,
1190 output_ins.oprs[0].segment,
1191 output_ins.oprs[0].offset,
1192 NULL, FALSE, FALSE, ofmt,
1193 report_error);
1194 } else if (output_ins.operands == 2
1195 && (output_ins.oprs[0].
1196 type & IMMEDIATE)
1197 && (output_ins.oprs[0].type & COLON)
1198 && output_ins.oprs[0].segment ==
1199 NO_SEG
1200 && (output_ins.oprs[1].
1201 type & IMMEDIATE)
1202 && output_ins.oprs[1].segment ==
1203 NO_SEG) {
1204 define_label(output_ins.label,
1205 output_ins.oprs[0].
1206 offset | SEG_ABS,
1207 output_ins.oprs[1].offset,
1208 NULL, FALSE, FALSE, ofmt,
1209 report_error);
1210 } else
1211 report_error(ERR_NONFATAL,
1212 "bad syntax for EQU");
1214 } /* pass == 2 */
1215 } else { /* instruction isn't an EQU */
1217 if (pass1 == 1) {
1219 long l = insn_size(location.segment, offs, sb, cpu,
1220 &output_ins, report_error);
1222 /* if (using_debug_info) && output_ins.opcode != -1) */
1223 if (using_debug_info)
1224 { /* fbk 03/25/01 */
1225 /* this is done here so we can do debug type info */
1226 long typeinfo =
1227 TYS_ELEMENTS(output_ins.operands);
1228 switch (output_ins.opcode) {
1229 case I_RESB:
1230 typeinfo =
1231 TYS_ELEMENTS(output_ins.oprs[0].
1232 offset) | TY_BYTE;
1233 break;
1234 case I_RESW:
1235 typeinfo =
1236 TYS_ELEMENTS(output_ins.oprs[0].
1237 offset) | TY_WORD;
1238 break;
1239 case I_RESD:
1240 typeinfo =
1241 TYS_ELEMENTS(output_ins.oprs[0].
1242 offset) | TY_DWORD;
1243 break;
1244 case I_RESQ:
1245 typeinfo =
1246 TYS_ELEMENTS(output_ins.oprs[0].
1247 offset) | TY_QWORD;
1248 break;
1249 case I_REST:
1250 typeinfo =
1251 TYS_ELEMENTS(output_ins.oprs[0].
1252 offset) | TY_TBYTE;
1253 break;
1254 case I_DB:
1255 typeinfo |= TY_BYTE;
1256 break;
1257 case I_DW:
1258 typeinfo |= TY_WORD;
1259 break;
1260 case I_DD:
1261 if (output_ins.eops_float)
1262 typeinfo |= TY_FLOAT;
1263 else
1264 typeinfo |= TY_DWORD;
1265 break;
1266 case I_DQ:
1267 typeinfo |= TY_QWORD;
1268 break;
1269 case I_DT:
1270 typeinfo |= TY_TBYTE;
1271 break;
1272 default:
1273 typeinfo = TY_LABEL;
1277 ofmt->current_dfmt->debug_typevalue(typeinfo);
1280 if (l != -1) {
1281 offs += l;
1282 SET_CURR_OFFS(offs);
1285 * else l == -1 => invalid instruction, which will be
1286 * flagged as an error on pass 2
1289 } else { /* pass == 2 */
1290 offs += assemble(location.segment, offs, sb, cpu,
1291 &output_ins, ofmt, report_error,
1292 &nasmlist);
1293 SET_CURR_OFFS(offs);
1296 } /* not an EQU */
1297 cleanup_insn(&output_ins);
1299 nasm_free(line);
1300 location.offset = offs = GET_CURR_OFFS;
1301 } /* end while (line = preproc->getline... */
1303 if (pass1 == 2 && global_offset_changed)
1304 report_error(ERR_NONFATAL,
1305 "phase error detected at end of assembly.");
1307 if (pass1 == 1)
1308 preproc->cleanup(1);
1310 if (pass1 == 1 && terminate_after_phase) {
1311 fclose(ofile);
1312 remove(outname);
1313 if (want_usage)
1314 usage();
1315 exit(1);
1317 pass_cnt++;
1318 if (pass > 1 && !global_offset_changed) {
1319 pass0++;
1320 if (pass0 == 2)
1321 pass = pass_max - 1;
1322 } else if (!(optimizing > 0))
1323 pass0++;
1325 } /* for (pass=1; pass<=2; pass++) */
1327 preproc->cleanup(0);
1328 nasmlist.cleanup();
1329 #if 1
1330 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1331 fprintf(stdout,
1332 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1333 #endif
1334 } /* exit from assemble_file (...) */
1336 static int getkw(char **directive, char **value)
1338 char *p, *q, *buf;
1340 buf = *directive;
1342 /* allow leading spaces or tabs */
1343 while (*buf == ' ' || *buf == '\t')
1344 buf++;
1346 if (*buf != '[')
1347 return 0;
1349 p = buf;
1351 while (*p && *p != ']')
1352 p++;
1354 if (!*p)
1355 return 0;
1357 q = p++;
1359 while (*p && *p != ';') {
1360 if (!isspace(*p))
1361 return 0;
1362 p++;
1364 q[1] = '\0';
1366 *directive = p = buf + 1;
1367 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1368 buf++;
1369 if (*buf == ']') {
1370 *buf = '\0';
1371 *value = buf;
1372 } else {
1373 *buf++ = '\0';
1374 while (isspace(*buf))
1375 buf++; /* beppu - skip leading whitespace */
1376 *value = buf;
1377 while (*buf != ']')
1378 buf++;
1379 *buf++ = '\0';
1381 #if 0
1382 for (q = p; *q; q++)
1383 *q = tolower(*q);
1384 #endif
1385 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1386 return 1;
1387 if (!nasm_stricmp(p, "extern"))
1388 return 2;
1389 if (!nasm_stricmp(p, "bits"))
1390 return 3;
1391 if (!nasm_stricmp(p, "global"))
1392 return 4;
1393 if (!nasm_stricmp(p, "common"))
1394 return 5;
1395 if (!nasm_stricmp(p, "absolute"))
1396 return 6;
1397 if (!nasm_stricmp(p, "debug"))
1398 return 7;
1399 if (!nasm_stricmp(p, "warning"))
1400 return 8;
1401 if (!nasm_stricmp(p, "cpu"))
1402 return 9;
1403 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1404 return 10;
1405 return -1;
1409 * gnu style error reporting
1410 * This function prints an error message to error_file in the
1411 * style used by GNU. An example would be:
1412 * file.asm:50: error: blah blah blah
1413 * where file.asm is the name of the file, 50 is the line number on
1414 * which the error occurs (or is detected) and "error:" is one of
1415 * the possible optional diagnostics -- it can be "error" or "warning"
1416 * or something else. Finally the line terminates with the actual
1417 * error message.
1419 * @param severity the severity of the warning or error
1420 * @param fmt the printf style format string
1422 static void report_error_gnu(int severity, const char *fmt, ...)
1424 va_list ap;
1426 if (is_suppressed_warning(severity))
1427 return;
1429 if (severity & ERR_NOFILE)
1430 fputs("nasm: ", error_file);
1431 else {
1432 char *currentfile = NULL;
1433 long lineno = 0;
1434 src_get(&lineno, &currentfile);
1435 fprintf(error_file, "%s:%ld: ", currentfile, lineno);
1436 nasm_free(currentfile);
1438 va_start(ap, fmt);
1439 report_error_common(severity, fmt, ap);
1440 va_end(ap);
1444 * MS style error reporting
1445 * This function prints an error message to error_file in the
1446 * style used by Visual C and some other Microsoft tools. An example
1447 * would be:
1448 * file.asm(50) : error: blah blah blah
1449 * where file.asm is the name of the file, 50 is the line number on
1450 * which the error occurs (or is detected) and "error:" is one of
1451 * the possible optional diagnostics -- it can be "error" or "warning"
1452 * or something else. Finally the line terminates with the actual
1453 * error message.
1455 * @param severity the severity of the warning or error
1456 * @param fmt the printf style format string
1458 static void report_error_vc(int severity, const char *fmt, ...)
1460 va_list ap;
1462 if (is_suppressed_warning(severity))
1463 return;
1465 if (severity & ERR_NOFILE)
1466 fputs("nasm: ", error_file);
1467 else {
1468 char *currentfile = NULL;
1469 long lineno = 0;
1470 src_get(&lineno, &currentfile);
1471 fprintf(error_file, "%s(%ld) : ", currentfile, lineno);
1472 nasm_free(currentfile);
1474 va_start(ap, fmt);
1475 report_error_common(severity, fmt, ap);
1476 va_end(ap);
1480 * check for supressed warning
1481 * checks for suppressed warning or pass one only warning and we're
1482 * not in pass 1
1484 * @param severity the severity of the warning or error
1485 * @return true if we should abort error/warning printing
1487 static int is_suppressed_warning(int severity)
1490 * See if it's a suppressed warning.
1492 return ((severity & ERR_MASK) == ERR_WARNING &&
1493 (severity & ERR_WARN_MASK) != 0 &&
1494 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1496 * See if it's a pass-one only warning and we're not in pass one.
1498 ((severity & ERR_PASS1) && pass0 == 2);
1502 * common error reporting
1503 * This is the common back end of the error reporting schemes currently
1504 * implemented. It prints the nature of the warning and then the
1505 * specific error message to error_file and may or may not return. It
1506 * doesn't return if the error severity is a "panic" or "debug" type.
1508 * @param severity the severity of the warning or error
1509 * @param fmt the printf style format string
1511 static void report_error_common(int severity, const char *fmt,
1512 va_list args)
1514 switch (severity & ERR_MASK) {
1515 case ERR_WARNING:
1516 fputs("warning: ", error_file);
1517 break;
1518 case ERR_NONFATAL:
1519 fputs("error: ", error_file);
1520 break;
1521 case ERR_FATAL:
1522 fputs("fatal: ", error_file);
1523 break;
1524 case ERR_PANIC:
1525 fputs("panic: ", error_file);
1526 break;
1527 case ERR_DEBUG:
1528 fputs("debug: ", error_file);
1529 break;
1532 vfprintf(error_file, fmt, args);
1533 fputc('\n', error_file);
1535 if (severity & ERR_USAGE)
1536 want_usage = TRUE;
1538 switch (severity & ERR_MASK) {
1539 case ERR_WARNING:
1540 case ERR_DEBUG:
1541 /* no further action, by definition */
1542 break;
1543 case ERR_NONFATAL:
1544 /* hack enables listing(!) on errors */
1545 terminate_after_phase = TRUE;
1546 break;
1547 case ERR_FATAL:
1548 if (ofile) {
1549 fclose(ofile);
1550 remove(outname);
1552 if (want_usage)
1553 usage();
1554 exit(1); /* instantly die */
1555 break; /* placate silly compilers */
1556 case ERR_PANIC:
1557 fflush(NULL);
1558 /* abort(); *//* halt, catch fire, and dump core */
1559 exit(3);
1560 break;
1564 static void usage(void)
1566 fputs("type `nasm -h' for help\n", error_file);
1569 static void register_output_formats(void)
1571 ofmt = ofmt_register(report_error);
1574 #define BUF_DELTA 512
1576 static FILE *no_pp_fp;
1577 static efunc no_pp_err;
1578 static ListGen *no_pp_list;
1579 static long no_pp_lineinc;
1581 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1582 ListGen * listgen)
1584 src_set_fname(nasm_strdup(file));
1585 src_set_linnum(0);
1586 no_pp_lineinc = 1;
1587 no_pp_err = error;
1588 no_pp_fp = fopen(file, "r");
1589 if (!no_pp_fp)
1590 no_pp_err(ERR_FATAL | ERR_NOFILE,
1591 "unable to open input file `%s'", file);
1592 no_pp_list = listgen;
1593 (void)pass; /* placate compilers */
1594 (void)eval; /* placate compilers */
1597 static char *no_pp_getline(void)
1599 char *buffer, *p, *q;
1600 int bufsize;
1602 bufsize = BUF_DELTA;
1603 buffer = nasm_malloc(BUF_DELTA);
1604 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1606 while (1) { /* Loop to handle %line */
1608 p = buffer;
1609 while (1) { /* Loop to handle long lines */
1610 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1611 if (!q)
1612 break;
1613 p += strlen(p);
1614 if (p > buffer && p[-1] == '\n')
1615 break;
1616 if (p - buffer > bufsize - 10) {
1617 int offset;
1618 offset = p - buffer;
1619 bufsize += BUF_DELTA;
1620 buffer = nasm_realloc(buffer, bufsize);
1621 p = buffer + offset;
1625 if (!q && p == buffer) {
1626 nasm_free(buffer);
1627 return NULL;
1631 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1632 * them are present at the end of the line.
1634 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1636 if (!strncmp(buffer, "%line", 5)) {
1637 long ln;
1638 int li;
1639 char *nm = nasm_malloc(strlen(buffer));
1640 if (sscanf(buffer + 5, "%ld+%d %s", &ln, &li, nm) == 3) {
1641 nasm_free(src_set_fname(nm));
1642 src_set_linnum(ln);
1643 no_pp_lineinc = li;
1644 continue;
1646 nasm_free(nm);
1648 break;
1651 no_pp_list->line(LIST_READ, buffer);
1653 return buffer;
1656 static void no_pp_cleanup(int pass)
1658 fclose(no_pp_fp);
1661 static unsigned long get_cpu(char *value)
1664 if (!strcmp(value, "8086"))
1665 return IF_8086;
1666 if (!strcmp(value, "186"))
1667 return IF_186;
1668 if (!strcmp(value, "286"))
1669 return IF_286;
1670 if (!strcmp(value, "386"))
1671 return IF_386;
1672 if (!strcmp(value, "486"))
1673 return IF_486;
1674 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1675 return IF_PENT;
1676 if (!strcmp(value, "686") ||
1677 !nasm_stricmp(value, "ppro") ||
1678 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1679 return IF_P6;
1680 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1681 return IF_KATMAI;
1682 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1683 !nasm_stricmp(value, "willamette"))
1684 return IF_WILLAMETTE;
1685 if (!nasm_stricmp(value, "prescott"))
1686 return IF_PRESCOTT;
1687 if (!nasm_stricmp(value, "ia64") ||
1688 !nasm_stricmp(value, "ia-64") ||
1689 !nasm_stricmp(value, "itanium") ||
1690 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1691 return IF_IA64;
1693 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1694 "unknown 'cpu' type");
1696 return IF_PLEVEL; /* the maximum level */
1699 static int get_bits(char *value)
1701 int i;
1703 if ((i = atoi(value)) == 16)
1704 return i; /* set for a 16-bit segment */
1705 else if (i == 32) {
1706 if (cpu < IF_386) {
1707 report_error(ERR_NONFATAL,
1708 "cannot specify 32-bit segment on processor below a 386");
1709 i = 16;
1711 } else {
1712 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1713 "`%s' is not a valid segment size; must be 16 or 32",
1714 value);
1715 i = 16;
1717 return i;
1720 /* end of nasm.c */