@@ -2815,6 +2815,14 @@ let do_backpatches = (wasm_mod, env, backpatches) => {
28152815 );
28162816};
28172817
2818+ let current_function = ref (None );
2819+ let get_current_function = () => {
2820+ switch (current_function^ ) {
2821+ | Some (func ) => func
2822+ | None => failwith ("No current function set" )
2823+ };
2824+ };
2825+ let set_current_function = func => current_function := Some (func);
28182826let loop_stack = ref ([] : list ((string , string )));
28192827
28202828let rec compile_store = (wasm_mod, env, binds) => {
@@ -3151,6 +3159,24 @@ and compile_instr = (wasm_mod, env, instr) =>
31513159 Expression . Const . make(wasm_mod, const_void() ),
31523160 );
31533161 | MError (err , args ) => call_error_handler(wasm_mod, env, err, args)
3162+ | MReturn (value ) =>
3163+ let current_function = get_current_function() ;
3164+ let value =
3165+ Option . fold(
3166+ ~none= Expression . Const . make(wasm_mod, const_void() ),
3167+ ~some= compile_instr(wasm_mod, env),
3168+ value,
3169+ );
3170+ Expression . Return . make(
3171+ wasm_mod,
3172+ cleanup_locals(
3173+ wasm_mod,
3174+ env,
3175+ value,
3176+ current_function. args,
3177+ current_function. return_type,
3178+ ),
3179+ );
31543180 | MArityOp (_ ) => failwith ("NYI: (compile_instr): MArityOp" )
31553181 | MTagOp (_ ) => failwith ("NYI: (compile_instr): MTagOp" )
31563182 };
@@ -3325,9 +3351,10 @@ let compile_function =
33253351 ~preamble=?,
33263352 wasm_mod,
33273353 env,
3328- {id, args, return_type, stack_size, body: body_instrs, func_loc},
3354+ {id, args, return_type, stack_size, body: body_instrs, func_loc} as func ,
33293355 ) => {
33303356 sources := [] ;
3357+ set_current_function(func);
33313358 let arity = List . length(args);
33323359 let func_name =
33333360 switch (name) {
0 commit comments