@@ -487,134 +487,6 @@ EmJsWalker fixEmJsFuncsAndReturnWalker(Module& wasm) {
487
487
return walker;
488
488
}
489
489
490
- // Fixes function name hacks caused by LLVM exception & setjmp/longjmp
491
- // handling pass for wasm.
492
- // This does two things:
493
- // 1. Change emscripten_longjmp_jmpbuf to emscripten_longjmp.
494
- // In setjmp/longjmp handling pass in wasm backend, what we want to do is
495
- // to change all function calls to longjmp to calls to emscripten_longjmp.
496
- // Because we replace all calls to longjmp to emscripten_longjmp, the
497
- // signature of that function should be the same as longjmp:
498
- // emscripten_longjmp(jmp_buf, int)
499
- // But after calling a function that might longjmp, while we test whether
500
- // a longjmp occurred, we have to load an int address value and call
501
- // emscripten_longjmp again with that address as the first argument. (Refer
502
- // to lib/Target/WebAssembly/WebAssemblyEmscriptenEHSjLj.cpp in LLVM for
503
- // details.)
504
- // In this case we need the signature of emscripten_longjmp to be (int,
505
- // int). So we need two different kinds of emscripten_longjmp signatures in
506
- // LLVM IR. Both signatures will be lowered to (int, int) eventually, but
507
- // in LLVM IR, types are not lowered yet.
508
- // So we declare two functions in LLVM:
509
- // emscripten_longjmp_jmpbuf(jmp_buf, int)
510
- // emscripten_longjmp(int, int)
511
- // And we change the name of emscripten_longjmp_jmpbuf to
512
- // emscripten_longjmp here.
513
- // 2. Converts invoke wrapper names.
514
- // Refer to the comments in fixEmExceptionInvoke below.
515
- struct FixInvokeFunctionNamesWalker
516
- : public PostWalker<FixInvokeFunctionNamesWalker> {
517
- Module& wasm;
518
- std::vector<Name> toRemove;
519
- std::map<Name, Name> importRenames;
520
- std::map<Name, Name> functionRenames;
521
- std::set<Signature> invokeSigs;
522
- ImportInfo imports;
523
-
524
- FixInvokeFunctionNamesWalker (Module& _wasm) : wasm(_wasm), imports(wasm) {}
525
-
526
- // Converts invoke wrapper names generated by LLVM backend to real invoke
527
- // wrapper names that are expected by JavaScript glue code.
528
- // This is required to support wasm exception handling (asm.js style).
529
- //
530
- // LLVM backend lowers
531
- // invoke @func(arg1, arg2) to label %invoke.cont unwind label %lpad
532
- // into
533
- // ... (some code)
534
- // call @invoke_SIG(func, arg1, arg2)
535
- // ... (some code)
536
- // SIG is a mangled string generated based on the LLVM IR-level function
537
- // signature. In LLVM IR, types are not lowered yet, so this mangling scheme
538
- // simply takes LLVM's string representtion of parameter types and concatenate
539
- // them with '_'. For example, the name of an invoke wrapper for function
540
- // void foo(struct mystruct*, int) will be
541
- // "__invoke_void_%struct.mystruct*_int".
542
- // This function converts the names of invoke wrappers based on their lowered
543
- // argument types and a return type. In the example above, the resulting new
544
- // wrapper name becomes "invoke_vii".
545
- Name fixEmExceptionInvoke (const Name& name, Signature sig) {
546
- std::string nameStr = name.c_str ();
547
- if (nameStr.front () == ' "' && nameStr.back () == ' "' ) {
548
- nameStr = nameStr.substr (1 , nameStr.size () - 2 );
549
- }
550
- if (nameStr.find (" __invoke_" ) != 0 ) {
551
- return name;
552
- }
553
-
554
- std::vector<Type> newParams (sig.params .begin () + 1 , sig.params .end ());
555
- Signature sigWoOrigFunc = Signature (Type (newParams), sig.results );
556
- invokeSigs.insert (sigWoOrigFunc);
557
- return Name (" invoke_" +
558
- getSig (sigWoOrigFunc.results , sigWoOrigFunc.params ));
559
- }
560
-
561
- void visitFunction (Function* curr) {
562
- if (!curr->imported ()) {
563
- return ;
564
- }
565
-
566
- Name newname = fixEmExceptionInvoke (curr->base , curr->sig );
567
- if (newname == curr->base ) {
568
- return ;
569
- }
570
-
571
- BYN_TRACE (" renaming import: " << curr->module << " ." << curr->base << " ("
572
- << curr->name << " ) -> " << newname << " \n " );
573
-
574
- if (auto * f = imports.getImportedFunction (curr->module , newname)) {
575
- BYN_TRACE (" remove redundant import: " << curr->base << " \n " );
576
- toRemove.push_back (curr->name );
577
- // Make sure the existing import has the correct internal name.
578
- if (f->name != newname) {
579
- functionRenames[f->name ] = newname;
580
- }
581
- } else {
582
- BYN_TRACE (" rename import: " << curr->base << " \n " );
583
- curr->base = newname;
584
- }
585
-
586
- functionRenames[curr->name ] = newname;
587
-
588
- // Ensure that an imported functions of this name exists.
589
- importRenames[curr->base ] = newname;
590
- }
591
-
592
- void visitModule (Module* curr) {
593
- for (auto name : toRemove) {
594
- wasm.removeFunction (name);
595
- }
596
-
597
- // Rename all uses of the old function to the new import name
598
- ModuleUtils::renameFunctions (wasm, functionRenames);
599
-
600
- // For imports that for renamed, update any associated GOT.func imports.
601
- for (auto & pair : importRenames) {
602
- BYN_TRACE (" looking for: GOT.func." << pair.first << " \n " );
603
- if (auto g = imports.getImportedGlobal (" GOT.func" , pair.first )) {
604
- BYN_TRACE (" renaming corresponding GOT entry: " << g->base << " -> "
605
- << pair.second << " \n " );
606
- g->base = pair.second ;
607
- }
608
- }
609
- }
610
- };
611
-
612
- void EmscriptenGlueGenerator::fixInvokeFunctionNames () {
613
- BYN_TRACE (" fixInvokeFunctionNames\n " );
614
- FixInvokeFunctionNamesWalker walker (wasm);
615
- walker.walkModule (&wasm);
616
- }
617
-
618
490
void printSignatures (std::ostream& o, const std::set<Signature>& c) {
619
491
o << " [" ;
620
492
bool first = true ;
@@ -746,7 +618,7 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
746
618
meta << " \" invokeFuncs\" : [" ;
747
619
commaFirst = true ;
748
620
ModuleUtils::iterImportedFunctions (wasm, [&](Function* import) {
749
- if (import->base .startsWith (" invoke_" )) {
621
+ if (import->module == ENV && import-> base .startsWith (" invoke_" )) {
750
622
if (invokeFuncs.insert (import->base .str ).second ) {
751
623
meta << nextElement () << ' "' << import->base .str << ' "' ;
752
624
}
0 commit comments