Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LLVM and Scala

LLVM and Scala

July 8 talk at ScalaSyd

Tony Sloane

July 08, 2015
Tweet

More Decks by Tony Sloane

Other Decks in Programming

Transcript

  1. LLVM and Scala Anthony M. Sloane Programming Languages and Verification

    Research Group Department of Computing Macquarie University @plvmq [email protected] @inkytonik
  2. LLVM Low-Level Virtual Machine Originally a project of the University

    of Illinois with the goal of providing a modern, SSA-based compilation strategy capable of supporting both static and dynamic compilation of arbitrary programming languages [llvm.org] The main project components are: A common intermediate representation (IR) roughly equivalent to a generic assembly language. Tools and libraries to create, transform and consume LLVM IR. Now strongly supported by Apple Inc. Lead developers are Apple employees. Apple ecosystem uses LLVM-based compilers, most notably for Objective-C and Swift. 2 / 15
  3. LLVM IR (assembly syntax) define i32 @test(i32 %i) { .entry:

    %i.addr = alloca i32 store i32 %i, i32* %i.addr %r.addr = alloca i32 br label %.block.6.10.pred .block.6.18.body: %0 = load i32, i32* %i.addr %1 = add nsw i32 %0, 1 store i32 %1, i32* %i.addr br label %.block.6.10.pred ... } 5 / 15
  4. LLVM at the Command Line int main (int argc, char

    *argv[]) { int i = 0; while (i < 10) { i = i + 1; } printf ("%d ", i); } ; clang -S -emit-llvm -c while.c ; opt -S -dce -reg2mem -o while-opt.ll while.ll ; lli while-opt.ll 10 ; llvm-as -o while-opt.bc while-opt.ll ; lli while-opt.bc 10 ; llc -o while-opt.s while-opt.ll ; clang -o while-opt while-opt.s ; ./while-opt 10 6 / 15
  5. LLVM and Kiama Kiama High-level declarative language processing using Scala-based

    DSLs. Features tree attribution, term rewriting and pretty-printing. 8 / 15
  6. Function Definition Syntax define i32 @test(i32 %i) { ... }

    ----- FunctionDefinition : Item = "define" ... Type sp Global ’(’ Arguments ")" ... FunctionBody \n. FunctionBody = "{" nestnl (Block+) \n "}". 10 / 15
  7. Block Syntax .entry: %i.addr = alloca i32, align 4 store

    i32 %i, i32* %i.addr, align 4 %r.addr = alloca i32, align 4 br label %.block.6.10.pred ----- Block = OptBlockLabel ... nest (Instruction*) nest (TerminatorInstruction) \n. 11 / 15
  8. LLVM Instruction Syntax %1 = add i32 %0, 1 -----

    Instruction = BinaryInstruction | ... BinaryInstruction : Instruction = Name sp "=" BinOp Type sp Value "," Value {Binary}. BinOp = "add" Wrap* {Add} | "and" {And} | ... 12 / 15
  9. Translating to LLVM IR (1) Assignment statement i = i

    + 1; Assign (Var ("i"), Add (Load (Var ("i")), IntLit (1))) Load i’s value into local zero Load (Binding (Local ("0")), NotVolatile (), IntT (32), PointerT (IntT (32), DefaultAddrSpace ()), Named (Local ("i.addr")), DefaultAlign ()) %0 = load i32, i32* %i.addr 13 / 15
  10. Translating to LLVM IR (2) Add one to local zero,

    result in local one Binary (Binding (Local ("1")), Add (List (NoSignedWrap ())), IntT (32), Named (Local ("0")), Const (IntC (1))) %1 = add nsw i32 %0, 1 Store local one to i’s address Store (NotVolatile (), IntT (32), Named (Local ("1")), PointerT (IntT (32), DefaultAddrSpace ()), Named (Local ("i.addr")), DefaultAlign ()) store i32 %1, i32* %i.addr 14 / 15
  11. The End Next step String interpolation support in sbt-rats Further

    information LLVM llvm.org The Architecture of Open Source Applications: http://www.aosabook.org/en/llvm.html Software Language Engineering sbt-rats: bitbucket.org/inkytonik/sbt-rats Kiama: bitbucket.org/inkytonik/kiama Twitter @plvmq @inkytonik 15 / 15