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 report_error (int, char *, ...);
34 static void parse_cmdline (int, char **);
35 static void assemble_file (char *);
36 static int getkw (char *buf
, char **value
);
37 static void register_output_formats(void);
38 static void usage(void);
40 static int using_debug_info
;
42 int tasm_compatible_mode
= FALSE
;
45 static char inname
[FILENAME_MAX
];
46 static char outname
[FILENAME_MAX
];
47 static char listname
[FILENAME_MAX
];
48 static int globallineno
; /* for forward-reference tracking */
49 /* static int pass = 0; */
50 static struct ofmt
*ofmt
= NULL
;
52 static FILE *error_file
; /* Where to write error messages */
54 static FILE *ofile
= NULL
;
55 static int optimizing
= 0; /* number of optimization passes to take */
56 static int sb
, cmd_sb
= 16; /* by default */
57 static unsigned long cmd_cpu
= IF_PLEVEL
; /* highest level by default */
58 static unsigned long cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
59 int global_offset_changed
; /* referenced in labels.c */
61 static loc_t location
;
62 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
65 static struct RAA
*offsets
;
66 static long abs_offset
;
68 static struct SAA
*forwrefs
; /* keep track of forward references */
69 static struct forwrefinfo
*forwref
;
71 static Preproc
*preproc
;
73 op_normal
, /* Preprocess and assemble */
74 op_preprocess
, /* Preprocess only */
75 op_depend
/* Generate dependencies */
77 static enum op_type operating_mode
;
80 * Which of the suppressible warnings are suppressed. Entry zero
81 * doesn't do anything. Initial defaults are given here.
83 static char suppressed
[1+ERR_WARN_MAX
] = {
84 0, TRUE
, TRUE
, TRUE
, FALSE
88 * The option names for the suppressible warnings. As before, entry
91 static char *suppressed_names
[1+ERR_WARN_MAX
] = {
92 NULL
, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
96 * The explanations for the suppressible warnings. As before, entry
99 static char *suppressed_what
[1+ERR_WARN_MAX
] = {
101 "macro calls with wrong no. of params",
102 "cyclic macro self-references",
103 "labels alone on lines without trailing `:'",
104 "numeric constants greater than 0xFFFFFFFF"
108 * This is a null preprocessor which just copies lines from input
109 * to output. It's used when someone explicitly requests that NASM
110 * not preprocess their source file.
113 static void no_pp_reset (char *, int, efunc
, evalfunc
, ListGen
*);
114 static char *no_pp_getline (void);
115 static void no_pp_cleanup (void);
116 static Preproc no_pp
= {
123 * get/set current offset...
125 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
126 raa_read(offsets,location.segment))
127 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
128 (void)(offsets=raa_write(offsets,location.segment,(x))))
130 static int want_usage
;
131 static int terminate_after_phase
;
132 int user_nolist
= 0; /* fbk 9/2/00 */
134 static void nasm_fputs(char *line
, FILE *ofile
)
143 int main(int argc
, char **argv
)
146 want_usage
= terminate_after_phase
= FALSE
;
148 nasm_set_malloc_error (report_error
);
149 offsets
= raa_init();
150 forwrefs
= saa_init ((long)sizeof(struct forwrefinfo
));
153 operating_mode
= op_normal
;
159 register_output_formats();
161 parse_cmdline(argc
, argv
);
163 if (terminate_after_phase
)
171 pp_extra_stdmac (ofmt
->stdmac
);
172 parser_global_info (ofmt
, &location
);
173 eval_global_info (ofmt
, lookup_label
, &location
);
175 /* define some macros dependent of command-line */
178 sprintf (temp
, "__OUTPUT_FORMAT__=%s\n", ofmt
->shortname
);
179 pp_pre_define (temp
);
182 switch ( operating_mode
) {
186 preproc
->reset (inname
, 0, report_error
, evaluate
, &nasmlist
);
187 if (outname
[0] == '\0')
188 ofmt
->filename (inname
, outname
, report_error
);
190 fprintf(stdout
, "%s: %s", outname
, inname
);
191 while ( (line
= preproc
->getline()) )
201 char *file_name
= NULL
;
206 ofile
= fopen(outname
, "w");
208 report_error (ERR_FATAL
| ERR_NOFILE
,
209 "unable to open output file `%s'", outname
);
213 location
.known
= FALSE
;
216 preproc
->reset (inname
, 2, report_error
, evaluate
, &nasmlist
);
217 while ( (line
= preproc
->getline()) ) {
219 * We generate %line directives if needed for later programs
221 long linnum
= prior_linnum
+= lineinc
;
222 int altline
= src_get(&linnum
, &file_name
);
224 if (altline
==1 && lineinc
==1)
225 nasm_fputs("", ofile
);
227 lineinc
= (altline
!= -1 || lineinc
!=1);
228 fprintf(ofile
? ofile
: stdout
, "%%line %ld+%d %s\n",
229 linnum
, lineinc
, file_name
);
231 prior_linnum
= linnum
;
233 nasm_fputs(line
, ofile
);
236 nasm_free(file_name
);
240 if (ofile
&& terminate_after_phase
)
248 * We must call ofmt->filename _anyway_, even if the user
249 * has specified their own output file, because some
250 * formats (eg OBJ and COFF) use ofmt->filename to find out
251 * the name of the input file and then put that inside the
254 ofmt
->filename (inname
, outname
, report_error
);
256 ofile
= fopen(outname
, "wb");
258 report_error (ERR_FATAL
| ERR_NOFILE
,
259 "unable to open output file `%s'", outname
);
263 * We must call init_labels() before ofmt->init() since
264 * some object formats will want to define labels in their
265 * init routines. (eg OS/2 defines the FLAT group)
269 ofmt
->init (ofile
, report_error
, define_label
, evaluate
);
271 assemble_file (inname
);
273 if (!terminate_after_phase
) {
274 ofmt
->cleanup (using_debug_info
);
278 * We had an fclose on the output file here, but we
279 * actually do that in all the object file drivers as well,
280 * so we're leaving out the one here.
300 if (terminate_after_phase
)
308 * Get a parameter for a command line option.
309 * First arg must be in the form of e.g. -f...
311 static char *get_param (char *p
, char *q
, int *advance
)
314 if (p
[2]) /* the parameter's in the option */
326 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
327 "option `-%c' requires an argument",
339 #define OPT_POSTFIX 1
340 struct textargs textopts
[] =
342 {"prefix",OPT_PREFIX
},
343 {"postfix",OPT_POSTFIX
},
349 static int process_arg (char *p
, char *q
)
357 if (p
[0]=='-' && ! stopoptions
)
363 case 'o': /* these parameters take values */
373 if ( !(param
= get_param (p
, q
, &advance
)) )
375 if (p
[1]=='o') { /* output file */
376 strcpy (outname
, param
);
377 } else if (p
[1]=='f') { /* output format */
378 ofmt
= ofmt_find(param
);
380 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
381 "unrecognised output format `%s' - "
382 "use -hf for a list",
386 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
387 } else if (p
[1]=='O') { /* Optimization level */
388 if (!isdigit(*param
)) report_error(ERR_FATAL
,
389 "command line optimization level must be 0..3 or <nn>");
390 optimizing
= atoi(param
);
391 if (optimizing
<= 0) optimizing
= 0;
392 else if (optimizing
<= 3) optimizing
*= 5; /* 5 passes for each level */
393 } else if (p
[1]=='P' || p
[1]=='p') { /* pre-include */
394 pp_pre_include (param
);
395 } else if (p
[1]=='D' || p
[1]=='d') { /* pre-define */
396 pp_pre_define (param
);
397 } else if (p
[1]=='U' || p
[1]=='u') { /* un-define */
398 pp_pre_undefine (param
);
399 } else if (p
[1]=='I' || p
[1]=='i') { /* include search path */
400 pp_include_path (param
);
401 } else if (p
[1]=='l') { /* listing file */
402 strcpy (listname
, param
);
403 } else if (p
[1]=='E') { /* error messages file */
404 error_file
= fopen(param
, "w");
406 error_file
= stderr
; /* Revert to default! */
407 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
408 "cannot open file `%s' for error messages",
411 } else if (p
[1] == 'F') { /* specify debug format */
412 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
413 if (!ofmt
->current_dfmt
) {
414 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
415 "unrecognized debug format `%s' for"
416 " output format `%s'",
417 param
, ofmt
->shortname
);
422 using_debug_info
= TRUE
;
425 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
427 " [options...] [--] filename\n"
428 " or nasm -r for version info\n\n"
430 " -t Assemble in SciTech TASM compatible mode\n"
431 " -g Generate debug information in selected format.\n"
433 " -e preprocess only (writes output to stdout by default)\n"
434 " -a don't preprocess (assemble only)\n"
435 " -M generate Makefile dependencies on stdout\n\n"
436 " -E<file> redirect error messages to file\n"
437 " -s redirect error messages to stdout\n\n"
438 " -g enable debug info\n"
439 " -F format select a debugging format\n\n"
440 " -I<path> adds a pathname to the include file path\n"
441 " -O<digit> optimize branch offsets -O0 disables, -O2 default\n"
442 " -P<file> pre-includes a file\n"
443 " -D<macro>[=<value>] pre-defines a macro\n"
444 " -U<macro> undefines a macro\n"
445 " -w+foo enables warnings about foo; -w-foo disables them\n"
446 "where foo can be:\n");
447 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
448 printf(" %-16s%s (default %s)\n",
449 suppressed_names
[i
], suppressed_what
[i
],
450 suppressed
[i
] ? "off" : "on");
451 printf ("\nresponse files should contain command line parameters"
452 ", one per line.\n");
454 printf("\nvalid output formats for -f are"
455 " (`*' denotes default):\n");
456 ofmt_list(ofmt
, stdout
);
459 printf ("\nFor a list of valid output formats, use -hf.\n");
460 printf ("For a list of debug formats, use -f <form> -y.\n");
462 exit (0); /* never need usage message here */
465 printf("\nvalid debug formats for '%s' output format are"
466 " ('*' denotes default):\n",
468 dfmt_list(ofmt
, stdout
);
473 tasm_compatible_mode
= TRUE
;
478 printf("NASM version %s - SciTech TASM compatible additions\n", NASM_VER
);
480 printf("NASM version %s\n", NASM_VER
);
483 printf("Compiled with -DDEBUG on " __DATE__
"\n");
485 exit (0); /* never need usage message here */
487 case 'e': /* preprocess only */
488 operating_mode
= op_preprocess
;
490 case 'a': /* assemble only - don't preprocess */
494 if (p
[2] != '+' && p
[2] != '-') {
495 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
496 "invalid option to `-w'");
498 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
499 if (!nasm_stricmp(p
+3, suppressed_names
[i
]))
501 if (i
<= ERR_WARN_MAX
)
502 suppressed
[i
] = (p
[2] == '-');
504 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
505 "invalid option to `-w'");
509 operating_mode
= op_depend
;
516 if (p
[2]==0) { /* -- => stop processing options */
520 for(s
=0; textopts
[s
].label
; s
++)
522 if(!nasm_stricmp(p
+2, textopts
[s
].label
))
536 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
537 "option `--%s' requires an argument",
543 advance
= 1, param
= q
;
548 strncpy(lprefix
,param
,PREFIX_MAX
-1);
549 lprefix
[PREFIX_MAX
-1]=0;
554 strncpy(lpostfix
,param
,POSTFIX_MAX
-1);
555 lpostfix
[POSTFIX_MAX
-1]=0;
562 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
563 "unrecognised option `--%s'",
572 if (!ofmt
->setinfo(GI_SWITCH
,&p
))
573 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
574 "unrecognised option `-%c'",
582 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
583 "more than one input file specified");
591 #define ARG_BUF_DELTA 128
593 static void process_respfile (FILE *rfile
)
595 char *buffer
, *p
, *q
, *prevarg
;
596 int bufsize
, prevargsize
;
598 bufsize
= prevargsize
= ARG_BUF_DELTA
;
599 buffer
= nasm_malloc(ARG_BUF_DELTA
);
600 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
603 while (1) { /* Loop to handle all lines in file */
606 while (1) { /* Loop to handle long lines */
607 q
= fgets(p
, bufsize
-(p
-buffer
), rfile
);
611 if (p
> buffer
&& p
[-1] == '\n')
613 if (p
-buffer
> bufsize
-10) {
616 bufsize
+= ARG_BUF_DELTA
;
617 buffer
= nasm_realloc(buffer
, bufsize
);
622 if (!q
&& p
== buffer
) {
624 process_arg (prevarg
, NULL
);
631 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
632 * them are present at the end of the line.
634 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
636 while (p
> buffer
&& isspace(p
[-1]))
643 if (process_arg (prevarg
, p
))
646 if (strlen(p
) > prevargsize
-10) {
647 prevargsize
+= ARG_BUF_DELTA
;
648 prevarg
= nasm_realloc(prevarg
, prevargsize
);
654 /* Function to process args from a string of args, rather than the
655 * argv array. Used by the environment variable and response file
659 static void process_args (char *args
) {
660 char *p
, *q
, *arg
, *prevarg
;
661 char separator
= ' ';
669 while (*p
&& *p
!= separator
) p
++;
670 while (*p
== separator
) *p
++ = '\0';
673 if (process_arg (prevarg
, arg
))
677 process_arg (arg
, NULL
);
681 static void parse_cmdline(int argc
, char **argv
)
684 char *envreal
, *envcopy
=NULL
, *p
, *arg
;
687 char separator
= ' ';
690 *inname
= *outname
= *listname
= '\0';
693 * First, process the NASM environment variable.
695 envreal
= getenv("NASM");
698 envcopy
= nasm_strdup(envreal
);
700 process_args(envcopy
);
707 while (*p
&& *p
!= separator
) p
++;
708 while (*p
== separator
) *p
++ = '\0';
711 if (process_arg (prevarg
, arg
))
715 process_arg (arg
, NULL
);
721 * Now process the actual command line.
728 if (argv
[0][0] == '@') {
729 /* We have a response file, so process this as a set of
730 * arguments like the environment variable. This allows us
731 * to have multiple arguments on a single line, which is
732 * different to the -@resp file processing below for regular
735 char *str
= malloc(2048);
736 FILE *f
= fopen(&argv
[0][1],"r");
738 printf("out of memory");
742 while (fgets(str
,2048,f
)) {
752 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
753 if ((p
= get_param (argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
))) {
754 if ((rfile
= fopen(p
, "r"))) {
755 process_respfile (rfile
);
758 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
759 "unable to open response file `%s'", p
);
762 i
= process_arg (argv
[0], argc
> 1 ? argv
[1] : NULL
);
763 argv
+= i
, argc
-= i
;
767 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
768 "no input file specified");
772 static void assemble_file (char *fname
)
774 char * value
, * p
, * q
, * special
, * line
, debugid
[80];
776 int i
, rn_error
, validid
;
778 struct tokenval tokval
;
781 int pass_cnt
= 0; /* count actual passes */
783 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
784 report_error(ERR_FATAL
, "command line: "
785 "32-bit segment size requires a higher cpu");
787 pass_max
= optimizing
+ 2; /* passes 1, optimizing, then 2 */
788 pass0
= !optimizing
; /* start at 1 if not optimizing */
789 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
793 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
794 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
795 /* pass0 seq is 0, 0, 0,..., 1, 2 */
797 def_label
= pass
> 1 ? redefine_label
: define_label
;
800 sb
= cmd_sb
; /* set 'bits' to command line default */
804 nasmlist
.init(listname
, report_error
);
807 global_offset_changed
= FALSE
; /* set by redefine_label */
808 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
810 saa_rewind (forwrefs
);
811 forwref
= saa_rstruct (forwrefs
);
813 offsets
= raa_init();
815 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
817 if (pass
== 1) location
.known
= TRUE
;
818 location
.offset
= offs
= GET_CURR_OFFS
;
820 while ( (line
= preproc
->getline()) )
824 /* here we parse our directives; this is not handled by the 'real'
826 if ( (i
= getkw (line
, &value
)) )
829 case 1: /* [SEGMENT n] */
830 seg
= ofmt
->section (value
, pass2
, &sb
);
832 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
833 "segment name `%s' not recognised",
837 location
.segment
= seg
;
840 case 2: /* [EXTERN label:special] */
841 if (*value
== '$') value
++; /* skip initial $ if present */
844 while (*q
&& *q
!= ':')
848 ofmt
->symdef(value
, 0L, 0L, 3, q
);
850 } else if (pass0
== 1) { /* pass == 1 */
855 while (*q
&& *q
!= ':') {
861 report_error (ERR_NONFATAL
,
862 "identifier expected after EXTERN");
870 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
871 declare_as_global (value
, special
, report_error
);
872 define_label (value
, seg_alloc(), 0L, NULL
, FALSE
, TRUE
,
875 } /* else pass0 == 1 */
877 case 3: /* [BITS bits] */
878 sb
= get_bits(value
);
880 case 4: /* [GLOBAL symbol:special] */
881 if (*value
== '$') value
++; /* skip initial $ if present */
882 if (pass0
== 2) { /* pass 2 */
884 while (*q
&& *q
!= ':')
888 ofmt
->symdef(value
, 0L, 0L, 3, q
);
890 } else if (pass2
== 1) { /* pass == 1 */
895 while (*q
&& *q
!= ':') {
901 report_error (ERR_NONFATAL
,
902 "identifier expected after GLOBAL");
910 declare_as_global (value
, special
, report_error
);
913 case 5: /* [COMMON symbol size:special] */
914 if (*value
== '$') value
++; /* skip initial $ if present */
920 while (*p
&& !isspace(*p
)) {
926 report_error (ERR_NONFATAL
,
927 "identifier expected after COMMON");
933 while (*p
&& isspace(*p
))
936 while (*q
&& *q
!= ':')
943 size
= readnum (p
, &rn_error
);
945 report_error (ERR_NONFATAL
, "invalid size specified"
946 " in COMMON declaration");
948 define_common (value
, seg_alloc(), size
,
949 special
, ofmt
, report_error
);
951 report_error (ERR_NONFATAL
, "no size specified in"
952 " COMMON declaration");
953 } else if (pass0
== 2) { /* pass == 2 */
955 while (*q
&& *q
!= ':') {
962 ofmt
->symdef(value
, 0L, 0L, 3, q
);
966 case 6: /* [ABSOLUTE address] */
968 stdscan_bufptr
= value
;
969 tokval
.t_type
= TOKEN_INVALID
;
970 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, report_error
,
974 report_error (pass0
==1 ? ERR_NONFATAL
: ERR_PANIC
,
975 "cannot use non-relocatable expression as "
978 abs_seg
= reloc_seg(e
);
979 abs_offset
= reloc_value(e
);
982 if (pass
==1) abs_offset
= 0x100;/* don't go near zero in case of / */
983 else report_error (ERR_PANIC
, "invalid ABSOLUTE address "
986 location
.segment
= abs_seg
;
994 while (*p
&& !isspace(*p
)) {
1001 report_error (pass
==1 ? ERR_NONFATAL
: ERR_PANIC
,
1002 "identifier expected after DEBUG");
1005 while (*p
&& isspace(*p
)) p
++;
1006 if (pass
==pass_max
) ofmt
->current_dfmt
->debug_directive (debugid
, p
);
1008 case 8: /* [WARNING {+|-}warn-name] */
1010 while (*value
&& isspace(*value
))
1013 if (*value
== '+' || *value
== '-') {
1014 validid
= (*value
== '-') ? TRUE
: FALSE
;
1019 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
1020 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1022 if (i
<= ERR_WARN_MAX
)
1023 suppressed
[i
] = validid
;
1025 report_error (ERR_NONFATAL
, "invalid warning id in WARNING directive");
1029 cpu
= get_cpu (value
);
1031 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1032 while (*value
&& isspace(*value
))
1035 if (*value
== '+') {
1039 if (*value
== '-') {
1043 report_error (ERR_NONFATAL
, "invalid parameter to \"list\" directive");
1048 if (!ofmt
->directive (line
+1, value
, pass2
))
1049 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
1050 "unrecognised directive [%s]",
1054 else /* it isn't a directive */
1056 parse_line (pass1
, line
, &output_ins
,
1057 report_error
, evaluate
,
1060 if (!optimizing
&& pass
== 2) {
1061 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1062 output_ins
.forw_ref
= TRUE
;
1064 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1065 forwref
= saa_rstruct (forwrefs
);
1066 } while (forwref
!= NULL
&& forwref
->lineno
== globallineno
);
1068 output_ins
.forw_ref
= FALSE
;
1072 if (!optimizing
&& output_ins
.forw_ref
)
1075 for(i
= 0; i
< output_ins
.operands
; i
++)
1077 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
)
1079 struct forwrefinfo
*fwinf
=
1080 (struct forwrefinfo
*)saa_wstruct(forwrefs
);
1081 fwinf
->lineno
= globallineno
;
1085 } else { /* pass == 2 */
1087 * Hack to prevent phase error in the code
1091 * If the second operand is a forward reference,
1092 * the UNITY property of the number 1 in that
1093 * operand is cancelled. Otherwise the above
1094 * sequence will cause a phase error.
1096 * This hack means that the above code will
1097 * generate 286+ code.
1099 * The forward reference will mean that the
1100 * operand will not have the UNITY property on
1101 * the first pass, so the pass behaviours will
1105 if (output_ins
.operands
>= 2 &&
1106 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1108 output_ins
.oprs
[1].type
&= ~(ONENESS
|BYTENESS
);
1116 if (output_ins
.opcode
== I_EQU
) {
1120 * Special `..' EQUs get processed in pass two,
1121 * except `..@' macro-processor EQUs which are done
1122 * in the normal place.
1124 if (!output_ins
.label
)
1125 report_error (ERR_NONFATAL
,
1126 "EQU not preceded by label");
1128 else if (output_ins
.label
[0] != '.' ||
1129 output_ins
.label
[1] != '.' ||
1130 output_ins
.label
[2] == '@')
1132 if (output_ins
.operands
== 1 &&
1133 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1134 output_ins
.oprs
[0].wrt
== NO_SEG
)
1136 int isext
= output_ins
.oprs
[0].opflags
& OPFLAG_EXTERN
;
1137 def_label (output_ins
.label
,
1138 output_ins
.oprs
[0].segment
,
1139 output_ins
.oprs
[0].offset
,
1140 NULL
, FALSE
, isext
, ofmt
, report_error
);
1142 else if (output_ins
.operands
== 2 &&
1143 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1144 (output_ins
.oprs
[0].type
& COLON
) &&
1145 output_ins
.oprs
[0].segment
== NO_SEG
&&
1146 output_ins
.oprs
[0].wrt
== NO_SEG
&&
1147 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1148 output_ins
.oprs
[1].segment
== NO_SEG
&&
1149 output_ins
.oprs
[1].wrt
== NO_SEG
)
1151 def_label (output_ins
.label
,
1152 output_ins
.oprs
[0].offset
| SEG_ABS
,
1153 output_ins
.oprs
[1].offset
,
1154 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1157 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1159 } else { /* pass == 2 */
1161 * Special `..' EQUs get processed here, except
1162 * `..@' macro processor EQUs which are done above.
1164 if (output_ins
.label
[0] == '.' &&
1165 output_ins
.label
[1] == '.' &&
1166 output_ins
.label
[2] != '@')
1168 if (output_ins
.operands
== 1 &&
1169 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1170 define_label (output_ins
.label
,
1171 output_ins
.oprs
[0].segment
,
1172 output_ins
.oprs
[0].offset
,
1173 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1175 else if (output_ins
.operands
== 2 &&
1176 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1177 (output_ins
.oprs
[0].type
& COLON
) &&
1178 output_ins
.oprs
[0].segment
== NO_SEG
&&
1179 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1180 output_ins
.oprs
[1].segment
== NO_SEG
)
1182 define_label (output_ins
.label
,
1183 output_ins
.oprs
[0].offset
| SEG_ABS
,
1184 output_ins
.oprs
[1].offset
,
1185 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1188 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1191 } else { /* instruction isn't an EQU */
1195 long l
= insn_size (location
.segment
, offs
, sb
, cpu
,
1196 &output_ins
, report_error
);
1198 /* if (using_debug_info) && output_ins.opcode != -1)*/
1199 if (using_debug_info
); /* fbk 12/29/00 */
1202 /* this is done here so we can do debug type info */
1203 long typeinfo
= TYS_ELEMENTS(output_ins
.operands
);
1204 switch (output_ins
.opcode
) {
1206 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
1209 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
1212 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
1215 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
1218 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
1221 typeinfo
|= TY_BYTE
;
1224 typeinfo
|= TY_WORD
;
1227 if (output_ins
.eops_float
)
1228 typeinfo
|= TY_FLOAT
;
1230 typeinfo
|= TY_DWORD
;
1233 typeinfo
|= TY_QWORD
;
1236 typeinfo
|= TY_TBYTE
;
1239 typeinfo
= TY_LABEL
;
1243 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1248 SET_CURR_OFFS (offs
);
1251 * else l == -1 => invalid instruction, which will be
1252 * flagged as an error on pass 2
1255 } else { /* pass == 2 */
1256 offs
+= assemble (location
.segment
, offs
, sb
, cpu
,
1257 &output_ins
, ofmt
, report_error
, &nasmlist
);
1258 SET_CURR_OFFS (offs
);
1262 cleanup_insn (&output_ins
);
1265 location
.offset
= offs
= GET_CURR_OFFS
;
1266 } /* end while (line = preproc->getline... */
1268 if (pass1
==2 && global_offset_changed
)
1269 report_error(ERR_NONFATAL
, "phase error detected at end of assembly.");
1271 if (pass1
== 1) preproc
->cleanup();
1273 if (pass1
==1 && terminate_after_phase
) {
1281 if (pass
>1 && !global_offset_changed
) {
1283 if (pass0
==2) pass
= pass_max
- 1;
1284 } else if (!optimizing
) pass0
++;
1286 } /* for (pass=1; pass<=2; pass++) */
1290 if (optimizing
&& using_debug_info
) /* -On and -g switches */
1292 "info:: assembly required 1+%d+1 passes\n", pass_cnt
-2);
1294 } /* exit from assemble_file (...) */
1297 static int getkw (char *buf
, char **value
)
1306 while (*p
&& *p
!= ']') p
++;
1313 while (*p
&& *p
!= ';') {
1321 while (*buf
&& *buf
!=' ' && *buf
!=']' && *buf
!='\t')
1328 while (isspace(*buf
)) buf
++; /* beppu - skip leading whitespace */
1330 while (*buf
!=']') buf
++;
1337 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1339 if (!nasm_stricmp(p
, "extern"))
1341 if (!nasm_stricmp(p
, "bits"))
1343 if (!nasm_stricmp(p
, "global"))
1345 if (!nasm_stricmp(p
, "common"))
1347 if (!nasm_stricmp(p
, "absolute"))
1349 if (!nasm_stricmp(p
, "debug"))
1351 if (!nasm_stricmp(p
, "warning"))
1353 if (!nasm_stricmp(p
, "cpu"))
1355 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1360 static void report_error (int severity
, char *fmt
, ...)
1365 * See if it's a suppressed warning.
1367 if ((severity
& ERR_MASK
) == ERR_WARNING
&&
1368 (severity
& ERR_WARN_MASK
) != 0 &&
1369 suppressed
[ (severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
])
1370 return; /* and bail out if so */
1373 * See if it's a pass-one only warning and we're not in pass one.
1375 if ((severity
& ERR_PASS1
) && pass0
== 2)
1378 if (severity
& ERR_NOFILE
)
1379 fputs ("nasm: ", error_file
);
1381 char * currentfile
= NULL
;
1383 src_get (&lineno
, ¤tfile
);
1384 fprintf (error_file
, "%s:%ld: ", currentfile
, lineno
);
1385 nasm_free (currentfile
);
1388 switch (severity
& ERR_MASK
) {
1390 fputs ("warning: ", error_file
); break;
1392 fputs ("error: ", error_file
); break;
1394 fputs ("fatal: ", error_file
); break;
1396 fputs ("panic: ", error_file
); break;
1398 fputs("debug: ", error_file
); break;
1402 vfprintf (error_file
, fmt
, ap
);
1403 fputc ('\n', error_file
);
1405 if (severity
& ERR_USAGE
)
1408 switch (severity
& ERR_MASK
) {
1409 case ERR_WARNING
: case ERR_DEBUG
:
1410 /* no further action, by definition */
1413 terminate_after_phase
= TRUE
;
1422 exit(1); /* instantly die */
1423 break; /* placate silly compilers */
1426 /* abort(); */ /* halt, catch fire, and dump core */
1432 static void usage(void)
1434 fputs("type `nasm -h' for help\n", error_file
);
1437 static void register_output_formats(void)
1439 ofmt
= ofmt_register (report_error
);
1442 #define BUF_DELTA 512
1444 static FILE *no_pp_fp
;
1445 static efunc no_pp_err
;
1446 static ListGen
*no_pp_list
;
1447 static long no_pp_lineinc
;
1449 static void no_pp_reset (char *file
, int pass
, efunc error
, evalfunc eval
,
1452 src_set_fname(nasm_strdup(file
));
1456 no_pp_fp
= fopen(file
, "r");
1458 no_pp_err (ERR_FATAL
| ERR_NOFILE
,
1459 "unable to open input file `%s'", file
);
1460 no_pp_list
= listgen
;
1461 (void) pass
; /* placate compilers */
1462 (void) eval
; /* placate compilers */
1465 static char *no_pp_getline (void)
1467 char *buffer
, *p
, *q
;
1470 bufsize
= BUF_DELTA
;
1471 buffer
= nasm_malloc(BUF_DELTA
);
1472 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1474 while (1) { /* Loop to handle %line */
1477 while (1) { /* Loop to handle long lines */
1478 q
= fgets(p
, bufsize
-(p
-buffer
), no_pp_fp
);
1482 if (p
> buffer
&& p
[-1] == '\n')
1484 if (p
-buffer
> bufsize
-10) {
1486 offset
= p
- buffer
;
1487 bufsize
+= BUF_DELTA
;
1488 buffer
= nasm_realloc(buffer
, bufsize
);
1489 p
= buffer
+ offset
;
1493 if (!q
&& p
== buffer
) {
1499 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1500 * them are present at the end of the line.
1502 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1504 if (!strncmp(buffer
, "%line", 5)) {
1507 char *nm
= nasm_malloc(strlen(buffer
));
1508 if (sscanf(buffer
+5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1509 nasm_free( src_set_fname(nm
) );
1519 no_pp_list
->line (LIST_READ
, buffer
);
1524 static void no_pp_cleanup (void)
1529 static unsigned long get_cpu (char *value
)
1532 if (!strcmp(value
, "8086")) return IF_8086
;
1533 if (!strcmp(value
, "186")) return IF_186
;
1534 if (!strcmp(value
, "286")) return IF_286
;
1535 if (!strcmp(value
, "386")) return IF_386
;
1536 if (!strcmp(value
, "486")) return IF_486
;
1537 if (!strcmp(value
, "586") ||
1538 !nasm_stricmp(value
, "pentium") ) return IF_PENT
;
1539 if (!strcmp(value
, "686") ||
1540 !nasm_stricmp(value
, "ppro") ||
1541 !nasm_stricmp(value
, "p2") ) return IF_P6
;
1542 if (!nasm_stricmp(value
, "p3") ||
1543 !nasm_stricmp(value
, "katmai") ) return IF_KATMAI
;
1545 report_error (pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
, "unknown 'cpu' type");
1547 return IF_PLEVEL
; /* the maximum level */
1551 static int get_bits (char *value
)
1555 if ((i
= atoi(value
)) == 16) return i
; /* set for a 16-bit segment */
1558 report_error(ERR_NONFATAL
,
1559 "cannot specify 32-bit segment on processor below a 386");
1563 report_error(pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
,
1564 "`%s' is not a valid segment size; must be 16 or 32",