简体   繁体   English

我可以获取简单的asm.js代码吗? 使用emscripten编译简单的c api代码时

[英]Can I get simple asm.js code? when simple c api code compile using emscripten

First, check my knowledge 首先,检查我的知识

  1. c/c++ code >> compile(emscripten [emcc]) >> asm.js code C / C ++代码>>编译(emscripten [emcc])>> asm.js代码
    I can get c/c++ code to asm.js code, is it right? 我可以将c / c ++代码转换为asm.js代码,对吗?

  2. asm.js code >> compile(binaryen [asm2wasm]) >> wasm code asm.js代码>>编译(binaryen [asm2wasm])>> wasm代码
    I can get asm.js code to wasm code, is it right? 我可以将asm.js代码转换为wasm代码,对吗?

Second, my main question, how can i get simple asm.js code? 其次,我的主要问题是,如何获得简单的asm.js代码?

I'm try to follow step, but I can't get simple asm.js code... I'm get always complex asm.js code(5000 line more...) when using emcc compile... 我试图按照步骤进行操作,但是我无法获得简单的asm.js代码...使用emcc编译时,我总是会得到复杂的asm.js代码(多出5000行...)。

my try step 我的尝试步骤

A. make simple C api code // my_add.c A.制作简单的C api代码// // my_add.c

 #include <stdio.h> #include <emscripten.h> EMSCRIPTEN_KEEPALIVE double my_add(double num1, double num2) { return num1 + num2; } 

B. compile my_add.c code to asm.js code (using emcc) B.将my_add.c代码编译为asm.js代码(使用emcc)
I'm try many emcc options... 我正在尝试许多emcc选项...

emcc my_add.c emcc my_add.c

emcc my_add.c -s WASM=1 emcc my_add.c -s WASM = 1

etc... 等等...

My expected... simple asm.js code (something like follow code...) 我期望的...简单的asm.js代码(类似于遵循代码...)

 function my_add(num1, num2) { "use asm"; num1 = num1|0; num2 = num2|0; retrurn (num1 + num2)|0; } 

But emcc complied result is complex asm.js code 但是符合emcc的结果是复杂的asm.js代码

  1 // The Module object: Our interface to the outside world. We import 2 // and export values on it, and do the work to get that through 3 // closure compiler if necessary. There are various ways Module can be used: 4 // 1. Not defined. We create it here 5 // 2. A function parameter, function(Module) { ..generated code.. } 6 // 3. pre-run appended it, var Module = {}; ..generated code.. 7 // 4. External script tag defines var Module. 8 // We need to do an eval in order to handle the closure compiler 9 // case, where this code here is minified but Module was defined 10 // elsewhere (eg case 4 above). We also need to check if Module 11 // already exists (eg case 3 above). 12 // Note that if you want to run closure, and also to use Module 13 // after the generated code, you will need to define var Module = {}; 14 // before the code. Then that object will be used in the code, and you 15 // can continue to use Module afterwards as well. 16 var Module; 17 if (!Module) Module = (typeof Module !== 'undefined' ? Module : null) || {}; 18 19 // Sometimes an existing Module object exists with properties 20 // meant to overwrite the default module functionality. Here 21 // we collect those properties and reapply _after_ we configure 22 // the current environment's defaults to avoid having to be so 23 // defensive during initialization. 24 var moduleOverrides = {}; 25 for (var key in Module) { 26 if (Module.hasOwnProperty(key)) { 27 moduleOverrides[key] = Module[key]; 28 } 29 } 30 31 // The environment setup code below is customized to use Module. 32 // *** Environment setup code *** 33 var ENVIRONMENT_IS_WEB = false; 34 var ENVIRONMENT_IS_WORKER = false; 35 var ENVIRONMENT_IS_NODE = false; 36 var ENVIRONMENT_IS_SHELL = false; 37 ...... 2038 function _my_add($num1,$num2) { 2039 $num1 = +$num1; 2040 $num2 = +$num2; 2041 var $0 = 0.0, $1 = 0.0, $2 = 0.0, $3 = 0.0, $4 = 0.0, label = 0, sp = 0; 2042 sp = STACKTOP; 2043 STACKTOP = STACKTOP + 16|0; if ((STACKTOP|0) >= (STACK_MAX|0)) abort(); 2044 $0 = $num1; 2045 $1 = $num2; 2046 $2 = $0; 2047 $3 = $1; 2048 $4 = $2 + $3; 2049 STACKTOP = sp;return (+$4); 2050 } ....... 

My C/C++ code is 5~6 line.. but complied asm.js code is 5000~10000 line... 我的C / C ++代码是5〜6行..但是编译的asm.js代码是5000〜10000行...
I'm just want simple 'asm.js' code at compile result... 我只想在编译结果中使用简单的'asm.js'代码...
Is It possible? 可能吗?
And... How can I do that? 而且...我该怎么做?

I have found a solution! 我找到了解决方案!

Using the ONLY_MY_CODE and --separate-asm options, 使用ONLY_MY_CODE--separate-asm选项,

 emcc my_add.c -O2 -profiling -s ONLY_MY_CODE=1 -g2 --separate-asm -o abc.js 

the abc.asm.js file is created: 创建了abc.asm.js文件:

 Module["asm"] = (function(global, env, buffer) { "use asm"; function _my_add(d1, d2) { d1 = +d1; d2 = +d2; return +(d1 + d2); } return { _my_add: _my_add }; }); 

IRC ch. IRC频道 #emscripten #emscripten
'pdox' answer to me.. “ pdox”回答我。

pdox: emcc -O2 -profiling pdox:emcc -O2-分析

 3601 function _my_add(d1, d2) { 3602 d1 = +d1; 3603 d2 = +d2; 3604 return +(d1 + d2); 3605 } 

emcc -O2 -profiling emcc -O2-分析

command make simple function.. but.. But it still produces a long code.(3000 line...) (complie result file 'a.out.js' is 3000 lines....) 命令执行简单的功能..但是..但是它仍然会产生很长的代码。(3000行...)(比较结果文件'a.out.js'是3000行....)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM