2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2009-07-13 03:04:56 +08:00
|
|
|
*
|
2017-03-08 09:45:01 +08:00
|
|
|
* Copyright 1996-2017 The NASM Authors - All Rights Reserved
|
2009-06-29 08:13:04 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2009-07-13 03:04:56 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
2009-07-13 03:04:56 +08:00
|
|
|
/*
|
2009-06-29 08:13:04 +08:00
|
|
|
* The Netwide Assembler main program module
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2007-10-11 05:06:59 +08:00
|
|
|
#include <limits.h>
|
2008-02-17 06:54:10 +08:00
|
|
|
#include <time.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "error.h"
|
2008-06-10 08:32:43 +08:00
|
|
|
#include "saa.h"
|
2008-06-10 08:40:16 +08:00
|
|
|
#include "raa.h"
|
2007-10-17 05:40:27 +08:00
|
|
|
#include "float.h"
|
2007-08-31 06:35:34 +08:00
|
|
|
#include "stdscan.h"
|
2002-05-01 04:59:21 +08:00
|
|
|
#include "insns.h"
|
2002-05-01 04:52:08 +08:00
|
|
|
#include "preproc.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
#include "parser.h"
|
2002-05-01 04:52:49 +08:00
|
|
|
#include "eval.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
#include "assemble.h"
|
|
|
|
#include "labels.h"
|
2016-05-26 03:06:29 +08:00
|
|
|
#include "outform.h"
|
2002-05-01 04:52:26 +08:00
|
|
|
#include "listing.h"
|
2013-11-10 02:16:11 +08:00
|
|
|
#include "iflag.h"
|
2016-03-08 18:17:36 +08:00
|
|
|
#include "ver.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2010-07-16 09:28:52 +08:00
|
|
|
/*
|
|
|
|
* This is the maximum number of optimization passes to do. If we ever
|
|
|
|
* find a case where the optimizer doesn't naturally converge, we might
|
|
|
|
* have to drop this value so the assembler doesn't appear to just hang.
|
|
|
|
*/
|
|
|
|
#define MAX_OPTIMIZE (INT_MAX >> 1)
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
struct forwrefinfo { /* info held on forward refs. */
|
2002-05-01 04:53:55 +08:00
|
|
|
int lineno;
|
|
|
|
int operand;
|
|
|
|
};
|
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
static void parse_cmdline(int, char **, int);
|
2008-05-30 12:38:00 +08:00
|
|
|
static void assemble_file(char *, StrList **);
|
2016-02-18 12:27:41 +08:00
|
|
|
static bool is_suppressed_warning(int severity);
|
2016-02-18 12:47:01 +08:00
|
|
|
static bool skip_this_pass(int severity);
|
2009-07-19 09:07:17 +08:00
|
|
|
static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
|
|
|
|
static void nasm_verror_vc(int severity, const char *fmt, va_list args);
|
|
|
|
static void nasm_verror_common(int severity, const char *fmt, va_list args);
|
2002-05-01 04:51:32 +08:00
|
|
|
static void usage(void);
|
|
|
|
|
2016-03-08 15:18:30 +08:00
|
|
|
static bool using_debug_info, opt_verbose_info;
|
|
|
|
static const char *debug_format;
|
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
bool tasm_compatible_mode = false;
|
2008-01-09 15:03:57 +08:00
|
|
|
int pass0, passn;
|
2017-03-08 09:45:01 +08:00
|
|
|
static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
|
2007-08-29 07:06:00 +08:00
|
|
|
int globalrel = 0;
|
2013-12-05 12:05:55 +08:00
|
|
|
int globalbnd = 0;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
static time_t official_compile_time;
|
2008-02-17 06:54:10 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static char inname[FILENAME_MAX];
|
|
|
|
static char outname[FILENAME_MAX];
|
|
|
|
static char listname[FILENAME_MAX];
|
2007-10-01 13:15:36 +08:00
|
|
|
static char errname[FILENAME_MAX];
|
2005-01-16 06:15:51 +08:00
|
|
|
static int globallineno; /* for forward-reference tracking */
|
2002-05-01 05:00:33 +08:00
|
|
|
/* static int pass = 0; */
|
2016-02-18 12:59:22 +08:00
|
|
|
const struct ofmt *ofmt = &OF_DEFAULT;
|
|
|
|
const struct ofmt_alias *ofmt_alias = NULL;
|
2009-07-19 09:07:17 +08:00
|
|
|
const struct dfmt *dfmt;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static FILE *error_file; /* Where to write error messages */
|
2002-05-01 04:57:38 +08:00
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
FILE *ofile = NULL;
|
2010-07-16 09:28:52 +08:00
|
|
|
int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
|
2016-11-16 23:48:13 +08:00
|
|
|
static int cmd_sb = 16; /* by default */
|
2013-11-10 02:16:11 +08:00
|
|
|
|
2017-03-08 11:23:03 +08:00
|
|
|
iflag_t cpu;
|
2013-11-10 02:16:11 +08:00
|
|
|
static iflag_t cmd_cpu;
|
|
|
|
|
2016-02-18 17:25:46 +08:00
|
|
|
struct location location;
|
2017-03-08 11:23:03 +08:00
|
|
|
bool in_absolute; /* Flag we are in ABSOLUTE seg */
|
|
|
|
struct location absolute; /* Segment/offset inside ABSOLUTE */
|
2002-05-01 04:52:26 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
static struct RAA *offsets;
|
2002-05-01 04:51:53 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static struct SAA *forwrefs; /* keep track of forward references */
|
2007-10-05 04:42:56 +08:00
|
|
|
static const struct forwrefinfo *forwref;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2016-02-18 18:20:59 +08:00
|
|
|
static const struct preproc_ops *preproc;
|
2011-07-01 14:38:25 +08:00
|
|
|
|
2014-06-23 06:22:02 +08:00
|
|
|
#define OP_NORMAL (1u << 0)
|
|
|
|
#define OP_PREPROCESS (1u << 1)
|
|
|
|
#define OP_DEPEND (1u << 2)
|
|
|
|
|
|
|
|
static unsigned int operating_mode;
|
2014-06-23 00:53:12 +08:00
|
|
|
|
2008-05-30 10:09:11 +08:00
|
|
|
/* Dependency flags */
|
2008-05-30 12:38:00 +08:00
|
|
|
static bool depend_emit_phony = false;
|
2008-05-30 10:09:11 +08:00
|
|
|
static bool depend_missing_ok = false;
|
|
|
|
static const char *depend_target = NULL;
|
|
|
|
static const char *depend_file = NULL;
|
2002-05-01 04:52:08 +08:00
|
|
|
|
2009-06-28 06:34:32 +08:00
|
|
|
static bool want_usage;
|
|
|
|
static bool terminate_after_phase;
|
2016-02-18 12:27:41 +08:00
|
|
|
bool user_nolist = false;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2012-09-10 08:09:00 +08:00
|
|
|
static char *quote_for_make(const char *str);
|
|
|
|
|
2013-02-15 16:35:04 +08:00
|
|
|
static int64_t get_curr_offs(void)
|
|
|
|
{
|
2017-03-08 11:23:03 +08:00
|
|
|
return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
|
2013-02-15 16:35:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_curr_offs(int64_t l_off)
|
|
|
|
{
|
2017-03-08 11:23:03 +08:00
|
|
|
if (in_absolute)
|
|
|
|
absolute.offset = l_off;
|
2013-02-15 16:35:04 +08:00
|
|
|
else
|
|
|
|
offsets = raa_write(offsets, location.segment, l_off);
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void nasm_fputs(const char *line, FILE * outfile)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-15 06:38:55 +08:00
|
|
|
if (outfile) {
|
2005-01-16 06:15:51 +08:00
|
|
|
fputs(line, outfile);
|
2007-11-14 01:37:59 +08:00
|
|
|
putc('\n', outfile);
|
2002-05-01 04:53:55 +08:00
|
|
|
} else
|
2005-01-16 06:15:51 +08:00
|
|
|
puts(line);
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
|
|
|
|
2008-02-17 06:54:10 +08:00
|
|
|
/* Convert a struct tm to a POSIX-style time constant */
|
2015-01-06 07:17:56 +08:00
|
|
|
static int64_t make_posix_time(struct tm *tm)
|
2008-02-17 06:54:10 +08:00
|
|
|
{
|
|
|
|
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) {
|
2013-02-15 06:16:58 +08:00
|
|
|
lt = *lt_p;
|
|
|
|
|
|
|
|
strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", <);
|
|
|
|
preproc->pre_define(temp);
|
|
|
|
strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", <);
|
|
|
|
preproc->pre_define(temp);
|
|
|
|
strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", <);
|
|
|
|
preproc->pre_define(temp);
|
|
|
|
strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", <);
|
|
|
|
preproc->pre_define(temp);
|
2008-02-17 06:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gm_p = gmtime(&official_compile_time);
|
|
|
|
if (gm_p) {
|
2013-02-15 06:16:58 +08:00
|
|
|
gm = *gm_p;
|
|
|
|
|
|
|
|
strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
|
|
|
|
preproc->pre_define(temp);
|
|
|
|
strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
|
|
|
|
preproc->pre_define(temp);
|
|
|
|
strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
|
|
|
|
preproc->pre_define(temp);
|
|
|
|
strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
|
|
|
|
preproc->pre_define(temp);
|
2008-02-17 06:54:10 +08:00
|
|
|
}
|
2008-05-05 08:53:31 +08:00
|
|
|
|
2008-02-17 06:54:10 +08:00
|
|
|
if (gm_p)
|
2015-01-06 07:17:56 +08:00
|
|
|
posix_time = make_posix_time(&gm);
|
2008-02-17 06:54:10 +08:00
|
|
|
else if (lt_p)
|
2015-01-06 07:17:56 +08:00
|
|
|
posix_time = make_posix_time(<);
|
2008-02-17 06:54:10 +08:00
|
|
|
else
|
2013-02-15 06:16:58 +08:00
|
|
|
posix_time = 0;
|
2008-02-17 06:54:10 +08:00
|
|
|
|
|
|
|
if (posix_time) {
|
2013-02-15 06:16:58 +08:00
|
|
|
snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
|
|
|
|
preproc->pre_define(temp);
|
2008-02-17 06:54:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void define_macros_late(void)
|
|
|
|
{
|
|
|
|
char temp[128];
|
|
|
|
|
2011-04-06 22:32:15 +08:00
|
|
|
/*
|
|
|
|
* In case if output format is defined by alias
|
|
|
|
* we have to put shortname of the alias itself here
|
|
|
|
* otherwise ABI backward compatibility gets broken.
|
|
|
|
*/
|
2010-07-12 19:19:17 +08:00
|
|
|
snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
|
2011-04-06 22:32:15 +08:00
|
|
|
ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
|
2012-05-07 05:57:55 +08:00
|
|
|
preproc->pre_define(temp);
|
2008-02-17 06:54:10 +08:00
|
|
|
}
|
|
|
|
|
2008-05-30 12:38:00 +08:00
|
|
|
static void emit_dependencies(StrList *list)
|
|
|
|
{
|
|
|
|
FILE *deps;
|
|
|
|
int linepos, len;
|
|
|
|
StrList *l, *nl;
|
|
|
|
|
|
|
|
if (depend_file && strcmp(depend_file, "-")) {
|
2016-05-25 19:28:46 +08:00
|
|
|
deps = nasm_open_write(depend_file, NF_TEXT);
|
2013-02-15 16:25:04 +08:00
|
|
|
if (!deps) {
|
|
|
|
nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
|
|
|
|
"unable to write dependency file `%s'", depend_file);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-30 12:38:00 +08:00
|
|
|
} else {
|
2013-02-15 16:25:04 +08:00
|
|
|
deps = stdout;
|
2008-05-30 12:38:00 +08:00
|
|
|
}
|
2009-07-13 03:04:56 +08:00
|
|
|
|
2008-05-30 12:38:00 +08:00
|
|
|
linepos = fprintf(deps, "%s:", depend_target);
|
2009-07-27 20:26:26 +08:00
|
|
|
list_for_each(l, list) {
|
2012-09-10 08:09:00 +08:00
|
|
|
char *file = quote_for_make(l->str);
|
2013-02-15 16:25:04 +08:00
|
|
|
len = strlen(file);
|
|
|
|
if (linepos + len > 62 && linepos > 1) {
|
|
|
|
fprintf(deps, " \\\n ");
|
|
|
|
linepos = 1;
|
|
|
|
}
|
|
|
|
fprintf(deps, " %s", file);
|
|
|
|
linepos += len+1;
|
2012-09-10 08:09:00 +08:00
|
|
|
nasm_free(file);
|
2008-05-30 12:38:00 +08:00
|
|
|
}
|
|
|
|
fprintf(deps, "\n\n");
|
2009-07-13 03:04:56 +08:00
|
|
|
|
2009-07-27 20:26:26 +08:00
|
|
|
list_for_each_safe(l, nl, list) {
|
2013-02-15 16:25:04 +08:00
|
|
|
if (depend_emit_phony)
|
|
|
|
fprintf(deps, "%s:\n\n", l->str);
|
|
|
|
nasm_free(l);
|
2008-05-30 12:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (deps != stdout)
|
2013-02-15 16:25:04 +08:00
|
|
|
fclose(deps);
|
2008-05-30 12:38:00 +08:00
|
|
|
}
|
|
|
|
|
2007-04-13 00:54:50 +08:00
|
|
|
int main(int argc, char **argv)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2008-05-30 12:38:00 +08:00
|
|
|
StrList *depend_list = NULL, **depend_ptr;
|
2008-05-30 10:09:11 +08:00
|
|
|
|
2008-02-17 06:54:10 +08:00
|
|
|
time(&official_compile_time);
|
|
|
|
|
2013-11-10 02:16:11 +08:00
|
|
|
iflag_set(&cpu, IF_PLEVEL);
|
|
|
|
iflag_set(&cmd_cpu, IF_PLEVEL);
|
|
|
|
|
2008-09-11 10:21:52 +08:00
|
|
|
pass0 = 0;
|
2007-10-11 05:58:45 +08:00
|
|
|
want_usage = terminate_after_phase = false;
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_set_verror(nasm_verror_gnu);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-25 23:47:47 +08:00
|
|
|
error_file = stderr;
|
|
|
|
|
2008-06-12 06:49:41 +08:00
|
|
|
tolower_init();
|
2016-05-10 17:56:29 +08:00
|
|
|
src_init();
|
2008-06-12 06:49:41 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
offsets = raa_init();
|
2007-04-12 10:40:54 +08:00
|
|
|
forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:08 +08:00
|
|
|
preproc = &nasmpp;
|
2014-06-23 00:53:12 +08:00
|
|
|
operating_mode = OP_NORMAL;
|
2002-05-01 05:01:08 +08:00
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
parse_cmdline(argc, argv, 1);
|
|
|
|
if (terminate_after_phase) {
|
|
|
|
if (want_usage)
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-26 08:08:05 +08:00
|
|
|
/*
|
|
|
|
* Define some macros dependent on the runtime, but not
|
2016-10-04 10:46:49 +08:00
|
|
|
* on the command line (as those are scanned in cmdline pass 2.)
|
2016-09-26 08:08:05 +08:00
|
|
|
*/
|
|
|
|
preproc->init();
|
2008-02-17 06:54:10 +08:00
|
|
|
define_macros_early();
|
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
parse_cmdline(argc, argv, 2);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (terminate_after_phase) {
|
|
|
|
if (want_usage)
|
|
|
|
usage();
|
|
|
|
return 1;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:26:40 +08:00
|
|
|
/* Save away the default state of warnings */
|
|
|
|
memcpy(warning_state_init, warning_state, sizeof warning_state);
|
|
|
|
|
2016-03-08 15:18:30 +08:00
|
|
|
if (!using_debug_info) {
|
|
|
|
/* No debug info, redirect to the null backend (empty stubs) */
|
2016-02-18 12:55:08 +08:00
|
|
|
dfmt = &null_debug_form;
|
2016-03-08 15:18:30 +08:00
|
|
|
} else if (!debug_format) {
|
|
|
|
/* Default debug format for this backend */
|
2016-02-18 12:55:08 +08:00
|
|
|
dfmt = ofmt->default_dfmt;
|
2016-03-08 15:18:30 +08:00
|
|
|
} else {
|
|
|
|
dfmt = dfmt_find(ofmt, debug_format);
|
|
|
|
if (!dfmt) {
|
|
|
|
nasm_fatal(ERR_NOFILE | ERR_USAGE,
|
|
|
|
"unrecognized debug format `%s' for"
|
|
|
|
" output format `%s'",
|
|
|
|
debug_format, ofmt->shortname);
|
|
|
|
}
|
|
|
|
}
|
2003-09-11 07:34:23 +08:00
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
if (ofmt->stdmac)
|
2012-05-07 05:57:55 +08:00
|
|
|
preproc->extra_stdmac(ofmt->stdmac);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-01 04:59:21 +08:00
|
|
|
/* define some macros dependent of command-line */
|
2008-02-17 06:54:10 +08:00
|
|
|
define_macros_late();
|
2002-05-01 04:59:21 +08:00
|
|
|
|
2014-06-24 04:55:17 +08:00
|
|
|
depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
|
2008-05-30 10:09:11 +08:00
|
|
|
if (!depend_target)
|
2013-02-15 16:24:11 +08:00
|
|
|
depend_target = quote_for_make(outname);
|
2008-05-30 10:09:11 +08:00
|
|
|
|
2014-06-23 06:22:02 +08:00
|
|
|
if (operating_mode & OP_DEPEND) {
|
2007-04-14 00:47:53 +08:00
|
|
|
char *line;
|
2008-05-30 10:09:11 +08:00
|
|
|
|
2013-02-15 16:24:11 +08:00
|
|
|
if (depend_missing_ok)
|
|
|
|
preproc->include_path(NULL); /* "assume generated" */
|
2008-05-30 10:09:11 +08:00
|
|
|
|
2016-02-18 12:27:41 +08:00
|
|
|
preproc->reset(inname, 0, depend_ptr);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (outname[0] == '\0')
|
2009-07-19 09:07:17 +08:00
|
|
|
ofmt->filename(inname, outname);
|
2005-01-16 06:15:51 +08:00
|
|
|
ofile = NULL;
|
|
|
|
while ((line = preproc->getline()))
|
|
|
|
nasm_free(line);
|
|
|
|
preproc->cleanup(0);
|
2014-06-23 06:22:02 +08:00
|
|
|
} else if (operating_mode & OP_PREPROCESS) {
|
2007-04-14 00:47:53 +08:00
|
|
|
char *line;
|
2016-05-10 17:56:29 +08:00
|
|
|
const char *file_name = NULL;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t prior_linnum = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
int lineinc = 0;
|
|
|
|
|
|
|
|
if (*outname) {
|
2016-05-25 19:28:46 +08:00
|
|
|
ofile = nasm_open_write(outname, NF_TEXT);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!ofile)
|
2016-03-04 06:27:34 +08:00
|
|
|
nasm_fatal(ERR_NOFILE,
|
2005-01-16 06:15:51 +08:00
|
|
|
"unable to open output file `%s'",
|
|
|
|
outname);
|
|
|
|
} else
|
|
|
|
ofile = NULL;
|
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
location.known = false;
|
2002-05-01 05:01:08 +08:00
|
|
|
|
2011-12-05 05:56:40 +08:00
|
|
|
/* pass = 1; */
|
2016-02-18 12:27:41 +08:00
|
|
|
preproc->reset(inname, 3, depend_ptr);
|
2017-03-08 17:26:40 +08:00
|
|
|
|
|
|
|
/* Revert all warnings to the default state */
|
|
|
|
memcpy(warning_state, warning_state_init, sizeof warning_state);
|
2008-05-30 10:09:11 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
while ((line = preproc->getline())) {
|
|
|
|
/*
|
|
|
|
* We generate %line directives if needed for later programs
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t linnum = prior_linnum += lineinc;
|
2005-01-16 06:15:51 +08: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 08:10:59 +08:00
|
|
|
"%%line %"PRId32"+%d %s\n", linnum, lineinc,
|
2005-01-16 06:15:51 +08:00
|
|
|
file_name);
|
|
|
|
}
|
|
|
|
prior_linnum = linnum;
|
|
|
|
}
|
|
|
|
nasm_fputs(line, ofile);
|
|
|
|
nasm_free(line);
|
|
|
|
}
|
|
|
|
preproc->cleanup(0);
|
|
|
|
if (ofile)
|
|
|
|
fclose(ofile);
|
|
|
|
if (ofile && terminate_after_phase)
|
|
|
|
remove(outname);
|
2013-02-15 16:24:11 +08:00
|
|
|
ofile = NULL;
|
2014-06-24 04:55:17 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2014-06-24 04:55:17 +08:00
|
|
|
if (operating_mode & OP_NORMAL) {
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2016-05-25 19:28:46 +08:00
|
|
|
ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
|
2014-06-24 04:55:17 +08:00
|
|
|
if (!ofile)
|
2016-03-04 06:27:34 +08:00
|
|
|
nasm_fatal(ERR_NOFILE,
|
2014-06-24 04:55:17 +08:00
|
|
|
"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();
|
|
|
|
dfmt->init();
|
|
|
|
|
|
|
|
assemble_file(inname, depend_ptr);
|
|
|
|
|
|
|
|
if (!terminate_after_phase) {
|
2016-03-08 14:53:06 +08:00
|
|
|
ofmt->cleanup();
|
2014-06-24 04:55:17 +08:00
|
|
|
cleanup_labels();
|
|
|
|
fflush(ofile);
|
2016-05-10 17:56:29 +08:00
|
|
|
if (ferror(ofile)) {
|
2014-06-24 04:55:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_NOFILE,
|
|
|
|
"write error on output file `%s'", outname);
|
2016-05-10 17:56:29 +08:00
|
|
|
terminate_after_phase = true;
|
|
|
|
}
|
2014-06-24 04:55:17 +08:00
|
|
|
}
|
2013-02-15 16:24:11 +08:00
|
|
|
|
2014-06-24 04:55:17 +08:00
|
|
|
if (ofile) {
|
|
|
|
fclose(ofile);
|
|
|
|
if (terminate_after_phase)
|
|
|
|
remove(outname);
|
|
|
|
ofile = NULL;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 05:01:08 +08:00
|
|
|
|
2009-06-28 06:34:32 +08:00
|
|
|
if (depend_list && !terminate_after_phase)
|
2013-02-15 16:24:11 +08:00
|
|
|
emit_dependencies(depend_list);
|
2008-05-30 10:09:11 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
if (want_usage)
|
2005-01-16 06:15:51 +08:00
|
|
|
usage();
|
2002-05-01 05:01:08 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
raa_free(offsets);
|
|
|
|
saa_free(forwrefs);
|
|
|
|
eval_cleanup();
|
2007-08-31 06:35:34 +08:00
|
|
|
stdscan_cleanup();
|
2016-05-10 17:56:29 +08:00
|
|
|
src_free();
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2009-06-28 06:34:32 +08:00
|
|
|
return terminate_after_phase;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
/*
|
|
|
|
* Get a parameter for a command line option.
|
|
|
|
* First arg must be in the form of e.g. -f...
|
|
|
|
*/
|
2007-11-16 09:12:29 +08:00
|
|
|
static char *get_param(char *p, char *q, bool *advance)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-11-16 09:12:29 +08:00
|
|
|
*advance = false;
|
2009-10-13 23:38:52 +08:00
|
|
|
if (p[2]) /* the parameter's in the option */
|
|
|
|
return nasm_skip_spaces(p + 2);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (q && q[0]) {
|
2007-11-16 09:12:29 +08:00
|
|
|
*advance = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
return q;
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
|
2005-01-16 06:15:51 +08:00
|
|
|
"option `-%c' requires an argument", p[1]);
|
2002-05-01 04:53:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-19 03:55:10 +08:00
|
|
|
/*
|
|
|
|
* Copy a filename
|
|
|
|
*/
|
|
|
|
static void copy_filename(char *dst, const char *src)
|
|
|
|
{
|
|
|
|
size_t len = strlen(src);
|
|
|
|
|
|
|
|
if (len >= (size_t)FILENAME_MAX) {
|
2016-03-04 06:27:34 +08:00
|
|
|
nasm_fatal(ERR_NOFILE, "file name too long");
|
2013-02-15 16:22:59 +08:00
|
|
|
return;
|
2007-11-19 03:55:10 +08:00
|
|
|
}
|
|
|
|
strncpy(dst, src, FILENAME_MAX);
|
|
|
|
}
|
|
|
|
|
2008-05-30 10:09:11 +08:00
|
|
|
/*
|
|
|
|
* Convert a string to Make-safe form
|
|
|
|
*/
|
|
|
|
static char *quote_for_make(const char *str)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
char *os, *q;
|
|
|
|
|
2013-02-15 16:22:27 +08:00
|
|
|
size_t n = 1; /* Terminating zero */
|
2008-05-30 10:09:11 +08:00
|
|
|
size_t nbs = 0;
|
|
|
|
|
|
|
|
if (!str)
|
2013-02-15 16:22:27 +08:00
|
|
|
return NULL;
|
2008-05-30 10:09:11 +08:00
|
|
|
|
|
|
|
for (p = str; *p; p++) {
|
2013-02-15 16:22:27 +08:00
|
|
|
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;
|
|
|
|
}
|
2008-05-30 10:09:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert N backslashes at the end of filename to 2N backslashes */
|
|
|
|
if (nbs)
|
2013-02-15 16:22:27 +08:00
|
|
|
n += nbs;
|
2008-05-30 10:09:11 +08:00
|
|
|
|
|
|
|
os = q = nasm_malloc(n);
|
|
|
|
|
|
|
|
nbs = 0;
|
|
|
|
for (p = str; *p; p++) {
|
2013-02-15 16:22:27 +08:00
|
|
|
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;
|
|
|
|
}
|
2008-05-30 10:09:11 +08:00
|
|
|
}
|
|
|
|
while (nbs--)
|
2013-02-15 16:22:27 +08:00
|
|
|
*q++ = '\\';
|
2008-05-30 10:09:11 +08:00
|
|
|
|
|
|
|
*q = '\0';
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
struct textargs {
|
2007-04-14 00:47:53 +08:00
|
|
|
const char *label;
|
2005-01-16 06:15:51 +08:00
|
|
|
int value;
|
2002-05-01 05:00:33 +08:00
|
|
|
};
|
|
|
|
|
2015-11-11 05:07:20 +08:00
|
|
|
enum text_options {
|
|
|
|
OPT_PREFIX,
|
2016-03-02 14:43:51 +08:00
|
|
|
OPT_POSTFIX
|
2015-11-11 05:07:20 +08:00
|
|
|
};
|
2016-02-18 18:28:15 +08:00
|
|
|
static const struct textargs textopts[] = {
|
2005-01-16 06:15:51 +08:00
|
|
|
{"prefix", OPT_PREFIX},
|
|
|
|
{"postfix", OPT_POSTFIX},
|
|
|
|
{NULL, 0}
|
2002-05-01 05:00:33 +08:00
|
|
|
};
|
|
|
|
|
2014-05-10 02:34:34 +08:00
|
|
|
static void show_version(void)
|
|
|
|
{
|
|
|
|
printf("NASM version %s compiled on %s%s\n",
|
|
|
|
nasm_version, nasm_date, nasm_compile_options);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2007-11-16 09:12:29 +08:00
|
|
|
static bool stopoptions = false;
|
2016-10-04 10:46:49 +08:00
|
|
|
static bool process_arg(char *p, char *q, int pass)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *param;
|
2007-11-16 09:12:29 +08:00
|
|
|
int i;
|
|
|
|
bool advance = false;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:52:26 +08:00
|
|
|
if (!p || !p[0])
|
2007-11-16 09:12:29 +08:00
|
|
|
return false;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
if (p[0] == '-' && !stopoptions) {
|
2013-02-15 16:20:58 +08:00
|
|
|
if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
|
|
|
|
/* These parameters take values */
|
|
|
|
if (!(param = get_param(p, q, &advance)))
|
|
|
|
return advance;
|
|
|
|
}
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (p[1]) {
|
|
|
|
case 's':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 1)
|
|
|
|
error_file = stdout;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'o': /* output file */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
copy_filename(outname, param);
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'f': /* output format */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 1) {
|
|
|
|
ofmt = ofmt_find(param, &ofmt_alias);
|
|
|
|
if (!ofmt) {
|
|
|
|
nasm_fatal(ERR_NOFILE | ERR_USAGE,
|
|
|
|
"unrecognised output format `%s' - "
|
|
|
|
"use -hf for a list", param);
|
|
|
|
}
|
2013-02-15 16:20:58 +08:00
|
|
|
}
|
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'O': /* Optimization level */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2) {
|
|
|
|
int opt;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
if (!*param) {
|
|
|
|
/* Naked -O == -Ox */
|
|
|
|
optimizing = MAX_OPTIMIZE;
|
|
|
|
} 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, ¶m, 10);
|
|
|
|
|
|
|
|
/* -O0 -> optimizing == -1, 0.98 behaviour */
|
|
|
|
/* -O1 -> optimizing == 0, 0.98.09 behaviour */
|
|
|
|
if (opt < 2)
|
|
|
|
optimizing = opt - 1;
|
|
|
|
else
|
|
|
|
optimizing = opt;
|
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
case 'v':
|
|
|
|
case '+':
|
2013-02-15 16:20:58 +08:00
|
|
|
param++;
|
|
|
|
opt_verbose_info = true;
|
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
case 'x':
|
|
|
|
param++;
|
|
|
|
optimizing = MAX_OPTIMIZE;
|
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
default:
|
|
|
|
nasm_fatal(0,
|
|
|
|
"unknown optimization option -O%c\n",
|
|
|
|
*param);
|
|
|
|
break;
|
|
|
|
}
|
2013-02-15 16:20:58 +08:00
|
|
|
}
|
2016-10-04 10:46:49 +08:00
|
|
|
if (optimizing > MAX_OPTIMIZE)
|
|
|
|
optimizing = MAX_OPTIMIZE;
|
2013-02-15 16:20:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'p': /* pre-include */
|
|
|
|
case 'P':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
preproc->pre_include(param);
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'd': /* pre-define */
|
|
|
|
case 'D':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
preproc->pre_define(param);
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'u': /* un-define */
|
|
|
|
case 'U':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
preproc->pre_undefine(param);
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i': /* include search path */
|
|
|
|
case 'I':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
preproc->include_path(param);
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': /* listing file */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
copy_filename(listname, param);
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Z': /* error messages file */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 1)
|
|
|
|
copy_filename(errname, param);
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F': /* specify debug format */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2) {
|
|
|
|
using_debug_info = true;
|
|
|
|
debug_format = param;
|
|
|
|
}
|
2013-02-15 16:20:58 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'X': /* specify error reporting format */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 1) {
|
|
|
|
if (nasm_stricmp("vc", param) == 0)
|
|
|
|
nasm_set_verror(nasm_verror_vc);
|
|
|
|
else if (nasm_stricmp("gnu", param) == 0)
|
|
|
|
nasm_set_verror(nasm_verror_gnu);
|
|
|
|
else
|
|
|
|
nasm_fatal(ERR_NOFILE | ERR_USAGE,
|
|
|
|
"unrecognized error reporting format `%s'",
|
|
|
|
param);
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 'g':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2) {
|
|
|
|
using_debug_info = true;
|
|
|
|
if (p[2])
|
|
|
|
debug_format = nasm_skip_spaces(p + 2);
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 'h':
|
|
|
|
printf
|
|
|
|
("usage: nasm [-@ response file] [-o outfile] [-f format] "
|
|
|
|
"[-l listfile]\n"
|
|
|
|
" [options...] [--] filename\n"
|
2014-05-10 02:34:34 +08:00
|
|
|
" or nasm -v (or --v) for version info\n\n"
|
2016-03-08 15:18:30 +08:00
|
|
|
" -t assemble in SciTech TASM compatible mode\n");
|
2005-01-16 06:15:51 +08:00
|
|
|
printf
|
2007-09-27 06:19:28 +08:00
|
|
|
(" -E (or -e) preprocess only (writes output to stdout by default)\n"
|
2005-01-16 06:15:51 +08:00
|
|
|
" -a don't preprocess (assemble only)\n"
|
2007-09-25 04:41:58 +08:00
|
|
|
" -M generate Makefile dependencies on stdout\n"
|
2009-07-31 04:32:17 +08:00
|
|
|
" -MG d:o, missing files assumed generated\n"
|
|
|
|
" -MF <file> set Makefile dependency file\n"
|
|
|
|
" -MD <file> assemble and generate dependencies\n"
|
|
|
|
" -MT <file> dependency target name\n"
|
|
|
|
" -MQ <file> dependency target name (quoted)\n"
|
|
|
|
" -MP emit phony target\n\n"
|
2007-09-27 06:19:28 +08:00
|
|
|
" -Z<file> redirect error messages to file\n"
|
2005-01-16 06:15:51 +08:00
|
|
|
" -s redirect error messages to stdout\n\n"
|
2016-03-08 15:18:30 +08:00
|
|
|
" -g generate debugging information\n\n"
|
2005-01-16 06:15:51 +08:00
|
|
|
" -F format select a debugging format\n\n"
|
2016-03-08 15:18:30 +08:00
|
|
|
" -gformat same as -g -F format\n\n"
|
2013-04-21 00:37:17 +08:00
|
|
|
" -o outfile write output to an outfile\n\n"
|
|
|
|
" -f format select an output format\n\n"
|
|
|
|
" -l listfile write listing to a listfile\n\n"
|
2005-01-16 06:15:51 +08:00
|
|
|
" -I<path> adds a pathname to the include file path\n");
|
|
|
|
printf
|
2010-07-26 03:43:30 +08:00
|
|
|
(" -O<digit> optimize branch offsets\n"
|
2012-03-11 18:19:17 +08:00
|
|
|
" -O0: No optimization\n"
|
2009-07-31 14:26:55 +08:00
|
|
|
" -O1: Minimal optimization\n"
|
2012-03-11 18:19:17 +08:00
|
|
|
" -Ox: Multipass optimization (default)\n\n"
|
2005-01-16 06:15:51 +08:00
|
|
|
" -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"
|
2007-11-14 03:31:15 +08:00
|
|
|
" -w+foo enables warning foo (equiv. -Wfoo)\n"
|
2009-07-31 04:32:17 +08:00
|
|
|
" -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
|
2017-03-08 17:26:40 +08:00
|
|
|
" -w[+-]error[=foo] can be used to promote warnings to errors\n"
|
|
|
|
" -h show invocation summary and exit\n\n"
|
2009-07-31 04:32:17 +08:00
|
|
|
"--prefix,--postfix\n"
|
2017-03-08 17:26:40 +08:00
|
|
|
" these options prepend or append the given string\n"
|
|
|
|
" to all extern and global variables\n"
|
|
|
|
"\n"
|
|
|
|
"Response files should contain command line parameters,\n"
|
|
|
|
"one per line.\n"
|
|
|
|
"\n"
|
|
|
|
"Warnings for the -W/-w options:\n");
|
|
|
|
for (i = 0; i <= ERR_WARN_ALL; i++)
|
|
|
|
printf(" %-23s %s%s\n",
|
2008-10-01 08:01:23 +08:00
|
|
|
warnings[i].name, warnings[i].help,
|
2017-03-08 17:26:40 +08:00
|
|
|
i == ERR_WARN_ALL ? "\n" :
|
|
|
|
warnings[i].enabled ? " (default on)" :
|
|
|
|
" (default off)");
|
2005-01-16 06:15:51 +08:00
|
|
|
if (p[2] == 'f') {
|
2017-03-08 17:26:40 +08:00
|
|
|
printf("valid output formats for -f are"
|
2005-01-16 06:15:51 +08:00
|
|
|
" (`*' denotes default):\n");
|
|
|
|
ofmt_list(ofmt, stdout);
|
|
|
|
} else {
|
2017-03-08 17:26:40 +08:00
|
|
|
printf("For a list of valid output formats, use -hf.\n");
|
2005-01-16 06:15:51 +08:00
|
|
|
printf("For a list of debug formats, use -f <form> -y.\n");
|
|
|
|
}
|
2013-02-15 16:20:58 +08:00
|
|
|
exit(0); /* never need usage message here */
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 'y':
|
|
|
|
printf("\nvalid debug formats for '%s' output format are"
|
|
|
|
" ('*' denotes default):\n", ofmt->shortname);
|
|
|
|
dfmt_list(ofmt, stdout);
|
|
|
|
exit(0);
|
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 't':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
tasm_compatible_mode = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 'v':
|
2014-05-10 02:34:34 +08:00
|
|
|
show_version();
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'e': /* preprocess only */
|
|
|
|
case 'E':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 1)
|
|
|
|
operating_mode = OP_PREPROCESS;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2013-02-15 16:20:58 +08:00
|
|
|
case 'a': /* assemble only - don't preprocess */
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 1)
|
|
|
|
preproc = &preproc_nop;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 'w':
|
2017-03-08 17:26:40 +08:00
|
|
|
case 'W':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2) {
|
2017-03-08 17:26:40 +08:00
|
|
|
if (!set_warning_status(param)) {
|
|
|
|
nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
|
|
|
|
"unknown warning option: %s", param);
|
2016-10-04 10:46:49 +08:00
|
|
|
}
|
2011-12-05 05:39:04 +08:00
|
|
|
}
|
2017-03-08 17:26:40 +08:00
|
|
|
break;
|
2007-11-16 09:12:29 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 'M':
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2) {
|
|
|
|
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':
|
|
|
|
operating_mode = OP_NORMAL;
|
|
|
|
depend_file = q;
|
|
|
|
advance = true;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
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:
|
|
|
|
nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
|
|
|
|
"unknown dependency option `-M%c'", p[2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (advance && (!q || !q[0])) {
|
|
|
|
nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
|
|
|
|
"option `-M%c' requires a parameter", p[2]);
|
|
|
|
break;
|
|
|
|
}
|
2013-02-15 16:20:58 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (p[2] == 0) { /* -- => stop processing options */
|
|
|
|
stopoptions = 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-05-10 02:34:34 +08:00
|
|
|
|
|
|
|
if (!nasm_stricmp(p, "--v"))
|
|
|
|
show_version();
|
|
|
|
|
2016-09-13 05:02:21 +08:00
|
|
|
if (!nasm_stricmp(p, "--version"))
|
|
|
|
show_version();
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
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) {
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL | ERR_NOFILE |
|
2005-01-16 06:15:51 +08:00
|
|
|
ERR_USAGE,
|
|
|
|
"option `--%s' requires an argument",
|
|
|
|
p + 2);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
advance = 1, param = q;
|
|
|
|
}
|
|
|
|
|
2015-11-11 05:07:20 +08:00
|
|
|
switch (s) {
|
|
|
|
case OPT_PREFIX:
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
strlcpy(lprefix, param, PREFIX_MAX);
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2015-11-11 05:07:20 +08:00
|
|
|
case OPT_POSTFIX:
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass == 2)
|
|
|
|
strlcpy(lpostfix, param, POSTFIX_MAX);
|
2015-11-11 05:07:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
2016-03-04 06:27:34 +08:00
|
|
|
nasm_panic(ERR_NOFILE,
|
2015-11-11 05:07:20 +08:00
|
|
|
"internal error");
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-02-09 02:32:13 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
default:
|
|
|
|
{
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
|
2005-01-16 06:15:51 +08:00
|
|
|
"unrecognised option `--%s'", p + 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!ofmt->setinfo(GI_SWITCH, &p))
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
|
2005-01-16 06:15:51 +08:00
|
|
|
"unrecognised option `-%c'", p[1]);
|
|
|
|
break;
|
|
|
|
}
|
2016-10-04 10:46:49 +08:00
|
|
|
} else if (pass == 2) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*inname) {
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
|
2005-01-16 06:15:51 +08:00
|
|
|
"more than one input file specified");
|
2007-11-19 03:55:10 +08:00
|
|
|
} else {
|
|
|
|
copy_filename(inname, p);
|
2013-02-15 16:20:58 +08:00
|
|
|
}
|
2002-05-01 04:52:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return advance;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
#define ARG_BUF_DELTA 128
|
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
static void process_respfile(FILE * rfile, int pass)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *buffer, *p, *q, *prevarg;
|
2002-05-01 04:53:55 +08: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-16 06:15:51 +08: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])
|
2016-10-04 10:46:49 +08:00
|
|
|
process_arg(prevarg, NULL, pass);
|
2005-01-16 06:15:51 +08:00
|
|
|
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';
|
|
|
|
|
2008-06-22 01:23:17 +08:00
|
|
|
while (p > buffer && nasm_isspace(p[-1]))
|
2005-01-16 06:15:51 +08:00
|
|
|
*--p = '\0';
|
|
|
|
|
2009-10-13 23:38:52 +08:00
|
|
|
p = nasm_skip_spaces(buffer);
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
if (process_arg(prevarg, p, pass))
|
2005-01-16 06:15:51 +08:00
|
|
|
*p = '\0';
|
|
|
|
|
2007-10-19 10:02:42 +08:00
|
|
|
if ((int) strlen(p) > prevargsize - 10) {
|
2005-01-16 06:15:51 +08:00
|
|
|
prevargsize += ARG_BUF_DELTA;
|
|
|
|
prevarg = nasm_realloc(prevarg, prevargsize);
|
|
|
|
}
|
2007-11-19 03:55:10 +08:00
|
|
|
strncpy(prevarg, p, prevargsize);
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-01 05:00:33 +08:00
|
|
|
/* Function to process args from a string of args, rather than the
|
|
|
|
* argv array. Used by the environment variable and response file
|
|
|
|
* processing.
|
|
|
|
*/
|
2016-10-04 10:46:49 +08:00
|
|
|
static void process_args(char *args, int pass)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p, *q, *arg, *prevarg;
|
|
|
|
char separator = ' ';
|
2002-05-01 05:00:33 +08:00
|
|
|
|
|
|
|
p = args;
|
|
|
|
if (*p && *p != '-')
|
2005-01-16 06:15:51 +08:00
|
|
|
separator = *p++;
|
2002-05-01 05:00:33 +08:00
|
|
|
arg = NULL;
|
|
|
|
while (*p) {
|
2005-01-16 06:15:51 +08:00
|
|
|
q = p;
|
|
|
|
while (*p && *p != separator)
|
|
|
|
p++;
|
|
|
|
while (*p == separator)
|
|
|
|
*p++ = '\0';
|
|
|
|
prevarg = arg;
|
|
|
|
arg = q;
|
2016-10-04 10:46:49 +08:00
|
|
|
if (process_arg(prevarg, arg, pass))
|
2005-01-16 06:15:51 +08:00
|
|
|
arg = NULL;
|
2002-05-01 05:00:33 +08:00
|
|
|
}
|
|
|
|
if (arg)
|
2016-10-04 10:46:49 +08:00
|
|
|
process_arg(arg, NULL, pass);
|
2002-05-01 05:00:33 +08:00
|
|
|
}
|
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
static void process_response_file(const char *file, int pass)
|
2008-01-22 08:23:59 +08:00
|
|
|
{
|
|
|
|
char str[2048];
|
2016-05-25 19:28:46 +08:00
|
|
|
FILE *f = nasm_open_read(file, NF_TEXT);
|
2008-01-22 08:23:59 +08:00
|
|
|
if (!f) {
|
2013-02-15 16:14:06 +08:00
|
|
|
perror(file);
|
|
|
|
exit(-1);
|
2008-01-22 08:23:59 +08:00
|
|
|
}
|
|
|
|
while (fgets(str, sizeof str, f)) {
|
2016-10-04 10:46:49 +08:00
|
|
|
process_args(str, pass);
|
2008-01-22 08:23:59 +08:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2016-10-04 10:46:49 +08:00
|
|
|
static void parse_cmdline(int argc, char **argv, int pass)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
|
|
|
FILE *rfile;
|
2011-07-17 17:55:25 +08:00
|
|
|
char *envreal, *envcopy = NULL, *p;
|
2008-10-01 08:01:23 +08:00
|
|
|
int i;
|
2002-05-01 04:52:26 +08:00
|
|
|
|
2007-10-01 13:15:36 +08:00
|
|
|
*inname = *outname = *listname = *errname = '\0';
|
2011-12-05 05:44:43 +08:00
|
|
|
|
2017-03-08 17:26:40 +08:00
|
|
|
/* Initialize all the warnings to their default state */
|
|
|
|
for (i = 0; i < ERR_WARN_ALL; i++) {
|
|
|
|
warning_state_init[i] = warning_state[i] =
|
|
|
|
warnings[i].enabled ? WARN_ST_ENABLED : 0;
|
|
|
|
}
|
2002-05-01 04:52:26 +08:00
|
|
|
|
|
|
|
/*
|
2002-05-07 03:41:57 +08:00
|
|
|
* First, process the NASMENV environment variable.
|
2002-05-01 04:52:26 +08:00
|
|
|
*/
|
2002-05-07 03:41:57 +08:00
|
|
|
envreal = getenv("NASMENV");
|
2002-05-01 04:52:26 +08:00
|
|
|
if (envreal) {
|
2005-01-16 06:15:51 +08:00
|
|
|
envcopy = nasm_strdup(envreal);
|
2016-10-04 10:46:49 +08:00
|
|
|
process_args(envcopy, pass);
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(envcopy);
|
2002-05-01 04:52:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now process the actual command line.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
while (--argc) {
|
2011-12-05 05:44:43 +08:00
|
|
|
bool advance;
|
2005-01-16 06:15:51 +08:00
|
|
|
argv++;
|
|
|
|
if (argv[0][0] == '@') {
|
2011-12-05 05:44:43 +08:00
|
|
|
/*
|
|
|
|
* We have a response file, so process this as a set of
|
2005-01-16 06:15:51 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
2016-10-04 10:46:49 +08:00
|
|
|
process_response_file(argv[0]+1, pass);
|
2005-01-16 06:15:51 +08:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
|
2011-12-05 05:44:43 +08:00
|
|
|
p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
|
2007-04-14 00:47:53 +08:00
|
|
|
if (p) {
|
2016-05-25 19:28:46 +08:00
|
|
|
rfile = nasm_open_read(p, NF_TEXT);
|
2007-04-14 00:47:53 +08:00
|
|
|
if (rfile) {
|
2016-10-04 10:46:49 +08:00
|
|
|
process_respfile(rfile, pass);
|
2005-01-16 06:15:51 +08:00
|
|
|
fclose(rfile);
|
|
|
|
} else
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
|
2005-01-16 06:15:51 +08:00
|
|
|
"unable to open response file `%s'", p);
|
|
|
|
}
|
|
|
|
} else
|
2016-10-04 10:46:49 +08:00
|
|
|
advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
|
2007-11-16 09:12:29 +08:00
|
|
|
argv += advance, argc -= advance;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 04:52:26 +08:00
|
|
|
|
2011-12-05 05:44:43 +08:00
|
|
|
/*
|
|
|
|
* Look for basic command line typos. This definitely doesn't
|
|
|
|
* catch all errors, but it might help cases of fumbled fingers.
|
|
|
|
*/
|
2016-10-04 10:46:49 +08:00
|
|
|
if (pass != 2)
|
|
|
|
return;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
if (!*inname)
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
|
2011-12-05 05:44:43 +08:00
|
|
|
"no input file specified");
|
|
|
|
else if (!strcmp(inname, errname) ||
|
|
|
|
!strcmp(inname, outname) ||
|
|
|
|
!strcmp(inname, listname) ||
|
|
|
|
(depend_file && !strcmp(inname, depend_file)))
|
2016-03-04 06:27:34 +08:00
|
|
|
nasm_fatal(ERR_NOFILE | ERR_USAGE,
|
2011-12-05 05:44:43 +08:00
|
|
|
"file `%s' is both input and output file",
|
|
|
|
inname);
|
2007-10-02 02:26:31 +08:00
|
|
|
|
2007-10-20 05:42:29 +08:00
|
|
|
if (*errname) {
|
2016-05-25 19:28:46 +08:00
|
|
|
error_file = nasm_open_write(errname, NF_TEXT);
|
2011-12-05 05:44:43 +08:00
|
|
|
if (!error_file) {
|
|
|
|
error_file = stderr; /* Revert to default! */
|
2016-03-04 06:27:34 +08:00
|
|
|
nasm_fatal(ERR_NOFILE | ERR_USAGE,
|
2011-12-05 05:44:43 +08:00
|
|
|
"cannot open file `%s' for error messages",
|
|
|
|
errname);
|
|
|
|
}
|
2007-10-01 13:15:36 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2008-05-30 12:38:00 +08:00
|
|
|
static void assemble_file(char *fname, StrList **depend_ptr)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2017-03-08 09:45:01 +08:00
|
|
|
char *line;
|
2005-01-16 06:15:51 +08:00
|
|
|
insn output_ins;
|
2017-03-08 09:45:01 +08:00
|
|
|
int i;
|
2007-11-08 11:03:46 +08:00
|
|
|
int64_t offs;
|
2008-01-09 15:03:57 +08:00
|
|
|
int pass_max;
|
2016-11-16 23:48:13 +08:00
|
|
|
int sb;
|
2017-03-08 11:44:21 +08:00
|
|
|
uint64_t prev_offset_changed;
|
|
|
|
unsigned int stall_count = 0; /* Make sure we make forward progress... */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2013-11-10 02:16:11 +08:00
|
|
|
if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2008-09-12 09:54:06 +08:00
|
|
|
pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
|
2008-01-09 15:03:57 +08:00
|
|
|
for (passn = 1; pass0 <= 2; passn++) {
|
2005-01-16 06:15:51 +08:00
|
|
|
ldfunc def_label;
|
|
|
|
|
2013-02-15 16:13:09 +08:00
|
|
|
pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
|
2008-01-09 15:03:57 +08:00
|
|
|
pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
|
|
|
|
/* pass0 0, 0, 0, ..., 1, 2 */
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2008-01-09 15:03:57 +08:00
|
|
|
def_label = passn > 1 ? redefine_label : define_label;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
globalbits = sb = cmd_sb; /* set 'bits' to command line default */
|
2005-01-16 06:15:51 +08:00
|
|
|
cpu = cmd_cpu;
|
|
|
|
if (pass0 == 2) {
|
2016-02-18 17:16:18 +08:00
|
|
|
lfmt->init(listname);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2017-03-08 11:23:03 +08:00
|
|
|
in_absolute = false;
|
2008-09-12 09:54:06 +08:00
|
|
|
global_offset_changed = 0; /* set by redefine_label */
|
2005-01-16 06:15:51 +08:00
|
|
|
location.segment = ofmt->section(NULL, pass2, &sb);
|
2007-04-12 10:40:54 +08:00
|
|
|
globalbits = sb;
|
2008-01-09 15:03:57 +08:00
|
|
|
if (passn > 1) {
|
2005-01-16 06:15:51 +08:00
|
|
|
saa_rewind(forwrefs);
|
|
|
|
forwref = saa_rstruct(forwrefs);
|
|
|
|
raa_free(offsets);
|
|
|
|
offsets = raa_init();
|
|
|
|
}
|
2016-02-18 12:27:41 +08:00
|
|
|
preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
|
2017-03-08 17:26:40 +08:00
|
|
|
|
|
|
|
/* Revert all warnings to the default state */
|
|
|
|
memcpy(warning_state, warning_state_init, sizeof warning_state);
|
2008-07-16 21:20:56 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
globallineno = 0;
|
2008-01-09 15:03:57 +08:00
|
|
|
if (passn == 1)
|
2007-10-11 05:58:45 +08:00
|
|
|
location.known = true;
|
2013-02-15 16:35:04 +08:00
|
|
|
location.offset = offs = get_curr_offs();
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
while ((line = preproc->getline())) {
|
|
|
|
globallineno++;
|
|
|
|
|
2009-06-28 07:30:00 +08:00
|
|
|
/*
|
2013-02-15 16:13:09 +08:00
|
|
|
* Here we parse our directives; this is not handled by the
|
2017-03-08 09:45:01 +08:00
|
|
|
* main parser.
|
2013-02-15 16:13:09 +08:00
|
|
|
*/
|
2017-03-08 13:32:37 +08:00
|
|
|
if (process_directives(line))
|
2017-03-08 09:45:01 +08:00
|
|
|
goto end_of_line; /* Just do final cleanup */
|
|
|
|
|
|
|
|
/* Not a directive, or even something that starts with [ */
|
|
|
|
|
|
|
|
parse_line(pass1, line, &output_ins, def_label);
|
2016-02-17 09:37:18 +08:00
|
|
|
|
2017-03-08 09:45:01 +08:00
|
|
|
if (optimizing > 0) {
|
|
|
|
if (forwref != NULL && globallineno == forwref->lineno) {
|
|
|
|
output_ins.forw_ref = true;
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (output_ins.forw_ref) {
|
|
|
|
if (passn == 1) {
|
|
|
|
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;
|
|
|
|
}
|
2010-04-20 19:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 09:45:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* forw_ref */
|
|
|
|
if (output_ins.opcode == I_EQU) {
|
|
|
|
if (pass1 == 1) {
|
|
|
|
/*
|
|
|
|
* Special `..' EQUs get processed in pass two,
|
|
|
|
* except `..@' macro-processor EQUs which are done
|
|
|
|
* in the normal place.
|
|
|
|
*/
|
|
|
|
if (!output_ins.label)
|
|
|
|
nasm_error(ERR_NONFATAL,
|
|
|
|
"EQU not preceded by label");
|
|
|
|
|
|
|
|
else if (output_ins.label[0] != '.' ||
|
|
|
|
output_ins.label[1] != '.' ||
|
|
|
|
output_ins.label[2] == '@') {
|
|
|
|
if (output_ins.operands == 1 &&
|
|
|
|
(output_ins.oprs[0].type & IMMEDIATE) &&
|
|
|
|
output_ins.oprs[0].wrt == NO_SEG) {
|
|
|
|
bool 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);
|
|
|
|
} 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);
|
2002-05-01 04:59:21 +08:00
|
|
|
} else
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
2017-03-08 09:45:01 +08:00
|
|
|
"bad syntax for EQU");
|
2013-02-15 16:13:09 +08:00
|
|
|
}
|
2017-03-08 09:45:01 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Special `..' EQUs get processed here, except
|
|
|
|
* `..@' macro processor EQUs which are done above.
|
|
|
|
*/
|
|
|
|
if (output_ins.label[0] == '.' &&
|
|
|
|
output_ins.label[1] == '.' &&
|
|
|
|
output_ins.label[2] != '@') {
|
|
|
|
if (output_ins.operands == 1 &&
|
|
|
|
(output_ins.oprs[0].type & IMMEDIATE)) {
|
|
|
|
define_label(output_ins.label,
|
|
|
|
output_ins.oprs[0].segment,
|
|
|
|
output_ins.oprs[0].offset,
|
|
|
|
NULL, false, false);
|
|
|
|
} 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,
|
|
|
|
output_ins.oprs[1].offset,
|
|
|
|
NULL, false, false);
|
|
|
|
} else
|
2013-02-15 16:13:09 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
2017-03-08 09:45:01 +08:00
|
|
|
"bad syntax for EQU");
|
2013-02-15 16:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-08 09:45:01 +08:00
|
|
|
} else { /* instruction isn't an EQU */
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2017-03-08 09:45:01 +08:00
|
|
|
if (pass1 == 1) {
|
2017-03-08 11:23:03 +08:00
|
|
|
int64_t l = insn_size(location.segment, offs, sb,
|
2017-03-08 09:45:01 +08:00
|
|
|
&output_ins);
|
|
|
|
l *= output_ins.times;
|
|
|
|
|
|
|
|
/* 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 =
|
|
|
|
TYS_ELEMENTS(output_ins.operands);
|
|
|
|
switch (output_ins.opcode) {
|
|
|
|
case I_RESB:
|
|
|
|
typeinfo =
|
|
|
|
TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
|
2013-02-15 16:13:09 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_RESW:
|
|
|
|
typeinfo =
|
|
|
|
TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
|
2013-02-15 16:13:09 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_RESD:
|
|
|
|
typeinfo =
|
|
|
|
TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
|
2013-02-15 16:13:09 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_RESQ:
|
|
|
|
typeinfo =
|
|
|
|
TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
|
2013-02-15 16:13:09 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_REST:
|
|
|
|
typeinfo =
|
|
|
|
TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
|
2013-02-15 16:13:09 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_RESO:
|
|
|
|
typeinfo =
|
|
|
|
TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
|
2013-02-15 16:13:09 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_RESY:
|
|
|
|
typeinfo =
|
|
|
|
TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
|
2013-12-05 12:05:55 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_DB:
|
|
|
|
typeinfo |= TY_BYTE;
|
2013-12-05 12:05:55 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
case I_DW:
|
|
|
|
typeinfo |= TY_WORD;
|
2013-02-15 16:13:09 +08:00
|
|
|
break;
|
2017-03-08 09:45:01 +08:00
|
|
|
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;
|
|
|
|
case I_DO:
|
|
|
|
typeinfo |= TY_OWORD;
|
|
|
|
break;
|
|
|
|
case I_DY:
|
|
|
|
typeinfo |= TY_YWORD;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
typeinfo = TY_LABEL;
|
2002-05-01 04:59:21 +08:00
|
|
|
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2017-03-08 09:45:01 +08:00
|
|
|
dfmt->debug_typevalue(typeinfo);
|
|
|
|
}
|
|
|
|
if (l != -1) {
|
|
|
|
offs += l;
|
|
|
|
set_curr_offs(offs);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* else l == -1 => invalid instruction, which will be
|
|
|
|
* flagged as an error on pass 2
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2017-03-08 09:45:01 +08:00
|
|
|
} else {
|
2017-03-08 11:23:03 +08:00
|
|
|
offs += assemble(location.segment, offs, sb, &output_ins);
|
2017-03-08 09:45:01 +08:00
|
|
|
set_curr_offs(offs);
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2017-03-08 09:45:01 +08:00
|
|
|
}
|
|
|
|
} /* not an EQU */
|
|
|
|
cleanup_insn(&output_ins);
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2017-03-08 09:45:01 +08:00
|
|
|
end_of_line:
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(line);
|
2013-02-15 16:35:04 +08:00
|
|
|
location.offset = offs = get_curr_offs();
|
2005-01-16 06:15:51 +08:00
|
|
|
} /* end while (line = preproc->getline... */
|
2009-06-28 07:30:00 +08:00
|
|
|
|
2009-07-02 13:12:59 +08:00
|
|
|
if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
2013-02-15 16:13:09 +08:00
|
|
|
"phase error detected at end of assembly.");
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
if (pass1 == 1)
|
|
|
|
preproc->cleanup(1);
|
|
|
|
|
2009-06-28 06:34:32 +08:00
|
|
|
if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
|
2005-01-16 06:15:51 +08:00
|
|
|
pass0++;
|
2013-02-15 16:13:09 +08:00
|
|
|
} else if (global_offset_changed &&
|
|
|
|
global_offset_changed < prev_offset_changed) {
|
2008-09-12 09:54:06 +08:00
|
|
|
prev_offset_changed = global_offset_changed;
|
|
|
|
stall_count = 0;
|
2013-02-15 16:13:09 +08:00
|
|
|
} else {
|
|
|
|
stall_count++;
|
|
|
|
}
|
2009-06-28 06:34:32 +08:00
|
|
|
|
2013-02-15 16:13:09 +08:00
|
|
|
if (terminate_after_phase)
|
|
|
|
break;
|
2008-09-11 21:07:05 +08:00
|
|
|
|
2017-03-08 11:44:21 +08:00
|
|
|
if ((stall_count > 997U) || (passn >= pass_max)) {
|
2008-09-11 21:07:05 +08:00
|
|
|
/* We get here if the labels don't converge
|
|
|
|
* Example: FOO equ FOO + 1
|
|
|
|
*/
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
2008-09-11 21:07:05 +08:00
|
|
|
"Can't find valid values for all labels "
|
2009-06-28 06:34:32 +08:00
|
|
|
"after %d passes, giving up.", passn);
|
2013-02-15 16:13:09 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
|
|
|
"Possible causes: recursive EQUs, macro abuse.");
|
|
|
|
break;
|
|
|
|
}
|
2008-01-09 15:03:57 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
preproc->cleanup(0);
|
2016-02-18 17:16:18 +08:00
|
|
|
lfmt->cleanup();
|
2009-06-28 06:34:32 +08:00
|
|
|
if (!terminate_after_phase && opt_verbose_info) {
|
2013-02-15 16:13:09 +08:00
|
|
|
/* -On and -Ov switches */
|
2009-06-28 06:34:32 +08:00
|
|
|
fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
|
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-06-10 04:56:40 +08: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
|
2007-10-20 05:42:29 +08:00
|
|
|
* where file.asm is the name of the file, 50 is the line number on
|
2002-06-10 04:56:40 +08:00
|
|
|
* which the error occurs (or is detected) and "error:" is one of
|
|
|
|
* the possible optional diagnostics -- it can be "error" or "warning"
|
2007-10-20 05:42:29 +08:00
|
|
|
* or something else. Finally the line terminates with the actual
|
2002-06-10 04:56:40 +08:00
|
|
|
* error message.
|
2007-10-20 05:42:29 +08:00
|
|
|
*
|
|
|
|
* @param severity the severity of the warning or error
|
2002-06-10 04:56:40 +08:00
|
|
|
* @param fmt the printf style format string
|
|
|
|
*/
|
2009-07-19 09:07:17 +08:00
|
|
|
static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2016-05-10 17:56:29 +08:00
|
|
|
const char *currentfile = NULL;
|
2009-07-02 13:12:59 +08:00
|
|
|
int32_t lineno = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-06-10 04:56:40 +08:00
|
|
|
if (is_suppressed_warning(severity))
|
2005-01-16 06:15:51 +08:00
|
|
|
return;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2009-07-02 13:12:59 +08:00
|
|
|
if (!(severity & ERR_NOFILE))
|
2016-05-10 04:59:44 +08:00
|
|
|
src_get(&lineno, ¤tfile);
|
2009-07-02 13:12:59 +08:00
|
|
|
|
2016-02-18 12:47:01 +08:00
|
|
|
if (!skip_this_pass(severity)) {
|
|
|
|
if (currentfile) {
|
|
|
|
fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
|
|
|
|
} else {
|
|
|
|
fputs("nasm: ", error_file);
|
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
2009-07-02 13:12:59 +08:00
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_verror_common(severity, fmt, ap);
|
2002-06-10 04:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MS style error reporting
|
|
|
|
* This function prints an error message to error_file in the
|
2007-10-20 05:42:29 +08:00
|
|
|
* style used by Visual C and some other Microsoft tools. An example
|
2002-06-10 04:56:40 +08:00
|
|
|
* would be:
|
2002-06-11 11:29:36 +08:00
|
|
|
* file.asm(50) : error: blah blah blah
|
2007-10-20 05:42:29 +08:00
|
|
|
* where file.asm is the name of the file, 50 is the line number on
|
2002-06-11 11:29:36 +08:00
|
|
|
* which the error occurs (or is detected) and "error:" is one of
|
|
|
|
* the possible optional diagnostics -- it can be "error" or "warning"
|
2007-10-20 05:42:29 +08:00
|
|
|
* or something else. Finally the line terminates with the actual
|
2002-06-11 11:29:36 +08:00
|
|
|
* error message.
|
2007-10-20 05:42:29 +08:00
|
|
|
*
|
|
|
|
* @param severity the severity of the warning or error
|
2002-06-10 04:56:40 +08:00
|
|
|
* @param fmt the printf style format string
|
|
|
|
*/
|
2009-07-19 09:07:17 +08:00
|
|
|
static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
|
2002-06-10 04:56:40 +08:00
|
|
|
{
|
2016-05-10 17:56:29 +08:00
|
|
|
const char *currentfile = NULL;
|
2009-07-02 13:12:59 +08:00
|
|
|
int32_t lineno = 0;
|
2002-06-10 04:56:40 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (is_suppressed_warning(severity))
|
|
|
|
return;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2009-07-02 13:12:59 +08:00
|
|
|
if (!(severity & ERR_NOFILE))
|
2005-01-16 06:15:51 +08:00
|
|
|
src_get(&lineno, ¤tfile);
|
2009-07-02 13:12:59 +08:00
|
|
|
|
2016-02-18 12:47:01 +08:00
|
|
|
if (!skip_this_pass(severity)) {
|
|
|
|
if (currentfile) {
|
|
|
|
fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
|
|
|
|
} else {
|
|
|
|
fputs("nasm: ", error_file);
|
|
|
|
}
|
2002-06-10 04:56:40 +08:00
|
|
|
}
|
2009-07-02 13:12:59 +08:00
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_verror_common(severity, fmt, ap);
|
2002-06-10 04:56:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-08 11:23:03 +08:00
|
|
|
/*
|
|
|
|
* check to see if this is a suppressable warning
|
|
|
|
*/
|
2017-03-08 17:26:40 +08:00
|
|
|
static inline bool is_valid_warning(int severity)
|
2017-03-08 11:23:03 +08:00
|
|
|
{
|
|
|
|
/* Not a warning at all */
|
|
|
|
if ((severity & ERR_MASK) != ERR_WARNING)
|
|
|
|
return false;
|
|
|
|
|
2017-03-08 17:26:40 +08:00
|
|
|
return WARN_IDX(severity) < ERR_WARN_ALL;
|
2017-03-08 11:23:03 +08:00
|
|
|
}
|
|
|
|
|
2002-06-10 04:56:40 +08:00
|
|
|
/**
|
2017-03-08 11:23:03 +08:00
|
|
|
* check for suppressed warning
|
2007-10-20 05:42:29 +08:00
|
|
|
* checks for suppressed warning or pass one only warning and we're
|
2002-06-10 04:56:40 +08:00
|
|
|
* not in pass 1
|
|
|
|
*
|
|
|
|
* @param severity the severity of the warning or error
|
|
|
|
* @return true if we should abort error/warning printing
|
|
|
|
*/
|
2008-01-23 06:08:36 +08:00
|
|
|
static bool is_suppressed_warning(int severity)
|
2002-06-10 04:56:40 +08:00
|
|
|
{
|
2012-02-25 13:50:53 +08:00
|
|
|
/* Might be a warning but suppresed explicitly */
|
2017-03-08 17:26:40 +08:00
|
|
|
if (is_valid_warning(severity))
|
|
|
|
return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool warning_is_error(int severity)
|
|
|
|
{
|
|
|
|
if (is_valid_warning(severity))
|
|
|
|
return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
|
2012-02-25 13:50:53 +08:00
|
|
|
else
|
|
|
|
return false;
|
2002-06-10 04:56:40 +08:00
|
|
|
}
|
|
|
|
|
2016-02-18 12:47:01 +08:00
|
|
|
static bool skip_this_pass(int severity)
|
|
|
|
{
|
2016-05-10 03:00:19 +08:00
|
|
|
/* See if it's a pass-specific warning which should be skipped. */
|
|
|
|
|
2016-02-18 12:47:01 +08:00
|
|
|
if ((severity & ERR_MASK) > ERR_WARNING)
|
|
|
|
return false;
|
|
|
|
|
2016-05-10 03:00:19 +08:00
|
|
|
/*
|
|
|
|
* passn is 1 on the very first pass only.
|
|
|
|
* pass0 is 2 on the code-generation (final) pass only.
|
|
|
|
* These are the passes we care about in this case.
|
|
|
|
*/
|
|
|
|
return (((severity & ERR_PASS1) && passn != 1) ||
|
|
|
|
((severity & ERR_PASS2) && pass0 != 2));
|
2016-02-18 12:47:01 +08:00
|
|
|
}
|
|
|
|
|
2002-06-10 04:56:40 +08:00
|
|
|
/**
|
|
|
|
* common error reporting
|
|
|
|
* This is the common back end of the error reporting schemes currently
|
2007-10-20 05:42:29 +08:00
|
|
|
* implemented. It prints the nature of the warning and then the
|
2002-06-10 04:56:40 +08:00
|
|
|
* 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.
|
2007-10-20 05:42:29 +08:00
|
|
|
*
|
|
|
|
* @param severity the severity of the warning or error
|
2002-06-10 04:56:40 +08:00
|
|
|
* @param fmt the printf style format string
|
|
|
|
*/
|
2009-07-19 09:07:17 +08:00
|
|
|
static void nasm_verror_common(int severity, const char *fmt, va_list args)
|
2002-06-10 04:56:40 +08:00
|
|
|
{
|
2009-07-08 03:04:12 +08:00
|
|
|
char msg[1024];
|
|
|
|
const char *pfx;
|
2009-07-13 03:04:56 +08:00
|
|
|
|
2008-06-11 09:27:38 +08:00
|
|
|
switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case ERR_WARNING:
|
2009-07-08 03:04:12 +08:00
|
|
|
pfx = "warning: ";
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case ERR_NONFATAL:
|
2009-07-08 03:04:12 +08:00
|
|
|
pfx = "error: ";
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case ERR_FATAL:
|
2009-07-08 03:04:12 +08:00
|
|
|
pfx = "fatal: ";
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case ERR_PANIC:
|
2009-07-08 03:04:12 +08:00
|
|
|
pfx = "panic: ";
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case ERR_DEBUG:
|
2009-07-08 03:04:12 +08:00
|
|
|
pfx = "debug: ";
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2008-06-11 09:27:38 +08:00
|
|
|
default:
|
2013-02-15 06:20:26 +08:00
|
|
|
pfx = "";
|
|
|
|
break;
|
2002-05-01 04:59:21 +08:00
|
|
|
}
|
2002-05-01 05:01:08 +08:00
|
|
|
|
2016-05-10 03:00:19 +08:00
|
|
|
vsnprintf(msg, sizeof msg - 64, fmt, args);
|
2017-03-08 17:26:40 +08:00
|
|
|
if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
|
2017-03-08 11:23:03 +08:00
|
|
|
char *p = strchr(msg, '\0');
|
2016-05-10 03:00:19 +08:00
|
|
|
snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
|
|
|
|
}
|
2009-07-08 03:04:12 +08:00
|
|
|
|
2016-02-18 12:47:01 +08:00
|
|
|
if (!skip_this_pass(severity))
|
|
|
|
fprintf(error_file, "%s%s\n", pfx, msg);
|
2009-07-08 03:04:12 +08:00
|
|
|
|
2016-05-10 04:59:44 +08:00
|
|
|
/* Are we recursing from error_list_macros? */
|
|
|
|
if (severity & ERR_PP_LISTMACRO)
|
|
|
|
return;
|
|
|
|
|
2016-02-18 12:47:01 +08:00
|
|
|
/*
|
|
|
|
* Don't suppress this with skip_this_pass(), or we don't get
|
2016-05-10 03:00:19 +08:00
|
|
|
* pass1 or preprocessor warnings in the list file
|
2016-02-18 12:47:01 +08:00
|
|
|
*/
|
2016-07-07 02:55:25 +08:00
|
|
|
lfmt->error(severity, pfx, msg);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (severity & ERR_USAGE)
|
2007-10-11 05:58:45 +08:00
|
|
|
want_usage = true;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2016-05-10 04:59:44 +08:00
|
|
|
preproc->error_list_macros(severity);
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
switch (severity & ERR_MASK) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case ERR_DEBUG:
|
|
|
|
/* no further action, by definition */
|
|
|
|
break;
|
2007-11-14 03:31:15 +08:00
|
|
|
case ERR_WARNING:
|
2013-02-15 06:20:26 +08:00
|
|
|
/* Treat warnings as errors */
|
2017-03-08 17:26:40 +08:00
|
|
|
if (warning_is_error(severity))
|
2013-02-15 06:20:26 +08:00
|
|
|
terminate_after_phase = true;
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
case ERR_NONFATAL:
|
2007-10-11 05:58:45 +08:00
|
|
|
terminate_after_phase = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case ERR_FATAL:
|
|
|
|
if (ofile) {
|
|
|
|
fclose(ofile);
|
|
|
|
remove(outname);
|
2013-02-15 06:20:26 +08:00
|
|
|
ofile = NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
if (want_usage)
|
|
|
|
usage();
|
|
|
|
exit(1); /* instantly die */
|
|
|
|
break; /* placate silly compilers */
|
|
|
|
case ERR_PANIC:
|
|
|
|
fflush(NULL);
|
2013-02-15 06:20:26 +08:00
|
|
|
/* abort(); */ /* halt, catch fire, and dump core */
|
2016-02-18 12:47:01 +08:00
|
|
|
if (ofile) {
|
|
|
|
fclose(ofile);
|
|
|
|
remove(outname);
|
|
|
|
ofile = NULL;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
exit(3);
|
|
|
|
break;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-01 05:01:08 +08:00
|
|
|
static void usage(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:57:38 +08:00
|
|
|
fputs("type `nasm -h' for help\n", error_file);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|