Skip to content

Commit

Permalink
Move logic into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Oct 23, 2019
1 parent afe5d86 commit 14f8d92
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions magma/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,35 +489,38 @@ def is_definition(self):
def instances(self):
return self._instances

def inspect_name(cls, inst):
# Try to fetch instance name
with open(inst.debug_info.filename, "r") as f:
line = f.read().splitlines()[inst.debug_info.lineno - 1]
tree = ast.parse(textwrap.dedent(line)).body[0]
# Simple case when <Name> = <Instance>()
if isinstance(tree, ast.Assign) and len(tree.targets) == 1 \
and isinstance(tree.targets[0], ast.Name):
name = tree.targets[0].id
# Handle case when we've seen a name multiple times
# (e.g. reused inside a loop)
if cls.instance_name_counter[name] == 0:
inst.name = name
cls.instance_name_counter[name] += 1
else:
if cls.instance_name_counter[name] == 1:
# Append `_0` to the first instance with this
# name
orig = cls.instance_name_map[name]
orig.name += "_0"
del cls.instance_name_map[name]
cls.instance_name_map[orig.name] = orig
inst.name = f"{name}_{cls.instance_name_counter[name]}"
cls.instance_name_counter[name] += 1

#
# place a circuit instance in this definition
#
def place(cls, inst):
if not inst.name:
if get_debug_mode():
# Try to fetch instance name
with open(inst.debug_info.filename, "r") as f:
line = f.read().splitlines()[inst.debug_info.lineno - 1]
tree = ast.parse(textwrap.dedent(line)).body[0]
# Simple case when <Name> = <Instance>()
if isinstance(tree, ast.Assign) and len(tree.targets) == 1 \
and isinstance(tree.targets[0], ast.Name):
name = tree.targets[0].id
# Handle case when we've seen a name multiple times
# (e.g. reused inside a loop)
if cls.instance_name_counter[name] == 0:
inst.name = name
cls.instance_name_counter[name] += 1
else:
if cls.instance_name_counter[name] == 1:
# Append `_0` to the first instance with this
# name
orig = cls.instance_name_map[name]
orig.name += "_0"
del cls.instance_name_map[name]
cls.instance_name_map[orig.name] = orig
inst.name = f"{name}_{cls.instance_name_counter[name]}"
cls.instance_name_counter[name] += 1
cls.inspect_name(inst)
if not inst.name:
# Default name if we could not find one or debug mode is off
inst.name = f"{type(inst).name}_inst{str(cls.instanced_circuits_counter[type(inst).name])}"
Expand Down

0 comments on commit 14f8d92

Please sign in to comment.