Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calyx-py: add line numbers to group enables, similar to Dhalia #2285

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions calyx-py/calyx/py_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from dataclasses import dataclass, field
from typing import List, Any, Tuple, Optional
from calyx.utils import block
import inspect
import os


@dataclass
Expand Down Expand Up @@ -461,6 +463,51 @@ def doc(self) -> str:
return f"({self.left.doc()} >= {self.right.doc()})"


@dataclass
class SourceLoc:
line: int
file: str


def frame_to_source_loc(frame: inspect.FrameInfo) -> SourceLoc:
"""builds a SourceLoc object from a Python frame"""
return SourceLoc(frame.lineno, os.path.basename(frame.filename))


def determine_source_loc() -> Optional[SourceLoc]:
"""Inspects the call stack to determine the first call site outside the calyx-py library."""
stacktrace = inspect.stack()

# inspect top frame to determine the path to the calyx-py library
top = stacktrace[0]
assert top.function == "determine_source_loc"
library_path = os.path.dirname(top.filename)
assert os.path.join(library_path, "py_ast.py") == top.filename

# find first stack frame that is not part of the library
user = None
for frame in stacktrace:
# skip frames that do not have a real filename
if frame.filename == "<string>":
continue
if not frame.filename.startswith(library_path):
user = frame
break
if user is None:
return None

# build source locator from frame
return frame_to_source_loc(user)


def with_pos_attribute(source: str, loc: Optional[SourceLoc]) -> str:
"""adds the @pos attribute of loc is not None"""
if loc is None:
return source
else:
return f"@pos({loc.line}) {source}"


# Control
@dataclass
class Control(Emittable):
Expand All @@ -470,9 +517,10 @@ class Control(Emittable):
@dataclass
class Enable(Control):
stmt: str
loc: Optional[SourceLoc] = field(default_factory=determine_source_loc)

def doc(self) -> str:
return f"{self.stmt};"
return with_pos_attribute(f"{self.stmt};", self.loc)


@dataclass
Expand Down Expand Up @@ -515,6 +563,7 @@ class Invoke(Control):
ref_cells: List[Tuple[str, CompVar]] = field(default_factory=list)
comb_group: Optional[CompVar] = None
attributes: List[Tuple[str, int]] = field(default_factory=list)
loc: Optional[SourceLoc] = field(default_factory=determine_source_loc)

def doc(self) -> str:
inv = f"invoke {self.id.doc()}"
Expand All @@ -539,7 +588,7 @@ def doc(self) -> str:
inv += f" with {self.comb_group.doc()}"
inv += ";"

return inv
return with_pos_attribute(inv, self.loc)

def with_attr(self, key: str, value: int) -> Invoke:
self.attributes.append((key, value))
Expand Down
Loading