2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 * A very simple and straightforward frontend for the RTLIL text
21 * representation (as generated by the 'ilang' backend).
27 #include "ilang_frontend.h"
29 namespace ILANG_FRONTEND {
30 RTLIL::Design *current_design;
31 RTLIL::Module *current_module;
32 RTLIL::Wire *current_wire;
33 RTLIL::Memory *current_memory;
34 RTLIL::Cell *current_cell;
35 RTLIL::Process *current_process;
36 std::vector<std::vector<RTLIL::SwitchRule*>*> switch_stack;
37 std::vector<RTLIL::CaseRule*> case_stack;
38 std::map<RTLIL::IdString, RTLIL::Const> attrbuf;
40 using namespace ILANG_FRONTEND;
45 %name-prefix "rtlil_frontend_ilang_yy"
50 YOSYS_NAMESPACE_PREFIX RTLIL::Const *data;
51 YOSYS_NAMESPACE_PREFIX RTLIL::SigSpec *sigspec;
54 %token <string> TOK_ID TOK_VALUE TOK_STRING
55 %token <integer> TOK_INT
56 %token TOK_AUTOIDX TOK_MODULE TOK_WIRE TOK_WIDTH TOK_INPUT TOK_OUTPUT TOK_INOUT
57 %token TOK_CELL TOK_CONNECT TOK_SWITCH TOK_CASE TOK_ASSIGN TOK_SYNC
58 %token TOK_LOW TOK_HIGH TOK_POSEDGE TOK_NEGEDGE TOK_EDGE TOK_ALWAYS TOK_INIT
59 %token TOK_UPDATE TOK_PROCESS TOK_END TOK_INVALID TOK_EOL TOK_OFFSET
60 %token TOK_PARAMETER TOK_ATTRIBUTE TOK_MEMORY TOK_SIZE TOK_SIGNED TOK_UPTO
62 %type <sigspec> sigspec sigspec_list
63 %type <integer> sync_type
75 if (attrbuf.size() != 0)
76 rtlil_frontend_ilang_yyerror("dangling attribute");
83 optional_eol TOK_EOL | /* empty */;
92 TOK_MODULE TOK_ID EOL {
93 if (current_design->modules_.count($2) != 0)
94 rtlil_frontend_ilang_yyerror(stringf("ilang error: redefinition of module %s.", $2).c_str());
95 current_module = new RTLIL::Module;
96 current_module->name = $2;
97 current_module->attributes = attrbuf;
98 current_design->modules_[$2] = current_module;
101 } module_body TOK_END {
102 if (attrbuf.size() != 0)
103 rtlil_frontend_ilang_yyerror("dangling attribute");
107 module_body module_stmt |
111 attr_stmt | wire_stmt | memory_stmt | cell_stmt | proc_stmt | conn_stmt;
114 TOK_ATTRIBUTE TOK_ID constant EOL {
121 TOK_AUTOIDX TOK_INT EOL {
122 autoidx = std::max(autoidx, $2);
127 current_wire = current_module->addWire("$__ilang_frontend_tmp__");
128 current_wire->attributes = attrbuf;
130 } wire_options TOK_ID EOL {
131 if (current_module->wires_.count($4) != 0)
132 rtlil_frontend_ilang_yyerror(stringf("ilang error: redefinition of wire %s.", $4).c_str());
133 current_module->rename(current_wire, $4);
138 wire_options TOK_WIDTH TOK_INT {
139 current_wire->width = $3;
141 wire_options TOK_UPTO {
142 current_wire->upto = true;
144 wire_options TOK_OFFSET TOK_INT {
145 current_wire->start_offset = $3;
147 wire_options TOK_INPUT TOK_INT {
148 current_wire->port_id = $3;
149 current_wire->port_input = true;
150 current_wire->port_output = false;
152 wire_options TOK_OUTPUT TOK_INT {
153 current_wire->port_id = $3;
154 current_wire->port_input = false;
155 current_wire->port_output = true;
157 wire_options TOK_INOUT TOK_INT {
158 current_wire->port_id = $3;
159 current_wire->port_input = true;
160 current_wire->port_output = true;
166 current_memory = new RTLIL::Memory;
167 current_memory->attributes = attrbuf;
169 } memory_options TOK_ID EOL {
170 if (current_module->memories.count($4) != 0)
171 rtlil_frontend_ilang_yyerror(stringf("ilang error: redefinition of memory %s.", $4).c_str());
172 current_memory->name = $4;
173 current_module->memories[$4] = current_memory;
178 memory_options TOK_WIDTH TOK_INT {
179 current_memory->width = $3;
181 memory_options TOK_SIZE TOK_INT {
182 current_memory->size = $3;
187 TOK_CELL TOK_ID TOK_ID EOL {
188 if (current_module->cells_.count($3) != 0)
189 rtlil_frontend_ilang_yyerror(stringf("ilang error: redefinition of cell %s.", $3).c_str());
190 current_cell = current_module->addCell($3, $2);
191 current_cell->attributes = attrbuf;
195 } cell_body TOK_END EOL;
198 cell_body TOK_PARAMETER TOK_ID constant EOL {
199 current_cell->parameters[$3] = *$4;
203 cell_body TOK_PARAMETER TOK_SIGNED TOK_ID constant EOL {
204 current_cell->parameters[$4] = *$5;
205 current_cell->parameters[$4].flags |= RTLIL::CONST_FLAG_SIGNED;
209 cell_body TOK_CONNECT TOK_ID sigspec EOL {
210 if (current_cell->has($3))
211 rtlil_frontend_ilang_yyerror(stringf("ilang error: redefinition of cell port %s.", $3).c_str());
212 current_cell->set($3, *$4);
219 TOK_PROCESS TOK_ID EOL {
220 if (current_module->processes.count($2) != 0)
221 rtlil_frontend_ilang_yyerror(stringf("ilang error: redefinition of process %s.", $2).c_str());
222 current_process = new RTLIL::Process;
223 current_process->name = $2;
224 current_process->attributes = attrbuf;
225 current_module->processes[$2] = current_process;
226 switch_stack.clear();
227 switch_stack.push_back(¤t_process->root_case.switches);
229 case_stack.push_back(¤t_process->root_case);
232 } case_body sync_list TOK_END EOL;
235 attr_list TOK_SWITCH sigspec EOL {
236 RTLIL::SwitchRule *rule = new RTLIL::SwitchRule;
238 rule->attributes = attrbuf;
239 switch_stack.back()->push_back(rule);
242 } switch_body TOK_END EOL;
249 switch_body TOK_CASE {
250 RTLIL::CaseRule *rule = new RTLIL::CaseRule;
251 switch_stack.back()->back()->cases.push_back(rule);
252 switch_stack.push_back(&rule->switches);
253 case_stack.push_back(rule);
254 } compare_list EOL case_body {
255 switch_stack.pop_back();
256 case_stack.pop_back();
262 case_stack.back()->compare.push_back(*$1);
265 compare_list ',' sigspec {
266 case_stack.back()->compare.push_back(*$3);
272 switch_stmt case_body |
273 assign_stmt case_body |
277 TOK_ASSIGN sigspec sigspec EOL {
278 case_stack.back()->actions.push_back(RTLIL::SigSig(*$2, *$3));
284 sync_list TOK_SYNC sync_type sigspec EOL {
285 RTLIL::SyncRule *rule = new RTLIL::SyncRule;
286 rule->type = RTLIL::SyncType($3);
288 current_process->syncs.push_back(rule);
291 sync_list TOK_SYNC TOK_ALWAYS EOL {
292 RTLIL::SyncRule *rule = new RTLIL::SyncRule;
293 rule->type = RTLIL::SyncType::STa;
294 rule->signal = RTLIL::SigSpec();
295 current_process->syncs.push_back(rule);
297 sync_list TOK_SYNC TOK_INIT EOL {
298 RTLIL::SyncRule *rule = new RTLIL::SyncRule;
299 rule->type = RTLIL::SyncType::STi;
300 rule->signal = RTLIL::SigSpec();
301 current_process->syncs.push_back(rule);
306 TOK_LOW { $$ = RTLIL::ST0; } |
307 TOK_HIGH { $$ = RTLIL::ST1; } |
308 TOK_POSEDGE { $$ = RTLIL::STp; } |
309 TOK_NEGEDGE { $$ = RTLIL::STn; } |
310 TOK_EDGE { $$ = RTLIL::STe; };
313 update_list TOK_UPDATE sigspec sigspec EOL {
314 current_process->syncs.back()->actions.push_back(RTLIL::SigSig(*$3, *$4));
323 int width = strtol($1, &ep, 10);
324 std::list<RTLIL::State> bits;
325 while (*(++ep) != 0) {
326 RTLIL::State bit = RTLIL::Sx;
328 case '0': bit = RTLIL::S0; break;
329 case '1': bit = RTLIL::S1; break;
330 case 'x': bit = RTLIL::Sx; break;
331 case 'z': bit = RTLIL::Sz; break;
332 case '-': bit = RTLIL::Sa; break;
333 case 'm': bit = RTLIL::Sm; break;
335 bits.push_front(bit);
337 if (bits.size() == 0)
338 bits.push_back(RTLIL::Sx);
339 while ((int)bits.size() < width) {
340 RTLIL::State bit = bits.back();
341 if (bit == RTLIL::S1)
345 while ((int)bits.size() > width)
347 $$ = new RTLIL::Const;
348 for (auto it = bits.begin(); it != bits.end(); it++)
349 $$->bits.push_back(*it);
353 $$ = new RTLIL::Const($1, 32);
356 $$ = new RTLIL::Const($1);
362 $$ = new RTLIL::SigSpec(*$1);
366 if (current_module->wires_.count($1) == 0)
367 rtlil_frontend_ilang_yyerror(stringf("ilang error: wire %s not found", $1).c_str());
368 $$ = new RTLIL::SigSpec(current_module->wires_[$1]);
371 TOK_ID '[' TOK_INT ']' {
372 if (current_module->wires_.count($1) == 0)
373 rtlil_frontend_ilang_yyerror(stringf("ilang error: wire %s not found", $1).c_str());
374 $$ = new RTLIL::SigSpec(current_module->wires_[$1], $3);
377 TOK_ID '[' TOK_INT ':' TOK_INT ']' {
378 if (current_module->wires_.count($1) == 0)
379 rtlil_frontend_ilang_yyerror(stringf("ilang error: wire %s not found", $1).c_str());
380 $$ = new RTLIL::SigSpec(current_module->wires_[$1], $5, $3 - $5 + 1);
383 '{' sigspec_list '}' {
388 sigspec_list sigspec {
389 $$ = new RTLIL::SigSpec;
396 $$ = new RTLIL::SigSpec;
400 TOK_CONNECT sigspec sigspec EOL {
401 if (attrbuf.size() != 0)
402 rtlil_frontend_ilang_yyerror("dangling attribute");
403 current_module->connect(*$2, *$3);