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.
28 struct forwrefinfo
{ /* info held on forward refs. */
33 static int get_bits(char *value
);
34 static uint32_t get_cpu(char *cpu_str
);
35 static void parse_cmdline(int, char **);
36 static void assemble_file(char *);
37 static int getkw(char **directive
, char **value
);
38 static void register_output_formats(void);
39 static void report_error_gnu(int severity
, const char *fmt
, ...);
40 static void report_error_vc(int severity
, const char *fmt
, ...);
41 static void report_error_common(int severity
, const char *fmt
,
43 static int is_suppressed_warning(int severity
);
44 static void usage(void);
45 static efunc report_error
;
47 static int using_debug_info
, opt_verbose_info
;
48 int tasm_compatible_mode
= FALSE
;
53 static char inname
[FILENAME_MAX
];
54 static char outname
[FILENAME_MAX
];
55 static char listname
[FILENAME_MAX
];
56 static int globallineno
; /* for forward-reference tracking */
57 /* static int pass = 0; */
58 static struct ofmt
*ofmt
= NULL
;
60 static FILE *error_file
; /* Where to write error messages */
62 static FILE *ofile
= NULL
;
63 int optimizing
= -1; /* number of optimization passes to take */
64 static int sb
, cmd_sb
= 16; /* by default */
65 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
66 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
67 int global_offset_changed
; /* referenced in labels.c */
69 static loc_t location
;
70 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
71 int32_t abs_seg
; /* ABSOLUTE segment basis */
72 int32_t abs_offset
; /* ABSOLUTE offset */
74 static struct RAA
*offsets
;
76 static struct SAA
*forwrefs
; /* keep track of forward references */
77 static struct forwrefinfo
*forwref
;
79 static Preproc
*preproc
;
81 op_normal
, /* Preprocess and assemble */
82 op_preprocess
, /* Preprocess only */
83 op_depend
/* Generate dependencies */
85 static enum op_type operating_mode
;
88 * Which of the suppressible warnings are suppressed. Entry zero
89 * doesn't do anything. Initial defaults are given here.
91 static char suppressed
[1 + ERR_WARN_MAX
] = {
92 0, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
96 * The option names for the suppressible warnings. As before, entry
99 static const char *suppressed_names
[1 + ERR_WARN_MAX
] = {
100 NULL
, "macro-params", "macro-selfref", "orphan-labels",
106 * The explanations for the suppressible warnings. As before, entry
109 static const char *suppressed_what
[1 + ERR_WARN_MAX
] = {
111 "macro calls with wrong no. of params",
112 "cyclic macro self-references",
113 "labels alone on lines without trailing `:'",
114 "numeric constants greater than 0xFFFFFFFF",
115 "using 8- or 16-bit relocation in ELF, a GNU extension"
119 * This is a null preprocessor which just copies lines from input
120 * to output. It's used when someone explicitly requests that NASM
121 * not preprocess their source file.
124 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*);
125 static char *no_pp_getline(void);
126 static void no_pp_cleanup(int);
127 static Preproc no_pp
= {
134 * get/set current offset...
136 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
137 raa_read(offsets,location.segment))
138 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
139 (void)(offsets=raa_write(offsets,location.segment,(x))))
141 static int want_usage
;
142 static int terminate_after_phase
;
143 int user_nolist
= 0; /* fbk 9/2/00 */
145 static void nasm_fputs(const char *line
, FILE * outfile
)
148 fputs(line
, outfile
);
149 fputc('\n', outfile
);
154 int main(int argc
, char **argv
)
157 want_usage
= terminate_after_phase
= FALSE
;
158 report_error
= report_error_gnu
;
160 nasm_set_malloc_error(report_error
);
161 offsets
= raa_init();
162 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
165 operating_mode
= op_normal
;
171 register_output_formats();
173 parse_cmdline(argc
, argv
);
175 if (terminate_after_phase
) {
181 /* If debugging info is disabled, suppress any debug calls */
182 if (!using_debug_info
)
183 ofmt
->current_dfmt
= &null_debug_form
;
186 pp_extra_stdmac(ofmt
->stdmac
);
187 parser_global_info(ofmt
, &location
);
188 eval_global_info(ofmt
, lookup_label
, &location
);
190 /* define some macros dependent of command-line */
193 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
198 switch (operating_mode
) {
202 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
203 if (outname
[0] == '\0')
204 ofmt
->filename(inname
, outname
, report_error
);
206 fprintf(stdout
, "%s: %s", outname
, inname
);
207 while ((line
= preproc
->getline()))
217 char *file_name
= NULL
;
218 int32_t prior_linnum
= 0;
222 ofile
= fopen(outname
, "w");
224 report_error(ERR_FATAL
| ERR_NOFILE
,
225 "unable to open output file `%s'",
230 location
.known
= FALSE
;
233 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
234 while ((line
= preproc
->getline())) {
236 * We generate %line directives if needed for later programs
238 int32_t linnum
= prior_linnum
+= lineinc
;
239 int altline
= src_get(&linnum
, &file_name
);
241 if (altline
== 1 && lineinc
== 1)
242 nasm_fputs("", ofile
);
244 lineinc
= (altline
!= -1 || lineinc
!= 1);
245 fprintf(ofile
? ofile
: stdout
,
246 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
249 prior_linnum
= linnum
;
251 nasm_fputs(line
, ofile
);
254 nasm_free(file_name
);
258 if (ofile
&& terminate_after_phase
)
266 * We must call ofmt->filename _anyway_, even if the user
267 * has specified their own output file, because some
268 * formats (eg OBJ and COFF) use ofmt->filename to find out
269 * the name of the input file and then put that inside the
272 ofmt
->filename(inname
, outname
, report_error
);
274 ofile
= fopen(outname
, "wb");
276 report_error(ERR_FATAL
| ERR_NOFILE
,
277 "unable to open output file `%s'", outname
);
281 * We must call init_labels() before ofmt->init() since
282 * some object formats will want to define labels in their
283 * init routines. (eg OS/2 defines the FLAT group)
287 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
289 assemble_file(inname
);
291 if (!terminate_after_phase
) {
292 ofmt
->cleanup(using_debug_info
);
296 * We had an fclose on the output file here, but we
297 * actually do that in all the object file drivers as well,
298 * so we're leaving out the one here.
317 if (terminate_after_phase
)
324 * Get a parameter for a command line option.
325 * First arg must be in the form of e.g. -f...
327 static char *get_param(char *p
, char *q
, int *advance
)
330 if (p
[2]) { /* the parameter's in the option */
340 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
341 "option `-%c' requires an argument", p
[1]);
351 #define OPT_POSTFIX 1
352 struct textargs textopts
[] = {
353 {"prefix", OPT_PREFIX
},
354 {"postfix", OPT_POSTFIX
},
359 static int process_arg(char *p
, char *q
)
367 if (p
[0] == '-' && !stopoptions
) {
372 case 'o': /* these parameters take values */
387 if (!(param
= get_param(p
, q
, &advance
)))
389 if (p
[1] == 'o') { /* output file */
390 strcpy(outname
, param
);
391 } else if (p
[1] == 'f') { /* output format */
392 ofmt
= ofmt_find(param
);
394 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
395 "unrecognised output format `%s' - "
396 "use -hf for a list", param
);
398 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
399 } else if (p
[1] == 'O') { /* Optimization level */
403 if (isdigit(*param
)) {
405 while (isdigit(*++param
)) ;
407 optimizing
= -1; /* 0.98 behaviour */
409 optimizing
= 0; /* Two passes, 0.98.09 behavior */
411 optimizing
= opt
; /* Multiple passes */
413 if (*param
== 'v' || *param
== '+') {
415 opt_verbose_info
= TRUE
;
417 } else { /* garbage */
422 } /* while (*param) */
424 report_error(ERR_FATAL
,
425 "command line optimization level must be 'v', 0..3 or <nn>");
426 } else if (p
[1] == 'P' || p
[1] == 'p') { /* pre-include */
427 pp_pre_include(param
);
428 } else if (p
[1] == 'D' || p
[1] == 'd') { /* pre-define */
429 pp_pre_define(param
);
430 } else if (p
[1] == 'U' || p
[1] == 'u') { /* un-define */
431 pp_pre_undefine(param
);
432 } else if (p
[1] == 'I' || p
[1] == 'i') { /* include search path */
433 pp_include_path(param
);
434 } else if (p
[1] == 'l') { /* listing file */
435 strcpy(listname
, param
);
436 } else if (p
[1] == 'E') { /* error messages file */
437 error_file
= fopen(param
, "w");
439 error_file
= stderr
; /* Revert to default! */
440 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
441 "cannot open file `%s' for error messages",
444 } else if (p
[1] == 'F') { /* specify debug format */
445 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
446 if (!ofmt
->current_dfmt
) {
447 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
448 "unrecognized debug format `%s' for"
449 " output format `%s'",
450 param
, ofmt
->shortname
);
452 } else if (p
[1] == 'X') { /* specify error reporting format */
453 if (nasm_stricmp("vc", param
) == 0)
454 report_error
= report_error_vc
;
455 else if (nasm_stricmp("gnu", param
) == 0)
456 report_error
= report_error_gnu
;
458 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
459 "unrecognized error reporting format `%s'",
464 using_debug_info
= TRUE
;
468 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
470 " [options...] [--] filename\n"
471 " or nasm -r for version info (obsolete)\n"
472 " or nasm -v for version info (preferred)\n\n"
473 " -t assemble in SciTech TASM compatible mode\n"
474 " -g generate debug information in selected format.\n");
476 (" -e preprocess only (writes output to stdout by default)\n"
477 " -a don't preprocess (assemble only)\n"
478 " -M generate Makefile dependencies on stdout\n\n"
479 " -E<file> redirect error messages to file\n"
480 " -s redirect error messages to stdout\n\n"
481 " -F format select a debugging format\n\n"
482 " -I<path> adds a pathname to the include file path\n");
484 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
485 " -P<file> pre-includes a file\n"
486 " -D<macro>[=<value>] pre-defines a macro\n"
487 " -U<macro> undefines a macro\n"
488 " -X<format> specifies error reporting format (gnu or vc)\n"
489 " -w+foo enables warnings about foo; -w-foo disables them\n"
490 "where foo can be:\n");
491 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
492 printf(" %-23s %s (default %s)\n",
493 suppressed_names
[i
], suppressed_what
[i
],
494 suppressed
[i
] ? "off" : "on");
496 ("\nresponse files should contain command line parameters"
497 ", one per line.\n");
499 printf("\nvalid output formats for -f are"
500 " (`*' denotes default):\n");
501 ofmt_list(ofmt
, stdout
);
503 printf("\nFor a list of valid output formats, use -hf.\n");
504 printf("For a list of debug formats, use -f <form> -y.\n");
506 exit(0); /* never need usage message here */
509 printf("\nvalid debug formats for '%s' output format are"
510 " ('*' denotes default):\n", ofmt
->shortname
);
511 dfmt_list(ofmt
, stdout
);
515 tasm_compatible_mode
= TRUE
;
520 const char *nasm_version_string
=
521 "NASM version " NASM_VER
" compiled on " __DATE__
526 puts(nasm_version_string
);
527 exit(0); /* never need usage message here */
530 case 'e': /* preprocess only */
531 operating_mode
= op_preprocess
;
533 case 'a': /* assemble only - don't preprocess */
537 if (p
[2] != '+' && p
[2] != '-') {
538 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
539 "invalid option to `-w'");
541 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
542 if (!nasm_stricmp(p
+ 3, suppressed_names
[i
]))
544 if (i
<= ERR_WARN_MAX
)
545 suppressed
[i
] = (p
[2] == '-');
547 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
548 "invalid option to `-w'");
552 operating_mode
= op_depend
;
559 if (p
[2] == 0) { /* -- => stop processing options */
563 for (s
= 0; textopts
[s
].label
; s
++) {
564 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
575 report_error(ERR_NONFATAL
| ERR_NOFILE
|
577 "option `--%s' requires an argument",
581 advance
= 1, param
= q
;
584 if (s
== OPT_PREFIX
) {
585 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
586 lprefix
[PREFIX_MAX
- 1] = 0;
589 if (s
== OPT_POSTFIX
) {
590 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
591 lpostfix
[POSTFIX_MAX
- 1] = 0;
598 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
599 "unrecognised option `--%s'", p
+ 2);
607 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
608 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
609 "unrecognised option `-%c'", p
[1]);
614 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
615 "more than one input file specified");
623 #define ARG_BUF_DELTA 128
625 static void process_respfile(FILE * rfile
)
627 char *buffer
, *p
, *q
, *prevarg
;
628 int bufsize
, prevargsize
;
630 bufsize
= prevargsize
= ARG_BUF_DELTA
;
631 buffer
= nasm_malloc(ARG_BUF_DELTA
);
632 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
635 while (1) { /* Loop to handle all lines in file */
638 while (1) { /* Loop to handle long lines */
639 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
643 if (p
> buffer
&& p
[-1] == '\n')
645 if (p
- buffer
> bufsize
- 10) {
648 bufsize
+= ARG_BUF_DELTA
;
649 buffer
= nasm_realloc(buffer
, bufsize
);
654 if (!q
&& p
== buffer
) {
656 process_arg(prevarg
, NULL
);
663 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
664 * them are present at the end of the line.
666 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
668 while (p
> buffer
&& isspace(p
[-1]))
675 if (process_arg(prevarg
, p
))
678 if (strlen(p
) > prevargsize
- 10) {
679 prevargsize
+= ARG_BUF_DELTA
;
680 prevarg
= nasm_realloc(prevarg
, prevargsize
);
686 /* Function to process args from a string of args, rather than the
687 * argv array. Used by the environment variable and response file
690 static void process_args(char *args
)
692 char *p
, *q
, *arg
, *prevarg
;
693 char separator
= ' ';
701 while (*p
&& *p
!= separator
)
703 while (*p
== separator
)
707 if (process_arg(prevarg
, arg
))
711 process_arg(arg
, NULL
);
714 static void parse_cmdline(int argc
, char **argv
)
717 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
719 *inname
= *outname
= *listname
= '\0';
722 * First, process the NASMENV environment variable.
724 envreal
= getenv("NASMENV");
727 envcopy
= nasm_strdup(envreal
);
728 process_args(envcopy
);
733 * Now process the actual command line.
738 if (argv
[0][0] == '@') {
739 /* We have a response file, so process this as a set of
740 * arguments like the environment variable. This allows us
741 * to have multiple arguments on a single line, which is
742 * different to the -@resp file processing below for regular
745 char *str
= malloc(2048);
746 FILE *f
= fopen(&argv
[0][1], "r");
748 printf("out of memory");
752 while (fgets(str
, 2048, f
)) {
761 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
762 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
);
764 rfile
= fopen(p
, "r");
766 process_respfile(rfile
);
769 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
770 "unable to open response file `%s'", p
);
773 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
774 argv
+= i
, argc
-= i
;
778 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
779 "no input file specified");
782 /* List of directives */
784 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
785 D_EXTERN
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
787 static const char *directives
[] = {
788 "", "absolute", "bits", "common", "cpu", "debug", "default",
789 "extern", "global", "list", "section", "segment", "warning"
792 static void assemble_file(char *fname
)
794 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
796 int i
, rn_error
, validid
;
798 struct tokenval tokval
;
801 int pass_cnt
= 0; /* count actual passes */
803 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
804 report_error(ERR_FATAL
, "command line: "
805 "32-bit segment size requires a higher cpu");
807 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
808 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
809 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
813 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
814 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
815 /* pass0 seq is 0, 0, 0,..., 1, 2 */
817 def_label
= pass
> 1 ? redefine_label
: define_label
;
819 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
823 nasmlist
.init(listname
, report_error
);
826 global_offset_changed
= FALSE
; /* set by redefine_label */
827 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
830 saa_rewind(forwrefs
);
831 forwref
= saa_rstruct(forwrefs
);
833 offsets
= raa_init();
835 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
838 location
.known
= TRUE
;
839 location
.offset
= offs
= GET_CURR_OFFS
;
841 while ((line
= preproc
->getline())) {
844 /* here we parse our directives; this is not handled by the 'real'
847 i
= getkw(&directive
, &value
);
852 case D_SEGMENT
: /* [SEGMENT n] */
854 seg
= ofmt
->section(value
, pass2
, &sb
);
856 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
857 "segment name `%s' not recognized",
861 location
.segment
= seg
;
864 case D_EXTERN
: /* [EXTERN label:special] */
866 value
++; /* skip initial $ if present */
869 while (*q
&& *q
!= ':')
873 ofmt
->symdef(value
, 0L, 0L, 3, q
);
875 } else if (pass
== 1) { /* pass == 1 */
880 while (*q
&& *q
!= ':') {
886 report_error(ERR_NONFATAL
,
887 "identifier expected after EXTERN");
895 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
897 pass0
= 1; /* fake pass 1 in labels.c */
898 declare_as_global(value
, special
,
900 define_label(value
, seg_alloc(), 0L, NULL
,
901 FALSE
, TRUE
, ofmt
, report_error
);
904 } /* else pass0 == 1 */
906 case D_BITS
: /* [BITS bits] */
907 globalbits
= sb
= get_bits(value
);
909 case D_GLOBAL
: /* [GLOBAL symbol:special] */
911 value
++; /* skip initial $ if present */
912 if (pass0
== 2) { /* pass 2 */
914 while (*q
&& *q
!= ':')
918 ofmt
->symdef(value
, 0L, 0L, 3, q
);
920 } else if (pass2
== 1) { /* pass == 1 */
925 while (*q
&& *q
!= ':') {
931 report_error(ERR_NONFATAL
,
932 "identifier expected after GLOBAL");
940 declare_as_global(value
, special
, report_error
);
943 case D_COMMON
: /* [COMMON symbol size:special] */
945 value
++; /* skip initial $ if present */
951 while (*p
&& !isspace(*p
)) {
957 report_error(ERR_NONFATAL
,
958 "identifier expected after COMMON");
964 while (*p
&& isspace(*p
))
967 while (*q
&& *q
!= ':')
974 size
= readnum(p
, &rn_error
);
976 report_error(ERR_NONFATAL
,
977 "invalid size specified"
978 " in COMMON declaration");
980 define_common(value
, seg_alloc(), size
,
981 special
, ofmt
, report_error
);
983 report_error(ERR_NONFATAL
,
984 "no size specified in"
985 " COMMON declaration");
986 } else if (pass0
== 2) { /* pass == 2 */
988 while (*q
&& *q
!= ':') {
995 ofmt
->symdef(value
, 0L, 0L, 3, q
);
999 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1001 stdscan_bufptr
= value
;
1002 tokval
.t_type
= TOKEN_INVALID
;
1003 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1004 report_error
, NULL
);
1007 report_error(pass0
==
1008 1 ? ERR_NONFATAL
: ERR_PANIC
,
1009 "cannot use non-relocatable expression as "
1010 "ABSOLUTE address");
1012 abs_seg
= reloc_seg(e
);
1013 abs_offset
= reloc_value(e
);
1015 } else if (pass
== 1)
1016 abs_offset
= 0x100; /* don't go near zero in case of / */
1018 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1021 location
.segment
= NO_SEG
;
1023 case D_DEBUG
: /* [DEBUG] */
1029 while (*p
&& !isspace(*p
)) {
1036 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1037 "identifier expected after DEBUG");
1040 while (*p
&& isspace(*p
))
1042 if (pass
== pass_max
)
1043 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1045 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1047 while (*value
&& isspace(*value
))
1050 if (*value
== '+' || *value
== '-') {
1051 validid
= (*value
== '-') ? TRUE
: FALSE
;
1056 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1057 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1059 if (i
<= ERR_WARN_MAX
)
1060 suppressed
[i
] = validid
;
1062 report_error(ERR_NONFATAL
,
1063 "invalid warning id in WARNING directive");
1066 case D_CPU
: /* [CPU] */
1067 cpu
= get_cpu(value
);
1069 case D_LIST
: /* [LIST {+|-}] */
1070 while (*value
&& isspace(*value
))
1073 if (*value
== '+') {
1076 if (*value
== '-') {
1083 case D_DEFAULT
: /* [DEFAULT] */
1085 stdscan_bufptr
= value
;
1086 tokval
.t_type
= TOKEN_INVALID
;
1087 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1088 switch ((int)tokval
.t_integer
) {
1104 if (!ofmt
->directive(directive
, value
, pass2
))
1105 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1106 "unrecognised directive [%s]",
1110 report_error(ERR_NONFATAL
,
1111 "invalid parameter to [%s] directive",
1114 } else { /* it isn't a directive */
1116 parse_line(pass1
, line
, &output_ins
,
1117 report_error
, evaluate
, def_label
);
1119 if (!(optimizing
> 0) && pass
== 2) {
1120 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1121 output_ins
.forw_ref
= TRUE
;
1123 output_ins
.oprs
[forwref
->operand
].opflags
|=
1125 forwref
= saa_rstruct(forwrefs
);
1126 } while (forwref
!= NULL
1127 && forwref
->lineno
== globallineno
);
1129 output_ins
.forw_ref
= FALSE
;
1132 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1134 for (i
= 0; i
< output_ins
.operands
; i
++) {
1135 if (output_ins
.oprs
[i
].
1136 opflags
& OPFLAG_FORWARD
) {
1137 struct forwrefinfo
*fwinf
=
1138 (struct forwrefinfo
*)
1139 saa_wstruct(forwrefs
);
1140 fwinf
->lineno
= globallineno
;
1144 } else { /* pass == 2 */
1146 * Hack to prevent phase error in the code
1150 * If the second operand is a forward reference,
1151 * the UNITY property of the number 1 in that
1152 * operand is cancelled. Otherwise the above
1153 * sequence will cause a phase error.
1155 * This hack means that the above code will
1156 * generate 286+ code.
1158 * The forward reference will mean that the
1159 * operand will not have the UNITY property on
1160 * the first pass, so the pass behaviours will
1164 if (output_ins
.operands
>= 2 &&
1165 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1166 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1168 /* Remove special properties bits */
1169 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1177 if (output_ins
.opcode
== I_EQU
) {
1180 * Special `..' EQUs get processed in pass two,
1181 * except `..@' macro-processor EQUs which are done
1182 * in the normal place.
1184 if (!output_ins
.label
)
1185 report_error(ERR_NONFATAL
,
1186 "EQU not preceded by label");
1188 else if (output_ins
.label
[0] != '.' ||
1189 output_ins
.label
[1] != '.' ||
1190 output_ins
.label
[2] == '@') {
1191 if (output_ins
.operands
== 1 &&
1192 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1193 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1196 opflags
& OPFLAG_EXTERN
;
1197 def_label(output_ins
.label
,
1198 output_ins
.oprs
[0].segment
,
1199 output_ins
.oprs
[0].offset
, NULL
,
1202 } else if (output_ins
.operands
== 2
1203 && (output_ins
.oprs
[0].
1205 && (output_ins
.oprs
[0].type
& COLON
)
1206 && output_ins
.oprs
[0].segment
==
1208 && output_ins
.oprs
[0].wrt
== NO_SEG
1209 && (output_ins
.oprs
[1].
1211 && output_ins
.oprs
[1].segment
==
1213 && output_ins
.oprs
[1].wrt
==
1215 def_label(output_ins
.label
,
1218 output_ins
.oprs
[1].offset
, NULL
,
1222 report_error(ERR_NONFATAL
,
1223 "bad syntax for EQU");
1225 } else { /* pass == 2 */
1227 * Special `..' EQUs get processed here, except
1228 * `..@' macro processor EQUs which are done above.
1230 if (output_ins
.label
[0] == '.' &&
1231 output_ins
.label
[1] == '.' &&
1232 output_ins
.label
[2] != '@') {
1233 if (output_ins
.operands
== 1 &&
1234 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1235 define_label(output_ins
.label
,
1236 output_ins
.oprs
[0].segment
,
1237 output_ins
.oprs
[0].offset
,
1238 NULL
, FALSE
, FALSE
, ofmt
,
1240 } else if (output_ins
.operands
== 2
1241 && (output_ins
.oprs
[0].
1243 && (output_ins
.oprs
[0].type
& COLON
)
1244 && output_ins
.oprs
[0].segment
==
1246 && (output_ins
.oprs
[1].
1248 && output_ins
.oprs
[1].segment
==
1250 define_label(output_ins
.label
,
1253 output_ins
.oprs
[1].offset
,
1254 NULL
, FALSE
, FALSE
, ofmt
,
1257 report_error(ERR_NONFATAL
,
1258 "bad syntax for EQU");
1261 } else { /* instruction isn't an EQU */
1265 int32_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1266 &output_ins
, report_error
);
1268 /* if (using_debug_info) && output_ins.opcode != -1) */
1269 if (using_debug_info
)
1270 { /* fbk 03/25/01 */
1271 /* this is done here so we can do debug type info */
1273 TYS_ELEMENTS(output_ins
.operands
);
1274 switch (output_ins
.opcode
) {
1277 TYS_ELEMENTS(output_ins
.oprs
[0].
1282 TYS_ELEMENTS(output_ins
.oprs
[0].
1287 TYS_ELEMENTS(output_ins
.oprs
[0].
1292 TYS_ELEMENTS(output_ins
.oprs
[0].
1297 TYS_ELEMENTS(output_ins
.oprs
[0].
1301 typeinfo
|= TY_BYTE
;
1304 typeinfo
|= TY_WORD
;
1307 if (output_ins
.eops_float
)
1308 typeinfo
|= TY_FLOAT
;
1310 typeinfo
|= TY_DWORD
;
1313 typeinfo
|= TY_QWORD
;
1316 typeinfo
|= TY_TBYTE
;
1319 typeinfo
= TY_LABEL
;
1323 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1328 SET_CURR_OFFS(offs
);
1331 * else l == -1 => invalid instruction, which will be
1332 * flagged as an error on pass 2
1335 } else { /* pass == 2 */
1336 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1337 &output_ins
, ofmt
, report_error
,
1339 SET_CURR_OFFS(offs
);
1343 cleanup_insn(&output_ins
);
1346 location
.offset
= offs
= GET_CURR_OFFS
;
1347 } /* end while (line = preproc->getline... */
1349 if (pass1
== 2 && global_offset_changed
)
1350 report_error(ERR_NONFATAL
,
1351 "phase error detected at end of assembly.");
1354 preproc
->cleanup(1);
1356 if (pass1
== 1 && terminate_after_phase
) {
1364 if (pass
> 1 && !global_offset_changed
) {
1367 pass
= pass_max
- 1;
1368 } else if (!(optimizing
> 0))
1371 } /* for (pass=1; pass<=2; pass++) */
1373 preproc
->cleanup(0);
1376 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1378 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1380 } /* exit from assemble_file (...) */
1382 static int getkw(char **directive
, char **value
)
1388 /* allow leading spaces or tabs */
1389 while (*buf
== ' ' || *buf
== '\t')
1397 while (*p
&& *p
!= ']')
1405 while (*p
&& *p
!= ';') {
1412 *directive
= p
= buf
+ 1;
1413 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1420 while (isspace(*buf
))
1421 buf
++; /* beppu - skip leading whitespace */
1428 return bsii(*directive
, directives
, elements(directives
));
1432 * gnu style error reporting
1433 * This function prints an error message to error_file in the
1434 * style used by GNU. An example would be:
1435 * file.asm:50: error: blah blah blah
1436 * where file.asm is the name of the file, 50 is the line number on
1437 * which the error occurs (or is detected) and "error:" is one of
1438 * the possible optional diagnostics -- it can be "error" or "warning"
1439 * or something else. Finally the line terminates with the actual
1442 * @param severity the severity of the warning or error
1443 * @param fmt the printf style format string
1445 static void report_error_gnu(int severity
, const char *fmt
, ...)
1449 if (is_suppressed_warning(severity
))
1452 if (severity
& ERR_NOFILE
)
1453 fputs("nasm: ", error_file
);
1455 char *currentfile
= NULL
;
1457 src_get(&lineno
, ¤tfile
);
1458 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1459 nasm_free(currentfile
);
1462 report_error_common(severity
, fmt
, ap
);
1467 * MS style error reporting
1468 * This function prints an error message to error_file in the
1469 * style used by Visual C and some other Microsoft tools. An example
1471 * file.asm(50) : error: blah blah blah
1472 * where file.asm is the name of the file, 50 is the line number on
1473 * which the error occurs (or is detected) and "error:" is one of
1474 * the possible optional diagnostics -- it can be "error" or "warning"
1475 * or something else. Finally the line terminates with the actual
1478 * @param severity the severity of the warning or error
1479 * @param fmt the printf style format string
1481 static void report_error_vc(int severity
, const char *fmt
, ...)
1485 if (is_suppressed_warning(severity
))
1488 if (severity
& ERR_NOFILE
)
1489 fputs("nasm: ", error_file
);
1491 char *currentfile
= NULL
;
1493 src_get(&lineno
, ¤tfile
);
1494 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1495 nasm_free(currentfile
);
1498 report_error_common(severity
, fmt
, ap
);
1503 * check for supressed warning
1504 * checks for suppressed warning or pass one only warning and we're
1507 * @param severity the severity of the warning or error
1508 * @return true if we should abort error/warning printing
1510 static int is_suppressed_warning(int severity
)
1513 * See if it's a suppressed warning.
1515 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1516 (severity
& ERR_WARN_MASK
) != 0 &&
1517 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1519 * See if it's a pass-one only warning and we're not in pass one.
1521 ((severity
& ERR_PASS1
) && pass0
== 2);
1525 * common error reporting
1526 * This is the common back end of the error reporting schemes currently
1527 * implemented. It prints the nature of the warning and then the
1528 * specific error message to error_file and may or may not return. It
1529 * doesn't return if the error severity is a "panic" or "debug" type.
1531 * @param severity the severity of the warning or error
1532 * @param fmt the printf style format string
1534 static void report_error_common(int severity
, const char *fmt
,
1537 switch (severity
& ERR_MASK
) {
1539 fputs("warning: ", error_file
);
1542 fputs("error: ", error_file
);
1545 fputs("fatal: ", error_file
);
1548 fputs("panic: ", error_file
);
1551 fputs("debug: ", error_file
);
1555 vfprintf(error_file
, fmt
, args
);
1556 fputc('\n', error_file
);
1558 if (severity
& ERR_USAGE
)
1561 switch (severity
& ERR_MASK
) {
1564 /* no further action, by definition */
1567 /* hack enables listing(!) on errors */
1568 terminate_after_phase
= TRUE
;
1577 exit(1); /* instantly die */
1578 break; /* placate silly compilers */
1581 /* abort(); *//* halt, catch fire, and dump core */
1587 static void usage(void)
1589 fputs("type `nasm -h' for help\n", error_file
);
1592 static void register_output_formats(void)
1594 ofmt
= ofmt_register(report_error
);
1597 #define BUF_DELTA 512
1599 static FILE *no_pp_fp
;
1600 static efunc no_pp_err
;
1601 static ListGen
*no_pp_list
;
1602 static int32_t no_pp_lineinc
;
1604 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1607 src_set_fname(nasm_strdup(file
));
1611 no_pp_fp
= fopen(file
, "r");
1613 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1614 "unable to open input file `%s'", file
);
1615 no_pp_list
= listgen
;
1616 (void)pass
; /* placate compilers */
1617 (void)eval
; /* placate compilers */
1620 static char *no_pp_getline(void)
1622 char *buffer
, *p
, *q
;
1625 bufsize
= BUF_DELTA
;
1626 buffer
= nasm_malloc(BUF_DELTA
);
1627 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1629 while (1) { /* Loop to handle %line */
1632 while (1) { /* Loop to handle long lines */
1633 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1637 if (p
> buffer
&& p
[-1] == '\n')
1639 if (p
- buffer
> bufsize
- 10) {
1641 offset
= p
- buffer
;
1642 bufsize
+= BUF_DELTA
;
1643 buffer
= nasm_realloc(buffer
, bufsize
);
1644 p
= buffer
+ offset
;
1648 if (!q
&& p
== buffer
) {
1654 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1655 * them are present at the end of the line.
1657 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1659 if (!strncmp(buffer
, "%line", 5)) {
1662 char *nm
= nasm_malloc(strlen(buffer
));
1663 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1664 nasm_free(src_set_fname(nm
));
1674 no_pp_list
->line(LIST_READ
, buffer
);
1679 static void no_pp_cleanup(int pass
)
1681 (void)pass
; /* placate GCC */
1685 static uint32_t get_cpu(char *value
)
1688 if (!strcmp(value
, "8086"))
1690 if (!strcmp(value
, "186"))
1692 if (!strcmp(value
, "286"))
1694 if (!strcmp(value
, "386"))
1696 if (!strcmp(value
, "486"))
1698 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1700 if (!strcmp(value
, "686") ||
1701 !nasm_stricmp(value
, "ppro") ||
1702 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1704 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1706 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1707 !nasm_stricmp(value
, "willamette"))
1708 return IF_WILLAMETTE
;
1709 if (!nasm_stricmp(value
, "prescott"))
1711 if (!nasm_stricmp(value
, "x64") ||
1712 !nasm_stricmp(value
, "x86-64"))
1714 if (!nasm_stricmp(value
, "ia64") ||
1715 !nasm_stricmp(value
, "ia-64") ||
1716 !nasm_stricmp(value
, "itanium") ||
1717 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1720 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1721 "unknown 'cpu' type");
1723 return IF_PLEVEL
; /* the maximum level */
1726 static int get_bits(char *value
)
1730 if ((i
= atoi(value
)) == 16)
1731 return i
; /* set for a 16-bit segment */
1734 report_error(ERR_NONFATAL
,
1735 "cannot specify 32-bit segment on processor below a 386");
1738 } else if (i
== 64) {
1739 if (cpu
< IF_X86_64
) {
1740 report_error(ERR_NONFATAL
,
1741 "cannot specify 64-bit segment on processor below an x86-64");
1745 report_error(ERR_NONFATAL
,
1746 "%s output format does not support 64-bit code",
1751 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1752 "`%s' is not a valid segment size; must be 16, 32 or 64",