-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.hxx
428 lines (385 loc) · 16.5 KB
/
instruction.hxx
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
#ifndef instruction_hxx
#define instruction_hxx
#include <iostream>
#define debug_msg(x) //std::cout << (x) << std::endl;
#include <memory>
#include <string>
#include "llvm/DerivedTypes.h"
#include "asserts.hxx"
#include "state.hxx"
#include "APInt.hxx"
#include "DataFlowNode.hxx"
class instruction {
public:
virtual ~instruction() = default;
virtual void Codegen(std::shared_ptr<codegenState> state) = 0;
virtual std::string name() = 0;
};
class stackInstruction : public instruction {
};
class arithmeticInstruction : public instruction {
};
class heapInstruction : public instruction {
};
class controlInstruction : public instruction {
};
class ioInstruction : public instruction {
};
class PushInstruction : public stackInstruction {
private:
APInt Num;
public:
PushInstruction(APInt value): Num(value){debug_msg("constructed Pushinstruction");}
~PushInstruction() {debug_msg("deconstructed Pushinstruction");}
virtual void Codegen(std::shared_ptr<codegenState> state){
VALGRIND_CHECK_VALUE_IS_DEFINED(state);
llvm::Value* serializednum = Num.Serialize(state);
VALGRIND_CHECK_VALUE_IS_DEFINED(serializednum);
DataSourceNode::createPushNode(state,serializednum);
}
virtual std::string name () {return "Push";};
};
class DupInstruction : public stackInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* value = DataConsumerNode::createPopNode(state);
DataSourceNode::createPushNode(state,value);
DataSourceNode::createPushNode(state,value);
/*llvm::Function* PopInstr = state->TheModule->getFunction("PopInstr");
llvm::Value* value = state->Builder.CreateCall(PopInstr,"dupval");
llvm::Function* PushInstr = state->TheModule->getFunction("PushInstr");
state->Builder.CreateCall(PushInstr, value);
state->Builder.CreateCall(PushInstr, value);*/
}
virtual std::string name () {return "Dup";};
};
class DiscardInstruction : public stackInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
DataConsumerNode::createPopNode(state);
}
virtual std::string name () {return "Discard";};
};
class SwapInstruction : public stackInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* value1 = DataConsumerNode::createPopNode(state);
llvm::Value* value2 = DataConsumerNode::createPopNode(state);
DataSourceNode::createPushNode(state,value1);
DataSourceNode::createPushNode(state,value2);
}
virtual std::string name () {return "Swap";};
};
class CopyInstruction : public stackInstruction {
private:
APInt value;
public:
CopyInstruction(APInt val) : value(val){}
virtual void Codegen(std::shared_ptr<codegenState> state){
std::vector<llvm::Value*> nodes;
for(APIntBuilder iter; iter < this->value; iter.addOne()) {
nodes.push_back(DataConsumerNode::createPopNode(state));
}
llvm::Value* tocopy = DataConsumerNode::createPopNode(state);
DataSourceNode::createPushNode(state,tocopy);
for(llvm::Value* node: nodes) {
DataSourceNode::createPushNode(state,node);
}
DataSourceNode::createPushNode(state,tocopy);
}
virtual std::string name () {return "Copy";};
};
class SlideInstruction : public stackInstruction {
private:
APInt value;
public:
SlideInstruction(APInt val) : value(val){}
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* top = DataConsumerNode::createPopNode(state);
for(APIntBuilder iter; iter < this->value; iter.addOne()) {
DataConsumerNode::createPopNode(state);
}
DataSourceNode::createPushNode(state,top);
}
virtual std::string name () {return "Slide";};
};
class PlusInstruction : public arithmeticInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
/*llvm::Function* PopInstr = state->TheModule->getFunction("PopInstr");
llvm::Value* value1 = state->Builder.CreateCall(PopInstr,"addval");
llvm::Value* value2 = state->Builder.CreateCall(PopInstr,"addval");*/
llvm::Value* value1 = DataConsumerNode::createPopNode(state);
llvm::Value* value2 = DataConsumerNode::createPopNode(state);
llvm::Function* AddInstr = state->TheModule->getFunction("AddInstr");
llvm::Value* result = state->Builder.CreateCall2(AddInstr,value1,value2,"addresult");
DataSourceNode::createPushNode(state,result);
/*llvm::Function* PushInstr = state->TheModule->getFunction("PushInstr");
state->Builder.CreateCall(PushInstr, result);*/
}
virtual std::string name () {return "Plus";};
};
class MinusInstruction : public arithmeticInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
//top value is rhs
llvm::Value* value2 = DataConsumerNode::createPopNode(state);
llvm::Value* value1 = DataConsumerNode::createPopNode(state);
llvm::Function* MinusInstr = state->TheModule->getFunction("MinusInstr");
llvm::Value* result = state->Builder.CreateCall2(MinusInstr,value1,value2,"Minusresult");
DataSourceNode::createPushNode(state,result);
}
virtual std::string name () {return "Minus";};
};
class TimesInstruction : public arithmeticInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* value1 = DataConsumerNode::createPopNode(state);
llvm::Value* value2 = DataConsumerNode::createPopNode(state);
llvm::Function* TimesInstr = state->TheModule->getFunction("TimesInstr");
llvm::Value* result = state->Builder.CreateCall2(TimesInstr,value1,value2,"Timesresult");
DataSourceNode::createPushNode(state,result);
}
virtual std::string name () {return "Times";};
};
class DivideInstruction : public arithmeticInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
abort();
}
virtual std::string name () {return "Divide";};
};
class ModuloInstruction : public arithmeticInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
abort();
}
virtual std::string name () {return "Modulo";};
};
class StoreInstruction : public heapInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* value = DataConsumerNode::createPopNode(state);
llvm::Value* address = DataConsumerNode::createPopNode(state);
assertdefined(value)
assertdefined(address)
DataSourceNode::createMemStore(state,address,value);
}
virtual std::string name () {return "Store";};
};
class RetriveInstruction : public heapInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* address = DataConsumerNode::createPopNode(state);
llvm::Value* value = DataConsumerNode::createMemRetrive(state,address);
DataSourceNode::createPushNode(state,value);
}
virtual std::string name () {return "Retrive";};
};
//uses reverse tail call optemiztion for jumps
//tail call optimization is when you convert a call followed by a return in to a jump
//in reverse tail call a jump is represented by a call followed by a return
//this allows the labels to use a common namespace for jumps and calls and use
//predecareation for backwards jumps
inline llvm::Function* MakeFunction(APInt label,std::shared_ptr<codegenState> state){
llvm::FunctionType *FT = llvm::FunctionType::get(llvm::Type::getInt32Ty(llvm::getGlobalContext()), false);
llvm::Function *Function = llvm::Function::Create(FT, llvm::Function::ExternalLinkage, (std::string)label, state->TheModule);
//Fastcall so tailcail optermization can be applied
Function->setCallingConv(llvm::CallingConv::ID::Fast);
// If F conflicted, there was already something named 'Name'. If it has a
// body, don't allow redefinition or reextern.
if (Function->getName() != (std::string)label) {
// Delete the one we just made and get the existing one.
Function->eraseFromParent();
Function = state->TheModule->getFunction((std::string)label);
// If F already has a body, reject this.
if (!Function->empty()) {
std::cerr << "warning: redefinition of label " << (std::string)label << std::endl;
return nullptr;
}
}
return Function;
}
class LabelInstruction : public controlInstruction {
private:
APInt label;
public:
LabelInstruction(APInt value) : label(value) {}
virtual void Codegen(std::shared_ptr<codegenState> state){
/*llvm::BasicBlock* currentblock = state->Builder.GetInsertBlock();
llvm::Function *TheFunction = currentblock->getParent();*/
DataConsumerNode::SaveStack(state);
llvm::Function* Function = MakeFunction(label,state);
//ignore redefs
if (Function) {
llvm::BasicBlock *functionstartBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", Function);
llvm::Value* success = state->Builder.CreateCall(Function,"callsuccess");
state->Builder.CreateRet(success);
state->Builder.SetInsertPoint(functionstartBB);
}
}
virtual std::string name () {return ((std::string)"Label").append(label);};
};
class JumpInstruction : public controlInstruction {
private:
APInt label;
public:
JumpInstruction(APInt value) : label(value) {}
std::string getLabelAsString() {return label;}
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Function* placetojump = state->TheModule->getFunction((std::string)label);
if (!placetojump)
placetojump = MakeFunction(label,state);
DataConsumerNode::SaveStack(state);
llvm::Value* success = state->Builder.CreateCall(placetojump,"callsuccess");
state->Builder.CreateRet(success);
llvm::BasicBlock* currentblock = state->Builder.GetInsertBlock();
llvm::Function *TheFunction = currentblock->getParent();
llvm::BasicBlock *continueBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterjump", TheFunction);
state->Builder.SetInsertPoint(continueBB);
}
virtual std::string name () {return ((std::string)"Jump").append(this->getLabelAsString());};
};
//TODO:test return before call
class CallInstruction : public controlInstruction {
private:
APInt label;
public:
CallInstruction(APInt value) : label (value){}
virtual void Codegen(std::shared_ptr<codegenState> state){
DataConsumerNode::SaveStack(state);
llvm::Function* placetojump = state->TheModule->getFunction((std::string)label);
if (!placetojump)
placetojump = MakeFunction(label,state);
llvm::Value* successcode = state->Builder.CreateCall(placetojump,"callsuccess");
llvm::Value* success = state->Builder.CreateICmpEQ(successcode,llvm::ConstantInt::get(llvm::Type::getInt32Ty(llvm::getGlobalContext()),0),"callsuccessbool");
//return if failed
llvm::BasicBlock* currentblock = state->Builder.GetInsertBlock();
llvm::Function *TheFunction = currentblock->getParent();
llvm::BasicBlock *retBlock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "callblock", TheFunction);
llvm::BasicBlock *continueBlock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "aftercalljump");
state->Builder.CreateCondBr(success,continueBlock,retBlock);
state->Builder.SetInsertPoint(retBlock);
state->Builder.CreateRet(successcode);
TheFunction->getBasicBlockList().push_back(continueBlock);
state->Builder.SetInsertPoint(continueBlock);
}
virtual std::string name () {return ((std::string)"Call").append(label);};
};
class IfZeroInstruction : public controlInstruction {
private:
APInt label;
public:
IfZeroInstruction(APInt value) : label(value){}
virtual void Codegen(std::shared_ptr<codegenState> state){
//get value
llvm::Value* value = DataConsumerNode::createPopNode(state);
//preform comparason
llvm::StructType* APIntType = state->TheModule->getTypeByName("JITAPInt");
llvm::Constant* child = llvm::ConstantPointerNull::get(APIntType->getPointerTo());
llvm::Constant* zerostructvalue = llvm::ConstantInt::get(llvm::Type::getInt64Ty(llvm::getGlobalContext()),0,true);
llvm::Constant* zerostruct = llvm::ConstantStruct::get(APIntType,zerostructvalue,child, nullptr);
llvm::Function* CmpZeroInstr = state->TheModule->getFunction("CmpEqualInstr");
llvm::Value* cond = state->Builder.CreateCall2(CmpZeroInstr,value,zerostruct,"cmp");
//do jump
llvm::BasicBlock* currentblock = state->Builder.GetInsertBlock();
llvm::Function *TheFunction = currentblock->getParent();
llvm::BasicBlock *callBlock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "callblock", TheFunction);
llvm::BasicBlock *elseBlock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "aftercondjump");
state->Builder.CreateCondBr(cond,callBlock,elseBlock);
state->Builder.SetInsertPoint(callBlock);
DataConsumerNode::SaveStack(state,false);
llvm::Function* placetojump = state->TheModule->getFunction((std::string)label);
if (!placetojump)
placetojump = MakeFunction(label,state);
llvm::Value* success = state->Builder.CreateCall(placetojump,"callsuccess");
state->Builder.CreateRet(success);
TheFunction->getBasicBlockList().push_back(elseBlock);
state->Builder.SetInsertPoint(elseBlock);
}
virtual std::string name () {return "Jump If Zero";};
};
class IfNegInstruction : public controlInstruction {
public:
IfNegInstruction(APInt value){
abort();
}
virtual void Codegen(std::shared_ptr<codegenState> state){}
virtual std::string name () {return "Jump If Negative";};
};
class ReturnInstruction : public controlInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
DataConsumerNode::SaveStack(state);
state->Builder.CreateRet(llvm::ConstantInt::get(llvm::Type::getInt32Ty(llvm::getGlobalContext()),0));
llvm::BasicBlock* currentblock = state->Builder.GetInsertBlock();
llvm::Function *TheFunction = currentblock->getParent();
llvm::BasicBlock *continueBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterreturn", TheFunction);
state->Builder.SetInsertPoint(continueBB);
}
virtual std::string name () {return "Return";};
};
class EndInstruction : public controlInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::BasicBlock* currentblock = state->Builder.GetInsertBlock();
llvm::Function *TheFunction = currentblock->getParent();
//put apInt as label once output sorted
llvm::BasicBlock *continueBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterend", TheFunction);
state->Builder.CreateRet(llvm::ConstantInt::get(llvm::Type::getInt32Ty(llvm::getGlobalContext()),0));
if (!state->stack.empty()) {
DataConsumerNode::SaveStack(state);
std::cerr << "program cannot end normally" << std::endl;
abort();
}
state->Builder.SetInsertPoint(continueBB);
}
virtual std::string name () {return "End";};
};
class OutputCharInstruction : public ioInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
/*llvm::Function* PopInstr = state->TheModule->getFunction("PopInstr");
llvm::Value* ch = state->Builder.CreateCall(PopInstr,"outputchar");*/
llvm::Value* ch = DataConsumerNode::createPopNode(state);
llvm::Function* OutputCharInstr = state->TheModule->getFunction("OutputCharInstr");
state->Builder.CreateCall(OutputCharInstr, ch);
}
virtual std::string name () {return "Output Char";};
};
class OutputNumInstruction : public ioInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
/*llvm::Function* PopInstr = state->TheModule->getFunction("PopInstr");
llvm::Value* num = state->Builder.CreateCall(PopInstr,"outputnum");*/
llvm::Value* num = DataConsumerNode::createPopNode(state);
llvm::Function* OutputNumInstr = state->TheModule->getFunction("OutputNumInstr");
state->Builder.CreateCall(OutputNumInstr, num);
}
virtual std::string name () {return "Output Num";};
};
class ReadCharInstruction : public ioInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* address = DataConsumerNode::createPopNode(state);
llvm::Function* ReadNumInstr = state->TheModule->getFunction("ReadCharInstr");
llvm::Value* ch = state->Builder.CreateCall(ReadNumInstr,"charread");
DataSourceNode::createMemStore(state,address,ch);
/*llvm::Function* PushInstr = state->TheModule->getFunction("PushInstr");
state->Builder.CreateCall(PushInstr, ch);*/
}
virtual std::string name () {return "Read Char";};
};
class ReadNumInstruction : public ioInstruction {
public:
virtual void Codegen(std::shared_ptr<codegenState> state){
llvm::Value* address = DataConsumerNode::createPopNode(state);
llvm::Function* ReadNumInstr = state->TheModule->getFunction("ReadNumInstr");
llvm::Value* num = state->Builder.CreateCall(ReadNumInstr,"numread");
DataSourceNode::createMemStore(state,address,num);
/*llvm::Function* PushInstr = state->TheModule->getFunction("PushInstr");
state->Builder.CreateCall(PushInstr, num);*/
}
virtual std::string name () {return "Read Num";};
};
#endif