Comments (1)
Here is my own solution (Thanks to C and clang).
for those interested, here is the revised code:
func TestStrAllocGEN(*testing.T) {
// Create a new LLVM IR module.
m := ir.NewModule()
// Convenience types and values.
i32 := types.I32
i8 := types.I8
i8ptr := types.NewPointer(i8)
// create link to stdlib.h
// add string functions
strcpy := m.NewFunc("strcpy", i8ptr, ir.NewParam("dst", i8ptr), ir.NewParam("src", i8ptr))
puts := m.NewFunc("puts", i32, ir.NewParam("s", i8ptr))
// memory management
malloc := m.NewFunc("malloc", i8ptr, ir.NewParam("size", types.I32))
free := m.NewFunc("free", types.Void, ir.NewParam("ptr", i8ptr))
// Create a global variable of type string.
str := m.NewGlobal("str", i8ptr)
str.Init = constant.NewNull(i8ptr)
// test constant
constantStr0 := constant.NewCharArrayFromString("Hello, World!\n\x00")
constantStr1 := m.NewGlobalDef(".str0", constantStr0)
// ---------------------------------------------------------
// main ()
// ---------------------------------------------------------
// Create a new function main which returns an i32.
main := m.NewFunc("main", types.I32)
entry := main.NewBlock("")
length := int64(len(constantStr0.X))
// allocate heap memory for global strings
str2 := entry.NewCall(malloc, constant.NewInt(types.I32, length))
entry.NewStore(str2, str)
// copy string to allocated memory
str3 := entry.NewLoad(i8ptr, str)
entry.NewCall(strcpy, str3, constantStr1)
// capture the pointer to the string
str10 := entry.NewLoad(i8ptr, str)
// call puts
entry.NewCall(puts, str10)
// free memory
entry.NewCall(free, str10)
// Return 0 from main.
entry.NewRet(constant.NewInt(types.I32, 0))
fmt.Println(m)
}
And the resulting LLVM:
@str = global i8* null
@.str0 = global [15 x i8] c"Hello, World!\0A\00"
declare i8* @strcpy(i8* %dst, i8* %src)
declare i32 @puts(i8* %s)
declare i8* @malloc(i32 %size)
declare void @free(i8* %ptr)
define i32 @main() {
0:
%1 = call i8* @malloc(i32 15)
store i8* %1, i8** @str
%2 = load i8*, i8** @str
%3 = call i8* @strcpy(i8* %2, [15 x i8]* @.str0)
%4 = load i8*, i8** @str
%5 = call i32 @puts(i8* %4)
call void @free(i8* %4)
ret i32 0
}
from llvm.
Related Issues (20)
- 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
- incorrect cmpxchg signature HOT 4
- How to generate ir for embedded struct? HOT 6
- Convert to string HOT 1
- Hello 10 instead of Hello world. Passing a IntPtr to printf (go - LLIR/LLVM) HOT 3
- unable to generate LLVM: structure, external , noundef and ptr HOT 1
- No way to find underlying type of pointer or array HOT 1
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.