OpenASIP  2.0
IPXact.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2017 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file IPXact.cc
26  *
27  * @author Lasse Lehtonen 2017 (lasse.lehtonen-no.spam-tut.fi)
28  */
29 #include "IPXact.hh"
30 
31 #include <boost/optional.hpp>
32 #include <boost/property_tree/ptree.hpp>
33 #include <boost/property_tree/xml_parser.hpp>
34 #include <boost/regex.hpp>
35 
36 namespace ipxact {
37 
38 BusInfo
39 parseBus(std::string file) {
40  BusInfo bus;
41  using boost::property_tree::ptree;
42 
43  ptree pt;
44  read_xml(file, pt);
45 
46  bus.name = pt.get_child(
47  "ipxact:abstractionDefinition.ipxact:busType"
48  ".<xmlattr>.name")
49  .data();
50  ptree ports = pt.get_child(
51  "ipxact:abstractionDefinition."
52  "ipxact:ports");
53 
54  for (auto&& p : ports) {
55  if (p.first == "ipxact:port") {
56  std::string name =
57  p.second.get_child("ipxact:logicalName").data();
58  std::string width = p.second
59  .get_child(
60  "ipxact:wire."
61  "ipxact:onMaster.ipxact:width")
62  .data();
63  std::string direction =
64  p.second
65  .get_child(
66  "ipxact:wire."
67  "ipxact:onMaster.ipxact:direction")
68  .data();
69  std::string left = width + "-1";
70  std::string right = "0";
71  std::string defaultValue =
72  direction == "out"
73  ? p.second.get_child("ipxact:wire.ipxact:defaultValue")
74  .data()
75  : "";
76 
77  bus.ports.emplace_back(Port{
78  name, direction, true, left, right, width, defaultValue});
79  }
80  }
81 
82  return bus;
83 }
84 
85 ModuleInfo
86 parseComponent(std::string file) {
87  ModuleInfo module;
88  using boost::property_tree::ptree;
89 
90  ptree pt;
91  read_xml(file, pt);
92  boost::optional<ptree&> parameters =
93  pt.get_child_optional("ipxact:component.ipxact:parameters");
94  ptree ports = pt.get_child("ipxact:component.ipxact:model.ipxact:ports");
95  module.name = pt.get_child("ipxact:component.ipxact:name").data();
96 
97  // get parameters if any.
98  if (parameters) {
99  for (auto&& p : *parameters) {
100  if (p.first == "ipxact:parameter") {
101  std::string id =
102  p.second.get_child("<xmlattr>.parameterId").data();
103  std::string type =
104  p.second.get_child("<xmlattr>.type").data();
105  std::string name = p.second.get_child("ipxact:name").data();
106  std::string value = p.second.get_child("ipxact:value").data();
107  Parameter parameter = {id, type, name, value};
108  module.parameters.emplace_back(parameter);
109  }
110  }
111  }
112 
113  // get ports
114  for (auto&& p : ports) {
115  if (p.first == "ipxact:port") {
116  std::string name = p.second.get_child("ipxact:name").data();
117  std::string direction =
118  p.second.get_child("ipxact:wire.ipxact:direction").data();
119  std::transform(
120  direction.begin(), direction.end(), direction.begin(),
121  ::tolower);
122  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
123  bool vector = false;
124  std::string left = "0";
125  std::string right = "0";
126  std::string width = "0";
127  ptree wire = p.second.get_child("ipxact:wire");
128  for (auto&& w : wire) {
129  if (w.first == "ipxact:vectors") {
130  vector = true;
131  left = w.second.get_child("ipxact:vector.ipxact:left")
132  .data();
133  width = left;
134  right = w.second.get_child("ipxact:vector.ipxact:right")
135  .data();
136  }
137  }
138  // replace uuids in left and right.
139  for (auto&& m : module.parameters) {
140  boost::regex expr("\\b" + m.id + "\\b");
141  width = boost::regex_replace(width, expr, m.value);
142  left = boost::regex_replace(left, expr, m.name);
143  right = "0"; // boost::regex_replace(right, expr, m.name);
144  }
145  width += "+1";
146 
147  module.ports.emplace_back(
148  Port{name, direction, vector, left, right, width, ""});
149  }
150  }
151 
152  return module;
153 }
154 } // namespace ipxact
ipxact::BusInfo
Definition: IPXact.hh:59
ipxact::parseBus
BusInfo parseBus(std::string file)
Definition: IPXact.cc:39
ipxact::BusInfo::name
std::string name
Definition: IPXact.hh:60
ipxact::Port
Definition: IPXact.hh:43
ipxact::ModuleInfo::ports
std::vector< Port > ports
Definition: IPXact.hh:56
ipxact::ModuleInfo::name
std::string name
Definition: IPXact.hh:54
IPXact.hh
ipxact::BusInfo::ports
std::vector< Port > ports
Definition: IPXact.hh:61
ipxact
Definition: IPXact.cc:36
ipxact::ModuleInfo
Definition: IPXact.hh:53
ipxact::Parameter
Definition: IPXact.hh:36
ipxact::parseComponent
ModuleInfo parseComponent(std::string file)
Definition: IPXact.cc:86
ipxact::ModuleInfo::parameters
std::vector< Parameter > parameters
Definition: IPXact.hh:55