mirror of
https://github.com/yasm/yasm
synced 2026-08-26 22:26:05 -04:00
Initial revision
svn path=/trunk/yasm/; revision=794
This commit is contained in:
parent
da2a49113a
commit
dd915a3eef
18 changed files with 15428 additions and 0 deletions
48
modules/preprocs/nasm/macros.pl
Normal file
48
modules/preprocs/nasm/macros.pl
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/perl -w
|
||||
#
|
||||
# macros.pl produce macros.c from standard.mac
|
||||
#
|
||||
# The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
# Julian Hall. All rights reserved. The software is
|
||||
# redistributable under the licence given in the file "Licence"
|
||||
# distributed in the NASM archive.
|
||||
|
||||
use strict;
|
||||
|
||||
my $fname;
|
||||
my $line = 0;
|
||||
my $index = 0;
|
||||
my $tasm_count;
|
||||
|
||||
undef $tasm_count;
|
||||
|
||||
open(OUTPUT,">macros.c") or die "unable to open macros.c\n";
|
||||
|
||||
print OUTPUT "/* This file auto-generated from standard.mac by macros.pl" .
|
||||
" - don't edit it */\n\n#include <stddef.h>\n\nstatic const char *stdmac[] = {\n";
|
||||
|
||||
foreach $fname ( @ARGV ) {
|
||||
open(INPUT,$fname) or die "unable to open $fname\n";
|
||||
while (<INPUT>) {
|
||||
$line++;
|
||||
chomp;
|
||||
if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
|
||||
$tasm_count = $index;
|
||||
} elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
|
||||
$_ = $1;
|
||||
s/\\/\\\\/g;
|
||||
s/"/\\"/g;
|
||||
if (length > 0) {
|
||||
print OUTPUT " \"$_\",\n";
|
||||
$index++;
|
||||
}
|
||||
} else {
|
||||
die "$fname:$line: error unterminated quote";
|
||||
}
|
||||
}
|
||||
close(INPUT);
|
||||
}
|
||||
print OUTPUT " NULL\n};\n";
|
||||
$tasm_count = $index unless ( defined($tasm_count) );
|
||||
print OUTPUT "#define TASM_MACRO_COUNT $tasm_count\n";
|
||||
close(OUTPUT);
|
||||
825
modules/preprocs/nasm/nasm-eval.c
Normal file
825
modules/preprocs/nasm/nasm-eval.c
Normal file
|
|
@ -0,0 +1,825 @@
|
|||
/* eval.c expression evaluator for the Netwide Assembler
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*
|
||||
* initial version 27/iii/95 by Simon Tatham
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "nasm.h"
|
||||
#include "nasmlib.h"
|
||||
#include "eval.h"
|
||||
#include "labels.h"
|
||||
|
||||
#define TEMPEXPRS_DELTA 128
|
||||
#define TEMPEXPR_DELTA 8
|
||||
|
||||
static scanner scan; /* Address of scanner routine */
|
||||
static efunc error; /* Address of error reporting routine */
|
||||
static lfunc labelfunc; /* Address of label routine */
|
||||
|
||||
static struct ofmt *outfmt; /* Structure of addresses of output routines */
|
||||
|
||||
static expr **tempexprs = NULL;
|
||||
static int ntempexprs;
|
||||
static int tempexprs_size = 0;
|
||||
|
||||
static expr *tempexpr;
|
||||
static int ntempexpr;
|
||||
static int tempexpr_size;
|
||||
|
||||
static struct tokenval *tokval; /* The current token */
|
||||
static int i; /* The t_type of tokval */
|
||||
|
||||
static void *scpriv;
|
||||
static loc_t *location; /* Pointer to current line's segment,offset */
|
||||
static int *opflags;
|
||||
|
||||
static struct eval_hints *hint;
|
||||
|
||||
extern int in_abs_seg; /* ABSOLUTE segment flag */
|
||||
extern long abs_seg; /* ABSOLUTE segment */
|
||||
extern long abs_offset; /* ABSOLUTE segment offset */
|
||||
|
||||
/*
|
||||
* Unimportant cleanup is done to avoid confusing people who are trying
|
||||
* to debug real memory leaks
|
||||
*/
|
||||
void eval_cleanup(void)
|
||||
{
|
||||
while (ntempexprs)
|
||||
nasm_free (tempexprs[--ntempexprs]);
|
||||
nasm_free (tempexprs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Construct a temporary expression.
|
||||
*/
|
||||
static void begintemp(void)
|
||||
{
|
||||
tempexpr = NULL;
|
||||
tempexpr_size = ntempexpr = 0;
|
||||
}
|
||||
|
||||
static void addtotemp(long type, long value)
|
||||
{
|
||||
while (ntempexpr >= tempexpr_size) {
|
||||
tempexpr_size += TEMPEXPR_DELTA;
|
||||
tempexpr = nasm_realloc(tempexpr,
|
||||
tempexpr_size*sizeof(*tempexpr));
|
||||
}
|
||||
tempexpr[ntempexpr].type = type;
|
||||
tempexpr[ntempexpr++].value = value;
|
||||
}
|
||||
|
||||
static expr *finishtemp(void)
|
||||
{
|
||||
addtotemp (0L, 0L); /* terminate */
|
||||
while (ntempexprs >= tempexprs_size) {
|
||||
tempexprs_size += TEMPEXPRS_DELTA;
|
||||
tempexprs = nasm_realloc(tempexprs,
|
||||
tempexprs_size*sizeof(*tempexprs));
|
||||
}
|
||||
return tempexprs[ntempexprs++] = tempexpr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add two vector datatypes. We have some bizarre behaviour on far-
|
||||
* absolute segment types: we preserve them during addition _only_
|
||||
* if one of the segments is a truly pure scalar.
|
||||
*/
|
||||
static expr *add_vectors(expr *p, expr *q)
|
||||
{
|
||||
int preserve;
|
||||
|
||||
preserve = is_really_simple(p) || is_really_simple(q);
|
||||
|
||||
begintemp();
|
||||
|
||||
while (p->type && q->type &&
|
||||
p->type < EXPR_SEGBASE+SEG_ABS &&
|
||||
q->type < EXPR_SEGBASE+SEG_ABS)
|
||||
{
|
||||
int lasttype;
|
||||
|
||||
if (p->type > q->type) {
|
||||
addtotemp(q->type, q->value);
|
||||
lasttype = q++->type;
|
||||
} else if (p->type < q->type) {
|
||||
addtotemp(p->type, p->value);
|
||||
lasttype = p++->type;
|
||||
} else { /* *p and *q have same type */
|
||||
long sum = p->value + q->value;
|
||||
if (sum)
|
||||
addtotemp(p->type, sum);
|
||||
lasttype = p->type;
|
||||
p++, q++;
|
||||
}
|
||||
if (lasttype == EXPR_UNKNOWN) {
|
||||
return finishtemp();
|
||||
}
|
||||
}
|
||||
while (p->type &&
|
||||
(preserve || p->type < EXPR_SEGBASE+SEG_ABS))
|
||||
{
|
||||
addtotemp(p->type, p->value);
|
||||
p++;
|
||||
}
|
||||
while (q->type &&
|
||||
(preserve || q->type < EXPR_SEGBASE+SEG_ABS))
|
||||
{
|
||||
addtotemp(q->type, q->value);
|
||||
q++;
|
||||
}
|
||||
|
||||
return finishtemp();
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiply a vector by a scalar. Strip far-absolute segment part
|
||||
* if present.
|
||||
*
|
||||
* Explicit treatment of UNKNOWN is not required in this routine,
|
||||
* since it will silently do the Right Thing anyway.
|
||||
*
|
||||
* If `affect_hints' is set, we also change the hint type to
|
||||
* NOTBASE if a MAKEBASE hint points at a register being
|
||||
* multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
|
||||
* as the base register.
|
||||
*/
|
||||
static expr *scalar_mult(expr *vect, long scalar, int affect_hints)
|
||||
{
|
||||
expr *p = vect;
|
||||
|
||||
while (p->type && p->type < EXPR_SEGBASE+SEG_ABS) {
|
||||
p->value = scalar * (p->value);
|
||||
if (hint && hint->type == EAH_MAKEBASE &&
|
||||
p->type == hint->base && affect_hints)
|
||||
hint->type = EAH_NOTBASE;
|
||||
p++;
|
||||
}
|
||||
p->type = 0;
|
||||
|
||||
return vect;
|
||||
}
|
||||
|
||||
static expr *scalarvect (long scalar)
|
||||
{
|
||||
begintemp();
|
||||
addtotemp(EXPR_SIMPLE, scalar);
|
||||
return finishtemp();
|
||||
}
|
||||
|
||||
static expr *unknown_expr (void)
|
||||
{
|
||||
begintemp();
|
||||
addtotemp(EXPR_UNKNOWN, 1L);
|
||||
return finishtemp();
|
||||
}
|
||||
|
||||
/*
|
||||
* The SEG operator: calculate the segment part of a relocatable
|
||||
* value. Return NULL, as usual, if an error occurs. Report the
|
||||
* error too.
|
||||
*/
|
||||
static expr *segment_part (expr *e)
|
||||
{
|
||||
long seg;
|
||||
|
||||
if (is_unknown(e))
|
||||
return unknown_expr();
|
||||
|
||||
if (!is_reloc(e)) {
|
||||
error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
seg = reloc_seg(e);
|
||||
if (seg == NO_SEG) {
|
||||
error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
|
||||
return NULL;
|
||||
} else if (seg & SEG_ABS) {
|
||||
return scalarvect(seg & ~SEG_ABS);
|
||||
} else if (seg & 1) {
|
||||
error(ERR_NONFATAL, "SEG applied to something which"
|
||||
" is already a segment base");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
long base = outfmt->segbase(seg+1);
|
||||
|
||||
begintemp();
|
||||
addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE+base), 1L);
|
||||
return finishtemp();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Recursive-descent parser. Called with a single boolean operand,
|
||||
* which is TRUE if the evaluation is critical (i.e. unresolved
|
||||
* symbols are an error condition). Must update the global `i' to
|
||||
* reflect the token after the parsed string. May return NULL.
|
||||
*
|
||||
* evaluate() should report its own errors: on return it is assumed
|
||||
* that if NULL has been returned, the error has already been
|
||||
* reported.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Grammar parsed is:
|
||||
*
|
||||
* expr : bexpr [ WRT expr6 ]
|
||||
* bexpr : rexp0 or expr0 depending on relative-mode setting
|
||||
* rexp0 : rexp1 [ {||} rexp1...]
|
||||
* rexp1 : rexp2 [ {^^} rexp2...]
|
||||
* rexp2 : rexp3 [ {&&} rexp3...]
|
||||
* rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
|
||||
* expr0 : expr1 [ {|} expr1...]
|
||||
* expr1 : expr2 [ {^} expr2...]
|
||||
* expr2 : expr3 [ {&} expr3...]
|
||||
* expr3 : expr4 [ {<<,>>} expr4...]
|
||||
* expr4 : expr5 [ {+,-} expr5...]
|
||||
* expr5 : expr6 [ {*,/,%,//,%%} expr6...]
|
||||
* expr6 : { ~,+,-,SEG } expr6
|
||||
* | (bexpr)
|
||||
* | symbol
|
||||
* | $
|
||||
* | number
|
||||
*/
|
||||
|
||||
static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
|
||||
|
||||
static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
|
||||
static expr *expr4(int), *expr5(int), *expr6(int);
|
||||
|
||||
static expr *(*bexpr)(int);
|
||||
|
||||
static expr *rexp0(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = rexp1(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_DBL_OR)
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = rexp1(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`|' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect ((long) (reloc_value(e) || reloc_value(f)));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *rexp1(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = rexp2(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_DBL_XOR)
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = rexp2(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`^' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect ((long) (!reloc_value(e) ^ !reloc_value(f)));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *rexp2(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = rexp3(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
while (i == TOKEN_DBL_AND)
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = rexp3(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`&' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect ((long) (reloc_value(e) && reloc_value(f)));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *rexp3(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
long v;
|
||||
|
||||
e = expr0(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
|
||||
i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE)
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr0(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
|
||||
e = add_vectors (e, scalar_mult(f, -1L, FALSE));
|
||||
|
||||
switch (j)
|
||||
{
|
||||
case TOKEN_EQ: case TOKEN_NE:
|
||||
if (is_unknown(e))
|
||||
v = -1; /* means unknown */
|
||||
else if (!is_really_simple(e) || reloc_value(e) != 0)
|
||||
v = (j == TOKEN_NE); /* unequal, so return TRUE if NE */
|
||||
else
|
||||
v = (j == TOKEN_EQ); /* equal, so return TRUE if EQ */
|
||||
break;
|
||||
default:
|
||||
if (is_unknown(e))
|
||||
v = -1; /* means unknown */
|
||||
else if (!is_really_simple(e)) {
|
||||
error(ERR_NONFATAL, "`%s': operands differ by a non-scalar",
|
||||
(j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" :
|
||||
j == TOKEN_GE ? ">=" : ">"));
|
||||
v = 0; /* must set it to _something_ */
|
||||
} else {
|
||||
int vv = reloc_value(e);
|
||||
if (vv == 0)
|
||||
v = (j == TOKEN_LE || j == TOKEN_GE);
|
||||
else if (vv > 0)
|
||||
v = (j == TOKEN_GE || j == TOKEN_GT);
|
||||
else /* vv < 0 */
|
||||
v = (j == TOKEN_LE || j == TOKEN_LT);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (v == -1)
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect(v);
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr0(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr1(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == '|')
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr1(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`|' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (reloc_value(e) | reloc_value(f));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr1(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr2(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == '^') {
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr2(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`^' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (reloc_value(e) ^ reloc_value(f));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr2(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr3(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == '&') {
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr3(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`&' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (reloc_value(e) & reloc_value(f));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr3(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr4(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_SHL || i == TOKEN_SHR)
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr4(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "shift operator may only be applied to"
|
||||
" scalar values");
|
||||
} else if (is_just_unknown(e) || is_just_unknown(f)) {
|
||||
e = unknown_expr();
|
||||
} else switch (j) {
|
||||
case TOKEN_SHL:
|
||||
e = scalarvect (reloc_value(e) << reloc_value(f));
|
||||
break;
|
||||
case TOKEN_SHR:
|
||||
e = scalarvect (((unsigned long)reloc_value(e)) >>
|
||||
reloc_value(f));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr4(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr5(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
while (i == '+' || i == '-')
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr5(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
switch (j) {
|
||||
case '+':
|
||||
e = add_vectors (e, f);
|
||||
break;
|
||||
case '-':
|
||||
e = add_vectors (e, scalar_mult(f, -1L, FALSE));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr5(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
while (i == '*' || i == '/' || i == '%' ||
|
||||
i == TOKEN_SDIV || i == TOKEN_SMOD)
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr6(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f))))
|
||||
{
|
||||
error(ERR_NONFATAL, "division operator may only be applied to"
|
||||
" scalar values");
|
||||
return NULL;
|
||||
}
|
||||
if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
|
||||
error(ERR_NONFATAL, "division by zero");
|
||||
return NULL;
|
||||
}
|
||||
switch (j) {
|
||||
case '*':
|
||||
if (is_simple(e))
|
||||
e = scalar_mult (f, reloc_value(e), TRUE);
|
||||
else if (is_simple(f))
|
||||
e = scalar_mult (e, reloc_value(f), TRUE);
|
||||
else if (is_just_unknown(e) && is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else {
|
||||
error(ERR_NONFATAL, "unable to multiply two "
|
||||
"non-scalar objects");
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
case '/':
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((unsigned long)reloc_value(e)) /
|
||||
((unsigned long)reloc_value(f)));
|
||||
break;
|
||||
case '%':
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((unsigned long)reloc_value(e)) %
|
||||
((unsigned long)reloc_value(f)));
|
||||
break;
|
||||
case TOKEN_SDIV:
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((signed long)reloc_value(e)) /
|
||||
((signed long)reloc_value(f)));
|
||||
break;
|
||||
case TOKEN_SMOD:
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((signed long)reloc_value(e)) %
|
||||
((signed long)reloc_value(f)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr6(int critical)
|
||||
{
|
||||
long type;
|
||||
expr *e;
|
||||
long label_seg, label_ofs;
|
||||
|
||||
if (i == '-') {
|
||||
i = scan(scpriv, tokval);
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
return scalar_mult (e, -1L, FALSE);
|
||||
} else if (i == '+') {
|
||||
i = scan(scpriv, tokval);
|
||||
return expr6(critical);
|
||||
} else if (i == '~') {
|
||||
i = scan(scpriv, tokval);
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
if (is_just_unknown(e))
|
||||
return unknown_expr();
|
||||
else if (!is_simple(e)) {
|
||||
error(ERR_NONFATAL, "`~' operator may only be applied to"
|
||||
" scalar values");
|
||||
return NULL;
|
||||
}
|
||||
return scalarvect(~reloc_value(e));
|
||||
} else if (i == TOKEN_SEG) {
|
||||
i = scan(scpriv, tokval);
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
e = segment_part(e);
|
||||
if (!e)
|
||||
return NULL;
|
||||
if (is_unknown(e) && critical) {
|
||||
error(ERR_NONFATAL, "unable to determine segment base");
|
||||
return NULL;
|
||||
}
|
||||
return e;
|
||||
} else if (i == '(') {
|
||||
i = scan(scpriv, tokval);
|
||||
e = bexpr(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
if (i != ')') {
|
||||
error(ERR_NONFATAL, "expecting `)'");
|
||||
return NULL;
|
||||
}
|
||||
i = scan(scpriv, tokval);
|
||||
return e;
|
||||
}
|
||||
else if (i == TOKEN_NUM || i == TOKEN_REG || i == TOKEN_ID ||
|
||||
i == TOKEN_HERE || i == TOKEN_BASE)
|
||||
{
|
||||
begintemp();
|
||||
switch (i) {
|
||||
case TOKEN_NUM:
|
||||
addtotemp(EXPR_SIMPLE, tokval->t_integer);
|
||||
break;
|
||||
case TOKEN_REG:
|
||||
addtotemp(tokval->t_integer, 1L);
|
||||
if (hint && hint->type == EAH_NOHINT)
|
||||
hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
|
||||
break;
|
||||
case TOKEN_ID:
|
||||
case TOKEN_HERE:
|
||||
case TOKEN_BASE:
|
||||
/*
|
||||
* If !location->known, this indicates that no
|
||||
* symbol, Here or Base references are valid because we
|
||||
* are in preprocess-only mode.
|
||||
*/
|
||||
if (!location->known) {
|
||||
error(ERR_NONFATAL,
|
||||
"%s not supported in preprocess-only mode",
|
||||
(i == TOKEN_ID ? "symbol references" :
|
||||
i == TOKEN_HERE ? "`$'" : "`$$'"));
|
||||
addtotemp(EXPR_UNKNOWN, 1L);
|
||||
break;
|
||||
}
|
||||
|
||||
type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
|
||||
if (i == TOKEN_BASE)
|
||||
{
|
||||
label_seg = in_abs_seg ? abs_seg : location->segment;
|
||||
label_ofs = 0;
|
||||
} else if (i == TOKEN_HERE) {
|
||||
label_seg = in_abs_seg ? abs_seg : location->segment;
|
||||
label_ofs = in_abs_seg ? abs_offset : location->offset;
|
||||
} else {
|
||||
if (!labelfunc(tokval->t_charptr,&label_seg,&label_ofs))
|
||||
{
|
||||
if (critical == 2) {
|
||||
error (ERR_NONFATAL, "symbol `%s' undefined",
|
||||
tokval->t_charptr);
|
||||
return NULL;
|
||||
} else if (critical == 1) {
|
||||
error (ERR_NONFATAL,
|
||||
"symbol `%s' not defined before use",
|
||||
tokval->t_charptr);
|
||||
return NULL;
|
||||
} else {
|
||||
if (opflags)
|
||||
*opflags |= 1;
|
||||
type = EXPR_UNKNOWN;
|
||||
label_seg = NO_SEG;
|
||||
label_ofs = 1;
|
||||
}
|
||||
}
|
||||
if (opflags && is_extern (tokval->t_charptr))
|
||||
*opflags |= OPFLAG_EXTERN;
|
||||
}
|
||||
addtotemp(type, label_ofs);
|
||||
if (label_seg!=NO_SEG)
|
||||
addtotemp(EXPR_SEGBASE + label_seg, 1L);
|
||||
break;
|
||||
}
|
||||
i = scan(scpriv, tokval);
|
||||
return finishtemp();
|
||||
} else {
|
||||
error(ERR_NONFATAL, "expression syntax error");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void eval_global_info (struct ofmt *output, lfunc lookup_label, loc_t *locp)
|
||||
{
|
||||
outfmt = output;
|
||||
labelfunc = lookup_label;
|
||||
location = locp;
|
||||
}
|
||||
|
||||
expr *evaluate (scanner sc, void *scprivate, struct tokenval *tv,
|
||||
int *fwref, int critical, efunc report_error,
|
||||
struct eval_hints *hints)
|
||||
{
|
||||
expr *e;
|
||||
expr *f = NULL;
|
||||
|
||||
hint = hints;
|
||||
if (hint)
|
||||
hint->type = EAH_NOHINT;
|
||||
|
||||
if (critical & CRITICAL) {
|
||||
critical &= ~CRITICAL;
|
||||
bexpr = rexp0;
|
||||
} else
|
||||
bexpr = expr0;
|
||||
|
||||
scan = sc;
|
||||
scpriv = scprivate;
|
||||
tokval = tv;
|
||||
error = report_error;
|
||||
opflags = fwref;
|
||||
|
||||
if (tokval->t_type == TOKEN_INVALID)
|
||||
i = scan(scpriv, tokval);
|
||||
else
|
||||
i = tokval->t_type;
|
||||
|
||||
while (ntempexprs) /* initialise temporary storage */
|
||||
nasm_free (tempexprs[--ntempexprs]);
|
||||
|
||||
e = bexpr (critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
if (i == TOKEN_WRT) {
|
||||
i = scan(scpriv, tokval); /* eat the WRT */
|
||||
f = expr6 (critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
}
|
||||
e = scalar_mult (e, 1L, FALSE); /* strip far-absolute segment part */
|
||||
if (f) {
|
||||
expr *g;
|
||||
if (is_just_unknown(f))
|
||||
g = unknown_expr();
|
||||
else {
|
||||
long value;
|
||||
begintemp();
|
||||
if (!is_reloc(f)) {
|
||||
error(ERR_NONFATAL, "invalid right-hand operand to WRT");
|
||||
return NULL;
|
||||
}
|
||||
value = reloc_seg(f);
|
||||
if (value == NO_SEG)
|
||||
value = reloc_value(f) | SEG_ABS;
|
||||
else if (!(value & SEG_ABS) && !(value % 2) && critical)
|
||||
{
|
||||
error(ERR_NONFATAL, "invalid right-hand operand to WRT");
|
||||
return NULL;
|
||||
}
|
||||
addtotemp(EXPR_WRT, value);
|
||||
g = finishtemp();
|
||||
}
|
||||
e = add_vectors (e, g);
|
||||
}
|
||||
return e;
|
||||
}
|
||||
28
modules/preprocs/nasm/nasm-eval.h
Normal file
28
modules/preprocs/nasm/nasm-eval.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* eval.h header file for eval.c
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*/
|
||||
|
||||
#ifndef NASM_EVAL_H
|
||||
#define NASM_EVAL_H
|
||||
|
||||
/*
|
||||
* Called once to tell the evaluator what output format is
|
||||
* providing segment-base details, and what function can be used to
|
||||
* look labels up.
|
||||
*/
|
||||
void eval_global_info (struct ofmt *output, lfunc lookup_label, loc_t *locp);
|
||||
|
||||
/*
|
||||
* The evaluator itself.
|
||||
*/
|
||||
expr *evaluate (scanner sc, void *scprivate, struct tokenval *tv,
|
||||
int *fwref, int critical, efunc report_error,
|
||||
struct eval_hints *hints);
|
||||
|
||||
void eval_cleanup(void);
|
||||
|
||||
#endif
|
||||
4459
modules/preprocs/nasm/nasm-pp.c
Normal file
4459
modules/preprocs/nasm/nasm-pp.c
Normal file
File diff suppressed because it is too large
Load diff
20
modules/preprocs/nasm/nasm-pp.h
Normal file
20
modules/preprocs/nasm/nasm-pp.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* preproc.h header file for preproc.c
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*/
|
||||
|
||||
#ifndef NASM_PREPROC_H
|
||||
#define NASM_PREPROC_H
|
||||
|
||||
void pp_include_path (char *);
|
||||
void pp_pre_include (char *);
|
||||
void pp_pre_define (char *);
|
||||
void pp_pre_undefine (char *);
|
||||
void pp_extra_stdmac (const char **);
|
||||
|
||||
extern Preproc nasmpp;
|
||||
|
||||
#endif
|
||||
850
modules/preprocs/nasm/nasm.h
Normal file
850
modules/preprocs/nasm/nasm.h
Normal file
|
|
@ -0,0 +1,850 @@
|
|||
/* nasm.h main header file for the Netwide Assembler: inter-module interface
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*
|
||||
* initial version: 27/iii/95 by Simon Tatham
|
||||
*/
|
||||
|
||||
#ifndef NASM_NASM_H
|
||||
#define NASM_NASM_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "version.h" /* generated NASM version macros */
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0 /* comes in handy */
|
||||
#endif
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#define NO_SEG -1L /* null segment value */
|
||||
#define SEG_ABS 0x40000000L /* mask for far-absolute segments */
|
||||
|
||||
#ifndef FILENAME_MAX
|
||||
#define FILENAME_MAX 256
|
||||
#endif
|
||||
|
||||
#ifndef PREFIX_MAX
|
||||
#define PREFIX_MAX 10
|
||||
#endif
|
||||
|
||||
#ifndef POSTFIX_MAX
|
||||
#define POSTFIX_MAX 10
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Name pollution problems: <time.h> on Digital UNIX pulls in some
|
||||
* strange hardware header file which sees fit to define R_SP. We
|
||||
* undefine it here so as not to break the enum below.
|
||||
*/
|
||||
#ifdef R_SP
|
||||
#undef R_SP
|
||||
#endif
|
||||
|
||||
/*
|
||||
* We must declare the existence of this structure type up here,
|
||||
* since we have to reference it before we define it...
|
||||
*/
|
||||
struct ofmt;
|
||||
|
||||
/*
|
||||
* -------------------------
|
||||
* Error reporting functions
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* An error reporting function should look like this.
|
||||
*/
|
||||
typedef void (*efunc) (int severity, const char *fmt, ...);
|
||||
|
||||
/*
|
||||
* These are the error severity codes which get passed as the first
|
||||
* argument to an efunc.
|
||||
*/
|
||||
|
||||
#define ERR_DEBUG 0x00000008 /* put out debugging message */
|
||||
#define ERR_WARNING 0x00000000 /* warn only: no further action */
|
||||
#define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
|
||||
#define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
|
||||
#define ERR_PANIC 0x00000003 /* internal error: panic instantly
|
||||
* and dump core for reference */
|
||||
#define ERR_MASK 0x0000000F /* mask off the above codes */
|
||||
#define ERR_NOFILE 0x00000010 /* don't give source file name/line */
|
||||
#define ERR_USAGE 0x00000020 /* print a usage message */
|
||||
#define ERR_PASS1 0x00000040 /* only print this error on pass one */
|
||||
|
||||
/*
|
||||
* These codes define specific types of suppressible warning.
|
||||
*/
|
||||
|
||||
#define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */
|
||||
#define ERR_WARN_SHR 8 /* how far to shift right */
|
||||
|
||||
#define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */
|
||||
#define ERR_WARN_MSR 0x00000200 /* macro self-reference */
|
||||
#define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and
|
||||
* alone on line) */
|
||||
#define ERR_WARN_NOV 0x00000400 /* numeric overflow */
|
||||
#define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */
|
||||
#define ERR_WARN_MAX 5 /* the highest numbered one */
|
||||
|
||||
/*
|
||||
* -----------------------
|
||||
* Other function typedefs
|
||||
* -----------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* A label-lookup function should look like this.
|
||||
*/
|
||||
typedef int (*lfunc) (char *label, long *segment, long *offset);
|
||||
|
||||
/*
|
||||
* And a label-definition function like this. The boolean parameter
|
||||
* `is_norm' states whether the label is a `normal' label (which
|
||||
* should affect the local-label system), or something odder like
|
||||
* an EQU or a segment-base symbol, which shouldn't.
|
||||
*/
|
||||
typedef void (*ldfunc) (char *label, long segment, long offset, char *special,
|
||||
int is_norm, int isextrn, struct ofmt *ofmt,
|
||||
efunc error);
|
||||
|
||||
/*
|
||||
* List-file generators should look like this:
|
||||
*/
|
||||
typedef struct {
|
||||
/*
|
||||
* Called to initialise the listing file generator. Before this
|
||||
* is called, the other routines will silently do nothing when
|
||||
* called. The `char *' parameter is the file name to write the
|
||||
* listing to.
|
||||
*/
|
||||
void (*init) (char *, efunc);
|
||||
|
||||
/*
|
||||
* Called to clear stuff up and close the listing file.
|
||||
*/
|
||||
void (*cleanup) (void);
|
||||
|
||||
/*
|
||||
* Called to output binary data. Parameters are: the offset;
|
||||
* the data; the data type. Data types are similar to the
|
||||
* output-format interface, only OUT_ADDRESS will _always_ be
|
||||
* displayed as if it's relocatable, so ensure that any non-
|
||||
* relocatable address has been converted to OUT_RAWDATA by
|
||||
* then. Note that OUT_RAWDATA+0 is a valid data type, and is a
|
||||
* dummy call used to give the listing generator an offset to
|
||||
* work with when doing things like uplevel(LIST_TIMES) or
|
||||
* uplevel(LIST_INCBIN).
|
||||
*/
|
||||
void (*output) (long, const void *, unsigned long);
|
||||
|
||||
/*
|
||||
* Called to send a text line to the listing generator. The
|
||||
* `int' parameter is LIST_READ or LIST_MACRO depending on
|
||||
* whether the line came directly from an input file or is the
|
||||
* result of a multi-line macro expansion.
|
||||
*/
|
||||
void (*line) (int, char *);
|
||||
|
||||
/*
|
||||
* Called to change one of the various levelled mechanisms in
|
||||
* the listing generator. LIST_INCLUDE and LIST_MACRO can be
|
||||
* used to increase the nesting level of include files and
|
||||
* macro expansions; LIST_TIMES and LIST_INCBIN switch on the
|
||||
* two binary-output-suppression mechanisms for large-scale
|
||||
* pseudo-instructions.
|
||||
*
|
||||
* LIST_MACRO_NOLIST is synonymous with LIST_MACRO except that
|
||||
* it indicates the beginning of the expansion of a `nolist'
|
||||
* macro, so anything under that level won't be expanded unless
|
||||
* it includes another file.
|
||||
*/
|
||||
void (*uplevel) (int);
|
||||
|
||||
/*
|
||||
* Reverse the effects of uplevel.
|
||||
*/
|
||||
void (*downlevel) (int);
|
||||
} ListGen;
|
||||
|
||||
/*
|
||||
* The expression evaluator must be passed a scanner function; a
|
||||
* standard scanner is provided as part of nasmlib.c. The
|
||||
* preprocessor will use a different one. Scanners, and the
|
||||
* token-value structures they return, look like this.
|
||||
*
|
||||
* The return value from the scanner is always a copy of the
|
||||
* `t_type' field in the structure.
|
||||
*/
|
||||
struct tokenval {
|
||||
int t_type;
|
||||
long t_integer, t_inttwo;
|
||||
char *t_charptr;
|
||||
};
|
||||
typedef int (*scanner) (void *private_data, struct tokenval *tv);
|
||||
|
||||
/*
|
||||
* Token types returned by the scanner, in addition to ordinary
|
||||
* ASCII character values, and zero for end-of-string.
|
||||
*/
|
||||
enum { /* token types, other than chars */
|
||||
TOKEN_INVALID = -1, /* a placeholder value */
|
||||
TOKEN_EOS = 0, /* end of string */
|
||||
TOKEN_EQ = '=', TOKEN_GT = '>', TOKEN_LT = '<', /* aliases */
|
||||
TOKEN_ID = 256, TOKEN_NUM, TOKEN_REG, TOKEN_INSN, /* major token types */
|
||||
TOKEN_ERRNUM, /* numeric constant with error in */
|
||||
TOKEN_HERE, TOKEN_BASE, /* $ and $$ */
|
||||
TOKEN_SPECIAL, /* BYTE, WORD, DWORD, FAR, NEAR, etc */
|
||||
TOKEN_PREFIX, /* A32, O16, LOCK, REPNZ, TIMES, etc */
|
||||
TOKEN_SHL, TOKEN_SHR, /* << and >> */
|
||||
TOKEN_SDIV, TOKEN_SMOD, /* // and %% */
|
||||
TOKEN_GE, TOKEN_LE, TOKEN_NE, /* >=, <= and <> (!= is same as <>) */
|
||||
TOKEN_DBL_AND, TOKEN_DBL_OR, TOKEN_DBL_XOR, /* &&, || and ^^ */
|
||||
TOKEN_SEG, TOKEN_WRT, /* SEG and WRT */
|
||||
TOKEN_FLOAT /* floating-point constant */
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
long segment;
|
||||
long offset;
|
||||
int known;
|
||||
} loc_t;
|
||||
|
||||
/*
|
||||
* Expression-evaluator datatype. Expressions, within the
|
||||
* evaluator, are stored as an array of these beasts, terminated by
|
||||
* a record with type==0. Mostly, it's a vector type: each type
|
||||
* denotes some kind of a component, and the value denotes the
|
||||
* multiple of that component present in the expression. The
|
||||
* exception is the WRT type, whose `value' field denotes the
|
||||
* segment to which the expression is relative. These segments will
|
||||
* be segment-base types, i.e. either odd segment values or SEG_ABS
|
||||
* types. So it is still valid to assume that anything with a
|
||||
* `value' field of zero is insignificant.
|
||||
*/
|
||||
typedef struct {
|
||||
long type; /* a register, or EXPR_xxx */
|
||||
long value; /* must be >= 32 bits */
|
||||
} expr;
|
||||
|
||||
/*
|
||||
* The evaluator can also return hints about which of two registers
|
||||
* used in an expression should be the base register. See also the
|
||||
* `operand' structure.
|
||||
*/
|
||||
struct eval_hints {
|
||||
int base;
|
||||
int type;
|
||||
};
|
||||
|
||||
/*
|
||||
* The actual expression evaluator function looks like this. When
|
||||
* called, it expects the first token of its expression to already
|
||||
* be in `*tv'; if it is not, set tv->t_type to TOKEN_INVALID and
|
||||
* it will start by calling the scanner.
|
||||
*
|
||||
* If a forward reference happens during evaluation, the evaluator
|
||||
* must set `*fwref' to TRUE if `fwref' is non-NULL.
|
||||
*
|
||||
* `critical' is non-zero if the expression may not contain forward
|
||||
* references. The evaluator will report its own error if this
|
||||
* occurs; if `critical' is 1, the error will be "symbol not
|
||||
* defined before use", whereas if `critical' is 2, the error will
|
||||
* be "symbol undefined".
|
||||
*
|
||||
* If `critical' has bit 8 set (in addition to its main value: 0x101
|
||||
* and 0x102 correspond to 1 and 2) then an extended expression
|
||||
* syntax is recognised, in which relational operators such as =, <
|
||||
* and >= are accepted, as well as low-precedence logical operators
|
||||
* &&, ^^ and ||.
|
||||
*
|
||||
* If `hints' is non-NULL, it gets filled in with some hints as to
|
||||
* the base register in complex effective addresses.
|
||||
*/
|
||||
#define CRITICAL 0x100
|
||||
typedef expr *(*evalfunc) (scanner sc, void *scprivate, struct tokenval *tv,
|
||||
int *fwref, int critical, efunc error,
|
||||
struct eval_hints *hints);
|
||||
|
||||
/*
|
||||
* Special values for expr->type. ASSUMPTION MADE HERE: the number
|
||||
* of distinct register names (i.e. possible "type" fields for an
|
||||
* expr structure) does not exceed 124 (EXPR_REG_START through
|
||||
* EXPR_REG_END).
|
||||
*/
|
||||
#define EXPR_REG_START 1
|
||||
#define EXPR_REG_END 124
|
||||
#define EXPR_UNKNOWN 125L /* for forward references */
|
||||
#define EXPR_SIMPLE 126L
|
||||
#define EXPR_WRT 127L
|
||||
#define EXPR_SEGBASE 128L
|
||||
|
||||
/*
|
||||
* Preprocessors ought to look like this:
|
||||
*/
|
||||
typedef struct {
|
||||
/*
|
||||
* Called at the start of a pass; given a file name, the number
|
||||
* of the pass, an error reporting function, an evaluator
|
||||
* function, and a listing generator to talk to.
|
||||
*/
|
||||
void (*reset) (char *, int, efunc, evalfunc, ListGen *);
|
||||
|
||||
/*
|
||||
* Called to fetch a line of preprocessed source. The line
|
||||
* returned has been malloc'ed, and so should be freed after
|
||||
* use.
|
||||
*/
|
||||
char *(*getline) (void);
|
||||
|
||||
/*
|
||||
* Called at the end of a pass.
|
||||
*/
|
||||
void (*cleanup) (int);
|
||||
} Preproc;
|
||||
|
||||
/*
|
||||
* ----------------------------------------------------------------
|
||||
* Some lexical properties of the NASM source language, included
|
||||
* here because they are shared between the parser and preprocessor
|
||||
* ----------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* isidstart matches any character that may start an identifier, and isidchar
|
||||
* matches any character that may appear at places other than the start of an
|
||||
* identifier. E.g. a period may only appear at the start of an identifier
|
||||
* (for local labels), whereas a number may appear anywhere *but* at the
|
||||
* start.
|
||||
*/
|
||||
|
||||
#define isidstart(c) ( isalpha(c) || (c)=='_' || (c)=='.' || (c)=='?' \
|
||||
|| (c)=='@' )
|
||||
#define isidchar(c) ( isidstart(c) || isdigit(c) || (c)=='$' || (c)=='#' \
|
||||
|| (c)=='~' )
|
||||
|
||||
/* Ditto for numeric constants. */
|
||||
|
||||
#define isnumstart(c) ( isdigit(c) || (c)=='$' )
|
||||
#define isnumchar(c) ( isalnum(c) )
|
||||
|
||||
/* This returns the numeric value of a given 'digit'. */
|
||||
|
||||
#define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
|
||||
|
||||
/*
|
||||
* Data-type flags that get passed to listing-file routines.
|
||||
*/
|
||||
enum {
|
||||
LIST_READ, LIST_MACRO, LIST_MACRO_NOLIST, LIST_INCLUDE,
|
||||
LIST_INCBIN, LIST_TIMES
|
||||
};
|
||||
|
||||
/*
|
||||
* -----------------------------------------------------------
|
||||
* Format of the `insn' structure returned from `parser.c' and
|
||||
* passed into `assemble.c'
|
||||
* -----------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* Here we define the operand types. These are implemented as bit
|
||||
* masks, since some are subsets of others; e.g. AX in a MOV
|
||||
* instruction is a special operand type, whereas AX in other
|
||||
* contexts is just another 16-bit register. (Also, consider CL in
|
||||
* shift instructions, DX in OUT, etc.)
|
||||
*/
|
||||
|
||||
/* size, and other attributes, of the operand */
|
||||
#define BITS8 0x00000001L
|
||||
#define BITS16 0x00000002L
|
||||
#define BITS32 0x00000004L
|
||||
#define BITS64 0x00000008L /* FPU only */
|
||||
#define BITS80 0x00000010L /* FPU only */
|
||||
#define FAR 0x00000020L /* grotty: this means 16:16 or */
|
||||
/* 16:32, like in CALL/JMP */
|
||||
#define NEAR 0x00000040L
|
||||
#define SHORT 0x00000080L /* and this means what it says :) */
|
||||
|
||||
#define SIZE_MASK 0x000000FFL /* all the size attributes */
|
||||
#define NON_SIZE (~SIZE_MASK)
|
||||
|
||||
#define TO 0x00000100L /* reverse effect in FADD, FSUB &c */
|
||||
#define COLON 0x00000200L /* operand is followed by a colon */
|
||||
#define STRICT 0x00000400L /* do not optimize this operand */
|
||||
|
||||
/* type of operand: memory reference, register, etc. */
|
||||
#define MEMORY 0x00204000L
|
||||
#define REGISTER 0x00001000L /* register number in 'basereg' */
|
||||
#define IMMEDIATE 0x00002000L
|
||||
|
||||
#define REGMEM 0x00200000L /* for r/m, ie EA, operands */
|
||||
#define REGNORM 0x00201000L /* 'normal' reg, qualifies as EA */
|
||||
#define REG8 0x00201001L
|
||||
#define REG16 0x00201002L
|
||||
#define REG32 0x00201004L
|
||||
#define MMXREG 0x00201008L /* MMX registers */
|
||||
#define XMMREG 0x00201010L /* XMM Katmai reg */
|
||||
#define FPUREG 0x01000000L /* floating point stack registers */
|
||||
#define FPU0 0x01000800L /* FPU stack register zero */
|
||||
|
||||
/* special register operands: these may be treated differently */
|
||||
#define REG_SMASK 0x00070000L /* a mask for the following */
|
||||
#define REG_ACCUM 0x00211000L /* accumulator: AL, AX or EAX */
|
||||
#define REG_AL 0x00211001L /* REG_ACCUM | BITSxx */
|
||||
#define REG_AX 0x00211002L /* ditto */
|
||||
#define REG_EAX 0x00211004L /* and again */
|
||||
#define REG_COUNT 0x00221000L /* counter: CL, CX or ECX */
|
||||
#define REG_CL 0x00221001L /* REG_COUNT | BITSxx */
|
||||
#define REG_CX 0x00221002L /* ditto */
|
||||
#define REG_ECX 0x00221004L /* another one */
|
||||
#define REG_DX 0x00241002L
|
||||
#define REG_SREG 0x00081002L /* any segment register */
|
||||
#define REG_CS 0x01081002L /* CS */
|
||||
#define REG_DESS 0x02081002L /* DS, ES, SS (non-CS 86 registers) */
|
||||
#define REG_FSGS 0x04081002L /* FS, GS (386 extended registers) */
|
||||
#define REG_SEG67 0x08081002L /* Non-implemented segment registers */
|
||||
#define REG_CDT 0x00101004L /* CRn, DRn and TRn */
|
||||
#define REG_CREG 0x08101004L /* CRn */
|
||||
#define REG_DREG 0x10101004L /* DRn */
|
||||
#define REG_TREG 0x20101004L /* TRn */
|
||||
|
||||
/* special type of EA */
|
||||
#define MEM_OFFS 0x00604000L /* simple [address] offset */
|
||||
|
||||
/* special type of immediate operand */
|
||||
#define ONENESS 0x00800000L /* so UNITY == IMMEDIATE | ONENESS */
|
||||
#define UNITY 0x00802000L /* for shift/rotate instructions */
|
||||
#define BYTENESS 0x40000000L /* so SBYTE == IMMEDIATE | BYTENESS */
|
||||
#define SBYTE 0x40002000L /* for op r16/32,immediate instrs. */
|
||||
|
||||
/* Register names automatically generated from regs.dat */
|
||||
#include "regs.h"
|
||||
|
||||
enum { /* condition code names */
|
||||
C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
|
||||
C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
|
||||
C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z
|
||||
};
|
||||
|
||||
/*
|
||||
* Note that because segment registers may be used as instruction
|
||||
* prefixes, we must ensure the enumerations for prefixes and
|
||||
* register names do not overlap.
|
||||
*/
|
||||
enum { /* instruction prefixes */
|
||||
PREFIX_ENUM_START = REG_ENUM_LIMIT,
|
||||
P_A16 = PREFIX_ENUM_START, P_A32, P_LOCK, P_O16, P_O32, P_REP, P_REPE,
|
||||
P_REPNE, P_REPNZ, P_REPZ, P_TIMES
|
||||
};
|
||||
|
||||
enum { /* extended operand types */
|
||||
EOT_NOTHING, EOT_DB_STRING, EOT_DB_NUMBER
|
||||
};
|
||||
|
||||
enum { /* special EA flags */
|
||||
EAF_BYTEOFFS = 1, /* force offset part to byte size */
|
||||
EAF_WORDOFFS = 2, /* force offset part to [d]word size */
|
||||
EAF_TIMESTWO = 4 /* really do EAX*2 not EAX+EAX */
|
||||
};
|
||||
|
||||
enum { /* values for `hinttype' */
|
||||
EAH_NOHINT = 0, /* no hint at all - our discretion */
|
||||
EAH_MAKEBASE = 1, /* try to make given reg the base */
|
||||
EAH_NOTBASE = 2 /* try _not_ to make reg the base */
|
||||
};
|
||||
|
||||
typedef struct { /* operand to an instruction */
|
||||
long type; /* type of operand */
|
||||
int addr_size; /* 0 means default; 16; 32 */
|
||||
int basereg, indexreg, scale; /* registers and scale involved */
|
||||
int hintbase, hinttype; /* hint as to real base register */
|
||||
long segment; /* immediate segment, if needed */
|
||||
long offset; /* any immediate number */
|
||||
long wrt; /* segment base it's relative to */
|
||||
int eaflags; /* special EA flags */
|
||||
int opflags; /* see OPFLAG_* defines below */
|
||||
} operand;
|
||||
|
||||
#define OPFLAG_FORWARD 1 /* operand is a forward reference */
|
||||
#define OPFLAG_EXTERN 2 /* operand is an external reference */
|
||||
|
||||
typedef struct extop { /* extended operand */
|
||||
struct extop *next; /* linked list */
|
||||
long type; /* defined above */
|
||||
char *stringval; /* if it's a string, then here it is */
|
||||
int stringlen; /* ... and here's how long it is */
|
||||
long segment; /* if it's a number/address, then... */
|
||||
long offset; /* ... it's given here ... */
|
||||
long wrt; /* ... and here */
|
||||
} extop;
|
||||
|
||||
#define MAXPREFIX 4
|
||||
|
||||
typedef struct { /* an instruction itself */
|
||||
char *label; /* the label defined, or NULL */
|
||||
int prefixes[MAXPREFIX]; /* instruction prefixes, if any */
|
||||
int nprefix; /* number of entries in above */
|
||||
int opcode; /* the opcode - not just the string */
|
||||
int condition; /* the condition code, if Jcc/SETcc */
|
||||
int operands; /* how many operands? 0-3
|
||||
* (more if db et al) */
|
||||
operand oprs[3]; /* the operands, defined as above */
|
||||
extop *eops; /* extended operands */
|
||||
int eops_float; /* true if DD and floating */
|
||||
long times; /* repeat count (TIMES prefix) */
|
||||
int forw_ref; /* is there a forward reference? */
|
||||
} insn;
|
||||
|
||||
enum geninfo { GI_SWITCH };
|
||||
/*
|
||||
* ------------------------------------------------------------
|
||||
* The data structure defining an output format driver, and the
|
||||
* interfaces to the functions therein.
|
||||
* ------------------------------------------------------------
|
||||
*/
|
||||
|
||||
struct ofmt {
|
||||
/*
|
||||
* This is a short (one-liner) description of the type of
|
||||
* output generated by the driver.
|
||||
*/
|
||||
const char *fullname;
|
||||
|
||||
/*
|
||||
* This is a single keyword used to select the driver.
|
||||
*/
|
||||
const char *shortname;
|
||||
|
||||
/*
|
||||
* this is reserved for out module specific help.
|
||||
* It is set to NULL in all the out modules but is not implemented
|
||||
* in the main program
|
||||
*/
|
||||
const char *helpstring;
|
||||
|
||||
/*
|
||||
* this is a pointer to the first element of the debug information
|
||||
*/
|
||||
struct dfmt **debug_formats;
|
||||
|
||||
/*
|
||||
* and a pointer to the element that is being used
|
||||
* note: this is set to the default at compile time and changed if the
|
||||
* -F option is selected. If developing a set of new debug formats for
|
||||
* an output format, be sure to set this to whatever default you want
|
||||
*
|
||||
*/
|
||||
struct dfmt *current_dfmt;
|
||||
|
||||
/*
|
||||
* This, if non-NULL, is a NULL-terminated list of `char *'s
|
||||
* pointing to extra standard macros supplied by the object
|
||||
* format (e.g. a sensible initial default value of __SECT__,
|
||||
* and user-level equivalents for any format-specific
|
||||
* directives).
|
||||
*/
|
||||
const char **stdmac;
|
||||
|
||||
/*
|
||||
* This procedure is called at the start of an output session.
|
||||
* It tells the output format what file it will be writing to,
|
||||
* what routine to report errors through, and how to interface
|
||||
* to the label manager and expression evaluator if necessary.
|
||||
* It also gives it a chance to do other initialisation.
|
||||
*/
|
||||
void (*init) (FILE *fp, efunc error, ldfunc ldef, evalfunc eval);
|
||||
|
||||
/*
|
||||
* This procedure is called to pass generic information to the
|
||||
* object file. The first parameter gives the information type
|
||||
* (currently only command line switches)
|
||||
* and the second parameter gives the value. This function returns
|
||||
* 1 if recognized, 0 if unrecognized
|
||||
*/
|
||||
int (*setinfo)(enum geninfo type, char **string);
|
||||
|
||||
/*
|
||||
* This procedure is called by assemble() to write actual
|
||||
* generated code or data to the object file. Typically it
|
||||
* doesn't have to actually _write_ it, just store it for
|
||||
* later.
|
||||
*
|
||||
* The `type' argument specifies the type of output data, and
|
||||
* usually the size as well: its contents are described below.
|
||||
*/
|
||||
void (*output) (long segto, const void *data, unsigned long type,
|
||||
long segment, long wrt);
|
||||
|
||||
/*
|
||||
* This procedure is called once for every symbol defined in
|
||||
* the module being assembled. It gives the name and value of
|
||||
* the symbol, in NASM's terms, and indicates whether it has
|
||||
* been declared to be global. Note that the parameter "name",
|
||||
* when passed, will point to a piece of static storage
|
||||
* allocated inside the label manager - it's safe to keep using
|
||||
* that pointer, because the label manager doesn't clean up
|
||||
* until after the output driver has.
|
||||
*
|
||||
* Values of `is_global' are: 0 means the symbol is local; 1
|
||||
* means the symbol is global; 2 means the symbol is common (in
|
||||
* which case `offset' holds the _size_ of the variable).
|
||||
* Anything else is available for the output driver to use
|
||||
* internally.
|
||||
*
|
||||
* This routine explicitly _is_ allowed to call the label
|
||||
* manager to define further symbols, if it wants to, even
|
||||
* though it's been called _from_ the label manager. That much
|
||||
* re-entrancy is guaranteed in the label manager. However, the
|
||||
* label manager will in turn call this routine, so it should
|
||||
* be prepared to be re-entrant itself.
|
||||
*
|
||||
* The `special' parameter contains special information passed
|
||||
* through from the command that defined the label: it may have
|
||||
* been an EXTERN, a COMMON or a GLOBAL. The distinction should
|
||||
* be obvious to the output format from the other parameters.
|
||||
*/
|
||||
void (*symdef) (char *name, long segment, long offset, int is_global,
|
||||
char *special);
|
||||
|
||||
/*
|
||||
* This procedure is called when the source code requests a
|
||||
* segment change. It should return the corresponding segment
|
||||
* _number_ for the name, or NO_SEG if the name is not a valid
|
||||
* segment name.
|
||||
*
|
||||
* It may also be called with NULL, in which case it is to
|
||||
* return the _default_ section number for starting assembly in.
|
||||
*
|
||||
* It is allowed to modify the string it is given a pointer to.
|
||||
*
|
||||
* It is also allowed to specify a default instruction size for
|
||||
* the segment, by setting `*bits' to 16 or 32. Or, if it
|
||||
* doesn't wish to define a default, it can leave `bits' alone.
|
||||
*/
|
||||
long (*section) (char *name, int pass, int *bits);
|
||||
|
||||
/*
|
||||
* This procedure is called to modify the segment base values
|
||||
* returned from the SEG operator. It is given a segment base
|
||||
* value (i.e. a segment value with the low bit set), and is
|
||||
* required to produce in return a segment value which may be
|
||||
* different. It can map segment bases to absolute numbers by
|
||||
* means of returning SEG_ABS types.
|
||||
*
|
||||
* It should return NO_SEG if the segment base cannot be
|
||||
* determined; the evaluator (which calls this routine) is
|
||||
* responsible for throwing an error condition if that occurs
|
||||
* in pass two or in a critical expression.
|
||||
*/
|
||||
long (*segbase) (long segment);
|
||||
|
||||
/*
|
||||
* This procedure is called to allow the output driver to
|
||||
* process its own specific directives. When called, it has the
|
||||
* directive word in `directive' and the parameter string in
|
||||
* `value'. It is called in both assembly passes, and `pass'
|
||||
* will be either 1 or 2.
|
||||
*
|
||||
* This procedure should return zero if it does not _recognise_
|
||||
* the directive, so that the main program can report an error.
|
||||
* If it recognises the directive but then has its own errors,
|
||||
* it should report them itself and then return non-zero. It
|
||||
* should also return non-zero if it correctly processes the
|
||||
* directive.
|
||||
*/
|
||||
int (*directive) (char *directive, char *value, int pass);
|
||||
|
||||
/*
|
||||
* This procedure is called before anything else - even before
|
||||
* the "init" routine - and is passed the name of the input
|
||||
* file from which this output file is being generated. It
|
||||
* should return its preferred name for the output file in
|
||||
* `outname', if outname[0] is not '\0', and do nothing to
|
||||
* `outname' otherwise. Since it is called before the driver is
|
||||
* properly initialised, it has to be passed its error handler
|
||||
* separately.
|
||||
*
|
||||
* This procedure may also take its own copy of the input file
|
||||
* name for use in writing the output file: it is _guaranteed_
|
||||
* that it will be called before the "init" routine.
|
||||
*
|
||||
* The parameter `outname' points to an area of storage
|
||||
* guaranteed to be at least FILENAME_MAX in size.
|
||||
*/
|
||||
void (*filename) (char *inname, char *outname, efunc error);
|
||||
|
||||
/*
|
||||
* This procedure is called after assembly finishes, to allow
|
||||
* the output driver to clean itself up and free its memory.
|
||||
* Typically, it will also be the point at which the object
|
||||
* file actually gets _written_.
|
||||
*
|
||||
* One thing the cleanup routine should always do is to close
|
||||
* the output file pointer.
|
||||
*/
|
||||
void (*cleanup) (int debuginfo);
|
||||
};
|
||||
|
||||
/*
|
||||
* values for the `type' parameter to an output function. Each one
|
||||
* must have the actual number of _bytes_ added to it.
|
||||
*
|
||||
* Exceptions are OUT_RELxADR, which denote an x-byte relocation
|
||||
* which will be a relative jump. For this we need to know the
|
||||
* distance in bytes from the start of the relocated record until
|
||||
* the end of the containing instruction. _This_ is what is stored
|
||||
* in the size part of the parameter, in this case.
|
||||
*
|
||||
* Also OUT_RESERVE denotes reservation of N bytes of BSS space,
|
||||
* and the contents of the "data" parameter is irrelevant.
|
||||
*
|
||||
* The "data" parameter for the output function points to a "long",
|
||||
* containing the address in question, unless the type is
|
||||
* OUT_RAWDATA, in which case it points to an "unsigned char"
|
||||
* array.
|
||||
*/
|
||||
#define OUT_RAWDATA 0x00000000UL
|
||||
#define OUT_ADDRESS 0x10000000UL
|
||||
#define OUT_REL2ADR 0x20000000UL
|
||||
#define OUT_REL4ADR 0x30000000UL
|
||||
#define OUT_RESERVE 0x40000000UL
|
||||
#define OUT_TYPMASK 0xF0000000UL
|
||||
#define OUT_SIZMASK 0x0FFFFFFFUL
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------------
|
||||
* The data structure defining a debug format driver, and the
|
||||
* interfaces to the functions therein.
|
||||
* ------------------------------------------------------------
|
||||
*/
|
||||
|
||||
struct dfmt {
|
||||
|
||||
/*
|
||||
* This is a short (one-liner) description of the type of
|
||||
* output generated by the driver.
|
||||
*/
|
||||
const char *fullname;
|
||||
|
||||
/*
|
||||
* This is a single keyword used to select the driver.
|
||||
*/
|
||||
const char *shortname;
|
||||
|
||||
|
||||
/*
|
||||
* init - called initially to set up local pointer to object format,
|
||||
* void pointer to implementation defined data, file pointer (which
|
||||
* probably won't be used, but who knows?), and error function.
|
||||
*/
|
||||
void (*init) (struct ofmt * of, void * id, FILE * fp, efunc error);
|
||||
|
||||
/*
|
||||
* linenum - called any time there is output with a change of
|
||||
* line number or file.
|
||||
*/
|
||||
void (*linenum) (const char * filename, long linenumber, long segto);
|
||||
|
||||
/*
|
||||
* debug_deflabel - called whenever a label is defined. Parameters
|
||||
* are the same as to 'symdef()' in the output format. This function
|
||||
* would be called before the output format version.
|
||||
*/
|
||||
|
||||
void (*debug_deflabel) (char * name, long segment, long offset,
|
||||
int is_global, char * special);
|
||||
/*
|
||||
* debug_directive - called whenever a DEBUG directive other than 'LINE'
|
||||
* is encountered. 'directive' contains the first parameter to the
|
||||
* DEBUG directive, and params contains the rest. For example,
|
||||
* 'DEBUG VAR _somevar:int' would translate to a call to this
|
||||
* function with 'directive' equal to "VAR" and 'params' equal to
|
||||
* "_somevar:int".
|
||||
*/
|
||||
void (*debug_directive) (const char * directive, const char * params);
|
||||
|
||||
/*
|
||||
* typevalue - called whenever the assembler wishes to register a type
|
||||
* for the last defined label. This routine MUST detect if a type was
|
||||
* already registered and not re-register it.
|
||||
*/
|
||||
void (*debug_typevalue) (long type);
|
||||
|
||||
/*
|
||||
* debug_output - called whenever output is required
|
||||
* 'type' is the type of info required, and this is format-specific
|
||||
*/
|
||||
void (*debug_output) (int type, void *param);
|
||||
|
||||
/*
|
||||
* cleanup - called after processing of file is complete
|
||||
*/
|
||||
void (*cleanup) (void);
|
||||
|
||||
};
|
||||
/*
|
||||
* The type definition macros
|
||||
* for debugging
|
||||
*
|
||||
* low 3 bits: reserved
|
||||
* next 5 bits: type
|
||||
* next 24 bits: number of elements for arrays (0 for labels)
|
||||
*/
|
||||
|
||||
#define TY_UNKNOWN 0x00
|
||||
#define TY_LABEL 0x08
|
||||
#define TY_BYTE 0x10
|
||||
#define TY_WORD 0x18
|
||||
#define TY_DWORD 0x20
|
||||
#define TY_FLOAT 0x28
|
||||
#define TY_QWORD 0x30
|
||||
#define TY_TBYTE 0x38
|
||||
#define TY_COMMON 0xE0
|
||||
#define TY_SEG 0xE8
|
||||
#define TY_EXTERN 0xF0
|
||||
#define TY_EQU 0xF8
|
||||
|
||||
#define TYM_TYPE(x) ((x) & 0xF8)
|
||||
#define TYM_ELEMENTS(x) (((x) & 0xFFFFFF00) >> 8)
|
||||
|
||||
#define TYS_ELEMENTS(x) ((x) << 8)
|
||||
/*
|
||||
* -----
|
||||
* Other
|
||||
* -----
|
||||
*/
|
||||
|
||||
/*
|
||||
* This is a useful #define which I keep meaning to use more often:
|
||||
* the number of elements of a statically defined array.
|
||||
*/
|
||||
|
||||
#define elements(x) ( sizeof(x) / sizeof(*(x)) )
|
||||
|
||||
extern int tasm_compatible_mode;
|
||||
|
||||
/*
|
||||
* This declaration passes the "pass" number to all other modules
|
||||
* "pass0" assumes the values: 0, 0, ..., 0, 1, 2
|
||||
* where 0 = optimizing pass
|
||||
* 1 = pass 1
|
||||
* 2 = pass 2
|
||||
*/
|
||||
|
||||
extern int pass0; /* this is globally known */
|
||||
extern int optimizing;
|
||||
|
||||
#endif
|
||||
1116
modules/preprocs/nasm/nasmlib.c
Normal file
1116
modules/preprocs/nasm/nasmlib.c
Normal file
File diff suppressed because it is too large
Load diff
258
modules/preprocs/nasm/nasmlib.h
Normal file
258
modules/preprocs/nasm/nasmlib.h
Normal file
|
|
@ -0,0 +1,258 @@
|
|||
/* nasmlib.h header file for nasmlib.c
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*/
|
||||
|
||||
#ifndef NASM_NASMLIB_H
|
||||
#define NASM_NASMLIB_H
|
||||
|
||||
/*
|
||||
* If this is defined, the wrappers around malloc et al will
|
||||
* transform into logging variants, which will cause NASM to create
|
||||
* a file called `malloc.log' when run, and spew details of all its
|
||||
* memory management into that. That can then be analysed to detect
|
||||
* memory leaks and potentially other problems too.
|
||||
*/
|
||||
/* #define LOGALLOC */
|
||||
|
||||
/*
|
||||
* Wrappers around malloc, realloc and free. nasm_malloc will
|
||||
* fatal-error and die rather than return NULL; nasm_realloc will
|
||||
* do likewise, and will also guarantee to work right on being
|
||||
* passed a NULL pointer; nasm_free will do nothing if it is passed
|
||||
* a NULL pointer.
|
||||
*/
|
||||
#ifdef NASM_NASM_H /* need efunc defined for this */
|
||||
void nasm_set_malloc_error (efunc);
|
||||
#ifndef LOGALLOC
|
||||
void *nasm_malloc (size_t);
|
||||
void *nasm_realloc (void *, size_t);
|
||||
void nasm_free (void *);
|
||||
char *nasm_strdup (const char *);
|
||||
char *nasm_strndup (char *, size_t);
|
||||
#else
|
||||
void *nasm_malloc_log (char *, int, size_t);
|
||||
void *nasm_realloc_log (char *, int, void *, size_t);
|
||||
void nasm_free_log (char *, int, void *);
|
||||
char *nasm_strdup_log (char *, int, const char *);
|
||||
char *nasm_strndup_log (char *, int, char *, size_t);
|
||||
#define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
|
||||
#define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
|
||||
#define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
|
||||
#define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
|
||||
#define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ANSI doesn't guarantee the presence of `stricmp' or
|
||||
* `strcasecmp'.
|
||||
*/
|
||||
#if defined(stricmp) || defined(strcasecmp)
|
||||
#if defined(stricmp)
|
||||
#define nasm_stricmp stricmp
|
||||
#else
|
||||
#define nasm_stricmp strcasecmp
|
||||
#endif
|
||||
#else
|
||||
int nasm_stricmp (const char *, const char *);
|
||||
#endif
|
||||
|
||||
#if defined(strnicmp) || defined(strncasecmp)
|
||||
#if defined(strnicmp)
|
||||
#define nasm_strnicmp strnicmp
|
||||
#else
|
||||
#define nasm_strnicmp strncasecmp
|
||||
#endif
|
||||
#else
|
||||
int nasm_strnicmp (const char *, const char *, int);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Convert a string into a number, using NASM number rules. Sets
|
||||
* `*error' to TRUE if an error occurs, and FALSE otherwise.
|
||||
*/
|
||||
long readnum(char *str, int *error);
|
||||
|
||||
/*
|
||||
* Convert a character constant into a number. Sets
|
||||
* `*warn' to TRUE if an overflow occurs, and FALSE otherwise.
|
||||
* str points to and length covers the middle of the string,
|
||||
* without the quotes.
|
||||
*/
|
||||
long readstrnum(char *str, int length, int *warn);
|
||||
|
||||
/*
|
||||
* seg_init: Initialise the segment-number allocator.
|
||||
* seg_alloc: allocate a hitherto unused segment number.
|
||||
*/
|
||||
void seg_init(void);
|
||||
long seg_alloc(void);
|
||||
|
||||
/*
|
||||
* many output formats will be able to make use of this: a standard
|
||||
* function to add an extension to the name of the input file
|
||||
*/
|
||||
#ifdef NASM_NASM_H
|
||||
void standard_extension (char *inname, char *outname, char *extension,
|
||||
efunc error);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* some handy macros that will probably be of use in more than one
|
||||
* output format: convert integers into little-endian byte packed
|
||||
* format in memory
|
||||
*/
|
||||
|
||||
#define WRITELONG(p,v) \
|
||||
do { \
|
||||
*(p)++ = (v) & 0xFF; \
|
||||
*(p)++ = ((v) >> 8) & 0xFF; \
|
||||
*(p)++ = ((v) >> 16) & 0xFF; \
|
||||
*(p)++ = ((v) >> 24) & 0xFF; \
|
||||
} while (0)
|
||||
|
||||
#define WRITESHORT(p,v) \
|
||||
do { \
|
||||
*(p)++ = (v) & 0xFF; \
|
||||
*(p)++ = ((v) >> 8) & 0xFF; \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* and routines to do the same thing to a file
|
||||
*/
|
||||
void fwriteshort (int data, FILE *fp);
|
||||
void fwritelong (long data, FILE *fp);
|
||||
|
||||
/*
|
||||
* Routines to manage a dynamic random access array of longs which
|
||||
* may grow in size to be more than the largest single malloc'able
|
||||
* chunk.
|
||||
*/
|
||||
|
||||
#define RAA_BLKSIZE 4096 /* this many longs allocated at once */
|
||||
#define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
|
||||
|
||||
typedef struct RAA RAA;
|
||||
typedef union RAA_UNION RAA_UNION;
|
||||
typedef struct RAA_LEAF RAA_LEAF;
|
||||
typedef struct RAA_BRANCH RAA_BRANCH;
|
||||
|
||||
struct RAA {
|
||||
/*
|
||||
* Number of layers below this one to get to the real data. 0
|
||||
* means this structure is a leaf, holding RAA_BLKSIZE real
|
||||
* data items; 1 and above mean it's a branch, holding
|
||||
* RAA_LAYERSIZE pointers to the next level branch or leaf
|
||||
* structures.
|
||||
*/
|
||||
int layers;
|
||||
/*
|
||||
* Number of real data items spanned by one position in the
|
||||
* `data' array at this level. This number is 1, trivially, for
|
||||
* a leaf (level 0): for a level 1 branch it should be
|
||||
* RAA_BLKSIZE, and for a level 2 branch it's
|
||||
* RAA_LAYERSIZE*RAA_BLKSIZE.
|
||||
*/
|
||||
long stepsize;
|
||||
union RAA_UNION {
|
||||
struct RAA_LEAF {
|
||||
long data[RAA_BLKSIZE];
|
||||
} l;
|
||||
struct RAA_BRANCH {
|
||||
struct RAA *data[RAA_LAYERSIZE];
|
||||
} b;
|
||||
} u;
|
||||
};
|
||||
|
||||
|
||||
struct RAA *raa_init (void);
|
||||
void raa_free (struct RAA *);
|
||||
long raa_read (struct RAA *, long);
|
||||
struct RAA *raa_write (struct RAA *r, long posn, long value);
|
||||
|
||||
/*
|
||||
* Routines to manage a dynamic sequential-access array, under the
|
||||
* same restriction on maximum mallocable block. This array may be
|
||||
* written to in two ways: a contiguous chunk can be reserved of a
|
||||
* given size, and a pointer returned, or single-byte data may be
|
||||
* written. The array can also be read back in the same two ways:
|
||||
* as a series of big byte-data blocks or as a list of structures
|
||||
* of a given size.
|
||||
*/
|
||||
|
||||
struct SAA {
|
||||
/*
|
||||
* members `end' and `elem_len' are only valid in first link in
|
||||
* list; `rptr' and `rpos' are used for reading
|
||||
*/
|
||||
struct SAA *next, *end, *rptr;
|
||||
long elem_len, length, posn, start, rpos;
|
||||
char *data;
|
||||
};
|
||||
|
||||
struct SAA *saa_init (long elem_len); /* 1 == byte */
|
||||
void saa_free (struct SAA *);
|
||||
void *saa_wstruct (struct SAA *); /* return a structure of elem_len */
|
||||
void saa_wbytes (struct SAA *, const void *, long); /* write arbitrary bytes */
|
||||
void saa_rewind (struct SAA *); /* for reading from beginning */
|
||||
void *saa_rstruct (struct SAA *); /* return NULL on EOA */
|
||||
void *saa_rbytes (struct SAA *, long *); /* return 0 on EOA */
|
||||
void saa_rnbytes (struct SAA *, void *, long); /* read a given no. of bytes */
|
||||
void saa_fread (struct SAA *s, long posn, void *p, long len); /* fixup */
|
||||
void saa_fwrite (struct SAA *s, long posn, void *p, long len); /* fixup */
|
||||
void saa_fpwrite (struct SAA *, FILE *);
|
||||
|
||||
#ifdef NASM_NASM_H
|
||||
/*
|
||||
* Standard scanner.
|
||||
*/
|
||||
extern char *stdscan_bufptr;
|
||||
void stdscan_reset(void);
|
||||
int stdscan (void *private_data, struct tokenval *tv);
|
||||
#endif
|
||||
|
||||
#ifdef NASM_NASM_H
|
||||
/*
|
||||
* Library routines to manipulate expression data types.
|
||||
*/
|
||||
int is_reloc(expr *);
|
||||
int is_simple(expr *);
|
||||
int is_really_simple (expr *);
|
||||
int is_unknown(expr *);
|
||||
int is_just_unknown(expr *);
|
||||
long reloc_value(expr *);
|
||||
long reloc_seg(expr *);
|
||||
long reloc_wrt(expr *);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Binary search routine. Returns index into `array' of an entry
|
||||
* matching `string', or <0 if no match. `array' is taken to
|
||||
* contain `size' elements.
|
||||
*/
|
||||
int bsi (char *string, const char **array, int size);
|
||||
|
||||
|
||||
char *src_set_fname(char *newname);
|
||||
long src_set_linnum(long newline);
|
||||
long src_get_linnum(void);
|
||||
/*
|
||||
* src_get may be used if you simply want to know the source file and line.
|
||||
* It is also used if you maintain private status about the source location
|
||||
* It return 0 if the information was the same as the last time you
|
||||
* checked, -1 if the name changed and (new-old) if just the line changed.
|
||||
*/
|
||||
int src_get(long *xline, char **xname);
|
||||
|
||||
void nasm_quote(char **str);
|
||||
char *nasm_strcat(char *one, char *two);
|
||||
void nasmlib_cleanup(void);
|
||||
|
||||
void null_debug_routine(const char *directive, const char *params);
|
||||
extern struct dfmt null_debug_form;
|
||||
extern struct dfmt *null_debug_arr[2];
|
||||
#endif
|
||||
110
modules/preprocs/nasm/standard.mac
Normal file
110
modules/preprocs/nasm/standard.mac
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
; Standard macro set for NASM -*- nasm -*-
|
||||
|
||||
; Macros to make NASM ignore some TASM directives before the first include
|
||||
; directive.
|
||||
|
||||
%idefine IDEAL
|
||||
%idefine JUMPS
|
||||
%idefine P386
|
||||
%idefine P486
|
||||
%idefine P586
|
||||
%idefine END
|
||||
|
||||
; This is a magic token which indicates the end of the TASM macros
|
||||
*END*TASM*MACROS*
|
||||
|
||||
; Note that although some user-level forms of directives are defined
|
||||
; here, not all of them are: the user-level form of a format-specific
|
||||
; directive should be defined in the module for that directive.
|
||||
|
||||
; These two need to be defined, though the actual definitions will
|
||||
; be constantly updated during preprocessing.
|
||||
%define __FILE__
|
||||
%define __LINE__
|
||||
|
||||
%define __SECT__ ; it ought to be defined, even if as nothing
|
||||
|
||||
%imacro section 1+.nolist
|
||||
%define __SECT__ [section %1]
|
||||
__SECT__
|
||||
%endmacro
|
||||
%imacro segment 1+.nolist
|
||||
%define __SECT__ [segment %1]
|
||||
__SECT__
|
||||
%endmacro
|
||||
|
||||
%imacro absolute 1+.nolist
|
||||
%define __SECT__ [absolute %1]
|
||||
__SECT__
|
||||
%endmacro
|
||||
|
||||
%imacro struc 1.nolist
|
||||
%push struc
|
||||
%define %$strucname %1
|
||||
[absolute 0]
|
||||
%$strucname: ; allow definition of `.member' to work sanely
|
||||
%endmacro
|
||||
%imacro endstruc 0.nolist
|
||||
%{$strucname}_size:
|
||||
%pop
|
||||
__SECT__
|
||||
%endmacro
|
||||
|
||||
%imacro istruc 1.nolist
|
||||
%push istruc
|
||||
%define %$strucname %1
|
||||
%$strucstart:
|
||||
%endmacro
|
||||
%imacro at 1-2+.nolist
|
||||
times %1-($-%$strucstart) db 0
|
||||
%2
|
||||
%endmacro
|
||||
%imacro iend 0.nolist
|
||||
times %{$strucname}_size-($-%$strucstart) db 0
|
||||
%pop
|
||||
%endmacro
|
||||
|
||||
%imacro align 1-2+.nolist nop
|
||||
times ($$-$) & ((%1)-1) %2
|
||||
%endmacro
|
||||
%imacro alignb 1-2+.nolist resb 1
|
||||
times ($$-$) & ((%1)-1) %2
|
||||
%endmacro
|
||||
|
||||
%imacro extern 1-*.nolist
|
||||
%rep %0
|
||||
[extern %1]
|
||||
%rotate 1
|
||||
%endrep
|
||||
%endmacro
|
||||
|
||||
%imacro bits 1+.nolist
|
||||
[bits %1]
|
||||
%endmacro
|
||||
|
||||
%imacro use16 0.nolist
|
||||
[bits 16]
|
||||
%endmacro
|
||||
%imacro use32 0.nolist
|
||||
[bits 32]
|
||||
%endmacro
|
||||
|
||||
%imacro global 1-*.nolist
|
||||
%rep %0
|
||||
[global %1]
|
||||
%rotate 1
|
||||
%endrep
|
||||
%endmacro
|
||||
|
||||
%imacro common 1-*.nolist
|
||||
%rep %0
|
||||
[common %1]
|
||||
%rotate 1
|
||||
%endrep
|
||||
%endmacro
|
||||
|
||||
%imacro cpu 1+.nolist
|
||||
[cpu %1]
|
||||
%endmacro
|
||||
|
||||
|
||||
48
src/preprocs/nasm/macros.pl
Normal file
48
src/preprocs/nasm/macros.pl
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/perl -w
|
||||
#
|
||||
# macros.pl produce macros.c from standard.mac
|
||||
#
|
||||
# The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
# Julian Hall. All rights reserved. The software is
|
||||
# redistributable under the licence given in the file "Licence"
|
||||
# distributed in the NASM archive.
|
||||
|
||||
use strict;
|
||||
|
||||
my $fname;
|
||||
my $line = 0;
|
||||
my $index = 0;
|
||||
my $tasm_count;
|
||||
|
||||
undef $tasm_count;
|
||||
|
||||
open(OUTPUT,">macros.c") or die "unable to open macros.c\n";
|
||||
|
||||
print OUTPUT "/* This file auto-generated from standard.mac by macros.pl" .
|
||||
" - don't edit it */\n\n#include <stddef.h>\n\nstatic const char *stdmac[] = {\n";
|
||||
|
||||
foreach $fname ( @ARGV ) {
|
||||
open(INPUT,$fname) or die "unable to open $fname\n";
|
||||
while (<INPUT>) {
|
||||
$line++;
|
||||
chomp;
|
||||
if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
|
||||
$tasm_count = $index;
|
||||
} elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
|
||||
$_ = $1;
|
||||
s/\\/\\\\/g;
|
||||
s/"/\\"/g;
|
||||
if (length > 0) {
|
||||
print OUTPUT " \"$_\",\n";
|
||||
$index++;
|
||||
}
|
||||
} else {
|
||||
die "$fname:$line: error unterminated quote";
|
||||
}
|
||||
}
|
||||
close(INPUT);
|
||||
}
|
||||
print OUTPUT " NULL\n};\n";
|
||||
$tasm_count = $index unless ( defined($tasm_count) );
|
||||
print OUTPUT "#define TASM_MACRO_COUNT $tasm_count\n";
|
||||
close(OUTPUT);
|
||||
825
src/preprocs/nasm/nasm-eval.c
Normal file
825
src/preprocs/nasm/nasm-eval.c
Normal file
|
|
@ -0,0 +1,825 @@
|
|||
/* eval.c expression evaluator for the Netwide Assembler
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*
|
||||
* initial version 27/iii/95 by Simon Tatham
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "nasm.h"
|
||||
#include "nasmlib.h"
|
||||
#include "eval.h"
|
||||
#include "labels.h"
|
||||
|
||||
#define TEMPEXPRS_DELTA 128
|
||||
#define TEMPEXPR_DELTA 8
|
||||
|
||||
static scanner scan; /* Address of scanner routine */
|
||||
static efunc error; /* Address of error reporting routine */
|
||||
static lfunc labelfunc; /* Address of label routine */
|
||||
|
||||
static struct ofmt *outfmt; /* Structure of addresses of output routines */
|
||||
|
||||
static expr **tempexprs = NULL;
|
||||
static int ntempexprs;
|
||||
static int tempexprs_size = 0;
|
||||
|
||||
static expr *tempexpr;
|
||||
static int ntempexpr;
|
||||
static int tempexpr_size;
|
||||
|
||||
static struct tokenval *tokval; /* The current token */
|
||||
static int i; /* The t_type of tokval */
|
||||
|
||||
static void *scpriv;
|
||||
static loc_t *location; /* Pointer to current line's segment,offset */
|
||||
static int *opflags;
|
||||
|
||||
static struct eval_hints *hint;
|
||||
|
||||
extern int in_abs_seg; /* ABSOLUTE segment flag */
|
||||
extern long abs_seg; /* ABSOLUTE segment */
|
||||
extern long abs_offset; /* ABSOLUTE segment offset */
|
||||
|
||||
/*
|
||||
* Unimportant cleanup is done to avoid confusing people who are trying
|
||||
* to debug real memory leaks
|
||||
*/
|
||||
void eval_cleanup(void)
|
||||
{
|
||||
while (ntempexprs)
|
||||
nasm_free (tempexprs[--ntempexprs]);
|
||||
nasm_free (tempexprs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Construct a temporary expression.
|
||||
*/
|
||||
static void begintemp(void)
|
||||
{
|
||||
tempexpr = NULL;
|
||||
tempexpr_size = ntempexpr = 0;
|
||||
}
|
||||
|
||||
static void addtotemp(long type, long value)
|
||||
{
|
||||
while (ntempexpr >= tempexpr_size) {
|
||||
tempexpr_size += TEMPEXPR_DELTA;
|
||||
tempexpr = nasm_realloc(tempexpr,
|
||||
tempexpr_size*sizeof(*tempexpr));
|
||||
}
|
||||
tempexpr[ntempexpr].type = type;
|
||||
tempexpr[ntempexpr++].value = value;
|
||||
}
|
||||
|
||||
static expr *finishtemp(void)
|
||||
{
|
||||
addtotemp (0L, 0L); /* terminate */
|
||||
while (ntempexprs >= tempexprs_size) {
|
||||
tempexprs_size += TEMPEXPRS_DELTA;
|
||||
tempexprs = nasm_realloc(tempexprs,
|
||||
tempexprs_size*sizeof(*tempexprs));
|
||||
}
|
||||
return tempexprs[ntempexprs++] = tempexpr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add two vector datatypes. We have some bizarre behaviour on far-
|
||||
* absolute segment types: we preserve them during addition _only_
|
||||
* if one of the segments is a truly pure scalar.
|
||||
*/
|
||||
static expr *add_vectors(expr *p, expr *q)
|
||||
{
|
||||
int preserve;
|
||||
|
||||
preserve = is_really_simple(p) || is_really_simple(q);
|
||||
|
||||
begintemp();
|
||||
|
||||
while (p->type && q->type &&
|
||||
p->type < EXPR_SEGBASE+SEG_ABS &&
|
||||
q->type < EXPR_SEGBASE+SEG_ABS)
|
||||
{
|
||||
int lasttype;
|
||||
|
||||
if (p->type > q->type) {
|
||||
addtotemp(q->type, q->value);
|
||||
lasttype = q++->type;
|
||||
} else if (p->type < q->type) {
|
||||
addtotemp(p->type, p->value);
|
||||
lasttype = p++->type;
|
||||
} else { /* *p and *q have same type */
|
||||
long sum = p->value + q->value;
|
||||
if (sum)
|
||||
addtotemp(p->type, sum);
|
||||
lasttype = p->type;
|
||||
p++, q++;
|
||||
}
|
||||
if (lasttype == EXPR_UNKNOWN) {
|
||||
return finishtemp();
|
||||
}
|
||||
}
|
||||
while (p->type &&
|
||||
(preserve || p->type < EXPR_SEGBASE+SEG_ABS))
|
||||
{
|
||||
addtotemp(p->type, p->value);
|
||||
p++;
|
||||
}
|
||||
while (q->type &&
|
||||
(preserve || q->type < EXPR_SEGBASE+SEG_ABS))
|
||||
{
|
||||
addtotemp(q->type, q->value);
|
||||
q++;
|
||||
}
|
||||
|
||||
return finishtemp();
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiply a vector by a scalar. Strip far-absolute segment part
|
||||
* if present.
|
||||
*
|
||||
* Explicit treatment of UNKNOWN is not required in this routine,
|
||||
* since it will silently do the Right Thing anyway.
|
||||
*
|
||||
* If `affect_hints' is set, we also change the hint type to
|
||||
* NOTBASE if a MAKEBASE hint points at a register being
|
||||
* multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
|
||||
* as the base register.
|
||||
*/
|
||||
static expr *scalar_mult(expr *vect, long scalar, int affect_hints)
|
||||
{
|
||||
expr *p = vect;
|
||||
|
||||
while (p->type && p->type < EXPR_SEGBASE+SEG_ABS) {
|
||||
p->value = scalar * (p->value);
|
||||
if (hint && hint->type == EAH_MAKEBASE &&
|
||||
p->type == hint->base && affect_hints)
|
||||
hint->type = EAH_NOTBASE;
|
||||
p++;
|
||||
}
|
||||
p->type = 0;
|
||||
|
||||
return vect;
|
||||
}
|
||||
|
||||
static expr *scalarvect (long scalar)
|
||||
{
|
||||
begintemp();
|
||||
addtotemp(EXPR_SIMPLE, scalar);
|
||||
return finishtemp();
|
||||
}
|
||||
|
||||
static expr *unknown_expr (void)
|
||||
{
|
||||
begintemp();
|
||||
addtotemp(EXPR_UNKNOWN, 1L);
|
||||
return finishtemp();
|
||||
}
|
||||
|
||||
/*
|
||||
* The SEG operator: calculate the segment part of a relocatable
|
||||
* value. Return NULL, as usual, if an error occurs. Report the
|
||||
* error too.
|
||||
*/
|
||||
static expr *segment_part (expr *e)
|
||||
{
|
||||
long seg;
|
||||
|
||||
if (is_unknown(e))
|
||||
return unknown_expr();
|
||||
|
||||
if (!is_reloc(e)) {
|
||||
error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
seg = reloc_seg(e);
|
||||
if (seg == NO_SEG) {
|
||||
error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
|
||||
return NULL;
|
||||
} else if (seg & SEG_ABS) {
|
||||
return scalarvect(seg & ~SEG_ABS);
|
||||
} else if (seg & 1) {
|
||||
error(ERR_NONFATAL, "SEG applied to something which"
|
||||
" is already a segment base");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
long base = outfmt->segbase(seg+1);
|
||||
|
||||
begintemp();
|
||||
addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE+base), 1L);
|
||||
return finishtemp();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Recursive-descent parser. Called with a single boolean operand,
|
||||
* which is TRUE if the evaluation is critical (i.e. unresolved
|
||||
* symbols are an error condition). Must update the global `i' to
|
||||
* reflect the token after the parsed string. May return NULL.
|
||||
*
|
||||
* evaluate() should report its own errors: on return it is assumed
|
||||
* that if NULL has been returned, the error has already been
|
||||
* reported.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Grammar parsed is:
|
||||
*
|
||||
* expr : bexpr [ WRT expr6 ]
|
||||
* bexpr : rexp0 or expr0 depending on relative-mode setting
|
||||
* rexp0 : rexp1 [ {||} rexp1...]
|
||||
* rexp1 : rexp2 [ {^^} rexp2...]
|
||||
* rexp2 : rexp3 [ {&&} rexp3...]
|
||||
* rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
|
||||
* expr0 : expr1 [ {|} expr1...]
|
||||
* expr1 : expr2 [ {^} expr2...]
|
||||
* expr2 : expr3 [ {&} expr3...]
|
||||
* expr3 : expr4 [ {<<,>>} expr4...]
|
||||
* expr4 : expr5 [ {+,-} expr5...]
|
||||
* expr5 : expr6 [ {*,/,%,//,%%} expr6...]
|
||||
* expr6 : { ~,+,-,SEG } expr6
|
||||
* | (bexpr)
|
||||
* | symbol
|
||||
* | $
|
||||
* | number
|
||||
*/
|
||||
|
||||
static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
|
||||
|
||||
static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
|
||||
static expr *expr4(int), *expr5(int), *expr6(int);
|
||||
|
||||
static expr *(*bexpr)(int);
|
||||
|
||||
static expr *rexp0(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = rexp1(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_DBL_OR)
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = rexp1(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`|' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect ((long) (reloc_value(e) || reloc_value(f)));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *rexp1(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = rexp2(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_DBL_XOR)
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = rexp2(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`^' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect ((long) (!reloc_value(e) ^ !reloc_value(f)));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *rexp2(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = rexp3(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
while (i == TOKEN_DBL_AND)
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = rexp3(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`&' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect ((long) (reloc_value(e) && reloc_value(f)));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *rexp3(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
long v;
|
||||
|
||||
e = expr0(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
|
||||
i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE)
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr0(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
|
||||
e = add_vectors (e, scalar_mult(f, -1L, FALSE));
|
||||
|
||||
switch (j)
|
||||
{
|
||||
case TOKEN_EQ: case TOKEN_NE:
|
||||
if (is_unknown(e))
|
||||
v = -1; /* means unknown */
|
||||
else if (!is_really_simple(e) || reloc_value(e) != 0)
|
||||
v = (j == TOKEN_NE); /* unequal, so return TRUE if NE */
|
||||
else
|
||||
v = (j == TOKEN_EQ); /* equal, so return TRUE if EQ */
|
||||
break;
|
||||
default:
|
||||
if (is_unknown(e))
|
||||
v = -1; /* means unknown */
|
||||
else if (!is_really_simple(e)) {
|
||||
error(ERR_NONFATAL, "`%s': operands differ by a non-scalar",
|
||||
(j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" :
|
||||
j == TOKEN_GE ? ">=" : ">"));
|
||||
v = 0; /* must set it to _something_ */
|
||||
} else {
|
||||
int vv = reloc_value(e);
|
||||
if (vv == 0)
|
||||
v = (j == TOKEN_LE || j == TOKEN_GE);
|
||||
else if (vv > 0)
|
||||
v = (j == TOKEN_GE || j == TOKEN_GT);
|
||||
else /* vv < 0 */
|
||||
v = (j == TOKEN_LE || j == TOKEN_LT);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (v == -1)
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect(v);
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr0(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr1(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == '|')
|
||||
{
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr1(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`|' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (reloc_value(e) | reloc_value(f));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr1(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr2(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == '^') {
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr2(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`^' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (reloc_value(e) ^ reloc_value(f));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr2(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr3(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == '&') {
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr3(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "`&' operator may only be applied to"
|
||||
" scalar values");
|
||||
}
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (reloc_value(e) & reloc_value(f));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr3(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr4(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
while (i == TOKEN_SHL || i == TOKEN_SHR)
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr4(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f)))
|
||||
{
|
||||
error(ERR_NONFATAL, "shift operator may only be applied to"
|
||||
" scalar values");
|
||||
} else if (is_just_unknown(e) || is_just_unknown(f)) {
|
||||
e = unknown_expr();
|
||||
} else switch (j) {
|
||||
case TOKEN_SHL:
|
||||
e = scalarvect (reloc_value(e) << reloc_value(f));
|
||||
break;
|
||||
case TOKEN_SHR:
|
||||
e = scalarvect (((unsigned long)reloc_value(e)) >>
|
||||
reloc_value(f));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr4(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr5(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
while (i == '+' || i == '-')
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr5(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
switch (j) {
|
||||
case '+':
|
||||
e = add_vectors (e, f);
|
||||
break;
|
||||
case '-':
|
||||
e = add_vectors (e, scalar_mult(f, -1L, FALSE));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr5(int critical)
|
||||
{
|
||||
expr *e, *f;
|
||||
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
while (i == '*' || i == '/' || i == '%' ||
|
||||
i == TOKEN_SDIV || i == TOKEN_SMOD)
|
||||
{
|
||||
int j = i;
|
||||
i = scan(scpriv, tokval);
|
||||
f = expr6(critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
|
||||
!(is_simple(f) || is_just_unknown(f))))
|
||||
{
|
||||
error(ERR_NONFATAL, "division operator may only be applied to"
|
||||
" scalar values");
|
||||
return NULL;
|
||||
}
|
||||
if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
|
||||
error(ERR_NONFATAL, "division by zero");
|
||||
return NULL;
|
||||
}
|
||||
switch (j) {
|
||||
case '*':
|
||||
if (is_simple(e))
|
||||
e = scalar_mult (f, reloc_value(e), TRUE);
|
||||
else if (is_simple(f))
|
||||
e = scalar_mult (e, reloc_value(f), TRUE);
|
||||
else if (is_just_unknown(e) && is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else {
|
||||
error(ERR_NONFATAL, "unable to multiply two "
|
||||
"non-scalar objects");
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
case '/':
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((unsigned long)reloc_value(e)) /
|
||||
((unsigned long)reloc_value(f)));
|
||||
break;
|
||||
case '%':
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((unsigned long)reloc_value(e)) %
|
||||
((unsigned long)reloc_value(f)));
|
||||
break;
|
||||
case TOKEN_SDIV:
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((signed long)reloc_value(e)) /
|
||||
((signed long)reloc_value(f)));
|
||||
break;
|
||||
case TOKEN_SMOD:
|
||||
if (is_just_unknown(e) || is_just_unknown(f))
|
||||
e = unknown_expr();
|
||||
else
|
||||
e = scalarvect (((signed long)reloc_value(e)) %
|
||||
((signed long)reloc_value(f)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
static expr *expr6(int critical)
|
||||
{
|
||||
long type;
|
||||
expr *e;
|
||||
long label_seg, label_ofs;
|
||||
|
||||
if (i == '-') {
|
||||
i = scan(scpriv, tokval);
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
return scalar_mult (e, -1L, FALSE);
|
||||
} else if (i == '+') {
|
||||
i = scan(scpriv, tokval);
|
||||
return expr6(critical);
|
||||
} else if (i == '~') {
|
||||
i = scan(scpriv, tokval);
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
if (is_just_unknown(e))
|
||||
return unknown_expr();
|
||||
else if (!is_simple(e)) {
|
||||
error(ERR_NONFATAL, "`~' operator may only be applied to"
|
||||
" scalar values");
|
||||
return NULL;
|
||||
}
|
||||
return scalarvect(~reloc_value(e));
|
||||
} else if (i == TOKEN_SEG) {
|
||||
i = scan(scpriv, tokval);
|
||||
e = expr6(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
e = segment_part(e);
|
||||
if (!e)
|
||||
return NULL;
|
||||
if (is_unknown(e) && critical) {
|
||||
error(ERR_NONFATAL, "unable to determine segment base");
|
||||
return NULL;
|
||||
}
|
||||
return e;
|
||||
} else if (i == '(') {
|
||||
i = scan(scpriv, tokval);
|
||||
e = bexpr(critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
if (i != ')') {
|
||||
error(ERR_NONFATAL, "expecting `)'");
|
||||
return NULL;
|
||||
}
|
||||
i = scan(scpriv, tokval);
|
||||
return e;
|
||||
}
|
||||
else if (i == TOKEN_NUM || i == TOKEN_REG || i == TOKEN_ID ||
|
||||
i == TOKEN_HERE || i == TOKEN_BASE)
|
||||
{
|
||||
begintemp();
|
||||
switch (i) {
|
||||
case TOKEN_NUM:
|
||||
addtotemp(EXPR_SIMPLE, tokval->t_integer);
|
||||
break;
|
||||
case TOKEN_REG:
|
||||
addtotemp(tokval->t_integer, 1L);
|
||||
if (hint && hint->type == EAH_NOHINT)
|
||||
hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
|
||||
break;
|
||||
case TOKEN_ID:
|
||||
case TOKEN_HERE:
|
||||
case TOKEN_BASE:
|
||||
/*
|
||||
* If !location->known, this indicates that no
|
||||
* symbol, Here or Base references are valid because we
|
||||
* are in preprocess-only mode.
|
||||
*/
|
||||
if (!location->known) {
|
||||
error(ERR_NONFATAL,
|
||||
"%s not supported in preprocess-only mode",
|
||||
(i == TOKEN_ID ? "symbol references" :
|
||||
i == TOKEN_HERE ? "`$'" : "`$$'"));
|
||||
addtotemp(EXPR_UNKNOWN, 1L);
|
||||
break;
|
||||
}
|
||||
|
||||
type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
|
||||
if (i == TOKEN_BASE)
|
||||
{
|
||||
label_seg = in_abs_seg ? abs_seg : location->segment;
|
||||
label_ofs = 0;
|
||||
} else if (i == TOKEN_HERE) {
|
||||
label_seg = in_abs_seg ? abs_seg : location->segment;
|
||||
label_ofs = in_abs_seg ? abs_offset : location->offset;
|
||||
} else {
|
||||
if (!labelfunc(tokval->t_charptr,&label_seg,&label_ofs))
|
||||
{
|
||||
if (critical == 2) {
|
||||
error (ERR_NONFATAL, "symbol `%s' undefined",
|
||||
tokval->t_charptr);
|
||||
return NULL;
|
||||
} else if (critical == 1) {
|
||||
error (ERR_NONFATAL,
|
||||
"symbol `%s' not defined before use",
|
||||
tokval->t_charptr);
|
||||
return NULL;
|
||||
} else {
|
||||
if (opflags)
|
||||
*opflags |= 1;
|
||||
type = EXPR_UNKNOWN;
|
||||
label_seg = NO_SEG;
|
||||
label_ofs = 1;
|
||||
}
|
||||
}
|
||||
if (opflags && is_extern (tokval->t_charptr))
|
||||
*opflags |= OPFLAG_EXTERN;
|
||||
}
|
||||
addtotemp(type, label_ofs);
|
||||
if (label_seg!=NO_SEG)
|
||||
addtotemp(EXPR_SEGBASE + label_seg, 1L);
|
||||
break;
|
||||
}
|
||||
i = scan(scpriv, tokval);
|
||||
return finishtemp();
|
||||
} else {
|
||||
error(ERR_NONFATAL, "expression syntax error");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void eval_global_info (struct ofmt *output, lfunc lookup_label, loc_t *locp)
|
||||
{
|
||||
outfmt = output;
|
||||
labelfunc = lookup_label;
|
||||
location = locp;
|
||||
}
|
||||
|
||||
expr *evaluate (scanner sc, void *scprivate, struct tokenval *tv,
|
||||
int *fwref, int critical, efunc report_error,
|
||||
struct eval_hints *hints)
|
||||
{
|
||||
expr *e;
|
||||
expr *f = NULL;
|
||||
|
||||
hint = hints;
|
||||
if (hint)
|
||||
hint->type = EAH_NOHINT;
|
||||
|
||||
if (critical & CRITICAL) {
|
||||
critical &= ~CRITICAL;
|
||||
bexpr = rexp0;
|
||||
} else
|
||||
bexpr = expr0;
|
||||
|
||||
scan = sc;
|
||||
scpriv = scprivate;
|
||||
tokval = tv;
|
||||
error = report_error;
|
||||
opflags = fwref;
|
||||
|
||||
if (tokval->t_type == TOKEN_INVALID)
|
||||
i = scan(scpriv, tokval);
|
||||
else
|
||||
i = tokval->t_type;
|
||||
|
||||
while (ntempexprs) /* initialise temporary storage */
|
||||
nasm_free (tempexprs[--ntempexprs]);
|
||||
|
||||
e = bexpr (critical);
|
||||
if (!e)
|
||||
return NULL;
|
||||
|
||||
if (i == TOKEN_WRT) {
|
||||
i = scan(scpriv, tokval); /* eat the WRT */
|
||||
f = expr6 (critical);
|
||||
if (!f)
|
||||
return NULL;
|
||||
}
|
||||
e = scalar_mult (e, 1L, FALSE); /* strip far-absolute segment part */
|
||||
if (f) {
|
||||
expr *g;
|
||||
if (is_just_unknown(f))
|
||||
g = unknown_expr();
|
||||
else {
|
||||
long value;
|
||||
begintemp();
|
||||
if (!is_reloc(f)) {
|
||||
error(ERR_NONFATAL, "invalid right-hand operand to WRT");
|
||||
return NULL;
|
||||
}
|
||||
value = reloc_seg(f);
|
||||
if (value == NO_SEG)
|
||||
value = reloc_value(f) | SEG_ABS;
|
||||
else if (!(value & SEG_ABS) && !(value % 2) && critical)
|
||||
{
|
||||
error(ERR_NONFATAL, "invalid right-hand operand to WRT");
|
||||
return NULL;
|
||||
}
|
||||
addtotemp(EXPR_WRT, value);
|
||||
g = finishtemp();
|
||||
}
|
||||
e = add_vectors (e, g);
|
||||
}
|
||||
return e;
|
||||
}
|
||||
28
src/preprocs/nasm/nasm-eval.h
Normal file
28
src/preprocs/nasm/nasm-eval.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* eval.h header file for eval.c
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*/
|
||||
|
||||
#ifndef NASM_EVAL_H
|
||||
#define NASM_EVAL_H
|
||||
|
||||
/*
|
||||
* Called once to tell the evaluator what output format is
|
||||
* providing segment-base details, and what function can be used to
|
||||
* look labels up.
|
||||
*/
|
||||
void eval_global_info (struct ofmt *output, lfunc lookup_label, loc_t *locp);
|
||||
|
||||
/*
|
||||
* The evaluator itself.
|
||||
*/
|
||||
expr *evaluate (scanner sc, void *scprivate, struct tokenval *tv,
|
||||
int *fwref, int critical, efunc report_error,
|
||||
struct eval_hints *hints);
|
||||
|
||||
void eval_cleanup(void);
|
||||
|
||||
#endif
|
||||
4459
src/preprocs/nasm/nasm-pp.c
Normal file
4459
src/preprocs/nasm/nasm-pp.c
Normal file
File diff suppressed because it is too large
Load diff
20
src/preprocs/nasm/nasm-pp.h
Normal file
20
src/preprocs/nasm/nasm-pp.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* preproc.h header file for preproc.c
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*/
|
||||
|
||||
#ifndef NASM_PREPROC_H
|
||||
#define NASM_PREPROC_H
|
||||
|
||||
void pp_include_path (char *);
|
||||
void pp_pre_include (char *);
|
||||
void pp_pre_define (char *);
|
||||
void pp_pre_undefine (char *);
|
||||
void pp_extra_stdmac (const char **);
|
||||
|
||||
extern Preproc nasmpp;
|
||||
|
||||
#endif
|
||||
850
src/preprocs/nasm/nasm.h
Normal file
850
src/preprocs/nasm/nasm.h
Normal file
|
|
@ -0,0 +1,850 @@
|
|||
/* nasm.h main header file for the Netwide Assembler: inter-module interface
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*
|
||||
* initial version: 27/iii/95 by Simon Tatham
|
||||
*/
|
||||
|
||||
#ifndef NASM_NASM_H
|
||||
#define NASM_NASM_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "version.h" /* generated NASM version macros */
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0 /* comes in handy */
|
||||
#endif
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#define NO_SEG -1L /* null segment value */
|
||||
#define SEG_ABS 0x40000000L /* mask for far-absolute segments */
|
||||
|
||||
#ifndef FILENAME_MAX
|
||||
#define FILENAME_MAX 256
|
||||
#endif
|
||||
|
||||
#ifndef PREFIX_MAX
|
||||
#define PREFIX_MAX 10
|
||||
#endif
|
||||
|
||||
#ifndef POSTFIX_MAX
|
||||
#define POSTFIX_MAX 10
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Name pollution problems: <time.h> on Digital UNIX pulls in some
|
||||
* strange hardware header file which sees fit to define R_SP. We
|
||||
* undefine it here so as not to break the enum below.
|
||||
*/
|
||||
#ifdef R_SP
|
||||
#undef R_SP
|
||||
#endif
|
||||
|
||||
/*
|
||||
* We must declare the existence of this structure type up here,
|
||||
* since we have to reference it before we define it...
|
||||
*/
|
||||
struct ofmt;
|
||||
|
||||
/*
|
||||
* -------------------------
|
||||
* Error reporting functions
|
||||
* -------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* An error reporting function should look like this.
|
||||
*/
|
||||
typedef void (*efunc) (int severity, const char *fmt, ...);
|
||||
|
||||
/*
|
||||
* These are the error severity codes which get passed as the first
|
||||
* argument to an efunc.
|
||||
*/
|
||||
|
||||
#define ERR_DEBUG 0x00000008 /* put out debugging message */
|
||||
#define ERR_WARNING 0x00000000 /* warn only: no further action */
|
||||
#define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
|
||||
#define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
|
||||
#define ERR_PANIC 0x00000003 /* internal error: panic instantly
|
||||
* and dump core for reference */
|
||||
#define ERR_MASK 0x0000000F /* mask off the above codes */
|
||||
#define ERR_NOFILE 0x00000010 /* don't give source file name/line */
|
||||
#define ERR_USAGE 0x00000020 /* print a usage message */
|
||||
#define ERR_PASS1 0x00000040 /* only print this error on pass one */
|
||||
|
||||
/*
|
||||
* These codes define specific types of suppressible warning.
|
||||
*/
|
||||
|
||||
#define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */
|
||||
#define ERR_WARN_SHR 8 /* how far to shift right */
|
||||
|
||||
#define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */
|
||||
#define ERR_WARN_MSR 0x00000200 /* macro self-reference */
|
||||
#define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and
|
||||
* alone on line) */
|
||||
#define ERR_WARN_NOV 0x00000400 /* numeric overflow */
|
||||
#define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */
|
||||
#define ERR_WARN_MAX 5 /* the highest numbered one */
|
||||
|
||||
/*
|
||||
* -----------------------
|
||||
* Other function typedefs
|
||||
* -----------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* A label-lookup function should look like this.
|
||||
*/
|
||||
typedef int (*lfunc) (char *label, long *segment, long *offset);
|
||||
|
||||
/*
|
||||
* And a label-definition function like this. The boolean parameter
|
||||
* `is_norm' states whether the label is a `normal' label (which
|
||||
* should affect the local-label system), or something odder like
|
||||
* an EQU or a segment-base symbol, which shouldn't.
|
||||
*/
|
||||
typedef void (*ldfunc) (char *label, long segment, long offset, char *special,
|
||||
int is_norm, int isextrn, struct ofmt *ofmt,
|
||||
efunc error);
|
||||
|
||||
/*
|
||||
* List-file generators should look like this:
|
||||
*/
|
||||
typedef struct {
|
||||
/*
|
||||
* Called to initialise the listing file generator. Before this
|
||||
* is called, the other routines will silently do nothing when
|
||||
* called. The `char *' parameter is the file name to write the
|
||||
* listing to.
|
||||
*/
|
||||
void (*init) (char *, efunc);
|
||||
|
||||
/*
|
||||
* Called to clear stuff up and close the listing file.
|
||||
*/
|
||||
void (*cleanup) (void);
|
||||
|
||||
/*
|
||||
* Called to output binary data. Parameters are: the offset;
|
||||
* the data; the data type. Data types are similar to the
|
||||
* output-format interface, only OUT_ADDRESS will _always_ be
|
||||
* displayed as if it's relocatable, so ensure that any non-
|
||||
* relocatable address has been converted to OUT_RAWDATA by
|
||||
* then. Note that OUT_RAWDATA+0 is a valid data type, and is a
|
||||
* dummy call used to give the listing generator an offset to
|
||||
* work with when doing things like uplevel(LIST_TIMES) or
|
||||
* uplevel(LIST_INCBIN).
|
||||
*/
|
||||
void (*output) (long, const void *, unsigned long);
|
||||
|
||||
/*
|
||||
* Called to send a text line to the listing generator. The
|
||||
* `int' parameter is LIST_READ or LIST_MACRO depending on
|
||||
* whether the line came directly from an input file or is the
|
||||
* result of a multi-line macro expansion.
|
||||
*/
|
||||
void (*line) (int, char *);
|
||||
|
||||
/*
|
||||
* Called to change one of the various levelled mechanisms in
|
||||
* the listing generator. LIST_INCLUDE and LIST_MACRO can be
|
||||
* used to increase the nesting level of include files and
|
||||
* macro expansions; LIST_TIMES and LIST_INCBIN switch on the
|
||||
* two binary-output-suppression mechanisms for large-scale
|
||||
* pseudo-instructions.
|
||||
*
|
||||
* LIST_MACRO_NOLIST is synonymous with LIST_MACRO except that
|
||||
* it indicates the beginning of the expansion of a `nolist'
|
||||
* macro, so anything under that level won't be expanded unless
|
||||
* it includes another file.
|
||||
*/
|
||||
void (*uplevel) (int);
|
||||
|
||||
/*
|
||||
* Reverse the effects of uplevel.
|
||||
*/
|
||||
void (*downlevel) (int);
|
||||
} ListGen;
|
||||
|
||||
/*
|
||||
* The expression evaluator must be passed a scanner function; a
|
||||
* standard scanner is provided as part of nasmlib.c. The
|
||||
* preprocessor will use a different one. Scanners, and the
|
||||
* token-value structures they return, look like this.
|
||||
*
|
||||
* The return value from the scanner is always a copy of the
|
||||
* `t_type' field in the structure.
|
||||
*/
|
||||
struct tokenval {
|
||||
int t_type;
|
||||
long t_integer, t_inttwo;
|
||||
char *t_charptr;
|
||||
};
|
||||
typedef int (*scanner) (void *private_data, struct tokenval *tv);
|
||||
|
||||
/*
|
||||
* Token types returned by the scanner, in addition to ordinary
|
||||
* ASCII character values, and zero for end-of-string.
|
||||
*/
|
||||
enum { /* token types, other than chars */
|
||||
TOKEN_INVALID = -1, /* a placeholder value */
|
||||
TOKEN_EOS = 0, /* end of string */
|
||||
TOKEN_EQ = '=', TOKEN_GT = '>', TOKEN_LT = '<', /* aliases */
|
||||
TOKEN_ID = 256, TOKEN_NUM, TOKEN_REG, TOKEN_INSN, /* major token types */
|
||||
TOKEN_ERRNUM, /* numeric constant with error in */
|
||||
TOKEN_HERE, TOKEN_BASE, /* $ and $$ */
|
||||
TOKEN_SPECIAL, /* BYTE, WORD, DWORD, FAR, NEAR, etc */
|
||||
TOKEN_PREFIX, /* A32, O16, LOCK, REPNZ, TIMES, etc */
|
||||
TOKEN_SHL, TOKEN_SHR, /* << and >> */
|
||||
TOKEN_SDIV, TOKEN_SMOD, /* // and %% */
|
||||
TOKEN_GE, TOKEN_LE, TOKEN_NE, /* >=, <= and <> (!= is same as <>) */
|
||||
TOKEN_DBL_AND, TOKEN_DBL_OR, TOKEN_DBL_XOR, /* &&, || and ^^ */
|
||||
TOKEN_SEG, TOKEN_WRT, /* SEG and WRT */
|
||||
TOKEN_FLOAT /* floating-point constant */
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
long segment;
|
||||
long offset;
|
||||
int known;
|
||||
} loc_t;
|
||||
|
||||
/*
|
||||
* Expression-evaluator datatype. Expressions, within the
|
||||
* evaluator, are stored as an array of these beasts, terminated by
|
||||
* a record with type==0. Mostly, it's a vector type: each type
|
||||
* denotes some kind of a component, and the value denotes the
|
||||
* multiple of that component present in the expression. The
|
||||
* exception is the WRT type, whose `value' field denotes the
|
||||
* segment to which the expression is relative. These segments will
|
||||
* be segment-base types, i.e. either odd segment values or SEG_ABS
|
||||
* types. So it is still valid to assume that anything with a
|
||||
* `value' field of zero is insignificant.
|
||||
*/
|
||||
typedef struct {
|
||||
long type; /* a register, or EXPR_xxx */
|
||||
long value; /* must be >= 32 bits */
|
||||
} expr;
|
||||
|
||||
/*
|
||||
* The evaluator can also return hints about which of two registers
|
||||
* used in an expression should be the base register. See also the
|
||||
* `operand' structure.
|
||||
*/
|
||||
struct eval_hints {
|
||||
int base;
|
||||
int type;
|
||||
};
|
||||
|
||||
/*
|
||||
* The actual expression evaluator function looks like this. When
|
||||
* called, it expects the first token of its expression to already
|
||||
* be in `*tv'; if it is not, set tv->t_type to TOKEN_INVALID and
|
||||
* it will start by calling the scanner.
|
||||
*
|
||||
* If a forward reference happens during evaluation, the evaluator
|
||||
* must set `*fwref' to TRUE if `fwref' is non-NULL.
|
||||
*
|
||||
* `critical' is non-zero if the expression may not contain forward
|
||||
* references. The evaluator will report its own error if this
|
||||
* occurs; if `critical' is 1, the error will be "symbol not
|
||||
* defined before use", whereas if `critical' is 2, the error will
|
||||
* be "symbol undefined".
|
||||
*
|
||||
* If `critical' has bit 8 set (in addition to its main value: 0x101
|
||||
* and 0x102 correspond to 1 and 2) then an extended expression
|
||||
* syntax is recognised, in which relational operators such as =, <
|
||||
* and >= are accepted, as well as low-precedence logical operators
|
||||
* &&, ^^ and ||.
|
||||
*
|
||||
* If `hints' is non-NULL, it gets filled in with some hints as to
|
||||
* the base register in complex effective addresses.
|
||||
*/
|
||||
#define CRITICAL 0x100
|
||||
typedef expr *(*evalfunc) (scanner sc, void *scprivate, struct tokenval *tv,
|
||||
int *fwref, int critical, efunc error,
|
||||
struct eval_hints *hints);
|
||||
|
||||
/*
|
||||
* Special values for expr->type. ASSUMPTION MADE HERE: the number
|
||||
* of distinct register names (i.e. possible "type" fields for an
|
||||
* expr structure) does not exceed 124 (EXPR_REG_START through
|
||||
* EXPR_REG_END).
|
||||
*/
|
||||
#define EXPR_REG_START 1
|
||||
#define EXPR_REG_END 124
|
||||
#define EXPR_UNKNOWN 125L /* for forward references */
|
||||
#define EXPR_SIMPLE 126L
|
||||
#define EXPR_WRT 127L
|
||||
#define EXPR_SEGBASE 128L
|
||||
|
||||
/*
|
||||
* Preprocessors ought to look like this:
|
||||
*/
|
||||
typedef struct {
|
||||
/*
|
||||
* Called at the start of a pass; given a file name, the number
|
||||
* of the pass, an error reporting function, an evaluator
|
||||
* function, and a listing generator to talk to.
|
||||
*/
|
||||
void (*reset) (char *, int, efunc, evalfunc, ListGen *);
|
||||
|
||||
/*
|
||||
* Called to fetch a line of preprocessed source. The line
|
||||
* returned has been malloc'ed, and so should be freed after
|
||||
* use.
|
||||
*/
|
||||
char *(*getline) (void);
|
||||
|
||||
/*
|
||||
* Called at the end of a pass.
|
||||
*/
|
||||
void (*cleanup) (int);
|
||||
} Preproc;
|
||||
|
||||
/*
|
||||
* ----------------------------------------------------------------
|
||||
* Some lexical properties of the NASM source language, included
|
||||
* here because they are shared between the parser and preprocessor
|
||||
* ----------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* isidstart matches any character that may start an identifier, and isidchar
|
||||
* matches any character that may appear at places other than the start of an
|
||||
* identifier. E.g. a period may only appear at the start of an identifier
|
||||
* (for local labels), whereas a number may appear anywhere *but* at the
|
||||
* start.
|
||||
*/
|
||||
|
||||
#define isidstart(c) ( isalpha(c) || (c)=='_' || (c)=='.' || (c)=='?' \
|
||||
|| (c)=='@' )
|
||||
#define isidchar(c) ( isidstart(c) || isdigit(c) || (c)=='$' || (c)=='#' \
|
||||
|| (c)=='~' )
|
||||
|
||||
/* Ditto for numeric constants. */
|
||||
|
||||
#define isnumstart(c) ( isdigit(c) || (c)=='$' )
|
||||
#define isnumchar(c) ( isalnum(c) )
|
||||
|
||||
/* This returns the numeric value of a given 'digit'. */
|
||||
|
||||
#define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
|
||||
|
||||
/*
|
||||
* Data-type flags that get passed to listing-file routines.
|
||||
*/
|
||||
enum {
|
||||
LIST_READ, LIST_MACRO, LIST_MACRO_NOLIST, LIST_INCLUDE,
|
||||
LIST_INCBIN, LIST_TIMES
|
||||
};
|
||||
|
||||
/*
|
||||
* -----------------------------------------------------------
|
||||
* Format of the `insn' structure returned from `parser.c' and
|
||||
* passed into `assemble.c'
|
||||
* -----------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* Here we define the operand types. These are implemented as bit
|
||||
* masks, since some are subsets of others; e.g. AX in a MOV
|
||||
* instruction is a special operand type, whereas AX in other
|
||||
* contexts is just another 16-bit register. (Also, consider CL in
|
||||
* shift instructions, DX in OUT, etc.)
|
||||
*/
|
||||
|
||||
/* size, and other attributes, of the operand */
|
||||
#define BITS8 0x00000001L
|
||||
#define BITS16 0x00000002L
|
||||
#define BITS32 0x00000004L
|
||||
#define BITS64 0x00000008L /* FPU only */
|
||||
#define BITS80 0x00000010L /* FPU only */
|
||||
#define FAR 0x00000020L /* grotty: this means 16:16 or */
|
||||
/* 16:32, like in CALL/JMP */
|
||||
#define NEAR 0x00000040L
|
||||
#define SHORT 0x00000080L /* and this means what it says :) */
|
||||
|
||||
#define SIZE_MASK 0x000000FFL /* all the size attributes */
|
||||
#define NON_SIZE (~SIZE_MASK)
|
||||
|
||||
#define TO 0x00000100L /* reverse effect in FADD, FSUB &c */
|
||||
#define COLON 0x00000200L /* operand is followed by a colon */
|
||||
#define STRICT 0x00000400L /* do not optimize this operand */
|
||||
|
||||
/* type of operand: memory reference, register, etc. */
|
||||
#define MEMORY 0x00204000L
|
||||
#define REGISTER 0x00001000L /* register number in 'basereg' */
|
||||
#define IMMEDIATE 0x00002000L
|
||||
|
||||
#define REGMEM 0x00200000L /* for r/m, ie EA, operands */
|
||||
#define REGNORM 0x00201000L /* 'normal' reg, qualifies as EA */
|
||||
#define REG8 0x00201001L
|
||||
#define REG16 0x00201002L
|
||||
#define REG32 0x00201004L
|
||||
#define MMXREG 0x00201008L /* MMX registers */
|
||||
#define XMMREG 0x00201010L /* XMM Katmai reg */
|
||||
#define FPUREG 0x01000000L /* floating point stack registers */
|
||||
#define FPU0 0x01000800L /* FPU stack register zero */
|
||||
|
||||
/* special register operands: these may be treated differently */
|
||||
#define REG_SMASK 0x00070000L /* a mask for the following */
|
||||
#define REG_ACCUM 0x00211000L /* accumulator: AL, AX or EAX */
|
||||
#define REG_AL 0x00211001L /* REG_ACCUM | BITSxx */
|
||||
#define REG_AX 0x00211002L /* ditto */
|
||||
#define REG_EAX 0x00211004L /* and again */
|
||||
#define REG_COUNT 0x00221000L /* counter: CL, CX or ECX */
|
||||
#define REG_CL 0x00221001L /* REG_COUNT | BITSxx */
|
||||
#define REG_CX 0x00221002L /* ditto */
|
||||
#define REG_ECX 0x00221004L /* another one */
|
||||
#define REG_DX 0x00241002L
|
||||
#define REG_SREG 0x00081002L /* any segment register */
|
||||
#define REG_CS 0x01081002L /* CS */
|
||||
#define REG_DESS 0x02081002L /* DS, ES, SS (non-CS 86 registers) */
|
||||
#define REG_FSGS 0x04081002L /* FS, GS (386 extended registers) */
|
||||
#define REG_SEG67 0x08081002L /* Non-implemented segment registers */
|
||||
#define REG_CDT 0x00101004L /* CRn, DRn and TRn */
|
||||
#define REG_CREG 0x08101004L /* CRn */
|
||||
#define REG_DREG 0x10101004L /* DRn */
|
||||
#define REG_TREG 0x20101004L /* TRn */
|
||||
|
||||
/* special type of EA */
|
||||
#define MEM_OFFS 0x00604000L /* simple [address] offset */
|
||||
|
||||
/* special type of immediate operand */
|
||||
#define ONENESS 0x00800000L /* so UNITY == IMMEDIATE | ONENESS */
|
||||
#define UNITY 0x00802000L /* for shift/rotate instructions */
|
||||
#define BYTENESS 0x40000000L /* so SBYTE == IMMEDIATE | BYTENESS */
|
||||
#define SBYTE 0x40002000L /* for op r16/32,immediate instrs. */
|
||||
|
||||
/* Register names automatically generated from regs.dat */
|
||||
#include "regs.h"
|
||||
|
||||
enum { /* condition code names */
|
||||
C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
|
||||
C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
|
||||
C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z
|
||||
};
|
||||
|
||||
/*
|
||||
* Note that because segment registers may be used as instruction
|
||||
* prefixes, we must ensure the enumerations for prefixes and
|
||||
* register names do not overlap.
|
||||
*/
|
||||
enum { /* instruction prefixes */
|
||||
PREFIX_ENUM_START = REG_ENUM_LIMIT,
|
||||
P_A16 = PREFIX_ENUM_START, P_A32, P_LOCK, P_O16, P_O32, P_REP, P_REPE,
|
||||
P_REPNE, P_REPNZ, P_REPZ, P_TIMES
|
||||
};
|
||||
|
||||
enum { /* extended operand types */
|
||||
EOT_NOTHING, EOT_DB_STRING, EOT_DB_NUMBER
|
||||
};
|
||||
|
||||
enum { /* special EA flags */
|
||||
EAF_BYTEOFFS = 1, /* force offset part to byte size */
|
||||
EAF_WORDOFFS = 2, /* force offset part to [d]word size */
|
||||
EAF_TIMESTWO = 4 /* really do EAX*2 not EAX+EAX */
|
||||
};
|
||||
|
||||
enum { /* values for `hinttype' */
|
||||
EAH_NOHINT = 0, /* no hint at all - our discretion */
|
||||
EAH_MAKEBASE = 1, /* try to make given reg the base */
|
||||
EAH_NOTBASE = 2 /* try _not_ to make reg the base */
|
||||
};
|
||||
|
||||
typedef struct { /* operand to an instruction */
|
||||
long type; /* type of operand */
|
||||
int addr_size; /* 0 means default; 16; 32 */
|
||||
int basereg, indexreg, scale; /* registers and scale involved */
|
||||
int hintbase, hinttype; /* hint as to real base register */
|
||||
long segment; /* immediate segment, if needed */
|
||||
long offset; /* any immediate number */
|
||||
long wrt; /* segment base it's relative to */
|
||||
int eaflags; /* special EA flags */
|
||||
int opflags; /* see OPFLAG_* defines below */
|
||||
} operand;
|
||||
|
||||
#define OPFLAG_FORWARD 1 /* operand is a forward reference */
|
||||
#define OPFLAG_EXTERN 2 /* operand is an external reference */
|
||||
|
||||
typedef struct extop { /* extended operand */
|
||||
struct extop *next; /* linked list */
|
||||
long type; /* defined above */
|
||||
char *stringval; /* if it's a string, then here it is */
|
||||
int stringlen; /* ... and here's how long it is */
|
||||
long segment; /* if it's a number/address, then... */
|
||||
long offset; /* ... it's given here ... */
|
||||
long wrt; /* ... and here */
|
||||
} extop;
|
||||
|
||||
#define MAXPREFIX 4
|
||||
|
||||
typedef struct { /* an instruction itself */
|
||||
char *label; /* the label defined, or NULL */
|
||||
int prefixes[MAXPREFIX]; /* instruction prefixes, if any */
|
||||
int nprefix; /* number of entries in above */
|
||||
int opcode; /* the opcode - not just the string */
|
||||
int condition; /* the condition code, if Jcc/SETcc */
|
||||
int operands; /* how many operands? 0-3
|
||||
* (more if db et al) */
|
||||
operand oprs[3]; /* the operands, defined as above */
|
||||
extop *eops; /* extended operands */
|
||||
int eops_float; /* true if DD and floating */
|
||||
long times; /* repeat count (TIMES prefix) */
|
||||
int forw_ref; /* is there a forward reference? */
|
||||
} insn;
|
||||
|
||||
enum geninfo { GI_SWITCH };
|
||||
/*
|
||||
* ------------------------------------------------------------
|
||||
* The data structure defining an output format driver, and the
|
||||
* interfaces to the functions therein.
|
||||
* ------------------------------------------------------------
|
||||
*/
|
||||
|
||||
struct ofmt {
|
||||
/*
|
||||
* This is a short (one-liner) description of the type of
|
||||
* output generated by the driver.
|
||||
*/
|
||||
const char *fullname;
|
||||
|
||||
/*
|
||||
* This is a single keyword used to select the driver.
|
||||
*/
|
||||
const char *shortname;
|
||||
|
||||
/*
|
||||
* this is reserved for out module specific help.
|
||||
* It is set to NULL in all the out modules but is not implemented
|
||||
* in the main program
|
||||
*/
|
||||
const char *helpstring;
|
||||
|
||||
/*
|
||||
* this is a pointer to the first element of the debug information
|
||||
*/
|
||||
struct dfmt **debug_formats;
|
||||
|
||||
/*
|
||||
* and a pointer to the element that is being used
|
||||
* note: this is set to the default at compile time and changed if the
|
||||
* -F option is selected. If developing a set of new debug formats for
|
||||
* an output format, be sure to set this to whatever default you want
|
||||
*
|
||||
*/
|
||||
struct dfmt *current_dfmt;
|
||||
|
||||
/*
|
||||
* This, if non-NULL, is a NULL-terminated list of `char *'s
|
||||
* pointing to extra standard macros supplied by the object
|
||||
* format (e.g. a sensible initial default value of __SECT__,
|
||||
* and user-level equivalents for any format-specific
|
||||
* directives).
|
||||
*/
|
||||
const char **stdmac;
|
||||
|
||||
/*
|
||||
* This procedure is called at the start of an output session.
|
||||
* It tells the output format what file it will be writing to,
|
||||
* what routine to report errors through, and how to interface
|
||||
* to the label manager and expression evaluator if necessary.
|
||||
* It also gives it a chance to do other initialisation.
|
||||
*/
|
||||
void (*init) (FILE *fp, efunc error, ldfunc ldef, evalfunc eval);
|
||||
|
||||
/*
|
||||
* This procedure is called to pass generic information to the
|
||||
* object file. The first parameter gives the information type
|
||||
* (currently only command line switches)
|
||||
* and the second parameter gives the value. This function returns
|
||||
* 1 if recognized, 0 if unrecognized
|
||||
*/
|
||||
int (*setinfo)(enum geninfo type, char **string);
|
||||
|
||||
/*
|
||||
* This procedure is called by assemble() to write actual
|
||||
* generated code or data to the object file. Typically it
|
||||
* doesn't have to actually _write_ it, just store it for
|
||||
* later.
|
||||
*
|
||||
* The `type' argument specifies the type of output data, and
|
||||
* usually the size as well: its contents are described below.
|
||||
*/
|
||||
void (*output) (long segto, const void *data, unsigned long type,
|
||||
long segment, long wrt);
|
||||
|
||||
/*
|
||||
* This procedure is called once for every symbol defined in
|
||||
* the module being assembled. It gives the name and value of
|
||||
* the symbol, in NASM's terms, and indicates whether it has
|
||||
* been declared to be global. Note that the parameter "name",
|
||||
* when passed, will point to a piece of static storage
|
||||
* allocated inside the label manager - it's safe to keep using
|
||||
* that pointer, because the label manager doesn't clean up
|
||||
* until after the output driver has.
|
||||
*
|
||||
* Values of `is_global' are: 0 means the symbol is local; 1
|
||||
* means the symbol is global; 2 means the symbol is common (in
|
||||
* which case `offset' holds the _size_ of the variable).
|
||||
* Anything else is available for the output driver to use
|
||||
* internally.
|
||||
*
|
||||
* This routine explicitly _is_ allowed to call the label
|
||||
* manager to define further symbols, if it wants to, even
|
||||
* though it's been called _from_ the label manager. That much
|
||||
* re-entrancy is guaranteed in the label manager. However, the
|
||||
* label manager will in turn call this routine, so it should
|
||||
* be prepared to be re-entrant itself.
|
||||
*
|
||||
* The `special' parameter contains special information passed
|
||||
* through from the command that defined the label: it may have
|
||||
* been an EXTERN, a COMMON or a GLOBAL. The distinction should
|
||||
* be obvious to the output format from the other parameters.
|
||||
*/
|
||||
void (*symdef) (char *name, long segment, long offset, int is_global,
|
||||
char *special);
|
||||
|
||||
/*
|
||||
* This procedure is called when the source code requests a
|
||||
* segment change. It should return the corresponding segment
|
||||
* _number_ for the name, or NO_SEG if the name is not a valid
|
||||
* segment name.
|
||||
*
|
||||
* It may also be called with NULL, in which case it is to
|
||||
* return the _default_ section number for starting assembly in.
|
||||
*
|
||||
* It is allowed to modify the string it is given a pointer to.
|
||||
*
|
||||
* It is also allowed to specify a default instruction size for
|
||||
* the segment, by setting `*bits' to 16 or 32. Or, if it
|
||||
* doesn't wish to define a default, it can leave `bits' alone.
|
||||
*/
|
||||
long (*section) (char *name, int pass, int *bits);
|
||||
|
||||
/*
|
||||
* This procedure is called to modify the segment base values
|
||||
* returned from the SEG operator. It is given a segment base
|
||||
* value (i.e. a segment value with the low bit set), and is
|
||||
* required to produce in return a segment value which may be
|
||||
* different. It can map segment bases to absolute numbers by
|
||||
* means of returning SEG_ABS types.
|
||||
*
|
||||
* It should return NO_SEG if the segment base cannot be
|
||||
* determined; the evaluator (which calls this routine) is
|
||||
* responsible for throwing an error condition if that occurs
|
||||
* in pass two or in a critical expression.
|
||||
*/
|
||||
long (*segbase) (long segment);
|
||||
|
||||
/*
|
||||
* This procedure is called to allow the output driver to
|
||||
* process its own specific directives. When called, it has the
|
||||
* directive word in `directive' and the parameter string in
|
||||
* `value'. It is called in both assembly passes, and `pass'
|
||||
* will be either 1 or 2.
|
||||
*
|
||||
* This procedure should return zero if it does not _recognise_
|
||||
* the directive, so that the main program can report an error.
|
||||
* If it recognises the directive but then has its own errors,
|
||||
* it should report them itself and then return non-zero. It
|
||||
* should also return non-zero if it correctly processes the
|
||||
* directive.
|
||||
*/
|
||||
int (*directive) (char *directive, char *value, int pass);
|
||||
|
||||
/*
|
||||
* This procedure is called before anything else - even before
|
||||
* the "init" routine - and is passed the name of the input
|
||||
* file from which this output file is being generated. It
|
||||
* should return its preferred name for the output file in
|
||||
* `outname', if outname[0] is not '\0', and do nothing to
|
||||
* `outname' otherwise. Since it is called before the driver is
|
||||
* properly initialised, it has to be passed its error handler
|
||||
* separately.
|
||||
*
|
||||
* This procedure may also take its own copy of the input file
|
||||
* name for use in writing the output file: it is _guaranteed_
|
||||
* that it will be called before the "init" routine.
|
||||
*
|
||||
* The parameter `outname' points to an area of storage
|
||||
* guaranteed to be at least FILENAME_MAX in size.
|
||||
*/
|
||||
void (*filename) (char *inname, char *outname, efunc error);
|
||||
|
||||
/*
|
||||
* This procedure is called after assembly finishes, to allow
|
||||
* the output driver to clean itself up and free its memory.
|
||||
* Typically, it will also be the point at which the object
|
||||
* file actually gets _written_.
|
||||
*
|
||||
* One thing the cleanup routine should always do is to close
|
||||
* the output file pointer.
|
||||
*/
|
||||
void (*cleanup) (int debuginfo);
|
||||
};
|
||||
|
||||
/*
|
||||
* values for the `type' parameter to an output function. Each one
|
||||
* must have the actual number of _bytes_ added to it.
|
||||
*
|
||||
* Exceptions are OUT_RELxADR, which denote an x-byte relocation
|
||||
* which will be a relative jump. For this we need to know the
|
||||
* distance in bytes from the start of the relocated record until
|
||||
* the end of the containing instruction. _This_ is what is stored
|
||||
* in the size part of the parameter, in this case.
|
||||
*
|
||||
* Also OUT_RESERVE denotes reservation of N bytes of BSS space,
|
||||
* and the contents of the "data" parameter is irrelevant.
|
||||
*
|
||||
* The "data" parameter for the output function points to a "long",
|
||||
* containing the address in question, unless the type is
|
||||
* OUT_RAWDATA, in which case it points to an "unsigned char"
|
||||
* array.
|
||||
*/
|
||||
#define OUT_RAWDATA 0x00000000UL
|
||||
#define OUT_ADDRESS 0x10000000UL
|
||||
#define OUT_REL2ADR 0x20000000UL
|
||||
#define OUT_REL4ADR 0x30000000UL
|
||||
#define OUT_RESERVE 0x40000000UL
|
||||
#define OUT_TYPMASK 0xF0000000UL
|
||||
#define OUT_SIZMASK 0x0FFFFFFFUL
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------------
|
||||
* The data structure defining a debug format driver, and the
|
||||
* interfaces to the functions therein.
|
||||
* ------------------------------------------------------------
|
||||
*/
|
||||
|
||||
struct dfmt {
|
||||
|
||||
/*
|
||||
* This is a short (one-liner) description of the type of
|
||||
* output generated by the driver.
|
||||
*/
|
||||
const char *fullname;
|
||||
|
||||
/*
|
||||
* This is a single keyword used to select the driver.
|
||||
*/
|
||||
const char *shortname;
|
||||
|
||||
|
||||
/*
|
||||
* init - called initially to set up local pointer to object format,
|
||||
* void pointer to implementation defined data, file pointer (which
|
||||
* probably won't be used, but who knows?), and error function.
|
||||
*/
|
||||
void (*init) (struct ofmt * of, void * id, FILE * fp, efunc error);
|
||||
|
||||
/*
|
||||
* linenum - called any time there is output with a change of
|
||||
* line number or file.
|
||||
*/
|
||||
void (*linenum) (const char * filename, long linenumber, long segto);
|
||||
|
||||
/*
|
||||
* debug_deflabel - called whenever a label is defined. Parameters
|
||||
* are the same as to 'symdef()' in the output format. This function
|
||||
* would be called before the output format version.
|
||||
*/
|
||||
|
||||
void (*debug_deflabel) (char * name, long segment, long offset,
|
||||
int is_global, char * special);
|
||||
/*
|
||||
* debug_directive - called whenever a DEBUG directive other than 'LINE'
|
||||
* is encountered. 'directive' contains the first parameter to the
|
||||
* DEBUG directive, and params contains the rest. For example,
|
||||
* 'DEBUG VAR _somevar:int' would translate to a call to this
|
||||
* function with 'directive' equal to "VAR" and 'params' equal to
|
||||
* "_somevar:int".
|
||||
*/
|
||||
void (*debug_directive) (const char * directive, const char * params);
|
||||
|
||||
/*
|
||||
* typevalue - called whenever the assembler wishes to register a type
|
||||
* for the last defined label. This routine MUST detect if a type was
|
||||
* already registered and not re-register it.
|
||||
*/
|
||||
void (*debug_typevalue) (long type);
|
||||
|
||||
/*
|
||||
* debug_output - called whenever output is required
|
||||
* 'type' is the type of info required, and this is format-specific
|
||||
*/
|
||||
void (*debug_output) (int type, void *param);
|
||||
|
||||
/*
|
||||
* cleanup - called after processing of file is complete
|
||||
*/
|
||||
void (*cleanup) (void);
|
||||
|
||||
};
|
||||
/*
|
||||
* The type definition macros
|
||||
* for debugging
|
||||
*
|
||||
* low 3 bits: reserved
|
||||
* next 5 bits: type
|
||||
* next 24 bits: number of elements for arrays (0 for labels)
|
||||
*/
|
||||
|
||||
#define TY_UNKNOWN 0x00
|
||||
#define TY_LABEL 0x08
|
||||
#define TY_BYTE 0x10
|
||||
#define TY_WORD 0x18
|
||||
#define TY_DWORD 0x20
|
||||
#define TY_FLOAT 0x28
|
||||
#define TY_QWORD 0x30
|
||||
#define TY_TBYTE 0x38
|
||||
#define TY_COMMON 0xE0
|
||||
#define TY_SEG 0xE8
|
||||
#define TY_EXTERN 0xF0
|
||||
#define TY_EQU 0xF8
|
||||
|
||||
#define TYM_TYPE(x) ((x) & 0xF8)
|
||||
#define TYM_ELEMENTS(x) (((x) & 0xFFFFFF00) >> 8)
|
||||
|
||||
#define TYS_ELEMENTS(x) ((x) << 8)
|
||||
/*
|
||||
* -----
|
||||
* Other
|
||||
* -----
|
||||
*/
|
||||
|
||||
/*
|
||||
* This is a useful #define which I keep meaning to use more often:
|
||||
* the number of elements of a statically defined array.
|
||||
*/
|
||||
|
||||
#define elements(x) ( sizeof(x) / sizeof(*(x)) )
|
||||
|
||||
extern int tasm_compatible_mode;
|
||||
|
||||
/*
|
||||
* This declaration passes the "pass" number to all other modules
|
||||
* "pass0" assumes the values: 0, 0, ..., 0, 1, 2
|
||||
* where 0 = optimizing pass
|
||||
* 1 = pass 1
|
||||
* 2 = pass 2
|
||||
*/
|
||||
|
||||
extern int pass0; /* this is globally known */
|
||||
extern int optimizing;
|
||||
|
||||
#endif
|
||||
1116
src/preprocs/nasm/nasmlib.c
Normal file
1116
src/preprocs/nasm/nasmlib.c
Normal file
File diff suppressed because it is too large
Load diff
258
src/preprocs/nasm/nasmlib.h
Normal file
258
src/preprocs/nasm/nasmlib.h
Normal file
|
|
@ -0,0 +1,258 @@
|
|||
/* nasmlib.h header file for nasmlib.c
|
||||
*
|
||||
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
||||
* Julian Hall. All rights reserved. The software is
|
||||
* redistributable under the licence given in the file "Licence"
|
||||
* distributed in the NASM archive.
|
||||
*/
|
||||
|
||||
#ifndef NASM_NASMLIB_H
|
||||
#define NASM_NASMLIB_H
|
||||
|
||||
/*
|
||||
* If this is defined, the wrappers around malloc et al will
|
||||
* transform into logging variants, which will cause NASM to create
|
||||
* a file called `malloc.log' when run, and spew details of all its
|
||||
* memory management into that. That can then be analysed to detect
|
||||
* memory leaks and potentially other problems too.
|
||||
*/
|
||||
/* #define LOGALLOC */
|
||||
|
||||
/*
|
||||
* Wrappers around malloc, realloc and free. nasm_malloc will
|
||||
* fatal-error and die rather than return NULL; nasm_realloc will
|
||||
* do likewise, and will also guarantee to work right on being
|
||||
* passed a NULL pointer; nasm_free will do nothing if it is passed
|
||||
* a NULL pointer.
|
||||
*/
|
||||
#ifdef NASM_NASM_H /* need efunc defined for this */
|
||||
void nasm_set_malloc_error (efunc);
|
||||
#ifndef LOGALLOC
|
||||
void *nasm_malloc (size_t);
|
||||
void *nasm_realloc (void *, size_t);
|
||||
void nasm_free (void *);
|
||||
char *nasm_strdup (const char *);
|
||||
char *nasm_strndup (char *, size_t);
|
||||
#else
|
||||
void *nasm_malloc_log (char *, int, size_t);
|
||||
void *nasm_realloc_log (char *, int, void *, size_t);
|
||||
void nasm_free_log (char *, int, void *);
|
||||
char *nasm_strdup_log (char *, int, const char *);
|
||||
char *nasm_strndup_log (char *, int, char *, size_t);
|
||||
#define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
|
||||
#define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
|
||||
#define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
|
||||
#define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
|
||||
#define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ANSI doesn't guarantee the presence of `stricmp' or
|
||||
* `strcasecmp'.
|
||||
*/
|
||||
#if defined(stricmp) || defined(strcasecmp)
|
||||
#if defined(stricmp)
|
||||
#define nasm_stricmp stricmp
|
||||
#else
|
||||
#define nasm_stricmp strcasecmp
|
||||
#endif
|
||||
#else
|
||||
int nasm_stricmp (const char *, const char *);
|
||||
#endif
|
||||
|
||||
#if defined(strnicmp) || defined(strncasecmp)
|
||||
#if defined(strnicmp)
|
||||
#define nasm_strnicmp strnicmp
|
||||
#else
|
||||
#define nasm_strnicmp strncasecmp
|
||||
#endif
|
||||
#else
|
||||
int nasm_strnicmp (const char *, const char *, int);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Convert a string into a number, using NASM number rules. Sets
|
||||
* `*error' to TRUE if an error occurs, and FALSE otherwise.
|
||||
*/
|
||||
long readnum(char *str, int *error);
|
||||
|
||||
/*
|
||||
* Convert a character constant into a number. Sets
|
||||
* `*warn' to TRUE if an overflow occurs, and FALSE otherwise.
|
||||
* str points to and length covers the middle of the string,
|
||||
* without the quotes.
|
||||
*/
|
||||
long readstrnum(char *str, int length, int *warn);
|
||||
|
||||
/*
|
||||
* seg_init: Initialise the segment-number allocator.
|
||||
* seg_alloc: allocate a hitherto unused segment number.
|
||||
*/
|
||||
void seg_init(void);
|
||||
long seg_alloc(void);
|
||||
|
||||
/*
|
||||
* many output formats will be able to make use of this: a standard
|
||||
* function to add an extension to the name of the input file
|
||||
*/
|
||||
#ifdef NASM_NASM_H
|
||||
void standard_extension (char *inname, char *outname, char *extension,
|
||||
efunc error);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* some handy macros that will probably be of use in more than one
|
||||
* output format: convert integers into little-endian byte packed
|
||||
* format in memory
|
||||
*/
|
||||
|
||||
#define WRITELONG(p,v) \
|
||||
do { \
|
||||
*(p)++ = (v) & 0xFF; \
|
||||
*(p)++ = ((v) >> 8) & 0xFF; \
|
||||
*(p)++ = ((v) >> 16) & 0xFF; \
|
||||
*(p)++ = ((v) >> 24) & 0xFF; \
|
||||
} while (0)
|
||||
|
||||
#define WRITESHORT(p,v) \
|
||||
do { \
|
||||
*(p)++ = (v) & 0xFF; \
|
||||
*(p)++ = ((v) >> 8) & 0xFF; \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* and routines to do the same thing to a file
|
||||
*/
|
||||
void fwriteshort (int data, FILE *fp);
|
||||
void fwritelong (long data, FILE *fp);
|
||||
|
||||
/*
|
||||
* Routines to manage a dynamic random access array of longs which
|
||||
* may grow in size to be more than the largest single malloc'able
|
||||
* chunk.
|
||||
*/
|
||||
|
||||
#define RAA_BLKSIZE 4096 /* this many longs allocated at once */
|
||||
#define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
|
||||
|
||||
typedef struct RAA RAA;
|
||||
typedef union RAA_UNION RAA_UNION;
|
||||
typedef struct RAA_LEAF RAA_LEAF;
|
||||
typedef struct RAA_BRANCH RAA_BRANCH;
|
||||
|
||||
struct RAA {
|
||||
/*
|
||||
* Number of layers below this one to get to the real data. 0
|
||||
* means this structure is a leaf, holding RAA_BLKSIZE real
|
||||
* data items; 1 and above mean it's a branch, holding
|
||||
* RAA_LAYERSIZE pointers to the next level branch or leaf
|
||||
* structures.
|
||||
*/
|
||||
int layers;
|
||||
/*
|
||||
* Number of real data items spanned by one position in the
|
||||
* `data' array at this level. This number is 1, trivially, for
|
||||
* a leaf (level 0): for a level 1 branch it should be
|
||||
* RAA_BLKSIZE, and for a level 2 branch it's
|
||||
* RAA_LAYERSIZE*RAA_BLKSIZE.
|
||||
*/
|
||||
long stepsize;
|
||||
union RAA_UNION {
|
||||
struct RAA_LEAF {
|
||||
long data[RAA_BLKSIZE];
|
||||
} l;
|
||||
struct RAA_BRANCH {
|
||||
struct RAA *data[RAA_LAYERSIZE];
|
||||
} b;
|
||||
} u;
|
||||
};
|
||||
|
||||
|
||||
struct RAA *raa_init (void);
|
||||
void raa_free (struct RAA *);
|
||||
long raa_read (struct RAA *, long);
|
||||
struct RAA *raa_write (struct RAA *r, long posn, long value);
|
||||
|
||||
/*
|
||||
* Routines to manage a dynamic sequential-access array, under the
|
||||
* same restriction on maximum mallocable block. This array may be
|
||||
* written to in two ways: a contiguous chunk can be reserved of a
|
||||
* given size, and a pointer returned, or single-byte data may be
|
||||
* written. The array can also be read back in the same two ways:
|
||||
* as a series of big byte-data blocks or as a list of structures
|
||||
* of a given size.
|
||||
*/
|
||||
|
||||
struct SAA {
|
||||
/*
|
||||
* members `end' and `elem_len' are only valid in first link in
|
||||
* list; `rptr' and `rpos' are used for reading
|
||||
*/
|
||||
struct SAA *next, *end, *rptr;
|
||||
long elem_len, length, posn, start, rpos;
|
||||
char *data;
|
||||
};
|
||||
|
||||
struct SAA *saa_init (long elem_len); /* 1 == byte */
|
||||
void saa_free (struct SAA *);
|
||||
void *saa_wstruct (struct SAA *); /* return a structure of elem_len */
|
||||
void saa_wbytes (struct SAA *, const void *, long); /* write arbitrary bytes */
|
||||
void saa_rewind (struct SAA *); /* for reading from beginning */
|
||||
void *saa_rstruct (struct SAA *); /* return NULL on EOA */
|
||||
void *saa_rbytes (struct SAA *, long *); /* return 0 on EOA */
|
||||
void saa_rnbytes (struct SAA *, void *, long); /* read a given no. of bytes */
|
||||
void saa_fread (struct SAA *s, long posn, void *p, long len); /* fixup */
|
||||
void saa_fwrite (struct SAA *s, long posn, void *p, long len); /* fixup */
|
||||
void saa_fpwrite (struct SAA *, FILE *);
|
||||
|
||||
#ifdef NASM_NASM_H
|
||||
/*
|
||||
* Standard scanner.
|
||||
*/
|
||||
extern char *stdscan_bufptr;
|
||||
void stdscan_reset(void);
|
||||
int stdscan (void *private_data, struct tokenval *tv);
|
||||
#endif
|
||||
|
||||
#ifdef NASM_NASM_H
|
||||
/*
|
||||
* Library routines to manipulate expression data types.
|
||||
*/
|
||||
int is_reloc(expr *);
|
||||
int is_simple(expr *);
|
||||
int is_really_simple (expr *);
|
||||
int is_unknown(expr *);
|
||||
int is_just_unknown(expr *);
|
||||
long reloc_value(expr *);
|
||||
long reloc_seg(expr *);
|
||||
long reloc_wrt(expr *);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Binary search routine. Returns index into `array' of an entry
|
||||
* matching `string', or <0 if no match. `array' is taken to
|
||||
* contain `size' elements.
|
||||
*/
|
||||
int bsi (char *string, const char **array, int size);
|
||||
|
||||
|
||||
char *src_set_fname(char *newname);
|
||||
long src_set_linnum(long newline);
|
||||
long src_get_linnum(void);
|
||||
/*
|
||||
* src_get may be used if you simply want to know the source file and line.
|
||||
* It is also used if you maintain private status about the source location
|
||||
* It return 0 if the information was the same as the last time you
|
||||
* checked, -1 if the name changed and (new-old) if just the line changed.
|
||||
*/
|
||||
int src_get(long *xline, char **xname);
|
||||
|
||||
void nasm_quote(char **str);
|
||||
char *nasm_strcat(char *one, char *two);
|
||||
void nasmlib_cleanup(void);
|
||||
|
||||
void null_debug_routine(const char *directive, const char *params);
|
||||
extern struct dfmt null_debug_form;
|
||||
extern struct dfmt *null_debug_arr[2];
|
||||
#endif
|
||||
110
src/preprocs/nasm/standard.mac
Normal file
110
src/preprocs/nasm/standard.mac
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
; Standard macro set for NASM -*- nasm -*-
|
||||
|
||||
; Macros to make NASM ignore some TASM directives before the first include
|
||||
; directive.
|
||||
|
||||
%idefine IDEAL
|
||||
%idefine JUMPS
|
||||
%idefine P386
|
||||
%idefine P486
|
||||
%idefine P586
|
||||
%idefine END
|
||||
|
||||
; This is a magic token which indicates the end of the TASM macros
|
||||
*END*TASM*MACROS*
|
||||
|
||||
; Note that although some user-level forms of directives are defined
|
||||
; here, not all of them are: the user-level form of a format-specific
|
||||
; directive should be defined in the module for that directive.
|
||||
|
||||
; These two need to be defined, though the actual definitions will
|
||||
; be constantly updated during preprocessing.
|
||||
%define __FILE__
|
||||
%define __LINE__
|
||||
|
||||
%define __SECT__ ; it ought to be defined, even if as nothing
|
||||
|
||||
%imacro section 1+.nolist
|
||||
%define __SECT__ [section %1]
|
||||
__SECT__
|
||||
%endmacro
|
||||
%imacro segment 1+.nolist
|
||||
%define __SECT__ [segment %1]
|
||||
__SECT__
|
||||
%endmacro
|
||||
|
||||
%imacro absolute 1+.nolist
|
||||
%define __SECT__ [absolute %1]
|
||||
__SECT__
|
||||
%endmacro
|
||||
|
||||
%imacro struc 1.nolist
|
||||
%push struc
|
||||
%define %$strucname %1
|
||||
[absolute 0]
|
||||
%$strucname: ; allow definition of `.member' to work sanely
|
||||
%endmacro
|
||||
%imacro endstruc 0.nolist
|
||||
%{$strucname}_size:
|
||||
%pop
|
||||
__SECT__
|
||||
%endmacro
|
||||
|
||||
%imacro istruc 1.nolist
|
||||
%push istruc
|
||||
%define %$strucname %1
|
||||
%$strucstart:
|
||||
%endmacro
|
||||
%imacro at 1-2+.nolist
|
||||
times %1-($-%$strucstart) db 0
|
||||
%2
|
||||
%endmacro
|
||||
%imacro iend 0.nolist
|
||||
times %{$strucname}_size-($-%$strucstart) db 0
|
||||
%pop
|
||||
%endmacro
|
||||
|
||||
%imacro align 1-2+.nolist nop
|
||||
times ($$-$) & ((%1)-1) %2
|
||||
%endmacro
|
||||
%imacro alignb 1-2+.nolist resb 1
|
||||
times ($$-$) & ((%1)-1) %2
|
||||
%endmacro
|
||||
|
||||
%imacro extern 1-*.nolist
|
||||
%rep %0
|
||||
[extern %1]
|
||||
%rotate 1
|
||||
%endrep
|
||||
%endmacro
|
||||
|
||||
%imacro bits 1+.nolist
|
||||
[bits %1]
|
||||
%endmacro
|
||||
|
||||
%imacro use16 0.nolist
|
||||
[bits 16]
|
||||
%endmacro
|
||||
%imacro use32 0.nolist
|
||||
[bits 32]
|
||||
%endmacro
|
||||
|
||||
%imacro global 1-*.nolist
|
||||
%rep %0
|
||||
[global %1]
|
||||
%rotate 1
|
||||
%endrep
|
||||
%endmacro
|
||||
|
||||
%imacro common 1-*.nolist
|
||||
%rep %0
|
||||
[common %1]
|
||||
%rotate 1
|
||||
%endrep
|
||||
%endmacro
|
||||
|
||||
%imacro cpu 1+.nolist
|
||||
[cpu %1]
|
||||
%endmacro
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue