Debian Codegraph @

himbaechel/uarch/xilinx/xdc.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2019-2023 gatecat <[email protected]>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include <boost/algorithm/string.hpp>
21 #include <fstream>
22 #include <regex>
23
24 #include "extra_data.h"
25 #include "himbaechel_api.h"
26 #include "log.h"
27 #include "nextpnr.h"
28 #include "util.h"
29
30 #include "xilinx.h"
31
32 #define HIMBAECHEL_CONSTIDS "uarch/xilinx/constids.inc"
33 #include "himbaechel_constids.h"
34
35 NEXTPNR_NAMESPACE_BEGIN
36
37 void XilinxImpl::parse_xdc(const std::string &filename)
38 {
39 std::ifstream in(filename);
40 if (!in)
41 log_error("failed to open XDC file '%s'\n", filename.c_str());
42 log_info("Parsing XDC file...\n");
43 std::string line;
44 std::string linebuf;
45 int lineno = 0;
46 unsigned num_errors = 0;
47
48 auto isempty = [](const std::string &str) {
49 return std::all_of(str.begin(), str.end(), [](char c) { return std::isspace(c); });
50 };
51 auto strip_quotes = [](const std::string &str) {
52 if (str.at(0) == '"') {
53 NPNR_ASSERT(str.back() == '"');
54 return str.substr(1, str.size() - 2);
55 } else if (str.at(0) == '{') {
56 NPNR_ASSERT(str.back() == '}');
57 return str.substr(1, str.size() - 2);
58 } else {
59 return str;
60 }
61 };
62 auto split_to_args = [](const std::string &str, bool group_brackets) {
63 std::vector<std::string> split_args;
64 std::string buffer;
65 auto flush = [&]() {
66 if (!buffer.empty())
67 split_args.push_back(buffer);
68 buffer.clear();
69 };
70 int brcount = 0;
71 for (char c : str) {
72 if ((c == '[' || c == '{') && group_brackets) {
73 ++brcount;
74 }
75 if ((c == ']' || c == '}') && group_brackets) {
76 --brcount;
77 buffer += c;
78 if (brcount == 0)
79 flush();
80 continue;
81 }
82 if (std::isspace(c)) {
83 if (brcount == 0) {
84 flush();
85 continue;
86 }
87 }
88 buffer += c;
89 }
90 flush();
91 return split_args;
92 };
93
94 auto get_cells = [&](std::string str) {
95 std::vector<CellInfo *> tgt_cells;
96 if (str.empty() || str.front() != '[')
97 log_error("failed to parse target (on line %d)\n", lineno);
98 str = str.substr(1, str.size() - 2);
99 auto split = split_to_args(str, false);
100 if (split.size() < 1)
101 log_error("failed to parse target (on line %d)\n", lineno);
102 if (split.front() != "get_ports")
103 log_error("targets other than 'get_ports' are not supported (on line %d)\n", lineno);
104 if (split.size() < 2)
105 log_error("failed to parse target (on line %d)\n", lineno);
106 IdString cellname = ctx->id(strip_quotes(split.at(1)));
107 if (ctx->cells.count(cellname))
108 tgt_cells.push_back(ctx->cells.at(cellname).get());
109 return tgt_cells;
110 };
111
112 auto get_nets = [&](std::string str) {
113 std::vector<NetInfo *> tgt_nets;
114 if (str.empty())
115 return tgt_nets;
116 if (str.front() != '[' || str.back() != ']')
117 log_error("failed to parse target '%s' (on line %d)\n", str.c_str(), lineno);
118 str = str.substr(1, str.size() - 2);
119 auto split = split_to_args(str, false);
120 if (split.size() < 1)
121 log_error("failed to parse target (on line %d)\n", lineno);
122 if (split.front() != "get_ports" && split.front() != "get_nets")
123 log_error("targets other than 'get_ports' or 'get_nets' are not supported (on line %d)\n", lineno);
124 if (split.size() < 2)
125 log_error("failed to parse target (on line %d)\n", lineno);
126 str = strip_quotes(split.at(1));
127 if (str.empty())
128 return tgt_nets;
129 IdString netname = ctx->id(str);
130 NetInfo *maybe_net = ctx->getNetByAlias(netname);
131 if (maybe_net != nullptr) {
132 tgt_nets.push_back(maybe_net);
133 return tgt_nets;
134 }
135 // Also test the lowercase variant, for better interoperability with synthesis tools
136 boost::algorithm::to_lower(str);
137 netname = ctx->id(str);
138 maybe_net = ctx->getNetByAlias(netname);
139 if (maybe_net != nullptr)
140 tgt_nets.push_back(maybe_net);
141 return tgt_nets;
142 };
143
144 while (std::getline(in, line)) {
145 ++lineno;
146 // Trim comments, from # until end of the line
147 size_t cstart = line.find('#');
148 if (cstart != std::string::npos)
149 line = line.substr(0, cstart);
150 if (isempty(line))
151 continue;
152
153 std::vector<std::string> arguments = split_to_args(line, true);
154 if (arguments.empty())
155 continue;
156 std::string &cmd = arguments.front();
157
158 if (cmd == "set_property") {
159 std::vector<std::pair<std::string, std::string>> arg_pairs;
160 if (arguments.size() < 4) {
161 log_nonfatal_error("expected at least four arguments to 'set_property' (on line %d)\n", lineno);
162 num_errors++;
163 goto nextline;
164 } else if (arguments.at(1) == "-dict") {
165 std::vector<std::string> dict_args = split_to_args(strip_quotes(arguments.at(2)), false);
166 if ((dict_args.size() % 2) != 0) {
167 log_nonfatal_error("expected an even number of argument for dictionary (on line %d)\n", lineno);
168 num_errors++;
169 goto nextline;
170 }
171 arg_pairs.reserve(dict_args.size() / 2);
172 for (int cursor = 0; cursor + 1 < int(dict_args.size()); cursor += 2) {
173 arg_pairs.emplace_back(std::move(dict_args.at(cursor)), std::move(dict_args.at(cursor + 1)));
174 }
175 } else {
176 arg_pairs.emplace_back(std::move(arguments.at(1)), std::move(arguments.at(2)));
177 }
178 // Warning : ug835 has lowercase example, so probably supporting lowercase too is needed
179 if (arg_pairs.size() == 1 && arg_pairs.front().first == "INTERNAL_VREF") { // get_iobanks not supported
180 log_warning("INTERNAL_VREF isn't supported, ignoring (on line %d)\n", lineno);
181 continue;
182 }
183 if (arguments.at(3).size() > 2 && arguments.at(3) == "[current_design]") {
184 log_warning("[current_design] isn't supported, ignoring (on line %d)\n", lineno);
185 continue;
186 }
187 // All remaining arguments are supposed to designate cells
188 std::vector<CellInfo *> dest;
189 for (int cursor = 3; cursor < int(arguments.size()); cursor++) {
190 std::vector<CellInfo *> dest_loc = get_cells(arguments.at(cursor));
191 if (dest_loc.empty())
192 log_warning("found set_property with no cells matching '%s' (on line %d)\n",
193 arguments.at(cursor).c_str(), lineno);
194 dest.insert(dest.end(), dest_loc.begin(), dest_loc.end());
195 }
196 for (auto c : dest) {
197 for (const auto &pair : arg_pairs) {
198 IdString id_prop = ctx->id(pair.first);
199 if (ctx->debug)
200 log_info("applying property '%s' = '%s' to cell '%s' (on line %d)\n", pair.first.c_str(),
201 pair.second.c_str(), c->name.c_str(ctx), lineno);
202 if (c->attrs.find(id_prop) != c->attrs.end()) {
203 log_nonfatal_error("found multiple properties '%s' for cell '%s' (on line %d)\n",
204 pair.first.c_str(), c->name.c_str(ctx), lineno);
205 num_errors++;
206 }
207 c->attrs[id_prop] = std::string(pair.second);
208 }
209 }
210 } else if (cmd == "create_clock") {
211 double period = 0;
212 bool got_period = false;
213 int cursor = 1;
214 for (cursor = 1; cursor < int(arguments.size()); cursor++) {
215 std::string opt = arguments.at(cursor);
216 if (opt == "-add") {
217 log_warning("ignoring unsupported XDC option '%s' (on line %d)\n", opt.c_str(), lineno);
218 } else if (opt == "-name" || opt == "-waveform") {
219 log_warning("ignoring unsupported XDC option '%s' (on line %d)\n", opt.c_str(), lineno);
220 cursor++;
221 } else if (opt == "-period") {
222 cursor++;
223 period = std::stod(arguments.at(cursor));
224 got_period = true;
225 } else
226 break;
227 }
228 if (!got_period) {
229 log_nonfatal_error("found create_clock without period (on line %d)\n", lineno);
230 num_errors++;
231 goto nextline;
232 }
233 // All remaining arguments are supposed to designate ports/nets
234 std::vector<NetInfo *> dest;
235 if (cursor >= int(arguments.size()))
236 log_warning("found create_clock without designated nets (on line %d)\n", lineno);
237 for (; cursor < (int)arguments.size(); cursor++) {
238 std::vector<NetInfo *> dest_loc = get_nets(arguments.at(cursor));
239 if (dest_loc.empty())
240 log_warning("found create_clock with no nets matching '%s' (on line %d)\n",
241 arguments.at(cursor).c_str(), lineno);
242 dest.insert(dest.end(), dest_loc.begin(), dest_loc.end());
243 }
244 for (auto n : dest) {
245 if (ctx->debug)
246 log_info("applying clock period constraint on net '%s' (on line %d)\n", n->name.c_str(ctx), lineno);
247 if (n->clkconstr.get() != nullptr) {
248 log_nonfatal_error("found multiple clock constraints on net '%s' (on line %d)\n",
249 n->name.c_str(ctx), lineno);
250 num_errors++;
251 }
252 n->clkconstr = std::unique_ptr<ClockConstraint>(new ClockConstraint);
253 n->clkconstr->period = DelayPair(ctx->getDelayFromNS(period));
254 n->clkconstr->high = DelayPair(ctx->getDelayFromNS(period / 2));
255 n->clkconstr->low = DelayPair(ctx->getDelayFromNS(period / 2));
256 }
257 } else {
258 log_warning("ignoring unsupported XDC command '%s' (on line %d)\n", cmd.c_str(), lineno);
259 }
260
261 nextline:; // Phony statement to have something legal after the label
262 }
263 if (!isempty(linebuf)) {
264 log_nonfatal_error("unexpected end of XDC file\n");
265 num_errors++;
266 }
267 if (num_errors > 0) {
268 log_error("Stopping the program after %u errors found in XDC file\n", num_errors);
269 }
270 }
271
272 NEXTPNR_NAMESPACE_END