-
Notifications
You must be signed in to change notification settings - Fork 211
/
corefn.cpp
80 lines (64 loc) · 2.64 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
#include <iostream>
#include "codegen.h"
#include "node.h"
using namespace std;
extern int yyparse();
extern NBlock* programBlock;
llvm::Function* createPrintfFunction(CodeGenContext& context)
{
std::vector<llvm::Type*> printf_arg_types;
printf_arg_types.push_back(llvm::Type::getInt8PtrTy(MyContext)); //char*
std::cout << "printf" << std::endl;
llvm::FunctionType* printf_type =
llvm::FunctionType::get(
llvm::Type::getInt32Ty(MyContext), 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 createEchoFunction(CodeGenContext& context, llvm::Function* printfFn)
{
std::vector<llvm::Type*> echo_arg_types;
echo_arg_types.push_back(llvm::Type::getInt64Ty(MyContext));
llvm::FunctionType* echo_type =
llvm::FunctionType::get(
llvm::Type::getVoidTy(MyContext), echo_arg_types, false);
llvm::Function *func = llvm::Function::Create(
echo_type, llvm::Function::InternalLinkage,
llvm::Twine("echo"),
context.module
);
llvm::BasicBlock *bblock = llvm::BasicBlock::Create(MyContext, "entry", func, 0);
context.pushBlock(bblock);
const char *constValue = "%d\n";
llvm::Constant *format_const = llvm::ConstantDataArray::getString(MyContext, constValue);
llvm::GlobalVariable *var =
new llvm::GlobalVariable(
*context.module, llvm::ArrayType::get(llvm::IntegerType::get(MyContext, 8), strlen(constValue)+1),
true, llvm::GlobalValue::PrivateLinkage, format_const, ".str");
llvm::Constant *zero =
llvm::Constant::getNullValue(llvm::IntegerType::getInt32Ty(MyContext));
std::vector<llvm::Constant*> indices;
indices.push_back(zero);
indices.push_back(zero);
llvm::Constant *var_ref = llvm::ConstantExpr::getGetElementPtr(
llvm::ArrayType::get(llvm::IntegerType::get(MyContext, 8), strlen(constValue)+1),
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(MyContext, bblock);
context.popBlock();
}
void createCoreFunctions(CodeGenContext& context){
llvm::Function* printfFn = createPrintfFunction(context);
createEchoFunction(context, printfFn);
}