Commit 4b85db43 authored by Lyude Paul's avatar Lyude Paul

more wip

parent 6e8a1f2f
......@@ -22,16 +22,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import enum
import struct
import re
import argparse
from abc import ABC
from enum import Enum
from sys import stderr
try:
import bitarray
except e:
from sys import stderr
print("The bitarray module for python3 is needed to run this!", file=stderr)
import sys
sys.exit(-1)
......@@ -54,6 +53,7 @@ class Parser:
# Strip in-line comments /* like this */
s = self.INLINE_COMMENT.sub('', s).rstrip()
if s != '':
self.last_line = i, s
yield (i, s)
......@@ -81,6 +81,47 @@ def scan_clause_header_tokens(string):
return ret
OP_RDWR_TARGET = re.compile(r'^([RUT]|(?:(?:location)\s*:\s*))([0-9]*)$')
def parse_port_src(string):
match = OP_RDWR_TARGET.match(string)
type_ = match.group(1)
if type_ == 'T':
idx = match.group(2)
if idx:
return (type_, int(idx))
else:
return (type_, None)
# return (type_, int(idx) if idx is not None else None)
else:
# Strip the trailing ':' in special src args
if type_.endswith(':'):
type_ = type_[:-1]
try:
return (type_, int(match.group(2)))
except TypeError:
if type_ == 'R' or type_ == 'U':
raise ParsingException('%s without index' % type_)
else:
raise ParsingException('%s without arg' % type_)
def parse_op_src(string):
try:
match = OP_RDWR_TARGET.match(string)
if match:
return parse_port_src(string)
elif string.startswith('0x'):
return int(string, base=16)
else:
return int(string, base=10)
except ParsingException as e:
raise e from e
except Exception as e:
raise ParsingException("Invalid target '%s'" % string)
OP_TOKEN = re.compile(r'^(\S+)\s+((?:\{[^}]+\})|\S+),\s*(.*)$')
DST_WITH_OUT = re.compile(r'^\{(\S+)\s*,\s*(T[01])\}$')
DST_NO_OUT = re.compile(r'^T[01]$')
......@@ -109,7 +150,9 @@ def scan_op_tokens(string):
# Split args
try:
args = [a.strip() for a in args.split(',')]
args = [parse_op_src(a.strip()) for a in args.split(',')]
except ParsingException as e:
raise e from e
except Exception:
raise ParsingException('In %s: %s is not a valid set of arguments' % (
string, args))
......@@ -117,18 +160,17 @@ def scan_op_tokens(string):
return inst, dst, args
# TODO: Each inst class should handle the base instruction + modifiers
class OpParser(ABC):
class OpParser:
def __init__(self, modifiers, dst, srcs):
self.modifiers = modifiers
self.dst = dst
self.srcs = srcs
@abstract
def parse(self):
raise NotImplemented()
@abstract
def encode(self):
raise NotImplemented()
def ModifierException(Exception):
def __init__(self, mods):
......@@ -148,26 +190,22 @@ class OpParser(ABC):
if self.modifiers:
raise ParsingException('Instruction does not take modifiers')
def check_mod_opcode_map(self, modifier):
try:
self.opcode = self.MOD_TO_OPCODE[modifier]
except KeyError:
raise ModifierException(modifier)
# Base Parsers/encoders for FMA operations
class fma:
class OneSrcOpParser(OpParser):
def parse(self):
self.check_src_cnt(1)
def encode(self):
ret = self.op_code;
# FIXME: yeah. handling the encoding here would be next to impossible?
# I think?
# def encode(self):
# ret = self.op_code;
class TwoSrcOpParser(OpParser):
def parse(self):
self.check_src_cnt(2)
class TwoSrcFmodOpParser(fma.TwoSrcOpParser):
class TwoSrcFmodOpParser(TwoSrcOpParser):
def parse(self):
raise NotImplemented()
......@@ -179,7 +217,7 @@ class fma:
def parse(self):
self.check_src_cnt(3)
class ThreeSrcFmodOpParser(fma.ThreeSrcOpParser):
class ThreeSrcFmodOpParser(ThreeSrcOpParser):
def parse(self):
raise NotImplemented()
......@@ -188,14 +226,14 @@ class fma:
self.check_src_cnt(4)
# Instruction parsers
class NopOp(fma.OneSrcOpParser):
class NopOp(OneSrcOpParser):
op_code = 0xe032c
def parse(self):
super().parse()
self.check_no_mods()
class MovOp(fma.OneSrcOpParser):
class MovOp(OneSrcOpParser):
op_code = 0xe032d
def parse(self):
......@@ -302,6 +340,14 @@ try:
except StopIteration:
pass
except ParsingException as e:
i, l = p.last_line
print('', file=stderr)
print('On line %d:' % i, file=stderr)
print('\t%s' % l, file=stderr)
print('', file=stderr)
print('Error: %s' % e.args[0], file=stderr)
unexpected_eof = False
except Exception as e:
unexpected_eof = False
raise e
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment