History

tccpp: cleanup

- "utf8 in identifiers" from 936819a1b90f2618bb3f86730189cf2895948ba0 - CValue: remove member str.data_allocated - make tiny allocator private to tccpp - allocate macro_stack objects on heap because otherwise it could crash after error/setjmp in preprocess_delete():end_macro() - mov "TinyAlloc" defs to tccpp.c - define_push: take int* str again

grischka committed 10 years ago

Commit 766ba361 parent eacdc425 files · +169 −184Browse files

Unified · Split

5 files
  1. libtcc.c +0 −2
  2. tcc.h +48 −93
  3. tccasm.c +7 −7
  4. tccgen.c +13 −13
  5. tccpp.c +101 −69
libtcc.c+0−2
@@ -2199,8 +2199,6 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) · 2198 unmodified lines
2199 s->dflag = 3;2199 s->dflag = 3;
2200 else if (*optarg == 'M')2200 else if (*optarg == 'M')
2201 s->dflag = 7;2201 s->dflag = 7;
2202 else if (*optarg == 'b')
2203 s->dflag = 8;
2204 else2202 else
2205 goto unsupported_option;2203 goto unsupported_option;
2206 break;2204 break;
tcc.h+48−93
@@ -36,13 +36,6 @@ · 35 unmodified lines
36#include <time.h>36#include <time.h>
37#include <sys/stat.h> /* stat() */37#include <sys/stat.h> /* stat() */
38 38
39#ifdef CONFIG_TCCASSERT
40#include <assert.h>
41#define TCC_ASSERT(ex) assert(ex)
42#else
43#define TCC_ASSERT(ex)
44#endif
45
46#ifndef _WIN3239#ifndef _WIN32
47# include <unistd.h>40# include <unistd.h>
48# include <sys/time.h>41# include <sys/time.h>
@@ -138,51 +131,6 @@ · 89 unmodified lines
138#include "stab.h"131#include "stab.h"
139#include "libtcc.h"132#include "libtcc.h"
140 133
141static inline uint16_t read16le(unsigned char *p)
142{
143 return p[0] | (uint16_t)p[1] << 8;
144}
145
146static inline void write16le(unsigned char *p, uint16_t x)
147{
148 p[0] = x & 255;
149 p[1] = x >> 8 & 255;
150}
151
152static inline uint32_t read32le(unsigned char *p)
153{
154 return (p[0] | (uint32_t)p[1] << 8 |
155 (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24);
156}
157
158static inline void write32le(unsigned char *p, uint32_t x)
159{
160 p[0] = x & 255;
161 p[1] = x >> 8 & 255;
162 p[2] = x >> 16 & 255;
163 p[3] = x >> 24 & 255;
164}
165
166static inline uint64_t read64le(unsigned char *p)
167{
168 return (p[0] | (uint64_t)p[1] << 8 |
169 (uint64_t)p[2] << 16 | (uint64_t)p[3] << 24 |
170 (uint64_t)p[4] << 32 | (uint64_t)p[5] << 40 |
171 (uint64_t)p[6] << 48 | (uint64_t)p[7] << 56);
172}
173
174static inline void write64le(unsigned char *p, uint64_t x)
175{
176 p[0] = x & 255;
177 p[1] = x >> 8 & 255;
178 p[2] = x >> 16 & 255;
179 p[3] = x >> 24 & 255;
180 p[4] = x >> 32 & 255;
181 p[5] = x >> 40 & 255;
182 p[6] = x >> 48 & 255;
183 p[7] = x >> 56 & 255;
184}
185
186/* parser debug */134/* parser debug */
187/* #define PARSE_DEBUG */135/* #define PARSE_DEBUG */
188/* preprocessor debug */136/* preprocessor debug */
@@ -399,7 +347,6 @@ typedef struct CString { · 210 unmodified lines
399 int size; /* size in bytes */347 int size; /* size in bytes */
400 void *data; /* either 'char *' or 'nwchar_t *' */348 void *data; /* either 'char *' or 'nwchar_t *' */
401 int size_allocated;349 int size_allocated;
402 void *data_allocated; /* if non NULL, data has been malloced */
403} CString;350} CString;
404 351
405/* type definition */352/* type definition */
@@ -417,7 +364,6 @@ typedef union CValue { · 11 unmodified lines
417 struct {364 struct {
418 int size;365 int size;
419 const void *data;366 const void *data;
420 void *data_allocated;
421 } str;367 } str;
422 int tab[LDOUBLE_SIZE/4];368 int tab[LDOUBLE_SIZE/4];
423} CValue;369} CValue;
@@ -638,43 +584,6 @@ struct sym_attr { · 214 unmodified lines
638#endif584#endif
639};585};
640 586
641#if !defined(MEM_DEBUG)
642#define tal_free(al, p) tal_free_impl(al, p)
643#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
644#define TAL_DEBUG_PARAMS
645#else
646#define TAL_DEBUG 1
647#define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
648#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
649#define TAL_DEBUG_PARAMS , const char *file, int line
650#define TAL_DEBUG_FILE_LEN 15
651#endif
652//#define TAL_INFO 1 /* collect and dump allocators stats */
653
654typedef struct TinyAlloc {
655 size_t limit;
656 size_t size;
657 uint8_t *buffer;
658 uint8_t *p;
659 size_t nb_allocs;
660 struct TinyAlloc *next, *top;
661#ifdef TAL_INFO
662 size_t nb_peak;
663 size_t nb_total;
664 size_t nb_missed;
665 uint8_t *peak_p;
666#endif
667} TinyAlloc;
668
669typedef struct tal_header_t {
670 size_t size;
671#ifdef TAL_DEBUG
672 int line_num; /* negative line_num used for double free check */
673 char file_name[TAL_DEBUG_FILE_LEN + 1];
674#endif
675} tal_header_t;
676
677
678struct TCCState {587struct TCCState {
679 588
680 int verbose; /* if true, display some information during compilation */589 int verbose; /* if true, display some information during compilation */
@@ -1204,7 +1113,7 @@ PUB_FUNC void tcc_warning(const char *fmt, ...); · 523 unmodified lines
1204/* other utilities */1113/* other utilities */
1205ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);1114ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);
1206ST_FUNC void dynarray_reset(void *pp, int *n);1115ST_FUNC void dynarray_reset(void *pp, int *n);
1207ST_FUNC void cstr_ccat(CString *cstr, int ch);1116ST_INLN void cstr_ccat(CString *cstr, int ch);
1208ST_FUNC void cstr_cat(CString *cstr, const char *str, int len);1117ST_FUNC void cstr_cat(CString *cstr, const char *str, int len);
1209ST_FUNC void cstr_wccat(CString *cstr, int ch);1118ST_FUNC void cstr_wccat(CString *cstr, int ch);
1210ST_FUNC void cstr_new(CString *cstr);1119ST_FUNC void cstr_new(CString *cstr);
@@ -1287,10 +1196,11 @@ ST_FUNC void end_macro(void); · 76 unmodified lines
1287ST_FUNC void save_parse_state(ParseState *s);1196ST_FUNC void save_parse_state(ParseState *s);
1288ST_FUNC void restore_parse_state(ParseState *s);1197ST_FUNC void restore_parse_state(ParseState *s);
1289ST_INLN void tok_str_new(TokenString *s);1198ST_INLN void tok_str_new(TokenString *s);
1199ST_FUNC TokenString *tok_str_alloc(void);
1290ST_FUNC void tok_str_free(int *str);1200ST_FUNC void tok_str_free(int *str);
1291ST_FUNC void tok_str_add(TokenString *s, int t);1201ST_FUNC void tok_str_add(TokenString *s, int t);
1292ST_FUNC void tok_str_add_tok(TokenString *s);1202ST_FUNC void tok_str_add_tok(TokenString *s);
1293ST_INLN void define_push(int v, int macro_type, TokenString *str, Sym *first_arg);1203ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
1294ST_FUNC void define_undef(Sym *s);1204ST_FUNC void define_undef(Sym *s);
1295ST_INLN Sym *define_find(int v);1205ST_INLN Sym *define_find(int v);
1296ST_FUNC void free_defines(Sym *b);1206ST_FUNC void free_defines(Sym *b);
@@ -1489,6 +1399,51 @@ ST_FUNC void gen_vla_sp_save(int addr); · 192 unmodified lines
1489ST_FUNC void gen_vla_sp_restore(int addr);1399ST_FUNC void gen_vla_sp_restore(int addr);
1490ST_FUNC void gen_vla_alloc(CType *type, int align);1400ST_FUNC void gen_vla_alloc(CType *type, int align);
1491 1401
1402static inline uint16_t read16le(unsigned char *p)
1403{
1404 return p[0] | (uint16_t)p[1] << 8;
1405}
1406
1407static inline void write16le(unsigned char *p, uint16_t x)
1408{
1409 p[0] = x & 255;
1410 p[1] = x >> 8 & 255;
1411}
1412
1413static inline uint32_t read32le(unsigned char *p)
1414{
1415 return (p[0] | (uint32_t)p[1] << 8 |
1416 (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24);
1417}
1418
1419static inline void write32le(unsigned char *p, uint32_t x)
1420{
1421 p[0] = x & 255;
1422 p[1] = x >> 8 & 255;
1423 p[2] = x >> 16 & 255;
1424 p[3] = x >> 24 & 255;
1425}
1426
1427static inline uint64_t read64le(unsigned char *p)
1428{
1429 return (p[0] | (uint64_t)p[1] << 8 |
1430 (uint64_t)p[2] << 16 | (uint64_t)p[3] << 24 |
1431 (uint64_t)p[4] << 32 | (uint64_t)p[5] << 40 |
1432 (uint64_t)p[6] << 48 | (uint64_t)p[7] << 56);
1433}
1434
1435static inline void write64le(unsigned char *p, uint64_t x)
1436{
1437 p[0] = x & 255;
1438 p[1] = x >> 8 & 255;
1439 p[2] = x >> 16 & 255;
1440 p[3] = x >> 24 & 255;
1441 p[4] = x >> 32 & 255;
1442 p[5] = x >> 40 & 255;
1443 p[6] = x >> 48 & 255;
1444 p[7] = x >> 56 & 255;
1445}
1446
1492/* ------------ i386-gen.c ------------ */1447/* ------------ i386-gen.c ------------ */
1493#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_641448#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1494ST_FUNC void g(int c);1449ST_FUNC void g(int c);
tccasm.c+7−7
@@ -488,25 +488,25 @@ static void asm_parse_directive(TCCState *s1) · 487 unmodified lines
488 case TOK_ASMDIR_rept:488 case TOK_ASMDIR_rept:
489 {489 {
490 int repeat;490 int repeat;
491 TokenString init_str;491 TokenString *init_str;
492 ParseState saved_parse_state = {0};492 ParseState saved_parse_state = {0};
493 next();493 next();
494 repeat = asm_int_expr(s1);494 repeat = asm_int_expr(s1);
495 tok_str_new(&init_str);495 init_str = tok_str_alloc();
496 next();496 next();
497 while ((tok != TOK_ASMDIR_endr) && (tok != CH_EOF)) {497 while ((tok != TOK_ASMDIR_endr) && (tok != CH_EOF)) {
498 tok_str_add_tok(&init_str);498 tok_str_add_tok(init_str);
499 next();499 next();
500 }500 }
501 if (tok == CH_EOF) tcc_error("we at end of file, .endr not found");501 if (tok == CH_EOF) tcc_error("we at end of file, .endr not found");
502 next();502 next();
503 tok_str_add(&init_str, -1);503 tok_str_add(init_str, -1);
504 tok_str_add(&init_str, 0);504 tok_str_add(init_str, 0);
505 save_parse_state(&saved_parse_state);505 save_parse_state(&saved_parse_state);
506 begin_macro(&init_str, 0);506 begin_macro(init_str, 1);
507 while (repeat-- > 0) {507 while (repeat-- > 0) {
508 tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS));508 tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS));
509 macro_ptr = init_str.str;509 macro_ptr = init_str->str;
510 }510 }
511 end_macro();511 end_macro();
512 restore_parse_state(&saved_parse_state);512 restore_parse_state(&saved_parse_state);
tccgen.c+13−13
@@ -5857,7 +5857,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 5856 unmodified lines
5857 int size, align, addr, data_offset;5857 int size, align, addr, data_offset;
5858 int level;5858 int level;
5859 ParseState saved_parse_state = {0};5859 ParseState saved_parse_state = {0};
5860 TokenString init_str;5860 TokenString *init_str = NULL;
5861 Section *sec;5861 Section *sec;
5862 Sym *flexible_array;5862 Sym *flexible_array;
5863 5863
@@ -5879,15 +5879,15 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 15 unmodified lines
5879 (e.g. string pointers or ISOC99 compound5879 (e.g. string pointers or ISOC99 compound
5880 literals). It also simplifies local5880 literals). It also simplifies local
5881 initializers handling */5881 initializers handling */
5882 tok_str_new(&init_str);
5883 if (size < 0 || (flexible_array && has_init)) {5882 if (size < 0 || (flexible_array && has_init)) {
5884 if (!has_init) 5883 if (!has_init)
5885 tcc_error("unknown type size");5884 tcc_error("unknown type size");
5886 /* get all init string */5885 /* get all init string */
5886 init_str = tok_str_alloc();
5887 if (has_init == 2) {5887 if (has_init == 2) {
5888 /* only get strings */5888 /* only get strings */
5889 while (tok == TOK_STR || tok == TOK_LSTR) {5889 while (tok == TOK_STR || tok == TOK_LSTR) {
5890 tok_str_add_tok(&init_str);5890 tok_str_add_tok(init_str);
5891 next();5891 next();
5892 }5892 }
5893 } else {5893 } else {
@@ -5895,7 +5895,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 1 unmodified line
5895 while (level > 0 || (tok != ',' && tok != ';')) {5895 while (level > 0 || (tok != ',' && tok != ';')) {
5896 if (tok < 0)5896 if (tok < 0)
5897 tcc_error("unexpected end of file in initializer");5897 tcc_error("unexpected end of file in initializer");
5898 tok_str_add_tok(&init_str);5898 tok_str_add_tok(init_str);
5899 if (tok == '{')5899 if (tok == '{')
5900 level++;5900 level++;
5901 else if (tok == '}') {5901 else if (tok == '}') {
@@ -5908,17 +5908,17 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 6 unmodified lines
5908 next();5908 next();
5909 }5909 }
5910 }5910 }
5911 tok_str_add(&init_str, -1);5911 tok_str_add(init_str, -1);
5912 tok_str_add(&init_str, 0);5912 tok_str_add(init_str, 0);
5913 5913
5914 /* compute size */5914 /* compute size */
5915 save_parse_state(&saved_parse_state);5915 save_parse_state(&saved_parse_state);
5916 5916
5917 begin_macro(&init_str, 0);5917 begin_macro(init_str, 1);
5918 next();5918 next();
5919 decl_initializer(type, NULL, 0, 1, 1);5919 decl_initializer(type, NULL, 0, 1, 1);
5920 /* prepare second initializer parsing */5920 /* prepare second initializer parsing */
5921 macro_ptr = init_str.str;5921 macro_ptr = init_str->str;
5922 next();5922 next();
5923 5923
5924 /* if still unknown size, error */5924 /* if still unknown size, error */
@@ -6076,17 +6076,17 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 151 unmodified lines
6076 }6076 }
6077 if (has_init || (type->t & VT_VLA)) {6077 if (has_init || (type->t & VT_VLA)) {
6078 decl_initializer(type, sec, addr, 1, 0);6078 decl_initializer(type, sec, addr, 1, 0);
6079 /* restore parse state if needed */
6080 if (init_str.str) {
6081 end_macro();
6082 restore_parse_state(&saved_parse_state);
6083 }
6084 /* patch flexible array member size back to -1, */6079 /* patch flexible array member size back to -1, */
6085 /* for possible subsequent similar declarations */6080 /* for possible subsequent similar declarations */
6086 if (flexible_array)6081 if (flexible_array)
6087 flexible_array->type.ref->c = -1;6082 flexible_array->type.ref->c = -1;
6088 }6083 }
6089 no_alloc: ;6084 no_alloc: ;
6085 /* restore parse state if needed */
6086 if (init_str) {
6087 end_macro();
6088 restore_parse_state(&saved_parse_state);
6089 }
6090}6090}
6091 6091
6092static void put_func_debug(Sym *sym)6092static void put_func_debug(Sym *sym)
tccpp.c+101−69
@@ -38,10 +38,6 @@ ST_DATA int total_bytes; · 37 unmodified lines
38ST_DATA int tok_ident;38ST_DATA int tok_ident;
39ST_DATA TokenSym **table_ident;39ST_DATA TokenSym **table_ident;
40 40
41ST_DATA TinyAlloc *toksym_alloc;
42ST_DATA TinyAlloc *tokstr_alloc;
43ST_DATA TinyAlloc *cstr_alloc;
44
45/* ------------------------------------------------------------------------- */41/* ------------------------------------------------------------------------- */
46 42
47static TokenSym *hash_ident[TOK_HASH_SIZE];43static TokenSym *hash_ident[TOK_HASH_SIZE];
@@ -53,6 +49,10 @@ static int pp_debug_tok, pp_debug_symv; · 5 unmodified lines
53static int pp_once;49static int pp_once;
54static void tok_print(const char *msg, const int *str);50static void tok_print(const char *msg, const int *str);
55 51
52static struct TinyAlloc *toksym_alloc;
53static struct TinyAlloc *tokstr_alloc;
54static struct TinyAlloc *cstr_alloc;
55
56/* isidnum_table flags: */56/* isidnum_table flags: */
57#define IS_SPC 157#define IS_SPC 1
58#define IS_ID 258#define IS_ID 2
@@ -110,31 +110,54 @@ ST_FUNC void expect(const char *msg) · 51 unmodified lines
110 tcc_error("%s expected", msg);110 tcc_error("%s expected", msg);
111}111}
112 112
113ST_FUNC void begin_macro(TokenString *str, int alloc)113/* ------------------------------------------------------------------------- */
114{114/* Custom allocator for tiny objects */
115 str->alloc = alloc;
116 str->prev = macro_stack;
117 str->prev_ptr = macro_ptr;
118 macro_ptr = str->str;
119 macro_stack = str;
120}
121 115
122ST_FUNC void end_macro(void)116#define USE_TAL
123{117
124 TokenString *str = macro_stack;118#ifndef USE_TAL
125 macro_stack = str->prev;119#define tal_free(al, p) tcc_free(p)
126 macro_ptr = str->prev_ptr;120#define tal_realloc(al, p, size) tcc_realloc(p, size)
127 if (str->alloc == 2) {121#define tal_new(a,b,c)
128 str->alloc = 3; /* just mark as finished */122#define tal_delete(a)
129 } else {123#else
130 tok_str_free(str->str);124#if !defined(MEM_DEBUG)
131 if (str->alloc == 1)125#define tal_free(al, p) tal_free_impl(al, p)
132 tcc_free(str);126#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
133 }127#define TAL_DEBUG_PARAMS
134}128#else
129#define TAL_DEBUG 1
130#define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
131#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
132#define TAL_DEBUG_PARAMS , const char *file, int line
133#define TAL_DEBUG_FILE_LEN 15
134#endif
135//#define TAL_INFO 1 /* collect and dump allocators stats */
136
137typedef struct TinyAlloc {
138 size_t limit;
139 size_t size;
140 uint8_t *buffer;
141 uint8_t *p;
142 size_t nb_allocs;
143 struct TinyAlloc *next, *top;
144#ifdef TAL_INFO
145 size_t nb_peak;
146 size_t nb_total;
147 size_t nb_missed;
148 uint8_t *peak_p;
149#endif
150} TinyAlloc;
151
152typedef struct tal_header_t {
153 size_t size;
154#ifdef TAL_DEBUG
155 int line_num; /* negative line_num used for double free check */
156 char file_name[TAL_DEBUG_FILE_LEN + 1];
157#endif
158} tal_header_t;
135 159
136/* ------------------------------------------------------------------------- */160/* ------------------------------------------------------------------------- */
137/* Custom allocator for tiny objects */
138 161
139ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t size)162ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t size)
140{163{
@@ -160,9 +183,10 @@ tail_call: · 19 unmodified lines
160#endif183#endif
161#ifdef TAL_DEBUG184#ifdef TAL_DEBUG
162 if (al->nb_allocs > 0) {185 if (al->nb_allocs > 0) {
186 uint8_t *p;
163 fprintf(stderr, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",187 fprintf(stderr, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
164 al->nb_allocs, al->limit);188 al->nb_allocs, al->limit);
165 uint8_t *p = al->buffer;189 p = al->buffer;
166 while (p < al->p) {190 while (p < al->p) {
167 tal_header_t *header = (tal_header_t *)p;191 tal_header_t *header = (tal_header_t *)p;
168 if (header->line_num > 0) {192 if (header->line_num > 0) {
@@ -222,10 +246,10 @@ tail_call: · 53 unmodified lines
222 header = (tal_header_t *)al->p;246 header = (tal_header_t *)al->p;
223 header->size = adj_size;247 header->size = adj_size;
224#ifdef TAL_DEBUG248#ifdef TAL_DEBUG
225 int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;249 { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
226 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);250 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
227 header->file_name[TAL_DEBUG_FILE_LEN] = 0;251 header->file_name[TAL_DEBUG_FILE_LEN] = 0;
228 header->line_num = line;252 header->line_num = line; }
229#endif253#endif
230 ret = al->p + sizeof(tal_header_t);254 ret = al->p + sizeof(tal_header_t);
231 al->p += adj_size + sizeof(tal_header_t);255 al->p += adj_size + sizeof(tal_header_t);
@@ -286,26 +310,25 @@ tail_call: · 54 unmodified lines
286 return ret;310 return ret;
287}311}
288 312
313#endif /* USE_TAL */
314
289/* ------------------------------------------------------------------------- */315/* ------------------------------------------------------------------------- */
290/* CString handling */316/* CString handling */
291static void cstr_realloc(CString *cstr, int new_size)317static void cstr_realloc(CString *cstr, int new_size)
292{318{
293 int size;319 int size;
294 void *data;
295 320
296 size = cstr->size_allocated;321 size = cstr->size_allocated;
297 if (size < 8)322 if (size < 8)
298 size = 8; /* no need to allocate a too small first string */323 size = 8; /* no need to allocate a too small first string */
299 while (size < new_size)324 while (size < new_size)
300 size = size * 2;325 size = size * 2;
301 data = tal_realloc(cstr_alloc, cstr->data_allocated, size);326 cstr->data = tal_realloc(cstr_alloc, cstr->data, size);
302 cstr->data_allocated = data;
303 cstr->size_allocated = size;327 cstr->size_allocated = size;
304 cstr->data = data;
305}328}
306 329
307/* add a byte */330/* add a byte */
308ST_FUNC void cstr_ccat(CString *cstr, int ch)331ST_INLN void cstr_ccat(CString *cstr, int ch)
309{332{
310 int size;333 int size;
311 size = cstr->size + 1;334 size = cstr->size + 1;
@@ -346,7 +369,7 @@ ST_FUNC void cstr_new(CString *cstr) · 34 unmodified lines
346/* free string and reset it to NULL */369/* free string and reset it to NULL */
347ST_FUNC void cstr_free(CString *cstr)370ST_FUNC void cstr_free(CString *cstr)
348{371{
349 tal_free(cstr_alloc, cstr->data_allocated);372 tal_free(cstr_alloc, cstr->data);
350 cstr_new(cstr);373 cstr_new(cstr);
351}374}
352 375
@@ -1026,6 +1049,13 @@ ST_INLN void tok_str_new(TokenString *s) · 673 unmodified lines
1026 s->last_line_num = -1;1049 s->last_line_num = -1;
1027}1050}
1028 1051
1052ST_FUNC TokenString *tok_str_alloc(void)
1053{
1054 TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
1055 tok_str_new(str);
1056 return str;
1057}
1058
1029ST_FUNC int *tok_str_dup(TokenString *s)1059ST_FUNC int *tok_str_dup(TokenString *s)
1030{1060{
1031 int *str;1061 int *str;
@@ -1049,7 +1079,6 @@ ST_FUNC int *tok_str_realloc(TokenString *s, int new_size) · 17 unmodified lines
1049 size = 16;1079 size = 16;
1050 while (size < new_size)1080 while (size < new_size)
1051 size = size * 2;1081 size = size * 2;
1052 TCC_ASSERT((size & (size -1)) == 0);
1053 if (size > s->allocated_len) {1082 if (size > s->allocated_len) {
1054 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));1083 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
1055 s->allocated_len = size;1084 s->allocated_len = size;
@@ -1070,6 +1099,29 @@ ST_FUNC void tok_str_add(TokenString *s, int t) · 14 unmodified lines
1070 s->len = len;1099 s->len = len;
1071}1100}
1072 1101
1102ST_FUNC void begin_macro(TokenString *str, int alloc)
1103{
1104 str->alloc = alloc;
1105 str->prev = macro_stack;
1106 str->prev_ptr = macro_ptr;
1107 macro_ptr = str->str;
1108 macro_stack = str;
1109}
1110
1111ST_FUNC void end_macro(void)
1112{
1113 TokenString *str = macro_stack;
1114 macro_stack = str->prev;
1115 macro_ptr = str->prev_ptr;
1116 if (str->alloc == 2) {
1117 str->alloc = 3; /* just mark as finished */
1118 } else {
1119 tok_str_free(str->str);
1120 if (str->alloc == 1)
1121 tal_free(tokstr_alloc, str);
1122 }
1123}
1124
1073static void tok_str_add2(TokenString *s, int t, CValue *cv)1125static void tok_str_add2(TokenString *s, int t, CValue *cv)
1074{1126{
1075 int len, *str;1127 int len, *str;
@@ -1172,7 +1224,6 @@ static inline void TOK_GET(int *t, const int **pp, CValue *cv) · 96 unmodified lines
1172 case TOK_PPSTR:1224 case TOK_PPSTR:
1173 cv->str.size = *p++;1225 cv->str.size = *p++;
1174 cv->str.data = p;1226 cv->str.data = p;
1175 cv->str.data_allocated = 0;
1176 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);1227 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1177 break;1228 break;
1178 case TOK_CDOUBLE:1229 case TOK_CDOUBLE:
@@ -1235,13 +1286,13 @@ static int macro_is_equal(const int *a, const int *b) · 56 unmodified lines
1235}1286}
1236 1287
1237/* defines handling */1288/* defines handling */
1238ST_INLN void define_push(int v, int macro_type, TokenString *str, Sym *first_arg)1289ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1239{1290{
1240 Sym *s, *o;1291 Sym *s, *o;
1241 1292
1242 o = define_find(v);1293 o = define_find(v);
1243 s = sym_push2(&define_stack, v, macro_type, 0);1294 s = sym_push2(&define_stack, v, macro_type, 0);
1244 s->d = str ? tok_str_dup(str) : NULL;1295 s->d = str;
1245 s->next = first_arg;1296 s->next = first_arg;
1246 table_ident[v - TOK_IDENT]->sym_define = s;1297 table_ident[v - TOK_IDENT]->sym_define = s;
1247 1298
@@ -1344,9 +1395,9 @@ ST_FUNC void label_pop(Sym **ptop, Sym *slast) · 96 unmodified lines
1344static int expr_preprocess(void)1395static int expr_preprocess(void)
1345{1396{
1346 int c, t;1397 int c, t;
1347 TokenString str;1398 TokenString *str;
1348 1399
1349 tok_str_new(&str);1400 str = tok_str_alloc();
1350 while (tok != TOK_LINEFEED && tok != TOK_EOF) {1401 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1351 next(); /* do macro subst */1402 next(); /* do macro subst */
1352 if (tok == TOK_DEFINED) {1403 if (tok == TOK_DEFINED) {
@@ -1364,12 +1415,12 @@ static int expr_preprocess(void) · 11 unmodified lines
1364 tok = TOK_CINT;1415 tok = TOK_CINT;
1365 tokc.i = 0;1416 tokc.i = 0;
1366 }1417 }
1367 tok_str_add_tok(&str);1418 tok_str_add_tok(str);
1368 }1419 }
1369 tok_str_add(&str, -1); /* simulate end of file */1420 tok_str_add(str, -1); /* simulate end of file */
1370 tok_str_add(&str, 0);1421 tok_str_add(str, 0);
1371 /* now evaluate C constant expression */1422 /* now evaluate C constant expression */
1372 begin_macro(&str, 0);1423 begin_macro(str, 1);
1373 next();1424 next();
1374 c = expr_const();1425 c = expr_const();
1375 end_macro();1426 end_macro();
@@ -1457,7 +1508,7 @@ ST_FUNC void parse_define(void) · 81 unmodified lines
1457 if (3 == spc)1508 if (3 == spc)
1458bad_twosharp:1509bad_twosharp:
1459 tcc_error("'##' cannot appear at either end of macro");1510 tcc_error("'##' cannot appear at either end of macro");
1460 define_push(v, t, &tokstr_buf, first);1511 define_push(v, t, tok_str_dup(&tokstr_buf), first);
1461}1512}
1462 1513
1463static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)1514static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
@@ -2061,7 +2112,6 @@ static void parse_string(const char *s, int len) · 597 unmodified lines
2061 } else {2112 } else {
2062 tokc.str.size = tokcstr.size;2113 tokc.str.size = tokcstr.size;
2063 tokc.str.data = tokcstr.data;2114 tokc.str.data = tokcstr.data;
2064 tokc.str.data_allocated = tokcstr.data_allocated;
2065 if (!is_long)2115 if (!is_long)
2066 tok = TOK_STR;2116 tok = TOK_STR;
2067 else2117 else
@@ -2403,11 +2453,6 @@ static inline void next_nomacro1(void) · 335 unmodified lines
2403 p = file->buf_ptr;2453 p = file->buf_ptr;
2404 redo_no_start:2454 redo_no_start:
2405 c = *p;2455 c = *p;
2406#if (__TINYC__ || __GNUC__)
2407#else
2408 if (c & 0x80)
2409 goto parse_ident_fast;
2410#endif
2411 switch(c) {2456 switch(c) {
2412 case ' ':2457 case ' ':
2413 case '\t':2458 case '\t':
@@ -2513,13 +2558,6 @@ maybe_newline: · 99 unmodified lines
2513 || (parse_flags & PARSE_FLAG_ASM_FILE))2558 || (parse_flags & PARSE_FLAG_ASM_FILE))
2514 goto parse_simple;2559 goto parse_simple;
2515 2560
2516#if (__TINYC__ || __GNUC__)
2517 case 'a' ... 'z':
2518 case 'A' ... 'K':
2519 case 'M' ... 'Z':
2520 case '_':
2521 case 0x80 ... 0xFF:
2522#else
2523 case 'a': case 'b': case 'c': case 'd':2561 case 'a': case 'b': case 'c': case 'd':
2524 case 'e': case 'f': case 'g': case 'h':2562 case 'e': case 'f': case 'g': case 'h':
2525 case 'i': case 'j': case 'k': case 'l':2563 case 'i': case 'j': case 'k': case 'l':
@@ -2535,7 +2573,6 @@ maybe_newline: · 9 unmodified lines
2535 case 'U': case 'V': case 'W': case 'X':2573 case 'U': case 'V': case 'W': case 'X':
2536 case 'Y': case 'Z': 2574 case 'Y': case 'Z':
2537 case '_':2575 case '_':
2538#endif
2539 parse_ident_fast:2576 parse_ident_fast:
2540 p1 = p;2577 p1 = p;
2541 h = TOK_HASH_INIT;2578 h = TOK_HASH_INIT;
@@ -2617,7 +2654,6 @@ maybe_newline: · 75 unmodified lines
2617 cstr_ccat(&tokcstr, '\0');2654 cstr_ccat(&tokcstr, '\0');
2618 tokc.str.size = tokcstr.size;2655 tokc.str.size = tokcstr.size;
2619 tokc.str.data = tokcstr.data;2656 tokc.str.data = tokcstr.data;
2620 tokc.str.data_allocated = tokcstr.data_allocated;
2621 tok = TOK_PPNUM;2657 tok = TOK_PPNUM;
2622 break;2658 break;
2623 2659
@@ -2658,7 +2694,6 @@ maybe_newline: · 34 unmodified lines
2658 cstr_ccat(&tokcstr, '\0');2694 cstr_ccat(&tokcstr, '\0');
2659 tokc.str.size = tokcstr.size;2695 tokc.str.size = tokcstr.size;
2660 tokc.str.data = tokcstr.data;2696 tokc.str.data = tokcstr.data;
2661 tokc.str.data_allocated = tokcstr.data_allocated;
2662 tok = TOK_PPSTR;2697 tok = TOK_PPSTR;
2663 break;2698 break;
2664 2699
@@ -2796,6 +2831,8 @@ maybe_newline: · 131 unmodified lines
2796 p++;2831 p++;
2797 break;2832 break;
2798 default:2833 default:
2834 if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
2835 goto parse_ident_fast;
2799 if (parse_flags & PARSE_FLAG_ASM_FILE)2836 if (parse_flags & PARSE_FLAG_ASM_FILE)
2800 goto parse_simple;2837 goto parse_simple;
2801 tcc_error("unrecognized character \\x%02x", c);2838 tcc_error("unrecognized character \\x%02x", c);
@@ -2895,7 +2932,6 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) · 93 unmodified lines
2895 /* add string */2932 /* add string */
2896 cval.str.size = cstr.size;2933 cval.str.size = cstr.size;
2897 cval.str.data = cstr.data;2934 cval.str.data = cstr.data;
2898 cval.str.data_allocated = cstr.data_allocated;
2899 tok_str_add2(&str, TOK_PPSTR, &cval);2935 tok_str_add2(&str, TOK_PPSTR, &cval);
2900 cstr_free(&cstr);2936 cstr_free(&cstr);
2901 } else {2937 } else {
@@ -3062,7 +3098,6 @@ static int macro_subst_tok( · 160 unmodified lines
3062 cstr_cat(&cstr, cstrval, 0);3098 cstr_cat(&cstr, cstrval, 0);
3063 cval.str.size = cstr.size;3099 cval.str.size = cstr.size;
3064 cval.str.data = cstr.data;3100 cval.str.data = cstr.data;
3065 cval.str.data_allocated = cstr.data_allocated;
3066 tok_str_add2(tok_str, t1, &cval);3101 tok_str_add2(tok_str, t1, &cval);
3067 cstr_free(&cstr);3102 cstr_free(&cstr);
3068 } else {3103 } else {
@@ -3381,7 +3416,6 @@ ST_FUNC void next(void) · 312 unmodified lines
3381 if (s) {3416 if (s) {
3382 Sym *nested_list = NULL;3417 Sym *nested_list = NULL;
3383 tokstr_buf.len = 0;3418 tokstr_buf.len = 0;
3384 nested_list = NULL;
3385 macro_subst_tok(&tokstr_buf, &nested_list, s, 1);3419 macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
3386 tok_str_add(&tokstr_buf, 0);3420 tok_str_add(&tokstr_buf, 0);
3387 begin_macro(&tokstr_buf, 2);3421 begin_macro(&tokstr_buf, 2);
@@ -3402,8 +3436,8 @@ ST_FUNC void next(void) · 14 unmodified lines
3402 identifier case handled for labels. */3436 identifier case handled for labels. */
3403ST_INLN void unget_tok(int last_tok)3437ST_INLN void unget_tok(int last_tok)
3404{3438{
3405 TokenString *str = tcc_malloc(sizeof *str);3439
3406 tok_str_new(str);3440 TokenString *str = tok_str_alloc();
3407 tok_str_add2(str, tok, &tokc);3441 tok_str_add2(str, tok, &tokc);
3408 tok_str_add(str, 0);3442 tok_str_add(str, 0);
3409 begin_macro(str, 1);3443 begin_macro(str, 1);
@@ -3529,10 +3563,8 @@ static void tok_print(const char *msg, const int *str) · 119 unmodified lines
3529static void pp_line(TCCState *s1, BufferedFile *f, int level)3563static void pp_line(TCCState *s1, BufferedFile *f, int level)
3530{3564{
3531 int d = f->line_num - f->line_ref;3565 int d = f->line_num - f->line_ref;
3532
3533 if (s1->dflag & 4)3566 if (s1->dflag & 4)
3534 return;3567 return;
3535
3536 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {3568 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3537 if (level == 0 && f->line_ref && d) {3569 if (level == 0 && f->line_ref && d) {
3538 d = 1;3570 d = 1;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t