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

Provide working Hello, World example #227

Closed
eigenhombre opened this issue Feb 15, 2023 · 1 comment
Closed

Provide working Hello, World example #227

eigenhombre opened this issue Feb 15, 2023 · 1 comment

Comments

@eigenhombre
Copy link
Contributor

I'd like to maybe use this for a language implementation, but am inexperienced with LLVM. It took me some time to come up with a snippet equivalent to this example (Also found here).

I think the README would benefit from a Hello, World example, something along the lines of:

package main

import (
	"fmt"

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

// Hello, World in LLVM IR.
func main() {
	// Create a new LLVM IR module.
	m := ir.NewModule()
	arr := constant.NewCharArrayFromString("Hello, world!\n\x00")
	gd := m.NewGlobalDef("str", arr)
	// link to external function puts
	puts := m.NewFunc("puts", types.I32, ir.NewParam("", types.NewPointer(types.I8)))
	main := m.NewFunc("main", types.I32)
	entry := main.NewBlock("")

	// Perform cast per [1]:
	gep := constant.NewGetElementPtr(types.NewArray(15, types.I8),
		gd,
		constant.NewInt(types.I8, 0),
		constant.NewInt(types.I8, 0))
	entry.NewCall(puts, gep)
	entry.NewRet(constant.NewInt(types.I32, 0))
	fmt.Println(m)
}

// See also:
// https://github.com/anoopsarkar/compilers-class-hw/blob/master/llvm-practice/helloworld.ll
eigenhombre added a commit to eigenhombre/llvm that referenced this issue Feb 15, 2023
Alternatively, one of the two IR generation examples could be source-only in an `examples` directory.

For llir#227.
eigenhombre added a commit to eigenhombre/llvm that referenced this issue Feb 17, 2023
mewmew pushed a commit that referenced this issue Feb 17, 2023
Alternatively, one of the two IR generation examples could be source-only in an `examples` directory.

For #227.
@mewmew
Copy link
Member

mewmew commented Feb 17, 2023

Resolved by #228. Thanks @eigenhombre for adding the most canonical example in all of programming to llir/llvm :D

@mewmew mewmew closed this as completed Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants