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