-
Notifications
You must be signed in to change notification settings - Fork 79
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
How to create malloc in llir/llvm? #52
Comments
I'm afraid the answer is likely not what you're looking for, but at the moment to the best of my knowledge you need to implement something like this, which is effectively what happens in the bindings you linked to. That's what's going on under the hood: The I can imagine that at some point |
I don't know if this answers your question, but: I've been using the glibc malloc like this in tre. I'm then able to use the malloc function like this. mallocFunc := module.NewFunc("malloc",
types.NewPointer(types.I8),
ir.NewParam("", types.I64),
)
mallocatedSpaceRaw := block.NewCall(mallocFunc, constant.NewInt(types.I64, structType.Size()))
alloc := block.NewBitCast(mallocatedSpaceRaw, types.NewPointer(structType.LLVM())) |
Perfect! It's brilliant to have a quick answer to this. I didn't know it myself. Closing this issue as it seem to have been resolved. Cheers, |
Indeed. A package with IR builder like functionality will most likely live somewhere in When it comes to the IR builder package, we are essentially collecting experiences from any Gopher playing with LLVM IR to build compilers and other tools and libraries. It may very well be that common practices emerge that are similar but also unlike those used in the C++ builder API, and that is what we want. To see what such an API may look, if re-designed for the Go ecosystem. The predominant approach today, seem to be to create a structure which tracks the context of the IR generator/builder (i.e. current function being generated, current basic block, etc); e.g. as is done by the Compiler type of tre: type Compiler struct {
module *ir.Module
...
contextBlock *ir.BasicBlock
...
} A similar approach it taken by uc, which uses irgen.Function to keep the context of functions being generated: // A Function represents an LLVM IR function generator.
type Function struct {
// Function being generated.
*ir.Function
// Current basic block being generated.
curBlock *BasicBlock
... As we gain more experience using llir to develop compilers and analysis tools, we will also see what approaches for IR generation look clean, are general and powerful. We'll probably iterate a few times before declaring an "official" llir At least, those are the thoughts on the top of my head. Something similar would probably happen for |
@zegl Cool, didn't know tre had escape analysis :) |
Basically what I need is something like this:
https://godoc.org/llvm.org/llvm/bindings/go/llvm#Builder.CreateMalloc
In my case, I'm trying to create a currying function call, it would need to store parameters in a structure
The text was updated successfully, but these errors were encountered: