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.
32 struct forwrefinfo
{ /* info held on forward refs. */
37 static int get_bits(char *value
);
38 static uint32_t get_cpu(char *cpu_str
);
39 static void parse_cmdline(int, char **);
40 static void assemble_file(char *);
41 static void register_output_formats(void);
42 static void report_error_gnu(int severity
, const char *fmt
, ...);
43 static void report_error_vc(int severity
, const char *fmt
, ...);
44 static void report_error_common(int severity
, const char *fmt
,
46 static int is_suppressed_warning(int severity
);
47 static void usage(void);
48 static efunc report_error
;
50 static int using_debug_info
, opt_verbose_info
;
51 bool tasm_compatible_mode
= false;
56 static char inname
[FILENAME_MAX
];
57 static char outname
[FILENAME_MAX
];
58 static char listname
[FILENAME_MAX
];
59 static char errname
[FILENAME_MAX
];
60 static int globallineno
; /* for forward-reference tracking */
61 /* static int pass = 0; */
62 static struct ofmt
*ofmt
= NULL
;
64 static FILE *error_file
; /* Where to write error messages */
66 static FILE *ofile
= NULL
;
67 int optimizing
= -1; /* number of optimization passes to take */
68 static int sb
, cmd_sb
= 16; /* by default */
69 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
70 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
71 bool global_offset_changed
; /* referenced in labels.c */
73 static struct location location
;
74 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
75 int32_t abs_seg
; /* ABSOLUTE segment basis */
76 int32_t abs_offset
; /* ABSOLUTE offset */
78 static struct RAA
*offsets
;
80 static struct SAA
*forwrefs
; /* keep track of forward references */
81 static const struct forwrefinfo
*forwref
;
83 static Preproc
*preproc
;
85 op_normal
, /* Preprocess and assemble */
86 op_preprocess
, /* Preprocess only */
87 op_depend
, /* Generate dependencies */
88 op_depend_missing_ok
, /* Generate dependencies, missing OK */
90 static enum op_type operating_mode
;
93 * Which of the suppressible warnings are suppressed. Entry zero
94 * doesn't do anything. Initial defaults are given here.
96 static bool suppressed
[1 + ERR_WARN_MAX
] = {
97 0, true, true, true, false, true, false, true, true, false
101 * The option names for the suppressible warnings. As before, entry
104 static const char *suppressed_names
[1 + ERR_WARN_MAX
] = {
105 NULL
, "macro-params", "macro-selfref", "orphan-labels",
106 "number-overflow", "gnu-elf-extensions", "float-overflow",
107 "float-denorm", "float-underflow", "float-toolong"
111 * The explanations for the suppressible warnings. As before, entry
114 static const char *suppressed_what
[1 + ERR_WARN_MAX
] = {
116 "macro calls with wrong no. of params",
117 "cyclic macro self-references",
118 "labels alone on lines without trailing `:'",
119 "numeric constants do not fit in 32 bits",
120 "using 8- or 16-bit relocation in ELF32, a GNU extension",
121 "floating point overflow",
122 "floating point denormal",
123 "floating point underflow",
124 "too many digits in floating-point number"
128 * This is a null preprocessor which just copies lines from input
129 * to output. It's used when someone explicitly requests that NASM
130 * not preprocess their source file.
133 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*);
134 static char *no_pp_getline(void);
135 static void no_pp_cleanup(int);
136 static Preproc no_pp
= {
143 * get/set current offset...
145 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
146 raa_read(offsets,location.segment))
147 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
148 (void)(offsets=raa_write(offsets,location.segment,(x))))
150 static int want_usage
;
151 static int terminate_after_phase
;
152 int user_nolist
= 0; /* fbk 9/2/00 */
154 static void nasm_fputs(const char *line
, FILE * outfile
)
157 fputs(line
, outfile
);
158 fputc('\n', outfile
);
163 int main(int argc
, char **argv
)
166 want_usage
= terminate_after_phase
= false;
167 report_error
= report_error_gnu
;
171 nasm_set_malloc_error(report_error
);
172 offsets
= raa_init();
173 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
176 operating_mode
= op_normal
;
180 register_output_formats();
182 parse_cmdline(argc
, argv
);
184 if (terminate_after_phase
) {
190 /* If debugging info is disabled, suppress any debug calls */
191 if (!using_debug_info
)
192 ofmt
->current_dfmt
= &null_debug_form
;
195 pp_extra_stdmac(ofmt
->stdmac
);
196 parser_global_info(ofmt
, &location
);
197 eval_global_info(ofmt
, lookup_label
, &location
);
199 /* define some macros dependent of command-line */
202 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
207 switch (operating_mode
) {
208 case op_depend_missing_ok
:
209 pp_include_path(NULL
); /* "assume generated" */
214 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
215 if (outname
[0] == '\0')
216 ofmt
->filename(inname
, outname
, report_error
);
218 fprintf(stdout
, "%s: %s", outname
, inname
);
219 while ((line
= preproc
->getline()))
229 char *file_name
= NULL
;
230 int32_t prior_linnum
= 0;
234 ofile
= fopen(outname
, "w");
236 report_error(ERR_FATAL
| ERR_NOFILE
,
237 "unable to open output file `%s'",
242 location
.known
= false;
245 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
246 while ((line
= preproc
->getline())) {
248 * We generate %line directives if needed for later programs
250 int32_t linnum
= prior_linnum
+= lineinc
;
251 int altline
= src_get(&linnum
, &file_name
);
253 if (altline
== 1 && lineinc
== 1)
254 nasm_fputs("", ofile
);
256 lineinc
= (altline
!= -1 || lineinc
!= 1);
257 fprintf(ofile
? ofile
: stdout
,
258 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
261 prior_linnum
= linnum
;
263 nasm_fputs(line
, ofile
);
266 nasm_free(file_name
);
270 if (ofile
&& terminate_after_phase
)
278 * We must call ofmt->filename _anyway_, even if the user
279 * has specified their own output file, because some
280 * formats (eg OBJ and COFF) use ofmt->filename to find out
281 * the name of the input file and then put that inside the
284 ofmt
->filename(inname
, outname
, report_error
);
286 ofile
= fopen(outname
, "wb");
288 report_error(ERR_FATAL
| ERR_NOFILE
,
289 "unable to open output file `%s'", outname
);
293 * We must call init_labels() before ofmt->init() since
294 * some object formats will want to define labels in their
295 * init routines. (eg OS/2 defines the FLAT group)
299 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
301 assemble_file(inname
);
303 if (!terminate_after_phase
) {
304 ofmt
->cleanup(using_debug_info
);
308 * We had an fclose on the output file here, but we
309 * actually do that in all the object file drivers as well,
310 * so we're leaving out the one here.
329 if (terminate_after_phase
)
336 * Get a parameter for a command line option.
337 * First arg must be in the form of e.g. -f...
339 static char *get_param(char *p
, char *q
, int *advance
)
342 if (p
[2]) { /* the parameter's in the option */
352 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
353 "option `-%c' requires an argument", p
[1]);
363 #define OPT_POSTFIX 1
364 struct textargs textopts
[] = {
365 {"prefix", OPT_PREFIX
},
366 {"postfix", OPT_POSTFIX
},
371 static int process_arg(char *p
, char *q
)
379 if (p
[0] == '-' && !stopoptions
) {
384 case 'o': /* these parameters take values */
399 if (!(param
= get_param(p
, q
, &advance
)))
401 if (p
[1] == 'o') { /* output file */
402 strcpy(outname
, param
);
403 } else if (p
[1] == 'f') { /* output format */
404 ofmt
= ofmt_find(param
);
406 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
407 "unrecognised output format `%s' - "
408 "use -hf for a list", param
);
410 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
411 } else if (p
[1] == 'O') { /* Optimization level */
415 /* Naked -O == -Ox */
416 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
420 case '0': case '1': case '2': case '3': case '4':
421 case '5': case '6': case '7': case '8': case '9':
422 opt
= strtoul(param
, ¶m
, 10);
424 /* -O0 -> optimizing == -1, 0.98 behaviour */
425 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
427 optimizing
= opt
- 1;
429 /* The optimizer seems to have problems with
430 < 5 passes? Hidden bug? */
431 optimizing
= 5; /* 5 passes */
433 optimizing
= opt
; /* More than 5 passes */
439 opt_verbose_info
= true;
444 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
448 report_error(ERR_FATAL
,
449 "unknown optimization option -O%c\n",
455 } else if (p
[1] == 'P' || p
[1] == 'p') { /* pre-include */
456 pp_pre_include(param
);
457 } else if (p
[1] == 'D' || p
[1] == 'd') { /* pre-define */
458 pp_pre_define(param
);
459 } else if (p
[1] == 'U' || p
[1] == 'u') { /* un-define */
460 pp_pre_undefine(param
);
461 } else if (p
[1] == 'I' || p
[1] == 'i') { /* include search path */
462 pp_include_path(param
);
463 } else if (p
[1] == 'l') { /* listing file */
464 strcpy(listname
, param
);
465 } else if (p
[1] == 'Z') { /* error messages file */
466 strcpy(errname
, param
);
467 } else if (p
[1] == 'F') { /* specify debug format */
468 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
469 if (!ofmt
->current_dfmt
) {
470 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
471 "unrecognized debug format `%s' for"
472 " output format `%s'",
473 param
, ofmt
->shortname
);
475 } else if (p
[1] == 'X') { /* specify error reporting format */
476 if (nasm_stricmp("vc", param
) == 0)
477 report_error
= report_error_vc
;
478 else if (nasm_stricmp("gnu", param
) == 0)
479 report_error
= report_error_gnu
;
481 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
482 "unrecognized error reporting format `%s'",
487 using_debug_info
= true;
491 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
493 " [options...] [--] filename\n"
494 " or nasm -v for version info\n\n"
495 " -t assemble in SciTech TASM compatible mode\n"
496 " -g generate debug information in selected format.\n");
498 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
499 " -a don't preprocess (assemble only)\n"
500 " -M generate Makefile dependencies on stdout\n"
501 " -MG d:o, missing files assumed generated\n\n"
502 " -Z<file> redirect error messages to file\n"
503 " -s redirect error messages to stdout\n\n"
504 " -F format select a debugging format\n\n"
505 " -I<path> adds a pathname to the include file path\n");
507 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
508 " -P<file> pre-includes a file\n"
509 " -D<macro>[=<value>] pre-defines a macro\n"
510 " -U<macro> undefines a macro\n"
511 " -X<format> specifies error reporting format (gnu or vc)\n"
512 " -w+foo enables warnings about foo; -w-foo disables them\n"
513 "where foo can be:\n");
514 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
515 printf(" %-23s %s (default %s)\n",
516 suppressed_names
[i
], suppressed_what
[i
],
517 suppressed
[i
] ? "off" : "on");
519 ("\nresponse files should contain command line parameters"
520 ", one per line.\n");
522 printf("\nvalid output formats for -f are"
523 " (`*' denotes default):\n");
524 ofmt_list(ofmt
, stdout
);
526 printf("\nFor a list of valid output formats, use -hf.\n");
527 printf("For a list of debug formats, use -f <form> -y.\n");
529 exit(0); /* never need usage message here */
532 printf("\nvalid debug formats for '%s' output format are"
533 " ('*' denotes default):\n", ofmt
->shortname
);
534 dfmt_list(ofmt
, stdout
);
538 tasm_compatible_mode
= true;
542 const char *nasm_version_string
=
543 "NASM version " NASM_VER
" compiled on " __DATE__
548 puts(nasm_version_string
);
549 exit(0); /* never need usage message here */
552 case 'e': /* preprocess only */
554 operating_mode
= op_preprocess
;
556 case 'a': /* assemble only - don't preprocess */
560 if (p
[2] != '+' && p
[2] != '-') {
561 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
562 "invalid option to `-w'");
564 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
565 if (!nasm_stricmp(p
+ 3, suppressed_names
[i
]))
567 if (i
<= ERR_WARN_MAX
)
568 suppressed
[i
] = (p
[2] == '-');
570 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
571 "invalid option to `-w'");
575 operating_mode
= p
[2] == 'G' ? op_depend_missing_ok
: op_depend
;
582 if (p
[2] == 0) { /* -- => stop processing options */
586 for (s
= 0; textopts
[s
].label
; s
++) {
587 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
598 report_error(ERR_NONFATAL
| ERR_NOFILE
|
600 "option `--%s' requires an argument",
604 advance
= 1, param
= q
;
607 if (s
== OPT_PREFIX
) {
608 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
609 lprefix
[PREFIX_MAX
- 1] = 0;
612 if (s
== OPT_POSTFIX
) {
613 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
614 lpostfix
[POSTFIX_MAX
- 1] = 0;
621 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
622 "unrecognised option `--%s'", p
+ 2);
630 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
631 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
632 "unrecognised option `-%c'", p
[1]);
637 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
638 "more than one input file specified");
646 #define ARG_BUF_DELTA 128
648 static void process_respfile(FILE * rfile
)
650 char *buffer
, *p
, *q
, *prevarg
;
651 int bufsize
, prevargsize
;
653 bufsize
= prevargsize
= ARG_BUF_DELTA
;
654 buffer
= nasm_malloc(ARG_BUF_DELTA
);
655 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
658 while (1) { /* Loop to handle all lines in file */
661 while (1) { /* Loop to handle long lines */
662 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
666 if (p
> buffer
&& p
[-1] == '\n')
668 if (p
- buffer
> bufsize
- 10) {
671 bufsize
+= ARG_BUF_DELTA
;
672 buffer
= nasm_realloc(buffer
, bufsize
);
677 if (!q
&& p
== buffer
) {
679 process_arg(prevarg
, NULL
);
686 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
687 * them are present at the end of the line.
689 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
691 while (p
> buffer
&& isspace(p
[-1]))
698 if (process_arg(prevarg
, p
))
701 if ((int) strlen(p
) > prevargsize
- 10) {
702 prevargsize
+= ARG_BUF_DELTA
;
703 prevarg
= nasm_realloc(prevarg
, prevargsize
);
709 /* Function to process args from a string of args, rather than the
710 * argv array. Used by the environment variable and response file
713 static void process_args(char *args
)
715 char *p
, *q
, *arg
, *prevarg
;
716 char separator
= ' ';
724 while (*p
&& *p
!= separator
)
726 while (*p
== separator
)
730 if (process_arg(prevarg
, arg
))
734 process_arg(arg
, NULL
);
737 static void parse_cmdline(int argc
, char **argv
)
740 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
742 *inname
= *outname
= *listname
= *errname
= '\0';
745 * First, process the NASMENV environment variable.
747 envreal
= getenv("NASMENV");
750 envcopy
= nasm_strdup(envreal
);
751 process_args(envcopy
);
756 * Now process the actual command line.
761 if (argv
[0][0] == '@') {
762 /* We have a response file, so process this as a set of
763 * arguments like the environment variable. This allows us
764 * to have multiple arguments on a single line, which is
765 * different to the -@resp file processing below for regular
768 char *str
= malloc(2048);
769 FILE *f
= fopen(&argv
[0][1], "r");
771 printf("out of memory");
775 while (fgets(str
, 2048, f
)) {
784 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
785 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
);
787 rfile
= fopen(p
, "r");
789 process_respfile(rfile
);
792 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
793 "unable to open response file `%s'", p
);
796 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
797 argv
+= i
, argc
-= i
;
801 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
802 "no input file specified");
804 /* Look for basic command line typos. This definitely doesn't
805 catch all errors, but it might help cases of fumbled fingers. */
806 if (!strcmp(inname
, errname
) || !strcmp(inname
, outname
) ||
807 !strcmp(inname
, listname
))
808 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
809 "file `%s' is both input and output file",
813 error_file
= fopen(errname
, "w");
815 error_file
= stderr
; /* Revert to default! */
816 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
817 "cannot open file `%s' for error messages",
823 /* List of directives */
825 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
826 D_EXTERN
, D_FLOAT
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
828 static const char *directives
[] = {
829 "", "absolute", "bits", "common", "cpu", "debug", "default",
830 "extern", "float", "global", "list", "section", "segment", "warning"
832 static enum directives
getkw(char **directive
, char **value
);
834 static void assemble_file(char *fname
)
836 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
842 struct tokenval tokval
;
845 int pass_cnt
= 0; /* count actual passes */
847 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
848 report_error(ERR_FATAL
, "command line: "
849 "32-bit segment size requires a higher cpu");
851 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
852 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
853 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
857 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
858 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
859 /* pass0 seq is 0, 0, 0,..., 1, 2 */
861 def_label
= pass
> 1 ? redefine_label
: define_label
;
863 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
867 nasmlist
.init(listname
, report_error
);
870 global_offset_changed
= false; /* set by redefine_label */
871 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
874 saa_rewind(forwrefs
);
875 forwref
= saa_rstruct(forwrefs
);
877 offsets
= raa_init();
879 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
882 location
.known
= true;
883 location
.offset
= offs
= GET_CURR_OFFS
;
885 while ((line
= preproc
->getline())) {
889 /* here we parse our directives; this is not handled by the 'real'
892 d
= getkw(&directive
, &value
);
897 case D_SEGMENT
: /* [SEGMENT n] */
899 seg
= ofmt
->section(value
, pass2
, &sb
);
901 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
902 "segment name `%s' not recognized",
906 location
.segment
= seg
;
909 case D_EXTERN
: /* [EXTERN label:special] */
911 value
++; /* skip initial $ if present */
914 while (*q
&& *q
!= ':')
918 ofmt
->symdef(value
, 0L, 0L, 3, q
);
920 } else if (pass
== 1) { /* pass == 1 */
925 while (*q
&& *q
!= ':') {
931 report_error(ERR_NONFATAL
,
932 "identifier expected after EXTERN");
940 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
942 pass0
= 1; /* fake pass 1 in labels.c */
943 declare_as_global(value
, special
,
945 define_label(value
, seg_alloc(), 0L, NULL
,
946 false, true, ofmt
, report_error
);
949 } /* else pass0 == 1 */
951 case D_BITS
: /* [BITS bits] */
952 globalbits
= sb
= get_bits(value
);
954 case D_GLOBAL
: /* [GLOBAL symbol:special] */
956 value
++; /* skip initial $ if present */
957 if (pass0
== 2) { /* pass 2 */
959 while (*q
&& *q
!= ':')
963 ofmt
->symdef(value
, 0L, 0L, 3, q
);
965 } else if (pass2
== 1) { /* pass == 1 */
970 while (*q
&& *q
!= ':') {
976 report_error(ERR_NONFATAL
,
977 "identifier expected after GLOBAL");
985 declare_as_global(value
, special
, report_error
);
988 case D_COMMON
: /* [COMMON symbol size:special] */
990 value
++; /* skip initial $ if present */
996 while (*p
&& !isspace(*p
)) {
1002 report_error(ERR_NONFATAL
,
1003 "identifier expected after COMMON");
1009 while (*p
&& isspace(*p
))
1012 while (*q
&& *q
!= ':')
1019 size
= readnum(p
, &rn_error
);
1021 report_error(ERR_NONFATAL
,
1022 "invalid size specified"
1023 " in COMMON declaration");
1025 define_common(value
, seg_alloc(), size
,
1026 special
, ofmt
, report_error
);
1028 report_error(ERR_NONFATAL
,
1029 "no size specified in"
1030 " COMMON declaration");
1031 } else if (pass0
== 2) { /* pass == 2 */
1033 while (*q
&& *q
!= ':') {
1040 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1044 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1046 stdscan_bufptr
= value
;
1047 tokval
.t_type
= TOKEN_INVALID
;
1048 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1049 report_error
, NULL
);
1052 report_error(pass0
==
1053 1 ? ERR_NONFATAL
: ERR_PANIC
,
1054 "cannot use non-relocatable expression as "
1055 "ABSOLUTE address");
1057 abs_seg
= reloc_seg(e
);
1058 abs_offset
= reloc_value(e
);
1060 } else if (pass
== 1)
1061 abs_offset
= 0x100; /* don't go near zero in case of / */
1063 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1066 location
.segment
= NO_SEG
;
1068 case D_DEBUG
: /* [DEBUG] */
1074 while (*p
&& !isspace(*p
)) {
1081 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1082 "identifier expected after DEBUG");
1085 while (*p
&& isspace(*p
))
1087 if (pass
== pass_max
)
1088 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1090 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1092 while (*value
&& isspace(*value
))
1095 if (*value
== '+' || *value
== '-') {
1096 validid
= (*value
== '-') ? true : false;
1101 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1102 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1104 if (i
<= ERR_WARN_MAX
)
1105 suppressed
[i
] = validid
;
1107 report_error(ERR_NONFATAL
,
1108 "invalid warning id in WARNING directive");
1111 case D_CPU
: /* [CPU] */
1112 cpu
= get_cpu(value
);
1114 case D_LIST
: /* [LIST {+|-}] */
1115 while (*value
&& isspace(*value
))
1118 if (*value
== '+') {
1121 if (*value
== '-') {
1128 case D_DEFAULT
: /* [DEFAULT] */
1130 stdscan_bufptr
= value
;
1131 tokval
.t_type
= TOKEN_INVALID
;
1132 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1133 switch ((int)tokval
.t_integer
) {
1149 if (float_option(value
)) {
1150 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1151 "unknown 'float' directive: %s",
1156 if (!ofmt
->directive(directive
, value
, pass2
))
1157 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1158 "unrecognised directive [%s]",
1162 report_error(ERR_NONFATAL
,
1163 "invalid parameter to [%s] directive",
1166 } else { /* it isn't a directive */
1168 parse_line(pass1
, line
, &output_ins
,
1169 report_error
, evaluate
, def_label
);
1171 if (!(optimizing
> 0) && pass
== 2) {
1172 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1173 output_ins
.forw_ref
= true;
1175 output_ins
.oprs
[forwref
->operand
].opflags
|=
1177 forwref
= saa_rstruct(forwrefs
);
1178 } while (forwref
!= NULL
1179 && forwref
->lineno
== globallineno
);
1181 output_ins
.forw_ref
= false;
1184 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1186 for (i
= 0; i
< output_ins
.operands
; i
++) {
1187 if (output_ins
.oprs
[i
].
1188 opflags
& OPFLAG_FORWARD
) {
1189 struct forwrefinfo
*fwinf
=
1190 (struct forwrefinfo
*)
1191 saa_wstruct(forwrefs
);
1192 fwinf
->lineno
= globallineno
;
1196 } else { /* pass == 2 */
1198 * Hack to prevent phase error in the code
1202 * If the second operand is a forward reference,
1203 * the UNITY property of the number 1 in that
1204 * operand is cancelled. Otherwise the above
1205 * sequence will cause a phase error.
1207 * This hack means that the above code will
1208 * generate 286+ code.
1210 * The forward reference will mean that the
1211 * operand will not have the UNITY property on
1212 * the first pass, so the pass behaviours will
1216 if (output_ins
.operands
>= 2 &&
1217 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1218 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1220 /* Remove special properties bits */
1221 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1229 if (output_ins
.opcode
== I_EQU
) {
1232 * Special `..' EQUs get processed in pass two,
1233 * except `..@' macro-processor EQUs which are done
1234 * in the normal place.
1236 if (!output_ins
.label
)
1237 report_error(ERR_NONFATAL
,
1238 "EQU not preceded by label");
1240 else if (output_ins
.label
[0] != '.' ||
1241 output_ins
.label
[1] != '.' ||
1242 output_ins
.label
[2] == '@') {
1243 if (output_ins
.operands
== 1 &&
1244 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1245 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1248 opflags
& OPFLAG_EXTERN
;
1249 def_label(output_ins
.label
,
1250 output_ins
.oprs
[0].segment
,
1251 output_ins
.oprs
[0].offset
, NULL
,
1254 } else if (output_ins
.operands
== 2
1255 && (output_ins
.oprs
[0].
1257 && (output_ins
.oprs
[0].type
& COLON
)
1258 && output_ins
.oprs
[0].segment
==
1260 && output_ins
.oprs
[0].wrt
== NO_SEG
1261 && (output_ins
.oprs
[1].
1263 && output_ins
.oprs
[1].segment
==
1265 && output_ins
.oprs
[1].wrt
==
1267 def_label(output_ins
.label
,
1270 output_ins
.oprs
[1].offset
, NULL
,
1274 report_error(ERR_NONFATAL
,
1275 "bad syntax for EQU");
1277 } else { /* pass == 2 */
1279 * Special `..' EQUs get processed here, except
1280 * `..@' macro processor EQUs which are done above.
1282 if (output_ins
.label
[0] == '.' &&
1283 output_ins
.label
[1] == '.' &&
1284 output_ins
.label
[2] != '@') {
1285 if (output_ins
.operands
== 1 &&
1286 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1287 define_label(output_ins
.label
,
1288 output_ins
.oprs
[0].segment
,
1289 output_ins
.oprs
[0].offset
,
1290 NULL
, false, false, ofmt
,
1292 } else if (output_ins
.operands
== 2
1293 && (output_ins
.oprs
[0].
1295 && (output_ins
.oprs
[0].type
& COLON
)
1296 && output_ins
.oprs
[0].segment
==
1298 && (output_ins
.oprs
[1].
1300 && output_ins
.oprs
[1].segment
==
1302 define_label(output_ins
.label
,
1305 output_ins
.oprs
[1].offset
,
1306 NULL
, false, false, ofmt
,
1309 report_error(ERR_NONFATAL
,
1310 "bad syntax for EQU");
1313 } else { /* instruction isn't an EQU */
1317 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1318 &output_ins
, report_error
);
1320 /* if (using_debug_info) && output_ins.opcode != -1) */
1321 if (using_debug_info
)
1322 { /* fbk 03/25/01 */
1323 /* this is done here so we can do debug type info */
1325 TYS_ELEMENTS(output_ins
.operands
);
1326 switch (output_ins
.opcode
) {
1329 TYS_ELEMENTS(output_ins
.oprs
[0].
1334 TYS_ELEMENTS(output_ins
.oprs
[0].
1339 TYS_ELEMENTS(output_ins
.oprs
[0].
1344 TYS_ELEMENTS(output_ins
.oprs
[0].
1349 TYS_ELEMENTS(output_ins
.oprs
[0].
1353 typeinfo
|= TY_BYTE
;
1356 typeinfo
|= TY_WORD
;
1359 if (output_ins
.eops_float
)
1360 typeinfo
|= TY_FLOAT
;
1362 typeinfo
|= TY_DWORD
;
1365 typeinfo
|= TY_QWORD
;
1368 typeinfo
|= TY_TBYTE
;
1371 typeinfo
|= TY_OWORD
;
1374 typeinfo
= TY_LABEL
;
1378 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1383 SET_CURR_OFFS(offs
);
1386 * else l == -1 => invalid instruction, which will be
1387 * flagged as an error on pass 2
1390 } else { /* pass == 2 */
1391 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1392 &output_ins
, ofmt
, report_error
,
1394 SET_CURR_OFFS(offs
);
1398 cleanup_insn(&output_ins
);
1401 location
.offset
= offs
= GET_CURR_OFFS
;
1402 } /* end while (line = preproc->getline... */
1404 if (pass1
== 2 && global_offset_changed
)
1405 report_error(ERR_NONFATAL
,
1406 "phase error detected at end of assembly.");
1409 preproc
->cleanup(1);
1411 if (pass1
== 1 && terminate_after_phase
) {
1419 if (pass
> 1 && !global_offset_changed
) {
1422 pass
= pass_max
- 1;
1423 } else if (!(optimizing
> 0))
1426 } /* for (pass=1; pass<=2; pass++) */
1428 preproc
->cleanup(0);
1431 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1433 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1435 } /* exit from assemble_file (...) */
1437 static enum directives
getkw(char **directive
, char **value
)
1443 /* allow leading spaces or tabs */
1444 while (*buf
== ' ' || *buf
== '\t')
1452 while (*p
&& *p
!= ']')
1460 while (*p
&& *p
!= ';') {
1467 *directive
= p
= buf
+ 1;
1468 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1475 while (isspace(*buf
))
1476 buf
++; /* beppu - skip leading whitespace */
1483 return bsii(*directive
, directives
, elements(directives
));
1487 * gnu style error reporting
1488 * This function prints an error message to error_file in the
1489 * style used by GNU. An example would be:
1490 * file.asm:50: error: blah blah blah
1491 * where file.asm is the name of the file, 50 is the line number on
1492 * which the error occurs (or is detected) and "error:" is one of
1493 * the possible optional diagnostics -- it can be "error" or "warning"
1494 * or something else. Finally the line terminates with the actual
1497 * @param severity the severity of the warning or error
1498 * @param fmt the printf style format string
1500 static void report_error_gnu(int severity
, const char *fmt
, ...)
1504 if (is_suppressed_warning(severity
))
1507 if (severity
& ERR_NOFILE
)
1508 fputs("nasm: ", error_file
);
1510 char *currentfile
= NULL
;
1512 src_get(&lineno
, ¤tfile
);
1513 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1514 nasm_free(currentfile
);
1517 report_error_common(severity
, fmt
, ap
);
1522 * MS style error reporting
1523 * This function prints an error message to error_file in the
1524 * style used by Visual C and some other Microsoft tools. An example
1526 * file.asm(50) : error: blah blah blah
1527 * where file.asm is the name of the file, 50 is the line number on
1528 * which the error occurs (or is detected) and "error:" is one of
1529 * the possible optional diagnostics -- it can be "error" or "warning"
1530 * or something else. Finally the line terminates with the actual
1533 * @param severity the severity of the warning or error
1534 * @param fmt the printf style format string
1536 static void report_error_vc(int severity
, const char *fmt
, ...)
1540 if (is_suppressed_warning(severity
))
1543 if (severity
& ERR_NOFILE
)
1544 fputs("nasm: ", error_file
);
1546 char *currentfile
= NULL
;
1548 src_get(&lineno
, ¤tfile
);
1549 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1550 nasm_free(currentfile
);
1553 report_error_common(severity
, fmt
, ap
);
1558 * check for supressed warning
1559 * checks for suppressed warning or pass one only warning and we're
1562 * @param severity the severity of the warning or error
1563 * @return true if we should abort error/warning printing
1565 static int is_suppressed_warning(int severity
)
1568 * See if it's a suppressed warning.
1570 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1571 (severity
& ERR_WARN_MASK
) != 0 &&
1572 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1574 * See if it's a pass-one only warning and we're not in pass one.
1576 ((severity
& ERR_PASS1
) && pass0
== 2);
1580 * common error reporting
1581 * This is the common back end of the error reporting schemes currently
1582 * implemented. It prints the nature of the warning and then the
1583 * specific error message to error_file and may or may not return. It
1584 * doesn't return if the error severity is a "panic" or "debug" type.
1586 * @param severity the severity of the warning or error
1587 * @param fmt the printf style format string
1589 static void report_error_common(int severity
, const char *fmt
,
1592 switch (severity
& ERR_MASK
) {
1594 fputs("warning: ", error_file
);
1597 fputs("error: ", error_file
);
1600 fputs("fatal: ", error_file
);
1603 fputs("panic: ", error_file
);
1606 fputs("debug: ", error_file
);
1610 vfprintf(error_file
, fmt
, args
);
1611 fputc('\n', error_file
);
1613 if (severity
& ERR_USAGE
)
1616 switch (severity
& ERR_MASK
) {
1619 /* no further action, by definition */
1622 /* hack enables listing(!) on errors */
1623 terminate_after_phase
= true;
1632 exit(1); /* instantly die */
1633 break; /* placate silly compilers */
1636 /* abort(); *//* halt, catch fire, and dump core */
1642 static void usage(void)
1644 fputs("type `nasm -h' for help\n", error_file
);
1647 static void register_output_formats(void)
1649 ofmt
= ofmt_register(report_error
);
1652 #define BUF_DELTA 512
1654 static FILE *no_pp_fp
;
1655 static efunc no_pp_err
;
1656 static ListGen
*no_pp_list
;
1657 static int32_t no_pp_lineinc
;
1659 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1662 src_set_fname(nasm_strdup(file
));
1666 no_pp_fp
= fopen(file
, "r");
1668 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1669 "unable to open input file `%s'", file
);
1670 no_pp_list
= listgen
;
1671 (void)pass
; /* placate compilers */
1672 (void)eval
; /* placate compilers */
1675 static char *no_pp_getline(void)
1677 char *buffer
, *p
, *q
;
1680 bufsize
= BUF_DELTA
;
1681 buffer
= nasm_malloc(BUF_DELTA
);
1682 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1684 while (1) { /* Loop to handle %line */
1687 while (1) { /* Loop to handle long lines */
1688 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1692 if (p
> buffer
&& p
[-1] == '\n')
1694 if (p
- buffer
> bufsize
- 10) {
1696 offset
= p
- buffer
;
1697 bufsize
+= BUF_DELTA
;
1698 buffer
= nasm_realloc(buffer
, bufsize
);
1699 p
= buffer
+ offset
;
1703 if (!q
&& p
== buffer
) {
1709 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1710 * them are present at the end of the line.
1712 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1714 if (!nasm_strnicmp(buffer
, "%line", 5)) {
1717 char *nm
= nasm_malloc(strlen(buffer
));
1718 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1719 nasm_free(src_set_fname(nm
));
1729 no_pp_list
->line(LIST_READ
, buffer
);
1734 static void no_pp_cleanup(int pass
)
1736 (void)pass
; /* placate GCC */
1740 static uint32_t get_cpu(char *value
)
1742 if (!strcmp(value
, "8086"))
1744 if (!strcmp(value
, "186"))
1746 if (!strcmp(value
, "286"))
1748 if (!strcmp(value
, "386"))
1750 if (!strcmp(value
, "486"))
1752 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1754 if (!strcmp(value
, "686") ||
1755 !nasm_stricmp(value
, "ppro") ||
1756 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1758 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1760 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1761 !nasm_stricmp(value
, "willamette"))
1762 return IF_WILLAMETTE
;
1763 if (!nasm_stricmp(value
, "prescott"))
1765 if (!nasm_stricmp(value
, "x64") ||
1766 !nasm_stricmp(value
, "x86-64"))
1768 if (!nasm_stricmp(value
, "ia64") ||
1769 !nasm_stricmp(value
, "ia-64") ||
1770 !nasm_stricmp(value
, "itanium") ||
1771 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1774 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1775 "unknown 'cpu' type");
1777 return IF_PLEVEL
; /* the maximum level */
1780 static int get_bits(char *value
)
1784 if ((i
= atoi(value
)) == 16)
1785 return i
; /* set for a 16-bit segment */
1788 report_error(ERR_NONFATAL
,
1789 "cannot specify 32-bit segment on processor below a 386");
1792 } else if (i
== 64) {
1793 if (cpu
< IF_X86_64
) {
1794 report_error(ERR_NONFATAL
,
1795 "cannot specify 64-bit segment on processor below an x86-64");
1799 report_error(ERR_NONFATAL
,
1800 "%s output format does not support 64-bit code",
1805 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1806 "`%s' is not a valid segment size; must be 16, 32 or 64",