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.
26 struct forwrefinfo
{ /* info held on forward refs. */
31 static int get_bits(char *value
);
32 static unsigned long get_cpu(char *cpu_str
);
33 static void parse_cmdline(int, char **);
34 static void assemble_file(char *);
35 static int getkw(char **directive
, char **value
);
36 static void register_output_formats(void);
37 static void report_error_gnu(int severity
, const char *fmt
, ...);
38 static void report_error_vc(int severity
, const char *fmt
, ...);
39 static void report_error_common(int severity
, const char *fmt
,
41 static int is_suppressed_warning(int severity
);
42 static void usage(void);
43 static efunc report_error
;
45 static int using_debug_info
, opt_verbose_info
;
46 int tasm_compatible_mode
= FALSE
;
49 static char inname
[FILENAME_MAX
];
50 static char outname
[FILENAME_MAX
];
51 static char listname
[FILENAME_MAX
];
52 static int globallineno
; /* for forward-reference tracking */
53 /* static int pass = 0; */
54 static struct ofmt
*ofmt
= NULL
;
56 static FILE *error_file
; /* Where to write error messages */
58 static FILE *ofile
= NULL
;
59 int optimizing
= -1; /* number of optimization passes to take */
60 static int sb
, cmd_sb
= 16; /* by default */
61 static unsigned long cmd_cpu
= IF_PLEVEL
; /* highest level by default */
62 static unsigned long cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
63 int global_offset_changed
; /* referenced in labels.c */
65 static loc_t location
;
66 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
67 long abs_seg
; /* ABSOLUTE segment basis */
68 long abs_offset
; /* ABSOLUTE offset */
70 static struct RAA
*offsets
;
72 static struct SAA
*forwrefs
; /* keep track of forward references */
73 static struct forwrefinfo
*forwref
;
75 static Preproc
*preproc
;
77 op_normal
, /* Preprocess and assemble */
78 op_preprocess
, /* Preprocess only */
79 op_depend
/* Generate dependencies */
81 static enum op_type operating_mode
;
84 * Which of the suppressible warnings are suppressed. Entry zero
85 * doesn't do anything. Initial defaults are given here.
87 static char suppressed
[1 + ERR_WARN_MAX
] = {
88 0, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
92 * The option names for the suppressible warnings. As before, entry
95 static const char *suppressed_names
[1 + ERR_WARN_MAX
] = {
96 NULL
, "macro-params", "macro-selfref", "orphan-labels",
102 * The explanations for the suppressible warnings. As before, entry
105 static const char *suppressed_what
[1 + ERR_WARN_MAX
] = {
107 "macro calls with wrong no. of params",
108 "cyclic macro self-references",
109 "labels alone on lines without trailing `:'",
110 "numeric constants greater than 0xFFFFFFFF",
111 "using 8- or 16-bit relocation in ELF, a GNU extension"
115 * This is a null preprocessor which just copies lines from input
116 * to output. It's used when someone explicitly requests that NASM
117 * not preprocess their source file.
120 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*);
121 static char *no_pp_getline(void);
122 static void no_pp_cleanup(int);
123 static Preproc no_pp
= {
130 * get/set current offset...
132 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
133 raa_read(offsets,location.segment))
134 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
135 (void)(offsets=raa_write(offsets,location.segment,(x))))
137 static int want_usage
;
138 static int terminate_after_phase
;
139 int user_nolist
= 0; /* fbk 9/2/00 */
141 static void nasm_fputs(const char *line
, FILE * outfile
)
144 fputs(line
, outfile
);
145 fputc('\n', outfile
);
150 int main(int argc
, char **argv
)
153 want_usage
= terminate_after_phase
= FALSE
;
154 report_error
= report_error_gnu
;
156 nasm_set_malloc_error(report_error
);
157 offsets
= raa_init();
158 forwrefs
= saa_init((long)sizeof(struct forwrefinfo
));
161 operating_mode
= op_normal
;
167 register_output_formats();
169 parse_cmdline(argc
, argv
);
171 if (terminate_after_phase
) {
177 /* If debugging info is disabled, suppress any debug calls */
178 if (!using_debug_info
)
179 ofmt
->current_dfmt
= &null_debug_form
;
182 pp_extra_stdmac(ofmt
->stdmac
);
183 parser_global_info(ofmt
, &location
);
184 eval_global_info(ofmt
, lookup_label
, &location
);
186 /* define some macros dependent of command-line */
189 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
194 switch (operating_mode
) {
198 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
199 if (outname
[0] == '\0')
200 ofmt
->filename(inname
, outname
, report_error
);
202 fprintf(stdout
, "%s: %s", outname
, inname
);
203 while ((line
= preproc
->getline()))
213 char *file_name
= NULL
;
214 long prior_linnum
= 0;
218 ofile
= fopen(outname
, "w");
220 report_error(ERR_FATAL
| ERR_NOFILE
,
221 "unable to open output file `%s'",
226 location
.known
= FALSE
;
229 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
230 while ((line
= preproc
->getline())) {
232 * We generate %line directives if needed for later programs
234 long linnum
= prior_linnum
+= lineinc
;
235 int altline
= src_get(&linnum
, &file_name
);
237 if (altline
== 1 && lineinc
== 1)
238 nasm_fputs("", ofile
);
240 lineinc
= (altline
!= -1 || lineinc
!= 1);
241 fprintf(ofile
? ofile
: stdout
,
242 "%%line %ld+%d %s\n", linnum
, lineinc
,
245 prior_linnum
= linnum
;
247 nasm_fputs(line
, ofile
);
250 nasm_free(file_name
);
254 if (ofile
&& terminate_after_phase
)
262 * We must call ofmt->filename _anyway_, even if the user
263 * has specified their own output file, because some
264 * formats (eg OBJ and COFF) use ofmt->filename to find out
265 * the name of the input file and then put that inside the
268 ofmt
->filename(inname
, outname
, report_error
);
270 ofile
= fopen(outname
, "wb");
272 report_error(ERR_FATAL
| ERR_NOFILE
,
273 "unable to open output file `%s'", outname
);
277 * We must call init_labels() before ofmt->init() since
278 * some object formats will want to define labels in their
279 * init routines. (eg OS/2 defines the FLAT group)
283 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
285 assemble_file(inname
);
287 if (!terminate_after_phase
) {
288 ofmt
->cleanup(using_debug_info
);
292 * We had an fclose on the output file here, but we
293 * actually do that in all the object file drivers as well,
294 * so we're leaving out the one here.
313 if (terminate_after_phase
)
320 * Get a parameter for a command line option.
321 * First arg must be in the form of e.g. -f...
323 static char *get_param(char *p
, char *q
, int *advance
)
326 if (p
[2]) { /* the parameter's in the option */
336 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
337 "option `-%c' requires an argument", p
[1]);
347 #define OPT_POSTFIX 1
348 struct textargs textopts
[] = {
349 {"prefix", OPT_PREFIX
},
350 {"postfix", OPT_POSTFIX
},
355 static int process_arg(char *p
, char *q
)
363 if (p
[0] == '-' && !stopoptions
) {
368 case 'o': /* these parameters take values */
383 if (!(param
= get_param(p
, q
, &advance
)))
385 if (p
[1] == 'o') { /* output file */
386 strcpy(outname
, param
);
387 } else if (p
[1] == 'f') { /* output format */
388 ofmt
= ofmt_find(param
);
390 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
391 "unrecognised output format `%s' - "
392 "use -hf for a list", param
);
394 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
395 } else if (p
[1] == 'O') { /* Optimization level */
399 if (isdigit(*param
)) {
401 while (isdigit(*++param
)) ;
403 optimizing
= -1; /* 0.98 behaviour */
405 optimizing
= 0; /* Two passes, 0.98.09 behavior */
407 optimizing
= opt
; /* Multiple passes */
409 if (*param
== 'v' || *param
== '+') {
411 opt_verbose_info
= TRUE
;
413 } else { /* garbage */
418 } /* while (*param) */
420 report_error(ERR_FATAL
,
421 "command line optimization level must be 'v', 0..3 or <nn>");
422 } else if (p
[1] == 'P' || p
[1] == 'p') { /* pre-include */
423 pp_pre_include(param
);
424 } else if (p
[1] == 'D' || p
[1] == 'd') { /* pre-define */
425 pp_pre_define(param
);
426 } else if (p
[1] == 'U' || p
[1] == 'u') { /* un-define */
427 pp_pre_undefine(param
);
428 } else if (p
[1] == 'I' || p
[1] == 'i') { /* include search path */
429 pp_include_path(param
);
430 } else if (p
[1] == 'l') { /* listing file */
431 strcpy(listname
, param
);
432 } else if (p
[1] == 'E') { /* error messages file */
433 error_file
= fopen(param
, "w");
435 error_file
= stderr
; /* Revert to default! */
436 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
437 "cannot open file `%s' for error messages",
440 } else if (p
[1] == 'F') { /* specify debug format */
441 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
442 if (!ofmt
->current_dfmt
) {
443 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
444 "unrecognized debug format `%s' for"
445 " output format `%s'",
446 param
, ofmt
->shortname
);
448 } else if (p
[1] == 'X') { /* specify error reporting format */
449 if (nasm_stricmp("vc", param
) == 0)
450 report_error
= report_error_vc
;
451 else if (nasm_stricmp("gnu", param
) == 0)
452 report_error
= report_error_gnu
;
454 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
455 "unrecognized error reporting format `%s'",
460 using_debug_info
= TRUE
;
464 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
466 " [options...] [--] filename\n"
467 " or nasm -r for version info (obsolete)\n"
468 " or nasm -v for version info (preferred)\n\n"
469 " -t assemble in SciTech TASM compatible mode\n"
470 " -g generate debug information in selected format.\n");
472 (" -e preprocess only (writes output to stdout by default)\n"
473 " -a don't preprocess (assemble only)\n"
474 " -M generate Makefile dependencies on stdout\n\n"
475 " -E<file> redirect error messages to file\n"
476 " -s redirect error messages to stdout\n\n"
477 " -F format select a debugging format\n\n"
478 " -I<path> adds a pathname to the include file path\n");
480 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
481 " -P<file> pre-includes a file\n"
482 " -D<macro>[=<value>] pre-defines a macro\n"
483 " -U<macro> undefines a macro\n"
484 " -X<format> specifies error reporting format (gnu or vc)\n"
485 " -w+foo enables warnings about foo; -w-foo disables them\n"
486 "where foo can be:\n");
487 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
488 printf(" %-23s %s (default %s)\n",
489 suppressed_names
[i
], suppressed_what
[i
],
490 suppressed
[i
] ? "off" : "on");
492 ("\nresponse files should contain command line parameters"
493 ", one per line.\n");
495 printf("\nvalid output formats for -f are"
496 " (`*' denotes default):\n");
497 ofmt_list(ofmt
, stdout
);
499 printf("\nFor a list of valid output formats, use -hf.\n");
500 printf("For a list of debug formats, use -f <form> -y.\n");
502 exit(0); /* never need usage message here */
505 printf("\nvalid debug formats for '%s' output format are"
506 " ('*' denotes default):\n", ofmt
->shortname
);
507 dfmt_list(ofmt
, stdout
);
511 tasm_compatible_mode
= TRUE
;
516 const char *nasm_version_string
=
517 "NASM version " NASM_VER
" compiled on " __DATE__
522 puts(nasm_version_string
);
523 exit(0); /* never need usage message here */
526 case 'e': /* preprocess only */
527 operating_mode
= op_preprocess
;
529 case 'a': /* assemble only - don't preprocess */
533 if (p
[2] != '+' && p
[2] != '-') {
534 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
535 "invalid option to `-w'");
537 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
538 if (!nasm_stricmp(p
+ 3, suppressed_names
[i
]))
540 if (i
<= ERR_WARN_MAX
)
541 suppressed
[i
] = (p
[2] == '-');
543 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
544 "invalid option to `-w'");
548 operating_mode
= op_depend
;
555 if (p
[2] == 0) { /* -- => stop processing options */
559 for (s
= 0; textopts
[s
].label
; s
++) {
560 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
571 report_error(ERR_NONFATAL
| ERR_NOFILE
|
573 "option `--%s' requires an argument",
577 advance
= 1, param
= q
;
580 if (s
== OPT_PREFIX
) {
581 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
582 lprefix
[PREFIX_MAX
- 1] = 0;
585 if (s
== OPT_POSTFIX
) {
586 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
587 lpostfix
[POSTFIX_MAX
- 1] = 0;
594 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
595 "unrecognised option `--%s'", p
+ 2);
603 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
604 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
605 "unrecognised option `-%c'", p
[1]);
610 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
611 "more than one input file specified");
619 #define ARG_BUF_DELTA 128
621 static void process_respfile(FILE * rfile
)
623 char *buffer
, *p
, *q
, *prevarg
;
624 int bufsize
, prevargsize
;
626 bufsize
= prevargsize
= ARG_BUF_DELTA
;
627 buffer
= nasm_malloc(ARG_BUF_DELTA
);
628 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
631 while (1) { /* Loop to handle all lines in file */
634 while (1) { /* Loop to handle long lines */
635 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
639 if (p
> buffer
&& p
[-1] == '\n')
641 if (p
- buffer
> bufsize
- 10) {
644 bufsize
+= ARG_BUF_DELTA
;
645 buffer
= nasm_realloc(buffer
, bufsize
);
650 if (!q
&& p
== buffer
) {
652 process_arg(prevarg
, NULL
);
659 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
660 * them are present at the end of the line.
662 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
664 while (p
> buffer
&& isspace(p
[-1]))
671 if (process_arg(prevarg
, p
))
674 if (strlen(p
) > prevargsize
- 10) {
675 prevargsize
+= ARG_BUF_DELTA
;
676 prevarg
= nasm_realloc(prevarg
, prevargsize
);
682 /* Function to process args from a string of args, rather than the
683 * argv array. Used by the environment variable and response file
686 static void process_args(char *args
)
688 char *p
, *q
, *arg
, *prevarg
;
689 char separator
= ' ';
697 while (*p
&& *p
!= separator
)
699 while (*p
== separator
)
703 if (process_arg(prevarg
, arg
))
707 process_arg(arg
, NULL
);
710 static void parse_cmdline(int argc
, char **argv
)
713 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
715 *inname
= *outname
= *listname
= '\0';
718 * First, process the NASMENV environment variable.
720 envreal
= getenv("NASMENV");
723 envcopy
= nasm_strdup(envreal
);
724 process_args(envcopy
);
729 * Now process the actual command line.
734 if (argv
[0][0] == '@') {
735 /* We have a response file, so process this as a set of
736 * arguments like the environment variable. This allows us
737 * to have multiple arguments on a single line, which is
738 * different to the -@resp file processing below for regular
741 char *str
= malloc(2048);
742 FILE *f
= fopen(&argv
[0][1], "r");
744 printf("out of memory");
748 while (fgets(str
, 2048, f
)) {
757 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
758 if ((p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
))) {
759 if ((rfile
= fopen(p
, "r"))) {
760 process_respfile(rfile
);
763 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
764 "unable to open response file `%s'", p
);
767 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
768 argv
+= i
, argc
-= i
;
772 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
773 "no input file specified");
776 static void assemble_file(char *fname
)
778 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
780 int i
, rn_error
, validid
;
782 struct tokenval tokval
;
785 int pass_cnt
= 0; /* count actual passes */
787 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
788 report_error(ERR_FATAL
, "command line: "
789 "32-bit segment size requires a higher cpu");
791 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
792 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
793 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
797 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
798 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
799 /* pass0 seq is 0, 0, 0,..., 1, 2 */
801 def_label
= pass
> 1 ? redefine_label
: define_label
;
803 sb
= cmd_sb
; /* set 'bits' to command line default */
807 nasmlist
.init(listname
, report_error
);
810 global_offset_changed
= FALSE
; /* set by redefine_label */
811 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
813 saa_rewind(forwrefs
);
814 forwref
= saa_rstruct(forwrefs
);
816 offsets
= raa_init();
818 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
821 location
.known
= TRUE
;
822 location
.offset
= offs
= GET_CURR_OFFS
;
824 while ((line
= preproc
->getline())) {
827 /* here we parse our directives; this is not handled by the 'real'
830 if ((i
= getkw(&directive
, &value
))) {
832 case 1: /* [SEGMENT n] */
833 seg
= ofmt
->section(value
, pass2
, &sb
);
835 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
836 "segment name `%s' not recognised",
840 location
.segment
= seg
;
843 case 2: /* [EXTERN label:special] */
845 value
++; /* skip initial $ if present */
848 while (*q
&& *q
!= ':')
852 ofmt
->symdef(value
, 0L, 0L, 3, q
);
854 } else if (pass
== 1) { /* pass == 1 */
859 while (*q
&& *q
!= ':') {
865 report_error(ERR_NONFATAL
,
866 "identifier expected after EXTERN");
874 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
876 pass0
= 1; /* fake pass 1 in labels.c */
877 declare_as_global(value
, special
,
879 define_label(value
, seg_alloc(), 0L, NULL
,
880 FALSE
, TRUE
, ofmt
, report_error
);
883 } /* else pass0 == 1 */
885 case 3: /* [BITS bits] */
886 sb
= get_bits(value
);
888 case 4: /* [GLOBAL symbol:special] */
890 value
++; /* skip initial $ if present */
891 if (pass0
== 2) { /* pass 2 */
893 while (*q
&& *q
!= ':')
897 ofmt
->symdef(value
, 0L, 0L, 3, q
);
899 } else if (pass2
== 1) { /* pass == 1 */
904 while (*q
&& *q
!= ':') {
910 report_error(ERR_NONFATAL
,
911 "identifier expected after GLOBAL");
919 declare_as_global(value
, special
, report_error
);
922 case 5: /* [COMMON symbol size:special] */
924 value
++; /* skip initial $ if present */
930 while (*p
&& !isspace(*p
)) {
936 report_error(ERR_NONFATAL
,
937 "identifier expected after COMMON");
943 while (*p
&& isspace(*p
))
946 while (*q
&& *q
!= ':')
953 size
= readnum(p
, &rn_error
);
955 report_error(ERR_NONFATAL
,
956 "invalid size specified"
957 " in COMMON declaration");
959 define_common(value
, seg_alloc(), size
,
960 special
, ofmt
, report_error
);
962 report_error(ERR_NONFATAL
,
963 "no size specified in"
964 " COMMON declaration");
965 } else if (pass0
== 2) { /* pass == 2 */
967 while (*q
&& *q
!= ':') {
974 ofmt
->symdef(value
, 0L, 0L, 3, q
);
978 case 6: /* [ABSOLUTE address] */
980 stdscan_bufptr
= value
;
981 tokval
.t_type
= TOKEN_INVALID
;
982 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
986 report_error(pass0
==
987 1 ? ERR_NONFATAL
: ERR_PANIC
,
988 "cannot use non-relocatable expression as "
991 abs_seg
= reloc_seg(e
);
992 abs_offset
= reloc_value(e
);
994 } else if (pass
== 1)
995 abs_offset
= 0x100; /* don't go near zero in case of / */
997 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1000 location
.segment
= NO_SEG
;
1008 while (*p
&& !isspace(*p
)) {
1015 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1016 "identifier expected after DEBUG");
1019 while (*p
&& isspace(*p
))
1021 if (pass
== pass_max
)
1022 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1024 case 8: /* [WARNING {+|-}warn-name] */
1026 while (*value
&& isspace(*value
))
1029 if (*value
== '+' || *value
== '-') {
1030 validid
= (*value
== '-') ? TRUE
: FALSE
;
1035 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1036 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1038 if (i
<= ERR_WARN_MAX
)
1039 suppressed
[i
] = validid
;
1041 report_error(ERR_NONFATAL
,
1042 "invalid warning id in WARNING directive");
1046 cpu
= get_cpu(value
);
1048 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1049 while (*value
&& isspace(*value
))
1052 if (*value
== '+') {
1055 if (*value
== '-') {
1058 report_error(ERR_NONFATAL
,
1059 "invalid parameter to \"list\" directive");
1064 if (!ofmt
->directive(directive
, value
, pass2
))
1065 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1066 "unrecognised directive [%s]",
1069 } else { /* it isn't a directive */
1071 parse_line(pass1
, line
, &output_ins
,
1072 report_error
, evaluate
, def_label
);
1074 if (!(optimizing
> 0) && pass
== 2) {
1075 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1076 output_ins
.forw_ref
= TRUE
;
1078 output_ins
.oprs
[forwref
->operand
].opflags
|=
1080 forwref
= saa_rstruct(forwrefs
);
1081 } while (forwref
!= NULL
1082 && forwref
->lineno
== globallineno
);
1084 output_ins
.forw_ref
= FALSE
;
1087 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1089 for (i
= 0; i
< output_ins
.operands
; i
++) {
1090 if (output_ins
.oprs
[i
].
1091 opflags
& OPFLAG_FORWARD
) {
1092 struct forwrefinfo
*fwinf
=
1093 (struct forwrefinfo
*)
1094 saa_wstruct(forwrefs
);
1095 fwinf
->lineno
= globallineno
;
1099 } else { /* pass == 2 */
1101 * Hack to prevent phase error in the code
1105 * If the second operand is a forward reference,
1106 * the UNITY property of the number 1 in that
1107 * operand is cancelled. Otherwise the above
1108 * sequence will cause a phase error.
1110 * This hack means that the above code will
1111 * generate 286+ code.
1113 * The forward reference will mean that the
1114 * operand will not have the UNITY property on
1115 * the first pass, so the pass behaviours will
1119 if (output_ins
.operands
>= 2 &&
1120 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1122 output_ins
.oprs
[1].type
&=
1123 ~(ONENESS
| BYTENESS
);
1131 if (output_ins
.opcode
== I_EQU
) {
1134 * Special `..' EQUs get processed in pass two,
1135 * except `..@' macro-processor EQUs which are done
1136 * in the normal place.
1138 if (!output_ins
.label
)
1139 report_error(ERR_NONFATAL
,
1140 "EQU not preceded by label");
1142 else if (output_ins
.label
[0] != '.' ||
1143 output_ins
.label
[1] != '.' ||
1144 output_ins
.label
[2] == '@') {
1145 if (output_ins
.operands
== 1 &&
1146 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1147 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1150 opflags
& OPFLAG_EXTERN
;
1151 def_label(output_ins
.label
,
1152 output_ins
.oprs
[0].segment
,
1153 output_ins
.oprs
[0].offset
, NULL
,
1156 } else if (output_ins
.operands
== 2
1157 && (output_ins
.oprs
[0].
1159 && (output_ins
.oprs
[0].type
& COLON
)
1160 && output_ins
.oprs
[0].segment
==
1162 && output_ins
.oprs
[0].wrt
== NO_SEG
1163 && (output_ins
.oprs
[1].
1165 && output_ins
.oprs
[1].segment
==
1167 && output_ins
.oprs
[1].wrt
==
1169 def_label(output_ins
.label
,
1172 output_ins
.oprs
[1].offset
, NULL
,
1176 report_error(ERR_NONFATAL
,
1177 "bad syntax for EQU");
1179 } else { /* pass == 2 */
1181 * Special `..' EQUs get processed here, except
1182 * `..@' macro processor EQUs which are done above.
1184 if (output_ins
.label
[0] == '.' &&
1185 output_ins
.label
[1] == '.' &&
1186 output_ins
.label
[2] != '@') {
1187 if (output_ins
.operands
== 1 &&
1188 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1189 define_label(output_ins
.label
,
1190 output_ins
.oprs
[0].segment
,
1191 output_ins
.oprs
[0].offset
,
1192 NULL
, FALSE
, FALSE
, ofmt
,
1194 } else if (output_ins
.operands
== 2
1195 && (output_ins
.oprs
[0].
1197 && (output_ins
.oprs
[0].type
& COLON
)
1198 && output_ins
.oprs
[0].segment
==
1200 && (output_ins
.oprs
[1].
1202 && output_ins
.oprs
[1].segment
==
1204 define_label(output_ins
.label
,
1207 output_ins
.oprs
[1].offset
,
1208 NULL
, FALSE
, FALSE
, ofmt
,
1211 report_error(ERR_NONFATAL
,
1212 "bad syntax for EQU");
1215 } else { /* instruction isn't an EQU */
1219 long l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1220 &output_ins
, report_error
);
1222 /* if (using_debug_info) && output_ins.opcode != -1) */
1223 if (using_debug_info
)
1224 { /* fbk 03/25/01 */
1225 /* this is done here so we can do debug type info */
1227 TYS_ELEMENTS(output_ins
.operands
);
1228 switch (output_ins
.opcode
) {
1231 TYS_ELEMENTS(output_ins
.oprs
[0].
1236 TYS_ELEMENTS(output_ins
.oprs
[0].
1241 TYS_ELEMENTS(output_ins
.oprs
[0].
1246 TYS_ELEMENTS(output_ins
.oprs
[0].
1251 TYS_ELEMENTS(output_ins
.oprs
[0].
1255 typeinfo
|= TY_BYTE
;
1258 typeinfo
|= TY_WORD
;
1261 if (output_ins
.eops_float
)
1262 typeinfo
|= TY_FLOAT
;
1264 typeinfo
|= TY_DWORD
;
1267 typeinfo
|= TY_QWORD
;
1270 typeinfo
|= TY_TBYTE
;
1273 typeinfo
= TY_LABEL
;
1277 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1282 SET_CURR_OFFS(offs
);
1285 * else l == -1 => invalid instruction, which will be
1286 * flagged as an error on pass 2
1289 } else { /* pass == 2 */
1290 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1291 &output_ins
, ofmt
, report_error
,
1293 SET_CURR_OFFS(offs
);
1297 cleanup_insn(&output_ins
);
1300 location
.offset
= offs
= GET_CURR_OFFS
;
1301 } /* end while (line = preproc->getline... */
1303 if (pass1
== 2 && global_offset_changed
)
1304 report_error(ERR_NONFATAL
,
1305 "phase error detected at end of assembly.");
1308 preproc
->cleanup(1);
1310 if (pass1
== 1 && terminate_after_phase
) {
1318 if (pass
> 1 && !global_offset_changed
) {
1321 pass
= pass_max
- 1;
1322 } else if (!(optimizing
> 0))
1325 } /* for (pass=1; pass<=2; pass++) */
1327 preproc
->cleanup(0);
1330 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1332 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1334 } /* exit from assemble_file (...) */
1336 static int getkw(char **directive
, char **value
)
1342 /* allow leading spaces or tabs */
1343 while (*buf
== ' ' || *buf
== '\t')
1351 while (*p
&& *p
!= ']')
1359 while (*p
&& *p
!= ';') {
1366 *directive
= p
= buf
+ 1;
1367 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1374 while (isspace(*buf
))
1375 buf
++; /* beppu - skip leading whitespace */
1382 for (q
= p
; *q
; q
++)
1385 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1387 if (!nasm_stricmp(p
, "extern"))
1389 if (!nasm_stricmp(p
, "bits"))
1391 if (!nasm_stricmp(p
, "global"))
1393 if (!nasm_stricmp(p
, "common"))
1395 if (!nasm_stricmp(p
, "absolute"))
1397 if (!nasm_stricmp(p
, "debug"))
1399 if (!nasm_stricmp(p
, "warning"))
1401 if (!nasm_stricmp(p
, "cpu"))
1403 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1409 * gnu style error reporting
1410 * This function prints an error message to error_file in the
1411 * style used by GNU. An example would be:
1412 * file.asm:50: error: blah blah blah
1413 * where file.asm is the name of the file, 50 is the line number on
1414 * which the error occurs (or is detected) and "error:" is one of
1415 * the possible optional diagnostics -- it can be "error" or "warning"
1416 * or something else. Finally the line terminates with the actual
1419 * @param severity the severity of the warning or error
1420 * @param fmt the printf style format string
1422 static void report_error_gnu(int severity
, const char *fmt
, ...)
1426 if (is_suppressed_warning(severity
))
1429 if (severity
& ERR_NOFILE
)
1430 fputs("nasm: ", error_file
);
1432 char *currentfile
= NULL
;
1434 src_get(&lineno
, ¤tfile
);
1435 fprintf(error_file
, "%s:%ld: ", currentfile
, lineno
);
1436 nasm_free(currentfile
);
1439 report_error_common(severity
, fmt
, ap
);
1444 * MS style error reporting
1445 * This function prints an error message to error_file in the
1446 * style used by Visual C and some other Microsoft tools. An example
1448 * file.asm(50) : error: blah blah blah
1449 * where file.asm is the name of the file, 50 is the line number on
1450 * which the error occurs (or is detected) and "error:" is one of
1451 * the possible optional diagnostics -- it can be "error" or "warning"
1452 * or something else. Finally the line terminates with the actual
1455 * @param severity the severity of the warning or error
1456 * @param fmt the printf style format string
1458 static void report_error_vc(int severity
, const char *fmt
, ...)
1462 if (is_suppressed_warning(severity
))
1465 if (severity
& ERR_NOFILE
)
1466 fputs("nasm: ", error_file
);
1468 char *currentfile
= NULL
;
1470 src_get(&lineno
, ¤tfile
);
1471 fprintf(error_file
, "%s(%ld) : ", currentfile
, lineno
);
1472 nasm_free(currentfile
);
1475 report_error_common(severity
, fmt
, ap
);
1480 * check for supressed warning
1481 * checks for suppressed warning or pass one only warning and we're
1484 * @param severity the severity of the warning or error
1485 * @return true if we should abort error/warning printing
1487 static int is_suppressed_warning(int severity
)
1490 * See if it's a suppressed warning.
1492 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1493 (severity
& ERR_WARN_MASK
) != 0 &&
1494 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1496 * See if it's a pass-one only warning and we're not in pass one.
1498 ((severity
& ERR_PASS1
) && pass0
== 2);
1502 * common error reporting
1503 * This is the common back end of the error reporting schemes currently
1504 * implemented. It prints the nature of the warning and then the
1505 * specific error message to error_file and may or may not return. It
1506 * doesn't return if the error severity is a "panic" or "debug" type.
1508 * @param severity the severity of the warning or error
1509 * @param fmt the printf style format string
1511 static void report_error_common(int severity
, const char *fmt
,
1514 switch (severity
& ERR_MASK
) {
1516 fputs("warning: ", error_file
);
1519 fputs("error: ", error_file
);
1522 fputs("fatal: ", error_file
);
1525 fputs("panic: ", error_file
);
1528 fputs("debug: ", error_file
);
1532 vfprintf(error_file
, fmt
, args
);
1533 fputc('\n', error_file
);
1535 if (severity
& ERR_USAGE
)
1538 switch (severity
& ERR_MASK
) {
1541 /* no further action, by definition */
1544 /* hack enables listing(!) on errors */
1545 terminate_after_phase
= TRUE
;
1554 exit(1); /* instantly die */
1555 break; /* placate silly compilers */
1558 /* abort(); *//* halt, catch fire, and dump core */
1564 static void usage(void)
1566 fputs("type `nasm -h' for help\n", error_file
);
1569 static void register_output_formats(void)
1571 ofmt
= ofmt_register(report_error
);
1574 #define BUF_DELTA 512
1576 static FILE *no_pp_fp
;
1577 static efunc no_pp_err
;
1578 static ListGen
*no_pp_list
;
1579 static long no_pp_lineinc
;
1581 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1584 src_set_fname(nasm_strdup(file
));
1588 no_pp_fp
= fopen(file
, "r");
1590 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1591 "unable to open input file `%s'", file
);
1592 no_pp_list
= listgen
;
1593 (void)pass
; /* placate compilers */
1594 (void)eval
; /* placate compilers */
1597 static char *no_pp_getline(void)
1599 char *buffer
, *p
, *q
;
1602 bufsize
= BUF_DELTA
;
1603 buffer
= nasm_malloc(BUF_DELTA
);
1604 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1606 while (1) { /* Loop to handle %line */
1609 while (1) { /* Loop to handle long lines */
1610 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1614 if (p
> buffer
&& p
[-1] == '\n')
1616 if (p
- buffer
> bufsize
- 10) {
1618 offset
= p
- buffer
;
1619 bufsize
+= BUF_DELTA
;
1620 buffer
= nasm_realloc(buffer
, bufsize
);
1621 p
= buffer
+ offset
;
1625 if (!q
&& p
== buffer
) {
1631 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1632 * them are present at the end of the line.
1634 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1636 if (!strncmp(buffer
, "%line", 5)) {
1639 char *nm
= nasm_malloc(strlen(buffer
));
1640 if (sscanf(buffer
+ 5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1641 nasm_free(src_set_fname(nm
));
1651 no_pp_list
->line(LIST_READ
, buffer
);
1656 static void no_pp_cleanup(int pass
)
1661 static unsigned long get_cpu(char *value
)
1664 if (!strcmp(value
, "8086"))
1666 if (!strcmp(value
, "186"))
1668 if (!strcmp(value
, "286"))
1670 if (!strcmp(value
, "386"))
1672 if (!strcmp(value
, "486"))
1674 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1676 if (!strcmp(value
, "686") ||
1677 !nasm_stricmp(value
, "ppro") ||
1678 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1680 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1682 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1683 !nasm_stricmp(value
, "willamette"))
1684 return IF_WILLAMETTE
;
1685 if (!nasm_stricmp(value
, "prescott"))
1687 if (!nasm_stricmp(value
, "ia64") ||
1688 !nasm_stricmp(value
, "ia-64") ||
1689 !nasm_stricmp(value
, "itanium") ||
1690 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1693 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1694 "unknown 'cpu' type");
1696 return IF_PLEVEL
; /* the maximum level */
1699 static int get_bits(char *value
)
1703 if ((i
= atoi(value
)) == 16)
1704 return i
; /* set for a 16-bit segment */
1707 report_error(ERR_NONFATAL
,
1708 "cannot specify 32-bit segment on processor below a 386");
1712 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1713 "`%s' is not a valid segment size; must be 16 or 32",