1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2020 gatecat <gatecat@ds0.me>
* Copyright (C) 2024 rowanG077 <goemansrowan@gmail.com>
*
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "log.h"
#include "nextpnr.h"
#include <algorithm>
#include <iterator>
NEXTPNR_NAMESPACE_BEGIN
struct SdcEntity
{
enum EntityType
{
ENTITY_CELL,
ENTITY_PORT,
ENTITY_NET,
ENTITY_PIN,
} type;
IdString name;
IdString pin; // for cell pins only
SdcEntity(EntityType type, IdString name) : type(type), name(name) {}
SdcEntity(EntityType type, IdString name, IdString pin) : type(type), name(name), pin(pin) {}
const std::string &to_string(Context *ctx) { return name.str(ctx); }
CellInfo *get_cell(Context *ctx) const
{
if (type != ENTITY_CELL)
return nullptr;
return ctx->cells.at(name).get();
}
PortInfo *get_port(Context *ctx) const
{
if (type != ENTITY_PORT)
return nullptr;
return &ctx->ports.at(name);
}
NetInfo *get_net(Context *ctx) const
{
if (type == ENTITY_PIN) {
CellInfo *cell = nullptr;
if (ctx->cells.count(name)) {
cell = ctx->cells.at(name).get();
} else {
return nullptr;
}
if (!cell->ports.count(pin))
return nullptr;
return cell->ports.at(pin).net;
} else if (type == ENTITY_NET) {
return ctx->nets.at(name).get();
} else {
return nullptr;
}
}
};
struct SdcValue
{
SdcValue(const std::string &s) : is_string(true), str(s) {};
SdcValue(const std::vector<SdcEntity> &l) : is_string(false), list(l) {};
bool is_string;
std::string str; // simple string value
std::vector<SdcEntity> list; // list of entities
};
struct SDCParser
{
std::string buf;
int pos = 0;
int lineno = 1;
Context *ctx;
SDCParser(const std::string &buf, Context *ctx) : buf(buf), ctx(ctx) {};
inline bool eof() const { return pos == int(buf.size()); }
inline char peek() const { return buf.at(pos); }
inline char get()
{
char c = buf.at(pos++);
if (c == '\n')
++lineno;
return c;
}
std::string get(int n)
{
std::string s = buf.substr(pos, n);
pos += n;
return s;
}
// If next char matches c, take it from the stream and return true
bool check_get(char c)
{
if (peek() == c) {
get();
return true;
} else {
return false;
}
}
// If next char matches any in chars, take it from the stream and return true
bool check_get_any(const std::string &chrs)
{
char c = peek();
if (chrs.find(c) != std::string::npos) {
get();
return true;
} else {
return false;
}
}
inline void skip_blank(bool nl = false)
{
while (!eof() && check_get_any(nl ? " \t\n\r" : " \t"))
;
}
// Return true if end of line (or file)
inline bool skip_check_eol()
{
skip_blank(false);
if (eof())
return true;
char c = peek();
// Comments count as end of line
if (c == '#') {
get();
while (!eof() && peek() != '\n' && peek() != '\r')
get();
return true;
}
if (c == ';') {
// Forced end of line
get();
return true;
}
return (c == '\n' || c == '\r');
}
inline std::string get_str()
{
std::string s;
skip_blank(false);
if (eof())
return "";
bool in_quotes = false, in_braces = false, escaped = false;
char c = get();
if (c == '"')
in_quotes = true;
else if (c == '{')
in_braces = true;
else
s += c;
while (true) {
if (eof()) {
if (in_quotes || in_braces || escaped)
log_error("EOF while parsing string '%s'\n", s.c_str());
else
break;
}
char c = peek();
if (!in_quotes && !in_braces && !escaped && (std::isblank(c) || c == ']')) {
break;
}
get();
if (escaped) {
s += c;
escaped = false;
} else if ((in_quotes && c == '"') || (in_braces && c == '}')) {
break;
} else if (c == '\\') {
escaped = true;
} else {
s += c;
}
}
return s;
}
SdcValue evaluate(const std::vector<SdcValue> &arguments)
{
NPNR_ASSERT(!arguments.empty());
auto &arg0 = arguments.at(0);
NPNR_ASSERT(arg0.is_string);
const std::string &cmd = arg0.str;
if (cmd == "get_ports")
return cmd_get_ports(arguments);
else if (cmd == "get_cells")
return cmd_get_cells(arguments);
else if (cmd == "get_nets")
return cmd_get_nets(arguments);
else if (cmd == "get_pins")
return cmd_get_pins(arguments);
else if (cmd == "create_clock")
return cmd_create_clock(arguments);
else if (cmd == "set_false_path")
return cmd_set_false_path(arguments);
else
log_error("Unsupported SDC command '%s'\n", cmd.c_str());
}
std::vector<SdcValue> get_arguments()
{
std::vector<SdcValue> args;
while (!skip_check_eol()) {
if (check_get('[')) {
// Start of a sub-expression
auto result = evaluate(get_arguments());
NPNR_ASSERT(check_get(']'));
args.push_back(result);
} else if (peek() == ']') {
break;
} else {
args.push_back(get_str());
}
}
skip_blank(true);
return args;
}
SdcValue cmd_get_nets(const std::vector<SdcValue> &arguments)
{
std::vector<SdcEntity> nets;
for (int i = 1; i < int(arguments.size()); i++) {
auto &arg = arguments.at(i);
if (!arg.is_string)
log_error("get_nets expected string arguments (line %d)\n", lineno);
std::string s = arg.str;
if (s.at(0) == '-')
log_error("unsupported argument '%s' to get_nets (line %d)\n", s.c_str(), lineno);
IdString id = ctx->id(s);
if (ctx->nets.count(id) || ctx->net_aliases.count(id))
nets.emplace_back(SdcEntity::ENTITY_NET, ctx->net_aliases.count(id) ? ctx->net_aliases.at(id) : id);
else
log_warning("get_nets argument '%s' matched no objects.\n", s.c_str());
}
return nets;
}
SdcValue cmd_get_ports(const std::vector<SdcValue> &arguments)
{
std::vector<SdcEntity> ports;
for (int i = 1; i < int(arguments.size()); i++) {
auto &arg = arguments.at(i);
if (!arg.is_string)
log_error("get_ports expected string arguments (line %d)\n", lineno);
std::string s = arg.str;
if (s.at(0) == '-')
log_error("unsupported argument '%s' to get_ports (line %d)\n", s.c_str(), lineno);
IdString id = ctx->id(s);
if (ctx->ports.count(id))
ports.emplace_back(SdcEntity::ENTITY_PORT, id);
}
return ports;
}
SdcValue cmd_get_cells(const std::vector<SdcValue> &arguments)
{
std::vector<SdcEntity> cells;
for (int i = 1; i < int(arguments.size()); i++) {
auto &arg = arguments.at(i);
if (!arg.is_string)
log_error("get_cells expected string arguments (line %d)\n", lineno);
std::string s = arg.str;
if (s.at(0) == '-')
log_error("unsupported argument '%s' to get_cells (line %d)\n", s.c_str(), lineno);
IdString id = ctx->id(s);
if (ctx->cells.count(id))
cells.emplace_back(SdcEntity::ENTITY_CELL, id);
}
return cells;
}
SdcValue cmd_get_pins(const std::vector<SdcValue> &arguments)
{
std::vector<SdcEntity> pins;
for (int i = 1; i < int(arguments.size()); i++) {
auto &arg = arguments.at(i);
if (!arg.is_string)
log_error("get_pins expected string arguments (line %d)\n", lineno);
std::string s = arg.str;
if (s.at(0) == '-')
log_error("unsupported argument '%s' to get_pins (line %d)\n", s.c_str(), lineno);
auto pos = s.rfind('/');
if (pos == std::string::npos)
log_error("expected / in cell pin name '%s' (line %d)\n", s.c_str(), lineno);
pins.emplace_back(SdcEntity::ENTITY_PIN, ctx->id(s.substr(0, pos)), ctx->id(s.substr(pos + 1)));
if (pins.back().get_net(ctx) == nullptr) {
log_warning("cell pin '%s' not found\n", s.c_str());
pins.pop_back();
}
}
return pins;
}
SdcValue cmd_create_clock(const std::vector<SdcValue> &arguments)
{
float period = 10;
for (int i = 1; i < int(arguments.size()); i++) {
auto &arg = arguments.at(i);
if (arg.is_string) {
std::string s = arg.str;
if (s == "-period") {
i++;
auto &val = arguments.at(i);
if (!val.is_string)
log_error("expecting string argument to -period (line %d)\n", lineno);
try {
period = std::stof(val.str);
} catch (std::exception &e) {
log_error("invalid argument '%s' to -period (line %d)\n", val.str.c_str(), lineno);
}
} else if (s == "-name") {
i++;
} else {
log_error("unsupported argument '%s' to create_clock\n", s.c_str());
}
} else {
for (const auto &ety : arg.list) {
NetInfo *net = nullptr;
if (ety.type == SdcEntity::ENTITY_PIN)
net = ety.get_net(ctx);
else if (ety.type == SdcEntity::ENTITY_NET)
net = ctx->nets.at(ety.name).get();
else if (ety.type == SdcEntity::ENTITY_PORT)
net = ctx->ports.at(ety.name).net;
else
log_error("create_clock applies only to cells, cell pins, or IO ports (line %d)\n", lineno);
ctx->addClock(net->name, 1000.0f / period);
}
}
}
return std::string{};
}
SdcValue cmd_set_false_path(const std::vector<SdcValue> &arguments)
{
NetInfo *from = nullptr;
NetInfo *to = nullptr;
for (int i = 1; i < int(arguments.size()); i++) {
auto &arg = arguments.at(i);
if (arg.is_string) {
std::string s = arg.str;
bool is_from = true;
if (s == "-to") {
is_from = false;
} else if (s != "-from") {
log_error("expecting either -to or -from to set_false_path(line %d)\n", lineno);
}
i++;
auto &val = arguments.at(i);
if (val.is_string) {
log_error("expecting SdcValue argument to -from (line %d)\n", lineno);
}
if (val.list.size() != 1) {
log_error("Expected a single SdcEntity as argument to -to/-from (line %d)\n", lineno);
}
auto &ety = val.list.at(0);
NetInfo *net = nullptr;
if (ety.type == SdcEntity::ENTITY_PIN)
net = ety.get_net(ctx);
else if (ety.type == SdcEntity::ENTITY_NET)
net = ctx->nets.at(ety.name).get();
else if (ety.type == SdcEntity::ENTITY_PORT)
net = ctx->ports.at(ety.name).net;
else
log_error("set_false_path applies only to nets, cell pins, or IO ports (line %d)\n", lineno);
if (is_from) {
from = net;
} else {
to = net;
}
}
}
if (from == nullptr) {
log_error("-from is required for set_false_path (line %d)\n", lineno);
} else if (to == nullptr) {
log_error("-to is required for set_false_path (line %d)\n", lineno);
}
log_warning("set_false_path from: %s, to: %s does not do anything(yet).\n", from->name.c_str(ctx),
to->name.c_str(ctx));
return std::string{};
}
void operator()()
{
while (!eof()) {
skip_blank(true);
auto args = get_arguments();
if (args.empty())
continue;
evaluate(args);
}
}
};
void Context::read_sdc(std::istream &in)
{
std::string buf(std::istreambuf_iterator<char>(in), {});
SDCParser(buf, getCtx())();
}
NEXTPNR_NAMESPACE_END
|