From 06c53c3777d7d66ae52387accdcb273466a98d8e Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 23 Mar 2021 16:19:58 +0100 Subject: [PATCH] internal/ops: expose PC and ResetAt Signed-off-by: Elias Naur --- internal/ops/reader.go | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/internal/ops/reader.go b/internal/ops/reader.go index fdab7a3d..c0ed5ef4 100644 --- a/internal/ops/reader.go +++ b/internal/ops/reader.go @@ -12,7 +12,7 @@ import ( // Reader parses an ops list. type Reader struct { - pc pc + pc PC stack []macro ops *op.Ops deferOps op.Ops @@ -38,33 +38,48 @@ type Key struct { // Shadow of op.MacroOp. type macroOp struct { ops *op.Ops - pc pc + pc PC } -type pc struct { +// PC is an instruction counter for an operation list. +type PC struct { data int refs int } type macro struct { ops *op.Ops - retPC pc - endPC pc + retPC PC + endPC PC } type opMacroDef struct { - endpc pc + endpc PC } -// Reset start reading from the op list. +// Reset start reading from the beginning of ops. func (r *Reader) Reset(ops *op.Ops) { + r.ResetAt(ops, PC{}) +} + +// ResetAt is like Reset, except it starts reading from pc. +func (r *Reader) ResetAt(ops *op.Ops, pc PC) { r.stack = r.stack[:0] r.deferOps.Reset() r.deferDone = false - r.pc = pc{} + r.pc = pc r.ops = ops } +// NewPC returns a PC representing the current instruction counter of +// ops. +func NewPC(ops *op.Ops) PC { + return PC{ + data: len(ops.Data()), + refs: len(ops.Refs()), + } +} + func (k Key) SetTransform(t f32.Affine2D) Key { sx, hx, _, hy, sy, _ := t.Elems() k.sx = sx @@ -99,7 +114,7 @@ func (r *Reader) Decode() (EncodedOp, bool) { r.deferDone = true // Execute deferred macros. r.ops = &r.deferOps - r.pc = pc{} + r.pc = PC{} continue } key := Key{ops: r.ops, pc: r.pc.data, version: r.ops.Version()} @@ -174,7 +189,7 @@ func (op *opMacroDef) decode(data []byte) { dataIdx := int(int32(bo.Uint32(data[1:]))) refsIdx := int(int32(bo.Uint32(data[5:]))) *op = opMacroDef{ - endpc: pc{ + endpc: PC{ data: dataIdx, refs: refsIdx, }, @@ -191,7 +206,7 @@ func (m *macroOp) decode(data []byte, refs []interface{}) { refsIdx := int(int32(bo.Uint32(data[5:]))) *m = macroOp{ ops: refs[0].(*op.Ops), - pc: pc{ + pc: PC{ data: dataIdx, refs: refsIdx, },