{"id":2958,"date":"2013-08-26T18:32:47","date_gmt":"2013-08-26T09:32:47","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=2958"},"modified":"2013-08-26T18:32:47","modified_gmt":"2013-08-26T09:32:47","slug":"4-4-jit%e3%82%b3%e3%83%b3%e3%83%91%e3%82%a4%e3%83%a9%e3%81%ae%e8%bf%bd%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/2958","title":{"rendered":"4.4. JIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u8ffd\u52a0"},"content":{"rendered":"<p><a href=\"https:\/\/peta.okechan.net\/blog\/archives\/2836\" title=\"LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\">LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u65e5\u672c\u8a9e\u8a33<\/a><br \/>\n\u7b2c4\u7ae0 \u4e07\u83ef\u93e1: JIT\u306e\u8ffd\u52a0\u3068\u6700\u9069\u5316\u306e\u30b5\u30dd\u30fc\u30c8<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl04.html#adding-a-jit-compiler\" target=\"_blank\">\u7b2c4\u7bc0 JIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u8ffd\u52a0<\/a><\/p>\n<p>LLVM IR\u3067\u5f97\u3089\u308c\u308b\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u9069\u7528\u53ef\u80fd\u306a\u30c4\u30fc\u30eb\u306b\u306f\u3044\u308d\u3093\u306a\u7a2e\u985e\u306e\u3082\u306e\u304c\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\uff08\u524d\u7bc0\u3067\u3084\u3063\u305f\u3088\u3046\u306b\uff09LLVM IR\u306e\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u6700\u9069\u5316\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3060\u3057\u3001\u305d\u308c\u3092\u30c6\u30ad\u30b9\u30c8\u5f62\u5f0f\u307e\u305f\u306f\u30d0\u30a4\u30ca\u30ea\u3067\u30c0\u30f3\u30d7\u3057\u3066\u51fa\u529b\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u308b\u3057\u3001\u8272\u3093\u306a\u30bf\u30fc\u30b2\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u305d\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u30a2\u30bb\u30f3\u30d6\u30ea\u30d5\u30a1\u30a4\u30eb(.s)\u3092\u5f97\u308b\u4e8b\u3082\u3067\u304d\u308b\u3057\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u308b\u3002<br \/>\nLLVM IR\u8868\u73fe\u306e\u3044\u3044\u3068\u3053\u308d\u306f\u3001\u305d\u308c\u304c\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u304a\u3051\u308b\u591a\u304f\u306e\u30d1\u30fc\u30c4\u9593\u306b\u304a\u3051\u308b&#8221;\u5171\u901a\u8a00\u8a9e\u201d\u3067\u3042\u308b\u70b9\u3067\u3042\u308b\u3002<\/p>\n<p>\u3053\u306e\u7bc0\u3067\u306f\u3001\u6211\u3005\u306e\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306b\u5bfe\u3057\u3066JIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u30b5\u30dd\u30fc\u30c8\u3092\u8ffd\u52a0\u3059\u308b\u3002<br \/>\n\u305d\u308c\u3092\u4e07\u83ef\u93e1\u306b\u8ffd\u52a0\u3059\u308b\u305f\u3081\u306b\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u95a2\u6570\u3092\u5165\u529b\u3057\u7d42\u3048\u305f\u3089JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u5b9f\u884c\u3059\u308b\u3001\u3068\u3044\u3046\u3053\u3068\u3092\u57fa\u672c\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u3068\u3059\u308b\u3002<br \/>\n\u305f\u3060\u3057\u3001\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u306f\u5165\u529b\u3055\u308c\u305f\u3089\u305f\u3060\u3061\u306b\u8a55\u4fa1\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001&#8221;1+2;&#8221;\u3068\u5165\u529b\u3055\u308c\u305f\u3089\u3001\u305d\u308c\u3092\u8a55\u4fa1\u3057\u30663\u3068\u5370\u5b57\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u3082\u3057\u95a2\u6570\u304c\u5b9a\u7fa9\u3055\u308c\u305f\u3089\u3001\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u305d\u308c\u3092\u547c\u3079\u308b\u3088\u3046\u306b\u3059\u3079\u304d\u3067\u3042\u308b\u3002<\/p>\n<p>\u3053\u308c\u3092\u884c\u3046\u305f\u3081\u3001\u307e\u305a\u306fJIT\u3092\u5ba3\u8a00\u3057\u3066\u521d\u671f\u5316\u3059\u308b\u3002<br \/>\n\u3053\u308c\u3092\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3068main\u95a2\u6570\u306b\u304a\u3051\u308b\u95a2\u6570\u547c\u3073\u51fa\u3057\u306e\u8ffd\u52a0\u306b\u3088\u3063\u3066\u884c\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static ExecutionEngine *TheExecutionEngine;\r\n...\r\nint main() {\r\n  ..\r\n  \/\/ JIT\u306e\u751f\u6210\u3002Module\u306e\u6240\u6709\u6a29\u3092\u3068\u308b\u3002\r\n  TheExecutionEngine = EngineBuilder(TheModule).create();\r\n  ..\r\n}<\/pre>\n<p>\u3053\u308c\u306b\u3088\u3063\u3066\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u3082LLVM\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306b\u3082\u306a\u308c\u308b\u3001\u62bd\u8c61\u7684\u306a&#8221;\u5b9f\u884c\u30a8\u30f3\u30b8\u30f3\uff08Execution Engine\uff09&#8221;\u304c\u751f\u6210\u3055\u308c\u308b\u3002<br \/>\n\u3042\u306a\u305f\u306e\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u304a\u3044\u3066\u5229\u7528\u53ef\u80fd\u306aJIT\u30b3\u30f3\u30d1\u30a4\u30e9\u304c\u3042\u308c\u3070\u3001LLVM\u306f\u81ea\u52d5\u7684\u306b\u305d\u308c\u3092\u9078\u629e\u3059\u308b\u3057\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u3078\u30d5\u30a9\u30fc\u30eb\u30d0\u30c3\u30af\u3059\u308b\u3002<\/p>\n<p>ExecutionEngine\u304c\u751f\u6210\u3055\u308c\u305f\u3089\u3001JIT\u3092\u5229\u7528\u3059\u308b\u6e96\u5099\u306f\u5b8c\u4e86\u3067\u3042\u308b\u3002<br \/>\n\u305f\u304f\u3055\u3093\u306e\u4fbf\u5229\u306aAPI\u304c\u305d\u308c\u306b\u306f\u5b58\u5728\u3059\u308b\u304c\u3001\u4e00\u756a\u30b7\u30f3\u30d7\u30eb\u306a\u306e\u306f&#8221;getPointerToFunction(F)&#8221;\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308b\u3002<br \/>\n\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u4e0e\u3048\u3089\u308c\u305fLLVM\u95a2\u6570\u306b\u5bfe\u3057\u3066JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u884c\u3044\u3001\u751f\u6210\u3055\u308c\u305f\u6a5f\u68b0\u8a9e\u3078\u306e\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u8fd4\u3059\u3002<br \/>\n\u6211\u3005\u306e\u5834\u5408\u3001\u3053\u308c\u306f\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u3092\u69cb\u6587\u89e3\u6790\u3059\u308b\u30b3\u30fc\u30c9\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static void HandleTopLevelExpression() {\r\n  \/\/ \u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u3092\u8a55\u4fa1\u3057\u3001\u7121\u540d\u95a2\u6570\u306b\u3059\u308b\u3002\r\n  if (FunctionAST *F = ParseTopLevelExpr()) {\r\n    if (Function *LF = F-&gt;Codegen()) {\r\n      LF-&gt;dump();  \/\/ \u8aac\u660e\u76ee\u7684\u3068\u3057\u3066\u95a2\u6570\u3092\u30c0\u30f3\u30d7\u3059\u308b\u3002\r\n\r\n      \/\/ \u95a2\u6570\u3092JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3001\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u8fd4\u3059\u3002\r\n      void *FPtr = TheExecutionEngine-&gt;getPointerToFunction(LF);\r\n\r\n      \/\/ \u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u6b63\u3057\u3044\u578b(\u5f15\u6570\u306a\u3057\u3001 double\u3092\u8fd4\u3059)\u3078\u5909\u63db\u3059\u308b\u3002\r\n      \/\/ \u306a\u306e\u3067\u3053\u306e\u95a2\u6570\u306f\u30cd\u30a4\u30c6\u30a3\u30d6\u306e\u95a2\u6570\u3068\u3057\u3066\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u53ef\u80fd\u3002\r\n      double (*FP)() = (double (*)())(intptr_t)FPtr;\r\n      fprintf(stderr, &quot;Evaluated to %f\\n&quot;, FP());\r\n    }<\/pre>\n<p>\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u3092\u3001&#8221;\u5f15\u6570\u306a\u3057\u3067double\u3092\u8fd4\u3059LLVM\u95a2\u6570&#8221;\u3078\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u4e8b\u3092\u601d\u3044\u51fa\u3057\u3066\u6b32\u3057\u3044\u3002<br \/>\nLLVM\u306eJIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306f\u3001\u30cd\u30a4\u30c6\u30a3\u30d6\u306a\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0ABI\uff08Application Binary Interface\uff09\u306b\u9069\u5408\u3059\u308b\u306e\u3067\u3001getPointerToFunction\u3067\u5f97\u3089\u308c\u305f\u30dd\u30a4\u30f3\u30bf\u3092\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u306b\u30ad\u30e3\u30b9\u30c8\u3067\u304d\u3001\u76f4\u63a5\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u3055\u3089\u306b\u305d\u308c\u306f\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u30b3\u30fc\u30c9\u3068\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u9759\u7684\u30ea\u30f3\u30af\u3055\u308c\u305f\u30cd\u30a4\u30c6\u30a3\u30d6\u306a\u6a5f\u68b0\u8a9e\u306e\u9593\u306b\u9055\u3044\u304c\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3082\u610f\u5473\u3059\u308b\u3002<\/p>\n<p>\u4ee5\u4e0a\u306e\u305f\u3063\u305f2\u3064\u306e\u5909\u66f4\u306b\u3088\u3063\u3066\u3001\u4e07\u83ef\u93e1\u304c\u3069\u3046\u52d5\u304f\u3088\u3046\u306b\u306a\u3063\u305f\u304b\u3092\u898b\u3066\u307f\u3088\u3046\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ready&gt; 4+5;\r\nRead top-level expression:\r\ndefine double @0() {\r\nentry:\r\n  ret double 9.000000e+00\r\n}\r\n\r\nEvaluated to 9.000000<\/pre>\n<p>\u57fa\u672c\u7684\u306b\u306f\u3061\u3083\u3093\u3068\u52d5\u3044\u3066\u308b\u3088\u3046\u3067\u3042\u308b\u3002<br \/>\n\u95a2\u6570\u306e\u30c0\u30f3\u30d7\u306f\u3001\u5165\u529b\u3055\u308c\u305f\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u304c\u7dcf\u5408\u3055\u308c\u3001&#8221;\u5f15\u6570\u304c\u306a\u304f\u5e38\u306bdouble\u306e\u5024\u3092\u8fd4\u3059\u95a2\u6570&#8221;\u3068\u306a\u3063\u305f\u3068\u3044\u3046\u4e8b\u3092\u793a\u3057\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u30c7\u30e2\u306f\u3001\u975e\u5e38\u306b\u57fa\u672c\u7684\u306a\u6a5f\u80fd\u306b\u3064\u3044\u3066\u306e\u3082\u306e\u3067\u3042\u308b\u3002<br \/>\n\u3057\u304b\u3057\u3082\u3063\u3068\u8907\u96d1\u306a\u4f8b\u3067\u306f\u3069\u3046\u3060\u308d\u3046\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ready&gt; def testfunc(x y) x + y*2;\r\nRead function definition:\r\ndefine double @testfunc(double %x, double %y) {\r\nentry:\r\n  %multmp = fmul double %y, 2.000000e+00\r\n  %addtmp = fadd double %multmp, %x\r\n  ret double %addtmp\r\n}\r\n\r\nready&gt; testfunc(4, 10);\r\nRead top-level expression:\r\ndefine double @1() {\r\nentry:\r\n  %calltmp = call double @testfunc(double 4.000000e+00, double 1.000000e+01)\r\n  ret double %calltmp\r\n}\r\n\r\nEvaluated to 24.000000<\/pre>\n<p>\u3053\u308c\u306f\u3001\u30e6\u30fc\u30b6\u5b9a\u7fa9\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3068\u3053\u308d\u3092\u793a\u3057\u3066\u3044\u308b\u304c\u3001\u3061\u3087\u3063\u3068\u5206\u304b\u308a\u306b\u304f\u3044\u70b9\u304c\u3042\u308b\u3002<br \/>\ntestfunc\u306e\u547c\u3073\u51fa\u3057\u3092\u884c\u3046\u7121\u540d\u95a2\u6570\uff08\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\uff09\u306b\u3064\u3044\u3066\u306e\u307fJIT\u306e\u547c\u3073\u51fa\u3057\u3092\u884c\u3063\u3066\u3044\u3066\u3001testfunc\u305d\u306e\u3082\u306e\u3067\u306fJIT\u306e\u547c\u3073\u51fa\u3057\u306f\u884c\u308f\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\nJIT\u306f\u3001\u533f\u540d\u95a2\u6570\u304b\u3089\u547c\u3073\u51fa\u3055\u308c\u3066\u3044\u308b\u5168\u3066\u306e\u672aJIT\u306e\u95a2\u6570\u3092\u30b9\u30ad\u30e3\u30f3\u3057\u3001\u305d\u3057\u3066getPointerToFunction()\u304b\u3089\u623b\u308b\u524d\u306b\u672aJIT\u306e\u95a2\u6570\u5168\u3066\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u8fd4\u3059\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u5b9f\u969b\u306b\u306f\u884c\u308f\u308c\u3066\u3044\u308b\u3002<\/p>\n<p>JIT\u306b\u306f\u3001\u5272\u308a\u5f53\u3066\u6e08\u307f\u306e\u6a5f\u68b0\u8a9e\u306e\u89e3\u653e\u3084\u3001\u95a2\u6570\u306e\u66f4\u65b0\u306b\u5408\u308f\u305b\u3066\u518dJIT\u3059\u308b\u3088\u3046\u306a\u3001\u3088\u308a\u9032\u3093\u3060\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u304c\u3044\u304f\u3064\u304b\u3042\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u306f\u3001\u305d\u306e\u3088\u3046\u306a\u3082\u306e\u3092\u4f7f\u308f\u306a\u304f\u3066\u3082\u9a5a\u304f\u3079\u304d\u30d1\u30ef\u30d5\u30eb\u306a\u80fd\u529b\u3092\u5f97\u3066\u3044\u308b\u3002<br \/>\n\u4ee5\u4e0b\u3092\u78ba\u8a8d\u3057\u3066\u307f\u3088\u3046\u3002\uff08\u79c1\u306f\u533f\u540d\u95a2\u6570\u306b\u3064\u3044\u3066\u306e\u30c0\u30f3\u30d7\u3092\u51fa\u529b\u3059\u308b\u30b3\u30fc\u30c9\u3092\u53d6\u308a\u9664\u3044\u305f\u3002\u4eca\u306e\u3042\u306a\u305f\u306a\u3089\u3001\u305d\u3046\u3059\u308b\u305f\u3081\u306b\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u304b\u306f\u5206\u304b\u308b\u3060\u308d\u3046\u3002\uff09<br \/>\n<em>\u8a33\u6ce8: \u3053\u306e\u6642\u70b9\u3067\u306f\u3001\u533f\u540d\u95a2\u6570\u306b\u3064\u3044\u3066\u30c0\u30f3\u30d7\u3092\u51fa\u529b\u3059\u308b\u30b3\u30fc\u30c9\u53d6\u308a\u9664\u304b\u308c\u3066\u3044\u306a\u3044\u3002\uff08\u306a\u306e\u3067\u4ee5\u4e0b\u306e\u7d50\u679c\u306b\u3082\u30c0\u30f3\u30d7\u306f\u6b8b\u3063\u305f\u307e\u307e\u3002\uff09<br \/>\n\u591a\u5206\u3001\u3061\u3087\u3063\u3068\u3057\u305f\u6f14\u7fd2\u3068\u3057\u3066&#8221;\u5404\u81ea\u3001\u533f\u540d\u95a2\u6570\u306b\u3064\u3044\u3066\u30c0\u30f3\u30d7\u3092\u51fa\u529b\u3059\u308b\u30b3\u30fc\u30c9\u3092\u53d6\u308a\u9664\u3044\u3066\u307f\u3088\u3046\uff01&#8221;\u3068\u8a00\u3063\u3066\u308b\u306e\u3060\u3068\u601d\u3046\u3002<br \/>\n\u3061\u306a\u307f\u306b\u3001\u3053\u306e\u7ae0\u306e\u6700\u5f8c\u306b\u5168\u30b3\u30fc\u30c9\u304c\u8f09\u3063\u3066\u308b\u304c\u3001\u305d\u308c\u306f\u53d6\u308a\u9664\u3044\u305f\u5f8c\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/em><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ready&gt; extern sin(x);\r\nRead extern:\r\ndeclare double @sin(double)\r\n\r\nready&gt; extern cos(x);\r\nRead extern:\r\ndeclare double @cos(double)\r\n\r\nready&gt; sin(1.0);\r\nRead top-level expression:\r\ndefine double @2() {\r\nentry:\r\n  ret double 0x3FEAED548F090CEE\r\n}\r\n\r\nEvaluated to 0.841471\r\n\r\nready&gt; def foo(x) sin(x)*sin(x) + cos(x)*cos(x);\r\nRead function definition:\r\ndefine double @foo(double %x) {\r\nentry:\r\n  %calltmp = call double @sin(double %x)\r\n  %multmp = fmul double %calltmp, %calltmp\r\n  %calltmp2 = call double @cos(double %x)\r\n  %multmp4 = fmul double %calltmp2, %calltmp2\r\n  %addtmp = fadd double %multmp, %multmp4\r\n  ret double %addtmp\r\n}\r\n\r\nready&gt; foo(4.0);\r\nRead top-level expression:\r\ndefine double @3() {\r\nentry:\r\n  %calltmp = call double @foo(double 4.000000e+00)\r\n  ret double %calltmp\r\n}\r\n\r\nEvaluated to 1.000000<\/pre>\n<p>\u3048\u3047\uff01\uff1fsin\u3084cos\u306b\u3064\u3044\u3066JIT\u306f\u3069\u3046\u3084\u3063\u3066\u77e5\u3063\u305f\u306e\uff1f<br \/>\n\u3053\u306e\u7b54\u3048\u306f\u610f\u5916\u306b\u3082\u7c21\u5358\u3067\u3042\u308b\u3002<br \/>\n\u3053\u306e\u4f8b\u3067\u306f\u3001\u307e\u305aJIT\u306f\u95a2\u6570\u306e\u51e6\u7406\u304b\u3089\u306f\u3058\u3081\u3066\u3001\u305d\u3057\u3066\u95a2\u6570\u547c\u3073\u51fa\u3057\u3092\u5f97\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089JIT\u306f\u3001\u3053\u306e\u95a2\u6570\u306f\u307e\u3060JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u3066\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306b\u6c17\u3065\u304d\u3001\u305d\u3057\u3066\u305d\u306e\u95a2\u6570\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u3072\u3068\u307e\u3068\u307e\u308a\u306e\u30eb\u30fc\u30c1\u30f3\u306e\u8d77\u52d5\u3092\u958b\u59cb\u3059\u308b\u3002<br \/>\n\u4e0a\u306e\u5834\u5408\u3060\u3068\u3001\u95a2\u6570\u672c\u6587\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u306a\u3044\u306e\u3067\u3001JIT\u306f\u4e07\u83ef\u93e1\u306e\u30d7\u30ed\u30bb\u30b9\u305d\u308c\u81ea\u8eab\u306e\u4e0a\u3067&#8221;dlsym(&#8220;sin&#8221;)&#8221;\u3092\u547c\u3093\u3067\u7d42\u308f\u308b\u3002<br \/>\n&#8220;sin&#8221;\u306fJIT\u306e\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u3067\u5b9a\u7fa9\u3055\u308c\u308b\u306e\u3067\u3001JIT\u306f\u305d\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u304a\u3051\u308b\u547c\u3073\u51fa\u3057\u3092\u3001libm\u306esin\u3092\u76f4\u63a5\u547c\u3073\u51fa\u3059\u3088\u3046\u53d6\u308a\u7e55\u3046\u3002<\/p>\n<p>LLVM\u306eJIT\u306f\u3001\u672a\u77e5\u306e\u95a2\u6570\u3092\u3069\u3046\u89e3\u6c7a\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u5236\u5fa1\u3059\u308b\u305f\u3081\u306e\u591a\u304f\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u63d0\u4f9b\u3059\u308b\u3002\uff08ExecutionEngine.h\u3092\u53c2\u7167\uff09<br \/>\n\u305d\u308c\u306f\u3001IR\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u30a2\u30c9\u30ec\u30b9\u306e\u53b3\u5bc6\u306a\u5bfe\u5fdc\u4ed8\u3051\u3092\u898f\u5b9a\u3059\u308b\u3053\u3068\uff08\u4f8b\u3048\u3070\u9759\u7684\u306a\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u5fdc\u4ed8\u3051\u305f\u3044\u3068\u304d\u306bLLVM\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306f\u4fbf\u5229\u3067\u3042\u308b\uff09\u3082\u3067\u304d\u308b\u3057\u3001\u95a2\u6570\u540d\u306b\u3088\u3063\u3066\u30aa\u30f3\u30b6\u30d5\u30e9\u30a4\u3067\u52d5\u7684\u306b\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u308b\u3057\u3001\u95a2\u6570\u304c\u6700\u521d\u306b\u547c\u3070\u308c\u308b\u307e\u3067JIT\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u9045\u3089\u305b\u308b\u4e8b\u3082\u3067\u304d\u308b\u3002<\/p>\n<p>\u305d\u306e\u6709\u7528\u6027\u306e\u4e2d\u3067\u3082\u3072\u3068\u304d\u308f\u9762\u767d\u3044\u306e\u306f\u3001\u547d\u4ee4\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4efb\u610f\u306eC++\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u8a00\u8a9e\u3092\u62e1\u5f35\u3067\u304d\u308b\u3068\u3044\u3046\u70b9\u3067\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u305f\u3068\u3059\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ putchard - double\u578b\u306e\u5f15\u65701\u3064\u3092\u3046\u3051\u3068\u308a0\u3092\u8fd4\u3059putchar\u3002\r\nextern &quot;C&quot;\r\ndouble putchard(double X) {\r\n  putchar((char)X);\r\n  return 0;\r\n}<\/pre>\n<p>\u305d\u3057\u305f\u3089\u3001&#8221;extern putchard(x); putchard(120);&#8221;\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u3078\u7c21\u5358\u306a\u51fa\u529b\u3092\u751f\u3058\u3055\u305b\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u30b3\u30f3\u30bd\u30fc\u30eb\u3078\u306f\u5c0f\u6587\u5b57\u306ex\u304c\u5370\u5b57\u3055\u308c\u308b\u3002\uff08120\u306f\u30a2\u30b9\u30ad\u30fc\u30b3\u30fc\u30c9\u306b\u304a\u3051\u308b&#8217;x&#8217;\u3092\u8868\u3059\u3002\uff09<br \/>\n\u4f3c\u305f\u3088\u3046\u306a\u30b3\u30fc\u30c9\u306f\u3001\u30d5\u30a1\u30a4\u30ebI\/O\u3084\u3001\u6a19\u6e96\u5165\u529b\u304b\u3089\u306e\u5165\u529b\u3084\u3001\u4e07\u83ef\u93e1\u306b\u304a\u3051\u308b\u305d\u306e\u4ed6\u8af8\u3005\u306e\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308b\u306e\u306b\u4f7f\u3048\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u3053\u306e\u7ae0\u306e\u6700\u5f8c\u306e\u5168\u30b3\u30fc\u30c9\u306b\u3001\u3059\u3067\u306bputchard\u306e\u5b9a\u7fa9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u4e07\u83ef\u93e1\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u5b9f\u884c\u3057\u3066\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u306b&#8221;extern putchard(x); putchard(120);&#8221;\u3068\u6253\u3061\u8fbc\u3080\u3060\u3051\u3067\u3001\u4e0a\u306e\u4f8b\u306f\u518d\u73fe\u51fa\u6765\u308b\u3002<\/em><\/p>\n<p>\u3053\u308c\u306b\u3066\u3001\u4e07\u83ef\u93e1\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306b\u304a\u3051\u308bJIT\u3068\u6700\u9069\u5316\u306b\u3064\u3044\u3066\u306e\u7ae0\u306f\u5b8c\u4e86\u3067\u3042\u308b\u3002<br \/>\n\u3053\u308c\u3067\u3001\u975e\u30c1\u30e5\u30fc\u30ea\u30f3\u30b0\u5b8c\u5168\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3092\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u4f7f\u7528\u6cd5\u6b21\u7b2c\u3067\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u305f\u308a\u3001\u6700\u9069\u5316\u3057\u305f\u308a\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002<br \/>\n\u6b21\u306e\u7ae0\u3067\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u8208\u5473\u6df1\u3044LLVM IR\u306e\u554f\u984c\u70b9\u306b\u7acb\u3061\u5411\u304b\u3044\u306a\u304c\u3089\u3001\u5236\u5fa1\u30d5\u30ed\u30fc\u69cb\u9020\u306b\u3088\u308b\u8a00\u8a9e\u306e\u62e1\u5f35\u306b\u3064\u3044\u3066\u898b\u3066\u307f\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"https:\/\/peta.okechan.net\/blog\/archives\/2836\" title=\"LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\">LLVM\u306b\u3088\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u5b9f\u88c5\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u65e5\u672c\u8a9e\u8a33<\/a><br \/>\n\u7b2c4\u7ae0 \u4e07\u83ef\u93e1: JIT\u306e\u8ffd\u52a0\u3068\u6700\u9069\u5316\u306e\u30b5\u30dd\u30fc\u30c8<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl04.html#adding-a-jit-compiler\" target=\"_blank\">\u7b2c4\u7bc0 JIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u8ffd\u52a0<\/a><\/p>\n<p>LLVM IR\u3067\u5f97\u3089\u308c\u308b\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u9069\u7528\u53ef\u80fd\u306a\u30c4\u30fc\u30eb\u306b\u306f\u3044\u308d\u3093\u306a\u7a2e\u985e\u306e\u3082\u306e\u304c\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\uff08\u524d\u7bc0\u3067\u3084\u3063\u305f\u3088\u3046\u306b\uff09LLVM IR\u306e\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u6700\u9069\u5316\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3060\u3057\u3001\u305d\u308c\u3092\u30c6\u30ad\u30b9\u30c8\u5f62\u5f0f\u307e\u305f\u306f\u30d0\u30a4\u30ca\u30ea\u3067\u30c0\u30f3\u30d7\u3057\u3066\u51fa\u529b\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u308b\u3057\u3001\u8272\u3093\u306a\u30bf\u30fc\u30b2\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u305d\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u30a2\u30bb\u30f3\u30d6\u30ea\u30d5\u30a1\u30a4\u30eb(.s)\u3092\u5f97\u308b\u4e8b\u3082\u3067\u304d\u308b\u3057\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u308b\u3002<br \/>\nLLVM IR\u8868\u73fe\u306e\u3044\u3044\u3068\u3053\u308d\u306f\u3001\u305d\u308c\u304c\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u304a\u3051\u308b\u591a\u304f\u306e\u30d1\u30fc\u30c4\u9593\u306b\u304a\u3051\u308b&#8221;\u5171\u901a\u8a00\u8a9e\u201d\u3067\u3042\u308b\u70b9\u3067\u3042\u308b\u3002<\/p>\n<p>\u3053\u306e\u7bc0\u3067\u306f\u3001\u6211\u3005\u306e\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306b\u5bfe\u3057\u3066JIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u30b5\u30dd\u30fc\u30c8\u3092\u8ffd\u52a0\u3059\u308b\u3002<br \/>\n\u305d\u308c\u3092\u4e07\u83ef\u93e1\u306b\u8ffd\u52a0\u3059\u308b\u305f\u3081\u306b\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u95a2\u6570\u3092\u5165\u529b\u3057\u7d42\u3048\u305f\u3089JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u5b9f\u884c\u3059\u308b\u3001\u3068\u3044\u3046\u3053\u3068\u3092\u57fa\u672c\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u3068\u3059\u308b\u3002<br \/>\n\u305f\u3060\u3057\u3001\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u306f\u5165\u529b\u3055\u308c\u305f\u3089\u305f\u3060\u3061\u306b\u8a55\u4fa1\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001&#8221;1+2;&#8221;\u3068\u5165\u529b\u3055\u308c\u305f\u3089\u3001\u305d\u308c\u3092\u8a55\u4fa1\u3057\u30663\u3068\u5370\u5b57\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u3082\u3057\u95a2\u6570\u304c\u5b9a\u7fa9\u3055\u308c\u305f\u3089\u3001\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u305d\u308c\u3092\u547c\u3079\u308b\u3088\u3046\u306b\u3059\u3079\u304d\u3067\u3042\u308b\u3002<\/p>\n<p>\u3053\u308c\u3092\u884c\u3046\u305f\u3081\u3001\u307e\u305a\u306fJIT\u3092\u5ba3\u8a00\u3057\u3066\u521d\u671f\u5316\u3059\u308b\u3002<br \/>\n\u3053\u308c\u3092\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3068main\u95a2\u6570\u306b\u304a\u3051\u308b\u95a2\u6570\u547c\u3073\u51fa\u3057\u306e\u8ffd\u52a0\u306b\u3088\u3063\u3066\u884c\u3046\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static ExecutionEngine *TheExecutionEngine;\r\n...\r\nint main() {\r\n  ..\r\n  \/\/ JIT\u306e\u751f\u6210\u3002Module\u306e\u6240\u6709\u6a29\u3092\u3068\u308b\u3002\r\n  TheExecutionEngine = EngineBuilder(TheModule).create();\r\n  ..\r\n}<\/pre>\n<p>\u3053\u308c\u306b\u3088\u3063\u3066\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u3082LLVM\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u306b\u3082\u306a\u308c\u308b\u3001\u62bd\u8c61\u7684\u306a&#8221;\u5b9f\u884c\u30a8\u30f3\u30b8\u30f3\uff08Execution Engine\uff09&#8221;\u304c\u751f\u6210\u3055\u308c\u308b\u3002<br \/>\n\u3042\u306a\u305f\u306e\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u306b\u304a\u3044\u3066\u5229\u7528\u53ef\u80fd\u306aJIT\u30b3\u30f3\u30d1\u30a4\u30e9\u304c\u3042\u308c\u3070\u3001LLVM\u306f\u81ea\u52d5\u7684\u306b\u305d\u308c\u3092\u9078\u629e\u3059\u308b\u3057\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u3078\u30d5\u30a9\u30fc\u30eb\u30d0\u30c3\u30af\u3059\u308b\u3002<\/p>\n<p>ExecutionEngine\u304c\u751f\u6210\u3055\u308c\u305f\u3089\u3001JIT\u3092\u5229\u7528\u3059\u308b\u6e96\u5099\u306f\u5b8c\u4e86\u3067\u3042\u308b\u3002<br \/>\n\u305f\u304f\u3055\u3093\u306e\u4fbf\u5229\u306aAPI\u304c\u305d\u308c\u306b\u306f\u5b58\u5728\u3059\u308b\u304c\u3001\u4e00\u756a\u30b7\u30f3\u30d7\u30eb\u306a\u306e\u306f&#8221;getPointerToFunction(F)&#8221;\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308b\u3002<br \/>\n\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u4e0e\u3048\u3089\u308c\u305fLLVM\u95a2\u6570\u306b\u5bfe\u3057\u3066JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u884c\u3044\u3001\u751f\u6210\u3055\u308c\u305f\u6a5f\u68b0\u8a9e\u3078\u306e\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u8fd4\u3059\u3002<br \/>\n\u6211\u3005\u306e\u5834\u5408\u3001\u3053\u308c\u306f\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u3092\u69cb\u6587\u89e3\u6790\u3059\u308b\u30b3\u30fc\u30c9\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u610f\u5473\u3059\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static void HandleTopLevelExpression() {\r\n  \/\/ \u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u3092\u8a55\u4fa1\u3057\u3001\u7121\u540d\u95a2\u6570\u306b\u3059\u308b\u3002\r\n  if (FunctionAST *F = ParseTopLevelExpr()) {\r\n    if (Function *LF = F-&gt;Codegen()) {\r\n      LF-&gt;dump();  \/\/ \u8aac\u660e\u76ee\u7684\u3068\u3057\u3066\u95a2\u6570\u3092\u30c0\u30f3\u30d7\u3059\u308b\u3002\r\n\r\n      \/\/ \u95a2\u6570\u3092JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3001\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u8fd4\u3059\u3002\r\n      void *FPtr = TheExecutionEngine-&gt;getPointerToFunction(LF);\r\n\r\n      \/\/ \u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u3092\u6b63\u3057\u3044\u578b(\u5f15\u6570\u306a\u3057\u3001 double\u3092\u8fd4\u3059)\u3078\u5909\u63db\u3059\u308b\u3002\r\n      \/\/ \u306a\u306e\u3067\u3053\u306e\u95a2\u6570\u306f\u30cd\u30a4\u30c6\u30a3\u30d6\u306e\u95a2\u6570\u3068\u3057\u3066\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u53ef\u80fd\u3002\r\n      double (*FP)() = (double (*)())(intptr_t)FPtr;\r\n      fprintf(stderr, &quot;Evaluated to %f\\n&quot;, FP());\r\n    }<\/pre>\n<p>\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u3092\u3001&#8221;\u5f15\u6570\u306a\u3057\u3067double\u3092\u8fd4\u3059LLVM\u95a2\u6570&#8221;\u3078\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u4e8b\u3092\u601d\u3044\u51fa\u3057\u3066\u6b32\u3057\u3044\u3002<br \/>\nLLVM\u306eJIT\u30b3\u30f3\u30d1\u30a4\u30e9\u306f\u3001\u30cd\u30a4\u30c6\u30a3\u30d6\u306a\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0ABI\uff08Application Binary Interface\uff09\u306b\u9069\u5408\u3059\u308b\u306e\u3067\u3001getPointerToFunction\u3067\u5f97\u3089\u308c\u305f\u30dd\u30a4\u30f3\u30bf\u3092\u95a2\u6570\u30dd\u30a4\u30f3\u30bf\u306b\u30ad\u30e3\u30b9\u30c8\u3067\u304d\u3001\u76f4\u63a5\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u3055\u3089\u306b\u305d\u308c\u306f\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u30b3\u30fc\u30c9\u3068\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u9759\u7684\u30ea\u30f3\u30af\u3055\u308c\u305f\u30cd\u30a4\u30c6\u30a3\u30d6\u306a\u6a5f\u68b0\u8a9e\u306e\u9593\u306b\u9055\u3044\u304c\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3082\u610f\u5473\u3059\u308b\u3002<\/p>\n<p>\u4ee5\u4e0a\u306e\u305f\u3063\u305f2\u3064\u306e\u5909\u66f4\u306b\u3088\u3063\u3066\u3001\u4e07\u83ef\u93e1\u304c\u3069\u3046\u52d5\u304f\u3088\u3046\u306b\u306a\u3063\u305f\u304b\u3092\u898b\u3066\u307f\u3088\u3046\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ready&gt; 4+5;\r\nRead top-level expression:\r\ndefine double @0() {\r\nentry:\r\n  ret double 9.000000e+00\r\n}\r\n\r\nEvaluated to 9.000000<\/pre>\n<p>\u57fa\u672c\u7684\u306b\u306f\u3061\u3083\u3093\u3068\u52d5\u3044\u3066\u308b\u3088\u3046\u3067\u3042\u308b\u3002<br \/>\n\u95a2\u6570\u306e\u30c0\u30f3\u30d7\u306f\u3001\u5165\u529b\u3055\u308c\u305f\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\u304c\u7dcf\u5408\u3055\u308c\u3001&#8221;\u5f15\u6570\u304c\u306a\u304f\u5e38\u306bdouble\u306e\u5024\u3092\u8fd4\u3059\u95a2\u6570&#8221;\u3068\u306a\u3063\u305f\u3068\u3044\u3046\u4e8b\u3092\u793a\u3057\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u30c7\u30e2\u306f\u3001\u975e\u5e38\u306b\u57fa\u672c\u7684\u306a\u6a5f\u80fd\u306b\u3064\u3044\u3066\u306e\u3082\u306e\u3067\u3042\u308b\u3002<br \/>\n\u3057\u304b\u3057\u3082\u3063\u3068\u8907\u96d1\u306a\u4f8b\u3067\u306f\u3069\u3046\u3060\u308d\u3046\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ready&gt; def testfunc(x y) x + y*2;\r\nRead function definition:\r\ndefine double @testfunc(double %x, double %y) {\r\nentry:\r\n  %multmp = fmul double %y, 2.000000e+00\r\n  %addtmp = fadd double %multmp, %x\r\n  ret double %addtmp\r\n}\r\n\r\nready&gt; testfunc(4, 10);\r\nRead top-level expression:\r\ndefine double @1() {\r\nentry:\r\n  %calltmp = call double @testfunc(double 4.000000e+00, double 1.000000e+01)\r\n  ret double %calltmp\r\n}\r\n\r\nEvaluated to 24.000000<\/pre>\n<p>\u3053\u308c\u306f\u3001\u30e6\u30fc\u30b6\u5b9a\u7fa9\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3068\u3053\u308d\u3092\u793a\u3057\u3066\u3044\u308b\u304c\u3001\u3061\u3087\u3063\u3068\u5206\u304b\u308a\u306b\u304f\u3044\u70b9\u304c\u3042\u308b\u3002<br \/>\ntestfunc\u306e\u547c\u3073\u51fa\u3057\u3092\u884c\u3046\u7121\u540d\u95a2\u6570\uff08\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u5f0f\uff09\u306b\u3064\u3044\u3066\u306e\u307fJIT\u306e\u547c\u3073\u51fa\u3057\u3092\u884c\u3063\u3066\u3044\u3066\u3001testfunc\u305d\u306e\u3082\u306e\u3067\u306fJIT\u306e\u547c\u3073\u51fa\u3057\u306f\u884c\u308f\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\nJIT\u306f\u3001\u533f\u540d\u95a2\u6570\u304b\u3089\u547c\u3073\u51fa\u3055\u308c\u3066\u3044\u308b\u5168\u3066\u306e\u672aJIT\u306e\u95a2\u6570\u3092\u30b9\u30ad\u30e3\u30f3\u3057\u3001\u305d\u3057\u3066getPointerToFunction()\u304b\u3089\u623b\u308b\u524d\u306b\u672aJIT\u306e\u95a2\u6570\u5168\u3066\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u8fd4\u3059\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u5b9f\u969b\u306b\u306f\u884c\u308f\u308c\u3066\u3044\u308b\u3002<\/p>\n<p>JIT\u306b\u306f\u3001\u5272\u308a\u5f53\u3066\u6e08\u307f\u306e\u6a5f\u68b0\u8a9e\u306e\u89e3\u653e\u3084\u3001\u95a2\u6570\u306e\u66f4\u65b0\u306b\u5408\u308f\u305b\u3066\u518dJIT\u3059\u308b\u3088\u3046\u306a\u3001\u3088\u308a\u9032\u3093\u3060\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u304c\u3044\u304f\u3064\u304b\u3042\u308b\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u306f\u3001\u305d\u306e\u3088\u3046\u306a\u3082\u306e\u3092\u4f7f\u308f\u306a\u304f\u3066\u3082\u9a5a\u304f\u3079\u304d\u30d1\u30ef\u30d5\u30eb\u306a\u80fd\u529b\u3092\u5f97\u3066\u3044\u308b\u3002<br \/>\n\u4ee5\u4e0b\u3092\u78ba\u8a8d\u3057\u3066\u307f\u3088\u3046\u3002\uff08\u79c1\u306f\u533f\u540d\u95a2\u6570\u306b\u3064\u3044\u3066\u306e\u30c0\u30f3\u30d7\u3092\u51fa\u529b\u3059\u308b\u30b3\u30fc\u30c9\u3092\u53d6\u308a\u9664\u3044\u305f\u3002\u4eca\u306e\u3042\u306a\u305f\u306a\u3089\u3001\u305d\u3046\u3059\u308b\u305f\u3081\u306b\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u304b\u306f\u5206\u304b\u308b\u3060\u308d\u3046\u3002\uff09<br \/>\n<em>\u8a33\u6ce8: \u3053\u306e\u6642\u70b9\u3067\u306f\u3001\u533f\u540d\u95a2\u6570\u306b\u3064\u3044\u3066\u30c0\u30f3\u30d7\u3092\u51fa\u529b\u3059\u308b\u30b3\u30fc\u30c9\u53d6\u308a\u9664\u304b\u308c\u3066\u3044\u306a\u3044\u3002\uff08\u306a\u306e\u3067\u4ee5\u4e0b\u306e\u7d50\u679c\u306b\u3082\u30c0\u30f3\u30d7\u306f\u6b8b\u3063\u305f\u307e\u307e\u3002\uff09<br \/>\n\u591a\u5206\u3001\u3061\u3087\u3063\u3068\u3057\u305f\u6f14\u7fd2\u3068\u3057\u3066&#8221;\u5404\u81ea\u3001\u533f\u540d\u95a2\u6570\u306b\u3064\u3044\u3066\u30c0\u30f3\u30d7\u3092\u51fa\u529b\u3059\u308b\u30b3\u30fc\u30c9\u3092\u53d6\u308a\u9664\u3044\u3066\u307f\u3088\u3046\uff01&#8221;\u3068\u8a00\u3063\u3066\u308b\u306e\u3060\u3068\u601d\u3046\u3002<br \/>\n\u3061\u306a\u307f\u306b\u3001\u3053\u306e\u7ae0\u306e\u6700\u5f8c\u306b\u5168\u30b3\u30fc\u30c9\u304c\u8f09\u3063\u3066\u308b\u304c\u3001\u305d\u308c\u306f\u53d6\u308a\u9664\u3044\u305f\u5f8c\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/em><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ready&gt; extern sin(x);\r\nRead extern:\r\ndeclare double @sin(double)\r\n\r\nready&gt; extern cos(x);\r\nRead extern:\r\ndeclare double @cos(double)\r\n\r\nready&gt; sin(1.0);\r\nRead top-level expression:\r\ndefine double @2() {\r\nentry:\r\n  ret double 0x3FEAED548F090CEE\r\n}\r\n\r\nEvaluated to 0.841471\r\n\r\nready&gt; def foo(x) sin(x)*sin(x) + cos(x)*cos(x);\r\nRead function definition:\r\ndefine double @foo(double %x) {\r\nentry:\r\n  %calltmp = call double @sin(double %x)\r\n  %multmp = fmul double %calltmp, %calltmp\r\n  %calltmp2 = call double @cos(double %x)\r\n  %multmp4 = fmul double %calltmp2, %calltmp2\r\n  %addtmp = fadd double %multmp, %multmp4\r\n  ret double %addtmp\r\n}\r\n\r\nready&gt; foo(4.0);\r\nRead top-level expression:\r\ndefine double @3() {\r\nentry:\r\n  %calltmp = call double @foo(double 4.000000e+00)\r\n  ret double %calltmp\r\n}\r\n\r\nEvaluated to 1.000000<\/pre>\n<p>\u3048\u3047\uff01\uff1fsin\u3084cos\u306b\u3064\u3044\u3066JIT\u306f\u3069\u3046\u3084\u3063\u3066\u77e5\u3063\u305f\u306e\uff1f<br \/>\n\u3053\u306e\u7b54\u3048\u306f\u610f\u5916\u306b\u3082\u7c21\u5358\u3067\u3042\u308b\u3002<br \/>\n\u3053\u306e\u4f8b\u3067\u306f\u3001\u307e\u305aJIT\u306f\u95a2\u6570\u306e\u51e6\u7406\u304b\u3089\u306f\u3058\u3081\u3066\u3001\u305d\u3057\u3066\u95a2\u6570\u547c\u3073\u51fa\u3057\u3092\u5f97\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089JIT\u306f\u3001\u3053\u306e\u95a2\u6570\u306f\u307e\u3060JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u3066\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306b\u6c17\u3065\u304d\u3001\u305d\u3057\u3066\u305d\u306e\u95a2\u6570\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u3072\u3068\u307e\u3068\u307e\u308a\u306e\u30eb\u30fc\u30c1\u30f3\u306e\u8d77\u52d5\u3092\u958b\u59cb\u3059\u308b\u3002<br \/>\n\u4e0a\u306e\u5834\u5408\u3060\u3068\u3001\u95a2\u6570\u672c\u6587\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u306a\u3044\u306e\u3067\u3001JIT\u306f\u4e07\u83ef\u93e1\u306e\u30d7\u30ed\u30bb\u30b9\u305d\u308c\u81ea\u8eab\u306e\u4e0a\u3067&#8221;dlsym(&#8220;sin&#8221;)&#8221;\u3092\u547c\u3093\u3067\u7d42\u308f\u308b\u3002<br \/>\n&#8220;sin&#8221;\u306fJIT\u306e\u30a2\u30c9\u30ec\u30b9\u7a7a\u9593\u3067\u5b9a\u7fa9\u3055\u308c\u308b\u306e\u3067\u3001JIT\u306f\u305d\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u304a\u3051\u308b\u547c\u3073\u51fa\u3057\u3092\u3001libm\u306esin\u3092\u76f4\u63a5\u547c\u3073\u51fa\u3059\u3088\u3046\u53d6\u308a\u7e55\u3046\u3002<\/p>\n<p>LLVM\u306eJIT\u306f\u3001\u672a\u77e5\u306e\u95a2\u6570\u3092\u3069\u3046\u89e3\u6c7a\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u5236\u5fa1\u3059\u308b\u305f\u3081\u306e\u591a\u304f\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3092\u63d0\u4f9b\u3059\u308b\u3002\uff08ExecutionEngine.h\u3092\u53c2\u7167\uff09<br \/>\n\u305d\u308c\u306f\u3001IR\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u30a2\u30c9\u30ec\u30b9\u306e\u53b3\u5bc6\u306a\u5bfe\u5fdc\u4ed8\u3051\u3092\u898f\u5b9a\u3059\u308b\u3053\u3068\uff08\u4f8b\u3048\u3070\u9759\u7684\u306a\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u5fdc\u4ed8\u3051\u305f\u3044\u3068\u304d\u306bLLVM\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306f\u4fbf\u5229\u3067\u3042\u308b\uff09\u3082\u3067\u304d\u308b\u3057\u3001\u95a2\u6570\u540d\u306b\u3088\u3063\u3066\u30aa\u30f3\u30b6\u30d5\u30e9\u30a4\u3067\u52d5\u7684\u306b\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u308b\u3057\u3001\u95a2\u6570\u304c\u6700\u521d\u306b\u547c\u3070\u308c\u308b\u307e\u3067JIT\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u9045\u3089\u305b\u308b\u4e8b\u3082\u3067\u304d\u308b\u3002<\/p>\n<p>\u305d\u306e\u6709\u7528\u6027\u306e\u4e2d\u3067\u3082\u3072\u3068\u304d\u308f\u9762\u767d\u3044\u306e\u306f\u3001\u547d\u4ee4\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4efb\u610f\u306eC++\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u8a00\u8a9e\u3092\u62e1\u5f35\u3067\u304d\u308b\u3068\u3044\u3046\u70b9\u3067\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u305f\u3068\u3059\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/\/ putchard - double\u578b\u306e\u5f15\u65701\u3064\u3092\u3046\u3051\u3068\u308a0\u3092\u8fd4\u3059putchar\u3002\r\nextern &quot;C&quot;\r\ndouble putchard(double X) {\r\n  putchar((char)X);\r\n  return 0;\r\n}<\/pre>\n<p>\u305d\u3057\u305f\u3089\u3001&#8221;extern putchard(x); putchard(120);&#8221;\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u3078\u7c21\u5358\u306a\u51fa\u529b\u3092\u751f\u3058\u3055\u305b\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u30b3\u30f3\u30bd\u30fc\u30eb\u3078\u306f\u5c0f\u6587\u5b57\u306ex\u304c\u5370\u5b57\u3055\u308c\u308b\u3002\uff08120\u306f\u30a2\u30b9\u30ad\u30fc\u30b3\u30fc\u30c9\u306b\u304a\u3051\u308b&#8217;x&#8217;\u3092\u8868\u3059\u3002\uff09<br \/>\n\u4f3c\u305f\u3088\u3046\u306a\u30b3\u30fc\u30c9\u306f\u3001\u30d5\u30a1\u30a4\u30ebI\/O\u3084\u3001\u6a19\u6e96\u5165\u529b\u304b\u3089\u306e\u5165\u529b\u3084\u3001\u4e07\u83ef\u93e1\u306b\u304a\u3051\u308b\u305d\u306e\u4ed6\u8af8\u3005\u306e\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308b\u306e\u306b\u4f7f\u3048\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u3053\u306e\u7ae0\u306e\u6700\u5f8c\u306e\u5168\u30b3\u30fc\u30c9\u306b\u3001\u3059\u3067\u306bputchard\u306e\u5b9a\u7fa9\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u4e07\u83ef\u93e1\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u5b9f\u884c\u3057\u3066\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u306b&#8221;extern putchard(x); putchard(120);&#8221;\u3068\u6253\u3061\u8fbc\u3080\u3060\u3051\u3067\u3001\u4e0a\u306e\u4f8b\u306f\u518d\u73fe\u51fa\u6765\u308b\u3002<\/em><\/p>\n<p>\u3053\u308c\u306b\u3066\u3001\u4e07\u83ef\u93e1\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306b\u304a\u3051\u308bJIT\u3068\u6700\u9069\u5316\u306b\u3064\u3044\u3066\u306e\u7ae0\u306f\u5b8c\u4e86\u3067\u3042\u308b\u3002<br \/>\n\u3053\u308c\u3067\u3001\u975e\u30c1\u30e5\u30fc\u30ea\u30f3\u30b0\u5b8c\u5168\u306a\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3092\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u4f7f\u7528\u6cd5\u6b21\u7b2c\u3067\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u305f\u308a\u3001\u6700\u9069\u5316\u3057\u305f\u308a\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002<br \/>\n\u6b21\u306e\u7ae0\u3067\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u8208\u5473\u6df1\u3044LLVM IR\u306e\u554f\u984c\u70b9\u306b\u7acb\u3061\u5411\u304b\u3044\u306a\u304c\u3089\u3001\u5236\u5fa1\u30d5\u30ed\u30fc\u69cb\u9020\u306b\u3088\u308b\u8a00\u8a9e\u306e\u62e1\u5f35\u306b\u3064\u3044\u3066\u898b\u3066\u307f\u308b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[466,467],"class_list":["post-2958","post","type-post","status-publish","format-standard","hentry","category-tech","tag-kaleidoscope","tag-llvm"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=2958"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2958\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=2958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=2958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=2958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}