Skip to content

Commit

Permalink
ReferencedTypeInfo now includes Size field
Browse files Browse the repository at this point in the history
  • Loading branch information
RealA10N committed Dec 10, 2024
1 parent 5e71ec9 commit 9dbfd56
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
5 changes: 1 addition & 4 deletions core/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ type UsmUint = uint32
const UsmUintBitSize = 8 * unsafe.Sizeof(UsmUint(0))

func ParseUint(s string) (UsmUint, error) {
// TODO: assert that strconv.ParseUint does not have edge cases and is dead
// simple, so it won't break our grammar and allow weird behavior which is
// not defined in the USM spec.
n, err := strconv.ParseUint(s, 10, int(UsmUintBitSize))
n, err := strconv.ParseUint(s, 0, int(UsmUintBitSize))
return UsmUint(n), err
}

Expand Down
7 changes: 4 additions & 3 deletions core/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ func TestParseUintDecimal(t *testing.T) {
// expected behavior).
// TODO: consider adding support for other bases?

func TestParseUintBinaryFail(t *testing.T) {
_, err := core.ParseUint("0b101")
assert.NotNil(t, err)
func TestParseUintBinary(t *testing.T) {
n, err := core.ParseUint("0b101")
assert.Nil(t, err)
assert.Equal(t, core.UsmUint(5), n)
}

func TestParseUintPadding(t *testing.T) {
Expand Down
84 changes: 44 additions & 40 deletions gen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,44 +40,11 @@ type TypeDescriptorInfo struct {
type ReferencedTypeInfo struct {
// A pointer to the base, named type that this type reference refers to.
Base *NamedTypeInfo
Size core.UsmUint
Descriptors []TypeDescriptorInfo
Declaration core.UnmanagedSourceView
}

// Calculate the size of the type.
// Iterates over all type descriptors in linear time.
func (i *ReferencedTypeInfo) Size(
archInfo *ArchInfo,
) (core.UsmUint, core.ResultList) {
size := core.UsmUint(i.Base.Size)

for _, descriptor := range i.Descriptors {
switch descriptor.Type {
case PointerTypeDescriptor:
size = archInfo.PointerSize
case RepeatTypeDescriptor:
var ok bool
size, ok = core.Mul(size, descriptor.Amount)
if !ok {
return 0, list.FromSingle(core.Result{{
Type: core.ErrorResult,
Message: "Type size overflow",
Location: &i.Declaration,
}})
}
default:
// notest
return 0, list.FromSingle(core.Result{{
Type: core.InternalErrorResult,
Message: "Unknown type descriptor",
Location: &i.Declaration,
}})
}
}

return size, core.ResultList{}
}

// MARK: Manager

type TypeManager interface {
Expand Down Expand Up @@ -202,7 +169,43 @@ func NewReferencedTypeGenerator[InstT BaseInstruction]() Generator[InstT, parse.
DescriptorGenerator: NewDescriptorGenerator[InstT](),
},
)
}

func (g *ReferencedTypeGenerator[InstT]) calculateTypeSize(
ctx *GenerationContext[InstT],
node parse.TypeNode,
baseType *NamedTypeInfo,
descriptors []TypeDescriptorInfo,
) (core.UsmUint, core.ResultList) {
size := core.UsmUint(baseType.Size)

for _, descriptor := range descriptors {
switch descriptor.Type {
case PointerTypeDescriptor:
size = ctx.ArchInfo.PointerSize
case RepeatTypeDescriptor:
var ok bool
size, ok = core.Mul(size, descriptor.Amount)
if !ok {
v := node.View()
return 0, list.FromSingle(core.Result{{
Type: core.ErrorResult,
Message: "Type size overflow",
Location: &v,
}})
}
default:
// notest
v := node.View()
return 0, list.FromSingle(core.Result{{
Type: core.InternalErrorResult,
Message: "Unknown type descriptor",
Location: &v,
}})
}
}

return size, core.ResultList{}
}

func (g *ReferencedTypeGenerator[InstT]) Generate(
Expand Down Expand Up @@ -233,8 +236,14 @@ func (g *ReferencedTypeGenerator[InstT]) Generate(
descriptors = append(descriptors, descriptorInfo)
}

size, results := g.calculateTypeSize(ctx, node, baseType, descriptors)
if !results.IsEmpty() {
return nil, results
}

typeInfo := &ReferencedTypeInfo{
Base: baseType,
Size: size,
Descriptors: descriptors,
Declaration: node.View(),
}
Expand Down Expand Up @@ -293,14 +302,9 @@ func (g *NamedTypeGenerator[InstT]) Generate(
return nil, results
}

typeSize, results := referencedTypeInfo.Size(&ctx.ArchInfo)
if !results.IsEmpty() {
return nil, results
}

typeInfo = &NamedTypeInfo{
Name: identifier,
Size: typeSize,
Size: referencedTypeInfo.Size,
Declaration: declaration,
}

Expand Down

0 comments on commit 9dbfd56

Please sign in to comment.