-
Notifications
You must be signed in to change notification settings - Fork 0
/
trailer.y
200 lines (156 loc) · 3.96 KB
/
trailer.y
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
%%
int yyerror(char *x) {
printf("Illegal C program\nERROR:%s",x);
}
int main(int argc, char *argv[]) {
if(argc == 2) {
if(!strcmp("-tree", argv[1])) {
showtree = 1;
}
}
printf("\nEnter a C Code :\n\n");
yyparse();
//system("mv "__rodata__" test.s");
//system("cat "__textdata__" >> test.s");
//system("rm "__textdata__);
}
struct Node *create_scon(char *val) {
struct Node *n;
if((n = (struct Node*)malloc(sizeof(struct Node))) == NULL) {
printf("malloc fail\n");
exit(0);
}
n->type = typeScon;
n->con.strVal = val;
return n;
}
struct Node *create_con(long long int val) {
struct Node *n;
if((n = (struct Node*)malloc(sizeof(struct Node))) == NULL) {
printf("malloc fail\n");
exit(0);
}
n->type = typeCon;
n->con.val = val;
return n;
}
#include "symtab.h"
char *new_cstr(char *str) {
char *n = (char*)malloc(sizeof(char) * (strlen(str)+1));
strcpy(n, str);
return n;
}
void set_type(struct Node *n, int type) {
}
struct Node *create_var(char *name, int type, void *extra) {
struct Node *n;
if((n = sym_lookup(name))) {
return n;
}
if((n = (struct Node*)malloc(sizeof(struct Node))) == NULL) {
printf("malloc failed\n");
exit(0);
}
n->type = typeVar;
n->var.type = typeVar;
n->var.name = new_cstr(name);
n->var.extra = extra;
n->hits = 0;
n->reg = ANYREG;
n->sfof = stack_pointer;
stack_pointer += 4; /* assume 4 byte size */
insert_symtab(n);
return n;
}
struct Node *create_oper(int type, int nops, ...) {
struct Node *n;
int i;
va_list ap;
if((n = (struct Node*)malloc(sizeof(struct Node))) == NULL) {
printf("malloc failed\n");
exit(0);
}
n->type = typeOp;
n->oper.op = type;
n->oper.nops = nops;
n->oper.operands = malloc(sizeof(struct Node*) * nops);
va_start(ap, nops);
for (i = 0; i < nops; i++) {
n->oper.operands[i] = (void*)va_arg(ap, struct Node*);
}
va_end(ap);
return n;
}
int depth, _pline_buffer_init__=0;
void print_tree(struct Node *, int);
char _pline_buffer__[512];
void display_tree(struct Node *root) {
int i;
depth = 0;
if(_pline_buffer_init__==0) {
for(i=0; i<512; i++) {
_pline_buffer__[i] = ' ';
}
_pline_buffer_init__ = 1;
}
print_tree(root, 1);
printf("\n\n");
}
#ifndef LINEHEIGHT
#define LINEHEIGHT 1
#endif
void print_tree(struct Node* root, int last)
{
if(depth == 0) {
printf("\n\n");
}
int tab, i;
for(i=0; i<LINEHEIGHT; i++) {
for( tab=0; tab<depth; tab++ ) {
printf("%c ", _pline_buffer__[tab]);
}
if(i == 0) {
printf("|\n");
}
}
printf("%c-----", last?'+':'|');
if(!root) {
puts("(nil.)");
return;
}
++depth;
_pline_buffer__[depth] = '|';
if( root->type == typeCon ) {
printf("[constant { value : %lld } ]\n", root->con.val);
}
if( root->type == typeOp ) {
printf("[node: %s ]\n", NODETYPES[root->oper.op]);
for(i=root->oper.nops-1; i > -1; i--) {
if(i == 0) {
_pline_buffer__[depth] = ' ';
}
print_tree((struct Node*)root->oper.operands[i], !i);
}
}
if( root->type == typeVar ) {
printf("[variable { name : %s, symoff : %d} ]\n", root->var.name, root->var.symoff);
}
if( root->type == typeScon ) {
printf("[string-literal {value : %s} ]\n", root->con.strVal);
}
--depth;
}
void add_child(struct Node *parent, struct Node *child) {
void **newn;
int i;
if(parent->type != typeOp) return;
newn = malloc(sizeof(struct Node*) * (parent->oper.nops+1));
if(!newn) return;
for(i=0; i<parent->oper.nops; i++) {
newn[i] = parent->oper.operands[i];
}
newn[i] = child;
parent->oper.nops++;
free(parent->oper.operands);
parent->oper.operands = newn;
}