-
Notifications
You must be signed in to change notification settings - Fork 1
/
corefn.cpp
111 lines (92 loc) · 3.71 KB
/
corefn.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
#include <iostream>
#include "codegen.h"
#include "node.h"
extern "C" int wow();
llvm::Function* insertWowFunction(CodeGenContext& context)
{
llvm::FunctionType* type =
llvm::FunctionType::get(
llvm::Type::getInt32Ty(getGlobalContext()), false);
llvm::Function *func = llvm::Function::Create(
type, llvm::Function::ExternalLinkage,
llvm::Twine("wow"),
context.module
);
func->setCallingConv(llvm::CallingConv::C);
return func;
}
llvm::Function* insertPlzFunction(CodeGenContext& context)
{
std::vector<llvm::Type*> args;
args.push_back(llvm::Type::getInt8PtrTy(getGlobalContext())); //char*
llvm::FunctionType* printf_type =
llvm::FunctionType::get(llvm::Type::getVoidTy(getGlobalContext()), args, false);
llvm::Function *func = llvm::Function::Create(
printf_type, llvm::Function::ExternalLinkage,
llvm::Twine("plz"),
context.module
);
func->setCallingConv(llvm::CallingConv::C);
return func;
}
llvm::Function* insertPrintfFunction(CodeGenContext& context)
{
std::vector<llvm::Type*> printf_arg_types;
printf_arg_types.push_back(llvm::Type::getInt8PtrTy(getGlobalContext())); //char*
llvm::FunctionType* printf_type =
llvm::FunctionType::get(
llvm::Type::getInt32Ty(getGlobalContext()), printf_arg_types, true);
llvm::Function *func = llvm::Function::Create(
printf_type, llvm::Function::ExternalLinkage,
llvm::Twine("printf"),
context.module
);
func->setCallingConv(llvm::CallingConv::C);
return func;
}
void createSuchFunction(CodeGenContext& context, llvm::Function* printfFn)
{
std::vector<llvm::Type*> echo_arg_types;
echo_arg_types.push_back(llvm::Type::getInt64Ty(getGlobalContext()));
llvm::FunctionType* echo_type =
llvm::FunctionType::get(
llvm::Type::getVoidTy(getGlobalContext()), echo_arg_types, false);
llvm::Function *func = llvm::Function::Create(
echo_type, llvm::Function::InternalLinkage,
llvm::Twine("such"),
context.module
);
llvm::BasicBlock *bblock = llvm::BasicBlock::Create(getGlobalContext(), "entry", func, 0);
context.pushBlock(bblock);
const char *constValue = "Such %d\n";
llvm::Constant *format_const = llvm::ConstantDataArray::getString(getGlobalContext(), constValue);
llvm::GlobalVariable *var =
new llvm::GlobalVariable(
*context.module, llvm::ArrayType::get(llvm::IntegerType::get(getGlobalContext(), 8), strlen(constValue)+1),
true, llvm::GlobalValue::PrivateLinkage, format_const, ".str");
llvm::Constant *zero =
llvm::Constant::getNullValue(llvm::IntegerType::getInt32Ty(getGlobalContext()));
std::vector<llvm::Constant*> indices;
indices.push_back(zero);
indices.push_back(zero);
llvm::Constant *var_ref = llvm::ConstantExpr::getGetElementPtr(var, indices);
std::vector<Value*> args;
args.push_back(var_ref);
Function::arg_iterator argsValues = func->arg_begin();
Value* toPrint = argsValues++;
toPrint->setName("toPrint");
args.push_back(toPrint);
CallInst *call = CallInst::Create(printfFn, makeArrayRef(args), "", bblock);
ReturnInst::Create(getGlobalContext(), bblock);
context.popBlock();
}
void createCoreFunctions(CodeGenContext& context){
insertWowFunction(context);
insertPlzFunction(context);
llvm::Function* printfFn = insertPrintfFunction(context);
if (!printfFn) {
std::cout << "Error" << std::endl;
wow(); //We can actually use it here xD
}
createSuchFunction(context, printfFn);
}