{"id":73,"date":"2020-09-03T20:25:05","date_gmt":"2020-09-03T12:25:05","guid":{"rendered":"http:\/\/47.108.144.50\/?p=73"},"modified":"2020-09-04T11:28:10","modified_gmt":"2020-09-04T03:28:10","slug":"llvm-%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"http:\/\/pareto.fun\/?p=73","title":{"rendered":"LLVM \u5165\u95e8"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"782\" height=\"331\" src=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image.png\" alt=\"\" class=\"wp-image-81\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image.png 782w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image-300x127.png 300w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image-768x325.png 768w\" sizes=\"(max-width: 782px) 100vw, 782px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">llvm \u662f\u4ec0\u4e48<\/h1>\n\n\n\n<p>low level virtual machine \uff0c \u662f\u4e2a\u7f16\u8bd1\u5668\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">llvm\u7684\u8bbe\u8ba1<\/h2>\n\n\n\n<p>\u8ba1\u7b97\u673a\u5e38\u7528\u89e3\u51b3\u65b9\u6cd5\u3002\u767e\u5206\u4e4b80\u7684\u6027\u80fd\u548c\u529f\u80fd\u95ee\u9898\u90fd\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u7f13\u51b2\u533a\u6765\u5b8c\u6210\uff0cllvm\u4e5f\u662f\u4f7f\u7528\u4e86\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6dfb\u52a0\u4e86IR\u4e2d\u95f4\u8bed\u8a00\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"514\" height=\"90\" src=\"http:\/\/47.108.144.50\/wp-content\/uploads\/2020\/09\/image-20200903112945927.png\" alt=\"image-20200903112945927\" class=\"wp-image-76\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image-20200903112945927.png 514w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image-20200903112945927-300x53.png 300w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/figure>\n\n\n\n<p>source code -&gt; IR -&gt; IR -&gt; target-indepent code \uff08\u4ee3\u7801\u5c42\u9762\uff09\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5b66\u4e60\u7684\u76ee\u7684<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728\u51fd\u6570\u4e2d\u63d2\u5165\u6307\u4ee4<\/h2>\n\n\n\n<p>\u8be5\u6307\u4ee4\u4e3a\u6846\u67b6\u76f8\u5173\u7684\u6307\u4ee4\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u9700\u8981\u7684\u77e5\u8bc6\u70b9<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">IR \u8bed\u6cd5\u548c\u7ed3\u6784<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bed\u6cd5<\/h3>\n\n\n\n<p>\u4ee5helloworld\u4e3a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;stdio.h&gt;<br>int main(void){<br> &nbsp; &nbsp; &nbsp;  printf(\"Hello World\\n\");<br> &nbsp; &nbsp; &nbsp;  return 0;<br>} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/pre>\n\n\n\n<p>\u7f16\u8bd1\u4e3aIR\u4e2d\u95f4\u8bed\u8a00<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"780\" src=\"http:\/\/47.108.144.50\/wp-content\/uploads\/2020\/09\/image-20200903144821210.png\" alt=\"image-20200903144821210\" class=\"wp-image-77\" srcset=\"http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image-20200903144821210.png 600w, http:\/\/pareto.fun\/wp-content\/uploads\/2020\/09\/image-20200903144821210-231x300.png 231w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u6784<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u6a21\u5757module<\/h4>\n\n\n\n<p>\u4e2a\u4eba\u7406\u89e3 \u4e00\u4e2a\u6587\u4ef6\u5c31\u53ef\u4ee5\u5f53\u4f5c\u4e00\u4e2a\u6a21\u5757<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u51fd\u6570<\/h4>\n\n\n\n<p>\u51fd\u6570\u5c31\u662f\u51fd\u6570<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5757<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u51fd\u6570\u7531\u4ee3\u7801\u5757\u7ec4\u6210<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6307\u4ee4<\/h4>\n\n\n\n<p>\u5757\u7531\u6307\u4ee4\u7ec4\u6210<\/p>\n\n\n\n<p>\u904d\u5386HelloWorld.ll \u7684\u6a21\u5757\u3001\u51fd\u6570\u3001\u5757\u3001\u6307\u4ee4\uff0c\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Module Name:    HelloWorld.ll<br>Function Name:  main<br>BasicBlock Name:    <br>OpccodeName:    alloca<br>OpccodeName:    store<br>OpccodeName:    call<br>OpccodeName:    ret<br>Function Name:  printf<\/pre>\n\n\n\n<p>\u904d\u5386\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">errs() &lt;&lt; \"Module Name:\\t\" &lt;&lt; M.getName() &lt;&lt; \"\\n\";<br>  for(auto &amp;F : M){<br> &nbsp;  errs() &lt;&lt; \"Function Name:\\t\" &lt;&lt;F.getName() &lt;&lt; \"\\n\";<br> &nbsp;  for(auto &amp;B : F){<br> &nbsp; &nbsp;  errs() &lt;&lt; \"BasicBlock Name:\\t\" &lt;&lt;B.getName() &lt;&lt; \"\\n\";<br> &nbsp; &nbsp;  B.setName(\"mc\");<br> &nbsp; &nbsp;  for (Instruction  &amp;I : B)<br> &nbsp; &nbsp; &nbsp;  errs() &lt;&lt; \"OpccodeName:\\t\" &lt;&lt;I.getOpcodeName() &lt;&lt; \"\\n\";<br> &nbsp;  }<br>  }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PASS<\/h2>\n\n\n\n<p>IR\u5230IR\u7684\u5904\u7406,\u770b\u4e2a\u7b80\u5355\u7684PASS<\/p>\n\n\n\n<p>\u7528InjectFuncCall\u5b9e\u4f8b\u6765\u5206\u6790pass,\u6e90\u4ee3\u7801\u5728<a href=\"https:\/\/github.com\/banach-space\/llvm-tutor\/blob\/master\/lib\/InjectFuncCall.cpp\">https:\/\/github.com\/banach-space\/llvm-tutor\/blob\/master\/lib\/InjectFuncCall.cpp<\/a>  \uff0c InjectFuncCall\u7684\u529f\u80fd\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>(llvm-tutor) Hello from: main<br>(llvm-tutor) number of arguments: 0<br>Hello World<\/p>\n\n\n\n<p>\u4eceC\u4ee3\u7801\u6765\u770b\u5c31\u662f\u63d2\u5165\u4e86\u7c7b\u4f3c\u8fd9\u6837\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>printf(&#8220;(llvm-tutor) Hello from: %s\\n(llvm-tutor) number of arguments: %s&#8221;,funcname ,arg_num);<\/p>\n\n\n\n<p>\u518d\u770b\u6e90\u7801\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u5206\u6790\u4e0b\u5728\u63d2\u5165\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u9700\u8981\u4ec0\u4e48\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol><li>\u8c03\u7528\u6307\u4ee4 <\/li><li>\u51fd\u6570\u8fd4\u56de\u503c <\/li><li>\u51fd\u6570\u540d\u79f0<\/li><li>\u53c2\u6570<ol><li>\u53c2\u6570\u7c7b\u578b<\/li><li>\u53c2\u6570\u540d\u79f0<\/li><li>\u53c2\u6570\u4e2a\u6570<\/li><\/ol><\/li><\/ol>\n\n\n\n<p>\u7136\u540e\u518d\u6765\u770bPASS \u64cd\u4f5cIR \u6dfb\u52a0\u51fd\u6570\u8c03\u7528\u5c31\u5f88\u7b80\u5355\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bool InjectFuncCall::runOnModule(Module &amp;M) {\n  bool InsertedAtLeastOnePrintf = false;\n  \/\/\u83b7\u53d6llvmContext\n  auto &amp;CTX = M.getContext();\n\/\/\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u53c2\u6570\u7c7b\u578b\n  PointerType *PrintfArgTy = PointerType::getUnqual(Type::getInt8Ty(CTX));\n  FunctionType *PrintfTy = FunctionType::get(\n \u00a0 \u00a0  IntegerType::getInt32Ty(CTX),\n \u00a0 \u00a0  PrintfArgTy,\n \u00a0 \u00a0  \/*IsVarArgs=*\/true);\n\n  FunctionCallee Printf = M.getOrInsertFunction(\"printf\", PrintfTy);\n \u00a0\/\/\u83b7\u53d6\u8c03\u7528printf\u7684\u51fd\u6570\n  Function *PrintfF = dyn_cast&lt;Function>(Printf.getCallee());\n  errs()&lt;&lt;PrintfF.getCallee()&lt;&lt;\"\\n\";\n\n\u200b\/\/ \u63d2\u5165\u540d\u79f0\u4e3aPrintfFormatStr \u7684\u5168\u5c40\u53d8\u91cf\n  llvm::Constant *PrintfFormatStr = llvm::ConstantDataArray::getString(\n \u00a0 \u00a0  CTX, \"(llvm-tutor) Hello from11: %s\\n(llvm-tutor) \u00a0 number of arguments: %d\\n\");\n  Constant *PrintfFormatStrVar =\n \u00a0 \u00a0  M.getOrInsertGlobal(\"PrintfFormatStr\", PrintfFormatStr->getType());\n  dyn_cast&lt;GlobalVariable>(PrintfFormatStrVar)->setInitializer(PrintfFormatStr);\n\u200b\n  for (auto &amp;F : M) {\n \u00a0  \/\/\u7528\u6765\u533a\u5206\u662f\u5426\u4e3a\u5f15\u7528\u51fd\u6570\u3002\n \u00a0  if (F.isDeclaration())\n \u00a0 \u00a0  continue;\n\u200b\n \u00a0  \/\/ Get an IR builder. Sets the insertion point to the top of the function\n \u00a0  IRBuilder&lt;> Builder(&amp;*F.getEntryBlock().getFirstInsertionPt());\n\u200b\n \u00a0  \/\/ Inject a global variable that contains the function name\n     \/\/\u521b\u5efa\u5168\u5c40\u53d8\u91cf\u5e76\u83b7\u53d6\u4ed6\u7684\u6307\u9488\u3002\n \u00a0  auto FuncName = Builder.CreateGlobalStringPtr(F.getName());\n\u200b\n \u00a0  \/\/ Printf requires i8*, but PrintfFormatStrVar is an array: [n x i8]. Add\n \u00a0  \/\/ \u521b\u5efa\u6307\u5411PrintfFormatStrVar \u53c2\u6570\u503c PrintfArgTy \u53c2\u6570\u7c7b\u578b\u7684\u6307\u9488\u3002 \u7b2c\u4e09\u4e2a\u53c2\u6570\"formatStr\"\u4f3c\u4e4e\u662f\u53ef\u9009\u53c2\u6570\uff0c\u5220\u9664\u540e\u4e0d\u5f71\u54cd\u64cd\u4f5c\u3002\n \u00a0  llvm::Value *FormatStrPtr =\n \u00a0 \u00a0 \u00a0  Builder.CreatePointerCast(PrintfFormatStrVar, PrintfArgTy, \"formatStr\");\n\u200b\n \u00a0  \/\/ The following is visible only if you pass -debug on the command line\n \u00a0  \/\/ *and* you have an assert build.\n \u00a0  LLVM_DEBUG(dbgs() &lt;&lt; \" Injecting call to printf inside \" &lt;&lt; F.getName()\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  &lt;&lt; \"\\n\");\n\u200b\n \u00a0  \/\/ \u8fd9\u6bb5\u4ee3\u7801\u6bd4\u8f83\u660e\u663e\uff0c\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\uff0c\u7ed3\u5408\u53c2\u6570\u4e86\u89e3\u5230\u521b\u5efa\u51fd\u6570\u8c03\u7528\u6211\u4eec\u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\u548c\u51fd\u6570\u53c2\u6570\u3002\n \u00a0  Builder.CreateCall(\n \u00a0 \u00a0 \u00a0  Printf, {FormatStrPtr, FuncName, Builder.getInt32(F.arg_size())});\n\u200b\n \u00a0  InsertedAtLeastOnePrintf = true;\n  }\n\u200b\n  return InsertedAtLeastOnePrintf;\n}\n\u200b<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>llvm \u662f\u4ec0\u4e48 low level virtual machine \uff0c \u662f\u4e2a\u7f16\u8bd1\u5668\u3002 llvm\u7684\u8bbe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/73"}],"collection":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":16,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":97,"href":"http:\/\/pareto.fun\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions\/97"}],"wp:attachment":[{"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pareto.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}