blob: 4b53411d7c38c6c9e1baa1c5b16c294f668cf007 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* The Netwide Assembler main program module
2 *
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.
7 */
8
9#include <stdio.h>
10#include <stdarg.h>
11#include <stdlib.h>
12#include <string.h>
13#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000014#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000015
16#include "nasm.h"
17#include "nasmlib.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000018#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000019#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000020#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000021#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022#include "assemble.h"
23#include "labels.h"
24#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000025#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000026
H. Peter Anvine2c80182005-01-15 22:15:51 +000027struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000028 int lineno;
29 int operand;
30};
31
Keith Kaniosb7a89542007-04-12 02:40:54 +000032static int get_bits(int8_t *value);
33static uint32_t get_cpu(int8_t *cpu_str);
34static void parse_cmdline(int, int8_t **);
35static void assemble_file(int8_t *);
36static int getkw(int8_t **directive, int8_t **value);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037static void register_output_formats(void);
Keith Kaniosb7a89542007-04-12 02:40:54 +000038static void report_error_gnu(int severity, const int8_t *fmt, ...);
39static void report_error_vc(int severity, const int8_t *fmt, ...);
40static void report_error_common(int severity, const int8_t *fmt,
H. Peter Anvine2c80182005-01-15 22:15:51 +000041 va_list args);
42static int is_suppressed_warning(int severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000043static void usage(void);
Ed Berosetfa771012002-06-09 20:56:40 +000044static efunc report_error;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000045
John Coffman0efaec92002-05-26 19:20:08 +000046static int using_debug_info, opt_verbose_info;
H. Peter Anvine2c80182005-01-15 22:15:51 +000047int tasm_compatible_mode = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +000048int pass0;
Keith Kaniosb7a89542007-04-12 02:40:54 +000049int maxbits = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000050
Keith Kaniosb7a89542007-04-12 02:40:54 +000051static int8_t inname[FILENAME_MAX];
52static int8_t outname[FILENAME_MAX];
53static int8_t listname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +000054static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +000055/* static int pass = 0; */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056static struct ofmt *ofmt = NULL;
57
H. Peter Anvine2c80182005-01-15 22:15:51 +000058static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +000059
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060static FILE *ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000061int optimizing = -1; /* number of optimization passes to take */
62static int sb, cmd_sb = 16; /* by default */
Keith Kaniosb7a89542007-04-12 02:40:54 +000063static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
64static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
H. Peter Anvine2c80182005-01-15 22:15:51 +000065int global_offset_changed; /* referenced in labels.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066
H. Peter Anvineba20a72002-04-30 20:53:55 +000067static loc_t location;
H. Peter Anvine2c80182005-01-15 22:15:51 +000068int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +000069int32_t abs_seg; /* ABSOLUTE segment basis */
70int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +000071
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +000073
H. Peter Anvine2c80182005-01-15 22:15:51 +000074static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvineba20a72002-04-30 20:53:55 +000075static struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000076
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000077static Preproc *preproc;
H. Peter Anvin620515a2002-04-30 20:57:38 +000078enum op_type {
H. Peter Anvine2c80182005-01-15 22:15:51 +000079 op_normal, /* Preprocess and assemble */
80 op_preprocess, /* Preprocess only */
81 op_depend /* Generate dependencies */
H. Peter Anvin620515a2002-04-30 20:57:38 +000082};
83static enum op_type operating_mode;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +000086 * Which of the suppressible warnings are suppressed. Entry zero
87 * doesn't do anything. Initial defaults are given here.
88 */
Keith Kaniosb7a89542007-04-12 02:40:54 +000089static int8_t suppressed[1 + ERR_WARN_MAX] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +000090 0, TRUE, TRUE, TRUE, FALSE, TRUE
H. Peter Anvin6768eb72002-04-30 20:52:26 +000091};
92
93/*
94 * The option names for the suppressible warnings. As before, entry
95 * zero does nothing.
96 */
Keith Kaniosb7a89542007-04-12 02:40:54 +000097static const int8_t *suppressed_names[1 + ERR_WARN_MAX] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +000098 NULL, "macro-params", "macro-selfref", "orphan-labels",
99 "number-overflow",
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000100 "gnu-elf-extensions"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000101};
102
103/*
104 * The explanations for the suppressible warnings. As before, entry
105 * zero does nothing.
106 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000107static const int8_t *suppressed_what[1 + ERR_WARN_MAX] = {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000108 NULL,
109 "macro calls with wrong no. of params",
110 "cyclic macro self-references",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000111 "labels alone on lines without trailing `:'",
H. Peter Anvin8ac36412002-04-30 21:09:12 +0000112 "numeric constants greater than 0xFFFFFFFF",
113 "using 8- or 16-bit relocation in ELF, a GNU extension"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000114};
115
116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 * This is a null preprocessor which just copies lines from input
118 * to output. It's used when someone explicitly requests that NASM
119 * not preprocess their source file.
120 */
121
Keith Kaniosb7a89542007-04-12 02:40:54 +0000122static void no_pp_reset(int8_t *, int, efunc, evalfunc, ListGen *);
123static int8_t *no_pp_getline(void);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124static void no_pp_cleanup(int);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000125static Preproc no_pp = {
126 no_pp_reset,
127 no_pp_getline,
128 no_pp_cleanup
129};
130
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131/*
132 * get/set current offset...
133 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000134#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000135 raa_read(offsets,location.segment))
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000136#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
H. Peter Anvineba20a72002-04-30 20:53:55 +0000137 (void)(offsets=raa_write(offsets,location.segment,(x))))
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000138
139static int want_usage;
140static int terminate_after_phase;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000141int user_nolist = 0; /* fbk 9/2/00 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142
Keith Kaniosb7a89542007-04-12 02:40:54 +0000143static void nasm_fputs(const int8_t *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000144{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000145 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000146 fputs(line, outfile);
147 fputc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000148 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000150}
151
H. Peter Anvin038d8612007-04-12 16:54:50 +0000152int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000153{
Ed Berosetfa771012002-06-09 20:56:40 +0000154 pass0 = 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155 want_usage = terminate_after_phase = FALSE;
Ed Berosetfa771012002-06-09 20:56:40 +0000156 report_error = report_error_gnu;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000157
H. Peter Anvine2c80182005-01-15 22:15:51 +0000158 nasm_set_malloc_error(report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000159 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000160 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000161
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000162 preproc = &nasmpp;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000163 operating_mode = op_normal;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000164
H. Peter Anvinef7468f2002-04-30 20:57:59 +0000165 error_file = stderr;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000166
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000167 seg_init();
168
169 register_output_formats();
170
171 parse_cmdline(argc, argv);
172
H. Peter Anvine2c80182005-01-15 22:15:51 +0000173 if (terminate_after_phase) {
174 if (want_usage)
175 usage();
176 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177 }
178
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000179 /* If debugging info is disabled, suppress any debug calls */
180 if (!using_debug_info)
181 ofmt->current_dfmt = &null_debug_form;
182
H. Peter Anvin76690a12002-04-30 20:52:49 +0000183 if (ofmt->stdmac)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184 pp_extra_stdmac(ofmt->stdmac);
185 parser_global_info(ofmt, &location);
186 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000187
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000188 /* define some macros dependent of command-line */
189 {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000190 int8_t temp[64];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000191 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
192 ofmt->shortname);
193 pp_pre_define(temp);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000194 }
195
H. Peter Anvine2c80182005-01-15 22:15:51 +0000196 switch (operating_mode) {
H. Peter Anvin620515a2002-04-30 20:57:38 +0000197 case op_depend:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000199 int8_t *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000200 preproc->reset(inname, 0, report_error, evaluate, &nasmlist);
201 if (outname[0] == '\0')
202 ofmt->filename(inname, outname, report_error);
203 ofile = NULL;
204 fprintf(stdout, "%s: %s", outname, inname);
205 while ((line = preproc->getline()))
206 nasm_free(line);
207 preproc->cleanup(0);
208 putc('\n', stdout);
209 }
210 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000211
212 case op_preprocess:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000213 {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000214 int8_t *line;
215 int8_t *file_name = NULL;
216 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000218
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219 if (*outname) {
220 ofile = fopen(outname, "w");
221 if (!ofile)
222 report_error(ERR_FATAL | ERR_NOFILE,
223 "unable to open output file `%s'",
224 outname);
225 } else
226 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000227
H. Peter Anvine2c80182005-01-15 22:15:51 +0000228 location.known = FALSE;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000229
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000230/* pass = 1; */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000231 preproc->reset(inname, 2, report_error, evaluate, &nasmlist);
232 while ((line = preproc->getline())) {
233 /*
234 * We generate %line directives if needed for later programs
235 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000236 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000237 int altline = src_get(&linnum, &file_name);
238 if (altline) {
239 if (altline == 1 && lineinc == 1)
240 nasm_fputs("", ofile);
241 else {
242 lineinc = (altline != -1 || lineinc != 1);
243 fprintf(ofile ? ofile : stdout,
244 "%%line %ld+%d %s\n", linnum, lineinc,
245 file_name);
246 }
247 prior_linnum = linnum;
248 }
249 nasm_fputs(line, ofile);
250 nasm_free(line);
251 }
252 nasm_free(file_name);
253 preproc->cleanup(0);
254 if (ofile)
255 fclose(ofile);
256 if (ofile && terminate_after_phase)
257 remove(outname);
258 }
259 break;
H. Peter Anvin620515a2002-04-30 20:57:38 +0000260
261 case op_normal:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000262 {
263 /*
264 * We must call ofmt->filename _anyway_, even if the user
265 * has specified their own output file, because some
266 * formats (eg OBJ and COFF) use ofmt->filename to find out
267 * the name of the input file and then put that inside the
268 * file.
269 */
270 ofmt->filename(inname, outname, report_error);
271
272 ofile = fopen(outname, "wb");
273 if (!ofile) {
274 report_error(ERR_FATAL | ERR_NOFILE,
275 "unable to open output file `%s'", outname);
276 }
277
278 /*
279 * We must call init_labels() before ofmt->init() since
280 * some object formats will want to define labels in their
281 * init routines. (eg OS/2 defines the FLAT group)
282 */
283 init_labels();
284
285 ofmt->init(ofile, report_error, define_label, evaluate);
286
287 assemble_file(inname);
288
289 if (!terminate_after_phase) {
290 ofmt->cleanup(using_debug_info);
291 cleanup_labels();
292 } else {
293 /*
294 * We had an fclose on the output file here, but we
295 * actually do that in all the object file drivers as well,
296 * so we're leaving out the one here.
297 * fclose (ofile);
298 */
299 remove(outname);
300 if (listname[0])
301 remove(listname);
302 }
303 }
304 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000305 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000306
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000307 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000308 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000309
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310 raa_free(offsets);
311 saa_free(forwrefs);
312 eval_cleanup();
313 nasmlib_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000314
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000315 if (terminate_after_phase)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000316 return 1;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000317 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000318 return 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000319}
320
H. Peter Anvineba20a72002-04-30 20:53:55 +0000321/*
322 * Get a parameter for a command line option.
323 * First arg must be in the form of e.g. -f...
324 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000325static int8_t *get_param(int8_t *p, int8_t *q, int *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000326{
327 *advance = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 if (p[2]) { /* the parameter's in the option */
329 p += 2;
330 while (isspace(*p))
331 p++;
332 return p;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000333 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000334 if (q && q[0]) {
335 *advance = 1;
336 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000337 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000338 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
339 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000340 return NULL;
341}
342
H. Peter Anvine2c80182005-01-15 22:15:51 +0000343struct textargs {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000344 const int8_t *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000345 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000346};
347
348#define OPT_PREFIX 0
349#define OPT_POSTFIX 1
H. Peter Anvine2c80182005-01-15 22:15:51 +0000350struct textargs textopts[] = {
351 {"prefix", OPT_PREFIX},
352 {"postfix", OPT_POSTFIX},
353 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000354};
355
H. Peter Anvineba20a72002-04-30 20:53:55 +0000356int stopoptions = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000357static int process_arg(int8_t *p, int8_t *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000359 int8_t *param;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360 int i, advance = 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000361
362 if (!p || !p[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000363 return 0;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000364
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365 if (p[0] == '-' && !stopoptions) {
366 switch (p[1]) {
367 case 's':
368 error_file = stdout;
369 break;
370 case 'o': /* these parameters take values */
371 case 'O':
372 case 'f':
373 case 'p':
374 case 'P':
375 case 'd':
376 case 'D':
377 case 'i':
378 case 'I':
379 case 'l':
380 case 'E':
381 case 'F':
382 case 'X':
383 case 'u':
384 case 'U':
385 if (!(param = get_param(p, q, &advance)))
386 break;
387 if (p[1] == 'o') { /* output file */
388 strcpy(outname, param);
389 } else if (p[1] == 'f') { /* output format */
390 ofmt = ofmt_find(param);
391 if (!ofmt) {
392 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
393 "unrecognised output format `%s' - "
394 "use -hf for a list", param);
395 } else
396 ofmt->current_dfmt = ofmt->debug_formats[0];
397 } else if (p[1] == 'O') { /* Optimization level */
398 int opt;
399 opt = -99;
400 while (*param) {
401 if (isdigit(*param)) {
402 opt = atoi(param);
403 while (isdigit(*++param)) ;
404 if (opt <= 0)
405 optimizing = -1; /* 0.98 behaviour */
406 else if (opt == 1)
407 optimizing = 0; /* Two passes, 0.98.09 behavior */
408 else
409 optimizing = opt; /* Multiple passes */
410 } else {
411 if (*param == 'v' || *param == '+') {
412 ++param;
413 opt_verbose_info = TRUE;
414 opt = 0;
415 } else { /* garbage */
416 opt = -99;
417 break;
418 }
419 }
420 } /* while (*param) */
421 if (opt == -99)
422 report_error(ERR_FATAL,
423 "command line optimization level must be 'v', 0..3 or <nn>");
424 } else if (p[1] == 'P' || p[1] == 'p') { /* pre-include */
425 pp_pre_include(param);
426 } else if (p[1] == 'D' || p[1] == 'd') { /* pre-define */
427 pp_pre_define(param);
428 } else if (p[1] == 'U' || p[1] == 'u') { /* un-define */
429 pp_pre_undefine(param);
430 } else if (p[1] == 'I' || p[1] == 'i') { /* include search path */
431 pp_include_path(param);
432 } else if (p[1] == 'l') { /* listing file */
433 strcpy(listname, param);
434 } else if (p[1] == 'E') { /* error messages file */
435 error_file = fopen(param, "w");
436 if (!error_file) {
437 error_file = stderr; /* Revert to default! */
438 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
439 "cannot open file `%s' for error messages",
440 param);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000441 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 } else if (p[1] == 'F') { /* specify debug format */
443 ofmt->current_dfmt = dfmt_find(ofmt, param);
444 if (!ofmt->current_dfmt) {
445 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
446 "unrecognized debug format `%s' for"
447 " output format `%s'",
448 param, ofmt->shortname);
449 }
450 } else if (p[1] == 'X') { /* specify error reporting format */
451 if (nasm_stricmp("vc", param) == 0)
452 report_error = report_error_vc;
453 else if (nasm_stricmp("gnu", param) == 0)
454 report_error = report_error_gnu;
455 else
456 report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
457 "unrecognized error reporting format `%s'",
458 param);
459 }
460 break;
461 case 'g':
462 using_debug_info = TRUE;
463 break;
464 case 'h':
465 printf
466 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
467 "[-l listfile]\n"
468 " [options...] [--] filename\n"
469 " or nasm -r for version info (obsolete)\n"
470 " or nasm -v for version info (preferred)\n\n"
471 " -t assemble in SciTech TASM compatible mode\n"
472 " -g generate debug information in selected format.\n");
473 printf
474 (" -e preprocess only (writes output to stdout by default)\n"
475 " -a don't preprocess (assemble only)\n"
476 " -M generate Makefile dependencies on stdout\n\n"
477 " -E<file> redirect error messages to file\n"
478 " -s redirect error messages to stdout\n\n"
479 " -F format select a debugging format\n\n"
480 " -I<path> adds a pathname to the include file path\n");
481 printf
482 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
483 " -P<file> pre-includes a file\n"
484 " -D<macro>[=<value>] pre-defines a macro\n"
485 " -U<macro> undefines a macro\n"
486 " -X<format> specifies error reporting format (gnu or vc)\n"
487 " -w+foo enables warnings about foo; -w-foo disables them\n"
488 "where foo can be:\n");
489 for (i = 1; i <= ERR_WARN_MAX; i++)
490 printf(" %-23s %s (default %s)\n",
491 suppressed_names[i], suppressed_what[i],
492 suppressed[i] ? "off" : "on");
493 printf
494 ("\nresponse files should contain command line parameters"
495 ", one per line.\n");
496 if (p[2] == 'f') {
497 printf("\nvalid output formats for -f are"
498 " (`*' denotes default):\n");
499 ofmt_list(ofmt, stdout);
500 } else {
501 printf("\nFor a list of valid output formats, use -hf.\n");
502 printf("For a list of debug formats, use -f <form> -y.\n");
503 }
504 exit(0); /* never need usage message here */
505 break;
506 case 'y':
507 printf("\nvalid debug formats for '%s' output format are"
508 " ('*' denotes default):\n", ofmt->shortname);
509 dfmt_list(ofmt, stdout);
510 exit(0);
511 break;
512 case 't':
513 tasm_compatible_mode = TRUE;
514 break;
515 case 'r':
516 case 'v':
517 {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000518 const int8_t *nasm_version_string =
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 "NASM version " NASM_VER " compiled on " __DATE__
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520#ifdef DEBUG
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 " with -DDEBUG"
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 ;
524 puts(nasm_version_string);
525 exit(0); /* never need usage message here */
526 }
527 break;
528 case 'e': /* preprocess only */
529 operating_mode = op_preprocess;
530 break;
531 case 'a': /* assemble only - don't preprocess */
532 preproc = &no_pp;
533 break;
534 case 'w':
535 if (p[2] != '+' && p[2] != '-') {
536 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
537 "invalid option to `-w'");
538 } else {
539 for (i = 1; i <= ERR_WARN_MAX; i++)
540 if (!nasm_stricmp(p + 3, suppressed_names[i]))
541 break;
542 if (i <= ERR_WARN_MAX)
543 suppressed[i] = (p[2] == '-');
544 else
545 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
546 "invalid option to `-w'");
547 }
548 break;
549 case 'M':
550 operating_mode = op_depend;
551 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000552
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 case '-':
554 {
555 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000556
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 if (p[2] == 0) { /* -- => stop processing options */
558 stopoptions = 1;
559 break;
560 }
561 for (s = 0; textopts[s].label; s++) {
562 if (!nasm_stricmp(p + 2, textopts[s].label)) {
563 break;
564 }
565 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000566
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 switch (s) {
568
569 case OPT_PREFIX:
570 case OPT_POSTFIX:
571 {
572 if (!q) {
573 report_error(ERR_NONFATAL | ERR_NOFILE |
574 ERR_USAGE,
575 "option `--%s' requires an argument",
576 p + 2);
577 break;
578 } else {
579 advance = 1, param = q;
580 }
581
582 if (s == OPT_PREFIX) {
583 strncpy(lprefix, param, PREFIX_MAX - 1);
584 lprefix[PREFIX_MAX - 1] = 0;
585 break;
586 }
587 if (s == OPT_POSTFIX) {
588 strncpy(lpostfix, param, POSTFIX_MAX - 1);
589 lpostfix[POSTFIX_MAX - 1] = 0;
590 break;
591 }
592 break;
593 }
594 default:
595 {
596 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
597 "unrecognised option `--%s'", p + 2);
598 break;
599 }
600 }
601 break;
602 }
603
604 default:
605 if (!ofmt->setinfo(GI_SWITCH, &p))
606 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
607 "unrecognised option `-%c'", p[1]);
608 break;
609 }
610 } else {
611 if (*inname) {
612 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
613 "more than one input file specified");
614 } else
615 strcpy(inname, p);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000616 }
617
618 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000619}
620
H. Peter Anvineba20a72002-04-30 20:53:55 +0000621#define ARG_BUF_DELTA 128
622
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000625 int8_t *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000626 int bufsize, prevargsize;
627
628 bufsize = prevargsize = ARG_BUF_DELTA;
629 buffer = nasm_malloc(ARG_BUF_DELTA);
630 prevarg = nasm_malloc(ARG_BUF_DELTA);
631 prevarg[0] = '\0';
632
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 while (1) { /* Loop to handle all lines in file */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 p = buffer;
636 while (1) { /* Loop to handle long lines */
637 q = fgets(p, bufsize - (p - buffer), rfile);
638 if (!q)
639 break;
640 p += strlen(p);
641 if (p > buffer && p[-1] == '\n')
642 break;
643 if (p - buffer > bufsize - 10) {
644 int offset;
645 offset = p - buffer;
646 bufsize += ARG_BUF_DELTA;
647 buffer = nasm_realloc(buffer, bufsize);
648 p = buffer + offset;
649 }
650 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 if (!q && p == buffer) {
653 if (prevarg[0])
654 process_arg(prevarg, NULL);
655 nasm_free(buffer);
656 nasm_free(prevarg);
657 return;
658 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 /*
661 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
662 * them are present at the end of the line.
663 */
664 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +0000665
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 while (p > buffer && isspace(p[-1]))
667 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +0000668
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 p = buffer;
670 while (isspace(*p))
671 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000672
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 if (process_arg(prevarg, p))
674 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +0000675
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676 if (strlen(p) > prevargsize - 10) {
677 prevargsize += ARG_BUF_DELTA;
678 prevarg = nasm_realloc(prevarg, prevargsize);
679 }
680 strcpy(prevarg, p);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000681 }
682}
683
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000684/* Function to process args from a string of args, rather than the
685 * argv array. Used by the environment variable and response file
686 * processing.
687 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000688static void process_args(int8_t *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000690 int8_t *p, *q, *arg, *prevarg;
691 int8_t separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000692
693 p = args;
694 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000696 arg = NULL;
697 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 q = p;
699 while (*p && *p != separator)
700 p++;
701 while (*p == separator)
702 *p++ = '\0';
703 prevarg = arg;
704 arg = q;
705 if (process_arg(prevarg, arg))
706 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000707 }
708 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000710}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000711
Keith Kaniosb7a89542007-04-12 02:40:54 +0000712static void parse_cmdline(int argc, int8_t **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000713{
714 FILE *rfile;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000715 int8_t *envreal, *envcopy = NULL, *p, *arg;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000716
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000717 *inname = *outname = *listname = '\0';
718
719 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +0000720 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000721 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +0000722 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000723 arg = NULL;
724 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 envcopy = nasm_strdup(envreal);
726 process_args(envcopy);
727 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000729
730 /*
731 * Now process the actual command line.
732 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 while (--argc) {
734 int i;
735 argv++;
736 if (argv[0][0] == '@') {
737 /* We have a response file, so process this as a set of
738 * arguments like the environment variable. This allows us
739 * to have multiple arguments on a single line, which is
740 * different to the -@resp file processing below for regular
741 * NASM.
742 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000743 int8_t *str = malloc(2048);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 FILE *f = fopen(&argv[0][1], "r");
745 if (!str) {
746 printf("out of memory");
747 exit(-1);
748 }
749 if (f) {
750 while (fgets(str, 2048, f)) {
751 process_args(str);
752 }
753 fclose(f);
754 }
755 free(str);
756 argc--;
757 argv++;
758 }
759 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
760 if ((p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &i))) {
761 if ((rfile = fopen(p, "r"))) {
762 process_respfile(rfile);
763 fclose(rfile);
764 } else
765 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
766 "unable to open response file `%s'", p);
767 }
768 } else
769 i = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
770 argv += i, argc -= i;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000771 }
772
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000773 if (!*inname)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
775 "no input file specified");
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776}
777
Keith Kaniosb7a89542007-04-12 02:40:54 +0000778static void assemble_file(int8_t *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000779{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000780 int8_t *directive, *value, *p, *q, *special, *line, debugid[80];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 insn output_ins;
782 int i, rn_error, validid;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000783 int32_t seg, offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000784 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 expr *e;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000786 int pass, pass_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 int pass_cnt = 0; /* count actual passes */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000788
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000789 if (cmd_sb == 32 && cmd_cpu < IF_386)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 report_error(ERR_FATAL, "command line: "
791 "32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000792
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
794 pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
795 for (pass = 1; pass <= pass_max && pass0 <= 2; pass++) {
796 int pass1, pass2;
797 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000798
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 pass1 = pass < pass_max ? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
800 pass2 = pass > 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
801 /* pass0 seq is 0, 0, 0,..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000802
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 def_label = pass > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000804
Keith Kaniosb7a89542007-04-12 02:40:54 +0000805 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 cpu = cmd_cpu;
807 if (pass0 == 2) {
808 if (*listname)
809 nasmlist.init(listname, report_error);
810 }
811 in_abs_seg = FALSE;
812 global_offset_changed = FALSE; /* set by redefine_label */
813 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000814 globalbits = sb;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 if (pass > 1) {
816 saa_rewind(forwrefs);
817 forwref = saa_rstruct(forwrefs);
818 raa_free(offsets);
819 offsets = raa_init();
820 }
821 preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
822 globallineno = 0;
823 if (pass == 1)
824 location.known = TRUE;
825 location.offset = offs = GET_CURR_OFFS;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 while ((line = preproc->getline())) {
828 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000829
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 /* here we parse our directives; this is not handled by the 'real'
831 * parser. */
832 directive = line;
833 if ((i = getkw(&directive, &value))) {
834 switch (i) {
835 case 1: /* [SEGMENT n] */
836 seg = ofmt->section(value, pass2, &sb);
837 if (seg == NO_SEG) {
838 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000839 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 value);
841 } else {
842 in_abs_seg = FALSE;
843 location.segment = seg;
844 }
845 break;
846 case 2: /* [EXTERN label:special] */
847 if (*value == '$')
848 value++; /* skip initial $ if present */
849 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000850 q = value;
851 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000853 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 *q++ = '\0';
855 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000856 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 } else if (pass == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000858 q = value;
859 validid = TRUE;
860 if (!isidstart(*q))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 validid = FALSE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000862 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 if (!isidchar(*q))
864 validid = FALSE;
865 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000866 }
867 if (!validid) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 report_error(ERR_NONFATAL,
869 "identifier expected after EXTERN");
870 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000871 }
872 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 *q++ = '\0';
874 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000875 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 special = NULL;
877 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
878 int temp = pass0;
879 pass0 = 1; /* fake pass 1 in labels.c */
880 declare_as_global(value, special,
881 report_error);
882 define_label(value, seg_alloc(), 0L, NULL,
883 FALSE, TRUE, ofmt, report_error);
884 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000885 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 } /* else pass0 == 1 */
887 break;
888 case 3: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000889 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 break;
891 case 4: /* [GLOBAL symbol:special] */
892 if (*value == '$')
893 value++; /* skip initial $ if present */
894 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000895 q = value;
896 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000898 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 *q++ = '\0';
900 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000901 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000903 q = value;
904 validid = TRUE;
905 if (!isidstart(*q))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 validid = FALSE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000907 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 if (!isidchar(*q))
909 validid = FALSE;
910 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000911 }
912 if (!validid) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 report_error(ERR_NONFATAL,
914 "identifier expected after GLOBAL");
915 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000916 }
917 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 *q++ = '\0';
919 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000920 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 special = NULL;
922 declare_as_global(value, special, report_error);
923 } /* pass == 1 */
924 break;
925 case 5: /* [COMMON symbol size:special] */
926 if (*value == '$')
927 value++; /* skip initial $ if present */
928 if (pass0 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000929 p = value;
930 validid = TRUE;
931 if (!isidstart(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 validid = FALSE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000933 while (*p && !isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 if (!isidchar(*p))
935 validid = FALSE;
936 p++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000937 }
938 if (!validid) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 report_error(ERR_NONFATAL,
940 "identifier expected after COMMON");
941 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000942 }
943 if (*p) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000944 int64_t size;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 while (*p && isspace(*p))
947 *p++ = '\0';
948 q = p;
949 while (*q && *q != ':')
950 q++;
951 if (*q == ':') {
952 *q++ = '\0';
953 special = q;
954 } else
955 special = NULL;
956 size = readnum(p, &rn_error);
957 if (rn_error)
958 report_error(ERR_NONFATAL,
959 "invalid size specified"
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000960 " in COMMON declaration");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 else
962 define_common(value, seg_alloc(), size,
963 special, ofmt, report_error);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000964 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 report_error(ERR_NONFATAL,
966 "no size specified in"
967 " COMMON declaration");
968 } else if (pass0 == 2) { /* pass == 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000969 q = value;
970 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 if (isspace(*q))
972 *q = '\0';
973 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000974 }
975 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 *q++ = '\0';
977 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000978 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 }
980 break;
981 case 6: /* [ABSOLUTE address] */
982 stdscan_reset();
983 stdscan_bufptr = value;
984 tokval.t_type = TOKEN_INVALID;
985 e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
986 report_error, NULL);
987 if (e) {
988 if (!is_reloc(e))
989 report_error(pass0 ==
990 1 ? ERR_NONFATAL : ERR_PANIC,
991 "cannot use non-relocatable expression as "
992 "ABSOLUTE address");
993 else {
994 abs_seg = reloc_seg(e);
995 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000996 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 } else if (pass == 1)
998 abs_offset = 0x100; /* don't go near zero in case of / */
999 else
1000 report_error(ERR_PANIC, "invalid ABSOLUTE address "
1001 "in pass two");
1002 in_abs_seg = TRUE;
1003 location.segment = NO_SEG;
1004 break;
1005 case 7: /* DEBUG */
1006 p = value;
1007 q = debugid;
1008 validid = TRUE;
1009 if (!isidstart(*p))
1010 validid = FALSE;
1011 while (*p && !isspace(*p)) {
1012 if (!isidchar(*p))
1013 validid = FALSE;
1014 *q++ = *p++;
1015 }
1016 *q++ = 0;
1017 if (!validid) {
1018 report_error(pass == 1 ? ERR_NONFATAL : ERR_PANIC,
1019 "identifier expected after DEBUG");
1020 break;
1021 }
1022 while (*p && isspace(*p))
1023 p++;
1024 if (pass == pass_max)
1025 ofmt->current_dfmt->debug_directive(debugid, p);
1026 break;
1027 case 8: /* [WARNING {+|-}warn-name] */
1028 if (pass1 == 1) {
1029 while (*value && isspace(*value))
1030 value++;
1031
1032 if (*value == '+' || *value == '-') {
1033 validid = (*value == '-') ? TRUE : FALSE;
1034 value++;
1035 } else
1036 validid = FALSE;
1037
1038 for (i = 1; i <= ERR_WARN_MAX; i++)
1039 if (!nasm_stricmp(value, suppressed_names[i]))
1040 break;
1041 if (i <= ERR_WARN_MAX)
1042 suppressed[i] = validid;
1043 else
1044 report_error(ERR_NONFATAL,
1045 "invalid warning id in WARNING directive");
1046 }
1047 break;
1048 case 9: /* cpu */
1049 cpu = get_cpu(value);
1050 break;
1051 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1052 while (*value && isspace(*value))
1053 value++;
1054
1055 if (*value == '+') {
1056 user_nolist = 0;
1057 } else {
1058 if (*value == '-') {
1059 user_nolist = 1;
1060 } else {
1061 report_error(ERR_NONFATAL,
1062 "invalid parameter to \"list\" directive");
1063 }
1064 }
1065 break;
1066 default:
1067 if (!ofmt->directive(directive, value, pass2))
1068 report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1069 "unrecognised directive [%s]",
1070 directive);
1071 }
1072 } else { /* it isn't a directive */
1073
1074 parse_line(pass1, line, &output_ins,
1075 report_error, evaluate, def_label);
1076
1077 if (!(optimizing > 0) && pass == 2) {
1078 if (forwref != NULL && globallineno == forwref->lineno) {
1079 output_ins.forw_ref = TRUE;
1080 do {
1081 output_ins.oprs[forwref->operand].opflags |=
1082 OPFLAG_FORWARD;
1083 forwref = saa_rstruct(forwrefs);
1084 } while (forwref != NULL
1085 && forwref->lineno == globallineno);
1086 } else
1087 output_ins.forw_ref = FALSE;
1088 }
1089
1090 if (!(optimizing > 0) && output_ins.forw_ref) {
1091 if (pass == 1) {
1092 for (i = 0; i < output_ins.operands; i++) {
1093 if (output_ins.oprs[i].
1094 opflags & OPFLAG_FORWARD) {
1095 struct forwrefinfo *fwinf =
1096 (struct forwrefinfo *)
1097 saa_wstruct(forwrefs);
1098 fwinf->lineno = globallineno;
1099 fwinf->operand = i;
1100 }
1101 }
1102 } else { /* pass == 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001103 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 * Hack to prevent phase error in the code
1105 * rol ax,x
1106 * x equ 1
1107 *
1108 * If the second operand is a forward reference,
1109 * the UNITY property of the number 1 in that
1110 * operand is cancelled. Otherwise the above
1111 * sequence will cause a phase error.
1112 *
1113 * This hack means that the above code will
1114 * generate 286+ code.
1115 *
1116 * The forward reference will mean that the
1117 * operand will not have the UNITY property on
1118 * the first pass, so the pass behaviours will
1119 * be consistent.
1120 */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001121
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001122 if (output_ins.operands >= 2 &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001124 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 output_ins.oprs[1].type &=
1126 ~(ONENESS | BYTENESS);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001127 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001128
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 } /* pass == 2 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001130
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001132
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 /* forw_ref */
1134 if (output_ins.opcode == I_EQU) {
1135 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001136 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 * Special `..' EQUs get processed in pass two,
1138 * except `..@' macro-processor EQUs which are done
1139 * in the normal place.
1140 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001141 if (!output_ins.label)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 report_error(ERR_NONFATAL,
1143 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001144
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001145 else if (output_ins.label[0] != '.' ||
1146 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 output_ins.label[2] == '@') {
1148 if (output_ins.operands == 1 &&
1149 (output_ins.oprs[0].type & IMMEDIATE) &&
1150 output_ins.oprs[0].wrt == NO_SEG) {
1151 int isext =
1152 output_ins.oprs[0].
1153 opflags & OPFLAG_EXTERN;
1154 def_label(output_ins.label,
1155 output_ins.oprs[0].segment,
1156 output_ins.oprs[0].offset, NULL,
1157 FALSE, isext, ofmt,
1158 report_error);
1159 } else if (output_ins.operands == 2
1160 && (output_ins.oprs[0].
1161 type & IMMEDIATE)
1162 && (output_ins.oprs[0].type & COLON)
1163 && output_ins.oprs[0].segment ==
1164 NO_SEG
1165 && output_ins.oprs[0].wrt == NO_SEG
1166 && (output_ins.oprs[1].
1167 type & IMMEDIATE)
1168 && output_ins.oprs[1].segment ==
1169 NO_SEG
1170 && output_ins.oprs[1].wrt ==
1171 NO_SEG) {
1172 def_label(output_ins.label,
1173 output_ins.oprs[0].
1174 offset | SEG_ABS,
1175 output_ins.oprs[1].offset, NULL,
1176 FALSE, FALSE, ofmt,
1177 report_error);
1178 } else
1179 report_error(ERR_NONFATAL,
1180 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001181 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 } else { /* pass == 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001183 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 * Special `..' EQUs get processed here, except
1185 * `..@' macro processor EQUs which are done above.
1186 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001187 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 output_ins.label[1] == '.' &&
1189 output_ins.label[2] != '@') {
1190 if (output_ins.operands == 1 &&
1191 (output_ins.oprs[0].type & IMMEDIATE)) {
1192 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001193 output_ins.oprs[0].segment,
1194 output_ins.oprs[0].offset,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 NULL, FALSE, FALSE, ofmt,
1196 report_error);
1197 } else if (output_ins.operands == 2
1198 && (output_ins.oprs[0].
1199 type & IMMEDIATE)
1200 && (output_ins.oprs[0].type & COLON)
1201 && output_ins.oprs[0].segment ==
1202 NO_SEG
1203 && (output_ins.oprs[1].
1204 type & IMMEDIATE)
1205 && output_ins.oprs[1].segment ==
1206 NO_SEG) {
1207 define_label(output_ins.label,
1208 output_ins.oprs[0].
1209 offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001210 output_ins.oprs[1].offset,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 NULL, FALSE, FALSE, ofmt,
1212 report_error);
1213 } else
1214 report_error(ERR_NONFATAL,
1215 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001216 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 } /* pass == 2 */
1218 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001219
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001221
Keith Kaniosb7a89542007-04-12 02:40:54 +00001222 int32_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 &output_ins, report_error);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001224
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 /* if (using_debug_info) && output_ins.opcode != -1) */
1226 if (using_debug_info)
1227 { /* fbk 03/25/01 */
1228 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001229 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 TYS_ELEMENTS(output_ins.operands);
1231 switch (output_ins.opcode) {
1232 case I_RESB:
1233 typeinfo =
1234 TYS_ELEMENTS(output_ins.oprs[0].
1235 offset) | TY_BYTE;
1236 break;
1237 case I_RESW:
1238 typeinfo =
1239 TYS_ELEMENTS(output_ins.oprs[0].
1240 offset) | TY_WORD;
1241 break;
1242 case I_RESD:
1243 typeinfo =
1244 TYS_ELEMENTS(output_ins.oprs[0].
1245 offset) | TY_DWORD;
1246 break;
1247 case I_RESQ:
1248 typeinfo =
1249 TYS_ELEMENTS(output_ins.oprs[0].
1250 offset) | TY_QWORD;
1251 break;
1252 case I_REST:
1253 typeinfo =
1254 TYS_ELEMENTS(output_ins.oprs[0].
1255 offset) | TY_TBYTE;
1256 break;
1257 case I_DB:
1258 typeinfo |= TY_BYTE;
1259 break;
1260 case I_DW:
1261 typeinfo |= TY_WORD;
1262 break;
1263 case I_DD:
1264 if (output_ins.eops_float)
1265 typeinfo |= TY_FLOAT;
1266 else
1267 typeinfo |= TY_DWORD;
1268 break;
1269 case I_DQ:
1270 typeinfo |= TY_QWORD;
1271 break;
1272 case I_DT:
1273 typeinfo |= TY_TBYTE;
1274 break;
1275 default:
1276 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001277
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001279
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 ofmt->current_dfmt->debug_typevalue(typeinfo);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001281
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001282 }
1283 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 offs += l;
1285 SET_CURR_OFFS(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001286 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001287 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 * else l == -1 => invalid instruction, which will be
1289 * flagged as an error on pass 2
1290 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001291
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 } else { /* pass == 2 */
1293 offs += assemble(location.segment, offs, sb, cpu,
1294 &output_ins, ofmt, report_error,
1295 &nasmlist);
1296 SET_CURR_OFFS(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001297
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 }
1299 } /* not an EQU */
1300 cleanup_insn(&output_ins);
1301 }
1302 nasm_free(line);
1303 location.offset = offs = GET_CURR_OFFS;
1304 } /* end while (line = preproc->getline... */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001305
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 if (pass1 == 2 && global_offset_changed)
1307 report_error(ERR_NONFATAL,
1308 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001309
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 if (pass1 == 1)
1311 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (pass1 == 1 && terminate_after_phase) {
1314 fclose(ofile);
1315 remove(outname);
1316 if (want_usage)
1317 usage();
1318 exit(1);
1319 }
1320 pass_cnt++;
1321 if (pass > 1 && !global_offset_changed) {
1322 pass0++;
1323 if (pass0 == 2)
1324 pass = pass_max - 1;
1325 } else if (!(optimizing > 0))
1326 pass0++;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001327
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 } /* for (pass=1; pass<=2; pass++) */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 preproc->cleanup(0);
1331 nasmlist.cleanup();
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001332#if 1
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
1334 fprintf(stdout,
1335 "info:: assembly required 1+%d+1 passes\n", pass_cnt - 2);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001336#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337} /* exit from assemble_file (...) */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001338
Keith Kaniosb7a89542007-04-12 02:40:54 +00001339static int getkw(int8_t **directive, int8_t **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001340{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001341 int8_t *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342
Michael K. Ter Louw1d392362003-08-15 22:25:53 +00001343 buf = *directive;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001344
H. Peter Anvincaa82a12002-04-30 21:03:11 +00001345 /* allow leading spaces or tabs */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 while (*buf == ' ' || *buf == '\t')
1347 buf++;
H. Peter Anvincaa82a12002-04-30 21:03:11 +00001348
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 if (*buf != '[')
1350 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001351
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001352 p = buf;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001353
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 while (*p && *p != ']')
1355 p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001356
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001357 if (!*p)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 return 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001359
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001360 q = p++;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001361
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001362 while (*p && *p != ';') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (!isspace(*p))
1364 return 0;
1365 p++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001366 }
1367 q[1] = '\0';
1368
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 *directive = p = buf + 1;
1370 while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
1371 buf++;
1372 if (*buf == ']') {
1373 *buf = '\0';
1374 *value = buf;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001375 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 *buf++ = '\0';
1377 while (isspace(*buf))
1378 buf++; /* beppu - skip leading whitespace */
1379 *value = buf;
1380 while (*buf != ']')
1381 buf++;
1382 *buf++ = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001383 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001384#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 for (q = p; *q; q++)
1386 *q = tolower(*q);
1387#endif
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001388 if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 return 1;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001390 if (!nasm_stricmp(p, "extern"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 return 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001392 if (!nasm_stricmp(p, "bits"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 return 3;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001394 if (!nasm_stricmp(p, "global"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 return 4;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001396 if (!nasm_stricmp(p, "common"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 return 5;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001398 if (!nasm_stricmp(p, "absolute"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 return 6;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001400 if (!nasm_stricmp(p, "debug"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 return 7;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001402 if (!nasm_stricmp(p, "warning"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 return 8;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001404 if (!nasm_stricmp(p, "cpu"))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 return 9;
1406 if (!nasm_stricmp(p, "list")) /* fbk 9/2/00 */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001407 return 10;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001408 return -1;
1409}
1410
Ed Berosetfa771012002-06-09 20:56:40 +00001411/**
1412 * gnu style error reporting
1413 * This function prints an error message to error_file in the
1414 * style used by GNU. An example would be:
1415 * file.asm:50: error: blah blah blah
1416 * where file.asm is the name of the file, 50 is the line number on
1417 * which the error occurs (or is detected) and "error:" is one of
1418 * the possible optional diagnostics -- it can be "error" or "warning"
1419 * or something else. Finally the line terminates with the actual
1420 * error message.
1421 *
1422 * @param severity the severity of the warning or error
1423 * @param fmt the printf style format string
1424 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001425static void report_error_gnu(int severity, const int8_t *fmt, ...)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001426{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001427 va_list ap;
1428
Ed Berosetfa771012002-06-09 20:56:40 +00001429 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001431
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001432 if (severity & ERR_NOFILE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 fputs("nasm: ", error_file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001434 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001435 int8_t *currentfile = NULL;
1436 int32_t lineno = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 src_get(&lineno, &currentfile);
1438 fprintf(error_file, "%s:%ld: ", currentfile, lineno);
1439 nasm_free(currentfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001440 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 va_start(ap, fmt);
1442 report_error_common(severity, fmt, ap);
1443 va_end(ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001444}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001445
Ed Berosetfa771012002-06-09 20:56:40 +00001446/**
1447 * MS style error reporting
1448 * This function prints an error message to error_file in the
1449 * style used by Visual C and some other Microsoft tools. An example
1450 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001451 * file.asm(50) : error: blah blah blah
1452 * where file.asm is the name of the file, 50 is the line number on
1453 * which the error occurs (or is detected) and "error:" is one of
1454 * the possible optional diagnostics -- it can be "error" or "warning"
1455 * or something else. Finally the line terminates with the actual
1456 * error message.
Ed Berosetfa771012002-06-09 20:56:40 +00001457 *
1458 * @param severity the severity of the warning or error
1459 * @param fmt the printf style format string
1460 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001461static void report_error_vc(int severity, const int8_t *fmt, ...)
Ed Berosetfa771012002-06-09 20:56:40 +00001462{
1463 va_list ap;
1464
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 if (is_suppressed_warning(severity))
1466 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001467
1468 if (severity & ERR_NOFILE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 fputs("nasm: ", error_file);
Ed Berosetfa771012002-06-09 20:56:40 +00001470 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001471 int8_t *currentfile = NULL;
1472 int32_t lineno = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 src_get(&lineno, &currentfile);
1474 fprintf(error_file, "%s(%ld) : ", currentfile, lineno);
1475 nasm_free(currentfile);
Ed Berosetfa771012002-06-09 20:56:40 +00001476 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 va_start(ap, fmt);
1478 report_error_common(severity, fmt, ap);
1479 va_end(ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001480}
1481
1482/**
1483 * check for supressed warning
1484 * checks for suppressed warning or pass one only warning and we're
1485 * not in pass 1
1486 *
1487 * @param severity the severity of the warning or error
1488 * @return true if we should abort error/warning printing
1489 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490static int is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001491{
1492 /*
1493 * See if it's a suppressed warning.
1494 */
1495 return ((severity & ERR_MASK) == ERR_WARNING &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 (severity & ERR_WARN_MASK) != 0 &&
1497 suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
1498 /*
1499 * See if it's a pass-one only warning and we're not in pass one.
1500 */
1501 ((severity & ERR_PASS1) && pass0 == 2);
Ed Berosetfa771012002-06-09 20:56:40 +00001502}
1503
1504/**
1505 * common error reporting
1506 * This is the common back end of the error reporting schemes currently
1507 * implemented. It prints the nature of the warning and then the
1508 * specific error message to error_file and may or may not return. It
1509 * doesn't return if the error severity is a "panic" or "debug" type.
1510 *
1511 * @param severity the severity of the warning or error
1512 * @param fmt the printf style format string
1513 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001514static void report_error_common(int severity, const int8_t *fmt,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001516{
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001517 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 case ERR_WARNING:
1519 fputs("warning: ", error_file);
1520 break;
1521 case ERR_NONFATAL:
1522 fputs("error: ", error_file);
1523 break;
1524 case ERR_FATAL:
1525 fputs("fatal: ", error_file);
1526 break;
1527 case ERR_PANIC:
1528 fputs("panic: ", error_file);
1529 break;
1530 case ERR_DEBUG:
1531 fputs("debug: ", error_file);
1532 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001533 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001534
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 vfprintf(error_file, fmt, args);
1536 fputc('\n', error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001537
1538 if (severity & ERR_USAGE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 want_usage = TRUE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001540
1541 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542 case ERR_WARNING:
1543 case ERR_DEBUG:
1544 /* no further action, by definition */
1545 break;
1546 case ERR_NONFATAL:
1547 /* hack enables listing(!) on errors */
H. Peter Anvincaa82a12002-04-30 21:03:11 +00001548 terminate_after_phase = TRUE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 break;
1550 case ERR_FATAL:
1551 if (ofile) {
1552 fclose(ofile);
1553 remove(outname);
1554 }
1555 if (want_usage)
1556 usage();
1557 exit(1); /* instantly die */
1558 break; /* placate silly compilers */
1559 case ERR_PANIC:
1560 fflush(NULL);
1561 /* abort(); *//* halt, catch fire, and dump core */
1562 exit(3);
1563 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001564 }
1565}
1566
H. Peter Anvin734b1882002-04-30 21:01:08 +00001567static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001568{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001569 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001570}
1571
H. Peter Anvin734b1882002-04-30 21:01:08 +00001572static void register_output_formats(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001573{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 ofmt = ofmt_register(report_error);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001575}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001576
1577#define BUF_DELTA 512
1578
1579static FILE *no_pp_fp;
1580static efunc no_pp_err;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001581static ListGen *no_pp_list;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001582static int32_t no_pp_lineinc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583
Keith Kaniosb7a89542007-04-12 02:40:54 +00001584static void no_pp_reset(int8_t *file, int pass, efunc error, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001586{
1587 src_set_fname(nasm_strdup(file));
1588 src_set_linnum(0);
1589 no_pp_lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001590 no_pp_err = error;
1591 no_pp_fp = fopen(file, "r");
1592 if (!no_pp_fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 no_pp_err(ERR_FATAL | ERR_NOFILE,
1594 "unable to open input file `%s'", file);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001595 no_pp_list = listgen;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 (void)pass; /* placate compilers */
1597 (void)eval; /* placate compilers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598}
1599
Keith Kaniosb7a89542007-04-12 02:40:54 +00001600static int8_t *no_pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001601{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001602 int8_t *buffer, *p, *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 int bufsize;
1604
1605 bufsize = BUF_DELTA;
1606 buffer = nasm_malloc(BUF_DELTA);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001607 src_set_linnum(src_get_linnum() + no_pp_lineinc);
1608
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 while (1) { /* Loop to handle %line */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001610
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 p = buffer;
1612 while (1) { /* Loop to handle long lines */
1613 q = fgets(p, bufsize - (p - buffer), no_pp_fp);
1614 if (!q)
1615 break;
1616 p += strlen(p);
1617 if (p > buffer && p[-1] == '\n')
1618 break;
1619 if (p - buffer > bufsize - 10) {
1620 int offset;
1621 offset = p - buffer;
1622 bufsize += BUF_DELTA;
1623 buffer = nasm_realloc(buffer, bufsize);
1624 p = buffer + offset;
1625 }
1626 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 if (!q && p == buffer) {
1629 nasm_free(buffer);
1630 return NULL;
1631 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001632
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 /*
1634 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1635 * them are present at the end of the line.
1636 */
1637 buffer[strcspn(buffer, "\r\n\032")] = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001638
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 if (!strncmp(buffer, "%line", 5)) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001640 int32_t ln;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 int li;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001642 int8_t *nm = nasm_malloc(strlen(buffer));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 if (sscanf(buffer + 5, "%ld+%d %s", &ln, &li, nm) == 3) {
1644 nasm_free(src_set_fname(nm));
1645 src_set_linnum(ln);
1646 no_pp_lineinc = li;
1647 continue;
1648 }
1649 nasm_free(nm);
1650 }
1651 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001652 }
1653
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 no_pp_list->line(LIST_READ, buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001655
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001656 return buffer;
1657}
1658
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659static void no_pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001660{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661 fclose(no_pp_fp);
1662}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001663
Keith Kaniosb7a89542007-04-12 02:40:54 +00001664static uint32_t get_cpu(int8_t *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001665{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001666
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 if (!strcmp(value, "8086"))
1668 return IF_8086;
1669 if (!strcmp(value, "186"))
1670 return IF_186;
1671 if (!strcmp(value, "286"))
1672 return IF_286;
1673 if (!strcmp(value, "386"))
1674 return IF_386;
1675 if (!strcmp(value, "486"))
1676 return IF_486;
1677 if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
1678 return IF_PENT;
1679 if (!strcmp(value, "686") ||
1680 !nasm_stricmp(value, "ppro") ||
1681 !nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
1682 return IF_P6;
1683 if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
1684 return IF_KATMAI;
1685 if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
1686 !nasm_stricmp(value, "willamette"))
1687 return IF_WILLAMETTE;
1688 if (!nasm_stricmp(value, "prescott"))
1689 return IF_PRESCOTT;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001690 if (!nasm_stricmp(value, "x64") ||
1691 !nasm_stricmp(value, "x86-64"))
1692 return IF_X64;
H. Peter Anvin3ab8de62002-05-28 01:25:06 +00001693 if (!nasm_stricmp(value, "ia64") ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 !nasm_stricmp(value, "ia-64") ||
1695 !nasm_stricmp(value, "itanium") ||
1696 !nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
1697 return IF_IA64;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001698
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
1700 "unknown 'cpu' type");
H. Peter Anvin734b1882002-04-30 21:01:08 +00001701
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 return IF_PLEVEL; /* the maximum level */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001703}
1704
Keith Kaniosb7a89542007-04-12 02:40:54 +00001705static int get_bits(int8_t *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706{
1707 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001708
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 if ((i = atoi(value)) == 16)
1710 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711 else if (i == 32) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 if (cpu < IF_386) {
1713 report_error(ERR_NONFATAL,
1714 "cannot specify 32-bit segment on processor below a 386");
1715 i = 16;
1716 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001717 } else if (i == 64) {
1718 if (cpu < IF_X64) {
1719 report_error(ERR_NONFATAL,
1720 "cannot specify 64-bit segment on processor below an x86-64");
1721 i = 16;
1722 }
1723 if (i != maxbits) {
1724 report_error(ERR_NONFATAL,
1725 "%s output format does not support 64-bit code",
1726 ofmt->shortname);
1727 i = 16;
1728 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001729 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001731 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 value);
1733 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001734 }
1735 return i;
1736}
1737
1738/* end of nasm.c */