summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ir/properties.h2
-rw-r--r--src/literal.h1
-rw-r--r--src/wasm-interpreter.h7
-rw-r--r--src/wasm/literal.cpp10
-rw-r--r--test/lit/passes/dae-gc.wast47
5 files changed, 61 insertions, 6 deletions
diff --git a/src/ir/properties.h b/src/ir/properties.h
index d47ee55a8..fb0065948 100644
--- a/src/ir/properties.h
+++ b/src/ir/properties.h
@@ -116,6 +116,8 @@ inline Literal getLiteral(const Expression* curr) {
if (auto* c = i->value->dynCast<Const>()) {
return Literal::makeI31(c->value.geti32());
}
+ } else if (auto* s = curr->dynCast<StringConst>()) {
+ return Literal(s->string.toString());
}
WASM_UNREACHABLE("non-constant expression");
}
diff --git a/src/literal.h b/src/literal.h
index 5e449c576..317839b72 100644
--- a/src/literal.h
+++ b/src/literal.h
@@ -85,6 +85,7 @@ public:
assert(type.isSignature());
}
explicit Literal(std::shared_ptr<GCData> gcData, HeapType type);
+ explicit Literal(std::string string);
Literal(const Literal& other);
Literal& operator=(const Literal& other);
~Literal();
diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h
index 851c4013c..87b0e0c67 100644
--- a/src/wasm-interpreter.h
+++ b/src/wasm-interpreter.h
@@ -1838,12 +1838,7 @@ public:
}
}
Flow visitStringConst(StringConst* curr) {
- Literals contents;
- for (size_t i = 0; i < curr->string.size(); i++) {
- contents.push_back(Literal(int32_t(curr->string[i])));
- }
- auto heapType = curr->type.getHeapType();
- return Literal(std::make_shared<GCData>(heapType, contents), heapType);
+ return Literal(curr->string.toString());
}
Flow visitStringMeasure(StringMeasure* curr) { WASM_UNREACHABLE("unimp"); }
Flow visitStringEncode(StringEncode* curr) { WASM_UNREACHABLE("unimp"); }
diff --git a/src/wasm/literal.cpp b/src/wasm/literal.cpp
index 80ff47b17..016df2e18 100644
--- a/src/wasm/literal.cpp
+++ b/src/wasm/literal.cpp
@@ -76,6 +76,16 @@ Literal::Literal(std::shared_ptr<GCData> gcData, HeapType type)
assert((isData() && gcData) || (type.isBottom() && !gcData));
}
+Literal::Literal(std::string string)
+ : gcData(nullptr), type(Type(HeapType::string, NonNullable)) {
+ // TODO: we could in theory internalize strings
+ Literals contents;
+ for (auto c : string) {
+ contents.push_back(Literal(int32_t(c)));
+ }
+ gcData = std::make_shared<GCData>(HeapType::string, contents);
+}
+
Literal::Literal(const Literal& other) : type(other.type) {
if (type.isBasic()) {
switch (type.getBasic()) {
diff --git a/test/lit/passes/dae-gc.wast b/test/lit/passes/dae-gc.wast
index 4e5144453..2100baa4c 100644
--- a/test/lit/passes/dae-gc.wast
+++ b/test/lit/passes/dae-gc.wast
@@ -247,3 +247,50 @@
;; NOMNL-NEXT: )
(func $c)
)
+
+;; Test that string constants can be applied.
+(module
+ ;; CHECK: (func $0 (type $none_=>_none)
+ ;; CHECK-NEXT: (call $1)
+ ;; CHECK-NEXT: )
+ ;; NOMNL: (func $0 (type $none_=>_none)
+ ;; NOMNL-NEXT: (call $1)
+ ;; NOMNL-NEXT: )
+ (func $0
+ (call $1
+ (string.const "310")
+ (string.const "929")
+ )
+ )
+ ;; CHECK: (func $1 (type $none_=>_none)
+ ;; CHECK-NEXT: (local $0 (ref string))
+ ;; CHECK-NEXT: (local $1 (ref string))
+ ;; CHECK-NEXT: (local.set $0
+ ;; CHECK-NEXT: (string.const "929")
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (block
+ ;; CHECK-NEXT: (local.set $1
+ ;; CHECK-NEXT: (string.const "310")
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (nop)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; NOMNL: (func $1 (type $none_=>_none)
+ ;; NOMNL-NEXT: (local $0 (ref string))
+ ;; NOMNL-NEXT: (local $1 (ref string))
+ ;; NOMNL-NEXT: (local.set $0
+ ;; NOMNL-NEXT: (string.const "929")
+ ;; NOMNL-NEXT: )
+ ;; NOMNL-NEXT: (block
+ ;; NOMNL-NEXT: (local.set $1
+ ;; NOMNL-NEXT: (string.const "310")
+ ;; NOMNL-NEXT: )
+ ;; NOMNL-NEXT: (nop)
+ ;; NOMNL-NEXT: )
+ ;; NOMNL-NEXT: )
+ (func $1 (param $0 (ref string)) (param $1 (ref string))
+ ;; The parameters here will be removed, and the constant values placed in the
+ ;; function.
+ (nop)
+ )
+)