-
Notifications
You must be signed in to change notification settings - Fork 2
/
tccgen.c
8696 lines (8081 loc) · 252 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
594
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
*/
#define USING_GLOBALS
#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 *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 *sym_free_first;
static void **sym_pools;
static int nb_sym_pools;
static Sym *all_cleanups, *pending_gotos;
static int local_scope;
static int in_sizeof;
static int constant_p;
ST_DATA char debug_modes;
ST_DATA SValue *vtop;
static SValue _vstack[1 + VSTACK_SIZE];
#define vstack (_vstack + 1)
ST_DATA int nocode_wanted; /* no code generation wanted */
#define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
#define DATA_ONLY_WANTED 0x80000000 /* ON outside of functions and for static initializers */
/* no code output after unconditional jumps such as with if (0) ... */
#define CODE_OFF_BIT 0x20000000
#define CODE_OFF() if(!nocode_wanted)(nocode_wanted |= CODE_OFF_BIT)
#define CODE_ON() (nocode_wanted &= ~CODE_OFF_BIT)
/* no code output when parsing sizeof()/typeof() etc. (using nocode_wanted++/--) */
#define NOEVAL_MASK 0x0000FFFF
#define NOEVAL_WANTED (nocode_wanted & NOEVAL_MASK)
/* no code output when parsing constant expressions */
#define CONST_WANTED_BIT 0x00010000
#define CONST_WANTED_MASK 0x0FFF0000
#define CONST_WANTED (nocode_wanted & CONST_WANTED_MASK)
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 func_ind;
ST_DATA const char *funcname;
ST_DATA CType int_type, func_old_type, char_type, char_pointer_type;
static CString initstr;
#if PTR_SIZE == 4
#define VT_SIZE_T (VT_INT | VT_UNSIGNED)
#define VT_PTRDIFF_T VT_INT
#elif LONG_SIZE == 4
#define VT_SIZE_T (VT_LLONG | VT_UNSIGNED)
#define VT_PTRDIFF_T VT_LLONG
#else
#define VT_SIZE_T (VT_LONG | VT_LLONG | VT_UNSIGNED)
#define VT_PTRDIFF_T (VT_LONG | VT_LLONG)
#endif
static struct switch_t {
struct case_t {
int64_t v1, v2;
int sym;
} **p; int n; /* list of case ranges */
int def_sym; /* default symbol */
int nocode_wanted;
int *bsym;
struct scope *scope;
struct switch_t *prev;
SValue sv;
} *cur_switch; /* current switch */
#define MAX_TEMP_LOCAL_VARIABLE_NUMBER 8
/*list of temporary local variables on the stack in current function. */
static 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];
static int nb_temp_local_vars;
static struct scope {
struct scope *prev;
struct { int loc, locorig, num; } vla;
struct { Sym *s; int n; } cl;
int *bsym, *csym;
Sym *lstk, *llstk;
} *cur_scope, *loop_scope, *root_scope;
typedef struct {
Section *sec;
int local_offset;
Sym *flex_array_ref;
} init_params;
#if 1
#define precedence_parser
static void init_prec(void);
#endif
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, int ignore_label);
static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td);
static void parse_expr_type(CType *type);
static void init_putv(init_params *p, CType *type, unsigned long c);
static void decl_initializer(init_params *p, CType *type, unsigned long c, int flags);
static void block(int is_expr);
static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
static int decl(int l);
static void expr_eq(void);
static void vpush_type_size(CType *type, int *a);
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 free_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 cast_error(CType *st, CType *dt);
/* ------------------------------------------------------------------------- */
/* Automagical code suppression */
/* Clear 'nocode_wanted' at forward label if it was used */
ST_FUNC void gsym(int t)
{
if (t) {
gsym_addr(t, ind);
CODE_ON();
}
}
/* Clear 'nocode_wanted' if current pc is a label */
static int gind()
{
int t = ind;
CODE_ON();
if (debug_modes)
tcc_tcov_block_begin(tcc_state);
return t;
}
/* Set 'nocode_wanted' after unconditional (backwards) jump */
static void gjmp_addr_acs(int t)
{
gjmp_addr(t);
CODE_OFF();
}
/* Set 'nocode_wanted' after unconditional (forwards) jump */
static int gjmp_acs(int t)
{
t = gjmp(t);
CODE_OFF();
return t;
}
/* These are #undef'd at the end of this file */
#define gjmp_addr gjmp_addr_acs
#define gjmp gjmp_acs
/* ------------------------------------------------------------------------- */
ST_INLN int is_float(int t)
{
int bt = t & VT_BTYPE;
return bt == VT_LDOUBLE
|| bt == VT_DOUBLE
|| bt == VT_FLOAT
|| bt == VT_QFLOAT;
}
static inline int is_integer_btype(int bt)
{
return bt == VT_BYTE
|| bt == VT_BOOL
|| bt == VT_SHORT
|| bt == VT_INT
|| bt == VT_LLONG;
}
static int btype_size(int bt)
{
return bt == VT_BYTE || bt == VT_BOOL ? 1 :
bt == VT_SHORT ? 2 :
bt == VT_INT ? 4 :
bt == VT_LLONG ? 8 :
bt == VT_PTR ? PTR_SIZE : 0;
}
/* returns function return register from type */
static int R_RET(int t)
{
if (!is_float(t))
return REG_IRET;
#ifdef TCC_TARGET_X86_64
if ((t & VT_BTYPE) == VT_LDOUBLE)
return TREG_ST0;
#elif defined TCC_TARGET_RISCV64
if ((t & VT_BTYPE) == VT_LDOUBLE)
return REG_IRET;
#endif
return REG_FRET;
}
/* returns 2nd function return register, if any */
static int R2_RET(int t)
{
t &= VT_BTYPE;
#if PTR_SIZE == 4
if (t == VT_LLONG)
return REG_IRE2;
#elif defined TCC_TARGET_X86_64
if (t == VT_QLONG)
return REG_IRE2;
if (t == VT_QFLOAT)
return REG_FRE2;
#elif defined TCC_TARGET_RISCV64
if (t == VT_LDOUBLE)
return REG_IRE2;
#endif
return VT_CONST;
}
/* returns true for two-word types */
#define USING_TWO_WORDS(t) (R2_RET(t) != VT_CONST)
/* put function return registers to stack value */
static void PUT_R_RET(SValue *sv, int t)
{
sv->r = R_RET(t), sv->r2 = R2_RET(t);
}
/* returns function return register class for type t */
static int RC_RET(int t)
{
return reg_classes[R_RET(t)] & ~(RC_FLOAT | RC_INT);
}
/* returns generic register class for type t */
static int RC_TYPE(int t)
{
if (!is_float(t))
return RC_INT;
#ifdef TCC_TARGET_X86_64
if ((t & VT_BTYPE) == VT_LDOUBLE)
return RC_ST0;
if ((t & VT_BTYPE) == VT_QFLOAT)
return RC_FRET;
#elif defined TCC_TARGET_RISCV64
if ((t & VT_BTYPE) == VT_LDOUBLE)
return RC_INT;
#endif
return RC_FLOAT;
}
/* returns 2nd register class corresponding to t and rc */
static int RC2_TYPE(int t, int rc)
{
if (!USING_TWO_WORDS(t))
return 0;
#ifdef RC_IRE2
if (rc == RC_IRET)
return RC_IRE2;
#endif
#ifdef RC_FRE2
if (rc == RC_FRET)
return RC_FRE2;
#endif
if (rc & RC_FLOAT)
return RC_FLOAT;
return RC_INT;
}
/* 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 (vtop != vstack - 1)
tcc_error("internal compiler error: vstack leak (%d)",
(int)(vtop - vstack + 1));
}
/* 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:%04x r:%04x r2:%04x c.i:%d\n",
lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
}
}
#endif
/* ------------------------------------------------------------------------- */
/* initialize vstack and types. This must be done also for tcc -E */
ST_FUNC void tccgen_init(TCCState *s1)
{
vtop = vstack - 1;
memset(vtop, 0, sizeof *vtop);
/* define some often used types */
int_type.t = VT_INT;
char_type.t = VT_BYTE;
if (s1->char_is_unsigned)
char_type.t |= VT_UNSIGNED;
char_pointer_type = char_type;
mk_pointer(&char_pointer_type);
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;
#ifdef precedence_parser
init_prec();
#endif
cstr_new(&initstr);
}
ST_FUNC int tccgen_compile(TCCState *s1)
{
cur_text_section = NULL;
funcname = "";
func_ind = -1;
anon_sym = SYM_FIRST_ANOM;
nocode_wanted = DATA_ONLY_WANTED; /* no code outside of functions */
local_scope = 0;
debug_modes = (s1->do_debug ? 1 : 0) | s1->test_coverage << 1;
tcc_debug_start(s1);
tcc_tcov_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);
tcc_tcov_end(s1);
return 0;
}
ST_FUNC void tccgen_finish(TCCState *s1)
{
tcc_debug_end(s1); /* just in case of errors: free memory */
free_inline_functions(s1);
sym_pop(&global_stack, NULL, 0);
sym_pop(&local_stack, NULL, 0);
/* free preprocessor macros */
free_defines(NULL);
/* free sym_pools */
dynarray_reset(&sym_pools, &nb_sym_pools);
sym_free_first = NULL;
global_label_stack = local_label_stack = NULL;
cstr_free(&initstr);
dynarray_reset(&stk_data, &nb_stk_data);
}
/* ------------------------------------------------------------------------- */
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 | VT_INLINE))
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];
if (!sym->c) {
name = get_tok_str(sym->v, NULL);
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;
if ((t & (VT_BTYPE|VT_ASM_FUNC)) == VT_ASM_FUNC)
sym_type = STT_FUNC;
} else {
sym_type = STT_OBJECT;
}
if (t & (VT_STATIC | VT_INLINE))
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 (sym->asm_label) {
name = get_tok_str(sym->asm_label, NULL);
can_add_underscore = 0;
}
if (tcc_state->leading_underscore && can_add_underscore) {
buf1[0] = '_';
pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
name = buf1;
}
info = ELFW(ST_INFO)(sym_bind, sym_type);
sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);
if (debug_modes)
tcc_debug_extern_sym(tcc_state, sym, sh_num, sym_bind, sym_type);
} 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 *s, addr_t value, unsigned long size)
{
if (nocode_wanted && (NODATA_WANTED || (s && s == cur_text_section)))
return;
put_extern_sym2(sym, s ? s->sh_num : SHN_UNDEF, 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, VT_STATIC, 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_c(warn_all)("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 */
if (s->r != LABEL_GONE)
table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
if (!keep)
sym_free(s);
else
s->r = LABEL_GONE;
}
if (!keep)
*ptop = slast;
}
/* ------------------------------------------------------------------------- */
static void vcheck_cmp(void)
{
/* 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. */
/* However if it's just automatic suppression via CODE_OFF/ON()
then it seems that we better let things work undisturbed.
How can it work at all under nocode_wanted? Well, gv() will
actually clear it at the gsym() in load()/VT_JMP in the
generator backends */
if (vtop->r == VT_CMP && 0 == (nocode_wanted & ~CODE_OFF_BIT))
gv(RC_INT);
}
static void vsetc(CType *type, int r, CValue *vc)
{
if (vtop >= vstack + (VSTACK_SIZE - 1))
tcc_error("memory full (vstack)");
vcheck_cmp();
vtop++;
vtop->type = *type;
vtop->r = r;
vtop->r2 = VT_CONST;
vtop->c = *vc;
vtop->sym = NULL;
}
ST_FUNC void vswap(void)
{
SValue tmp;
vcheck_cmp();
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_CMP) {
/* need to put correct jump if && or || without test */
gsym(vtop->jtrue);
gsym(vtop->jfalse);
}
vtop--;
}
/* push constant of type "type" with useless value */
static void vpush(CType *type)
{
vset(type, VT_CONST, 0);
}
/* push arbitrary 64bit constant */
static 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 integer constant */
ST_FUNC void vpushi(int v)
{
vpush64(VT_INT, v);
}
/* push a pointer sized constant */
static void vpushs(addr_t v)
{
vpush64(VT_SIZE_T, v);
}
/* 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;
vcheck_cmp();
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;
vcheck_cmp();
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);
}
/* ------------------------------------------------------------------------- */
/* vtop->r = VT_CMP means CPU-flags have been set from comparison or test. */
/* called from generators to set the result from relational ops */
ST_FUNC void vset_VT_CMP(int op)
{
vtop->r = VT_CMP;
vtop->cmp_op = op;
vtop->jfalse = 0;
vtop->jtrue = 0;
}