Skip to content
← Back

src/ir/wasmgc/wasmgc_wat_annotations.ghul

1
namespace IR.WasmGC is
2
use Collections.MAP
3
use Collections.SET
4
5
use Wasm.INSTRUCTION
6
7
/// What `--wat-lines` and `--wat-program-only` add to and leave out of
8
/// the text form of a module: the source position each statement's
9
/// first instruction came from, and which functions the program's own
10
/// sources compiled to. The binary module is the same either way.
11
class WASMGC_WAT_ANNOTATIONS: Wasm.WatAnnotations is
12
_positions: MAP[INSTRUCTION, string]
13
_program_functions: SET[int]
14
_local_names: MAP[int, MAP[int, string]]
15
_program_only: bool
16
17
/// - program_only: whether only the program's own functions are
18
/// printed, with none of the module's declarations
19
init(program_only: bool) is
20
_positions = MAP[INSTRUCTION, string]()
21
_program_functions = SET[int]()
22
_local_names = MAP[int, MAP[int, string]]()
23
_program_only = program_only
24
si
25
26
/// Records that `instruction` is the first a statement at
27
/// `location` was lowered to.
28
///
29
/// - instruction: the instruction, as the body holds it
30
/// - location: where the statement is in its source file
31
note_position(instruction: INSTRUCTION, location: Source.LOCATION) is
32
if !_positions.contains_key(instruction) then
33
_positions[instruction] = "{location.file_name}:{location.start_line}"
34
fi
35
si
36
37
/// Records the name of a local of a function.
38
///
39
/// - function_index: the function's index, counting imported functions
40
/// - local_index: the local's index, counting the parameters
41
/// - name: the name the source gave it
42
note_local_name(function_index: int, local_index: int, name: string) is
43
if !_local_names.contains_key(function_index) then
44
_local_names[function_index] = MAP[int, string]()
45
fi
46
47
_local_names[function_index][local_index] = name
48
si
49
50
/// Records that the function with index `index` was compiled from
51
/// the program's own sources.
52
///
53
/// - index: the function's index, counting imported functions
54
note_program_function(index: int) is
55
_program_functions.add(index)
56
si
57
58
comment_before(instruction: INSTRUCTION) -> string? is
59
let position: string mut
60
61
if _positions.try_get_value(instruction, position ref) then
62
return position
63
fi
64
65
return null
66
si
67
68
local_name(function_index: int, local_index: int) -> string? is
69
let names: MAP[int, string] mut
70
let name: string mut
71
72
if _local_names.try_get_value(function_index, names ref) /\ names.try_get_value(local_index, name ref) then
73
return name
74
fi
75
76
return null
77
si
78
79
includes_function(index: int) -> bool => !_program_only \/ _program_functions.contains(index)
80
81
includes_declarations: bool => !_program_only
82
si
83
si