-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlExpressions.cpp
More file actions
440 lines (351 loc) · 13.2 KB
/
ControlExpressions.cpp
File metadata and controls
440 lines (351 loc) · 13.2 KB
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
#include "Expressions.h"
#include "Compiler.h"
#include "Parser.h"
#include "Types/Function.h"
#include "ControlExpressions.h"
using namespace Jet;
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/DIBuilder.h>
#include <llvm/IR/DerivedTypes.h>
CValue DefaultExpression::Compile(CompilerContext* context)
{
context->CurrentToken(&token);
SwitchExpression* sw = dynamic_cast<SwitchExpression*>(this->parent->parent);
if (sw == 0)
context->root->Error("Cannot use default expression outside of a switch!", token);
//create a new block for this case
llvm::BasicBlock* block = sw->def;
//add the case to the switch
bool is_first = sw->first_case;
sw->first_case = false;
//jump to end block
if (!is_first)
context->root->builder.CreateBr(sw->switch_end);
//start using our new block
context->function->f->getBasicBlockList().push_back(block);
context->root->builder.SetInsertPoint(block);
return CValue();
}
CValue CaseExpression::Compile(CompilerContext* context)
{
context->CurrentToken(&token);
SwitchExpression* sw = dynamic_cast<SwitchExpression*>(this->parent->parent);
if (sw == 0)
context->root->Error("Cannot use case expression outside of a switch!", token);
//create a new block for this case
llvm::BasicBlock* block = llvm::BasicBlock::Create(context->context, "case" + value.text);
//add the case to the switch
bool is_first = sw->AddCase(context->root->builder.getInt32(std::stol(this->value.text)), block);
//jump to end block
if (!is_first)
context->root->builder.CreateBr(sw->switch_end);
//start using our new block
context->function->f->getBasicBlockList().push_back(block);
context->root->builder.SetInsertPoint(block);
return CValue();
}
CValue IfExpression::Compile(CompilerContext* context)
{
context->CurrentToken(&token);
int pos = 0;
bool hasElse = this->Else ? this->Else->block->statements.size() > 0 : false;
llvm::BasicBlock *EndBB = llvm::BasicBlock::Create(context->context, "endif");
llvm::BasicBlock *ElseBB = 0;
if (hasElse)
{
ElseBB = llvm::BasicBlock::Create(context->context, "else");
}
llvm::BasicBlock *NextBB = 0;
for (auto& ii : this->branches)
{
if (NextBB)
context->root->builder.SetInsertPoint(NextBB);
auto cond = ii->condition->Compile(context);
cond = context->DoCast(context->root->BoolType, cond);//try and cast to bool
llvm::BasicBlock *ThenBB = llvm::BasicBlock::Create(context->context, "then", context->function->f);
NextBB = pos == (branches.size() - 1) ? (hasElse ? ElseBB : EndBB) : llvm::BasicBlock::Create(context->context, "elseif", context->function->f);
context->root->builder.CreateCondBr(cond.val, ThenBB, NextBB);
//statement body
context->root->builder.SetInsertPoint(ThenBB);
ii->block->Compile(context);
context->root->builder.CreateBr(EndBB);//branch to end
pos++;
}
if (hasElse)
{
context->function->f->getBasicBlockList().push_back(ElseBB);
context->root->builder.SetInsertPoint(ElseBB);
this->Else->block->Compile(context);
context->root->builder.CreateBr(EndBB);
}
context->function->f->getBasicBlockList().push_back(EndBB);
context->root->builder.SetInsertPoint(EndBB);
return CValue();
}
CValue SwitchExpression::Compile(CompilerContext* context)
{
context->CurrentToken(&token);
CValue value = this->var->Compile(context);
if (value.type->type != Types::Int)// todo need to make this work with other integer types
context->root->Error("Argument to Case Statement Must Be an Integer", token);
this->switch_end = llvm::BasicBlock::Create(context->context, "switchend");
//look for all case and default expressions
std::vector < CaseExpression* > cases;
for (auto expr : this->block->statements)
{
auto Case = dynamic_cast<CaseExpression*>(expr);
if (Case)
cases.push_back(Case);
//add default parser and expression
else if (auto def = dynamic_cast<DefaultExpression*>(expr))
{
//do default
if (this->def)
context->root->Error("Multiple defaults defined for the same switch!", token);
this->def = llvm::BasicBlock::Create(context->context, "switchdefault");
}
}
bool no_def = def ? false : true;
if (def == 0)
{
//create default block at end if there isnt one
this->def = llvm::BasicBlock::Create(context->context, "switchdefault");
}
//create the switch instruction
this->sw = context->root->builder.CreateSwitch(value.val, def, cases.size());
//compile the block
this->block->Compile(context);
context->root->builder.CreateBr(this->switch_end);
if (no_def)
{
//insert and create a dummy default
context->function->f->getBasicBlockList().push_back(def);
context->root->builder.SetInsertPoint(def);
context->root->builder.CreateBr(this->switch_end);
}
//start using end
context->function->f->getBasicBlockList().push_back(this->switch_end);
context->root->builder.SetInsertPoint(this->switch_end);
return CValue();
}
Type* CallExpression::TypeCheck(CompilerContext* context)
{
Type* stru = 0;
std::string fname;
if (auto name = dynamic_cast<NameExpression*>(left))
{
//ok handle what to do if im an index expression
fname = name->GetName();
//need to use the template stuff how to get it working with index expressions tho???
}
else if (auto index = dynamic_cast<IndexExpression*>(left))
{
//im a struct yo
fname = index->member.text;
stru = index->GetBaseType(context, true);
//stru->Load(context->root);
//assert(stru->loaded);
//llvm::Value* self = index->GetBaseElementPointer(context).val;
if (index->token.type == TokenType::Pointy)
{
if (stru->type != Types::Pointer && stru->type != Types::Array)
context->root->Error("Cannot dereference type " + stru->ToString(), this->open);
stru = stru->base;
//self = context->root->builder.CreateLoad(self);
}
//push in the this pointer argument kay
//argsv.push_back(CValue(stru->GetPointerType(), self));
}
else
{
throw 7;
/*auto lhs = this->left->Compile(context);
if (lhs.type->type != Types::Function)
context->root->Error("Cannot call non-function", *context->current_token);
std::vector<llvm::Value*> argts;
for (auto ii : *this->args)
{
auto val = ii->Compile(context);
argts.push_back(val.val);
}
return lhs.type->function;*/// CValue(lhs.type->function->return_type, context->root->builder.CreateCall(lhs.val, argts));
}
std::vector<Type*> arg;
if (stru)
arg.push_back(stru->GetPointerType());
for (auto ii : *args)
arg.push_back(ii.first->TypeCheck(context));
auto fun = context->GetMethod(fname, arg, stru);
if (fun == 0)
{
//check variables
context->CurrentToken(&this->open);
auto var = context->TCGetVariable(fname);
if (var->type == Types::Pointer && var->base->type == Types::Struct && var->base->data->template_base && var->base->data->template_base->name == "function")
return var->base->data->members.find("T")->second.ty->function->return_type;
else if (var->base->type == Types::Function)
return var->base->function->return_type;
context->root->Error("Cannot call method '" + fname + "'", this->open);
}
if (fun->arguments.size() == arg.size() + 1)
{
//its a constructor or something
return fun->arguments[0].first->base;
}
//keep working on this, dont forget constructors
//auto left = this->left->TypeCheck(context);
//todo: check args
//throw 7;
return fun->return_type;
}
CValue YieldExpression::Compile(CompilerContext* context)
{
//first make sure we are in a generator...
if (context->function->is_generator == false)
context->root->Error("Cannot use yield outside of a generator!", this->token);
//create a new block for after the yield
auto bb = llvm::BasicBlock::Create(context->context, "yield");
context->function->f->getBasicBlockList().push_back(bb);
//add the new block to the generator's indirect branch list
context->function->generator.ibr->addDestination(bb);
//store the current location into the generator context so we can jump back
auto data = context->Load("_context");
auto br = context->root->builder.CreateGEP(data.val, { context->root->builder.getInt32(0), context->root->builder.getInt32(0) });
auto ba = llvm::BlockAddress::get(bb);
context->root->builder.CreateStore(ba, br);
if (this->right)
{
//compile the yielded value
auto value = right->Compile(context);
//store result into the generator context
value = context->DoCast(data.type->base->data->struct_members[1].type, value);//cast to the correct type
br = context->root->builder.CreateGEP(data.val, { context->root->builder.getInt32(0), context->root->builder.getInt32(1) });
context->root->builder.CreateStore(value.val, br);
}
//return 1 to say the function isnt done yet, we havent returned, just yielded
context->root->builder.CreateRet(context->root->builder.getInt1(true));
//start inserting in new block
context->root->builder.SetInsertPoint(bb);
return CValue();
}
CValue MatchExpression::Compile(CompilerContext* context)
{
CValue val;//first get pointer to union
auto i = dynamic_cast<NameExpression*>(var);
auto p = dynamic_cast<IndexExpression*>(var);
if (i)
val = context->GetVariable(i->GetName());
else if (p)
val = p->GetElementPointer(context);
if (val.type->base->type != Types::Union)
context->root->Error("Cannot match with a non-union", token);
auto endbb = llvm::BasicBlock::Create(context->context, "match.end");
//from val get the type
auto key = context->root->builder.CreateGEP(val.val, { context->root->builder.getInt32(0), context->root->builder.getInt32(0) });
auto sw = context->root->builder.CreateSwitch(context->root->builder.CreateLoad(key), endbb, this->cases.size());
for (auto ii : this->cases)
{
context->PushScope();
if (ii.type.type == TokenType::Default)
{
//add bb for case
auto bb = llvm::BasicBlock::Create(context->context, "match.case", context->function->f);
context->root->builder.SetInsertPoint(bb);
sw->setDefaultDest(bb);
//build internal
ii.block->Compile(context);
//branch to end
context->root->builder.CreateBr(endbb);
break;
}
unsigned int pi = 0;//find what index it is
for (auto mem : val.type->base->_union->members)
{
if (mem->name == ii.type.text)
break;
pi++;
}
if (pi >= val.type->base->_union->members.size())
context->root->Error("Type '" + ii.type.text + "' not in union, cannot match to it.", ii.type);
//add bb for case
auto bb = llvm::BasicBlock::Create(context->context, "match.case", context->function->f);
context->root->builder.SetInsertPoint(bb);
auto i = llvm::ConstantInt::get(context->context, llvm::APInt(32, (uint64_t)pi));
sw->addCase(i, bb);
//add local
auto ptr = context->root->builder.CreateGEP(val.val, { context->root->builder.getInt32(0), context->root->builder.getInt32(1) });
ptr = context->root->builder.CreatePointerCast(ptr, val.type->base->_union->members[pi]->GetPointerType()->GetLLVMType());
context->RegisterLocal(ii.name.text, CValue(val.type->base->_union->members[pi]->GetPointerType(), ptr));
//build internal
ii.block->Compile(context);
//need to do this without destructing args
context->scope->named_values[ii.name.text] = CValue();
context->PopScope();
//branch to end
context->root->builder.CreateBr(endbb);
}
//start new basic block
context->function->f->getBasicBlockList().push_back(endbb);
context->root->builder.SetInsertPoint(endbb);
return CValue();
}
CValue CallExpression::Compile(CompilerContext* context)
{
context->CurrentToken(&this->open);
context->SetDebugLocation(this->open);
std::vector<CValue> argsv;
std::string fname;
Type* stru = 0;
bool devirtualize = false;
if (auto name = dynamic_cast<NameExpression*>(left))
{
//ok handle what to do if im an index expression
fname = name->GetName();
//need to use the template stuff how to get it working with index expressions tho???
}
else if (auto index = dynamic_cast<IndexExpression*>(left))
{
//im a struct yo
fname = index->member.text;
stru = index->GetBaseType(context);
assert(stru->loaded);
llvm::Value* self = index->GetBaseElementPointer(context).val;
if (index->token.type == TokenType::Pointy)
{
if (stru->type != Types::Pointer && stru->type != Types::Array)
context->root->Error("Cannot dereference type " + stru->ToString(), this->open);
stru = stru->base;
self = context->root->builder.CreateLoad(self);
}
else
{
devirtualize = true;//if we arent using -> we dont need a virtual call, right?
}
//push in the this pointer argument kay
argsv.push_back(CValue(stru->GetPointerType(), self));
}
else
{
//calling a function pointer type
auto lhs = this->left->Compile(context);
if (lhs.type->type != Types::Function)
context->root->Error("Cannot call non-function", *context->current_token);
std::vector<llvm::Value*> argts;
for (auto ii : *this->args)
argts.push_back(ii.first->Compile(context).val);
return CValue(lhs.type->function->return_type, context->root->builder.CreateCall(lhs.val, argts));
}
//build arg list
for (auto ii : *this->args)
argsv.push_back(ii.first->Compile(context));
context->CurrentToken(&this->open);
auto ret = context->Call(fname, argsv, stru, devirtualize);
//destruct if my parent doesnt use me and todo if I have a destructor
if (ret.type->type == Types::Struct && dynamic_cast<BlockExpression*>(this->parent))
{
context->Destruct(CValue(ret.type->GetPointerType(), ret.pointer), 0);
}
return ret;
}