Commit 6d968e95 authored by Tristan Gingold's avatar Tristan Gingold

sourcefiles: simplify DepRelation.

parent 1d55f716
...@@ -34,10 +34,6 @@ class DepRelation(object): ...@@ -34,10 +34,6 @@ class DepRelation(object):
"""Class used to create instances representing HDL dependency relations""" """Class used to create instances representing HDL dependency relations"""
# direction
PROVIDE = 1
USE = 2
# rel_type # rel_type
ENTITY = 1 ENTITY = 1
PACKAGE = 2 PACKAGE = 2
...@@ -45,37 +41,32 @@ class DepRelation(object): ...@@ -45,37 +41,32 @@ class DepRelation(object):
ARCHITECTURE = 4 ARCHITECTURE = 4
MODULE = ENTITY MODULE = ENTITY
def __init__(self, obj_name, direction, rel_type): def __init__(self, obj_name, lib_name, rel_type):
assert direction in [DepRelation.PROVIDE, DepRelation.USE]
assert rel_type in [ assert rel_type in [
DepRelation.ENTITY, DepRelation.ENTITY,
DepRelation.PACKAGE, DepRelation.PACKAGE,
DepRelation.INCLUDE, DepRelation.INCLUDE,
DepRelation.ARCHITECTURE, DepRelation.ARCHITECTURE,
DepRelation.MODULE] DepRelation.MODULE]
self.direction = direction
self.rel_type = rel_type self.rel_type = rel_type
self.obj_name = obj_name.lower() self.obj_name = obj_name.lower()
self.lib_name = None if lib_name is None else lib_name.lower()
def satisfies(self, rel_b): def satisfies(self, rel_b):
"""Check if the current dependency relation matches the provided one""" """Check if the current dependency relation matches the provided one"""
if (rel_b.direction == DepRelation.PROVIDE or return (rel_b.rel_type == self.rel_type
self.direction == DepRelation.USE): and rel_b.obj_name == self.obj_name
return False and rel_b.lib_name == self.lib_name)
if rel_b.rel_type == self.rel_type and rel_b.obj_name == self.obj_name:
return True
return False
def __repr__(self): def __repr__(self):
dstr = {self.USE: "Use", self.PROVIDE: "Provide"}
ostr = { ostr = {
self.ENTITY: "entity", self.ENTITY: "entity",
self.PACKAGE: "package", self.PACKAGE: "package",
self.INCLUDE: "include/header", self.INCLUDE: "include/header",
self.ARCHITECTURE: "architecture", self.ARCHITECTURE: "architecture",
self.MODULE: "module"} self.MODULE: "module"}
return "%s %s '%s'" % (dstr[self.direction], return "%s '%s.%s'" % (ostr[self.rel_type],
ostr[self.rel_type], self.lib_name or '',
self.obj_name) self.obj_name)
def __hash__(self): def __hash__(self):
...@@ -154,12 +145,10 @@ class DepFile(File): ...@@ -154,12 +145,10 @@ class DepFile(File):
def add_require(self, rel): def add_require(self, rel):
"""Add dependency :param rel:""" """Add dependency :param rel:"""
assert rel.direction == DepRelation.USE
self.requires.add(rel) self.requires.add(rel)
def add_provide(self, rel): def add_provide(self, rel):
"""Add provide :param rel:""" """Add provide :param rel:"""
assert rel.direction == DepRelation.PROVIDE
self.provides.add(rel) self.provides.add(rel)
def satisfies(self, rel_b): def satisfies(self, rel_b):
......
...@@ -68,7 +68,6 @@ def solve(fileset, standard_libs=None): ...@@ -68,7 +68,6 @@ def solve(fileset, standard_libs=None):
# logging.info("- relation: %s" % rel) # logging.info("- relation: %s" % rel)
# logging.info("- direction: %s" % rel.direction) # logging.info("- direction: %s" % rel.direction)
# Only analyze USE relations, we are looking for dependencies # Only analyze USE relations, we are looking for dependencies
assert rel.direction == DepRelation.USE
satisfied_by = set() satisfied_by = set()
for dep_file in fset: for dep_file in fset:
if dep_file.satisfies(rel): if dep_file.satisfies(rel):
...@@ -88,7 +87,6 @@ def solve(fileset, standard_libs=None): ...@@ -88,7 +87,6 @@ def solve(fileset, standard_libs=None):
required_lib = rel.obj_name.split('.')[0] required_lib = rel.obj_name.split('.')[0]
if (not standard_libs is None and if (not standard_libs is None and
required_lib in standard_libs and required_lib in standard_libs and
rel.direction is DepRelation.USE and
rel.rel_type is DepRelation.PACKAGE): rel.rel_type is DepRelation.PACKAGE):
logging.debug("Not satisfied relation %s in %s will " logging.debug("Not satisfied relation %s in %s will "
"be covered by the target compiler " "be covered by the target compiler "
...@@ -134,14 +132,9 @@ def make_dependency_set(fileset, top_level_entity, extra_modules=None): ...@@ -134,14 +132,9 @@ def make_dependency_set(fileset, top_level_entity, extra_modules=None):
""" Check if :param test_file: provides the entity pointed by :param entity_name:""" """ Check if :param test_file: provides the entity pointed by :param entity_name:"""
if entity_name == None: if entity_name == None:
return False return False
entity_rel_vhdl = DepRelation( entity_rel = DepRelation(entity_name, "work", DepRelation.MODULE)
entity_name,
DepRelation.PROVIDE, DepRelation.ENTITY)
entity_rel_vlog = DepRelation(
"%s.%s" %
("work", entity_name), DepRelation.PROVIDE, DepRelation.MODULE)
for rel in test_file.provides: for rel in test_file.provides:
if (rel == entity_rel_vhdl) or (rel == entity_rel_vlog): if rel == entity_rel:
return True return True
return False return False
......
...@@ -66,22 +66,15 @@ class VHDLParser(DepParser): ...@@ -66,22 +66,15 @@ class VHDLParser(DepParser):
use_pattern in the VHDL code -- group() returns positive matches use_pattern in the VHDL code -- group() returns positive matches
as indexed plain strings. It adds the found USE relations to the as indexed plain strings. It adds the found USE relations to the
file""" file"""
if text.group(1).lower() == "work": lib_name = text.group(1).lower()
logging.debug("use package %s.%s", pkg_name = text.group(2).lower()
dep_file.library, text.group(2)) if lib_name == "work":
dep_file.add_require( # Work is an alias for the current library
DepRelation("%s.%s" % (dep_file.library, text.group(2)), lib_name = dep_file.library
DepRelation.USE, logging.debug("use package %s.%s", lib_name, pkg_name)
DepRelation.PACKAGE)) dep_file.add_require(
else: DepRelation(pkg_name, lib_name, DepRelation.PACKAGE))
logging.debug("use package %s.%s", return "<hdlmake use_pattern %s.%s>" % (lib_name, pkg_name)
text.group(1), text.group(2))
dep_file.add_require(
DepRelation("%s.%s" % (text.group(1), text.group(2)),
DepRelation.USE,
DepRelation.PACKAGE))
return "<hdlmake use_pattern %s.%s>" % (text.group(1),
text.group(2))
buf = re.sub(use_pattern, do_use, buf) buf = re.sub(use_pattern, do_use, buf)
# new entity # new entity
entity_pattern = re.compile( entity_pattern = re.compile(
...@@ -93,14 +86,11 @@ class VHDLParser(DepParser): ...@@ -93,14 +86,11 @@ class VHDLParser(DepParser):
entity_pattern in the VHDL code -- group() returns positive matches entity_pattern in the VHDL code -- group() returns positive matches
as indexed plain strings. It adds the found PROVIDE relations as indexed plain strings. It adds the found PROVIDE relations
to the file""" to the file"""
logging.debug("found entity %s.%s", ent_name = text.group(1)
dep_file.library, text.group(1)) logging.debug("found entity %s.%s", dep_file.library, ent_name)
dep_file.add_provide( dep_file.add_provide(
DepRelation("%s.%s" % (dep_file.library, text.group(1)), DepRelation(ent_name, dep_file.library, DepRelation.ENTITY))
DepRelation.PROVIDE, return "<hdlmake entity_pattern %s.%s>" % (dep_file.library, ent_name)
DepRelation.ENTITY))
return "<hdlmake entity_pattern %s.%s>" % (dep_file.library,
text.group(1))
buf = re.sub(entity_pattern, do_entity, buf) buf = re.sub(entity_pattern, do_entity, buf)
...@@ -114,16 +104,14 @@ class VHDLParser(DepParser): ...@@ -114,16 +104,14 @@ class VHDLParser(DepParser):
architecture_pattern in the VHDL code -- group() returns positive architecture_pattern in the VHDL code -- group() returns positive
matches as indexed plain strings. It adds the found PROVIDE matches as indexed plain strings. It adds the found PROVIDE
relations to the file""" relations to the file"""
arch_name = text.group(1)
ent_name = text.group(2)
logging.debug("found architecture %s of entity %s.%s", logging.debug("found architecture %s of entity %s.%s",
text.group(1), dep_file.library, text.group(2)) arch_name, dep_file.library, ent_name)
dep_file.add_provide( dep_file.add_provide(
DepRelation("%s.%s" % (dep_file.library, text.group(2)), DepRelation(ent_name, dep_file.library, DepRelation.ARCHITECTURE))
DepRelation.PROVIDE,
DepRelation.ARCHITECTURE))
dep_file.add_require( dep_file.add_require(
DepRelation("%s.%s" % (dep_file.library, text.group(2)), DepRelation(ent_name, dep_file.library, DepRelation.ENTITY))
DepRelation.USE,
DepRelation.ENTITY))
return "<hdlmake architecture %s.%s>" % (dep_file.library, return "<hdlmake architecture %s.%s>" % (dep_file.library,
text.group(2)) text.group(2))
...@@ -139,14 +127,11 @@ class VHDLParser(DepParser): ...@@ -139,14 +127,11 @@ class VHDLParser(DepParser):
package_pattern in the VHDL code -- group() returns positive package_pattern in the VHDL code -- group() returns positive
matches as indexed plain strings. It adds the found PROVIDE matches as indexed plain strings. It adds the found PROVIDE
relations to the file""" relations to the file"""
logging.debug("found package %s.%s", dep_file.library, pkg_name = text.group(1)
text.group(1)) logging.debug("found package %s.%s", dep_file.library, pkg_name)
dep_file.add_provide( dep_file.add_provide(
DepRelation("%s.%s" % (dep_file.library, text.group(1)), DepRelation(pkg_name, dep_file.library, DepRelation.PACKAGE))
DepRelation.PROVIDE, return "<hdlmake package %s.%s>" % (dep_file.library, pkg_name)
DepRelation.PACKAGE))
return "<hdlmake package %s.%s>" % (dep_file.library,
text.group(1))
buf = re.sub(package_pattern, do_package, buf) buf = re.sub(package_pattern, do_package, buf)
# component declaration # component declaration
...@@ -160,10 +145,6 @@ class VHDLParser(DepParser): ...@@ -160,10 +145,6 @@ class VHDLParser(DepParser):
matches as indexed plain strings. It doesn't add any relation matches as indexed plain strings. It doesn't add any relation
to the file""" to the file"""
logging.debug("found component declaration %s", text.group(1)) logging.debug("found component declaration %s", text.group(1))
#dep_file.add_relation(
# DepRelation("%s.%s" % (dep_file.library, text.group(1)),
# DepRelation.USE,
# DepRelation.ENTITY))
return "<hdlmake component %s>" % text.group(1) return "<hdlmake component %s>" % text.group(1)
buf = re.sub(component_pattern, do_component, buf) buf = re.sub(component_pattern, do_component, buf)
...@@ -249,15 +230,12 @@ class VHDLParser(DepParser): ...@@ -249,15 +230,12 @@ class VHDLParser(DepParser):
relations to the file""" relations to the file"""
logging.debug("-> instantiates %s.%s(%s) as %s", logging.debug("-> instantiates %s.%s(%s) as %s",
text.group("LIB"), text.group("ENTITY"), text.group("ARCH"), text.group("LABEL")) text.group("LIB"), text.group("ENTITY"), text.group("ARCH"), text.group("LABEL"))
lib = text.group("LIB") lib_name = text.group("LIB")
if not lib or lib == "work": if not lib_name or lib_name == "work":
lib = dep_file.library lib_name = dep_file.library
dep_file.add_require(DepRelation( ent_name = text.group("ENTITY")
"%s.%s" % (lib, text.group("ENTITY")), dep_file.add_require(DepRelation(ent_name, lib_name, DepRelation.ENTITY))
DepRelation.USE, DepRelation.ENTITY)) return "<hdlmake instance %s|%s|%s>" % (text.group("LABEL"), lib_name, ent_name)
return "<hdlmake instance %s|%s|%s>" % (text.group("LABEL"),
lib,
text.group("ENTITY"))
buf = re.sub(instance_pattern, do_instance, buf) buf = re.sub(instance_pattern, do_instance, buf)
# libraries # libraries
......
...@@ -526,11 +526,11 @@ class VerilogParser(DepParser): ...@@ -526,11 +526,11 @@ class VerilogParser(DepParser):
import_pattern in the Verilog code -- group() returns positive import_pattern in the Verilog code -- group() returns positive
matches as indexed plain strings. It adds the found USE matches as indexed plain strings. It adds the found USE
relations to the file""" relations to the file"""
pkg_name = text.group(1)
logging.debug("file %s imports/uses %s.%s package", logging.debug("file %s imports/uses %s.%s package",
dep_file.path, dep_file.library, text.group(1)) dep_file.path, dep_file.library, pkg_name)
dep_file.add_require( dep_file.add_require(
DepRelation("%s.%s" % (dep_file.library, text.group(1)), DepRelation(pkg_name, dep_file.library, DepRelation.PACKAGE))
DepRelation.USE, DepRelation.PACKAGE))
import_pattern.subn(do_imports, buf) import_pattern.subn(do_imports, buf)
# packages # packages
m_inside_package = re.compile( m_inside_package = re.compile(
...@@ -542,11 +542,10 @@ class VerilogParser(DepParser): ...@@ -542,11 +542,10 @@ class VerilogParser(DepParser):
m_inside_pattern in the Verilog code -- group() returns positive m_inside_pattern in the Verilog code -- group() returns positive
matches as indexed plain strings. It adds the found PROVIDE matches as indexed plain strings. It adds the found PROVIDE
relations to the file""" relations to the file"""
logging.debug("found pacakge %s.%s", dep_file.library, pkg_name = text.group(1)
text.group(1)) logging.debug("found pacakge %s.%s", dep_file.library, pkg_name)
dep_file.add_provide( dep_file.add_provide(
DepRelation("%s.%s" % (dep_file.library, text.group(1)), DepRelation(pkg_name, dep_file.library, DepRelation.PACKAGE))
DepRelation.PROVIDE, DepRelation.PACKAGE))
m_inside_package.subn(do_package, buf) m_inside_package.subn(do_package, buf)
# modules and instantiations # modules and instantiations
...@@ -566,11 +565,10 @@ class VerilogParser(DepParser): ...@@ -566,11 +565,10 @@ class VerilogParser(DepParser):
m_inside_module in the Verilog code -- group() returns m_inside_module in the Verilog code -- group() returns
positive matches as indexed plain strings. It adds the found positive matches as indexed plain strings. It adds the found
PROVIDE relations to the file""" PROVIDE relations to the file"""
logging.debug("found module %s.%s", dep_file.library, module_name = text.group(1)
text.group(1)) logging.debug("found module %s.%s", dep_file.library, module_name)
dep_file.add_provide( dep_file.add_provide(
DepRelation("%s.%s" % (dep_file.library, text.group(1)), DepRelation(module_name, dep_file.library, DepRelation.MODULE))
DepRelation.PROVIDE, DepRelation.MODULE))
def do_inst(text): def do_inst(text):
"""Function to be applied by re.sub to every match of the """Function to be applied by re.sub to every match of the
...@@ -579,20 +577,17 @@ class VerilogParser(DepParser): ...@@ -579,20 +577,17 @@ class VerilogParser(DepParser):
relations to the file""" relations to the file"""
mod_name = text.group(1) mod_name = text.group(1)
if mod_name in self.reserved_words: if mod_name in self.reserved_words:
# A gate (and, or, ...)
return return
logging.debug("-> instantiates %s.%s as %s", logging.debug("-> instantiates %s.%s as %s",
dep_file.library, text.group(1), text.group(2)) dep_file.library, mod_name, text.group(2))
dep_file.add_require( dep_file.add_require(
DepRelation("%s.%s" % (dep_file.library, text.group(1)), DepRelation(mod_name, dep_file.library, DepRelation.MODULE))
DepRelation.USE, DepRelation.MODULE))
for stmt in [x for x in m_stmt.split(text.group(2)) if x and x[-1] == ")"]: for stmt in [x for x in m_stmt.split(text.group(2)) if x and x[-1] == ")"]:
match = m_instantiation.match(stmt) match = m_instantiation.match(stmt)
if match: if match:
do_inst(match) do_inst(match)
m_inside_module.subn(do_module, buf) m_inside_module.subn(do_module, buf)
dep_file.add_provide( dep_file.add_provide(
DepRelation( DepRelation(dep_file.path, None, DepRelation.INCLUDE))
dep_file.path,
DepRelation.PROVIDE,
DepRelation.INCLUDE))
dep_file.is_parsed = True dep_file.is_parsed = True
...@@ -49,10 +49,9 @@ class XCIParser(DepParser): ...@@ -49,10 +49,9 @@ class XCIParser(DepParser):
nsmap = dict(xmlnsre.findall(xml)) nsmap = dict(xmlnsre.findall(xml))
value = ET.fromstring(xml).find('spirit:componentInstances/spirit:componentInstance/spirit:instanceName', nsmap) value = ET.fromstring(xml).find('spirit:componentInstances/spirit:componentInstance/spirit:instanceName', nsmap)
if not value is None: if not value is None:
modulename = value.text module_name = value.text
logging.debug("found module %s.%s", dep_file.library, modulename) logging.debug("found module %s.%s", dep_file.library, module_name)
dep_file.add_provide( dep_file.add_provide(
DepRelation("%s.%s" % (dep_file.library, modulename), DepRelation(module_name, dep_file.library, DepRelation.MODULE))
DepRelation.PROVIDE, DepRelation.MODULE))
dep_file.is_parsed = True dep_file.is_parsed = True
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment