简体   繁体   English

chisel 中如何解释这种语法?

[英]How is this syntax explained in chisel?

I am learning chisel and rocket-chip.我正在学习凿子和火箭芯片。 I recently found an unreadable syntax in the rocket / RocketCore.scala file.我最近在 Rocket/RocketCore.scala 文件中发现了一个不可读的语法。

val perfEvents = new EventSets(Seq(
new EventSet((mask, hits) => Mux(mask(0), wb_xcpt, wb_valid && pipelineIDToWB((mask & hits).orR)), Seq(
  ("exception", () => false.B),
  ("load", () => id_ctrl.mem && id_ctrl.mem_cmd === M_XRD && !id_ctrl.fp),
  ("store", () => id_ctrl.mem && id_ctrl.mem_cmd === M_XWR && !id_ctrl.fp),
  ("amo", () => Bool(usingAtomics) && id_ctrl.mem && (isAMO(id_ctrl.mem_cmd) || id_ctrl.mem_cmd.isOneOf(M_XLR, M_XSC))),
  ("system", () => id_ctrl.csr =/= CSR.N),
  ("arith", () => id_ctrl.wxd && !(id_ctrl.jal || id_ctrl.jalr || id_ctrl.mem || id_ctrl.fp || id_ctrl.mul || id_ctrl.div || id_ctrl.csr =/= CSR.N)),
  ("branch", () => id_ctrl.branch),
  ("jal", () => id_ctrl.jal),
  ("jalr", () => id_ctrl.jalr))
  ++ (if (!usingMulDiv) Seq() else Seq(
    ("mul", () => if (pipelinedMul) id_ctrl.mul else id_ctrl.div && (id_ctrl.alu_fn & ALU.FN_DIV) =/= ALU.FN_DIV),
    ("div", () => if (pipelinedMul) id_ctrl.div else id_ctrl.div && (id_ctrl.alu_fn & ALU.FN_DIV) === ALU.FN_DIV)))
  ++ (if (!usingFPU) Seq() else Seq(
    ("fp load", () => id_ctrl.fp && io.fpu.dec.ldst && io.fpu.dec.wen),
    ("fp store", () => id_ctrl.fp && io.fpu.dec.ldst && !io.fpu.dec.wen),
    ("fp add", () => id_ctrl.fp && io.fpu.dec.fma && io.fpu.dec.swap23),
    ("fp mul", () => id_ctrl.fp && io.fpu.dec.fma && !io.fpu.dec.swap23 && !io.fpu.dec.ren3),
    ("fp mul-add", () => id_ctrl.fp && io.fpu.dec.fma && io.fpu.dec.ren3),
    ("fp div/sqrt", () => id_ctrl.fp && (io.fpu.dec.div || io.fpu.dec.sqrt)),
    ("fp other", () => id_ctrl.fp && !(io.fpu.dec.ldst || io.fpu.dec.fma || io.fpu.dec.div || io.fpu.dec.sqrt))))),
new EventSet((mask, hits) => (mask & hits).orR, Seq(
  ("load-use interlock", () => id_ex_hazard && ex_ctrl.mem || id_mem_hazard && mem_ctrl.mem || id_wb_hazard && wb_ctrl.mem),
  ("long-latency interlock", () => id_sboard_hazard),
  ("csr interlock", () => id_ex_hazard && ex_ctrl.csr =/= CSR.N || id_mem_hazard && mem_ctrl.csr =/= CSR.N || id_wb_hazard && wb_ctrl.csr =/= CSR.N),
  ("I$ blocked", () => icache_blocked),
  ("D$ blocked", () => id_ctrl.mem && dcache_blocked),
  ("branch misprediction", () => take_pc_mem && mem_direction_misprediction),
  ("control-flow target misprediction", () => take_pc_mem && mem_misprediction && mem_cfi && !mem_direction_misprediction && !icache_blocked),
  ("flush", () => wb_reg_flush_pipe),
  ("replay", () => replay_wb))
  ++ (if (!usingMulDiv) Seq() else Seq(
    ("mul/div interlock", () => id_ex_hazard && (ex_ctrl.mul || ex_ctrl.div) || id_mem_hazard && (mem_ctrl.mul || mem_ctrl.div) || id_wb_hazard && wb_ctrl.div)))
  ++ (if (!usingFPU) Seq() else Seq(
    ("fp interlock", () => id_ex_hazard && ex_ctrl.fp || id_mem_hazard && mem_ctrl.fp || id_wb_hazard && wb_ctrl.fp || id_ctrl.fp && id_stall_fpu)))),
new EventSet((mask, hits) => (mask & hits).orR, Seq(
  ("I$ miss", () => io.imem.perf.acquire),
  ("D$ miss", () => io.dmem.perf.acquire),
  ("D$ release", () => io.dmem.perf.release),
  ("ITLB miss", () => io.imem.perf.tlbMiss),
  ("DTLB miss", () => io.dmem.perf.tlbMiss),
  ("L2 TLB miss", () => io.ptw.perf.l2miss)))))

When using class EventSet, I did not find the definition of mask and hits.使用类EventSet的时候,没有找到mask和hits的定义。

That is a pretty intimidating piece of code.这是一段相当令人生畏的代码。 Breaking it down打破它

  • perfEvents is assigned to be an instance of EventSets perfEvents被分配为EventSets一个实例
  • EventSets requires a single parameter EventSets需要一个参数
    • class EventSets(val eventSets: Seq[EventSet])
    • So you see a Seq of EventSet begin created.所以你会看到一个EventSetSeq开始创建。
  • EventSet requires two parameters EventSet需要两个参数
    • class EventSet(gate: (UInt, UInt) => Bool, events: Seq[(String, () => Bool)])
    • First: A function of two UInts that returns a Bool第一:返回Bool的两个UInts的函数
    • Second: A Seq of 2 tuples of String and a function with no parameters that returns a Bool第二:一个由 2 个String元组组成的Seq和一个没有参数的函数,它返回一个Bool
    • So you see a long sequence of these tuples being defined.所以你会看到一长串这些元组被定义。
    • NOTE: The sequence of (String, () => Bool()) needs to be different depending on configuration parameters like usingMulDiv注意: (String, () => Bool())的顺序需要根据usingMulDiv等配置参数而有所不同
      • Thus you see the construct ++ (if (.. a couple of times.因此,您会看到构造++ (if (..几次。
      • This uses if conditionally add different subsequences of tuples to the EventSet parameter it is constucting.这使用if有条件地将元组的不同子序列添加到它正在构造的EventSet参数。

I hope that helps.我希望这有帮助。 This is a whole lot of stuff that is probably going to be either ugly and short or clearer and really long and verbose.这是一大堆东西,它们可能要么丑陋、简短,要么更清晰、又长又冗长。 It may help a little to run this through a formatter.通过格式化程序运行它可能会有所帮助。 Here's a bit of that.这里有一点。

val perfEvents = new EventSets(
  Seq(
    new EventSet(
      (mask, hits) => Mux(wb_xcpt, mask(0), wb_valid && pipelineIDToWB((mask & hits).orR)),
      Seq(
        ("exception", () => false.B),
        ("load", () => id_ctrl.mem && id_ctrl.mem_cmd === M_XRD && !id_ctrl.fp),
        ("store", () => id_ctrl.mem && id_ctrl.mem_cmd === M_XWR && !id_ctrl.fp),
        ("amo",
         () =>
           Bool(usingAtomics) && id_ctrl.mem && (isAMO(id_ctrl.mem_cmd) || id_ctrl.mem_cmd.isOneOf(M_XLR, M_XSC))),
        ("system", () => id_ctrl.csr =/= CSR.N),
        ("arith",
         () =>
           id_ctrl.wxd && !(id_ctrl.jal || id_ctrl.jalr || id_ctrl.mem || id_ctrl.fp || id_ctrl.mul || id_ctrl.div || id_ctrl.csr =/= CSR.N)),
        ("branch", () => id_ctrl.branch),
        ("jal", () => id_ctrl.jal),
        ("jalr", () => id_ctrl.jalr)
      )
        ++ (if (!usingMulDiv) Seq()
            else
              Seq(

mask and hits are just arguments of the lambda passed to the EventSet constructor, the (mask, hits) => ... part is their definition (or declaration, if you prefer). maskhits只是传递给EventSet构造函数的 lambda 参数, (mask, hits) => ...部分它们的定义(或声明,如果您愿意)。 If you look at EventSet source you can see the lambda is called gate and used only in this method:如果您查看EventSet源代码,您可以看到 lambda 被称为gate并且仅在此方法中使用:

def check(mask: UInt) = gate(mask, hits)

So mask will be the argument of check and hits come from所以mask将是check的参数, hits来自

def hits = events.map(_._2()).asUInt

In this case, for the first EventSet you'll get something like在这种情况下,对于第一个EventSet您将得到类似

Seq(
  false.B,
  id_ctrl.mem && id_ctrl.mem_cmd === M_XRD && !id_ctrl.fp,
  ...
).asUInt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM