Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use IR.Program | |
| 3 | ||
| 4 | use Wasm.MODULE | |
| 5 | use Wasm.FUNCTION | |
| 6 | ||
| 7 | use Collections.MAP | |
| 8 | ||
| 9 | use Semantic.Symbols.Function | |
| 10 | ||
| 11 | /// The function indices of a module: the program's function worklist, | |
| 12 | /// numbered from after the imports, with the bodies compiled for each | |
| 13 | /// and the functions the target generates for itself. | |
| 14 | class WASMGC_FUNCTION_NUMBERING is | |
| 15 | _first_index: int | |
| 16 | _worklist: FUNCTION_WORKLIST | |
| 17 | ||
| 18 | _reserved: MAP[int, () -> FUNCTION] | |
| 19 | _placed: MAP[int, FUNCTION] | |
| 20 | ||
| 21 | /// Numbers functions from `first_index` on. | |
| 22 | /// | |
| 23 | /// - first_index: the index the first function takes, after the | |
| 24 | /// imports | |
| 25 | /// - has_body: whether a function has a body the module compiles | |
| 26 | init(first_index: int, has_body: (Function) -> bool) is | |
| 27 | _first_index = first_index | |
| 28 | _worklist = FUNCTION_WORKLIST(has_body) | |
| 29 | ||
| 30 | _reserved = MAP[int, () -> FUNCTION]() | |
| 31 | _placed = MAP[int, FUNCTION]() | |
| 32 | si | |
| 33 | ||
| 34 | /// The instances numbered, as the program's worklist. | |
| 35 | worklist: FUNCTION_WORKLIST => _worklist | |
| 36 | ||
| 37 | /// How many indices have been given out. | |
| 38 | count: int => _worklist.count | |
| 39 | ||
| 40 | /// The index of `function`, numbering it the first time it is asked | |
| 41 | /// for, or null where it has no body to compile. | |
| 42 | index_of(function: Function) -> int? => index_of(FUNCTION_INSTANCE.of(function)) | |
| 43 | ||
| 44 | /// The index of `instance`, numbering it the first time it is asked | |
| 45 | /// for, or null where its definition has no body to compile. | |
| 46 | index_of(instance: FUNCTION_INSTANCE) -> int? => _index(_worklist.add(instance)) | |
| 47 | ||
| 48 | /// The index `instance` already has, or null where nothing has | |
| 49 | /// asked for it. Unlike `index_of`, this numbers nothing. | |
| 50 | indexed(instance: FUNCTION_INSTANCE) -> int? => _index(_worklist.ordinal_of(instance)) | |
| 51 | ||
| 52 | /// The index `function`, taking no type arguments, already has. | |
| 53 | indexed(function: Function) -> int? => indexed(FUNCTION_INSTANCE.of(function)) | |
| 54 | ||
| 55 | _index(ordinal: int?) -> int? => if ordinal? then _first_index + ordinal else null fi | |
| 56 | ||
| 57 | /// Numbers a function the target generates, which `build` makes when | |
| 58 | /// the module is assembled. | |
| 59 | reserve(build: () -> FUNCTION) -> int is | |
| 60 | let index = _first_index + _worklist.reserve() | |
| 61 | ||
| 62 | _reserved[index] = build | |
| 63 | ||
| 64 | return index | |
| 65 | si | |
| 66 | ||
| 67 | /// The next numbered function not yet handed out, or null when every | |
| 68 | /// one has been. | |
| 69 | next() -> FUNCTION_INSTANCE? => _worklist.next() | |
| 70 | ||
| 71 | /// Records that every body has been compiled. | |
| 72 | seal() => _worklist.seal() | |
| 73 | ||
| 74 | /// Hands out the numbered functions again from the first, in index | |
| 75 | /// order, keeping every number. | |
| 76 | restart() => _worklist.restart() | |
| 77 | ||
| 78 | /// Records the compiled body of `instance`. | |
| 79 | place(instance: FUNCTION_INSTANCE, function: FUNCTION) is | |
| 80 | let index = indexed(instance) | |
| 81 | ||
| 82 | assert index? else "{instance} was compiled without having been numbered" | |
| 83 | ||
| 84 | _placed[index] = function | |
| 85 | si | |
| 86 | ||
| 87 | /// Adds every numbered function to `module` in index order, | |
| 88 | /// building the generated ones. Every function with a body must | |
| 89 | /// have been placed. | |
| 90 | add_to(module: MODULE) is | |
| 91 | let i mut = 0 | |
| 92 | ||
| 93 | while i < _worklist.count do | |
| 94 | let index = _first_index + i | |
| 95 | ||
| 96 | let build: () -> FUNCTION mut | |
| 97 | ||
| 98 | let function = | |
| 99 | if _reserved.try_get_value(index, build ref) then | |
| 100 | build() | |
| 101 | else | |
| 102 | let placed: FUNCTION mut | |
| 103 | ||
| 104 | assert _placed.try_get_value(index, placed ref) else "function {index}, {_worklist.instance_at(i)}, was numbered but never compiled" | |
| 105 | ||
| 106 | placed | |
| 107 | fi | |
| 108 | ||
| 109 | let added = module.add_function(function) | |
| 110 | ||
| 111 | assert added == index else "function {index} added at {added}" | |
| 112 | ||
| 113 | i = i + 1 | |
| 114 | od | |
| 115 | si | |
| 116 | si | |
| 117 | si |