Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use Wasm.MODULE | |
| 3 | use Wasm.FUNCTION | |
| 4 | use Wasm.FUNC_TYPE | |
| 5 | use Wasm.GLOBAL | |
| 6 | use Wasm.VALUE_TYPE | |
| 7 | use Wasm.NumericType | |
| 8 | use Wasm.EXPORT | |
| 9 | use Wasm.ExportKind | |
| 10 | use Wasm.NAME_MAP | |
| 11 | use Wasm.INSTRUCTION | |
| 12 | use Wasm.INDEX_INSTRUCTION | |
| 13 | use Wasm.I32_CONSTANT | |
| 14 | use Wasm.Opcode | |
| 15 | ||
| 16 | use Collections.LIST | |
| 17 | ||
| 18 | use Semantic.Symbols.Function | |
| 19 | ||
| 20 | /// The two functions a host starts a module through: `_start` runs | |
| 21 | /// the program's entry point, and `_exit_code` answers the exit | |
| 22 | /// status it left, which is zero for an entry point that returns | |
| 23 | /// nothing. | |
| 24 | class WASMGC_ENTRY is | |
| 25 | /// Adds and exports `_start` and `_exit_code` around the entry | |
| 26 | /// point. | |
| 27 | /// | |
| 28 | /// - module: the module to add them to | |
| 29 | /// - entry: the program's entry point | |
| 30 | /// - index: the entry point's function index | |
| 31 | /// - arguments_index: the index of the function answering the | |
| 32 | /// command-line arguments, which `_start` passes to an entry point | |
| 33 | /// that takes them | |
| 34 | /// - names: the function names, which the two are added to | |
| 35 | add(module: MODULE, entry: Function, index: int, arguments_index: int?, names: NAME_MAP) static is | |
| 36 | let i32 = VALUE_TYPE.of_numeric(NumericType.I32) | |
| 37 | let none: VALUE_TYPE[] = [] | |
| 38 | ||
| 39 | let exit_code = | |
| 40 | module.add_global( | |
| 41 | GLOBAL(i32, true, LIST[INSTRUCTION]([I32_CONSTANT(0)]))) | |
| 42 | ||
| 43 | let start_body = LIST[INSTRUCTION]() | |
| 44 | ||
| 45 | if entry.arguments.count > 0 then | |
| 46 | if let arguments = arguments_index then | |
| 47 | start_body.add(INDEX_INSTRUCTION(Opcode.CALL, arguments)) | |
| 48 | fi | |
| 49 | fi | |
| 50 | ||
| 51 | start_body.add(INDEX_INSTRUCTION(Opcode.CALL, index)) | |
| 52 | ||
| 53 | if !WASMGC_TYPES.is_void(entry.return_type) then | |
| 54 | start_body.add(INDEX_INSTRUCTION(Opcode.GLOBAL_SET, exit_code)) | |
| 55 | fi | |
| 56 | ||
| 57 | let start = | |
| 58 | module.add_function( | |
| 59 | FUNCTION( | |
| 60 | module.add_type(FUNC_TYPE(none, none)), | |
| 61 | none, | |
| 62 | start_body)) | |
| 63 | ||
| 64 | let exit_code_body = LIST[INSTRUCTION]() | |
| 65 | ||
| 66 | exit_code_body.add(INDEX_INSTRUCTION(Opcode.GLOBAL_GET, exit_code)) | |
| 67 | ||
| 68 | let exit_code_function = | |
| 69 | module.add_function( | |
| 70 | FUNCTION( | |
| 71 | module.add_type(FUNC_TYPE(none, [i32])), | |
| 72 | none, | |
| 73 | exit_code_body)) | |
| 74 | ||
| 75 | module.add_export(EXPORT("_start", ExportKind.FUNCTION, start)) | |
| 76 | module.add_export(EXPORT("_exit_code", ExportKind.FUNCTION, exit_code_function)) | |
| 77 | ||
| 78 | names.add(start, "_start") | |
| 79 | names.add(exit_code_function, "_exit_code") | |
| 80 | si | |
| 81 | si | |
| 82 | si |