eurg start putting namespace-injection (GPR, MEM) in place
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 4 Apr 2020 16:39:34 +0000 (17:39 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 4 Apr 2020 16:39:34 +0000 (17:39 +0100)
src/soc/decoder/isa/caller.py [new file with mode: 0644]
src/soc/decoder/pseudo/pywriter.py

diff --git a/src/soc/decoder/isa/caller.py b/src/soc/decoder/isa/caller.py
new file mode 100644 (file)
index 0000000..cc78a3f
--- /dev/null
@@ -0,0 +1,50 @@
+from functools import wraps
+
+class ISACaller:
+    def __init__(self):
+        self.gpr = {} # TODO
+        self.mem = {} # TODO
+        self.namespace = {'GPR': self.gpr, 'MEM': self.mem}
+
+def inject(context):
+    """ Decorator factory. """
+    def variable_injector(func):
+        @wraps(func)
+        def decorator(*args, **kwargs):
+            try:
+                func_globals = func.__globals__  # Python 2.6+
+            except AttributeError:
+                func_globals = func.func_globals  # Earlier versions.
+
+            saved_values = func_globals.copy()  # Shallow copy of dict.
+            func_globals.update(context)
+
+            result = func(*args, **kwargs)
+            #exec (func.__code__, func_globals)
+
+            #finally:
+            #    func_globals = saved_values  # Undo changes.
+
+            return result
+
+        return decorator
+
+    return variable_injector
+
+if __name__ == '__main__':
+    d = {'1': 1}
+    namespace = {'a': 5, 'b': 3, 'd': d}
+
+    @inject(namespace)
+    def test():
+        print (globals())
+        print('a:', a)
+        print('b:', b)
+        print('d1:', d['1'])
+        d[2] = 5
+        
+        return locals()
+
+    test()
+
+    print (namespace)
index 2bc9dc5e27fbeb26a04482094c00653e72bb84fd..36948940b4db74100f64b6f8c7be162034c8c956 100644 (file)
@@ -23,7 +23,7 @@ def create_args(reglist, extra=None):
 header = """\
 # auto-generated by pywriter.py, do not edit or commit
 
-from soc.decoder.isa import ISACaller
+from soc.decoder.isa.caller import ISACaller, inject
 from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
 from soc.decoder.selectable_int import SelectableInt
 from soc.decoder.selectable_int import selectconcat as concat
@@ -59,6 +59,7 @@ class PyISAWriter(ISA):
                 # write out function.  pre-pend "op_" because some instrs are
                 # also python keywords (cmp).  also replace "." with "_"
                 op_fname ="op_%s" % page.replace(".", "_")
+                f.write("    @inject(self.namespace)\n")
                 f.write("    def %s(%s):\n" % (op_fname, args))
                 pycode = pycode.split("\n")
                 pycode = '\n'.join(map(lambda x: "        %s" % x, pycode))