2022-04-04 12:29:23 +02:00
|
|
|
# Copyright 2022 Mattia Giambirtone & All Contributors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2022-04-28 18:06:53 +02:00
|
|
|
## The Peon runtime environment
|
|
|
|
import types
|
|
|
|
import ../config
|
|
|
|
import ../frontend/meta/bytecode
|
2022-04-04 12:29:23 +02:00
|
|
|
|
|
|
|
|
2022-05-18 13:32:32 +02:00
|
|
|
type
|
2022-04-28 18:06:53 +02:00
|
|
|
PeonVM* = ref object
|
|
|
|
## The Peon Virtual Machine
|
|
|
|
stack: seq[PeonObject]
|
2022-05-18 13:32:32 +02:00
|
|
|
ip: int # Instruction pointer
|
|
|
|
sp: int # Stack pointer
|
|
|
|
cache: array[6, PeonObject] # Singletons cache
|
|
|
|
chunk: Chunk # Piece of bytecode to execute
|
2022-05-02 17:26:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
proc initCache*(self: PeonVM) =
|
|
|
|
## Initializes the VM's
|
|
|
|
## singletons cache
|
2022-05-02 23:19:17 +02:00
|
|
|
self.cache[0] = PeonObject(kind: Nil)
|
|
|
|
self.cache[1] = PeonObject(kind: Bool, boolean: true)
|
|
|
|
self.cache[2] = PeonObject(kind: Bool, boolean: false)
|
|
|
|
self.cache[3] = PeonObject(kind: ObjectKind.Inf, positive: true)
|
|
|
|
self.cache[4] = PeonObject(kind: ObjectKind.Inf, positive: false)
|
|
|
|
self.cache[5] = PeonObject(kind: ObjectKind.Nan)
|
2022-04-28 18:06:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
proc newPeonVM*: PeonVM =
|
|
|
|
## Initializes a new, blank VM
|
|
|
|
## for executing Peon bytecode
|
|
|
|
new(result)
|
|
|
|
result.ip = 0
|
|
|
|
result.sp = 0
|
|
|
|
result.stack = newSeqOfCap[PeonObject](INITIAL_STACK_SIZE)
|
2022-05-02 17:26:38 +02:00
|
|
|
result.initCache()
|
2022-04-28 18:06:53 +02:00
|
|
|
for _ in 0..<INITIAL_STACK_SIZE:
|
|
|
|
result.stack.add(result.cache[0])
|
|
|
|
|
2022-05-02 23:19:17 +02:00
|
|
|
## Getters for singleton types (they are cached!)
|
2022-04-28 18:06:53 +02:00
|
|
|
|
2022-05-02 23:19:17 +02:00
|
|
|
proc getNil*(self: PeonVM): PeonObject = self.cache[0]
|
2022-04-28 18:06:53 +02:00
|
|
|
|
2022-05-02 23:19:17 +02:00
|
|
|
proc getBool*(self: PeonVM, value: bool): PeonObject =
|
2022-04-28 18:06:53 +02:00
|
|
|
if value:
|
2022-05-02 23:19:17 +02:00
|
|
|
return self.cache[1]
|
|
|
|
return self.cache[2]
|
2022-04-28 18:06:53 +02:00
|
|
|
|
2022-05-02 23:19:17 +02:00
|
|
|
proc getInf*(self: PeonVM, positive: bool): PeonObject =
|
2022-04-28 18:06:53 +02:00
|
|
|
if positive:
|
2022-05-02 23:19:17 +02:00
|
|
|
return self.cache[3]
|
|
|
|
return self.cache[4]
|
2022-04-28 18:06:53 +02:00
|
|
|
|
2022-05-02 23:19:17 +02:00
|
|
|
proc getNan*(self: PeonVM): PeonObject = self.cache[5]
|
2022-04-28 18:06:53 +02:00
|
|
|
|
2022-05-02 23:19:17 +02:00
|
|
|
## Stack primitives
|
2022-04-28 18:06:53 +02:00
|
|
|
|
|
|
|
proc push(self: PeonVM, obj: PeonObject) =
|
2022-04-29 17:15:57 +02:00
|
|
|
## Pushes a Peon object onto the
|
|
|
|
## stack
|
2022-04-28 18:06:53 +02:00
|
|
|
if self.sp >= self.stack.high():
|
|
|
|
for _ in 0..self.stack.len():
|
2022-05-02 23:19:17 +02:00
|
|
|
self.stack.add(self.getNil())
|
2022-04-28 18:06:53 +02:00
|
|
|
self.stack[self.sp] = obj
|
|
|
|
inc(self.sp)
|
|
|
|
|
|
|
|
|
|
|
|
proc pop(self: PeonVM): PeonObject =
|
2022-04-29 17:15:57 +02:00
|
|
|
## Pops a Peon object off the
|
|
|
|
## stack, decreasing the stack
|
|
|
|
## pointer. The object is returned
|
2022-04-28 18:06:53 +02:00
|
|
|
dec(self.sp)
|
|
|
|
return self.stack[self.sp]
|
|
|
|
|
|
|
|
|
2022-05-07 10:48:01 +02:00
|
|
|
proc peek(self: PeonVM): PeonObject =
|
|
|
|
## Returns the element at the top
|
|
|
|
## of the stack without consuming
|
|
|
|
## it
|
|
|
|
return self.stack[self.sp]
|
|
|
|
|
|
|
|
|
2022-05-02 17:26:38 +02:00
|
|
|
proc readByte(self: PeonVM): uint8 =
|
|
|
|
## Reads a single byte from the
|
|
|
|
## bytecode and returns it as an
|
|
|
|
## unsigned 8 bit integer
|
2022-04-28 18:06:53 +02:00
|
|
|
inc(self.ip)
|
2022-05-02 17:26:38 +02:00
|
|
|
return self.chunk.code[self.ip - 1]
|
|
|
|
|
|
|
|
|
|
|
|
proc readShort(self: PeonVM): uint16 =
|
|
|
|
## Reads two bytes from the
|
|
|
|
## bytecode and returns them
|
|
|
|
## as an unsigned 16 bit
|
|
|
|
## integer
|
2022-05-07 10:48:01 +02:00
|
|
|
var arr: array[2, uint8] = [self.readByte(), self.readByte()]
|
2022-05-02 17:26:38 +02:00
|
|
|
copyMem(result.addr, unsafeAddr(arr), sizeof(arr))
|
|
|
|
|
|
|
|
|
2022-05-02 23:19:17 +02:00
|
|
|
proc readLong(self: PeonVM): uint32 =
|
2022-05-02 17:26:38 +02:00
|
|
|
## Reads three bytes from the
|
|
|
|
## bytecode and returns them
|
|
|
|
## as an unsigned 32 bit
|
|
|
|
## integer. Note however that
|
|
|
|
## the boundary is capped at
|
|
|
|
## 24 bits instead of 32
|
2022-05-07 10:48:01 +02:00
|
|
|
var arr: array[3, uint8] = [self.readByte(), self.readByte(), self.readByte()]
|
2022-05-02 17:26:38 +02:00
|
|
|
copyMem(result.addr, unsafeAddr(arr), sizeof(arr))
|
|
|
|
|
|
|
|
|
2022-05-07 10:48:01 +02:00
|
|
|
proc readInt64(self: PeonVM, idx: int): PeonObject =
|
|
|
|
## Reads a constant from the
|
|
|
|
## chunk's constant table and
|
|
|
|
## returns a Peon object. Assumes
|
|
|
|
## the constant is an Int64
|
2022-05-18 13:32:32 +02:00
|
|
|
var arr = [self.chunk.byteConsts[idx], self.chunk.byteConsts[idx + 1],
|
|
|
|
self.chunk.byteConsts[idx + 2], self.chunk.byteConsts[idx + 3]]
|
2022-05-07 10:48:01 +02:00
|
|
|
result = PeonObject(kind: Int64)
|
|
|
|
copyMem(result.long.addr, arr.addr, sizeof(arr))
|
2022-05-02 23:19:17 +02:00
|
|
|
|
|
|
|
|
2022-05-07 10:48:01 +02:00
|
|
|
proc readUInt64(self: PeonVM, idx: int): PeonObject =
|
2022-05-02 17:26:38 +02:00
|
|
|
## Reads a constant from the
|
2022-05-02 23:19:17 +02:00
|
|
|
## chunk's constant table and
|
|
|
|
## returns a Peon object. Assumes
|
2022-05-07 10:48:01 +02:00
|
|
|
## the constant is an UInt64
|
2022-05-18 13:32:32 +02:00
|
|
|
var arr = [self.chunk.byteConsts[idx], self.chunk.byteConsts[idx + 1],
|
|
|
|
self.chunk.byteConsts[idx + 2], self.chunk.byteConsts[idx + 3]]
|
2022-05-07 10:48:01 +02:00
|
|
|
result = PeonObject(kind: UInt64)
|
|
|
|
copyMem(result.uLong.addr, arr.addr, sizeof(arr))
|
2022-04-28 18:06:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
proc dispatch*(self: PeonVM) =
|
|
|
|
## Main bytecode dispatch loop
|
|
|
|
var instruction: OpCode
|
|
|
|
while true:
|
2022-05-02 17:26:38 +02:00
|
|
|
instruction = OpCode(self.readByte())
|
2022-04-28 18:06:53 +02:00
|
|
|
case instruction:
|
2022-05-07 10:48:01 +02:00
|
|
|
of LoadTrue:
|
2022-04-28 18:06:53 +02:00
|
|
|
self.push(self.getBool(true))
|
2022-05-07 10:48:01 +02:00
|
|
|
of LoadFalse:
|
2022-04-28 18:06:53 +02:00
|
|
|
self.push(self.getBool(false))
|
2022-05-07 10:48:01 +02:00
|
|
|
of LoadNan:
|
2022-04-28 18:06:53 +02:00
|
|
|
self.push(self.getNan())
|
2022-05-07 10:48:01 +02:00
|
|
|
of LoadNil:
|
2022-04-28 18:06:53 +02:00
|
|
|
self.push(self.getNil())
|
2022-05-07 10:48:01 +02:00
|
|
|
of LoadInf:
|
2022-04-28 18:06:53 +02:00
|
|
|
self.push(self.getInf(true))
|
2022-05-07 10:48:01 +02:00
|
|
|
of LoadInt64:
|
|
|
|
self.push(self.readInt64(int(self.readLong())))
|
|
|
|
of LoadUInt64:
|
|
|
|
self.push(self.readUInt64(int(self.readLong())))
|
2022-04-28 18:06:53 +02:00
|
|
|
of OpCode.Return:
|
2022-04-29 23:04:53 +02:00
|
|
|
# TODO
|
2022-04-28 18:06:53 +02:00
|
|
|
return
|
2022-05-07 10:48:01 +02:00
|
|
|
of NoOp:
|
2022-04-29 23:04:53 +02:00
|
|
|
continue
|
2022-05-07 10:48:01 +02:00
|
|
|
of Pop:
|
2022-05-02 17:26:38 +02:00
|
|
|
discard self.pop()
|
2022-05-07 10:48:01 +02:00
|
|
|
of Jump:
|
|
|
|
self.ip = int(self.readShort())
|
|
|
|
of JumpForwards:
|
2022-05-02 17:26:38 +02:00
|
|
|
self.ip += int(self.readShort())
|
2022-05-07 10:48:01 +02:00
|
|
|
of JumpBackwards:
|
|
|
|
self.ip -= int(self.readShort())
|
|
|
|
of JumpIfFalse:
|
|
|
|
if not self.peek().boolean:
|
|
|
|
self.ip += int(self.readShort())
|
|
|
|
of JumpIfTrue:
|
|
|
|
if self.peek().boolean:
|
|
|
|
self.ip += int(self.readShort())
|
|
|
|
of JumpIfFalsePop:
|
|
|
|
if not self.peek().boolean:
|
|
|
|
self.ip += int(self.readShort())
|
|
|
|
discard self.pop()
|
|
|
|
of JumpIfFalseOrPop:
|
|
|
|
if not self.peek().boolean:
|
|
|
|
self.ip += int(self.readShort())
|
|
|
|
else:
|
|
|
|
discard self.pop()
|
|
|
|
of LongJumpIfFalse:
|
|
|
|
if not self.peek().boolean:
|
|
|
|
self.ip += int(self.readLong())
|
|
|
|
of LongJumpIfFalsePop:
|
|
|
|
if not self.peek().boolean:
|
|
|
|
self.ip += int(self.readLong())
|
|
|
|
discard self.pop()
|
|
|
|
of LongJumpForwards:
|
|
|
|
self.ip += int(self.readLong())
|
|
|
|
of LongJumpBackwards:
|
|
|
|
self.ip -= int(self.readLong())
|
|
|
|
of LongJump:
|
|
|
|
self.ip = int(self.readLong())
|
|
|
|
of LongJumpIfFalseOrPop:
|
|
|
|
if not self.peek().boolean:
|
|
|
|
self.ip += int(self.readLong())
|
|
|
|
else:
|
|
|
|
discard self.pop()
|
2022-04-28 18:06:53 +02:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
|
|
|
|
proc run*(self: PeonVM, chunk: Chunk) =
|
|
|
|
## Executes a piece of Peon bytecode.
|
|
|
|
self.chunk = chunk
|
|
|
|
self.sp = 0
|
|
|
|
self.ip = 0
|
|
|
|
self.dispatch()
|