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

sourcefiles: simplify DepRelation.

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