1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // CodeGenerator.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Rob Schluntz
|
---|
12 | // Last Modified On : Thu Jun 11 13:22:39 2015
|
---|
13 | // Update Count : 137
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <algorithm>
|
---|
17 | #include <iostream>
|
---|
18 | #include <cassert>
|
---|
19 | #include <list>
|
---|
20 |
|
---|
21 | #include "SynTree/Type.h"
|
---|
22 | #include "SynTree/Declaration.h"
|
---|
23 | #include "SynTree/Statement.h"
|
---|
24 | #include "SynTree/Expression.h"
|
---|
25 | #include "SynTree/Initializer.h"
|
---|
26 |
|
---|
27 | #include "utility.h"
|
---|
28 | #include "UnimplementedError.h"
|
---|
29 |
|
---|
30 | #include "CodeGenerator.h"
|
---|
31 | #include "OperatorTable.h"
|
---|
32 | #include "GenType.h"
|
---|
33 |
|
---|
34 | using namespace std;
|
---|
35 |
|
---|
36 | namespace CodeGen {
|
---|
37 | int CodeGenerator::tabsize = 4;
|
---|
38 |
|
---|
39 | // the kinds of statements that would ideally be separated by more whitespace
|
---|
40 | bool wantSpacing( Statement * stmt) {
|
---|
41 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
|
---|
42 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
|
---|
43 | }
|
---|
44 |
|
---|
45 | ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
|
---|
46 | return output << string( cg.cur_indent, ' ' );
|
---|
47 | }
|
---|
48 |
|
---|
49 | ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
|
---|
50 | return indent( output );
|
---|
51 | }
|
---|
52 |
|
---|
53 | CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
|
---|
54 |
|
---|
55 | CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
|
---|
56 | : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
|
---|
57 | //output << std::string( init );
|
---|
58 | }
|
---|
59 |
|
---|
60 | CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
|
---|
61 | : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
|
---|
62 | //output << std::string( init );
|
---|
63 | }
|
---|
64 |
|
---|
65 | string mangleName( DeclarationWithType *decl ) {
|
---|
66 | if ( decl->get_mangleName() != "" ) {
|
---|
67 | return decl->get_mangleName();
|
---|
68 | } else {
|
---|
69 | return decl->get_name();
|
---|
70 | } // if
|
---|
71 | }
|
---|
72 |
|
---|
73 | //*** Declarations
|
---|
74 | void CodeGenerator::visit( FunctionDecl *functionDecl ) {
|
---|
75 | handleStorageClass( functionDecl );
|
---|
76 | if ( functionDecl->get_isInline() ) {
|
---|
77 | output << "inline ";
|
---|
78 | } // if
|
---|
79 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
|
---|
80 |
|
---|
81 | // how to get this to the Functype?
|
---|
82 | std::list< Declaration * > olds = functionDecl->get_oldDecls();
|
---|
83 | if ( ! olds.empty() ) {
|
---|
84 | output << " /* function has old declaration */";
|
---|
85 | } // if
|
---|
86 |
|
---|
87 | // acceptAll( functionDecl->get_oldDecls(), *this );
|
---|
88 | if ( functionDecl->get_statements() ) {
|
---|
89 | functionDecl->get_statements()->accept(*this );
|
---|
90 | } // if
|
---|
91 | }
|
---|
92 |
|
---|
93 | void CodeGenerator::visit( ObjectDecl *objectDecl ) {
|
---|
94 | handleStorageClass( objectDecl );
|
---|
95 | output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
|
---|
96 |
|
---|
97 | if ( objectDecl->get_init() ) {
|
---|
98 | output << " = ";
|
---|
99 | objectDecl->get_init()->accept( *this );
|
---|
100 | } // if
|
---|
101 | if ( objectDecl->get_bitfieldWidth() ) {
|
---|
102 | output << ":";
|
---|
103 | objectDecl->get_bitfieldWidth()->accept( *this );
|
---|
104 | } // if
|
---|
105 | }
|
---|
106 |
|
---|
107 | void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
|
---|
108 | if ( aggDecl->get_name() != "" )
|
---|
109 | output << aggDecl->get_name();
|
---|
110 |
|
---|
111 | std::list< Declaration * > &memb = aggDecl->get_members();
|
---|
112 |
|
---|
113 | if ( ! memb.empty() ) {
|
---|
114 | output << endl << indent << "{" << endl;
|
---|
115 |
|
---|
116 | cur_indent += CodeGenerator::tabsize;
|
---|
117 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
---|
118 | output << indent;
|
---|
119 | (*i)->accept(*this );
|
---|
120 | output << ";" << endl;
|
---|
121 | }
|
---|
122 |
|
---|
123 | cur_indent -= CodeGenerator::tabsize;
|
---|
124 |
|
---|
125 | output << indent << "}";
|
---|
126 | } // if
|
---|
127 | }
|
---|
128 |
|
---|
129 | void CodeGenerator::visit( StructDecl *structDecl ) {
|
---|
130 | output << "struct ";
|
---|
131 | handleAggregate( structDecl );
|
---|
132 | }
|
---|
133 |
|
---|
134 | void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
|
---|
135 | output << "union ";
|
---|
136 | handleAggregate( aggregateDecl );
|
---|
137 | }
|
---|
138 |
|
---|
139 | void CodeGenerator::visit( EnumDecl *aggDecl ) {
|
---|
140 | output << "enum ";
|
---|
141 |
|
---|
142 | if ( aggDecl->get_name() != "" )
|
---|
143 | output << aggDecl->get_name();
|
---|
144 |
|
---|
145 | std::list< Declaration* > &memb = aggDecl->get_members();
|
---|
146 |
|
---|
147 | if ( ! memb.empty() ) {
|
---|
148 | output << " {" << endl;
|
---|
149 |
|
---|
150 | cur_indent += CodeGenerator::tabsize;
|
---|
151 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
---|
152 | ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
|
---|
153 | assert( obj );
|
---|
154 | output << indent << mangleName( obj );
|
---|
155 | if ( obj->get_init() ) {
|
---|
156 | output << " = ";
|
---|
157 | obj->get_init()->accept(*this );
|
---|
158 | } // if
|
---|
159 | output << "," << endl;
|
---|
160 | } // for
|
---|
161 |
|
---|
162 | cur_indent -= CodeGenerator::tabsize;
|
---|
163 |
|
---|
164 | output << indent << "}";
|
---|
165 | } // if
|
---|
166 | }
|
---|
167 |
|
---|
168 | void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
|
---|
169 |
|
---|
170 | void CodeGenerator::visit( TypedefDecl *typeDecl ) {
|
---|
171 | output << "typedef ";
|
---|
172 | output << genType( typeDecl->get_base(), typeDecl->get_name() );
|
---|
173 | }
|
---|
174 |
|
---|
175 | void CodeGenerator::visit( TypeDecl *typeDecl ) {
|
---|
176 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
|
---|
177 | // still to be done
|
---|
178 | output << "extern unsigned long " << typeDecl->get_name();
|
---|
179 | if ( typeDecl->get_base() ) {
|
---|
180 | output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
|
---|
181 | } // if
|
---|
182 | }
|
---|
183 |
|
---|
184 | void CodeGenerator::visit( SingleInit *init ) {
|
---|
185 | init->get_value()->accept( *this );
|
---|
186 | }
|
---|
187 |
|
---|
188 | void CodeGenerator::visit( ListInit *init ) {
|
---|
189 | output << "{ ";
|
---|
190 | genCommaList( init->begin_initializers(), init->end_initializers() );
|
---|
191 | output << " }";
|
---|
192 | }
|
---|
193 |
|
---|
194 | void CodeGenerator::visit( Constant *constant ) {
|
---|
195 | output << constant->get_value() ;
|
---|
196 | }
|
---|
197 |
|
---|
198 | //*** Expressions
|
---|
199 | void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
|
---|
200 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
|
---|
201 | OperatorInfo opInfo;
|
---|
202 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
|
---|
203 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
|
---|
204 | switch ( opInfo.type ) {
|
---|
205 | case OT_PREFIXASSIGN:
|
---|
206 | case OT_POSTFIXASSIGN:
|
---|
207 | case OT_INFIXASSIGN:
|
---|
208 | {
|
---|
209 | assert( arg != applicationExpr->get_args().end() );
|
---|
210 | if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
|
---|
211 |
|
---|
212 | *arg = addrExpr->get_arg();
|
---|
213 | } else {
|
---|
214 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
215 | newExpr->get_args().push_back( *arg );
|
---|
216 | *arg = newExpr;
|
---|
217 | } // if
|
---|
218 | break;
|
---|
219 | }
|
---|
220 |
|
---|
221 | default:
|
---|
222 | // do nothing
|
---|
223 | ;
|
---|
224 | }
|
---|
225 |
|
---|
226 | switch ( opInfo.type ) {
|
---|
227 | case OT_INDEX:
|
---|
228 | assert( applicationExpr->get_args().size() == 2 );
|
---|
229 | (*arg++)->accept( *this );
|
---|
230 | output << "[";
|
---|
231 | (*arg)->accept( *this );
|
---|
232 | output << "]";
|
---|
233 | break;
|
---|
234 |
|
---|
235 | case OT_CALL:
|
---|
236 | // there are no intrinsic definitions of the function call operator
|
---|
237 | assert( false );
|
---|
238 | break;
|
---|
239 |
|
---|
240 | case OT_PREFIX:
|
---|
241 | case OT_PREFIXASSIGN:
|
---|
242 | assert( applicationExpr->get_args().size() == 1 );
|
---|
243 | output << "(";
|
---|
244 | output << opInfo.symbol;
|
---|
245 | (*arg)->accept( *this );
|
---|
246 | output << ")";
|
---|
247 | break;
|
---|
248 |
|
---|
249 | case OT_POSTFIX:
|
---|
250 | case OT_POSTFIXASSIGN:
|
---|
251 | assert( applicationExpr->get_args().size() == 1 );
|
---|
252 | (*arg)->accept( *this );
|
---|
253 | output << opInfo.symbol;
|
---|
254 | break;
|
---|
255 |
|
---|
256 | case OT_INFIX:
|
---|
257 | case OT_INFIXASSIGN:
|
---|
258 | assert( applicationExpr->get_args().size() == 2 );
|
---|
259 | output << "(";
|
---|
260 | (*arg++)->accept( *this );
|
---|
261 | output << opInfo.symbol;
|
---|
262 | (*arg)->accept( *this );
|
---|
263 | output << ")";
|
---|
264 | break;
|
---|
265 |
|
---|
266 | case OT_CONSTANT:
|
---|
267 | // there are no intrinsic definitions of 0 or 1 as functions
|
---|
268 | assert( false );
|
---|
269 | }
|
---|
270 | } else {
|
---|
271 | varExpr->accept( *this );
|
---|
272 | output << "(";
|
---|
273 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
274 | output << ")";
|
---|
275 | } // if
|
---|
276 | } else {
|
---|
277 | applicationExpr->get_function()->accept( *this );
|
---|
278 | output << "(";
|
---|
279 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
280 | output << ")";
|
---|
281 | } // if
|
---|
282 | }
|
---|
283 |
|
---|
284 | void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
|
---|
285 | if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
|
---|
286 | OperatorInfo opInfo;
|
---|
287 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
288 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
|
---|
289 | switch ( opInfo.type ) {
|
---|
290 | case OT_INDEX:
|
---|
291 | assert( untypedExpr->get_args().size() == 2 );
|
---|
292 | (*arg++)->accept( *this );
|
---|
293 | output << "[";
|
---|
294 | (*arg)->accept( *this );
|
---|
295 | output << "]";
|
---|
296 | break;
|
---|
297 |
|
---|
298 | case OT_CALL:
|
---|
299 | assert( false );
|
---|
300 | break;
|
---|
301 |
|
---|
302 | case OT_PREFIX:
|
---|
303 | case OT_PREFIXASSIGN:
|
---|
304 | assert( untypedExpr->get_args().size() == 1 );
|
---|
305 | output << "(";
|
---|
306 | output << opInfo.symbol;
|
---|
307 | (*arg)->accept( *this );
|
---|
308 | output << ")";
|
---|
309 | break;
|
---|
310 |
|
---|
311 | case OT_POSTFIX:
|
---|
312 | case OT_POSTFIXASSIGN:
|
---|
313 | assert( untypedExpr->get_args().size() == 1 );
|
---|
314 | (*arg)->accept( *this );
|
---|
315 | output << opInfo.symbol;
|
---|
316 | break;
|
---|
317 |
|
---|
318 | case OT_INFIX:
|
---|
319 | case OT_INFIXASSIGN:
|
---|
320 | assert( untypedExpr->get_args().size() == 2 );
|
---|
321 | output << "(";
|
---|
322 | (*arg++)->accept( *this );
|
---|
323 | output << opInfo.symbol;
|
---|
324 | (*arg)->accept( *this );
|
---|
325 | output << ")";
|
---|
326 | break;
|
---|
327 |
|
---|
328 | case OT_CONSTANT:
|
---|
329 | // there are no intrinsic definitions of 0 or 1 as functions
|
---|
330 | assert( false );
|
---|
331 | }
|
---|
332 | } else {
|
---|
333 | nameExpr->accept( *this );
|
---|
334 | output << "(";
|
---|
335 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
336 | output << ")";
|
---|
337 | } // if
|
---|
338 | } else {
|
---|
339 | untypedExpr->get_function()->accept( *this );
|
---|
340 | output << "(";
|
---|
341 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
342 | output << ")";
|
---|
343 | } // if
|
---|
344 | }
|
---|
345 |
|
---|
346 | void CodeGenerator::visit( NameExpr *nameExpr ) {
|
---|
347 | OperatorInfo opInfo;
|
---|
348 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
349 | assert( opInfo.type == OT_CONSTANT );
|
---|
350 | output << opInfo.symbol;
|
---|
351 | } else {
|
---|
352 | output << nameExpr->get_name();
|
---|
353 | } // if
|
---|
354 | }
|
---|
355 |
|
---|
356 | void CodeGenerator::visit( AddressExpr *addressExpr ) {
|
---|
357 | output << "(&";
|
---|
358 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
|
---|
359 | if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
|
---|
360 | output << mangleName( variableExpr->get_var() );
|
---|
361 | } else {
|
---|
362 | addressExpr->get_arg()->accept( *this );
|
---|
363 | } // if
|
---|
364 | output << ")";
|
---|
365 | }
|
---|
366 |
|
---|
367 | void CodeGenerator::visit( CastExpr *castExpr ) {
|
---|
368 | output << "((";
|
---|
369 | if ( castExpr->get_results().empty() ) {
|
---|
370 | output << "void" ;
|
---|
371 | } else {
|
---|
372 | output << genType( castExpr->get_results().front(), "" );
|
---|
373 | } // if
|
---|
374 | output << ")";
|
---|
375 | castExpr->get_arg()->accept( *this );
|
---|
376 | output << ")";
|
---|
377 | }
|
---|
378 |
|
---|
379 | void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
|
---|
380 | assert( false );
|
---|
381 | }
|
---|
382 |
|
---|
383 | void CodeGenerator::visit( MemberExpr *memberExpr ) {
|
---|
384 | memberExpr->get_aggregate()->accept( *this );
|
---|
385 | output << "." << mangleName( memberExpr->get_member() );
|
---|
386 | }
|
---|
387 |
|
---|
388 | void CodeGenerator::visit( VariableExpr *variableExpr ) {
|
---|
389 | OperatorInfo opInfo;
|
---|
390 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
|
---|
391 | output << opInfo.symbol;
|
---|
392 | } else {
|
---|
393 | output << mangleName( variableExpr->get_var() );
|
---|
394 | } // if
|
---|
395 | }
|
---|
396 |
|
---|
397 | void CodeGenerator::visit( ConstantExpr *constantExpr ) {
|
---|
398 | assert( constantExpr->get_constant() );
|
---|
399 | constantExpr->get_constant()->accept( *this );
|
---|
400 | }
|
---|
401 |
|
---|
402 | void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
|
---|
403 | output << "sizeof(";
|
---|
404 | if ( sizeofExpr->get_isType() ) {
|
---|
405 | output << genType( sizeofExpr->get_type(), "" );
|
---|
406 | } else {
|
---|
407 | sizeofExpr->get_expr()->accept( *this );
|
---|
408 | } // if
|
---|
409 | output << ")";
|
---|
410 | }
|
---|
411 |
|
---|
412 | void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
|
---|
413 | output << "(";
|
---|
414 | logicalExpr->get_arg1()->accept( *this );
|
---|
415 | if ( logicalExpr->get_isAnd() ) {
|
---|
416 | output << " && ";
|
---|
417 | } else {
|
---|
418 | output << " || ";
|
---|
419 | } // if
|
---|
420 | logicalExpr->get_arg2()->accept( *this );
|
---|
421 | output << ")";
|
---|
422 | }
|
---|
423 |
|
---|
424 | void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
|
---|
425 | output << "(";
|
---|
426 | conditionalExpr->get_arg1()->accept( *this );
|
---|
427 | output << " ? ";
|
---|
428 | conditionalExpr->get_arg2()->accept( *this );
|
---|
429 | output << " : ";
|
---|
430 | conditionalExpr->get_arg3()->accept( *this );
|
---|
431 | output << ")";
|
---|
432 | }
|
---|
433 |
|
---|
434 | void CodeGenerator::visit( CommaExpr *commaExpr ) {
|
---|
435 | output << "(";
|
---|
436 | commaExpr->get_arg1()->accept( *this );
|
---|
437 | output << " , ";
|
---|
438 | commaExpr->get_arg2()->accept( *this );
|
---|
439 | output << ")";
|
---|
440 | }
|
---|
441 |
|
---|
442 | void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
|
---|
443 |
|
---|
444 | void CodeGenerator::visit( TypeExpr *typeExpr ) {}
|
---|
445 |
|
---|
446 | //*** Statements
|
---|
447 | void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
|
---|
448 | std::list<Statement*> ks = compoundStmt->get_kids();
|
---|
449 | output << "{" << endl;
|
---|
450 |
|
---|
451 | cur_indent += CodeGenerator::tabsize;
|
---|
452 |
|
---|
453 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) {
|
---|
454 | output << indent << printLabels( (*i)->get_labels() );
|
---|
455 | (*i)->accept(*this );
|
---|
456 |
|
---|
457 | output << endl;
|
---|
458 | if ( wantSpacing( *i ) ) {
|
---|
459 | output << endl;
|
---|
460 | }
|
---|
461 | }
|
---|
462 | cur_indent -= CodeGenerator::tabsize;
|
---|
463 |
|
---|
464 | output << indent << "}";
|
---|
465 | }
|
---|
466 |
|
---|
467 | void CodeGenerator::visit( ExprStmt *exprStmt ) {
|
---|
468 | // I don't see why this check is necessary.
|
---|
469 | // If this starts to cause problems then put it back in,
|
---|
470 | // with an explanation
|
---|
471 | assert( exprStmt );
|
---|
472 |
|
---|
473 | // if ( exprStmt != 0 ) {
|
---|
474 | exprStmt->get_expr()->accept( *this );
|
---|
475 | output << ";" ;
|
---|
476 | // } // if
|
---|
477 | }
|
---|
478 |
|
---|
479 | void CodeGenerator::visit( IfStmt *ifStmt ) {
|
---|
480 | output << "if (";
|
---|
481 | ifStmt->get_condition()->accept(*this );
|
---|
482 | output << ") ";
|
---|
483 |
|
---|
484 | ifStmt->get_thenPart()->accept(*this );
|
---|
485 |
|
---|
486 | if ( ifStmt->get_elsePart() != 0) {
|
---|
487 | output << " else ";
|
---|
488 | ifStmt->get_elsePart()->accept(*this );
|
---|
489 | } // if
|
---|
490 | }
|
---|
491 |
|
---|
492 | void CodeGenerator::visit( SwitchStmt *switchStmt ) {
|
---|
493 | output << "switch (" ;
|
---|
494 | switchStmt->get_condition()->accept(*this );
|
---|
495 | output << ") ";
|
---|
496 |
|
---|
497 | output << "{" << std::endl;
|
---|
498 | cur_indent += CodeGenerator::tabsize;
|
---|
499 |
|
---|
500 | acceptAll( switchStmt->get_branches(), *this );
|
---|
501 |
|
---|
502 | cur_indent -= CodeGenerator::tabsize;
|
---|
503 |
|
---|
504 | output << indent << "}";
|
---|
505 | }
|
---|
506 |
|
---|
507 | void CodeGenerator::visit( CaseStmt *caseStmt ) {
|
---|
508 | output << indent;
|
---|
509 | if ( caseStmt->isDefault()) {
|
---|
510 | output << "default";
|
---|
511 | } else {
|
---|
512 | output << "case ";
|
---|
513 | caseStmt->get_condition()->accept(*this );
|
---|
514 | } // if
|
---|
515 | output << ":\n";
|
---|
516 |
|
---|
517 | std::list<Statement *> sts = caseStmt->get_statements();
|
---|
518 |
|
---|
519 | cur_indent += CodeGenerator::tabsize;
|
---|
520 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
|
---|
521 | output << indent << printLabels( (*i)->get_labels() ) ;
|
---|
522 | (*i)->accept(*this );
|
---|
523 | output << endl;
|
---|
524 | }
|
---|
525 | cur_indent -= CodeGenerator::tabsize;
|
---|
526 | }
|
---|
527 |
|
---|
528 | void CodeGenerator::visit( BranchStmt *branchStmt ) {
|
---|
529 | switch ( branchStmt->get_type()) {
|
---|
530 | case BranchStmt::Goto:
|
---|
531 | if ( ! branchStmt->get_target().empty() )
|
---|
532 | output << "goto " << branchStmt->get_target();
|
---|
533 | else {
|
---|
534 | if ( branchStmt->get_computedTarget() != 0 ) {
|
---|
535 | output << "goto *";
|
---|
536 | branchStmt->get_computedTarget()->accept( *this );
|
---|
537 | } // if
|
---|
538 | } // if
|
---|
539 | break;
|
---|
540 | case BranchStmt::Break:
|
---|
541 | output << "break";
|
---|
542 | break;
|
---|
543 | case BranchStmt::Continue:
|
---|
544 | output << "continue";
|
---|
545 | break;
|
---|
546 | }
|
---|
547 | output << ";";
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | void CodeGenerator::visit( ReturnStmt *returnStmt ) {
|
---|
552 | output << "return ";
|
---|
553 |
|
---|
554 | // xxx -- check for null expression;
|
---|
555 | if ( returnStmt->get_expr() ) {
|
---|
556 | returnStmt->get_expr()->accept( *this );
|
---|
557 | } // if
|
---|
558 | output << ";";
|
---|
559 | }
|
---|
560 |
|
---|
561 | void CodeGenerator::visit( WhileStmt *whileStmt ) {
|
---|
562 | if ( whileStmt->get_isDoWhile() )
|
---|
563 | output << "do" ;
|
---|
564 | else {
|
---|
565 | output << "while (" ;
|
---|
566 | whileStmt->get_condition()->accept(*this );
|
---|
567 | output << ")";
|
---|
568 | } // if
|
---|
569 | output << " ";
|
---|
570 |
|
---|
571 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
|
---|
572 | whileStmt->get_body()->accept( *this );
|
---|
573 |
|
---|
574 | output << indent;
|
---|
575 |
|
---|
576 | if ( whileStmt->get_isDoWhile() ) {
|
---|
577 | output << " while (" ;
|
---|
578 | whileStmt->get_condition()->accept(*this );
|
---|
579 | output << ");";
|
---|
580 | } // if
|
---|
581 | }
|
---|
582 |
|
---|
583 | void CodeGenerator::visit( ForStmt *forStmt ) {
|
---|
584 | output << "for (";
|
---|
585 |
|
---|
586 | if ( forStmt->get_initialization() != 0 )
|
---|
587 | forStmt->get_initialization()->accept( *this );
|
---|
588 | else
|
---|
589 | output << ";";
|
---|
590 |
|
---|
591 | if ( forStmt->get_condition() != 0 )
|
---|
592 | forStmt->get_condition()->accept( *this );
|
---|
593 | output << ";";
|
---|
594 |
|
---|
595 | if ( forStmt->get_increment() != 0 )
|
---|
596 | forStmt->get_increment()->accept( *this );
|
---|
597 | output << ") ";
|
---|
598 |
|
---|
599 | if ( forStmt->get_body() != 0 ) {
|
---|
600 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
|
---|
601 | forStmt->get_body()->accept( *this );
|
---|
602 | } // if
|
---|
603 | }
|
---|
604 |
|
---|
605 | void CodeGenerator::visit( NullStmt *nullStmt ) {
|
---|
606 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
|
---|
607 | output << "/* null statement */ ;";
|
---|
608 | }
|
---|
609 |
|
---|
610 | void CodeGenerator::visit( DeclStmt *declStmt ) {
|
---|
611 | declStmt->get_decl()->accept( *this );
|
---|
612 |
|
---|
613 | if ( doSemicolon( declStmt->get_decl() ) ) {
|
---|
614 | output << ";";
|
---|
615 | } // if
|
---|
616 | }
|
---|
617 |
|
---|
618 | std::string CodeGenerator::printLabels( std::list< Label > &l ) {
|
---|
619 | std::string str( "" );
|
---|
620 | l.unique(); // assumes a sorted list. Why not use set?
|
---|
621 |
|
---|
622 | for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
|
---|
623 | str += *i + ": ";
|
---|
624 |
|
---|
625 | return str;
|
---|
626 | }
|
---|
627 |
|
---|
628 | void CodeGenerator::handleStorageClass( Declaration *decl ) {
|
---|
629 | switch ( decl->get_storageClass() ) {
|
---|
630 | case Declaration::NoStorageClass:
|
---|
631 | break;
|
---|
632 | case Declaration::Extern:
|
---|
633 | output << "extern ";
|
---|
634 | break;
|
---|
635 | case Declaration::Static:
|
---|
636 | output << "static ";
|
---|
637 | break;
|
---|
638 | case Declaration::Auto:
|
---|
639 | // silently drop storage class
|
---|
640 | break;
|
---|
641 | case Declaration::Register:
|
---|
642 | output << "register ";
|
---|
643 | break;
|
---|
644 | case Declaration::Inline:
|
---|
645 | // handled as special via isInline flag (FIX)
|
---|
646 | break;
|
---|
647 | case Declaration::Fortran:
|
---|
648 | // not handled
|
---|
649 | break;
|
---|
650 | } // switch
|
---|
651 | }
|
---|
652 | } // namespace CodeGen
|
---|
653 |
|
---|
654 | // Local Variables: //
|
---|
655 | // tab-width: 4 //
|
---|
656 | // mode: c++ //
|
---|
657 | // compile-command: "make install" //
|
---|
658 | // End: //
|
---|