Swapped i and w in type

This commit is contained in:
Louis Burke 2023-12-19 00:48:15 -05:00
parent 13f9330eea
commit d234d5180d

View file

@ -10,8 +10,8 @@ type
ArcticType* {.union.} = object
b: int8
s: int16
i: int32
w: int64 # TODO: swap i and w to match instruction naming -.-
w: int32
i: int64
u: uint64
d: float64
f: ArcticSymbol # also used for pointers
@ -357,7 +357,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
# Memory Ops -----------------------------------------------------------
of '1': # LDAB
let p = state.stack.pop.f
state.stack.add ArcticType(w: state[p].int64)
state.stack.add ArcticType(i: state[p].int64)
of '2': # LDAS
let
@ -365,9 +365,9 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
a = state[p].int64
b = state[p+1].int64
if state.pc.section == "CODE": # bigendian
state.stack.add ArcticType(w: a or b shl 8)
state.stack.add ArcticType(i: a or b shl 8)
else:
state.stack.add ArcticType(w: b or a shl 8)
state.stack.add ArcticType(i: b or a shl 8)
of '3': # LDAI
let
@ -377,9 +377,9 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
c = state[p+2].int64
d = state[p+3].int64
if state.pc.section == "CODE": # bigendian
state.stack.add ArcticType(w: d or c shl 8 or b shl 16 or a shl 24)
state.stack.add ArcticType(i: d or c shl 8 or b shl 16 or a shl 24)
else:
state.stack.add ArcticType(w: a or b shl 8 or c shl 16 or d shl 24)
state.stack.add ArcticType(i: a or b shl 8 or c shl 16 or d shl 24)
of '4': # LDAW
let
@ -393,14 +393,14 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
g = state[p+6].int64
h = state[p+7].int64
if state.pc.section == "CODE": # bigendian
state.stack.add ArcticType(w: h or g shl 8 or f shl 16 or e shl 24 or d shl 32 or c shl 40 or b shl 48 or a shl 56)
state.stack.add ArcticType(i: h or g shl 8 or f shl 16 or e shl 24 or d shl 32 or c shl 40 or b shl 48 or a shl 56)
else:
state.stack.add ArcticType(w: a or b shl 8 or c shl 16 or d shl 24 or e shl 32 or f shl 40 or g shl 48 or h shl 56)
state.stack.add ArcticType(i: a or b shl 8 or c shl 16 or d shl 24 or e shl 32 or f shl 40 or g shl 48 or h shl 56)
of 'o': # LDOB
assert op.immediate.kind == INTEGER
let p = state.stack.pop.f
state.stack.add ArcticType(w: state[p+op.immediate.i].int64)
state.stack.add ArcticType(i: state[p+op.immediate.i].int64)
of 'H': # LDOS
assert op.immediate.kind == INTEGER
@ -409,9 +409,9 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
a = state[p].int64
b = state[p+1].int64
if state.pc.section == "CODE": # bigendian
state.stack.add ArcticType(w: a or b shl 8)
state.stack.add ArcticType(i: a or b shl 8)
else:
state.stack.add ArcticType(w: b or a shl 8)
state.stack.add ArcticType(i: b or a shl 8)
of 'G': # LDOI
let
@ -421,9 +421,9 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
c = state[p+2].int64
d = state[p+3].int64
if state.pc.section == "CODE": # bigendian
state.stack.add ArcticType(w: d or c shl 8 or b shl 16 or a shl 24)
state.stack.add ArcticType(i: d or c shl 8 or b shl 16 or a shl 24)
else:
state.stack.add ArcticType(w: a or b shl 8 or c shl 16 or d shl 24)
state.stack.add ArcticType(i: a or b shl 8 or c shl 16 or d shl 24)
of 'Q': # LDOW
let
@ -437,9 +437,9 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
g = state[p+6].int64
h = state[p+7].int64
if state.pc.section == "CODE": # bigendian
state.stack.add ArcticType(w: h or g shl 8 or f shl 16 or e shl 24 or d shl 32 or c shl 40 or b shl 48 or a shl 56)
state.stack.add ArcticType(i: h or g shl 8 or f shl 16 or e shl 24 or d shl 32 or c shl 40 or b shl 48 or a shl 56)
else:
state.stack.add ArcticType(w: a or b shl 8 or c shl 16 or d shl 24 or e shl 32 or f shl 40 or g shl 48 or h shl 56)
state.stack.add ArcticType(i: a or b shl 8 or c shl 16 or d shl 24 or e shl 32 or f shl 40 or g shl 48 or h shl 56)
of '5': # STAB
let
@ -475,7 +475,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
of '8': # STAW
let
x = cast[uint64](state.stack.pop.w)
x = cast[uint64](state.stack.pop.i)
p = state.stack.pop.f
if state.pc.section == "CODE": # bigendian
state[p] = uint8(x and 0xFF)
@ -498,7 +498,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
of 'D': # MCLR
let
x = state.stack.pop.w
x = state.stack.pop.i
p = state.stack.pop.f
for i in 0 .. x:
state[p+i] = 0
@ -506,7 +506,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
of 'd': # MSET
assert op.immediate.kind == INTEGER
let
x = state.stack.pop.w
x = state.stack.pop.i
p = state.stack.pop.f
b = op.immediate.i.uint8
for i in 0 .. x:
@ -514,7 +514,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
of 'K': # MCPY
let
x = state.stack.pop.w
x = state.stack.pop.i
q = state.stack.pop.f
p = state.stack.pop.f
for i in 0 .. x:
@ -522,7 +522,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
of 'J': # MOFF
let
x = state.stack.pop.w
x = state.stack.pop.i
p = state.stack.pop.f
state.stack.add ArcticType(f: (section: p.section, index: p.index + 1))
@ -533,11 +533,11 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
n = op.immediate.i
if n == 0:
if top.w != 0:
if top.i != 0:
state.sections.excl top.f.section
state.stack.add ArcticType(w: 0)
state.stack.add ArcticType(i: 0)
elif n > 0:
if top.w == 0:
if top.i == 0:
state.sections.incl("allocated " & $next_malloc_id, ArcticSection(iscode: false, data: @[]))
inc next_malloc_id
else:
@ -552,11 +552,11 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
x = state.stack.pop.i
if x == 0:
if top.w != 0:
if top.i != 0:
state.sections.excl top.f.section
state.stack.add ArcticType(w: 0)
state.stack.add ArcticType(i: 0)
elif x > 0:
if top.w == 0:
if top.i == 0:
state.sections.incl("allocated " & $next_malloc_id, ArcticSection(iscode: false, data: @[]))
inc next_malloc_id
else:
@ -570,8 +570,8 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
# implementation would probably require an "atomic" memory
# section
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
p = state.stack.pop.f
a = state[p].int64
b = state[p+1].int64
@ -609,59 +609,59 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
state[p+6] = uint8((y shr 8) and 0xFF)
state[p+7] = uint8(y and 0xFF)
else:
state.stack.add ArcticType(w: 0)
state.stack.add ArcticType(i: 0)
# Math Ops -----------------------------------------------------------
of '+': # IADD
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x + y)
state.stack.add ArcticType(i: x + y)
of '-': # ISUB
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x - y)
state.stack.add ArcticType(i: x - y)
of '*': # IMUL
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x * y)
state.stack.add ArcticType(i: x * y)
of '/': # IDIV
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: int(x / y))
state.stack.add ArcticType(i: int(x / y))
of '%': # IMOD
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x.floorMod(y))
state.stack.add ArcticType(i: x.floorMod(y))
of '\\': # IREM
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x mod y)
state.stack.add ArcticType(i: x mod y)
of '_': # INEG
let x = state.stack.pop.w
state.stack.add ArcticType(w: -x)
let x = state.stack.pop.i
state.stack.add ArcticType(i: -x)
of '|': # IABS
let x = state.stack.pop.w
state.stack.add ArcticType(w: x.abs)
let x = state.stack.pop.i
state.stack.add ArcticType(i: x.abs)
of 's': # FADD
let
@ -719,53 +719,53 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
of 'V': # BIOR
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x or y)
state.stack.add ArcticType(i: x or y)
of '&': # BAND
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x and y)
state.stack.add ArcticType(i: x and y)
of '^': # BXOR
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x xor y)
state.stack.add ArcticType(i: x xor y)
of 'l': # BITC
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
state.stack.add ArcticType(w: x and (not y))
state.stack.add ArcticType(i: x and (not y))
of '!': # BNOT
let x = state.stack.pop.w
let x = state.stack.pop.i
state.stack.add ArcticType(w: not x)
state.stack.add ArcticType(i: not x)
of 'u': # USHR
let x = state.stack.pop.w
let x = state.stack.pop.i
var i: int64
case op.immediate.kind:
of INTEGER:
i = op.immediate.i
of VARIABLE:
let v = op.immediate.v
i = state.registers[v].w
i = state.registers[v].i
else:
assert false
if i > 0:
state.stack.add ArcticType(w: x shr i)
state.stack.add ArcticType(i: x shr i)
else:
state.stack.add ArcticType(w: x shl (-i))
state.stack.add ArcticType(i: x shl (-i))
of '[': # ROTR
let x = state.stack.pop.u
@ -775,7 +775,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
i = op.immediate.i
of VARIABLE:
let v = op.immediate.v
i = state.registers[v].w
i = state.registers[v].i
else:
assert false
@ -792,7 +792,7 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
i = op.immediate.i
of VARIABLE:
let v = op.immediate.v
i = state.registers[v].w
i = state.registers[v].i
else:
assert false
@ -803,14 +803,14 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
of '?': # CMPI
let
y = state.stack.pop.w
x = state.stack.pop.w
y = state.stack.pop.i
x = state.stack.pop.i
if x > y:
state.stack.add ArcticType(w: 1)
state.stack.add ArcticType(i: 1)
elif y > x:
state.stack.add ArcticType(w: -1)
state.stack.add ArcticType(i: -1)
else:
state.stack.add ArcticType(w: 0)
state.stack.add ArcticType(i: 0)
of '~': # CMPF
assert op.immediate.kind == INTEGER
@ -829,11 +829,11 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
y = state.stack.pop.u
x = state.stack.pop.u
if x > y:
state.stack.add ArcticType(w: 1)
state.stack.add ArcticType(i: 1)
elif y > x:
state.stack.add ArcticType(w: -1)
state.stack.add ArcticType(i: -1)
else:
state.stack.add ArcticType(w: 0)
state.stack.add ArcticType(i: 0)
of 'F': # BMIS
discard # TODO: define BMIS
@ -841,22 +841,22 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
# Type Conversion Ops --------------------------------------------------
of 'E': # FTOI
let a = state.stack.pop.d
state.stack.add ArcticType(w: a.int64)
state.stack.add ArcticType(i: a.int64)
of 'O': # ITOC
let x = state.stack.pop.b
state.stack.add ArcticType(w: x.int64)
state.stack.add ArcticType(i: x.int64)
of 'S': # ITOS
let x = state.stack.pop.s
state.stack.add ArcticType(w: x.int64)
state.stack.add ArcticType(i: x.int64)
of 'W': # ITOW
let x = state.stack.pop.i
state.stack.add ArcticType(w: x.int64)
state.stack.add ArcticType(i: x.int64)
of '.': # ITOF
let x = state.stack.pop.w
let x = state.stack.pop.i
state.stack.add ArcticType(d: x.float64)
# Stack Ops ------------------------------------------------------------
@ -910,11 +910,11 @@ proc step*(state: var ArcticState, builtins: CritBitTree[ArcticBuiltin]): Arctic
state.stack.insert(x, idx)
of '(': # DPTH
state.stack.add ArcticType(w: state.stack.len)
state.stack.add ArcticType(i: state.stack.len)
of ')': # PACK
let
x = state.stack.pop.w
x = state.stack.pop.i
p = state.stack.pop.f
# TODO: Can't implement this until we have unified addresses
# Once we have the addresses, can pack x items of stack at p+...