Added Dev-Cpp Makefile
[nasm/sigaren-mirror.git] / nasm.c
blob4b53411d7c38c6c9e1baa1c5b16c294f668cf007
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(int8_t *value);
33 static uint32_t get_cpu(int8_t *cpu_str);
34 static void parse_cmdline(int, int8_t **);
35 static void assemble_file(int8_t *);
36 static int getkw(int8_t **directive, int8_t **value);
37 static void register_output_formats(void);
38 static void report_error_gnu(int severity, const int8_t *fmt, ...);
39 static void report_error_vc(int severity, const int8_t *fmt, ...);
40 static void report_error_common(int severity, const int8_t *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 int8_t inname[FILENAME_MAX];
52 static int8_t outname[FILENAME_MAX];
53 static int8_t 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 int8_t 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 int8_t *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 int8_t *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(int8_t *, int, efunc, evalfunc, ListGen *);
123 static int8_t *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 int8_t *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 int8_t 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 int8_t *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 int8_t *line;
215 int8_t *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 %ld+%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 int8_t *get_param(int8_t *p, int8_t *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 int8_t *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(int8_t *p, int8_t *q)
359 int8_t *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 int8_t *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 int8_t *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(int8_t *args)
690 int8_t *p, *q, *arg, *prevarg;
691 int8_t 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, int8_t **argv)
714 FILE *rfile;
715 int8_t *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 int8_t *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 if ((p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i))) {
761 if ((rfile = fopen(p, "r"))) {
762 process_respfile(rfile);
763 fclose(rfile);
764 } else
765 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
766 "unable to open response file `%s'", p);
768 } else
769 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
770 argv += i, argc -= i;
773 if (!*inname)
774 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
775 "no input file specified");
778 static void assemble_file(int8_t *fname)
780 int8_t *directive, *value, *p, *q, *special, *line, debugid[80];
781 insn output_ins;
782 int i, rn_error, validid;
783 int32_t seg, offs;
784 struct tokenval tokval;
785 expr *e;
786 int pass, pass_max;
787 int pass_cnt = 0; /* count actual passes */
789 if (cmd_sb == 32 && cmd_cpu < IF_386)
790 report_error(ERR_FATAL, "command line: "
791 "32-bit segment size requires a higher cpu");
793 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
794 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
795 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
796 int pass1, pass2;
797 ldfunc def_label;
799 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
800 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
801 /* pass0 seq is 0, 0, 0,..., 1, 2 */
803 def_label = pass > 1 ? redefine_label : define_label;
805 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
806 cpu = cmd_cpu;
807 if (pass0 == 2) {
808 if (*listname)
809 nasmlist.init(listname, report_error);
811 in_abs_seg = FALSE;
812 global_offset_changed = FALSE; /* set by redefine_label */
813 location.segment = ofmt->section(NULL, pass2, &sb);
814 globalbits = sb;
815 if (pass > 1) {
816 saa_rewind(forwrefs);
817 forwref = saa_rstruct(forwrefs);
818 raa_free(offsets);
819 offsets = raa_init();
821 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
822 globallineno = 0;
823 if (pass == 1)
824 location.known = TRUE;
825 location.offset = offs = GET_CURR_OFFS;
827 while ((line = preproc->getline())) {
828 globallineno++;
830 /* here we parse our directives; this is not handled by the 'real'
831 * parser. */
832 directive = line;
833 if ((i = getkw(&directive, &value))) {
834 switch (i) {
835 case 1: /* [SEGMENT n] */
836 seg = ofmt->section(value, pass2, &sb);
837 if (seg == NO_SEG) {
838 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
839 "segment name `%s' not recognized",
840 value);
841 } else {
842 in_abs_seg = FALSE;
843 location.segment = seg;
845 break;
846 case 2: /* [EXTERN label:special] */
847 if (*value == '$')
848 value++; /* skip initial $ if present */
849 if (pass0 == 2) {
850 q = value;
851 while (*q && *q != ':')
852 q++;
853 if (*q == ':') {
854 *q++ = '\0';
855 ofmt->symdef(value, 0L, 0L, 3, q);
857 } else if (pass == 1) { /* pass == 1 */
858 q = value;
859 validid = TRUE;
860 if (!isidstart(*q))
861 validid = FALSE;
862 while (*q && *q != ':') {
863 if (!isidchar(*q))
864 validid = FALSE;
865 q++;
867 if (!validid) {
868 report_error(ERR_NONFATAL,
869 "identifier expected after EXTERN");
870 break;
872 if (*q == ':') {
873 *q++ = '\0';
874 special = q;
875 } else
876 special = NULL;
877 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
878 int temp = pass0;
879 pass0 = 1; /* fake pass 1 in labels.c */
880 declare_as_global(value, special,
881 report_error);
882 define_label(value, seg_alloc(), 0L, NULL,
883 FALSE, TRUE, ofmt, report_error);
884 pass0 = temp;
886 } /* else pass0 == 1 */
887 break;
888 case 3: /* [BITS bits] */
889 globalbits = sb = get_bits(value);
890 break;
891 case 4: /* [GLOBAL symbol:special] */
892 if (*value == '$')
893 value++; /* skip initial $ if present */
894 if (pass0 == 2) { /* pass 2 */
895 q = value;
896 while (*q && *q != ':')
897 q++;
898 if (*q == ':') {
899 *q++ = '\0';
900 ofmt->symdef(value, 0L, 0L, 3, q);
902 } else if (pass2 == 1) { /* pass == 1 */
903 q = value;
904 validid = TRUE;
905 if (!isidstart(*q))
906 validid = FALSE;
907 while (*q && *q != ':') {
908 if (!isidchar(*q))
909 validid = FALSE;
910 q++;
912 if (!validid) {
913 report_error(ERR_NONFATAL,
914 "identifier expected after GLOBAL");
915 break;
917 if (*q == ':') {
918 *q++ = '\0';
919 special = q;
920 } else
921 special = NULL;
922 declare_as_global(value, special, report_error);
923 } /* pass == 1 */
924 break;
925 case 5: /* [COMMON symbol size:special] */
926 if (*value == '$')
927 value++; /* skip initial $ if present */
928 if (pass0 == 1) {
929 p = value;
930 validid = TRUE;
931 if (!isidstart(*p))
932 validid = FALSE;
933 while (*p && !isspace(*p)) {
934 if (!isidchar(*p))
935 validid = FALSE;
936 p++;
938 if (!validid) {
939 report_error(ERR_NONFATAL,
940 "identifier expected after COMMON");
941 break;
943 if (*p) {
944 int64_t size;
946 while (*p && isspace(*p))
947 *p++ = '\0';
948 q = p;
949 while (*q && *q != ':')
950 q++;
951 if (*q == ':') {
952 *q++ = '\0';
953 special = q;
954 } else
955 special = NULL;
956 size = readnum(p, &rn_error);
957 if (rn_error)
958 report_error(ERR_NONFATAL,
959 "invalid size specified"
960 " in COMMON declaration");
961 else
962 define_common(value, seg_alloc(), size,
963 special, ofmt, report_error);
964 } else
965 report_error(ERR_NONFATAL,
966 "no size specified in"
967 " COMMON declaration");
968 } else if (pass0 == 2) { /* pass == 2 */
969 q = value;
970 while (*q && *q != ':') {
971 if (isspace(*q))
972 *q = '\0';
973 q++;
975 if (*q == ':') {
976 *q++ = '\0';
977 ofmt->symdef(value, 0L, 0L, 3, q);
980 break;
981 case 6: /* [ABSOLUTE address] */
982 stdscan_reset();
983 stdscan_bufptr = value;
984 tokval.t_type = TOKEN_INVALID;
985 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
986 report_error, NULL);
987 if (e) {
988 if (!is_reloc(e))
989 report_error(pass0 ==
990 1 ? ERR_NONFATAL : ERR_PANIC,
991 "cannot use non-relocatable expression as "
992 "ABSOLUTE address");
993 else {
994 abs_seg = reloc_seg(e);
995 abs_offset = reloc_value(e);
997 } else if (pass == 1)
998 abs_offset = 0x100; /* don't go near zero in case of / */
999 else
1000 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1001 "in pass two");
1002 in_abs_seg = TRUE;
1003 location.segment = NO_SEG;
1004 break;
1005 case 7: /* DEBUG */
1006 p = value;
1007 q = debugid;
1008 validid = TRUE;
1009 if (!isidstart(*p))
1010 validid = FALSE;
1011 while (*p && !isspace(*p)) {
1012 if (!isidchar(*p))
1013 validid = FALSE;
1014 *q++ = *p++;
1016 *q++ = 0;
1017 if (!validid) {
1018 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1019 "identifier expected after DEBUG");
1020 break;
1022 while (*p && isspace(*p))
1023 p++;
1024 if (pass == pass_max)
1025 ofmt->current_dfmt->debug_directive(debugid, p);
1026 break;
1027 case 8: /* [WARNING {+|-}warn-name] */
1028 if (pass1 == 1) {
1029 while (*value && isspace(*value))
1030 value++;
1032 if (*value == '+' || *value == '-') {
1033 validid = (*value == '-') ? TRUE : FALSE;
1034 value++;
1035 } else
1036 validid = FALSE;
1038 for (i = 1; i <= ERR_WARN_MAX; i++)
1039 if (!nasm_stricmp(value, suppressed_names[i]))
1040 break;
1041 if (i <= ERR_WARN_MAX)
1042 suppressed[i] = validid;
1043 else
1044 report_error(ERR_NONFATAL,
1045 "invalid warning id in WARNING directive");
1047 break;
1048 case 9: /* cpu */
1049 cpu = get_cpu(value);
1050 break;
1051 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1052 while (*value && isspace(*value))
1053 value++;
1055 if (*value == '+') {
1056 user_nolist = 0;
1057 } else {
1058 if (*value == '-') {
1059 user_nolist = 1;
1060 } else {
1061 report_error(ERR_NONFATAL,
1062 "invalid parameter to \"list\" directive");
1065 break;
1066 default:
1067 if (!ofmt->directive(directive, value, pass2))
1068 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1069 "unrecognised directive [%s]",
1070 directive);
1072 } else { /* it isn't a directive */
1074 parse_line(pass1, line, &output_ins,
1075 report_error, evaluate, def_label);
1077 if (!(optimizing > 0) && pass == 2) {
1078 if (forwref != NULL && globallineno == forwref->lineno) {
1079 output_ins.forw_ref = TRUE;
1080 do {
1081 output_ins.oprs[forwref->operand].opflags |=
1082 OPFLAG_FORWARD;
1083 forwref = saa_rstruct(forwrefs);
1084 } while (forwref != NULL
1085 && forwref->lineno == globallineno);
1086 } else
1087 output_ins.forw_ref = FALSE;
1090 if (!(optimizing > 0) && output_ins.forw_ref) {
1091 if (pass == 1) {
1092 for (i = 0; i < output_ins.operands; i++) {
1093 if (output_ins.oprs[i].
1094 opflags & OPFLAG_FORWARD) {
1095 struct forwrefinfo *fwinf =
1096 (struct forwrefinfo *)
1097 saa_wstruct(forwrefs);
1098 fwinf->lineno = globallineno;
1099 fwinf->operand = i;
1102 } else { /* pass == 2 */
1104 * Hack to prevent phase error in the code
1105 * rol ax,x
1106 * x equ 1
1108 * If the second operand is a forward reference,
1109 * the UNITY property of the number 1 in that
1110 * operand is cancelled. Otherwise the above
1111 * sequence will cause a phase error.
1113 * This hack means that the above code will
1114 * generate 286+ code.
1116 * The forward reference will mean that the
1117 * operand will not have the UNITY property on
1118 * the first pass, so the pass behaviours will
1119 * be consistent.
1122 if (output_ins.operands >= 2 &&
1123 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1125 output_ins.oprs[1].type &=
1126 ~(ONENESS | BYTENESS);
1129 } /* pass == 2 */
1133 /* forw_ref */
1134 if (output_ins.opcode == I_EQU) {
1135 if (pass1 == 1) {
1137 * Special `..' EQUs get processed in pass two,
1138 * except `..@' macro-processor EQUs which are done
1139 * in the normal place.
1141 if (!output_ins.label)
1142 report_error(ERR_NONFATAL,
1143 "EQU not preceded by label");
1145 else if (output_ins.label[0] != '.' ||
1146 output_ins.label[1] != '.' ||
1147 output_ins.label[2] == '@') {
1148 if (output_ins.operands == 1 &&
1149 (output_ins.oprs[0].type & IMMEDIATE) &&
1150 output_ins.oprs[0].wrt == NO_SEG) {
1151 int isext =
1152 output_ins.oprs[0].
1153 opflags & OPFLAG_EXTERN;
1154 def_label(output_ins.label,
1155 output_ins.oprs[0].segment,
1156 output_ins.oprs[0].offset, NULL,
1157 FALSE, isext, ofmt,
1158 report_error);
1159 } else if (output_ins.operands == 2
1160 && (output_ins.oprs[0].
1161 type & IMMEDIATE)
1162 && (output_ins.oprs[0].type & COLON)
1163 && output_ins.oprs[0].segment ==
1164 NO_SEG
1165 && output_ins.oprs[0].wrt == NO_SEG
1166 && (output_ins.oprs[1].
1167 type & IMMEDIATE)
1168 && output_ins.oprs[1].segment ==
1169 NO_SEG
1170 && output_ins.oprs[1].wrt ==
1171 NO_SEG) {
1172 def_label(output_ins.label,
1173 output_ins.oprs[0].
1174 offset | SEG_ABS,
1175 output_ins.oprs[1].offset, NULL,
1176 FALSE, FALSE, ofmt,
1177 report_error);
1178 } else
1179 report_error(ERR_NONFATAL,
1180 "bad syntax for EQU");
1182 } else { /* pass == 2 */
1184 * Special `..' EQUs get processed here, except
1185 * `..@' macro processor EQUs which are done above.
1187 if (output_ins.label[0] == '.' &&
1188 output_ins.label[1] == '.' &&
1189 output_ins.label[2] != '@') {
1190 if (output_ins.operands == 1 &&
1191 (output_ins.oprs[0].type & IMMEDIATE)) {
1192 define_label(output_ins.label,
1193 output_ins.oprs[0].segment,
1194 output_ins.oprs[0].offset,
1195 NULL, FALSE, FALSE, ofmt,
1196 report_error);
1197 } else if (output_ins.operands == 2
1198 && (output_ins.oprs[0].
1199 type & IMMEDIATE)
1200 && (output_ins.oprs[0].type & COLON)
1201 && output_ins.oprs[0].segment ==
1202 NO_SEG
1203 && (output_ins.oprs[1].
1204 type & IMMEDIATE)
1205 && output_ins.oprs[1].segment ==
1206 NO_SEG) {
1207 define_label(output_ins.label,
1208 output_ins.oprs[0].
1209 offset | SEG_ABS,
1210 output_ins.oprs[1].offset,
1211 NULL, FALSE, FALSE, ofmt,
1212 report_error);
1213 } else
1214 report_error(ERR_NONFATAL,
1215 "bad syntax for EQU");
1217 } /* pass == 2 */
1218 } else { /* instruction isn't an EQU */
1220 if (pass1 == 1) {
1222 int32_t l = insn_size(location.segment, offs, sb, cpu,
1223 &output_ins, report_error);
1225 /* if (using_debug_info) && output_ins.opcode != -1) */
1226 if (using_debug_info)
1227 { /* fbk 03/25/01 */
1228 /* this is done here so we can do debug type info */
1229 int32_t typeinfo =
1230 TYS_ELEMENTS(output_ins.operands);
1231 switch (output_ins.opcode) {
1232 case I_RESB:
1233 typeinfo =
1234 TYS_ELEMENTS(output_ins.oprs[0].
1235 offset) | TY_BYTE;
1236 break;
1237 case I_RESW:
1238 typeinfo =
1239 TYS_ELEMENTS(output_ins.oprs[0].
1240 offset) | TY_WORD;
1241 break;
1242 case I_RESD:
1243 typeinfo =
1244 TYS_ELEMENTS(output_ins.oprs[0].
1245 offset) | TY_DWORD;
1246 break;
1247 case I_RESQ:
1248 typeinfo =
1249 TYS_ELEMENTS(output_ins.oprs[0].
1250 offset) | TY_QWORD;
1251 break;
1252 case I_REST:
1253 typeinfo =
1254 TYS_ELEMENTS(output_ins.oprs[0].
1255 offset) | TY_TBYTE;
1256 break;
1257 case I_DB:
1258 typeinfo |= TY_BYTE;
1259 break;
1260 case I_DW:
1261 typeinfo |= TY_WORD;
1262 break;
1263 case I_DD:
1264 if (output_ins.eops_float)
1265 typeinfo |= TY_FLOAT;
1266 else
1267 typeinfo |= TY_DWORD;
1268 break;
1269 case I_DQ:
1270 typeinfo |= TY_QWORD;
1271 break;
1272 case I_DT:
1273 typeinfo |= TY_TBYTE;
1274 break;
1275 default:
1276 typeinfo = TY_LABEL;
1280 ofmt->current_dfmt->debug_typevalue(typeinfo);
1283 if (l != -1) {
1284 offs += l;
1285 SET_CURR_OFFS(offs);
1288 * else l == -1 => invalid instruction, which will be
1289 * flagged as an error on pass 2
1292 } else { /* pass == 2 */
1293 offs += assemble(location.segment, offs, sb, cpu,
1294 &output_ins, ofmt, report_error,
1295 &nasmlist);
1296 SET_CURR_OFFS(offs);
1299 } /* not an EQU */
1300 cleanup_insn(&output_ins);
1302 nasm_free(line);
1303 location.offset = offs = GET_CURR_OFFS;
1304 } /* end while (line = preproc->getline... */
1306 if (pass1 == 2 && global_offset_changed)
1307 report_error(ERR_NONFATAL,
1308 "phase error detected at end of assembly.");
1310 if (pass1 == 1)
1311 preproc->cleanup(1);
1313 if (pass1 == 1 && terminate_after_phase) {
1314 fclose(ofile);
1315 remove(outname);
1316 if (want_usage)
1317 usage();
1318 exit(1);
1320 pass_cnt++;
1321 if (pass > 1 && !global_offset_changed) {
1322 pass0++;
1323 if (pass0 == 2)
1324 pass = pass_max - 1;
1325 } else if (!(optimizing > 0))
1326 pass0++;
1328 } /* for (pass=1; pass<=2; pass++) */
1330 preproc->cleanup(0);
1331 nasmlist.cleanup();
1332 #if 1
1333 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1334 fprintf(stdout,
1335 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
1336 #endif
1337 } /* exit from assemble_file (...) */
1339 static int getkw(int8_t **directive, int8_t **value)
1341 int8_t *p, *q, *buf;
1343 buf = *directive;
1345 /* allow leading spaces or tabs */
1346 while (*buf == ' ' || *buf == '\t')
1347 buf++;
1349 if (*buf != '[')
1350 return 0;
1352 p = buf;
1354 while (*p && *p != ']')
1355 p++;
1357 if (!*p)
1358 return 0;
1360 q = p++;
1362 while (*p && *p != ';') {
1363 if (!isspace(*p))
1364 return 0;
1365 p++;
1367 q[1] = '\0';
1369 *directive = p = buf + 1;
1370 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1371 buf++;
1372 if (*buf == ']') {
1373 *buf = '\0';
1374 *value = buf;
1375 } else {
1376 *buf++ = '\0';
1377 while (isspace(*buf))
1378 buf++; /* beppu - skip leading whitespace */
1379 *value = buf;
1380 while (*buf != ']')
1381 buf++;
1382 *buf++ = '\0';
1384 #if 0
1385 for (q = p; *q; q++)
1386 *q = tolower(*q);
1387 #endif
1388 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1389 return 1;
1390 if (!nasm_stricmp(p, "extern"))
1391 return 2;
1392 if (!nasm_stricmp(p, "bits"))
1393 return 3;
1394 if (!nasm_stricmp(p, "global"))
1395 return 4;
1396 if (!nasm_stricmp(p, "common"))
1397 return 5;
1398 if (!nasm_stricmp(p, "absolute"))
1399 return 6;
1400 if (!nasm_stricmp(p, "debug"))
1401 return 7;
1402 if (!nasm_stricmp(p, "warning"))
1403 return 8;
1404 if (!nasm_stricmp(p, "cpu"))
1405 return 9;
1406 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
1407 return 10;
1408 return -1;
1412 * gnu style error reporting
1413 * This function prints an error message to error_file in the
1414 * style used by GNU. An example would be:
1415 * file.asm:50: error: blah blah blah
1416 * where file.asm is the name of the file, 50 is the line number on
1417 * which the error occurs (or is detected) and "error:" is one of
1418 * the possible optional diagnostics -- it can be "error" or "warning"
1419 * or something else. Finally the line terminates with the actual
1420 * error message.
1422 * @param severity the severity of the warning or error
1423 * @param fmt the printf style format string
1425 static void report_error_gnu(int severity, const int8_t *fmt, ...)
1427 va_list ap;
1429 if (is_suppressed_warning(severity))
1430 return;
1432 if (severity & ERR_NOFILE)
1433 fputs("nasm: ", error_file);
1434 else {
1435 int8_t *currentfile = NULL;
1436 int32_t lineno = 0;
1437 src_get(&lineno, &currentfile);
1438 fprintf(error_file, "%s:%ld: ", currentfile, lineno);
1439 nasm_free(currentfile);
1441 va_start(ap, fmt);
1442 report_error_common(severity, fmt, ap);
1443 va_end(ap);
1447 * MS style error reporting
1448 * This function prints an error message to error_file in the
1449 * style used by Visual C and some other Microsoft tools. An example
1450 * would be:
1451 * file.asm(50) : error: blah blah blah
1452 * where file.asm is the name of the file, 50 is the line number on
1453 * which the error occurs (or is detected) and "error:" is one of
1454 * the possible optional diagnostics -- it can be "error" or "warning"
1455 * or something else. Finally the line terminates with the actual
1456 * error message.
1458 * @param severity the severity of the warning or error
1459 * @param fmt the printf style format string
1461 static void report_error_vc(int severity, const int8_t *fmt, ...)
1463 va_list ap;
1465 if (is_suppressed_warning(severity))
1466 return;
1468 if (severity & ERR_NOFILE)
1469 fputs("nasm: ", error_file);
1470 else {
1471 int8_t *currentfile = NULL;
1472 int32_t lineno = 0;
1473 src_get(&lineno, &currentfile);
1474 fprintf(error_file, "%s(%ld) : ", currentfile, lineno);
1475 nasm_free(currentfile);
1477 va_start(ap, fmt);
1478 report_error_common(severity, fmt, ap);
1479 va_end(ap);
1483 * check for supressed warning
1484 * checks for suppressed warning or pass one only warning and we're
1485 * not in pass 1
1487 * @param severity the severity of the warning or error
1488 * @return true if we should abort error/warning printing
1490 static int is_suppressed_warning(int severity)
1493 * See if it's a suppressed warning.
1495 return ((severity & ERR_MASK) == ERR_WARNING &&
1496 (severity & ERR_WARN_MASK) != 0 &&
1497 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1499 * See if it's a pass-one only warning and we're not in pass one.
1501 ((severity & ERR_PASS1) && pass0 == 2);
1505 * common error reporting
1506 * This is the common back end of the error reporting schemes currently
1507 * implemented. It prints the nature of the warning and then the
1508 * specific error message to error_file and may or may not return. It
1509 * doesn't return if the error severity is a "panic" or "debug" type.
1511 * @param severity the severity of the warning or error
1512 * @param fmt the printf style format string
1514 static void report_error_common(int severity, const int8_t *fmt,
1515 va_list args)
1517 switch (severity & ERR_MASK) {
1518 case ERR_WARNING:
1519 fputs("warning: ", error_file);
1520 break;
1521 case ERR_NONFATAL:
1522 fputs("error: ", error_file);
1523 break;
1524 case ERR_FATAL:
1525 fputs("fatal: ", error_file);
1526 break;
1527 case ERR_PANIC:
1528 fputs("panic: ", error_file);
1529 break;
1530 case ERR_DEBUG:
1531 fputs("debug: ", error_file);
1532 break;
1535 vfprintf(error_file, fmt, args);
1536 fputc('\n', error_file);
1538 if (severity & ERR_USAGE)
1539 want_usage = TRUE;
1541 switch (severity & ERR_MASK) {
1542 case ERR_WARNING:
1543 case ERR_DEBUG:
1544 /* no further action, by definition */
1545 break;
1546 case ERR_NONFATAL:
1547 /* hack enables listing(!) on errors */
1548 terminate_after_phase = TRUE;
1549 break;
1550 case ERR_FATAL:
1551 if (ofile) {
1552 fclose(ofile);
1553 remove(outname);
1555 if (want_usage)
1556 usage();
1557 exit(1); /* instantly die */
1558 break; /* placate silly compilers */
1559 case ERR_PANIC:
1560 fflush(NULL);
1561 /* abort(); *//* halt, catch fire, and dump core */
1562 exit(3);
1563 break;
1567 static void usage(void)
1569 fputs("type `nasm -h' for help\n", error_file);
1572 static void register_output_formats(void)
1574 ofmt = ofmt_register(report_error);
1577 #define BUF_DELTA 512
1579 static FILE *no_pp_fp;
1580 static efunc no_pp_err;
1581 static ListGen *no_pp_list;
1582 static int32_t no_pp_lineinc;
1584 static void no_pp_reset(int8_t *file, int pass, efunc error, evalfunc eval,
1585 ListGen * listgen)
1587 src_set_fname(nasm_strdup(file));
1588 src_set_linnum(0);
1589 no_pp_lineinc = 1;
1590 no_pp_err = error;
1591 no_pp_fp = fopen(file, "r");
1592 if (!no_pp_fp)
1593 no_pp_err(ERR_FATAL | ERR_NOFILE,
1594 "unable to open input file `%s'", file);
1595 no_pp_list = listgen;
1596 (void)pass; /* placate compilers */
1597 (void)eval; /* placate compilers */
1600 static int8_t *no_pp_getline(void)
1602 int8_t *buffer, *p, *q;
1603 int bufsize;
1605 bufsize = BUF_DELTA;
1606 buffer = nasm_malloc(BUF_DELTA);
1607 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1609 while (1) { /* Loop to handle %line */
1611 p = buffer;
1612 while (1) { /* Loop to handle long lines */
1613 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1614 if (!q)
1615 break;
1616 p += strlen(p);
1617 if (p > buffer && p[-1] == '\n')
1618 break;
1619 if (p - buffer > bufsize - 10) {
1620 int offset;
1621 offset = p - buffer;
1622 bufsize += BUF_DELTA;
1623 buffer = nasm_realloc(buffer, bufsize);
1624 p = buffer + offset;
1628 if (!q && p == buffer) {
1629 nasm_free(buffer);
1630 return NULL;
1634 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1635 * them are present at the end of the line.
1637 buffer[strcspn(buffer, "\r\n\032")] = '\0';
1639 if (!strncmp(buffer, "%line", 5)) {
1640 int32_t ln;
1641 int li;
1642 int8_t *nm = nasm_malloc(strlen(buffer));
1643 if (sscanf(buffer + 5, "%ld+%d %s", &ln, &li, nm) == 3) {
1644 nasm_free(src_set_fname(nm));
1645 src_set_linnum(ln);
1646 no_pp_lineinc = li;
1647 continue;
1649 nasm_free(nm);
1651 break;
1654 no_pp_list->line(LIST_READ, buffer);
1656 return buffer;
1659 static void no_pp_cleanup(int pass)
1661 fclose(no_pp_fp);
1664 static uint32_t get_cpu(int8_t *value)
1667 if (!strcmp(value, "8086"))
1668 return IF_8086;
1669 if (!strcmp(value, "186"))
1670 return IF_186;
1671 if (!strcmp(value, "286"))
1672 return IF_286;
1673 if (!strcmp(value, "386"))
1674 return IF_386;
1675 if (!strcmp(value, "486"))
1676 return IF_486;
1677 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1678 return IF_PENT;
1679 if (!strcmp(value, "686") ||
1680 !nasm_stricmp(value, "ppro") ||
1681 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1682 return IF_P6;
1683 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1684 return IF_KATMAI;
1685 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1686 !nasm_stricmp(value, "willamette"))
1687 return IF_WILLAMETTE;
1688 if (!nasm_stricmp(value, "prescott"))
1689 return IF_PRESCOTT;
1690 if (!nasm_stricmp(value, "x64") ||
1691 !nasm_stricmp(value, "x86-64"))
1692 return IF_X64;
1693 if (!nasm_stricmp(value, "ia64") ||
1694 !nasm_stricmp(value, "ia-64") ||
1695 !nasm_stricmp(value, "itanium") ||
1696 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1697 return IF_IA64;
1699 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1700 "unknown 'cpu' type");
1702 return IF_PLEVEL; /* the maximum level */
1705 static int get_bits(int8_t *value)
1707 int i;
1709 if ((i = atoi(value)) == 16)
1710 return i; /* set for a 16-bit segment */
1711 else if (i == 32) {
1712 if (cpu < IF_386) {
1713 report_error(ERR_NONFATAL,
1714 "cannot specify 32-bit segment on processor below a 386");
1715 i = 16;
1717 } else if (i == 64) {
1718 if (cpu < IF_X64) {
1719 report_error(ERR_NONFATAL,
1720 "cannot specify 64-bit segment on processor below an x86-64");
1721 i = 16;
1723 if (i != maxbits) {
1724 report_error(ERR_NONFATAL,
1725 "%s output format does not support 64-bit code",
1726 ofmt->shortname);
1727 i = 16;
1729 } else {
1730 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1731 "`%s' is not a valid segment size; must be 16, 32 or 64",
1732 value);
1733 i = 16;
1735 return i;
1738 /* end of nasm.c */