Skip to content

Commit f377e3b

Browse files
committed
start
1 parent eedd15c commit f377e3b

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/passes/StringLowering.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,27 @@ struct StringLowering : public StringGathering {
243243

244244
Type nnExt = Type(HeapType::ext, NonNullable);
245245

246+
// Creates an imported string function, returning its name (which is equal to
247+
// the true name of the import, if there is no conflict).
248+
Name addImport(Module* module, Name trueName, Type params, Type results) {
249+
auto name = Names::getValidFunctionName(*module, trueName);
250+
auto sig = Signature(params, results);
251+
Builder builder(*module);
252+
auto* func = module->addFunction(builder.makeFunction(name, sig, {}));
253+
func->module = "wasm:js-string";
254+
func->base = trueName;
255+
return name;
256+
}
257+
246258
void replaceInstructions(Module* module) {
247259
// Add all the possible imports up front, to avoid adding them during
248260
// parallel work. Optimizations can remove unneeded ones later.
249-
Builder builder(*module);
250-
auto array16 = Array(Field(Field::i16, Mutable));
261+
auto nullArray16 = Type(Array(Field(Field::i16, Mutable)), Nullable);
251262

252-
{
253-
fromCharCodeArrayImport = Names::getValidFunctionName(
254-
*module, "string.fromCharCodeArray");
255-
auto sig = Signature({Type(array16, Nullable), Type::i32, Type::i32}, nnExt);
256-
module->addFunction(builder.makeFunction(fromCharCodeArrayImport, sig, {}));
257-
}
258-
{
259-
fromCodePointImport = Names::getValidFunctionName(
260-
*module, "string.fromCodePoint");
261-
auto sig = Signature(Type::i32, nnExt);
262-
module->addFunction(builder.makeFunction(fromCodePointImport, sig, {}));
263-
}
263+
// string.fromCharCodeArray: array, start, end -> ext
264+
fromCharCodeArrayImport = addImport(module, "fromCharCodeArray", {nullArray16, Type::i32, Type::i32}, nnExt);
265+
// string.fromCodePoint: codepoint -> ext
266+
fromCodePointImport = addImport(module, "fromCodePoint", Type::i32, nnExt);
264267

265268
// Replace the string instructions in parallel.
266269
struct Replacer : public WalkerPass<PostWalker<Replacer>> {

test/lit/passes/string-gathering.wast

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@
2727
;; CHECK: (global $global2 stringref (global.get $string.const_bar))
2828
;; LOWER: (type $0 (func))
2929

30+
;; LOWER: (type $1 (array (mut i16)))
31+
32+
;; LOWER: (type $2 (func (param (ref null $1) i32 i32) (result (ref extern))))
33+
34+
;; LOWER: (type $3 (func (param i32) (result (ref extern))))
35+
3036
;; LOWER: (import "string.const" "0" (global $string.const_bar (ref extern)))
3137

3238
;; LOWER: (import "string.const" "1" (global $string.const_other (ref extern)))
3339

3440
;; LOWER: (import "string.const" "2" (global $global (ref extern)))
3541

42+
;; LOWER: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $2) (param (ref null $1) i32 i32) (result (ref extern))))
43+
44+
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $3) (param i32) (result (ref extern))))
45+
3646
;; LOWER: (global $global2 externref (global.get $string.const_bar))
3747
(global $global2 (ref null string) (string.const "bar"))
3848

@@ -111,6 +121,12 @@
111121
;; Multiple possible reusable globals. Also test ignoring of imports.
112122
(module
113123
;; CHECK: (import "a" "b" (global $import (ref string)))
124+
;; LOWER: (type $0 (array (mut i16)))
125+
126+
;; LOWER: (type $1 (func (param (ref null $0) i32 i32) (result (ref extern))))
127+
128+
;; LOWER: (type $2 (func (param i32) (result (ref extern))))
129+
114130
;; LOWER: (import "a" "b" (global $import (ref extern)))
115131
(import "a" "b" (global $import (ref string)))
116132

@@ -122,6 +138,10 @@
122138

123139
;; LOWER: (import "string.const" "1" (global $global4 (ref extern)))
124140

141+
;; LOWER: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $1) (param (ref null $0) i32 i32) (result (ref extern))))
142+
143+
;; LOWER: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $2) (param i32) (result (ref extern))))
144+
125145
;; LOWER: (global $global2 (ref extern) (global.get $global1))
126146
(global $global2 (ref string) (string.const "foo"))
127147

test/lit/passes/string-lowering-instructions.wast

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
(module
66
;; CHECK: (type $0 (func (param externref externref externref externref)))
77

8+
;; CHECK: (type $1 (array (mut i16)))
9+
10+
;; CHECK: (type $2 (func (param (ref null $1) i32 i32) (result (ref extern))))
11+
12+
;; CHECK: (type $3 (func (param i32) (result (ref extern))))
13+
14+
;; CHECK: (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $2) (param (ref null $1) i32 i32) (result (ref extern))))
15+
16+
;; CHECK: (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $3) (param i32) (result (ref extern))))
17+
818
;; CHECK: (func $string.as (type $0) (param $a externref) (param $b externref) (param $c externref) (param $d externref)
919
;; CHECK-NEXT: (local.set $b
1020
;; CHECK-NEXT: (local.get $a)

0 commit comments

Comments
 (0)