From: Luke Kenneth Casson Leighton Date: Sat, 4 Apr 2020 16:39:34 +0000 (+0100) Subject: eurg start putting namespace-injection (GPR, MEM) in place X-Git-Tag: div_pipeline~1527 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=fd9e904d0cea7d499f7872441caa910906b0b3e1;p=soc.git eurg start putting namespace-injection (GPR, MEM) in place --- diff --git a/src/soc/decoder/isa/caller.py b/src/soc/decoder/isa/caller.py new file mode 100644 index 00000000..cc78a3f9 --- /dev/null +++ b/src/soc/decoder/isa/caller.py @@ -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) diff --git a/src/soc/decoder/pseudo/pywriter.py b/src/soc/decoder/pseudo/pywriter.py index 2bc9dc5e..36948940 100644 --- a/src/soc/decoder/pseudo/pywriter.py +++ b/src/soc/decoder/pseudo/pywriter.py @@ -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))