Skip to content

Commit

Permalink
Add pos tags to cells and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakayorihiro committed Jan 10, 2025
1 parent 9456939 commit 77dd2df
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 8 additions & 0 deletions calyx-py/calyx/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ def __init__(
)
)

if not is_comb:
position_id = determine_source_loc(self.prog.program.position_table)
if position_id is not None:
self.component.attributes.append(ast.CompAttribute("pos", position_id))

self.index: Dict[str, Union[GroupBuilder, CellBuilder]] = {}
self.continuous = GroupBuilder(None, self)
self.next_gen_idx = 0
Expand Down Expand Up @@ -368,6 +373,9 @@ def cell(

cell = ast.Cell(ast.CompVar(name), comp, is_external, is_ref)
assert cell not in self.component.cells, f"cell '{name}' already exists"
position_id = determine_source_loc(self.prog.program.position_table)
if position_id is not None:
cell.attributes.append(ast.CellAttribute("pos", position_id))

self.component.cells.append(cell)
builder = CellBuilder(cell)
Expand Down
23 changes: 20 additions & 3 deletions calyx-py/calyx/py_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ class CompAttribute(Attribute):
def doc(self) -> str:
return f'"{self.name}"={self.value}'

@dataclass
class CellAttribute(Attribute):
name: str
value: Optional[int] = None

def doc(self) -> str:
return f"@{self.name}" if self.value is None else f"@{self.name}({self.value})"

@dataclass
class GroupAttribute(Attribute):
name: str
Expand Down Expand Up @@ -339,14 +347,23 @@ class Cell(Structure):
comp: CompInst
is_external: bool = False
is_ref: bool = False
attributes: list[CellAttribute] = field(default_factory=list)

def doc(self) -> str:
# NOTE: adding external on the fly (instead of having the user add it to attributes)
# so that we can easily do this check
assert not (
self.is_ref and self.is_external
), "Cell cannot be both a ref and external"
external = "@external " if self.is_external else ""
if self.is_external:
self.attributes.append(CellAttribute("external"))
attribute_annotation = (
f"{' '.join([f'{a.doc()}' for a in self.attributes])} "
if len(self.attributes) > 0
else ""
)
ref = "ref " if self.is_ref else ""
return f"{external}{ref}{self.id.doc()} = {self.comp.doc()};"
return f"{attribute_annotation}{ref}{self.id.doc()} = {self.comp.doc()};"


@dataclass
Expand Down Expand Up @@ -375,7 +392,7 @@ class Group(Structure):
def doc(self) -> str:
# hack - add static delay on the fly. Might be problematic if the group was ever going to be written multiple times?
if self.static_delay is not None:
self.attributes += GroupAttribute("promotable", self.static_delay)
self.attributes.append(GroupAttribute("promotable", self.static_delay))
attribute_annotation = (
f"<{', '.join([f'{a.doc()}' for a in self.attributes])}>"
if len(self.attributes) > 0
Expand Down

0 comments on commit 77dd2df

Please sign in to comment.