Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve struct field offset calculation #113

Merged
merged 17 commits into from
Aug 21, 2023
23 changes: 17 additions & 6 deletions internal/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ func emitStructLiteral(meta *ir.MetaCompositLit) {
// push lhs address
emitPushStackTop(types.Uintptr, 0, "address of struct heaad")

fieldOffset := sema.GetStructFieldOffset(metaElm.Field)
fieldOffset := metaElm.Field.Offset
if fieldOffset < 0 {
panic("field offset ist not set")
}
emitAddConst(fieldOffset, "address of struct field")

// push rhs value
Expand Down Expand Up @@ -520,7 +523,7 @@ func emitCall(fv *ir.FuncValue, args []ir.MetaExpr, paramTypes []types.Type, ret
}

func emitAllocReturnVarsAreaFF(ff *ir.Func) {
rtypes := (ff.Signature.ReturnTypes)
rtypes := ff.FuncType.Typ.Results.Types
emitAllocReturnVarsArea(getTotalSizeOfType(rtypes))
}

Expand All @@ -533,8 +536,8 @@ func getTotalSizeOfType(ts []types.Type) int {
}

func emitCallFF(ff *ir.Func) {
ptypes := (ff.Signature.ParamTypes)
rtypes := (ff.Signature.ReturnTypes)
ptypes := ff.FuncType.Typ.Params.Types
rtypes := ff.FuncType.Typ.Results.Types
totalParamSize := getTotalSizeOfType(ptypes)
symbol := ff.PkgName + "." + ff.Name
emitCallQ(sema.NewFuncValueFromSymbol(symbol), totalParamSize, rtypes)
Expand Down Expand Up @@ -2102,8 +2105,7 @@ func GenerateDecls(pkg *ir.AnalyzedPackage, declFilePath string) {
for _, typ := range pkg.Types {
ut := typ.Underlying()
utAsString := sema.SerializeType(ut, true, pkg.Name)
named := typ.(*types.Named)
fmt.Fprintf(fout, "type %s %s\n", named.String(), utAsString)
fmt.Fprintf(fout, "type %s %s\n", typ.String(), utAsString)
}
for _, vr := range pkg.Vars {
fmt.Fprintf(fout, "var %s %s\n", vr.Name.Name, sema.SerializeType(vr.Type, true, pkg.Name))
Expand Down Expand Up @@ -2186,6 +2188,7 @@ func emitInterfaceTables(itab map[string]*sema.ITabEntry) {
// sort map in order to assure the deterministic results
for key, ent := range itab {
entries[ent.Id] = key

}

// skip id=0
Expand All @@ -2204,7 +2207,15 @@ func emitInterfaceTables(itab map[string]*sema.ITabEntry) {
if len(methods) == 0 {
printf(" # no methods\n")
}

for mi, m := range methods {
rcvT := ent.Dtype
rcvPointerType, isPtr := rcvT.(*types.Pointer)
if isPtr {
rcvT = rcvPointerType.Elem()
}
namedRcvT := rcvT.(*types.Named)
printf(" # Dtype %s.%s \n", namedRcvT.PkgName, namedRcvT.String())
dmethod := sema.LookupMethod(ent.Dtype, m.Name)
sym := sema.GetMethodSymbol(dmethod)
printf(" .quad .method_name_%d_%d # %s \n", id, mi, m.Name)
Expand Down
8 changes: 4 additions & 4 deletions internal/ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type IfcConversion struct {

type MetaStructLiteralElement struct {
Tpos token.Pos
Field *ast.Field
Field *types.Var
Type types.Type
Value MetaExpr
}
Expand Down Expand Up @@ -441,7 +441,7 @@ type Func struct {
Retvars []*Variable
Method *Method
Decl *ast.FuncDecl
Signature *Signature
FuncType *types.Func
HasDefer bool
DeferVar *Variable
}
Expand All @@ -451,7 +451,7 @@ type Method struct {
RcvNamedType *ast.Ident
IsPtrMethod bool
Name string
FuncType *ast.FuncType
FuncType *types.Func
}

type Variable struct {
Expand Down Expand Up @@ -498,7 +498,7 @@ type AnalyzedPackage struct {
Path string
Name string
Imports []string
Types []types.Type
Types []*types.Named
Consts []*PackageVarConst
Funcs []*Func
Vars []*PackageVarConst
Expand Down
Loading