github.com/primecitizens/pcz/std@v0.2.1/rt0/rt0_wasm.s (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2018 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  //go:build !noos && wasm
     9  
    10  #include "textflag.h"
    11  
    12  #ifdef GOOS_wasip1
    13  
    14  // NOTE: the name is not "rt0" because the go1.21 linker hard coded 
    15  // "_rt0_wasm_wasip1" being exported as "_start".
    16  //
    17  // See ${GOROOT}/src/cmd/link/internal/wasm/asm.go#func:writeExportSec
    18  TEXT _rt0_wasm_wasip1(SB),NOSPLIT|NOFRAME|TOPFRAME,$0
    19  	// See wasm_export_resume
    20  	MOVD $(4096+8192-(8/* argc */+8 /* argv */ + 8 /* LR */)), SP
    21  
    22  	I32Const $0
    23  	Call ·rt0(SB)
    24  	Drop
    25  	Return
    26  
    27  #endif
    28  
    29  #ifdef GOOS_js
    30  
    31  // rt0 for js/wasm only exists to mark the exported functions as alive.
    32  TEXT rt0(SB),NOSPLIT|NOFRAME|TOPFRAME,$0
    33  	I32Const $wasm_export_run(SB)
    34  	Drop
    35  	I32Const $wasm_export_resume(SB)
    36  	Drop
    37  	I32Const $wasm_export_getsp(SB)
    38  	Drop
    39  
    40  // wasm_export_resume() gets called from JavaScript.
    41  //
    42  // In the official go std, it resumes the execution of Go code until it
    43  // needs to wait for an event.
    44  //
    45  // But in pcz, we use it as the entrypoint of the program.
    46  //
    47  // NOTE: the symbol name is known to the go tool link, only change when the
    48  // linker changes its name or signature.
    49  TEXT wasm_export_resume(SB),NOSPLIT|NOFRAME|TOPFRAME,$0
    50  	// go linker reserves 4096 bytes for zero page, then 8192 bytes for wasm_exec.js
    51  	// to set argv and envv, but we don't do that, use the reserved 8192 bytes as
    52  	// the initial stack.
    53  	//
    54  	// see ${GOROOT}/src/cmd/link/internal/ld/data.go#const:wasmMinDataAddr
    55  	// 
    56  	// NOTE: argc (uint32) is 8 bytes in wasm.
    57  	MOVD $(4096+8192-(8/* argc */+8 /* argv */ + 8 /* LR */)), SP
    58  
    59  	I32Const $0
    60  	Call ·rt0(SB)
    61  	Drop
    62  	Return
    63  
    64  #endif