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
680680 name = buf1;
681681 }
682682 if (sym->asm_label) {
683− name = sym->asm_label;
683+ name = get_tok_str(sym->asm_label, NULL);
684684 }
685685 info = ELFW(ST_INFO)(sym_bind, sym_type);
686686 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
434434 struct Attribute a;
435435 struct Section *section;
436436 int alias_target; /* token */
437+ int asm_label; /* associated asm label */
437438 } AttributeDef;
438439
439440 /* symbol management */
440441 typedef struct Sym {
441442 int v; /* symbol token */
442− char *asm_label; /* associated asm label */
443+ int asm_label; /* associated asm label */
443444 union {
444445 long r; /* associated register */
445446 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
8181 static void parse_expr_type(CType *type);
8282 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
8383 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
84−static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
84+static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
8585 static int decl0(int l, int is_for_loop_init);
8686 static void expr_eq(void);
8787 static void unary_type(CType *type);
@@ -158,7 +158,6 @@ static inline Sym *sym_malloc(void) · 70 unmodified lines
158158 ST_INLN void sym_free(Sym *sym)
159159 {
160160 sym->next = sym_free_first;
161− tcc_free(sym->asm_label);
162161 sym_free_first = sym;
163162 }
164163
@@ -173,7 +172,7 @@ ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c) · 8 unmodified lines
173172 get_tok_str(v, NULL));
174173 }
175174 s = sym_malloc();
176− s->asm_label = NULL;
175+ s->asm_label = 0;
177176 s->v = v;
178177 s->type.t = t;
179178 s->type.ref = NULL;
@@ -443,10 +442,8 @@ ST_FUNC Sym *external_global_sym(int v, CType *type, int r) · 263 unmodified lines
443442 return s;
444443 }
445444
446−/* define a new external reference to a symbol 'v' with alternate asm
447− name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
448− is no alternate name (most cases) */
449−static Sym *external_sym(int v, CType *type, int r, char *asm_label)
445+/* define a new external reference to a symbol 'v' */
446+static Sym *external_sym(int v, CType *type, int r)
450447 {
451448 Sym *s;
452449
@@ -454,7 +451,6 @@ static Sym *external_sym(int v, CType *type, int r, char *asm_label) · 1 unmodified line
454451 if (!s) {
455452 /* push forward reference */
456453 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
457− s->asm_label = asm_label ? tcc_strdup(asm_label) : 0;
458454 s->type.t |= VT_EXTERN;
459455 } else if (s->type.ref == func_old_type.ref) {
460456 s->type.ref = type->ref;
@@ -3386,16 +3382,21 @@ ST_FUNC void parse_asm_str(CString *astr) · 2925 unmodified lines
33863382 cstr_ccat(astr, '\0');
33873383 }
33883384
3389−/* Parse an asm label and return the label
3390− * Don't forget to free the CString in the caller! */
3391−static void asm_label_instr(CString *astr)
3385+/* Parse an asm label and return the token */
3386+static int asm_label_instr(void)
33923387 {
3388+ int v;
3389+ CString astr;
3390+
33933391 next();
3394− parse_asm_str(astr);
3392+ parse_asm_str(&astr);
33953393 skip(')');
33963394 #ifdef ASM_DEBUG
3397− printf("asm_alias: \"%s\"\n", (char *)astr->data);
3395+ printf("asm_alias: \"%s\"\n", (char *)astr.data);
33983396 #endif
3397+ v = tok_alloc(astr.data, astr.size - 1)->tok;
3398+ cstr_free(&astr);
3399+ return v;
33993400 }
34003401
34013402 static void post_type(CType *type, AttributeDef *ad)
@@ -3801,7 +3802,7 @@ ST_FUNC void unary(void) · 399 unmodified lines
38013802 mk_pointer(&type);
38023803 type.t |= VT_ARRAY;
38033804 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);
38053806 break;
38063807 case '(':
38073808 next();
@@ -3820,7 +3821,7 @@ ST_FUNC void unary(void) · 12 unmodified lines
38203821 if (!(type.t & VT_ARRAY))
38213822 r |= lvalue_type(type.t);
38223823 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);
38243825 } else {
38253826 if (sizeof_caller) {
38263827 vpush(&type);
@@ -4199,7 +4200,7 @@ ST_FUNC void unary(void) · 372 unmodified lines
41994200 break;
42004201 }
42014202 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));
42034204 /* add field offset to pointer */
42044205 vtop->type = char_pointer_type; /* change type to 'char *' */
42054206 vpushi(s->c);
@@ -5742,14 +5743,12 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 1536 unmodified lines
57425743 /* parse an initializer for type 't' if 'has_init' is non zero, and
57435744 allocate space in local or global data space ('r' is either
57445745 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5745− variable 'v' with an associated name represented by 'asm_label' of
5746− scope 'scope' is declared before initializers are parsed. If 'v' is
5747− zero, then a reference to the new object is put in the value stack.
5748− If 'has_init' is 2, a special parsing is done to handle string
5749− constants. */
5746+ variable 'v' of scope 'scope' is declared before initializers
5747+ are parsed. If 'v' is zero, then a reference to the new object
5748+ is put in the value stack. If 'has_init' is 2, a special parsing
5749+ is done to handle string constants. */
57505750 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5751− int has_init, int v, char *asm_label,
5752− int scope)
5751+ int has_init, int v, int scope)
57535752 {
57545753 int size, align, addr, data_offset;
57555754 int level;
@@ -5931,7 +5930,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 175 unmodified lines
59315930 if (v) {
59325931 if (scope != VT_CONST || !sym) {
59335932 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;
59355934 }
59365935 /* update symbol definition */
59375936 if (sec) {
@@ -6162,7 +6161,6 @@ static int decl0(int l, int is_for_loop_init) · 224 unmodified lines
61626161 CType type, btype;
61636162 Sym *sym;
61646163 AttributeDef ad;
6165− char *asm_label = 0; // associated asm label
61666164
61676165 while (1) {
61686166 if (!parse_btype(&btype, &ad)) {
@@ -6218,17 +6216,12 @@ static int decl0(int l, int is_for_loop_init) · 49 unmodified lines
62186216 func_decl_list(sym);
62196217 }
62206218
6221− tcc_free(asm_label);
6222− asm_label = 0;
62236219 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6224− CString astr;
6225−
6226− asm_label_instr(&astr);
6227− asm_label = tcc_strdup(astr.data);
6228− cstr_free(&astr);
6229−
6220+ ad.asm_label = asm_label_instr();
62306221 /* parse one last attribute list, after asm label */
62316222 parse_attribute(&ad);
6223+ if (tok == '{')
6224+ expect(";");
62326225 }
62336226
62346227 if (ad.a.weak)
@@ -6371,7 +6364,8 @@ static int decl0(int l, int is_for_loop_init) · 136 unmodified lines
63716364 /* NOTE: as GCC, uninitialized global static
63726365 arrays of null size are considered as
63736366 extern */
6374− sym = external_sym(v, &type, r, asm_label);
6367+ sym = external_sym(v, &type, r);
6368+ sym->asm_label = ad.asm_label;
63756369
63766370 if (ad.alias_target) {
63776371 Section tsec;
@@ -6393,14 +6387,12 @@ static int decl0(int l, int is_for_loop_init) · 15 unmodified lines
63936387 r |= l;
63946388 if (has_init)
63956389 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);
63976391 }
63986392 }
63996393 if (tok != ',') {
6400− if (is_for_loop_init) {
6401− tcc_free(asm_label);
6394+ if (is_for_loop_init)
64026395 return 1;
6403− }
64046396 skip(';');
64056397 break;
64066398 }
@@ -6409,7 +6401,6 @@ static int decl0(int l, int is_for_loop_init) · 2 unmodified lines
64096401 ad.a.aligned = 0;
64106402 }
64116403 }
6412− tcc_free(asm_label);
64136404 return 0;
64146405 }
64156406
tccpp.c+9−1
@@ -334,10 +334,18 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 333 unmodified lines
334334 break;
335335
336336 case TOK_CFLOAT:
337+ cstr_cat(&cstr_buf, "<float>");
338+ break;
337339 case TOK_CDOUBLE:
340+ cstr_cat(&cstr_buf, "<double>");
341+ break;
338342 case TOK_CLDOUBLE:
343+ cstr_cat(&cstr_buf, "<long double>");
344+ break;
339345 case TOK_LINENUM:
340− return NULL; /* should not happen */
346+ cstr_cat(&cstr_buf, "<linenumber>");
347+ break;
348+ //return NULL; /* should not happen */
341349
342350 /* above tokens have value, the ones below don't */
343351

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