From 4e5170d4a59a6cadb3d11d918f5dd674141e3266 Mon Sep 17 00:00:00 2001 From: grischka Date: Thu, 14 Jan 2010 20:58:03 +0100 Subject: [PATCH] tccpp: convert TOK_GET macro into function --- tcc.h | 6 +-- tccasm.c | 3 +- tccpp.c | 137 +++++++++++++++++++++++++++---------------------------- 3 files changed, 71 insertions(+), 75 deletions(-) diff --git a/tcc.h b/tcc.h index 55f3626..3481f64 100644 --- a/tcc.h +++ b/tcc.h @@ -191,7 +191,7 @@ typedef union CValue { unsigned long long ull; struct CString *cstr; void *ptr; - int tab[1]; + int tab[2]; } CValue; /* value on stack */ @@ -334,7 +334,7 @@ typedef struct BufferedFile { /* parsing state (used to save parser state to reparse part of the source several times) */ typedef struct ParseState { - int *macro_ptr; + const int *macro_ptr; int line_num; int tok; CValue tokc; @@ -920,7 +920,7 @@ ST_FUNC int ieee_finite(double d); ST_DATA struct BufferedFile *file; ST_DATA int ch, tok; ST_DATA CValue tokc; -ST_DATA int *macro_ptr; +ST_DATA const int *macro_ptr; ST_DATA int parse_flags; ST_DATA int tok_flags; ST_DATA CString tokcstr; /* current parsed string, if any */ diff --git a/tccasm.c b/tccasm.c index 28161be..d2997a4 100644 --- a/tccasm.c +++ b/tccasm.c @@ -738,7 +738,8 @@ ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess) static void tcc_assemble_inline(TCCState *s1, char *str, int len) { BufferedFile *bf, *saved_file; - int saved_parse_flags, *saved_macro_ptr; + int saved_parse_flags; + const int *saved_macro_ptr; bf = tcc_malloc(sizeof(BufferedFile)); memset(bf, 0, sizeof(BufferedFile)); diff --git a/tccpp.c b/tccpp.c index 56b99d0..10b6dda 100644 --- a/tccpp.c +++ b/tccpp.c @@ -42,7 +42,7 @@ ST_DATA int parse_flags; ST_DATA struct BufferedFile *file; ST_DATA int ch, tok; ST_DATA CValue tokc; -ST_DATA int *macro_ptr; +ST_DATA const int *macro_ptr; ST_DATA CString tokcstr; /* current parsed string, if any */ /* display benchmark infos */ @@ -54,7 +54,7 @@ ST_DATA TokenSym **table_ident; /* ------------------------------------------------------------------------- */ static int *macro_ptr_allocated; -static int *unget_saved_macro_ptr; +static const int *unget_saved_macro_ptr; static int unget_saved_buffer[TOK_MAX_SIZE + 1]; static int unget_buffer_enabled; static TokenSym *hash_ident[TOK_HASH_SIZE]; @@ -75,7 +75,7 @@ static const unsigned char tok_two_chars[] = struct macro_level { struct macro_level *prev; - int *p; + const int *p; }; ST_FUNC void next_nomacro(void); @@ -940,61 +940,54 @@ ST_FUNC void tok_str_add_tok(TokenString *s) tok_str_add2(s, tok, &tokc); } -#if LDOUBLE_SIZE == 16 -#define LDOUBLE_GET(p, cv) \ - cv.tab[0] = p[0]; \ - cv.tab[1] = p[1]; \ - cv.tab[2] = p[2]; \ - cv.tab[3] = p[3]; -#elif LDOUBLE_SIZE == 12 -#define LDOUBLE_GET(p, cv) \ - cv.tab[0] = p[0]; \ - cv.tab[1] = p[1]; \ - cv.tab[2] = p[2]; -#elif LDOUBLE_SIZE == 8 -#define LDOUBLE_GET(p, cv) \ - cv.tab[0] = p[0]; \ - cv.tab[1] = p[1]; -#else -#error add long double size support -#endif - - /* get a token from an integer array and increment pointer accordingly. we code it as a macro to avoid pointer aliasing. */ -#define TOK_GET(t, p, cv) \ -{ \ - t = *p++; \ - switch(t) { \ - case TOK_CINT: \ - case TOK_CUINT: \ - case TOK_CCHAR: \ - case TOK_LCHAR: \ - case TOK_CFLOAT: \ - case TOK_LINENUM: \ - cv.tab[0] = *p++; \ - break; \ - case TOK_STR: \ - case TOK_LSTR: \ - case TOK_PPNUM: \ - cv.cstr = (CString *)p; \ - cv.cstr->data = (char *)p + sizeof(CString);\ - p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\ - break; \ - case TOK_CDOUBLE: \ - case TOK_CLLONG: \ - case TOK_CULLONG: \ - cv.tab[0] = p[0]; \ - cv.tab[1] = p[1]; \ - p += 2; \ - break; \ - case TOK_CLDOUBLE: \ - LDOUBLE_GET(p, cv); \ - p += LDOUBLE_SIZE / 4; \ - break; \ - default: \ - break; \ - } \ +static inline void TOK_GET(int *t, const int **pp, CValue *cv) +{ + const int *p = *pp; + int n, *tab; + + tab = cv->tab; + switch(*t = *p++) { + case TOK_CINT: + case TOK_CUINT: + case TOK_CCHAR: + case TOK_LCHAR: + case TOK_CFLOAT: + case TOK_LINENUM: + tab[0] = *p++; + break; + case TOK_STR: + case TOK_LSTR: + case TOK_PPNUM: + cv->cstr = (CString *)p; + cv->cstr->data = (char *)p + sizeof(CString); + p += (sizeof(CString) + cv->cstr->size + 3) >> 2; + break; + case TOK_CDOUBLE: + case TOK_CLLONG: + case TOK_CULLONG: + n = 2; + goto copy; + case TOK_CLDOUBLE: +#if LDOUBLE_SIZE == 16 + n = 4; +#elif LDOUBLE_SIZE == 12 + n = 3; +#elif LDOUBLE_SIZE == 8 + n = 2; +#else +# error add long double size support +#endif + copy: + do + *tab++ = *p++; + while (--n); + break; + default: + break; + } + *pp = p; } static int macro_is_equal(const int *a, const int *b) @@ -1003,9 +996,9 @@ static int macro_is_equal(const int *a, const int *b) CValue cv; int t; while (*a && *b) { - TOK_GET(t, a, cv); + TOK_GET(&t, &a, &cv); pstrcpy(buf, sizeof buf, get_tok_str(t, &cv)); - TOK_GET(t, b, cv); + TOK_GET(&t, &b, &cv); if (strcmp(buf, get_tok_str(t, &cv))) return 0; } @@ -1163,7 +1156,7 @@ static void tok_print(int *str) printf("<"); while (1) { - TOK_GET(t, str, cval); + TOK_GET(&t, &str, &cval); if (!t) break; printf("%s", get_tok_str(t, &cval)); @@ -2498,7 +2491,7 @@ static void next_nomacro_spc(void) redo: tok = *macro_ptr; if (tok) { - TOK_GET(tok, macro_ptr, tokc); + TOK_GET(&tok, ¯o_ptr, &tokc); if (tok == TOK_LINENUM) { file->line_num = tokc.i; goto redo; @@ -2517,9 +2510,10 @@ ST_FUNC void next_nomacro(void) } /* substitute args in macro_str and return allocated string */ -static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) +static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) { - int *st, last_tok, t, spc; + int last_tok, t, spc; + const int *st; Sym *s; CValue cval; TokenString str; @@ -2528,12 +2522,12 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) tok_str_new(&str); last_tok = 0; while(1) { - TOK_GET(t, macro_str, cval); + TOK_GET(&t, ¯o_str, &cval); if (!t) break; if (t == '#') { /* stringize */ - TOK_GET(t, macro_str, cval); + TOK_GET(&t, ¯o_str, &cval); if (!t) break; s = sym_find2(args, t); @@ -2542,7 +2536,7 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) st = s->d; spc = 0; while (*st) { - TOK_GET(t, st, cval); + TOK_GET(&t, &st, &cval); if (!check_space(t, &spc)) cstr_cat(&cstr, get_tok_str(t, &cval)); } @@ -2584,7 +2578,7 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) int t1; add_var: for(;;) { - TOK_GET(t1, st, cval); + TOK_GET(&t1, &st, &cval); if (!t1) break; tok_str_add2(&str, t1, &cval); @@ -2621,7 +2615,8 @@ static int macro_subst_tok(TokenString *tok_str, Sym **nested_list, Sym *s, struct macro_level **can_read_stream) { Sym *args, *sa, *sa1; - int mstr_allocated, parlevel, *mstr, t, t1, *p, spc; + int mstr_allocated, parlevel, *mstr, t, t1, spc; + const int *p; TokenString str; char *cstrval; CValue cval; @@ -2785,7 +2780,7 @@ static inline int *macro_twosharps(const int *macro_str) /* we search the first '##' */ for(ptr = macro_str;;) { - TOK_GET(t, ptr, cval); + TOK_GET(&t, &ptr, &cval); if (t == TOK_TWOSHARPS) break; /* nothing more to do if end of string */ @@ -2796,7 +2791,7 @@ static inline int *macro_twosharps(const int *macro_str) /* we saw '##', so we need more processing to handle it */ tok_str_new(¯o_str1); for(ptr = macro_str;;) { - TOK_GET(tok, ptr, tokc); + TOK_GET(&tok, &ptr, &tokc); if (tok == 0) break; if (tok == TOK_TWOSHARPS) @@ -2804,7 +2799,7 @@ static inline int *macro_twosharps(const int *macro_str) while (*ptr == TOK_TWOSHARPS) { t = *++ptr; if (t && t != TOK_TWOSHARPS) { - TOK_GET(t, ptr, cval); + TOK_GET(&t, &ptr, &cval); /* We concatenate the two tokens */ cstr_new(&cstr); @@ -2859,7 +2854,7 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, file stream due to a macro function call */ if (ptr == NULL) break; - TOK_GET(t, ptr, cval); + TOK_GET(&t, &ptr, &cval); if (t == 0) break; s = define_find(t);