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