forked from blitz-research/blitz3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeclnode.cpp
280 lines (239 loc) · 7.18 KB
/
declnode.cpp
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
#include "std.h"
#include "nodes.h"
//////////////////////////////
// Sequence of declarations //
//////////////////////////////
void DeclSeqNode::proto( DeclSeq *d,Environ *e ){
for( int k=0;k<decls.size();++k ){
try{ decls[k]->proto( d,e ); }
catch( Ex &x ){
if( x.pos<0 ) x.pos=decls[k]->pos;
if(!x.file.size() ) x.file=decls[k]->file;
throw;
}
}
}
void DeclSeqNode::semant( Environ *e ){
for( int k=0;k<decls.size();++k ){
try{ decls[k]->semant( e ); }
catch( Ex &x ){
if( x.pos<0 ) x.pos=decls[k]->pos;
if(!x.file.size() ) x.file=decls[k]->file;
throw;
}
}
}
void DeclSeqNode::translate( Codegen *g ){
for( int k=0;k<decls.size();++k ){
try{ decls[k]->translate( g ); }
catch( Ex &x ){
if( x.pos<0 ) x.pos=decls[k]->pos;
if(!x.file.size() ) x.file=decls[k]->file;
throw;
}
}
}
void DeclSeqNode::transdata( Codegen *g ){
for( int k=0;k<decls.size();++k ){
try{ decls[k]->transdata( g ); }
catch( Ex &x ){
if( x.pos<0 ) x.pos=decls[k]->pos;
if(!x.file.size() ) x.file=decls[k]->file;
throw;
}
}
}
////////////////////////////
// Simple var declaration //
////////////////////////////
void VarDeclNode::proto( DeclSeq *d,Environ *e ){
Type *ty=tagType( tag,e );
if( !ty ) ty=Type::int_type;
ConstType *defType=0;
if( expr ){
expr=expr->semant( e );
expr=expr->castTo( ty,e );
if( constant || (kind&DECL_PARAM) ){
ConstNode *c=expr->constNode();
if( !c ) ex( "Expression must be constant" );
if( ty==Type::int_type ) ty=d_new ConstType( c->intValue() );
else if( ty==Type::float_type ) ty=d_new ConstType( c->floatValue() );
else ty=d_new ConstType( c->stringValue() );
e->types.push_back( ty );
delete expr;expr=0;
}
if( kind&DECL_PARAM ){
defType=ty->constType();ty=defType->valueType;
}
}else if( constant ) ex( "Constants must be initialized" );
Decl *decl=d->insertDecl( ident,ty,kind,defType );
if( !decl ) ex( "Duplicate variable name" );
if( expr ) sem_var=d_new DeclVarNode( decl );
}
void VarDeclNode::semant( Environ *e ){
}
void VarDeclNode::translate( Codegen *g ){
if( kind & DECL_GLOBAL ){
g->align_data( 4 );
g->i_data( 0,"_v"+ident );
}
if( expr ) g->code( sem_var->store( g,expr->translate( g ) ) );
}
//////////////////////////
// Function Declaration //
//////////////////////////
void FuncDeclNode::proto( DeclSeq *d,Environ *e ){
Type *t=tagType( tag,e );if( !t ) t=Type::int_type;
a_ptr<DeclSeq> decls( d_new DeclSeq() );
params->proto( decls,e );
sem_type=d_new FuncType( t,decls.release(),false,false );
if( !d->insertDecl( ident,sem_type,DECL_FUNC ) ){
delete sem_type;ex( "duplicate identifier" );
}
e->types.push_back( sem_type );
}
void FuncDeclNode::semant( Environ *e ){
sem_env=d_new Environ( genLabel(),sem_type->returnType,1,e );
DeclSeq *decls=sem_env->decls;
int k;
for( k=0;k<sem_type->params->size();++k ){
Decl *d=sem_type->params->decls[k];
if( !decls->insertDecl( d->name,d->type,d->kind ) ) ex( "duplicate identifier" );
}
stmts->semant( sem_env );
}
void FuncDeclNode::translate( Codegen *g ){
//var offsets
int size=enumVars( sem_env );
//enter function
g->enter( "_f"+ident,size );
//initialize locals
TNode *t=createVars( sem_env );
if( t ) g->code( t );
if( g->debug ){
string t=genLabel();
g->s_data( ident,t );
g->code( call( "__bbDebugEnter",local(0),iconst((int)sem_env),global(t) ) );
}
//translate statements
stmts->translate( g );
for( int k=0;k<sem_env->labels.size();++k ){
if( sem_env->labels[k]->def<0 ) ex( "Undefined label",sem_env->labels[k]->ref );
}
//leave the function
g->label( sem_env->funcLabel+"_leave" );
t=deleteVars( sem_env );
if( g->debug ) t=d_new TNode( IR_SEQ,call( "__bbDebugLeave" ),t );
g->leave( t,sem_type->params->size()*4 );
}
//////////////////////
// Type Declaration //
//////////////////////
void StructDeclNode::proto( DeclSeq *d,Environ *e ){
sem_type=d_new StructType( ident,d_new DeclSeq() );
if( !d->insertDecl( ident,sem_type,DECL_STRUCT ) ){
delete sem_type;ex( "Duplicate identifier" );
}
e->types.push_back( sem_type );
}
void StructDeclNode::semant( Environ *e ){
fields->proto( sem_type->fields,e );
for( int k=0;k<sem_type->fields->size();++k ) sem_type->fields->decls[k]->offset=k*4;
}
void StructDeclNode::translate( Codegen *g ){
//translate fields
fields->translate( g );
//type ID
g->align_data( 4 );
g->i_data( 5,"_t"+ident );
//used and free lists for type
int k;
for( k=0;k<2;++k ){
string lab=genLabel();
g->i_data( 0,lab ); //fields
g->p_data( lab ); //next
g->p_data( lab ); //prev
g->i_data( 0 ); //type
g->i_data( -1 ); //ref_cnt
}
//number of fields
g->i_data( sem_type->fields->size() );
//type of each field
for( k=0;k<sem_type->fields->size();++k ){
Decl *field=sem_type->fields->decls[k];
Type *type=field->type;
string t;
if( type==Type::int_type ) t="__bbIntType";
else if( type==Type::float_type ) t="__bbFltType";
else if( type==Type::string_type ) t="__bbStrType";
else if( StructType *s=type->structType() ) t="_t"+s->ident;
else if( VectorType *v=type->vectorType() ) t=v->label;
g->p_data( t );
}
}
//////////////////////
// Data declaration //
//////////////////////
void DataDeclNode::proto( DeclSeq *d,Environ *e ){
expr=expr->semant( e );
ConstNode *c=expr->constNode();
if( !c ) ex( "Data expression must be constant" );
if( expr->sem_type==Type::string_type ) str_label=genLabel();
}
void DataDeclNode::semant( Environ *e ){
}
void DataDeclNode::translate( Codegen *g ){
if( expr->sem_type!=Type::string_type ) return;
ConstNode *c=expr->constNode();
g->s_data( c->stringValue(),str_label );
}
void DataDeclNode::transdata( Codegen *g ){
ConstNode *c=expr->constNode();
if( expr->sem_type==Type::int_type ){
g->i_data( 1 );g->i_data( c->intValue() );
}else if( expr->sem_type==Type::float_type ){
float n=c->floatValue();
g->i_data( 2 );g->i_data( *(int*)&n );
}else{
g->i_data( 4 );g->p_data( str_label );
}
}
////////////////////////
// Vector declaration //
////////////////////////
void VectorDeclNode::proto( DeclSeq *d,Environ *env ){
Type *ty=tagType( tag,env );if( !ty ) ty=Type::int_type;
vector<int> sizes;
for( int k=0;k<exprs->size();++k ){
ExprNode *e=exprs->exprs[k]=exprs->exprs[k]->semant( env );
ConstNode *c=e->constNode();
if( !c ) ex( "Blitz array sizes must be constant" );
int n=c->intValue();
if( n<0 ) ex( "Blitz array sizes must not be negative" );
sizes.push_back( n+1 );
}
string label=genLabel();
sem_type=d_new VectorType( label,ty,sizes );
if( !d->insertDecl( ident,sem_type,kind ) ){
delete sem_type;ex( "Duplicate identifier" );
}
env->types.push_back( sem_type );
}
void VectorDeclNode::translate( Codegen *g ){
//type tag!
g->align_data( 4 );
VectorType *v=sem_type->vectorType();
g->i_data( 6,v->label );
int sz=1;
for( int k=0;k<v->sizes.size();++k ) sz*=v->sizes[k];
g->i_data( sz );
string t;
Type *type=v->elementType;
if( type==Type::int_type ) t="__bbIntType";
else if( type==Type::float_type ) t="__bbFltType";
else if( type==Type::string_type ) t="__bbStrType";
else if( StructType *s=type->structType() ) t="_t"+s->ident;
else if( VectorType *v=type->vectorType() ) t=v->label;
g->p_data( t );
if( kind==DECL_GLOBAL ) g->i_data( 0,"_v"+ident );
}