forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirfuncty.cpp
134 lines (105 loc) · 3.31 KB
/
irfuncty.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//===-- irfuncty.cpp ------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "ir/irfuncty.h"
#include "mtype.h"
#include "gen/abi.h"
#include "gen/dvalue.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/tollvm.h"
IrFuncTyArg::IrFuncTyArg(Type *t, bool bref, AttrBuilder a)
: type(t),
ltype(t != Type::tvoid && bref ? DtoType(t->pointerTo()) : DtoType(t)),
attrs(std::move(a)), byref(bref) {}
bool IrFuncTyArg::isInReg() const { return attrs.contains(LLAttribute::InReg); }
bool IrFuncTyArg::isSRet() const {
return attrs.contains(LLAttribute::StructRet);
}
bool IrFuncTyArg::isByVal() const { return attrs.contains(LLAttribute::ByVal); }
llvm::Value *IrFuncTy::putRet(DValue *dval) {
assert(!arg_sret);
if (ret->rewrite) {
Logger::println("Rewrite: putRet");
LOG_SCOPE
return ret->rewrite->put(dval);
}
if (ret->byref || DtoIsInMemoryOnly(dval->type))
return DtoLVal(dval);
return DtoRVal(dval);
}
llvm::Value *IrFuncTy::getRetRVal(Type *dty, LLValue *val) {
assert(!arg_sret);
if (ret->rewrite) {
Logger::println("Rewrite: getRetRVal");
LOG_SCOPE
return ret->rewrite->getRVal(dty, val);
}
return val;
}
llvm::Value *IrFuncTy::getRetLVal(Type *dty, LLValue *val) {
assert(!arg_sret);
if (ret->rewrite) {
Logger::println("Rewrite: getRetLVal");
LOG_SCOPE
return ret->rewrite->getLVal(dty, val);
}
return DtoAllocaDump(val, dty);
}
llvm::Value *IrFuncTy::putParam(size_t idx, DValue *dval) {
assert(idx < args.size() && "invalid putParam");
return putParam(*args[idx], dval);
}
llvm::Value *IrFuncTy::putParam(const IrFuncTyArg &arg, DValue *dval) {
if (arg.rewrite) {
Logger::println("Rewrite: putParam");
LOG_SCOPE
return arg.rewrite->put(dval);
}
if (arg.byref || DtoIsInMemoryOnly(dval->type))
return DtoLVal(dval);
return DtoRVal(dval);
}
LLValue *IrFuncTy::getParamLVal(Type *dty, size_t idx, LLValue *val) {
assert(idx < args.size() && "invalid getParam");
if (args[idx]->rewrite) {
Logger::println("Rewrite: getParamLVal");
LOG_SCOPE
return args[idx]->rewrite->getLVal(dty, val);
}
return DtoAllocaDump(val, dty);
}
AttrSet IrFuncTy::getParamAttrs(bool passThisBeforeSret) {
AttrSet newAttrs;
int idx = 0;
// handle implicit args
#define ADD_PA(X) \
if (X) { \
newAttrs.add(idx, (X)->attrs); \
idx++; \
}
ADD_PA(ret)
if (arg_sret && arg_this && passThisBeforeSret) {
ADD_PA(arg_this)
ADD_PA(arg_sret)
} else {
ADD_PA(arg_sret)
ADD_PA(arg_this)
}
ADD_PA(arg_nest)
ADD_PA(arg_arguments)
#undef ADD_PA
// Set attributes on the explicit parameters.
const size_t n = args.size();
for (size_t k = 0; k < n; k++) {
const size_t i = idx + (reverseParams ? (n - k - 1) : k);
newAttrs.add(i, args[k]->attrs);
}
return newAttrs;
}