Skip to content

Commit

Permalink
ReferencedTypeInfo does not store size anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
RealA10N committed Dec 12, 2024
1 parent a099a52 commit 8023286
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 68 deletions.
2 changes: 0 additions & 2 deletions gen/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (AddInstructionDefinition) InferTargetTypes(
return []gen.ReferencedTypeInfo{
{
Base: arguments[0].Base,
Size: arguments[0].Size,
Descriptors: arguments[0].Descriptors,
},
}, core.ResultList{}
Expand Down Expand Up @@ -87,7 +86,6 @@ func TestInstructionCreateTarget(t *testing.T) {

intTypeRef := gen.ReferencedTypeInfo{
Base: intType,
Size: intType.Size,
}

registers := RegisterMap{
Expand Down
1 change: 0 additions & 1 deletion gen/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestTargetRegisterAlreadyDefined(t *testing.T) {

intTypeRef := gen.ReferencedTypeInfo{
Base: intType,
Size: intType.Size,
}

registers := RegisterMap{
Expand Down
95 changes: 44 additions & 51 deletions gen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ 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
}

Expand All @@ -52,10 +51,6 @@ func (info ReferencedTypeInfo) Equals(other ReferencedTypeInfo) bool {
return false
}

if info.Size != other.Size {
return false
}

if len(info.Descriptors) != len(other.Descriptors) {
return false
}
Expand Down Expand Up @@ -197,43 +192,6 @@ func NewReferencedTypeGenerator[InstT BaseInstruction]() FileContextGenerator[In
)
}

func (g *ReferencedTypeGenerator[InstT]) calculateTypeSize(
ctx *FileGenerationContext[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.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(
ctx *FileGenerationContext[InstT],
node parse.TypeNode,
Expand Down Expand Up @@ -262,14 +220,8 @@ func (g *ReferencedTypeGenerator[InstT]) Generate(
descriptors = append(descriptors, descriptorInfo)
}

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

typeInfo := ReferencedTypeInfo{
Base: baseType,
Size: size,
Descriptors: descriptors,
}

Expand All @@ -290,6 +242,42 @@ func NewNamedTypeGenerator[InstT BaseInstruction]() FileContextGenerator[InstT,
)
}

func (g *NamedTypeGenerator[InstT]) calculateTypeSize(
ctx *FileGenerationContext[InstT],
node parse.TypeNode,
typeInfo ReferencedTypeInfo,
) (core.UsmUint, core.ResultList) {
size := core.UsmUint(typeInfo.Base.Size)

for _, descriptor := range typeInfo.Descriptors {
switch descriptor.Type {
case PointerTypeDescriptor:
size = ctx.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 *NamedTypeGenerator[InstT]) Generate(
ctx *FileGenerationContext[InstT],
node parse.TypeDeclarationNode,
Expand Down Expand Up @@ -323,15 +311,20 @@ func (g *NamedTypeGenerator[InstT]) Generate(
})
}

referencedType := node.Fields.Nodes[0].Type
referencedTypeInfo, results := g.ReferencedTypeGenerator.Generate(ctx, referencedType)
referencedTypeNode := node.Fields.Nodes[0].Type
referencedTypeInfo, results := g.ReferencedTypeGenerator.Generate(ctx, referencedTypeNode)
if !results.IsEmpty() {
return nil, results
}

size, results := g.calculateTypeSize(ctx, referencedTypeNode, referencedTypeInfo)
if !results.IsEmpty() {
return nil, results
}

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

Expand Down
39 changes: 25 additions & 14 deletions gen/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,41 @@ func TestRepeatTypeTooLarge(t *testing.T) {
typeManager := make(TypeMap)
typeManager.newBuiltinType("$32", 4)

v := core.NewSourceView("$32 ^1_000_000 ^1_000_000")
v := core.NewSourceView("type $tooLarge { $32 ^1_000_000 ^1_000_000 }")
unmanaged := v.Unmanaged()

node := parse.TypeNode{
Identifier: unmanaged.Subview(0, 3),
Decorators: []parse.TypeDecoratorNode{
{
UnmanagedSourceView: unmanaged.Subview(4, 14),
Type: parse.RepeatTypeDecorator,
},
{
UnmanagedSourceView: unmanaged.Subview(15, 25),
Type: parse.RepeatTypeDecorator,
node := parse.TypeDeclarationNode{
UnmanagedSourceView: unmanaged,
Identifier: unmanaged.Subview(5, 14),
Fields: parse.BlockNode[parse.TypeFieldNode]{
UnmanagedSourceView: unmanaged.Subview(15, 44),
Nodes: []parse.TypeFieldNode{
{
Type: parse.TypeNode{
Identifier: unmanaged.Subview(17, 20),
Decorators: []parse.TypeDecoratorNode{
{
UnmanagedSourceView: unmanaged.Subview(21, 31),
Type: parse.RepeatTypeDecorator,
},
{
UnmanagedSourceView: unmanaged.Subview(32, 42),
Type: parse.RepeatTypeDecorator,
},
},
},
},
},
},
}

genCtx := gen.FileGenerationContext[Instruction]{
ctx := &gen.FileGenerationContext[Instruction]{
GenerationContext: &testGenerationContext,
SourceContext: v.Ctx(),
Types: &typeManager,
}

generator := gen.NewReferencedTypeGenerator[Instruction]()
_, results := generator.Generate(&genCtx, node)
generator := gen.NewNamedTypeGenerator[Instruction]()
_, results := generator.Generate(ctx, node)
assert.False(t, results.IsEmpty())
}

0 comments on commit 8023286

Please sign in to comment.