1 /*
2  * Copyright (c) 2012 Stephen Williams (steve@icarus.com)
3  *
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.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 # include  "entity.h"
21 # include  "architec.h"
22 # include  "expression.h"
23 # include  <ivl_assert.h>
24 # include  <fstream>
25 # include  <iomanip>
26 # include  <typeinfo>
27 
28 using namespace std;
29 
dump(ostream & out,int indent) const30 void ExpArithmetic::dump(ostream&out, int indent) const
31 {
32       const char*fun_name = "?";
33       switch (fun_) {
34 	  case PLUS:
35 	    fun_name = "+";
36 	    break;
37 	  case MINUS:
38 	    fun_name = "-";
39 	    break;
40 	  case MULT:
41 	    fun_name = "*";
42 	    break;
43 	  case DIV:
44 	    fun_name = "/";
45 	    break;
46 	  case MOD:
47 	    fun_name = "mod";
48 	    break;
49 	  case REM:
50 	    fun_name = "rem";
51 	    break;
52 	  case POW:
53 	    fun_name = "**";
54 	    break;
55 	  case xCONCAT:
56 	    ivl_assert(*this, 0);
57 	    break;
58       }
59 
60       out << setw(indent) << "" << "Arithmetic " << fun_name
61 	  << " at " << get_fileline() << endl;
62       dump_operands(out, indent+4);
63 }
64 
dump(ostream & out,int indent) const65 void ExpConcat::dump(ostream&out, int indent) const
66 {
67       out << setw(indent) << "" << "Concatenation at " << get_fileline() << endl;
68       operand1_->dump(out, indent);
69       operand2_->dump(out, indent);
70 }
71 
dump(ostream & out,int indent) const72 void ExpCast::dump(ostream&out, int indent) const
73 {
74       out << setw(indent) << "" << "Casting ";
75       base_->dump(out, indent+4);
76       out << " to ";
77       type_->emit_def(out, empty_perm_string);
78 }
79 
dump(ostream & out,int indent) const80 void ExpNew::dump(ostream&out, int indent) const
81 {
82       out << setw(indent) << "" << "New dynamic array size: " << endl;
83       size_->dump(out, indent);
84 }
85 
dump(ostream & out,int indent) const86 void ExpScopedName::dump(ostream&out, int indent) const
87 {
88     out << setw(indent) << "" << "Scoped name expression: " << endl;
89     out << "    scope " << scope_name_ << " " << scope_ << endl;
90     name_->dump(out, indent+4);
91 }
92 
dump(ostream & out,int indent) const93 void ExpShift::dump(ostream&out, int indent) const
94 {
95       const char*fun_name = "?";
96       switch (shift_) {
97 	  case SRL:
98 	    fun_name = "srl";
99 	    break;
100 	  case SLL:
101 	    fun_name = "sll";
102 	    break;
103 	  case SLA:
104 	    fun_name = "sla";
105 	    break;
106 	  case SRA:
107 	    fun_name = "sra";
108 	    break;
109 	  case ROR:
110 	    fun_name = "ror";
111 	    break;
112 	  case ROL:
113 	    fun_name = "rol";
114 	    break;
115       }
116 
117       out << setw(indent) << "" << "Shift " << fun_name
118 	  << " at " << get_fileline() << endl;
119       dump_operands(out, indent+4);
120 }
121 
dump(ostream & out,int indent) const122 void ExpString::dump(ostream&out, int indent) const
123 {
124     out << setw(indent) << "" << "String \"" << value_;
125     out << "\"" << " at " << get_fileline() << endl;
126 }
127 
dump(ostream & out,int indent) const128 void ExpUAbs::dump(ostream&out, int indent) const
129 {
130       out << setw(indent) << "" << "abs() at " << get_fileline() << endl;
131       dump_operand1(out, indent+4);
132 }
133 
dump_operand1(ostream & out,int indent) const134 void ExpUnary::dump_operand1(ostream&out, int indent) const
135 {
136       operand1_->dump(out, indent);
137 }
138 
dump(ostream & out,int indent) const139 void ExpUNot::dump(ostream&out, int indent) const
140 {
141       out << setw(indent) << "" << "not() at " << get_fileline() << endl;
142       dump_operand1(out, indent+4);
143 }
144 
dump(ostream & out,int indent) const145 void ExpUMinus::dump(ostream&out, int indent) const
146 {
147       out << setw(indent) << "" << "unary_minus() at " << get_fileline() << endl;
148       dump_operand1(out, indent+4);
149 }
150 
dump(ostream & out,int indent) const151 void ExpTime::dump(ostream&out, int indent) const
152 {
153       out << setw(indent) << "" << "Time ";
154       write_to_stream(out);
155 }
156 
dump(ostream & out,int indent) const157 void ExpRange::dump(ostream&out, int indent) const
158 {
159       out << setw(indent) << "" << "Range ";
160       write_to_stream(out);
161 }
162 
dump(ostream & out,int indent) const163 void ExpDelay::dump(ostream&out, int indent) const
164 {
165       out << setw(indent) << "" << "Expression ";
166       expr_->write_to_stream(out);
167       out << " delayed by ";
168       delay_->write_to_stream(out);
169 }
170