From 78a431331603e01f8b507d0f8f008acf87612019 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Thu, 2 Apr 2020 11:53:30 +0100 Subject: [PATCH] add in ability to concat ints --- src/soc/decoder/power_pseudo.py | 8 +++----- src/soc/decoder/pseudo/lexer.py | 4 ++-- src/soc/decoder/pseudo/parser.py | 5 +++-- src/soc/decoder/selectable_int.py | 9 +++++++++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/soc/decoder/power_pseudo.py b/src/soc/decoder/power_pseudo.py index eb71052c..104edd98 100644 --- a/src/soc/decoder/power_pseudo.py +++ b/src/soc/decoder/power_pseudo.py @@ -19,7 +19,7 @@ from nmigen.back.pysim import Simulator, Delay from nmigen import Module, Signal from soc.decoder.pseudo.parser import GardenSnakeCompiler -from soc.decoder.selectable_int import SelectableInt +from soc.decoder.selectable_int import SelectableInt, selectconcat ####### Test code ####### @@ -132,16 +132,14 @@ def test(): print ("args", args) print ("-->", " ".join(map(str,args))) - def listconcat(l1, l2): - return l1 + l2 - from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,) d = {} d["print"] = print_ d["EXTS64"] = EXTS64 d["EXTZ64"] = EXTZ64 - d["concat"] = listconcat + d["SelectableInt"] = SelectableInt + d["concat"] = selectconcat d["GPR"] = gsc.gpr form = 'X' diff --git a/src/soc/decoder/pseudo/lexer.py b/src/soc/decoder/pseudo/lexer.py index e2ba0a2b..9076672d 100644 --- a/src/soc/decoder/pseudo/lexer.py +++ b/src/soc/decoder/pseudo/lexer.py @@ -10,7 +10,7 @@ # Modifications for inclusion in PLY distribution from copy import copy from ply import lex - +from soc.decoder.selectable_int import SelectableInt ## I implemented INDENT / DEDENT generation as a post-processing filter @@ -264,7 +264,7 @@ class PowerLexer: def t_BINARY(self, t): r"""0b[01]+""" - t.value = int(t.value, 2) + t.value = SelectableInt(int(t.value, 2), len(t.value)-2) return t #t_NUMBER = r'\d+' diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index 1db988e7..69fe6aa9 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -90,8 +90,9 @@ def check_concat(node): # checks if the comparison is already a concat print (node) if not isinstance(node, ast.Call): return [node] - if node[0].id != 'concat': - return node + print (node.func.id) + if node.func.id != 'concat': + return [node] return node[1] diff --git a/src/soc/decoder/selectable_int.py b/src/soc/decoder/selectable_int.py index a73ceb5d..e1fd8d64 100644 --- a/src/soc/decoder/selectable_int.py +++ b/src/soc/decoder/selectable_int.py @@ -1,4 +1,5 @@ import unittest +from copy import copy class SelectableInt: @@ -99,6 +100,14 @@ class SelectableInt: return "SelectableInt(value={:x}, bits={})".format(self.value, self.bits) +def selectconcat(*args): + res = copy(args[0]) + for i in args[1:]: + assert isinstance(i, SelectableInt), "can only concat SIs, sorry" + res.bits += i.bits + res.value = (res.value << i.bits) | i.value + return res + class SelectableIntTestCase(unittest.TestCase): def test_arith(self): -- 2.30.2