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 *buf
, 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
, va_list args
);
40 static int is_suppressed_warning (int severity
);
41 static void usage(void);
42 static efunc report_error
;
44 static int using_debug_info
, opt_verbose_info
;
45 int tasm_compatible_mode
= FALSE
;
48 static char inname
[FILENAME_MAX
];
49 static char outname
[FILENAME_MAX
];
50 static char listname
[FILENAME_MAX
];
51 static int globallineno
; /* for forward-reference tracking */
52 /* static int pass = 0; */
53 static struct ofmt
*ofmt
= NULL
;
55 static FILE *error_file
; /* Where to write error messages */
57 static FILE *ofile
= NULL
;
58 int optimizing
= -1; /* number of optimization passes to take */
59 static int sb
, cmd_sb
= 16; /* by default */
60 static unsigned long cmd_cpu
= IF_PLEVEL
; /* highest level by default */
61 static unsigned long cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
62 int global_offset_changed
; /* referenced in labels.c */
64 static loc_t location
;
65 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
66 long abs_seg
; /* ABSOLUTE segment basis */
67 long abs_offset
; /* ABSOLUTE offset */
69 static struct RAA
*offsets
;
71 static struct SAA
*forwrefs
; /* keep track of forward references */
72 static struct forwrefinfo
*forwref
;
74 static Preproc
*preproc
;
76 op_normal
, /* Preprocess and assemble */
77 op_preprocess
, /* Preprocess only */
78 op_depend
/* Generate dependencies */
80 static enum op_type operating_mode
;
83 * Which of the suppressible warnings are suppressed. Entry zero
84 * doesn't do anything. Initial defaults are given here.
86 static char suppressed
[1+ERR_WARN_MAX
] = {
87 0, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
91 * The option names for the suppressible warnings. As before, entry
94 static const char *suppressed_names
[1+ERR_WARN_MAX
] = {
95 NULL
, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
100 * The explanations for the suppressible warnings. As before, entry
103 static const char *suppressed_what
[1+ERR_WARN_MAX
] = {
105 "macro calls with wrong no. of params",
106 "cyclic macro self-references",
107 "labels alone on lines without trailing `:'",
108 "numeric constants greater than 0xFFFFFFFF",
109 "using 8- or 16-bit relocation in ELF, a GNU extension"
113 * This is a null preprocessor which just copies lines from input
114 * to output. It's used when someone explicitly requests that NASM
115 * not preprocess their source file.
118 static void no_pp_reset (char *, int, efunc
, evalfunc
, ListGen
*);
119 static char *no_pp_getline (void);
120 static void no_pp_cleanup (int);
121 static Preproc no_pp
= {
128 * get/set current offset...
130 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
131 raa_read(offsets,location.segment))
132 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
133 (void)(offsets=raa_write(offsets,location.segment,(x))))
135 static int want_usage
;
136 static int terminate_after_phase
;
137 int user_nolist
= 0; /* fbk 9/2/00 */
139 static void nasm_fputs(const char *line
, FILE *outfile
)
142 fputs(line
, outfile
);
143 fputc('\n', outfile
);
148 int main(int argc
, char **argv
)
151 want_usage
= terminate_after_phase
= FALSE
;
152 report_error
= report_error_gnu
;
154 nasm_set_malloc_error (report_error
);
155 offsets
= raa_init();
156 forwrefs
= saa_init ((long)sizeof(struct forwrefinfo
));
159 operating_mode
= op_normal
;
165 register_output_formats();
167 parse_cmdline(argc
, argv
);
169 if (terminate_after_phase
)
177 pp_extra_stdmac (ofmt
->stdmac
);
178 parser_global_info (ofmt
, &location
);
179 eval_global_info (ofmt
, lookup_label
, &location
);
181 /* define some macros dependent of command-line */
184 sprintf (temp
, "__OUTPUT_FORMAT__=%s\n", ofmt
->shortname
);
185 pp_pre_define (temp
);
188 switch ( operating_mode
) {
192 preproc
->reset (inname
, 0, report_error
, evaluate
, &nasmlist
);
193 if (outname
[0] == '\0')
194 ofmt
->filename (inname
, outname
, report_error
);
196 fprintf(stdout
, "%s: %s", outname
, inname
);
197 while ( (line
= preproc
->getline()) )
207 char *file_name
= NULL
;
212 ofile
= fopen(outname
, "w");
214 report_error (ERR_FATAL
| ERR_NOFILE
,
215 "unable to open output file `%s'", outname
);
219 location
.known
= FALSE
;
222 preproc
->reset (inname
, 2, report_error
, evaluate
, &nasmlist
);
223 while ( (line
= preproc
->getline()) ) {
225 * We generate %line directives if needed for later programs
227 long linnum
= prior_linnum
+= lineinc
;
228 int altline
= src_get(&linnum
, &file_name
);
230 if (altline
==1 && lineinc
==1)
231 nasm_fputs("", ofile
);
233 lineinc
= (altline
!= -1 || lineinc
!=1);
234 fprintf(ofile
? ofile
: stdout
, "%%line %ld+%d %s\n",
235 linnum
, lineinc
, file_name
);
237 prior_linnum
= linnum
;
239 nasm_fputs(line
, ofile
);
242 nasm_free(file_name
);
246 if (ofile
&& terminate_after_phase
)
254 * We must call ofmt->filename _anyway_, even if the user
255 * has specified their own output file, because some
256 * formats (eg OBJ and COFF) use ofmt->filename to find out
257 * the name of the input file and then put that inside the
260 ofmt
->filename (inname
, outname
, report_error
);
262 ofile
= fopen(outname
, "wb");
264 report_error (ERR_FATAL
| ERR_NOFILE
,
265 "unable to open output file `%s'", outname
);
269 * We must call init_labels() before ofmt->init() since
270 * some object formats will want to define labels in their
271 * init routines. (eg OS/2 defines the FLAT group)
275 ofmt
->init (ofile
, report_error
, define_label
, evaluate
);
277 assemble_file (inname
);
279 if (!terminate_after_phase
) {
280 ofmt
->cleanup (using_debug_info
);
284 * We had an fclose on the output file here, but we
285 * actually do that in all the object file drivers as well,
286 * so we're leaving out the one here.
305 if (terminate_after_phase
)
313 * Get a parameter for a command line option.
314 * First arg must be in the form of e.g. -f...
316 static char *get_param (char *p
, char *q
, int *advance
)
319 if (p
[2]) /* the parameter's in the option */
331 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
332 "option `-%c' requires an argument",
344 #define OPT_POSTFIX 1
345 struct textargs textopts
[] =
347 {"prefix",OPT_PREFIX
},
348 {"postfix",OPT_POSTFIX
},
354 static int process_arg (char *p
, char *q
)
362 if (p
[0]=='-' && ! stopoptions
)
368 case 'o': /* these parameters take values */
381 if ( !(param
= get_param (p
, q
, &advance
)) )
383 if (p
[1]=='o') { /* output file */
384 strcpy (outname
, param
);
385 } else if (p
[1]=='f') { /* output format */
386 ofmt
= ofmt_find(param
);
388 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
389 "unrecognised output format `%s' - "
390 "use -hf for a list",
394 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
395 } else if (p
[1]=='O') { /* Optimization level */
399 if (isdigit(*param
)) {
401 while(isdigit(*++param
)) ;
402 if (opt
<=0) optimizing
= -1; /* 0.98 behaviour */
403 else if (opt
==1) optimizing
= 0; /* Two passes, 0.98.09 behavior */
404 else if (opt
<=3) optimizing
= opt
*5; /* Multiple passes */
405 else optimizing
= opt
; /* Multiple passes */
407 if (*param
== 'v' || *param
== '+') {
409 opt_verbose_info
= TRUE
;
413 } /* while (*param) */
414 if (opt
== -99) report_error(ERR_FATAL
,
415 "command line optimization level must be 'v', 0..3 or <nn>");
416 } else if (p
[1]=='P' || p
[1]=='p') { /* pre-include */
417 pp_pre_include (param
);
418 } else if (p
[1]=='D' || p
[1]=='d') { /* pre-define */
419 pp_pre_define (param
);
420 } else if (p
[1]=='U' || p
[1]=='u') { /* un-define */
421 pp_pre_undefine (param
);
422 } else if (p
[1]=='I' || p
[1]=='i') { /* include search path */
423 pp_include_path (param
);
424 } else if (p
[1]=='l') { /* listing file */
425 strcpy (listname
, param
);
426 } else if (p
[1]=='E') { /* error messages file */
427 error_file
= fopen(param
, "w");
429 error_file
= stderr
; /* Revert to default! */
430 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
431 "cannot open file `%s' for error messages",
434 } else if (p
[1] == 'F') { /* specify debug format */
435 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
436 if (!ofmt
->current_dfmt
) {
437 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
438 "unrecognized debug format `%s' for"
439 " output format `%s'",
440 param
, ofmt
->shortname
);
442 } else if (p
[1] == 'X') { /* specify error reporting format */
443 if (nasm_stricmp("vc", param
) == 0)
444 report_error
= report_error_vc
;
445 else if (nasm_stricmp("gnu", param
) == 0)
446 report_error
= report_error_gnu
;
448 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
449 "unrecognized error reporting format `%s'",
454 using_debug_info
= TRUE
;
457 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
459 " [options...] [--] filename\n"
460 " or nasm -r for version info (obsolete)\n"
461 " or nasm -v for version info (preferred)\n\n"
462 " -t Assemble in SciTech TASM compatible mode\n"
463 " -g Generate debug information in selected format.\n");
464 printf(" -e preprocess only (writes output to stdout by default)\n"
465 " -a don't preprocess (assemble only)\n"
466 " -M generate Makefile dependencies on stdout\n\n"
467 " -E<file> redirect error messages to file\n"
468 " -s redirect error messages to stdout\n\n"
469 " -F format select a debugging format\n\n"
470 " -I<path> adds a pathname to the include file path\n");
471 printf(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
472 " -P<file> pre-includes a file\n"
473 " -D<macro>[=<value>] pre-defines a macro\n"
474 " -U<macro> undefines a macro\n"
475 " -X<format> specifies error reporting format (gnu or vc)\n"
476 " -w+foo enables warnings about foo; -w-foo disables them\n"
477 "where foo can be:\n");
478 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
479 printf(" %-23s %s (default %s)\n",
480 suppressed_names
[i
], suppressed_what
[i
],
481 suppressed
[i
] ? "off" : "on");
482 printf ("\nresponse files should contain command line parameters"
483 ", one per line.\n");
485 printf("\nvalid output formats for -f are"
486 " (`*' denotes default):\n");
487 ofmt_list(ofmt
, stdout
);
490 printf ("\nFor a list of valid output formats, use -hf.\n");
491 printf ("For a list of debug formats, use -f <form> -y.\n");
493 exit (0); /* never need usage message here */
496 printf("\nvalid debug formats for '%s' output format are"
497 " ('*' denotes default):\n",
499 dfmt_list(ofmt
, stdout
);
503 tasm_compatible_mode
= TRUE
;
508 const char *nasm_version_string
=
509 "NASM version " NASM_VER
" compiled on " __DATE__
514 puts(nasm_version_string
);
515 exit (0); /* never need usage message here */
518 case 'e': /* preprocess only */
519 operating_mode
= op_preprocess
;
521 case 'a': /* assemble only - don't preprocess */
525 if (p
[2] != '+' && p
[2] != '-') {
526 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
527 "invalid option to `-w'");
529 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
530 if (!nasm_stricmp(p
+3, suppressed_names
[i
]))
532 if (i
<= ERR_WARN_MAX
)
533 suppressed
[i
] = (p
[2] == '-');
535 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
536 "invalid option to `-w'");
540 operating_mode
= op_depend
;
547 if (p
[2]==0) { /* -- => stop processing options */
551 for(s
=0; textopts
[s
].label
; s
++)
553 if(!nasm_stricmp(p
+2, textopts
[s
].label
))
567 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
568 "option `--%s' requires an argument",
574 advance
= 1, param
= q
;
579 strncpy(lprefix
,param
,PREFIX_MAX
-1);
580 lprefix
[PREFIX_MAX
-1]=0;
585 strncpy(lpostfix
,param
,POSTFIX_MAX
-1);
586 lpostfix
[POSTFIX_MAX
-1]=0;
593 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
594 "unrecognised option `--%s'",
603 if (!ofmt
->setinfo(GI_SWITCH
,&p
))
604 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
605 "unrecognised option `-%c'",
613 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
614 "more than one input file specified");
622 #define ARG_BUF_DELTA 128
624 static void process_respfile (FILE *rfile
)
626 char *buffer
, *p
, *q
, *prevarg
;
627 int bufsize
, prevargsize
;
629 bufsize
= prevargsize
= ARG_BUF_DELTA
;
630 buffer
= nasm_malloc(ARG_BUF_DELTA
);
631 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
634 while (1) { /* Loop to handle all lines in file */
637 while (1) { /* Loop to handle long lines */
638 q
= fgets(p
, bufsize
-(p
-buffer
), rfile
);
642 if (p
> buffer
&& p
[-1] == '\n')
644 if (p
-buffer
> bufsize
-10) {
647 bufsize
+= ARG_BUF_DELTA
;
648 buffer
= nasm_realloc(buffer
, bufsize
);
653 if (!q
&& p
== buffer
) {
655 process_arg (prevarg
, NULL
);
662 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
663 * them are present at the end of the line.
665 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
667 while (p
> buffer
&& isspace(p
[-1]))
674 if (process_arg (prevarg
, p
))
677 if (strlen(p
) > prevargsize
-10) {
678 prevargsize
+= ARG_BUF_DELTA
;
679 prevarg
= nasm_realloc(prevarg
, prevargsize
);
685 /* Function to process args from a string of args, rather than the
686 * argv array. Used by the environment variable and response file
689 static void process_args (char *args
) {
690 char *p
, *q
, *arg
, *prevarg
;
691 char separator
= ' ';
699 while (*p
&& *p
!= separator
) p
++;
700 while (*p
== separator
) *p
++ = '\0';
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.
735 if (argv
[0][0] == '@') {
736 /* We have a response file, so process this as a set of
737 * arguments like the environment variable. This allows us
738 * to have multiple arguments on a single line, which is
739 * different to the -@resp file processing below for regular
742 char *str
= malloc(2048);
743 FILE *f
= fopen(&argv
[0][1],"r");
745 printf("out of memory");
749 while (fgets(str
,2048,f
)) {
758 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
759 if ((p
= get_param (argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
))) {
760 if ((rfile
= fopen(p
, "r"))) {
761 process_respfile (rfile
);
764 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
765 "unable to open response file `%s'", p
);
768 i
= process_arg (argv
[0], argc
> 1 ? argv
[1] : NULL
);
769 argv
+= i
, argc
-= i
;
773 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
774 "no input file specified");
778 static void assemble_file (char *fname
)
780 char * value
, * p
, * q
, * special
, * line
, debugid
[80];
782 int i
, rn_error
, validid
;
784 struct tokenval tokval
;
787 int pass_cnt
= 0; /* count actual passes */
789 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
790 report_error(ERR_FATAL
, "command line: "
791 "32-bit segment size requires a higher cpu");
793 pass_max
= (optimizing
>0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
794 pass0
= !(optimizing
>0); /* start at 1 if not optimizing */
795 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
799 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
800 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
801 /* pass0 seq is 0, 0, 0,..., 1, 2 */
803 def_label
= pass
> 1 ? redefine_label
: define_label
;
806 sb
= cmd_sb
; /* set 'bits' to command line default */
810 nasmlist
.init(listname
, report_error
);
813 global_offset_changed
= FALSE
; /* set by redefine_label */
814 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
816 saa_rewind (forwrefs
);
817 forwref
= saa_rstruct (forwrefs
);
819 offsets
= raa_init();
821 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
823 if (pass
== 1) location
.known
= TRUE
;
824 location
.offset
= offs
= GET_CURR_OFFS
;
826 while ( (line
= preproc
->getline()) )
830 /* here we parse our directives; this is not handled by the 'real'
832 if ( (i
= getkw (line
, &value
)) )
835 case 1: /* [SEGMENT n] */
836 seg
= ofmt
->section (value
, pass2
, &sb
);
838 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
839 "segment name `%s' not recognised",
843 location
.segment
= seg
;
846 case 2: /* [EXTERN label:special] */
847 if (*value
== '$') value
++; /* skip initial $ if present */
850 while (*q
&& *q
!= ':')
854 ofmt
->symdef(value
, 0L, 0L, 3, q
);
856 } else if (pass
== 1) { /* pass == 1 */
861 while (*q
&& *q
!= ':') {
867 report_error (ERR_NONFATAL
,
868 "identifier expected after EXTERN");
876 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
878 pass0
= 1; /* fake pass 1 in labels.c */
879 declare_as_global (value
, special
, report_error
);
880 define_label (value
, seg_alloc(), 0L, NULL
, FALSE
, TRUE
,
884 } /* else pass0 == 1 */
886 case 3: /* [BITS bits] */
887 sb
= get_bits(value
);
889 case 4: /* [GLOBAL symbol:special] */
890 if (*value
== '$') 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] */
923 if (*value
== '$') value
++; /* skip initial $ if present */
929 while (*p
&& !isspace(*p
)) {
935 report_error (ERR_NONFATAL
,
936 "identifier expected after COMMON");
942 while (*p
&& isspace(*p
))
945 while (*q
&& *q
!= ':')
952 size
= readnum (p
, &rn_error
);
954 report_error (ERR_NONFATAL
, "invalid size specified"
955 " in COMMON declaration");
957 define_common (value
, seg_alloc(), size
,
958 special
, ofmt
, report_error
);
960 report_error (ERR_NONFATAL
, "no size specified in"
961 " COMMON declaration");
962 } else if (pass0
== 2) { /* pass == 2 */
964 while (*q
&& *q
!= ':') {
971 ofmt
->symdef(value
, 0L, 0L, 3, q
);
975 case 6: /* [ABSOLUTE address] */
977 stdscan_bufptr
= value
;
978 tokval
.t_type
= TOKEN_INVALID
;
979 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, report_error
,
983 report_error (pass0
==1 ? ERR_NONFATAL
: ERR_PANIC
,
984 "cannot use non-relocatable expression as "
987 abs_seg
= reloc_seg(e
);
988 abs_offset
= reloc_value(e
);
991 if (pass
==1) abs_offset
= 0x100;/* don't go near zero in case of / */
992 else report_error (ERR_PANIC
, "invalid ABSOLUTE address "
995 location
.segment
= NO_SEG
;
1003 while (*p
&& !isspace(*p
)) {
1010 report_error (pass
==1 ? ERR_NONFATAL
: ERR_PANIC
,
1011 "identifier expected after DEBUG");
1014 while (*p
&& isspace(*p
)) p
++;
1015 if (pass
==pass_max
) ofmt
->current_dfmt
->debug_directive (debugid
, p
);
1017 case 8: /* [WARNING {+|-}warn-name] */
1019 while (*value
&& isspace(*value
))
1022 if (*value
== '+' || *value
== '-') {
1023 validid
= (*value
== '-') ? TRUE
: FALSE
;
1028 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
1029 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1031 if (i
<= ERR_WARN_MAX
)
1032 suppressed
[i
] = validid
;
1034 report_error (ERR_NONFATAL
, "invalid warning id in WARNING directive");
1038 cpu
= get_cpu (value
);
1040 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1041 while (*value
&& isspace(*value
))
1044 if (*value
== '+') {
1048 if (*value
== '-') {
1052 report_error (ERR_NONFATAL
, "invalid parameter to \"list\" directive");
1057 if (!ofmt
->directive (line
+1, value
, pass2
))
1058 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
1059 "unrecognised directive [%s]",
1063 else /* it isn't a directive */
1065 parse_line (pass1
, line
, &output_ins
,
1066 report_error
, evaluate
,
1069 if (!(optimizing
>0) && pass
== 2) {
1070 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1071 output_ins
.forw_ref
= TRUE
;
1073 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1074 forwref
= saa_rstruct (forwrefs
);
1075 } while (forwref
!= NULL
&& forwref
->lineno
== globallineno
);
1077 output_ins
.forw_ref
= FALSE
;
1081 if (!(optimizing
>0) && output_ins
.forw_ref
)
1084 for(i
= 0; i
< output_ins
.operands
; i
++)
1086 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
)
1088 struct forwrefinfo
*fwinf
=
1089 (struct forwrefinfo
*)saa_wstruct(forwrefs
);
1090 fwinf
->lineno
= globallineno
;
1094 } else { /* pass == 2 */
1096 * Hack to prevent phase error in the code
1100 * If the second operand is a forward reference,
1101 * the UNITY property of the number 1 in that
1102 * operand is cancelled. Otherwise the above
1103 * sequence will cause a phase error.
1105 * This hack means that the above code will
1106 * generate 286+ code.
1108 * The forward reference will mean that the
1109 * operand will not have the UNITY property on
1110 * the first pass, so the pass behaviours will
1114 if (output_ins
.operands
>= 2 &&
1115 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1117 output_ins
.oprs
[1].type
&= ~(ONENESS
|BYTENESS
);
1125 if (output_ins
.opcode
== I_EQU
) {
1129 * Special `..' EQUs get processed in pass two,
1130 * except `..@' macro-processor EQUs which are done
1131 * in the normal place.
1133 if (!output_ins
.label
)
1134 report_error (ERR_NONFATAL
,
1135 "EQU not preceded by label");
1137 else if (output_ins
.label
[0] != '.' ||
1138 output_ins
.label
[1] != '.' ||
1139 output_ins
.label
[2] == '@')
1141 if (output_ins
.operands
== 1 &&
1142 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1143 output_ins
.oprs
[0].wrt
== NO_SEG
)
1145 int isext
= output_ins
.oprs
[0].opflags
& OPFLAG_EXTERN
;
1146 def_label (output_ins
.label
,
1147 output_ins
.oprs
[0].segment
,
1148 output_ins
.oprs
[0].offset
,
1149 NULL
, FALSE
, isext
, ofmt
, report_error
);
1151 else if (output_ins
.operands
== 2 &&
1152 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1153 (output_ins
.oprs
[0].type
& COLON
) &&
1154 output_ins
.oprs
[0].segment
== NO_SEG
&&
1155 output_ins
.oprs
[0].wrt
== NO_SEG
&&
1156 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1157 output_ins
.oprs
[1].segment
== NO_SEG
&&
1158 output_ins
.oprs
[1].wrt
== NO_SEG
)
1160 def_label (output_ins
.label
,
1161 output_ins
.oprs
[0].offset
| SEG_ABS
,
1162 output_ins
.oprs
[1].offset
,
1163 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1166 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1168 } else { /* pass == 2 */
1170 * Special `..' EQUs get processed here, except
1171 * `..@' macro processor EQUs which are done above.
1173 if (output_ins
.label
[0] == '.' &&
1174 output_ins
.label
[1] == '.' &&
1175 output_ins
.label
[2] != '@')
1177 if (output_ins
.operands
== 1 &&
1178 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1179 define_label (output_ins
.label
,
1180 output_ins
.oprs
[0].segment
,
1181 output_ins
.oprs
[0].offset
,
1182 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1184 else if (output_ins
.operands
== 2 &&
1185 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1186 (output_ins
.oprs
[0].type
& COLON
) &&
1187 output_ins
.oprs
[0].segment
== NO_SEG
&&
1188 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1189 output_ins
.oprs
[1].segment
== NO_SEG
)
1191 define_label (output_ins
.label
,
1192 output_ins
.oprs
[0].offset
| SEG_ABS
,
1193 output_ins
.oprs
[1].offset
,
1194 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1197 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1200 } else { /* instruction isn't an EQU */
1204 long l
= insn_size (location
.segment
, offs
, sb
, cpu
,
1205 &output_ins
, report_error
);
1207 /* if (using_debug_info) && output_ins.opcode != -1)*/
1208 if (using_debug_info
) /* fbk 03/25/01 */
1211 /* this is done here so we can do debug type info */
1212 long typeinfo
= TYS_ELEMENTS(output_ins
.operands
);
1213 switch (output_ins
.opcode
) {
1215 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
1218 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
1221 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
1224 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
1227 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
1230 typeinfo
|= TY_BYTE
;
1233 typeinfo
|= TY_WORD
;
1236 if (output_ins
.eops_float
)
1237 typeinfo
|= TY_FLOAT
;
1239 typeinfo
|= TY_DWORD
;
1242 typeinfo
|= TY_QWORD
;
1245 typeinfo
|= TY_TBYTE
;
1248 typeinfo
= TY_LABEL
;
1252 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1257 SET_CURR_OFFS (offs
);
1260 * else l == -1 => invalid instruction, which will be
1261 * flagged as an error on pass 2
1264 } else { /* pass == 2 */
1265 offs
+= assemble (location
.segment
, offs
, sb
, cpu
,
1266 &output_ins
, ofmt
, report_error
, &nasmlist
);
1267 SET_CURR_OFFS (offs
);
1271 cleanup_insn (&output_ins
);
1274 location
.offset
= offs
= GET_CURR_OFFS
;
1275 } /* end while (line = preproc->getline... */
1277 if (pass1
==2 && global_offset_changed
)
1278 report_error(ERR_NONFATAL
, "phase error detected at end of assembly.");
1280 if (pass1
== 1) preproc
->cleanup(1);
1282 if (pass1
==1 && terminate_after_phase
) {
1290 if (pass
>1 && !global_offset_changed
) {
1292 if (pass0
==2) pass
= pass_max
- 1;
1293 } else if (!(optimizing
>0)) pass0
++;
1295 } /* for (pass=1; pass<=2; pass++) */
1297 preproc
->cleanup(0);
1300 if (optimizing
>0 && opt_verbose_info
) /* -On and -Ov switches */
1302 "info:: assembly required 1+%d+1 passes\n", pass_cnt
-2);
1304 } /* exit from assemble_file (...) */
1307 static int getkw (char *buf
, char **value
)
1311 /* allow leading spaces or tabs */
1312 while (*buf
==' ' || *buf
=='\t')
1320 while (*p
&& *p
!= ']') p
++;
1327 while (*p
&& *p
!= ';') {
1335 while (*buf
&& *buf
!=' ' && *buf
!=']' && *buf
!='\t')
1342 while (isspace(*buf
)) buf
++; /* beppu - skip leading whitespace */
1344 while (*buf
!=']') buf
++;
1351 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1353 if (!nasm_stricmp(p
, "extern"))
1355 if (!nasm_stricmp(p
, "bits"))
1357 if (!nasm_stricmp(p
, "global"))
1359 if (!nasm_stricmp(p
, "common"))
1361 if (!nasm_stricmp(p
, "absolute"))
1363 if (!nasm_stricmp(p
, "debug"))
1365 if (!nasm_stricmp(p
, "warning"))
1367 if (!nasm_stricmp(p
, "cpu"))
1369 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1375 * gnu style error reporting
1376 * This function prints an error message to error_file in the
1377 * style used by GNU. An example would be:
1378 * file.asm:50: error: blah blah blah
1379 * where file.asm is the name of the file, 50 is the line number on
1380 * which the error occurs (or is detected) and "error:" is one of
1381 * the possible optional diagnostics -- it can be "error" or "warning"
1382 * or something else. Finally the line terminates with the actual
1385 * @param severity the severity of the warning or error
1386 * @param fmt the printf style format string
1388 static void report_error_gnu (int severity
, const char *fmt
, ...)
1392 if (is_suppressed_warning(severity
))
1395 if (severity
& ERR_NOFILE
)
1396 fputs ("nasm: ", error_file
);
1398 char * currentfile
= NULL
;
1400 src_get (&lineno
, ¤tfile
);
1401 fprintf (error_file
, "%s:%ld: ", currentfile
, lineno
);
1402 nasm_free (currentfile
);
1405 report_error_common (severity
, fmt
, ap
);
1410 * MS style error reporting
1411 * This function prints an error message to error_file in the
1412 * style used by Visual C and some other Microsoft tools. An example
1414 * file.asm(50) : error: blah blah blah
1415 * where file.asm is the name of the file, 50 is the line number on
1416 * which the error occurs (or is detected) and "error:" is one of
1417 * the possible optional diagnostics -- it can be "error" or "warning"
1418 * or something else. Finally the line terminates with the actual
1421 * @param severity the severity of the warning or error
1422 * @param fmt the printf style format string
1424 static void report_error_vc (int severity
, const char *fmt
, ...)
1428 if (is_suppressed_warning (severity
))
1431 if (severity
& ERR_NOFILE
)
1432 fputs ("nasm: ", error_file
);
1434 char * currentfile
= NULL
;
1436 src_get (&lineno
, ¤tfile
);
1437 fprintf (error_file
, "%s(%ld) : ", currentfile
, lineno
);
1438 nasm_free (currentfile
);
1441 report_error_common (severity
, fmt
, ap
);
1446 * check for supressed warning
1447 * checks for suppressed warning or pass one only warning and we're
1450 * @param severity the severity of the warning or error
1451 * @return true if we should abort error/warning printing
1453 static int is_suppressed_warning (int severity
)
1456 * See if it's a suppressed warning.
1458 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1459 (severity
& ERR_WARN_MASK
) != 0 &&
1460 suppressed
[ (severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1462 * See if it's a pass-one only warning and we're not in pass one.
1464 ((severity
& ERR_PASS1
) && pass0
== 2);
1468 * common error reporting
1469 * This is the common back end of the error reporting schemes currently
1470 * implemented. It prints the nature of the warning and then the
1471 * specific error message to error_file and may or may not return. It
1472 * doesn't return if the error severity is a "panic" or "debug" type.
1474 * @param severity the severity of the warning or error
1475 * @param fmt the printf style format string
1477 static void report_error_common (int severity
, const char *fmt
, va_list args
)
1479 switch (severity
& ERR_MASK
) {
1481 fputs ("warning: ", error_file
); break;
1483 fputs ("error: ", error_file
); break;
1485 fputs ("fatal: ", error_file
); break;
1487 fputs ("panic: ", error_file
); break;
1489 fputs("debug: ", error_file
); break;
1492 vfprintf (error_file
, fmt
, args
);
1493 fputc ('\n', error_file
);
1495 if (severity
& ERR_USAGE
)
1498 switch (severity
& ERR_MASK
) {
1499 case ERR_WARNING
: case ERR_DEBUG
:
1500 /* no further action, by definition */
1503 /* hack enables listing(!) on errors */
1504 terminate_after_phase
= TRUE
;
1513 exit(1); /* instantly die */
1514 break; /* placate silly compilers */
1517 /* abort(); */ /* halt, catch fire, and dump core */
1523 static void usage(void)
1525 fputs("type `nasm -h' for help\n", error_file
);
1528 static void register_output_formats(void)
1530 ofmt
= ofmt_register (report_error
);
1533 #define BUF_DELTA 512
1535 static FILE *no_pp_fp
;
1536 static efunc no_pp_err
;
1537 static ListGen
*no_pp_list
;
1538 static long no_pp_lineinc
;
1540 static void no_pp_reset (char *file
, int pass
, efunc error
, evalfunc eval
,
1543 src_set_fname(nasm_strdup(file
));
1547 no_pp_fp
= fopen(file
, "r");
1549 no_pp_err (ERR_FATAL
| ERR_NOFILE
,
1550 "unable to open input file `%s'", file
);
1551 no_pp_list
= listgen
;
1552 (void) pass
; /* placate compilers */
1553 (void) eval
; /* placate compilers */
1556 static char *no_pp_getline (void)
1558 char *buffer
, *p
, *q
;
1561 bufsize
= BUF_DELTA
;
1562 buffer
= nasm_malloc(BUF_DELTA
);
1563 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1565 while (1) { /* Loop to handle %line */
1568 while (1) { /* Loop to handle long lines */
1569 q
= fgets(p
, bufsize
-(p
-buffer
), no_pp_fp
);
1573 if (p
> buffer
&& p
[-1] == '\n')
1575 if (p
-buffer
> bufsize
-10) {
1577 offset
= p
- buffer
;
1578 bufsize
+= BUF_DELTA
;
1579 buffer
= nasm_realloc(buffer
, bufsize
);
1580 p
= buffer
+ offset
;
1584 if (!q
&& p
== buffer
) {
1590 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1591 * them are present at the end of the line.
1593 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1595 if (!strncmp(buffer
, "%line", 5)) {
1598 char *nm
= nasm_malloc(strlen(buffer
));
1599 if (sscanf(buffer
+5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1600 nasm_free( src_set_fname(nm
) );
1610 no_pp_list
->line (LIST_READ
, buffer
);
1615 static void no_pp_cleanup (int pass
)
1620 static unsigned long get_cpu (char *value
)
1623 if (!strcmp(value
, "8086")) return IF_8086
;
1624 if (!strcmp(value
, "186")) return IF_186
;
1625 if (!strcmp(value
, "286")) return IF_286
;
1626 if (!strcmp(value
, "386")) return IF_386
;
1627 if (!strcmp(value
, "486")) return IF_486
;
1628 if (!strcmp(value
, "586") ||
1629 !nasm_stricmp(value
, "pentium") ) return IF_PENT
;
1630 if (!strcmp(value
, "686") ||
1631 !nasm_stricmp(value
, "ppro") ||
1632 !nasm_stricmp(value
, "pentiumpro") ||
1633 !nasm_stricmp(value
, "p2") ) return IF_P6
;
1634 if (!nasm_stricmp(value
, "p3") ||
1635 !nasm_stricmp(value
, "katmai") ) return IF_KATMAI
;
1636 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1637 !nasm_stricmp(value
, "willamette") ) return IF_WILLAMETTE
;
1638 if (!nasm_stricmp(value
, "ia64") ||
1639 !nasm_stricmp(value
, "ia-64") ||
1640 !nasm_stricmp(value
, "itanium") ||
1641 !nasm_stricmp(value
, "itanic") ||
1642 !nasm_stricmp(value
, "merced") ) return IF_IA64
;
1644 report_error (pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
, "unknown 'cpu' type");
1646 return IF_PLEVEL
; /* the maximum level */
1650 static int get_bits (char *value
)
1654 if ((i
= atoi(value
)) == 16) return i
; /* set for a 16-bit segment */
1657 report_error(ERR_NONFATAL
,
1658 "cannot specify 32-bit segment on processor below a 386");
1662 report_error(pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
,
1663 "`%s' is not a valid segment size; must be 16 or 32",