forked from jckarter/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternals.hpp
141 lines (115 loc) · 4 KB
/
externals.hpp
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
#pragma once
#include "clay.hpp"
#include "codegen.hpp"
namespace clay {
struct ExtArgInfo {
enum Kind {
Direct, Indirect
};
Kind kind;
llvm::Type *type;
ExtArgInfo(enum Kind k = Direct, llvm::Type *ll = NULL)
: kind(k), type(ll) {}
static ExtArgInfo getDirect(llvm::Type* ll = NULL) {
return ExtArgInfo(Direct, ll);
}
static ExtArgInfo getIndirect() {
return ExtArgInfo(Indirect, NULL);
}
bool isDirect() const { return kind == Direct; }
bool isIndirect() const { return kind == Indirect; }
};
struct ExternalFunction : public llvm::FoldingSetNode {
struct ArgInfo {
TypePtr type;
ExtArgInfo ext;
};
CallingConv conv;
size_t numReqArg;
bool isVarArg;
llvm::CallingConv::ID llConv;
ArgInfo retInfo;
vector<ArgInfo> argInfos;
vector< pair<unsigned, llvm::Attributes> > attrs;
ExternalFunction(CallingConv conv, TypePtr ret,
vector<TypePtr> &args, size_t numReqArg,
bool isVarArg) {
this->conv = conv;
this->retInfo.type = ret;
for (vector<TypePtr>::iterator it = args.begin(), ie = args.end();
it != ie; ++it)
{
ArgInfo info = { *it, ExtArgInfo::getDirect() };
this->argInfos.push_back(info);
}
this->numReqArg = numReqArg;
this->isVarArg = isVarArg;
}
static void Profile(llvm::FoldingSetNodeID &ID,
CallingConv conv, TypePtr ret,
vector<TypePtr> &args, size_t numReqArg,
bool isVarArg) {
ID.AddInteger(conv);
ID.AddPointer(ret.ptr());
for (vector<TypePtr>::iterator it = args.begin(), ie = args.end();
it != ie; ++it)
{
ID.AddPointer(it->ptr());
}
ID.AddInteger(numReqArg);
ID.AddBoolean(isVarArg);
}
void Profile(llvm::FoldingSetNodeID &ID) {
ID.AddInteger(conv);
ID.AddPointer(retInfo.type.ptr());
for (vector<ArgInfo>::iterator
it = argInfos.begin(), ie = argInfos.end();
it != ie; ++it)
{
ID.AddPointer(it->type.ptr());
}
ID.AddInteger(numReqArg);
ID.AddBoolean(isVarArg);
}
llvm::FunctionType* getLLVMFunctionType() const;
// for generating C function definitions
void allocReturnValue(ArgInfo info,
llvm::Function::arg_iterator &ai,
vector<CReturn> &returns,
CodegenContext* ctx);
CValuePtr allocArgumentValue(ArgInfo info,
llvm::StringRef name,
llvm::Function::arg_iterator &ai,
CodegenContext* ctx);
void returnStatement(ArgInfo info,
vector<CReturn> &returns,
CodegenContext* ctx);
// for calling C functions
void loadStructRetArgument(ArgInfo info,
vector<llvm::Value *> &llArgs,
CodegenContext* ctx,
MultiCValuePtr out);
void loadArgument(ArgInfo info,
CValuePtr cv,
vector<llvm::Value *> &llArgs,
CodegenContext* ctx,
bool isVarArg);
void storeReturnValue(ArgInfo info,
llvm::Value *callReturnValue,
CodegenContext* ctx,
MultiCValuePtr out);
};
struct ExternalTarget : public RefCounted {
llvm::FoldingSet<ExternalFunction> extFuncs;
ExternalTarget() {}
virtual ~ExternalTarget() {}
ExternalFunction* getExternalFunction(
CallingConv conv,
TypePtr ret,
vector<TypePtr> &args,
size_t numReqArg, bool varArg);
virtual void computeInfo(ExternalFunction *f) = 0;
};
typedef Pointer<ExternalTarget> ExternalTargetPtr;
ExternalTargetPtr getExternalTarget();
}