Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to latest version of llir/llvm #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions example.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ func main() {

// Create convenience types and constants.
i32 := types.I32
zero := constant.NewInt(0, i32)
a := constant.NewInt(0x15A4E35, i32) // multiplier of the PRNG.
c := constant.NewInt(1, i32) // increment of the PRNG.
zero := 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.NewFunction("abs", i32, ir.NewParam("x", i32))
abs := m.NewFunc("abs", i32, ir.NewParam("x", i32))

// Create a global variable definition and append it to the module.
//
Expand All @@ -61,7 +61,7 @@ func main() {
// Create a function definition and append it to the module.
//
// int rand(void) { ... }
rand := m.NewFunction("rand", i32)
rand := m.NewFunc("rand", i32)

// Create an unnamed entry basic block and append it to the `rand` function.
entry := rand.NewBlock("")
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/scottshotgg/llvmIRTest

go 1.12

require (
github.com/inspirer/textmapper v0.0.0-20181209132054-5280caf89d64 // indirect
github.com/llir/llvm v0.3.0-pre4
golang.org/x/tools v0.0.0-20181207222222-4c874b978acb // indirect
)
29 changes: 29 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inspirer/textmapper v0.0.0-20181202234051-312f7a0d146c h1:213Qb/08zOK1MMtHGdf8yuhnil4FcyJQpiP46PvLcCE=
github.com/inspirer/textmapper v0.0.0-20181202234051-312f7a0d146c/go.mod h1:SpoIwXu07A3gguovN379QUCTHpUk1lhX2KIjVxpQOas=
github.com/inspirer/textmapper v0.0.0-20181209132054-5280caf89d64 h1:eWslgvSlGoJ2x2+/xbb+yjGXC1jaeXAF3DnMZkD2TGY=
github.com/inspirer/textmapper v0.0.0-20181209132054-5280caf89d64/go.mod h1:SpoIwXu07A3gguovN379QUCTHpUk1lhX2KIjVxpQOas=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/llir/ll v0.0.0-20181207163918-0cfcde00de3f h1:OJxImTgaK6hQ8w6OOEeNox1LFsORsRxLVQidQCm04nA=
github.com/llir/ll v0.0.0-20181207163918-0cfcde00de3f/go.mod h1:jPRfHdh8JEos8a05CSejK/nIKptCRRixCfhg0zngzpg=
github.com/llir/llvm v0.3.0-pre4 h1:gRG6TiB2QsShFH8PG6vcz00LcwLtZcRX0AlCUbp+5fE=
github.com/llir/llvm v0.3.0-pre4/go.mod h1:5oQbQPEpQQem6CujHgRhfvDzTpCW5iinW3s+W33dG3w=
github.com/mewkiz/pkg v0.0.0-20181119122551-9729f4f4ff2b h1:XHFBx9ZEVHnSCRiTz7w1a/NRBk9x7iyFiqnoN6R+vu8=
github.com/mewkiz/pkg v0.0.0-20181119122551-9729f4f4ff2b/go.mod h1:bhmdGJSMX5WCIBFmk27tBnUvBJm5WxXmarBV41qvbNI=
github.com/mewmew/float v0.0.0-20181121163145-c0f786d7da73 h1:bTqCgPsW3TFb9MFtvaOmGFWVhCmN3EmRw02zkchdOHo=
github.com/mewmew/float v0.0.0-20181121163145-c0f786d7da73/go.mod h1:obQBs6O+vjhgOZLkGdALxItKw4xrI49lSBEnAMO6lWI=
github.com/mewspring/tools v0.0.0-20181204020634-6c6637dc82b6 h1:J/FwQ6PvTeHbDkhGt+nDlIXXGRdXUCqVdL85tmHflAg=
github.com/mewspring/tools v0.0.0-20181204020634-6c6637dc82b6/go.mod h1:UAdVbSksr+7Bg+z4mga16OaBg3qAcgdaF3x3AeqJHEs=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/tools v0.0.0-20181206194817-bcd4e47d0288 h1:uFWEpVI5nk2F/22rsVg40mbEixNPRNrL/d/Qea/VtQk=
golang.org/x/tools v0.0.0-20181206194817-bcd4e47d0288/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181207222222-4c874b978acb h1:YIXCxYolAiiPmVSqA4gVUVcHo8Mi1ivU7ANnK9a63JY=
golang.org/x/tools v0.0.0-20181207222222-4c874b978acb/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
17 changes: 9 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/enum"
"github.com/llir/llvm/ir/types"
)

Expand All @@ -30,13 +31,13 @@ func main() {
// Create a function definition and append it to the module.
//
// int rand(void) { ... }
mainFunc := m.NewFunction("main", types.I32)
mainFunc := m.NewFunc("main", types.I32)
returnBlock := ir.NewBlock("")

For(mainFunc, returnBlock, constant.NewInt(0, types.I32), constant.NewInt(10, types.I32))
For(mainFunc, returnBlock, constant.NewInt(types.I32, 0), constant.NewInt(types.I32, 10))

mainFunc.AppendBlock(returnBlock)
returnBlock.NewRet(constant.NewInt(0, types.I32))
mainFunc.Blocks = append(mainFunc.Blocks, returnBlock)
returnBlock.NewRet(constant.NewInt(types.I32, 0))

// // Print the LLVM IR assembly of the module.
fmt.Println(m)
Expand All @@ -49,15 +50,15 @@ func For(fromFunction *ir.Function, targetBlock *ir.BasicBlock, start, end *cons

startVar := allocBlock.NewAlloca(types.I32)
indexVar := allocBlock.NewAlloca(types.I32)
allocBlock.NewStore(constant.NewInt(0, types.I32), startVar)
allocBlock.NewStore(constant.NewInt(0, types.I32), indexVar)
allocBlock.NewStore(constant.NewInt(types.I32, 0), startVar)
allocBlock.NewStore(constant.NewInt(types.I32, 0), indexVar)
allocBlock.NewBr(compareBlock)

loadedIndexVar := compareBlock.NewLoad(indexVar)
breakOut := compareBlock.NewICmp(ir.IntSLT, loadedIndexVar, end)
breakOut := compareBlock.NewICmp(enum.IPredSLT, loadedIndexVar, end)
compareBlock.NewCondBr(breakOut, incrementBlock, targetBlock)

added := incrementBlock.NewAdd(loadedIndexVar, constant.NewInt(1, types.I32))
added := incrementBlock.NewAdd(loadedIndexVar, constant.NewInt(types.I32, 1))
incrementBlock.NewStore(added, indexVar)
incrementBlock.NewBr(compareBlock)
}