Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/llvmPublic

Library for interacting with LLVM IR in pure Go.

License

0BSD, Unlicense licenses found

Licenses found

0BSD
LICENSE
Unlicense
UNLICENSE
NotificationsYou must be signed in to change notification settings

llir/llvm

Repository files navigation

Build StatusCoverage StatusGo Report Cardgo.dev reference

Library for interacting withLLVM IR in pure Go.

Introduction

Installation

go get github.com/llir/llvm/...

Versions

Map betweenllir/llvm tagged releases and LLVM release versions.

Users

Usage

Input example, parsing LLVM IR assembly

Example usage in GoDoc.

// This example parses an LLVM IR assembly file and pretty-prints the data types// of the parsed module to standard output.package mainimport ("log""github.com/kr/pretty""github.com/llir/llvm/asm")funcmain() {// Parse the LLVM IR assembly file `foo.ll`.m,err:=asm.ParseFile("foo.ll")iferr!=nil {log.Fatalf("%+v",err)}// Pretty-print the data types of the parsed LLVM IR module.pretty.Println(m)}

Output examples, producing LLVM IR assembly

Hello, World

Example usage in GoDoc.

// This example produces LLVM IR generating "Hello, World" output.package mainimport ("fmt""github.com/llir/llvm/ir""github.com/llir/llvm/ir/constant""github.com/llir/llvm/ir/types")funcmain() {// Create a new LLVM IR module.m:=ir.NewModule()hello:=constant.NewCharArrayFromString("Hello, world!\n\x00")str:=m.NewGlobalDef("str",hello)// Add external function declaration of puts.puts:=m.NewFunc("puts",types.I32,ir.NewParam("",types.NewPointer(types.I8)))main:=m.NewFunc("main",types.I32)entry:=main.NewBlock("")// Cast *[15]i8 to *i8.zero:=constant.NewInt(types.I64,0)gep:=constant.NewGetElementPtr(hello.Typ,str,zero,zero)entry.NewCall(puts,gep)entry.NewRet(constant.NewInt(types.I32,0))fmt.Println(m)}

Pseudo Random-Number Generator

Example usage in GoDoc.

// This example produces LLVM IR code equivalent to the following C code, which// implements a pseudo-random number generator.////    int abs(int x);////    int seed = 0;////    // ref: https://en.wikipedia.org/wiki/Linear_congruential_generator//    //    a = 0x15A4E35//    //    c = 1//    int rand(void) {//       seed = seed*0x15A4E35 + 1;//       return abs(seed);//    }package mainimport ("fmt""github.com/llir/llvm/ir""github.com/llir/llvm/ir/constant""github.com/llir/llvm/ir/types")funcmain() {// Create convenience types and constants.i32:=types.I32zero:=constant.NewInt(i32,0)a:=constant.NewInt(i32,0x15A4E35)// multiplier of the PRNG.c:=constant.NewInt(i32,1)// increment of the PRNG.// Create a new LLVM IR module.m:=ir.NewModule()// Create an external function declaration and append it to the module.////    int abs(int x);abs:=m.NewFunc("abs",i32,ir.NewParam("x",i32))// Create a global variable definition and append it to the module.////    int seed = 0;seed:=m.NewGlobalDef("seed",zero)// Create a function definition and append it to the module.////    int rand(void) { ... }rand:=m.NewFunc("rand",i32)// Create an unnamed entry basic block and append it to the `rand` function.entry:=rand.NewBlock("")// Create instructions and append them to the entry basic block.tmp1:=entry.NewLoad(i32,seed)tmp2:=entry.NewMul(tmp1,a)tmp3:=entry.NewAdd(tmp2,c)entry.NewStore(tmp3,seed)tmp4:=entry.NewCall(abs,tmp3)entry.NewRet(tmp4)// Print the LLVM IR assembly of the module.fmt.Println(m)}

Analysis example, processing LLVM IR

Example usage in GoDoc.

// This example program analyses an LLVM IR module to produce a callgraph in// Graphviz DOT format.package mainimport ("fmt""strings""github.com/llir/llvm/asm""github.com/llir/llvm/ir")funcmain() {// Parse LLVM IR assembly file.m,err:=asm.ParseFile("foo.ll")iferr!=nil {panic(err)}// Produce callgraph of module.callgraph:=genCallgraph(m)// Output callgraph in Graphviz DOT format.fmt.Println(callgraph)}// genCallgraph returns the callgraph in Graphviz DOT format of the given LLVM// IR module.funcgenCallgraph(m*ir.Module)string {buf:=&strings.Builder{}buf.WriteString("digraph {\n")// For each function of the module.for_,f:=rangem.Funcs {// Add caller node.caller:=f.Ident()fmt.Fprintf(buf,"\t%q\n",caller)// For each basic block of the function.for_,block:=rangef.Blocks {// For each non-branching instruction of the basic block.for_,inst:=rangeblock.Insts {// Type switch on instruction to find call instructions.switchinst:=inst.(type) {case*ir.InstCall:callee:=inst.Callee.Ident()// Add edges from caller to callee.fmt.Fprintf(buf,"\t%q -> %q\n",caller,callee)}}// Terminator of basic block.switchterm:=block.Term.(type) {case*ir.TermRet:// do something._=term}}}buf.WriteString("}")returnbuf.String()}

License

Thellir/llvm project is dual-licensed to thepublic domain and under azero-clause BSD license. You may choose either license to govern your use ofllir/llvm.

About

Library for interacting with LLVM IR in pure Go.

Topics

Resources

License

0BSD, Unlicense licenses found

Licenses found

0BSD
LICENSE
Unlicense
UNLICENSE

Stars

Watchers

Forks

Contributors14

Languages


[8]ページ先頭

©2009-2025 Movatter.jp