📄 nasm.c
字号:
/* 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"
* distributed in the NASM archive.
*/
#include "compiler.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <time.h>
#include "nasm.h"
#include "nasmlib.h"
#include "saa.h"
#include "raa.h"
#include "float.h"
#include "stdscan.h"
#include "insns.h"
#include "preproc.h"
#include "parser.h"
#include "eval.h"
#include "assemble.h"
#include "labels.h"
#include "outform.h"
#include "listing.h"
struct forwrefinfo { /* info held on forward refs. */
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 **);
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,
va_list args);
static bool is_suppressed_warning(int severity);
static void usage(void);
static efunc report_error;
static int using_debug_info, opt_verbose_info;
bool tasm_compatible_mode = false;
int pass0, passn;
int maxbits = 0;
int globalrel = 0;
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];
static int globallineno; /* for forward-reference tracking */
/* static int pass = 0; */
static struct ofmt *ofmt = NULL;
static FILE *error_file; /* Where to write error messages */
static FILE *ofile = NULL;
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 */
int64_t global_offset_changed; /* referenced in labels.c */
int64_t prev_offset_changed;
int32_t stall_count;
static struct location location;
int in_abs_seg; /* Flag we are in ABSOLUTE seg */
int32_t abs_seg; /* ABSOLUTE segment basis */
int32_t abs_offset; /* ABSOLUTE offset */
static struct RAA *offsets;
static struct SAA *forwrefs; /* keep track of forward references */
static const struct forwrefinfo *forwref;
static Preproc *preproc;
enum op_type {
op_normal, /* Preprocess and assemble */
op_preprocess, /* Preprocess only */
op_depend, /* Generate dependencies */
};
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;
/*
* Which of the suppressible warnings are suppressed. Entry zero
* isn't an actual warning, but it used for -w+error/-Werror.
*/
static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
static const struct warning {
const char *name;
const char *help;
bool enabled;
} warnings[ERR_WARN_MAX+1] = {
{"error", "treat warnings as errors", false},
{"macro-params", "macro calls with wrong parameter count", true},
{"macro-selfref", "cyclic macro references", false},
{"macro-defaults", "macros with more default than optional parameters", true},
{"orphan-labels", "labels alone on lines without trailing `:'", true},
{"number-overflow", "numeric constants does not fit in 64 bits", true},
{"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
{"float-overflow", "floating point overflow", true},
{"float-denorm", "floating point denormal", false},
{"float-underflow", "floating point underflow", false},
{"float-toolong", "too many digits in floating-point number", true},
{"user", "%warning directives", true},
};
/*
* 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);
static void no_pp_cleanup(int);
static Preproc no_pp = {
no_pp_reset,
no_pp_getline,
no_pp_cleanup
};
/*
* get/set current offset...
*/
#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
raa_read(offsets,location.segment))
#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
(void)(offsets=raa_write(offsets,location.segment,(x))))
static int want_usage;
static int terminate_after_phase;
int user_nolist = 0; /* fbk 9/2/00 */
static void nasm_fputs(const char *line, FILE * outfile)
{
if (outfile) {
fputs(line, outfile);
putc('\n', outfile);
} else
puts(line);
}
/* 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\"", <);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", <);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", <);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", <);
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(<);
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)
{
StrList *depend_list = NULL, **depend_ptr;
time(&official_compile_time);
pass0 = 0;
want_usage = terminate_after_phase = false;
report_error = report_error_gnu;
error_file = stderr;
tolower_init();
nasm_set_malloc_error(report_error);
offsets = raa_init();
forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
preproc = &nasmpp;
operating_mode = op_normal;
seg_init();
register_output_formats();
/* Define some macros dependent on the runtime, but not
on the command line. */
define_macros_early();
parse_cmdline(argc, argv);
if (terminate_after_phase) {
if (want_usage)
usage();
return 1;
}
/* If debugging info is disabled, suppress any debug calls */
if (!using_debug_info)
ofmt->current_dfmt = &null_debug_form;
if (ofmt->stdmac)
pp_extra_stdmac(ofmt->stdmac);
parser_global_info(ofmt, &location);
eval_global_info(ofmt, lookup_label, &location);
/* define some macros dependent of command-line */
define_macros_late();
depend_ptr = (depend_file || (operating_mode == op_depend))
? &depend_list : NULL;
if (!depend_target)
depend_target = outname;
switch (operating_mode) {
case op_depend:
{
char *line;
if (depend_missing_ok)
pp_include_path(NULL); /* "assume generated" */
preproc->reset(inname, 0, report_error, evaluate, &nasmlist,
depend_ptr);
if (outname[0] == '\0')
ofmt->filename(inname, outname, report_error);
ofile = NULL;
while ((line = preproc->getline()))
nasm_free(line);
preproc->cleanup(0);
}
break;
case op_preprocess:
{
char *line;
char *file_name = NULL;
int32_t prior_linnum = 0;
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;
/* pass = 1; */
preproc->reset(inname, 3, report_error, evaluate, &nasmlist,
depend_ptr);
while ((line = preproc->getline())) {
/*
* We generate %line directives if needed for later programs
*/
int32_t linnum = prior_linnum += lineinc;
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,
"%%line %"PRId32"+%d %s\n", linnum, lineinc,
file_name);
}
prior_linnum = linnum;
}
nasm_fputs(line, ofile);
nasm_free(line);
}
nasm_free(file_name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -