Fixed uninitialized structure data.
[nasm/sigaren-mirror.git] / nasm.c
blob1c8772a871c207c2363b2901612202754b339cb4
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>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "insns.h"
19 #include "preproc.h"
20 #include "parser.h"
21 #include "eval.h"
22 #include "assemble.h"
23 #include "labels.h"
24 #include "outform.h"
25 #include "listing.h"
27 struct forwrefinfo { /* info held on forward refs. */
28 int lineno;
29 int operand;
32 static int get_bits(char *value);
33 static uint32_t get_cpu(char *cpu_str);
34 static void parse_cmdline(int, char **);
35 static void assemble_file(char *);
36 static int getkw(char **directive, char **value);
37 static void register_output_formats(void);
38 static void report_error_gnu(int severity, const char *fmt, ...);
39 static void report_error_vc(int severity, const char *fmt, ...);
40 static void report_error_common(int severity, const char *fmt,
41 va_list args);
42 static int is_suppressed_warning(int severity);
43 static void usage(void);
44 static efunc report_error;
46 static int using_debug_info, opt_verbose_info;
47 int tasm_compatible_mode = FALSE;
48 int pass0;
49 int maxbits = 0;
51 static char inname[FILENAME_MAX];
52 static char outname[FILENAME_MAX];
53 static char listname[FILENAME_MAX];
54 static int globallineno; /* for forward-reference tracking */
55 /* static int pass = 0; */
56 static struct ofmt *ofmt = NULL;
58 static FILE *error_file; /* Where to write error messages */
60 static FILE *ofile = NULL;
61 int optimizing = -1; /* number of optimization passes to take */
62 static int sb, cmd_sb = 16; /* by default */
63 static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
64 static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
65 int global_offset_changed; /* referenced in labels.c */
67 static loc_t location;
68 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
69 int32_t abs_seg; /* ABSOLUTE segment basis */
70 int32_t abs_offset; /* ABSOLUTE offset */
72 static struct RAA *offsets;
74 static struct SAA *forwrefs; /* keep track of forward references */
75 static struct forwrefinfo *forwref;
77 static Preproc *preproc;
78 enum op_type {
79 op_normal, /* Preprocess and assemble */
80 op_preprocess, /* Preprocess only */
81 op_depend /* Generate dependencies */
83 static enum op_type operating_mode;
86 * Which of the suppressible warnings are suppressed. Entry zero
87 * doesn't do anything. Initial defaults are given here.
89 static char suppressed[1 + ERR_WARN_MAX] = {
90 0, TRUE, TRUE, TRUE, FALSE, TRUE
94 * The option names for the suppressible warnings. As before, entry
95 * zero does nothing.
97 static const char *suppressed_names[1 + ERR_WARN_MAX] = {
98 NULL, "macro-params", "macro-selfref", "orphan-labels",
99 "number-overflow",
100 "gnu-elf-extensions"
104 * The explanations for the suppressible warnings. As before, entry
105 * zero does nothing.
107 static const char *suppressed_what[1 + ERR_WARN_MAX] = {
108 NULL,
109 "macro calls with wrong no. of params",
110 "cyclic macro self-references",
111 "labels alone on lines without trailing `:'",
112 "numeric constants greater than 0xFFFFFFFF",
113 "using 8- or 16-bit relocation in ELF, a GNU extension"
117 * This is a null preprocessor which just copies lines from input
118 * to output. It's used when someone explicitly requests that NASM
119 * not preprocess their source file.
122 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
123 static char *no_pp_getline(void);
124 static void no_pp_cleanup(int);
125 static Preproc no_pp = {
126 no_pp_reset,
127 no_pp_getline,
128 no_pp_cleanup
132 * get/set current offset...
134 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
135 raa_read(offsets,location.segment))
136 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
137 (void)(offsets=raa_write(offsets,location.segment,(x))))
139 static int want_usage;
140 static int terminate_after_phase;
141 int user_nolist = 0; /* fbk 9/2/00 */
143 static void nasm_fputs(const char *line, FILE * outfile)
145 if (outfile) {
146 fputs(line, outfile);
147 fputc('\n', outfile);
148 } else
149 puts(line);
152 int main(int argc, char **argv)
154 pass0 = 1;
155 want_usage = terminate_after_phase = FALSE;
156 report_error = report_error_gnu;
158 nasm_set_malloc_error(report_error);
159 offsets = raa_init();
160 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
162 preproc = &nasmpp;
163 operating_mode = op_normal;
165 error_file = stderr;
167 seg_init();
169 register_output_formats();
171 parse_cmdline(argc, argv);
173 if (terminate_after_phase) {
174 if (want_usage)
175 usage();
176 return 1;
179 /* If debugging info is disabled, suppress any debug calls */
180 if (!using_debug_info)
181 ofmt->current_dfmt = &null_debug_form;
183 if (ofmt->stdmac)
184 pp_extra_stdmac(ofmt->stdmac);
185 parser_global_info(ofmt, &location);
186 eval_global_info(ofmt, lookup_label, &location);
188 /* define some macros dependent of command-line */
190 char temp[64];
191 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
192 ofmt->shortname);
193 pp_pre_define(temp);
196 switch (operating_mode) {
197 case op_depend:
199 char *line;
200 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
201 if (outname[0] == '\0')
202 ofmt->filename(inname, outname, report_error);
203 ofile = NULL;
204 fprintf(stdout, "%s: %s", outname, inname);
205 while ((line = preproc->getline()))
206 nasm_free(line);
207 preproc->cleanup(0);
208 putc('\n', stdout);
210 break;
212 case op_preprocess:
214 char *line;
215 char *file_name = NULL;
216 int32_t prior_linnum = 0;
217 int lineinc = 0;
219 if (*outname) {
220 ofile = fopen(outname, "w");
221 if (!ofile)
222 report_error(ERR_FATAL | ERR_NOFILE,
223 "unable to open output file `%s'",
224 outname);
225 } else
226 ofile = NULL;
228 location.known = FALSE;
230 /* pass = 1; */
231 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
232 while ((line = preproc->getline())) {
234 * We generate %line directives if needed for later programs
236 int32_t linnum = prior_linnum += lineinc;
237 int altline = src_get(&linnum, &file_name);
238 if (altline) {
239 if (altline == 1 && lineinc == 1)
240 nasm_fputs("", ofile);
241 else {
242 lineinc = (altline != -1 || lineinc != 1);
243 fprintf(ofile ? ofile : stdout,
244 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
245 file_name);
247 prior_linnum = linnum;
249 nasm_fputs(line, ofile);
250 nasm_free(line);
252 nasm_free(file_name);
253 preproc->cleanup(0);
254 if (ofile)
255 fclose(ofile);
256 if (ofile && terminate_after_phase)
257 remove(outname);
259 break;
261 case op_normal:
264 * We must call ofmt->filename _anyway_, even if the user
265 * has specified their own output file, because some
266 * formats (eg OBJ and COFF) use ofmt->filename to find out
267 * the name of the input file and then put that inside the
268 * file.
270 ofmt->filename(inname, outname, report_error);
272 ofile = fopen(outname, "wb");
273 if (!ofile) {
274 report_error(ERR_FATAL | ERR_NOFILE,
275 "unable to open output file `%s'", outname);
279 * We must call init_labels() before ofmt->init() since
280 * some object formats will want to define labels in their
281 * init routines. (eg OS/2 defines the FLAT group)
283 init_labels();
285 ofmt->init(ofile, report_error, define_label, evaluate);
287 assemble_file(inname);
289 if (!terminate_after_phase) {
290 ofmt->cleanup(using_debug_info);
291 cleanup_labels();
292 } else {
294 * We had an fclose on the output file here, but we
295 * actually do that in all the object file drivers as well,
296 * so we're leaving out the one here.
297 * fclose (ofile);
299 remove(outname);
300 if (listname[0])
301 remove(listname);
304 break;
307 if (want_usage)
308 usage();
310 raa_free(offsets);
311 saa_free(forwrefs);
312 eval_cleanup();
313 nasmlib_cleanup();
315 if (terminate_after_phase)
316 return 1;
317 else
318 return 0;
322 * Get a parameter for a command line option.
323 * First arg must be in the form of e.g. -f...
325 static char *get_param(char *p, char *q, int *advance)
327 *advance = 0;
328 if (p[2]) { /* the parameter's in the option */
329 p += 2;
330 while (isspace(*p))
331 p++;
332 return p;
334 if (q && q[0]) {
335 *advance = 1;
336 return q;
338 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
339 "option `-%c' requires an argument", p[1]);
340 return NULL;
343 struct textargs {
344 const char *label;
345 int value;
348 #define OPT_PREFIX 0
349 #define OPT_POSTFIX 1
350 struct textargs textopts[] = {
351 {"prefix", OPT_PREFIX},
352 {"postfix", OPT_POSTFIX},
353 {NULL, 0}
356 int stopoptions = 0;
357 static int process_arg(char *p, char *q)
359 char *param;
360 int i, advance = 0;
362 if (!p || !p[0])
363 return 0;
365 if (p[0] == '-' && !stopoptions) {
366 switch (p[1]) {
367 case 's':
368 error_file = stdout;
369 break;
370 case 'o': /* these parameters take values */
371 case 'O':
372 case 'f':
373 case 'p':
374 case 'P':
375 case 'd':
376 case 'D':
377 case 'i':
378 case 'I':
379 case 'l':
380 case 'E':
381 case 'F':
382 case 'X':
383 case 'u':
384 case 'U':
385 if (!(param = get_param(p, q, &advance)))
386 break;
387 if (p[1] == 'o') { /* output file */
388 strcpy(outname, param);
389 } else if (p[1] == 'f') { /* output format */
390 ofmt = ofmt_find(param);
391 if (!ofmt) {
392 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
393 "unrecognised output format `%s' - "
394 "use -hf for a list", param);
395 } else
396 ofmt->current_dfmt = ofmt->debug_formats[0];
397 } else if (p[1] == 'O') { /* Optimization level */
398 int opt;
399 opt = -99;
400 while (*param) {
401 if (isdigit(*param)) {
402 opt = atoi(param);
403 while (isdigit(*++param)) ;
404 if (opt <= 0)
405 optimizing = -1; /* 0.98 behaviour */
406 else if (opt == 1)
407 optimizing = 0; /* Two passes, 0.98.09 behavior */
408 else
409 optimizing = opt; /* Multiple passes */
410 } else {
411 if (*param == 'v' || *param == '+') {
412 ++param;
413 opt_verbose_info = TRUE;
414 opt = 0;
415 } else { /* garbage */
416 opt = -99;
417 break;
420 } /* while (*param) */
421 if (opt == -99)
422 report_error(ERR_FATAL,
423 "command line optimization level must be 'v', 0..3 or <nn>");
424 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
425 pp_pre_include(param);
426 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
427 pp_pre_define(param);
428 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
429 pp_pre_undefine(param);
430 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
431 pp_include_path(param);
432 } else if (p[1] == 'l') { /* listing file */
433 strcpy(listname, param);
434 } else if (p[1] == 'E') { /* error messages file */
435 error_file = fopen(param, "w");
436 if (!error_file) {
437 error_file = stderr; /* Revert to default! */
438 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
439 "cannot open file `%s' for error messages",
440 param);
442 } else if (p[1] == 'F') { /* specify debug format */
443 ofmt->current_dfmt = dfmt_find(ofmt, param);
444 if (!ofmt->current_dfmt) {
445 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
446 "unrecognized debug format `%s' for"
447 " output format `%s'",
448 param, ofmt->shortname);
450 } else if (p[1] == 'X') { /* specify error reporting format */
451 if (nasm_stricmp("vc", param) == 0)
452 report_error = report_error_vc;
453 else if (nasm_stricmp("gnu", param) == 0)
454 report_error = report_error_gnu;
455 else
456 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
457 "unrecognized error reporting format `%s'",
458 param);
460 break;
461 case 'g':
462 using_debug_info = TRUE;
463 break;
464 case 'h':
465 printf
466 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
467 "[-l listfile]\n"
468 " [options...] [--] filename\n"
469 " or nasm -r for version info (obsolete)\n"
470 " or nasm -v for version info (preferred)\n\n"
471 " -t assemble in SciTech TASM compatible mode\n"
472 " -g generate debug information in selected format.\n");
473 printf
474 (" -e preprocess only (writes output to stdout by default)\n"
475 " -a don't preprocess (assemble only)\n"
476 " -M generate Makefile dependencies on stdout\n\n"
477 " -E<file> redirect error messages to file\n"
478 " -s redirect error messages to stdout\n\n"
479 " -F format select a debugging format\n\n"
480 " -I<path> adds a pathname to the include file path\n");
481 printf
482 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
483 " -P<file> pre-includes a file\n"
484 " -D<macro>[=<value>] pre-defines a macro\n"
485 " -U<macro> undefines a macro\n"
486 " -X<format> specifies error reporting format (gnu or vc)\n"
487 " -w+foo enables warnings about foo; -w-foo disables them\n"
488 "where foo can be:\n");
489 for (i = 1; i <= ERR_WARN_MAX; i++)
490 printf(" %-23s %s (default %s)\n",
491 suppressed_names[i], suppressed_what[i],
492 suppressed[i] ? "off" : "on");
493 printf
494 ("\nresponse files should contain command line parameters"
495 ", one per line.\n");
496 if (p[2] == 'f') {
497 printf("\nvalid output formats for -f are"
498 " (`*' denotes default):\n");
499 ofmt_list(ofmt, stdout);
500 } else {
501 printf("\nFor a list of valid output formats, use -hf.\n");
502 printf("For a list of debug formats, use -f <form> -y.\n");
504 exit(0); /* never need usage message here */
505 break;
506 case 'y':
507 printf("\nvalid debug formats for '%s' output format are"
508 " ('*' denotes default):\n", ofmt->shortname);
509 dfmt_list(ofmt, stdout);
510 exit(0);
511 break;
512 case 't':
513 tasm_compatible_mode = TRUE;
514 break;
515 case 'r':
516 case 'v':
518 const char *nasm_version_string =
519 "NASM version " NASM_VER " compiled on " __DATE__
520 #ifdef DEBUG
521 " with -DDEBUG"
522 #endif
524 puts(nasm_version_string);
525 exit(0); /* never need usage message here */
527 break;
528 case 'e': /* preprocess only */
529 operating_mode = op_preprocess;
530 break;
531 case 'a': /* assemble only - don't preprocess */
532 preproc = &no_pp;
533 break;
534 case 'w':
535 if (p[2] != '+' && p[2] != '-') {
536 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
537 "invalid option to `-w'");
538 } else {
539 for (i = 1; i <= ERR_WARN_MAX; i++)
540 if (!nasm_stricmp(p + 3, suppressed_names[i]))
541 break;
542 if (i <= ERR_WARN_MAX)
543 suppressed[i] = (p[2] == '-');
544 else
545 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
546 "invalid option to `-w'");
548 break;
549 case 'M':
550 operating_mode = op_depend;
551 break;
553 case '-':
555 int s;
557 if (p[2] == 0) { /* -- => stop processing options */
558 stopoptions = 1;
559 break;
561 for (s = 0; textopts[s].label; s++) {
562 if (!nasm_stricmp(p + 2, textopts[s].label)) {
563 break;
567 switch (s) {
569 case OPT_PREFIX:
570 case OPT_POSTFIX:
572 if (!q) {
573 report_error(ERR_NONFATAL | ERR_NOFILE |
574 ERR_USAGE,
575 "option `--%s' requires an argument",
576 p + 2);
577 break;
578 } else {
579 advance = 1, param = q;
582 if (s == OPT_PREFIX) {
583 strncpy(lprefix, param, PREFIX_MAX - 1);
584 lprefix[PREFIX_MAX - 1] = 0;
585 break;
587 if (s == OPT_POSTFIX) {
588 strncpy(lpostfix, param, POSTFIX_MAX - 1);
589 lpostfix[POSTFIX_MAX - 1] = 0;
590 break;
592 break;
594 default:
596 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
597 "unrecognised option `--%s'", p + 2);
598 break;
601 break;
604 default:
605 if (!ofmt->setinfo(GI_SWITCH, &p))
606 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
607 "unrecognised option `-%c'", p[1]);
608 break;
610 } else {
611 if (*inname) {
612 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
613 "more than one input file specified");
614 } else
615 strcpy(inname, p);
618 return advance;
621 #define ARG_BUF_DELTA 128
623 static void process_respfile(FILE * rfile)
625 char *buffer, *p, *q, *prevarg;
626 int bufsize, prevargsize;
628 bufsize = prevargsize = ARG_BUF_DELTA;
629 buffer = nasm_malloc(ARG_BUF_DELTA);
630 prevarg = nasm_malloc(ARG_BUF_DELTA);
631 prevarg[0] = '\0';
633 while (1) { /* Loop to handle all lines in file */
635 p = buffer;
636 while (1) { /* Loop to handle long lines */
637 q = fgets(p, bufsize - (p - buffer), rfile);
638 if (!q)
639 break;
640 p += strlen(p);
641 if (p > buffer && p[-1] == '\n')
642 break;
643 if (p - buffer > bufsize - 10) {
644 int offset;
645 offset = p - buffer;
646 bufsize += ARG_BUF_DELTA;
647 buffer = nasm_realloc(buffer, bufsize);
648 p = buffer + offset;
652 if (!q && p == buffer) {
653 if (prevarg[0])
654 process_arg(prevarg, NULL);
655 nasm_free(buffer);
656 nasm_free(prevarg);
657 return;
661 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
662 * them are present at the end of the line.
664 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
666 while (p > buffer && isspace(p[-1]))
667 *--p = '\0';
669 p = buffer;
670 while (isspace(*p))
671 p++;
673 if (process_arg(prevarg, p))
674 *p = '\0';
676 if (strlen(p) > prevargsize - 10) {
677 prevargsize += ARG_BUF_DELTA;
678 prevarg = nasm_realloc(prevarg, prevargsize);
680 strcpy(prevarg, p);
684 /* Function to process args from a string of args, rather than the
685 * argv array. Used by the environment variable and response file
686 * processing.
688 static void process_args(char *args)
690 char *p, *q, *arg, *prevarg;
691 char separator = ' ';
693 p = args;
694 if (*p && *p != '-')
695 separator = *p++;
696 arg = NULL;
697 while (*p) {
698 q = p;
699 while (*p && *p != separator)
700 p++;
701 while (*p == separator)
702 *p++ = '\0';
703 prevarg = arg;
704 arg = q;
705 if (process_arg(prevarg, arg))
706 arg = NULL;
708 if (arg)
709 process_arg(arg, NULL);
712 static void parse_cmdline(int argc, char **argv)
714 FILE *rfile;
715 char *envreal, *envcopy = NULL, *p, *arg;
717 *inname = *outname = *listname = '\0';
720 * First, process the NASMENV environment variable.
722 envreal = getenv("NASMENV");
723 arg = NULL;
724 if (envreal) {
725 envcopy = nasm_strdup(envreal);
726 process_args(envcopy);
727 nasm_free(envcopy);
731 * Now process the actual command line.
733 while (--argc) {
734 int i;
735 argv++;
736 if (argv[0][0] == '@') {
737 /* We have a response file, so process this as a set of
738 * arguments like the environment variable. This allows us
739 * to have multiple arguments on a single line, which is
740 * different to the -@resp file processing below for regular
741 * NASM.
743 char *str = malloc(2048);
744 FILE *f = fopen(&argv[0][1], "r");
745 if (!str) {
746 printf("out of memory");
747 exit(-1);
749 if (f) {
750 while (fgets(str, 2048, f)) {
751 process_args(str);
753 fclose(f);
755 free(str);
756 argc--;
757 argv++;
759 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
760 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
761 if (p) {
762 rfile = fopen(p, "r");
763 if (rfile) {
764 process_respfile(rfile);
765 fclose(rfile);
766 } else
767 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
768 "unable to open response file `%s'", p);
770 } else
771 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
772 argv += i, argc -= i;
775 if (!*inname)
776 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
777 "no input file specified");
780 static void assemble_file(char *fname)
782 char *directive, *value, *p, *q, *special, *line, debugid[80];
783 insn output_ins;
784 int i, rn_error, validid;
785 int32_t seg, offs;
786 struct tokenval tokval;
787 expr *e;
788 int pass, pass_max;
789 int pass_cnt = 0; /* count actual passes */
791 if (cmd_sb == 32 && cmd_cpu < IF_386)
792 report_error(ERR_FATAL, "command line: "
793 "32-bit segment size requires a higher cpu");
795 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
796 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
797 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
798 int pass1, pass2;
799 ldfunc def_label;
801 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
802 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
803 /* pass0 seq is 0, 0, 0,..., 1, 2 */
805 def_label = pass > 1 ? redefine_label : define_label;
807 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
808 cpu = cmd_cpu;
809 if (pass0 == 2) {
810 if (*listname)
811 nasmlist.init(listname, report_error);
813 in_abs_seg = FALSE;
814 global_offset_changed = FALSE; /* set by redefine_label */
815 location.segment = ofmt->section(NULL, pass2, &sb);
816 globalbits = sb;
817 if (pass > 1) {
818 saa_rewind(forwrefs);
819 forwref = saa_rstruct(forwrefs);
820 raa_free(offsets);
821 offsets = raa_init();
823 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
824 globallineno = 0;
825 if (pass == 1)
826 location.known = TRUE;
827 location.offset = offs = GET_CURR_OFFS;
829 while ((line = preproc->getline())) {
830 globallineno++;
832 /* here we parse our directives; this is not handled by the 'real'
833 * parser. */
834 directive = line;
835 i = getkw(&directive, &value);
836 if (i) {
837 switch (i) {
838 case 1: /* [SEGMENT n] */
839 seg = ofmt->section(value, pass2, &sb);
840 if (seg == NO_SEG) {
841 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
842 "segment name `%s' not recognized",
843 value);
844 } else {
845 in_abs_seg = FALSE;
846 location.segment = seg;
848 break;
849 case 2: /* [EXTERN label:special] */
850 if (*value == '$')
851 value++; /* skip initial $ if present */
852 if (pass0 == 2) {
853 q = value;
854 while (*q && *q != ':')
855 q++;
856 if (*q == ':') {
857 *q++ = '\0';
858 ofmt->symdef(value, 0L, 0L, 3, q);
860 } else if (pass == 1) { /* pass == 1 */
861 q = value;
862 validid = TRUE;
863 if (!isidstart(*q))
864 validid = FALSE;
865 while (*q && *q != ':') {
866 if (!isidchar(*q))
867 validid = FALSE;
868 q++;
870 if (!validid) {
871 report_error(ERR_NONFATAL,
872 "identifier expected after EXTERN");
873 break;
875 if (*q == ':') {
876 *q++ = '\0';
877 special = q;
878 } else
879 special = NULL;
880 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
881 int temp = pass0;
882 pass0 = 1; /* fake pass 1 in labels.c */
883 declare_as_global(value, special,
884 report_error);
885 define_label(value, seg_alloc(), 0L, NULL,
886 FALSE, TRUE, ofmt, report_error);
887 pass0 = temp;
889 } /* else pass0 == 1 */
890 break;
891 case 3: /* [BITS bits] */
892 globalbits = sb = get_bits(value);
893 break;
894 case 4: /* [GLOBAL symbol:special] */
895 if (*value == '$')
896 value++; /* skip initial $ if present */
897 if (pass0 == 2) { /* pass 2 */
898 q = value;
899 while (*q && *q != ':')
900 q++;
901 if (*q == ':') {
902 *q++ = '\0';
903 ofmt->symdef(value, 0L, 0L, 3, q);
905 } else if (pass2 == 1) { /* pass == 1 */
906 q = value;
907 validid = TRUE;
908 if (!isidstart(*q))
909 validid = FALSE;
910 while (*q && *q != ':') {
911 if (!isidchar(*q))
912 validid = FALSE;
913 q++;
915 if (!validid) {
916 report_error(ERR_NONFATAL,
917 "identifier expected after GLOBAL");
918 break;
920 if (*q == ':') {
921 *q++ = '\0';
922 special = q;
923 } else
924 special = NULL;
925 declare_as_global(value, special, report_error);
926 } /* pass == 1 */
927 break;
928 case 5: /* [COMMON symbol size:special] */
929 if (*value == '$')
930 value++; /* skip initial $ if present */
931 if (pass0 == 1) {
932 p = value;
933 validid = TRUE;
934 if (!isidstart(*p))
935 validid = FALSE;
936 while (*p && !isspace(*p)) {
937 if (!isidchar(*p))
938 validid = FALSE;
939 p++;
941 if (!validid) {
942 report_error(ERR_NONFATAL,
943 "identifier expected after COMMON");
944 break;
946 if (*p) {
947 int64_t size;
949 while (*p && isspace(*p))
950 *p++ = '\0';
951 q = p;
952 while (*q && *q != ':')
953 q++;
954 if (*q == ':') {
955 *q++ = '\0';
956 special = q;
957 } else
958 special = NULL;
959 size = readnum(p, &rn_error);
960 if (rn_error)
961 report_error(ERR_NONFATAL,
962 "invalid size specified"
963 " in COMMON declaration");
964 else
965 define_common(value, seg_alloc(), size,
966 special, ofmt, report_error);
967 } else
968 report_error(ERR_NONFATAL,
969 "no size specified in"
970 " COMMON declaration");
971 } else if (pass0 == 2) { /* pass == 2 */
972 q = value;
973 while (*q && *q != ':') {
974 if (isspace(*q))
975 *q = '\0';
976 q++;
978 if (*q == ':') {
979 *q++ = '\0';
980 ofmt->symdef(value, 0L, 0L, 3, q);
983 break;
984 case 6: /* [ABSOLUTE address] */
985 stdscan_reset();
986 stdscan_bufptr = value;
987 tokval.t_type = TOKEN_INVALID;
988 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
989 report_error, NULL);
990 if (e) {
991 if (!is_reloc(e))
992 report_error(pass0 ==
993 1 ? ERR_NONFATAL : ERR_PANIC,
994 "cannot use non-relocatable expression as "
995 "ABSOLUTE address");
996 else {
997 abs_seg = reloc_seg(e);
998 abs_offset = reloc_value(e);
1000 } else if (pass == 1)
1001 abs_offset = 0x100; /* don't go near zero in case of / */
1002 else
1003 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1004 "in pass two");
1005 in_abs_seg = TRUE;
1006 location.segment = NO_SEG;
1007 break;
1008 case 7: /* DEBUG */
1009 p = value;
1010 q = debugid;
1011 validid = TRUE;
1012 if (!isidstart(*p))
1013 validid = FALSE;
1014 while (*p && !isspace(*p)) {
1015 if (!isidchar(*p))
1016 validid = FALSE;
1017 *q++ = *p++;
1019 *q++ = 0;
1020 if (!validid) {
1021 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1022 "identifier expected after DEBUG");
1023 break;
1025 while (*p && isspace(*p))
1026 p++;
1027 if (pass == pass_max)
1028 ofmt->current_dfmt->debug_directive(debugid, p);
1029 break;
1030 case 8: /* [WARNING {+|-}warn-name] */
1031 if (pass1 == 1) {
1032 while (*value && isspace(*value))
1033 value++;
1035 if (*value == '+' || *value == '-') {
1036 validid = (*value == '-') ? TRUE : FALSE;
1037 value++;
1038 } else
1039 validid = FALSE;
1041 for (i = 1; i <= ERR_WARN_MAX; i++)
1042 if (!nasm_stricmp(value, suppressed_names[i]))
1043 break;
1044 if (i <= ERR_WARN_MAX)
1045 suppressed[i] = validid;
1046 else
1047 report_error(ERR_NONFATAL,
1048 "invalid warning id in WARNING directive");
1050 break;
1051 case 9: /* cpu */
1052 cpu = get_cpu(value);
1053 break;
1054 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1055 while (*value && isspace(*value))
1056 value++;
1058 if (*value == '+') {
1059 user_nolist = 0;
1060 } else {
1061 if (*value == '-') {
1062 user_nolist = 1;
1063 } else {
1064 report_error(ERR_NONFATAL,
1065 "invalid parameter to \"list\" directive");
1068 break;
1069 default:
1070 if (!ofmt->directive(directive, value, pass2))
1071 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1072 "unrecognised directive [%s]",
1073 directive);
1075 } else { /* it isn't a directive */
1077 parse_line(pass1, line, &output_ins,
1078 report_error, evaluate, def_label);
1080 if (!(optimizing > 0) && pass == 2) {
1081 if (forwref != NULL && globallineno == forwref->lineno) {
1082 output_ins.forw_ref = TRUE;
1083 do {
1084 output_ins.oprs[forwref->operand].opflags |=
1085 OPFLAG_FORWARD;
1086 forwref = saa_rstruct(forwrefs);
1087 } while (forwref != NULL
1088 && forwref->lineno == globallineno);
1089 } else
1090 output_ins.forw_ref = FALSE;
1093 if (!(optimizing > 0) && output_ins.forw_ref) {
1094 if (pass == 1) {
1095 for (i = 0; i < output_ins.operands; i++) {
1096 if (output_ins.oprs[i].
1097 opflags & OPFLAG_FORWARD) {
1098 struct forwrefinfo *fwinf =
1099 (struct forwrefinfo *)
1100 saa_wstruct(forwrefs);
1101 fwinf->lineno = globallineno;
1102 fwinf->operand = i;
1105 } else { /* pass == 2 */
1107 * Hack to prevent phase error in the code
1108 * rol ax,x
1109 * x equ 1
1111 * If the second operand is a forward reference,
1112 * the UNITY property of the number 1 in that
1113 * operand is cancelled. Otherwise the above
1114 * sequence will cause a phase error.
1116 * This hack means that the above code will
1117 * generate 286+ code.
1119 * The forward reference will mean that the
1120 * operand will not have the UNITY property on
1121 * the first pass, so the pass behaviours will
1122 * be consistent.
1125 if (output_ins.operands >= 2 &&
1126 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1128 output_ins.oprs[1].type &=
1129 ~(ONENESS | BYTENESS);
1132 } /* pass == 2 */
1136 /* forw_ref */
1137 if (output_ins.opcode == I_EQU) {
1138 if (pass1 == 1) {
1140 * Special `..' EQUs get processed in pass two,
1141 * except `..@' macro-processor EQUs which are done
1142 * in the normal place.
1144 if (!output_ins.label)
1145 report_error(ERR_NONFATAL,
1146 "EQU not preceded by label");
1148 else if (output_ins.label[0] != '.' ||
1149 output_ins.label[1] != '.' ||
1150 output_ins.label[2] == '@') {
1151 if (output_ins.operands == 1 &&
1152 (output_ins.oprs[0].type & IMMEDIATE) &&
1153 output_ins.oprs[0].wrt == NO_SEG) {
1154 int isext =
1155 output_ins.oprs[0].
1156 opflags & OPFLAG_EXTERN;
1157 def_label(output_ins.label,
1158 output_ins.oprs[0].segment,
1159 output_ins.oprs[0].offset, NULL,
1160 FALSE, isext, ofmt,
1161 report_error);
1162 } else if (output_ins.operands == 2
1163 && (output_ins.oprs[0].
1164 type & IMMEDIATE)
1165 && (output_ins.oprs[0].type & COLON)
1166 && output_ins.oprs[0].segment ==
1167 NO_SEG
1168 && output_ins.oprs[0].wrt == NO_SEG
1169 && (output_ins.oprs[1].
1170 type & IMMEDIATE)
1171 && output_ins.oprs[1].segment ==
1172 NO_SEG
1173 && output_ins.oprs[1].wrt ==
1174 NO_SEG) {
1175 def_label(output_ins.label,
1176 output_ins.oprs[0].
1177 offset | SEG_ABS,
1178 output_ins.oprs[1].offset, NULL,
1179 FALSE, FALSE, ofmt,
1180 report_error);
1181 } else
1182 report_error(ERR_NONFATAL,
1183 "bad syntax for EQU");
1185 } else { /* pass == 2 */
1187 * Special `..' EQUs get processed here, except
1188 * `..@' macro processor EQUs which are done above.
1190 if (output_ins.label[0] == '.' &&
1191 output_ins.label[1] == '.' &&
1192 output_ins.label[2] != '@') {
1193 if (output_ins.operands == 1 &&
1194 (output_ins.oprs[0].type & IMMEDIATE)) {
1195 define_label(output_ins.label,
1196 output_ins.oprs[0].segment,
1197 output_ins.oprs[0].offset,
1198 NULL, FALSE, FALSE, ofmt,
1199 report_error);
1200 } else if (output_ins.operands == 2
1201 && (output_ins.oprs[0].
1202 type & IMMEDIATE)
1203 && (output_ins.oprs[0].type & COLON)
1204 && output_ins.oprs[0].segment ==
1205 NO_SEG
1206 && (output_ins.oprs[1].
1207 type & IMMEDIATE)
1208 && output_ins.oprs[1].segment ==
1209 NO_SEG) {
1210 define_label(output_ins.label,
1211 output_ins.oprs[0].
1212 offset | SEG_ABS,
1213 output_ins.oprs[1].offset,
1214 NULL, FALSE, FALSE, ofmt,
1215 report_error);
1216 } else
1217 report_error(ERR_NONFATAL,
1218 "bad syntax for EQU");
1220 } /* pass == 2 */
1221 } else { /* instruction isn't an EQU */
1223 if (pass1 == 1) {
1225 int32_t l = insn_size(location.segment, offs, sb, cpu,
1226 &output_ins, report_error);
1228 /* if (using_debug_info) && output_ins.opcode != -1) */
1229 if (using_debug_info)
1230 { /* fbk 03/25/01 */
1231 /* this is done here so we can do debug type info */
1232 int32_t typeinfo =
1233 TYS_ELEMENTS(output_ins.operands);
1234 switch (output_ins.opcode) {
1235 case I_RESB:
1236 typeinfo =
1237 TYS_ELEMENTS(output_ins.oprs[0].
1238 offset) | TY_BYTE;
1239 break;
1240 case I_RESW:
1241 typeinfo =
1242 TYS_ELEMENTS(output_ins.oprs[0].
1243 offset) | TY_WORD;
1244 break;
1245 case I_RESD:
1246 typeinfo =
1247 TYS_ELEMENTS(output_ins.oprs[0].
1248 offset) | TY_DWORD;
1249 break;
1250 case I_RESQ:
1251 typeinfo =
1252 TYS_ELEMENTS(output_ins.oprs[0].
1253 offset) | TY_QWORD;
1254 break;
1255 case I_REST:
1256 typeinfo =
1257 TYS_ELEMENTS(output_ins.oprs[0].
1258 offset) | TY_TBYTE;
1259 break;
1260 case I_DB:
1261 typeinfo |= TY_BYTE;
1262 break;
1263 case I_DW:
1264 typeinfo |= TY_WORD;
1265 break;
1266 case I_DD:
1267 if (output_ins.eops_float)
1268 typeinfo |= TY_FLOAT;
1269 else
1270 typeinfo |= TY_DWORD;
1271 break;
1272 case I_DQ:
1273 typeinfo |= TY_QWORD;
1274 break;
1275 case I_DT:
1276 typeinfo |= TY_TBYTE;
1277 break;
1278 default:
1279 typeinfo = TY_LABEL;
1283 ofmt->current_dfmt->debug_typevalue(typeinfo);
1286 if (l != -1) {
1287 offs += l;
1288 SET_CURR_OFFS(offs);
1291 * else l == -1 => invalid instruction, which will be
1292 * flagged as an error on pass 2
1295 } else { /* pass == 2 */
1296 offs += assemble(location.segment, offs, sb, cpu,
1297 &output_ins, ofmt, report_error,
1298 &nasmlist);
1299 SET_CURR_OFFS(offs);
1302 } /* not an EQU */
1303 cleanup_insn(&output_ins);
1305 nasm_free(line);
1306 location.offset = offs = GET_CURR_OFFS;
1307 } /* end while (line = preproc->getline... */
1309 if (pass1 == 2 && global_offset_changed)
1310 report_error(ERR_NONFATAL,
1311 "phase error detected at end of assembly.");
1313 if (pass1 == 1)
1314 preproc->cleanup(1);
1316 if (pass1 == 1 && terminate_after_phase) {
1317 fclose(ofile);
1318 remove(outname);
1319 if (want_usage)
1320 usage();
1321 exit(1);
1323 pass_cnt++;
1324 if (pass > 1 && !global_offset_changed) {
1325 pass0++;
1326 if (pass0 == 2)
1327 pass = pass_max - 1;
1328 } else if (!(optimizing > 0))
1329 pass0++;
1331 } /* for (pass=1; pass<=2; pass++) */
1333 preproc->cleanup(0);
1334 nasmlist.cleanup();
1335 #if 1
1336 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1337 fprintf(stdout,
1338 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1339 #endif
1340 } /* exit from assemble_file (...) */
1342 static int getkw(char **directive, char **value)
1344 char *p, *q, *buf;
1346 buf = *directive;
1348 /* allow leading spaces or tabs */
1349 while (*buf == ' ' || *buf == '\t')
1350 buf++;
1352 if (*buf != '[')
1353 return 0;
1355 p = buf;
1357 while (*p && *p != ']')
1358 p++;
1360 if (!*p)
1361 return 0;
1363 q = p++;
1365 while (*p && *p != ';') {
1366 if (!isspace(*p))
1367 return 0;
1368 p++;
1370 q[1] = '\0';
1372 *directive = p = buf + 1;
1373 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1374 buf++;
1375 if (*buf == ']') {
1376 *buf = '\0';
1377 *value = buf;
1378 } else {
1379 *buf++ = '\0';
1380 while (isspace(*buf))
1381 buf++; /* beppu - skip leading whitespace */
1382 *value = buf;
1383 while (*buf != ']')
1384 buf++;
1385 *buf++ = '\0';
1387 #if 0
1388 for (q = p; *q; q++)
1389 *q = tolower(*q);
1390 #endif
1391 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1392 return 1;
1393 if (!nasm_stricmp(p, "extern"))
1394 return 2;
1395 if (!nasm_stricmp(p, "bits"))
1396 return 3;
1397 if (!nasm_stricmp(p, "global"))
1398 return 4;
1399 if (!nasm_stricmp(p, "common"))
1400 return 5;
1401 if (!nasm_stricmp(p, "absolute"))
1402 return 6;
1403 if (!nasm_stricmp(p, "debug"))
1404 return 7;
1405 if (!nasm_stricmp(p, "warning"))
1406 return 8;
1407 if (!nasm_stricmp(p, "cpu"))
1408 return 9;
1409 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1410 return 10;
1411 return -1;
1415 * gnu style error reporting
1416 * This function prints an error message to error_file in the
1417 * style used by GNU. An example would be:
1418 * file.asm:50: error: blah blah blah
1419 * where file.asm is the name of the file, 50 is the line number on
1420 * which the error occurs (or is detected) and "error:" is one of
1421 * the possible optional diagnostics -- it can be "error" or "warning"
1422 * or something else. Finally the line terminates with the actual
1423 * error message.
1425 * @param severity the severity of the warning or error
1426 * @param fmt the printf style format string
1428 static void report_error_gnu(int severity, const char *fmt, ...)
1430 va_list ap;
1432 if (is_suppressed_warning(severity))
1433 return;
1435 if (severity & ERR_NOFILE)
1436 fputs("nasm: ", error_file);
1437 else {
1438 char *currentfile = NULL;
1439 int32_t lineno = 0;
1440 src_get(&lineno, &currentfile);
1441 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1442 nasm_free(currentfile);
1444 va_start(ap, fmt);
1445 report_error_common(severity, fmt, ap);
1446 va_end(ap);
1450 * MS style error reporting
1451 * This function prints an error message to error_file in the
1452 * style used by Visual C and some other Microsoft tools. An example
1453 * would be:
1454 * file.asm(50) : error: blah blah blah
1455 * where file.asm is the name of the file, 50 is the line number on
1456 * which the error occurs (or is detected) and "error:" is one of
1457 * the possible optional diagnostics -- it can be "error" or "warning"
1458 * or something else. Finally the line terminates with the actual
1459 * error message.
1461 * @param severity the severity of the warning or error
1462 * @param fmt the printf style format string
1464 static void report_error_vc(int severity, const char *fmt, ...)
1466 va_list ap;
1468 if (is_suppressed_warning(severity))
1469 return;
1471 if (severity & ERR_NOFILE)
1472 fputs("nasm: ", error_file);
1473 else {
1474 char *currentfile = NULL;
1475 int32_t lineno = 0;
1476 src_get(&lineno, &currentfile);
1477 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1478 nasm_free(currentfile);
1480 va_start(ap, fmt);
1481 report_error_common(severity, fmt, ap);
1482 va_end(ap);
1486 * check for supressed warning
1487 * checks for suppressed warning or pass one only warning and we're
1488 * not in pass 1
1490 * @param severity the severity of the warning or error
1491 * @return true if we should abort error/warning printing
1493 static int is_suppressed_warning(int severity)
1496 * See if it's a suppressed warning.
1498 return ((severity & ERR_MASK) == ERR_WARNING &&
1499 (severity & ERR_WARN_MASK) != 0 &&
1500 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1502 * See if it's a pass-one only warning and we're not in pass one.
1504 ((severity & ERR_PASS1) && pass0 == 2);
1508 * common error reporting
1509 * This is the common back end of the error reporting schemes currently
1510 * implemented. It prints the nature of the warning and then the
1511 * specific error message to error_file and may or may not return. It
1512 * doesn't return if the error severity is a "panic" or "debug" type.
1514 * @param severity the severity of the warning or error
1515 * @param fmt the printf style format string
1517 static void report_error_common(int severity, const char *fmt,
1518 va_list args)
1520 switch (severity & ERR_MASK) {
1521 case ERR_WARNING:
1522 fputs("warning: ", error_file);
1523 break;
1524 case ERR_NONFATAL:
1525 fputs("error: ", error_file);
1526 break;
1527 case ERR_FATAL:
1528 fputs("fatal: ", error_file);
1529 break;
1530 case ERR_PANIC:
1531 fputs("panic: ", error_file);
1532 break;
1533 case ERR_DEBUG:
1534 fputs("debug: ", error_file);
1535 break;
1538 vfprintf(error_file, fmt, args);
1539 fputc('\n', error_file);
1541 if (severity & ERR_USAGE)
1542 want_usage = TRUE;
1544 switch (severity & ERR_MASK) {
1545 case ERR_WARNING:
1546 case ERR_DEBUG:
1547 /* no further action, by definition */
1548 break;
1549 case ERR_NONFATAL:
1550 /* hack enables listing(!) on errors */
1551 terminate_after_phase = TRUE;
1552 break;
1553 case ERR_FATAL:
1554 if (ofile) {
1555 fclose(ofile);
1556 remove(outname);
1558 if (want_usage)
1559 usage();
1560 exit(1); /* instantly die */
1561 break; /* placate silly compilers */
1562 case ERR_PANIC:
1563 fflush(NULL);
1564 /* abort(); *//* halt, catch fire, and dump core */
1565 exit(3);
1566 break;
1570 static void usage(void)
1572 fputs("type `nasm -h' for help\n", error_file);
1575 static void register_output_formats(void)
1577 ofmt = ofmt_register(report_error);
1580 #define BUF_DELTA 512
1582 static FILE *no_pp_fp;
1583 static efunc no_pp_err;
1584 static ListGen *no_pp_list;
1585 static int32_t no_pp_lineinc;
1587 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1588 ListGen * listgen)
1590 src_set_fname(nasm_strdup(file));
1591 src_set_linnum(0);
1592 no_pp_lineinc = 1;
1593 no_pp_err = error;
1594 no_pp_fp = fopen(file, "r");
1595 if (!no_pp_fp)
1596 no_pp_err(ERR_FATAL | ERR_NOFILE,
1597 "unable to open input file `%s'", file);
1598 no_pp_list = listgen;
1599 (void)pass; /* placate compilers */
1600 (void)eval; /* placate compilers */
1603 static char *no_pp_getline(void)
1605 char *buffer, *p, *q;
1606 int bufsize;
1608 bufsize = BUF_DELTA;
1609 buffer = nasm_malloc(BUF_DELTA);
1610 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1612 while (1) { /* Loop to handle %line */
1614 p = buffer;
1615 while (1) { /* Loop to handle long lines */
1616 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1617 if (!q)
1618 break;
1619 p += strlen(p);
1620 if (p > buffer && p[-1] == '\n')
1621 break;
1622 if (p - buffer > bufsize - 10) {
1623 int offset;
1624 offset = p - buffer;
1625 bufsize += BUF_DELTA;
1626 buffer = nasm_realloc(buffer, bufsize);
1627 p = buffer + offset;
1631 if (!q && p == buffer) {
1632 nasm_free(buffer);
1633 return NULL;
1637 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1638 * them are present at the end of the line.
1640 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1642 if (!strncmp(buffer, "%line", 5)) {
1643 int32_t ln;
1644 int li;
1645 char *nm = nasm_malloc(strlen(buffer));
1646 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1647 nasm_free(src_set_fname(nm));
1648 src_set_linnum(ln);
1649 no_pp_lineinc = li;
1650 continue;
1652 nasm_free(nm);
1654 break;
1657 no_pp_list->line(LIST_READ, buffer);
1659 return buffer;
1662 static void no_pp_cleanup(int pass)
1664 (void)pass; /* placate GCC */
1665 fclose(no_pp_fp);
1668 static uint32_t get_cpu(char *value)
1671 if (!strcmp(value, "8086"))
1672 return IF_8086;
1673 if (!strcmp(value, "186"))
1674 return IF_186;
1675 if (!strcmp(value, "286"))
1676 return IF_286;
1677 if (!strcmp(value, "386"))
1678 return IF_386;
1679 if (!strcmp(value, "486"))
1680 return IF_486;
1681 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1682 return IF_PENT;
1683 if (!strcmp(value, "686") ||
1684 !nasm_stricmp(value, "ppro") ||
1685 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1686 return IF_P6;
1687 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1688 return IF_KATMAI;
1689 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1690 !nasm_stricmp(value, "willamette"))
1691 return IF_WILLAMETTE;
1692 if (!nasm_stricmp(value, "prescott"))
1693 return IF_PRESCOTT;
1694 if (!nasm_stricmp(value, "x64") ||
1695 !nasm_stricmp(value, "x86-64"))
1696 return IF_X64;
1697 if (!nasm_stricmp(value, "ia64") ||
1698 !nasm_stricmp(value, "ia-64") ||
1699 !nasm_stricmp(value, "itanium") ||
1700 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1701 return IF_IA64;
1703 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1704 "unknown 'cpu' type");
1706 return IF_PLEVEL; /* the maximum level */
1709 static int get_bits(char *value)
1711 int i;
1713 if ((i = atoi(value)) == 16)
1714 return i; /* set for a 16-bit segment */
1715 else if (i == 32) {
1716 if (cpu < IF_386) {
1717 report_error(ERR_NONFATAL,
1718 "cannot specify 32-bit segment on processor below a 386");
1719 i = 16;
1721 } else if (i == 64) {
1722 if (cpu < IF_X64) {
1723 report_error(ERR_NONFATAL,
1724 "cannot specify 64-bit segment on processor below an x86-64");
1725 i = 16;
1727 if (i != maxbits) {
1728 report_error(ERR_NONFATAL,
1729 "%s output format does not support 64-bit code",
1730 ofmt->shortname);
1731 i = 16;
1733 } else {
1734 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1735 "`%s' is not a valid segment size; must be 16, 32 or 64",
1736 value);
1737 i = 16;
1739 return i;
1742 /* end of nasm.c */