Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace IR.Emitter is | |
| 4 | use System.Reflection.Metadata.TypeDefinitionHandle | |
| 5 | use System.Reflection.Metadata.MethodDefinitionHandle | |
| 6 | use System.Reflection.Metadata.FieldDefinitionHandle | |
| 7 | ||
| 8 | use Semantic.Symbols.Symbol | |
| 9 | use Semantic.Symbols.Classy | |
| 10 | use Semantic.Symbols.Function | |
| 11 | use Semantic.Symbols.Field | |
| 12 | ||
| 13 | // The non-generic framework members an iterable or iterator type | |
| 14 | // has to answer for, which its generic ones derive from. Neither | |
| 15 | // is a declaration the symbol table holds, so like `FrameMember` | |
| 16 | // each is named by which member it is. | |
| 17 | enum BoilerplateMember is | |
| 18 | ENUMERABLE_GET_ENUMERATOR, | |
| 19 | ENUMERATOR_GET_CURRENT, | |
| 20 | si | |
| 21 | ||
| 22 | // Every metadata identity the emitter has assigned to a symbol, one | |
| 23 | // map per table. | |
| 24 | // | |
| 25 | // These are deliberately not fields on the symbols themselves. | |
| 26 | // `specialize` clones symbols, so a row number stored on a symbol | |
| 27 | // would be copied onto every specialization and silently name the | |
| 28 | // wrong row; a map forces each table to state which identity it | |
| 29 | // keys on. Definitions key on the symbol, because a definition is | |
| 30 | // never specialized in the assembly that emits it. References key | |
| 31 | // on `root_specialized_from`, because the format expects one | |
| 32 | // TypeRef / MemberRef per open definition, with instantiations | |
| 33 | // expressed as TypeSpec / MethodSpec built on top of it. | |
| 34 | class SRM_HANDLES is | |
| 35 | _type_defs: Collections.MAP[Classy, TypeDefinitionHandle] | |
| 36 | _method_defs: Collections.MAP[Function, MethodDefinitionHandle] | |
| 37 | _field_defs: Collections.MAP[Field, FieldDefinitionHandle] | |
| 38 | ||
| 39 | // Offsets into the method body stream, deposited by the tree | |
| 40 | // walk as each body is encoded and read back when the owning | |
| 41 | // MethodDef row is written. A function with no entry has no | |
| 42 | // body, which is what an abstract or bodyless member is. | |
| 43 | _bodies: Collections.MAP[Function, int] | |
| 44 | ||
| 45 | // `@IL.output` ranges per method, set as each body is emitted and | |
| 46 | // read when the method's attribute is written. | |
| 47 | _il_outputs: Collections.MAP[Function, Collections.List[SRM_IL_OUTPUT_RANGE]] | |
| 48 | ||
| 49 | // A unit variant carries one interned instance in a static | |
| 50 | // field, initialized by a static constructor. Neither is | |
| 51 | // declared: the back end synthesises both, so they are keyed on | |
| 52 | // the variant they belong to rather than on a symbol of their | |
| 53 | // own. | |
| 54 | _unit_variant_instances: Collections.MAP[Classy, FieldDefinitionHandle] | |
| 55 | _unit_variant_initializers: Collections.MAP[Classy, MethodDefinitionHandle] | |
| 56 | _unit_variant_initializer_bodies: Collections.MAP[Classy, int] | |
| 57 | ||
| 58 | // A state-machine frame's own members, which the lowering | |
| 59 | // synthesises rather than declares. Keyed on the frame and which | |
| 60 | // member it is, for the same reason the unit variant's are: the | |
| 61 | // member has no symbol of its own to key on. | |
| 62 | _frame_members: Collections.MAP[(frame: Classy, member: FrameMember), MethodDefinitionHandle] | |
| 63 | _frame_member_bodies: Collections.MAP[(frame: Classy, member: FrameMember), int] | |
| 64 | ||
| 65 | // The sequence the rows above were assigned in, so the pass | |
| 66 | // that writes them replays the pass that numbered them. | |
| 67 | emission_plan: SRM_EMISSION_PLAN | |
| 68 | ||
| 69 | init() is | |
| 70 | emission_plan = SRM_EMISSION_PLAN() | |
| 71 | ||
| 72 | _type_defs = Collections.MAP[Classy, TypeDefinitionHandle]() | |
| 73 | _method_defs = Collections.MAP[Function, MethodDefinitionHandle]() | |
| 74 | _field_defs = Collections.MAP[Field, FieldDefinitionHandle]() | |
| 75 | _bodies = Collections.MAP[Function, int]() | |
| 76 | _il_outputs = | |
| 77 | Collections.MAP[Function, Collections.List[SRM_IL_OUTPUT_RANGE]]() | |
| 78 | _unit_variant_instances = Collections.MAP[Classy, FieldDefinitionHandle]() | |
| 79 | _unit_variant_initializers = Collections.MAP[Classy, MethodDefinitionHandle]() | |
| 80 | _unit_variant_initializer_bodies = Collections.MAP[Classy, int]() | |
| 81 | _frame_members = Collections.MAP[(frame: Classy, member: FrameMember), MethodDefinitionHandle]() | |
| 82 | _frame_member_bodies = Collections.MAP[(frame: Classy, member: FrameMember), int]() | |
| 83 | si | |
| 84 | ||
| 85 | set_frame_member(frame: Classy, member: FrameMember, handle: MethodDefinitionHandle) is | |
| 86 | _frame_members[(frame = frame, member = member)] = handle | |
| 87 | si | |
| 88 | ||
| 89 | frame_member(frame: Classy, member: FrameMember) -> MethodDefinitionHandle? => | |
| 90 | if _frame_members.contains_key((frame = frame, member = member)) then | |
| 91 | _frame_members[(frame = frame, member = member)] | |
| 92 | else | |
| 93 | null | |
| 94 | fi | |
| 95 | ||
| 96 | set_frame_member_body(frame: Classy, member: FrameMember, offset: int) is | |
| 97 | _frame_member_bodies[(frame = frame, member = member)] = offset | |
| 98 | si | |
| 99 | ||
| 100 | frame_member_body(frame: Classy, member: FrameMember) -> int? => | |
| 101 | if _frame_member_bodies.contains_key((frame = frame, member = member)) then | |
| 102 | _frame_member_bodies[(frame = frame, member = member)] | |
| 103 | else | |
| 104 | null | |
| 105 | fi | |
| 106 | ||
| 107 | set_type_definition(symbol: Classy, handle: TypeDefinitionHandle) is | |
| 108 | _type_defs[symbol] = handle | |
| 109 | si | |
| 110 | ||
| 111 | set_method_definition(symbol: Function, handle: MethodDefinitionHandle) is | |
| 112 | _method_defs[symbol] = handle | |
| 113 | si | |
| 114 | ||
| 115 | set_field_definition(symbol: Field, handle: FieldDefinitionHandle) is | |
| 116 | _field_defs[symbol] = handle | |
| 117 | si | |
| 118 | ||
| 119 | // Type and method lookups take the root symbol, so a call | |
| 120 | // through a specialization still finds the definition it was | |
| 121 | // specialized from. | |
| 122 | type_definition(symbol: Classy) -> TypeDefinitionHandle? is | |
| 123 | let root = cast Classy?(symbol.root_specialized_from) | |
| 124 | ||
| 125 | if root? /\ _type_defs.contains_key(root) then | |
| 126 | return _type_defs[root] | |
| 127 | fi | |
| 128 | ||
| 129 | return null | |
| 130 | si | |
| 131 | ||
| 132 | method_definition(symbol: Function) -> MethodDefinitionHandle? is | |
| 133 | let root = cast Function?(symbol.root_specialized_from) | |
| 134 | ||
| 135 | if root? /\ _method_defs.contains_key(root) then | |
| 136 | return _method_defs[root] | |
| 137 | fi | |
| 138 | ||
| 139 | return null | |
| 140 | si | |
| 141 | ||
| 142 | field_definition(symbol: Field) -> FieldDefinitionHandle? is | |
| 143 | let root = cast Field?(symbol.root_specialized_from) | |
| 144 | ||
| 145 | if root? /\ _field_defs.contains_key(root) then | |
| 146 | return _field_defs[root] | |
| 147 | fi | |
| 148 | ||
| 149 | return null | |
| 150 | si | |
| 151 | ||
| 152 | set_unit_variant_instance(variant: Classy, handle: FieldDefinitionHandle) is | |
| 153 | _unit_variant_instances[variant] = handle | |
| 154 | si | |
| 155 | ||
| 156 | set_unit_variant_initializer(variant: Classy, handle: MethodDefinitionHandle) is | |
| 157 | _unit_variant_initializers[variant] = handle | |
| 158 | si | |
| 159 | ||
| 160 | set_unit_variant_initializer_body(variant: Classy, offset: int) is | |
| 161 | _unit_variant_initializer_bodies[variant] = offset | |
| 162 | si | |
| 163 | ||
| 164 | unit_variant_instance(variant: Classy) -> FieldDefinitionHandle? is | |
| 165 | let root = cast Classy?(variant.root_specialized_from) | |
| 166 | ||
| 167 | if root? /\ _unit_variant_instances.contains_key(root) then | |
| 168 | return _unit_variant_instances[root] | |
| 169 | fi | |
| 170 | ||
| 171 | return null | |
| 172 | si | |
| 173 | ||
| 174 | unit_variant_initializer_body(variant: Classy) -> int? => | |
| 175 | if _unit_variant_initializer_bodies.contains_key(variant) then | |
| 176 | _unit_variant_initializer_bodies[variant] | |
| 177 | else | |
| 178 | null | |
| 179 | fi | |
| 180 | ||
| 181 | set_body_offset(symbol: Function, offset: int) is | |
| 182 | _bodies[symbol] = offset | |
| 183 | si | |
| 184 | ||
| 185 | body_offset(symbol: Function) -> int? => | |
| 186 | if _bodies.contains_key(symbol) then _bodies[symbol] else null fi | |
| 187 | ||
| 188 | // `@IL.output` ranges captured while the method's body was emitted, | |
| 189 | // carried to the phase-3 structure walk that writes the method's | |
| 190 | // attribute. Null/empty is stored as absent so the attribute is | |
| 191 | // written only for methods that actually mark something. | |
| 192 | set_il_outputs( | |
| 193 | symbol: Function, | |
| 194 | outputs: Collections.List[SRM_IL_OUTPUT_RANGE]? | |
| 195 | ) is | |
| 196 | if outputs? /\ outputs.count > 0 then | |
| 197 | _il_outputs[symbol] = outputs | |
| 198 | fi | |
| 199 | si | |
| 200 | ||
| 201 | il_outputs(symbol: Function) -> Collections.List[SRM_IL_OUTPUT_RANGE]? => | |
| 202 | if _il_outputs.contains_key(symbol) then _il_outputs[symbol] else null fi | |
| 203 | si | |
| 204 | si |