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
|
/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2021 gatecat <gatecat@ds0.me>
*
* 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 "util.h"
#include <iterator>
NEXTPNR_NAMESPACE_BEGIN
namespace {
struct QsfOption
{
std::string name; // name, excluding the initial '-'
int arg_count; // number of arguments that follow the option
bool required; // error out if this option isn't passed
};
typedef dict<std::string, std::vector<std::string>> option_map_t;
struct QsfCommand
{
std::string name; // name of the command
std::vector<QsfOption> options; // list of "-options"
int pos_arg_count; // number of positional arguments expected to follow the command, -1 for any
std::function<void(Context *ctx, const option_map_t &options, const std::vector<std::string> &pos_args)> func;
};
void set_location_assignment_cmd(Context *ctx, const option_map_t &options, const std::vector<std::string> &pos_args)
{
ctx->io_attr[ctx->id(options.at("to").at(0))][id_LOC] = pos_args.at(0);
}
void set_instance_assignment_cmd(Context *ctx, const option_map_t &options, const std::vector<std::string> &pos_args)
{
ctx->io_attr[ctx->id(options.at("to").at(0))][ctx->id(options.at("name").at(0))] = pos_args.at(0);
}
void set_global_assignment_cmd(Context *ctx, const option_map_t &options, const std::vector<std::string> &pos_args)
{
// TODO
}
static const std::vector<QsfCommand> commands = {
{"set_location_assignment", {{"to", 1, true}}, 1, set_location_assignment_cmd},
{"set_instance_assignment",
{{"to", 1, true}, {"name", 1, true}, {"section_id", 1, false}},
1,
set_instance_assignment_cmd},
{"set_global_assignment",
{{"name", 1, true}, {"section_id", 1, false}, {"rise", 0, false}, {"fall", 0, false}},
1,
set_global_assignment_cmd},
};
struct QsfParser
{
std::string buf;
int pos = 0;
int lineno = 0;
Context *ctx;
QsfParser(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');
}
// We need to distinguish between quoted and unquoted strings, the former don't count as options
struct StringVal
{
std::string str;
bool is_quoted = false;
};
inline StringVal get_str()
{
StringVal s;
skip_blank(false);
if (eof())
return {"", false};
bool in_quotes = false, in_braces = false, escaped = false;
char c = get();
if (c == '"') {
in_quotes = true;
s.is_quoted = true;
} else if (c == '{') {
in_braces = true;
s.is_quoted = true;
} else {
s.str += c;
}
while (!eof()) {
char c = peek();
if (!in_quotes && !in_braces && !escaped && (std::isblank(c) || c == '\n' || c == '\r')) {
break;
}
get();
if (escaped) {
s.str += c;
escaped = false;
} else if ((in_quotes && c == '"') || (in_braces && c == '}')) {
break;
} else if (c == '\\') {
escaped = true;
} else {
s.str += c;
}
}
return s;
}
std::vector<StringVal> get_arguments()
{
std::vector<StringVal> args;
while (!skip_check_eol()) {
args.push_back(get_str());
}
skip_blank(true);
return args;
}
void evaluate(const std::vector<StringVal> &args)
{
if (args.empty())
return;
auto cmd_name = args.at(0).str;
auto fnd_cmd =
std::find_if(commands.begin(), commands.end(), [&](const QsfCommand &c) { return c.name == cmd_name; });
if (fnd_cmd == commands.end()) {
log_warning("Ignoring unknown command '%s' (line %d)\n", cmd_name.c_str(), lineno);
return;
}
option_map_t opt;
std::vector<std::string> pos_args;
for (size_t i = 1; i < args.size(); i++) {
auto arg = args.at(i);
if (arg.str.at(0) == '-' && !arg.is_quoted) {
for (auto &opt_data : fnd_cmd->options) {
if (arg.str.compare(1, std::string::npos, opt_data.name) != 0)
continue;
opt[opt_data.name]; // create empty entry, even if 0 arguments
for (int j = 0; j < opt_data.arg_count; j++) {
++i;
if (i >= args.size())
log_error("Unexpected end of argument list to option '%s' (line %d)\n", arg.str.c_str(),
lineno);
opt[opt_data.name].push_back(args.at(i).str);
}
goto done;
}
log_error("Unknown option '%s' to command '%s' (line %d)\n", arg.str.c_str(), cmd_name.c_str(), lineno);
done:;
} else {
// positional argument
pos_args.push_back(arg.str);
}
}
// Check positional argument count
if (int(pos_args.size()) != fnd_cmd->pos_arg_count && fnd_cmd->pos_arg_count != -1) {
log_error("Expected %d positional arguments to command '%s', got %d (line %d)\n", fnd_cmd->pos_arg_count,
cmd_name.c_str(), int(pos_args.size()), lineno);
}
// Check required options
for (auto &opt_data : fnd_cmd->options) {
if (opt_data.required && !opt.count(opt_data.name))
log_error("Missing required option '%s' to command '%s' (line %d)\n", opt_data.name.c_str(),
cmd_name.c_str(), lineno);
}
// Execute
fnd_cmd->func(ctx, opt, pos_args);
}
void operator()()
{
while (!eof()) {
skip_blank(true);
auto args = get_arguments();
evaluate(args);
}
}
};
}; // namespace
void Arch::read_qsf(std::istream &in)
{
std::string buf(std::istreambuf_iterator<char>(in), {});
QsfParser(buf, getCtx())();
}
NEXTPNR_NAMESPACE_END
|