Skip to content

Commit 5680660

Browse files
committed
gh-121700 Shorten Emscripten trampolines by a little
I found out about the `if` instruction which lets us have slightly shorter code here.
1 parent bb5ec6e commit 5680660

File tree

1 file changed

+21
-37
lines changed

1 file changed

+21
-37
lines changed

Python/emscripten_trampoline.c

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ EM_JS(CountArgsFunc, _PyEM_GetCountArgsPtr, (), {
3535
// (type $type1 (func (param i32) (result i32)))
3636
// (type $type2 (func (param i32 i32) (result i32)))
3737
// (type $type3 (func (param i32 i32 i32) (result i32)))
38-
// (type $blocktype (func (param i32) (result)))
38+
// (type $blocktype (func (param) (result)))
3939
// (table $funcs (import "e" "t") 0 funcref)
4040
// (export "f" (func $f))
4141
// (func $f (param $fptr i32) (result i32)
@@ -44,36 +44,28 @@ EM_JS(CountArgsFunc, _PyEM_GetCountArgsPtr, (), {
4444
// table.get $funcs
4545
// local.tee $fref
4646
// ref.test $type3
47-
// (block $b (type $blocktype)
48-
// i32.eqz
49-
// br_if $b
47+
// if $blocktype
5048
// i32.const 3
5149
// return
52-
// )
50+
// end
5351
// local.get $fref
5452
// ref.test $type2
55-
// (block $b (type $blocktype)
56-
// i32.eqz
57-
// br_if $b
53+
// if $blocktype
5854
// i32.const 2
5955
// return
60-
// )
56+
// end
6157
// local.get $fref
6258
// ref.test $type1
63-
// (block $b (type $blocktype)
64-
// i32.eqz
65-
// br_if $b
59+
// if $blocktype
6660
// i32.const 1
6761
// return
68-
// )
62+
// end
6963
// local.get $fref
7064
// ref.test $type0
71-
// (block $b (type $blocktype)
72-
// i32.eqz
73-
// br_if $b
65+
// if $blocktype
7466
// i32.const 0
7567
// return
76-
// )
68+
// end
7769
// i32.const -1
7870
// )
7971
// )
@@ -88,13 +80,13 @@ function getPyEMCountArgsPtr() {
8880
const code = new Uint8Array([
8981
0x00, 0x61, 0x73, 0x6d, // \0asm magic number
9082
0x01, 0x00, 0x00, 0x00, // version 1
91-
0x01, 0x1b, // Type section, body is 0x1b bytes
83+
0x01, 0x1a, // Type section, body is 0x1a bytes
9284
0x05, // 6 entries
93-
0x60, 0x00, 0x01, 0x7f, // (type $type0 (func (param) (result i32)))
94-
0x60, 0x01, 0x7f, 0x01, 0x7f, // (type $type1 (func (param i32) (result i32)))
95-
0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, // (type $type2 (func (param i32 i32) (result i32)))
96-
0x60, 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, // (type $type3 (func (param i32 i32 i32) (result i32)))
97-
0x60, 0x01, 0x7f, 0x00, // (type $blocktype (func (param i32) (result)))
85+
0x60, 0x00, 0x01, 0x7f, // (type $type0 (func (param) (result i32)))
86+
0x60, 0x01, 0x7f, 0x01, 0x7f, // (type $type1 (func (param i32) (result i32)))
87+
0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, // (type $type2 (func (param i32 i32) (result i32)))
88+
0x60, 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, // (type $type3 (func (param i32 i32 i32) (result i32)))
89+
0x60, 0x00, 0x00, // (type $blocktype (func (param) (result)))
9890
0x02, 0x09, // Import section, 0x9 byte body
9991
0x01, // 1 import (table $funcs (import "e" "t") 0 funcref)
10092
0x01, 0x65, // "e"
@@ -110,44 +102,36 @@ function getPyEMCountArgsPtr() {
110102
0x00, // a function
111103
0x00, // at index 0
112104

113-
0x0a, 0x44, // Code section,
114-
0x01, 0x42, // one entry of length 50
105+
0x0a, 56, // Code section,
106+
0x01, 54, // one entry of length 54
115107
0x01, 0x01, 0x70, // one local of type funcref
116108
// Body of the function
117109
0x20, 0x00, // local.get $fptr
118110
0x25, 0x00, // table.get $funcs
119111
0x22, 0x01, // local.tee $fref
120112
0xfb, 0x14, 0x03, // ref.test $type3
121-
0x02, 0x04, // block $b (type $blocktype)
122-
0x45, // i32.eqz
123-
0x0d, 0x00, // br_if $b
113+
0x04, 0x04, // if (type $blocktype)
124114
0x41, 0x03, // i32.const 3
125115
0x0f, // return
126116
0x0b, // end block
127117

128118
0x20, 0x01, // local.get $fref
129119
0xfb, 0x14, 0x02, // ref.test $type2
130-
0x02, 0x04, // block $b (type $blocktype)
131-
0x45, // i32.eqz
132-
0x0d, 0x00, // br_if $b
120+
0x04, 0x04, // if (type $blocktype)
133121
0x41, 0x02, // i32.const 2
134122
0x0f, // return
135123
0x0b, // end block
136124

137125
0x20, 0x01, // local.get $fref
138126
0xfb, 0x14, 0x01, // ref.test $type1
139-
0x02, 0x04, // block $b (type $blocktype)
140-
0x45, // i32.eqz
141-
0x0d, 0x00, // br_if $b
127+
0x04, 0x04, // if (type $blocktype)
142128
0x41, 0x01, // i32.const 1
143129
0x0f, // return
144130
0x0b, // end block
145131

146132
0x20, 0x01, // local.get $fref
147133
0xfb, 0x14, 0x00, // ref.test $type0
148-
0x02, 0x04, // block $b (type $blocktype)
149-
0x45, // i32.eqz
150-
0x0d, 0x00, // br_if $b
134+
0x04, 0x04, // if (type $blocktype)
151135
0x41, 0x00, // i32.const 0
152136
0x0f, // return
153137
0x0b, // end block

0 commit comments

Comments
 (0)