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.
25 struct forwrefinfo
{ /* info held on forward refs. */
30 static void report_error (int, char *, ...);
31 static void parse_cmdline (int, char **);
32 static void assemble_file (char *);
33 static int getkw (char *buf
, char **value
);
34 static void register_output_formats(void);
35 static void usage(void);
37 static int using_debug_info
;
39 static char inname
[FILENAME_MAX
];
40 static char outname
[FILENAME_MAX
];
41 static char listname
[FILENAME_MAX
];
42 static int globallineno
; /* for forward-reference tracking */
44 static struct ofmt
*ofmt
= NULL
;
46 static FILE *error_file
; /* Where to write error messages */
48 static FILE *ofile
= NULL
;
49 static int sb
= 16; /* by default */
51 static loc_t location
;
52 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
55 static struct RAA
*offsets
;
56 static long abs_offset
;
58 static struct SAA
*forwrefs
; /* keep track of forward references */
59 static struct forwrefinfo
*forwref
;
61 static Preproc
*preproc
;
63 op_normal
, /* Preprocess and assemble */
64 op_preprocess
, /* Preprocess only */
65 op_depend
/* Generate dependencies */
67 static enum op_type operating_mode
;
69 /* used by error function to report location */
72 * Which of the suppressible warnings are suppressed. Entry zero
73 * doesn't do anything. Initial defaults are given here.
75 static char suppressed
[1+ERR_WARN_MAX
] = {
80 * The option names for the suppressible warnings. As before, entry
83 static char *suppressed_names
[1+ERR_WARN_MAX
] = {
84 NULL
, "macro-params", "orphan-labels", "number-overflow"
88 * The explanations for the suppressible warnings. As before, entry
91 static char *suppressed_what
[1+ERR_WARN_MAX
] = {
92 NULL
, "macro calls with wrong no. of params",
93 "labels alone on lines without trailing `:'",
94 "numeric constants greater than 0xFFFFFFFF"
98 * This is a null preprocessor which just copies lines from input
99 * to output. It's used when someone explicitly requests that NASM
100 * not preprocess their source file.
103 static void no_pp_reset (char *, int, efunc
, evalfunc
, ListGen
*);
104 static char *no_pp_getline (void);
105 static void no_pp_cleanup (void);
106 static Preproc no_pp
= {
113 * get/set current offset...
115 #define get_curr_ofs (in_abs_seg?abs_offset:\
116 raa_read(offsets,location.segment))
117 #define set_curr_ofs(x) (in_abs_seg?(void)(abs_offset=(x)):\
118 (void)(offsets=raa_write(offsets,location.segment,(x))))
120 static int want_usage
;
121 static int terminate_after_phase
;
123 static void nasm_fputs(char *line
, FILE *ofile
)
132 int main(int argc
, char **argv
)
134 want_usage
= terminate_after_phase
= FALSE
;
136 nasm_set_malloc_error (report_error
);
137 offsets
= raa_init();
138 forwrefs
= saa_init ((long)sizeof(struct forwrefinfo
));
141 operating_mode
= op_normal
;
147 register_output_formats();
149 parse_cmdline(argc
, argv
);
151 if (terminate_after_phase
)
159 pp_extra_stdmac (ofmt
->stdmac
);
160 parser_global_info (ofmt
, &location
);
161 eval_global_info (ofmt
, lookup_label
, &location
);
163 switch ( operating_mode
) {
167 preproc
->reset (inname
, 0, report_error
, evaluate
, &nasmlist
);
168 if (outname
[0] == '\0')
169 ofmt
->filename (inname
, outname
, report_error
);
171 printf("%s: %s", outname
, inname
);
172 while ( (line
= preproc
->getline()) != NULL
)
182 char *file_name
= NULL
;
187 ofile
= fopen(outname
, "w");
189 report_error (ERR_FATAL
| ERR_NOFILE
,
190 "unable to open output file `%s'", outname
);
194 location
.known
= FALSE
;
196 preproc
->reset (inname
, 2, report_error
, evaluate
, &nasmlist
);
197 while ( (line
= preproc
->getline()) != NULL
) {
199 * We generate %line directives if needed for later programs
201 long linnum
= prior_linnum
+= lineinc
;
202 int altline
= src_get(&linnum
, &file_name
);
204 if (altline
==1 && lineinc
==1)
205 nasm_fputs("", ofile
);
207 lineinc
= (altline
!= -1 || lineinc
!=1);
208 fprintf(ofile
? ofile
: stdout
, "%%line %ld+%d %s\n",
209 linnum
, lineinc
, file_name
);
211 prior_linnum
= linnum
;
213 nasm_fputs(line
, ofile
);
216 nasm_free(file_name
);
220 if (ofile
&& terminate_after_phase
)
228 * We must call ofmt->filename _anyway_, even if the user
229 * has specified their own output file, because some
230 * formats (eg OBJ and COFF) use ofmt->filename to find out
231 * the name of the input file and then put that inside the
234 ofmt
->filename (inname
, outname
, report_error
);
236 ofile
= fopen(outname
, "wb");
238 report_error (ERR_FATAL
| ERR_NOFILE
,
239 "unable to open output file `%s'", outname
);
243 * We must call init_labels() before ofmt->init() since
244 * some object formats will want to define labels in their
245 * init routines. (eg OS/2 defines the FLAT group)
249 ofmt
->init (ofile
, report_error
, define_label
, evaluate
);
251 assemble_file (inname
);
253 if (!terminate_after_phase
) {
254 ofmt
->cleanup (using_debug_info
);
260 * We had an fclose on the output file here, but we
261 * actually do that in all the object file drivers as well,
262 * so we're leaving out the one here.
282 if (terminate_after_phase
)
290 * Get a parameter for a command line option.
291 * First arg must be in the form of e.g. -f...
293 static char *get_param (char *p
, char *q
, int *advance
)
296 if (p
[2]) /* the parameter's in the option */
308 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
309 "option `-%c' requires an argument",
315 static int process_arg (char *p
, char *q
)
323 if (p
[0]=='-' && ! stopoptions
)
326 case '-': /* -- => stop processing options */
332 case 'o': /* these parameters take values */
342 if ( !(param
= get_param (p
, q
, &advance
)) )
344 if (p
[1]=='o') { /* output file */
345 strcpy (outname
, param
);
346 } else if (p
[1]=='f') { /* output format */
347 ofmt
= ofmt_find(param
);
349 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
350 "unrecognised output format `%s' - "
351 "use -hf for a list",
355 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
356 } else if (p
[1]=='P' || p
[1]=='p') { /* pre-include */
357 pp_pre_include (param
);
358 } else if (p
[1]=='D' || p
[1]=='d') { /* pre-define */
359 pp_pre_define (param
);
360 } else if (p
[1]=='U' || p
[1]=='u') { /* un-define */
361 pp_pre_undefine (param
);
362 } else if (p
[1]=='I' || p
[1]=='i') { /* include search path */
363 pp_include_path (param
);
364 } else if (p
[1]=='l') { /* listing file */
365 strcpy (listname
, param
);
366 } else if (p
[1]=='E') { /* error messages file */
367 error_file
= fopen(param
, "wt");
369 error_file
= stderr
; /* Revert to default! */
370 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
371 "cannot open file `%s' for error messages",
374 } else if (p
[1] == 'F') { /* specify debug format */
375 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
376 if (!ofmt
->current_dfmt
) {
377 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
378 "unrecognized debug format `%s' for"
379 " output format `%s'",
380 param
, ofmt
->shortname
);
385 using_debug_info
= TRUE
;
388 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
390 " [options...] [--] filename\n"
391 " or nasm -r for version info\n\n"
392 " -e preprocess only (writes output to stdout by default)\n"
393 " -a don't preprocess (assemble only)\n"
394 " -M generate Makefile dependencies on stdout\n\n"
395 " -E<file> redirect error messages to file\n"
396 " -s redirect error messages to stdout\n\n"
397 " -g enable debug info\n"
398 " -F format select a debugging format\n\n"
399 " -I<path> adds a pathname to the include file path\n"
400 " -P<file> pre-includes a file\n"
401 " -D<macro>[=<value>] pre-defines a macro\n"
402 " -U<macro> undefines a macro\n"
403 " -w+foo enables warnings about foo; -w-foo disables them\n"
404 "where foo can be:\n");
405 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
406 printf(" %-16s%s (default %s)\n",
407 suppressed_names
[i
], suppressed_what
[i
],
408 suppressed
[i
] ? "off" : "on");
409 printf ("\nresponse files should contain command line parameters"
410 ", one per line.\n");
412 printf("\nvalid output formats for -f are"
413 " (`*' denotes default):\n");
414 ofmt_list(ofmt
, stdout
);
417 printf ("\nFor a list of valid output formats, use -hf.\n");
418 printf ("For a list of debug formats, use -f <form> -y.\n");
420 exit (0); /* never need usage message here */
423 printf("\nvalid debug formats for '%s' output format are"
424 " ('*' denotes default):\n",
426 dfmt_list(ofmt
, stdout
);
430 printf("NASM version %s\n", NASM_VER
);
432 printf("Compiled with -DDEBUG on " __DATE__
"\n");
434 exit (0); /* never need usage message here */
436 case 'e': /* preprocess only */
437 operating_mode
= op_preprocess
;
439 case 'a': /* assemble only - don't preprocess */
443 if (p
[2] != '+' && p
[2] != '-') {
444 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
445 "invalid option to `-w'");
447 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
448 if (!nasm_stricmp(p
+3, suppressed_names
[i
]))
450 if (i
<= ERR_WARN_MAX
)
451 suppressed
[i
] = (p
[2] == '-');
453 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
454 "invalid option to `-w'");
458 operating_mode
= op_depend
;
461 if (!ofmt
->setinfo(GI_SWITCH
,&p
))
462 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
463 "unrecognised option `-%c'",
471 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
472 "more than one input file specified");
480 #define ARG_BUF_DELTA 128
482 static void process_respfile (FILE *rfile
)
484 char *buffer
, *p
, *q
, *prevarg
;
485 int bufsize
, prevargsize
;
487 bufsize
= prevargsize
= ARG_BUF_DELTA
;
488 buffer
= nasm_malloc(ARG_BUF_DELTA
);
489 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
492 while (1) { /* Loop to handle all lines in file */
495 while (1) { /* Loop to handle long lines */
496 q
= fgets(p
, bufsize
-(p
-buffer
), rfile
);
500 if (p
> buffer
&& p
[-1] == '\n')
502 if (p
-buffer
> bufsize
-10) {
505 bufsize
+= ARG_BUF_DELTA
;
506 buffer
= nasm_realloc(buffer
, bufsize
);
511 if (!q
&& p
== buffer
) {
513 process_arg (prevarg
, NULL
);
520 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
521 * them are present at the end of the line.
523 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
525 while (p
> buffer
&& isspace(p
[-1]))
532 if (process_arg (prevarg
, p
))
535 if ((int)strlen(p
) > prevargsize
-10) {
536 prevargsize
+= ARG_BUF_DELTA
;
537 prevarg
= nasm_realloc(prevarg
, prevargsize
);
543 static void parse_cmdline(int argc
, char **argv
)
546 char *envreal
, *envcopy
, *p
, *q
, *arg
, *prevarg
;
547 char separator
= ' ';
549 *inname
= *outname
= *listname
= '\0';
552 * First, process the NASM environment variable.
554 envreal
= getenv("NASM");
557 envcopy
= nasm_strdup(envreal
);
563 while (*p
&& *p
!= separator
) p
++;
564 while (*p
== separator
) *p
++ = '\0';
567 if (process_arg (prevarg
, arg
))
571 process_arg (arg
, NULL
);
576 * Now process the actual command line.
582 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
583 if ((p
= get_param (argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
))) {
584 if ((rfile
= fopen(p
, "r"))) {
585 process_respfile (rfile
);
588 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
589 "unable to open response file `%s'", p
);
592 i
= process_arg (argv
[0], argc
> 1 ? argv
[1] : NULL
);
593 argv
+= i
, argc
-= i
;
597 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
598 "no input file specified");
601 static void assemble_file (char *fname
)
603 char * value
, * p
, * q
, * special
, * line
, debugid
[80];
605 int i
, rn_error
, validid
;
607 struct tokenval tokval
;
615 location
.segment
= ofmt
->section(NULL
, pass
, &sb
);
616 preproc
->reset(fname
, 1, report_error
, evaluate
, &nasmlist
);
618 location
.known
= TRUE
;
619 location
.offset
= offs
= get_curr_ofs
;
621 while ( (line
= preproc
->getline()) != NULL
)
625 /* here we parse our directives; this is not handled by the 'real'
627 if ( (i
= getkw (line
, &value
)) )
630 case 1: /* [SEGMENT n] */
631 seg
= ofmt
->section (value
, pass
, &sb
);
633 report_error (ERR_NONFATAL
,
634 "segment name `%s' not recognised",
638 location
.segment
= seg
;
641 case 2: /* [EXTERN label:special] */
643 value
++; /* skip initial $ if present */
648 while (*q
&& *q
!= ':') {
654 report_error (ERR_NONFATAL
,
655 "identifier expected after EXTERN");
663 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
664 declare_as_global (value
, special
, report_error
);
665 define_label (value
, seg_alloc(), 0L, NULL
, FALSE
, TRUE
,
669 case 3: /* [BITS bits] */
670 switch (atoi(value
)) {
676 report_error(ERR_NONFATAL
,
677 "`%s' is not a valid argument to [BITS]",
682 case 4: /* [GLOBAL symbol:special] */
684 value
++; /* skip initial $ if present */
689 while (*q
&& *q
!= ':') {
695 report_error (ERR_NONFATAL
,
696 "identifier expected after GLOBAL");
704 declare_as_global (value
, special
, report_error
);
706 case 5: /* [COMMON symbol size:special] */
711 while (*p
&& !isspace(*p
)) {
717 report_error (ERR_NONFATAL
,
718 "identifier expected after COMMON");
724 while (*p
&& isspace(*p
))
727 while (*q
&& *q
!= ':')
734 size
= readnum (p
, &rn_error
);
736 report_error (ERR_NONFATAL
, "invalid size specified"
737 " in COMMON declaration");
739 define_common (value
, seg_alloc(), size
,
740 special
, ofmt
, report_error
);
742 report_error (ERR_NONFATAL
, "no size specified in"
743 " COMMON declaration");
745 case 6: /* [ABSOLUTE address] */
747 stdscan_bufptr
= value
;
748 tokval
.t_type
= TOKEN_INVALID
;
749 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, report_error
,
753 report_error (ERR_NONFATAL
, "cannot use non-"
754 "relocatable expression as ABSOLUTE"
757 abs_seg
= reloc_seg(e
);
758 abs_offset
= reloc_value(e
);
761 abs_offset
= 0x100;/* don't go near zero in case of / */
763 location
.segment
= abs_seg
;
770 while (*p
&& !isspace(*p
)) {
776 report_error (ERR_NONFATAL
,
777 "identifier expected after DEBUG");
780 while (*p
&& isspace(*p
)) p
++;
783 if (!ofmt
->directive (line
+1, value
, 1))
784 report_error (ERR_NONFATAL
, "unrecognised directive [%s]",
789 else /* it isn't a directive */
791 parse_line (1, line
, &output_ins
,
792 report_error
, evaluate
, define_label
);
794 if (output_ins
.forw_ref
)
796 for(i
= 0; i
< output_ins
.operands
; i
++)
798 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
)
800 struct forwrefinfo
*fwinf
=
801 (struct forwrefinfo
*)saa_wstruct(forwrefs
);
802 fwinf
->lineno
= globallineno
;
808 if (output_ins
.opcode
== I_EQU
)
811 * Special `..' EQUs get processed in pass two,
812 * except `..@' macro-processor EQUs which are done
813 * in the normal place.
815 if (!output_ins
.label
)
816 report_error (ERR_NONFATAL
,
817 "EQU not preceded by label");
819 else if (output_ins
.label
[0] != '.' ||
820 output_ins
.label
[1] != '.' ||
821 output_ins
.label
[2] == '@')
823 if (output_ins
.operands
== 1 &&
824 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
825 output_ins
.oprs
[0].wrt
== NO_SEG
)
827 int isext
= output_ins
.oprs
[0].opflags
& OPFLAG_EXTERN
;
828 define_label (output_ins
.label
,
829 output_ins
.oprs
[0].segment
,
830 output_ins
.oprs
[0].offset
,
831 NULL
, FALSE
, isext
, ofmt
, report_error
);
833 else if (output_ins
.operands
== 2 &&
834 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
835 (output_ins
.oprs
[0].type
& COLON
) &&
836 output_ins
.oprs
[0].segment
== NO_SEG
&&
837 output_ins
.oprs
[0].wrt
== NO_SEG
&&
838 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
839 output_ins
.oprs
[1].segment
== NO_SEG
&&
840 output_ins
.oprs
[1].wrt
== NO_SEG
)
842 define_label (output_ins
.label
,
843 output_ins
.oprs
[0].offset
| SEG_ABS
,
844 output_ins
.oprs
[1].offset
,
845 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
848 report_error(ERR_NONFATAL
, "bad syntax for EQU");
851 else /* instruction isn't an EQU */
853 long l
= insn_size (location
.segment
, offs
, sb
,
854 &output_ins
, report_error
);
855 if (using_debug_info
&& output_ins
.opcode
!= -1) {
856 /* this is done here so we can do debug type info */
857 long typeinfo
= TYS_ELEMENTS(output_ins
.operands
);
858 switch (output_ins
.opcode
) {
860 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
863 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
866 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
869 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
872 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
881 if (output_ins
.eops_float
)
882 typeinfo
|= TY_FLOAT
;
884 typeinfo
|= TY_DWORD
;
887 typeinfo
|= TY_QWORD
;
890 typeinfo
|= TY_TBYTE
;
895 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
902 * else l == -1 => invalid instruction, which will be
903 * flagged as an error on pass 2
906 cleanup_insn (&output_ins
);
909 location
.offset
= offs
= get_curr_ofs
;
914 if (terminate_after_phase
) {
927 saa_rewind (forwrefs
);
929 nasmlist
.init(listname
, report_error
);
930 forwref
= saa_rstruct (forwrefs
);
932 location
.segment
= ofmt
->section(NULL
, pass
, &sb
);
934 offsets
= raa_init();
935 preproc
->reset(fname
, 2, report_error
, evaluate
, &nasmlist
);
937 location
.offset
= offs
= get_curr_ofs
;
939 while ( (line
= preproc
->getline()) != NULL
)
943 /* here we parse our directives; this is not handled by
944 * the 'real' parser. */
945 if ( (i
= getkw (line
, &value
)) ) {
947 case 1: /* [SEGMENT n] */
948 seg
= ofmt
->section (value
, pass
, &sb
);
950 report_error (ERR_PANIC
,
951 "invalid segment name on pass two");
954 location
.segment
= seg
;
956 case 2: /* [EXTERN label] */
958 while (*q
&& *q
!= ':')
962 ofmt
->symdef(value
, 0L, 0L, 3, q
);
965 case 3: /* [BITS bits] */
966 switch (atoi(value
)) {
972 report_error(ERR_PANIC
,
973 "invalid [BITS] value on pass two",
978 case 4: /* [GLOBAL symbol] */
980 while (*q
&& *q
!= ':')
984 ofmt
->symdef(value
, 0L, 0L, 3, q
);
987 case 5: /* [COMMON symbol size] */
989 while (*q
&& *q
!= ':') {
996 ofmt
->symdef(value
, 0L, 0L, 3, q
);
999 case 6: /* [ABSOLUTE addr] */
1001 stdscan_bufptr
= value
;
1002 tokval
.t_type
= TOKEN_INVALID
;
1003 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 2, report_error
,
1007 report_error (ERR_PANIC
, "non-reloc ABSOLUTE address"
1010 abs_seg
= reloc_seg(e
);
1011 abs_offset
= reloc_value(e
);
1014 report_error (ERR_PANIC
, "invalid ABSOLUTE address "
1017 location
.segment
= abs_seg
;
1025 while (*p
&& !isspace(*p
)) {
1032 report_error (ERR_PANIC
,
1033 "identifier expected after DEBUG in pass 2");
1036 while (*p
&& isspace(*p
))
1038 ofmt
->current_dfmt
->debug_directive (debugid
, p
);
1041 if (!ofmt
->directive (line
+1, value
, 2))
1042 report_error (ERR_PANIC
, "invalid directive on pass two");
1046 else /* not a directive */
1048 parse_line (2, line
, &output_ins
,
1049 report_error
, evaluate
, redefine_label
);
1050 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1051 output_ins
.forw_ref
= TRUE
;
1053 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1054 forwref
= saa_rstruct (forwrefs
);
1055 } while (forwref
!= NULL
&& forwref
->lineno
== globallineno
);
1057 output_ins
.forw_ref
= FALSE
;
1060 * Hack to prevent phase error in the code
1064 * If the second operand is a forward reference,
1065 * the UNITY property of the number 1 in that
1066 * operand is cancelled. Otherwise the above
1067 * sequence will cause a phase error.
1069 * This hack means that the above code will
1070 * generate 286+ code.
1072 * The forward reference will mean that the
1073 * operand will not have the UNITY property on
1074 * the first pass, so the pass behaviours will
1078 if (output_ins
.forw_ref
&&
1079 output_ins
.operands
>= 2 &&
1080 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1082 output_ins
.oprs
[1].type
&= ~ONENESS
;
1085 if (output_ins
.opcode
== I_EQU
)
1088 * Special `..' EQUs get processed here, except
1089 * `..@' macro processor EQUs which are done above.
1091 if (output_ins
.label
[0] == '.' &&
1092 output_ins
.label
[1] == '.' &&
1093 output_ins
.label
[2] != '@')
1095 if (output_ins
.operands
== 1 &&
1096 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1097 define_label (output_ins
.label
,
1098 output_ins
.oprs
[0].segment
,
1099 output_ins
.oprs
[0].offset
,
1100 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1102 else if (output_ins
.operands
== 2 &&
1103 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1104 (output_ins
.oprs
[0].type
& COLON
) &&
1105 output_ins
.oprs
[0].segment
== NO_SEG
&&
1106 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1107 output_ins
.oprs
[1].segment
== NO_SEG
)
1109 define_label (output_ins
.label
,
1110 output_ins
.oprs
[0].offset
| SEG_ABS
,
1111 output_ins
.oprs
[1].offset
,
1112 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1115 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1118 offs
+= assemble (location
.segment
, offs
, sb
,
1119 &output_ins
, ofmt
, report_error
, &nasmlist
);
1120 cleanup_insn (&output_ins
);
1121 set_curr_ofs (offs
);
1126 location
.offset
= offs
= get_curr_ofs
;
1133 static int getkw (char *buf
, char **value
)
1142 while (*p
&& *p
!= ']') p
++;
1149 while (*p
&& *p
!= ';') {
1157 while (*buf
&& *buf
!=' ' && *buf
!=']' && *buf
!='\t')
1164 while (isspace(*buf
)) buf
++; /* beppu - skip leading whitespace */
1166 while (*buf
!=']') buf
++;
1171 if (!strcmp(p
, "segment") || !strcmp(p
, "section"))
1173 if (!strcmp(p
, "extern"))
1175 if (!strcmp(p
, "bits"))
1177 if (!strcmp(p
, "global"))
1179 if (!strcmp(p
, "common"))
1181 if (!strcmp(p
, "absolute"))
1183 if (!strcmp(p
, "debug"))
1188 static void report_error (int severity
, char *fmt
, ...)
1193 * See if it's a suppressed warning.
1195 if ((severity
& ERR_MASK
) == ERR_WARNING
&&
1196 (severity
& ERR_WARN_MASK
) != 0 &&
1197 suppressed
[ (severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
])
1198 return; /* and bail out if so */
1201 * See if it's a pass-one only warning and we're not in pass one.
1203 if ((severity
& ERR_PASS1
) && pass
!= 1)
1206 if (severity
& ERR_NOFILE
)
1207 fputs ("nasm: ", error_file
);
1209 char * currentfile
= NULL
;
1211 src_get (&lineno
, ¤tfile
);
1212 fprintf (error_file
, "%s:%ld: ", currentfile
, lineno
);
1213 nasm_free (currentfile
);
1216 if ( (severity
& ERR_MASK
) == ERR_WARNING
)
1217 fputs ("warning: ", error_file
);
1218 else if ( (severity
& ERR_MASK
) == ERR_PANIC
)
1219 fputs ("panic: ", error_file
);
1222 vfprintf (error_file
, fmt
, ap
);
1223 fputc ('\n', error_file
);
1225 if (severity
& ERR_USAGE
)
1228 switch (severity
& ERR_MASK
) {
1230 /* no further action, by definition */
1233 terminate_after_phase
= TRUE
;
1242 exit(1); /* instantly die */
1243 break; /* placate silly compilers */
1246 abort(); /* halt, catch fire, and dump core */
1251 static void usage(void)
1253 fputs("type `nasm -h' for help\n", error_file
);
1256 static void register_output_formats(void)
1258 ofmt
= ofmt_register (report_error
);
1261 #define BUF_DELTA 512
1263 static FILE *no_pp_fp
;
1264 static efunc no_pp_err
;
1265 static ListGen
*no_pp_list
;
1266 static long no_pp_lineinc
;
1268 static void no_pp_reset (char *file
, int pass
, efunc error
, evalfunc eval
,
1271 src_set_fname(nasm_strdup(file
));
1275 no_pp_fp
= fopen(file
, "r");
1277 no_pp_err (ERR_FATAL
| ERR_NOFILE
,
1278 "unable to open input file `%s'", file
);
1279 no_pp_list
= listgen
;
1280 (void) pass
; /* placate compilers */
1281 (void) eval
; /* placate compilers */
1284 static char *no_pp_getline (void)
1286 char *buffer
, *p
, *q
;
1289 bufsize
= BUF_DELTA
;
1290 buffer
= nasm_malloc(BUF_DELTA
);
1291 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1293 while (1) { /* Loop to handle %line */
1296 while (1) { /* Loop to handle long lines */
1297 q
= fgets(p
, bufsize
-(p
-buffer
), no_pp_fp
);
1301 if (p
> buffer
&& p
[-1] == '\n')
1303 if (p
-buffer
> bufsize
-10) {
1305 offset
= p
- buffer
;
1306 bufsize
+= BUF_DELTA
;
1307 buffer
= nasm_realloc(buffer
, bufsize
);
1308 p
= buffer
+ offset
;
1312 if (!q
&& p
== buffer
) {
1318 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1319 * them are present at the end of the line.
1321 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1323 if (!strncmp(buffer
, "%line", 5)) {
1326 char *nm
= nasm_malloc(strlen(buffer
));
1327 if (sscanf(buffer
+5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1328 nasm_free( src_set_fname(nm
) );
1338 no_pp_list
->line (LIST_READ
, buffer
);
1343 static void no_pp_cleanup (void)