【LLVM】《Getting Started with LLVM Core Libararies》读书笔记——写一个PASS

来源:互联网 发布:山东省农产品出口数据 编辑:程序博客网 时间:2024/06/05 20:20

【Building and running your new pass with the LLVM build system】

1.“We name our pass FnArgCnt and place it under the LLVM source code tree:”

     给PASS命名为FnArgCnt,并用以下命令将PASS文件放到LLVM_SRC_ROOT/lib/Transforms/FnArgCnt目录下:

     $ cd <llvm_source_tree>
     $ mkdir lib/Transforms/FnArgCnt
     $ cd lib/Transforms/FnArgCnt

2.写cpp文件,其中需要注意的部分:

(1)“The ID is determined internally by LLVM to identify a pass, and it can be declared with any value: char FnArgCnt::ID = 0;”

        ID用于定义PASS,可以用任意值声明。(如果用同一个值声明两个不同的PASS会如何?)

 (2)“static RegisterPass<FnArgCnt> X("fnargcnt", "Function Argument Count Pass", false, false);” 

          “The first argument, fnargcnt , is the name used by the opt tool to identify the pass, whereas the second argument contains its extended name. The third argument tells        us whether the pass changes the current CFG, and the last returns true only if it implements an analysis pass.”

         第一个参数:被opt tool使用的定义PASS的名字

         第二个参数:扩展名

         第三个参数:是否会改变现有的CFG

         第四个参数:是否实现的是一个分析PASS

3.“To compile and install the pass, we need a Makefile within the same directory of the source code. Different from our previous projects, we are not building a standalone tool anymore, and this Makefile is integrated in the LLVM build system. Since it relies on the LLVM main Makefile, which implements a great deal of rules, its contents are considerably simpler than a standalone Makefile. Refer to the following code:”

要编译和install这个PASS,需要使Makefile和源代码在同一个文件目录下。这个Makefile集成在LLVM系统中,所以我们不再建立一个独立的工具。因为它依赖于LLVM的main Makefile。Makefile内容如下:

      # Makefile for FnArgCnt pass
      # Path to top level of LLVM hierarchy

      LEVEL = ../../..
      # Name of the library to build
      LIBRARYNAME = LLVMFnArgCnt
      # Make the shared library become a loadable module so the tools can
      # dlopen/dlsym on the resulting library.
      LOADABLE_MODULE = 1
      # Include the makefile implementation stuff
      include $(LEVEL)/Makefile.common



  

原创粉丝点击