tinycc/tccasm.c

1278 lines
35 KiB
C
Raw Normal View History

2003-01-06 23:21:42 +03:00
/*
* GAS like assembler for TCC
*
2004-10-28 01:38:03 +04:00
* Copyright (c) 2001-2004 Fabrice Bellard
2003-01-06 23:21:42 +03:00
*
2003-05-24 18:11:17 +04:00
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
2003-01-06 23:21:42 +03:00
*
2003-05-24 18:11:17 +04:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2003-01-06 23:21:42 +03:00
*
2003-05-24 18:11:17 +04:00
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2003-01-06 23:21:42 +03:00
*/
#include "tcc.h"
#ifdef CONFIG_TCC_ASM
ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n)
2003-01-06 23:21:42 +03:00
{
char buf[64];
TokenSym *ts;
snprintf(buf, sizeof(buf), "L..%u", n);
ts = tok_alloc(buf, strlen(buf));
return ts->tok;
}
static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global);
static Sym* asm_new_label(TCCState *s1, int label, int is_local);
static Sym* asm_new_label1(TCCState *s1, int label, int is_local, int sh_num, int value);
static Sym *asm_label_find(int v)
{
Sym *sym = sym_find(v);
while (sym && sym->sym_scope)
sym = sym->prev_tok;
return sym;
}
static Sym *asm_label_push(int v)
{
/* We always add VT_EXTERN, for sym definition that's tentative
(for .set, removed for real defs), for mere references it's correct
as is. */
Sym *sym = global_identifier_push(v, VT_ASM | VT_EXTERN | VT_STATIC, 0);
sym->r = VT_CONST | VT_SYM;
return sym;
}
/* Return a symbol we can use inside the assembler, having name NAME.
Symbols from asm and C source share a namespace. If we generate
an asm symbol it's also a (file-global) C symbol, but it's
either not accessible by name (like "L.123"), or its type information
is such that it's not usable without a proper C declaration.
Sometimes we need symbols accessible by name from asm, which
are anonymous in C, in this case CSYM can be used to transfer
all information from that symbol to the (possibly newly created)
asm symbol. */
ST_FUNC Sym* get_asm_sym(int name, Sym *csym)
{
Sym *sym = asm_label_find(name);
if (!sym) {
sym = asm_label_push(name);
if (csym)
sym->c = csym->c;
}
return sym;
}
static Sym* asm_section_sym(TCCState *s1, Section *sec)
{
char buf[100];
int label = tok_alloc(buf,
snprintf(buf, sizeof buf, "L.%s", sec->name)
)->tok;
Sym *sym = asm_label_find(label);
return sym ? sym : asm_new_label1(s1, label, 1, sec->sh_num, 0);
}
2003-01-06 23:21:42 +03:00
/* We do not use the C expression parser to handle symbols. Maybe the
C expression parser could be tweaked to do so. */
static void asm_expr_unary(TCCState *s1, ExprValue *pe)
{
Sym *sym;
int op, label;
uint64_t n;
2003-01-06 23:21:42 +03:00
const char *p;
switch(tok) {
case TOK_PPNUM:
p = tokc.str.data;
n = strtoull(p, (char **)&p, 0);
2003-01-06 23:21:42 +03:00
if (*p == 'b' || *p == 'f') {
/* backward or forward label */
label = asm_get_local_label_name(s1, n);
sym = asm_label_find(label);
2003-01-06 23:21:42 +03:00
if (*p == 'b') {
/* backward : find the last corresponding defined label */
if (sym && (!sym->c || elfsym(sym)->st_shndx == SHN_UNDEF))
2003-01-06 23:21:42 +03:00
sym = sym->prev_tok;
if (!sym)
tcc_error("local label '%d' not found backward", n);
2003-01-06 23:21:42 +03:00
} else {
/* forward */
if (!sym || (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF)) {
2003-01-06 23:21:42 +03:00
/* if the last label is defined, then define a new one */
sym = asm_label_push(label);
2003-01-06 23:21:42 +03:00
}
}
pe->v = 0;
pe->sym = sym;
pe->pcrel = 0;
2003-01-06 23:21:42 +03:00
} else if (*p == '\0') {
pe->v = n;
pe->sym = NULL;
pe->pcrel = 0;
2003-01-06 23:21:42 +03:00
} else {
tcc_error("invalid number syntax");
2003-01-06 23:21:42 +03:00
}
next();
break;
case '+':
next();
asm_expr_unary(s1, pe);
break;
case '-':
case '~':
op = tok;
next();
asm_expr_unary(s1, pe);
if (pe->sym)
tcc_error("invalid operation with label");
2003-01-06 23:21:42 +03:00
if (op == '-')
pe->v = -pe->v;
else
pe->v = ~pe->v;
break;
case TOK_CCHAR:
case TOK_LCHAR:
pe->v = tokc.i;
pe->sym = NULL;
pe->pcrel = 0;
next();
break;
case '(':
next();
asm_expr(s1, pe);
skip(')');
break;
case '.':
pe->v = ind;
pe->sym = asm_section_sym(s1, cur_text_section);
pe->pcrel = 0;
next();
break;
2003-01-06 23:21:42 +03:00
default:
if (tok >= TOK_IDENT) {
ElfSym *esym;
2003-01-06 23:21:42 +03:00
/* label case : if the label was not found, add one */
sym = get_asm_sym(tok, NULL);
esym = elfsym(sym);
if (esym && esym->st_shndx == SHN_ABS) {
/* if absolute symbol, no need to put a symbol value */
pe->v = esym->st_value;
pe->sym = NULL;
pe->pcrel = 0;
} else {
pe->v = 0;
pe->sym = sym;
pe->pcrel = 0;
}
2003-01-06 23:21:42 +03:00
next();
} else {
tcc_error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
2003-01-06 23:21:42 +03:00
}
break;
}
}
2003-01-06 23:21:42 +03:00
static void asm_expr_prod(TCCState *s1, ExprValue *pe)
{
int op;
ExprValue e2;
asm_expr_unary(s1, pe);
for(;;) {
op = tok;
if (op != '*' && op != '/' && op != '%' &&
2003-01-06 23:21:42 +03:00
op != TOK_SHL && op != TOK_SAR)
break;
next();
asm_expr_unary(s1, &e2);
if (pe->sym || e2.sym)
tcc_error("invalid operation with label");
2003-01-06 23:21:42 +03:00
switch(op) {
case '*':
pe->v *= e2.v;
break;
case '/':
2003-01-06 23:21:42 +03:00
if (e2.v == 0) {
div_error:
tcc_error("division by zero");
2003-01-06 23:21:42 +03:00
}
pe->v /= e2.v;
break;
case '%':
2003-01-06 23:21:42 +03:00
if (e2.v == 0)
goto div_error;
pe->v %= e2.v;
break;
case TOK_SHL:
pe->v <<= e2.v;
break;
default:
case TOK_SAR:
pe->v >>= e2.v;
break;
}
}
}
static void asm_expr_logic(TCCState *s1, ExprValue *pe)
{
int op;
ExprValue e2;
asm_expr_prod(s1, pe);
for(;;) {
op = tok;
if (op != '&' && op != '|' && op != '^')
break;
next();
asm_expr_prod(s1, &e2);
if (pe->sym || e2.sym)
tcc_error("invalid operation with label");
2003-01-06 23:21:42 +03:00
switch(op) {
case '&':
pe->v &= e2.v;
break;
case '|':
2003-01-06 23:21:42 +03:00
pe->v |= e2.v;
break;
default:
case '^':
pe->v ^= e2.v;
break;
}
}
}
static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
{
int op;
ExprValue e2;
asm_expr_logic(s1, pe);
for(;;) {
op = tok;
if (op != '+' && op != '-')
break;
next();
asm_expr_logic(s1, &e2);
if (op == '+') {
if (pe->sym != NULL && e2.sym != NULL)
goto cannot_relocate;
pe->v += e2.v;
if (pe->sym == NULL && e2.sym != NULL)
pe->sym = e2.sym;
} else {
pe->v -= e2.v;
/* NOTE: we are less powerful than gas in that case
because we store only one symbol in the expression */
if (!e2.sym) {
/* OK */
} else if (pe->sym == e2.sym) {
/* OK */
pe->sym = NULL; /* same symbols can be subtracted to NULL */
} else {
ElfSym *esym1, *esym2;
esym1 = elfsym(pe->sym);
esym2 = elfsym(e2.sym);
if (esym1 && esym1->st_shndx == esym2->st_shndx
&& esym1->st_shndx != SHN_UNDEF) {
/* we also accept defined symbols in the same section */
pe->v += esym1->st_value - esym2->st_value;
pe->sym = NULL;
} else if (esym2->st_shndx == cur_text_section->sh_num) {
/* When subtracting a defined symbol in current section
this actually makes the value PC-relative. */
pe->v -= esym2->st_value - ind - 4;
pe->pcrel = 1;
e2.sym = NULL;
} else {
cannot_relocate:
tcc_error("invalid operation with label");
}
}
2003-01-06 23:21:42 +03:00
}
}
}
static inline void asm_expr_cmp(TCCState *s1, ExprValue *pe)
2003-01-06 23:21:42 +03:00
{
int op;
ExprValue e2;
2003-01-06 23:21:42 +03:00
asm_expr_sum(s1, pe);
for(;;) {
op = tok;
if (op != TOK_EQ && op != TOK_NE
&& (op > TOK_GT || op < TOK_ULE))
break;
next();
asm_expr_sum(s1, &e2);
if (pe->sym || e2.sym)
tcc_error("invalid operation with label");
switch(op) {
case TOK_EQ:
pe->v = pe->v == e2.v;
break;
case TOK_NE:
pe->v = pe->v != e2.v;
break;
case TOK_LT:
pe->v = (int64_t)pe->v < (int64_t)e2.v;
break;
case TOK_GE:
pe->v = (int64_t)pe->v >= (int64_t)e2.v;
break;
case TOK_LE:
pe->v = (int64_t)pe->v <= (int64_t)e2.v;
break;
case TOK_GT:
pe->v = (int64_t)pe->v > (int64_t)e2.v;
break;
default:
break;
}
/* GAS compare results are -1/0 not 1/0. */
pe->v = -(int64_t)pe->v;
}
}
ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
{
asm_expr_cmp(s1, pe);
2003-01-06 23:21:42 +03:00
}
ST_FUNC int asm_int_expr(TCCState *s1)
2003-01-06 23:21:42 +03:00
{
ExprValue e;
asm_expr(s1, &e);
if (e.sym)
expect("constant");
return e.v;
}
static Sym* asm_new_label1(TCCState *s1, int label, int is_local,
int sh_num, int value)
2003-01-06 23:21:42 +03:00
{
Sym *sym;
ElfSym *esym;
2003-01-06 23:21:42 +03:00
sym = asm_label_find(label);
2003-01-06 23:21:42 +03:00
if (sym) {
esym = elfsym(sym);
/* A VT_EXTERN symbol, even if it has a section is considered
overridable. This is how we "define" .set targets. Real
definitions won't have VT_EXTERN set. */
if (esym && esym->st_shndx != SHN_UNDEF) {
2003-01-06 23:21:42 +03:00
/* the label is already defined */
if (IS_ASM_SYM(sym)
&& (is_local == 1 || (sym->type.t & VT_EXTERN)))
2003-01-06 23:21:42 +03:00
goto new_label;
if (!(sym->type.t & VT_EXTERN))
tcc_error("assembler label '%s' already defined",
get_tok_str(label, NULL));
2003-01-06 23:21:42 +03:00
}
} else {
new_label:
sym = asm_label_push(label);
2003-01-06 23:21:42 +03:00
}
if (!sym->c)
put_extern_sym2(sym, SHN_UNDEF, 0, 0, 0);
esym = elfsym(sym);
esym->st_shndx = sh_num;
esym->st_value = value;
if (is_local != 2)
sym->type.t &= ~VT_EXTERN;
return sym;
}
static Sym* asm_new_label(TCCState *s1, int label, int is_local)
{
return asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
}
/* Set the value of LABEL to that of some expression (possibly
involving other symbols). LABEL can be overwritten later still. */
static Sym* set_symbol(TCCState *s1, int label)
{
long n;
ExprValue e;
Sym *sym;
ElfSym *esym;
next();
asm_expr(s1, &e);
n = e.v;
esym = elfsym(e.sym);
if (esym)
n += esym->st_value;
sym = asm_new_label1(s1, label, 2, esym ? esym->st_shndx : SHN_ABS, n);
elfsym(sym)->st_other |= ST_ASM_SET;
return sym;
}
static void use_section1(TCCState *s1, Section *sec)
{
cur_text_section->data_offset = ind;
cur_text_section = sec;
ind = cur_text_section->data_offset;
}
static void use_section(TCCState *s1, const char *name)
{
Section *sec;
sec = find_section(s1, name);
use_section1(s1, sec);
}
static void push_section(TCCState *s1, const char *name)
{
Section *sec = find_section(s1, name);
sec->prev = cur_text_section;
use_section1(s1, sec);
}
static void pop_section(TCCState *s1)
{
Section *prev = cur_text_section->prev;
if (!prev)
tcc_error(".popsection without .pushsection");
cur_text_section->prev = NULL;
use_section1(s1, prev);
}
static void asm_parse_directive(TCCState *s1, int global)
2003-01-06 23:21:42 +03:00
{
int n, offset, v, size, tok1;
Section *sec;
uint8_t *ptr;
/* assembler directive */
sec = cur_text_section;
switch(tok) {
case TOK_ASMDIR_align:
2016-06-29 18:22:05 +03:00
case TOK_ASMDIR_balign:
case TOK_ASMDIR_p2align:
case TOK_ASMDIR_skip:
case TOK_ASMDIR_space:
2003-01-06 23:21:42 +03:00
tok1 = tok;
next();
n = asm_int_expr(s1);
if (tok1 == TOK_ASMDIR_p2align)
{
if (n < 0 || n > 30)
tcc_error("invalid p2align, must be between 0 and 30");
n = 1 << n;
tok1 = TOK_ASMDIR_align;
}
2016-06-29 18:22:05 +03:00
if (tok1 == TOK_ASMDIR_align || tok1 == TOK_ASMDIR_balign) {
2003-01-06 23:21:42 +03:00
if (n < 0 || (n & (n-1)) != 0)
tcc_error("alignment must be a positive power of two");
2003-01-06 23:21:42 +03:00
offset = (ind + n - 1) & -n;
size = offset - ind;
/* the section must have a compatible alignment */
if (sec->sh_addralign < n)
sec->sh_addralign = n;
2003-01-06 23:21:42 +03:00
} else {
if (n < 0)
n = 0;
2003-01-06 23:21:42 +03:00
size = n;
}
v = 0;
if (tok == ',') {
next();
v = asm_int_expr(s1);
}
zero_pad:
2003-01-06 23:21:42 +03:00
if (sec->sh_type != SHT_NOBITS) {
sec->data_offset = ind;
ptr = section_ptr_add(sec, size);
memset(ptr, v, size);
}
ind += size;
break;
case TOK_ASMDIR_quad:
#ifdef TCC_TARGET_X86_64
size = 8;
goto asm_data;
#else
2004-10-25 22:54:29 +04:00
next();
for(;;) {
uint64_t vl;
const char *p;
p = tokc.str.data;
2004-10-25 22:54:29 +04:00
if (tok != TOK_PPNUM) {
error_constant:
tcc_error("64 bit constant");
2004-10-25 22:54:29 +04:00
}
vl = strtoll(p, (char **)&p, 0);
if (*p != '\0')
goto error_constant;
next();
if (sec->sh_type != SHT_NOBITS) {
/* XXX: endianness */
gen_le32(vl);
gen_le32(vl >> 32);
} else {
ind += 8;
}
if (tok != ',')
break;
next();
}
break;
#endif
case TOK_ASMDIR_byte:
2003-01-06 23:21:42 +03:00
size = 1;
goto asm_data;
case TOK_ASMDIR_word:
case TOK_ASMDIR_short:
2003-01-06 23:21:42 +03:00
size = 2;
goto asm_data;
case TOK_ASMDIR_long:
case TOK_ASMDIR_int:
2003-01-06 23:21:42 +03:00
size = 4;
asm_data:
next();
for(;;) {
ExprValue e;
asm_expr(s1, &e);
if (sec->sh_type != SHT_NOBITS) {
if (size == 4) {
gen_expr32(&e);
#ifdef TCC_TARGET_X86_64
} else if (size == 8) {
gen_expr64(&e);
#endif
2003-01-06 23:21:42 +03:00
} else {
if (e.sym)
expect("constant");
if (size == 1)
g(e.v);
else
gen_le16(e.v);
}
} else {
ind += size;
}
if (tok != ',')
break;
next();
}
break;
case TOK_ASMDIR_fill:
{
int repeat, size, val, i, j;
uint8_t repeat_buf[8];
next();
repeat = asm_int_expr(s1);
if (repeat < 0) {
tcc_error("repeat < 0; .fill ignored");
break;
}
size = 1;
val = 0;
if (tok == ',') {
next();
size = asm_int_expr(s1);
if (size < 0) {
tcc_error("size < 0; .fill ignored");
break;
}
if (size > 8)
size = 8;
if (tok == ',') {
next();
val = asm_int_expr(s1);
}
}
/* XXX: endianness */
repeat_buf[0] = val;
repeat_buf[1] = val >> 8;
repeat_buf[2] = val >> 16;
repeat_buf[3] = val >> 24;
repeat_buf[4] = 0;
repeat_buf[5] = 0;
repeat_buf[6] = 0;
repeat_buf[7] = 0;
for(i = 0; i < repeat; i++) {
for(j = 0; j < size; j++) {
g(repeat_buf[j]);
}
}
}
break;
case TOK_ASMDIR_rept:
{
int repeat;
TokenString *init_str;
next();
repeat = asm_int_expr(s1);
init_str = tok_str_alloc();
while (next(), tok != TOK_ASMDIR_endr) {
if (tok == CH_EOF)
tcc_error("we at end of file, .endr not found");
tok_str_add_tok(init_str);
}
tok_str_add(init_str, -1);
tok_str_add(init_str, 0);
begin_macro(init_str, 1);
while (repeat-- > 0) {
tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS),
global);
macro_ptr = init_str->str;
}
end_macro();
next();
break;
}
case TOK_ASMDIR_org:
{
unsigned long n;
ExprValue e;
ElfSym *esym;
next();
asm_expr(s1, &e);
n = e.v;
esym = elfsym(e.sym);
if (esym) {
if (esym->st_shndx != cur_text_section->sh_num)
expect("constant or same-section symbol");
n += esym->st_value;
}
if (n < ind)
tcc_error("attempt to .org backwards");
v = 0;
size = n - ind;
goto zero_pad;
}
break;
case TOK_ASMDIR_set:
next();
tok1 = tok;
next();
/* Also accept '.set stuff', but don't do anything with this.
It's used in GAS to set various features like '.set mips16'. */
if (tok == ',')
set_symbol(s1, tok1);
break;
case TOK_ASMDIR_globl:
case TOK_ASMDIR_global:
case TOK_ASMDIR_weak:
case TOK_ASMDIR_hidden:
tok1 = tok;
do {
Sym *sym;
next();
sym = get_asm_sym(tok, NULL);
if (tok1 != TOK_ASMDIR_hidden)
sym->type.t &= ~VT_STATIC;
if (tok1 == TOK_ASMDIR_weak)
sym->a.weak = 1;
else if (tok1 == TOK_ASMDIR_hidden)
sym->a.visibility = STV_HIDDEN;
update_storage(sym);
next();
} while (tok == ',');
break;
case TOK_ASMDIR_string:
case TOK_ASMDIR_ascii:
case TOK_ASMDIR_asciz:
{
const uint8_t *p;
2004-10-24 02:52:42 +04:00
int i, size, t;
2004-10-24 02:52:42 +04:00
t = tok;
next();
2004-10-24 02:52:42 +04:00
for(;;) {
if (tok != TOK_STR)
expect("string constant");
p = tokc.str.data;
size = tokc.str.size;
if (t == TOK_ASMDIR_ascii && size > 0)
2004-10-24 02:52:42 +04:00
size--;
for(i = 0; i < size; i++)
g(p[i]);
next();
if (tok == ',') {
next();
} else if (tok != TOK_STR) {
break;
}
}
}
break;
case TOK_ASMDIR_text:
case TOK_ASMDIR_data:
case TOK_ASMDIR_bss:
{
char sname[64];
tok1 = tok;
n = 0;
next();
if (tok != ';' && tok != TOK_LINEFEED) {
n = asm_int_expr(s1);
next();
}
if (n)
sprintf(sname, "%s%d", get_tok_str(tok1, NULL), n);
else
sprintf(sname, "%s", get_tok_str(tok1, NULL));
use_section(s1, sname);
}
break;
case TOK_ASMDIR_file:
{
char filename[512];
filename[0] = '\0';
next();
if (tok == TOK_STR)
pstrcat(filename, sizeof(filename), tokc.str.data);
else
pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
if (s1->warn_unsupported)
tcc_warning("ignoring .file %s", filename);
next();
}
break;
case TOK_ASMDIR_ident:
{
char ident[256];
ident[0] = '\0';
next();
if (tok == TOK_STR)
pstrcat(ident, sizeof(ident), tokc.str.data);
else
pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
if (s1->warn_unsupported)
tcc_warning("ignoring .ident %s", ident);
next();
}
break;
case TOK_ASMDIR_size:
{
Sym *sym;
next();
sym = asm_label_find(tok);
if (!sym) {
tcc_error("label not found: %s", get_tok_str(tok, NULL));
}
/* XXX .size name,label2-label1 */
if (s1->warn_unsupported)
tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL));
next();
skip(',');
while (tok != TOK_LINEFEED && tok != ';' && tok != CH_EOF) {
next();
}
}
break;
case TOK_ASMDIR_type:
{
Sym *sym;
const char *newtype;
next();
sym = get_asm_sym(tok, NULL);
next();
skip(',');
if (tok == TOK_STR) {
newtype = tokc.str.data;
} else {
if (tok == '@' || tok == '%')
next();
newtype = get_tok_str(tok, NULL);
}
if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
}
else if (s1->warn_unsupported)
tcc_warning("change type of '%s' from 0x%x to '%s' ignored",
get_tok_str(sym->v, NULL), sym->type.t, newtype);
next();
}
break;
case TOK_ASMDIR_pushsection:
case TOK_ASMDIR_section:
{
char sname[256];
int old_nb_section = s1->nb_sections;
tok1 = tok;
/* XXX: support more options */
next();
sname[0] = '\0';
while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
if (tok == TOK_STR)
pstrcat(sname, sizeof(sname), tokc.str.data);
else
pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
next();
}
if (tok == ',') {
/* skip section options */
next();
if (tok != TOK_STR)
expect("string constant");
next();
if (tok == ',') {
next();
if (tok == '@' || tok == '%')
next();
next();
}
}
last_text_section = cur_text_section;
if (tok1 == TOK_ASMDIR_section)
use_section(s1, sname);
else
push_section(s1, sname);
/* If we just allocated a new section reset its alignment to
1. new_section normally acts for GCC compatibility and
sets alignment to PTR_SIZE. The assembler behaves different. */
if (old_nb_section != s1->nb_sections)
cur_text_section->sh_addralign = 1;
}
break;
case TOK_ASMDIR_previous:
{
Section *sec;
next();
if (!last_text_section)
tcc_error("no previous section referenced");
sec = cur_text_section;
use_section1(s1, last_text_section);
last_text_section = sec;
}
break;
case TOK_ASMDIR_popsection:
next();
pop_section(s1);
break;
2009-08-27 11:34:35 +04:00
#ifdef TCC_TARGET_I386
case TOK_ASMDIR_code16:
2009-08-27 11:34:35 +04:00
{
next();
s1->seg_size = 16;
}
break;
case TOK_ASMDIR_code32:
2009-08-27 11:34:35 +04:00
{
next();
s1->seg_size = 32;
}
break;
2009-08-27 11:53:50 +04:00
#endif
#ifdef TCC_TARGET_X86_64
/* added for compatibility with GAS */
case TOK_ASMDIR_code64:
2009-08-27 11:53:50 +04:00
next();
break;
2009-08-27 11:34:35 +04:00
#endif
2003-01-06 23:21:42 +03:00
default:
tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
2003-01-06 23:21:42 +03:00
break;
}
}
/* assemble a file */
static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global)
2003-01-06 23:21:42 +03:00
{
int opcode;
int saved_parse_flags = parse_flags;
2003-01-06 23:21:42 +03:00
parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR;
2003-01-06 23:21:42 +03:00
if (do_preprocess)
parse_flags |= PARSE_FLAG_PREPROCESS;
for(;;) {
next();
2003-01-06 23:21:42 +03:00
if (tok == TOK_EOF)
break;
/* generate line number info */
if (global && s1->do_debug)
tcc_debug_line(s1);
2003-01-06 23:21:42 +03:00
parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
redo:
if (tok == '#') {
/* horrible gas comment */
while (tok != TOK_LINEFEED)
next();
} else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
asm_parse_directive(s1, global);
2003-01-06 23:21:42 +03:00
} else if (tok == TOK_PPNUM) {
const char *p;
int n;
p = tokc.str.data;
n = strtoul(p, (char **)&p, 10);
2003-01-06 23:21:42 +03:00
if (*p != '\0')
expect("':'");
/* new local label */
asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
2003-01-06 23:21:42 +03:00
next();
skip(':');
goto redo;
} else if (tok >= TOK_IDENT) {
/* instruction or label */
opcode = tok;
next();
if (tok == ':') {
/* new label */
asm_new_label(s1, opcode, 0);
2003-01-06 23:21:42 +03:00
next();
goto redo;
} else if (tok == '=') {
set_symbol(s1, opcode);
goto redo;
2003-01-06 23:21:42 +03:00
} else {
asm_opcode(s1, opcode);
}
}
/* end of line */
if (tok != ';' && tok != TOK_LINEFEED)
2003-01-06 23:21:42 +03:00
expect("end of line");
parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
}
parse_flags = saved_parse_flags;
2003-01-06 23:21:42 +03:00
return 0;
}
/* Assemble the current file */
ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
2003-01-06 23:21:42 +03:00
{
int ret;
tcc_debug_start(s1);
2003-01-06 23:21:42 +03:00
/* default section is text */
cur_text_section = text_section;
ind = cur_text_section->data_offset;
nocode_wanted = 0;
ret = tcc_assemble_internal(s1, do_preprocess, 1);
2003-01-06 23:21:42 +03:00
cur_text_section->data_offset = ind;
tcc_debug_end(s1);
2003-01-06 23:21:42 +03:00
return ret;
}
/********************************************************************/
/* GCC inline asm support */
/* assemble the string 'str' in the current C compilation unit without
C preprocessing. NOTE: str is modified by modifying the '\0' at the
end */
static void tcc_assemble_inline(TCCState *s1, char *str, int len, int global)
2003-01-06 23:21:42 +03:00
{
const int *saved_macro_ptr = macro_ptr;
int dotid = set_idnum('.', IS_ID);
tcc_open_bf(s1, ":asm:", len);
memcpy(file->buffer, str, len);
2003-01-06 23:21:42 +03:00
macro_ptr = NULL;
tcc_assemble_internal(s1, 0, global);
tcc_close();
2003-01-06 23:21:42 +03:00
set_idnum('.', dotid);
2003-01-06 23:21:42 +03:00
macro_ptr = saved_macro_ptr;
}
/* find a constraint by its number or id (gcc 3 extended
syntax). return -1 if not found. Return in *pp in char after the
constraint */
ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
2003-01-06 23:21:42 +03:00
const char *name, const char **pp)
{
int index;
TokenSym *ts;
const char *p;
if (isnum(*name)) {
index = 0;
while (isnum(*name)) {
index = (index * 10) + (*name) - '0';
name++;
}
if ((unsigned)index >= nb_operands)
index = -1;
} else if (*name == '[') {
name++;
p = strchr(name, ']');
if (p) {
ts = tok_alloc(name, p - name);
for(index = 0; index < nb_operands; index++) {
if (operands[index].id == ts->tok)
goto found;
}
index = -1;
found:
name = p + 1;
} else {
index = -1;
}
} else {
index = -1;
}
if (pp)
*pp = name;
return index;
}
static void subst_asm_operands(ASMOperand *operands, int nb_operands,
2003-01-06 23:21:42 +03:00
CString *out_str, CString *in_str)
{
int c, index, modifier;
const char *str;
ASMOperand *op;
SValue sv;
cstr_new(out_str);
str = in_str->data;
for(;;) {
c = *str++;
if (c == '%') {
if (*str == '%') {
str++;
goto add_char;
}
modifier = 0;
if (*str == 'c' || *str == 'n' ||
*str == 'b' || *str == 'w' || *str == 'h' || *str == 'k' ||
*str == 'q' ||
/* P in GCC would add "@PLT" to symbol refs in PIC mode,
and make literal operands not be decorated with '$'. */
*str == 'P')
2003-01-06 23:21:42 +03:00
modifier = *str++;
index = find_constraint(operands, nb_operands, str, &str);
if (index < 0)
tcc_error("invalid operand reference after %%");
2003-01-06 23:21:42 +03:00
op = &operands[index];
sv = *op->vt;
if (op->reg >= 0) {
sv.r = op->reg;
if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
2003-01-06 23:21:42 +03:00
sv.r |= VT_LVAL;
}
subst_asm_operand(out_str, &sv, modifier);
} else {
add_char:
cstr_ccat(out_str, c);
if (c == '\0')
break;
}
}
}
static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
int is_output)
{
ASMOperand *op;
int nb_operands;
if (tok != ':') {
nb_operands = *nb_operands_ptr;
for(;;) {
CString astr;
2003-01-06 23:21:42 +03:00
if (nb_operands >= MAX_ASM_OPERANDS)
tcc_error("too many asm operands");
2003-01-06 23:21:42 +03:00
op = &operands[nb_operands++];
op->id = 0;
if (tok == '[') {
next();
if (tok < TOK_IDENT)
expect("identifier");
op->id = tok;
next();
skip(']');
}
parse_mult_str(&astr, "string constant");
op->constraint = tcc_malloc(astr.size);
strcpy(op->constraint, astr.data);
cstr_free(&astr);
2003-01-06 23:21:42 +03:00
skip('(');
gexpr();
if (is_output) {
if (!(vtop->type.t & VT_ARRAY))
test_lvalue();
2003-01-06 23:21:42 +03:00
} else {
/* we want to avoid LLOCAL case, except when the 'm'
constraint is used. Note that it may come from
register storage, so we need to convert (reg)
2003-01-06 23:21:42 +03:00
case */
if ((vtop->r & VT_LVAL) &&
((vtop->r & VT_VALMASK) == VT_LLOCAL ||
(vtop->r & VT_VALMASK) < VT_CONST) &&
!strchr(op->constraint, 'm')) {
2003-01-06 23:21:42 +03:00
gv(RC_INT);
}
}
op->vt = vtop;
skip(')');
if (tok == ',') {
next();
} else {
break;
}
}
*nb_operands_ptr = nb_operands;
}
}
/* parse the GCC asm() instruction */
ST_FUNC void asm_instr(void)
2003-01-06 23:21:42 +03:00
{
CString astr, astr1;
ASMOperand operands[MAX_ASM_OPERANDS];
int nb_outputs, nb_operands, i, must_subst, out_reg;
2003-01-06 23:21:42 +03:00
uint8_t clobber_regs[NB_ASM_REGS];
next();
/* since we always generate the asm() instruction, we can ignore
volatile */
if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
next();
}
parse_asm_str(&astr);
2003-01-06 23:21:42 +03:00
nb_operands = 0;
nb_outputs = 0;
must_subst = 0;
2003-01-06 23:21:42 +03:00
memset(clobber_regs, 0, sizeof(clobber_regs));
if (tok == ':') {
next();
must_subst = 1;
2003-01-06 23:21:42 +03:00
/* output args */
parse_asm_operands(operands, &nb_operands, 1);
nb_outputs = nb_operands;
if (tok == ':') {
next();
if (tok != ')') {
/* input args */
parse_asm_operands(operands, &nb_operands, 0);
if (tok == ':') {
/* clobber list */
/* XXX: handle registers */
2003-01-06 23:21:42 +03:00
next();
for(;;) {
if (tok != TOK_STR)
expect("string constant");
asm_clobber(clobber_regs, tokc.str.data);
2003-01-06 23:21:42 +03:00
next();
if (tok == ',') {
next();
} else {
break;
}
2003-01-06 23:21:42 +03:00
}
}
}
}
}
skip(')');
/* NOTE: we do not eat the ';' so that we can restore the current
token after the assembler parsing */
if (tok != ';')
expect("';'");
2003-01-06 23:21:42 +03:00
/* save all values in the memory */
save_regs(0);
/* compute constraints */
asm_compute_constraints(operands, nb_operands, nb_outputs,
clobber_regs, &out_reg);
2003-01-06 23:21:42 +03:00
2003-04-15 02:22:34 +04:00
/* substitute the operands in the asm string. No substitution is
done if no operands (GCC behaviour) */
2003-01-06 23:21:42 +03:00
#ifdef ASM_DEBUG
printf("asm: \"%s\"\n", (char *)astr.data);
#endif
if (must_subst) {
subst_asm_operands(operands, nb_operands, &astr1, &astr);
2003-04-15 02:22:34 +04:00
cstr_free(&astr);
} else {
astr1 = astr;
}
2003-01-06 23:21:42 +03:00
#ifdef ASM_DEBUG
printf("subst_asm: \"%s\"\n", (char *)astr1.data);
#endif
/* generate loads */
asm_gen_code(operands, nb_operands, nb_outputs, 0,
clobber_regs, out_reg);
2003-01-06 23:21:42 +03:00
/* assemble the string with tcc internal assembler */
tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1, 0);
2003-01-06 23:21:42 +03:00
/* restore the current C token */
next();
/* store the output values if needed */
asm_gen_code(operands, nb_operands, nb_outputs, 1,
clobber_regs, out_reg);
2003-01-06 23:21:42 +03:00
/* free everything */
for(i=0;i<nb_operands;i++) {
ASMOperand *op;
op = &operands[i];
tcc_free(op->constraint);
vpop();
}
cstr_free(&astr1);
}
ST_FUNC void asm_global_instr(void)
{
CString astr;
int saved_nocode_wanted = nocode_wanted;
/* Global asm blocks are always emitted. */
nocode_wanted = 0;
next();
parse_asm_str(&astr);
skip(')');
/* NOTE: we do not eat the ';' so that we can restore the current
token after the assembler parsing */
if (tok != ';')
expect("';'");
#ifdef ASM_DEBUG
printf("asm_global: \"%s\"\n", (char *)astr.data);
#endif
cur_text_section = text_section;
ind = cur_text_section->data_offset;
/* assemble the string with tcc internal assembler */
tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 1);
cur_text_section->data_offset = ind;
/* restore the current C token */
next();
cstr_free(&astr);
nocode_wanted = saved_nocode_wanted;
}
#endif /* CONFIG_TCC_ASM */