-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE: By default use a custom fireball struct layout which re-orders …
…fields to minimize padding CORE: Add C struct attribute to use the C layout
- Loading branch information
1 parent
548b40e
commit d5ae248
Showing
13 changed files
with
239 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package abi | ||
|
||
import ( | ||
"fireball/core/ast" | ||
"slices" | ||
) | ||
|
||
var FbLayout Layout = &fbLayout{} | ||
|
||
type fbLayout struct{} | ||
|
||
func (f *fbLayout) Size(abi Abi, decl *ast.Struct) uint32 { | ||
fields := f.sorted(abi, decl) | ||
|
||
layout := cFieldAligner{} | ||
|
||
for _, field := range fields { | ||
layout.add(abi.Size(field.Type), abi.Align(field.Type)) | ||
} | ||
|
||
return layout.size() | ||
} | ||
|
||
func (f *fbLayout) Fields(abi Abi, decl *ast.Struct) ([]*ast.Field, []uint32) { | ||
fields := f.sorted(abi, decl) | ||
|
||
layout := cFieldAligner{} | ||
offsets := make([]uint32, len(fields)) | ||
|
||
for i, field := range fields { | ||
offsets[i] = layout.add(abi.Size(field.Type), abi.Align(field.Type)) | ||
} | ||
|
||
return fields, offsets | ||
} | ||
|
||
func (f *fbLayout) sorted(abi Abi, decl *ast.Struct) []*ast.Field { | ||
fields := make([]*ast.Field, len(decl.Fields)) | ||
copy(fields, decl.Fields) | ||
|
||
slices.SortStableFunc(fields, func(f1, f2 *ast.Field) int { | ||
a1 := abi.Align(f1.Type) | ||
a2 := abi.Align(f2.Type) | ||
|
||
if a1 < a2 { | ||
return +1 | ||
} | ||
if a1 > a2 { | ||
return -1 | ||
} | ||
return 0 | ||
}) | ||
|
||
return fields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.