-
Notifications
You must be signed in to change notification settings - Fork 3.5k
dynCallLegacy: Fill in dynCall_sig with a Wasm adaptor if it is missing #17328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9121505
Fill in dynCall_sig with a custom Wasm module if it is missing
hoodmane 380052c
generate_func_type ==> generateFuncType
hoodmane b678b94
Update #ifdef guards
hoodmane 99b057d
Remove debug statement, add more comments
hoodmane ffc9857
Merge branch 'main' into generate-wrapper
hoodmane 905e4c5
Fix createDyncallWrapper
hoodmane e1e0e0a
Make localGet function
hoodmane e90c349
Merge branch 'main' into generate-wrapper
hoodmane 2a2bb84
Address some review comments
hoodmane ab67a52
Move createDyncallWrapper into separate file
hoodmane 14a3c1a
Fix closure error
hoodmane 1341807
Fix type section
hoodmane 5cde82b
Add test
hoodmane 0ff7b96
Fix formatting
hoodmane fd40684
Apply suggestions from code review
hoodmane 8a477cb
Merge branch 'main' into generate-wrapper
hoodmane 2b95712
Merge branch 'generate-wrapper' of github.com:hoodmane/emscripten int…
hoodmane fe19443
Merge branch 'main' into generate-wrapper
hoodmane 49ab4c2
add getTempRet0 to REQUIRED_EXPORTS
hoodmane 1950321
Fix flake errors
hoodmane d989126
Fix test
hoodmane c32bf3b
Fix flake8
hoodmane bc55996
Fix two more tests
hoodmane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2020 The Emscripten Authors | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| mergeInto(LibraryManager.library, { | ||
| $createDyncallWrapper__deps: ['$generateFuncType', '$uleb128Encode'], | ||
| $createDyncallWrapper: function(sig) { | ||
| var sections = []; | ||
| var prelude = [ | ||
| 0x00, 0x61, 0x73, 0x6d, // magic ("\0asm") | ||
| 0x01, 0x00, 0x00, 0x00, // version: 1 | ||
| ]; | ||
| sections.push(prelude); | ||
| var wrappersig = [ | ||
| // if return type is j, we will put the upper 32 bits into tempRet0. | ||
| sig[0].replace("j", "i"), | ||
| "i", // The first argument is the function pointer to call | ||
| // in the rest of the argument list, one 64 bit integer is legalized into | ||
| // two 32 bit integers. | ||
| sig.slice(1).replace("j", "ii"), | ||
| ].join(""); | ||
|
|
||
| var typeSectionBody = [ | ||
| 0x03, // number of types = 3 | ||
| ]; | ||
| generateFuncType(wrappersig, typeSectionBody); // The signature of the wrapper we are generating | ||
| generateFuncType(sig, typeSectionBody); // the signature of the function pointer we will call | ||
| generateFuncType("vi", typeSectionBody); // the signature of setTempRet0 | ||
|
|
||
| var typeSection = [0x01 /* Type section code */]; | ||
| uleb128Encode(typeSectionBody.length, typeSection); // length of section in bytes | ||
| typeSection.push.apply(typeSection, typeSectionBody); | ||
| sections.push(typeSection); | ||
|
|
||
| var importSection = [ | ||
| 0x02, // import section code | ||
| 0x0F, // length of section in bytes | ||
| 0x02, // number of imports = 2 | ||
| // Import the wasmTable, which we will call "t" | ||
| 0x01, 0x65, // name "e" | ||
| 0x01, 0x74, // name "t" | ||
| 0x01, 0x70, // importing a table | ||
| 0x00, // with no max # of elements | ||
| 0x00, // and min of 0 elements | ||
| // Import the setTempRet0 function, which we will call "r" | ||
| 0x01, 0x65, // name "e" | ||
| 0x01, 0x72, // name "r" | ||
| 0x00, // importing a function | ||
| 0x02, // type 2 | ||
| ]; | ||
| sections.push(importSection); | ||
|
|
||
| var functionSection = [ | ||
| 0x03, // function section code | ||
| 0x02, // length of section in bytes | ||
| 0x01, // number of functions = 1 | ||
| 0x00, // type 0 = wrappersig | ||
| ]; | ||
| sections.push(functionSection); | ||
|
|
||
| var exportSection = [ | ||
| 0x07, // export section code | ||
| 0x05, // length of section in bytes | ||
| 0x01, // One export | ||
| 0x01, 0x66, // name "f" | ||
| 0x00, // type: function | ||
| 0x01, // function index 1 = the wrapper function (index 0 is setTempRet0) | ||
| ]; | ||
| sections.push(exportSection); | ||
|
|
||
| var convert_code = []; | ||
| if (sig[0] === "j") { | ||
| // Add a single extra i64 local. In order to legalize the return value we | ||
| // need a local to store it in. Local variables are run length encoded. | ||
| convert_code = [ | ||
| 0x01, // One run | ||
| 0x01, // of length 1 | ||
| 0x7e, // of i64 | ||
| ]; | ||
| } else { | ||
| convert_code.push(0x00); // no local variables (except the arguments) | ||
| } | ||
|
|
||
| function localGet(j){ | ||
hoodmane marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| convert_code.push(0x20); // local.get | ||
| uleb128Encode(j, convert_code); | ||
| } | ||
|
|
||
| var j = 1; | ||
| for (var i = 1; i < sig.length; i++) { | ||
| if (sig[i] == "j") { | ||
| localGet(j + 1); | ||
| convert_code.push( | ||
| 0xad, // i64.extend_i32_unsigned | ||
| 0x42, 0x20, // i64.const 32 | ||
| 0x86, // i64.shl, | ||
| ) | ||
| localGet(j); | ||
| convert_code.push( | ||
| 0xac, // i64.extend_i32_signed | ||
| 0x84, // i64.or | ||
| ); | ||
| j+=2; | ||
| } else { | ||
| localGet(j); | ||
| j++; | ||
| } | ||
| } | ||
|
|
||
| convert_code.push( | ||
| 0x20, 0x00, // local.get 0 (put function pointer on stack) | ||
| 0x11, 0x01, 0x00, // call_indirect type 1 = wrapped_sig, table 0 = only table | ||
| ); | ||
| if (sig[0] === "j") { | ||
| // tee into j (after the argument handling loop, j is one past the | ||
| // argument list so it points to the i64 local we added) | ||
| convert_code.push(0x22); | ||
| uleb128Encode(j, convert_code); | ||
| convert_code.push( | ||
| 0x42, 0x20, // i64.const 32 | ||
| 0x88, // i64.shr_u | ||
| 0xa7, // i32.wrap_i64 | ||
| 0x10, 0x00, // Call function 0 | ||
| ); | ||
| localGet(j); | ||
| convert_code.push( | ||
| 0xa7, // i32.wrap_i64 | ||
| ); | ||
| } | ||
| convert_code.push(0x0b); // end | ||
|
|
||
| var codeBody = [0x01]; // one code | ||
| uleb128Encode(convert_code.length, codeBody); | ||
| codeBody.push.apply(codeBody, convert_code); | ||
| var codeSection = [0x0A /* Code section code */]; | ||
| uleb128Encode(codeBody.length, codeSection); | ||
| codeSection.push.apply(codeSection, codeBody); | ||
| sections.push(codeSection); | ||
|
|
||
| var bytes = new Uint8Array([].concat.apply([], sections)); | ||
| // We can compile this wasm module synchronously because it is small. | ||
| var module = new WebAssembly.Module(bytes); | ||
| var instance = new WebAssembly.Instance(module, { | ||
| 'e': { | ||
| 't': wasmTable, | ||
| 'r': setTempRet0, | ||
| } | ||
| }); | ||
| var wrappedFunc = instance.exports['f']; | ||
| return wrappedFunc; | ||
| }, | ||
| }); | ||
hoodmane marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #include <emscripten.h> | ||
| #include "stdint.h" | ||
| #include "stdio.h" | ||
|
|
||
| uint64_t f1(uint64_t x){ | ||
| return x; | ||
| } | ||
|
|
||
| void f2(int i, uint64_t j, float f, double d){ | ||
| printf("i: %d j: %lld f: %f d: %lf\n", i, j, f, d); | ||
| } | ||
|
|
||
|
|
||
| int main(){ | ||
| EM_ASM({ | ||
| var w = createDyncallWrapper("jj"); | ||
| console.log(w($0, 2, 7), tempRet0); | ||
| }, f1); | ||
|
|
||
| EM_ASM({ | ||
| var w = createDyncallWrapper("vijfd"); | ||
| w($0, 2, 7, 2, 3.12, 77.12); | ||
| }, f2); | ||
| } | ||
hoodmane marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.