Skip to content

Commit

Permalink
Fix bug in handling of tuple names
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Oct 11, 2019
1 parent 762507b commit 49478a6
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 24 deletions.
45 changes: 21 additions & 24 deletions magma/backend/coreir_.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..backend.verilog import find
from ..logging import error
import coreir
from ..ref import ArrayRef, DefnRef, TupleRef
from ..ref import ArrayRef, DefnRef, TupleRef, InstRef
from ..passes import InstanceGraphPass
from ..t import In
import logging
Expand Down Expand Up @@ -45,31 +45,28 @@ def __missing__(self, key):
ret = self[key] = self.default_factory(key)
return ret

def get_top_name(name):
if isinstance(name, TupleRef):
return get_top_name(name.tuple.name)
if isinstance(name, ArrayRef):
return get_top_name(name.array.name)
return name

def name_to_coreir_select(name):
if isinstance(name, InstRef):
return name.inst.name + "." + str(name.name)
elif isinstance(name, DefnRef):
return "self." + name.name
elif isinstance(name, ArrayRef):
return name_to_coreir_select(name.array.name) + "." + str(name.index)
elif isinstance(name, TupleRef):
index = name.index
try:
int(index)
index = f"_{index}"
except ValueError:
pass
return name_to_coreir_select(name.tuple.name) + "." + index
else:
raise NotImplementedError(name)


def magma_port_to_coreir(port):
select = repr(port)

name = port.name
if isinstance(name, TupleRef):
# Prefix integer indexes for unnamed tuples (e.g. 0, 1, 2) with "_"
if name.index.isdigit():
select = select.split(".")
select[-1] = "_" + select[-1]
select = ".".join(select)
name = get_top_name(name)
if isinstance(name, DefnRef):
if name.defn.name != "":
select_list = select.split(".")
select_list[0] = "self"
select = ".".join(select_list)

return select.replace("[", ".").replace("]", "")
return name_to_coreir_select(port.name)

# Singleton context meant to be used with coreir/magma code
@singleton
Expand Down
16 changes: 16 additions & 0 deletions tests/test_coreir/gold/test_anon_tuple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{"top":"global.Foo",
"namespaces":{
"global":{
"modules":{
"Foo":{
"type":["Record",[
["ifc",["Record",[["_0","BitIn"],["_1","Bit"]]]]
]],
"connections":[
["self.ifc._1","self.ifc._0"]
]
}
}
}
}
}
17 changes: 17 additions & 0 deletions tests/test_coreir/gold/test_anon_tuple_nested_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{"top":"global.Foo",
"namespaces":{
"global":{
"modules":{
"Foo":{
"type":["Record",[
["ifc",["Record",[["_0",["Array",2,"BitIn"]],["_1",["Array",2,"Bit"]]]]]
]],
"connections":[
["self.ifc._1.1","self.ifc._0.0"],
["self.ifc._1.0","self.ifc._0.1"]
]
}
}
}
}
}
17 changes: 17 additions & 0 deletions tests/test_coreir/gold/test_nested_anon_tuple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{"top":"global.Foo",
"namespaces":{
"global":{
"modules":{
"Foo":{
"type":["Record",[
["ifc",["Array",2,["Record",[["_0","BitIn"],["_1","Bit"]]]]]
]],
"connections":[
["self.ifc.1._1","self.ifc.0._0"],
["self.ifc.1._0","self.ifc.0._1"]
]
}
}
}
}
}
29 changes: 29 additions & 0 deletions tests/test_coreir/test_coreir_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,34 @@ def test_array_nesting():
assert check_files_equal(__file__, f"build/test_array_nesting.json",
f"gold/test_array_nesting.json")


def test_anon_tuple():
foo = m.DefineCircuit("Foo", "ifc", m.Tuple(m.In(m.Bit), m.Out(m.Bit)))
m.wire(foo.ifc[0], foo.ifc[1])
m.EndCircuit()
m.compile("build/test_anon_tuple", foo, output="coreir")
assert check_files_equal(__file__, f"build/test_anon_tuple.json",
f"gold/test_anon_tuple.json")


def test_nested_anon_tuple():
foo = m.DefineCircuit("Foo", "ifc", m.Array[2, m.Tuple(m.In(m.Bit), m.Out(m.Bit))])
m.wire(foo.ifc[0][0], foo.ifc[1][1])
m.wire(foo.ifc[1][0], foo.ifc[0][1])
m.EndCircuit()
m.compile("build/test_nested_anon_tuple", foo, output="coreir")
assert check_files_equal(__file__, f"build/test_nested_anon_tuple.json",
f"gold/test_nested_anon_tuple.json")


def test_anon_tuple_nested_array():
foo = m.DefineCircuit("Foo", "ifc", m.Tuple(m.In(m.Bits[2]), m.Out(m.Bits[2])))
m.wire(foo.ifc[0][0], foo.ifc[1][1])
m.wire(foo.ifc[0][1], foo.ifc[1][0])
m.EndCircuit()
m.compile("build/test_anon_tuple_nested_array", foo, output="coreir")
assert check_files_equal(__file__, f"build/test_anon_tuple_nested_array.json",
f"gold/test_anon_tuple_nested_array.json")

if __name__ == "__main__":
test_multi_direction_tuple()

0 comments on commit 49478a6

Please sign in to comment.