| 439 | } |
| 440 | |
| 441 | void Linker::compose_start_funcs(){ |
| 442 | // Collect start functions |
| 443 | std::vector<index_t> func_indices; |
| 444 | std::visit(overloaded { |
| 445 | [&](Config::StartMode& mode){ |
| 446 | if(mode == Config::StartMode::All){ |
| 447 | for(ModuleEntry& module_entry : modules){ |
| 448 | if(module_entry.start){ |
| 449 | index_t index = module_entry.start.value(); |
| 450 | update_index(index, func) |
| 451 | func_indices.emplace_back(index); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | }, |
| 456 | [&](std::vector<Config::StartEntry>& entries){ |
| 457 | for(Config::StartEntry& entry : entries){ |
| 458 | std::filesystem::path start_path = entry.first; |
| 459 | if(std::filesystem::exists(start_path)){ |
| 460 | start_path = std::filesystem::canonical(start_path); |
| 461 | } |
| 462 | for(ModuleEntry& module_entry : modules){ |
| 463 | if(entry.first == module_entry.path || start_path == module_entry.path){ |
| 464 | if(!entry.second && !module_entry.start){ |
| 465 | throw Exception::Exception("start section not exists in explicit start entry"); |
| 466 | } |
| 467 | index_t index; |
| 468 | if(entry.second){ |
| 469 | index = entry.second.value(); |
| 470 | }else{ |
| 471 | index = module_entry.start.value(); |
| 472 | } |
| 473 | update_index(index, func) |
| 474 | func_indices.emplace_back(index); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | }, config.start_func); |
| 480 | |
| 481 | // Compose |
| 482 | if(func_indices.size() > 0){ |
| 483 | if(func_indices.size() > 1){ |
| 484 | output.start.emplace<index_t>(output.funcs.size() + import_counter.func); |
| 485 | WasmFunc& start_func = output.funcs.emplace_back(); |
| 486 | FuncType type; |
| 487 | if(type_map.contains(type)){ |
| 488 | start_func.typeidx = type_map[type]; |
| 489 | }else{ |
| 490 | start_func.typeidx = output.types.size(); |
| 491 | output.types.emplace_back(type); |
| 492 | } |
| 493 | for(index_t index : func_indices){ |
| 494 | start_func.body.emplace_back<Instr::Call>(Instr::Call(index)); |
| 495 | } |
| 496 | start_func.body.emplace_back(Instr::End()); |
| 497 | }else{ |
| 498 | output.start.emplace(func_indices[0]); |