switch to exact version of cython
[ieee754fpu.git] / src / nmutil / dynamicpipe.py
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 # See Notices.txt for copyright information
3
4 """ Meta-class that allows a dynamic runtime parameter-selectable "mixin"
5
6 The reasons why this technique is being deployed is because SimpleHandshake
7 needs to be dynamically replaced at the end-users' choice, without having
8 to duplicate dozens of classes using multiple-inheritanc "Mix-in" techniques.
9
10 It is however extremely unusual, and has been explicitly limited to this *one*
11 module. DO NOT try to use this technique elsewhere, it is extremely hard to
12 understand (meta-class programming).
13
14 """
15
16 from abc import ABCMeta
17
18 from nmutil.singlepipe import SimpleHandshake
19 from nmutil.singlepipe import MaskCancellable
20
21 import threading
22
23 # with many thanks to jsbueno on stackexchange for this one
24 # https://stackoverflow.com/questions/57273070/
25 # list post:
26 # http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-July/002259.html
27
28 class Meta(ABCMeta):
29 registry = {}
30 recursing = threading.local()
31 recursing.check = False
32 mlock = threading.Lock()
33
34 def __call__(cls, *args, **kw):
35 mcls = cls.__class__
36 if mcls.recursing.check:
37 return super().__call__(*args, **kw)
38 spec = args[0]
39 base = spec.pipekls # pick up the dynamic class from PipelineSpec, HERE
40
41 if (cls, base) not in mcls.registry:
42 print ("__call__", args, kw, cls, base,
43 base.__bases__, cls.__bases__)
44 mcls.registry[cls, base] = type(
45 cls.__name__,
46 (cls, base) + cls.__bases__[1:],
47 {}
48 )
49 real_cls = mcls.registry[cls, base]
50
51 with mcls.mlock:
52 mcls.recursing.check = True
53 instance = real_cls.__class__.__call__(real_cls, *args, **kw)
54 mcls.recursing.check = False
55 return instance
56
57
58 # Inherit from this class instead of SimpleHandshake (or other ControlBase
59 # derivative), and the metaclass will instead *replace* DynamicPipe -
60 # *at runtime* - with the class that is specified *as a parameter*
61 # in PipelineSpec.
62 #
63 # as explained in the list posting and in the stackexchange post, this is
64 # needed to avoid a MASSIVE suite of duplicated multiple-inheritance classes
65 # that "Mix in" SimpleHandshake (or other).
66 #
67 # unfortunately, composition does not work in this instance
68 # (make an *instance* of SimpleHandshake or other class and pass it in)
69 # due to the multiple level inheritance, and in several places
70 # the inheriting class needs to do some setup that the deriving class
71 # needs in order to function correctly.
72
73 class DynamicPipe(metaclass=Meta):
74 def __init__(self, *args):
75 print ("DynamicPipe init", super(), args)
76 super().__init__(self, *args)
77
78
79 # bad hack: the DynamicPipe metaclass ends up creating an __init__ signature
80 # for the dynamically-derived class. luckily, SimpleHandshake only needs
81 # "self" as the 1st argument (it is its own "Stage"). anything else
82 # could hypothetically be passed through the pspec.
83 class SimpleHandshakeRedir(SimpleHandshake):
84 def __init__(self, mod, *args):
85 print ("redir", mod, args)
86 stage = self
87 if args and args[0].stage:
88 stage = args[0].stage
89 SimpleHandshake.__init__(self, stage)
90
91
92 class MaskCancellableRedir(MaskCancellable):
93 def __init__(self, mod, *args):
94 stage = self
95 maskwid = args[0].maskwid
96 if args[0].stage:
97 stage = args[0].stage
98 print ("redir mask", mod, args, maskwid)
99 MaskCancellable.__init__(self, stage, maskwid)
100