Exemplo n.º 1
0
func (tm *LLVMTypeMap) ToLLVM(t types.Type) llvm.Type {
	t = types.Underlying(t)
	tstr := t.String()
	lt, ok := tm.types[tstr]
	if !ok {
		lt = tm.makeLLVMType(tstr, t)
		if lt.IsNil() {
			panic(fmt.Sprint("Failed to create LLVM type for: ", t))
		}
	}
	return lt
}
Exemplo n.º 2
0
func (tm *TypeMap) toRuntime(t types.Type) (global, value llvm.Value) {
	tstr := t.String()
	info, ok := tm.types[tstr]
	if !ok {
		info.global, info.dyntyp = tm.makeRuntimeType(t)
		if info.dyntyp.IsNil() {
			panic(fmt.Sprint("Failed to create runtime type for: ", t))
		}
		tm.types[tstr] = info
	}
	return info.global, info.dyntyp
}
Exemplo n.º 3
0
func (tm *TypeMap) makeCommonType(t types.Type, k reflect.Kind) llvm.Value {
	// Not sure if there's an easier way to do this, but if you just
	// use ConstStruct, you end up getting a different llvm.Type.
	lt := tm.ToLLVM(t)
	typ := llvm.ConstNull(tm.runtimeCommonType)
	elementTypes := tm.runtimeCommonType.StructElementTypes()

	// Size.
	size := llvm.SizeOf(lt)
	if size.Type().IntTypeWidth() > elementTypes[0].IntTypeWidth() {
		size = llvm.ConstTrunc(size, elementTypes[0])
	}
	typ = llvm.ConstInsertValue(typ, size, []uint32{0})

	// TODO hash
	// TODO padding

	// Alignment.
	align := llvm.ConstTrunc(llvm.AlignOf(lt), llvm.Int8Type())
	typ = llvm.ConstInsertValue(typ, align, []uint32{3}) // var
	typ = llvm.ConstInsertValue(typ, align, []uint32{4}) // field

	// Kind.
	kind := llvm.ConstInt(llvm.Int8Type(), uint64(k), false)
	typ = llvm.ConstInsertValue(typ, kind, []uint32{5})

	// Algorithm table.
	alg := tm.makeAlgorithmTable(t)
	algptr := llvm.AddGlobal(tm.module, alg.Type(), "")
	algptr.SetInitializer(alg)
	algptr = llvm.ConstBitCast(algptr, elementTypes[6])
	typ = llvm.ConstInsertValue(typ, algptr, []uint32{6})

	// String representation.
	stringrep := tm.globalStringPtr(t.String())
	typ = llvm.ConstInsertValue(typ, stringrep, []uint32{8})

	// TODO gc
	return typ
}