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