Text file src/runtime/rt0_js_wasm.s

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include "go_asm.h"
     6  #include "textflag.h"
     7  
     8  // _rt0_wasm_js is not used itself. It only exists to mark the exported functions as alive.
     9  TEXT _rt0_wasm_js(SB),NOSPLIT,$0
    10  	I32Const $wasm_export_run(SB)
    11  	Drop
    12  	I32Const $wasm_export_resume(SB)
    13  	Drop
    14  	I32Const $wasm_export_getsp(SB)
    15  	Drop
    16  
    17  // wasm_export_run gets called from JavaScript. It initializes the Go runtime and executes Go code until it needs
    18  // to wait for an event. It does NOT follow the Go ABI. It has two WebAssembly parameters:
    19  // R0: argc (i32)
    20  // R1: argv (i32)
    21  TEXT wasm_export_run(SB),NOSPLIT,$0
    22  	MOVD $runtime·wasmStack+(m0Stack__size-16)(SB), SP
    23  
    24  	Get SP
    25  	Get R0 // argc
    26  	I64ExtendI32U
    27  	I64Store $0
    28  
    29  	Get SP
    30  	Get R1 // argv
    31  	I64ExtendI32U
    32  	I64Store $8
    33  
    34  	I32Const $0 // entry PC_B
    35  	Call runtime·rt0_go(SB)
    36  	Drop
    37  	Call wasm_pc_f_loop(SB)
    38  
    39  	Return
    40  
    41  // wasm_export_resume gets called from JavaScript. It resumes the execution of Go code until it needs to wait for
    42  // an event.
    43  TEXT wasm_export_resume(SB),NOSPLIT,$0
    44  	I32Const $0
    45  	Call runtime·handleEvent(SB)
    46  	Drop
    47  	Call wasm_pc_f_loop(SB)
    48  
    49  	Return
    50  
    51  TEXT wasm_pc_f_loop(SB),NOSPLIT,$0
    52  // Call the function for the current PC_F. Repeat until PAUSE != 0 indicates pause or exit.
    53  // The WebAssembly stack may unwind, e.g. when switching goroutines.
    54  // The Go stack on the linear memory is then used to jump to the correct functions
    55  // with this loop, without having to restore the full WebAssembly stack.
    56  // It is expected to have a pending call before entering the loop, so check PAUSE first.
    57  	Get PAUSE
    58  	I32Eqz
    59  	If
    60  	loop:
    61  		Loop
    62  			// Get PC_B & PC_F from -8(SP)
    63  			Get SP
    64  			I32Const $8
    65  			I32Sub
    66  			I32Load16U $0 // PC_B
    67  
    68  			Get SP
    69  			I32Const $8
    70  			I32Sub
    71  			I32Load16U $2 // PC_F
    72  
    73  			CallIndirect $0
    74  			Drop
    75  
    76  			Get PAUSE
    77  			I32Eqz
    78  			BrIf loop
    79  		End
    80  	End
    81  
    82  	I32Const $0
    83  	Set PAUSE
    84  
    85  	Return
    86  
    87  // wasm_export_getsp gets called from JavaScript to retrieve the SP.
    88  TEXT wasm_export_getsp(SB),NOSPLIT,$0
    89  	Get SP
    90  	Return
    91  
    92  TEXT runtime·pause(SB), NOSPLIT, $0-8
    93  	MOVD newsp+0(FP), SP
    94  	I32Const $1
    95  	Set PAUSE
    96  	RETUNWIND
    97  
    98  TEXT runtime·exit(SB), NOSPLIT, $0-4
    99  	I32Const $0
   100  	Call runtime·wasmExit(SB)
   101  	Drop
   102  	I32Const $1
   103  	Set PAUSE
   104  	RETUNWIND
   105  
   106  TEXT wasm_export_lib(SB),NOSPLIT,$0
   107  	UNDEF
   108  

View as plain text