resolve awful meta-class hacking (with thanks to jsbueno on stackexchange)
[ieee754fpu.git] / src / ieee754 / pipeline.py
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 # See Notices.txt for copyright information
3
4 from abc import ABCMeta
5 from nmigen import Elaboratable
6
7 from nmutil.singlepipe import SimpleHandshake
8 import threading
9
10
11 class PipelineSpec:
12 """ Pipeline Specification base class.
13
14 :attribute width: the IEEE754 FP bitwidth
15 :attribute id_wid: the Reservation Station muxid bitwidth
16 :attribute op_wid: an "operand bitwidth" passed down all stages
17 :attribute opkls: an optional class that is instantiated as the "operand"
18
19 See ieee754/fpcommon/getop FPPipeContext for how (where) PipelineSpec
20 is used. FPPipeContext is passed down *every* stage of a pipeline
21 and contains the Reservation Station multiplexer ID as well as
22 an optional "operand". This "operand" may be used to *change*
23 the behaviour of the pipeline. In RISC-V terminology it would
24 typically be set to e.g. funct7 or parts thereof.
25
26 """
27
28 def __init__(self, width, id_width, op_wid=0, opkls=None, pipekls=None):
29 """ Create a PipelineSpec. """
30 self.width = width
31 self.id_wid = id_width
32 self.op_wid = op_wid
33 self.opkls = opkls
34 self.pipekls = pipekls or SimpleHandshakeRedir
35 self.core_config = None
36 self.fpformat = None
37 self.n_comb_stages = None
38
39 # with many thanks to jsbueno on stackexchange for this one
40 # https://stackoverflow.com/questions/57273070/
41
42 class Meta(ABCMeta):
43 registry = {}
44 recursing = threading.local()
45 recursing.check = False
46 mlock = threading.Lock()
47
48 def __call__(cls, *args, **kw):
49 mcls = cls.__class__
50 if mcls.recursing.check:
51 return super().__call__(*args, **kw)
52 spec = args[0]
53 base = spec.pipekls
54
55 if (cls, base) not in mcls.registry:
56 print ("__call__", args, kw, cls, base, base.__bases__, cls.__bases__)
57 mcls.registry[cls, base] = type(
58 cls.__name__,
59 (cls, base) + cls.__bases__[1:],
60 {}
61 )
62 real_cls = mcls.registry[cls, base]
63
64 with mcls.mlock:
65 mcls.recursing.check = True
66 instance = real_cls.__class__.__call__(real_cls, *args, **kw)
67 mcls.recursing.check = False
68 return instance
69
70
71 class DynamicPipe(metaclass=Meta):
72 def __init__(self, *args):
73 print ("DynamicPipe init", super(), args)
74 super().__init__(self, *args)
75
76
77 # bad hack: the DynamicPipe metaclass ends up creating an __init__ signature
78 # for the dynamically-derived class. luckily, SimpleHandshake only needs
79 # "self" as the 1st argument (it is its own "Stage"). anything else
80 # could hypothetically be passed through the pspec.
81 class SimpleHandshakeRedir(SimpleHandshake):
82 def __init__(self, pspec, *args):
83 print ("redir", pspec, args)
84 SimpleHandshake.__init__(self, self)
85