Slide 1

Slide 1 text

LLVM and Scala Anthony M. Sloane Programming Languages and Verification Research Group Department of Computing Macquarie University @plvmq [email protected] @inkytonik

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Three-Phase Compilation 3 / 15

Slide 4

Slide 4 text

LLVM-based Compilation 4 / 15

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Why LLVM and Scala? 7 / 15

Slide 8

Slide 8 text

LLVM and Kiama Kiama High-level declarative language processing using Scala-based DSLs. Features tree attribution, term rewriting and pretty-printing. 8 / 15

Slide 9

Slide 9 text

Consuming and Creating LLVM IR in Scala 9 / 15

Slide 10

Slide 10 text

Function Definition Syntax define i32 @test(i32 %i) { ... } ----- FunctionDefinition : Item = "define" ... Type sp Global ’(’ Arguments ")" ... FunctionBody \n. FunctionBody = "{" nestnl (Block+) \n "}". 10 / 15

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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