Skip to content

Commit b88bedb

Browse files
authored
[Strings] string.concat (#4777)
1 parent 19a4375 commit b88bedb

20 files changed

+113
-3
lines changed

scripts/gen-s-parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@
621621
("string.measure_wtf16", "makeStringMeasure(s, StringMeasureWTF16)"),
622622
("string.encode_wtf8", "makeStringEncode(s, StringEncodeWTF8)"),
623623
("string.encode_wtf16", "makeStringEncode(s, StringEncodeWTF16)"),
624+
("string.concat", "makeStringConcat(s)"),
624625
]
625626

626627

src/gen-s-parser.inc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,9 +3129,17 @@ switch (op[0]) {
31293129
switch (op[3]) {
31303130
case 'i': {
31313131
switch (op[7]) {
3132-
case 'c':
3133-
if (strcmp(op, "string.const") == 0) { return makeStringConst(s); }
3134-
goto parse_error;
3132+
case 'c': {
3133+
switch (op[10]) {
3134+
case 'c':
3135+
if (strcmp(op, "string.concat") == 0) { return makeStringConcat(s); }
3136+
goto parse_error;
3137+
case 's':
3138+
if (strcmp(op, "string.const") == 0) { return makeStringConst(s); }
3139+
goto parse_error;
3140+
default: goto parse_error;
3141+
}
3142+
}
31353143
case 'e': {
31363144
switch (op[17]) {
31373145
case '1':

src/ir/ReFinalize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void ReFinalize::visitStringNew(StringNew* curr) { curr->finalize(); }
176176
void ReFinalize::visitStringConst(StringConst* curr) { curr->finalize(); }
177177
void ReFinalize::visitStringMeasure(StringMeasure* curr) { curr->finalize(); }
178178
void ReFinalize::visitStringEncode(StringEncode* curr) { curr->finalize(); }
179+
void ReFinalize::visitStringConcat(StringConcat* curr) { curr->finalize(); }
179180

180181
void ReFinalize::visitFunction(Function* curr) {
181182
// we may have changed the body from unreachable to none, which might be bad

src/ir/cost.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
681681
CostType visitStringEncode(StringEncode* curr) {
682682
return 6 + visit(curr->ref) + visit(curr->ptr);
683683
}
684+
CostType visitStringConcat(StringConcat* curr) {
685+
return 10 + visit(curr->left) + visit(curr->right);
686+
}
684687

685688
private:
686689
CostType nullCheckCost(Expression* ref) {

src/ir/effects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,10 @@ class EffectAnalyzer {
742742
// traps when ref is null or we write out of bounds.
743743
parent.implicitTrap = true;
744744
}
745+
void visitStringConcat(StringConcat* curr) {
746+
// traps when an input is null.
747+
parent.implicitTrap = true;
748+
}
745749
};
746750

747751
public:

src/ir/possible-contents.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ struct InfoCollector
689689
// TODO: optimize when possible
690690
addRoot(curr);
691691
}
692+
void visitStringConcat(StringConcat* curr) {
693+
// TODO: optimize when possible
694+
addRoot(curr);
695+
}
692696

693697
// TODO: Model which throws can go to which catches. For now, anything thrown
694698
// is sent to the location of that tag, and any catch of that tag can

src/passes/Print.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,9 @@ struct PrintExpressionContents
22722272
WASM_UNREACHABLE("invalid string.encode*");
22732273
}
22742274
}
2275+
void visitStringConcat(StringConcat* curr) {
2276+
printMedium(o, "string.concat");
2277+
}
22752278
};
22762279

22772280
// Prints an expression in s-expr format, including both the

src/wasm-binary.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ enum ASTNodes {
11441144
StringMeasureWTF16 = 0x85,
11451145
StringEncodeWTF8 = 0x86,
11461146
StringEncodeWTF16 = 0x87,
1147+
StringConcat = 0x88,
11471148
};
11481149

11491150
enum MemoryAccess {
@@ -1728,6 +1729,7 @@ class WasmBinaryBuilder {
17281729
bool maybeVisitStringConst(Expression*& out, uint32_t code);
17291730
bool maybeVisitStringMeasure(Expression*& out, uint32_t code);
17301731
bool maybeVisitStringEncode(Expression*& out, uint32_t code);
1732+
bool maybeVisitStringConcat(Expression*& out, uint32_t code);
17311733
void visitSelect(Select* curr, uint8_t code);
17321734
void visitReturn(Return* curr);
17331735
void visitMemorySize(MemorySize* curr);

src/wasm-builder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,13 @@ class Builder {
10201020
ret->finalize();
10211021
return ret;
10221022
}
1023+
StringConcat* makeStringConcat(Expression* left, Expression* right) {
1024+
auto* ret = wasm.allocator.alloc<StringConcat>();
1025+
ret->left = left;
1026+
ret->right = right;
1027+
ret->finalize();
1028+
return ret;
1029+
}
10231030

10241031
// Additional helpers
10251032

src/wasm-delegations-fields.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ switch (DELEGATE_ID) {
742742
DELEGATE_END(StringEncode);
743743
break;
744744
}
745+
case Expression::Id::StringConcatId: {
746+
DELEGATE_START(StringConcat);
747+
DELEGATE_FIELD_CHILD(StringConcat, right);
748+
DELEGATE_FIELD_CHILD(StringConcat, left);
749+
DELEGATE_END(StringConcat);
750+
break;
751+
}
745752
}
746753

747754
#undef DELEGATE_ID

0 commit comments

Comments
 (0)