Skip to content

Commit

Permalink
all: start removing most dependencies on unsafe
Browse files Browse the repository at this point in the history
unsafe was primarily used to get the cell size in bits. It's now a const.
  • Loading branch information
db47h committed Aug 24, 2016
1 parent 8296245 commit 01f2557
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 11 deletions.
3 changes: 1 addition & 2 deletions asm/parser.go
Expand Up @@ -23,7 +23,6 @@ import (
"strings"
"text/scanner"
"unicode"
"unsafe"

"github.com/db47h/ngaro/vm"
)
Expand Down Expand Up @@ -215,7 +214,7 @@ func (p *parser) scan() (tok rune, s string, v int) {
}

// check int
n, err := strconv.ParseInt(s, 0, 8*int(unsafe.Sizeof(vm.Cell(0))))
n, err := strconv.ParseInt(s, 0, vm.CellBits)
if err == nil {
return scanner.Int, s, int(n)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/retro/main.go
Expand Up @@ -23,7 +23,6 @@ import (
"io"
"os"
"strconv"
"unsafe"

"github.com/db47h/ngaro/vm"
"github.com/pkg/errors"
Expand Down Expand Up @@ -59,7 +58,7 @@ var (
debug bool
dump bool
outFileName string
srcCellSz = cellSizeBits(unsafe.Sizeof(vm.Cell(0)) * 8)
srcCellSz = cellSizeBits(vm.CellBits)
dstCellSz = srcCellSz
)

Expand Down
2 changes: 1 addition & 1 deletion vm/io.go
Expand Up @@ -281,7 +281,7 @@ func (i *Instance) Wait(v, port Cell) error {
i.Ports[5] = 0
}
case -13:
i.Ports[5] = Cell(unsafe.Sizeof(Cell(0)) * 8)
i.Ports[5] = CellBits
case -14:
v = 0x01000000
i.Ports[5] = Cell(*(*int8)(unsafe.Pointer(&v)))
Expand Down
6 changes: 3 additions & 3 deletions vm/io_test.go
Expand Up @@ -108,7 +108,7 @@ func Test_io_Caps(t *testing.T) {
}
assertEqualI(t, "io_Caps port 8", -1, int(i.Pop()))
assertEqualI(t, "io_Caps endian", 0, int(i.Pop()))
assertEqualI(t, "io_Caps Cell bits", 8*int(unsafe.Sizeof(vm.Cell(0))), int(i.Pop()))
assertEqualI(t, "io_Caps Cell bits", 8*int(unsafe.Sizeof(vm.Cell(0))), int(i.Pop())) // do not use vm.CellBits, just to check
assertEqualI(t, "io_Caps rstack", 3, int(i.Pop()))
assertEqualI(t, "io_Caps rstack", 1, int(i.Pop()))
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestLoad(t *testing.T) {
t.Fatalf("Expected -1, got %d", mem[0])
}
// force failure if vm.Cell is 32 bits
if unsafe.Sizeof(vm.Cell(0)) == 4 {
if vm.CellBits == 32 {
_, _, err = vm.Load(fn, 0, 64)
exp := "load error: 64 bits value 8589934591 at memory location 0 too large"
if err == nil || err.Error() != exp {
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestSave_32(t *testing.T) {
return vm.Save(fileName, mem, 32)
}
// force failure if vm.Cell is 64 bits
if unsafe.Sizeof(vm.Cell(0)) == 8 {
if vm.CellBits == 64 {
x := int64(1)
img[2] = vm.Cell(x << 32)
_, err := runImage(img, d, vm.SaveMemImage(sf))
Expand Down
5 changes: 2 additions & 3 deletions vm/mem.go
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/binary"
"io"
"os"
"unsafe"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -79,7 +78,7 @@ func load64(mem []Cell, r io.Reader, fileCells int) error {
func Load(fileName string, minSize, cellBits int) (mem []Cell, fileCells int, err error) {
switch cellBits {
case 0:
cellBits = int(unsafe.Sizeof(Cell(0))) * 8
cellBits = CellBits
case 32, 64:
default:
return nil, 0, errors.Errorf("loading of %d bits images is not supported", cellBits)
Expand Down Expand Up @@ -132,7 +131,7 @@ func Save(fileName string, mem []Cell, cellBits int) error {
}
}()
if cellBits == 0 {
cellBits = int(unsafe.Sizeof(Cell(0))) * 8
cellBits = CellBits
}
switch cellBits {
case 32:
Expand Down
8 changes: 8 additions & 0 deletions vm/vm.go
Expand Up @@ -29,6 +29,14 @@ const (
addressSize = 1024
)

// Bits per Cell
const (
// Compute the size of a Cell
_m = ^uCell(0)
_log = _m>>8&1 + _m>>16&1 + (_m>>31)>>1&1 // >>31>>1 is to trick go vet
CellBits = (1 << _log) << 3
)

// Instance represents an Ngaro VM instance.
type Instance struct {
PC int // Program Counter (aka. Instruction Pointer)
Expand Down
1 change: 1 addition & 0 deletions vm/vm_int.go
Expand Up @@ -20,3 +20,4 @@ package vm

// Cell is the basic type stored in a VM memory location.
type Cell int
type uCell uint
1 change: 1 addition & 0 deletions vm/vm_int32.go
Expand Up @@ -20,3 +20,4 @@ package vm

// Cell is the basic type stored in a VM memory location.
type Cell int32
type uCell uint32
1 change: 1 addition & 0 deletions vm/vm_int64.go
Expand Up @@ -20,3 +20,4 @@ package vm

// Cell is the basic type stored in a VM memory location.
type Cell int64
type uCell uint64

0 comments on commit 01f2557

Please sign in to comment.