@@ -21,7 +21,7 @@ use boolinator::*;
21
21
22
22
use std:: collections:: HashMap ;
23
23
24
- struct EncodeContext < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , Heap : HeapManager > {
24
+ struct EncodeContext < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , ' i , Heap : HeapManager > {
25
25
// Local to this function
26
26
return_type : Option < ir:: VarType > ,
27
27
@@ -38,19 +38,20 @@ struct EncodeContext<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, Heap: HeapManager> {
38
38
stackptr : wasmgen:: GlobalIdx ,
39
39
memidx : wasmgen:: MemIdx ,
40
40
thunk_map : & ' f HashMap < Box < [ ir:: OverloadEntry ] > , u32 > , // map from overloads to elemidx
41
- heap : & ' g Heap ,
42
- string_pool : & ' h ShiftedStringPool ,
41
+ appl_data_encoder : & ' g HashMap < ir:: SourceLocation , u32 > , // map from source location to the location in memory of the args
42
+ heap : & ' h Heap ,
43
+ string_pool : & ' i ShiftedStringPool ,
43
44
error_func : wasmgen:: FuncIdx , // imported function to call to error out (e.g. runtime type errors)
44
45
options : Options , // Compilation options (it implements Copy)
45
46
}
46
47
47
48
// Have to implement Copy and Clone manually, because #[derive(Copy, Clone)] doesn't work for generic types like Heap
48
- impl < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , Heap : HeapManager > Copy
49
- for EncodeContext < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , Heap >
49
+ impl < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , ' i , Heap : HeapManager > Copy
50
+ for EncodeContext < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , ' i , Heap >
50
51
{
51
52
}
52
- impl < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , Heap : HeapManager > Clone
53
- for EncodeContext < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , Heap >
53
+ impl < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , ' i , Heap : HeapManager > Clone
54
+ for EncodeContext < ' a , ' b , ' c , ' d , ' e , ' f , ' g , ' h , ' i , Heap >
54
55
{
55
56
fn clone ( & self ) -> Self {
56
57
* self
@@ -106,14 +107,14 @@ pub fn encode_funcs<'a, Heap: HeapManager>(
106
107
globalidx_stackptr : wasmgen:: GlobalIdx ,
107
108
memidx : wasmgen:: MemIdx ,
108
109
thunk_sv : SearchableVec < Box < [ ir:: OverloadEntry ] > > ,
110
+ appl_data_encoder : HashMap < ir:: SourceLocation , u32 > ,
109
111
heap : & Heap ,
110
112
string_pool : & ShiftedStringPool ,
111
113
error_func : wasmgen:: FuncIdx ,
112
114
options : Options ,
113
115
wasm_module : & mut wasmgen:: WasmModule ,
114
116
) {
115
117
struct WasmRegistry {
116
- typeidx : wasmgen:: TypeIdx ,
117
118
funcidx : wasmgen:: FuncIdx ,
118
119
wasm_param_map : Box < [ wasmgen:: LocalIdx ] > , // map converts wasm_param_map index to actual wasm param index
119
120
param_map : Box < [ usize ] > , // map converts ir param index to wasm_param_map index
@@ -140,11 +141,10 @@ pub fn encode_funcs<'a, Heap: HeapManager>(
140
141
wasm_param_valtypes,
141
142
encode_result ( ir_func. result , options. wasm_multi_value ) ,
142
143
) ;
143
- let ( wasm_typeidx , wasm_funcidx) = wasm_module. register_func ( & wasm_functype) ;
144
+ let ( _ , wasm_funcidx) = wasm_module. register_func ( & wasm_functype) ;
144
145
let code_builder = wasmgen:: CodeBuilder :: new ( wasm_functype) ;
145
146
(
146
147
WasmRegistry {
147
- typeidx : wasm_typeidx,
148
148
funcidx : wasm_funcidx,
149
149
wasm_param_map : wasm_param_map,
150
150
param_map : param_map,
@@ -194,6 +194,7 @@ pub fn encode_funcs<'a, Heap: HeapManager>(
194
194
memidx : memidx,
195
195
heap : heap,
196
196
thunk_map : & new_thunk_map,
197
+ appl_data_encoder : & appl_data_encoder,
197
198
string_pool : string_pool,
198
199
error_func : error_func,
199
200
options : options,
@@ -240,6 +241,7 @@ pub fn encode_funcs<'a, Heap: HeapManager>(
240
241
memidx : memidx,
241
242
heap : heap,
242
243
thunk_map : & new_thunk_map,
244
+ appl_data_encoder : & appl_data_encoder,
243
245
string_pool : string_pool,
244
246
error_func : error_func,
245
247
options : options,
@@ -757,9 +759,21 @@ fn encode_expr<H: HeapManager>(
757
759
encode_prim_inst ( expr. vartype , * prim_inst, args, ctx, mutctx, expr_builder) ;
758
760
true
759
761
}
760
- ir:: ExprKind :: Appl { func, args } => {
762
+ ir:: ExprKind :: Appl {
763
+ func,
764
+ args,
765
+ location,
766
+ } => {
761
767
// encodes an indirect function call
762
- encode_appl ( expr. vartype , func, args, ctx, mutctx, expr_builder) ;
768
+ encode_appl (
769
+ expr. vartype ,
770
+ func,
771
+ args,
772
+ location,
773
+ ctx,
774
+ mutctx,
775
+ expr_builder,
776
+ ) ;
763
777
true
764
778
}
765
779
ir:: ExprKind :: DirectAppl { funcidx, args } => {
@@ -1227,6 +1241,7 @@ fn encode_appl<H: HeapManager>(
1227
1241
return_type : Option < ir:: VarType > ,
1228
1242
func_expr : & ir:: Expr ,
1229
1243
args : & [ ir:: Expr ] ,
1244
+ location : & ir:: SourceLocation ,
1230
1245
ctx : EncodeContext < H > ,
1231
1246
mutctx : & mut MutContext ,
1232
1247
expr_builder : & mut wasmgen:: ExprBuilder ,
@@ -1269,10 +1284,9 @@ fn encode_appl<H: HeapManager>(
1269
1284
expr_builder,
1270
1285
) ;
1271
1286
1272
- // TODO: encode the proper caller id
1273
- // for now just use zero
1287
+ // encode the proper caller id (which is the memory location of the SourceLocation)
1274
1288
// net wasm stack: [] -> [i32(callerid)]
1275
- expr_builder. i32_const ( 0 ) ;
1289
+ expr_builder. i32_const ( * ctx . appl_data_encoder . get ( location ) . unwrap ( ) as i32 ) ;
1276
1290
1277
1291
// push the tableidx onto the stack
1278
1292
// net wasm stack: [] -> [tableidx]
@@ -1921,14 +1935,23 @@ fn encode_thunk<H: HeapManager>(
1921
1935
sourceloc_ref : wasmgen:: LocalIdx ,
1922
1936
expr_builder : & mut wasmgen:: ExprBuilder ,
1923
1937
) {
1938
+ // we need to fetch the actual source location from the static memory to set as arguments of the error_func
1924
1939
expr_builder. i32_const ( ir:: error:: ERROR_CODE_FUNCTION_PARAM_TYPE as i32 ) ;
1925
1940
expr_builder. i32_const ( 0 ) ;
1926
- expr_builder. i32_const ( 0 ) ; // todo!(add actual source location)
1927
- expr_builder. i32_const ( 0 ) ;
1928
- expr_builder. i32_const ( 0 ) ;
1929
- expr_builder. i32_const ( 0 ) ;
1930
- expr_builder. i32_const ( 0 ) ;
1941
+ // the source location is 5 of u32s
1942
+ expr_builder. local_get ( sourceloc_ref) ;
1943
+ expr_builder. i32_load ( wasmgen:: MemArg :: new4 ( 0 ) ) ;
1944
+ expr_builder. local_get ( sourceloc_ref) ;
1945
+ expr_builder. i32_load ( wasmgen:: MemArg :: new4 ( 4 ) ) ;
1946
+ expr_builder. local_get ( sourceloc_ref) ;
1947
+ expr_builder. i32_load ( wasmgen:: MemArg :: new4 ( 8 ) ) ;
1948
+ expr_builder. local_get ( sourceloc_ref) ;
1949
+ expr_builder. i32_load ( wasmgen:: MemArg :: new4 ( 12 ) ) ;
1950
+ expr_builder. local_get ( sourceloc_ref) ;
1951
+ expr_builder. i32_load ( wasmgen:: MemArg :: new4 ( 16 ) ) ;
1952
+ // call the error_func with 7 arguments
1931
1953
expr_builder. call ( error_func) ;
1954
+ // the error func is noreturn, so we want to tell wasm that it is indeed noreturn
1932
1955
expr_builder. unreachable ( ) ;
1933
1956
}
1934
1957
}
0 commit comments