Merge branch 'master' into verilog-ams
[sverilog.git] / emit.cc
blob079a41c842da1cb827e2f861ee48c687bcda66a1
1 /*
2 * Copyright (c) 1998-2008 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 # include "config.h"
22 # include <iostream>
25 * The emit function is called to generate the output required of the
26 * target.
28 # include "target.h"
29 # include "netlist.h"
30 # include <typeinfo>
31 # include <cassert>
32 # include <cstring>
34 bool NetNode::emit_node(struct target_t*tgt) const
36 cerr << "EMIT: Gate type? " << typeid(*this).name() << endl;
37 return false;
40 bool NetLogic::emit_node(struct target_t*tgt) const
42 tgt->logic(this);
43 return true;
46 bool NetUDP::emit_node(struct target_t*tgt) const
48 tgt->udp(this);
49 return true;
52 bool NetAbs::emit_node(struct target_t*tgt) const
54 tgt->lpm_abs(this);
55 return true;
58 bool NetAddSub::emit_node(struct target_t*tgt) const
60 tgt->lpm_add_sub(this);
61 return true;
64 bool NetArrayDq::emit_node(struct target_t*tgt) const
66 return tgt->lpm_array_dq(this);
69 bool NetCaseCmp::emit_node(struct target_t*tgt) const
71 tgt->net_case_cmp(this);
72 return true;
75 bool NetCLShift::emit_node(struct target_t*tgt) const
77 tgt->lpm_clshift(this);
78 return true;
81 bool NetCompare::emit_node(struct target_t*tgt) const
83 tgt->lpm_compare(this);
84 return true;
87 bool NetConcat::emit_node(struct target_t*tgt) const
89 return tgt->concat(this);
92 bool NetConst::emit_node(struct target_t*tgt) const
94 return tgt->net_const(this);
97 bool NetDivide::emit_node(struct target_t*tgt) const
99 tgt->lpm_divide(this);
100 return true;
103 bool NetFF::emit_node(struct target_t*tgt) const
105 tgt->lpm_ff(this);
106 return true;
109 bool NetLiteral::emit_node(struct target_t*tgt) const
111 return tgt->net_literal(this);
114 bool NetModulo::emit_node(struct target_t*tgt) const
116 tgt->lpm_modulo(this);
117 return true;
120 bool NetMult::emit_node(struct target_t*tgt) const
122 tgt->lpm_mult(this);
123 return true;
126 bool NetMux::emit_node(struct target_t*tgt) const
128 tgt->lpm_mux(this);
129 return true;
132 bool NetPartSelect::emit_node(struct target_t*tgt) const
134 return tgt->part_select(this);
137 bool NetPow::emit_node(struct target_t*tgt) const
139 tgt->lpm_pow(this);
140 return true;
143 bool NetReplicate::emit_node(struct target_t*tgt) const
145 return tgt->replicate(this);
148 bool NetSignExtend::emit_node(struct target_t*tgt) const
150 return tgt->sign_extend(this);
153 bool NetUReduce::emit_node(struct target_t*tgt) const
155 return tgt->ureduce(this);
158 bool NetSysFunc::emit_node(struct target_t*tgt) const
160 return tgt->net_sysfunction(this);
163 bool NetUserFunc::emit_node(struct target_t*tgt) const
165 return tgt->net_function(this);
168 bool NetTran::emit_node(struct target_t*tgt) const
170 return tgt->tran(this);
173 bool NetBUFZ::emit_node(struct target_t*tgt) const
175 return tgt->bufz(this);
178 bool NetProcTop::emit(struct target_t*tgt) const
180 return tgt->process(this);
183 bool NetProc::emit_proc(struct target_t*tgt) const
185 cerr << "EMIT: Proc type? " << typeid(*this).name() << endl;
186 return false;
189 bool NetAssign::emit_proc(struct target_t*tgt) const
191 tgt->proc_assign(this);
192 return true;
195 bool NetAssignNB::emit_proc(struct target_t*tgt) const
197 tgt->proc_assign_nb(this);
198 return true;
201 bool NetBlock::emit_proc(struct target_t*tgt) const
203 return tgt->proc_block(this);
206 bool NetCase::emit_proc(struct target_t*tgt) const
208 tgt->proc_case(this);
209 return true;
212 bool NetCAssign::emit_proc(struct target_t*tgt) const
214 return tgt->proc_cassign(this);
217 bool NetCondit::emit_proc(struct target_t*tgt) const
219 return tgt->proc_condit(this);
222 bool NetDeassign::emit_proc(struct target_t*tgt) const
224 return tgt->proc_deassign(this);
227 bool NetDisable::emit_proc(struct target_t*tgt) const
229 return tgt->proc_disable(this);
232 bool NetForce::emit_proc(struct target_t*tgt) const
234 return tgt->proc_force(this);
237 bool NetForever::emit_proc(struct target_t*tgt) const
239 tgt->proc_forever(this);
240 return true;
243 bool NetPDelay::emit_proc(struct target_t*tgt) const
245 return tgt->proc_delay(this);
248 bool NetPDelay::emit_proc_recurse(struct target_t*tgt) const
250 if (statement_) return statement_->emit_proc(tgt);
251 return true;
254 bool NetRelease::emit_proc(struct target_t*tgt) const
256 return tgt->proc_release(this);
259 bool NetRepeat::emit_proc(struct target_t*tgt) const
261 tgt->proc_repeat(this);
262 return true;
265 bool NetSTask::emit_proc(struct target_t*tgt) const
267 tgt->proc_stask(this);
268 return true;
271 bool NetUTask::emit_proc(struct target_t*tgt) const
273 tgt->proc_utask(this);
274 return true;
277 bool NetWhile::emit_proc(struct target_t*tgt) const
279 tgt->proc_while(this);
280 return true;
283 void NetBlock::emit_recurse(struct target_t*tgt) const
285 if (last_ == 0)
286 return;
288 NetProc*cur = last_;
289 do {
290 cur = cur->next_;
291 cur->emit_proc(tgt);
292 } while (cur != last_);
295 bool NetCondit::emit_recurse_if(struct target_t*tgt) const
297 if (if_)
298 return if_->emit_proc(tgt);
299 else
300 return true;
303 bool NetCondit::emit_recurse_else(struct target_t*tgt) const
305 if (else_)
306 return else_->emit_proc(tgt);
307 else
308 return true;
311 bool NetEvProbe::emit_node(struct target_t*tgt) const
313 tgt->net_probe(this);
314 return true;
317 bool NetEvTrig::emit_proc(struct target_t*tgt) const
319 return tgt->proc_trigger(this);
322 bool NetEvWait::emit_proc(struct target_t*tgt) const
324 return tgt->proc_wait(this);
327 bool NetEvWait::emit_recurse(struct target_t*tgt) const
329 if (!statement_) return true;
330 return statement_->emit_proc(tgt);
333 void NetForever::emit_recurse(struct target_t*tgt) const
335 if (statement_)
336 statement_->emit_proc(tgt);
339 void NetRepeat::emit_recurse(struct target_t*tgt) const
341 if (statement_)
342 statement_->emit_proc(tgt);
345 void NetScope::emit_scope(struct target_t*tgt) const
347 tgt->scope(this);
349 for (NetEvent*cur = events_ ; cur ; cur = cur->snext_)
350 tgt->event(cur);
352 for (NetScope*cur = sub_ ; cur ; cur = cur->sib_)
353 cur->emit_scope(tgt);
355 if (signals_) {
356 NetNet*cur = signals_->sig_next_;
357 do {
358 tgt->signal(cur);
359 cur = cur->sig_next_;
360 } while (cur != signals_->sig_next_);
362 /* Run the signals again, but this time to connect the
363 delay paths. This is done as a second pass because
364 the paths reference other signals that may be later
365 in the list. We can do it here becase delay paths are
366 always connected within the scope. */
367 cur = signals_->sig_next_;
368 do {
369 tgt->signal_paths(cur);
370 cur = cur->sig_next_;
371 } while (cur != signals_->sig_next_);
376 bool NetScope::emit_defs(struct target_t*tgt) const
378 bool flag = true;
380 switch (type_) {
381 case MODULE:
382 for (NetScope*cur = sub_ ; cur ; cur = cur->sib_)
383 flag &= cur->emit_defs(tgt);
384 break;
386 case FUNC:
387 flag &= tgt->func_def(this);
388 break;
389 case TASK:
390 tgt->task_def(this);
391 break;
392 default: /* BEGIN_END and FORK_JOIN, GENERATE... */
393 for (NetScope*cur = sub_ ; cur ; cur = cur->sib_)
394 flag &= cur->emit_defs(tgt);
395 break;
398 return flag;
401 void NetWhile::emit_proc_recurse(struct target_t*tgt) const
403 proc_->emit_proc(tgt);
406 int Design::emit(struct target_t*tgt) const
408 int rc = 0;
410 if (tgt->start_design(this) == false)
411 return -2;
413 // enumerate the scopes
414 for (list<NetScope*>::const_iterator scope = root_scopes_.begin();
415 scope != root_scopes_.end(); scope++)
416 (*scope)->emit_scope(tgt);
419 // emit nodes
420 bool nodes_rc = true;
421 if (nodes_) {
422 NetNode*cur = nodes_->node_next_;
423 do {
424 nodes_rc = nodes_rc && cur->emit_node(tgt);
425 cur = cur->node_next_;
426 } while (cur != nodes_->node_next_);
430 // emit task and function definitions
431 bool tasks_rc = true;
432 for (list<NetScope*>::const_iterator scope = root_scopes_.begin();
433 scope != root_scopes_.end(); scope++)
434 tasks_rc &= (*scope)->emit_defs(tgt);
437 // emit the processes
438 bool proc_rc = true;
439 for (const NetProcTop*idx = procs_ ; idx ; idx = idx->next_)
440 proc_rc &= idx->emit(tgt);
442 rc = tgt->end_design(this);
444 if (nodes_rc == false)
445 return -1;
446 if (tasks_rc == false)
447 return -2;
448 if (proc_rc == false)
449 return -3;
451 return rc;
454 void NetEBinary::expr_scan(struct expr_scan_t*tgt) const
456 tgt->expr_binary(this);
459 void NetEConcat::expr_scan(struct expr_scan_t*tgt) const
461 tgt->expr_concat(this);
464 void NetEConst::expr_scan(struct expr_scan_t*tgt) const
466 tgt->expr_const(this);
469 void NetEConstParam::expr_scan(struct expr_scan_t*tgt) const
471 tgt->expr_param(this);
474 void NetECReal::expr_scan(struct expr_scan_t*tgt) const
476 tgt->expr_creal(this);
479 void NetECRealParam::expr_scan(struct expr_scan_t*tgt) const
481 tgt->expr_rparam(this);
484 void NetEParam::expr_scan(struct expr_scan_t*tgt) const
486 cerr << get_fileline() << ":internal error: unexpected NetEParam."
487 << endl;
490 void NetEEvent::expr_scan(struct expr_scan_t*tgt) const
492 tgt->expr_event(this);
495 void NetEScope::expr_scan(struct expr_scan_t*tgt) const
497 tgt->expr_scope(this);
500 void NetESelect::expr_scan(struct expr_scan_t*tgt) const
502 tgt->expr_select(this);
505 void NetESFunc::expr_scan(struct expr_scan_t*tgt) const
507 tgt->expr_sfunc(this);
510 void NetEUFunc::expr_scan(struct expr_scan_t*tgt) const
512 tgt->expr_ufunc(this);
515 void NetESignal::expr_scan(struct expr_scan_t*tgt) const
517 tgt->expr_signal(this);
520 void NetETernary::expr_scan(struct expr_scan_t*tgt) const
522 tgt->expr_ternary(this);
525 void NetEUnary::expr_scan(struct expr_scan_t*tgt) const
527 tgt->expr_unary(this);
530 int emit(const Design*des, const char*type)
532 for (unsigned idx = 0 ; target_table[idx] ; idx += 1) {
533 const struct target*tgt = target_table[idx];
534 if (strcmp(tgt->name, type) == 0)
535 return des->emit(tgt->meth);
539 cerr << "error: Code generator type " << type
540 << " not found." << endl;
541 return -1;