Giter Club home page Giter Club logo

Comments (4)

mewmew avatar mewmew commented on September 21, 2024 2

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

from llvm.

dannypsnl avatar dannypsnl commented on September 21, 2024 1

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

from llvm.

mewmew avatar mewmew commented on September 21, 2024 1

@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

from llvm.

dannypsnl avatar dannypsnl commented on September 21, 2024

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

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.