-
Notifications
You must be signed in to change notification settings - Fork 0
/
tccgen.c
8887 lines (8264 loc) · 269 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
*/
//static SValue _vstack[1 + VSTACK_SIZE];
#define vstack (S->tccgen__vstack + 1)
#define unevalmask 0xffff /* unevaluated subexpression */
#define NODATA_WANTED (S->nocode_wanted > 0) /* no static data output wanted either */
#define STATIC_DATA_WANTED (S->nocode_wanted & 0xC0000000) /* only static data output */
/* Automagical code suppression ----> */
#define CODE_OFF() (S->nocode_wanted |= 0x20000000)
#define CODE_ON() (S->nocode_wanted &= ~0x20000000)
static void tcc_tcov_block_begin(TCCState *S);
/* Clear 'nocode_wanted' at label if it was used */
ST_FUNC void gsym(TCCState *S, int t) { if (t) { gsym_addr(S, t, S->ind); CODE_ON(); }}
static int gind(TCCState *S) { int t = S->ind; CODE_ON(); if (S->tccgen_debug_modes) tcc_tcov_block_begin(S); return t; }
/* Set 'nocode_wanted' after unconditional jumps */
static void gjmp_addr_acs(TCCState *S, int t) { gjmp_addr(S, t); CODE_OFF(); }
static int gjmp_acs(TCCState *S, int t) { t = gjmp(S, 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
/* <---- */
#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
typedef struct {
Section *sec;
int local_offset;
Sym *flex_array_ref;
} init_params;
#if 1
#define precedence_parser
static void init_prec(TCCState *S);
#endif
/********************************************************/
/* stab debug support */
static const struct {
int type;
const char *name;
} default_debug[] = {
{ VT_INT, "int:t1=r1;-2147483648;2147483647;" },
{ VT_BYTE, "char:t2=r2;0;127;" },
#if LONG_SIZE == 4
{ VT_LONG | VT_INT, "long int:t3=r3;-2147483648;2147483647;" },
#else
{ VT_LLONG | VT_LONG, "long int:t3=r3;-9223372036854775808;9223372036854775807;" },
#endif
{ VT_INT | VT_UNSIGNED, "unsigned int:t4=r4;0;037777777777;" },
#if LONG_SIZE == 4
{ VT_LONG | VT_INT | VT_UNSIGNED, "long unsigned int:t5=r5;0;037777777777;" },
#else
/* use octal instead of -1 so size_t works (-gstabs+ in gcc) */
{ VT_LLONG | VT_LONG | VT_UNSIGNED, "long unsigned int:t5=r5;0;01777777777777777777777;" },
#endif
{ VT_QLONG, "__int128:t6=r6;0;-1;" },
{ VT_QLONG | VT_UNSIGNED, "__int128 unsigned:t7=r7;0;-1;" },
{ VT_LLONG, "long long int:t8=r8;-9223372036854775808;9223372036854775807;" },
{ VT_LLONG | VT_UNSIGNED, "long long unsigned int:t9=r9;0;01777777777777777777777;" },
{ VT_SHORT, "short int:t10=r10;-32768;32767;" },
{ VT_SHORT | VT_UNSIGNED, "short unsigned int:t11=r11;0;65535;" },
{ VT_BYTE | VT_DEFSIGN, "signed char:t12=r12;-128;127;" },
{ VT_BYTE | VT_DEFSIGN | VT_UNSIGNED, "unsigned char:t13=r13;0;255;" },
{ VT_FLOAT, "float:t14=r1;4;0;" },
{ VT_DOUBLE, "double:t15=r1;8;0;" },
#ifdef TCC_USING_DOUBLE_FOR_LDOUBLE
{ VT_DOUBLE | VT_LONG, "long double:t16=r1;8;0;" },
#else
{ VT_LDOUBLE, "long double:t16=r1;16;0;" },
#endif
{ -1, "_Float32:t17=r1;4;0;" },
{ -1, "_Float64:t18=r1;8;0;" },
{ -1, "_Float128:t19=r1;16;0;" },
{ -1, "_Float32x:t20=r1;8;0;" },
{ -1, "_Float64x:t21=r1;16;0;" },
{ -1, "_Decimal32:t22=r1;4;0;" },
{ -1, "_Decimal64:t23=r1;8;0;" },
{ -1, "_Decimal128:t24=r1;16;0;" },
/* if default char is unsigned */
{ VT_BYTE | VT_UNSIGNED, "unsigned char:t25=r25;0;255;" },
/* boolean type */
{ VT_BOOL, "bool:t26=r26;0;255;" },
{ VT_VOID, "void:t27=27" },
};
/********************************************************/
static void gen_cast(TCCState *S, CType *type);
static void gen_cast_s(TCCState *S, int t);
static inline CType *pointed_type(CType *type);
static int is_compatible_types(CType *type1, CType *type2);
static int parse_btype(TCCState *S, CType *type, AttributeDef *ad);
static CType *type_decl(TCCState *S, CType *type, AttributeDef *ad, int *v, int td);
static void parse_expr_type(TCCState *S, CType *type);
static void init_putv(TCCState *S, init_params *p, CType *type, unsigned long c);
static void decl_initializer(TCCState *S, init_params *p, CType *type, unsigned long c, int flags);
static void block(TCCState *S, int is_expr);
static void decl_initializer_alloc(TCCState *S, CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
static void decl(TCCState *S, int l);
static int decl0(TCCState *S, int l, int is_for_loop_init, Sym *);
static void expr_eq(TCCState *S);
static void vla_runtime_type_size(TCCState *S, CType *type, int *a);
static int is_compatible_unqualified_types(CType *type1, CType *type2);
static inline int64_t expr_const64(TCCState *S);
static void vpush64(TCCState *S, int ty, unsigned long long v);
static void vpush(TCCState *S, CType *type);
static int gvtst(TCCState *S, int inv, int t);
static void gen_inline_functions(TCCState *S);
static void free_inline_functions(TCCState *S);
static void skip_or_save_block(TCCState *S, TokenString **str);
static void gv_dup(TCCState *S);
static int get_temp_local_var(TCCState *S, int size,int align);
static void clear_temp_local_var_list();
static void cast_error(TCCState *S, CType *st, CType *dt);
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(TCCState *S)
{
if (!(S->vtop->r & VT_LVAL))
expect(S, "lvalue");
}
ST_FUNC void check_vstack(TCCState *S)
{
if (S->vtop != vstack - 1)
tcc_error(S, "internal compiler error: vstack leak (%d)",
(int)(S->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 = &S->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
/* ------------------------------------------------------------------------- */
/* start of translation unit info */
ST_FUNC void tcc_debug_start(TCCState *S)
{
if (S->do_debug) {
int i;
char buf[512];
/* file info: full path + filename */
S->tccgen_section_sym = put_elf_sym(S, 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(S, buf, N_SO, 0, 0,
text_section->data_offset, text_section, S->tccgen_section_sym);
put_stabs_r(S, S->tccpp_file->prev ? S->tccpp_file->prev->filename : S->tccpp_file->filename,
N_SO, 0, 0,
text_section->data_offset, text_section, S->tccgen_section_sym);
for (i = 0; i < sizeof (default_debug) / sizeof (default_debug[0]); i++)
put_stabs(S, default_debug[i].name, N_LSYM, 0, 0, 0);
S->tccgen_new_file = S->tccgen_last_line_num = 0;
S->tccgen_func_ind = -1;
S->tccgen_debug_next_type = sizeof(default_debug) / sizeof(default_debug[0]);
S->tccgen_debug_hash = NULL;
S->tccgen_n_debug_hash = 0;
/* we're currently 'including' the <command line> */
tcc_debug_bincl(S);
}
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
symbols can be safely used */
put_elf_sym(S, symtab_section, 0, 0,
ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
SHN_ABS, S->tccpp_file->filename);
}
/* put end of translation unit info */
ST_FUNC void tcc_debug_end(TCCState *S)
{
if (!S->do_debug)
return;
put_stabs_r(S, NULL, N_SO, 0, 0,
text_section->data_offset, text_section, S->tccgen_section_sym);
tcc_free(S, S->tccgen_debug_hash);
}
static BufferedFile* put_new_file(TCCState *S)
{
BufferedFile *f = S->tccpp_file;
/* use upper file if from inline ":asm:" */
if (f->filename[0] == ':')
f = f->prev;
if (f && S->tccgen_new_file) {
put_stabs_r(S, f->filename, N_SOL, 0, 0, S->ind, text_section, S->tccgen_section_sym);
S->tccgen_new_file = S->tccgen_last_line_num = 0;
}
return f;
}
/* put alternative filename */
ST_FUNC void tcc_debug_putfile(TCCState *S, const char *filename)
{
if (0 == strcmp(S->tccpp_file->filename, filename))
return;
pstrcpy(S->tccpp_file->filename, sizeof(S->tccpp_file->filename), filename);
S->tccgen_new_file = 1;
}
/* begin of #include */
ST_FUNC void tcc_debug_bincl(TCCState *S)
{
if (!S->do_debug)
return;
put_stabs(S, S->tccpp_file->filename, N_BINCL, 0, 0, 0);
S->tccgen_new_file = 1;
}
/* end of #include */
ST_FUNC void tcc_debug_eincl(TCCState *S)
{
if (!S->do_debug)
return;
put_stabn(S, N_EINCL, 0, 0, 0);
S->tccgen_new_file = 1;
}
/* generate line number info */
static void tcc_debug_line(TCCState *S)
{
BufferedFile *f;
if (!S->do_debug
|| cur_text_section != text_section
|| !(f = put_new_file(S))
|| S->tccgen_last_line_num == f->line_num)
return;
if (S->tccgen_func_ind != -1) {
put_stabn(S, N_SLINE, 0, f->line_num, S->ind - S->tccgen_func_ind);
} else {
/* from tcc_assemble */
put_stabs_r(S, NULL, N_SLINE, 0, f->line_num, S->ind, text_section, S->tccgen_section_sym);
}
S->tccgen_last_line_num = f->line_num;
}
static void tcc_debug_stabs (TCCState *S, const char *str, int type, unsigned long value,
Section *sec, int sym_index)
{
struct debug_sym *s;
if (S->debug_info) {
S->debug_info->sym =
(struct debug_sym *)tcc_realloc (S, S->debug_info->sym,
sizeof(struct debug_sym) *
(S->debug_info->n_sym + 1));
s = S->debug_info->sym + S->debug_info->n_sym++;
s->type = type;
s->value = value;
s->str = tcc_strdup(S, str);
s->sec = sec;
s->sym_index = sym_index;
}
else if (sec)
put_stabs_r (S, str, type, 0, 0, value, sec, sym_index);
else
put_stabs (S, str, type, 0, 0, value);
}
static void tcc_debug_stabn(TCCState *S, int type, int value)
{
if (!S->do_debug)
return;
if (type == N_LBRAC) {
debug_info_t *info =
(debug_info_t *) tcc_mallocz(S, sizeof (*info));
info->start = value;
info->parent = S->debug_info;
if (S->debug_info) {
if (S->debug_info->child) {
if (S->debug_info->child->last)
S->debug_info->child->last->next = info;
else
S->debug_info->child->next = info;
S->debug_info->child->last = info;
}
else
S->debug_info->child = info;
}
else
S->debug_info_root = info;
S->debug_info = info;
}
else {
S->debug_info->end = value;
S->debug_info = S->debug_info->parent;
}
}
static void tcc_get_debug_info(TCCState *S, Sym *s, CString *result)
{
int type;
int n = 0;
int debug_type = -1;
Sym *t = s;
CString str;
for (;;) {
type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE);
if ((type & VT_BTYPE) != VT_BYTE)
type &= ~VT_DEFSIGN;
if (type == VT_PTR || type == (VT_PTR | VT_ARRAY))
n++, t = t->type.ref;
else
break;
}
if ((type & VT_BTYPE) == VT_STRUCT) {
int i;
t = t->type.ref;
for (i = 0; i < S->tccgen_n_debug_hash; i++) {
if (t == S->tccgen_debug_hash[i].type) {
debug_type = S->tccgen_debug_hash[i].debug_type;
break;
}
}
if (debug_type == -1) {
debug_type = ++S->tccgen_debug_next_type;
S->tccgen_debug_hash = (debug_hash_t *)
tcc_realloc (S, S->tccgen_debug_hash,
(S->tccgen_n_debug_hash + 1) * sizeof(*S->tccgen_debug_hash));
S->tccgen_debug_hash[S->tccgen_n_debug_hash].debug_type = debug_type;
S->tccgen_debug_hash[S->tccgen_n_debug_hash++].type = t;
cstr_new (S, &str);
cstr_printf (S, &str, "%s:T%d=%c%d",
(t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
? "" : get_tok_str(S, t->v & ~SYM_STRUCT, NULL),
debug_type,
IS_UNION (t->type.t) ? 'u' : 's',
t->c);
while (t->next) {
int pos, size, align;
t = t->next;
cstr_printf (S, &str, "%s:",
(t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
? "" : get_tok_str(S, t->v & ~SYM_FIELD, NULL));
tcc_get_debug_info (S, t, &str);
if (t->type.t & VT_BITFIELD) {
pos = t->c * 8 + BIT_POS(t->type.t);
size = BIT_SIZE(t->type.t);
}
else {
pos = t->c * 8;
size = type_size(&t->type, &align) * 8;
}
cstr_printf (S, &str, ",%d,%d;", pos, size);
}
cstr_printf (S, &str, ";");
tcc_debug_stabs(S, str.data, N_LSYM, 0, NULL, 0);
cstr_free (S, &str);
}
}
else if (IS_ENUM(type)) {
Sym *e = t = t->type.ref;
debug_type = ++S->tccgen_debug_next_type;
cstr_new (S, &str);
cstr_printf (S, &str, "%s:T%d=e",
(t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
? "" : get_tok_str(S, t->v & ~SYM_STRUCT, NULL),
debug_type);
while (t->next) {
t = t->next;
cstr_printf (S, &str, "%s:",
(t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
? "" : get_tok_str(S, t->v & ~SYM_FIELD, NULL));
cstr_printf (S, &str, e->type.t & VT_UNSIGNED ? "%u," : "%d,",
(int)t->enum_val);
}
cstr_printf (S, &str, ";");
tcc_debug_stabs(S, str.data, N_LSYM, 0, NULL, 0);
cstr_free (S, &str);
}
else if ((type & VT_BTYPE) != VT_FUNC) {
type &= ~VT_STRUCT_MASK;
for (debug_type = 1;
debug_type <= sizeof(default_debug) / sizeof(default_debug[0]);
debug_type++)
if (default_debug[debug_type - 1].type == type)
break;
if (debug_type > sizeof(default_debug) / sizeof(default_debug[0]))
return;
}
if (n > 0)
cstr_printf (S, result, "%d=", ++S->tccgen_debug_next_type);
t = s;
for (;;) {
type = t->type.t & ~(VT_STORAGE | VT_CONSTANT | VT_VOLATILE);
if ((type & VT_BTYPE) != VT_BYTE)
type &= ~VT_DEFSIGN;
if (type == VT_PTR)
cstr_printf (S, result, "%d=*", ++S->tccgen_debug_next_type);
else if (type == (VT_PTR | VT_ARRAY))
cstr_printf (S, result, "%d=ar1;0;%d;",
++S->tccgen_debug_next_type, t->type.ref->c - 1);
else if (type == VT_FUNC) {
cstr_printf (S, result, "%d=f", ++S->tccgen_debug_next_type);
tcc_get_debug_info (S, t->type.ref, result);
return;
}
else
break;
t = t->type.ref;
}
cstr_printf (S, result, "%d", debug_type);
}
static void tcc_debug_finish (TCCState *S, debug_info_t *cur)
{
while (cur) {
int i;
debug_info_t *next = cur->next;
for (i = 0; i < cur->n_sym; i++) {
struct debug_sym *s = &cur->sym[i];
if (s->sec)
put_stabs_r(S, s->str, s->type, 0, 0, s->value,
s->sec, s->sym_index);
else
put_stabs(S, s->str, s->type, 0, 0, s->value);
tcc_free (S, s->str);
}
tcc_free (S, cur->sym);
put_stabn(S, N_LBRAC, 0, 0, cur->start);
tcc_debug_finish (S, cur->child);
put_stabn(S, N_RBRAC, 0, 0, cur->end);
tcc_free (S, cur);
cur = next;
}
}
static void tcc_add_debug_info(TCCState *S, int param, Sym *s, Sym *e)
{
CString debug_str;
if (!S->do_debug)
return;
cstr_new (S, &debug_str);
for (; s != e; s = s->prev) {
if (!s->v || (s->r & VT_VALMASK) != VT_LOCAL)
continue;
cstr_reset (&debug_str);
cstr_printf (S, &debug_str, "%s:%s", get_tok_str(S, s->v, NULL), param ? "p" : "");
tcc_get_debug_info(S, s, &debug_str);
tcc_debug_stabs(S, debug_str.data, param ? N_PSYM : N_LSYM, s->c, NULL, 0);
}
cstr_free (S, &debug_str);
}
/* put function symbol */
static void tcc_debug_funcstart(TCCState *S, Sym *sym)
{
CString debug_str;
BufferedFile *f;
if (!S->do_debug)
return;
S->debug_info_root = NULL;
S->debug_info = NULL;
tcc_debug_stabn(S, N_LBRAC, S->ind - S->tccgen_func_ind);
if (!(f = put_new_file(S)))
return;
cstr_new (S, &debug_str);
cstr_printf(S, &debug_str, "%s:%c", S->tccgen_funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
tcc_get_debug_info(S, sym->type.ref, &debug_str);
put_stabs_r(S, debug_str.data, N_FUN, 0, f->line_num, 0, cur_text_section, sym->c);
cstr_free (S, &debug_str);
tcc_debug_line(S);
}
/* put function size */
static void tcc_debug_funcend(TCCState *S, int size)
{
if (!S->do_debug)
return;
tcc_debug_stabn(S, N_RBRAC, size);
tcc_debug_finish (S, S->debug_info_root);
}
static void tcc_debug_extern_sym(TCCState *S, Sym *sym, int sh_num, int sym_bind, int sym_type)
{
Section *s;
CString str;
if (!S->do_debug)
return;
if (sym_type == STT_FUNC || sym->v >= SYM_FIRST_ANOM)
return;
s = S->sections[sh_num];
cstr_new (S, &str);
cstr_printf (S, &str, "%s:%c",
get_tok_str(S, sym->v, NULL),
sym_bind == STB_GLOBAL ? 'G' : S->local_scope ? 'V' : 'S'
);
tcc_get_debug_info(S, sym, &str);
if (sym_bind == STB_GLOBAL)
tcc_debug_stabs(S, str.data, N_GSYM, 0, NULL, 0);
else
tcc_debug_stabs(S, str.data,
(sym->type.t & VT_STATIC) && data_section == s
? N_STSYM : N_LCSYM, 0, s, sym->c);
cstr_free (S, &str);
}
static void tcc_debug_typedef(TCCState *S, Sym *sym)
{
CString str;
if (!S->do_debug)
return;
cstr_new (S, &str);
cstr_printf (S, &str, "%s:t",
(sym->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
? "" : get_tok_str(S, sym->v & ~SYM_FIELD, NULL));
tcc_get_debug_info(S, sym, &str);
tcc_debug_stabs(S, str.data, N_LSYM, 0, NULL, 0);
cstr_free (S, &str);
}
/* ------------------------------------------------------------------------- */
/* for section layout see lib/tcov.c */
static void tcc_tcov_block_end(TCCState *S, int line);
static void tcc_tcov_block_begin(TCCState *S)
{
SValue sv;
void *ptr;
unsigned long last_offset = S->tccgen_tcov_data.offset;
tcc_tcov_block_end (S, 0);
if (S->test_coverage == 0 || S->nocode_wanted)
return;
if (S->tccgen_tcov_data.last_file_name == 0 ||
strcmp ((const char *)(tcov_section->data + S->tccgen_tcov_data.last_file_name),
S->tccpp_file->true_filename) != 0) {
char wd[1024];
CString cstr;
if (S->tccgen_tcov_data.last_func_name)
section_ptr_add(S, tcov_section, 1);
if (S->tccgen_tcov_data.last_file_name)
section_ptr_add(S, tcov_section, 1);
S->tccgen_tcov_data.last_func_name = 0;
cstr_new (S, &cstr);
if (S->tccpp_file->true_filename[0] == '/') {
S->tccgen_tcov_data.last_file_name = tcov_section->data_offset;
cstr_printf (S, &cstr, "%s", S->tccpp_file->true_filename);
}
else {
getcwd (wd, sizeof(wd));
S->tccgen_tcov_data.last_file_name = tcov_section->data_offset + strlen(wd) + 1;
cstr_printf (S, &cstr, "%s/%s", wd, S->tccpp_file->true_filename);
}
ptr = section_ptr_add(S, tcov_section, cstr.size + 1);
strcpy((char *)ptr, cstr.data);
#ifdef _WIN32
normalize_slashes((char *)ptr);
#endif
cstr_free (S, &cstr);
}
if (S->tccgen_tcov_data.last_func_name == 0 ||
strcmp ((const char *)(tcov_section->data + S->tccgen_tcov_data.last_func_name),
S->tccgen_funcname) != 0) {
size_t len;
if (S->tccgen_tcov_data.last_func_name)
section_ptr_add(S, tcov_section, 1);
S->tccgen_tcov_data.last_func_name = tcov_section->data_offset;
len = strlen (S->tccgen_funcname);
ptr = section_ptr_add(S, tcov_section, len + 1);
strcpy((char *)ptr, S->tccgen_funcname);
section_ptr_add(S, tcov_section, -tcov_section->data_offset & 7);
ptr = section_ptr_add(S, tcov_section, 8);
write64le (ptr, S->tccpp_file->line_num);
}
if (S->ind == S->tccgen_tcov_data.ind && S->tccgen_tcov_data.line == S->tccpp_file->line_num)
S->tccgen_tcov_data.offset = last_offset;
else {
Sym label = {0};
label.type.t = VT_LLONG | VT_STATIC;
ptr = section_ptr_add(S, tcov_section, 16);
S->tccgen_tcov_data.line = S->tccpp_file->line_num;
write64le (ptr, (S->tccgen_tcov_data.line << 8) | 0xff);
put_extern_sym(S, &label, tcov_section,
((unsigned char *)ptr - tcov_section->data) + 8, 0);
sv.type = label.type;
sv.r = VT_SYM | VT_LVAL | VT_CONST;
sv.r2 = VT_CONST;
sv.c.i = 0;
sv.sym = &label;
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || \
defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 || \
defined TCC_TARGET_RISCV64
gen_increment_tcov (S, &sv);
#else
vpushv(S, &sv);
inc(S, 0, TOK_INC);
vpop(S);
#endif
S->tccgen_tcov_data.offset = (unsigned char *)ptr - tcov_section->data;
S->tccgen_tcov_data.ind = S->ind;
}
}
static void tcc_tcov_block_end(TCCState *S, int line)
{
if (S->test_coverage == 0)
return;
if (S->tccgen_tcov_data.offset) {
void *ptr = tcov_section->data + S->tccgen_tcov_data.offset;
unsigned long long nline = line ? line : S->tccpp_file->line_num;
write64le (ptr, (read64le (ptr) & 0xfffffffffull) | (nline << 36));
S->tccgen_tcov_data.offset = 0;
}
}
static void tcc_tcov_check_line(TCCState *S, int start)
{
if (S->test_coverage == 0)
return;
if (S->tccgen_tcov_data.line != S->tccpp_file->line_num) {
if ((S->tccgen_tcov_data.line + 1) != S->tccpp_file->line_num) {
tcc_tcov_block_end (S, S->tccgen_tcov_data.line);
if (start)
tcc_tcov_block_begin (S);
}
else
S->tccgen_tcov_data.line = S->tccpp_file->line_num;
}
}
static void tcc_tcov_start(TCCState *S)
{
if (S->test_coverage == 0)
return;
memset (&S->tccgen_tcov_data, 0, sizeof (S->tccgen_tcov_data));
if (tcov_section == NULL) {
tcov_section = new_section(S, ".tcov", SHT_PROGBITS,
SHF_ALLOC | SHF_WRITE);
section_ptr_add(S, tcov_section, 4); // pointer to executable name
}
}
static void tcc_tcov_end(TCCState *S)
{
if (S->test_coverage == 0)
return;
if (S->tccgen_tcov_data.last_func_name)
section_ptr_add(S, tcov_section, 1);
if (S->tccgen_tcov_data.last_file_name)
section_ptr_add(S, tcov_section, 1);
}
/* ------------------------------------------------------------------------- */
/* initialize vstack and types. This must be done also for tcc -E */
ST_FUNC void tccgen_init(TCCState *S)
{
S->vtop = vstack - 1;
memset(S->vtop, 0, sizeof *S->vtop);
/* define some often used types */
S->tccgen_int_type.t = VT_INT;
S->tccgen_char_type.t = VT_BYTE;
if (S->char_is_unsigned)
S->tccgen_char_type.t |= VT_UNSIGNED;
S->char_pointer_type = S->tccgen_char_type;
mk_pointer(S, &S->char_pointer_type);
S->tccgen_func_old_type.t = VT_FUNC;
S->tccgen_func_old_type.ref = sym_push(S, SYM_FIELD, &S->tccgen_int_type, 0, 0);
S->tccgen_func_old_type.ref->f.func_call = FUNC_CDECL;
S->tccgen_func_old_type.ref->f.func_type = FUNC_OLD;
#ifdef precedence_parser
init_prec(S);
#endif
cstr_new(S, &S->tccgen_initstr);
}
ST_FUNC int tccgen_compile(TCCState *S)
{
cur_text_section = NULL;
S->tccgen_funcname = "";
S->tccgen_anon_sym = SYM_FIRST_ANOM;
S->tccgen_section_sym = 0;
S->tccgen_const_wanted = 0;
S->nocode_wanted = 0x80000000;
S->local_scope = 0;
S->tccgen_debug_modes = S->do_debug | S->test_coverage << 1;
tcc_debug_start(S);
tcc_tcov_start (S);
#ifdef TCC_TARGET_ARM
arm_init(S);
#endif
#ifdef INC_DEBUG
printf("%s: **** new file\n", file->filename);
#endif
S->tccpp_parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
next(S);
decl(S, VT_CONST);
gen_inline_functions(S);
check_vstack(S);
/* end of translation unit info */
tcc_debug_end(S);
tcc_tcov_end (S);
return 0;
}
ST_FUNC void tccgen_finish(TCCState *S)
{
cstr_free(S, &S->tccgen_initstr);
free_inline_functions(S);
sym_pop(S, &S->tccgen_global_stack, NULL, 0);
sym_pop(S, &S->local_stack, NULL, 0);
/* free preprocessor macros */
free_defines(S, NULL);
/* free sym_pools */
dynarray_reset(S, &S->tccgen_sym_pools, &S->tccgen_nb_sym_pools);
S->tccgen_sym_free_first = NULL;
}
/* ------------------------------------------------------------------------- */
ST_FUNC ElfSym *elfsym(TCCState *S, 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(TCCState *S, Sym *sym)
{
ElfSym *esym;
int sym_bind, old_sym_bind;
esym = elfsym(S, 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(S, 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(TCCState *S, 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(S, 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;