-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ir-generator.spice
75 lines (63 loc) · 3.04 KB
/
ir-generator.spice
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
// Std imports
import "std/data/vector";
import "std/data/stack";
// Own imports
import "bindings/llvm/llvm" as llvm;
type CommonLLVMTypes struct {
llvm::StructType fatPtrType
}
public type IRGenerator struct/* : ICompilerPass*/ {
llvm::Context& context
llvm::Builder& builder
llvm::Module* module
//OpRuleConversionManager conversionManager
//StdFunctionManager stdFunctionManager
//DebugInfoGenerator diGenerator
CommonLLVMTypes llvmTypes
Vector<llvm::BasicBlock> breakBlocks
Vector<llvm::BasicBlock> continueBlocks
Stack<llvm::BasicBlock> fallthroughBlocks
llvm::BasicBlock* allocaInsertBlock = nil<llvm::BasicBlock*>
llvm::Instruction* allocaInsertInst = nil<llvm::Instruction*>
bool blockAlreadyTerminated = false
//Vector<DeferredLogic> deferredVTableInitializations
}
public p IRGenerator.ctor(GlobalResourceManager& resourceManager, SourceFile* sourceFile) {
this.context = resourceManager.context;
this.builder = resourceManager.builder;
this.module = sourceFile.module;
this.stdFunctionManager = StdFunctionManager(resourceManager, sourceFile.llvmModule);
// Attach information to the module
this.module.setTargetTriple(this.cliOptions.targetTriple);
this.module.setDataLayout(resourceManager.targetMachine.createDataLayout());
// Initialiize debug info generator
if this.cliOptions.generateDebugInfo {
this.diGenerator.initialize(sourceFile.fileName, sourceFile.fileDir);
}
}
public f<llvm::Value> IRGenerator.createAlloca(llvm::Type llvmType, String varName) {
if !this.cliOptions.namesForIRValues { varName.clear(); }
if this.allocaInsertInst != nil<llvm::Instruction*> { // If there is already an alloca inst, insert right after that
llvm::AllocaInst allocaInst = this.builder.createAlloca(llvmType, llvm::Value(), varName);
allocaInst.setDebugLoc(llvm::DebugLoc());
allocaInstrInst = allocaInst;
} else { // This is the first alloca inst in the current function -> insert at the entry block
// Save current basic block and move insert cursor to entry block of the current function
llvm::BasicBlock currentBlock = this.builder.getInsertBlock();
this.builder.setInsertPoint(this.allocaInsertBlock);
// Allocate the size of the given LLVM type
this.allocaInsertInst = this.builder.createAlloca(llvmType, llvm::Value(), varName);
this.allocaInsertInst.setDebugLoc(llvm::DebugLoc());
// Resotre old basic block
this.builder.setInsertPoint(currentBlock);
}
return (llvm::Value) allocaInsertInst;
}
public f<llvm::Value> IRGenerator.insertLoad(llvm::Type llvmType, llvm::Value ptr, bool isVolatile, const String& varName = "") {
assert ptr.getType().isPointerTy();
return this.builder.createLoad(llvmType, ptr, isVolatile, this.cliOptions.namesForIRValues ? varName.getRaw() : "");
}
public p IRGenerator.insertStore(llvm::Value value, llvm::Value ptr, bool isVolatile) {
assert ptr.getType().isPointerTy();
this.builder.createStore(value, ptr, isVolatile);
}