Implement array (no padding)
MODIFY
ir/type.cpp
ir/type.cpp
@@ -803,9 +803,12 @@ const AggregateType* AggregateType::getAsAggregateType() const {
}
-expr ArrayType::getTypeConstraints() const {
- // TODO
- return false;
+ArrayType::ArrayType(string &&name, unsigned elements, Type &elementTy)
+ : AggregateType(move(name), false) {
+ this->elements = elements;
+ defined = true;
+ children.resize(elements, &elementTy);
+ is_padding.resize(elements, false);
}
bool ArrayType::isArrayType() const {
@@ -813,7 +816,7 @@ bool ArrayType::isArrayType() const {
}
void ArrayType::print(ostream &os) const {
- os << "TODO";
+ os << '[' << elements << " x " << *children[0] << ']';
}

MODIFY
ir/type.h
ir/type.h
@@ -281,7 +281,8 @@ public:
class ArrayType final : public AggregateType {
public:
ArrayType(std::string &&name) : AggregateType(std::move(name)) {}
- smt::expr getTypeConstraints() const override;
+ ArrayType(std::string &&name, unsigned elements, Type &elementTy);
+
bool isArrayType() const override;
void print(std::ostream &os) const override;
};

MODIFY
llvm_util/utils.cpp
llvm_util/utils.cpp
@@ -155,6 +155,19 @@ Type* llvm_type2alive(const llvm::Type *ty) {
}
return cache.get();
}
+ case llvm::Type::ArrayTyID: {
+ auto &cache = type_cache[ty];
+ if (!cache) {
+ auto aty = cast<llvm::ArrayType>(ty);
+ auto elems = aty->getNumElements();
+ auto ety = llvm_type2alive(aty->getElementType());
+ if (!ety || elems > 128)
+ return nullptr;
+ cache = make_unique<ArrayType>("ty_" + to_string(type_id_counter++),
+ elems, *ety);
+ }
+ return cache.get();
+ }
default:
err:
*out << "ERROR: Unsupported type: " << *ty << '\n';
@@ -233,10 +246,7 @@ Value* get_operand(llvm::Value *v,
if (gvar)
return gvar;
- if (gv->getValueType()->isArrayTy()) {
- // TODO: Array type of a global variable is not supported.
- return nullptr;
- } else if (auto st = dyn_cast<llvm::StructType>(gv->getValueType())) {
+ if (auto st = dyn_cast<llvm::StructType>(gv->getValueType())) {
if (st->isOpaque())
// TODO: Global variable of opaque type is not supported.
return nullptr;

ADD
tests/alive-tv/constexpr/string.src.ll
+;target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+;target triple = "x86_64-apple-macosx10.15.0"
+
+@.str = constant [6 x i8] c"hello\00", align 1
+@aa = constant i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0)
+
+define i8 @h() {
+ %aa = load i8*, i8** @aa
+ %c = load i8, i8* %aa
+ ret i8 %c
+}
+
+define i8 @e() {
+ %aa = load i8*, i8** @aa
+ %p = getelementptr i8, i8* %aa, i32 1
+ %c = load i8, i8* %p
+ ret i8 %c
+}
+
+define i8 @l() {
+ %aa = load i8*, i8** @aa
+ %p = getelementptr i8, i8* %aa, i32 2
+ %c = load i8, i8* %p
+ ret i8 %c
+}
+
+define i8 @l2() {
+ %aa = load i8*, i8** @aa
+ %p = getelementptr i8, i8* %aa, i32 3
+ %c = load i8, i8* %p
+ ret i8 %c
+}
+
+define i8 @o() {
+ %aa = load i8*, i8** @aa
+ %p = getelementptr i8, i8* %aa, i32 4
+ %c = load i8, i8* %p
+ ret i8 %c
+}
+
+define i8 @zero() {
+ %aa = load i8*, i8** @aa
+ %p = getelementptr i8, i8* %aa, i32 5
+ %c = load i8, i8* %p
+ ret i8 %c
+}
+
+

ADD
tests/alive-tv/constexpr/string.tgt.ll
+;target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+;target triple = "x86_64-apple-macosx10.15.0"
+
+@.str = constant [6 x i8] c"hello\00", align 1
+@aa = constant i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0)
+
+define i8 @h() {
+ ret i8 104
+}
+
+define i8 @e() {
+ ret i8 101
+}
+
+define i8 @l() {
+ ret i8 108
+}
+
+define i8 @l2() {
+ ret i8 108
+}
+
+define i8 @o() {
+ ret i8 111
+}
+
+define i8 @zero() {
+ ret i8 0
+}
+
+

Implement array (no padding) (#207)