Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use IR.Program | |
| 3 | ||
| 4 | use IO.File | |
| 5 | ||
| 6 | use Wasm.MODULE | |
| 7 | use Wasm.FUNCTION | |
| 8 | use Wasm.FUNC_TYPE | |
| 9 | use Wasm.VALUE_TYPE | |
| 10 | use Wasm.EXPORT | |
| 11 | use Wasm.ExportKind | |
| 12 | use Wasm.NAME_MAP | |
| 13 | use Wasm.NAME_SECTION | |
| 14 | use Wasm.CUSTOM_SECTION | |
| 15 | use Wasm.BINARY_WRITER | |
| 16 | use Wasm.WAT_PRINTER | |
| 17 | use Wasm.ELEMENT_SEGMENT | |
| 18 | use Wasm.ElementMode | |
| 19 | use Wasm.INSTRUCTION | |
| 20 | ||
| 21 | use Collections.LIST | |
| 22 | use Collections.MAP | |
| 23 | use Collections.SET | |
| 24 | use Collections.STACK | |
| 25 | ||
| 26 | use Semantic.Symbols.Symbol | |
| 27 | use Semantic.Symbols.Classy | |
| 28 | use Semantic.Symbols.Function | |
| 29 | ||
| 30 | use Values.Value | |
| 31 | ||
| 32 | /// Writes the program as a WebAssembly module using the GC proposal. | |
| 33 | /// | |
| 34 | /// Wasm numbers every function and type before a body can name one, | |
| 35 | /// so each body is kept as it is finished and lowered only once the | |
| 36 | /// last has been handed over, in `end_assembly`. | |
| 37 | class WASMGC_BACKEND: Backend is | |
| 38 | _bodies: LIST[(function: Function, values: LIST[Value])] | |
| 39 | _enclosing: STACK[LIST[Value]?] | |
| 40 | _current: LIST[Value]? | |
| 41 | ||
| 42 | _host_declarations: LIST[Function] | |
| 43 | ||
| 44 | // Whether the core library declares the identity hash, which the | |
| 45 | // host provides. | |
| 46 | _declares_identity_hash: bool | |
| 47 | ||
| 48 | // The state machine frames already reported, so a frame with several | |
| 49 | // members is reported once. | |
| 50 | _reported_frames: SET[Classy] | |
| 51 | ||
| 52 | // The methods generator frames have that no symbol of theirs | |
| 53 | // declares. | |
| 54 | _frame_members: LIST[(frame: Classy, method: Function)] | |
| 55 | ||
| 56 | _entry_point: Function? | |
| 57 | _module: MODULE? | |
| 58 | _wat_annotations: WASMGC_WAT_ANNOTATIONS? | |
| 59 | _want_library: bool | |
| 60 | ||
| 61 | init() is | |
| 62 | _bodies = LIST[(function: Function, values: LIST[Value])]() | |
| 63 | _host_declarations = LIST[Function]() | |
| 64 | _enclosing = STACK[LIST[Value]?]() | |
| 65 | _reported_frames = SET[Classy]() | |
| 66 | _frame_members = LIST[(frame: Classy, method: Function)]() | |
| 67 | si | |
| 68 | ||
| 69 | /// Starts a module, discarding anything kept from an earlier one. | |
| 70 | install(module_name: string, module_version: string, want_library: bool) is | |
| 71 | reset() | |
| 72 | ||
| 73 | _want_library = want_library | |
| 74 | si | |
| 75 | ||
| 76 | reset() is | |
| 77 | _bodies.clear() | |
| 78 | _host_declarations.clear() | |
| 79 | _declares_identity_hash = false | |
| 80 | _reported_frames.clear() | |
| 81 | _frame_members.clear() | |
| 82 | _enclosing.clear() | |
| 83 | _current = null | |
| 84 | _entry_point = null | |
| 85 | _module = null | |
| 86 | si | |
| 87 | ||
| 88 | begin_assembly() is si | |
| 89 | ||
| 90 | /// Numbers and lowers every kept body into the module `write` writes. | |
| 91 | end_assembly() is | |
| 92 | _module = _build_module() | |
| 93 | si | |
| 94 | ||
| 95 | /// The driver refuses resources before a wasm module is written. | |
| 96 | add_resource(name: string, content: ubyte[]) is si | |
| 97 | ||
| 98 | /// Writes the module to `path`, and its text form where the build | |
| 99 | /// asks for one. An executable also gets its JavaScript loader, | |
| 100 | /// beside it with the extension `.mjs`. | |
| 101 | write(path: string) is | |
| 102 | let module = _module | |
| 103 | ||
| 104 | if !module? then | |
| 105 | return | |
| 106 | fi | |
| 107 | ||
| 108 | File.write_all_bytes(path, BINARY_WRITER().write(module)) | |
| 109 | ||
| 110 | if !_want_library then | |
| 111 | WASMGC_LOADER.write(path) | |
| 112 | fi | |
| 113 | ||
| 114 | if let wat_path = IoC.CONTAINER.instance.build_flags.wat_output then | |
| 115 | File.write_all_text(wat_path, WAT_PRINTER().print(module, _wat_annotations)) | |
| 116 | fi | |
| 117 | si | |
| 118 | ||
| 119 | begin_body() is | |
| 120 | _enclosing.push(_current) | |
| 121 | _current = LIST[Value]() | |
| 122 | si | |
| 123 | ||
| 124 | begin_body(member: Symbol) is | |
| 125 | begin_body() | |
| 126 | si | |
| 127 | ||
| 128 | write(value: Value) is | |
| 129 | _current!.add(value) | |
| 130 | si | |
| 131 | ||
| 132 | _is_array_member(function: Function) -> bool => | |
| 133 | (function.intrinsic_operation?.starts_with("array.") ?? false) /\ function.is_instance | |
| 134 | ||
| 135 | /// Keeps the body for lowering. A function that provides an | |
| 136 | /// intrinsic has its body dropped, since a call to it lowers to the | |
| 137 | /// operation itself; one the host provides becomes an import. An | |
| 138 | /// array's members keep theirs, for a call that reaches one through | |
| 139 | /// a trait, and so does a constructor intrinsic, which is called as | |
| 140 | /// the ordinary method it is. | |
| 141 | finish_body(function: Function) is | |
| 142 | if function.intrinsic_operation =~ "object.identity_hash" then | |
| 143 | _declares_identity_hash = true | |
| 144 | fi | |
| 145 | ||
| 146 | if WASMGC_IMPORTS.is_host(function.intrinsic_operation) then | |
| 147 | _host_declarations.add(function) | |
| 148 | return | |
| 149 | elif function.intrinsic_operation? /\ !_is_array_member(function) /\ !function.intrinsic_operation.starts_with("constructor.") then | |
| 150 | return | |
| 151 | fi | |
| 152 | ||
| 153 | _bodies.add((function, _current!)) | |
| 154 | si | |
| 155 | ||
| 156 | /// Keeps a generator's or an asynchronous function's frame member | |
| 157 | /// as a method of the frame. A member no trait the frame implements | |
| 158 | /// stands for is dropped: nothing on this target calls it. Any other | |
| 159 | /// frame is reported, at the function the frame was made for, since | |
| 160 | /// the frame itself has no source location. | |
| 161 | finish_frame_member_body(frame: Classy, member: FrameMember) is | |
| 162 | if let generator: Semantic.Symbols.STATE_MACHINE_FRAME = frame then | |
| 163 | if let method = FRAME_MEMBER_METHODS.method_for(generator, member) then | |
| 164 | _frame_members.add((frame, method)) | |
| 165 | _bodies.add((method, _current!)) | |
| 166 | fi | |
| 167 | ||
| 168 | return | |
| 169 | fi | |
| 170 | ||
| 171 | if let asynchronous: Semantic.Symbols.ASYNC_STATE_MACHINE_FRAME = frame then | |
| 172 | if let method = FRAME_MEMBER_METHODS.method_for(asynchronous, member) then | |
| 173 | _frame_members.add((frame, method)) | |
| 174 | _bodies.add((method, _current!)) | |
| 175 | ||
| 176 | return | |
| 177 | fi | |
| 178 | fi | |
| 179 | ||
| 180 | if !_reported_frames.add(frame) then | |
| 181 | return | |
| 182 | fi | |
| 183 | ||
| 184 | let location = | |
| 185 | if let state_machine: Semantic.Symbols.STATE_MACHINE_FRAME_BASE = frame then | |
| 186 | state_machine.owning_function.location | |
| 187 | else | |
| 188 | frame.location | |
| 189 | fi | |
| 190 | ||
| 191 | _report(location, "a generator or asynchronous function") | |
| 192 | si | |
| 193 | ||
| 194 | /// Reports the initializer: this target generates no unions yet. | |
| 195 | finish_unit_variant_initializer(variant: Classy) is | |
| 196 | _report(variant.location, "a union") | |
| 197 | si | |
| 198 | ||
| 199 | end_body() is | |
| 200 | assert _enclosing.count > 0 else "body ended without one begun" | |
| 201 | ||
| 202 | _current = _enclosing.pop() | |
| 203 | si | |
| 204 | ||
| 205 | abandon_bodies() is | |
| 206 | _enclosing.clear() | |
| 207 | _current = null | |
| 208 | si | |
| 209 | ||
| 210 | /// Always false: this target generates no unions yet. | |
| 211 | has_unit_variant_instance(variant: Classy) -> bool => false | |
| 212 | ||
| 213 | /// The function the module starts at, run through the `_start` it | |
| 214 | /// exports. | |
| 215 | mark_entry_point(function: Function) is | |
| 216 | _entry_point = function | |
| 217 | si | |
| 218 | ||
| 219 | _report(location: Source.LOCATION, construct: string) is | |
| 220 | IoC.CONTAINER.instance.logger.error( | |
| 221 | location, | |
| 222 | "code generation for {construct} is not supported on the wasm target") | |
| 223 | si | |
| 224 | ||
| 225 | // Builds the types, declares the imports, numbers every kept | |
| 226 | // function after them, then lowers each body against those | |
| 227 | // numbers. | |
| 228 | _build_module() -> MODULE is | |
| 229 | let module = MODULE() | |
| 230 | ||
| 231 | let registry = _create_registry(module) | |
| 232 | ||
| 233 | // Discovery compiles bodies only for what they number, so it | |
| 234 | // declares the imports into a module that is thrown away: the | |
| 235 | // real module's recursion group has to come before any import's | |
| 236 | // signature. Both declare the same imports in the same order, | |
| 237 | // so the function indices agree. | |
| 238 | let message_getter = _exception_message_getter() | |
| 239 | let arguments_getter = _arguments_getter() | |
| 240 | ||
| 241 | let discovery_module = MODULE() | |
| 242 | let discovery_imports = | |
| 243 | _declare_imports( | |
| 244 | discovery_module, | |
| 245 | if registry.has_string_type then WASMGC_STRINGS(discovery_module, registry) else null fi, | |
| 246 | message_getter?) | |
| 247 | ||
| 248 | let bodies = MAP[Function, LIST[Value]]() | |
| 249 | ||
| 250 | for (function, values) in _bodies do | |
| 251 | bodies[function] = values | |
| 252 | od | |
| 253 | ||
| 254 | let numbering = WASMGC_FUNCTION_NUMBERING(discovery_imports.count, f => bodies.contains_key(f)) | |
| 255 | ||
| 256 | // An executable compiles what its entry point reaches; a library | |
| 257 | // compiles everything that is not generic, since anything can be | |
| 258 | // called from outside, and each instantiation that reaches. | |
| 259 | if let entry = _entry_point then | |
| 260 | numbering.index_of(entry) | |
| 261 | ||
| 262 | if let getter = message_getter then | |
| 263 | numbering.index_of(getter) | |
| 264 | fi | |
| 265 | ||
| 266 | if let getter = arguments_getter then | |
| 267 | numbering.index_of(getter) | |
| 268 | fi | |
| 269 | else | |
| 270 | for (function, _) in _bodies do | |
| 271 | if !FUNCTION_BODIES.is_generic(function) then | |
| 272 | numbering.index_of(function) | |
| 273 | fi | |
| 274 | od | |
| 275 | fi | |
| 276 | ||
| 277 | let bodies_compiled = LIST[Function]() | |
| 278 | ||
| 279 | for (function, _) in _bodies do | |
| 280 | bodies_compiled.add(function) | |
| 281 | od | |
| 282 | ||
| 283 | let plan = PROGRAM_PLAN(registry.class_set, numbering.worklist, registry.dispatched, registry.display_plan, bodies_compiled) | |
| 284 | ||
| 285 | registry.dispatch.reach = instance => numbering.index_of(instance) | |
| 286 | ||
| 287 | registry.discovering = true | |
| 288 | ||
| 289 | plan.discover( | |
| 290 | WASMGC_DISCOVERY( | |
| 291 | registry, | |
| 292 | (instance: FUNCTION_INSTANCE) -> void => _lower(registry, discovery_imports, null, null, numbering, instance, LIST[VALUE_TYPE]()), | |
| 293 | (location, construct) => _report(location, construct))) | |
| 294 | ||
| 295 | registry.discovering = false | |
| 296 | ||
| 297 | registry.build() | |
| 298 | ||
| 299 | for (location, construct) in registry.unsupported do | |
| 300 | _report(location, construct) | |
| 301 | od | |
| 302 | ||
| 303 | let strings = if registry.has_string_type then WASMGC_STRINGS(module, registry) else null fi | |
| 304 | let imports = _declare_imports(module, strings, message_getter?) | |
| 305 | let exceptions = WASMGC_EXCEPTIONS(module, registry) | |
| 306 | ||
| 307 | assert imports.count == discovery_imports.count else "discovery declared different imports" | |
| 308 | ||
| 309 | strings?.use_numbering(numbering) | |
| 310 | ||
| 311 | let names = NAME_MAP() | |
| 312 | ||
| 313 | let flags = IoC.CONTAINER.instance.build_flags | |
| 314 | ||
| 315 | if flags.wat_output? /\ (flags.want_wat_lines \/ flags.want_wat_program_only) then | |
| 316 | _wat_annotations = WASMGC_WAT_ANNOTATIONS(flags.want_wat_program_only) | |
| 317 | fi | |
| 318 | ||
| 319 | numbering.restart() | |
| 320 | ||
| 321 | while let instance = numbering.next() do | |
| 322 | let index = numbering.index_of(instance)! | |
| 323 | ||
| 324 | if let annotations = _wat_annotations /\ IR.PROGRAM_FUNCTIONS.contains(instance.definition) then | |
| 325 | annotations.note_program_function(index) | |
| 326 | fi | |
| 327 | ||
| 328 | numbering.place( | |
| 329 | instance, | |
| 330 | _build_function(module, registry, imports, strings, exceptions, numbering, instance)) | |
| 331 | ||
| 332 | names.add(index, instance.to_string()) | |
| 333 | od | |
| 334 | ||
| 335 | numbering.seal() | |
| 336 | ||
| 337 | // The loader describes an exception nothing caught through this, | |
| 338 | // where the program can throw one. | |
| 339 | if let getter = message_getter /\ strings? /\ exceptions.declared_tag_index? then | |
| 340 | if let getter_index = numbering.index_of(getter) then | |
| 341 | if let describe = exceptions.describe(strings, getter_index, build => numbering.reserve(build)) then | |
| 342 | module.add_export(EXPORT("_describe_exception", ExportKind.FUNCTION, describe)) | |
| 343 | fi | |
| 344 | fi | |
| 345 | fi | |
| 346 | ||
| 347 | WASMGC_DISPLAY(registry).reserve(numbering, strings) | |
| 348 | WASMGC_CLONE(registry).reserve(numbering) | |
| 349 | ||
| 350 | // The descriptors, vtables and trait table refer to what the | |
| 351 | // walk reached, and add the thunks their entries call through. | |
| 352 | registry.finish(numbering) | |
| 353 | ||
| 354 | strings?.finish() | |
| 355 | ||
| 356 | numbering.add_to(module) | |
| 357 | ||
| 358 | // A function value names its thunk with ref.func, which a | |
| 359 | // module has to declare in an element segment. | |
| 360 | let referenced = registry.closure_thunks | |
| 361 | ||
| 362 | if referenced.count > 0 then | |
| 363 | module.add_element_segment( | |
| 364 | ELEMENT_SEGMENT.of_functions(ElementMode.DECLARATIVE, 0, LIST[INSTRUCTION](), referenced.to_array())) | |
| 365 | fi | |
| 366 | ||
| 367 | if let entry = _entry_point then | |
| 368 | // `_start` passes the entry point the core library's command-line | |
| 369 | // arguments, so where the core library declares none, an entry | |
| 370 | // point that takes them cannot be started. | |
| 371 | if entry.arguments.count > 0 /\ !arguments_getter? then | |
| 372 | _report(entry.location, "an entry point that takes arguments") | |
| 373 | elif let index = numbering.index_of(entry) then | |
| 374 | let arguments_index = if let getter = arguments_getter then numbering.index_of(getter) else null fi | |
| 375 | ||
| 376 | WASMGC_ENTRY.add(module, entry, index, arguments_index, names) | |
| 377 | fi | |
| 378 | fi | |
| 379 | ||
| 380 | // The loader tells an exception the program threw from any | |
| 381 | // other error by this tag. | |
| 382 | if let tag = exceptions.declared_tag_index then | |
| 383 | module.add_export(EXPORT("_exception", ExportKind.TAG, tag)) | |
| 384 | fi | |
| 385 | ||
| 386 | module.add_custom_section( | |
| 387 | CUSTOM_SECTION("name", NAME_SECTION().with_functions(names).bytes())) | |
| 388 | ||
| 389 | return module | |
| 390 | si | |
| 391 | ||
| 392 | _declare_imports(module: MODULE, strings: WASMGC_STRINGS?, describes_exceptions: bool) -> WASMGC_IMPORTS is | |
| 393 | let imports = WASMGC_IMPORTS(module) | |
| 394 | ||
| 395 | for host in _host_declarations do | |
| 396 | if let signature = _host_signature(host) then | |
| 397 | imports.host(host.intrinsic_operation!, module.add_type(signature)) | |
| 398 | fi | |
| 399 | od | |
| 400 | ||
| 401 | if _declares_identity_hash then | |
| 402 | imports.host( | |
| 403 | WASMGC_IMPORTS.IDENTITY_HASH, | |
| 404 | module.add_type( | |
| 405 | FUNC_TYPE( | |
| 406 | [VALUE_TYPE.of_abstract(Wasm.AbstractHeapType.ANY)], | |
| 407 | [VALUE_TYPE.of_numeric(Wasm.NumericType.I32)]))) | |
| 408 | fi | |
| 409 | ||
| 410 | // Built with debug information, a program that can describe an | |
| 411 | // exception throws through the host, so the exception records the | |
| 412 | // stack it was thrown from. Otherwise the module throws it, which | |
| 413 | // costs no crossing into the host and records no stack. | |
| 414 | if describes_exceptions /\ IoC.CONTAINER.instance.build_flags.want_debug then | |
| 415 | imports.host( | |
| 416 | WASMGC_IMPORTS.THROW, | |
| 417 | module.add_type(FUNC_TYPE([VALUE_TYPE.of_abstract(Wasm.AbstractHeapType.ANY)], []))) | |
| 418 | fi | |
| 419 | ||
| 420 | // Every other import is declared here, before the defined | |
| 421 | // functions are numbered. | |
| 422 | ||
| 423 | if let present = strings /\ (_host_takes_a_string() \/ _host_answers_a_string() \/ describes_exceptions) then | |
| 424 | present.declare_imports(imports) | |
| 425 | fi | |
| 426 | ||
| 427 | imports.freeze() | |
| 428 | ||
| 429 | return imports | |
| 430 | si | |
| 431 | ||
| 432 | ||
| 433 | ||
| 434 | ||
| 435 | // The registry the classes are collected into. Their struct types | |
| 436 | // take the first type indices, so they are built before any | |
| 437 | // function's signature is added. | |
| 438 | _create_registry(module: MODULE) -> WASMGC_TYPE_REGISTRY is | |
| 439 | let container = IoC.CONTAINER.instance | |
| 440 | ||
| 441 | let object = cast Classy?(container.innate_symbol_lookup.get_object_type().symbol)! | |
| 442 | let string_type = cast Classy?(container.innate_symbol_lookup.get_string_type().symbol) | |
| 443 | ||
| 444 | let registry = WASMGC_TYPE_REGISTRY(module, object, string_type) | |
| 445 | ||
| 446 | registry.collect(container.symbol_table.global_scope) | |
| 447 | ||
| 448 | for (frame, method) in _frame_members do | |
| 449 | registry.add_method(frame, method) | |
| 450 | od | |
| 451 | ||
| 452 | // A boxed enum answers to_string with its member names. | |
| 453 | for (_, boxed) in registry.boxes do | |
| 454 | if let `enum: Semantic.Symbols.ENUM_STRUCT = boxed.symbol, (method, body) = ENUM_BOX_METHODS.to_string_for(`enum) then | |
| 455 | registry.add_method(`enum, method) | |
| 456 | _bodies.add((method, body)) | |
| 457 | fi | |
| 458 | od | |
| 459 | ||
| 460 | return registry | |
| 461 | si | |
| 462 | ||
| 463 | // The signature of a host import, or null where a part of it cannot | |
| 464 | // cross to the host. A string crosses as a JavaScript string. | |
| 465 | _host_signature(function: Function) -> FUNC_TYPE? is | |
| 466 | let parameters = LIST[VALUE_TYPE]() | |
| 467 | let results = LIST[VALUE_TYPE]() | |
| 468 | let is_valid mut = true | |
| 469 | ||
| 470 | for argument in function.arguments do | |
| 471 | if let type = _host_value_type(argument) then | |
| 472 | parameters.add(type) | |
| 473 | else | |
| 474 | _report(function.location, "a host argument of type {argument}") | |
| 475 | is_valid = false | |
| 476 | fi | |
| 477 | od | |
| 478 | ||
| 479 | // A constructor the host provides answers the value it builds. | |
| 480 | if function.is_constructor then | |
| 481 | if let owner: Semantic.Types.Typed = function.owner, owner_type = owner.type, type = _host_value_type(owner_type) then | |
| 482 | results.add(type) | |
| 483 | else | |
| 484 | _report(function.location, "a host constructor of {function.owner}") | |
| 485 | is_valid = false | |
| 486 | fi | |
| 487 | elif !WASMGC_TYPES.is_void(function.return_type) then | |
| 488 | if let type = _host_value_type(function.return_type!) then | |
| 489 | results.add(type) | |
| 490 | else | |
| 491 | _report(function.location, "a host result of type {function.return_type}") | |
| 492 | is_valid = false | |
| 493 | fi | |
| 494 | fi | |
| 495 | ||
| 496 | if !is_valid then | |
| 497 | return null | |
| 498 | fi | |
| 499 | ||
| 500 | return FUNC_TYPE(parameters.to_array(), results.to_array()) | |
| 501 | si | |
| 502 | ||
| 503 | // The getter of the core library's command-line arguments, which | |
| 504 | // `_start` passes to an entry point that takes them; null where the | |
| 505 | // entry point takes none, or the core library declares none. | |
| 506 | _arguments_getter() -> Function? is | |
| 507 | if !_entry_point? \/ _entry_point.arguments.count == 0 then | |
| 508 | return null | |
| 509 | fi | |
| 510 | ||
| 511 | if let environment = CORE_LIBRARY.environment_class() then | |
| 512 | if let getter = CORE_LIBRARY.find_getter(environment, "arguments") then | |
| 513 | for (function, _) in _bodies do | |
| 514 | if function == getter then | |
| 515 | return getter | |
| 516 | fi | |
| 517 | od | |
| 518 | fi | |
| 519 | fi | |
| 520 | ||
| 521 | return null | |
| 522 | si | |
| 523 | ||
| 524 | // The getter of the core library's exception message, which an | |
| 525 | // executable describes an exception nothing caught with; null in | |
| 526 | // a library, or where the core library declares no exception | |
| 527 | // message with a body. | |
| 528 | _exception_message_getter() -> Function? is | |
| 529 | if !_entry_point? then | |
| 530 | return null | |
| 531 | fi | |
| 532 | ||
| 533 | if let exception = CORE_LIBRARY.root_exception_class() then | |
| 534 | if let getter = CORE_LIBRARY.find_getter(exception, "message") then | |
| 535 | for (function, _) in _bodies do | |
| 536 | if function == getter then | |
| 537 | return getter | |
| 538 | fi | |
| 539 | od | |
| 540 | fi | |
| 541 | fi | |
| 542 | ||
| 543 | return null | |
| 544 | si | |
| 545 | ||
| 546 | // Whether any host operation the program declares answers with a | |
| 547 | // string, which the strings have to convert from the host's. | |
| 548 | _host_answers_a_string() -> bool is | |
| 549 | for host in _host_declarations do | |
| 550 | if WASMGC_TYPES.is_string(host.return_type) then | |
| 551 | return true | |
| 552 | fi | |
| 553 | od | |
| 554 | ||
| 555 | return false | |
| 556 | si | |
| 557 | ||
| 558 | // Whether any host operation the program declares takes a string, | |
| 559 | // which the strings have to convert for it. | |
| 560 | _host_takes_a_string() -> bool is | |
| 561 | for host in _host_declarations do | |
| 562 | for argument in host.arguments do | |
| 563 | if WASMGC_TYPES.is_string(argument) then | |
| 564 | return true | |
| 565 | fi | |
| 566 | od | |
| 567 | od | |
| 568 | ||
| 569 | return false | |
| 570 | si | |
| 571 | ||
| 572 | _host_value_type(type: Semantic.Types.Type) -> VALUE_TYPE? is | |
| 573 | if let scalar = WASMGC_TYPES.scalar(type) then | |
| 574 | return WASMGC_TYPES.value_type(scalar) | |
| 575 | elif WASMGC_TYPES.is_string(type) then | |
| 576 | return VALUE_TYPE.of_abstract(Wasm.AbstractHeapType.EXTERN) | |
| 577 | elif WASMGC_TYPES.is_bigint(type) then | |
| 578 | return VALUE_TYPE.of_abstract(Wasm.AbstractHeapType.EXTERN) | |
| 579 | fi | |
| 580 | ||
| 581 | return null | |
| 582 | si | |
| 583 | ||
| 584 | _build_function( | |
| 585 | module: MODULE, | |
| 586 | registry: WASMGC_TYPE_REGISTRY, | |
| 587 | imports: WASMGC_IMPORTS, | |
| 588 | strings: WASMGC_STRINGS?, | |
| 589 | exceptions: WASMGC_EXCEPTIONS?, | |
| 590 | numbering: WASMGC_FUNCTION_NUMBERING, | |
| 591 | function: FUNCTION_INSTANCE | |
| 592 | ) -> FUNCTION is | |
| 593 | let definition = function.definition | |
| 594 | let parameters = LIST[VALUE_TYPE]() | |
| 595 | ||
| 596 | if definition.is_emitted_with_receiver then | |
| 597 | if let receiver = registry.receiver_type(function) then | |
| 598 | parameters.add(receiver) | |
| 599 | else | |
| 600 | _report(definition.location, "an instance method of {definition.owner}") | |
| 601 | parameters.add(registry.object_reference) | |
| 602 | fi | |
| 603 | fi | |
| 604 | ||
| 605 | for argument in definition.arguments do | |
| 606 | if let value_type = registry.value_type(function.specialize(argument)) then | |
| 607 | parameters.add(value_type) | |
| 608 | else | |
| 609 | _report(definition.location, "an argument of type {argument}") | |
| 610 | fi | |
| 611 | od | |
| 612 | ||
| 613 | let results = LIST[VALUE_TYPE]() | |
| 614 | let return_type = function.specialize(definition.return_type) | |
| 615 | ||
| 616 | if !WASMGC_TYPES.is_void(return_type) then | |
| 617 | if let value_type = registry.value_type(return_type) then | |
| 618 | results.add(value_type) | |
| 619 | else | |
| 620 | _report(definition.location, "a result of type {return_type}") | |
| 621 | fi | |
| 622 | fi | |
| 623 | ||
| 624 | let (body, locals) = _lower(registry, imports, strings, exceptions, numbering, function, parameters) | |
| 625 | ||
| 626 | let type_index = module.add_type(FUNC_TYPE(parameters.to_array(), results.to_array())) | |
| 627 | ||
| 628 | return FUNCTION(type_index, locals, body) | |
| 629 | si | |
| 630 | ||
| 631 | // Lowers the body of `function`, reporting whatever it uses that the | |
| 632 | // target cannot generate, except while the registry is discovering, | |
| 633 | // when the result is wanted only for what it numbers. | |
| 634 | _lower( | |
| 635 | registry: WASMGC_TYPE_REGISTRY, | |
| 636 | imports: WASMGC_IMPORTS, | |
| 637 | strings: WASMGC_STRINGS?, | |
| 638 | exceptions: WASMGC_EXCEPTIONS?, | |
| 639 | numbering: WASMGC_FUNCTION_NUMBERING, | |
| 640 | function: FUNCTION_INSTANCE, | |
| 641 | parameters: LIST[VALUE_TYPE] | |
| 642 | ) -> (body: Collections.List[Wasm.INSTRUCTION], locals: VALUE_TYPE[]) is | |
| 643 | let definition = function.definition | |
| 644 | ||
| 645 | let parameter_types mut = parameters | |
| 646 | ||
| 647 | if registry.discovering then | |
| 648 | // Only how many there are matters to a lowering that is | |
| 649 | // discarded. | |
| 650 | parameter_types = LIST[VALUE_TYPE]() | |
| 651 | ||
| 652 | let count = definition.arguments.count + (if definition.is_emitted_with_receiver then 1 else 0 fi) | |
| 653 | ||
| 654 | for i in 0..count do | |
| 655 | parameter_types.add(registry.object_reference) | |
| 656 | od | |
| 657 | fi | |
| 658 | ||
| 659 | let lowering = WASMGC_LOWERING(numbering, registry, imports, strings, exceptions, function, parameter_types) | |
| 660 | ||
| 661 | if !registry.discovering /\ IoC.CONTAINER.instance.build_flags.want_wat_lines then | |
| 662 | lowering.source_lines = _wat_annotations | |
| 663 | fi | |
| 664 | ||
| 665 | let body = lowering.lower(_body_of(definition)) | |
| 666 | ||
| 667 | if let annotations = lowering.source_lines, index = numbering.indexed(function) then | |
| 668 | for (local, name) in lowering.local_names do | |
| 669 | annotations.note_local_name(index, local, name) | |
| 670 | od | |
| 671 | fi | |
| 672 | ||
| 673 | if !registry.discovering then | |
| 674 | for construct in lowering.unsupported do | |
| 675 | _report(IR.PROGRAM_FUNCTIONS.source_location(definition), construct) | |
| 676 | od | |
| 677 | fi | |
| 678 | ||
| 679 | return (body, lowering.local_types.to_array()) | |
| 680 | si | |
| 681 | ||
| 682 | _body_of(definition: Function) -> LIST[Value] is | |
| 683 | for (function, values) in _bodies do | |
| 684 | if function == definition then | |
| 685 | return values | |
| 686 | fi | |
| 687 | od | |
| 688 | ||
| 689 | return LIST[Value]() | |
| 690 | si | |
| 691 | si | |
| 692 | ||
| 693 | // Finds a program's reach by lowering each body it reaches through the | |
| 694 | // backend's own lowering, with the registry discovering, and keeping | |
| 695 | // nothing but what the lowering recorded. | |
| 696 | class WASMGC_DISCOVERY( | |
| 697 | registry: WASMGC_TYPE_REGISTRY private, | |
| 698 | lower: (FUNCTION_INSTANCE) -> void private, | |
| 699 | report: (Source.LOCATION, string) -> void private | |
| 700 | ): DiscoveryTarget is | |
| 701 | reach(instance: FUNCTION_INSTANCE) => _lower(instance) | |
| 702 | ||
| 703 | is_instantiated(type: CLASS_INSTANCE) -> bool => _registry.is_instantiated(type) | |
| 704 | ||
| 705 | // The cells and boxes the by-reference parameters of the slots and | |
| 706 | // selectors `type` takes a place in are held in have to exist | |
| 707 | // before any body reaching them is compiled. | |
| 708 | note_instantiated(type: CLASS_INSTANCE) is | |
| 709 | _registry.dispatch.discover_reference_parameters(type) | |
| 710 | _registry.dispatch.note_generic_implementor(type) | |
| 711 | si | |
| 712 | ||
| 713 | report(location: Source.LOCATION, construct: string) => _report(location, construct) | |
| 714 | si | |
| 715 | si |