nasm/nasm.c

2148 lines
67 KiB
C
Raw Normal View History

2002-04-30 20:51:32 +00:00
/* The Netwide Assembler main program module
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the license given in the file "LICENSE"
2002-04-30 20:51:32 +00:00
* distributed in the NASM archive.
*/
#include "compiler.h"
2002-04-30 20:51:32 +00:00
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <time.h>
2002-04-30 20:51:32 +00:00
#include "nasm.h"
#include "nasmlib.h"
#include "saa.h"
#include "raa.h"
#include "float.h"
#include "stdscan.h"
2002-04-30 20:59:21 +00:00
#include "insns.h"
2002-04-30 20:52:08 +00:00
#include "preproc.h"
2002-04-30 20:51:32 +00:00
#include "parser.h"
2002-04-30 20:52:49 +00:00
#include "eval.h"
2002-04-30 20:51:32 +00:00
#include "assemble.h"
#include "labels.h"
#include "outform.h"
2002-04-30 20:52:26 +00:00
#include "listing.h"
2002-04-30 20:51:32 +00:00
2005-01-15 22:15:51 +00:00
struct forwrefinfo { /* info held on forward refs. */
2002-04-30 20:53:55 +00:00
int lineno;
int operand;
};
static int get_bits(char *value);
static uint32_t get_cpu(char *cpu_str);
static void parse_cmdline(int, char **);
static void assemble_file(char *, StrList **);
2002-04-30 20:51:32 +00:00
static void register_output_formats(void);
static void report_error_gnu(int severity, const char *fmt, ...);
static void report_error_vc(int severity, const char *fmt, ...);
static void report_error_common(int severity, const char *fmt,
2005-01-15 22:15:51 +00:00
va_list args);
static bool is_suppressed_warning(int severity);
2002-04-30 20:51:32 +00:00
static void usage(void);
static efunc report_error;
2002-04-30 20:51:32 +00:00
static int using_debug_info, opt_verbose_info;
bool tasm_compatible_mode = false;
int pass0, passn;
int maxbits = 0;
int globalrel = 0;
2002-04-30 20:53:55 +00:00
time_t official_compile_time;
static char inname[FILENAME_MAX];
static char outname[FILENAME_MAX];
static char listname[FILENAME_MAX];
static char errname[FILENAME_MAX];
2005-01-15 22:15:51 +00:00
static int globallineno; /* for forward-reference tracking */
2002-04-30 21:00:33 +00:00
/* static int pass = 0; */
2002-04-30 20:51:32 +00:00
static struct ofmt *ofmt = NULL;
2005-01-15 22:15:51 +00:00
static FILE *error_file; /* Where to write error messages */
2002-04-30 20:57:38 +00:00
2002-04-30 20:51:32 +00:00
static FILE *ofile = NULL;
2005-01-15 22:15:51 +00:00
int optimizing = -1; /* number of optimization passes to take */
static int sb, cmd_sb = 16; /* by default */
static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
bool global_offset_changed; /* referenced in labels.c */
2002-04-30 20:51:32 +00:00
static struct location location;
2005-01-15 22:15:51 +00:00
int in_abs_seg; /* Flag we are in ABSOLUTE seg */
int32_t abs_seg; /* ABSOLUTE segment basis */
int32_t abs_offset; /* ABSOLUTE offset */
2002-04-30 20:52:26 +00:00
2002-04-30 20:51:32 +00:00
static struct RAA *offsets;
2002-04-30 20:51:53 +00:00
2005-01-15 22:15:51 +00:00
static struct SAA *forwrefs; /* keep track of forward references */
static const struct forwrefinfo *forwref;
2002-04-30 20:51:32 +00:00
2002-04-30 20:52:08 +00:00
static Preproc *preproc;
2002-04-30 20:57:38 +00:00
enum op_type {
2005-01-15 22:15:51 +00:00
op_normal, /* Preprocess and assemble */
op_preprocess, /* Preprocess only */
op_depend, /* Generate dependencies */
2002-04-30 20:57:38 +00:00
};
static enum op_type operating_mode;
/* Dependency flags */
static bool depend_emit_phony = false;
static bool depend_missing_ok = false;
static const char *depend_target = NULL;
static const char *depend_file = NULL;
2002-04-30 20:52:08 +00:00
2002-04-30 20:52:26 +00:00
/*
* Which of the suppressible warnings are suppressed. Entry zero
* isn't an actual warning, but it used for -w+error/-Werror.
2002-04-30 20:52:26 +00:00
*/
static bool suppressed[ERR_WARN_MAX+1] = {
true, false, true, false, false, true, false, true, true, false
2002-04-30 20:52:26 +00:00
};
/*
* The option names for the suppressible warnings. As before, entry
* zero does nothing.
*/
static const char *suppressed_names[ERR_WARN_MAX+1] = {
"error", "macro-params", "macro-selfref", "orphan-labels",
"number-overflow", "gnu-elf-extensions", "float-overflow",
"float-denorm", "float-underflow", "float-toolong"
2002-04-30 20:52:26 +00:00
};
/*
* The explanations for the suppressible warnings. As before, entry
* zero does nothing.
*/
static const char *suppressed_what[ERR_WARN_MAX+1] = {
"treat warnings as errors",
"macro calls with wrong parameter count",
"cyclic macro references",
2002-04-30 20:52:49 +00:00
"labels alone on lines without trailing `:'",
"numeric constants does not fit in 64 bits",
"using 8- or 16-bit relocation in ELF32, a GNU extension",
"floating point overflow",
"floating point denormal",
"floating point underflow",
"too many digits in floating-point number"
2002-04-30 20:52:26 +00:00
};
2002-04-30 20:52:08 +00:00
/*
* This is a null preprocessor which just copies lines from input
* to output. It's used when someone explicitly requests that NASM
* not preprocess their source file.
*/
static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *, StrList **);
static char *no_pp_getline(void);
2005-01-15 22:15:51 +00:00
static void no_pp_cleanup(int);
2002-04-30 20:52:08 +00:00
static Preproc no_pp = {
no_pp_reset,
no_pp_getline,
no_pp_cleanup
};
2002-04-30 20:51:32 +00:00
/*
* get/set current offset...
*/
2002-04-30 20:59:21 +00:00
#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
2002-04-30 20:53:55 +00:00
raa_read(offsets,location.segment))
2002-04-30 20:59:21 +00:00
#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
2002-04-30 20:53:55 +00:00
(void)(offsets=raa_write(offsets,location.segment,(x))))
2002-04-30 20:51:32 +00:00
static int want_usage;
static int terminate_after_phase;
2005-01-15 22:15:51 +00:00
int user_nolist = 0; /* fbk 9/2/00 */
2002-04-30 20:51:32 +00:00
static void nasm_fputs(const char *line, FILE * outfile)
2002-04-30 20:53:55 +00:00
{
2002-05-14 22:38:55 +00:00
if (outfile) {
2005-01-15 22:15:51 +00:00
fputs(line, outfile);
putc('\n', outfile);
2002-04-30 20:53:55 +00:00
} else
2005-01-15 22:15:51 +00:00
puts(line);
2002-04-30 20:53:55 +00:00
}
/* Convert a struct tm to a POSIX-style time constant */
static int64_t posix_mktime(struct tm *tm)
{
int64_t t;
int64_t y = tm->tm_year;
/* See IEEE 1003.1:2004, section 4.14 */
t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
t += tm->tm_yday;
t *= 24;
t += tm->tm_hour;
t *= 60;
t += tm->tm_min;
t *= 60;
t += tm->tm_sec;
return t;
}
static void define_macros_early(void)
{
char temp[128];
struct tm lt, *lt_p, gm, *gm_p;
int64_t posix_time;
lt_p = localtime(&official_compile_time);
if (lt_p) {
lt = *lt_p;
strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
pp_pre_define(temp);
}
gm_p = gmtime(&official_compile_time);
if (gm_p) {
gm = *gm_p;
strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
pp_pre_define(temp);
}
if (gm_p)
posix_time = posix_mktime(&gm);
else if (lt_p)
posix_time = posix_mktime(&lt);
else
posix_time = 0;
if (posix_time) {
snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
pp_pre_define(temp);
}
}
static void define_macros_late(void)
{
char temp[128];
snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
ofmt->shortname);
pp_pre_define(temp);
}
static void emit_dependencies(StrList *list)
{
FILE *deps;
int linepos, len;
StrList *l, *nl;
if (depend_file && strcmp(depend_file, "-")) {
deps = fopen(depend_file, "w");
if (!deps) {
report_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
"unable to write dependency file `%s'", depend_file);
return;
}
} else {
deps = stdout;
}
linepos = fprintf(deps, "%s:", depend_target);
for (l = list; l; l = l->next) {
len = strlen(l->str);
if (linepos + len > 62) {
fprintf(deps, " \\\n ");
linepos = 1;
}
fprintf(deps, " %s", l->str);
linepos += len+1;
}
fprintf(deps, "\n\n");
for (l = list; l; l = nl) {
if (depend_emit_phony)
fprintf(deps, "%s:\n\n", l->str);
nl = l->next;
nasm_free(l);
}
if (deps != stdout)
fclose(deps);
}
int main(int argc, char **argv)
2002-04-30 20:53:55 +00:00
{
StrList *depend_list = NULL, **depend_ptr;
time(&official_compile_time);
pass0 = 1;
want_usage = terminate_after_phase = false;
report_error = report_error_gnu;
2002-04-30 20:51:32 +00:00
error_file = stderr;
tolower_init();
2005-01-15 22:15:51 +00:00
nasm_set_malloc_error(report_error);
2002-04-30 20:51:32 +00:00
offsets = raa_init();
forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
2002-04-30 20:51:32 +00:00
2002-04-30 20:52:08 +00:00
preproc = &nasmpp;
2002-04-30 20:57:38 +00:00
operating_mode = op_normal;
2002-04-30 21:01:08 +00:00
2002-04-30 20:51:32 +00:00
seg_init();
register_output_formats();
/* Define some macros dependent on the runtime, but not
on the command line. */
define_macros_early();
2002-04-30 20:51:32 +00:00
parse_cmdline(argc, argv);
2005-01-15 22:15:51 +00:00
if (terminate_after_phase) {
if (want_usage)
usage();
return 1;
2002-04-30 20:51:32 +00:00
}
/* If debugging info is disabled, suppress any debug calls */
if (!using_debug_info)
ofmt->current_dfmt = &null_debug_form;
2002-04-30 20:52:49 +00:00
if (ofmt->stdmac)
2005-01-15 22:15:51 +00:00
pp_extra_stdmac(ofmt->stdmac);
parser_global_info(ofmt, &location);
eval_global_info(ofmt, lookup_label, &location);
2002-04-30 20:52:49 +00:00
2002-04-30 20:59:21 +00:00
/* define some macros dependent of command-line */
define_macros_late();
2002-04-30 20:59:21 +00:00
depend_ptr = (depend_file || (operating_mode == op_depend))
? &depend_list : NULL;
if (!depend_target)
depend_target = outname;
2005-01-15 22:15:51 +00:00
switch (operating_mode) {
2002-04-30 20:57:38 +00:00
case op_depend:
2005-01-15 22:15:51 +00:00
{
char *line;
if (depend_missing_ok)
pp_include_path(NULL); /* "assume generated" */
preproc->reset(inname, 0, report_error, evaluate, &nasmlist,
depend_ptr);
2005-01-15 22:15:51 +00:00
if (outname[0] == '\0')
ofmt->filename(inname, outname, report_error);
ofile = NULL;
while ((line = preproc->getline()))
nasm_free(line);
preproc->cleanup(0);
}
break;
2002-04-30 20:57:38 +00:00
case op_preprocess:
2005-01-15 22:15:51 +00:00
{
char *line;
char *file_name = NULL;
int32_t prior_linnum = 0;
2005-01-15 22:15:51 +00:00
int lineinc = 0;
if (*outname) {
ofile = fopen(outname, "w");
if (!ofile)
report_error(ERR_FATAL | ERR_NOFILE,
"unable to open output file `%s'",
outname);
} else
ofile = NULL;
location.known = false;
2002-04-30 21:01:08 +00:00
/* pass = 1; */
preproc->reset(inname, 2, report_error, evaluate, &nasmlist,
depend_ptr);
2005-01-15 22:15:51 +00:00
while ((line = preproc->getline())) {
/*
* We generate %line directives if needed for later programs
*/
int32_t linnum = prior_linnum += lineinc;
2005-01-15 22:15:51 +00:00
int altline = src_get(&linnum, &file_name);
if (altline) {
if (altline == 1 && lineinc == 1)
nasm_fputs("", ofile);
else {
lineinc = (altline != -1 || lineinc != 1);
fprintf(ofile ? ofile : stdout,
2007-04-14 00:10:59 +00:00
"%%line %"PRId32"+%d %s\n", linnum, lineinc,
2005-01-15 22:15:51 +00:00
file_name);
}
prior_linnum = linnum;
}
nasm_fputs(line, ofile);
nasm_free(line);
}
nasm_free(file_name);
preproc->cleanup(0);
if (ofile)
fclose(ofile);
if (ofile && terminate_after_phase)
remove(outname);
}
break;
2002-04-30 20:57:38 +00:00
case op_normal:
2005-01-15 22:15:51 +00:00
{
/*
* We must call ofmt->filename _anyway_, even if the user
* has specified their own output file, because some
* formats (eg OBJ and COFF) use ofmt->filename to find out
* the name of the input file and then put that inside the
* file.
*/
ofmt->filename(inname, outname, report_error);
ofile = fopen(outname, "wb");
if (!ofile) {
report_error(ERR_FATAL | ERR_NOFILE,
"unable to open output file `%s'", outname);
}
/*
* We must call init_labels() before ofmt->init() since
* some object formats will want to define labels in their
* init routines. (eg OS/2 defines the FLAT group)
*/
init_labels();
ofmt->init(ofile, report_error, define_label, evaluate);
assemble_file(inname, depend_ptr);
2005-01-15 22:15:51 +00:00
if (!terminate_after_phase) {
ofmt->cleanup(using_debug_info);
cleanup_labels();
} else {
/*
* We had an fclose on the output file here, but we
* actually do that in all the object file drivers as well,
* so we're leaving out the one here.
* fclose (ofile);
*/
remove(outname);
if (listname[0])
remove(listname);
}
}
break;
2002-04-30 20:51:32 +00:00
}
2002-04-30 21:01:08 +00:00
if (depend_list)
emit_dependencies(depend_list);
2002-04-30 20:51:32 +00:00
if (want_usage)
2005-01-15 22:15:51 +00:00
usage();
2002-04-30 21:01:08 +00:00
2005-01-15 22:15:51 +00:00
raa_free(offsets);
saa_free(forwrefs);
eval_cleanup();
stdscan_cleanup();
2002-04-30 20:51:32 +00:00
2002-04-30 20:52:26 +00:00
if (terminate_after_phase)
2005-01-15 22:15:51 +00:00
return 1;
2002-04-30 20:52:26 +00:00
else
2005-01-15 22:15:51 +00:00
return 0;
2002-04-30 20:51:32 +00:00
}
2002-04-30 20:53:55 +00:00
/*
* Get a parameter for a command line option.
* First arg must be in the form of e.g. -f...
*/
static char *get_param(char *p, char *q, bool *advance)
2002-04-30 20:53:55 +00:00
{
*advance = false;
2005-01-15 22:15:51 +00:00
if (p[2]) { /* the parameter's in the option */
p += 2;
while (nasm_isspace(*p))
2005-01-15 22:15:51 +00:00
p++;
return p;
2002-04-30 20:53:55 +00:00
}
2005-01-15 22:15:51 +00:00
if (q && q[0]) {
*advance = true;
2005-01-15 22:15:51 +00:00
return q;
2002-04-30 20:53:55 +00:00
}
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"option `-%c' requires an argument", p[1]);
2002-04-30 20:53:55 +00:00
return NULL;
}
/*
* Copy a filename
*/
static void copy_filename(char *dst, const char *src)
{
size_t len = strlen(src);
if (len >= (size_t)FILENAME_MAX) {
report_error(ERR_FATAL | ERR_NOFILE, "file name too long");
return;
}
strncpy(dst, src, FILENAME_MAX);
}
/*
* Convert a string to Make-safe form
*/
static char *quote_for_make(const char *str)
{
const char *p;
char *os, *q;
size_t n = 1; /* Terminating zero */
size_t nbs = 0;
if (!str)
return NULL;
for (p = str; *p; p++) {
switch (*p) {
case ' ':
case '\t':
/* Convert N backslashes + ws -> 2N+1 backslashes + ws */
n += nbs + 2;
nbs = 0;
break;
case '$':
case '#':
nbs = 0;
n += 2;
break;
case '\\':
nbs++;
n++;
break;
default:
nbs = 0;
n++;
break;
}
}
/* Convert N backslashes at the end of filename to 2N backslashes */
if (nbs)
n += nbs;
os = q = nasm_malloc(n);
nbs = 0;
for (p = str; *p; p++) {
switch (*p) {
case ' ':
case '\t':
while (nbs--)
*q++ = '\\';
*q++ = '\\';
*q++ = *p;
break;
case '$':
*q++ = *p;
*q++ = *p;
nbs = 0;
break;
case '#':
*q++ = '\\';
*q++ = *p;
nbs = 0;
break;
case '\\':
*q++ = *p;
nbs++;
break;
default:
*q++ = *p;
nbs = 0;
break;
}
}
while (nbs--)
*q++ = '\\';
*q = '\0';
return os;
}
2005-01-15 22:15:51 +00:00
struct textargs {
const char *label;
2005-01-15 22:15:51 +00:00
int value;
2002-04-30 21:00:33 +00:00
};
#define OPT_PREFIX 0
#define OPT_POSTFIX 1
2005-01-15 22:15:51 +00:00
struct textargs textopts[] = {
{"prefix", OPT_PREFIX},
{"postfix", OPT_POSTFIX},
{NULL, 0}
2002-04-30 21:00:33 +00:00
};
static bool stopoptions = false;
static bool process_arg(char *p, char *q)
2002-04-30 20:53:55 +00:00
{
char *param;
int i;
bool advance = false;
bool suppress;
2002-04-30 20:51:32 +00:00
2002-04-30 20:52:26 +00:00
if (!p || !p[0])
return false;
2005-01-15 22:15:51 +00:00
if (p[0] == '-' && !stopoptions) {
if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
/* These parameters take values */
if (!(param = get_param(p, q, &advance)))
return advance;
}
2005-01-15 22:15:51 +00:00
switch (p[1]) {
case 's':
error_file = stdout;
break;
case 'o': /* output file */
copy_filename(outname, param);
break;
case 'f': /* output format */
ofmt = ofmt_find(param);
if (!ofmt) {
report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"unrecognised output format `%s' - "
"use -hf for a list", param);
} else {
ofmt->current_dfmt = ofmt->debug_formats[0];
}
break;
case 'O': /* Optimization level */
{
int opt;
if (!*param) {
/* Naked -O == -Ox */
optimizing = INT_MAX >> 1; /* Almost unlimited */
} else {
while (*param) {
switch (*param) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
opt = strtoul(param, &param, 10);
/* -O0 -> optimizing == -1, 0.98 behaviour */
/* -O1 -> optimizing == 0, 0.98.09 behaviour */
if (opt < 2)
optimizing = opt - 1;
else
optimizing = opt;
break;
case 'v':
case '+':
param++;
opt_verbose_info = true;
break;
case 'x':
param++;
optimizing = INT_MAX >> 1; /* Almost unlimited */
break;
default:
report_error(ERR_FATAL,
"unknown optimization option -O%c\n",
*param);
break;
}
}
}
break;
}
case 'p': /* pre-include */
case 'P':
pp_pre_include(param);
break;
case 'd': /* pre-define */
case 'D':
pp_pre_define(param);
break;
case 'u': /* un-define */
case 'U':
pp_pre_undefine(param);
break;
case 'i': /* include search path */
case 'I':
pp_include_path(param);
break;
case 'l': /* listing file */
copy_filename(listname, param);
break;
case 'Z': /* error messages file */
strcpy(errname, param);
break;
case 'F': /* specify debug format */
ofmt->current_dfmt = dfmt_find(ofmt, param);
if (!ofmt->current_dfmt) {
report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"unrecognized debug format `%s' for"
" output format `%s'",
param, ofmt->shortname);
}
using_debug_info = true;
break;
case 'X': /* specify error reporting format */
if (nasm_stricmp("vc", param) == 0)
report_error = report_error_vc;
else if (nasm_stricmp("gnu", param) == 0)
report_error = report_error_gnu;
else
report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"unrecognized error reporting format `%s'",
param);
2005-01-15 22:15:51 +00:00
break;
2005-01-15 22:15:51 +00:00
case 'g':
using_debug_info = true;
2005-01-15 22:15:51 +00:00
break;
2005-01-15 22:15:51 +00:00
case 'h':
printf
("usage: nasm [-@ response file] [-o outfile] [-f format] "
"[-l listfile]\n"
" [options...] [--] filename\n"
" or nasm -v for version info\n\n"
2005-01-15 22:15:51 +00:00
" -t assemble in SciTech TASM compatible mode\n"
" -g generate debug information in selected format.\n");
printf
(" -E (or -e) preprocess only (writes output to stdout by default)\n"
2005-01-15 22:15:51 +00:00
" -a don't preprocess (assemble only)\n"
" -M generate Makefile dependencies on stdout\n"
" -MG d:o, missing files assumed generated\n\n"
" -Z<file> redirect error messages to file\n"
2005-01-15 22:15:51 +00:00
" -s redirect error messages to stdout\n\n"
" -F format select a debugging format\n\n"
" -I<path> adds a pathname to the include file path\n");
printf
(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
" -P<file> pre-includes a file\n"
" -D<macro>[=<value>] pre-defines a macro\n"
" -U<macro> undefines a macro\n"
" -X<format> specifies error reporting format (gnu or vc)\n"
" -w+foo enables warning foo (equiv. -Wfoo)\n"
" -w-foo disable warning foo (equiv. -Wno-foo)\n"
"Warnings:\n");
for (i = 0; i <= ERR_WARN_MAX; i++)
2005-01-15 22:15:51 +00:00
printf(" %-23s %s (default %s)\n",
suppressed_names[i], suppressed_what[i],
suppressed[i] ? "off" : "on");
printf
("\nresponse files should contain command line parameters"
", one per line.\n");
if (p[2] == 'f') {
printf("\nvalid output formats for -f are"
" (`*' denotes default):\n");
ofmt_list(ofmt, stdout);
} else {
printf("\nFor a list of valid output formats, use -hf.\n");
printf("For a list of debug formats, use -f <form> -y.\n");
}
exit(0); /* never need usage message here */
break;
2005-01-15 22:15:51 +00:00
case 'y':
printf("\nvalid debug formats for '%s' output format are"
" ('*' denotes default):\n", ofmt->shortname);
dfmt_list(ofmt, stdout);
exit(0);
break;
2005-01-15 22:15:51 +00:00
case 't':
tasm_compatible_mode = true;
2005-01-15 22:15:51 +00:00
break;
2005-01-15 22:15:51 +00:00
case 'v':
{
const char *nasm_version_string =
2005-01-15 22:15:51 +00:00
"NASM version " NASM_VER " compiled on " __DATE__
2002-04-30 20:53:55 +00:00
#ifdef DEBUG
2005-01-15 22:15:51 +00:00
" with -DDEBUG"
2002-04-30 20:53:55 +00:00
#endif
2005-01-15 22:15:51 +00:00
;
puts(nasm_version_string);
exit(0); /* never need usage message here */
}
break;
2005-01-15 22:15:51 +00:00
case 'e': /* preprocess only */
case 'E':
2005-01-15 22:15:51 +00:00
operating_mode = op_preprocess;
break;
2005-01-15 22:15:51 +00:00
case 'a': /* assemble only - don't preprocess */
preproc = &no_pp;
break;
case 'W':
if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
suppress = true;
param += 3;
} else {
suppress = false;
}
goto set_warning;
2005-01-15 22:15:51 +00:00
case 'w':
if (param[0] != '+' && param[0] != '-') {
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"invalid option to `-w'");
break;
2005-01-15 22:15:51 +00:00
}
suppress = (param[0] == '-');
param++;
goto set_warning;
set_warning:
for (i = 0; i <= ERR_WARN_MAX; i++)
if (!nasm_stricmp(param, suppressed_names[i]))
break;
if (i <= ERR_WARN_MAX)
suppressed[i] = suppress;
else if (!nasm_stricmp(param, "all"))
for (i = 1; i <= ERR_WARN_MAX; i++)
suppressed[i] = suppress;
else if (!nasm_stricmp(param, "none"))
for (i = 1; i <= ERR_WARN_MAX; i++)
suppressed[i] = !suppress;
else
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"invalid warning `%s'", param);
2005-01-15 22:15:51 +00:00
break;
2005-01-15 22:15:51 +00:00
case 'M':
switch (p[2]) {
case 0:
operating_mode = op_depend;
break;
case 'G':
operating_mode = op_depend;
depend_missing_ok = true;
break;
case 'P':
depend_emit_phony = true;
break;
case 'D':
depend_file = q;
advance = true;
break;
case 'T':
depend_target = q;
advance = true;
break;
case 'Q':
depend_target = quote_for_make(q);
advance = true;
break;
default:
report_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
"unknown dependency option `-M%c'", p[2]);
break;
}
if (advance && (!q || !q[0])) {
report_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
"option `-M%c' requires a parameter", p[2]);
break;
}
2005-01-15 22:15:51 +00:00
break;
case '-':
{
int s;
if (p[2] == 0) { /* -- => stop processing options */
stopoptions = 1;
break;
}
for (s = 0; textopts[s].label; s++) {
if (!nasm_stricmp(p + 2, textopts[s].label)) {
break;
}
}
switch (s) {
case OPT_PREFIX:
case OPT_POSTFIX:
{
if (!q) {
report_error(ERR_NONFATAL | ERR_NOFILE |
ERR_USAGE,
"option `--%s' requires an argument",
p + 2);
break;
} else {
advance = 1, param = q;
}
if (s == OPT_PREFIX) {
strncpy(lprefix, param, PREFIX_MAX - 1);
lprefix[PREFIX_MAX - 1] = 0;
break;
}
if (s == OPT_POSTFIX) {
strncpy(lpostfix, param, POSTFIX_MAX - 1);
lpostfix[POSTFIX_MAX - 1] = 0;
break;
}
break;
}
default:
{
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"unrecognised option `--%s'", p + 2);
break;
}
}
break;
}
default:
if (!ofmt->setinfo(GI_SWITCH, &p))
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"unrecognised option `-%c'", p[1]);
break;
}
} else {
if (*inname) {
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"more than one input file specified");
} else {
copy_filename(inname, p);
}
2002-04-30 20:52:26 +00:00
}
return advance;
}
2002-04-30 20:53:55 +00:00
#define ARG_BUF_DELTA 128
2005-01-15 22:15:51 +00:00
static void process_respfile(FILE * rfile)
2002-04-30 20:53:55 +00:00
{
char *buffer, *p, *q, *prevarg;
2002-04-30 20:53:55 +00:00
int bufsize, prevargsize;
bufsize = prevargsize = ARG_BUF_DELTA;
buffer = nasm_malloc(ARG_BUF_DELTA);
prevarg = nasm_malloc(ARG_BUF_DELTA);
prevarg[0] = '\0';
2005-01-15 22:15:51 +00:00
while (1) { /* Loop to handle all lines in file */
p = buffer;
while (1) { /* Loop to handle long lines */
q = fgets(p, bufsize - (p - buffer), rfile);
if (!q)
break;
p += strlen(p);
if (p > buffer && p[-1] == '\n')
break;
if (p - buffer > bufsize - 10) {
int offset;
offset = p - buffer;
bufsize += ARG_BUF_DELTA;
buffer = nasm_realloc(buffer, bufsize);
p = buffer + offset;
}
}
if (!q && p == buffer) {
if (prevarg[0])
process_arg(prevarg, NULL);
nasm_free(buffer);
nasm_free(prevarg);
return;
}
/*
* Play safe: remove CRs, LFs and any spurious ^Zs, if any of
* them are present at the end of the line.
*/
*(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
while (p > buffer && nasm_isspace(p[-1]))
2005-01-15 22:15:51 +00:00
*--p = '\0';
p = buffer;
while (nasm_isspace(*p))
2005-01-15 22:15:51 +00:00
p++;
if (process_arg(prevarg, p))
*p = '\0';
2007-10-19 02:02:42 +00:00
if ((int) strlen(p) > prevargsize - 10) {
2005-01-15 22:15:51 +00:00
prevargsize += ARG_BUF_DELTA;
prevarg = nasm_realloc(prevarg, prevargsize);
}
strncpy(prevarg, p, prevargsize);
2002-04-30 20:53:55 +00:00
}
}
2002-04-30 21:00:33 +00:00
/* Function to process args from a string of args, rather than the
* argv array. Used by the environment variable and response file
* processing.
*/
static void process_args(char *args)
2005-01-15 22:15:51 +00:00
{
char *p, *q, *arg, *prevarg;
char separator = ' ';
2002-04-30 21:00:33 +00:00
p = args;
if (*p && *p != '-')
2005-01-15 22:15:51 +00:00
separator = *p++;
2002-04-30 21:00:33 +00:00
arg = NULL;
while (*p) {
2005-01-15 22:15:51 +00:00
q = p;
while (*p && *p != separator)
p++;
while (*p == separator)
*p++ = '\0';
prevarg = arg;
arg = q;
if (process_arg(prevarg, arg))
arg = NULL;
2002-04-30 21:00:33 +00:00
}
if (arg)
2005-01-15 22:15:51 +00:00
process_arg(arg, NULL);
2002-04-30 21:00:33 +00:00
}
static void process_response_file(const char *file)
{
char str[2048];
FILE *f = fopen(file, "r");
if (!f) {
perror(file);
exit(-1);
}
while (fgets(str, sizeof str, f)) {
process_args(str);
}
fclose(f);
}
static void parse_cmdline(int argc, char **argv)
2002-04-30 20:53:55 +00:00
{
FILE *rfile;
char *envreal, *envcopy = NULL, *p, *arg;
2002-04-30 20:52:26 +00:00
*inname = *outname = *listname = *errname = '\0';
2002-04-30 20:52:26 +00:00
/*
2002-05-06 19:41:57 +00:00
* First, process the NASMENV environment variable.
2002-04-30 20:52:26 +00:00
*/
2002-05-06 19:41:57 +00:00
envreal = getenv("NASMENV");
2002-04-30 20:52:26 +00:00
arg = NULL;
if (envreal) {
2005-01-15 22:15:51 +00:00
envcopy = nasm_strdup(envreal);
process_args(envcopy);
nasm_free(envcopy);
2002-04-30 20:52:26 +00:00
}
/*
* Now process the actual command line.
*/
2005-01-15 22:15:51 +00:00
while (--argc) {
bool advance;
2005-01-15 22:15:51 +00:00
argv++;
if (argv[0][0] == '@') {
/* We have a response file, so process this as a set of
* arguments like the environment variable. This allows us
* to have multiple arguments on a single line, which is
* different to the -@resp file processing below for regular
* NASM.
*/
process_response_file(argv[0]+1);
2005-01-15 22:15:51 +00:00
argc--;
argv++;
}
if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
if (p) {
rfile = fopen(p, "r");
if (rfile) {
2005-01-15 22:15:51 +00:00
process_respfile(rfile);
fclose(rfile);
} else
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"unable to open response file `%s'", p);
}
} else
advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
argv += advance, argc -= advance;
2002-04-30 20:51:32 +00:00
}
2002-04-30 20:52:26 +00:00
/* Look for basic command line typos. This definitely doesn't
catch all errors, but it might help cases of fumbled fingers. */
2002-04-30 20:51:32 +00:00
if (!*inname)
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"no input file specified");
else if (!strcmp(inname, errname) ||
!strcmp(inname, outname) ||
!strcmp(inname, listname) ||
(depend_file && !strcmp(inname, depend_file)))
report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"file `%s' is both input and output file",
inname);
if (*errname) {
error_file = fopen(errname, "w");
if (!error_file) {
error_file = stderr; /* Revert to default! */
report_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"cannot open file `%s' for error messages",
errname);
}
}
2002-04-30 20:51:32 +00:00
}
/* List of directives */
enum directives {
D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT,
D_EXTERN, D_FLOAT, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING
};
static const char *directives[] = {
"", "absolute", "bits", "common", "cpu", "debug", "default",
"extern", "float", "global", "list", "section", "segment", "warning"
};
static enum directives getkw(char **directive, char **value);
static void assemble_file(char *fname, StrList **depend_ptr)
2002-04-30 20:53:55 +00:00
{
char *directive, *value, *p, *q, *special, *line, debugid[80];
2005-01-15 22:15:51 +00:00
insn output_ins;
int i, validid;
bool rn_error;
int32_t seg;
int64_t offs;
2002-04-30 20:52:49 +00:00
struct tokenval tokval;
2005-01-15 22:15:51 +00:00
expr *e;
int pass_max;
2002-04-30 20:51:32 +00:00
2002-04-30 20:59:21 +00:00
if (cmd_sb == 32 && cmd_cpu < IF_386)
2005-01-15 22:15:51 +00:00
report_error(ERR_FATAL, "command line: "
"32-bit segment size requires a higher cpu");
pass_max = (optimizing > 0 ? optimizing : 0) + 2; /* passes 1, optimizing, then 2 */
pass0 = !(optimizing > 0); /* start at 1 if not optimizing */
for (passn = 1; pass0 <= 2; passn++) {
2005-01-15 22:15:51 +00:00
int pass1, pass2;
ldfunc def_label;
pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
/* pass0 0, 0, 0, ..., 1, 2 */
2005-01-15 22:15:51 +00:00
def_label = passn > 1 ? redefine_label : define_label;
2005-01-15 22:15:51 +00:00
globalbits = sb = cmd_sb; /* set 'bits' to command line default */
2005-01-15 22:15:51 +00:00
cpu = cmd_cpu;
if (pass0 == 2) {
if (*listname)
nasmlist.init(listname, report_error);
}
in_abs_seg = false;
global_offset_changed = false; /* set by redefine_label */
2005-01-15 22:15:51 +00:00
location.segment = ofmt->section(NULL, pass2, &sb);
globalbits = sb;
if (passn > 1) {
2005-01-15 22:15:51 +00:00
saa_rewind(forwrefs);
forwref = saa_rstruct(forwrefs);
raa_free(offsets);
offsets = raa_init();
}
preproc->reset(fname, pass1, report_error, evaluate, &nasmlist,
pass1 == 2 ? depend_ptr : NULL);
2005-01-15 22:15:51 +00:00
globallineno = 0;
if (passn == 1)
location.known = true;
2005-01-15 22:15:51 +00:00
location.offset = offs = GET_CURR_OFFS;
while ((line = preproc->getline())) {
enum directives d;
2005-01-15 22:15:51 +00:00
globallineno++;
/* here we parse our directives; this is not handled by the 'real'
* parser. */
directive = line;
d = getkw(&directive, &value);
if (d) {
int err = 0;
switch (d) {
case D_SEGMENT: /* [SEGMENT n] */
case D_SECTION:
2005-01-15 22:15:51 +00:00
seg = ofmt->section(value, pass2, &sb);
if (seg == NO_SEG) {
report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
"segment name `%s' not recognized",
2005-01-15 22:15:51 +00:00
value);
} else {
in_abs_seg = false;
2005-01-15 22:15:51 +00:00
location.segment = seg;
}
break;
case D_EXTERN: /* [EXTERN label:special] */
2005-01-15 22:15:51 +00:00
if (*value == '$')
value++; /* skip initial $ if present */
if (pass0 == 2) {
2002-04-30 20:59:21 +00:00
q = value;
while (*q && *q != ':')
2005-01-15 22:15:51 +00:00
q++;
2002-04-30 20:59:21 +00:00
if (*q == ':') {
2005-01-15 22:15:51 +00:00
*q++ = '\0';
ofmt->symdef(value, 0L, 0L, 3, q);
2002-04-30 20:59:21 +00:00
}
} else if (passn == 1) {
2002-04-30 20:59:21 +00:00
q = value;
validid = true;
2002-04-30 20:59:21 +00:00
if (!isidstart(*q))
validid = false;
2002-04-30 20:59:21 +00:00
while (*q && *q != ':') {
2005-01-15 22:15:51 +00:00
if (!isidchar(*q))
validid = false;
2005-01-15 22:15:51 +00:00
q++;
2002-04-30 20:59:21 +00:00
}
if (!validid) {
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL,
"identifier expected after EXTERN");
break;
2002-04-30 20:59:21 +00:00
}
if (*q == ':') {
2005-01-15 22:15:51 +00:00
*q++ = '\0';
special = q;
2002-04-30 20:59:21 +00:00
} else
2005-01-15 22:15:51 +00:00
special = NULL;
if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
int temp = pass0;
pass0 = 1; /* fake pass 1 in labels.c */
declare_as_global(value, special,
report_error);
define_label(value, seg_alloc(), 0L, NULL,
false, true, ofmt, report_error);
2005-01-15 22:15:51 +00:00
pass0 = temp;
2002-04-30 20:59:21 +00:00
}
2005-01-15 22:15:51 +00:00
} /* else pass0 == 1 */
break;
case D_BITS: /* [BITS bits] */
globalbits = sb = get_bits(value);
2005-01-15 22:15:51 +00:00
break;
case D_GLOBAL: /* [GLOBAL symbol:special] */
2005-01-15 22:15:51 +00:00
if (*value == '$')
value++; /* skip initial $ if present */
if (pass0 == 2) { /* pass 2 */
2002-04-30 20:59:21 +00:00
q = value;
while (*q && *q != ':')
2005-01-15 22:15:51 +00:00
q++;
2002-04-30 20:59:21 +00:00
if (*q == ':') {
2005-01-15 22:15:51 +00:00
*q++ = '\0';
ofmt->symdef(value, 0L, 0L, 3, q);
2002-04-30 20:59:21 +00:00
}
2005-01-15 22:15:51 +00:00
} else if (pass2 == 1) { /* pass == 1 */
2002-04-30 20:59:21 +00:00
q = value;
validid = true;
2002-04-30 20:59:21 +00:00
if (!isidstart(*q))
validid = false;
2002-04-30 20:59:21 +00:00
while (*q && *q != ':') {
2005-01-15 22:15:51 +00:00
if (!isidchar(*q))
validid = false;
2005-01-15 22:15:51 +00:00
q++;
2002-04-30 20:59:21 +00:00
}
if (!validid) {
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL,
"identifier expected after GLOBAL");
break;
2002-04-30 20:59:21 +00:00
}
if (*q == ':') {
2005-01-15 22:15:51 +00:00
*q++ = '\0';
special = q;
2002-04-30 20:59:21 +00:00
} else
2005-01-15 22:15:51 +00:00
special = NULL;
declare_as_global(value, special, report_error);
} /* pass == 1 */
break;
case D_COMMON: /* [COMMON symbol size:special] */
2005-01-15 22:15:51 +00:00
if (*value == '$')
value++; /* skip initial $ if present */
if (pass0 == 1) {
2002-04-30 20:59:21 +00:00
p = value;
validid = true;
2002-04-30 20:59:21 +00:00
if (!isidstart(*p))
validid = false;
while (*p && !nasm_isspace(*p)) {
2005-01-15 22:15:51 +00:00
if (!isidchar(*p))
validid = false;
2005-01-15 22:15:51 +00:00
p++;
2002-04-30 20:59:21 +00:00
}
if (!validid) {
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL,
"identifier expected after COMMON");
break;
2002-04-30 20:59:21 +00:00
}
if (*p) {
int64_t size;
2005-01-15 22:15:51 +00:00
while (*p && nasm_isspace(*p))
2005-01-15 22:15:51 +00:00
*p++ = '\0';
q = p;
while (*q && *q != ':')
q++;
if (*q == ':') {
*q++ = '\0';
special = q;
} else
special = NULL;
size = readnum(p, &rn_error);
if (rn_error)
report_error(ERR_NONFATAL,
"invalid size specified"
2002-04-30 20:59:21 +00:00
" in COMMON declaration");
2005-01-15 22:15:51 +00:00
else
define_common(value, seg_alloc(), size,
special, ofmt, report_error);
2002-04-30 20:59:21 +00:00
} else
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL,
"no size specified in"
" COMMON declaration");
} else if (pass0 == 2) { /* pass == 2 */
2002-04-30 20:59:21 +00:00
q = value;
while (*q && *q != ':') {
if (nasm_isspace(*q))
2005-01-15 22:15:51 +00:00
*q = '\0';
q++;
2002-04-30 20:59:21 +00:00
}
if (*q == ':') {
2005-01-15 22:15:51 +00:00
*q++ = '\0';
ofmt->symdef(value, 0L, 0L, 3, q);
2002-04-30 20:59:21 +00:00
}
2005-01-15 22:15:51 +00:00
}
break;
case D_ABSOLUTE: /* [ABSOLUTE address] */
2005-01-15 22:15:51 +00:00
stdscan_reset();
stdscan_bufptr = value;
tokval.t_type = TOKEN_INVALID;
e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
report_error, NULL);
if (e) {
if (!is_reloc(e))
report_error(pass0 ==
1 ? ERR_NONFATAL : ERR_PANIC,
"cannot use non-relocatable expression as "
"ABSOLUTE address");
else {
abs_seg = reloc_seg(e);
abs_offset = reloc_value(e);
}
} else if (passn == 1)
2005-01-15 22:15:51 +00:00
abs_offset = 0x100; /* don't go near zero in case of / */
else
report_error(ERR_PANIC, "invalid ABSOLUTE address "
"in pass two");
in_abs_seg = true;
2005-01-15 22:15:51 +00:00
location.segment = NO_SEG;
break;
case D_DEBUG: /* [DEBUG] */
2005-01-15 22:15:51 +00:00
p = value;
q = debugid;
validid = true;
2005-01-15 22:15:51 +00:00
if (!isidstart(*p))
validid = false;
while (*p && !nasm_isspace(*p)) {
2005-01-15 22:15:51 +00:00
if (!isidchar(*p))
validid = false;
2005-01-15 22:15:51 +00:00
*q++ = *p++;
}
*q++ = 0;
if (!validid) {
report_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
2005-01-15 22:15:51 +00:00
"identifier expected after DEBUG");
break;
}
while (*p && nasm_isspace(*p))
2005-01-15 22:15:51 +00:00
p++;
if (pass0 == 2)
2005-01-15 22:15:51 +00:00
ofmt->current_dfmt->debug_directive(debugid, p);
break;
case D_WARNING: /* [WARNING {+|-}warn-name] */
2005-01-15 22:15:51 +00:00
if (pass1 == 1) {
while (*value && nasm_isspace(*value))
2005-01-15 22:15:51 +00:00
value++;
if (*value == '+' || *value == '-') {
validid = (*value == '-') ? true : false;
2005-01-15 22:15:51 +00:00
value++;
} else
validid = false;
2005-01-15 22:15:51 +00:00
for (i = 1; i <= ERR_WARN_MAX; i++)
if (!nasm_stricmp(value, suppressed_names[i]))
break;
if (i <= ERR_WARN_MAX)
suppressed[i] = validid;
else
report_error(ERR_NONFATAL,
"invalid warning id in WARNING directive");
}
break;
case D_CPU: /* [CPU] */
2005-01-15 22:15:51 +00:00
cpu = get_cpu(value);
break;
case D_LIST: /* [LIST {+|-}] */
while (*value && nasm_isspace(*value))
2005-01-15 22:15:51 +00:00
value++;
if (*value == '+') {
user_nolist = 0;
} else {
if (*value == '-') {
user_nolist = 1;
} else {
err = 1;
2002-04-30 20:59:21 +00:00
}
2005-01-15 22:15:51 +00:00
}
break;
case D_DEFAULT: /* [DEFAULT] */
stdscan_reset();
stdscan_bufptr = value;
tokval.t_type = TOKEN_INVALID;
if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
switch ((int)tokval.t_integer) {
case S_REL:
globalrel = 1;
break;
case S_ABS:
globalrel = 0;
break;
default:
err = 1;
break;
}
} else {
err = 1;
}
break;
case D_FLOAT:
if (float_option(value)) {
report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
"unknown 'float' directive: %s",
value);
}
break;
2005-01-15 22:15:51 +00:00
default:
if (!ofmt->directive(directive, value, pass2))
report_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
"unrecognised directive [%s]",
directive);
}
if (err) {
report_error(ERR_NONFATAL,
"invalid parameter to [%s] directive",
directive);
}
2005-01-15 22:15:51 +00:00
} else { /* it isn't a directive */
parse_line(pass1, line, &output_ins,
report_error, evaluate, def_label);
if (!(optimizing > 0) && pass0 == 2) {
2005-01-15 22:15:51 +00:00
if (forwref != NULL && globallineno == forwref->lineno) {
output_ins.forw_ref = true;
2005-01-15 22:15:51 +00:00
do {
output_ins.oprs[forwref->operand].opflags |=
OPFLAG_FORWARD;
forwref = saa_rstruct(forwrefs);
} while (forwref != NULL
&& forwref->lineno == globallineno);
} else
output_ins.forw_ref = false;
2005-01-15 22:15:51 +00:00
}
if (!(optimizing > 0) && output_ins.forw_ref) {
if (passn == 1) {
2005-01-15 22:15:51 +00:00
for (i = 0; i < output_ins.operands; i++) {
if (output_ins.oprs[i].
opflags & OPFLAG_FORWARD) {
struct forwrefinfo *fwinf =
(struct forwrefinfo *)
saa_wstruct(forwrefs);
fwinf->lineno = globallineno;
fwinf->operand = i;
}
}
} else { /* passn > 1 */
2002-04-30 20:59:21 +00:00
/*
2005-01-15 22:15:51 +00:00
* Hack to prevent phase error in the code
* rol ax,x
* x equ 1
*
* If the second operand is a forward reference,
* the UNITY property of the number 1 in that
* operand is cancelled. Otherwise the above
* sequence will cause a phase error.
*
* This hack means that the above code will
* generate 286+ code.
*
* The forward reference will mean that the
* operand will not have the UNITY property on
* the first pass, so the pass behaviours will
* be consistent.
*/
2002-04-30 20:59:21 +00:00
if (output_ins.operands >= 2 &&
(output_ins.oprs[1].opflags & OPFLAG_FORWARD) &&
!(IMMEDIATE & ~output_ins.oprs[1].type))
{
/* Remove special properties bits */
output_ins.oprs[1].type &= ~REG_SMASK;
2002-04-30 20:59:21 +00:00
}
}
2002-04-30 20:59:21 +00:00
2005-01-15 22:15:51 +00:00
}
2002-04-30 20:59:21 +00:00
2005-01-15 22:15:51 +00:00
/* forw_ref */
if (output_ins.opcode == I_EQU) {
if (pass1 == 1) {
2002-04-30 20:59:21 +00:00
/*
2005-01-15 22:15:51 +00:00
* Special `..' EQUs get processed in pass two,
* except `..@' macro-processor EQUs which are done
* in the normal place.
*/
2002-04-30 20:59:21 +00:00
if (!output_ins.label)
2005-01-15 22:15:51 +00:00
report_error(ERR_NONFATAL,
"EQU not preceded by label");
2002-04-30 20:59:21 +00:00
else if (output_ins.label[0] != '.' ||
output_ins.label[1] != '.' ||
2005-01-15 22:15:51 +00:00
output_ins.label[2] == '@') {
if (output_ins.operands == 1 &&
(output_ins.oprs[0].type & IMMEDIATE) &&
output_ins.oprs[0].wrt == NO_SEG) {
int isext =
output_ins.oprs[0].
opflags & OPFLAG_EXTERN;
def_label(output_ins.label,
output_ins.oprs[0].segment,
output_ins.oprs[0].offset, NULL,
false, isext, ofmt,
2005-01-15 22:15:51 +00:00
report_error);
} else if (output_ins.operands == 2
&& (output_ins.oprs[0].
type & IMMEDIATE)
&& (output_ins.oprs[0].type & COLON)
&& output_ins.oprs[0].segment ==
NO_SEG
&& output_ins.oprs[0].wrt == NO_SEG
&& (output_ins.oprs[1].
type & IMMEDIATE)
&& output_ins.oprs[1].segment ==
NO_SEG
&& output_ins.oprs[1].wrt ==
NO_SEG) {
def_label(output_ins.label,
output_ins.oprs[0].
offset | SEG_ABS,
output_ins.oprs[1].offset, NULL,
false, false, ofmt,
2005-01-15 22:15:51 +00:00
report_error);
} else
report_error(ERR_NONFATAL,
"bad syntax for EQU");
2002-04-30 20:59:21 +00:00
}
} else {
2002-04-30 20:59:21 +00:00
/*
2005-01-15 22:15:51 +00:00
* Special `..' EQUs get processed here, except
* `..@' macro processor EQUs which are done above.
*/
2002-04-30 20:59:21 +00:00
if (output_ins.label[0] == '.' &&
2005-01-15 22:15:51 +00:00
output_ins.label[1] == '.' &&
output_ins.label[2] != '@') {
if (output_ins.operands == 1 &&
(output_ins.oprs[0].type & IMMEDIATE)) {
define_label(output_ins.label,
2002-04-30 20:59:21 +00:00
output_ins.oprs[0].segment,
output_ins.oprs[0].offset,
NULL, false, false, ofmt,
2005-01-15 22:15:51 +00:00
report_error);
} else if (output_ins.operands == 2
&& (output_ins.oprs[0].
type & IMMEDIATE)
&& (output_ins.oprs[0].type & COLON)
&& output_ins.oprs[0].segment ==
NO_SEG
&& (output_ins.oprs[1].
type & IMMEDIATE)
&& output_ins.oprs[1].segment ==
NO_SEG) {
define_label(output_ins.label,
output_ins.oprs[0].
offset | SEG_ABS,
2002-04-30 20:59:21 +00:00
output_ins.oprs[1].offset,
NULL, false, false, ofmt,
2005-01-15 22:15:51 +00:00
report_error);
} else
report_error(ERR_NONFATAL,
"bad syntax for EQU");
2002-04-30 20:59:21 +00:00
}
}
2005-01-15 22:15:51 +00:00
} else { /* instruction isn't an EQU */
if (pass1 == 1) {
int64_t l = insn_size(location.segment, offs, sb, cpu,
2005-01-15 22:15:51 +00:00
&output_ins, report_error);
/* if (using_debug_info) && output_ins.opcode != -1) */
if (using_debug_info)
{ /* fbk 03/25/01 */
/* this is done here so we can do debug type info */
int32_t typeinfo =
2005-01-15 22:15:51 +00:00
TYS_ELEMENTS(output_ins.operands);
switch (output_ins.opcode) {
case I_RESB:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_BYTE;
break;
case I_RESW:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_WORD;
break;
case I_RESD:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_DWORD;
break;
case I_RESQ:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_QWORD;
break;
case I_REST:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_TBYTE;
break;
case I_RESO:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_OWORD;
break;
case I_RESY:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_YWORD;
break;
2005-01-15 22:15:51 +00:00
case I_DB:
typeinfo |= TY_BYTE;
break;
case I_DW:
typeinfo |= TY_WORD;
break;
case I_DD:
if (output_ins.eops_float)
typeinfo |= TY_FLOAT;
else
typeinfo |= TY_DWORD;
break;
case I_DQ:
typeinfo |= TY_QWORD;
break;
case I_DT:
typeinfo |= TY_TBYTE;
break;
2007-09-23 04:29:41 +00:00
case I_DO:
typeinfo |= TY_OWORD;
break;
case I_DY:
typeinfo |= TY_YWORD;
break;
2005-01-15 22:15:51 +00:00
default:
typeinfo = TY_LABEL;
}
ofmt->current_dfmt->debug_typevalue(typeinfo);
2002-04-30 21:00:33 +00:00
2002-04-30 20:59:21 +00:00
}
if (l != -1) {
2005-01-15 22:15:51 +00:00
offs += l;
SET_CURR_OFFS(offs);
2002-04-30 20:59:21 +00:00
}
2002-04-30 21:01:08 +00:00
/*
2005-01-15 22:15:51 +00:00
* else l == -1 => invalid instruction, which will be
* flagged as an error on pass 2
*/
} else {
2005-01-15 22:15:51 +00:00
offs += assemble(location.segment, offs, sb, cpu,
&output_ins, ofmt, report_error,
&nasmlist);
SET_CURR_OFFS(offs);
}
} /* not an EQU */
cleanup_insn(&output_ins);
}
nasm_free(line);
location.offset = offs = GET_CURR_OFFS;
} /* end while (line = preproc->getline... */
if (pass1 == 2 && global_offset_changed)
report_error(ERR_NONFATAL,
"phase error detected at end of assembly.");
if (pass1 == 1)
preproc->cleanup(1);
if (pass1 == 1 && terminate_after_phase) {
fclose(ofile);
remove(outname);
if (want_usage)
usage();
exit(1);
}
if (passn >= pass_max - 2 ||
(passn > 1 && !global_offset_changed))
2005-01-15 22:15:51 +00:00
pass0++;
}
2005-01-15 22:15:51 +00:00
preproc->cleanup(0);
nasmlist.cleanup();
2002-04-30 20:59:21 +00:00
#if 1
2005-01-15 22:15:51 +00:00
if (optimizing > 0 && opt_verbose_info) /* -On and -Ov switches */
fprintf(stdout,
"info:: assembly required 1+%d+1 passes\n", passn-3);
2002-04-30 21:01:08 +00:00
#endif
2005-01-15 22:15:51 +00:00
} /* exit from assemble_file (...) */
2002-04-30 20:53:55 +00:00
static enum directives getkw(char **directive, char **value)
2002-04-30 20:53:55 +00:00
{
char *p, *q, *buf;
2005-01-15 22:15:51 +00:00
buf = *directive;
2002-04-30 20:51:32 +00:00
2002-04-30 21:03:11 +00:00
/* allow leading spaces or tabs */
2005-01-15 22:15:51 +00:00
while (*buf == ' ' || *buf == '\t')
buf++;
2002-04-30 21:03:11 +00:00
2005-01-15 22:15:51 +00:00
if (*buf != '[')
return 0;
2002-04-30 20:53:55 +00:00
2002-04-30 20:51:32 +00:00
p = buf;
2002-04-30 20:53:55 +00:00
2005-01-15 22:15:51 +00:00
while (*p && *p != ']')
p++;
2002-04-30 20:53:55 +00:00
2002-04-30 20:51:32 +00:00
if (!*p)
2005-01-15 22:15:51 +00:00
return 0;
2002-04-30 20:53:55 +00:00
2002-04-30 20:51:32 +00:00
q = p++;
2002-04-30 20:53:55 +00:00
2002-04-30 20:51:32 +00:00
while (*p && *p != ';') {
if (!nasm_isspace(*p))
2005-01-15 22:15:51 +00:00
return 0;
p++;
2002-04-30 20:51:32 +00:00
}
q[1] = '\0';
2005-01-15 22:15:51 +00:00
*directive = p = buf + 1;
while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
buf++;
if (*buf == ']') {
*buf = '\0';
*value = buf;
2002-04-30 20:51:32 +00:00
} else {
2005-01-15 22:15:51 +00:00
*buf++ = '\0';
while (nasm_isspace(*buf))
2005-01-15 22:15:51 +00:00
buf++; /* beppu - skip leading whitespace */
*value = buf;
while (*buf != ']')
buf++;
*buf++ = '\0';
2002-04-30 20:51:32 +00:00
}
return bsii(*directive, directives, elements(directives));
2002-04-30 20:51:32 +00:00
}
/**
* gnu style error reporting
* This function prints an error message to error_file in the
* style used by GNU. An example would be:
* file.asm:50: error: blah blah blah
* where file.asm is the name of the file, 50 is the line number on
* which the error occurs (or is detected) and "error:" is one of
* the possible optional diagnostics -- it can be "error" or "warning"
* or something else. Finally the line terminates with the actual
* error message.
*
* @param severity the severity of the warning or error
* @param fmt the printf style format string
*/
static void report_error_gnu(int severity, const char *fmt, ...)
2002-04-30 20:53:55 +00:00
{
2002-04-30 20:51:32 +00:00
va_list ap;
if (is_suppressed_warning(severity))
2005-01-15 22:15:51 +00:00
return;
2002-04-30 20:52:49 +00:00
2002-04-30 20:51:32 +00:00
if (severity & ERR_NOFILE)
2005-01-15 22:15:51 +00:00
fputs("nasm: ", error_file);
2002-04-30 20:53:55 +00:00
else {
char *currentfile = NULL;
int32_t lineno = 0;
2005-01-15 22:15:51 +00:00
src_get(&lineno, &currentfile);
2007-04-14 00:10:59 +00:00
fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
2005-01-15 22:15:51 +00:00
nasm_free(currentfile);
2002-04-30 20:53:55 +00:00
}
2005-01-15 22:15:51 +00:00
va_start(ap, fmt);
report_error_common(severity, fmt, ap);
va_end(ap);
}
/**
* MS style error reporting
* This function prints an error message to error_file in the
* style used by Visual C and some other Microsoft tools. An example
* would be:
* file.asm(50) : error: blah blah blah
* where file.asm is the name of the file, 50 is the line number on
* which the error occurs (or is detected) and "error:" is one of
* the possible optional diagnostics -- it can be "error" or "warning"
* or something else. Finally the line terminates with the actual
* error message.
*
* @param severity the severity of the warning or error
* @param fmt the printf style format string
*/
static void report_error_vc(int severity, const char *fmt, ...)
{
va_list ap;
2005-01-15 22:15:51 +00:00
if (is_suppressed_warning(severity))
return;
2002-04-30 20:51:32 +00:00
if (severity & ERR_NOFILE)
2005-01-15 22:15:51 +00:00
fputs("nasm: ", error_file);
else {
char *currentfile = NULL;
int32_t lineno = 0;
2005-01-15 22:15:51 +00:00
src_get(&lineno, &currentfile);
2007-04-14 00:10:59 +00:00
fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
2005-01-15 22:15:51 +00:00
nasm_free(currentfile);
}
2005-01-15 22:15:51 +00:00
va_start(ap, fmt);
report_error_common(severity, fmt, ap);
va_end(ap);
}
/**
* check for supressed warning
* checks for suppressed warning or pass one only warning and we're
* not in pass 1
*
* @param severity the severity of the warning or error
* @return true if we should abort error/warning printing
*/
static bool is_suppressed_warning(int severity)
{
/*
* See if it's a suppressed warning.
*/
return (severity & ERR_MASK) == ERR_WARNING &&
(((severity & ERR_WARN_MASK) != 0 &&
suppressed[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
/* See if it's a pass-one only warning and we're not in pass one. */
((severity & ERR_PASS1) && pass0 != 1));
}
/**
* common error reporting
* This is the common back end of the error reporting schemes currently
* implemented. It prints the nature of the warning and then the
* specific error message to error_file and may or may not return. It
* doesn't return if the error severity is a "panic" or "debug" type.
*
* @param severity the severity of the warning or error
* @param fmt the printf style format string
*/
static void report_error_common(int severity, const char *fmt,
2005-01-15 22:15:51 +00:00
va_list args)
{
switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
2005-01-15 22:15:51 +00:00
case ERR_WARNING:
fputs("warning: ", error_file);
break;
case ERR_NONFATAL:
fputs("error: ", error_file);
break;
case ERR_FATAL:
fputs("fatal: ", error_file);
break;
case ERR_PANIC:
fputs("panic: ", error_file);
break;
case ERR_DEBUG:
fputs("debug: ", error_file);
break;
default:
break;
2002-04-30 20:59:21 +00:00
}
2002-04-30 21:01:08 +00:00
2005-01-15 22:15:51 +00:00
vfprintf(error_file, fmt, args);
putc('\n', error_file);
2002-04-30 20:51:32 +00:00
if (severity & ERR_USAGE)
want_usage = true;
2002-04-30 20:51:32 +00:00
switch (severity & ERR_MASK) {
2005-01-15 22:15:51 +00:00
case ERR_DEBUG:
/* no further action, by definition */
break;
case ERR_WARNING:
if (!suppressed[0]) /* Treat warnings as errors */
terminate_after_phase = true;
break;
2005-01-15 22:15:51 +00:00
case ERR_NONFATAL:
terminate_after_phase = true;
2005-01-15 22:15:51 +00:00
break;
case ERR_FATAL:
if (ofile) {
fclose(ofile);
remove(outname);
}
if (want_usage)
usage();
exit(1); /* instantly die */
break; /* placate silly compilers */
case ERR_PANIC:
fflush(NULL);
/* abort(); *//* halt, catch fire, and dump core */
exit(3);
break;
2002-04-30 20:51:32 +00:00
}
}
2002-04-30 21:01:08 +00:00
static void usage(void)
2002-04-30 20:53:55 +00:00
{
2002-04-30 20:57:38 +00:00
fputs("type `nasm -h' for help\n", error_file);
2002-04-30 20:51:32 +00:00
}
2002-04-30 21:01:08 +00:00
static void register_output_formats(void)
2002-04-30 20:53:55 +00:00
{
2005-01-15 22:15:51 +00:00
ofmt = ofmt_register(report_error);
2002-04-30 20:51:32 +00:00
}
2002-04-30 20:52:08 +00:00
#define BUF_DELTA 512
static FILE *no_pp_fp;
static efunc no_pp_err;
2002-04-30 20:52:49 +00:00
static ListGen *no_pp_list;
static int32_t no_pp_lineinc;
2002-04-30 20:52:08 +00:00
static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
ListGen * listgen, StrList **deplist)
2002-04-30 20:53:55 +00:00
{
src_set_fname(nasm_strdup(file));
src_set_linnum(0);
no_pp_lineinc = 1;
2002-04-30 20:52:08 +00:00
no_pp_err = error;
no_pp_fp = fopen(file, "r");
if (!no_pp_fp)
2005-01-15 22:15:51 +00:00
no_pp_err(ERR_FATAL | ERR_NOFILE,
"unable to open input file `%s'", file);
2002-04-30 20:52:49 +00:00
no_pp_list = listgen;
2005-01-15 22:15:51 +00:00
(void)pass; /* placate compilers */
(void)eval; /* placate compilers */
if (deplist) {
StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
sl->next = NULL;
strcpy(sl->str, file);
*deplist = sl;
}
2002-04-30 20:52:08 +00:00
}
static char *no_pp_getline(void)
2002-04-30 20:53:55 +00:00
{
char *buffer, *p, *q;
2002-04-30 20:52:08 +00:00
int bufsize;
bufsize = BUF_DELTA;
buffer = nasm_malloc(BUF_DELTA);
2002-04-30 20:53:55 +00:00
src_set_linnum(src_get_linnum() + no_pp_lineinc);
2002-04-30 20:52:08 +00:00
2005-01-15 22:15:51 +00:00
while (1) { /* Loop to handle %line */
p = buffer;
while (1) { /* Loop to handle long lines */
q = fgets(p, bufsize - (p - buffer), no_pp_fp);
if (!q)
break;
p += strlen(p);
if (p > buffer && p[-1] == '\n')
break;
if (p - buffer > bufsize - 10) {
int offset;
offset = p - buffer;
bufsize += BUF_DELTA;
buffer = nasm_realloc(buffer, bufsize);
p = buffer + offset;
}
}
if (!q && p == buffer) {
nasm_free(buffer);
return NULL;
}
/*
* Play safe: remove CRs, LFs and any spurious ^Zs, if any of
* them are present at the end of the line.
*/
buffer[strcspn(buffer, "\r\n\032")] = '\0';
2007-10-04 04:22:16 +00:00
if (!nasm_strnicmp(buffer, "%line", 5)) {
int32_t ln;
2005-01-15 22:15:51 +00:00
int li;
char *nm = nasm_malloc(strlen(buffer));
2007-04-14 00:10:59 +00:00
if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
2005-01-15 22:15:51 +00:00
nasm_free(src_set_fname(nm));
src_set_linnum(ln);
no_pp_lineinc = li;
continue;
}
nasm_free(nm);
}
break;
2002-04-30 20:53:55 +00:00
}
2002-04-30 20:52:08 +00:00
2005-01-15 22:15:51 +00:00
no_pp_list->line(LIST_READ, buffer);
2002-04-30 20:52:49 +00:00
2002-04-30 20:52:08 +00:00
return buffer;
}
2005-01-15 22:15:51 +00:00
static void no_pp_cleanup(int pass)
2002-04-30 20:53:55 +00:00
{
2007-04-14 00:46:25 +00:00
(void)pass; /* placate GCC */
2002-04-30 20:52:08 +00:00
fclose(no_pp_fp);
}
2002-04-30 20:59:21 +00:00
static uint32_t get_cpu(char *value)
2002-04-30 20:59:21 +00:00
{
2005-01-15 22:15:51 +00:00
if (!strcmp(value, "8086"))
return IF_8086;
if (!strcmp(value, "186"))
return IF_186;
if (!strcmp(value, "286"))
return IF_286;
if (!strcmp(value, "386"))
return IF_386;
if (!strcmp(value, "486"))
return IF_486;
if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
return IF_PENT;
if (!strcmp(value, "686") ||
!nasm_stricmp(value, "ppro") ||
!nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
return IF_P6;
if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
return IF_KATMAI;
if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
!nasm_stricmp(value, "willamette"))
return IF_WILLAMETTE;
if (!nasm_stricmp(value, "prescott"))
return IF_PRESCOTT;
if (!nasm_stricmp(value, "x64") ||
!nasm_stricmp(value, "x86-64"))
return IF_X86_64;
2002-05-28 01:25:06 +00:00
if (!nasm_stricmp(value, "ia64") ||
2005-01-15 22:15:51 +00:00
!nasm_stricmp(value, "ia-64") ||
!nasm_stricmp(value, "itanium") ||
!nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
return IF_IA64;
2002-04-30 20:59:21 +00:00
2005-01-15 22:15:51 +00:00
report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
"unknown 'cpu' type");
2002-04-30 21:01:08 +00:00
2005-01-15 22:15:51 +00:00
return IF_PLEVEL; /* the maximum level */
2002-04-30 20:59:21 +00:00
}
static int get_bits(char *value)
2002-04-30 20:59:21 +00:00
{
int i;
2002-04-30 21:01:08 +00:00
2005-01-15 22:15:51 +00:00
if ((i = atoi(value)) == 16)
return i; /* set for a 16-bit segment */
2002-04-30 20:59:21 +00:00
else if (i == 32) {
2005-01-15 22:15:51 +00:00
if (cpu < IF_386) {
report_error(ERR_NONFATAL,
"cannot specify 32-bit segment on processor below a 386");
i = 16;
}
} else if (i == 64) {
if (cpu < IF_X86_64) {
report_error(ERR_NONFATAL,
"cannot specify 64-bit segment on processor below an x86-64");
i = 16;
}
if (i != maxbits) {
report_error(ERR_NONFATAL,
"%s output format does not support 64-bit code",
ofmt->shortname);
i = 16;
}
2002-04-30 20:59:21 +00:00
} else {
2005-01-15 22:15:51 +00:00
report_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
"`%s' is not a valid segment size; must be 16, 32 or 64",
2005-01-15 22:15:51 +00:00
value);
i = 16;
2002-04-30 20:59:21 +00:00
}
return i;
}
/* end of nasm.c */