Implement REL/ABS modifiers
[nasm.git] / nasm.c
blob0dfad2aba07e89b4ee9f9d704259bd11ef87e650
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;
50 int globalrel = 0;
52 static char inname[FILENAME_MAX];
53 static char outname[FILENAME_MAX];
54 static char listname[FILENAME_MAX];
55 static int globallineno; /* for forward-reference tracking */
56 /* static int pass = 0; */
57 static struct ofmt *ofmt = NULL;
59 static FILE *error_file; /* Where to write error messages */
61 static FILE *ofile = NULL;
62 int optimizing = -1; /* number of optimization passes to take */
63 static int sb, cmd_sb = 16; /* by default */
64 static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
65 static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
66 int global_offset_changed; /* referenced in labels.c */
68 static loc_t location;
69 int in_abs_seg; /* Flag we are in ABSOLUTE seg */
70 int32_t abs_seg; /* ABSOLUTE segment basis */
71 int32_t abs_offset; /* ABSOLUTE offset */
73 static struct RAA *offsets;
75 static struct SAA *forwrefs; /* keep track of forward references */
76 static struct forwrefinfo *forwref;
78 static Preproc *preproc;
79 enum op_type {
80 op_normal, /* Preprocess and assemble */
81 op_preprocess, /* Preprocess only */
82 op_depend /* Generate dependencies */
84 static enum op_type operating_mode;
87 * Which of the suppressible warnings are suppressed. Entry zero
88 * doesn't do anything. Initial defaults are given here.
90 static char suppressed[1 + ERR_WARN_MAX] = {
91 0, TRUE, TRUE, TRUE, FALSE, TRUE
95 * The option names for the suppressible warnings. As before, entry
96 * zero does nothing.
98 static const char *suppressed_names[1 + ERR_WARN_MAX] = {
99 NULL, "macro-params", "macro-selfref", "orphan-labels",
100 "number-overflow",
101 "gnu-elf-extensions"
105 * The explanations for the suppressible warnings. As before, entry
106 * zero does nothing.
108 static const char *suppressed_what[1 + ERR_WARN_MAX] = {
109 NULL,
110 "macro calls with wrong no. of params",
111 "cyclic macro self-references",
112 "labels alone on lines without trailing `:'",
113 "numeric constants greater than 0xFFFFFFFF",
114 "using 8- or 16-bit relocation in ELF, a GNU extension"
118 * This is a null preprocessor which just copies lines from input
119 * to output. It's used when someone explicitly requests that NASM
120 * not preprocess their source file.
123 static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *);
124 static char *no_pp_getline(void);
125 static void no_pp_cleanup(int);
126 static Preproc no_pp = {
127 no_pp_reset,
128 no_pp_getline,
129 no_pp_cleanup
133 * get/set current offset...
135 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
136 raa_read(offsets,location.segment))
137 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
138 (void)(offsets=raa_write(offsets,location.segment,(x))))
140 static int want_usage;
141 static int terminate_after_phase;
142 int user_nolist = 0; /* fbk 9/2/00 */
144 static void nasm_fputs(const char *line, FILE * outfile)
146 if (outfile) {
147 fputs(line, outfile);
148 fputc('\n', outfile);
149 } else
150 puts(line);
153 int main(int argc, char **argv)
155 pass0 = 1;
156 want_usage = terminate_after_phase = FALSE;
157 report_error = report_error_gnu;
159 nasm_set_malloc_error(report_error);
160 offsets = raa_init();
161 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
163 preproc = &nasmpp;
164 operating_mode = op_normal;
166 error_file = stderr;
168 seg_init();
170 register_output_formats();
172 parse_cmdline(argc, argv);
174 if (terminate_after_phase) {
175 if (want_usage)
176 usage();
177 return 1;
180 /* If debugging info is disabled, suppress any debug calls */
181 if (!using_debug_info)
182 ofmt->current_dfmt = &null_debug_form;
184 if (ofmt->stdmac)
185 pp_extra_stdmac(ofmt->stdmac);
186 parser_global_info(ofmt, &location);
187 eval_global_info(ofmt, lookup_label, &location);
189 /* define some macros dependent of command-line */
191 char temp[64];
192 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
193 ofmt->shortname);
194 pp_pre_define(temp);
197 switch (operating_mode) {
198 case op_depend:
200 char *line;
201 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
202 if (outname[0] == '\0')
203 ofmt->filename(inname, outname, report_error);
204 ofile = NULL;
205 fprintf(stdout, "%s: %s", outname, inname);
206 while ((line = preproc->getline()))
207 nasm_free(line);
208 preproc->cleanup(0);
209 putc('\n', stdout);
211 break;
213 case op_preprocess:
215 char *line;
216 char *file_name = NULL;
217 int32_t prior_linnum = 0;
218 int lineinc = 0;
220 if (*outname) {
221 ofile = fopen(outname, "w");
222 if (!ofile)
223 report_error(ERR_FATAL | ERR_NOFILE,
224 "unable to open output file `%s'",
225 outname);
226 } else
227 ofile = NULL;
229 location.known = FALSE;
231 /* pass = 1; */
232 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
233 while ((line = preproc->getline())) {
235 * We generate %line directives if needed for later programs
237 int32_t linnum = prior_linnum += lineinc;
238 int altline = src_get(&linnum, &file_name);
239 if (altline) {
240 if (altline == 1 && lineinc == 1)
241 nasm_fputs("", ofile);
242 else {
243 lineinc = (altline != -1 || lineinc != 1);
244 fprintf(ofile ? ofile : stdout,
245 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
246 file_name);
248 prior_linnum = linnum;
250 nasm_fputs(line, ofile);
251 nasm_free(line);
253 nasm_free(file_name);
254 preproc->cleanup(0);
255 if (ofile)
256 fclose(ofile);
257 if (ofile && terminate_after_phase)
258 remove(outname);
260 break;
262 case op_normal:
265 * We must call ofmt->filename _anyway_, even if the user
266 * has specified their own output file, because some
267 * formats (eg OBJ and COFF) use ofmt->filename to find out
268 * the name of the input file and then put that inside the
269 * file.
271 ofmt->filename(inname, outname, report_error);
273 ofile = fopen(outname, "wb");
274 if (!ofile) {
275 report_error(ERR_FATAL | ERR_NOFILE,
276 "unable to open output file `%s'", outname);
280 * We must call init_labels() before ofmt->init() since
281 * some object formats will want to define labels in their
282 * init routines. (eg OS/2 defines the FLAT group)
284 init_labels();
286 ofmt->init(ofile, report_error, define_label, evaluate);
288 assemble_file(inname);
290 if (!terminate_after_phase) {
291 ofmt->cleanup(using_debug_info);
292 cleanup_labels();
293 } else {
295 * We had an fclose on the output file here, but we
296 * actually do that in all the object file drivers as well,
297 * so we're leaving out the one here.
298 * fclose (ofile);
300 remove(outname);
301 if (listname[0])
302 remove(listname);
305 break;
308 if (want_usage)
309 usage();
311 raa_free(offsets);
312 saa_free(forwrefs);
313 eval_cleanup();
314 nasmlib_cleanup();
316 if (terminate_after_phase)
317 return 1;
318 else
319 return 0;
323 * Get a parameter for a command line option.
324 * First arg must be in the form of e.g. -f...
326 static char *get_param(char *p, char *q, int *advance)
328 *advance = 0;
329 if (p[2]) { /* the parameter's in the option */
330 p += 2;
331 while (isspace(*p))
332 p++;
333 return p;
335 if (q && q[0]) {
336 *advance = 1;
337 return q;
339 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
340 "option `-%c' requires an argument", p[1]);
341 return NULL;
344 struct textargs {
345 const char *label;
346 int value;
349 #define OPT_PREFIX 0
350 #define OPT_POSTFIX 1
351 struct textargs textopts[] = {
352 {"prefix", OPT_PREFIX},
353 {"postfix", OPT_POSTFIX},
354 {NULL, 0}
357 int stopoptions = 0;
358 static int process_arg(char *p, char *q)
360 char *param;
361 int i, advance = 0;
363 if (!p || !p[0])
364 return 0;
366 if (p[0] == '-' && !stopoptions) {
367 switch (p[1]) {
368 case 's':
369 error_file = stdout;
370 break;
371 case 'o': /* these parameters take values */
372 case 'O':
373 case 'f':
374 case 'p':
375 case 'P':
376 case 'd':
377 case 'D':
378 case 'i':
379 case 'I':
380 case 'l':
381 case 'E':
382 case 'F':
383 case 'X':
384 case 'u':
385 case 'U':
386 if (!(param = get_param(p, q, &advance)))
387 break;
388 if (p[1] == 'o') { /* output file */
389 strcpy(outname, param);
390 } else if (p[1] == 'f') { /* output format */
391 ofmt = ofmt_find(param);
392 if (!ofmt) {
393 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
394 "unrecognised output format `%s' - "
395 "use -hf for a list", param);
396 } else
397 ofmt->current_dfmt = ofmt->debug_formats[0];
398 } else if (p[1] == 'O') { /* Optimization level */
399 int opt;
400 opt = -99;
401 while (*param) {
402 if (isdigit(*param)) {
403 opt = atoi(param);
404 while (isdigit(*++param)) ;
405 if (opt <= 0)
406 optimizing = -1; /* 0.98 behaviour */
407 else if (opt == 1)
408 optimizing = 0; /* Two passes, 0.98.09 behavior */
409 else
410 optimizing = opt; /* Multiple passes */
411 } else {
412 if (*param == 'v' || *param == '+') {
413 ++param;
414 opt_verbose_info = TRUE;
415 opt = 0;
416 } else { /* garbage */
417 opt = -99;
418 break;
421 } /* while (*param) */
422 if (opt == -99)
423 report_error(ERR_FATAL,
424 "command line optimization level must be 'v', 0..3 or <nn>");
425 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
426 pp_pre_include(param);
427 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
428 pp_pre_define(param);
429 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
430 pp_pre_undefine(param);
431 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
432 pp_include_path(param);
433 } else if (p[1] == 'l') { /* listing file */
434 strcpy(listname, param);
435 } else if (p[1] == 'E') { /* error messages file */
436 error_file = fopen(param, "w");
437 if (!error_file) {
438 error_file = stderr; /* Revert to default! */
439 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
440 "cannot open file `%s' for error messages",
441 param);
443 } else if (p[1] == 'F') { /* specify debug format */
444 ofmt->current_dfmt = dfmt_find(ofmt, param);
445 if (!ofmt->current_dfmt) {
446 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
447 "unrecognized debug format `%s' for"
448 " output format `%s'",
449 param, ofmt->shortname);
451 } else if (p[1] == 'X') { /* specify error reporting format */
452 if (nasm_stricmp("vc", param) == 0)
453 report_error = report_error_vc;
454 else if (nasm_stricmp("gnu", param) == 0)
455 report_error = report_error_gnu;
456 else
457 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
458 "unrecognized error reporting format `%s'",
459 param);
461 break;
462 case 'g':
463 using_debug_info = TRUE;
464 break;
465 case 'h':
466 printf
467 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
468 "[-l listfile]\n"
469 " [options...] [--] filename\n"
470 " or nasm -r for version info (obsolete)\n"
471 " or nasm -v for version info (preferred)\n\n"
472 " -t assemble in SciTech TASM compatible mode\n"
473 " -g generate debug information in selected format.\n");
474 printf
475 (" -e preprocess only (writes output to stdout by default)\n"
476 " -a don't preprocess (assemble only)\n"
477 " -M generate Makefile dependencies on stdout\n\n"
478 " -E<file> redirect error messages to file\n"
479 " -s redirect error messages to stdout\n\n"
480 " -F format select a debugging format\n\n"
481 " -I<path> adds a pathname to the include file path\n");
482 printf
483 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
484 " -P<file> pre-includes a file\n"
485 " -D<macro>[=<value>] pre-defines a macro\n"
486 " -U<macro> undefines a macro\n"
487 " -X<format> specifies error reporting format (gnu or vc)\n"
488 " -w+foo enables warnings about foo; -w-foo disables them\n"
489 "where foo can be:\n");
490 for (i = 1; i <= ERR_WARN_MAX; i++)
491 printf(" %-23s %s (default %s)\n",
492 suppressed_names[i], suppressed_what[i],
493 suppressed[i] ? "off" : "on");
494 printf
495 ("\nresponse files should contain command line parameters"
496 ", one per line.\n");
497 if (p[2] == 'f') {
498 printf("\nvalid output formats for -f are"
499 " (`*' denotes default):\n");
500 ofmt_list(ofmt, stdout);
501 } else {
502 printf("\nFor a list of valid output formats, use -hf.\n");
503 printf("For a list of debug formats, use -f <form> -y.\n");
505 exit(0); /* never need usage message here */
506 break;
507 case 'y':
508 printf("\nvalid debug formats for '%s' output format are"
509 " ('*' denotes default):\n", ofmt->shortname);
510 dfmt_list(ofmt, stdout);
511 exit(0);
512 break;
513 case 't':
514 tasm_compatible_mode = TRUE;
515 break;
516 case 'r':
517 case 'v':
519 const char *nasm_version_string =
520 "NASM version " NASM_VER " compiled on " __DATE__
521 #ifdef DEBUG
522 " with -DDEBUG"
523 #endif
525 puts(nasm_version_string);
526 exit(0); /* never need usage message here */
528 break;
529 case 'e': /* preprocess only */
530 operating_mode = op_preprocess;
531 break;
532 case 'a': /* assemble only - don't preprocess */
533 preproc = &no_pp;
534 break;
535 case 'w':
536 if (p[2] != '+' && p[2] != '-') {
537 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
538 "invalid option to `-w'");
539 } else {
540 for (i = 1; i <= ERR_WARN_MAX; i++)
541 if (!nasm_stricmp(p + 3, suppressed_names[i]))
542 break;
543 if (i <= ERR_WARN_MAX)
544 suppressed[i] = (p[2] == '-');
545 else
546 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
547 "invalid option to `-w'");
549 break;
550 case 'M':
551 operating_mode = op_depend;
552 break;
554 case '-':
556 int s;
558 if (p[2] == 0) { /* -- => stop processing options */
559 stopoptions = 1;
560 break;
562 for (s = 0; textopts[s].label; s++) {
563 if (!nasm_stricmp(p + 2, textopts[s].label)) {
564 break;
568 switch (s) {
570 case OPT_PREFIX:
571 case OPT_POSTFIX:
573 if (!q) {
574 report_error(ERR_NONFATAL | ERR_NOFILE |
575 ERR_USAGE,
576 "option `--%s' requires an argument",
577 p + 2);
578 break;
579 } else {
580 advance = 1, param = q;
583 if (s == OPT_PREFIX) {
584 strncpy(lprefix, param, PREFIX_MAX - 1);
585 lprefix[PREFIX_MAX - 1] = 0;
586 break;
588 if (s == OPT_POSTFIX) {
589 strncpy(lpostfix, param, POSTFIX_MAX - 1);
590 lpostfix[POSTFIX_MAX - 1] = 0;
591 break;
593 break;
595 default:
597 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
598 "unrecognised option `--%s'", p + 2);
599 break;
602 break;
605 default:
606 if (!ofmt->setinfo(GI_SWITCH, &p))
607 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
608 "unrecognised option `-%c'", p[1]);
609 break;
611 } else {
612 if (*inname) {
613 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
614 "more than one input file specified");
615 } else
616 strcpy(inname, p);
619 return advance;
622 #define ARG_BUF_DELTA 128
624 static void process_respfile(FILE * rfile)
626 char *buffer, *p, *q, *prevarg;
627 int bufsize, prevargsize;
629 bufsize = prevargsize = ARG_BUF_DELTA;
630 buffer = nasm_malloc(ARG_BUF_DELTA);
631 prevarg = nasm_malloc(ARG_BUF_DELTA);
632 prevarg[0] = '\0';
634 while (1) { /* Loop to handle all lines in file */
636 p = buffer;
637 while (1) { /* Loop to handle long lines */
638 q = fgets(p, bufsize - (p - buffer), rfile);
639 if (!q)
640 break;
641 p += strlen(p);
642 if (p > buffer && p[-1] == '\n')
643 break;
644 if (p - buffer > bufsize - 10) {
645 int offset;
646 offset = p - buffer;
647 bufsize += ARG_BUF_DELTA;
648 buffer = nasm_realloc(buffer, bufsize);
649 p = buffer + offset;
653 if (!q && p == buffer) {
654 if (prevarg[0])
655 process_arg(prevarg, NULL);
656 nasm_free(buffer);
657 nasm_free(prevarg);
658 return;
662 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
663 * them are present at the end of the line.
665 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
667 while (p > buffer && isspace(p[-1]))
668 *--p = '\0';
670 p = buffer;
671 while (isspace(*p))
672 p++;
674 if (process_arg(prevarg, p))
675 *p = '\0';
677 if (strlen(p) > prevargsize - 10) {
678 prevargsize += ARG_BUF_DELTA;
679 prevarg = nasm_realloc(prevarg, prevargsize);
681 strcpy(prevarg, p);
685 /* Function to process args from a string of args, rather than the
686 * argv array. Used by the environment variable and response file
687 * processing.
689 static void process_args(char *args)
691 char *p, *q, *arg, *prevarg;
692 char separator = ' ';
694 p = args;
695 if (*p && *p != '-')
696 separator = *p++;
697 arg = NULL;
698 while (*p) {
699 q = p;
700 while (*p && *p != separator)
701 p++;
702 while (*p == separator)
703 *p++ = '\0';
704 prevarg = arg;
705 arg = q;
706 if (process_arg(prevarg, arg))
707 arg = NULL;
709 if (arg)
710 process_arg(arg, NULL);
713 static void parse_cmdline(int argc, char **argv)
715 FILE *rfile;
716 char *envreal, *envcopy = NULL, *p, *arg;
718 *inname = *outname = *listname = '\0';
721 * First, process the NASMENV environment variable.
723 envreal = getenv("NASMENV");
724 arg = NULL;
725 if (envreal) {
726 envcopy = nasm_strdup(envreal);
727 process_args(envcopy);
728 nasm_free(envcopy);
732 * Now process the actual command line.
734 while (--argc) {
735 int i;
736 argv++;
737 if (argv[0][0] == '@') {
738 /* We have a response file, so process this as a set of
739 * arguments like the environment variable. This allows us
740 * to have multiple arguments on a single line, which is
741 * different to the -@resp file processing below for regular
742 * NASM.
744 char *str = malloc(2048);
745 FILE *f = fopen(&argv[0][1], "r");
746 if (!str) {
747 printf("out of memory");
748 exit(-1);
750 if (f) {
751 while (fgets(str, 2048, f)) {
752 process_args(str);
754 fclose(f);
756 free(str);
757 argc--;
758 argv++;
760 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
761 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i);
762 if (p) {
763 rfile = fopen(p, "r");
764 if (rfile) {
765 process_respfile(rfile);
766 fclose(rfile);
767 } else
768 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
769 "unable to open response file `%s'", p);
771 } else
772 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
773 argv += i, argc -= i;
776 if (!*inname)
777 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
778 "no input file specified");
781 static void assemble_file(char *fname)
783 char *directive, *value, *p, *q, *special, *line, debugid[80];
784 insn output_ins;
785 int i, rn_error, validid;
786 int32_t seg, offs;
787 struct tokenval tokval;
788 expr *e;
789 int pass, pass_max;
790 int pass_cnt = 0; /* count actual passes */
792 if (cmd_sb == 32 && cmd_cpu < IF_386)
793 report_error(ERR_FATAL, "command line: "
794 "32-bit segment size requires a higher cpu");
796 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
797 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
798 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
799 int pass1, pass2;
800 ldfunc def_label;
802 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
803 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
804 /* pass0 seq is 0, 0, 0,..., 1, 2 */
806 def_label = pass > 1 ? redefine_label : define_label;
808 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
809 cpu = cmd_cpu;
810 if (pass0 == 2) {
811 if (*listname)
812 nasmlist.init(listname, report_error);
814 in_abs_seg = FALSE;
815 global_offset_changed = FALSE; /* set by redefine_label */
816 location.segment = ofmt->section(NULL, pass2, &sb);
817 globalbits = sb;
818 if (pass > 1) {
819 saa_rewind(forwrefs);
820 forwref = saa_rstruct(forwrefs);
821 raa_free(offsets);
822 offsets = raa_init();
824 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
825 globallineno = 0;
826 if (pass == 1)
827 location.known = TRUE;
828 location.offset = offs = GET_CURR_OFFS;
830 while ((line = preproc->getline())) {
831 globallineno++;
833 /* here we parse our directives; this is not handled by the 'real'
834 * parser. */
835 directive = line;
836 i = getkw(&directive, &value);
837 if (i) {
838 switch (i) {
839 case 1: /* [SEGMENT n] */
840 seg = ofmt->section(value, pass2, &sb);
841 if (seg == NO_SEG) {
842 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
843 "segment name `%s' not recognized",
844 value);
845 } else {
846 in_abs_seg = FALSE;
847 location.segment = seg;
849 break;
850 case 2: /* [EXTERN label:special] */
851 if (*value == '$')
852 value++; /* skip initial $ if present */
853 if (pass0 == 2) {
854 q = value;
855 while (*q && *q != ':')
856 q++;
857 if (*q == ':') {
858 *q++ = '\0';
859 ofmt->symdef(value, 0L, 0L, 3, q);
861 } else if (pass == 1) { /* pass == 1 */
862 q = value;
863 validid = TRUE;
864 if (!isidstart(*q))
865 validid = FALSE;
866 while (*q && *q != ':') {
867 if (!isidchar(*q))
868 validid = FALSE;
869 q++;
871 if (!validid) {
872 report_error(ERR_NONFATAL,
873 "identifier expected after EXTERN");
874 break;
876 if (*q == ':') {
877 *q++ = '\0';
878 special = q;
879 } else
880 special = NULL;
881 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
882 int temp = pass0;
883 pass0 = 1; /* fake pass 1 in labels.c */
884 declare_as_global(value, special,
885 report_error);
886 define_label(value, seg_alloc(), 0L, NULL,
887 FALSE, TRUE, ofmt, report_error);
888 pass0 = temp;
890 } /* else pass0 == 1 */
891 break;
892 case 3: /* [BITS bits] */
893 globalbits = sb = get_bits(value);
894 break;
895 case 4: /* [GLOBAL symbol:special] */
896 if (*value == '$')
897 value++; /* skip initial $ if present */
898 if (pass0 == 2) { /* pass 2 */
899 q = value;
900 while (*q && *q != ':')
901 q++;
902 if (*q == ':') {
903 *q++ = '\0';
904 ofmt->symdef(value, 0L, 0L, 3, q);
906 } else if (pass2 == 1) { /* pass == 1 */
907 q = value;
908 validid = TRUE;
909 if (!isidstart(*q))
910 validid = FALSE;
911 while (*q && *q != ':') {
912 if (!isidchar(*q))
913 validid = FALSE;
914 q++;
916 if (!validid) {
917 report_error(ERR_NONFATAL,
918 "identifier expected after GLOBAL");
919 break;
921 if (*q == ':') {
922 *q++ = '\0';
923 special = q;
924 } else
925 special = NULL;
926 declare_as_global(value, special, report_error);
927 } /* pass == 1 */
928 break;
929 case 5: /* [COMMON symbol size:special] */
930 if (*value == '$')
931 value++; /* skip initial $ if present */
932 if (pass0 == 1) {
933 p = value;
934 validid = TRUE;
935 if (!isidstart(*p))
936 validid = FALSE;
937 while (*p && !isspace(*p)) {
938 if (!isidchar(*p))
939 validid = FALSE;
940 p++;
942 if (!validid) {
943 report_error(ERR_NONFATAL,
944 "identifier expected after COMMON");
945 break;
947 if (*p) {
948 int64_t size;
950 while (*p && isspace(*p))
951 *p++ = '\0';
952 q = p;
953 while (*q && *q != ':')
954 q++;
955 if (*q == ':') {
956 *q++ = '\0';
957 special = q;
958 } else
959 special = NULL;
960 size = readnum(p, &rn_error);
961 if (rn_error)
962 report_error(ERR_NONFATAL,
963 "invalid size specified"
964 " in COMMON declaration");
965 else
966 define_common(value, seg_alloc(), size,
967 special, ofmt, report_error);
968 } else
969 report_error(ERR_NONFATAL,
970 "no size specified in"
971 " COMMON declaration");
972 } else if (pass0 == 2) { /* pass == 2 */
973 q = value;
974 while (*q && *q != ':') {
975 if (isspace(*q))
976 *q = '\0';
977 q++;
979 if (*q == ':') {
980 *q++ = '\0';
981 ofmt->symdef(value, 0L, 0L, 3, q);
984 break;
985 case 6: /* [ABSOLUTE address] */
986 stdscan_reset();
987 stdscan_bufptr = value;
988 tokval.t_type = TOKEN_INVALID;
989 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
990 report_error, NULL);
991 if (e) {
992 if (!is_reloc(e))
993 report_error(pass0 ==
994 1 ? ERR_NONFATAL : ERR_PANIC,
995 "cannot use non-relocatable expression as "
996 "ABSOLUTE address");
997 else {
998 abs_seg = reloc_seg(e);
999 abs_offset = reloc_value(e);
1001 } else if (pass == 1)
1002 abs_offset = 0x100; /* don't go near zero in case of / */
1003 else
1004 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1005 "in pass two");
1006 in_abs_seg = TRUE;
1007 location.segment = NO_SEG;
1008 break;
1009 case 7: /* DEBUG */
1010 p = value;
1011 q = debugid;
1012 validid = TRUE;
1013 if (!isidstart(*p))
1014 validid = FALSE;
1015 while (*p && !isspace(*p)) {
1016 if (!isidchar(*p))
1017 validid = FALSE;
1018 *q++ = *p++;
1020 *q++ = 0;
1021 if (!validid) {
1022 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1023 "identifier expected after DEBUG");
1024 break;
1026 while (*p && isspace(*p))
1027 p++;
1028 if (pass == pass_max)
1029 ofmt->current_dfmt->debug_directive(debugid, p);
1030 break;
1031 case 8: /* [WARNING {+|-}warn-name] */
1032 if (pass1 == 1) {
1033 while (*value && isspace(*value))
1034 value++;
1036 if (*value == '+' || *value == '-') {
1037 validid = (*value == '-') ? TRUE : FALSE;
1038 value++;
1039 } else
1040 validid = FALSE;
1042 for (i = 1; i <= ERR_WARN_MAX; i++)
1043 if (!nasm_stricmp(value, suppressed_names[i]))
1044 break;
1045 if (i <= ERR_WARN_MAX)
1046 suppressed[i] = validid;
1047 else
1048 report_error(ERR_NONFATAL,
1049 "invalid warning id in WARNING directive");
1051 break;
1052 case 9: /* cpu */
1053 cpu = get_cpu(value);
1054 break;
1055 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1056 while (*value && isspace(*value))
1057 value++;
1059 if (*value == '+') {
1060 user_nolist = 0;
1061 } else {
1062 if (*value == '-') {
1063 user_nolist = 1;
1064 } else {
1065 report_error(ERR_NONFATAL,
1066 "invalid parameter to \"list\" directive");
1069 break;
1070 default:
1071 if (!ofmt->directive(directive, value, pass2))
1072 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1073 "unrecognised directive [%s]",
1074 directive);
1076 } else { /* it isn't a directive */
1078 parse_line(pass1, line, &output_ins,
1079 report_error, evaluate, def_label);
1081 if (!(optimizing > 0) && pass == 2) {
1082 if (forwref != NULL && globallineno == forwref->lineno) {
1083 output_ins.forw_ref = TRUE;
1084 do {
1085 output_ins.oprs[forwref->operand].opflags |=
1086 OPFLAG_FORWARD;
1087 forwref = saa_rstruct(forwrefs);
1088 } while (forwref != NULL
1089 && forwref->lineno == globallineno);
1090 } else
1091 output_ins.forw_ref = FALSE;
1094 if (!(optimizing > 0) && output_ins.forw_ref) {
1095 if (pass == 1) {
1096 for (i = 0; i < output_ins.operands; i++) {
1097 if (output_ins.oprs[i].
1098 opflags & OPFLAG_FORWARD) {
1099 struct forwrefinfo *fwinf =
1100 (struct forwrefinfo *)
1101 saa_wstruct(forwrefs);
1102 fwinf->lineno = globallineno;
1103 fwinf->operand = i;
1106 } else { /* pass == 2 */
1108 * Hack to prevent phase error in the code
1109 * rol ax,x
1110 * x equ 1
1112 * If the second operand is a forward reference,
1113 * the UNITY property of the number 1 in that
1114 * operand is cancelled. Otherwise the above
1115 * sequence will cause a phase error.
1117 * This hack means that the above code will
1118 * generate 286+ code.
1120 * The forward reference will mean that the
1121 * operand will not have the UNITY property on
1122 * the first pass, so the pass behaviours will
1123 * be consistent.
1126 if (output_ins.operands >= 2 &&
1127 (output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
1128 !(IMMEDIATE & ~output_ins.oprs[1].type))
1130 /* Remove special properties bits */
1131 output_ins.oprs[1].type &= ~REG_SMASK;
1134 } /* pass == 2 */
1138 /* forw_ref */
1139 if (output_ins.opcode == I_EQU) {
1140 if (pass1 == 1) {
1142 * Special `..' EQUs get processed in pass two,
1143 * except `..@' macro-processor EQUs which are done
1144 * in the normal place.
1146 if (!output_ins.label)
1147 report_error(ERR_NONFATAL,
1148 "EQU not preceded by label");
1150 else if (output_ins.label[0] != '.' ||
1151 output_ins.label[1] != '.' ||
1152 output_ins.label[2] == '@') {
1153 if (output_ins.operands == 1 &&
1154 (output_ins.oprs[0].type & IMMEDIATE) &&
1155 output_ins.oprs[0].wrt == NO_SEG) {
1156 int isext =
1157 output_ins.oprs[0].
1158 opflags & OPFLAG_EXTERN;
1159 def_label(output_ins.label,
1160 output_ins.oprs[0].segment,
1161 output_ins.oprs[0].offset, NULL,
1162 FALSE, isext, ofmt,
1163 report_error);
1164 } else if (output_ins.operands == 2
1165 && (output_ins.oprs[0].
1166 type & IMMEDIATE)
1167 && (output_ins.oprs[0].type & COLON)
1168 && output_ins.oprs[0].segment ==
1169 NO_SEG
1170 && output_ins.oprs[0].wrt == NO_SEG
1171 && (output_ins.oprs[1].
1172 type & IMMEDIATE)
1173 && output_ins.oprs[1].segment ==
1174 NO_SEG
1175 && output_ins.oprs[1].wrt ==
1176 NO_SEG) {
1177 def_label(output_ins.label,
1178 output_ins.oprs[0].
1179 offset | SEG_ABS,
1180 output_ins.oprs[1].offset, NULL,
1181 FALSE, FALSE, ofmt,
1182 report_error);
1183 } else
1184 report_error(ERR_NONFATAL,
1185 "bad syntax for EQU");
1187 } else { /* pass == 2 */
1189 * Special `..' EQUs get processed here, except
1190 * `..@' macro processor EQUs which are done above.
1192 if (output_ins.label[0] == '.' &&
1193 output_ins.label[1] == '.' &&
1194 output_ins.label[2] != '@') {
1195 if (output_ins.operands == 1 &&
1196 (output_ins.oprs[0].type & IMMEDIATE)) {
1197 define_label(output_ins.label,
1198 output_ins.oprs[0].segment,
1199 output_ins.oprs[0].offset,
1200 NULL, FALSE, FALSE, ofmt,
1201 report_error);
1202 } else if (output_ins.operands == 2
1203 && (output_ins.oprs[0].
1204 type & IMMEDIATE)
1205 && (output_ins.oprs[0].type & COLON)
1206 && output_ins.oprs[0].segment ==
1207 NO_SEG
1208 && (output_ins.oprs[1].
1209 type & IMMEDIATE)
1210 && output_ins.oprs[1].segment ==
1211 NO_SEG) {
1212 define_label(output_ins.label,
1213 output_ins.oprs[0].
1214 offset | SEG_ABS,
1215 output_ins.oprs[1].offset,
1216 NULL, FALSE, FALSE, ofmt,
1217 report_error);
1218 } else
1219 report_error(ERR_NONFATAL,
1220 "bad syntax for EQU");
1222 } /* pass == 2 */
1223 } else { /* instruction isn't an EQU */
1225 if (pass1 == 1) {
1227 int32_t l = insn_size(location.segment, offs, sb, cpu,
1228 &output_ins, report_error);
1230 /* if (using_debug_info) && output_ins.opcode != -1) */
1231 if (using_debug_info)
1232 { /* fbk 03/25/01 */
1233 /* this is done here so we can do debug type info */
1234 int32_t typeinfo =
1235 TYS_ELEMENTS(output_ins.operands);
1236 switch (output_ins.opcode) {
1237 case I_RESB:
1238 typeinfo =
1239 TYS_ELEMENTS(output_ins.oprs[0].
1240 offset) | TY_BYTE;
1241 break;
1242 case I_RESW:
1243 typeinfo =
1244 TYS_ELEMENTS(output_ins.oprs[0].
1245 offset) | TY_WORD;
1246 break;
1247 case I_RESD:
1248 typeinfo =
1249 TYS_ELEMENTS(output_ins.oprs[0].
1250 offset) | TY_DWORD;
1251 break;
1252 case I_RESQ:
1253 typeinfo =
1254 TYS_ELEMENTS(output_ins.oprs[0].
1255 offset) | TY_QWORD;
1256 break;
1257 case I_REST:
1258 typeinfo =
1259 TYS_ELEMENTS(output_ins.oprs[0].
1260 offset) | TY_TBYTE;
1261 break;
1262 case I_DB:
1263 typeinfo |= TY_BYTE;
1264 break;
1265 case I_DW:
1266 typeinfo |= TY_WORD;
1267 break;
1268 case I_DD:
1269 if (output_ins.eops_float)
1270 typeinfo |= TY_FLOAT;
1271 else
1272 typeinfo |= TY_DWORD;
1273 break;
1274 case I_DQ:
1275 typeinfo |= TY_QWORD;
1276 break;
1277 case I_DT:
1278 typeinfo |= TY_TBYTE;
1279 break;
1280 default:
1281 typeinfo = TY_LABEL;
1285 ofmt->current_dfmt->debug_typevalue(typeinfo);
1288 if (l != -1) {
1289 offs += l;
1290 SET_CURR_OFFS(offs);
1293 * else l == -1 => invalid instruction, which will be
1294 * flagged as an error on pass 2
1297 } else { /* pass == 2 */
1298 offs += assemble(location.segment, offs, sb, cpu,
1299 &output_ins, ofmt, report_error,
1300 &nasmlist);
1301 SET_CURR_OFFS(offs);
1304 } /* not an EQU */
1305 cleanup_insn(&output_ins);
1307 nasm_free(line);
1308 location.offset = offs = GET_CURR_OFFS;
1309 } /* end while (line = preproc->getline... */
1311 if (pass1 == 2 && global_offset_changed)
1312 report_error(ERR_NONFATAL,
1313 "phase error detected at end of assembly.");
1315 if (pass1 == 1)
1316 preproc->cleanup(1);
1318 if (pass1 == 1 && terminate_after_phase) {
1319 fclose(ofile);
1320 remove(outname);
1321 if (want_usage)
1322 usage();
1323 exit(1);
1325 pass_cnt++;
1326 if (pass > 1 && !global_offset_changed) {
1327 pass0++;
1328 if (pass0 == 2)
1329 pass = pass_max - 1;
1330 } else if (!(optimizing > 0))
1331 pass0++;
1333 } /* for (pass=1; pass<=2; pass++) */
1335 preproc->cleanup(0);
1336 nasmlist.cleanup();
1337 #if 1
1338 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1339 fprintf(stdout,
1340 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1341 #endif
1342 } /* exit from assemble_file (...) */
1344 static int getkw(char **directive, char **value)
1346 char *p, *q, *buf;
1348 buf = *directive;
1350 /* allow leading spaces or tabs */
1351 while (*buf == ' ' || *buf == '\t')
1352 buf++;
1354 if (*buf != '[')
1355 return 0;
1357 p = buf;
1359 while (*p && *p != ']')
1360 p++;
1362 if (!*p)
1363 return 0;
1365 q = p++;
1367 while (*p && *p != ';') {
1368 if (!isspace(*p))
1369 return 0;
1370 p++;
1372 q[1] = '\0';
1374 *directive = p = buf + 1;
1375 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1376 buf++;
1377 if (*buf == ']') {
1378 *buf = '\0';
1379 *value = buf;
1380 } else {
1381 *buf++ = '\0';
1382 while (isspace(*buf))
1383 buf++; /* beppu - skip leading whitespace */
1384 *value = buf;
1385 while (*buf != ']')
1386 buf++;
1387 *buf++ = '\0';
1389 #if 0
1390 for (q = p; *q; q++)
1391 *q = tolower(*q);
1392 #endif
1393 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1394 return 1;
1395 if (!nasm_stricmp(p, "extern"))
1396 return 2;
1397 if (!nasm_stricmp(p, "bits"))
1398 return 3;
1399 if (!nasm_stricmp(p, "global"))
1400 return 4;
1401 if (!nasm_stricmp(p, "common"))
1402 return 5;
1403 if (!nasm_stricmp(p, "absolute"))
1404 return 6;
1405 if (!nasm_stricmp(p, "debug"))
1406 return 7;
1407 if (!nasm_stricmp(p, "warning"))
1408 return 8;
1409 if (!nasm_stricmp(p, "cpu"))
1410 return 9;
1411 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1412 return 10;
1413 return -1;
1417 * gnu style error reporting
1418 * This function prints an error message to error_file in the
1419 * style used by GNU. An example would be:
1420 * file.asm:50: error: blah blah blah
1421 * where file.asm is the name of the file, 50 is the line number on
1422 * which the error occurs (or is detected) and "error:" is one of
1423 * the possible optional diagnostics -- it can be "error" or "warning"
1424 * or something else. Finally the line terminates with the actual
1425 * error message.
1427 * @param severity the severity of the warning or error
1428 * @param fmt the printf style format string
1430 static void report_error_gnu(int severity, const char *fmt, ...)
1432 va_list ap;
1434 if (is_suppressed_warning(severity))
1435 return;
1437 if (severity & ERR_NOFILE)
1438 fputs("nasm: ", error_file);
1439 else {
1440 char *currentfile = NULL;
1441 int32_t lineno = 0;
1442 src_get(&lineno, &currentfile);
1443 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1444 nasm_free(currentfile);
1446 va_start(ap, fmt);
1447 report_error_common(severity, fmt, ap);
1448 va_end(ap);
1452 * MS style error reporting
1453 * This function prints an error message to error_file in the
1454 * style used by Visual C and some other Microsoft tools. An example
1455 * would be:
1456 * file.asm(50) : error: blah blah blah
1457 * where file.asm is the name of the file, 50 is the line number on
1458 * which the error occurs (or is detected) and "error:" is one of
1459 * the possible optional diagnostics -- it can be "error" or "warning"
1460 * or something else. Finally the line terminates with the actual
1461 * error message.
1463 * @param severity the severity of the warning or error
1464 * @param fmt the printf style format string
1466 static void report_error_vc(int severity, const char *fmt, ...)
1468 va_list ap;
1470 if (is_suppressed_warning(severity))
1471 return;
1473 if (severity & ERR_NOFILE)
1474 fputs("nasm: ", error_file);
1475 else {
1476 char *currentfile = NULL;
1477 int32_t lineno = 0;
1478 src_get(&lineno, &currentfile);
1479 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1480 nasm_free(currentfile);
1482 va_start(ap, fmt);
1483 report_error_common(severity, fmt, ap);
1484 va_end(ap);
1488 * check for supressed warning
1489 * checks for suppressed warning or pass one only warning and we're
1490 * not in pass 1
1492 * @param severity the severity of the warning or error
1493 * @return true if we should abort error/warning printing
1495 static int is_suppressed_warning(int severity)
1498 * See if it's a suppressed warning.
1500 return ((severity & ERR_MASK) == ERR_WARNING &&
1501 (severity & ERR_WARN_MASK) != 0 &&
1502 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1504 * See if it's a pass-one only warning and we're not in pass one.
1506 ((severity & ERR_PASS1) && pass0 == 2);
1510 * common error reporting
1511 * This is the common back end of the error reporting schemes currently
1512 * implemented. It prints the nature of the warning and then the
1513 * specific error message to error_file and may or may not return. It
1514 * doesn't return if the error severity is a "panic" or "debug" type.
1516 * @param severity the severity of the warning or error
1517 * @param fmt the printf style format string
1519 static void report_error_common(int severity, const char *fmt,
1520 va_list args)
1522 switch (severity & ERR_MASK) {
1523 case ERR_WARNING:
1524 fputs("warning: ", error_file);
1525 break;
1526 case ERR_NONFATAL:
1527 fputs("error: ", error_file);
1528 break;
1529 case ERR_FATAL:
1530 fputs("fatal: ", error_file);
1531 break;
1532 case ERR_PANIC:
1533 fputs("panic: ", error_file);
1534 break;
1535 case ERR_DEBUG:
1536 fputs("debug: ", error_file);
1537 break;
1540 vfprintf(error_file, fmt, args);
1541 fputc('\n', error_file);
1543 if (severity & ERR_USAGE)
1544 want_usage = TRUE;
1546 switch (severity & ERR_MASK) {
1547 case ERR_WARNING:
1548 case ERR_DEBUG:
1549 /* no further action, by definition */
1550 break;
1551 case ERR_NONFATAL:
1552 /* hack enables listing(!) on errors */
1553 terminate_after_phase = TRUE;
1554 break;
1555 case ERR_FATAL:
1556 if (ofile) {
1557 fclose(ofile);
1558 remove(outname);
1560 if (want_usage)
1561 usage();
1562 exit(1); /* instantly die */
1563 break; /* placate silly compilers */
1564 case ERR_PANIC:
1565 fflush(NULL);
1566 /* abort(); *//* halt, catch fire, and dump core */
1567 exit(3);
1568 break;
1572 static void usage(void)
1574 fputs("type `nasm -h' for help\n", error_file);
1577 static void register_output_formats(void)
1579 ofmt = ofmt_register(report_error);
1582 #define BUF_DELTA 512
1584 static FILE *no_pp_fp;
1585 static efunc no_pp_err;
1586 static ListGen *no_pp_list;
1587 static int32_t no_pp_lineinc;
1589 static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
1590 ListGen * listgen)
1592 src_set_fname(nasm_strdup(file));
1593 src_set_linnum(0);
1594 no_pp_lineinc = 1;
1595 no_pp_err = error;
1596 no_pp_fp = fopen(file, "r");
1597 if (!no_pp_fp)
1598 no_pp_err(ERR_FATAL | ERR_NOFILE,
1599 "unable to open input file `%s'", file);
1600 no_pp_list = listgen;
1601 (void)pass; /* placate compilers */
1602 (void)eval; /* placate compilers */
1605 static char *no_pp_getline(void)
1607 char *buffer, *p, *q;
1608 int bufsize;
1610 bufsize = BUF_DELTA;
1611 buffer = nasm_malloc(BUF_DELTA);
1612 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1614 while (1) { /* Loop to handle %line */
1616 p = buffer;
1617 while (1) { /* Loop to handle long lines */
1618 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1619 if (!q)
1620 break;
1621 p += strlen(p);
1622 if (p > buffer && p[-1] == '\n')
1623 break;
1624 if (p - buffer > bufsize - 10) {
1625 int offset;
1626 offset = p - buffer;
1627 bufsize += BUF_DELTA;
1628 buffer = nasm_realloc(buffer, bufsize);
1629 p = buffer + offset;
1633 if (!q && p == buffer) {
1634 nasm_free(buffer);
1635 return NULL;
1639 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1640 * them are present at the end of the line.
1642 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1644 if (!strncmp(buffer, "%line", 5)) {
1645 int32_t ln;
1646 int li;
1647 char *nm = nasm_malloc(strlen(buffer));
1648 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
1649 nasm_free(src_set_fname(nm));
1650 src_set_linnum(ln);
1651 no_pp_lineinc = li;
1652 continue;
1654 nasm_free(nm);
1656 break;
1659 no_pp_list->line(LIST_READ, buffer);
1661 return buffer;
1664 static void no_pp_cleanup(int pass)
1666 (void)pass; /* placate GCC */
1667 fclose(no_pp_fp);
1670 static uint32_t get_cpu(char *value)
1673 if (!strcmp(value, "8086"))
1674 return IF_8086;
1675 if (!strcmp(value, "186"))
1676 return IF_186;
1677 if (!strcmp(value, "286"))
1678 return IF_286;
1679 if (!strcmp(value, "386"))
1680 return IF_386;
1681 if (!strcmp(value, "486"))
1682 return IF_486;
1683 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1684 return IF_PENT;
1685 if (!strcmp(value, "686") ||
1686 !nasm_stricmp(value, "ppro") ||
1687 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1688 return IF_P6;
1689 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1690 return IF_KATMAI;
1691 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1692 !nasm_stricmp(value, "willamette"))
1693 return IF_WILLAMETTE;
1694 if (!nasm_stricmp(value, "prescott"))
1695 return IF_PRESCOTT;
1696 if (!nasm_stricmp(value, "x64") ||
1697 !nasm_stricmp(value, "x86-64"))
1698 return IF_X86_64;
1699 if (!nasm_stricmp(value, "ia64") ||
1700 !nasm_stricmp(value, "ia-64") ||
1701 !nasm_stricmp(value, "itanium") ||
1702 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1703 return IF_IA64;
1705 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1706 "unknown 'cpu' type");
1708 return IF_PLEVEL; /* the maximum level */
1711 static int get_bits(char *value)
1713 int i;
1715 if ((i = atoi(value)) == 16)
1716 return i; /* set for a 16-bit segment */
1717 else if (i == 32) {
1718 if (cpu < IF_386) {
1719 report_error(ERR_NONFATAL,
1720 "cannot specify 32-bit segment on processor below a 386");
1721 i = 16;
1723 } else if (i == 64) {
1724 if (cpu < IF_X86_64) {
1725 report_error(ERR_NONFATAL,
1726 "cannot specify 64-bit segment on processor below an x86-64");
1727 i = 16;
1729 if (i != maxbits) {
1730 report_error(ERR_NONFATAL,
1731 "%s output format does not support 64-bit code",
1732 ofmt->shortname);
1733 i = 16;
1735 } else {
1736 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1737 "`%s' is not a valid segment size; must be 16, 32 or 64",
1738 value);
1739 i = 16;
1741 return i;
1744 /* end of nasm.c */