Skip to content

Commit 6968448

Browse files
feat: store local names in debug mode (#2437)
Co-authored-by: Max Graey <[email protected]>
1 parent 7406fa1 commit 6968448

File tree

138 files changed

+108841
-108821
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+108841
-108821
lines changed

src/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,13 +1016,14 @@ export class Compiler extends DiagnosticEmitter {
10161016
let numLocals = locals.length;
10171017
let varTypes = new Array<TypeRef>(numLocals);
10181018
for (let i = 0; i < numLocals; ++i) varTypes[i] = locals[i].type.toRef();
1019-
module.addFunction(
1019+
const funcRef = module.addFunction(
10201020
startFunction.internalName,
10211021
startSignature.paramRefs,
10221022
startSignature.resultRefs,
10231023
varTypes,
10241024
module.flatten(startFunctionBody)
10251025
);
1026+
startFunction.finalize(module, funcRef);
10261027
previousBody.push(
10271028
module.call(startFunction.internalName, null, TypeRef.None)
10281029
);
@@ -10295,7 +10296,6 @@ export class Compiler extends DiagnosticEmitter {
1029510296
}
1029610297

1029710298
// helpers
10298-
1029910299
function mangleImportName(
1030010300
element: Element,
1030110301
declaration: DeclarationStatement

src/module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,10 @@ export class Module {
19131913
return ret;
19141914
}
19151915

1916+
setLocalName(funcRef: FunctionRef, index: u32, name: string): void {
1917+
binaryen._BinaryenFunctionSetLocalName(funcRef, index, this.allocStringCached(name));
1918+
}
1919+
19161920
getFunction(
19171921
name: string
19181922
): FunctionRef {

src/program.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,6 +3773,22 @@ export class Function extends TypedElement {
37733773
);
37743774
}
37753775
}
3776+
if (this.program.options.debugInfo) {
3777+
let localNameMap = new Set<string>();
3778+
let localsByIndex = this.localsByIndex;
3779+
for (let i = 0, k = localsByIndex.length; i < k; i++) {
3780+
let localName = localsByIndex[i].name;
3781+
if (localNameMap.has(localName)) {
3782+
let repeat = 0;
3783+
while (localNameMap.has(`${localName}_${repeat}`)) {
3784+
repeat++;
3785+
}
3786+
localName = `${localName}_${repeat}`;
3787+
}
3788+
localNameMap.add(localName);
3789+
module.setLocalName(ref, i, localName);
3790+
}
3791+
}
37763792
}
37773793
}
37783794

tests/compiler/NonNullable.debug.wat

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,44 @@
2525
(elem $0 (i32.const 1))
2626
(export "memory" (memory $0))
2727
(start $~start)
28-
(func $~lib/string/String#get:length (param $0 i32) (result i32)
29-
local.get $0
28+
(func $~lib/string/String#get:length (param $this i32) (result i32)
29+
local.get $this
3030
i32.const 20
3131
i32.sub
3232
i32.load offset=16
3333
i32.const 1
3434
i32.shr_u
3535
)
36-
(func $~lib/util/string/compareImpl (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
37-
(local $5 i32)
38-
(local $6 i32)
39-
(local $7 i32)
40-
(local $8 i32)
41-
(local $9 i32)
42-
local.get $0
43-
local.get $1
36+
(func $~lib/util/string/compareImpl (param $str1 i32) (param $index1 i32) (param $str2 i32) (param $index2 i32) (param $len i32) (result i32)
37+
(local $ptr1 i32)
38+
(local $ptr2 i32)
39+
(local $var$7 i32)
40+
(local $a i32)
41+
(local $b i32)
42+
local.get $str1
43+
local.get $index1
4444
i32.const 1
4545
i32.shl
4646
i32.add
47-
local.set $5
48-
local.get $2
49-
local.get $3
47+
local.set $ptr1
48+
local.get $str2
49+
local.get $index2
5050
i32.const 1
5151
i32.shl
5252
i32.add
53-
local.set $6
53+
local.set $ptr2
5454
i32.const 0
5555
i32.const 2
5656
i32.lt_s
5757
drop
58-
local.get $4
58+
local.get $len
5959
i32.const 4
6060
i32.ge_u
6161
if (result i32)
62-
local.get $5
62+
local.get $ptr1
6363
i32.const 7
6464
i32.and
65-
local.get $6
65+
local.get $ptr2
6666
i32.const 7
6767
i32.and
6868
i32.or
@@ -73,124 +73,124 @@
7373
if
7474
block $do-break|0
7575
loop $do-loop|0
76-
local.get $5
76+
local.get $ptr1
7777
i64.load
78-
local.get $6
78+
local.get $ptr2
7979
i64.load
8080
i64.ne
8181
if
8282
br $do-break|0
8383
end
84-
local.get $5
84+
local.get $ptr1
8585
i32.const 8
8686
i32.add
87-
local.set $5
88-
local.get $6
87+
local.set $ptr1
88+
local.get $ptr2
8989
i32.const 8
9090
i32.add
91-
local.set $6
92-
local.get $4
91+
local.set $ptr2
92+
local.get $len
9393
i32.const 4
9494
i32.sub
95-
local.set $4
96-
local.get $4
95+
local.set $len
96+
local.get $len
9797
i32.const 4
9898
i32.ge_u
9999
br_if $do-loop|0
100100
end
101101
end
102102
end
103103
loop $while-continue|1
104-
local.get $4
105-
local.tee $7
104+
local.get $len
105+
local.tee $var$7
106106
i32.const 1
107107
i32.sub
108-
local.set $4
109-
local.get $7
110-
local.set $7
111-
local.get $7
108+
local.set $len
109+
local.get $var$7
110+
local.set $var$7
111+
local.get $var$7
112112
if
113-
local.get $5
113+
local.get $ptr1
114114
i32.load16_u
115-
local.set $8
116-
local.get $6
115+
local.set $a
116+
local.get $ptr2
117117
i32.load16_u
118-
local.set $9
119-
local.get $8
120-
local.get $9
118+
local.set $b
119+
local.get $a
120+
local.get $b
121121
i32.ne
122122
if
123-
local.get $8
124-
local.get $9
123+
local.get $a
124+
local.get $b
125125
i32.sub
126126
return
127127
end
128-
local.get $5
128+
local.get $ptr1
129129
i32.const 2
130130
i32.add
131-
local.set $5
132-
local.get $6
131+
local.set $ptr1
132+
local.get $ptr2
133133
i32.const 2
134134
i32.add
135-
local.set $6
135+
local.set $ptr2
136136
br $while-continue|1
137137
end
138138
end
139139
i32.const 0
140140
)
141-
(func $~lib/string/String.__eq (param $0 i32) (param $1 i32) (result i32)
142-
(local $2 i32)
143-
local.get $0
144-
local.get $1
141+
(func $~lib/string/String.__eq (param $left i32) (param $right i32) (result i32)
142+
(local $leftLength i32)
143+
local.get $left
144+
local.get $right
145145
i32.eq
146146
if
147147
i32.const 1
148148
return
149149
end
150-
local.get $0
150+
local.get $left
151151
i32.const 0
152152
i32.eq
153153
if (result i32)
154154
i32.const 1
155155
else
156-
local.get $1
156+
local.get $right
157157
i32.const 0
158158
i32.eq
159159
end
160160
if
161161
i32.const 0
162162
return
163163
end
164-
local.get $0
164+
local.get $left
165165
call $~lib/string/String#get:length
166-
local.set $2
167-
local.get $2
168-
local.get $1
166+
local.set $leftLength
167+
local.get $leftLength
168+
local.get $right
169169
call $~lib/string/String#get:length
170170
i32.ne
171171
if
172172
i32.const 0
173173
return
174174
end
175-
local.get $0
175+
local.get $left
176176
i32.const 0
177-
local.get $1
177+
local.get $right
178178
i32.const 0
179-
local.get $2
179+
local.get $leftLength
180180
call $~lib/util/string/compareImpl
181181
i32.eqz
182182
)
183-
(func $~lib/string/String.__ne (param $0 i32) (param $1 i32) (result i32)
184-
local.get $0
185-
local.get $1
183+
(func $~lib/string/String.__ne (param $left i32) (param $right i32) (result i32)
184+
local.get $left
185+
local.get $right
186186
call $~lib/string/String.__eq
187187
i32.eqz
188188
)
189-
(func $NonNullable/assertNonNull<~lib/string/String> (param $0 i32)
189+
(func $NonNullable/assertNonNull<~lib/string/String> (param $t i32)
190190
i32.const 0
191191
i32.eqz
192192
drop
193-
local.get $0
193+
local.get $t
194194
i32.const 0
195195
call $~lib/string/String.__ne
196196
i32.eqz
@@ -203,12 +203,12 @@
203203
unreachable
204204
end
205205
)
206-
(func $NonNullable/safetyCheck<~lib/string/String|null> (param $0 i32)
207-
local.get $0
206+
(func $NonNullable/safetyCheck<~lib/string/String|null> (param $t i32)
207+
local.get $t
208208
i32.const 0
209209
call $~lib/string/String.__ne
210210
if
211-
local.get $0
211+
local.get $t
212212
call $NonNullable/assertNonNull<~lib/string/String>
213213
end
214214
)

0 commit comments

Comments
 (0)