-
Notifications
You must be signed in to change notification settings - Fork 2
/
tccgen.c
7873 lines (7427 loc) · 243 KB
/
tccgen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TCC - Tiny C Compiler
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tcc.h"
/********************************************************/
/* global variables */
/* loc : local variable index
ind : output code index
rsym: return symbol
anon_sym: anonymous symbol index
*/
ST_DATA int rsym, anon_sym, ind, loc;
ST_DATA Sym *sym_free_first;
ST_DATA void **sym_pools;
ST_DATA int nb_sym_pools;
ST_DATA Sym *global_stack;
ST_DATA Sym *local_stack;
ST_DATA Sym *define_stack;
ST_DATA Sym *global_label_stack;
ST_DATA Sym *local_label_stack;
static Sym *all_cleanups, *current_cleanups, *pending_gotos;
static int ncleanups;
static int local_scope;
static int in_sizeof;
static int section_sym;
ST_DATA int vlas_in_scope; /* number of VLAs that are currently in scope */
ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
ST_DATA int vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
ST_DATA SValue __vstack[1 VSTACK_SIZE], *vtop, *pvtop;
ST_DATA int const_wanted; /* true if constant wanted */
ST_DATA int nocode_wanted; /* no code generation wanted */
#define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
#define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
ST_DATA CType func_vt; /* current function return type (used by return instruction) */
ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
ST_DATA int func_vc;
ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
ST_DATA const char *funcname;
ST_DATA int g_debug;
ST_DATA CType char_pointer_type, func_old_type, int_type, size_type, ptrdiff_type;
ST_DATA struct switch_t {
struct case_t {
int64_t v1, v2;
int sym;
} **p; int n; /* list of case ranges */
int def_sym; /* default symbol */
} *cur_switch; /* current switch */
#define MAX_TEMP_LOCAL_VARIABLE_NUMBER 0x4
/*list of temporary local variables on the stack in current function. */
ST_DATA struct temp_local_variable {
int location; //offset on stack. Svalue.c.i
short size;
short align;
} arr_temp_local_vars[MAX_TEMP_LOCAL_VARIABLE_NUMBER];
short nb_temp_local_vars;
/* ------------------------------------------------------------------------- */
static void gen_cast(CType *type);
static void gen_cast_s(int t);
static inline CType *pointed_type(CType *type);
static int is_compatible_types(CType *type1, CType *type2);
static int parse_btype(CType *type, AttributeDef *ad);
static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td);
static void parse_expr_type(CType *type);
static void init_putv(CType *type, Section *sec, unsigned long c);
static void decl_initializer(CType *type, Section *sec, unsigned long c, int flags);
static void block(int *bsym, Sym *bcl, int *csym, Sym *ccl, int is_expr);
static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
static void decl(int l);
static int decl0(int l, int is_for_loop_init, Sym *);
static void expr_eq(void);
static void vla_runtime_type_size(CType *type, int *a);
static void vla_sp_restore(void);
static void vla_sp_restore_root(void);
static int is_compatible_unqualified_types(CType *type1, CType *type2);
static inline int64_t expr_const64(void);
static void vpush64(int ty, unsigned long long v);
static void vpush(CType *type);
static int gvtst(int inv, int t);
static void gen_inline_functions(TCCState *s);
static void skip_or_save_block(TokenString **str);
static void gv_dup(void);
static int get_temp_local_var(int size, int align);
static void clear_temp_local_var_list();
static void reset_local_scope(void)
{
if (current_cleanups)
tcc_error("ICE current_cleanups");
sym_pop(&all_cleanups, NULL, 0);
local_scope = 0;
}
ST_INLN int is_float(int t)
{
int bt;
bt = t & VT_BTYPE;
return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
}
/* we use our own 'finite' function to avoid potential problems with
non standard math libs */
/* XXX: endianness dependent */
ST_FUNC int ieee_finite(double d)
{
int p[4];
memcpy(p, &d, sizeof(double));
return ((unsigned)((p[1] | 0x800fffff) 1)) >> 31;
}
/* compiling intel long double natively */
#if (defined __i386__ || defined __x86_64__) \
&& (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
#define TCC_IS_NATIVE_387
#endif
ST_FUNC void test_lvalue(void)
{
if (!(vtop->r & VT_LVAL))
expect("lvalue");
}
ST_FUNC void check_vstack(void)
{
if (pvtop != vtop)
tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
}
/* ------------------------------------------------------------------------- */
/* vstack debugging aid */
#if 0
void pv (const char *lbl, int a, int b)
{
int i;
for (i = a; i < a b; i) {
SValue *p = &vtop[-i];
printf("%s vtop[-%d] : type.t:x r:x r2:x c.i:%d\n",
lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
}
}
#endif
/* ------------------------------------------------------------------------- */
/* start of translation unit info */
ST_FUNC void tcc_debug_start(TCCState *s1)
{
if (s1->do_debug) {
char buf[512];
/* file info: full path filename */
section_sym = put_elf_sym(symtab_section, 0, 0,
ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
text_section->sh_num, NULL);
getcwd(buf, sizeof(buf));
#ifdef _WIN32
normalize_slashes(buf);
#endif
pstrcat(buf, sizeof(buf), "/");
put_stabs_r(buf, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym);
put_stabs_r(file->filename, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym);
last_ind = 0;
last_line_num = 0;
}
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
symbols can be safely used */
put_elf_sym(symtab_section, 0, 0,
ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
SHN_ABS, file->filename);
}
/* put end of translation unit info */
ST_FUNC void tcc_debug_end(TCCState *s1)
{
if (!s1->do_debug)
return;
put_stabs_r(NULL, N_SO, 0, 0,
text_section->data_offset, text_section, section_sym);
}
/* generate line number info */
ST_FUNC void tcc_debug_line(TCCState *s1)
{
if (!s1->do_debug)
return;
if ((last_line_num != file->line_num || last_ind != ind)) {
put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
last_ind = ind;
last_line_num = file->line_num;
}
}
/* put function symbol */
ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
{
char buf[512];
if (!s1->do_debug)
return;
/* stabs info */
/* XXX: we put here a dummy type */
snprintf(buf, sizeof(buf), "%s:�",
funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
cur_text_section, sym->c);
/* //gr gdb wants a line at the function */
put_stabn(N_SLINE, 0, file->line_num, 0);
last_ind = 0;
last_line_num = 0;
}
/* put function size */
ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
{
if (!s1->do_debug)
return;
put_stabn(N_FUN, 0, 0, size);
}
/* ------------------------------------------------------------------------- */
ST_FUNC int tccgen_compile(TCCState *s1)
{
cur_text_section = NULL;
funcname = "";
anon_sym = SYM_FIRST_ANOM;
section_sym = 0;
const_wanted = 0;
nocode_wanted = 0x80000000;
/* define some often used types */
int_type.t = VT_INT;
char_pointer_type.t = VT_BYTE;
mk_pointer(&char_pointer_type);
#if PTR_SIZE == 4
size_type.t = VT_INT | VT_UNSIGNED;
ptrdiff_type.t = VT_INT;
#elif LONG_SIZE == 4
size_type.t = VT_LLONG | VT_UNSIGNED;
ptrdiff_type.t = VT_LLONG;
#else
size_type.t = VT_LONG | VT_LLONG | VT_UNSIGNED;
ptrdiff_type.t = VT_LONG | VT_LLONG;
#endif
func_old_type.t = VT_FUNC;
func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);
func_old_type.ref->f.func_call = FUNC_CDECL;
func_old_type.ref->f.func_type = FUNC_OLD;
tcc_debug_start(s1);
#ifdef TCC_TARGET_ARM
arm_init(s1);
#endif
#ifdef INC_DEBUG
printf("%s: **** new file\n", file->filename);
#endif
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
next();
decl(VT_CONST);
gen_inline_functions(s1);
check_vstack();
/* end of translation unit info */
tcc_debug_end(s1);
return 0;
}
/* ------------------------------------------------------------------------- */
ST_FUNC ElfSym *elfsym(Sym *s)
{
if (!s || !s->c)
return NULL;
return &((ElfSym *)symtab_section->data)[s->c];
}
/* apply storage attributes to Elf symbol */
ST_FUNC void update_storage(Sym *sym)
{
ElfSym *esym;
int sym_bind, old_sym_bind;
esym = elfsym(sym);
if (!esym)
return;
if (sym->a.visibility)
esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
| sym->a.visibility;
if (sym->type.t & VT_STATIC)
sym_bind = STB_LOCAL;
else if (sym->a.weak)
sym_bind = STB_WEAK;
else
sym_bind = STB_GLOBAL;
old_sym_bind = ELFW(ST_BIND)(esym->st_info);
if (sym_bind != old_sym_bind)
esym->st_info = ELFW(ST_INFO)(sym_bind, ELFW(ST_TYPE)(esym->st_info));
#ifdef TCC_TARGET_PE
if (sym->a.dllimport)
esym->st_other |= ST_PE_IMPORT;
if (sym->a.dllexport)
esym->st_other |= ST_PE_EXPORT;
#endif
#if 0
printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
get_tok_str(sym->v, NULL),
sym_bind == STB_WEAK ? 'w' : sym_bind == STB_LOCAL ? 'l' : 'g',
sym->a.visibility,
sym->a.dllexport,
sym->a.dllimport
);
#endif
}
/* ------------------------------------------------------------------------- */
/* update sym->c so that it points to an external symbol in section
'section' with value 'value' */
ST_FUNC void put_extern_sym2(Sym *sym, int sh_num,
addr_t value, unsigned long size,
int can_add_underscore)
{
int sym_type, sym_bind, info, other, t;
ElfSym *esym;
const char *name;
char buf1[256];
#ifdef CONFIG_TCC_BCHECK
char buf[32];
#endif
if (!sym->c) {
name = get_tok_str(sym->v, NULL);
#ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check) {
/* XXX: avoid doing that for statics ? */
/* if bound checking is activated, we change some function
names by adding the "__bound" prefix */
switch (sym->v) {
#ifdef TCC_TARGET_PE
/* XXX: we rely only on malloc hooks */
case TOK_malloc:
case TOK_free:
case TOK_realloc:
case TOK_memalign:
case TOK_calloc:
#endif
case TOK_memcpy:
case TOK_memmove:
case TOK_memset:
case TOK_strlen:
case TOK_strcpy:
case TOK_alloca:
strcpy(buf, "__bound_");
strcat(buf, name);
name = buf;
break;
}
}
#endif
t = sym->type.t;
if ((t & VT_BTYPE) == VT_FUNC)
sym_type = STT_FUNC;
else if ((t & VT_BTYPE) == VT_VOID)
sym_type = STT_NOTYPE;
else
sym_type = STT_OBJECT;
if (t & VT_STATIC)
sym_bind = STB_LOCAL;
else
sym_bind = STB_GLOBAL;
other = 0;
#ifdef TCC_TARGET_PE
if (sym_type == STT_FUNC && sym->type.ref) {
Sym *ref = sym->type.ref;
if (ref->a.nodecorate)
can_add_underscore = 0;
if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) {
sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE);
name = buf1;
other |= ST_PE_STDCALL;
can_add_underscore = 0;
}
}
#endif
if (tcc_state->leading_underscore && can_add_underscore) {
buf1[0] = '_';
pstrcpy(buf1 1, sizeof(buf1) - 1, name);
name = buf1;
}
if (sym->asm_label)
name = get_tok_str(sym->asm_label, NULL);
info = ELFW(ST_INFO)(sym_bind, sym_type);
sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);
}
else {
esym = elfsym(sym);
esym->st_value = value;
esym->st_size = size;
esym->st_shndx = sh_num;
}
update_storage(sym);
}
ST_FUNC void put_extern_sym(Sym *sym, Section *section,
addr_t value, unsigned long size)
{
int sh_num = section ? section->sh_num : SHN_UNDEF;
put_extern_sym2(sym, sh_num, value, size, 1);
}
/* add a new relocation entry to symbol 'sym' in section 's' */
ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
addr_t addend)
{
int c = 0;
if (nocode_wanted && s == cur_text_section)
return;
if (sym) {
if (0 == sym->c)
put_extern_sym(sym, NULL, 0, 0);
c = sym->c;
}
/* now we can add ELF relocation info */
put_elf_reloca(symtab_section, s, offset, type, c, addend);
}
#if PTR_SIZE == 4
ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
{
greloca(s, sym, offset, type, 0);
}
#endif
/* ------------------------------------------------------------------------- */
/* symbol allocator */
static Sym *__sym_malloc(void)
{
Sym *sym_pool, *sym, *last_sym;
int i;
sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
last_sym = sym_free_first;
sym = sym_pool;
for (i = 0; i < SYM_POOL_NB; i ) {
sym->next = last_sym;
last_sym = sym;
sym ;
}
sym_free_first = last_sym;
return last_sym;
}
static inline Sym *sym_malloc(void)
{
Sym *sym;
#ifndef SYM_DEBUG
sym = sym_free_first;
if (!sym)
sym = __sym_malloc();
sym_free_first = sym->next;
return sym;
#else
sym = tcc_malloc(sizeof(Sym));
return sym;
#endif
}
ST_INLN void sym_free(Sym *sym)
{
#ifndef SYM_DEBUG
sym->next = sym_free_first;
sym_free_first = sym;
#else
tcc_free(sym);
#endif
}
/* push, without hashing */
ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c)
{
Sym *s;
s = sym_malloc();
memset(s, 0, sizeof * s);
s->v = v;
s->type.t = t;
s->c = c;
/* add in stack */
s->prev = *ps;
*ps = s;
return s;
}
/* find a symbol and return its associated structure. 's' is the top
of the symbol stack */
ST_FUNC Sym *sym_find2(Sym *s, int v)
{
while (s) {
if (s->v == v)
return s;
else if (s->v == -1)
return NULL;
s = s->prev;
}
return NULL;
}
/* structure lookup */
ST_INLN Sym *struct_find(int v)
{
v -= TOK_IDENT;
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
return NULL;
return table_ident[v]->sym_struct;
}
/* find an identifier */
ST_INLN Sym *sym_find(int v)
{
v -= TOK_IDENT;
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
return NULL;
return table_ident[v]->sym_identifier;
}
static int sym_scope(Sym *s)
{
if (IS_ENUM_VAL (s->type.t))
return s->type.ref->sym_scope;
else
return s->sym_scope;
}
/* push a given symbol on the symbol stack */
ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
{
Sym *s, **ps;
TokenSym *ts;
if (local_stack)
ps = &local_stack;
else
ps = &global_stack;
s = sym_push2(ps, v, type->t, c);
s->type.ref = type->ref;
s->r = r;
/* don't record fields or anonymous symbols */
/* XXX: simplify */
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
/* record symbol in token array */
ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
if (v & SYM_STRUCT)
ps = &ts->sym_struct;
else
ps = &ts->sym_identifier;
s->prev_tok = *ps;
*ps = s;
s->sym_scope = local_scope;
if (s->prev_tok && sym_scope(s->prev_tok) == s->sym_scope)
tcc_error("redeclaration of '%s'",
get_tok_str(v & ~SYM_STRUCT, NULL));
}
return s;
}
/* push a global identifier */
ST_FUNC Sym *global_identifier_push(int v, int t, int c)
{
Sym *s, **ps;
s = sym_push2(&global_stack, v, t, c);
s->r = VT_CONST | VT_SYM;
/* don't record anonymous symbol */
if (v < SYM_FIRST_ANOM) {
ps = &table_ident[v - TOK_IDENT]->sym_identifier;
/* modify the top most local identifier, so that sym_identifier will
point to 's' when popped; happens when called from inline asm */
while (*ps != NULL && (*ps)->sym_scope)
ps = &(*ps)->prev_tok;
s->prev_tok = *ps;
*ps = s;
}
return s;
}
/* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
pop them yet from the list, but do remove them from the token array. */
ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep)
{
Sym *s, *ss, **ps;
TokenSym *ts;
int v;
s = *ptop;
while (s != b) {
ss = s->prev;
v = s->v;
/* remove symbol in token array */
/* XXX: simplify */
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
if (v & SYM_STRUCT)
ps = &ts->sym_struct;
else
ps = &ts->sym_identifier;
*ps = s->prev_tok;
}
if (!keep)
sym_free(s);
s = ss;
}
if (!keep)
*ptop = b;
}
/* label lookup */
ST_FUNC Sym *label_find(int v)
{
v -= TOK_IDENT;
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT)) return NULL;
return table_ident[v]->sym_label;
}
ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
{
Sym *s, **ps;
s = sym_push2(ptop, v, 0, 0);
s->r = flags;
ps = &table_ident[v - TOK_IDENT]->sym_label;
if (ptop == &global_label_stack) {
/* modify the top most local identifier, so that
sym_identifier will point to 's' when popped */
while (*ps != NULL) ps = &(*ps)->prev_tok;
}
s->prev_tok = *ps;
*ps = s;
return s;
}
/* pop labels until element last is reached. Look if any labels are
undefined. Define symbols if '&&label' was used. */
ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep)
{
Sym *s, *s1;
for (s = *ptop; s != slast; s = s1) {
s1 = s->prev;
if (s->r == LABEL_DECLARED)
tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
else if (s->r == LABEL_FORWARD)
tcc_error("label '%s' used but not defined", get_tok_str(s->v, NULL));
else {
if (s->c) {
/* define corresponding symbol. A size of
1 is put. */
put_extern_sym(s, cur_text_section, s->jnext, 1);
}
}
/* remove label */
table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
if (!keep) sym_free(s);
}
if (!keep) *ptop = slast;
}
/* ------------------------------------------------------------------------- */
static void vsetc(CType *type, int r, CValue *vc)
{
int v;
if (vtop >= vstack (VSTACK_SIZE - 1))
tcc_error("memory full (vstack)");
/* cannot let cpu flags if other instruction are generated. Also
avoid leaving VT_JMP anywhere except on the top of the stack
because it would complicate the code generator.
Don't do this when nocode_wanted. vtop might come from
!nocode_wanted regions (see 88_codeopt.c) and transforming
it to a register without actually generating code is wrong
as their value might still be used for real. All values
we push under nocode_wanted will eventually be popped
again, so that the VT_CMP/VT_JMP value will be in vtop
when code is unsuppressed again.
Same logic below in vswap(); */
if (vtop >= vstack && !nocode_wanted) {
v = vtop->r & VT_VALMASK;
if (v == VT_CMP || (v & ~1) == VT_JMP)
gv(RC_INT);
}
vtop ;
vtop->type = *type;
vtop->r = r;
vtop->r2 = VT_CONST;
vtop->c = *vc;
vtop->sym = NULL;
}
ST_FUNC void vswap(void)
{
SValue tmp;
/* cannot vswap cpu flags. See comment at vsetc() above */
if (vtop >= vstack && !nocode_wanted) {
int v = vtop->r & VT_VALMASK;
if (v == VT_CMP || (v & ~1) == VT_JMP)
gv(RC_INT);
}
tmp = vtop[0];
vtop[0] = vtop[-1];
vtop[-1] = tmp;
}
/* pop stack value */
ST_FUNC void vpop(void)
{
int v;
v = vtop->r & VT_VALMASK;
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
/* for x86, we need to pop the FP stack */
if (v == TREG_ST0) {
o(0xd8dd); /* fstp %st(0) */
}
else
#endif
if (v == VT_JMP || v == VT_JMPI) {
/* need to put correct jump if && or || without test */
gsym(vtop->c.i);
}
vtop--;
}
/* push constant of type "type" with useless value */
ST_FUNC void vpush(CType *type)
{
vset(type, VT_CONST, 0);
}
/* push integer constant */
ST_FUNC void vpushi(int v)
{
CValue cval;
cval.i = v;
vsetc(&int_type, VT_CONST, &cval);
}
/* push a pointer sized constant */
static void vpushs(addr_t v)
{
CValue cval;
cval.i = v;
vsetc(&size_type, VT_CONST, &cval);
}
/* push arbitrary 64bit constant */
ST_FUNC void vpush64(int ty, unsigned long long v)
{
CValue cval;
CType ctype;
ctype.t = ty;
ctype.ref = NULL;
cval.i = v;
vsetc(&ctype, VT_CONST, &cval);
}
/* push long long constant */
static inline void vpushll(long long v)
{
vpush64(VT_LLONG, v);
}
ST_FUNC void vset(CType *type, int r, int v)
{
CValue cval;
cval.i = v;
vsetc(type, r, &cval);
}
static void vseti(int r, int v)
{
CType type;
type.t = VT_INT;
type.ref = NULL;
vset(&type, r, v);
}
ST_FUNC void vpushv(SValue *v)
{
if (vtop >= vstack (VSTACK_SIZE - 1))
tcc_error("memory full (vstack)");
vtop ;
*vtop = *v;
}
static void vdup(void)
{
vpushv(vtop);
}
/* rotate n first stack elements to the bottom
I1 ... In -> I2 ... In I1 [top is right]
*/
ST_FUNC void vrotb(int n)
{
int i;
SValue tmp;
tmp = vtop[-n 1];
for (i = -n 1; i != 0; i )
vtop[i] = vtop[i 1];
vtop[0] = tmp;
}
/* rotate the n elements before entry e towards the top
I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
*/
ST_FUNC void vrote(SValue *e, int n)
{
int i;
SValue tmp;
tmp = *e;
for (i = 0; i < n - 1; i )
e[-i] = e[-i - 1];
e[-n 1] = tmp;
}
/* rotate n first stack elements to the top
I1 ... In -> In I1 ... I(n-1) [top is right]
*/
ST_FUNC void vrott(int n)
{
vrote(vtop, n);
}
/* push a symbol value of TYPE */
static inline void vpushsym(CType *type, Sym *sym)
{
CValue cval;
cval.i = 0;
vsetc(type, VT_CONST | VT_SYM, &cval);
vtop->sym = sym;
}
/* Return a static symbol pointing to a section */
ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
{
int v;
Sym *sym;
v = anon_sym ;
sym = sym_push(v, type, VT_CONST | VT_SYM, 0);
sym->type.t |= VT_STATIC;
put_extern_sym(sym, sec, offset, size);
return sym;
}
/* push a reference to a section offset by adding a dummy symbol */
static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
{
vpushsym(type, get_sym_ref(type, sec, offset, size));
}
/* define a new external reference to a symbol 'v' of type 'u' */
ST_FUNC Sym *external_global_sym(int v, CType *type)
{
Sym *s;
s = sym_find(v);
if (!s) {
/* push forward reference */
s = global_identifier_push(v, type->t | VT_EXTERN, 0);
s->type.ref = type->ref;
}
else if (IS_ASM_SYM(s)) {
s->type.t = type->t | (s->type.t & VT_EXTERN);
s->type.ref = type->ref;
update_storage(s);
}
return s;
}
/* Merge symbol attributes. */
static void merge_symattr(struct SymAttr *sa, struct SymAttr *sa1)
{
if (sa1->aligned && !sa->aligned)
sa->aligned = sa1->aligned;
sa->packed |= sa1->packed;
sa->weak |= sa1->weak;
if (sa1->visibility != STV_DEFAULT) {
int vis = sa->visibility;
if (vis == STV_DEFAULT
|| vis > sa1->visibility)
vis = sa1->visibility;
sa->visibility = vis;
}
sa->dllexport |= sa1->dllexport;
sa->nodecorate |= sa1->nodecorate;
sa->dllimport |= sa1->dllimport;
}
/* Merge function attributes. */
static void merge_funcattr(struct FuncAttr *fa, struct FuncAttr *fa1)
{
if (fa1->func_call && !fa->func_call)
fa->func_call = fa1->func_call;
if (fa1->func_type && !fa->func_type)
fa->func_type = fa1->func_type;
if (fa1->func_args && !fa->func_args)
fa->func_args = fa1->func_args;
}
/* Merge attributes. */
static void merge_attr(AttributeDef *ad, AttributeDef *ad1)
{
merge_symattr(&ad->a, &ad1->a);
merge_funcattr(&ad->f, &ad1->f);
if (ad1->section)
ad->section = ad1->section;
if (ad1->alias_target)
ad->alias_target = ad1->alias_target;
if (ad1->asm_label)
ad->asm_label = ad1->asm_label;
if (ad1->attr_mode)
ad->attr_mode = ad1->attr_mode;
}
/* Merge some type attributes. */
static void patch_type(Sym *sym, CType *type)
{
if (!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) {
if (!(sym->type.t & VT_EXTERN))
tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
sym->type.t &= ~VT_EXTERN;
}
if (IS_ASM_SYM(sym)) {
/* stay static if both are static */
sym->type.t = type->t & (sym->type.t | ~VT_STATIC);
sym->type.ref = type->ref;
}
if (!is_compatible_types(&sym->type, type)) {
tcc_error("incompatible types for redefinition of '%s'",
get_tok_str(sym->v, NULL));
}
else if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
int static_proto = sym->type.t & VT_STATIC;
/* warn if static follows non-static function declaration */
if ((type->t & VT_STATIC) && !static_proto && !(type->t & VT_INLINE))
tcc_warning("static storage ignored for redefinition of '%s'",
get_tok_str(sym->v, NULL));