-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfunctions.cpp
More file actions
77 lines (61 loc) · 2.46 KB
/
functions.cpp
File metadata and controls
77 lines (61 loc) · 2.46 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
// SPDX-License-Identifier: Apache-2.0
#include "functions.h"
#include "../llvminstruction.h"
#include "../llvmbuildutils.h"
using namespace libscratchcpp;
using namespace libscratchcpp::llvmins;
ProcessResult Functions::process(LLVMInstruction *ins)
{
ProcessResult ret(true, ins);
switch (ins->type) {
case LLVMInstruction::Type::FunctionCall:
ret.next = buildFunctionCall(ins);
break;
default:
ret.match = false;
break;
}
return ret;
}
LLVMInstruction *Functions::buildFunctionCall(LLVMInstruction *ins)
{
std::vector<llvm::Type *> types;
std::vector<llvm::Value *> args;
// Variables must be synchronized because the function can read them
m_utils.syncVariables();
// Strings are returned through an output parameter
llvm::Value *stringRet = nullptr;
if (ins->functionReturnReg && ins->functionReturnReg->type() == Compiler::StaticType::String) {
stringRet = m_utils.addStringAlloca();
types.push_back(m_utils.getType(Compiler::StaticType::String, false));
args.push_back(stringRet);
}
// Add execution context arg
if (ins->functionCtxArg) {
types.push_back(llvm::PointerType::get(llvm::Type::getInt8Ty(m_utils.llvmCtx()), 0));
args.push_back(m_utils.executionContextPtr());
}
// Add target pointer arg
if (ins->functionTargetArg) {
types.push_back(llvm::PointerType::get(llvm::Type::getInt8Ty(m_utils.llvmCtx()), 0));
args.push_back(m_utils.targetPtr());
}
// Args
for (auto &arg : ins->args) {
types.push_back(m_utils.getType(arg.first, false));
args.push_back(m_utils.castValue(arg.second, arg.first));
}
llvm::Type *retType = m_utils.getType(ins->functionReturnReg ? ins->functionReturnReg->type() : Compiler::StaticType::Void, true);
llvm::Value *ret = m_builder.CreateCall(m_utils.functions().resolveFunction(ins->functionName, llvm::FunctionType::get(retType, types, false)), args);
if (ins->functionReturnReg) {
if (ins->functionReturnReg->type() == Compiler::StaticType::String)
ins->functionReturnReg->value = stringRet;
else if (m_utils.isSingleType(ins->functionReturnReg->type()))
ins->functionReturnReg->value = ret;
else {
ins->functionReturnReg->value = m_utils.addAlloca(retType);
m_builder.CreateStore(ret, ins->functionReturnReg->value);
}
}
return ins->next;
}