{"id":2932,"date":"2013-08-23T10:27:11","date_gmt":"2013-08-23T01:27:11","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=2932"},"modified":"2013-08-23T10:27:11","modified_gmt":"2013-08-23T01:27:11","slug":"3-3-%e5%bc%8f%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%89%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/2932","title":{"rendered":"3.3. \u5f0f\u306e\u30b3\u30fc\u30c9\u751f\u6210"},"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\u7b2c3\u7ae0 \u4e07\u83ef\u93e1: LLVM IR\u30b3\u30fc\u30c9\u306e\u751f\u6210<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl03.html#expression-code-generation\" target=\"_blank\">\u7b2c3\u7bc0 \u5f0f\u306e\u30b3\u30fc\u30c9\u751f\u6210<\/a><\/p>\n<p>\u5f0f\u30ce\u30fc\u30c9\u306b\u306e\u305f\u3081\u306eLLVM\u30b3\u30fc\u30c9\u306e\u751f\u6210\u306f\u3001\u3068\u3066\u3082\u76f4\u63a5\u7684\u306b\u884c\u3048\u308b\u3002<br \/>\n\u6211\u3005\u306e4\u7a2e\u985e\u5168\u3066\u306e\u5f0f\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u306e\u30b3\u30fc\u30c9\u306f\u3001\u30b3\u30e1\u30f3\u30c8\u5165\u308a\u306745\u884c\u4ee5\u4e0b\u3067\u3042\u308b\u3002<br \/>\n\u307e\u305a\u6570\u5024\u30ea\u30c6\u30e9\u30eb\u304b\u3089\u59cb\u3081\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *NumberExprAST::Codegen() {\r\n  return ConstantFP::get(getGlobalContext(), APFloat(Val));\r\n}<\/pre>\n<p>LLVM IR\u3067\u306f\u3001\u6570\u5024\u5b9a\u6570\u306fConstantFP\u30af\u30e9\u30b9\u306b\u3088\u3063\u3066\u8868\u73fe\u3055\u308c\u308b\u3002<br \/>\nConstantFP\u30af\u30e9\u30b9\u306f\u3001\u305d\u306e\u5185\u90e8\u306bAPFloat\u3092\u6301\u3061\u3001\u305d\u306e\u4e2d\u3067\u6570\u5024\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\uff08APFloat\u306b\u306f\u4efb\u610f\u306e\u7cbe\u5ea6\u306e\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u5b9a\u6570\u3092\u4fdd\u6301\u3059\u308b\u80fd\u529b\u304c\u3042\u308b\u3002\uff09<br \/>\n\u3053\u306e\u30b3\u30fc\u30c9\u306f\u57fa\u672c\u7684\u306b\u3001ConstantFP\u3092\u751f\u6210\u3057\u3001\u8fd4\u3057\u3066\u308b\u3060\u3051\u3067\u3042\u308b\u3002<br \/>\nLLVM IR\u3067\u306f\u3001\u5b9a\u6570\u306f\u3001\u3059\u3079\u3066\u304a\u4e92\u3044\u306b\u30e6\u30cb\u30fc\u30af\u3067\u3042\u308a\u5171\u6709\u3055\u308c\u308b\u70b9\u306b\u6ce8\u610f\u3002<br \/>\n\u306a\u306e\u3067\u3001&#8221;new foo(\u2026)&#8221;\u3068\u304b&#8221;foo::Create(\u2026)&#8221;\u3067\u306f\u306a\u304f\u3001&#8221;foo::get(\u2026)&#8221;\u3068\u3044\u3046\u8868\u73fe\u3092API\u306f\u7528\u3044\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *VariableExprAST::Codegen() {\r\n  \/\/ \u95a2\u6570\u306e\u4e2d\u304b\u3089\u3053\u306e\u5909\u6570\u3092\u63a2\u3059\r\n  Value *V = NamedValues&#x5B;Name];\r\n  return V ? V : ErrorV(&quot;Unknown variable name&quot;);\r\n}<\/pre>\n<p>\u5909\u6570\u3078\u306e\u53c2\u7167\u3082\u307e\u305f\u3001LLVM\u306b\u304a\u3044\u3066\u306f\u7c21\u5358\u3067\u3042\u308b\u3002<br \/>\n\u7c21\u6613\u7248\u306e\u4e07\u83ef\u93e1\u3067\u306f\u3001\u5909\u6570\u306f\u3059\u3067\u306b\u3069\u3053\u304b\u3067\u767a\u884c\u6e08\u307f\u3067\u3042\u308a\u3001\u305d\u306e\u5024\u304c\u4f7f\u7528\u3067\u304d\u308b\u72b6\u614b\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3057\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u6bb5\u968e\u3067\u306f\u3001NamedValues\u30de\u30c3\u30d7\u306e\u4e2d\u306b\u5b58\u5728\u3057\u3046\u308b\u5024\u306f\u3001\u95a2\u6570\u306e\u5f15\u6570\u3060\u3051\u3067\u3042\u308b\u3002<br \/>\n\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u540d\u524d\u304c\u30de\u30c3\u30d7\u306e\u306a\u304b\u306b\u5b58\u5728\u3059\u308b\u304b\u5358\u7d14\u306b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u3001\u305d\u306e\u5024\u3092\u8fd4\u3057\u3066\u3044\u308b\u3002<br \/>\n\uff08\u3082\u3057\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u3001\u4e0d\u660e\u306a\u5909\u6570\u304c\u53c2\u7167\u3055\u308c\u305f\u3053\u3068\u306b\u306a\u308b\u3002\uff09<br \/>\n\u5f8c\u306e\u7ae0\u3067\u3001\u30eb\u30fc\u30d7\u5e30\u7d0d\u5909\u6570\uff08loop induction variables\uff09\u3068\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u306e\u30b5\u30dd\u30fc\u30c8\u3092\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306b\u8ffd\u52a0\u3059\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u30eb\u30fc\u30d7\u5e30\u7d0d\u5909\u6570\u306f\u30eb\u30fc\u30d7\u5909\u6570\u306e\u4e8b\u3067\u3042\u308b\u304c\u3001\u5f8c\u306e\u7ae0\u3067\u8ffd\u52a0\u3059\u308b\u6700\u9069\u5316\u30b5\u30dd\u30fc\u30c8\u7d61\u307f\uff08\u5e30\u7d0d\u5909\u6570\u89e3\u6790\u3068\u304b\uff09\u3067\u6562\u3048\u3066\u5e30\u7d0d\u3068\u3044\u3046\u8a00\u8449\u304c\u4f7f\u308f\u308c\u3066\u3044\u308b\u3093\u3060\u3068\u601d\u3046\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *BinaryExprAST::Codegen() {\r\n  Value *L = LHS-&gt;Codegen();\r\n  Value *R = RHS-&gt;Codegen();\r\n  if (L == 0 || R == 0) return 0;\r\n\r\n  switch (Op) {\r\n  case '+': return Builder.CreateFAdd(L, R, &quot;addtmp&quot;);\r\n  case '-': return Builder.CreateFSub(L, R, &quot;subtmp&quot;);\r\n  case '*': return Builder.CreateFMul(L, R, &quot;multmp&quot;);\r\n  case '&lt;':\r\n    L = Builder.CreateFCmpULT(L, R, &quot;cmptmp&quot;);\r\n    \/\/ bool\u306e0\/1\u3092double\u306e0.0\/1.0\u306b\u5909\u63db\u3059\u308b\u3002\r\n    return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),\r\n                                &quot;booltmp&quot;);\r\n  default: return ErrorV(&quot;invalid binary operator&quot;);\r\n  }\r\n}<\/pre>\n<p>\u4e8c\u9805\u6f14\u7b97\u5b50\u306b\u3064\u3044\u3066\u306f\u3088\u308a\u8208\u5473\u6df1\u3044\u3002<br \/>\n\u57fa\u672c\u7684\u306b\u306f\u3001\u5f0f\u306e\u5de6\u624b\u5074\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u751f\u6210\u3057\u3001\u305d\u308c\u304b\u3089\u53f3\u624b\u5074\u3092\u51e6\u7406\u3057\u3001\u305d\u3057\u3066\u4e8c\u9805\u6f14\u7b97\u5f0f\uff08binary expression\uff09\u306e\u7d50\u679c\u3092\u8a08\u7b97\u3059\u308b\u3001\u3068\u3044\u3046\u30a2\u30a4\u30c7\u30a2\u306b\u57fa\u3065\u3044\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u6b63\u3057\u3044LLVM\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u6f14\u7b97\u5b50\u3092\u898b\u3066switch\u6587\u3067\u5358\u7d14\u306b\u51e6\u7406\u3092\u5207\u308a\u66ff\u3048\u3066\u3044\u308b\u3002<\/p>\n<p>\u4e0a\u306e\u4f8b\u3067\u306f\u3001LLVM Builder\u30af\u30e9\u30b9\u304c\u3001\u305d\u306e\u5024\u3092\u898b\u306f\u3058\u3081\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3042\u308b\u3002\uff08the LLVM builder class is starting to show its value.\uff09<br \/>\nIRBuilder\u306f\u3001\u76f4\u8fd1\u3067\u751f\u6210\u3057\u305f\u547d\u4ee4\u3092\u3069\u3053\u306b\u633f\u5165\u3057\u305f\u304b\u3092\u77e5\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u3042\u306a\u305f\u304c\u3059\u3079\u304d\u3053\u3068\u306f\u3069\u306e\u547d\u4ee4\u3092\uff08\u4f8b\u3048\u3070CreateFAdd\uff09\u751f\u6210\u3059\u308b\u306e\u304b\u3001\u3069\u306e\u30aa\u30da\u30e9\u30f3\u30c9\uff08\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3067\u306fL\u3084R\u3068\u306a\u3063\u3066\u308b\u90e8\u5206\uff09\u3092\u4f7f\u7528\u3059\u308b\u306e\u304b\u3001\u305d\u3057\u3066\u5fc5\u9808\u3067\u306f\u306a\u3044\u304c\u305d\u306e\u540d\u524d\u3092\u6307\u5b9a\u3059\u308b\u4e8b\u3060\u3051\u3067\u3042\u308b\u3002<\/p>\n<p>LLVM\u306e\u7d20\u6674\u3089\u3057\u3044\u70b9\u306f\u3001\u3042\u304f\u307e\u3067\u3082\u305d\u306e\u540d\u524d\u306f\u30d2\u30f3\u30c8\u306b\u904e\u304e\u306a\u3044\u3068\u3044\u3046\u4e8b\u3067\u3042\u308b\u3002<br \/>\n\u4e8b\u5b9f\u3001\u4e0a\u306e\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u8907\u6570\u306e&#8221;addtmp&#8221;\u5909\u6570\u304c\u751f\u6210\u3055\u308c\u305f\u5834\u5408\u3001LLVM\u306f\u81ea\u52d5\u7684\u306b\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3055\u308c\u305f\u30e6\u30cb\u30fc\u30af\u306a\u901a\u3057\u756a\u53f7\u3092\u305d\u306e\u63a5\u5c3e\u8f9e\u3068\u3057\u3066\u8ffd\u52a0\u3059\u308b\u3002<br \/>\n\u547d\u4ee4\u306b\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u540d\u3092\u4ed8\u3051\u308b\u304b\u3069\u3046\u304b\u306f\u5168\u304f\u81ea\u7531\u3067\u3042\u308b\u304c\u3001IR\u306e\u30c0\u30f3\u30d7\u3092\u8aad\u3080\u3068\u304d\u306b\u697d\u306b\u306a\u308b\u3060\u308d\u3046\u3002<\/p>\n<p>LLVM\u306e\u547d\u4ee4\u306b\u306f\u3001\u53b3\u683c\u306a\u30eb\u30fc\u30eb\u304c\u5f37\u5236\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u52a0\u7b97\u547d\u4ee4\u306e\u5de6\u5074\u3068\u53f3\u5074\u30aa\u30da\u30e9\u30f3\u30c9\u306f\u3001\u540c\u3058\u578b\uff08type\uff09\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u305d\u3057\u3066\u3001\u623b\u308a\u5024\u306e\u578b\u306f\u3001\u30aa\u30da\u30e9\u30f3\u30c9\u306e\u578b\u3068\u5408\u81f4\u3057\u3066\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u4e07\u83ef\u93e1\u3067\u306f\u5168\u3066\u306e\u5024\u304cdouble\u306a\u306e\u3067\u3001\u52a0\u7b97\u3084\u6e1b\u7b97\u3001\u4e57\u7b97\u306e\u30b3\u30fc\u30c9\u306f\u3068\u3066\u3082\u30b7\u30f3\u30d7\u30eb\u306b\u306a\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: LLVM IR\u306b\u304a\u3051\u308b\u52a0\u7b97\u547d\u4ee4\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = add &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt;<\/pre>\n<p>op1, op2\u304c\u5de6\u5074\u3001\u53f3\u5074\u30aa\u30da\u30e9\u30f3\u30c9\u3067\u3001ty\u304c\u578b\u3001result\u304c\u305d\u306e\u7d50\u679c\u3067\u3042\u308b\u3002<br \/>\n\u539f\u6587\u306f\u3001op1\u3068op2\u306e\u578b\u304c\u540c\u3058\u3067\u3001\u3055\u3089\u306b2\u3064\u306e\u30aa\u30da\u30e9\u30f3\u30c9\u3068result\u304c\u540c\u3058\u578b\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u4e8b\u3092\u8a00\u3063\u3066\u308b\u3093\u3060\u3068\u601d\u3046\u3002<br \/>\nLLVM\u306e\u547d\u4ee4\u306e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306f\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u306b\u3042\u308b\u3002<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/LangRef.html\" target=\"_blank\">http:\/\/llvm.org\/docs\/LangRef.html<\/a><\/em><\/p>\n<p>\u4e00\u65b9\u3001LLVM\u3067\u306f\u3001fcmp\u547d\u4ee4\u306f\u5e38\u306b&#8217;i1&#8217;\u578b\uff081bit\u6574\u6570\uff09\u306e\u5024\u3092\u8fd4\u3059\u3068\u5b9a\u3081\u3089\u308c\u3066\u3044\u308b\u3002<br \/>\n\u4e07\u83ef\u93e1\u3067\u306f\u3001\u3053\u308c\u30920.0\u30681.0\u3068\u3044\u3046\u5024\u3067\u6271\u3044\u305f\u3044\u3002<br \/>\n\u3053\u306e\u3088\u3046\u306a\u52d5\u4f5c\u3092\u3055\u305b\u308b\u305f\u3081\u306b\u3001fcmp\u547d\u4ee4\u3092uitofp\u547d\u4ee4\u3068\u4e00\u7dd2\u306b\u4f7f\u3063\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u547d\u4ee4\u306f\u3001\u5165\u529b\u3055\u308c\u305f\u6574\u6570\u3092\u7b26\u5408\u306a\u3057\u6574\u6570\u3068\u3057\u3066\u6271\u3044\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3078\u5909\u63db\u3059\u308b\u3002<br \/>\n\u3082\u3057\u3001uitofp\u3067\u306f\u7121\u304fsitofp\u547d\u4ee4\u3092\u4f7f\u3063\u305f\u3068\u3057\u305f\u3089\u3001\u4e07\u83ef\u93e1\u306e&#8221;<\"\u6f14\u7b97\u5b50\u306f\u3001\u5165\u529b\u5024\u306b\u5fdc\u3058\u30660.0\u304b-1.0\u306e\u5024\u3092\u8fd4\u3057\u3066\u3057\u307e\u3046\u3060\u308d\u3046\u3002\n<em>\u8a33\u6ce8: fcmp\u547d\u4ee4\u306f\u4ee5\u4e0b\u306e\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = fcmp &lt;cond&gt; &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt;<\/pre>\n<p>cond\u306b\u306f\u3001\u6f14\u7b97\u5b50\u3092\u8868\u3059\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u6307\u5b9a\u3059\u308b\u3002<br \/>\n\u4f8b\u3048\u3070&#8221;\u5c0f\u306a\u308a\u30a4\u30b3\u30fc\u30eb&#8221;\u306a\u3089\u3001&#8221;ole&#8221;\u3092\u6307\u5b9a\u3059\u308b\u3001\u3068\u3044\u3046\u98a8\u306b\u30ad\u30fc\u30ef\u30fc\u30c9\u304c\u6c7a\u307e\u3063\u3066\u3044\u308b\u3002<br \/>\n\u3042\u3068\u306f\u3001add\u3068\u540c\u3058\u3060\u304c\u3001\u7d50\u679c\u304c1bit\u6574\u6570\u3067\u8fd4\u3063\u3066\u304f\u308b\u70b9\u306b\u6ce8\u610f\u3002<br \/>\nuitofp\u3068sitofp\u306f\u4ee5\u4e0b\u306e\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = uitofp &lt;ty&gt; &lt;value&gt; to &lt;ty2&gt;\r\n&lt;result&gt; = sitofp &lt;ty&gt; &lt;value&gt; to &lt;ty2&gt;<\/pre>\n<p>\u3069\u3061\u3089\u3082\u6574\u6570\u3092\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306b\u5909\u63db\u3059\u308b\u547d\u4ee4\u3060\u304c\u3001uitofp\u306f\u5165\u529b\u3092\u7b26\u5408\u306a\u3057\u6574\u6570\u3068\u3057\u3066\u3001sitofp\u306f\u5165\u529b\u3092\u7b26\u53f7\u4ed8\u304d\u6574\u6570\u3068\u3057\u3066\u6271\u3046\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *CallExprAST::Codegen() {\r\n  \/\/ \u30b0\u30ed\u30fc\u30d0\u30eb\u30e2\u30b8\u30e5\u30fc\u30eb\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u540d\u524d\u3092\u63a2\u3059\u3002\r\n  Function *CalleeF = TheModule-&gt;getFunction(Callee);\r\n  if (CalleeF == 0)\r\n    return ErrorV(&quot;Unknown function referenced&quot;);\r\n\r\n  \/\/ \u5f15\u6570\u306e\u6570\u306e\u30c1\u30a7\u30c3\u30af\u3002\r\n  if (CalleeF-&gt;arg_size() != Args.size())\r\n    return ErrorV(&quot;Incorrect # arguments passed&quot;);\r\n\r\n  std::vector&lt;Value*&gt; ArgsV;\r\n  for (unsigned i = 0, e = Args.size(); i != e; ++i) {\r\n    ArgsV.push_back(Args&#x5B;i]-&gt;Codegen());\r\n    if (ArgsV.back() == 0) return 0;\r\n  }\r\n\r\n  return Builder.CreateCall(CalleeF, ArgsV, &quot;calltmp&quot;);\r\n}<\/pre>\n<p>\u95a2\u6570\u547c\u3073\u51fa\u3057\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u751f\u6210\u306f\u3001LLVM\u3092\u7528\u3044\u308b\u3068\u3068\u3066\u3082\u7d20\u76f4\u306b\u66f8\u3051\u308b\u3002<br \/>\n\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306f\u307e\u305a\u3001\u95a2\u6570\u540d\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306bLLVM\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u95a2\u6570\u540d\u3092\u63a2\u3059\u3002<br \/>\nLLVM\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u5168\u3066\u306e\u95a2\u6570\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u30b3\u30f3\u30c6\u30ca\u3067\u3042\u308b\u4e8b\u3092\u601d\u3044\u51fa\u305d\u3046\u3002<br \/>\n\u5404\u95a2\u6570\u306b\u3001\u30e6\u30fc\u30b6\u304c\u6307\u5b9a\u3059\u308b\u3082\u306e\u3068\u540c\u3058\u540d\u524d\u3092\u4e0e\u3048\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u95a2\u6570\u540d\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306bLLVM\u306e\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<\/p>\n<p>\u547c\u3073\u51fa\u3059\u95a2\u6570\u3092\u53d6\u5f97\u3067\u304d\u305f\u3089\u3001\u305d\u306e\u95a2\u6570\u306b\u6e21\u3055\u308c\u308b\u5404\u5f15\u6570\u306b\u5bfe\u3057\u3066\u518d\u5e30\u7684\u306b\u30b3\u30fc\u30c9\u751f\u6210\u3092\u884c\u3044\u3001LLVM\u306ecall\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\nLLVM\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u3001\u30cd\u30a4\u30c6\u30a3\u30d6\u306aC\u306e\u547c\u51fa\u898f\u7d04\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u306a\u306e\u3067call\u547d\u4ee4\u3092\u4f7f\u3063\u3066&#8221;sin\u201d\u3084&#8221;cos\u201d\u306e\u3088\u3046\u306a\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u95a2\u6570\u3092\u76f4\u63a5\u547c\u3073\u51fa\u305b\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: call\u547d\u4ee4\u306f\u4ee5\u4e0b\u306e\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = &#x5B;tail] call &#x5B;cconv] &#x5B;ret attrs] &lt;ty&gt; &#x5B;&lt;fnty&gt;*] &lt;fnptrval&gt;(&lt;function args&gt;) &#x5B;fn attrs]<\/pre>\n<p>ty\u304c\u623b\u308a\u5024\u306e\u578b\u3001function args\u304c\u5f15\u6570\u306e\u30ea\u30b9\u30c8\u306b\u306a\u308b\u307f\u305f\u3044\u3002<\/em><\/p>\n<p>\u3053\u308c\u3067\u3001\u4eca\u306e\u3068\u3053\u308d\u4e07\u83ef\u93e1\u304c\u6301\u30644\u7a2e\u985e\u306e\u57fa\u672c\u7684\u306a\u5f0f\u306b\u3064\u3044\u3066\u306e\u51e6\u7406\u306f\u5b8c\u4e86\u3057\u305f\u3002<br \/>\n\u3053\u308c\u306b\u3055\u3089\u306b\u4f55\u304b\u8ffd\u52a0\u3059\u308b\u306e\u3092\u5c3b\u8fbc\u307f\u3059\u308b\u5fc5\u8981\u306f\u306a\u3044\u3002<br \/>\n\u4f8b\u3048\u3070\u3001<a href=\"http:\/\/llvm.org\/docs\/LangRef.html\" target=\"_blank\">LLVM\u8a00\u8a9e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9<\/a>\u3092\u898b\u308b\u3068\u3001\u6211\u3005\u306e\u30b7\u30f3\u30d7\u30eb\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u3068\u3066\u3082\u7c21\u5358\u306b\u8ffd\u52a0\u51fa\u6765\u308b\u3088\u3046\u306a\u3001\u9762\u767d\u3044\u547d\u4ee4\u304c\u3044\u304f\u3064\u304b\u898b\u3064\u304b\u308b\u3060\u308d\u3046\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\u7b2c3\u7ae0 \u4e07\u83ef\u93e1: LLVM IR\u30b3\u30fc\u30c9\u306e\u751f\u6210<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/tutorial\/LangImpl03.html#expression-code-generation\" target=\"_blank\">\u7b2c3\u7bc0 \u5f0f\u306e\u30b3\u30fc\u30c9\u751f\u6210<\/a><\/p>\n<p>\u5f0f\u30ce\u30fc\u30c9\u306b\u306e\u305f\u3081\u306eLLVM\u30b3\u30fc\u30c9\u306e\u751f\u6210\u306f\u3001\u3068\u3066\u3082\u76f4\u63a5\u7684\u306b\u884c\u3048\u308b\u3002<br \/>\n\u6211\u3005\u306e4\u7a2e\u985e\u5168\u3066\u306e\u5f0f\u30ce\u30fc\u30c9\u306b\u3064\u3044\u3066\u306e\u30b3\u30fc\u30c9\u306f\u3001\u30b3\u30e1\u30f3\u30c8\u5165\u308a\u306745\u884c\u4ee5\u4e0b\u3067\u3042\u308b\u3002<br \/>\n\u307e\u305a\u6570\u5024\u30ea\u30c6\u30e9\u30eb\u304b\u3089\u59cb\u3081\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *NumberExprAST::Codegen() {\r\n  return ConstantFP::get(getGlobalContext(), APFloat(Val));\r\n}<\/pre>\n<p>LLVM IR\u3067\u306f\u3001\u6570\u5024\u5b9a\u6570\u306fConstantFP\u30af\u30e9\u30b9\u306b\u3088\u3063\u3066\u8868\u73fe\u3055\u308c\u308b\u3002<br \/>\nConstantFP\u30af\u30e9\u30b9\u306f\u3001\u305d\u306e\u5185\u90e8\u306bAPFloat\u3092\u6301\u3061\u3001\u305d\u306e\u4e2d\u3067\u6570\u5024\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\uff08APFloat\u306b\u306f\u4efb\u610f\u306e\u7cbe\u5ea6\u306e\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306e\u5b9a\u6570\u3092\u4fdd\u6301\u3059\u308b\u80fd\u529b\u304c\u3042\u308b\u3002\uff09<br \/>\n\u3053\u306e\u30b3\u30fc\u30c9\u306f\u57fa\u672c\u7684\u306b\u3001ConstantFP\u3092\u751f\u6210\u3057\u3001\u8fd4\u3057\u3066\u308b\u3060\u3051\u3067\u3042\u308b\u3002<br \/>\nLLVM IR\u3067\u306f\u3001\u5b9a\u6570\u306f\u3001\u3059\u3079\u3066\u304a\u4e92\u3044\u306b\u30e6\u30cb\u30fc\u30af\u3067\u3042\u308a\u5171\u6709\u3055\u308c\u308b\u70b9\u306b\u6ce8\u610f\u3002<br \/>\n\u306a\u306e\u3067\u3001&#8221;new foo(\u2026)&#8221;\u3068\u304b&#8221;foo::Create(\u2026)&#8221;\u3067\u306f\u306a\u304f\u3001&#8221;foo::get(\u2026)&#8221;\u3068\u3044\u3046\u8868\u73fe\u3092API\u306f\u7528\u3044\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *VariableExprAST::Codegen() {\r\n  \/\/ \u95a2\u6570\u306e\u4e2d\u304b\u3089\u3053\u306e\u5909\u6570\u3092\u63a2\u3059\r\n  Value *V = NamedValues&#x5B;Name];\r\n  return V ? V : ErrorV(&quot;Unknown variable name&quot;);\r\n}<\/pre>\n<p>\u5909\u6570\u3078\u306e\u53c2\u7167\u3082\u307e\u305f\u3001LLVM\u306b\u304a\u3044\u3066\u306f\u7c21\u5358\u3067\u3042\u308b\u3002<br \/>\n\u7c21\u6613\u7248\u306e\u4e07\u83ef\u93e1\u3067\u306f\u3001\u5909\u6570\u306f\u3059\u3067\u306b\u3069\u3053\u304b\u3067\u767a\u884c\u6e08\u307f\u3067\u3042\u308a\u3001\u305d\u306e\u5024\u304c\u4f7f\u7528\u3067\u304d\u308b\u72b6\u614b\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3057\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u6bb5\u968e\u3067\u306f\u3001NamedValues\u30de\u30c3\u30d7\u306e\u4e2d\u306b\u5b58\u5728\u3057\u3046\u308b\u5024\u306f\u3001\u95a2\u6570\u306e\u5f15\u6570\u3060\u3051\u3067\u3042\u308b\u3002<br \/>\n\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u4e0e\u3048\u3089\u308c\u305f\u540d\u524d\u304c\u30de\u30c3\u30d7\u306e\u306a\u304b\u306b\u5b58\u5728\u3059\u308b\u304b\u5358\u7d14\u306b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u3001\u305d\u306e\u5024\u3092\u8fd4\u3057\u3066\u3044\u308b\u3002<br \/>\n\uff08\u3082\u3057\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u3001\u4e0d\u660e\u306a\u5909\u6570\u304c\u53c2\u7167\u3055\u308c\u305f\u3053\u3068\u306b\u306a\u308b\u3002\uff09<br \/>\n\u5f8c\u306e\u7ae0\u3067\u3001\u30eb\u30fc\u30d7\u5e30\u7d0d\u5909\u6570\uff08loop induction variables\uff09\u3068\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u306e\u30b5\u30dd\u30fc\u30c8\u3092\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u306b\u8ffd\u52a0\u3059\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: \u30eb\u30fc\u30d7\u5e30\u7d0d\u5909\u6570\u306f\u30eb\u30fc\u30d7\u5909\u6570\u306e\u4e8b\u3067\u3042\u308b\u304c\u3001\u5f8c\u306e\u7ae0\u3067\u8ffd\u52a0\u3059\u308b\u6700\u9069\u5316\u30b5\u30dd\u30fc\u30c8\u7d61\u307f\uff08\u5e30\u7d0d\u5909\u6570\u89e3\u6790\u3068\u304b\uff09\u3067\u6562\u3048\u3066\u5e30\u7d0d\u3068\u3044\u3046\u8a00\u8449\u304c\u4f7f\u308f\u308c\u3066\u3044\u308b\u3093\u3060\u3068\u601d\u3046\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *BinaryExprAST::Codegen() {\r\n  Value *L = LHS-&gt;Codegen();\r\n  Value *R = RHS-&gt;Codegen();\r\n  if (L == 0 || R == 0) return 0;\r\n\r\n  switch (Op) {\r\n  case '+': return Builder.CreateFAdd(L, R, &quot;addtmp&quot;);\r\n  case '-': return Builder.CreateFSub(L, R, &quot;subtmp&quot;);\r\n  case '*': return Builder.CreateFMul(L, R, &quot;multmp&quot;);\r\n  case '&lt;':\r\n    L = Builder.CreateFCmpULT(L, R, &quot;cmptmp&quot;);\r\n    \/\/ bool\u306e0\/1\u3092double\u306e0.0\/1.0\u306b\u5909\u63db\u3059\u308b\u3002\r\n    return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),\r\n                                &quot;booltmp&quot;);\r\n  default: return ErrorV(&quot;invalid binary operator&quot;);\r\n  }\r\n}<\/pre>\n<p>\u4e8c\u9805\u6f14\u7b97\u5b50\u306b\u3064\u3044\u3066\u306f\u3088\u308a\u8208\u5473\u6df1\u3044\u3002<br \/>\n\u57fa\u672c\u7684\u306b\u306f\u3001\u5f0f\u306e\u5de6\u624b\u5074\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u518d\u5e30\u7684\u306b\u751f\u6210\u3057\u3001\u305d\u308c\u304b\u3089\u53f3\u624b\u5074\u3092\u51e6\u7406\u3057\u3001\u305d\u3057\u3066\u4e8c\u9805\u6f14\u7b97\u5f0f\uff08binary expression\uff09\u306e\u7d50\u679c\u3092\u8a08\u7b97\u3059\u308b\u3001\u3068\u3044\u3046\u30a2\u30a4\u30c7\u30a2\u306b\u57fa\u3065\u3044\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u6b63\u3057\u3044LLVM\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306b\u3001\u6f14\u7b97\u5b50\u3092\u898b\u3066switch\u6587\u3067\u5358\u7d14\u306b\u51e6\u7406\u3092\u5207\u308a\u66ff\u3048\u3066\u3044\u308b\u3002<\/p>\n<p>\u4e0a\u306e\u4f8b\u3067\u306f\u3001LLVM Builder\u30af\u30e9\u30b9\u304c\u3001\u305d\u306e\u5024\u3092\u898b\u306f\u3058\u3081\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3042\u308b\u3002\uff08the LLVM builder class is starting to show its value.\uff09<br \/>\nIRBuilder\u306f\u3001\u76f4\u8fd1\u3067\u751f\u6210\u3057\u305f\u547d\u4ee4\u3092\u3069\u3053\u306b\u633f\u5165\u3057\u305f\u304b\u3092\u77e5\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u3042\u306a\u305f\u304c\u3059\u3079\u304d\u3053\u3068\u306f\u3069\u306e\u547d\u4ee4\u3092\uff08\u4f8b\u3048\u3070CreateFAdd\uff09\u751f\u6210\u3059\u308b\u306e\u304b\u3001\u3069\u306e\u30aa\u30da\u30e9\u30f3\u30c9\uff08\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3067\u306fL\u3084R\u3068\u306a\u3063\u3066\u308b\u90e8\u5206\uff09\u3092\u4f7f\u7528\u3059\u308b\u306e\u304b\u3001\u305d\u3057\u3066\u5fc5\u9808\u3067\u306f\u306a\u3044\u304c\u305d\u306e\u540d\u524d\u3092\u6307\u5b9a\u3059\u308b\u4e8b\u3060\u3051\u3067\u3042\u308b\u3002<\/p>\n<p>LLVM\u306e\u7d20\u6674\u3089\u3057\u3044\u70b9\u306f\u3001\u3042\u304f\u307e\u3067\u3082\u305d\u306e\u540d\u524d\u306f\u30d2\u30f3\u30c8\u306b\u904e\u304e\u306a\u3044\u3068\u3044\u3046\u4e8b\u3067\u3042\u308b\u3002<br \/>\n\u4e8b\u5b9f\u3001\u4e0a\u306e\u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u8907\u6570\u306e&#8221;addtmp&#8221;\u5909\u6570\u304c\u751f\u6210\u3055\u308c\u305f\u5834\u5408\u3001LLVM\u306f\u81ea\u52d5\u7684\u306b\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3055\u308c\u305f\u30e6\u30cb\u30fc\u30af\u306a\u901a\u3057\u756a\u53f7\u3092\u305d\u306e\u63a5\u5c3e\u8f9e\u3068\u3057\u3066\u8ffd\u52a0\u3059\u308b\u3002<br \/>\n\u547d\u4ee4\u306b\u30ed\u30fc\u30ab\u30eb\u5909\u6570\u540d\u3092\u4ed8\u3051\u308b\u304b\u3069\u3046\u304b\u306f\u5168\u304f\u81ea\u7531\u3067\u3042\u308b\u304c\u3001IR\u306e\u30c0\u30f3\u30d7\u3092\u8aad\u3080\u3068\u304d\u306b\u697d\u306b\u306a\u308b\u3060\u308d\u3046\u3002<\/p>\n<p>LLVM\u306e\u547d\u4ee4\u306b\u306f\u3001\u53b3\u683c\u306a\u30eb\u30fc\u30eb\u304c\u5f37\u5236\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u52a0\u7b97\u547d\u4ee4\u306e\u5de6\u5074\u3068\u53f3\u5074\u30aa\u30da\u30e9\u30f3\u30c9\u306f\u3001\u540c\u3058\u578b\uff08type\uff09\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u305d\u3057\u3066\u3001\u623b\u308a\u5024\u306e\u578b\u306f\u3001\u30aa\u30da\u30e9\u30f3\u30c9\u306e\u578b\u3068\u5408\u81f4\u3057\u3066\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br \/>\n\u4e07\u83ef\u93e1\u3067\u306f\u5168\u3066\u306e\u5024\u304cdouble\u306a\u306e\u3067\u3001\u52a0\u7b97\u3084\u6e1b\u7b97\u3001\u4e57\u7b97\u306e\u30b3\u30fc\u30c9\u306f\u3068\u3066\u3082\u30b7\u30f3\u30d7\u30eb\u306b\u306a\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: LLVM IR\u306b\u304a\u3051\u308b\u52a0\u7b97\u547d\u4ee4\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = add &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt;<\/pre>\n<p>op1, op2\u304c\u5de6\u5074\u3001\u53f3\u5074\u30aa\u30da\u30e9\u30f3\u30c9\u3067\u3001ty\u304c\u578b\u3001result\u304c\u305d\u306e\u7d50\u679c\u3067\u3042\u308b\u3002<br \/>\n\u539f\u6587\u306f\u3001op1\u3068op2\u306e\u578b\u304c\u540c\u3058\u3067\u3001\u3055\u3089\u306b2\u3064\u306e\u30aa\u30da\u30e9\u30f3\u30c9\u3068result\u304c\u540c\u3058\u578b\u3067\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u4e8b\u3092\u8a00\u3063\u3066\u308b\u3093\u3060\u3068\u601d\u3046\u3002<br \/>\nLLVM\u306e\u547d\u4ee4\u306e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306f\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u306b\u3042\u308b\u3002<br \/>\n<a href=\"http:\/\/llvm.org\/docs\/LangRef.html\" target=\"_blank\">http:\/\/llvm.org\/docs\/LangRef.html<\/a><\/em><\/p>\n<p>\u4e00\u65b9\u3001LLVM\u3067\u306f\u3001fcmp\u547d\u4ee4\u306f\u5e38\u306b&#8217;i1&#8217;\u578b\uff081bit\u6574\u6570\uff09\u306e\u5024\u3092\u8fd4\u3059\u3068\u5b9a\u3081\u3089\u308c\u3066\u3044\u308b\u3002<br \/>\n\u4e07\u83ef\u93e1\u3067\u306f\u3001\u3053\u308c\u30920.0\u30681.0\u3068\u3044\u3046\u5024\u3067\u6271\u3044\u305f\u3044\u3002<br \/>\n\u3053\u306e\u3088\u3046\u306a\u52d5\u4f5c\u3092\u3055\u305b\u308b\u305f\u3081\u306b\u3001fcmp\u547d\u4ee4\u3092uitofp\u547d\u4ee4\u3068\u4e00\u7dd2\u306b\u4f7f\u3063\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u547d\u4ee4\u306f\u3001\u5165\u529b\u3055\u308c\u305f\u6574\u6570\u3092\u7b26\u5408\u306a\u3057\u6574\u6570\u3068\u3057\u3066\u6271\u3044\u3001\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u3078\u5909\u63db\u3059\u308b\u3002<br \/>\n\u3082\u3057\u3001uitofp\u3067\u306f\u7121\u304fsitofp\u547d\u4ee4\u3092\u4f7f\u3063\u305f\u3068\u3057\u305f\u3089\u3001\u4e07\u83ef\u93e1\u306e&#8221;<\"\u6f14\u7b97\u5b50\u306f\u3001\u5165\u529b\u5024\u306b\u5fdc\u3058\u30660.0\u304b-1.0\u306e\u5024\u3092\u8fd4\u3057\u3066\u3057\u307e\u3046\u3060\u308d\u3046\u3002\n<em>\u8a33\u6ce8: fcmp\u547d\u4ee4\u306f\u4ee5\u4e0b\u306e\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = fcmp &lt;cond&gt; &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt;<\/pre>\n<p>cond\u306b\u306f\u3001\u6f14\u7b97\u5b50\u3092\u8868\u3059\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u6307\u5b9a\u3059\u308b\u3002<br \/>\n\u4f8b\u3048\u3070&#8221;\u5c0f\u306a\u308a\u30a4\u30b3\u30fc\u30eb&#8221;\u306a\u3089\u3001&#8221;ole&#8221;\u3092\u6307\u5b9a\u3059\u308b\u3001\u3068\u3044\u3046\u98a8\u306b\u30ad\u30fc\u30ef\u30fc\u30c9\u304c\u6c7a\u307e\u3063\u3066\u3044\u308b\u3002<br \/>\n\u3042\u3068\u306f\u3001add\u3068\u540c\u3058\u3060\u304c\u3001\u7d50\u679c\u304c1bit\u6574\u6570\u3067\u8fd4\u3063\u3066\u304f\u308b\u70b9\u306b\u6ce8\u610f\u3002<br \/>\nuitofp\u3068sitofp\u306f\u4ee5\u4e0b\u306e\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = uitofp &lt;ty&gt; &lt;value&gt; to &lt;ty2&gt;\r\n&lt;result&gt; = sitofp &lt;ty&gt; &lt;value&gt; to &lt;ty2&gt;<\/pre>\n<p>\u3069\u3061\u3089\u3082\u6574\u6570\u3092\u6d6e\u52d5\u5c0f\u6570\u70b9\u6570\u306b\u5909\u63db\u3059\u308b\u547d\u4ee4\u3060\u304c\u3001uitofp\u306f\u5165\u529b\u3092\u7b26\u5408\u306a\u3057\u6574\u6570\u3068\u3057\u3066\u3001sitofp\u306f\u5165\u529b\u3092\u7b26\u53f7\u4ed8\u304d\u6574\u6570\u3068\u3057\u3066\u6271\u3046\u3002<\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">Value *CallExprAST::Codegen() {\r\n  \/\/ \u30b0\u30ed\u30fc\u30d0\u30eb\u30e2\u30b8\u30e5\u30fc\u30eb\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u540d\u524d\u3092\u63a2\u3059\u3002\r\n  Function *CalleeF = TheModule-&gt;getFunction(Callee);\r\n  if (CalleeF == 0)\r\n    return ErrorV(&quot;Unknown function referenced&quot;);\r\n\r\n  \/\/ \u5f15\u6570\u306e\u6570\u306e\u30c1\u30a7\u30c3\u30af\u3002\r\n  if (CalleeF-&gt;arg_size() != Args.size())\r\n    return ErrorV(&quot;Incorrect # arguments passed&quot;);\r\n\r\n  std::vector&lt;Value*&gt; ArgsV;\r\n  for (unsigned i = 0, e = Args.size(); i != e; ++i) {\r\n    ArgsV.push_back(Args&#x5B;i]-&gt;Codegen());\r\n    if (ArgsV.back() == 0) return 0;\r\n  }\r\n\r\n  return Builder.CreateCall(CalleeF, ArgsV, &quot;calltmp&quot;);\r\n}<\/pre>\n<p>\u95a2\u6570\u547c\u3073\u51fa\u3057\u306e\u305f\u3081\u306e\u30b3\u30fc\u30c9\u751f\u6210\u306f\u3001LLVM\u3092\u7528\u3044\u308b\u3068\u3068\u3066\u3082\u7d20\u76f4\u306b\u66f8\u3051\u308b\u3002<br \/>\n\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306f\u307e\u305a\u3001\u95a2\u6570\u540d\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306bLLVM\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u95a2\u6570\u540d\u3092\u63a2\u3059\u3002<br \/>\nLLVM\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u3001JIT\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\u5168\u3066\u306e\u95a2\u6570\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u30b3\u30f3\u30c6\u30ca\u3067\u3042\u308b\u4e8b\u3092\u601d\u3044\u51fa\u305d\u3046\u3002<br \/>\n\u5404\u95a2\u6570\u306b\u3001\u30e6\u30fc\u30b6\u304c\u6307\u5b9a\u3059\u308b\u3082\u306e\u3068\u540c\u3058\u540d\u524d\u3092\u4e0e\u3048\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u95a2\u6570\u540d\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306bLLVM\u306e\u30b7\u30f3\u30dc\u30eb\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<\/p>\n<p>\u547c\u3073\u51fa\u3059\u95a2\u6570\u3092\u53d6\u5f97\u3067\u304d\u305f\u3089\u3001\u305d\u306e\u95a2\u6570\u306b\u6e21\u3055\u308c\u308b\u5404\u5f15\u6570\u306b\u5bfe\u3057\u3066\u518d\u5e30\u7684\u306b\u30b3\u30fc\u30c9\u751f\u6210\u3092\u884c\u3044\u3001LLVM\u306ecall\u547d\u4ee4\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\nLLVM\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u3001\u30cd\u30a4\u30c6\u30a3\u30d6\u306aC\u306e\u547c\u51fa\u898f\u7d04\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002<br \/>\n\u306a\u306e\u3067call\u547d\u4ee4\u3092\u4f7f\u3063\u3066&#8221;sin\u201d\u3084&#8221;cos\u201d\u306e\u3088\u3046\u306a\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u95a2\u6570\u3092\u76f4\u63a5\u547c\u3073\u51fa\u305b\u308b\u3002<br \/>\n<em>\u8a33\u6ce8: call\u547d\u4ee4\u306f\u4ee5\u4e0b\u306e\u5f62\u3092\u3068\u308b\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;result&gt; = &#x5B;tail] call &#x5B;cconv] &#x5B;ret attrs] &lt;ty&gt; &#x5B;&lt;fnty&gt;*] &lt;fnptrval&gt;(&lt;function args&gt;) &#x5B;fn attrs]<\/pre>\n<p>ty\u304c\u623b\u308a\u5024\u306e\u578b\u3001function args\u304c\u5f15\u6570\u306e\u30ea\u30b9\u30c8\u306b\u306a\u308b\u307f\u305f\u3044\u3002<\/em><\/p>\n<p>\u3053\u308c\u3067\u3001\u4eca\u306e\u3068\u3053\u308d\u4e07\u83ef\u93e1\u304c\u6301\u30644\u7a2e\u985e\u306e\u57fa\u672c\u7684\u306a\u5f0f\u306b\u3064\u3044\u3066\u306e\u51e6\u7406\u306f\u5b8c\u4e86\u3057\u305f\u3002<br \/>\n\u3053\u308c\u306b\u3055\u3089\u306b\u4f55\u304b\u8ffd\u52a0\u3059\u308b\u306e\u3092\u5c3b\u8fbc\u307f\u3059\u308b\u5fc5\u8981\u306f\u306a\u3044\u3002<br \/>\n\u4f8b\u3048\u3070\u3001<a href=\"http:\/\/llvm.org\/docs\/LangRef.html\" target=\"_blank\">LLVM\u8a00\u8a9e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9<\/a>\u3092\u898b\u308b\u3068\u3001\u6211\u3005\u306e\u30b7\u30f3\u30d7\u30eb\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u3068\u3066\u3082\u7c21\u5358\u306b\u8ffd\u52a0\u51fa\u6765\u308b\u3088\u3046\u306a\u3001\u9762\u767d\u3044\u547d\u4ee4\u304c\u3044\u304f\u3064\u304b\u898b\u3064\u304b\u308b\u3060\u308d\u3046\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-2932","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\/2932","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=2932"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2932\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=2932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=2932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=2932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}