1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2011 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * The Netwide Assembler main program module
61 #include "output/outform.h"
65 * This is the maximum number of optimization passes to do. If we ever
66 * find a case where the optimizer doesn't naturally converge, we might
67 * have to drop this value so the assembler doesn't appear to just hang.
69 #define MAX_OPTIMIZE (INT_MAX >> 1)
71 struct forwrefinfo
{ /* info held on forward refs. */
76 static int get_bits(char *value
);
77 static uint32_t get_cpu(char *cpu_str
);
78 static void parse_cmdline(int, char **);
79 static void assemble_file(char *, StrList
**);
80 static void nasm_verror_gnu(int severity
, const char *fmt
, va_list args
);
81 static void nasm_verror_vc(int severity
, const char *fmt
, va_list args
);
82 static void nasm_verror_common(int severity
, const char *fmt
, va_list args
);
83 static bool is_suppressed_warning(int severity
);
84 static void usage(void);
86 static int using_debug_info
, opt_verbose_info
;
87 bool tasm_compatible_mode
= false;
92 static time_t official_compile_time
;
94 static char inname
[FILENAME_MAX
];
95 static char outname
[FILENAME_MAX
];
96 static char listname
[FILENAME_MAX
];
97 static char errname
[FILENAME_MAX
];
98 static int globallineno
; /* for forward-reference tracking */
99 /* static int pass = 0; */
100 struct ofmt
*ofmt
= &OF_DEFAULT
;
101 struct ofmt_alias
*ofmt_alias
= NULL
;
102 const struct dfmt
*dfmt
;
104 static FILE *error_file
; /* Where to write error messages */
107 int optimizing
= MAX_OPTIMIZE
; /* number of optimization passes to take */
108 static int sb
, cmd_sb
= 16; /* by default */
109 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
110 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
111 int64_t global_offset_changed
; /* referenced in labels.c */
112 int64_t prev_offset_changed
;
115 static struct location location
;
116 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
117 int32_t abs_seg
; /* ABSOLUTE segment basis */
118 int32_t abs_offset
; /* ABSOLUTE offset */
120 static struct RAA
*offsets
;
122 static struct SAA
*forwrefs
; /* keep track of forward references */
123 static const struct forwrefinfo
*forwref
;
125 static struct preproc_ops
*preproc
;
128 op_normal
, /* Preprocess and assemble */
129 op_preprocess
, /* Preprocess only */
130 op_depend
, /* Generate dependencies */
132 static enum op_type operating_mode
;
133 /* Dependency flags */
134 static bool depend_emit_phony
= false;
135 static bool depend_missing_ok
= false;
136 static const char *depend_target
= NULL
;
137 static const char *depend_file
= NULL
;
140 * Which of the suppressible warnings are suppressed. Entry zero
141 * isn't an actual warning, but it used for -w+error/-Werror.
144 static bool warning_on
[ERR_WARN_MAX
+1]; /* Current state */
145 static bool warning_on_global
[ERR_WARN_MAX
+1]; /* Command-line state */
147 static const struct warning
{
151 } warnings
[ERR_WARN_MAX
+1] = {
152 {"error", "treat warnings as errors", false},
153 {"macro-params", "macro calls with wrong parameter count", true},
154 {"macro-selfref", "cyclic macro references", false},
155 {"macro-defaults", "macros with more default than optional parameters", true},
156 {"orphan-labels", "labels alone on lines without trailing `:'", true},
157 {"number-overflow", "numeric constant does not fit", true},
158 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
159 {"float-overflow", "floating point overflow", true},
160 {"float-denorm", "floating point denormal", false},
161 {"float-underflow", "floating point underflow", false},
162 {"float-toolong", "too many digits in floating-point number", true},
163 {"user", "%warning directives", true},
167 * This is a null preprocessor which just copies lines from input
168 * to output. It's used when someone explicitly requests that NASM
169 * not preprocess their source file.
172 static void no_pp_reset(char *file
, int pass
, ListGen
*listgen
, StrList
**deplist
);
173 static char *no_pp_getline(void);
174 static void no_pp_cleanup(int pass
);
176 static struct preproc_ops no_pp
= {
183 * get/set current offset...
185 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
186 raa_read(offsets,location.segment))
187 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
188 (void)(offsets=raa_write(offsets,location.segment,(x))))
190 static bool want_usage
;
191 static bool terminate_after_phase
;
192 int user_nolist
= 0; /* fbk 9/2/00 */
194 static void nasm_fputs(const char *line
, FILE * outfile
)
197 fputs(line
, outfile
);
203 /* Convert a struct tm to a POSIX-style time constant */
204 static int64_t posix_mktime(struct tm
*tm
)
207 int64_t y
= tm
->tm_year
;
209 /* See IEEE 1003.1:2004, section 4.14 */
211 t
= (y
-70)*365 + (y
-69)/4 - (y
-1)/100 + (y
+299)/400;
223 static void define_macros_early(void)
226 struct tm lt
, *lt_p
, gm
, *gm_p
;
229 lt_p
= localtime(&official_compile_time
);
233 strftime(temp
, sizeof temp
, "__DATE__=\"%Y-%m-%d\"", <
);
235 strftime(temp
, sizeof temp
, "__DATE_NUM__=%Y%m%d", <
);
237 strftime(temp
, sizeof temp
, "__TIME__=\"%H:%M:%S\"", <
);
239 strftime(temp
, sizeof temp
, "__TIME_NUM__=%H%M%S", <
);
243 gm_p
= gmtime(&official_compile_time
);
247 strftime(temp
, sizeof temp
, "__UTC_DATE__=\"%Y-%m-%d\"", &gm
);
249 strftime(temp
, sizeof temp
, "__UTC_DATE_NUM__=%Y%m%d", &gm
);
251 strftime(temp
, sizeof temp
, "__UTC_TIME__=\"%H:%M:%S\"", &gm
);
253 strftime(temp
, sizeof temp
, "__UTC_TIME_NUM__=%H%M%S", &gm
);
258 posix_time
= posix_mktime(&gm
);
260 posix_time
= posix_mktime(<
);
265 snprintf(temp
, sizeof temp
, "__POSIX_TIME__=%"PRId64
, posix_time
);
270 static void define_macros_late(void)
275 * In case if output format is defined by alias
276 * we have to put shortname of the alias itself here
277 * otherwise ABI backward compatibility gets broken.
279 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s",
280 ofmt_alias
? ofmt_alias
->shortname
: ofmt
->shortname
);
284 static void emit_dependencies(StrList
*list
)
290 if (depend_file
&& strcmp(depend_file
, "-")) {
291 deps
= fopen(depend_file
, "w");
293 nasm_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
294 "unable to write dependency file `%s'", depend_file
);
301 linepos
= fprintf(deps
, "%s:", depend_target
);
302 list_for_each(l
, list
) {
303 len
= strlen(l
->str
);
304 if (linepos
+ len
> 62) {
305 fprintf(deps
, " \\\n ");
308 fprintf(deps
, " %s", l
->str
);
311 fprintf(deps
, "\n\n");
313 list_for_each_safe(l
, nl
, list
) {
314 if (depend_emit_phony
)
315 fprintf(deps
, "%s:\n\n", l
->str
);
323 int main(int argc
, char **argv
)
325 StrList
*depend_list
= NULL
, **depend_ptr
;
327 time(&official_compile_time
);
330 want_usage
= terminate_after_phase
= false;
331 nasm_set_verror(nasm_verror_gnu
);
337 nasm_init_malloc_error();
338 offsets
= raa_init();
339 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
342 operating_mode
= op_normal
;
346 /* Define some macros dependent on the runtime, but not
347 on the command line. */
348 define_macros_early();
350 parse_cmdline(argc
, argv
);
352 if (terminate_after_phase
) {
358 /* If debugging info is disabled, suppress any debug calls */
359 if (!using_debug_info
)
360 ofmt
->current_dfmt
= &null_debug_form
;
363 pp_extra_stdmac(ofmt
->stdmac
);
364 parser_global_info(&location
);
365 eval_global_info(ofmt
, lookup_label
, &location
);
367 /* define some macros dependent of command-line */
368 define_macros_late();
370 depend_ptr
= (depend_file
|| (operating_mode
== op_depend
))
371 ? &depend_list
: NULL
;
373 depend_target
= outname
;
375 switch (operating_mode
) {
380 if (depend_missing_ok
)
381 pp_include_path(NULL
); /* "assume generated" */
383 preproc
->reset(inname
, 0, &nasmlist
, depend_ptr
);
384 if (outname
[0] == '\0')
385 ofmt
->filename(inname
, outname
);
387 while ((line
= preproc
->getline()))
396 char *file_name
= NULL
;
397 int32_t prior_linnum
= 0;
401 ofile
= fopen(outname
, "w");
403 nasm_error(ERR_FATAL
| ERR_NOFILE
,
404 "unable to open output file `%s'",
409 location
.known
= false;
412 preproc
->reset(inname
, 3, &nasmlist
, depend_ptr
);
414 while ((line
= preproc
->getline())) {
416 * We generate %line directives if needed for later programs
418 int32_t linnum
= prior_linnum
+= lineinc
;
419 int altline
= src_get(&linnum
, &file_name
);
421 if (altline
== 1 && lineinc
== 1)
422 nasm_fputs("", ofile
);
424 lineinc
= (altline
!= -1 || lineinc
!= 1);
425 fprintf(ofile
? ofile
: stdout
,
426 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
429 prior_linnum
= linnum
;
431 nasm_fputs(line
, ofile
);
434 nasm_free(file_name
);
438 if (ofile
&& terminate_after_phase
)
447 * We must call ofmt->filename _anyway_, even if the user
448 * has specified their own output file, because some
449 * formats (eg OBJ and COFF) use ofmt->filename to find out
450 * the name of the input file and then put that inside the
453 ofmt
->filename(inname
, outname
);
455 ofile
= fopen(outname
, (ofmt
->flags
& OFMT_TEXT
) ? "w" : "wb");
457 nasm_error(ERR_FATAL
| ERR_NOFILE
,
458 "unable to open output file `%s'", outname
);
462 * We must call init_labels() before ofmt->init() since
463 * some object formats will want to define labels in their
464 * init routines. (eg OS/2 defines the FLAT group)
469 dfmt
= ofmt
->current_dfmt
;
472 assemble_file(inname
, depend_ptr
);
474 if (!terminate_after_phase
) {
475 ofmt
->cleanup(using_debug_info
);
479 nasm_error(ERR_NONFATAL
|ERR_NOFILE
,
480 "write error on output file `%s'", outname
);
486 if (terminate_after_phase
)
494 if (depend_list
&& !terminate_after_phase
)
495 emit_dependencies(depend_list
);
505 return terminate_after_phase
;
509 * Get a parameter for a command line option.
510 * First arg must be in the form of e.g. -f...
512 static char *get_param(char *p
, char *q
, bool *advance
)
515 if (p
[2]) /* the parameter's in the option */
516 return nasm_skip_spaces(p
+ 2);
521 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
522 "option `-%c' requires an argument", p
[1]);
529 static void copy_filename(char *dst
, const char *src
)
531 size_t len
= strlen(src
);
533 if (len
>= (size_t)FILENAME_MAX
) {
534 nasm_error(ERR_FATAL
| ERR_NOFILE
, "file name too long");
537 strncpy(dst
, src
, FILENAME_MAX
);
541 * Convert a string to Make-safe form
543 static char *quote_for_make(const char *str
)
548 size_t n
= 1; /* Terminating zero */
554 for (p
= str
; *p
; p
++) {
558 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
578 /* Convert N backslashes at the end of filename to 2N backslashes */
582 os
= q
= nasm_malloc(n
);
585 for (p
= str
; *p
; p
++) {
628 #define OPT_POSTFIX 1
629 struct textargs textopts
[] = {
630 {"prefix", OPT_PREFIX
},
631 {"postfix", OPT_POSTFIX
},
635 static bool stopoptions
= false;
636 static bool process_arg(char *p
, char *q
)
640 bool advance
= false;
646 if (p
[0] == '-' && !stopoptions
) {
647 if (strchr("oOfpPdDiIlFXuUZwW", p
[1])) {
648 /* These parameters take values */
649 if (!(param
= get_param(p
, q
, &advance
)))
658 case 'o': /* output file */
659 copy_filename(outname
, param
);
662 case 'f': /* output format */
663 ofmt
= ofmt_find(param
, &ofmt_alias
);
665 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
666 "unrecognised output format `%s' - "
667 "use -hf for a list", param
);
671 case 'O': /* Optimization level */
676 /* Naked -O == -Ox */
677 optimizing
= MAX_OPTIMIZE
;
681 case '0': case '1': case '2': case '3': case '4':
682 case '5': case '6': case '7': case '8': case '9':
683 opt
= strtoul(param
, ¶m
, 10);
685 /* -O0 -> optimizing == -1, 0.98 behaviour */
686 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
688 optimizing
= opt
- 1;
696 opt_verbose_info
= true;
701 optimizing
= MAX_OPTIMIZE
;
705 nasm_error(ERR_FATAL
,
706 "unknown optimization option -O%c\n",
711 if (optimizing
> MAX_OPTIMIZE
)
712 optimizing
= MAX_OPTIMIZE
;
717 case 'p': /* pre-include */
719 pp_pre_include(param
);
722 case 'd': /* pre-define */
724 pp_pre_define(param
);
727 case 'u': /* un-define */
729 pp_pre_undefine(param
);
732 case 'i': /* include search path */
734 pp_include_path(param
);
737 case 'l': /* listing file */
738 copy_filename(listname
, param
);
741 case 'Z': /* error messages file */
742 copy_filename(errname
, param
);
745 case 'F': /* specify debug format */
746 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
747 if (!ofmt
->current_dfmt
) {
748 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
749 "unrecognized debug format `%s' for"
750 " output format `%s'",
751 param
, ofmt
->shortname
);
753 using_debug_info
= true;
756 case 'X': /* specify error reporting format */
757 if (nasm_stricmp("vc", param
) == 0)
758 nasm_set_verror(nasm_verror_vc
);
759 else if (nasm_stricmp("gnu", param
) == 0)
760 nasm_set_verror(nasm_verror_gnu
);
762 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
763 "unrecognized error reporting format `%s'",
768 using_debug_info
= true;
773 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
775 " [options...] [--] filename\n"
776 " or nasm -v for version info\n\n"
777 " -t assemble in SciTech TASM compatible mode\n"
778 " -g generate debug information in selected format\n");
780 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
781 " -a don't preprocess (assemble only)\n"
782 " -M generate Makefile dependencies on stdout\n"
783 " -MG d:o, missing files assumed generated\n"
784 " -MF <file> set Makefile dependency file\n"
785 " -MD <file> assemble and generate dependencies\n"
786 " -MT <file> dependency target name\n"
787 " -MQ <file> dependency target name (quoted)\n"
788 " -MP emit phony target\n\n"
789 " -Z<file> redirect error messages to file\n"
790 " -s redirect error messages to stdout\n\n"
791 " -F format select a debugging format\n\n"
792 " -I<path> adds a pathname to the include file path\n");
794 (" -O<digit> optimize branch offsets\n"
795 " -O0: No optimization (default)\n"
796 " -O1: Minimal optimization\n"
797 " -Ox: Multipass optimization (recommended)\n\n"
798 " -P<file> pre-includes a file\n"
799 " -D<macro>[=<value>] pre-defines a macro\n"
800 " -U<macro> undefines a macro\n"
801 " -X<format> specifies error reporting format (gnu or vc)\n"
802 " -w+foo enables warning foo (equiv. -Wfoo)\n"
803 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
804 "--prefix,--postfix\n"
805 " this options prepend or append the given argument to all\n"
806 " extern and global variables\n\n"
808 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
809 printf(" %-23s %s (default %s)\n",
810 warnings
[i
].name
, warnings
[i
].help
,
811 warnings
[i
].enabled
? "on" : "off");
813 ("\nresponse files should contain command line parameters"
814 ", one per line.\n");
816 printf("\nvalid output formats for -f are"
817 " (`*' denotes default):\n");
818 ofmt_list(ofmt
, stdout
);
820 printf("\nFor a list of valid output formats, use -hf.\n");
821 printf("For a list of debug formats, use -f <form> -y.\n");
823 exit(0); /* never need usage message here */
827 printf("\nvalid debug formats for '%s' output format are"
828 " ('*' denotes default):\n", ofmt
->shortname
);
829 dfmt_list(ofmt
, stdout
);
834 tasm_compatible_mode
= true;
838 printf("NASM version %s compiled on %s%s\n",
839 nasm_version
, nasm_date
, nasm_compile_options
);
840 exit(0); /* never need usage message here */
843 case 'e': /* preprocess only */
845 operating_mode
= op_preprocess
;
848 case 'a': /* assemble only - don't preprocess */
853 if (param
[0] == 'n' && param
[1] == 'o' && param
[2] == '-') {
862 if (param
[0] != '+' && param
[0] != '-') {
863 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
864 "invalid option to `-w'");
867 do_warn
= (param
[0] == '+');
871 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
872 if (!nasm_stricmp(param
, warnings
[i
].name
))
874 if (i
<= ERR_WARN_MAX
)
875 warning_on_global
[i
] = do_warn
;
876 else if (!nasm_stricmp(param
, "all"))
877 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
878 warning_on_global
[i
] = do_warn
;
879 else if (!nasm_stricmp(param
, "none"))
880 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
881 warning_on_global
[i
] = !do_warn
;
883 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
884 "invalid warning `%s'", param
);
890 operating_mode
= op_depend
;
893 operating_mode
= op_depend
;
894 depend_missing_ok
= true;
897 depend_emit_phony
= true;
908 depend_target
= quote_for_make(q
);
912 nasm_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
913 "unknown dependency option `-M%c'", p
[2]);
916 if (advance
&& (!q
|| !q
[0])) {
917 nasm_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
918 "option `-M%c' requires a parameter", p
[2]);
927 if (p
[2] == 0) { /* -- => stop processing options */
931 for (s
= 0; textopts
[s
].label
; s
++) {
932 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
943 nasm_error(ERR_NONFATAL
| ERR_NOFILE
|
945 "option `--%s' requires an argument",
949 advance
= 1, param
= q
;
952 if (s
== OPT_PREFIX
) {
953 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
954 lprefix
[PREFIX_MAX
- 1] = 0;
957 if (s
== OPT_POSTFIX
) {
958 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
959 lpostfix
[POSTFIX_MAX
- 1] = 0;
966 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
967 "unrecognised option `--%s'", p
+ 2);
975 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
976 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
977 "unrecognised option `-%c'", p
[1]);
982 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
983 "more than one input file specified");
985 copy_filename(inname
, p
);
992 #define ARG_BUF_DELTA 128
994 static void process_respfile(FILE * rfile
)
996 char *buffer
, *p
, *q
, *prevarg
;
997 int bufsize
, prevargsize
;
999 bufsize
= prevargsize
= ARG_BUF_DELTA
;
1000 buffer
= nasm_malloc(ARG_BUF_DELTA
);
1001 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
1004 while (1) { /* Loop to handle all lines in file */
1006 while (1) { /* Loop to handle long lines */
1007 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
1011 if (p
> buffer
&& p
[-1] == '\n')
1013 if (p
- buffer
> bufsize
- 10) {
1015 offset
= p
- buffer
;
1016 bufsize
+= ARG_BUF_DELTA
;
1017 buffer
= nasm_realloc(buffer
, bufsize
);
1018 p
= buffer
+ offset
;
1022 if (!q
&& p
== buffer
) {
1024 process_arg(prevarg
, NULL
);
1031 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1032 * them are present at the end of the line.
1034 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
1036 while (p
> buffer
&& nasm_isspace(p
[-1]))
1039 p
= nasm_skip_spaces(buffer
);
1041 if (process_arg(prevarg
, p
))
1044 if ((int) strlen(p
) > prevargsize
- 10) {
1045 prevargsize
+= ARG_BUF_DELTA
;
1046 prevarg
= nasm_realloc(prevarg
, prevargsize
);
1048 strncpy(prevarg
, p
, prevargsize
);
1052 /* Function to process args from a string of args, rather than the
1053 * argv array. Used by the environment variable and response file
1056 static void process_args(char *args
)
1058 char *p
, *q
, *arg
, *prevarg
;
1059 char separator
= ' ';
1062 if (*p
&& *p
!= '-')
1067 while (*p
&& *p
!= separator
)
1069 while (*p
== separator
)
1073 if (process_arg(prevarg
, arg
))
1077 process_arg(arg
, NULL
);
1080 static void process_response_file(const char *file
)
1083 FILE *f
= fopen(file
, "r");
1088 while (fgets(str
, sizeof str
, f
)) {
1094 static void parse_cmdline(int argc
, char **argv
)
1097 char *envreal
, *envcopy
= NULL
, *p
;
1100 *inname
= *outname
= *listname
= *errname
= '\0';
1101 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
1102 warning_on_global
[i
] = warnings
[i
].enabled
;
1105 * First, process the NASMENV environment variable.
1107 envreal
= getenv("NASMENV");
1109 envcopy
= nasm_strdup(envreal
);
1110 process_args(envcopy
);
1115 * Now process the actual command line.
1120 if (argv
[0][0] == '@') {
1121 /* We have a response file, so process this as a set of
1122 * arguments like the environment variable. This allows us
1123 * to have multiple arguments on a single line, which is
1124 * different to the -@resp file processing below for regular
1127 process_response_file(argv
[0]+1);
1131 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
1132 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &advance
);
1134 rfile
= fopen(p
, "r");
1136 process_respfile(rfile
);
1139 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
1140 "unable to open response file `%s'", p
);
1143 advance
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
1144 argv
+= advance
, argc
-= advance
;
1147 /* Look for basic command line typos. This definitely doesn't
1148 catch all errors, but it might help cases of fumbled fingers. */
1150 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
1151 "no input file specified");
1152 else if (!strcmp(inname
, errname
) ||
1153 !strcmp(inname
, outname
) ||
1154 !strcmp(inname
, listname
) ||
1155 (depend_file
&& !strcmp(inname
, depend_file
)))
1156 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
1157 "file `%s' is both input and output file",
1161 error_file
= fopen(errname
, "w");
1163 error_file
= stderr
; /* Revert to default! */
1164 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
1165 "cannot open file `%s' for error messages",
1171 static enum directives
getkw(char **directive
, char **value
);
1173 static void assemble_file(char *fname
, StrList
**depend_ptr
)
1175 char *directive
, *value
, *p
, *q
, *special
, *line
;
1181 struct tokenval tokval
;
1185 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
1186 nasm_error(ERR_FATAL
, "command line: "
1187 "32-bit segment size requires a higher cpu");
1189 pass_max
= prev_offset_changed
= (INT_MAX
>> 1) + 2; /* Almost unlimited */
1190 for (passn
= 1; pass0
<= 2; passn
++) {
1194 pass1
= pass0
== 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
1195 pass2
= passn
> 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1196 /* pass0 0, 0, 0, ..., 1, 2 */
1198 def_label
= passn
> 1 ? redefine_label
: define_label
;
1200 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
1204 nasmlist
.init(listname
, nasm_error
);
1207 global_offset_changed
= 0; /* set by redefine_label */
1208 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
1211 saa_rewind(forwrefs
);
1212 forwref
= saa_rstruct(forwrefs
);
1214 offsets
= raa_init();
1216 preproc
->reset(fname
, pass1
, &nasmlist
,
1217 pass1
== 2 ? depend_ptr
: NULL
);
1218 memcpy(warning_on
, warning_on_global
, (ERR_WARN_MAX
+1) * sizeof(bool));
1222 location
.known
= true;
1223 location
.offset
= offs
= GET_CURR_OFFS
;
1225 while ((line
= preproc
->getline())) {
1230 * Here we parse our directives; this is not handled by the
1231 * 'real' parser. This really should be a separate function.
1234 d
= getkw(&directive
, &value
);
1239 case D_SEGMENT
: /* [SEGMENT n] */
1241 seg
= ofmt
->section(value
, pass2
, &sb
);
1242 if (seg
== NO_SEG
) {
1243 nasm_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1244 "segment name `%s' not recognized",
1248 location
.segment
= seg
;
1251 case D_SECTALIGN
: /* [SECTALIGN n] */
1255 tokval
.t_type
= TOKEN_INVALID
;
1256 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, nasm_error
, NULL
);
1258 unsigned int align
= (unsigned int)e
->value
;
1259 if ((uint64_t)e
->value
> 0x7fffffff) {
1261 * FIXME: Please make some sane message here
1262 * ofmt should have some 'check' method which
1263 * would report segment alignment bounds.
1265 nasm_error(ERR_FATAL
,
1266 "incorrect segment alignment `%s'", value
);
1267 } else if (!is_power2(align
)) {
1268 nasm_error(ERR_NONFATAL
,
1269 "segment alignment `%s' is not power of two",
1272 /* callee should be able to handle all details */
1273 ofmt
->sectalign(location
.segment
, align
);
1277 case D_EXTERN
: /* [EXTERN label:special] */
1279 value
++; /* skip initial $ if present */
1282 while (*q
&& *q
!= ':')
1286 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1288 } else if (passn
== 1) {
1293 while (*q
&& *q
!= ':') {
1299 nasm_error(ERR_NONFATAL
,
1300 "identifier expected after EXTERN");
1308 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
1310 pass0
= 1; /* fake pass 1 in labels.c */
1311 declare_as_global(value
, special
);
1312 define_label(value
, seg_alloc(), 0L, NULL
,
1316 } /* else pass0 == 1 */
1318 case D_BITS
: /* [BITS bits] */
1319 globalbits
= sb
= get_bits(value
);
1321 case D_GLOBAL
: /* [GLOBAL symbol:special] */
1323 value
++; /* skip initial $ if present */
1324 if (pass0
== 2) { /* pass 2 */
1326 while (*q
&& *q
!= ':')
1330 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1332 } else if (pass2
== 1) { /* pass == 1 */
1337 while (*q
&& *q
!= ':') {
1343 nasm_error(ERR_NONFATAL
,
1344 "identifier expected after GLOBAL");
1352 declare_as_global(value
, special
);
1355 case D_COMMON
: /* [COMMON symbol size:special] */
1360 value
++; /* skip initial $ if present */
1365 while (*p
&& !nasm_isspace(*p
)) {
1371 nasm_error(ERR_NONFATAL
,
1372 "identifier expected after COMMON");
1376 p
= nasm_zap_spaces_fwd(p
);
1378 while (*q
&& *q
!= ':')
1386 size
= readnum(p
, &rn_error
);
1388 nasm_error(ERR_NONFATAL
,
1389 "invalid size specified"
1390 " in COMMON declaration");
1394 nasm_error(ERR_NONFATAL
,
1395 "no size specified in"
1396 " COMMON declaration");
1401 define_common(value
, seg_alloc(), size
, special
);
1402 } else if (pass0
== 2) {
1404 ofmt
->symdef(value
, 0L, 0L, 3, special
);
1408 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1411 tokval
.t_type
= TOKEN_INVALID
;
1412 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1417 1 ? ERR_NONFATAL
: ERR_PANIC
,
1418 "cannot use non-relocatable expression as "
1419 "ABSOLUTE address");
1421 abs_seg
= reloc_seg(e
);
1422 abs_offset
= reloc_value(e
);
1424 } else if (passn
== 1)
1425 abs_offset
= 0x100; /* don't go near zero in case of / */
1427 nasm_error(ERR_PANIC
, "invalid ABSOLUTE address "
1430 location
.segment
= NO_SEG
;
1432 case D_DEBUG
: /* [DEBUG] */
1435 bool badid
, overlong
;
1439 badid
= overlong
= false;
1440 if (!isidstart(*p
)) {
1443 while (*p
&& !nasm_isspace(*p
)) {
1444 if (q
>= debugid
+ sizeof debugid
- 1) {
1455 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1456 "identifier expected after DEBUG");
1460 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1461 "DEBUG identifier too long");
1464 p
= nasm_skip_spaces(p
);
1466 dfmt
->debug_directive(debugid
, p
);
1469 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
1470 value
= nasm_skip_spaces(value
);
1472 case '-': validid
= 0; value
++; break;
1473 case '+': validid
= 1; value
++; break;
1474 case '*': validid
= 2; value
++; break;
1475 default: validid
= 1; break;
1478 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1479 if (!nasm_stricmp(value
, warnings
[i
].name
))
1481 if (i
<= ERR_WARN_MAX
) {
1484 warning_on
[i
] = false;
1487 warning_on
[i
] = true;
1490 warning_on
[i
] = warning_on_global
[i
];
1495 nasm_error(ERR_NONFATAL
,
1496 "invalid warning id in WARNING directive");
1498 case D_CPU
: /* [CPU] */
1499 cpu
= get_cpu(value
);
1501 case D_LIST
: /* [LIST {+|-}] */
1502 value
= nasm_skip_spaces(value
);
1503 if (*value
== '+') {
1506 if (*value
== '-') {
1513 case D_DEFAULT
: /* [DEFAULT] */
1516 tokval
.t_type
= TOKEN_INVALID
;
1517 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1518 switch ((int)tokval
.t_integer
) {
1534 if (float_option(value
)) {
1535 nasm_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1536 "unknown 'float' directive: %s",
1541 if (ofmt
->directive(d
, value
, pass2
))
1543 /* else fall through */
1545 nasm_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1546 "unrecognised directive [%s]",
1551 nasm_error(ERR_NONFATAL
,
1552 "invalid parameter to [%s] directive",
1555 } else { /* it isn't a directive */
1556 parse_line(pass1
, line
, &output_ins
, def_label
);
1558 if (optimizing
> 0) {
1559 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1560 output_ins
.forw_ref
= true;
1562 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1563 forwref
= saa_rstruct(forwrefs
);
1564 } while (forwref
!= NULL
1565 && forwref
->lineno
== globallineno
);
1567 output_ins
.forw_ref
= false;
1569 if (output_ins
.forw_ref
) {
1571 for (i
= 0; i
< output_ins
.operands
; i
++) {
1572 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
) {
1573 struct forwrefinfo
*fwinf
=
1574 (struct forwrefinfo
*)
1575 saa_wstruct(forwrefs
);
1576 fwinf
->lineno
= globallineno
;
1585 if (output_ins
.opcode
== I_EQU
) {
1588 * Special `..' EQUs get processed in pass two,
1589 * except `..@' macro-processor EQUs which are done
1590 * in the normal place.
1592 if (!output_ins
.label
)
1593 nasm_error(ERR_NONFATAL
,
1594 "EQU not preceded by label");
1596 else if (output_ins
.label
[0] != '.' ||
1597 output_ins
.label
[1] != '.' ||
1598 output_ins
.label
[2] == '@') {
1599 if (output_ins
.operands
== 1 &&
1600 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1601 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1602 bool isext
= !!(output_ins
.oprs
[0].opflags
1604 def_label(output_ins
.label
,
1605 output_ins
.oprs
[0].segment
,
1606 output_ins
.oprs
[0].offset
, NULL
,
1608 } else if (output_ins
.operands
== 2
1609 && (output_ins
.oprs
[0].type
& IMMEDIATE
)
1610 && (output_ins
.oprs
[0].type
& COLON
)
1611 && output_ins
.oprs
[0].segment
== NO_SEG
1612 && output_ins
.oprs
[0].wrt
== NO_SEG
1613 && (output_ins
.oprs
[1].type
& IMMEDIATE
)
1614 && output_ins
.oprs
[1].segment
== NO_SEG
1615 && output_ins
.oprs
[1].wrt
== NO_SEG
) {
1616 def_label(output_ins
.label
,
1617 output_ins
.oprs
[0].offset
| SEG_ABS
,
1618 output_ins
.oprs
[1].offset
,
1619 NULL
, false, false);
1621 nasm_error(ERR_NONFATAL
,
1622 "bad syntax for EQU");
1626 * Special `..' EQUs get processed here, except
1627 * `..@' macro processor EQUs which are done above.
1629 if (output_ins
.label
[0] == '.' &&
1630 output_ins
.label
[1] == '.' &&
1631 output_ins
.label
[2] != '@') {
1632 if (output_ins
.operands
== 1 &&
1633 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1634 define_label(output_ins
.label
,
1635 output_ins
.oprs
[0].segment
,
1636 output_ins
.oprs
[0].offset
,
1637 NULL
, false, false);
1638 } else if (output_ins
.operands
== 2
1639 && (output_ins
.oprs
[0].type
& IMMEDIATE
)
1640 && (output_ins
.oprs
[0].type
& COLON
)
1641 && output_ins
.oprs
[0].segment
== NO_SEG
1642 && (output_ins
.oprs
[1].type
& IMMEDIATE
)
1643 && output_ins
.oprs
[1].segment
== NO_SEG
) {
1644 define_label(output_ins
.label
,
1645 output_ins
.oprs
[0].offset
| SEG_ABS
,
1646 output_ins
.oprs
[1].offset
,
1647 NULL
, false, false);
1649 nasm_error(ERR_NONFATAL
,
1650 "bad syntax for EQU");
1653 } else { /* instruction isn't an EQU */
1657 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1658 &output_ins
, nasm_error
);
1660 /* if (using_debug_info) && output_ins.opcode != -1) */
1661 if (using_debug_info
)
1662 { /* fbk 03/25/01 */
1663 /* this is done here so we can do debug type info */
1665 TYS_ELEMENTS(output_ins
.operands
);
1666 switch (output_ins
.opcode
) {
1669 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
1673 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
1677 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
1681 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
1685 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
1689 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_OWORD
;
1693 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_YWORD
;
1696 typeinfo
|= TY_BYTE
;
1699 typeinfo
|= TY_WORD
;
1702 if (output_ins
.eops_float
)
1703 typeinfo
|= TY_FLOAT
;
1705 typeinfo
|= TY_DWORD
;
1708 typeinfo
|= TY_QWORD
;
1711 typeinfo
|= TY_TBYTE
;
1714 typeinfo
|= TY_OWORD
;
1717 typeinfo
|= TY_YWORD
;
1720 typeinfo
= TY_LABEL
;
1724 dfmt
->debug_typevalue(typeinfo
);
1728 SET_CURR_OFFS(offs
);
1731 * else l == -1 => invalid instruction, which will be
1732 * flagged as an error on pass 2
1736 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1737 &output_ins
, ofmt
, nasm_error
,
1739 SET_CURR_OFFS(offs
);
1743 cleanup_insn(&output_ins
);
1746 location
.offset
= offs
= GET_CURR_OFFS
;
1747 } /* end while (line = preproc->getline... */
1749 if (pass0
== 2 && global_offset_changed
&& !terminate_after_phase
)
1750 nasm_error(ERR_NONFATAL
,
1751 "phase error detected at end of assembly.");
1754 preproc
->cleanup(1);
1756 if ((passn
> 1 && !global_offset_changed
) || pass0
== 2) {
1758 } else if (global_offset_changed
&&
1759 global_offset_changed
< prev_offset_changed
) {
1760 prev_offset_changed
= global_offset_changed
;
1766 if (terminate_after_phase
)
1769 if ((stall_count
> 997) || (passn
>= pass_max
)) {
1770 /* We get here if the labels don't converge
1771 * Example: FOO equ FOO + 1
1773 nasm_error(ERR_NONFATAL
,
1774 "Can't find valid values for all labels "
1775 "after %d passes, giving up.", passn
);
1776 nasm_error(ERR_NONFATAL
,
1777 "Possible causes: recursive EQUs, macro abuse.");
1782 preproc
->cleanup(0);
1784 if (!terminate_after_phase
&& opt_verbose_info
) {
1785 /* -On and -Ov switches */
1786 fprintf(stdout
, "info: assembly required 1+%d+1 passes\n", passn
-3);
1790 static enum directives
getkw(char **directive
, char **value
)
1794 buf
= nasm_skip_spaces(*directive
);
1796 /* it should be enclosed in [ ] */
1799 q
= strchr(buf
, ']');
1803 /* stip off the comments */
1804 p
= strchr(buf
, ';');
1806 if (p
< q
) /* ouch! somwhere inside */
1811 /* no brace, no trailing spaces */
1813 nasm_zap_spaces_rev(--q
);
1816 p
= nasm_skip_spaces(++buf
);
1817 q
= nasm_skip_word(p
);
1819 return D_none
; /* sigh... no value there */
1823 /* and value finally */
1824 p
= nasm_skip_spaces(++q
);
1827 return find_directive(*directive
);
1831 * gnu style error reporting
1832 * This function prints an error message to error_file in the
1833 * style used by GNU. An example would be:
1834 * file.asm:50: error: blah blah blah
1835 * where file.asm is the name of the file, 50 is the line number on
1836 * which the error occurs (or is detected) and "error:" is one of
1837 * the possible optional diagnostics -- it can be "error" or "warning"
1838 * or something else. Finally the line terminates with the actual
1841 * @param severity the severity of the warning or error
1842 * @param fmt the printf style format string
1844 static void nasm_verror_gnu(int severity
, const char *fmt
, va_list ap
)
1846 char *currentfile
= NULL
;
1849 if (is_suppressed_warning(severity
))
1852 if (!(severity
& ERR_NOFILE
))
1853 src_get(&lineno
, ¤tfile
);
1856 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1857 nasm_free(currentfile
);
1859 fputs("nasm: ", error_file
);
1862 nasm_verror_common(severity
, fmt
, ap
);
1866 * MS style error reporting
1867 * This function prints an error message to error_file in the
1868 * style used by Visual C and some other Microsoft tools. An example
1870 * file.asm(50) : error: blah blah blah
1871 * where file.asm is the name of the file, 50 is the line number on
1872 * which the error occurs (or is detected) and "error:" is one of
1873 * the possible optional diagnostics -- it can be "error" or "warning"
1874 * or something else. Finally the line terminates with the actual
1877 * @param severity the severity of the warning or error
1878 * @param fmt the printf style format string
1880 static void nasm_verror_vc(int severity
, const char *fmt
, va_list ap
)
1882 char *currentfile
= NULL
;
1885 if (is_suppressed_warning(severity
))
1888 if (!(severity
& ERR_NOFILE
))
1889 src_get(&lineno
, ¤tfile
);
1892 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1893 nasm_free(currentfile
);
1895 fputs("nasm: ", error_file
);
1898 nasm_verror_common(severity
, fmt
, ap
);
1902 * check for supressed warning
1903 * checks for suppressed warning or pass one only warning and we're
1906 * @param severity the severity of the warning or error
1907 * @return true if we should abort error/warning printing
1909 static bool is_suppressed_warning(int severity
)
1912 * See if it's a suppressed warning.
1914 return (severity
& ERR_MASK
) == ERR_WARNING
&&
1915 (((severity
& ERR_WARN_MASK
) != 0 &&
1916 !warning_on
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1917 /* See if it's a pass-one only warning and we're not in pass one. */
1918 ((severity
& ERR_PASS1
) && pass0
!= 1) ||
1919 ((severity
& ERR_PASS2
) && pass0
!= 2));
1923 * common error reporting
1924 * This is the common back end of the error reporting schemes currently
1925 * implemented. It prints the nature of the warning and then the
1926 * specific error message to error_file and may or may not return. It
1927 * doesn't return if the error severity is a "panic" or "debug" type.
1929 * @param severity the severity of the warning or error
1930 * @param fmt the printf style format string
1932 static void nasm_verror_common(int severity
, const char *fmt
, va_list args
)
1937 switch (severity
& (ERR_MASK
|ERR_NO_SEVERITY
)) {
1958 vsnprintf(msg
, sizeof msg
, fmt
, args
);
1960 fprintf(error_file
, "%s%s\n", pfx
, msg
);
1963 nasmlist
.error(severity
, pfx
, msg
);
1965 if (severity
& ERR_USAGE
)
1968 switch (severity
& ERR_MASK
) {
1970 /* no further action, by definition */
1973 if (warning_on
[0]) /* Treat warnings as errors */
1974 terminate_after_phase
= true;
1977 terminate_after_phase
= true;
1987 exit(1); /* instantly die */
1988 break; /* placate silly compilers */
1991 /* abort(); *//* halt, catch fire, and dump core */
1997 static void usage(void)
1999 fputs("type `nasm -h' for help\n", error_file
);
2002 #define BUF_DELTA 512
2004 static FILE *no_pp_fp
;
2005 static ListGen
*no_pp_list
;
2006 static int32_t no_pp_lineinc
;
2008 static void no_pp_reset(char *file
, int pass
, ListGen
* listgen
,
2011 src_set_fname(nasm_strdup(file
));
2014 no_pp_fp
= fopen(file
, "r");
2016 nasm_error(ERR_FATAL
| ERR_NOFILE
,
2017 "unable to open input file `%s'", file
);
2018 no_pp_list
= listgen
;
2019 (void)pass
; /* placate compilers */
2022 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
2024 strcpy(sl
->str
, file
);
2029 static char *no_pp_getline(void)
2031 char *buffer
, *p
, *q
;
2034 bufsize
= BUF_DELTA
;
2035 buffer
= nasm_malloc(BUF_DELTA
);
2036 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
2038 while (1) { /* Loop to handle %line */
2041 while (1) { /* Loop to handle long lines */
2042 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
2046 if (p
> buffer
&& p
[-1] == '\n')
2048 if (p
- buffer
> bufsize
- 10) {
2050 offset
= p
- buffer
;
2051 bufsize
+= BUF_DELTA
;
2052 buffer
= nasm_realloc(buffer
, bufsize
);
2053 p
= buffer
+ offset
;
2057 if (!q
&& p
== buffer
) {
2063 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
2064 * them are present at the end of the line.
2066 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
2068 if (!nasm_strnicmp(buffer
, "%line", 5)) {
2071 char *nm
= nasm_malloc(strlen(buffer
));
2072 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
2073 nasm_free(src_set_fname(nm
));
2083 no_pp_list
->line(LIST_READ
, buffer
);
2088 static void no_pp_cleanup(int pass
)
2090 (void)pass
; /* placate GCC */
2097 static uint32_t get_cpu(char *value
)
2099 if (!strcmp(value
, "8086"))
2101 if (!strcmp(value
, "186"))
2103 if (!strcmp(value
, "286"))
2105 if (!strcmp(value
, "386"))
2107 if (!strcmp(value
, "486"))
2109 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
2111 if (!strcmp(value
, "686") ||
2112 !nasm_stricmp(value
, "ppro") ||
2113 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
2115 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
2117 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
2118 !nasm_stricmp(value
, "willamette"))
2119 return IF_WILLAMETTE
;
2120 if (!nasm_stricmp(value
, "prescott"))
2122 if (!nasm_stricmp(value
, "x64") ||
2123 !nasm_stricmp(value
, "x86-64"))
2125 if (!nasm_stricmp(value
, "ia64") ||
2126 !nasm_stricmp(value
, "ia-64") ||
2127 !nasm_stricmp(value
, "itanium") ||
2128 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
2131 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
2132 "unknown 'cpu' type");
2134 return IF_PLEVEL
; /* the maximum level */
2137 static int get_bits(char *value
)
2141 if ((i
= atoi(value
)) == 16)
2142 return i
; /* set for a 16-bit segment */
2145 nasm_error(ERR_NONFATAL
,
2146 "cannot specify 32-bit segment on processor below a 386");
2149 } else if (i
== 64) {
2150 if (cpu
< IF_X86_64
) {
2151 nasm_error(ERR_NONFATAL
,
2152 "cannot specify 64-bit segment on processor below an x86-64");
2156 nasm_error(ERR_NONFATAL
,
2157 "%s output format does not support 64-bit code",
2162 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
2163 "`%s' is not a valid segment size; must be 16, 32 or 64",