llir / llvm

Library for interacting with LLVM IR in pure Go.
https://llir.github.io/document/
BSD Zero Clause License
1.19k stars 78 forks source link

How to have two structs referencing eachother #224

Closed RedCubeDev-ByteSpace closed 2 years ago

RedCubeDev-ByteSpace commented 2 years ago

Hello there šŸ‘‹

I'm currently working on a compiler using this package and have hit a little road block while working on classes. I would like two classes (which are represented as structs) being able to reference eachother, so A having a pointer to B and B having a pointer to A but I couldn't figure out how to implement that using this library, as they need to be defined in some order and as far as I know typedefs are immutable after being created.

I'm sure theres a way of doing it that I just havent thought of yet haha

Heres a C++ example of exactly what I'm trying todo:

class A;
class B;

class A {
    public:
    B *num;
};

class B {
    public:
    A *num;
};

Compiling to:

%class.A = type { %class.B* }
%class.B = type { %class.A* }

Hope someone here can help me out!

dannypsnl commented 2 years ago

For short(Iā€™m not with my computer now), I believe you will just take the type back from module instance.

mewmew commented 2 years ago

Hi @RedCubeDev-ByteSpace,

Happy to see you playing around with LLVM IR : )

Adding a bit to what @dannypsnl wrote.

Self-referential types, and circular references are always a bit difficult to deal with. That being said, it should be possible to do what you wish as follows:

package main

import (
    "fmt"

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

func main() {
    m := ir.NewModule()
    classAType := &types.StructType{}
    classBType := &types.StructType{}
    classAPtrType := types.NewPointer(classAType)
    classBPtrType := types.NewPointer(classBType)
    classAType.Fields = append(classAType.Fields, classBPtrType)
    classBType.Fields = append(classBType.Fields, classAPtrType)
    m.NewTypeDef("class.A", classAType)
    m.NewTypeDef("class.B", classBType)
    // %class.A = type { %class.B* }
    // %class.B = type { %class.A* }
    fmt.Println(m)
}

Note, there is nothing "special" about type definitions, they are just a regular ir/types.Type with a name set. The type is then appended to the TypeDefs field of ir.Module as regular.

From running the command go doc -src github.com/llir/llvm/ir.Module.NewTypeDef:

package ir // import "github.com/llir/llvm/ir"

// NewTypeDef appends a new type definition to the module based on the given
// type name and underlying type.
func (m *Module) NewTypeDef(name string, typ types.Type) types.Type {
    typ.SetName(name)
    m.TypeDefs = append(m.TypeDefs, typ)
    return typ
}

(That's how type definitions are defined.)

So you can just set the name after you're done adding all the circular reference types to the StructType.Fields slice.

Happy coding!

Cheers, Robin

dannypsnl commented 2 years ago

@mewmew Do you think we need GetFuncByName(name string) and GetTypeByName(name string)?

mewmew commented 2 years ago

@mewmew Do you think we need GetFuncByName(name string) and GetTypeByName(name string)?

These would be easy to implement in irutil. The implementation may either just do a range loop over the slice and check the name.

Or, if the order of declarations and definitions does not matter, we could sort them alphanumerically and then use binary search. Probably this approach is overkill and we should also have to make sure to keep the list sorted after inserting new elements. So, if this is added to irutil, I'd suggest going with the simpler approach of just using a range loop.

Cheers, Robin