Skip to content

Commit

Permalink
CORE: Split the abi.Abi interface into an abi.Layout interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Feb 8, 2024
1 parent 3418ba0 commit 548b40e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 92 deletions.
2 changes: 0 additions & 2 deletions core/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ type Abi interface {
Size(type_ ast.Type) uint32
Align(type_ ast.Type) uint32

Fields(decl *ast.Struct) ([]*ast.Field, []uint32)

Classify(type_ ast.Type, args []Arg) []Arg
}

Expand Down
23 changes: 1 addition & 22 deletions core/abi/amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,13 @@ var AMD64 Abi = &amd64{}
type amd64 struct{}

func (a *amd64) Size(type_ ast.Type) uint32 {
if type_, ok := ast.As[*ast.Struct](type_); ok {
layout := cLayout{}

for _, field := range type_.Fields {
layout.add(a.Size(field.Type), a.Align(field.Type))
}

return layout.size()
}

return getX64Size(a, type_)
}

func (a *amd64) Align(type_ ast.Type) uint32 {
return getX64Align(type_)
}

func (a *amd64) Fields(decl *ast.Struct) ([]*ast.Field, []uint32) {
layout := cLayout{}
offsets := make([]uint32, len(decl.Fields))

for i, field := range decl.Fields {
offsets[i] = layout.add(a.Size(field.Type), a.Align(field.Type))
}

return decl.Fields, offsets
}

func (a *amd64) Classify(type_ ast.Type, args []Arg) []Arg {
switch type_ := type_.Resolved().(type) {
case *ast.Primitive:
Expand Down Expand Up @@ -120,7 +99,7 @@ func (a *amd64) flatten(type_ ast.Type, baseOffset uint32, args []Arg) []Arg {
return args

case *ast.Struct:
fields, offsets := a.Fields(type_)
fields, offsets := GetStructLayout(type_).Fields(a, type_)

for i, field := range fields {
offset := baseOffset + offsets[i]
Expand Down
35 changes: 32 additions & 3 deletions core/abi/c_layout.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
package abi

type cLayout struct {
import (
"fireball/core/ast"
)

var CLayout Layout = &cLayout{}

type cLayout struct{}

func (c *cLayout) Size(abi Abi, decl *ast.Struct) uint32 {
layout := cFieldAligner{}

for _, field := range decl.Fields {
layout.add(abi.Size(field.Type), abi.Align(field.Type))
}

return layout.size()
}

func (c *cLayout) Fields(abi Abi, decl *ast.Struct) ([]*ast.Field, []uint32) {
layout := cFieldAligner{}
offsets := make([]uint32, len(decl.Fields))

for i, field := range decl.Fields {
offsets[i] = layout.add(abi.Size(field.Type), abi.Align(field.Type))
}

return decl.Fields, offsets
}

type cFieldAligner struct {
biggestAlign uint32
offset uint32
}

func (l *cLayout) add(size, align uint32) uint32 {
func (l *cFieldAligner) add(size, align uint32) uint32 {
l.biggestAlign = max(l.biggestAlign, align)

offset := alignBytes(l.offset, align)
Expand All @@ -14,7 +43,7 @@ func (l *cLayout) add(size, align uint32) uint32 {
return offset
}

func (l *cLayout) size() uint32 {
func (l *cFieldAligner) size() uint32 {
if l.offset == 0 {
return 0
}
Expand Down
43 changes: 0 additions & 43 deletions core/abi/fb.go

This file was deleted.

13 changes: 13 additions & 0 deletions core/abi/layout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package abi

import "fireball/core/ast"

type Layout interface {
Size(abi Abi, decl *ast.Struct) uint32

Fields(abi Abi, decl *ast.Struct) ([]*ast.Field, []uint32)
}

func GetStructLayout(s *ast.Struct) Layout {
return CLayout
}
21 changes: 0 additions & 21 deletions core/abi/win64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,13 @@ var WIN64 Abi = &win64{}
type win64 struct{}

func (w *win64) Size(type_ ast.Type) uint32 {
if type_, ok := ast.As[*ast.Struct](type_); ok {
layout := cLayout{}

for _, field := range type_.Fields {
layout.add(w.Size(field.Type), w.Align(field.Type))
}

return layout.size()
}

return getX64Size(w, type_)
}

func (w *win64) Align(type_ ast.Type) uint32 {
return getX64Align(type_)
}

func (w *win64) Fields(decl *ast.Struct) ([]*ast.Field, []uint32) {
layout := cLayout{}
offsets := make([]uint32, len(decl.Fields))

for i, field := range decl.Fields {
offsets[i] = layout.add(w.Size(field.Type), w.Align(field.Type))
}

return decl.Fields, offsets
}

func (w *win64) Classify(type_ ast.Type, args []Arg) []Arg {
switch type_ := type_.Resolved().(type) {
case *ast.Primitive:
Expand Down
3 changes: 3 additions & 0 deletions core/abi/x64.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func getX64Size(abi Abi, type_ ast.Type) uint32 {
case *ast.Array:
return abi.Size(type_.Base) * type_.Count

case *ast.Struct:
return GetStructLayout(type_).Size(abi, type_)

case *ast.Enum:
return abi.Size(type_.ActualType)

Expand Down
2 changes: 1 addition & 1 deletion core/codegen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (t *types) getMeta(type_ ast.Type) ir.MetaID {
return t.cacheMeta(type_, typ)

case *ast.Struct:
fields, offsets := abi.GetTargetAbi().Fields(type_)
fields, offsets := abi.GetStructLayout(type_).Fields(abi.GetTargetAbi(), type_)
fieldsMeta := make([]ir.MetaID, len(fields))

for i, field := range fields {
Expand Down

0 comments on commit 548b40e

Please sign in to comment.