creole/asm/creole.py

449 lines
14 KiB
Python
Raw Normal View History

2023-02-12 15:54:21 -05:00
# Copyright (c) 2023 Peter McGoron <code@mcgoron.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2023-02-07 11:38:04 -05:00
from enum import Enum
2023-02-07 12:25:52 -05:00
class MalformedArgument(Exception):
pass
2023-02-11 12:04:17 -05:00
def word_2c(w):
2023-02-12 14:13:37 -05:00
""" Negate a non-negative integer using 32 bit two's compliment.
:param w: An integer in two's compliment. A non-negative Python
integer will work.
:return: The negation of the integer stored as a two's compliment
integer.
"""
2023-02-11 12:04:17 -05:00
return (~w + 1) & 0xFFFFFFFF
def ti(w):
2023-02-12 14:13:37 -05:00
""" Explicitly transform integer into two's compliment representation.
:param w: A python integer.
:return: The integer in two's compliment.
"""
2023-02-11 12:04:17 -05:00
return w if w >= 0 else word_wc(-w)
2023-02-11 15:49:39 -05:00
def from_2c(w):
2023-02-12 14:13:37 -05:00
""" Turn two's compliment word into Python integer.
:param w: An integer in 32 bit twos compliment.
:return: The integer as a proper Python string.
"""
2023-02-11 15:49:39 -05:00
if (w >> 31) & 1 == 0:
return w
return -word_2c(w)
2023-02-11 12:04:17 -05:00
class Argument:
2023-02-12 14:13:37 -05:00
""" Class of arguments. Not used directly: It is used to store
intermediate information during the assembly process. """
def __init__(self, argtype, val, sign=False):
2023-02-12 14:13:37 -05:00
""" Initialize an argument.
:param argtype: Type of the argument (instance of ArgType).
:param val: Python integer value of the argument.
:param sign: If the argument should be treated as signed.
Otherwise, the integer will be interpreted in execution
as an unsigned integer.
"""
self.at = argtype
self.sign = sign
self.val = val
def __str__(self):
return f'({self.at}, {self.sign}, {self.val})'
def high_bits(self):
2023-02-12 14:13:37 -05:00
""" Returns the high bits that the argument would have
in the opcode. """
return int(self.sign) << 1 | (self.at == ArgType.REG)
2023-02-18 11:05:09 -05:00
def __call__(self):
l = 2 if self.val < 0x80 else None
return encode_pseudo_utf8(self.val, self.high_bits(), l)
class StringArgument(Argument):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2023-02-18 11:05:09 -05:00
def __bytes__(self):
b = bytes()
for v in self.val:
b = b + Argument(ArgType.IMM, v)()
return b
class LabelArgument(Argument):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __call__(self):
return self.val
class TypecheckException(Exception):
""" Exception thrown when an argument to an instruction are of the
incorrect type. """
def __init__(self, got, argtype, sarg, opcode, i):
self.argtype = argtype
self.sarg = sarg
self.opcode = opcode
self.got = got
self.i = i
self.message = f'''\
opcode {self.opcode.name} has invalid value {self.sarg} ({self.got})
at {self.i} (expected {self.argtype})\
'''
2023-02-07 11:38:04 -05:00
class ArgType(Enum):
2023-02-11 12:04:17 -05:00
""" Class denoting the type of an argument to an instruction. """
2023-02-07 12:25:52 -05:00
IMM = 1
2023-02-11 12:04:17 -05:00
""" Immediate values are ones that must be numbers (positive or negative). """
2023-02-07 12:25:52 -05:00
REG = 2
2023-02-11 12:04:17 -05:00
""" Type of registers. """
2023-02-07 12:25:52 -05:00
VAL = 3
2023-02-11 12:04:17 -05:00
""" Type that denotes either immediate values or registers. """
2023-02-18 11:05:09 -05:00
DAT = 4
""" Type of data label. """
STR = 5
""" Type of a string of 32 bit integers. """
LAB = 6
""" Type of a label (.name). """
def is_number(t):
return t == ArgType.IMM or t == ArgType.REG
2023-02-07 11:38:04 -05:00
def gettype(s):
2023-02-11 12:04:17 -05:00
""" Parses the type of the argument represented as a string
and returns a tuple with the first the first element being
the type and the second element being the integer value of
the argument.
2023-02-12 14:13:37 -05:00
Valid parameters are:
* `r` followed by a nonnegative integer (register)
* `l` followed by a nonnegative integer (label)
* any integer (immediate value)
:param s: String representing the argument.
:return: The Argument object representing the argument.
:raises MalformedArgument:
2023-02-11 12:04:17 -05:00
"""
2023-02-18 11:05:09 -05:00
if type(s) is list:
return StringArgument(ArgType.STR, s)
elif s.isnumeric():
return Argument(ArgType.IMM, int(s))
2023-02-11 12:04:17 -05:00
elif s[0] == "-" and s[1:].isnumeric():
2023-02-12 14:13:37 -05:00
return Argument(ArgType.IMM, word_2c(int(s[1:])), True)
2023-02-07 11:48:22 -05:00
elif s[0] == 'r' and s[1:].isnumeric():
return Argument(ArgType.REG, int(s[1:]))
2023-02-18 11:05:09 -05:00
elif s[0] == 'd' and s[1:].isnumeric():
return Argument(ArgType.DAT, int(s[1:]))
elif s[0] == '.':
return LabelArgument(ArgType.LAB, s[:])
2023-02-07 11:38:04 -05:00
else:
2023-02-07 12:25:52 -05:00
raise MalformedArgument(s)
2023-02-07 11:38:04 -05:00
def typecheck(self, s, opcode, i):
2023-02-11 12:04:17 -05:00
""" Parses the type of the string and returns it if it fits
2023-02-12 14:13:37 -05:00
the type of the enum value.
:param s: String argument representing an argument.
:param opcode: Opcode of the argument. Used for debugging.
:param i: Argument number. Used for debugging.
:return: The Argument class containing the object.
:raises TypecheckException: """
2023-02-07 11:48:22 -05:00
t = ArgType.gettype(s)
2023-02-07 12:25:52 -05:00
if self == ArgType.VAL:
if t.at == ArgType.REG or t.at == ArgType.IMM:
2023-02-08 08:56:39 -05:00
return t
else:
raise TypecheckException(t.at, self, s, opcode, i)
elif t.at == self:
2023-02-08 08:56:39 -05:00
return t
2023-02-07 11:38:04 -05:00
else:
raise TypecheckException(t.at, self, s, opcode, i)
2023-02-07 11:38:04 -05:00
2023-02-07 12:25:52 -05:00
class OpcodeException(Exception):
pass
class TypecheckLenException(Exception):
2023-02-12 14:13:37 -05:00
""" Exception thrown when arguments to an instruction are of the
incorrect length. """
2023-02-08 08:56:39 -05:00
def __init__(self, opcode, insargs, argtypelen):
self.opcode = opcode
self.insargs = insargs
self.argtypelen = argtypelen
def __str__(self):
2023-02-12 14:13:37 -05:00
return f'''\
arguments {self.insargs} to opcode {self.opcode.name} not of length {self.argtypelen}\
2023-02-12 14:13:37 -05:00
'''
2023-02-11 12:04:17 -05:00
2023-02-09 11:32:32 -05:00
class Instruction(Enum):
2023-02-11 12:04:17 -05:00
""" Class of microcode instructions. The first number is the opcode
and the suceeding values are the types of each of the
arguments. The first argument is the opcode and the second
2023-02-12 14:13:37 -05:00
argument is what function is used to compile the instruction
(some instructions are actually versions of other instructions). """
2023-02-12 14:28:48 -05:00
NOP = 0, "_render_default"
PUSH = 1, "_render_default", ArgType.VAL
POP = 2, "_render_default", ArgType.REG
ADD = 3, "_render_default", ArgType.REG, ArgType.VAL, ArgType.VAL
MOV = "ADD", "_render_mov", ArgType.REG, ArgType.VAL
MUL = 4, "_render_default", ArgType.REG, ArgType.VAL, ArgType.VAL
DIV = 5, "_render_default", ArgType.REG, ArgType.VAL, ArgType.VAL
2023-02-12 14:13:37 -05:00
SDIV = "DIV", "_render_change_args", ArgType.REG, ArgType.VAL, ArgType.VAL
2023-02-12 14:28:48 -05:00
SYS = 6, "_render_default", ArgType.VAL
2023-02-18 11:05:09 -05:00
JL = 7, "_render_default", ArgType.LAB, ArgType.VAL, ArgType.VAL
2023-02-12 14:13:37 -05:00
JLS = "JL", "_render_change_args", ArgType.LAB, ArgType.VAL, ArgType.VAL
2023-02-18 11:05:09 -05:00
JLE = 8, "_render_default", ArgType.LAB, ArgType.VAL, ArgType.VAL
2023-02-12 14:13:37 -05:00
JLES = "JLE", "_render_change_args", ArgType.LAB, ArgType.VAL, ArgType.VAL
2023-02-18 11:05:09 -05:00
JE = 9, "_render_default", ArgType.LAB, ArgType.VAL, ArgType.VAL
2023-02-12 14:28:48 -05:00
J = "JE", "_render_j", ArgType.LAB
2023-02-18 11:05:09 -05:00
JNE = 10, "_render_default", ArgType.LAB, ArgType.VAL, ArgType.VAL
DB = 11, "_render_default", ArgType.DAT, ArgType.STR
def __int__(self):
2023-02-12 14:13:37 -05:00
""" Returns the opcode associated with the Instruction.
If it is a virtual instruction, it will resolve the string
name of the opcode and return its opcode. """
if type(self.opcode) is int:
return self.opcode
return int(Instruction[self.opcode])
def __init__(self, opcode, renderfun, *args):
""" Initialize an Instruction. Do not call this function: it is
used to make enum values. To add a new instruction, modify
the Instruction enum.
This function sometimes takes string arguments because
certain values may not be loaded until later.
:param opcode: Opcode of the instruction, or a string
containing the case-sensitive name of the instruction from
which this instruction derives from.
:param renderfun: a string with the name of a function
in the class that returns the instruction opcode.
:param *args: Type of each argument to the instruction.
The amount of arguments denotes the amount of instructions.
"""
if type(opcode) is int and (opcode > 0x7F or opcode < 0):
2023-02-07 12:25:52 -05:00
raise OpcodeException(opcode)
2023-02-07 11:38:04 -05:00
self.opcode = opcode
2023-02-09 11:32:32 -05:00
self.argtypes = args
2023-02-12 14:13:37 -05:00
self.render = getattr(self, renderfun)
2023-02-11 12:04:17 -05:00
2023-02-07 11:38:04 -05:00
def typecheck(self, sargs):
2023-02-11 12:04:17 -05:00
""" Pass arguments to the instruction and check if the
2023-02-12 14:13:37 -05:00
arguments are correct.
:param sargs: List of arguments to the instruction
as strings.
:return: List of arguments (as Argument objects).
:raises TypecheckLenException:
"""
2023-02-07 11:38:04 -05:00
rargs = []
if len(sargs) != len(self.argtypes):
raise TypecheckLenException(self, sargs,
2023-02-08 08:56:39 -05:00
len(self.argtypes))
2023-02-07 11:38:04 -05:00
for i in range(0, len(sargs)):
t = self.argtypes[i].typecheck(sargs[i], self, i)
2023-02-08 08:56:39 -05:00
rargs.append(t)
2023-02-07 11:38:04 -05:00
return rargs
2023-02-12 14:28:48 -05:00
def _render_mov(self, args):
args = [args[0], args[1], Argument(ArgType.IMM, 0)]
return Instruction[self.opcode].render(args)
def _render_j(self, args):
args = [args[0], Argument(ArgType.IMM, 0),
Argument(ArgType.IMM, 0)]
return Instruction[self.opcode].render(args)
def _render_change_args(self, args):
for i in range(0,len(args)):
2023-02-18 11:05:09 -05:00
if ArgType.is_number(args[i].at):
args[i].sign = True
return Instruction[self.opcode].render(args)
2023-02-12 14:28:48 -05:00
def _render_default(self, args):
comps = [bytes([self.opcode])]
for a in args:
comps.append(a())
comps.append(b'\x00')
return comps
2023-02-07 11:38:04 -05:00
encoding_types = {
# start mask B
2023-02-07 11:38:04 -05:00
2: (0x7F, 0xC0, 7),
3: (0xFFF, 0xE0, 12),
4: (0x1FFFF, 0xF0, 17),
5: (0x3FFFFF, 0xF8, 22),
6: (0x7FFFFFF, 0xFC, 27),
7: (0xFFFFFFFF, 0xFE, 32),
2023-02-07 11:38:04 -05:00
# B : Total number of bits excluding high bits
}
def pseudo_utf8_len(n):
for k in sorted(encoding_types):
if n <= encoding_types[k][0]:
return k
return None
2023-02-07 12:25:52 -05:00
class InvalidNumberException(Exception):
pass
class InvalidLengthException(Exception):
pass
2023-02-07 11:38:04 -05:00
def encode_pseudo_utf8(n, high_bits, to):
2023-02-07 12:25:52 -05:00
if n < 0:
raise InvalidNumberException(n)
if to is None or to < 0:
to = pseudo_utf8_len(n)
2023-02-07 12:25:52 -05:00
if to is None:
raise InvalidNumberException(n)
2023-02-07 11:38:04 -05:00
if to > 8 or to < 0:
2023-02-07 12:25:52 -05:00
raise InvalidLengthException(to)
2023-02-07 11:38:04 -05:00
elif to == 1:
if n < 0x80:
return bytes([n])
else:
2023-02-07 12:25:52 -05:00
raise InvalidNumberException(n,to)
2023-02-07 11:38:04 -05:00
(maxval, start_byte, n_tot) = encoding_types[to]
if n > maxval or high_bits > 15:
2023-02-07 12:25:52 -05:00
raise InvalidNumberException(n, high_bits)
2023-02-07 11:38:04 -05:00
n = n | (high_bits << n_tot)
all_bytes = []
for i in range(0, to - 1):
all_bytes.append(0x80 | (n & 0x3F))
n >>= 6
all_bytes.append(start_byte | n)
return bytes(reversed(all_bytes))
2023-02-07 12:25:52 -05:00
class RangeCheckException(Exception):
pass
2023-02-07 11:38:04 -05:00
class Line:
def __init__(self, ins, args):
self.ins = ins
2023-02-07 11:38:04 -05:00
self.args = args
2023-02-11 12:04:17 -05:00
2023-02-11 12:27:29 -05:00
def check_line(self, lablen, reglen):
2023-02-11 12:04:17 -05:00
for a in self.args:
if a.at == ArgType.REG:
if a.val < 0 or a.val >= reglen:
raise RangeCheckException(a.at,
a.val,
2023-02-11 12:27:29 -05:00
reglen)
2023-02-07 11:48:22 -05:00
def __call__(self):
return self.ins.render(self.args)
2023-02-07 11:38:04 -05:00
2023-02-07 12:25:52 -05:00
class InstructionNotFoundException(Exception):
pass
2023-02-07 11:38:04 -05:00
class Program:
def _asm_push_line(self, ins, args):
2023-02-11 12:04:17 -05:00
l = Line(ins, args)
2023-02-11 12:27:29 -05:00
l.check_line(self.lablen, self.reglen)
2023-02-11 12:04:17 -05:00
self.asm.append(l)
2023-02-07 11:38:04 -05:00
def parse_asm_line(self, line):
""" Parse and add a single assembly line to the program.
:param line: String containing the line.
:raises InstructionNotFoundException:
"""
2023-02-18 11:05:09 -05:00
line = line.strip().split()
2023-02-07 12:25:52 -05:00
line[0] = line[0].casefold()
2023-02-18 11:05:09 -05:00
if line[0][0] == '.':
self.asm.append(line[0])
2023-02-18 11:05:09 -05:00
return None
2023-02-09 11:32:32 -05:00
try:
ins = Instruction[line[0].upper()]
2023-02-09 11:32:32 -05:00
except Exception as e:
2023-02-07 12:25:52 -05:00
raise InstructionNotFoundException(line[0])
2023-02-09 11:32:32 -05:00
args_w_type = ins.typecheck(line[1:])
self._asm_push_line(ins, args_w_type)
2023-02-07 11:48:22 -05:00
2023-02-11 16:24:48 -05:00
def parse_lines(self, lines):
""" Parse a list of lines. See parse_asm_line.
:param lines: List of assembly lines.
"""
2023-02-11 16:24:48 -05:00
for l in lines:
self.parse_asm_line(l)
2023-02-07 11:48:22 -05:00
def __call__(self):
""" Generate bytecode. """
# Labels may jump forward in the program, which means
# multiple passes are required to properly calculate
# jump locations.
# This algorithm makes every jump destination the same
# width in each operation, and calculates the smallest
# width that will allow all labels to jump to any location
# in the program.
# The algorithm calculates the length of the program
# with all jump arguments given a length of 0. Each label
# is noted with its offset in the program (with all jump
# arguments given zero length) and the amount of jump arguments
# that occur prior to the label.
# When the code is emitted, the label length is properly
# calculated with the length of each label.
# This method is not optimal, but will work well for small
# programs.
ins = []
curlen = 0
# This dictonary contains a tuple (len, refs)
# that denotes that a label points to len + lablen*refs
# where lablen is a to-be-determined number.
2023-02-18 11:05:09 -05:00
labels = {}
labelrefs = 0
2023-02-07 11:48:22 -05:00
for line in self.asm:
2023-02-18 11:05:09 -05:00
if type(line) is str:
labels[line] = (curlen, labelrefs)
2023-02-18 11:05:09 -05:00
continue
next_ins = line()
for v in next_ins:
if type(v) is str:
labelrefs += 1
else:
curlen += len(v)
ins.append(next_ins)
# Calculate a label length, such that the entire program
# can be contained in this length.
for i in encoding_types:
if curlen + labelrefs*i < encoding_types[i][0]:
lablen = i
break
# Emit bytecode.
b = bytes()
for line in ins:
for arg in line:
if type(arg) is str:
off = labels[arg][0] + labels[arg][1]*lablen
arg = encode_pseudo_utf8(off, 0, lablen)
b = b + arg
assert len(b) < encoding_types[lablen][0]
2023-02-07 11:48:22 -05:00
return b
2023-02-11 12:27:29 -05:00
def __init__(self, lablen=16, reglen=16):
2023-02-07 11:48:22 -05:00
self.asm = []
2023-02-11 12:27:29 -05:00
self.lablen = lablen
self.reglen = reglen
2023-02-11 12:04:17 -05:00