llvm icon indicating copy to clipboard operation
llvm copied to clipboard

How to generate ir for embedded struct?

Open wiltchamberian opened this issue 1 year ago • 6 comments
trafficstars

I didn't find about anything to process embedded struct, such as type A struct{ value int } type B struct{ a A }

what should I fill in " b := types.NewStruct(/what to fill here?/) "

wiltchamberian avatar Dec 19 '23 05:12 wiltchamberian

From my understanding, embedded struct is a field that got an implicit name, so you insert reference to A and give a generated name.

dannypsnl avatar Dec 19 '23 05:12 dannypsnl

yes, I understand, but how to write the code there, it is a type of types.Var, so what if I have a types.Struct?

wiltchamberian avatar Dec 19 '23 05:12 wiltchamberian

I believe you can use the "variable" in Go as instance of "type"

dannypsnl avatar Dec 19 '23 06:12 dannypsnl

in fact, not that easy. the variable in Go has type *types.Struct but the input needs to be a *types.Var, types.Var includes types.object and types.object include golang "Type", but not a types.Struct, so I have no idea about how to input the parameters. Or if you can write down the code.

wiltchamberian avatar Dec 19 '23 07:12 wiltchamberian

	m := ir.NewModule()

	foo := m.NewTypeDef("foo", types.NewStruct(types.I32))
	_ = m.NewTypeDef("bar", types.NewStruct(foo))

	main := m.NewFunc("main", types.I32)

	{
		entry := main.NewBlock("")
		entry.NewRet(constant.NewInt(types.I32, 0))
	}

	fmt.Println(m)

output

%foo = type { i32 }
%bar = type { %foo }

define i32 @main() {
0:
	ret i32 0
}

dannypsnl avatar Dec 20 '23 02:12 dannypsnl

You can also use types.NewPointer(foo) to get:

%foo = type { i32 }
%bar = type { %foo* }

define i32 @main() {
0:
	ret i32 0
}

dannypsnl avatar Dec 20 '23 02:12 dannypsnl