History

tccgen: asm_label cleanup

- avoid memory allocation by using its (int) token number - avoid additional function parameter by using Attribute

grischka committed 10 years ago

Commit 54cf57a1 parent 992cbda4 files · +42 −42Browse files

Unified · Split

4 files
  1. libtcc.c +1 −1
  2. tcc.h +2 −1
  3. tccgen.c +30 −39
  4. tccpp.c +9 −1
libtcc.c+1−1
@@ -680,7 +680,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, · 679 unmodified lines
680 name = buf1;680 name = buf1;
681 }681 }
682 if (sym->asm_label) {682 if (sym->asm_label) {
683 name = sym->asm_label;683 name = get_tok_str(sym->asm_label, NULL);
684 }684 }
685 info = ELFW(ST_INFO)(sym_bind, sym_type);685 info = ELFW(ST_INFO)(sym_bind, sym_type);
686 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);686 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
tcc.h+2−1
@@ -434,12 +434,13 @@ typedef struct AttributeDef { · 433 unmodified lines
434 struct Attribute a;434 struct Attribute a;
435 struct Section *section;435 struct Section *section;
436 int alias_target; /* token */436 int alias_target; /* token */
437 int asm_label; /* associated asm label */
437} AttributeDef;438} AttributeDef;
438 439
439/* symbol management */440/* symbol management */
440typedef struct Sym {441typedef struct Sym {
441 int v; /* symbol token */442 int v; /* symbol token */
442 char *asm_label; /* associated asm label */443 int asm_label; /* associated asm label */
443 union {444 union {
444 long r; /* associated register */445 long r; /* associated register */
445 struct Attribute a;446 struct Attribute a;
tccgen.c+30−39
@@ -81,7 +81,7 @@ static void type_decl(CType *type, AttributeDef *ad, int *v, int td); · 80 unmodified lines
81static void parse_expr_type(CType *type);81static void parse_expr_type(CType *type);
82static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);82static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
83static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);83static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
84static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);84static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
85static int decl0(int l, int is_for_loop_init);85static int decl0(int l, int is_for_loop_init);
86static void expr_eq(void);86static void expr_eq(void);
87static void unary_type(CType *type);87static void unary_type(CType *type);
@@ -158,7 +158,6 @@ static inline Sym *sym_malloc(void) · 70 unmodified lines
158ST_INLN void sym_free(Sym *sym)158ST_INLN void sym_free(Sym *sym)
159{159{
160 sym->next = sym_free_first;160 sym->next = sym_free_first;
161 tcc_free(sym->asm_label);
162 sym_free_first = sym;161 sym_free_first = sym;
163}162}
164 163
@@ -173,7 +172,7 @@ ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c) · 8 unmodified lines
173 get_tok_str(v, NULL));172 get_tok_str(v, NULL));
174 }173 }
175 s = sym_malloc();174 s = sym_malloc();
176 s->asm_label = NULL;175 s->asm_label = 0;
177 s->v = v;176 s->v = v;
178 s->type.t = t;177 s->type.t = t;
179 s->type.ref = NULL;178 s->type.ref = NULL;
@@ -443,10 +442,8 @@ ST_FUNC Sym *external_global_sym(int v, CType *type, int r) · 263 unmodified lines
443 return s;442 return s;
444}443}
445 444
446/* define a new external reference to a symbol 'v' with alternate asm445/* define a new external reference to a symbol 'v' */
447 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there446static Sym *external_sym(int v, CType *type, int r)
448 is no alternate name (most cases) */
449static Sym *external_sym(int v, CType *type, int r, char *asm_label)
450{447{
451 Sym *s;448 Sym *s;
452 449
@@ -454,7 +451,6 @@ static Sym *external_sym(int v, CType *type, int r, char *asm_label) · 1 unmodified line
454 if (!s) {451 if (!s) {
455 /* push forward reference */452 /* push forward reference */
456 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);453 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
457 s->asm_label = asm_label ? tcc_strdup(asm_label) : 0;
458 s->type.t |= VT_EXTERN;454 s->type.t |= VT_EXTERN;
459 } else if (s->type.ref == func_old_type.ref) {455 } else if (s->type.ref == func_old_type.ref) {
460 s->type.ref = type->ref;456 s->type.ref = type->ref;
@@ -3386,16 +3382,21 @@ ST_FUNC void parse_asm_str(CString *astr) · 2925 unmodified lines
3386 cstr_ccat(astr, '\0');3382 cstr_ccat(astr, '\0');
3387}3383}
3388 3384
3389/* Parse an asm label and return the label3385/* Parse an asm label and return the token */
3390 * Don't forget to free the CString in the caller! */3386static int asm_label_instr(void)
3391static void asm_label_instr(CString *astr)
3392{3387{
3388 int v;
3389 CString astr;
3390
3393 next();3391 next();
3394 parse_asm_str(astr);3392 parse_asm_str(&astr);
3395 skip(')');3393 skip(')');
3396#ifdef ASM_DEBUG3394#ifdef ASM_DEBUG
3397 printf("asm_alias: \"%s\"\n", (char *)astr->data);3395 printf("asm_alias: \"%s\"\n", (char *)astr.data);
3398#endif3396#endif
3397 v = tok_alloc(astr.data, astr.size - 1)->tok;
3398 cstr_free(&astr);
3399 return v;
3399}3400}
3400 3401
3401static void post_type(CType *type, AttributeDef *ad)3402static void post_type(CType *type, AttributeDef *ad)
@@ -3801,7 +3802,7 @@ ST_FUNC void unary(void) · 399 unmodified lines
3801 mk_pointer(&type);3802 mk_pointer(&type);
3802 type.t |= VT_ARRAY;3803 type.t |= VT_ARRAY;
3803 memset(&ad, 0, sizeof(AttributeDef));3804 memset(&ad, 0, sizeof(AttributeDef));
3804 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);3805 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3805 break;3806 break;
3806 case '(':3807 case '(':
3807 next();3808 next();
@@ -3820,7 +3821,7 @@ ST_FUNC void unary(void) · 12 unmodified lines
3820 if (!(type.t & VT_ARRAY))3821 if (!(type.t & VT_ARRAY))
3821 r |= lvalue_type(type.t);3822 r |= lvalue_type(type.t);
3822 memset(&ad, 0, sizeof(AttributeDef));3823 memset(&ad, 0, sizeof(AttributeDef));
3823 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);3824 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3824 } else {3825 } else {
3825 if (sizeof_caller) {3826 if (sizeof_caller) {
3826 vpush(&type);3827 vpush(&type);
@@ -4199,7 +4200,7 @@ ST_FUNC void unary(void) · 372 unmodified lines
4199 break;4200 break;
4200 }4201 }
4201 if (!s)4202 if (!s)
4202 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));4203 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, &tokc));
4203 /* add field offset to pointer */4204 /* add field offset to pointer */
4204 vtop->type = char_pointer_type; /* change type to 'char *' */4205 vtop->type = char_pointer_type; /* change type to 'char *' */
4205 vpushi(s->c);4206 vpushi(s->c);
@@ -5742,14 +5743,12 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 1536 unmodified lines
5742/* parse an initializer for type 't' if 'has_init' is non zero, and5743/* parse an initializer for type 't' if 'has_init' is non zero, and
5743 allocate space in local or global data space ('r' is either5744 allocate space in local or global data space ('r' is either
5744 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated5745 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5745 variable 'v' with an associated name represented by 'asm_label' of5746 variable 'v' of scope 'scope' is declared before initializers
5746 scope 'scope' is declared before initializers are parsed. If 'v' is5747 are parsed. If 'v' is zero, then a reference to the new object
5747 zero, then a reference to the new object is put in the value stack.5748 is put in the value stack. If 'has_init' is 2, a special parsing
5748 If 'has_init' is 2, a special parsing is done to handle string5749 is done to handle string constants. */
5749 constants. */
5750static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, 5750static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5751 int has_init, int v, char *asm_label,5751 int has_init, int v, int scope)
5752 int scope)
5753{5752{
5754 int size, align, addr, data_offset;5753 int size, align, addr, data_offset;
5755 int level;5754 int level;
@@ -5931,7 +5930,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 175 unmodified lines
5931 if (v) {5930 if (v) {
5932 if (scope != VT_CONST || !sym) {5931 if (scope != VT_CONST || !sym) {
5933 sym = sym_push(v, type, r | VT_SYM, 0);5932 sym = sym_push(v, type, r | VT_SYM, 0);
5934 sym->asm_label = asm_label ? tcc_strdup(asm_label) : 0;5933 sym->asm_label = ad->asm_label;
5935 }5934 }
5936 /* update symbol definition */5935 /* update symbol definition */
5937 if (sec) {5936 if (sec) {
@@ -6162,7 +6161,6 @@ static int decl0(int l, int is_for_loop_init) · 224 unmodified lines
6162 CType type, btype;6161 CType type, btype;
6163 Sym *sym;6162 Sym *sym;
6164 AttributeDef ad;6163 AttributeDef ad;
6165 char *asm_label = 0; // associated asm label
6166 6164
6167 while (1) {6165 while (1) {
6168 if (!parse_btype(&btype, &ad)) {6166 if (!parse_btype(&btype, &ad)) {
@@ -6218,17 +6216,12 @@ static int decl0(int l, int is_for_loop_init) · 49 unmodified lines
6218 func_decl_list(sym);6216 func_decl_list(sym);
6219 }6217 }
6220 6218
6221 tcc_free(asm_label);
6222 asm_label = 0;
6223 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {6219 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6224 CString astr;6220 ad.asm_label = asm_label_instr();
6225
6226 asm_label_instr(&astr);
6227 asm_label = tcc_strdup(astr.data);
6228 cstr_free(&astr);
6229
6230 /* parse one last attribute list, after asm label */6221 /* parse one last attribute list, after asm label */
6231 parse_attribute(&ad);6222 parse_attribute(&ad);
6223 if (tok == '{')
6224 expect(";");
6232 }6225 }
6233 6226
6234 if (ad.a.weak)6227 if (ad.a.weak)
@@ -6371,7 +6364,8 @@ static int decl0(int l, int is_for_loop_init) · 136 unmodified lines
6371 /* NOTE: as GCC, uninitialized global static6364 /* NOTE: as GCC, uninitialized global static
6372 arrays of null size are considered as6365 arrays of null size are considered as
6373 extern */6366 extern */
6374 sym = external_sym(v, &type, r, asm_label);6367 sym = external_sym(v, &type, r);
6368 sym->asm_label = ad.asm_label;
6375 6369
6376 if (ad.alias_target) {6370 if (ad.alias_target) {
6377 Section tsec;6371 Section tsec;
@@ -6393,14 +6387,12 @@ static int decl0(int l, int is_for_loop_init) · 15 unmodified lines
6393 r |= l;6387 r |= l;
6394 if (has_init)6388 if (has_init)
6395 next();6389 next();
6396 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);6390 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
6397 }6391 }
6398 }6392 }
6399 if (tok != ',') {6393 if (tok != ',') {
6400 if (is_for_loop_init) {6394 if (is_for_loop_init)
6401 tcc_free(asm_label);
6402 return 1;6395 return 1;
6403 }
6404 skip(';');6396 skip(';');
6405 break;6397 break;
6406 }6398 }
@@ -6409,7 +6401,6 @@ static int decl0(int l, int is_for_loop_init) · 2 unmodified lines
6409 ad.a.aligned = 0;6401 ad.a.aligned = 0;
6410 }6402 }
6411 }6403 }
6412 tcc_free(asm_label);
6413 return 0;6404 return 0;
6414}6405}
6415 6406
tccpp.c+9−1
@@ -334,10 +334,18 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 333 unmodified lines
334 break;334 break;
335 335
336 case TOK_CFLOAT:336 case TOK_CFLOAT:
337 cstr_cat(&cstr_buf, "<float>");
338 break;
337 case TOK_CDOUBLE:339 case TOK_CDOUBLE:
340 cstr_cat(&cstr_buf, "<double>");
341 break;
338 case TOK_CLDOUBLE:342 case TOK_CLDOUBLE:
343 cstr_cat(&cstr_buf, "<long double>");
344 break;
339 case TOK_LINENUM:345 case TOK_LINENUM:
340 return NULL; /* should not happen */346 cstr_cat(&cstr_buf, "<linenumber>");
347 break;
348 //return NULL; /* should not happen */
341 349
342 /* above tokens have value, the ones below don't */350 /* above tokens have value, the ones below don't */
343 351

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