Skip to content

Commit

Permalink
Merge pull request #3 from VyperCore/example-context
Browse files Browse the repository at this point in the history
Create a context manager for contextual coverage data
  • Loading branch information
Kotarski authored Oct 30, 2023
2 parents ade9544 + f6a2367 commit 1a7f37c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
29 changes: 29 additions & 0 deletions fcov/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from copy import copy
from types import SimpleNamespace

class ContextBase:
_stack: list["SimpleNamespace"] = [SimpleNamespace()]

def __init__(self, replace=False, overlay=True, **kwargs):
if overlay:
self.data = copy(self._stack[-1])
for k, v in kwargs.items():
if not replace and hasattr(self.data, k):
raise KeyError(f"Context already contains `{k}` and `replace` is False")
setattr(self.data, k, v)
else:
self.data = SimpleNamespace(**kwargs)

def __enter__(self):
self._stack.append(self.data)

def __exit__(self, _extype, _exval, _extb):
self._stack.pop()

@classmethod
def get(cls):
if len(cls._stack):
return cls._stack[-1]
return SimpleNamespace()

class CoverageContext(ContextBase): ...
8 changes: 6 additions & 2 deletions fcov/covergroup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from types import SimpleNamespace
from .context import CoverageContext


class Covergroup:
"""This class groups coverpoints together, and adds them to the hierarchy"""

Expand All @@ -14,7 +18,7 @@ def __init__(self, name, description):

self.coverpoints = {}
self.covergroups = {}
self.setup()
self.setup(ctx=CoverageContext.get())

def add_coverpoint(self, coverpoint):
"""
Expand All @@ -41,7 +45,7 @@ def add_covergroup(self, covergroup):
self.covergroups[covergroup.name] = covergroup
setattr(self, covergroup.name, covergroup)

def setup(self):
def setup(self, ctx: SimpleNamespace):
raise NotImplementedError("This needs to be implemented by the coverpoint")

def print_tree(self, indent=0):
Expand Down
7 changes: 6 additions & 1 deletion fcov/coverpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from rich.console import Console
from rich.table import Table

from .context import CoverageContext

from .axis import Axis
from .cursor import Cursor
from .goal import GoalItem
Expand Down Expand Up @@ -42,7 +44,7 @@ def __init__(self, name: str, description: str, trigger=None):
# Instance of Cursor class to increment hit count for a bucket
self.cursor = Cursor(self)

self.setup()
self.setup(ctx=CoverageContext.get())

self.axis_names = [x.name for x in self.axes]
# TODO Check if goalsDict only has 1 entry
Expand All @@ -53,6 +55,9 @@ def __init__(self, name: str, description: str, trigger=None):
if goal:
self._cvg_goals[cursor] = goal

def setup(self, ctx: SimpleNamespace):
raise NotImplementedError("This needs to be implemented by the coverpoint")

def all_axis_value_combinations(self):
axis_values = []
for axis in self.axes:
Expand Down
11 changes: 6 additions & 5 deletions fcov/example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .context import CoverageContext
from .covergroup import Covergroup
from .coverpoint import Coverpoint
from .sampler import Sampler
Expand All @@ -20,7 +21,7 @@ class MyCoverpoint(Coverpoint):
def __init__(self, name: str, description: str, trigger=None):
super().__init__(name, description, trigger=None)

def setup(self):
def setup(self, ctx):
self.add_axis(
name="my_axis_1",
values=[0, 1, [2, 3]],
Expand Down Expand Up @@ -65,15 +66,15 @@ def sample(self, trace):

# covergroups
class MyCovergroup(Covergroup):
def setup(self):
def setup(self, ctx):
self.add_coverpoint(MyCoverpoint(name="my_coverpoint", description="A lovely coverpoint"))
self.add_coverpoint(
MyCoverpoint(name="another_coverpoint", description="A rather spiffing coverpoint")
)


class MyBigCoverGroup(Covergroup):
def setup(self):
def setup(self, ctx):
self.add_covergroup(
MyCovergroup(name="my_covergroup", description="A group of coverpoints")
)
Expand Down Expand Up @@ -103,8 +104,8 @@ def create_trace(self):

if __name__ == "__main__":
# testbench

cvg = MyBigCoverGroup(name="my_big_covergroup", description="A group of stuff")
with CoverageContext(isa="THIS IS AN ISA"):
cvg = MyBigCoverGroup(name="my_big_covergroup", description="A group of stuff")

cvg.print_tree()
cvg.my_covergroup.print_tree()
Expand Down

0 comments on commit 1a7f37c

Please sign in to comment.