Swift开源:LLVMSwift - LLVM C API 的 Swift 封装库

wanenjie 7年前
   <h2>LLVMSwift</h2>    <p>LLVMSwift is a set of Swifty API wrappers for the LLVM C API. It makes compiler development feel great from Swift!</p>    <h2>Usage</h2>    <p>To start emitting IR, you'll want to create a Module object, with an optional Context parameter, and an IRBuilder that will build instructions for that module.</p>    <pre>  <code class="language-swift">let module = Module(name: "main")  let builder = IRBuilder(module: module)</code></pre>    <p>Once you do that, you can start adding functions, global variables, and generating instructions!</p>    <pre>  <code class="language-swift">let main = builder.addFunction(name: "main",                                  type: FunctionType(argTypes: [],                                                     returnType: VoidType())  let entry = builder.appendBasicBlock(named: "entry")  builder.positionAtEnd(of: entry)    builder.buildRetVoid()    module.dump()</code></pre>    <p>The IRBuilder class has methods for almost all functions from the LLVM C API, like:</p>    <ul>     <li>builder.buildAdd</li>     <li>builder.buildSub</li>     <li>builder.buildMul</li>     <li>builder.buildCondBr</li>     <li>builder.addSwitch</li>    </ul>    <p>and so many more.</p>    <p>Plus, it provides common wrappers around oft-used types like Function , Global , Switch , and PhiNode .</p>    <h2>Installation</h2>    <p>There are a couple, annoying steps you need to get it working before it'll build. Number one, you'll need a custom cllvm pkg-config file, which is included in the repo. Drop that in /usr/local/lib/pkgconfig and make sure you have LLVM installed through homebrew :</p>    <pre>  <code class="language-swift">brew install llvm</code></pre>    <p>Once you do that, you can add LLVMSwift as a dependency for your own Swift compiler projects!</p>    <p>This project is used byTrill for all its code generation.</p>    <h2>Authors</h2>    <ul>     <li>Harlan Haskins (@harlanhaskins)</li>     <li>Robert Widmann (@CodaFi)</li>    </ul>    <h2>License</h2>    <p>This project is released under the MIT license, a copy of which is available in this repo.</p>    <p> </p>    <p> </p>    <p> </p>