Comments (3)
Hi Darrel!
To create character arrays, set the CharArray member to true
in the array constant struct.
package main
import (
"fmt"
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/types"
)
func main() {
// Convenience types and values.
i32 := types.I32
i8 := types.I8
i8x50 := types.NewArray(i8, 50)
i8ptr := types.NewPointer(i8)
zero := constant.NewInt(0, i32)
// Function declarations.
m := ir.NewModule()
strcpy := m.NewFunction("strcpy", i8ptr, ir.NewParam("dst", i8ptr), ir.NewParam("src", i8ptr))
strcat := m.NewFunction("strcat", i8ptr, ir.NewParam("dst", i8ptr), ir.NewParam("src", i8ptr))
printf := m.NewFunction("printf", i32, ir.NewParam("format", i8ptr))
printf.Sig.Variadic = true
// Global variables.
str := m.NewGlobalDef(".str", newCharArray("This is source\x00"))
str.IsConst = true
str1 := m.NewGlobalDef(".str.1", newCharArray("This is destination\x00"))
str1.IsConst = true
str2 := m.NewGlobalDef(".str.2", newCharArray("Final destination string : |%s|\x00"))
str2.IsConst = true
// Function definitions.
f := m.NewFunction("main", i32)
entry := f.NewBlock("")
tmp1 := entry.NewAlloca(i32)
tmp2 := entry.NewAlloca(i8x50)
tmp3 := entry.NewAlloca(i8x50)
entry.NewStore(zero, tmp1)
tmp4 := entry.NewGetElementPtr(tmp2, zero, zero)
entry.NewCall(strcpy, tmp4, constant.NewGetElementPtr(str, zero, zero))
tmp6 := entry.NewGetElementPtr(tmp3, zero, zero)
entry.NewCall(strcpy, tmp6, constant.NewGetElementPtr(str1, zero, zero))
tmp8 := entry.NewGetElementPtr(tmp3, zero, zero)
tmp9 := entry.NewGetElementPtr(tmp2, zero, zero)
entry.NewCall(strcat, tmp8, tmp9)
tmp11 := entry.NewGetElementPtr(tmp3, zero, zero)
entry.NewCall(printf, constant.NewGetElementPtr(str2, zero, zero), tmp11)
entry.NewRet(zero)
// Print module to standard output.
fmt.Println(m)
}
func newCharArray(s string) *constant.Array {
var bs []constant.Constant
for i := 0; i < len(s); i++ {
b := constant.NewInt(int64(s[i]), types.I8)
bs = append(bs, b)
}
c := constant.NewArray(bs...)
c.CharArray = true
return c
}
Produces the following LLVM IR output:
@.str = constant [15 x i8] c"This is source\00"
@.str.1 = constant [20 x i8] c"This is destination\00"
@.str.2 = constant [32 x i8] c"Final destination string : |%s|\00"
declare i8* @strcpy(i8* %dst, i8* %src)
declare i8* @strcat(i8* %dst, i8* %src)
declare i32 @printf(i8* %format, ...)
define i32 @main() {
; <label>:0
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca [50 x i8]
store i32 0, i32* %1
%4 = getelementptr [50 x i8], [50 x i8]* %2, i32 0, i32 0
%5 = call i8* @strcpy(i8* %4, i8* getelementptr ([15 x i8], [15 x i8]* @.str, i32 0, i32 0))
%6 = getelementptr [50 x i8], [50 x i8]* %3, i32 0, i32 0
%7 = call i8* @strcpy(i8* %6, i8* getelementptr ([20 x i8], [20 x i8]* @.str.1, i32 0, i32 0))
%8 = getelementptr [50 x i8], [50 x i8]* %3, i32 0, i32 0
%9 = getelementptr [50 x i8], [50 x i8]* %2, i32 0, i32 0
%10 = call i8* @strcat(i8* %8, i8* %9)
%11 = getelementptr [50 x i8], [50 x i8]* %3, i32 0, i32 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr ([32 x i8], [32 x i8]* @.str.2, i32 0, i32 0), i8* %11)
ret i32 0
}
from llvm.
Wow! Thank you so much! You went above and beyond here! Can't wait to experiment with this on the weekend.
The newCharArray function was exactly what I was missing!
PS. Kudos on all your hard work with this library.
from llvm.
Wow! Thank you so much! You went above and beyond here! Can't wait to experiment with this on the weekend.
The newCharArray function was exactly what I was missing!
PS. Kudos on all your hard work with this library.
Glad to see others eager to play with LLVM IR :)
If you play with something fun, would be lovely to check out your git repo later!
Happy hacking!
Cheers /u
from llvm.
Related Issues (20)
- ast parsed error HOT 4
- update llir/llvm to support 13.0 HOT 12
- used by HOT 1
- auto name mangling provider HOT 4
- reject non-pointer type for global HOT 14
- let string loading be easier HOT 6
- `interface{}` -> `any` HOT 7
- ast parse error HOT 10
- `SRet` struct location HOT 3
- How to use Function Pointers? HOT 3
- Wrong function pointer type HOT 1
- indirect br only takes constant addr HOT 1
- Returning void as a value HOT 3
- update llir/llvm to support 14.0 HOT 2
- update llir/llvm to support LLVM 15.0 HOT 7
- How to have two structs referencing eachother HOT 4
- How to convert to exe? HOT 3
- `module.NewTypeDef` has unexpected side effects HOT 17
- Provide working Hello, World example HOT 1
- support llvm 16.0.0 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from llvm.