Skip to content

Commit

Permalink
isolate opcodes source
Browse files Browse the repository at this point in the history
  • Loading branch information
larme committed Aug 3, 2019
1 parent bf8a717 commit 72e076e
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 227 deletions.
30 changes: 12 additions & 18 deletions muri.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
local class = require('middleclass')
local ops = require('ops')
local utils = require('utils')

local MAX_NAMES = 1024
local OP2CODE = {
['..'] = 0, li = 1,
du = 2, dr = 3,
sw = 4, pu = 5,
po = 6, ju = 7,
ca = 8, cc = 9,
re = 10, eq = 11,
ne = 12, lt = 13,
gt = 14, fe = 15,
st = 16, ad = 17,
su = 18, mu = 19,
di = 20, an = 21,
['or'] = 22, xo = 23,
sh = 24, zr = 25,
en = 26, ie = 27,
iq = 28, ii = 29,
}
local OP2CODE = {}

-- full op name

for op, code in pairs(ops.op2code) do
OP2CODE[op] = code
end

for shortname, op in pairs(ops.shortname2op) do
OP2CODE[shortname] = ops.op2code[op]
end

local Muri = class('Muri')

Expand Down
208 changes: 4 additions & 204 deletions nga.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local class = require('middleclass')
local utils = require('utils')
local OPMixin = require('op_mixin')
local ops = require('ops')

-- default values

Expand Down Expand Up @@ -207,210 +208,9 @@ function NgaVM:exec_packed_opcodes(raw_code)

end

-- ops

NgaVM.static.OPS = {
'nop',
'lit', 'dup', 'drop', 'swap', 'push', 'pop',
'jump', 'call', 'ccall', 'return_', 'eq', 'neq',
'lt', 'gt', 'fetch', 'store', 'add', 'sub', 'mul', 'divmod',
'and_', 'or_', 'xor', 'shift', 'zret', 'end_',
'io_enum', 'io_query', 'io_interact',
}

local _insts = {}
NgaVM.static.OP2INST = _insts

function _insts.nop(vm)
return
end

function _insts.lit(vm)
vm.state.sp = vm.state.sp + 1
vm.state.ip = vm.state.ip + 1
vm.alias.tos = vm.state.memory[vm.state.ip]
end

function _insts.dup(vm)
vm.state.sp = vm.state.sp + 1
vm.alias.tos = vm.alias.nos
return
end

function _insts.drop(vm)
vm.state.data[vm.state.sp] = 0
vm.state.sp = vm.state.sp - 1
if vm.state.sp < vm.conf.addr_start then
vm.state.ip = vm.conf.image_size
end
end

function _insts.swap(vm)
vm.alias.tos, vm.alias.nos = vm.alias.nos, vm.alias.tos
end

function _insts.push(vm)
vm.state.rp = vm.state.rp + 1
vm.alias.tors = vm.alias.tos
_insts.drop(vm)
end

function _insts.pop(vm)
vm.state.sp = vm.state.sp + 1
vm.alias.tos = vm.alias.tors
vm.state.rp = vm.state.rp - 1
end

function _insts.jump(vm)
vm.state.ip = vm.alias.tos - 1
_insts.drop(vm)
end

function _insts.call(vm)
vm.state.rp = vm.state.rp + 1
vm.alias.tors = vm.state.ip
vm.state.ip = vm.alias.tos - 1
_insts.drop(vm)
end

function _insts.ccall(vm)
local addr, flag
addr = vm.alias.tos
_insts.drop(vm)
flag = vm.alias.tos
_insts.drop(vm)

if flag ~= 0 then
vm.state.rp = vm.state.rp + 1
vm.alias.tors = vm.state.ip
vm.state.ip = addr - 1
end
end

function _insts.return_(vm)
vm.state.ip = vm.alias.tors
vm.state.rp = vm.state.rp - 1
end

function _insts.eq(vm)
vm.alias.nos = (vm.alias.nos == vm.alias.tos) and -1 or 0
_insts.drop(vm)
end

function _insts.neq(vm)
vm.alias.nos = (vm.alias.nos ~= vm.alias.tos) and -1 or 0
_insts.drop(vm)
end

function _insts.lt(vm)
vm.alias.nos = (vm.alias.nos < vm.alias.tos) and -1 or 0
_insts.drop(vm)
end

function _insts.gt(vm)
vm.alias.nos = (vm.alias.nos > vm.alias.tos) and -1 or 0
_insts.drop(vm)
end

function _insts.fetch(vm)
if vm.alias.tos >= vm.conf.addr_start then
vm.alias.tos = vm.state.memory[vm.alias.tos]
elseif vm.alias.tos == -1 then
vm.alias.tos = vm.state.sp - vm.conf.addr_start - 1
elseif vm.alias.tos == -2 then
vm.alias.tos = vm.state.rp
elseif vm.alias.tos == -3 then
vm.alias.tos = vm.conf.image_size
end
end

function _insts.store(vm)
vm.state.memory[vm.alias.tos] = vm.alias.nos
_insts.drop(vm)
_insts.drop(vm)
end

function _insts.add(vm)
vm.alias.nos = vm.alias.nos + vm.alias.tos
_insts.drop(vm)
end

function _insts.sub(vm)
vm.alias.nos = vm.alias.nos - vm.alias.tos
_insts.drop(vm)
end

function _insts.mul(vm)
vm.alias.nos = vm.alias.nos * vm.alias.tos
_insts.drop(vm)
end

function _insts.divmod(vm)
local a, b = vm.alias.tos, vm.alias.nos
vm.alias.tos = b // a
vm.alias.nos = b % a
end

function _insts.and_(vm)
vm.alias.nos = vm.alias.tos & vm.alias.nos
_insts.drop(vm)
end

function _insts.or_(vm)
vm.alias.nos = vm.alias.tos | vm.alias.nos
_insts.drop(vm)
end

function _insts.xor(vm)
vm.alias.nos = vm.alias.tos ~ vm.alias.nos
_insts.drop(vm)
end

function _insts.shift(vm)
local x, y
y = vm.alias.tos
x = vm.alias.nos

if y < 0 then
vm.alias.nos = x << (-1 * y)
elseif y > 0 then
if x < 0 then
vm.alias.nos = x >> y | ~(~0 >> y)
else
vm.alias.nos = x >> y
end
end
_insts.drop(vm)
end

function _insts.zret(vm)
if vm.alias.tos == 0 then
_insts.drop(vm)
vm.state.ip = vm.alias.tors
vm.state.rp = vm.state.rp - 1
end
end

function _insts.end_(vm)
vm.state.ip = vm.conf.image_size
end

function _insts.io_enum(vm)
vm.state.sp = vm.state.sp + 1
vm.alias.tos = vm.num_devices
end

function _insts.io_query(vm)
local device = vm.alias.tos
_insts.drop(vm)
vm.io.query_handlers[device](vm)
end

function _insts.io_interact(vm)
local device = vm.alias.tos
_insts.drop(vm)
vm.io.device_handlers[device](vm)
end
NgaVM.static.OPS = ops.ops
NgaVM.static.OP2CODE = ops.op2code
NgaVM.static.OP2INST = ops.insts

NgaVM:include(OPMixin)

Expand Down
7 changes: 2 additions & 5 deletions op_mixin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
local OPMixin = {}

function OPMixin:propagate_op_info()
local op2code = {}
local code2op = {}
local code2inst = {}
local op2inst = self._ops.op2inst

self._ops.op2code = op2code
self._ops.code2op = code2op
self._ops.code2inst = code2inst
self._ops.num_ops = 0

for _code, op in pairs(self._ops.ops) do
for op, code in pairs(self._ops.op2code) do
self._ops.num_ops = self._ops.num_ops + 1
code = _code - 1
op2code[op] = code
code2op[code] = op
code2inst[code] = op2inst[op]
end
Expand Down Expand Up @@ -53,6 +49,7 @@ function OPMixin:included(cls)
self._ops = {}
self._ops.ops = arg.ops or self.class.OPS
self._ops.op2inst = arg.op2inst or self.class.OP2INST
self._ops.op2code = arg.op2code or self.class.OP2CODE
self:propagate_op_info()
end

Expand Down
Loading

0 comments on commit 72e076e

Please sign in to comment.