Skip to content

Commit

Permalink
vm: remove toPC parameter in Run method.
Browse files Browse the repository at this point in the history
  • Loading branch information
db47h committed Aug 4, 2016
1 parent aa9b13e commit 4b4bfb6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/retro/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {
if err != nil {
return
}
err = proc.Run(len(proc.Image))
err = proc.Run()
// filter out EOF
if e := errors.Cause(err); e == io.EOF {
err = nil
Expand Down
10 changes: 7 additions & 3 deletions vm/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import "github.com/pkg/errors"
// Opcode represents an Ngaro VM opcode.
type Opcode Cell

// ngaro Virtual Machine Opcodes.
// Ngaro Virtual Machine Opcodes.
const (
OpNop Opcode = iota
OpLit
Expand Down Expand Up @@ -82,13 +82,17 @@ func (i *Instance) Rpop() Cell {
return i.address[rsp]
}

// Run starts execution of the VM until the program pointer reaches `toPC`.
// Run starts execution of the VM.
//
// If an error occurs, the PC will will point to the instruction that triggered
// the error.
//
// If the VM was exited cleanly from a user program with the `bye` word, the PC
// will be equal to len(i.Image) and err will be nil.
func (i *Instance) Run(toPC int) (err error) {
//
// If the last input stream gets closed, the VM will exit and return io.EOF.
// This is a normal exit condition in most use cases.
func (i *Instance) Run() (err error) {
defer func() {
if e := recover(); e != nil {
var ok bool
Expand Down
14 changes: 7 additions & 7 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// TODO:
// - complete file i/o
// - add a reset func: clear stacks/reset ip to 0, accept Options (input / output may need to be reset as well)
// - add a disasm func
// - add a disassembly function.
// - go routines
// - BUG: I/O trashes ports in interactive mode. For example, the following returns 0 instead of the image size:
// -1 5 out 0 0 out wait 5 in putn
Expand Down Expand Up @@ -110,7 +110,7 @@ func InHandler(port Cell, handler IOCallback) Option {
}
}

// OutHandler will make any OUT on the given port call the provided hadler.
// OutHandler will make any OUT on the given port call the provided handler.
// The OUT value will be passed to the handler and the handler's return value
// will be written to the port.
func OutHandler(port Cell, handler IOCallback) Option {
Expand All @@ -120,12 +120,12 @@ func OutHandler(port Cell, handler IOCallback) Option {
}
}

// WaitHandler will make any WAIT on the given port call the provided hadler.
// WaitHandler will make any WAIT on the given port call the provided handler.
// The port value will be passed to the handler and the handler's return value
// will be written to the port.
//
// The handler will only be called if port 0 value is 0 and if the bound port
// value is != 0. Wait handlers can be used to override default port behaviour.
// value is != 0. Wait handlers can be used to override default port behavior.
// If the returned error is ErrUnhandled, the returned value and error will be
// ignored, and the default implementation will handle the WAIT.
func WaitHandler(port int, handler IOCallback) Option {
Expand All @@ -145,7 +145,7 @@ func (i *Instance) SetOptions(opts ...Option) error {
return nil
}

// Instance represents an ngaro VM instance.
// Instance represents an Ngaro VM instance.
type Instance struct {
PC int
sp int
Expand Down Expand Up @@ -191,7 +191,7 @@ func New(image Image, imageFile string, opts ...Option) (*Instance, error) {
}

// Data returns the data stack. Note that value changes will be reflected in the
// instance's stack, but reslicing will not affect it. To add/remove values on
// instance's stack, but re-slicing will not affect it. To add/remove values on
// the data stack, use the Push and Pop functions.
func (i *Instance) Data() []Cell {
if i.sp < len(i.data) {
Expand All @@ -201,7 +201,7 @@ func (i *Instance) Data() []Cell {
}

// Address returns the address stack. Note that value changes will be reflected
// in the instance's stack, but reslicing will not affect it. To add/remove
// in the instance's stack, but re-slicing will not affect it. To add/remove
// values on the address stack, use the Rpush and Rpop functions.
func (i *Instance) Address() []Cell {
if i.rsp < len(i.address) {
Expand Down
18 changes: 9 additions & 9 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func setup(code, stack, rstack C) *vm.Instance {
}

func check(t *testing.T, i *vm.Instance, ip int, stack C, rstack C) {
err := i.Run(len(i.Image))
err := i.Run()
if err != nil {
t.Errorf("%+v", err)
return
Expand Down Expand Up @@ -138,7 +138,7 @@ func ExampleInstance_Run() {

// run it
if err == nil {
err = i.Run(len(i.Image))
err = i.Run()
}
if err != nil {
// in interactive use, err may be io.EOF if any of the IO channels gets closed
Expand Down Expand Up @@ -181,7 +181,7 @@ func ExampleOutHandler() {
panic(err)
}

if err = i.Run(len(i.Image)); err != nil && errors.Cause(err) != io.EOF {
if err = i.Run(); err != nil && errors.Cause(err) != io.EOF {
fmt.Fprintf(os.Stderr, "%+v\n", err)
}
// Retro 11.7.1
Expand Down Expand Up @@ -260,7 +260,7 @@ func Test_Fib_RetroLoop(t *testing.T) {
img, _ := vm.Load(imageFile, 50000)
i, _ := vm.New(img, imageFile,
vm.Input(strings.NewReader(fib)))
i.Run(len(i.Image))
i.Run()
for c := len(i.Address()); c > 0; c-- {
i.Rpop()
}
Expand All @@ -275,7 +275,7 @@ func Benchmark_Fib_AsmLoop(b *testing.B) {
i := setup(f, C{35}, nil)
for c := 0; c < b.N; c++ {
i.PC = 0
i.Run(len(i.Image))
i.Run()
i.Pop()
i.Push(35)
}
Expand All @@ -289,7 +289,7 @@ func Benchmark_Fib_AsmRecursive(b *testing.B) {
i := setup(f, C{35}, nil)
for c := 0; c < b.N; c++ {
i.PC = 0
i.Run(len(i.Image))
i.Run()
i.Pop()
i.Push(35)
}
Expand All @@ -303,7 +303,7 @@ func Benchmark_Fib_RetroLoop(b *testing.B) {
i, _ := vm.New(img, imageFile,
vm.Input(strings.NewReader(fib)))
b.StartTimer()
i.Run(len(i.Image))
i.Run()
}
}

Expand All @@ -315,7 +315,7 @@ func Benchmark_Fib_RetroRecursive(b *testing.B) {
i, _ := vm.New(img, imageFile,
vm.Input(strings.NewReader(fib)))
b.StartTimer()
i.Run(len(i.Image))
i.Run()
}
}

Expand All @@ -342,7 +342,7 @@ func BenchmarkRun(b *testing.B) {
n := time.Now()
b.StartTimer()

err = proc.Run(len(proc.Image))
err = proc.Run()

b.StopTimer()
el := time.Now().Sub(n).Seconds()
Expand Down

0 comments on commit 4b4bfb6

Please sign in to comment.