added linux define - added __FUNCTION - fixed spaces parsing - defined special macros

This commit is contained in:
bellard 2002-08-29 21:16:24 +00:00
parent 5e85ad260d
commit 41391fbb20

107
tcc.c
View file

@ -501,15 +501,7 @@ enum {
TOK_UNDEF,
TOK_ERROR,
TOK_LINE,
TOK___LINE__,
TOK___FILE__,
TOK___DATE__,
TOK___TIME__,
TOK___VA_ARGS__,
/* special identifiers */
TOK___FUNC__,
TOK_MAIN,
#define DEF(id, str) id,
#include "tcctok.h"
#undef DEF
@ -524,8 +516,6 @@ char *tcc_keywords =
/* the following are not keywords. They are included to ease parsing */
"define\0include\0ifdef\0ifndef\0elif\0endif\0"
"defined\0undef\0error\0line\0"
"__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0"
"__func__\0main\0"
/* builtin functions */
#define DEF(id, str) str "\0"
#include "tcctok.h"
@ -1470,22 +1460,38 @@ static inline void inp(void)
// printf("ch1=%c 0x%x\n", ch1, ch1);
}
/* input with '\\n' handling */
static inline void minp(void)
/* handle '\\n' and '\\r\n' */
static void handle_stray(void)
{
redo:
do {
if (ch1 == '\n') {
inp();
} else if (ch1 == '\r') {
inp();
if (ch1 != '\n')
error("invalid character after '\\'");
inp();
} else {
break;
}
ch = ch1;
inp();
if (ch == '\\' && ch1 == '\n') {
} while (ch == '\\');
}
/* input with '\\n' handling. Also supports '\\r\n' for horrible MSDOS
case */
static inline void minp(void)
{
ch = ch1;
inp();
goto redo;
}
//printf("ch=%c 0x%x\n", ch, ch);
if (ch == '\\')
handle_stray();
}
/* same as minp, but also skip comments */
void cinp(void)
static void cinp(void)
{
int c;
@ -1494,14 +1500,13 @@ void cinp(void)
if (ch1 == '/') {
/* single line C++ comments */
inp();
while (ch1 != '\n' && ch1 != -1)
inp();
while (ch1 != '\n' && ch1 != CH_EOF)
inp();
ch = ' '; /* return space */
} else if (ch1 == '*') {
/* C comments */
inp();
while (ch1 != -1) {
while (ch1 != CH_EOF) {
c = ch1;
inp();
if (c == '*' && ch1 == '/') {
@ -1518,9 +1523,15 @@ void cinp(void)
}
}
void skip_spaces(void)
/* space exlcuding newline */
static inline int is_space(int ch)
{
while (ch == ' ' || ch == '\t')
return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
}
static inline void skip_spaces(void)
{
while (is_space(ch))
cinp();
}
@ -1532,7 +1543,7 @@ void preprocess_skip(void)
a = 0;
while (1) {
while (ch != '\n') {
if (ch == -1)
if (ch == CH_EOF)
expect("#endif");
cinp();
}
@ -1798,7 +1809,7 @@ void parse_define(void)
tok_str_new(&str);
while (1) {
skip_spaces();
if (ch == '\n' || ch == -1)
if (ch == '\n' || ch == CH_EOF)
break;
next_nomacro();
tok_str_add2(&str, tok, &tokc);
@ -2067,6 +2078,9 @@ static int getq(void)
error("invalid escaped char");
minp();
}
} else if (c == '\r' && ch == '\n') {
minp();
c = '\n';
}
return c;
}
@ -2384,15 +2398,14 @@ void next_nomacro1(void)
return;
}
cinp();
while (ch == ' ' || ch == '\t')
cinp();
skip_spaces();
if (ch == '#') {
/* preprocessor command if # at start of line after
spaces */
preprocess();
}
}
if (ch != ' ' && ch != '\t' && ch != '\f')
if (!is_space(ch))
break;
cinp();
}
@ -2660,6 +2673,12 @@ void macro_subst(TokenString *tok_str,
next_nomacro();
if (tok == 0)
break;
if ((s = sym_find1(&define_stack, tok)) != NULL) {
/* if symbol is a macro, prepare substitution */
/* if nested substitution, do nothing */
if (sym_find2(*nested_list, tok))
goto no_subst;
/* special macros */
if (tok == TOK___LINE__) {
cval.i = file->line_num;
@ -2669,10 +2688,13 @@ void macro_subst(TokenString *tok_str,
goto add_cstr;
tok_str_add2(tok_str, TOK_STR, &cval);
} else if (tok == TOK___DATE__) {
cstrval = "Jan 1 1970";
cstrval = "Jan 1 2002";
goto add_cstr;
} else if (tok == TOK___TIME__) {
cstrval = "00:00:00";
goto add_cstr;
} else if (tok == TOK___FUNCTION__) {
cstrval = funcname;
add_cstr:
cstr_new(&cstr);
cstr_cat(&cstr, cstrval);
@ -2680,11 +2702,7 @@ void macro_subst(TokenString *tok_str,
cval.cstr = &cstr;
tok_str_add2(tok_str, TOK_STR, &cval);
cstr_free(&cstr);
} else if ((s = sym_find1(&define_stack, tok)) != NULL) {
/* if symbol is a macro, prepare substitution */
/* if nested substitution, do nothing */
if (sym_find2(*nested_list, tok))
goto no_subst;
} else {
mstr = (int *)s->c;
mstr_allocated = 0;
if (s->t == MACRO_FUNC) {
@ -2693,7 +2711,7 @@ void macro_subst(TokenString *tok_str,
if (macro_ptr) {
t = *macro_ptr;
} else {
while (ch == ' ' || ch == '\t' || ch == '\n')
while (is_space(ch) || ch == '\n')
cinp();
t = ch;
}
@ -2767,6 +2785,7 @@ void macro_subst(TokenString *tok_str,
tcc_free(sa1);
if (mstr_allocated)
tok_str_free(mstr);
}
} else {
no_subst:
/* no need to add if reading input stream */
@ -2880,7 +2899,7 @@ void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long size)
vsetc(t, VT_CONST | VT_SYM, &cval);
}
/* push a reference to symbol v */
/* push a reference to global symbol v */
void vpush_sym(int t, int v)
{
Sym *sym;
@ -6524,7 +6543,7 @@ void decl(int l)
/* compile the C file opened in 'file'. Return non zero if errors. */
static int tcc_compile(TCCState *s)
{
Sym *define_start;
Sym *define_start, *sym;
char buf[512];
int p, section_sym;
@ -6560,7 +6579,8 @@ static int tcc_compile(TCCState *s)
char_pointer_type = mk_pointer(VT_BYTE);
/* define an old type function 'int func()' */
p = anon_sym++;
sym_push1(&global_stack, p, 0, FUNC_OLD);
sym = sym_push1(&global_stack, p, 0, FUNC_OLD);
sym->r = FUNC_CDECL;
func_old_type = VT_FUNC | (p << VT_STRUCT_SHIFT);
define_start = define_stack.top;
@ -6894,10 +6914,21 @@ TCCState *tcc_new(void)
p = r;
}
/* we add dummy defines for some special macros to speed up tests
and to have working defined() */
sym_push1(&define_stack, TOK___LINE__, MACRO_OBJ, 0);
sym_push1(&define_stack, TOK___FILE__, MACRO_OBJ, 0);
sym_push1(&define_stack, TOK___DATE__, MACRO_OBJ, 0);
sym_push1(&define_stack, TOK___TIME__, MACRO_OBJ, 0);
sym_push1(&define_stack, TOK___FUNCTION__, MACRO_OBJ, 0);
/* standard defines */
tcc_define_symbol(s, "__STDC__", NULL);
#if defined(TCC_TARGET_I386)
tcc_define_symbol(s, "__i386__", NULL);
#endif
#if defined(linux)
tcc_define_symbol(s, "linux", NULL);
#endif
/* tiny C specific defines */
tcc_define_symbol(s, "__TINYC__", NULL);