xref: /XiangShan/macros/src/main/scala/CSRMacros.scala (revision 039cdc35f5f3b68b6295ec5ace90f22a77322e02)
1package xiangshan.macros
2
3import scala.annotation.compileTimeOnly
4import scala.language.experimental.macros
5import scala.reflect.macros.blackbox.Context
6
7object CSRMacros {
8  object CSRFieldsImpl {
9    private def calcuWidth(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int]): Int = {
10      import c.universe._
11
12      val i_msb = c.eval(msb)
13      val i_lsb = c.eval(lsb)
14
15      i_msb - i_lsb + 1
16    }
17
18    @compileTimeOnly("")
19    def CSRROFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], rfn: c.Tree): c.Tree = {
20      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.RO(${c.eval(msb)}, ${c.eval(lsb)}, $rfn)")
21    }
22
23    @compileTimeOnly("")
24    def CSRROFieldBit(c: Context)(bit: c.Expr[Int], rfn: c.Tree): c.Tree = {
25      CSRROFieldRange(c)(bit, bit, rfn)
26    }
27
28    @compileTimeOnly("")
29    def CSRROFieldRangeNoFn(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int]): c.Tree = {
30      CSRROFieldRange(c)(msb, lsb, null)
31    }
32
33    @compileTimeOnly("")
34    def CSRROFieldBitNoFn(c: Context)(bit: c.Expr[Int]): c.Tree = {
35      CSRROFieldRange(c)(bit, bit, null)
36    }
37
38    @compileTimeOnly("")
39    def CSRWARLFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], fn: c.Tree): c.Tree = {
40      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.WARL(${c.eval(msb)}, ${c.eval(lsb)}, $fn)")
41    }
42
43    @compileTimeOnly("")
44    def CSRWARLFieldBit(c: Context)(bit: c.Expr[Int], fn: c.Tree): c.Tree = {
45      CSRWARLFieldRange(c)(bit, bit, fn)
46    }
47
48    @compileTimeOnly("")
49    def CSRRWFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int]): c.Tree = {
50      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.RW(" +
51        s"${c.eval(msb)}, " +
52        s"${c.eval(lsb)}" +
53        s")"
54      )
55    }
56
57    @compileTimeOnly("")
58    def CSRRWFieldBit(c: Context)(bit: c.Expr[Int]): c.Tree = {
59      CSRRWFieldRange(c)(bit, bit)
60    }
61
62    @compileTimeOnly("")
63    def CSRWLRLFieldRange(c: Context)(msb: c.Expr[Int], lsb: c.Expr[Int], fn: c.Tree): c.Tree = {
64      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.WARL(${c.eval(msb)}, ${c.eval(lsb)}, $fn)")
65    }
66
67    @compileTimeOnly("")
68    def CSRWLRLFieldBit(c: Context)(bit: c.Expr[Int], fn: c.Tree): c.Tree = {
69      CSRWLRLFieldRange(c)(bit, bit, fn)
70    }
71
72    @compileTimeOnly("")
73    def CSRRefWARLFieldRange(c: Context)(ref: c.Tree, msb: c.Expr[Int], lsb: c.Expr[Int], wfn: c.Tree): c.Tree = {
74      c.parse(s"CSRDefines.CSRField${calcuWidth(c)(msb, lsb)}Bits.RefWARL($ref, ${c.eval(msb)}, ${c.eval(lsb)}, $wfn)")
75    }
76
77    @compileTimeOnly("")
78    def CSRRefWARLFieldBit(c: Context)(ref: c.Tree, bit: c.Expr[Int], wfn: c.Tree): c.Tree = {
79      CSRRefWARLFieldRange(c)(ref, bit, bit, wfn)
80    }
81  }
82}
83
84
85