risc-v中文社区

 找回密码
 立即注册
查看: 1308|回复: 0

[原创] rocketchip高级参数化机制--源码(1)

[复制链接]

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
发表于 2021-8-23 21:32:11 | 显示全部楼层 |阅读模式
要想了解rocketchip的高级参数化---site/here/up机制,先将源码放在此,后面再来分析及使用:
// Copyright 2016-2019 SiFive, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chipsalliance.rocketchip

object config {
  abstract class Field[T] private (val default: Option[T])
  {
    def this() = this(None)
    def this(default: T) = this(Some(default))
  }

  abstract class View {
    final def apply[T](pname: Field[T]): T = apply(pname, this)
    final def apply[T](pname: Field[T], site: View): T = {
      val out = find(pname, site)
      require (out.isDefined, s"Key ${pname} is not defined in Parameters")
      out.get
    }

    final def lift[T](pname: Field[T]): Option[T] = lift(pname, this)
    final def lift[T](pname: Field[T], site: View): Option[T] = find(pname, site).map(_.asInstanceOf[T])

    protected[config] def find[T](pname: Field[T], site: View): Option[T]
  }

  abstract class Parameters extends View {
    // x alter y: settings in 'y' overrule settings in 'x'
    final def alter(rhs: Parameters): Parameters =
      new ChainParameters(rhs, this)

    final def alter(f: (View, View, View) => PartialFunction[Any,Any]): Parameters =
      alter(Parameters(f))

    final def alterPartial(f: PartialFunction[Any,Any]): Parameters =
      alter(Parameters((_,_,_) => f))

    final def alterMap(m: Map[Any,Any]): Parameters =
      alter(new MapParameters(m))

    protected[config] def chain[T](site: View, tail: View, pname: Field[T]): Option[T]
    protected[config] def find[T](pname: Field[T], site: View) = chain(site, new TerminalView, pname)

    // x orElse y: settings in 'x' overrule settings in 'y'
    final def orElse(x: Parameters): Parameters = x.alter(this)

    // Please use 'alter' or 'orElse' instead of '++'.
    // People expect this to be alter (like Map ++ Map), but it's orElse.
    final def ++ (x: Parameters): Parameters = orElse(x)
  }

  object Parameters {
    def empty: Parameters = new EmptyParameters
    def apply(f: (View, View, View) => PartialFunction[Any,Any]): Parameters = new PartialParameters(f)
  }

  class Config(p: Parameters) extends Parameters {
    def this(f: (View, View, View) => PartialFunction[Any,Any]) = this(Parameters(f))

    protected[config] def chain[T](site: View, tail: View, pname: Field[T]) = p.chain(site, tail, pname)
    override def toString = this.getClass.getSimpleName
    def toInstance = this
  }

  // Internal implementation:

  private class TerminalView extends View {
    def find[T](pname: Field[T], site: View): Option[T] = pname.default
  }

  private class ChainView(head: Parameters, tail: View) extends View {
    def find[T](pname: Field[T], site: View) = head.chain(site, tail, pname)
  }

  private class ChainParameters(x: Parameters, y: Parameters) extends Parameters {
    def chain[T](site: View, tail: View, pname: Field[T]) = x.chain(site, new ChainView(y, tail), pname)
  }

  private class EmptyParameters extends Parameters {
    def chain[T](site: View, tail: View, pname: Field[T]) = tail.find(pname, site)
  }

  private class PartialParameters(f: (View, View, View) => PartialFunction[Any,Any]) extends Parameters {
    protected[config] def chain[T](site: View, tail: View, pname: Field[T]) = {
      val g = f(site, this, tail)
      if (g.isDefinedAt(pname)) Some(g.apply(pname).asInstanceOf[T]) else tail.find(pname, site)
    }
  }

  private class MapParameters(map: Map[Any, Any]) extends Parameters {
    protected[config] def chain[T](site: View, tail: View, pname: Field[T]) = {
      val g = map.get(pname)
      if (g.isDefined) Some(g.get.asInstanceOf[T]) else tail.find(pname, site)
    }
  }
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-5-3 14:42 , Processed in 0.026636 second(s), 17 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表