Skip to content

Commit

Permalink
initial addition of new StackType
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Feb 10, 2023
1 parent 42a112a commit e54d4e7
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 79 deletions.
22 changes: 11 additions & 11 deletions data/transactions/logic/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ type ProgramKnowledge struct {

func (pgm *ProgramKnowledge) top() (StackType, bool) {
if len(pgm.stack) == 0 {
return pgm.bottom, pgm.bottom != StackNone
return pgm.bottom, pgm.bottom.AVMType != avmNone
}
last := len(pgm.stack) - 1
return pgm.stack[last], true
Expand Down Expand Up @@ -387,7 +387,7 @@ func (ops *OpStream) returns(spec *OpSpec, replacement StackType) {
end := len(ops.known.stack)
tip := ops.known.stack[end-len(spec.Return.Types):]
for i := range tip {
if tip[i] == StackAny {
if tip[i].AVMType == avmAny {
tip[i] = replacement
return
}
Expand Down Expand Up @@ -1226,7 +1226,7 @@ func typeBury(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, err

idx := top - n
if idx < 0 {
if pgm.bottom == StackNone {
if pgm.bottom.AVMType == avmNone {
// By demanding n+1 elements, we'll trigger an error
return anyTypes(n + 1), nil, nil
}
Expand Down Expand Up @@ -1335,7 +1335,7 @@ func typeDupTwo(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, e
func typeSelect(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, error) {
top := len(pgm.stack) - 1
if top >= 2 {
if pgm.stack[top-1] == pgm.stack[top-2] {
if pgm.stack[top-1].AVMType == pgm.stack[top-2].AVMType {
return nil, StackTypes{pgm.stack[top-1]}, nil
}
}
Expand Down Expand Up @@ -1421,7 +1421,7 @@ func typeStores(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, e
for i := range pgm.scratchSpace {
// We can't know what slot stacktop is being stored in, but we can at least keep the slots that are the same type as stacktop
if pgm.scratchSpace[i] != pgm.stack[top] {
pgm.scratchSpace[i] = StackAny
pgm.scratchSpace[i].AVMType = avmAny
}
}
return nil, nil, nil
Expand All @@ -1442,7 +1442,7 @@ func typeProto(pgm *ProgramKnowledge, args []string) (StackTypes, StackTypes, er
return nil, nil, nil
}

if len(pgm.stack) != 0 || pgm.bottom != StackAny {
if len(pgm.stack) != 0 || pgm.bottom.AVMType != avmAny {
return nil, nil, fmt.Errorf("proto must be unreachable from previous PC")
}
pgm.stack = anyTypes(a)
Expand Down Expand Up @@ -1693,13 +1693,13 @@ func (le lineError) Unwrap() error {
return le.Err
}

func typecheck(expected, got StackType) bool {
func typecheck(expected, got avmType) bool {
// Some ops push 'any' and we wait for run time to see what it is.
// Some of those 'any' are based on fields that we _could_ know now but haven't written a more detailed system of typecheck for (yet).
if expected == StackAny && got == StackNone { // Any is lenient, but stack can't be empty
if expected == avmAny && got == avmNone { // Any is lenient, but stack can't be empty
return false
}
if (expected == StackAny) || (got == StackAny) {
if (expected == avmAny) || (got == avmAny) {
return true
}
return expected == got
Expand Down Expand Up @@ -1813,7 +1813,7 @@ func (ops *OpStream) trackStack(args StackTypes, returns StackTypes, instruction
return
}
argcount := len(args)
if argcount > len(ops.known.stack) && ops.known.bottom == StackNone {
if argcount > len(ops.known.stack) && ops.known.bottom.AVMType == avmNone {
ops.typeErrorf("%s expects %d stack arguments but stack height is %d",
strings.Join(instruction, " "), argcount, len(ops.known.stack))
} else {
Expand All @@ -1827,7 +1827,7 @@ func (ops *OpStream) trackStack(args StackTypes, returns StackTypes, instruction
} else {
ops.trace(", %s", argType)
}
if !typecheck(argType, stype) {
if !typecheck(argType.AVMType, stype.AVMType) {
ops.typeErrorf("%s arg %d wanted type %s got %s",
strings.Join(instruction, " "), i, argType, stype)
}
Expand Down
Loading

0 comments on commit e54d4e7

Please sign in to comment.