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 (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];
841 struct tokenval tokval
;
844 int pass_cnt
= 0; /* count actual passes */
846 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
847 report_error(ERR_FATAL
, "command line: "
848 "32-bit segment size requires a higher cpu");
850 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
851 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
852 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
856 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
857 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
858 /* pass0 seq is 0, 0, 0,..., 1, 2 */
860 def_label
= pass
> 1 ? redefine_label
: define_label
;
862 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
866 nasmlist
.init(listname
, report_error
);
869 global_offset_changed
= false; /* set by redefine_label */
870 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
873 saa_rewind(forwrefs
);
874 forwref
= saa_rstruct(forwrefs
);
876 offsets
= raa_init();
878 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
881 location
.known
= true;
882 location
.offset
= offs
= GET_CURR_OFFS
;
884 while ((line
= preproc
->getline())) {
888 /* here we parse our directives; this is not handled by the 'real'
891 d
= getkw(&directive
, &value
);
896 case D_SEGMENT
: /* [SEGMENT n] */
898 seg
= ofmt
->section(value
, pass2
, &sb
);
900 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
901 "segment name `%s' not recognized",
905 location
.segment
= seg
;
908 case D_EXTERN
: /* [EXTERN label:special] */
910 value
++; /* skip initial $ if present */
913 while (*q
&& *q
!= ':')
917 ofmt
->symdef(value
, 0L, 0L, 3, q
);
919 } else if (pass
== 1) { /* pass == 1 */
924 while (*q
&& *q
!= ':') {
930 report_error(ERR_NONFATAL
,
931 "identifier expected after EXTERN");
939 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
941 pass0
= 1; /* fake pass 1 in labels.c */
942 declare_as_global(value
, special
,
944 define_label(value
, seg_alloc(), 0L, NULL
,
945 false, true, ofmt
, report_error
);
948 } /* else pass0 == 1 */
950 case D_BITS
: /* [BITS bits] */
951 globalbits
= sb
= get_bits(value
);
953 case D_GLOBAL
: /* [GLOBAL symbol:special] */
955 value
++; /* skip initial $ if present */
956 if (pass0
== 2) { /* pass 2 */
958 while (*q
&& *q
!= ':')
962 ofmt
->symdef(value
, 0L, 0L, 3, q
);
964 } else if (pass2
== 1) { /* pass == 1 */
969 while (*q
&& *q
!= ':') {
975 report_error(ERR_NONFATAL
,
976 "identifier expected after GLOBAL");
984 declare_as_global(value
, special
, report_error
);
987 case D_COMMON
: /* [COMMON symbol size:special] */
989 value
++; /* skip initial $ if present */
995 while (*p
&& !isspace(*p
)) {
1001 report_error(ERR_NONFATAL
,
1002 "identifier expected after COMMON");
1008 while (*p
&& isspace(*p
))
1011 while (*q
&& *q
!= ':')
1018 size
= readnum(p
, &rn_error
);
1020 report_error(ERR_NONFATAL
,
1021 "invalid size specified"
1022 " in COMMON declaration");
1024 define_common(value
, seg_alloc(), size
,
1025 special
, ofmt
, report_error
);
1027 report_error(ERR_NONFATAL
,
1028 "no size specified in"
1029 " COMMON declaration");
1030 } else if (pass0
== 2) { /* pass == 2 */
1032 while (*q
&& *q
!= ':') {
1039 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1043 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1045 stdscan_bufptr
= value
;
1046 tokval
.t_type
= TOKEN_INVALID
;
1047 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1048 report_error
, NULL
);
1051 report_error(pass0
==
1052 1 ? ERR_NONFATAL
: ERR_PANIC
,
1053 "cannot use non-relocatable expression as "
1054 "ABSOLUTE address");
1056 abs_seg
= reloc_seg(e
);
1057 abs_offset
= reloc_value(e
);
1059 } else if (pass
== 1)
1060 abs_offset
= 0x100; /* don't go near zero in case of / */
1062 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1065 location
.segment
= NO_SEG
;
1067 case D_DEBUG
: /* [DEBUG] */
1073 while (*p
&& !isspace(*p
)) {
1080 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1081 "identifier expected after DEBUG");
1084 while (*p
&& isspace(*p
))
1086 if (pass
== pass_max
)
1087 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1089 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1091 while (*value
&& isspace(*value
))
1094 if (*value
== '+' || *value
== '-') {
1095 validid
= (*value
== '-') ? true : false;
1100 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1101 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1103 if (i
<= ERR_WARN_MAX
)
1104 suppressed
[i
] = validid
;
1106 report_error(ERR_NONFATAL
,
1107 "invalid warning id in WARNING directive");
1110 case D_CPU
: /* [CPU] */
1111 cpu
= get_cpu(value
);
1113 case D_LIST
: /* [LIST {+|-}] */
1114 while (*value
&& isspace(*value
))
1117 if (*value
== '+') {
1120 if (*value
== '-') {
1127 case D_DEFAULT
: /* [DEFAULT] */
1129 stdscan_bufptr
= value
;
1130 tokval
.t_type
= TOKEN_INVALID
;
1131 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1132 switch ((int)tokval
.t_integer
) {
1148 if (float_option(value
)) {
1149 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1150 "unknown 'float' directive: %s",
1155 if (!ofmt
->directive(directive
, value
, pass2
))
1156 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1157 "unrecognised directive [%s]",
1161 report_error(ERR_NONFATAL
,
1162 "invalid parameter to [%s] directive",
1165 } else { /* it isn't a directive */
1167 parse_line(pass1
, line
, &output_ins
,
1168 report_error
, evaluate
, def_label
);
1170 if (!(optimizing
> 0) && pass
== 2) {
1171 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1172 output_ins
.forw_ref
= true;
1174 output_ins
.oprs
[forwref
->operand
].opflags
|=
1176 forwref
= saa_rstruct(forwrefs
);
1177 } while (forwref
!= NULL
1178 && forwref
->lineno
== globallineno
);
1180 output_ins
.forw_ref
= false;
1183 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1185 for (i
= 0; i
< output_ins
.operands
; i
++) {
1186 if (output_ins
.oprs
[i
].
1187 opflags
& OPFLAG_FORWARD
) {
1188 struct forwrefinfo
*fwinf
=
1189 (struct forwrefinfo
*)
1190 saa_wstruct(forwrefs
);
1191 fwinf
->lineno
= globallineno
;
1195 } else { /* pass == 2 */
1197 * Hack to prevent phase error in the code
1201 * If the second operand is a forward reference,
1202 * the UNITY property of the number 1 in that
1203 * operand is cancelled. Otherwise the above
1204 * sequence will cause a phase error.
1206 * This hack means that the above code will
1207 * generate 286+ code.
1209 * The forward reference will mean that the
1210 * operand will not have the UNITY property on
1211 * the first pass, so the pass behaviours will
1215 if (output_ins
.operands
>= 2 &&
1216 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1217 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1219 /* Remove special properties bits */
1220 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1228 if (output_ins
.opcode
== I_EQU
) {
1231 * Special `..' EQUs get processed in pass two,
1232 * except `..@' macro-processor EQUs which are done
1233 * in the normal place.
1235 if (!output_ins
.label
)
1236 report_error(ERR_NONFATAL
,
1237 "EQU not preceded by label");
1239 else if (output_ins
.label
[0] != '.' ||
1240 output_ins
.label
[1] != '.' ||
1241 output_ins
.label
[2] == '@') {
1242 if (output_ins
.operands
== 1 &&
1243 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1244 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1247 opflags
& OPFLAG_EXTERN
;
1248 def_label(output_ins
.label
,
1249 output_ins
.oprs
[0].segment
,
1250 output_ins
.oprs
[0].offset
, NULL
,
1253 } else if (output_ins
.operands
== 2
1254 && (output_ins
.oprs
[0].
1256 && (output_ins
.oprs
[0].type
& COLON
)
1257 && output_ins
.oprs
[0].segment
==
1259 && output_ins
.oprs
[0].wrt
== NO_SEG
1260 && (output_ins
.oprs
[1].
1262 && output_ins
.oprs
[1].segment
==
1264 && output_ins
.oprs
[1].wrt
==
1266 def_label(output_ins
.label
,
1269 output_ins
.oprs
[1].offset
, NULL
,
1273 report_error(ERR_NONFATAL
,
1274 "bad syntax for EQU");
1276 } else { /* pass == 2 */
1278 * Special `..' EQUs get processed here, except
1279 * `..@' macro processor EQUs which are done above.
1281 if (output_ins
.label
[0] == '.' &&
1282 output_ins
.label
[1] == '.' &&
1283 output_ins
.label
[2] != '@') {
1284 if (output_ins
.operands
== 1 &&
1285 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1286 define_label(output_ins
.label
,
1287 output_ins
.oprs
[0].segment
,
1288 output_ins
.oprs
[0].offset
,
1289 NULL
, false, false, ofmt
,
1291 } else if (output_ins
.operands
== 2
1292 && (output_ins
.oprs
[0].
1294 && (output_ins
.oprs
[0].type
& COLON
)
1295 && output_ins
.oprs
[0].segment
==
1297 && (output_ins
.oprs
[1].
1299 && output_ins
.oprs
[1].segment
==
1301 define_label(output_ins
.label
,
1304 output_ins
.oprs
[1].offset
,
1305 NULL
, false, false, ofmt
,
1308 report_error(ERR_NONFATAL
,
1309 "bad syntax for EQU");
1312 } else { /* instruction isn't an EQU */
1316 int32_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1317 &output_ins
, report_error
);
1319 /* if (using_debug_info) && output_ins.opcode != -1) */
1320 if (using_debug_info
)
1321 { /* fbk 03/25/01 */
1322 /* this is done here so we can do debug type info */
1324 TYS_ELEMENTS(output_ins
.operands
);
1325 switch (output_ins
.opcode
) {
1328 TYS_ELEMENTS(output_ins
.oprs
[0].
1333 TYS_ELEMENTS(output_ins
.oprs
[0].
1338 TYS_ELEMENTS(output_ins
.oprs
[0].
1343 TYS_ELEMENTS(output_ins
.oprs
[0].
1348 TYS_ELEMENTS(output_ins
.oprs
[0].
1352 typeinfo
|= TY_BYTE
;
1355 typeinfo
|= TY_WORD
;
1358 if (output_ins
.eops_float
)
1359 typeinfo
|= TY_FLOAT
;
1361 typeinfo
|= TY_DWORD
;
1364 typeinfo
|= TY_QWORD
;
1367 typeinfo
|= TY_TBYTE
;
1370 typeinfo
|= TY_OWORD
;
1373 typeinfo
= TY_LABEL
;
1377 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1382 SET_CURR_OFFS(offs
);
1385 * else l == -1 => invalid instruction, which will be
1386 * flagged as an error on pass 2
1389 } else { /* pass == 2 */
1390 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1391 &output_ins
, ofmt
, report_error
,
1393 SET_CURR_OFFS(offs
);
1397 cleanup_insn(&output_ins
);
1400 location
.offset
= offs
= GET_CURR_OFFS
;
1401 } /* end while (line = preproc->getline... */
1403 if (pass1
== 2 && global_offset_changed
)
1404 report_error(ERR_NONFATAL
,
1405 "phase error detected at end of assembly.");
1408 preproc
->cleanup(1);
1410 if (pass1
== 1 && terminate_after_phase
) {
1418 if (pass
> 1 && !global_offset_changed
) {
1421 pass
= pass_max
- 1;
1422 } else if (!(optimizing
> 0))
1425 } /* for (pass=1; pass<=2; pass++) */
1427 preproc
->cleanup(0);
1430 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1432 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1434 } /* exit from assemble_file (...) */
1436 static enum directives
getkw(char **directive
, char **value
)
1442 /* allow leading spaces or tabs */
1443 while (*buf
== ' ' || *buf
== '\t')
1451 while (*p
&& *p
!= ']')
1459 while (*p
&& *p
!= ';') {
1466 *directive
= p
= buf
+ 1;
1467 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1474 while (isspace(*buf
))
1475 buf
++; /* beppu - skip leading whitespace */
1482 return bsii(*directive
, directives
, elements(directives
));
1486 * gnu style error reporting
1487 * This function prints an error message to error_file in the
1488 * style used by GNU. An example would be:
1489 * file.asm:50: error: blah blah blah
1490 * where file.asm is the name of the file, 50 is the line number on
1491 * which the error occurs (or is detected) and "error:" is one of
1492 * the possible optional diagnostics -- it can be "error" or "warning"
1493 * or something else. Finally the line terminates with the actual
1496 * @param severity the severity of the warning or error
1497 * @param fmt the printf style format string
1499 static void report_error_gnu(int severity
, const char *fmt
, ...)
1503 if (is_suppressed_warning(severity
))
1506 if (severity
& ERR_NOFILE
)
1507 fputs("nasm: ", error_file
);
1509 char *currentfile
= NULL
;
1511 src_get(&lineno
, ¤tfile
);
1512 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1513 nasm_free(currentfile
);
1516 report_error_common(severity
, fmt
, ap
);
1521 * MS style error reporting
1522 * This function prints an error message to error_file in the
1523 * style used by Visual C and some other Microsoft tools. An example
1525 * file.asm(50) : error: blah blah blah
1526 * where file.asm is the name of the file, 50 is the line number on
1527 * which the error occurs (or is detected) and "error:" is one of
1528 * the possible optional diagnostics -- it can be "error" or "warning"
1529 * or something else. Finally the line terminates with the actual
1532 * @param severity the severity of the warning or error
1533 * @param fmt the printf style format string
1535 static void report_error_vc(int severity
, const char *fmt
, ...)
1539 if (is_suppressed_warning(severity
))
1542 if (severity
& ERR_NOFILE
)
1543 fputs("nasm: ", error_file
);
1545 char *currentfile
= NULL
;
1547 src_get(&lineno
, ¤tfile
);
1548 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1549 nasm_free(currentfile
);
1552 report_error_common(severity
, fmt
, ap
);
1557 * check for supressed warning
1558 * checks for suppressed warning or pass one only warning and we're
1561 * @param severity the severity of the warning or error
1562 * @return true if we should abort error/warning printing
1564 static int is_suppressed_warning(int severity
)
1567 * See if it's a suppressed warning.
1569 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1570 (severity
& ERR_WARN_MASK
) != 0 &&
1571 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1573 * See if it's a pass-one only warning and we're not in pass one.
1575 ((severity
& ERR_PASS1
) && pass0
== 2);
1579 * common error reporting
1580 * This is the common back end of the error reporting schemes currently
1581 * implemented. It prints the nature of the warning and then the
1582 * specific error message to error_file and may or may not return. It
1583 * doesn't return if the error severity is a "panic" or "debug" type.
1585 * @param severity the severity of the warning or error
1586 * @param fmt the printf style format string
1588 static void report_error_common(int severity
, const char *fmt
,
1591 switch (severity
& ERR_MASK
) {
1593 fputs("warning: ", error_file
);
1596 fputs("error: ", error_file
);
1599 fputs("fatal: ", error_file
);
1602 fputs("panic: ", error_file
);
1605 fputs("debug: ", error_file
);
1609 vfprintf(error_file
, fmt
, args
);
1610 fputc('\n', error_file
);
1612 if (severity
& ERR_USAGE
)
1615 switch (severity
& ERR_MASK
) {
1618 /* no further action, by definition */
1621 /* hack enables listing(!) on errors */
1622 terminate_after_phase
= true;
1631 exit(1); /* instantly die */
1632 break; /* placate silly compilers */
1635 /* abort(); *//* halt, catch fire, and dump core */
1641 static void usage(void)
1643 fputs("type `nasm -h' for help\n", error_file
);
1646 static void register_output_formats(void)
1648 ofmt
= ofmt_register(report_error
);
1651 #define BUF_DELTA 512
1653 static FILE *no_pp_fp
;
1654 static efunc no_pp_err
;
1655 static ListGen
*no_pp_list
;
1656 static int32_t no_pp_lineinc
;
1658 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1661 src_set_fname(nasm_strdup(file
));
1665 no_pp_fp
= fopen(file
, "r");
1667 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1668 "unable to open input file `%s'", file
);
1669 no_pp_list
= listgen
;
1670 (void)pass
; /* placate compilers */
1671 (void)eval
; /* placate compilers */
1674 static char *no_pp_getline(void)
1676 char *buffer
, *p
, *q
;
1679 bufsize
= BUF_DELTA
;
1680 buffer
= nasm_malloc(BUF_DELTA
);
1681 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1683 while (1) { /* Loop to handle %line */
1686 while (1) { /* Loop to handle long lines */
1687 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1691 if (p
> buffer
&& p
[-1] == '\n')
1693 if (p
- buffer
> bufsize
- 10) {
1695 offset
= p
- buffer
;
1696 bufsize
+= BUF_DELTA
;
1697 buffer
= nasm_realloc(buffer
, bufsize
);
1698 p
= buffer
+ offset
;
1702 if (!q
&& p
== buffer
) {
1708 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1709 * them are present at the end of the line.
1711 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1713 if (!nasm_strnicmp(buffer
, "%line", 5)) {
1716 char *nm
= nasm_malloc(strlen(buffer
));
1717 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1718 nasm_free(src_set_fname(nm
));
1728 no_pp_list
->line(LIST_READ
, buffer
);
1733 static void no_pp_cleanup(int pass
)
1735 (void)pass
; /* placate GCC */
1739 static uint32_t get_cpu(char *value
)
1741 if (!strcmp(value
, "8086"))
1743 if (!strcmp(value
, "186"))
1745 if (!strcmp(value
, "286"))
1747 if (!strcmp(value
, "386"))
1749 if (!strcmp(value
, "486"))
1751 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1753 if (!strcmp(value
, "686") ||
1754 !nasm_stricmp(value
, "ppro") ||
1755 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1757 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1759 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1760 !nasm_stricmp(value
, "willamette"))
1761 return IF_WILLAMETTE
;
1762 if (!nasm_stricmp(value
, "prescott"))
1764 if (!nasm_stricmp(value
, "x64") ||
1765 !nasm_stricmp(value
, "x86-64"))
1767 if (!nasm_stricmp(value
, "ia64") ||
1768 !nasm_stricmp(value
, "ia-64") ||
1769 !nasm_stricmp(value
, "itanium") ||
1770 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1773 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1774 "unknown 'cpu' type");
1776 return IF_PLEVEL
; /* the maximum level */
1779 static int get_bits(char *value
)
1783 if ((i
= atoi(value
)) == 16)
1784 return i
; /* set for a 16-bit segment */
1787 report_error(ERR_NONFATAL
,
1788 "cannot specify 32-bit segment on processor below a 386");
1791 } else if (i
== 64) {
1792 if (cpu
< IF_X86_64
) {
1793 report_error(ERR_NONFATAL
,
1794 "cannot specify 64-bit segment on processor below an x86-64");
1798 report_error(ERR_NONFATAL
,
1799 "%s output format does not support 64-bit code",
1804 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1805 "`%s' is not a valid segment size; must be 16, 32 or 64",