Skip to content

Commit 90c726a

Browse files
authored
[Strings] Add experimental string.hash instruction (#5480)
See WebAssembly/stringref#60
1 parent 42b859f commit 90c726a

File tree

8 files changed

+34
-0
lines changed

8 files changed

+34
-0
lines changed

scripts/gen-s-parser.py

+1
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@
618618
("string.measure_wtf8", "makeStringMeasure(s, StringMeasureWTF8)"),
619619
("string.measure_wtf16", "makeStringMeasure(s, StringMeasureWTF16)"),
620620
("string.is_usv_sequence", "makeStringMeasure(s, StringMeasureIsUSV)"),
621+
("string.hash", "makeStringMeasure(s, StringMeasureHash)"),
621622
("string.encode_wtf8", "makeStringEncode(s, StringEncodeWTF8)"),
622623
("string.encode_wtf16", "makeStringEncode(s, StringEncodeWTF16)"),
623624
("string.encode_wtf8_array", "makeStringEncode(s, StringEncodeWTF8Array)"),

src/gen-s-parser.inc

+10
Original file line numberDiff line numberDiff line change
@@ -3194,6 +3194,9 @@ switch (buf[0]) {
31943194
case 'f':
31953195
if (op == "string.from_code_point"sv) { return makeStringNew(s, StringNewFromCodePoint, false); }
31963196
goto parse_error;
3197+
case 'h':
3198+
if (op == "string.hash"sv) { return makeStringMeasure(s, StringMeasureHash); }
3199+
goto parse_error;
31973200
case 'i':
31983201
if (op == "string.is_usv_sequence"sv) { return makeStringMeasure(s, StringMeasureIsUSV); }
31993202
goto parse_error;
@@ -8931,6 +8934,13 @@ switch (buf[0]) {
89318934
return *ret;
89328935
}
89338936
goto parse_error;
8937+
case 'h':
8938+
if (op == "string.hash"sv) {
8939+
auto ret = makeStringMeasure(ctx, pos, StringMeasureHash);
8940+
CHECK_ERR(ret);
8941+
return *ret;
8942+
}
8943+
goto parse_error;
89348944
case 'i':
89358945
if (op == "string.is_usv_sequence"sv) {
89368946
auto ret = makeStringMeasure(ctx, pos, StringMeasureIsUSV);

src/passes/Print.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,9 @@ struct PrintExpressionContents
24042404
case StringMeasureWTF16View:
24052405
printMedium(o, "stringview_wtf16.length");
24062406
break;
2407+
case StringMeasureHash:
2408+
printMedium(o, "string.hash");
2409+
break;
24072410
default:
24082411
WASM_UNREACHABLE("invalid string.measure*");
24092412
}

src/wasm-binary.h

+1
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,7 @@ enum ASTNodes {
11651165
StringViewIterSlice = 0xa4,
11661166
StringCompare = 0xa8,
11671167
StringFromCodePoint = 0xa9,
1168+
StringHash = 0xaa,
11681169
StringNewWTF8Array = 0xb0,
11691170
StringNewWTF16Array = 0xb1,
11701171
StringEncodeWTF8Array = 0xb2,

src/wasm.h

+1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ enum StringMeasureOp {
596596
StringMeasureWTF16,
597597
StringMeasureIsUSV,
598598
StringMeasureWTF16View,
599+
StringMeasureHash,
599600
};
600601

601602
enum StringEncodeOp {

src/wasm/wasm-binary.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -7286,6 +7286,8 @@ bool WasmBinaryBuilder::maybeVisitStringMeasure(Expression*& out,
72867286
op = StringMeasureIsUSV;
72877287
} else if (code == BinaryConsts::StringViewWTF16Length) {
72887288
op = StringMeasureWTF16View;
7289+
} else if (code == BinaryConsts::StringHash) {
7290+
op = StringMeasureHash;
72897291
} else {
72907292
return false;
72917293
}

src/wasm/wasm-stack.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,9 @@ void BinaryInstWriter::visitStringMeasure(StringMeasure* curr) {
23382338
case StringMeasureWTF16View:
23392339
o << U32LEB(BinaryConsts::StringViewWTF16Length);
23402340
break;
2341+
case StringMeasureHash:
2342+
o << U32LEB(BinaryConsts::StringHash);
2343+
break;
23412344
default:
23422345
WASM_UNREACHABLE("invalid string.new*");
23432346
}

test/lit/strings.wast

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
;; CHECK: (type $ref|$array|_=>_none (func (param (ref $array))))
3939

40+
;; CHECK: (type $stringref_=>_i32 (func (param stringref) (result i32)))
41+
4042
;; CHECK: (global $string-const stringref (string.const "string in a global \01\ff\00\t\t\n\n\r\r\"\"\'\'\\\\"))
4143
(global $string-const stringref (string.const "string in a global \01\ff\00\t\09\n\0a\r\0d\"\22\'\27\\\5c"))
4244

@@ -626,4 +628,15 @@
626628
)
627629
)
628630
)
631+
632+
;; CHECK: (func $string.hash (type $stringref_=>_i32) (param $ref stringref) (result i32)
633+
;; CHECK-NEXT: (string.hash
634+
;; CHECK-NEXT: (local.get $ref)
635+
;; CHECK-NEXT: )
636+
;; CHECK-NEXT: )
637+
(func $string.hash (param $ref stringref) (result i32)
638+
(string.hash
639+
(local.get $ref)
640+
)
641+
)
629642
)

0 commit comments

Comments
 (0)