2003-01-06 23:21:42 +03:00
|
|
|
/*
|
|
|
|
* GAS like assembler for TCC
|
2015-07-29 23:53:57 +03:00
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
#include "tcc.h"
|
2012-01-06 21:14:34 +04:00
|
|
|
#ifdef CONFIG_TCC_ASM
|
2009-12-20 03:53:49 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
|
2016-04-22 18:29:56 +03:00
|
|
|
static int tcc_assemble_internal(TCCState *s1, int do_preprocess);
|
|
|
|
static Sym sym_dot;
|
2004-10-18 04:19:51 +04:00
|
|
|
|
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;
|
2016-05-12 00:47:02 +03:00
|
|
|
int op, label;
|
|
|
|
long n;
|
2003-01-06 23:21:42 +03:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
switch(tok) {
|
|
|
|
case TOK_PPNUM:
|
2015-11-21 14:23:53 +03:00
|
|
|
p = tokc.str.data;
|
2003-04-21 19:21:19 +04:00
|
|
|
n = strtoul(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 = label_find(label);
|
|
|
|
if (*p == 'b') {
|
|
|
|
/* backward : find the last corresponding defined label */
|
|
|
|
if (sym && sym->r == 0)
|
|
|
|
sym = sym->prev_tok;
|
|
|
|
if (!sym)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("local label '%d' not found backward", n);
|
2003-01-06 23:21:42 +03:00
|
|
|
} else {
|
|
|
|
/* forward */
|
|
|
|
if (!sym || sym->r) {
|
|
|
|
/* if the last label is defined, then define a new one */
|
|
|
|
sym = label_push(&s1->asm_labels, label, 0);
|
|
|
|
sym->type.t = VT_STATIC | VT_VOID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pe->v = 0;
|
|
|
|
pe->sym = sym;
|
|
|
|
} else if (*p == '\0') {
|
|
|
|
pe->v = n;
|
|
|
|
pe->sym = NULL;
|
|
|
|
} else {
|
2011-08-11 19:07:56 +04:00
|
|
|
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)
|
2011-08-11 19:07:56 +04:00
|
|
|
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;
|
2003-04-29 01:23:53 +04:00
|
|
|
case TOK_CCHAR:
|
|
|
|
case TOK_LCHAR:
|
2015-07-29 23:53:57 +03:00
|
|
|
pe->v = tokc.i;
|
|
|
|
pe->sym = NULL;
|
|
|
|
next();
|
|
|
|
break;
|
2004-10-18 04:19:51 +04:00
|
|
|
case '(':
|
|
|
|
next();
|
|
|
|
asm_expr(s1, pe);
|
|
|
|
skip(')');
|
|
|
|
break;
|
2016-04-22 18:29:56 +03:00
|
|
|
case '.':
|
|
|
|
pe->v = 0;
|
|
|
|
pe->sym = &sym_dot;
|
|
|
|
sym_dot.type.t = VT_VOID | VT_STATIC;
|
|
|
|
sym_dot.r = cur_text_section->sh_num;
|
|
|
|
sym_dot.jnext = ind;
|
|
|
|
next();
|
|
|
|
break;
|
2003-01-06 23:21:42 +03:00
|
|
|
default:
|
|
|
|
if (tok >= TOK_IDENT) {
|
|
|
|
/* label case : if the label was not found, add one */
|
|
|
|
sym = label_find(tok);
|
|
|
|
if (!sym) {
|
|
|
|
sym = label_push(&s1->asm_labels, tok, 0);
|
|
|
|
/* NOTE: by default, the symbol is global */
|
|
|
|
sym->type.t = VT_VOID;
|
|
|
|
}
|
2004-10-18 04:19:51 +04:00
|
|
|
if (sym->r == SHN_ABS) {
|
|
|
|
/* if absolute symbol, no need to put a symbol value */
|
2009-07-19 00:07:51 +04:00
|
|
|
pe->v = sym->jnext;
|
2004-10-18 04:19:51 +04:00
|
|
|
pe->sym = NULL;
|
|
|
|
} else {
|
|
|
|
pe->v = 0;
|
|
|
|
pe->sym = sym;
|
|
|
|
}
|
2003-01-06 23:21:42 +03:00
|
|
|
next();
|
|
|
|
} else {
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
|
2003-01-06 23:21:42 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
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;
|
2015-07-29 23:53:57 +03:00
|
|
|
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)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("invalid operation with label");
|
2003-01-06 23:21:42 +03:00
|
|
|
switch(op) {
|
|
|
|
case '*':
|
|
|
|
pe->v *= e2.v;
|
|
|
|
break;
|
2015-07-29 23:53:57 +03:00
|
|
|
case '/':
|
2003-01-06 23:21:42 +03:00
|
|
|
if (e2.v == 0) {
|
|
|
|
div_error:
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("division by zero");
|
2003-01-06 23:21:42 +03:00
|
|
|
}
|
|
|
|
pe->v /= e2.v;
|
|
|
|
break;
|
2015-07-29 23:53:57 +03:00
|
|
|
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)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("invalid operation with label");
|
2003-01-06 23:21:42 +03:00
|
|
|
switch(op) {
|
|
|
|
case '&':
|
|
|
|
pe->v &= e2.v;
|
|
|
|
break;
|
2015-07-29 23:53:57 +03:00
|
|
|
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 (!pe->sym && !e2.sym) {
|
|
|
|
/* OK */
|
|
|
|
} else if (pe->sym && !e2.sym) {
|
|
|
|
/* OK */
|
|
|
|
} else if (pe->sym && e2.sym) {
|
2015-07-29 23:53:57 +03:00
|
|
|
if (pe->sym == e2.sym) {
|
2003-01-06 23:21:42 +03:00
|
|
|
/* OK */
|
|
|
|
} else if (pe->sym->r == e2.sym->r && pe->sym->r != 0) {
|
|
|
|
/* we also accept defined symbols in the same section */
|
2009-07-19 00:07:51 +04:00
|
|
|
pe->v += pe->sym->jnext - e2.sym->jnext;
|
2003-01-06 23:21:42 +03:00
|
|
|
} else {
|
|
|
|
goto cannot_relocate;
|
|
|
|
}
|
2014-04-07 15:31:00 +04:00
|
|
|
pe->sym = NULL; /* same symbols can be subtracted to NULL */
|
2003-01-06 23:21:42 +03:00
|
|
|
} else {
|
|
|
|
cannot_relocate:
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("invalid operation with label");
|
2003-01-06 23:21:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
|
2003-01-06 23:21:42 +03:00
|
|
|
{
|
|
|
|
asm_expr_sum(s1, pe);
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: the same name space as C labels is used to avoid using too
|
|
|
|
much memory when storing labels in TokenStrings */
|
2004-10-18 04:19:51 +04:00
|
|
|
static void 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;
|
|
|
|
|
|
|
|
sym = label_find(label);
|
|
|
|
if (sym) {
|
|
|
|
if (sym->r) {
|
|
|
|
/* the label is already defined */
|
|
|
|
if (!is_local) {
|
2015-07-29 23:53:57 +03:00
|
|
|
tcc_error("assembler label '%s' already defined",
|
2003-01-06 23:21:42 +03:00
|
|
|
get_tok_str(label, NULL));
|
|
|
|
} else {
|
|
|
|
/* redefinition of local labels is possible */
|
|
|
|
goto new_label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new_label:
|
|
|
|
sym = label_push(&s1->asm_labels, label, 0);
|
|
|
|
sym->type.t = VT_STATIC | VT_VOID;
|
|
|
|
}
|
2004-10-18 04:19:51 +04:00
|
|
|
sym->r = sh_num;
|
2009-07-19 00:07:51 +04:00
|
|
|
sym->jnext = value;
|
2004-10-18 04:19:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void asm_new_label(TCCState *s1, int label, int is_local)
|
|
|
|
{
|
|
|
|
asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
|
2003-01-06 23:21:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void asm_free_labels(TCCState *st)
|
|
|
|
{
|
|
|
|
Sym *s, *s1;
|
2004-10-18 04:19:51 +04:00
|
|
|
Section *sec;
|
2015-04-21 06:34:35 +03:00
|
|
|
|
2003-01-06 23:21:42 +03:00
|
|
|
for(s = st->asm_labels; s != NULL; s = s1) {
|
|
|
|
s1 = s->prev;
|
|
|
|
/* define symbol value in object file */
|
|
|
|
if (s->r) {
|
2004-10-18 04:19:51 +04:00
|
|
|
if (s->r == SHN_ABS)
|
|
|
|
sec = SECTION_ABS;
|
|
|
|
else
|
|
|
|
sec = st->sections[s->r];
|
2009-07-19 00:07:51 +04:00
|
|
|
put_extern_sym2(s, sec, s->jnext, 0, 0);
|
2003-01-06 23:21:42 +03:00
|
|
|
}
|
|
|
|
/* remove label */
|
|
|
|
table_ident[s->v - TOK_IDENT]->sym_label = NULL;
|
2004-11-07 18:45:40 +03:00
|
|
|
sym_free(s);
|
2003-01-06 23:21:42 +03:00
|
|
|
}
|
|
|
|
st->asm_labels = NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-18 04:19:51 +04:00
|
|
|
static void use_section1(TCCState *s1, Section *sec)
|
2003-04-29 01:23:53 +04:00
|
|
|
{
|
|
|
|
cur_text_section->data_offset = ind;
|
|
|
|
cur_text_section = sec;
|
|
|
|
ind = cur_text_section->data_offset;
|
|
|
|
}
|
|
|
|
|
2004-10-18 04:19:51 +04:00
|
|
|
static void use_section(TCCState *s1, const char *name)
|
|
|
|
{
|
|
|
|
Section *sec;
|
|
|
|
sec = find_section(s1, name);
|
|
|
|
use_section1(s1, sec);
|
|
|
|
}
|
|
|
|
|
2003-01-06 23:21:42 +03:00
|
|
|
static void asm_parse_directive(TCCState *s1)
|
|
|
|
{
|
|
|
|
int n, offset, v, size, tok1;
|
|
|
|
Section *sec;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
|
|
|
/* assembler directive */
|
|
|
|
sec = cur_text_section;
|
|
|
|
switch(tok) {
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_align:
|
|
|
|
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);
|
2016-04-13 10:23:46 +03:00
|
|
|
if (tok1 == TOK_ASMDIR_p2align)
|
2015-01-20 10:49:49 +03:00
|
|
|
{
|
|
|
|
if (n < 0 || n > 30)
|
|
|
|
tcc_error("invalid p2align, must be between 0 and 30");
|
|
|
|
n = 1 << n;
|
2016-04-13 10:23:46 +03:00
|
|
|
tok1 = TOK_ASMDIR_align;
|
2015-01-20 10:49:49 +03:00
|
|
|
}
|
2016-04-13 10:23:46 +03:00
|
|
|
if (tok1 == TOK_ASMDIR_align) {
|
2003-01-06 23:21:42 +03:00
|
|
|
if (n < 0 || (n & (n-1)) != 0)
|
2011-08-11 19:07:56 +04:00
|
|
|
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;
|
2004-10-18 04:19:51 +04:00
|
|
|
/* 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 {
|
|
|
|
size = n;
|
|
|
|
}
|
|
|
|
v = 0;
|
|
|
|
if (tok == ',') {
|
|
|
|
next();
|
|
|
|
v = asm_int_expr(s1);
|
|
|
|
}
|
2004-10-18 04:19:51 +04:00
|
|
|
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;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_quad:
|
2004-10-25 22:54:29 +04:00
|
|
|
next();
|
|
|
|
for(;;) {
|
|
|
|
uint64_t vl;
|
|
|
|
const char *p;
|
|
|
|
|
2015-11-21 14:23:53 +03:00
|
|
|
p = tokc.str.data;
|
2004-10-25 22:54:29 +04:00
|
|
|
if (tok != TOK_PPNUM) {
|
|
|
|
error_constant:
|
2011-08-11 19:07:56 +04:00
|
|
|
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;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_byte:
|
2003-01-06 23:21:42 +03:00
|
|
|
size = 1;
|
|
|
|
goto asm_data;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_word:
|
|
|
|
case TOK_ASMDIR_short:
|
2003-01-06 23:21:42 +03:00
|
|
|
size = 2;
|
|
|
|
goto asm_data;
|
2016-04-13 10:23:46 +03:00
|
|
|
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);
|
|
|
|
} 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;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_fill:
|
2004-10-18 04:19:51 +04:00
|
|
|
{
|
|
|
|
int repeat, size, val, i, j;
|
|
|
|
uint8_t repeat_buf[8];
|
|
|
|
next();
|
|
|
|
repeat = asm_int_expr(s1);
|
|
|
|
if (repeat < 0) {
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("repeat < 0; .fill ignored");
|
2004-10-18 04:19:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
size = 1;
|
|
|
|
val = 0;
|
|
|
|
if (tok == ',') {
|
|
|
|
next();
|
|
|
|
size = asm_int_expr(s1);
|
|
|
|
if (size < 0) {
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("size < 0; .fill ignored");
|
2004-10-18 04:19:51 +04:00
|
|
|
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;
|
2016-04-22 18:29:56 +03:00
|
|
|
case TOK_ASMDIR_rept:
|
|
|
|
{
|
|
|
|
int repeat;
|
|
|
|
TokenString init_str;
|
|
|
|
ParseState saved_parse_state = {0};
|
|
|
|
next();
|
|
|
|
repeat = asm_int_expr(s1);
|
|
|
|
tok_str_new(&init_str);
|
|
|
|
next();
|
|
|
|
while ((tok != TOK_ASMDIR_endr) && (tok != CH_EOF)) {
|
|
|
|
tok_str_add_tok(&init_str);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
if (tok == CH_EOF) tcc_error("we at end of file, .endr not found");
|
|
|
|
next();
|
|
|
|
tok_str_add(&init_str, -1);
|
|
|
|
tok_str_add(&init_str, 0);
|
|
|
|
save_parse_state(&saved_parse_state);
|
|
|
|
begin_macro(&init_str, 0);
|
|
|
|
while (repeat-- > 0) {
|
|
|
|
tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS));
|
|
|
|
macro_ptr = init_str.str;
|
|
|
|
}
|
|
|
|
end_macro();
|
|
|
|
restore_parse_state(&saved_parse_state);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_org:
|
2004-10-18 04:19:51 +04:00
|
|
|
{
|
|
|
|
unsigned long n;
|
|
|
|
next();
|
|
|
|
/* XXX: handle section symbols too */
|
|
|
|
n = asm_int_expr(s1);
|
|
|
|
if (n < ind)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("attempt to .org backwards");
|
2004-10-18 04:19:51 +04:00
|
|
|
v = 0;
|
|
|
|
size = n - ind;
|
|
|
|
goto zero_pad;
|
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_globl:
|
|
|
|
case TOK_ASMDIR_global:
|
|
|
|
case TOK_ASMDIR_weak:
|
|
|
|
case TOK_ASMDIR_hidden:
|
2015-07-29 23:53:57 +03:00
|
|
|
tok1 = tok;
|
|
|
|
do {
|
|
|
|
Sym *sym;
|
2003-04-29 01:23:53 +04:00
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
next();
|
|
|
|
sym = label_find(tok);
|
|
|
|
if (!sym) {
|
|
|
|
sym = label_push(&s1->asm_labels, tok, 0);
|
|
|
|
sym->type.t = VT_VOID;
|
|
|
|
}
|
2016-04-13 10:23:46 +03:00
|
|
|
if (tok1 != TOK_ASMDIR_hidden)
|
2015-07-29 23:53:57 +03:00
|
|
|
sym->type.t &= ~VT_STATIC;
|
2016-04-13 10:23:46 +03:00
|
|
|
if (tok1 == TOK_ASMDIR_weak)
|
2015-07-29 23:53:57 +03:00
|
|
|
sym->type.t |= VT_WEAK;
|
2016-04-13 10:23:46 +03:00
|
|
|
else if (tok1 == TOK_ASMDIR_hidden)
|
2015-07-29 23:53:57 +03:00
|
|
|
sym->type.t |= STV_HIDDEN << VT_VIS_SHIFT;
|
|
|
|
next();
|
|
|
|
} while (tok == ',');
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_string:
|
|
|
|
case TOK_ASMDIR_ascii:
|
|
|
|
case TOK_ASMDIR_asciz:
|
2003-04-29 01:23:53 +04:00
|
|
|
{
|
|
|
|
const uint8_t *p;
|
2004-10-24 02:52:42 +04:00
|
|
|
int i, size, t;
|
2003-04-29 01:23:53 +04:00
|
|
|
|
2004-10-24 02:52:42 +04:00
|
|
|
t = tok;
|
2003-04-29 01:23:53 +04:00
|
|
|
next();
|
2004-10-24 02:52:42 +04:00
|
|
|
for(;;) {
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
expect("string constant");
|
2015-11-21 14:23:53 +03:00
|
|
|
p = tokc.str.data;
|
|
|
|
size = tokc.str.size;
|
2016-04-13 10:23:46 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_text:
|
|
|
|
case TOK_ASMDIR_data:
|
|
|
|
case TOK_ASMDIR_bss:
|
2015-07-29 23:53:57 +03:00
|
|
|
{
|
2003-04-29 01:23:53 +04:00
|
|
|
char sname[64];
|
|
|
|
tok1 = tok;
|
|
|
|
n = 0;
|
|
|
|
next();
|
|
|
|
if (tok != ';' && tok != TOK_LINEFEED) {
|
2015-07-29 23:53:57 +03:00
|
|
|
n = asm_int_expr(s1);
|
|
|
|
next();
|
2003-04-29 01:23:53 +04:00
|
|
|
}
|
2015-11-19 21:26:47 +03:00
|
|
|
if (n)
|
2016-04-13 10:23:46 +03:00
|
|
|
sprintf(sname, "%s%d", get_tok_str(tok1, NULL), n);
|
2015-11-19 21:26:47 +03:00
|
|
|
else
|
2016-04-13 10:23:46 +03:00
|
|
|
sprintf(sname, "%s", get_tok_str(tok1, NULL));
|
2003-04-29 01:23:53 +04:00
|
|
|
use_section(s1, sname);
|
2015-07-29 23:53:57 +03:00
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_file:
|
2010-04-05 14:19:49 +04:00
|
|
|
{
|
|
|
|
char filename[512];
|
|
|
|
|
|
|
|
filename[0] = '\0';
|
|
|
|
next();
|
|
|
|
|
|
|
|
if (tok == TOK_STR)
|
2015-11-21 14:23:53 +03:00
|
|
|
pstrcat(filename, sizeof(filename), tokc.str.data);
|
2010-04-05 14:19:49 +04:00
|
|
|
else
|
|
|
|
pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
|
|
|
|
|
|
|
|
if (s1->warn_unsupported)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_warning("ignoring .file %s", filename);
|
2010-04-05 14:19:49 +04:00
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_ident:
|
2010-04-05 14:45:52 +04:00
|
|
|
{
|
|
|
|
char ident[256];
|
|
|
|
|
|
|
|
ident[0] = '\0';
|
|
|
|
next();
|
|
|
|
|
|
|
|
if (tok == TOK_STR)
|
2015-11-21 14:23:53 +03:00
|
|
|
pstrcat(ident, sizeof(ident), tokc.str.data);
|
2010-04-05 14:45:52 +04:00
|
|
|
else
|
|
|
|
pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
|
|
|
|
|
|
|
|
if (s1->warn_unsupported)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_warning("ignoring .ident %s", ident);
|
2010-04-05 14:45:52 +04:00
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_size:
|
2015-07-29 23:53:57 +03:00
|
|
|
{
|
2010-04-05 14:43:51 +04:00
|
|
|
Sym *sym;
|
|
|
|
|
|
|
|
next();
|
|
|
|
sym = label_find(tok);
|
|
|
|
if (!sym) {
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("label not found: %s", get_tok_str(tok, NULL));
|
2010-04-05 14:43:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX .size name,label2-label1 */
|
|
|
|
if (s1->warn_unsupported)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL));
|
2010-04-05 14:43:51 +04:00
|
|
|
|
2014-04-14 05:33:50 +04:00
|
|
|
next();
|
|
|
|
skip(',');
|
2010-04-05 14:43:51 +04:00
|
|
|
while (tok != '\n' && tok != CH_EOF) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_type:
|
2015-07-29 23:53:57 +03:00
|
|
|
{
|
2010-04-05 14:31:45 +04:00
|
|
|
Sym *sym;
|
2011-02-24 20:24:02 +03:00
|
|
|
const char *newtype;
|
2010-04-05 14:31:45 +04:00
|
|
|
|
|
|
|
next();
|
|
|
|
sym = label_find(tok);
|
|
|
|
if (!sym) {
|
|
|
|
sym = label_push(&s1->asm_labels, tok, 0);
|
|
|
|
sym->type.t = VT_VOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
skip(',');
|
2011-02-24 20:24:02 +03:00
|
|
|
if (tok == TOK_STR) {
|
2015-11-21 14:23:53 +03:00
|
|
|
newtype = tokc.str.data;
|
2011-02-24 20:24:02 +03:00
|
|
|
} else {
|
|
|
|
if (tok == '@' || tok == '%')
|
2016-04-13 10:23:46 +03:00
|
|
|
next();
|
2011-02-24 20:24:02 +03:00
|
|
|
newtype = get_tok_str(tok, NULL);
|
|
|
|
}
|
2010-04-05 14:31:45 +04:00
|
|
|
|
2011-02-24 20:24:02 +03:00
|
|
|
if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
|
2014-04-14 05:33:50 +04:00
|
|
|
sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
|
2010-04-05 14:31:45 +04:00
|
|
|
}
|
|
|
|
else if (s1->warn_unsupported)
|
2015-07-29 23:53:57 +03:00
|
|
|
tcc_warning("change type of '%s' from 0x%x to '%s' ignored",
|
2010-04-05 14:31:45 +04:00
|
|
|
get_tok_str(sym->v, NULL), sym->type.t, newtype);
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_section:
|
2003-04-29 01:23:53 +04:00
|
|
|
{
|
|
|
|
char sname[256];
|
|
|
|
|
|
|
|
/* XXX: support more options */
|
|
|
|
next();
|
|
|
|
sname[0] = '\0';
|
|
|
|
while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
|
|
|
|
if (tok == TOK_STR)
|
2015-11-21 14:23:53 +03:00
|
|
|
pstrcat(sname, sizeof(sname), tokc.str.data);
|
2003-04-29 01:23:53 +04:00
|
|
|
else
|
|
|
|
pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
|
|
|
|
next();
|
|
|
|
}
|
2004-10-18 04:19:51 +04:00
|
|
|
if (tok == ',') {
|
|
|
|
/* skip section options */
|
|
|
|
next();
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
expect("string constant");
|
|
|
|
next();
|
2016-04-13 10:23:46 +03:00
|
|
|
if (tok == ',') {
|
|
|
|
next();
|
|
|
|
if (tok == '@' || tok == '%')
|
|
|
|
next();
|
|
|
|
next();
|
|
|
|
}
|
2004-10-18 04:19:51 +04:00
|
|
|
}
|
|
|
|
last_text_section = cur_text_section;
|
2003-04-29 01:23:53 +04:00
|
|
|
use_section(s1, sname);
|
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_previous:
|
2015-07-29 23:53:57 +03:00
|
|
|
{
|
2004-10-18 04:19:51 +04:00
|
|
|
Section *sec;
|
|
|
|
next();
|
|
|
|
if (!last_text_section)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("no previous section referenced");
|
2004-10-18 04:19:51 +04:00
|
|
|
sec = cur_text_section;
|
|
|
|
use_section1(s1, last_text_section);
|
|
|
|
last_text_section = sec;
|
|
|
|
}
|
|
|
|
break;
|
2009-08-27 11:34:35 +04:00
|
|
|
#ifdef TCC_TARGET_I386
|
2016-04-13 10:23:46 +03:00
|
|
|
case TOK_ASMDIR_code16:
|
2009-08-27 11:34:35 +04:00
|
|
|
{
|
|
|
|
next();
|
|
|
|
s1->seg_size = 16;
|
|
|
|
}
|
|
|
|
break;
|
2016-04-13 10:23:46 +03:00
|
|
|
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 */
|
2016-04-13 10:23:46 +03:00
|
|
|
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:
|
2011-08-11 19:07:56 +04:00
|
|
|
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 opcode;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* print stats about opcodes */
|
|
|
|
{
|
|
|
|
const ASMInstr *pa;
|
|
|
|
int freq[4];
|
|
|
|
int op_vals[500];
|
|
|
|
int nb_op_vals, i, j;
|
|
|
|
|
|
|
|
nb_op_vals = 0;
|
|
|
|
memset(freq, 0, sizeof(freq));
|
|
|
|
for(pa = asm_instrs; pa->sym != 0; pa++) {
|
|
|
|
freq[pa->nb_ops]++;
|
|
|
|
for(i=0;i<pa->nb_ops;i++) {
|
|
|
|
for(j=0;j<nb_op_vals;j++) {
|
|
|
|
if (pa->op_type[i] == op_vals[j])
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
op_vals[nb_op_vals++] = pa->op_type[i];
|
|
|
|
found: ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i=0;i<nb_op_vals;i++) {
|
|
|
|
int v = op_vals[i];
|
|
|
|
if ((v & (v - 1)) != 0)
|
|
|
|
printf("%3d: %08x\n", i, v);
|
|
|
|
}
|
|
|
|
printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
|
|
|
|
sizeof(asm_instrs), sizeof(asm_instrs) / sizeof(ASMInstr),
|
|
|
|
freq[0], freq[1], freq[2], freq[3]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* XXX: undefine C labels */
|
|
|
|
|
|
|
|
ch = file->buf_ptr[0];
|
|
|
|
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 15:29:39 +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;
|
|
|
|
next();
|
|
|
|
for(;;) {
|
|
|
|
if (tok == TOK_EOF)
|
|
|
|
break;
|
|
|
|
parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
|
|
|
|
redo:
|
|
|
|
if (tok == '#') {
|
|
|
|
/* horrible gas comment */
|
|
|
|
while (tok != TOK_LINEFEED)
|
|
|
|
next();
|
2016-04-13 10:23:46 +03:00
|
|
|
} else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
|
2003-01-06 23:21:42 +03:00
|
|
|
asm_parse_directive(s1);
|
|
|
|
} else if (tok == TOK_PPNUM) {
|
|
|
|
const char *p;
|
|
|
|
int n;
|
2015-11-21 14:23:53 +03:00
|
|
|
p = tokc.str.data;
|
2003-04-21 19:21:19 +04:00
|
|
|
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);
|
|
|
|
next();
|
|
|
|
skip(':');
|
|
|
|
goto redo;
|
|
|
|
} else if (tok >= TOK_IDENT) {
|
|
|
|
/* instruction or label */
|
|
|
|
opcode = tok;
|
|
|
|
next();
|
|
|
|
if (tok == ':') {
|
2016-05-01 22:38:38 +03:00
|
|
|
/* handle "extern void vide(void); __asm__("vide: ret");" as
|
|
|
|
"__asm__("globl vide\nvide: ret");" */
|
|
|
|
Sym *sym = sym_find(opcode);
|
|
|
|
if (sym && (sym->type.t & VT_EXTERN) && nocode_wanted) {
|
|
|
|
sym = label_find(opcode);
|
|
|
|
if (!sym) {
|
|
|
|
sym = label_push(&s1->asm_labels, opcode, 0);
|
|
|
|
sym->type.t = VT_VOID;
|
|
|
|
}
|
|
|
|
}
|
2003-01-06 23:21:42 +03:00
|
|
|
/* new label */
|
2010-05-06 23:42:37 +04:00
|
|
|
asm_new_label(s1, opcode, 0);
|
2003-01-06 23:21:42 +03:00
|
|
|
next();
|
|
|
|
goto redo;
|
2004-10-18 04:19:51 +04:00
|
|
|
} else if (tok == '=') {
|
|
|
|
int n;
|
|
|
|
next();
|
|
|
|
n = asm_int_expr(s1);
|
|
|
|
asm_new_label1(s1, opcode, 0, SHN_ABS, n);
|
|
|
|
goto redo;
|
2003-01-06 23:21:42 +03:00
|
|
|
} else {
|
|
|
|
asm_opcode(s1, opcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* end of line */
|
|
|
|
if (tok != ';' && tok != TOK_LINEFEED){
|
|
|
|
expect("end of line");
|
|
|
|
}
|
|
|
|
parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
asm_free_labels(s1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assemble the current file */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
|
2003-01-06 23:21:42 +03:00
|
|
|
{
|
2004-10-18 04:19:51 +04:00
|
|
|
Sym *define_start;
|
2003-01-06 23:21:42 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
preprocess_init(s1);
|
|
|
|
|
|
|
|
/* default section is text */
|
|
|
|
cur_text_section = text_section;
|
|
|
|
ind = cur_text_section->data_offset;
|
|
|
|
|
2004-10-18 04:19:51 +04:00
|
|
|
define_start = define_stack;
|
|
|
|
|
2009-12-19 21:12:33 +03:00
|
|
|
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
|
|
|
|
symbols can be safely used */
|
|
|
|
put_elf_sym(symtab_section, 0, 0,
|
|
|
|
ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
|
|
|
|
SHN_ABS, file->filename);
|
|
|
|
|
2003-01-06 23:21:42 +03:00
|
|
|
ret = tcc_assemble_internal(s1, do_preprocess);
|
|
|
|
|
|
|
|
cur_text_section->data_offset = ind;
|
2004-10-18 04:19:51 +04:00
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
free_defines(define_start);
|
2004-10-18 04:19:51 +04:00
|
|
|
|
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)
|
|
|
|
{
|
2010-01-14 22:58:03 +03:00
|
|
|
int saved_parse_flags;
|
|
|
|
const int *saved_macro_ptr;
|
2003-01-06 23:21:42 +03:00
|
|
|
|
|
|
|
saved_parse_flags = parse_flags;
|
|
|
|
saved_macro_ptr = macro_ptr;
|
2010-11-25 15:29:15 +03:00
|
|
|
|
2013-02-14 09:53:07 +04:00
|
|
|
tcc_open_bf(s1, ":asm:", len);
|
2010-11-25 15:29:15 +03:00
|
|
|
memcpy(file->buffer, str, len);
|
|
|
|
|
2003-01-06 23:21:42 +03:00
|
|
|
macro_ptr = NULL;
|
|
|
|
tcc_assemble_internal(s1, 0);
|
2010-11-25 15:29:15 +03:00
|
|
|
tcc_close();
|
2003-01-06 23:21:42 +03:00
|
|
|
|
|
|
|
parse_flags = saved_parse_flags;
|
|
|
|
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 */
|
2015-07-29 23:53:57 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
static void subst_asm_operands(ASMOperand *operands, int nb_operands,
|
2003-01-06 23:21:42 +03:00
|
|
|
int nb_outputs,
|
|
|
|
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')
|
|
|
|
modifier = *str++;
|
|
|
|
index = find_constraint(operands, nb_operands, str, &str);
|
|
|
|
if (index < 0)
|
2011-08-11 19:07:56 +04:00
|
|
|
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;
|
2007-11-21 20:16:31 +03:00
|
|
|
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(;;) {
|
|
|
|
if (nb_operands >= MAX_ASM_OPERANDS)
|
2011-08-11 19:07:56 +04:00
|
|
|
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(']');
|
|
|
|
}
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
expect("string constant");
|
2015-11-21 14:23:53 +03:00
|
|
|
op->constraint = tcc_malloc(tokc.str.size);
|
|
|
|
strcpy(op->constraint, tokc.str.data);
|
2003-01-06 23:21:42 +03:00
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
gexpr();
|
|
|
|
if (is_output) {
|
|
|
|
test_lvalue();
|
|
|
|
} else {
|
2004-10-18 04:19:51 +04:00
|
|
|
/* 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 ||
|
2004-10-18 04:19:51 +04:00
|
|
|
(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 */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC void asm_instr(void)
|
2003-01-06 23:21:42 +03:00
|
|
|
{
|
|
|
|
CString astr, astr1;
|
|
|
|
ASMOperand operands[MAX_ASM_OPERANDS];
|
2011-05-16 16:15:32 +04:00
|
|
|
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();
|
|
|
|
}
|
2004-10-18 04:19:51 +04:00
|
|
|
parse_asm_str(&astr);
|
2003-01-06 23:21:42 +03:00
|
|
|
nb_operands = 0;
|
|
|
|
nb_outputs = 0;
|
2004-10-18 04:19:51 +04:00
|
|
|
must_subst = 0;
|
2003-01-06 23:21:42 +03:00
|
|
|
memset(clobber_regs, 0, sizeof(clobber_regs));
|
|
|
|
if (tok == ':') {
|
|
|
|
next();
|
2004-10-18 04:19:51 +04:00
|
|
|
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();
|
2005-09-04 02:23:59 +04:00
|
|
|
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();
|
2005-09-04 02:23:59 +04:00
|
|
|
for(;;) {
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
expect("string constant");
|
2015-11-21 14:23:53 +03:00
|
|
|
asm_clobber(clobber_regs, tokc.str.data);
|
2003-01-06 23:21:42 +03:00
|
|
|
next();
|
2005-09-04 02:23:59 +04:00
|
|
|
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("';'");
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2003-01-06 23:21:42 +03:00
|
|
|
/* save all values in the memory */
|
|
|
|
save_regs(0);
|
|
|
|
|
|
|
|
/* compute constraints */
|
2015-07-29 23:53:57 +03:00
|
|
|
asm_compute_constraints(operands, nb_operands, nb_outputs,
|
2004-10-18 04:19:51 +04:00
|
|
|
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
|
2004-10-18 04:19:51 +04:00
|
|
|
if (must_subst) {
|
2003-04-15 02:22:34 +04:00
|
|
|
subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
|
|
|
|
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 */
|
2015-07-29 23:53:57 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* restore the current C token */
|
|
|
|
next();
|
|
|
|
|
|
|
|
/* store the output values if needed */
|
2015-07-29 23:53:57 +03:00
|
|
|
asm_gen_code(operands, nb_operands, nb_outputs, 1,
|
2004-10-18 04:19:51 +04:00
|
|
|
clobber_regs, out_reg);
|
2015-07-29 23:53:57 +03:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC void asm_global_instr(void)
|
2004-10-18 04:19:51 +04:00
|
|
|
{
|
|
|
|
CString astr;
|
|
|
|
|
|
|
|
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("';'");
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2004-10-18 04:19:51 +04:00
|
|
|
#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);
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2004-10-18 04:19:51 +04:00
|
|
|
cur_text_section->data_offset = ind;
|
|
|
|
|
|
|
|
/* restore the current C token */
|
|
|
|
next();
|
|
|
|
|
|
|
|
cstr_free(&astr);
|
|
|
|
}
|
2012-01-06 21:14:34 +04:00
|
|
|
#endif /* CONFIG_TCC_ASM */
|