Skip to content

Commit 5206013

Browse files
committed
Update __ptr__ codegen
1 parent ccef154 commit 5206013

2 files changed

Lines changed: 74 additions & 10 deletions

File tree

codon/cir/llvm/llvisitor.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,9 +2145,17 @@ void LLVMVisitor::visit(const VarValue *x) {
21452145
}
21462146

21472147
void LLVMVisitor::visit(const PointerValue *x) {
2148-
std::vector<llvm::Value *> gepIndices;
2149-
auto *type = x->getVar()->getType();
2148+
const auto &fields = x->getFields();
2149+
llvm::Value *var = getVar(x->getVar());
2150+
seqassertn(var, "{} variable not found", *x);
2151+
2152+
if (fields.empty()) {
2153+
value = var; // note: we don't load the pointer
2154+
return;
2155+
}
21502156

2157+
auto *type = x->getVar()->getType();
2158+
std::vector<llvm::Value *> gepIndices = {B->getInt32(0)};
21512159
for (auto &field : x->getFields()) {
21522160
if (auto *ref = cast<types::RefType>(type)) {
21532161
auto membIndex = ref->getMemberIndex(field);
@@ -2169,14 +2177,7 @@ void LLVMVisitor::visit(const PointerValue *x) {
21692177
}
21702178
}
21712179

2172-
llvm::Value *var = getVar(x->getVar());
2173-
seqassertn(var, "{} variable not found", *x);
2174-
2175-
if (gepIndices.empty()) {
2176-
value = var; // note: we don't load the pointer
2177-
} else {
2178-
value = B->CreateInBoundsGEP(getLLVMType(x->getVar()->getType()), var, gepIndices);
2179-
}
2180+
value = B->CreateInBoundsGEP(getLLVMType(x->getVar()->getType()), var, gepIndices);
21802181
}
21812182

21822183
/*

test/core/bltin.codon

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,3 +1107,66 @@ test_wide_uint_str(UInt[1024])
11071107
# test_wide_int_str(Int[4096])
11081108
# test_wide_uint_str(UInt[2048])
11091109
# test_wide_uint_str(UInt[4096])
1110+
1111+
1112+
# __ptr__
1113+
@test
1114+
def test_ptr_fields():
1115+
@tuple
1116+
class A:
1117+
n1: int
1118+
n2: int
1119+
n3: int
1120+
1121+
@tuple
1122+
class B:
1123+
a1: A
1124+
a2: A
1125+
1126+
@tuple
1127+
class C:
1128+
b1: B
1129+
b2: B
1130+
1131+
x1 = A(1, 2, 3)
1132+
__ptr__(x1.n1)[0] += 10
1133+
__ptr__(x1.n2)[0] += 20
1134+
__ptr__(x1.n3)[0] += 30
1135+
1136+
assert x1.n1 == 10 + 1
1137+
assert x1.n2 == 20 + 2
1138+
assert x1.n3 == 30 + 3
1139+
1140+
x2 = C(B(A(1, 2, 3), A(4, 5, 6)), B(A(7, 8, 9), A(10, 11, 12)))
1141+
__ptr__(x2.b1.a1.n1)[0] += 10
1142+
__ptr__(x2.b1.a1.n2)[0] += 20
1143+
__ptr__(x2.b1.a1.n3)[0] += 30
1144+
__ptr__(x2.b1.a2.n1)[0] += 40
1145+
__ptr__(x2.b1.a2.n2)[0] += 50
1146+
__ptr__(x2.b1.a2.n3)[0] += 60
1147+
__ptr__(x2.b2.a1.n1)[0] += 70
1148+
__ptr__(x2.b2.a1.n2)[0] += 80
1149+
__ptr__(x2.b2.a1.n3)[0] += 90
1150+
__ptr__(x2.b2.a2.n1)[0] += 100
1151+
__ptr__(x2.b2.a2.n2)[0] += 110
1152+
__ptr__(x2.b2.a2.n3)[0] += 120
1153+
1154+
assert x2.b1.a1.n1 == 10 + 1
1155+
assert x2.b1.a1.n2 == 20 + 2
1156+
assert x2.b1.a1.n3 == 30 + 3
1157+
assert x2.b1.a2.n1 == 40 + 4
1158+
assert x2.b1.a2.n2 == 50 + 5
1159+
assert x2.b1.a2.n3 == 60 + 6
1160+
assert x2.b2.a1.n1 == 70 + 7
1161+
assert x2.b2.a1.n2 == 80 + 8
1162+
assert x2.b2.a1.n3 == 90 + 9
1163+
assert x2.b2.a2.n1 == 100 + 10
1164+
assert x2.b2.a2.n2 == 110 + 11
1165+
assert x2.b2.a2.n3 == 120 + 12
1166+
1167+
__ptr__(x1)[0] = A(-1, -2, -3)
1168+
assert x1.n1 == -1
1169+
assert x1.n2 == -2
1170+
assert x1.n3 == -3
1171+
1172+
test_ptr_fields()

0 commit comments

Comments
 (0)