Giter Club home page Giter Club logo

Comments (1)

dequeb avatar dequeb commented on June 23, 2024 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.