github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/runtime/rt0_js_wasm.s (about)

     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  // The register RUN indicates the current run state of the program.
     9  // Possible values are:
    10  #define RUN_STARTING 0
    11  #define RUN_RUNNING 1
    12  #define RUN_PAUSED 2
    13  #define RUN_EXITED 3
    14  
    15  // _rt0_wasm_js does NOT follow the Go ABI. It has two WebAssembly parameters:
    16  // R0: argc (i32)
    17  // R1: argv (i32)
    18  TEXT _rt0_wasm_js(SB),NOSPLIT,$0
    19  	Get RUN
    20  	I32Const $RUN_STARTING
    21  	I32Eq
    22  	If
    23  		MOVD $runtime·wasmStack+m0Stack__size(SB), SP
    24  
    25  		Get SP
    26  		Get R0 // argc
    27  		I64ExtendUI32
    28  		I64Store $0
    29  
    30  		Get SP
    31  		Get R1 // argv
    32  		I64ExtendUI32
    33  		I64Store $8
    34  
    35  		I32Const $runtime·rt0_go(SB)
    36  		I32Const $16
    37  		I32ShrU
    38  		Set PC_F
    39  
    40  		I32Const $RUN_RUNNING
    41  		Set RUN
    42  	Else
    43  		Get RUN
    44  		I32Const $RUN_PAUSED
    45  		I32Eq
    46  		If
    47  			I32Const $RUN_RUNNING
    48  			Set RUN
    49  		Else
    50  			Unreachable
    51  		End
    52  	End
    53  
    54  // Call the function for the current PC_F. Repeat until RUN != 0 indicates pause or exit.
    55  // The WebAssembly stack may unwind, e.g. when switching goroutines.
    56  // The Go stack on the linear memory is then used to jump to the correct functions
    57  // with this loop, without having to restore the full WebAssembly stack.
    58  loop:
    59  	Loop
    60  		Get PC_F
    61  		CallIndirect $0
    62  		Drop
    63  
    64  		Get RUN
    65  		I32Const $RUN_RUNNING
    66  		I32Eq
    67  		BrIf loop
    68  	End
    69  
    70  	Return
    71  
    72  TEXT runtime·pause(SB), NOSPLIT, $0
    73  	I32Const $RUN_PAUSED
    74  	Set RUN
    75  	RETUNWIND
    76  
    77  TEXT runtime·exit(SB), NOSPLIT, $0-4
    78  	Call runtime·wasmExit(SB)
    79  	Drop
    80  	I32Const $RUN_EXITED
    81  	Set RUN
    82  	RETUNWIND
    83  
    84  TEXT _rt0_wasm_js_lib(SB),NOSPLIT,$0
    85  	UNDEF