compiler, reflect: replace package and name length with null-byte termination

This commit is contained in:
Damian Gryski
2023-03-03 11:24:39 -08:00
committed by Damian Gryski
parent 2de64d3f4e
commit 6a45b73fcb
3 changed files with 25 additions and 18 deletions
+6 -10
View File
@@ -112,10 +112,8 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
typeFieldTypes = append(typeFieldTypes,
types.NewVar(token.NoPos, nil, "ptrTo", types.Typ[types.UnsafePointer]),
types.NewVar(token.NoPos, nil, "underlying", types.Typ[types.UnsafePointer]),
types.NewVar(token.NoPos, nil, "pkglen", types.Typ[types.Uintptr]),
types.NewVar(token.NoPos, nil, "pkgpath", types.Typ[types.UnsafePointer]),
types.NewVar(token.NoPos, nil, "len", types.Typ[types.Uintptr]),
types.NewVar(token.NoPos, nil, "name", types.NewArray(types.Typ[types.Int8], int64(len(typ.Obj().Name())))),
types.NewVar(token.NoPos, nil, "name", types.NewArray(types.Typ[types.Int8], 1+int64(len(typ.Obj().Name())))),
)
case *types.Chan, *types.Slice:
typeFieldTypes = append(typeFieldTypes,
@@ -183,7 +181,7 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
pkgpathName = "reflect/types.type.pkgpath:" + pkgpath
}
pkgpathInitializer := c.ctx.ConstString(pkgpath, false)
pkgpathInitializer := c.ctx.ConstString(pkgpath+"\x00", false)
pkgpathGlobal := llvm.AddGlobal(c.mod, pkgpathInitializer.Type(), pkgpathName)
pkgpathGlobal.SetInitializer(pkgpathInitializer)
pkgpathGlobal.SetAlignment(1)
@@ -196,12 +194,10 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
})
typeFields = []llvm.Value{
c.getTypeCode(types.NewPointer(typ)), // ptrTo
c.getTypeCode(typ.Underlying()), // underlying
llvm.ConstInt(c.uintptrType, uint64(len(pkgpath)), false), // pkgpath length
pkgpathPtr, // pkgpath pointer
llvm.ConstInt(c.uintptrType, uint64(len(name)), false), // length
c.ctx.ConstString(name, false), // name
c.getTypeCode(types.NewPointer(typ)), // ptrTo
c.getTypeCode(typ.Underlying()), // underlying
pkgpathPtr, // pkgpath pointer
c.ctx.ConstString(name+"\x00", false), // name
}
metabyte |= 1 << 5 // "named" flag
case *types.Chan: