-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
9278 lines (8126 loc) · 219 KB
/
main.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
/*
* C programming language parser.
*
* flex -o scan.c scan.l
* gcc main.c symtab.c stack.c -mno-cygwin -o cparser.exe
*
* Pan Wenjian
* 2009.6.3 Started this program
* ???? The syntax parser is released
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <stdarg.h>
#include "symtab.h"
#include "stack.h"
#include "scan.c"
#include "i386.h"
#include "pcc.h"
SqStack g_sym_stack;
/* global stack for statement context */
SqStack statement_ctx_stk;
func_ctx g_func_ctx; /* function context */
bool is_in_function = F;
bool g_ajust_offset_needed = F;
char * g_ajust_offset_ptr;
#define OUTBUF g_func_ctx.buf
#define OUTBUFIDX g_func_ctx.buf_index
int g_cur_stage;
int g_stk_h2l = 1; // for Intel CPU
int g_label = 0;
char out2[4096];
typedef struct {
CType **ap;
int cnts;
} call_rop;
struct pre_inc_code {
char * start;
char *end;
} g_pre_inc_code;
/* it's tricky, just handle like &(*a) */
SqStack addr_op_stk;
/******************** function prototype **********************/
int gettoken(void);
void restoretoken();
void restoretoken2(char *s);
int type_qualifier();
int type_qualifier_list(qualifier *qu);
void declarator(CType *pCType, int *dt);
void declarator2(CType *pCType, int *dt);
CType * pointer();
int parameter_declaration(CType **param_ctype);
int parameter_list(CType *pCType);
void parameter_type_list(CType *pCType);
void identifier_list(CType *pCType);
int type_specifier(CType *pCType, CType **ct_out, bool tag_must_def_before);
int storage_class_specifier();
int declaration_specifiers(CType *pCType, bool tag_must_def_before);
CType * init_declarator(CType *pctype, int idx, bool no_init);
CType_list * init_declarator_list(CType *pCType, bool no_init);
CType_list * declaration(bool has_decl_spec, bool no_init);
void type_copy(CType *to, CType *from);
char *store_name(const char *from, int strlen);
CType * create_ctype();
void free_ctype(void*);
void build_ctype(CType *pCType);
int symtabs_lookup( const char *key, int type, symvalue_t **value);
void error(const char *p);
void print_ctype(CType *pCType, char *out);
int struct_or_union_specifier(CType * pCType, CType **ct_out, bool tag_must_def_before);
int struct_or_union();
void struct_declaration_list(CType * pctype) ;
void struct_declarator(CType * pctype);
CType_list * struct_declarator_list(CType * pctype);
CType_list * struct_declaration();
int enum_specifier(CType **ct_out, bool tag_must_def_before);
CType_list * create_ctype_list();
void free_ctype_list(CType_list *ct);
void CType_cpy(CType * to, CType * from, bool copy_name_and_link);
CType * add_decl_to_symtab(CType* pctype);
bool is_ctype_compatible(CType * pctype1, CType * pctype2);
value_t* create_c_val();
void free_c_val(value_t *p);
int primary_expr(CType **p);
int postfix_expr(CType **p);
int unary_expr(CType **p);
int cast_expr(CType **p);
int multiplicative_expr(CType **p);
int additive_expr(CType **p);
int shift_expr(CType ** p);
int relational_expr(CType **p);
int equality_expr(CType **p);
int and_expr(CType **p);
int exclusive_or_expr(CType **p);
int inclusive_or_expr(CType **p);
int logical_and_expr(CType **p);
int logical_or_expr(CType **p);
int conditional_expr(CType **p);
int assignment_expr(CType **p);
int expr(CType **p, bool later_gen);
int constant_expr(CType ** p);
label_t *create_label(char *name);
void free_label(label_t *p);
statement_ctx *create_sta_ctx();
void free_sta_ctx(statement_ctx *p);
int find_sta_ctx(int sta_t, statement_ctx **p);
void do_label(statement_ctx *ctx);
int generate_tmp_ctype(CType ** to, CType * from);
bool is_ctype_array(CType * pctype);
void gen_assign(CType *pp, CType *tmp);
void add_ctype_to_stack(CType *pctype);
CType * genop(CType * ct);
void adjust_offset(int size);
/**************************************************************/
char out_buffer[4096];
void oprintf(char *format, ...)
{
static char buffer[1024] = {0};
va_list ap;
va_start(ap, format);
vsprintf(buffer, format, ap);
va_end(ap);
OUTBUFIDX += sprintf(&(OUTBUF[OUTBUFIDX]), buffer);
}
void error(const char *p)
{
printf(p);
exit(-1);
}
void dump(CType * tmp)
{
struct list_head *head;
struct list_head *curr;
int end = 0;
printf("-------Dump CType\n");
printf("CType is %x\n", tmp);
printf("getfrom 0x%x\n", tmp->get_from);
head = &(tmp->link);
printf("next is %x\n", head->next);
printf("prev is %x\n\n", head->prev);
for (curr = head->next; curr != NULL; ) {
printf("curr is %x\n", curr );
if(tmp->get_from != NULL && curr == head->prev) {
printf("end\n");
end = 1;
}
curr = curr->next;
if(end) break;
}
printf("------------------\n");
}
value_t *create_c_val()
{
value_t * p;
p = malloc(sizeof(value_t));
if (p == NULL) {
printf("error: Out of memory\n");
exit(-1);
}
memset(p, 0, sizeof(value_t));
INIT_LIST_HEAD(&(p->link));
return p;
}
void free_c_val(value_t *p)
{
if(p->string_t == STR_CHAR)
free(p->content.s);
else if(p->string_t == STR_LONG)
free(p->content.ls);
else {
/* do nothing, not string literal */
}
free(p);
}
void free_ctype(void *p)
{
CType * tmp = (CType *)p;
struct list_head *head;
struct list_head *curr;
int i;
int end = 0;
if(tmp->datatype == DATATYPE_ELLIPSIS || tmp->symtype == SYM_TYPE_LABEL
|| tmp->datatype == DATATYPE_OP){
free(tmp);
return;
}
if(tmp->datatype == DATATYPE_POINTER){ /* This is to free ctype pointer created from '&' */
if(tmp->types.pointer.is_from_addrop == T) {
free(tmp);
}
return;
}
if(tmp->datatype == DATATYPE_STRUCT|| tmp->datatype == DATATYPE_UNION)
{
if(tmp->name) {
//printf("free struct '%s'\n", tmp->name);
free(tmp->name);
}
if(tmp->types.struct_union_enum.table != NULL)
symtab_destroy(&(tmp->types.struct_union_enum.table), free_ctype);
if(tmp->types.struct_union_enum.item_list != NULL)
free(tmp->types.struct_union_enum.item_list);
free(p);
return;
}
if(tmp->datatype == DATATYPE_ENUM) {
if(tmp->name) {
//printf("free ENUM '%s'\n", tmp->name);
free(tmp->name);
}
// tmp->types.struct_union_enum.table is not need to be freed;
// it's NULL or !NULL
free(p);
return;
}
assert(tmp->datatype == DATATYPE_NORMAL);
if(tmp->symtype == SYM_TYPE_NO_NAME_VAL) {
free_c_val(tmp->v);
}
if(tmp->name) {
//printf("free '%s'\n", tmp->name);
free(tmp->name);
}
if(tmp->types.normal.d_attr.type_spec == TS_STRUCT
|| tmp->types.normal.d_attr.type_spec == TS_UNION) {
CType *ct = tmp->types.normal.d_attr.st_un;
//printf("is %x\n", ct);
if(tmp->types.normal.d_attr.tag) {
//printf("free tag '%s'\n", tmp->types.normal.d_attr.tag);
free(tmp->types.normal.d_attr.tag);
}
if(ct->types.struct_union_enum.attach_to == tmp) {
//printf("free attached %x\n", ct);
free_ctype(ct);
}
}
/* dump(tmp); */
head = &(tmp->link);
if(tmp->get_from != NULL && head == head->prev){
free(p);
return;
}
/*
if ctype list is not get from other, free all the list (i.e. free all ctypes
in the list until next is NULL), else only free until the head->prev point to.
e.g
typedef int *P;
P ptr;
P (DATATYPE_NORMAL) (DATATYPE_POINT)
+--------+ +-------+
| int | --- -> | point to | ----> NULL
| (typedef) | next | | next
+--------+ +-------+
| prev ^
+------------------|
|
ptr (DATATYPE_NORMAL) |
+--------+ |
| int | --- -> NULL |
| | |
+--------+ |
| get_from |
+------------------+
*/
for (curr = head->next; curr != NULL; ) {
if(tmp->get_from != NULL && curr == head->prev)
end = 1;
CType * elt = list_entry(curr, CType, link);
curr = curr->next;
assert(elt->name == NULL);
if(elt->datatype == DATATYPE_FUNCTION) {
if(elt->types.function.param_list != NULL) {
for(i = elt->types.function.nr_param; i>0; i--) {
//printf("free param\n");
CType * ct = elt->types.function.param_list[i-1];
if(ct->datatype == DATATYPE_ELLIPSIS)
free_ctype((void*)ct);
}
free(elt->types.function.param_list);
}
/* if elt->types.function.nr_param == 0, then elt->types.function.table should == NULL */
if(elt->types.function.table != NULL)
symtab_destroy(&(elt->types.function.table), free_ctype);
}
free(elt);
if(end) break;
}
free(p);
}
bool is_ctype_compatible(CType * pctype1, CType * pctype2)
{
struct list_head* list1;
struct list_head* list2;
CType *tmp1, *tmp2;
/* TODO: a well defined type compatible function */
list1 = &(pctype1->link);
list2 = &(pctype2->link);
while(list1 != NULL && list2 != NULL) {
tmp1 = list_entry(list1, CType, link);
tmp2 = list_entry(list2, CType, link);
if((tmp1->datatype == tmp2->datatype && tmp1->symtype == tmp2->symtype) ||
(tmp1->datatype == tmp2->datatype && tmp1->symtype != tmp2->symtype &&
(tmp1->symtype != SYM_TYPE_TAG && tmp1->symtype != SYM_TYPE_TYPEDEF) &&
(tmp2->symtype != SYM_TYPE_TAG && tmp2->symtype != SYM_TYPE_TYPEDEF))
){
if(tmp1->datatype == DATATYPE_NORMAL) {
if(tmp1->types.normal.d_attr.has_stor_spec ==
tmp2->types.normal.d_attr.has_stor_spec &&
tmp1->types.normal.d_attr.stor_spec ==
tmp2->types.normal.d_attr.stor_spec &&
tmp1->types.normal.d_attr.has_type_spec ==
tmp2->types.normal.d_attr.has_type_spec &&
tmp1->types.normal.d_attr.type_spec ==
tmp2->types.normal.d_attr.type_spec &&
tmp1->types.normal.d_attr.st_un ==
tmp2->types.normal.d_attr.st_un) {
if(tmp1->types.normal.d_attr.qua.q_const
== tmp2->types.normal.d_attr.qua.q_const
&& tmp1->types.normal.d_attr.qua.q_volatile
== tmp2->types.normal.d_attr.qua.q_volatile) {
} else {
printf("type not compatible0 %d %d\n", tmp1->symtype, tmp2->symtype);
return F;
}
} else {
if(tmp1->types.normal.d_attr.type_spec !=
tmp2->types.normal.d_attr.type_spec) {
if(tmp1->types.normal.d_attr.type_spec < TS_FLOAT &&
tmp2->types.normal.d_attr.type_spec < TS_FLOAT)
return T;
}
printf("type not compatible1 %d %d\n", tmp1->symtype, tmp2->symtype);
printf("%d %d\n", tmp1->types.normal.d_attr.has_stor_spec,
tmp2->types.normal.d_attr.has_stor_spec);
printf("%d %d\n", tmp1->types.normal.d_attr.stor_spec,
tmp2->types.normal.d_attr.stor_spec);
printf("%d %d\n", tmp1->types.normal.d_attr.has_type_spec,
tmp2->types.normal.d_attr.has_type_spec);
printf("%d %d\n", tmp1->types.normal.d_attr.type_spec,
tmp2->types.normal.d_attr.type_spec);
printf("%d %d\n", tmp1->types.normal.d_attr.st_un,
tmp2->types.normal.d_attr.st_un);
return F;
}
} else if(tmp1->datatype == DATATYPE_POINTER) {
if(tmp1->types.normal.d_attr.qua.q_const
== tmp2->types.normal.d_attr.qua.q_const
&& tmp1->types.normal.d_attr.qua.q_volatile
== tmp2->types.normal.d_attr.qua.q_volatile) {
} else return F;
} else if(tmp1->datatype == DATATYPE_FUNCTION) {
int i=0;
if(tmp1->types.function.nr_param == 0 || tmp2->types.function.nr_param == 0)
return T;
if(tmp1->types.function.is_old_style == T ||
tmp2->types.function.is_old_style == T) {
return T;
}
for(; i< tmp1->types.function.nr_param; i++) {
if(is_ctype_compatible(tmp1->types.function.param_list[i],
tmp2->types.function.param_list[i]) == F)
return F;
}
} else if(tmp1->datatype == DATATYPE_ARRAY) {
/* do nothing */
} else if(tmp1->datatype == DATATYPE_ELLIPSIS) {
/* do nothing */
} else {
printf("Type compare, This should not happen\n");
exit(0);
}
}
else {
printf("type not compatible2 %d %d\n", tmp1->symtype, tmp2->symtype);
return F;
}
go_on:
list1 = list1->next;
list2 = list2->next;
}
if(list1 == NULL && list2 == NULL) return T;
printf("type not compatible3 %d %d\n", tmp1->symtype, tmp2->symtype);
return F;
}
void free_ctype_list(CType_list *ct)
{
struct list_head *curr;
struct list_head *head;
CType_list * tmp;
head = &(ct->link);
for (curr = head->next; curr != head; ) {
tmp = list_entry(curr, CType_list, link);
curr = curr->next;
free(tmp);
}
free(ct);
}
void print_help()
{
printf(
"A tool for analyse C language declaration.\n"
"Pan Wenjian\n"
"2009.6.5\n"
"If you find bugs, please mail to [email protected]. Thank you! ;-) \n"
"\n"
"examples:\n"
"int f(char * const *(*next)());\n"
"f: function ( parameters: next: pointer to function returning pointer to const\n"
"pointer to char ) returning int\n"
"\n"
"char *(* c[])(int **p);\n"
"c: array[] of pointer to function ( parameters: p: pointer to pointer to int )\n"
"returning pointer to char\n"
"\n"
"void (*signal(int sig, void (*func)(int a)))(int b);\n"
"signal: function ( parameters: sig: int,func: pointer to function (parameters:\n"
"a: int ) returning void ) returning pointer to function ( parameters: b: int )\n"
"returning void\n\r"
);
}
/*
store name: make sure 'strlen' is string length of 'from'
*/
char *store_name(const char *from, int strlen)
{
char *name;
name = malloc(strlen+1);
if(name == NULL) {
error("Out of memory\n");
}
strcpy(name, from);
return name;
}
CType_list * create_ctype_list()
{
CType_list * p;
p = malloc(sizeof(CType_list));
if (p == NULL) {
printf("error: Out of memory\n");
exit(-1);
}
INIT_LIST_HEAD(&(p->link));
p->cnt = 0;
p->ct = NULL;
return p;
}
CType * create_ctype()
{
CType * p;
p = malloc(sizeof(CType));
if (p == NULL) {
printf("error: Out of memory\n");
exit(-1);
}
memset(p, 0, sizeof(CType));
INIT_LIST_HEAD(&(p->link));
p->datatype = DATATYPE_NORMAL; /* The first CType is always normal */
p->storage_type = STORAGE_UNKNOW;
p->lvalue = 1; /* default we assume this Ctype has lvalue */
return p;
}
CType * create_op_ctype()
{
CType * p;
p = malloc(sizeof(CType));
if (p == NULL) {
printf("error: Out of memory\n");
exit(-1);
}
memset(p, 0, sizeof(CType));
INIT_LIST_HEAD(&(p->link));
p->datatype = DATATYPE_OP;
return p;
}
static unsigned long sizeof_ctype(CType *pCType, bool check_size)
{
CType *temp = NULL;
if(pCType == NULL) {
// error
printf("sizeof ctype error!\n");
exit(0);
}
if(is_ctype_normal(pCType)){
int d_type = pCType->types.normal.d_attr.type_spec;
if((d_type & TS_LONG_LONG) == TS_LONG_LONG) {
return 8;
} else if((d_type & TS_LONG) == TS_LONG) {
return 4;
} else if((d_type & TS_SHORT) == TS_SHORT) {
return 2;
} else if((d_type & TS_SHORT_INT) == TS_SHORT_INT) {
return 2;
} else if((d_type & TS_INT) == TS_INT) {
return 4;
} else if((d_type & TS_CHAR) == TS_CHAR) {
return 1;
} else if((d_type & TS_FLOAT) == TS_FLOAT) {
return 4;
} else if((d_type & TS_DOUBLE) == TS_DOUBLE) {
return 8;
}
/* TODO: other basic types ....*/
} else if(is_ctype_pointer(pCType)){
struct list_head *curr;
curr = pCType->link.next;
if(curr != NULL) {
CType * elt = list_entry(curr, CType, link);
if(elt->datatype == DATATYPE_ARRAY &&
pCType->storage_type != STORAGE_PARAM)
goto is_array;
}
return 4;
} else if(is_ctype_array(pCType)) {
struct list_head *curr;
CType * elt;
is_array:
curr = pCType->link.next;
elt = list_entry(curr, CType, link);
generate_tmp_ctype(&temp, pCType);
temp->link.next = temp->link.next->next;
if(check_size && elt->types.array.is_pointer_alike) {
printf("%d: error: size unknown\n", line);
exit(0);
}
pCType->size_of = elt->types.array.array_size * sizeof_ctype(temp, check_size);
free(temp);
} else if(is_ctype_struct(pCType) || is_ctype_union(pCType)) {
int i = 0;
int size = 0;
int oldsize;
CType * tmp1 = pCType->types.normal.d_attr.st_un;
pCType->size_of = tmp1->size_of;
}
return pCType->size_of ;
}
void build_code_gen_fields(CType *pCType)
{
/* 1. storage type */
if(pCType->types.normal.d_attr.has_stor_spec == T &&
pCType->types.normal.d_attr.stor_spec == STATIC) {
pCType->storage_type = STORAGE_STATIC;
} else {
if(g_cur_stage == STAGE_FUNCTION) {
if(pCType->types.normal.d_attr.stor_spec != TYPEDEF)
pCType->storage_type = STORAGE_LOCAL;
} else if (g_cur_stage == STAGE_PARAM) {
pCType->storage_type = STORAGE_PARAM;
} else if(g_cur_stage == STAGE_GLOBAL) {
pCType->storage_type = STORAGE_GLOBAL;
} else {
// maybe reg or constant
}
}
/* 2. size of */
pCType->size_of = sizeof_ctype(pCType, F);
/* 3. */
}
void build_ctype(CType *pCType)
{
if(pCType->datatype == DATATYPE_ELLIPSIS){
pCType->link.prev->next = NULL;
return;
}
assert(pCType->datatype == DATATYPE_NORMAL);
if(pCType->name != NULL && (pCType->types.normal.d_attr.type_spec == TS_STRUCT ||
pCType->types.normal.d_attr.type_spec == TS_UNION) ){
CType *tmp;
assert(pCType->types.normal.d_attr.st_un != NULL);
tmp = pCType->types.normal.d_attr.st_un;
// struct or union table may be empty, ex. struct s {};
if(tmp->types.struct_union_enum.table == NULL &&
(tmp->types.struct_union_enum.is_empty != T &&
pCType->types.normal.d_attr.stor_spec != TYPEDEF)) {
printf("%d: size of struct '%s' unknow\n", line, pCType->name);
exit(0);
}
}
if(pCType->name != NULL && pCType->link.next == NULL
&& pCType->types.normal.d_attr.type_spec == TS_VOID) {
printf("%d: storage of '%s' unknow\n", line, pCType->name);
exit(0);
}
if(pCType->types.normal.d_attr.has_stor_spec == T &&
pCType->types.normal.d_attr.stor_spec == TYPEDEF) {
pCType->symtype = SYM_TYPE_TYPEDEF;
}
if(pCType->name != NULL) {
if(pCType->symtype == 0)
pCType->symtype = SYM_TYPE_OBJECT;
} else if(pCType->symtype == 0){
pCType->symtype = SYM_TYPE_NONAME;
}
if(pCType->get_from == NULL) {
pCType->link.prev->next = NULL;
} else {
assert(pCType->get_from->types.normal.d_attr.has_stor_spec == T
&& pCType->get_from->types.normal.d_attr.stor_spec == TYPEDEF );
pCType->datatype = pCType->get_from->datatype;
/* check if ()() ()[] []() , like
typedef int tf();
tf tf2();
*/
CType * tmp;
struct list_head * l;
assert(pCType->link.prev != NULL);
tmp = list_entry((pCType->link.prev), CType, link);
if(tmp->datatype == DATATYPE_ARRAY) {
l = pCType->get_from->link.next;
tmp = list_entry(l, CType, link);
if(tmp && tmp->datatype == DATATYPE_FUNCTION) {
printf("%d: error: '%s' declared as array of function\n", line, pCType->name);
exit(0);
}
} else if(tmp->datatype == DATATYPE_FUNCTION) {
l = pCType->get_from->link.next;
tmp = list_entry(l, CType, link);
/*
if tmp is NULL, this is happen when
typedef struct {} tf;
tf tf2();
*/
if(tmp) {
if(tmp->datatype == DATATYPE_FUNCTION) {
printf("%d: error: '%s' declared as function return a function\n", line, pCType->name);
exit(0);
}else if (tmp->datatype == DATATYPE_ARRAY){
printf("%d: error: '%s' declared as function return a array\n", line, pCType->name);
exit(0);
}
}
}
/* pCType->types.normal id get from pCType->get_from->types.normal
except the storage type
*/
pCType->types.normal.d_attr.has_type_spec
= pCType->get_from->types.normal.d_attr.has_type_spec;
pCType->types.normal.d_attr.type_spec
= pCType->get_from->types.normal.d_attr.type_spec;
if(pCType->types.normal.d_attr.qua.q_const == 0)
pCType->types.normal.d_attr.qua.q_const
= pCType->get_from->types.normal.d_attr.qua.q_const;
if(pCType->types.normal.d_attr.qua.q_volatile == 0)
pCType->types.normal.d_attr.qua.q_volatile
= pCType->get_from->types.normal.d_attr.qua.q_volatile;
if(pCType->get_from->types.normal.d_attr.tag) {
pCType->types.normal.d_attr.tag
= store_name(pCType->get_from->types.normal.d_attr.tag,
strlen(pCType->get_from->types.normal.d_attr.tag));
}
pCType->types.normal.d_attr.st_un
= pCType->get_from->types.normal.d_attr.st_un;
pCType->link.prev->next = pCType->get_from->link.next;
}
if(pCType->types.normal.d_attr.type_spec == TS_SIGNED ||
pCType->types.normal.d_attr.type_spec == TS_USIGNED) {
pCType->types.normal.d_attr.type_spec |= INT_MASK;
} else {
int d_type = pCType->types.normal.d_attr.type_spec & (~(SIGNED_MASK|UNSIGNED_MASK));
if( d_type == TS_LONG || d_type == TS_SHORT || d_type == TS_LONG_LONG)
pCType->types.normal.d_attr.type_spec |= INT_MASK;
}
if(pCType->types.normal.d_attr.type_spec == 0 && (pCType->types.normal.d_attr.has_stor_spec ||
(pCType->types.normal.d_attr.qua.q_const != 0 || pCType->types.normal.d_attr.qua.q_volatile != 0))) {
pCType->types.normal.d_attr.type_spec = INT_MASK;
pCType->types.normal.d_attr.has_type_spec = T;
}
/* next, build code generation fileds */
build_code_gen_fields(pCType);
#if 1
/*
If function return value (eg. structure ) which size is big than 4,
means a 32bits reg can not hold it, we need push a pointer of
a return value before the first pushed parameter
*/
if(is_ctype_function(pCType)){
CType * rct = NULL;
generate_tmp_ctype(&rct, pCType);
struct list_head *lh;
lh = rct->link.next->next;
rct->link.next = lh; // function return value ctype
rct->size_of = sizeof_ctype(rct, F);
CType *c_tmp;
struct list_head *cur = pCType->link.next;
c_tmp = list_entry(cur, CType, link);
if(rct->size_of > 4) {
int i = 0;
for(i = 0; i < c_tmp->types.function.nr_param; i++) {
CType *ct = c_tmp->types.function.param_list[i];
if(ct->datatype == DATATYPE_ELLIPSIS) {
assert(i+1 == c_tmp->types.function.nr_param);
break;
}
else
ct->offset += 4; /* pointer size is 4 */
}
}
free(rct);
}
#endif
return;
}
/*
This function only copy a head ctype, for create a new ctype list
*/
void CType_cpy(CType * to, CType * from, bool copy_name_and_link)
{
assert(from->datatype == DATATYPE_NORMAL);
memcpy(to, from, sizeof(CType));
if(copy_name_and_link == T) {
if(from->name != NULL)
to->name = store_name(from->name, strlen(from->name));
}
else {
to->name = NULL;
INIT_LIST_HEAD(&(to->link));
}
if(from->types.normal.d_attr.tag) {
to->types.normal.d_attr.tag
= store_name(from->types.normal.d_attr.tag, strlen(from->types.normal.d_attr.tag));
}
}
static char *
itoa(int value, char *string, int radix)
{
char tmp[33];
char *tp = tmp;
int i;
unsigned v;
int sign;
char *sp;
if (radix > 36 || radix <= 1)
{
return 0;
}
sign = (radix == 10 && value < 0);
if (sign)
v = -value;
else
v = (unsigned)value;
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}
if (string == 0)
string = (char *)malloc((tp-tmp)+sign+1);
sp = string;
if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}
void print_ctype(CType *pCType, char *out)
{
struct list_head *head;
struct list_head *curr;
int i;
int tmp;
char buff[256];
if(pCType->line > 0) {
char buf[100];
sprintf(buf, "%d: ", pCType->line);
strcat(out, buf);
}
if(pCType->datatype == DATATYPE_ELLIPSIS){
assert(pCType->link.next == NULL);
strcat(out, " ...");
return;
}
if(pCType->types.normal.d_attr.has_stor_spec) {
tmp = pCType->types.normal.d_attr.stor_spec;
switch(tmp)
{
case AUTO:
strcat(out, "auto <- ");
break;
case REGISTER:
strcat(out, "register <- ");
break;
case STATIC:
strcat(out, "static <- ");
break;
case EXTERN:
strcat(out, "extern <- ");
break;
case TYPEDEF:
strcat(out, "typedef <- ");
break;
default:
break;
}
}
if(pCType->name != NULL) {
strcat(out, pCType->name);
strcat(out, ": ");
}
if(pCType->types.normal.d_attr.has_enum_val == T) { /* is enum ID itself */
char buf[256];
assert(pCType->link.next == NULL);
strcat(out, " enum ID, val = ");
sprintf(buf, "%x", pCType->types.normal.d_attr.enum_val);
strcat(out, buf);
return;
}
head = &(pCType->link);
for (curr = head->next; curr != NULL; ) {
CType * elt = list_entry(curr, CType, link);
curr = curr->next;
assert(elt->datatype != DATATYPE_NORMAL);
if(elt->datatype == DATATYPE_ARRAY) {
strcat(out, " array[");
if(elt->types.array.array_size > 0) {
char *str = itoa(elt->types.array.array_size, 0, 10);
assert(str != NULL);
strcat(out, str);
free(str);
}
strcat(out, "] of");
}else if(elt->datatype == DATATYPE_FUNCTION) {
strcat(out, " function");
if(elt->types.function.param_list != NULL) {
strcat(out, " (parameters: ");
for(i = 0; i < elt->types.function.nr_param; ) {
print_ctype(elt->types.function.param_list[i], out);
if(++i < elt->types.function.nr_param)
strcat(out, ", ");
}
strcat(out, " )");
}
strcat(out, " return");
}else if(elt->datatype == DATATYPE_POINTER) {
if(tmp = elt->types.pointer.qua.q_const == CONST) {
strcat(out, " const");
}
if(tmp = elt->types.pointer.qua.q_volatile == VOLATILE) {
strcat(out, " volatile");
}
strcat(out, " pointer to");
} else if(elt->datatype == DATATYPE_OP){
return;
} else {
error("This should not happen!!\n");
}
}
if(tmp = pCType->types.normal.d_attr.qua.q_const == CONST) {
strcat(out, " const");
}
if(tmp = pCType->types.normal.d_attr.qua.q_volatile == VOLATILE) {
strcat(out, " volatile");
}
switch(pCType->types.normal.d_attr.type_spec)
{
case TS_SIGNED:
strcat(out, " signed");
break;
case TS_USIGNED:
strcat(out, " unsigned");
break;
case TS_CHAR:
strcat(out, " char");
break;
case TS_SCHAR:
strcat(out, " signed char");
break;
case TS_UCHAR:
strcat(out, " unsigned char");
break;
case TS_SHORT:
strcat(out, " short");
break;
case TS_SSHORT:
strcat(out, " signed short");