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 : Thu Jun 9 13:21:00 2016 |
---|
13 | // Update Count : 256 |
---|
14 | // |
---|
15 | |
---|
16 | #include <algorithm> |
---|
17 | #include <iostream> |
---|
18 | #include <cassert> |
---|
19 | #include <list> |
---|
20 | |
---|
21 | #include "Parser/ParseNode.h" |
---|
22 | |
---|
23 | #include "SynTree/Declaration.h" |
---|
24 | #include "SynTree/Expression.h" |
---|
25 | #include "SynTree/Initializer.h" |
---|
26 | #include "SynTree/Statement.h" |
---|
27 | #include "SynTree/Type.h" |
---|
28 | #include "SynTree/Attribute.h" |
---|
29 | |
---|
30 | #include "Common/utility.h" |
---|
31 | #include "Common/UnimplementedError.h" |
---|
32 | |
---|
33 | #include "CodeGenerator.h" |
---|
34 | #include "OperatorTable.h" |
---|
35 | #include "GenType.h" |
---|
36 | |
---|
37 | #include "InitTweak/InitTweak.h" |
---|
38 | |
---|
39 | using namespace std; |
---|
40 | |
---|
41 | namespace CodeGen { |
---|
42 | int CodeGenerator::tabsize = 4; |
---|
43 | |
---|
44 | // the kinds of statements that would ideally be followed by whitespace |
---|
45 | bool wantSpacing( Statement * stmt) { |
---|
46 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || |
---|
47 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt ); |
---|
48 | } |
---|
49 | |
---|
50 | ostream & CodeGenerator::Indenter::operator()( ostream & output ) const { |
---|
51 | return output << string( cg.cur_indent, ' ' ); |
---|
52 | } |
---|
53 | |
---|
54 | ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) { |
---|
55 | return indent( output ); |
---|
56 | } |
---|
57 | |
---|
58 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { |
---|
59 | labels = &l; |
---|
60 | return *this; |
---|
61 | } |
---|
62 | |
---|
63 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter &printLabels ) { |
---|
64 | std::list< Label > & labs = *printLabels.labels; |
---|
65 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter? |
---|
66 | for ( Label & l : labs ) { |
---|
67 | output << l.get_name() + ": "; |
---|
68 | printLabels.cg.genAttributes( l.get_attributes() ); |
---|
69 | } |
---|
70 | return output; |
---|
71 | } |
---|
72 | |
---|
73 | CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) { } |
---|
74 | |
---|
75 | CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp ) |
---|
76 | : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { |
---|
77 | //output << std::string( init ); |
---|
78 | } |
---|
79 | |
---|
80 | CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp ) |
---|
81 | : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { |
---|
82 | //output << std::string( init ); |
---|
83 | } |
---|
84 | |
---|
85 | string mangleName( DeclarationWithType *decl ) { |
---|
86 | if ( decl->get_mangleName() != "" ) { |
---|
87 | // need to incorporate scope level in order to differentiate names for destructors |
---|
88 | return decl->get_scopedMangleName(); |
---|
89 | } else { |
---|
90 | return decl->get_name(); |
---|
91 | } // if |
---|
92 | } |
---|
93 | |
---|
94 | void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) { |
---|
95 | if ( ! attributes.empty() ) { |
---|
96 | output << "__attribute__ (("; |
---|
97 | for ( Attribute *& attr : attributes ) { |
---|
98 | if ( ! attr->empty() ) { |
---|
99 | output << attr->get_name() << "("; |
---|
100 | genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() ); |
---|
101 | output << ")"; |
---|
102 | } |
---|
103 | output << ","; |
---|
104 | } |
---|
105 | output << ")) "; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | //*** Declarations |
---|
111 | void CodeGenerator::visit( FunctionDecl *functionDecl ) { |
---|
112 | genAttributes( functionDecl->get_attributes() ); |
---|
113 | |
---|
114 | handleStorageClass( functionDecl ); |
---|
115 | if ( functionDecl->get_isInline() ) { |
---|
116 | output << "inline "; |
---|
117 | } // if |
---|
118 | if ( functionDecl->get_isNoreturn() ) { |
---|
119 | output << "_Noreturn "; |
---|
120 | } // if |
---|
121 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) ); |
---|
122 | |
---|
123 | // how to get this to the Functype? |
---|
124 | std::list< Declaration * > olds = functionDecl->get_oldDecls(); |
---|
125 | if ( ! olds.empty() ) { |
---|
126 | output << " /* function has old declaration */"; |
---|
127 | } // if |
---|
128 | |
---|
129 | // acceptAll( functionDecl->get_oldDecls(), *this ); |
---|
130 | if ( functionDecl->get_statements() ) { |
---|
131 | functionDecl->get_statements()->accept( *this ); |
---|
132 | } // if |
---|
133 | } |
---|
134 | |
---|
135 | void CodeGenerator::visit( ObjectDecl *objectDecl ) { |
---|
136 | handleStorageClass( objectDecl ); |
---|
137 | output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); |
---|
138 | |
---|
139 | if ( objectDecl->get_init() ) { |
---|
140 | output << " = "; |
---|
141 | objectDecl->get_init()->accept( *this ); |
---|
142 | } // if |
---|
143 | if ( objectDecl->get_bitfieldWidth() ) { |
---|
144 | output << ":"; |
---|
145 | objectDecl->get_bitfieldWidth()->accept( *this ); |
---|
146 | } // if |
---|
147 | } |
---|
148 | |
---|
149 | void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) { |
---|
150 | if ( aggDecl->get_name() != "" ) |
---|
151 | output << aggDecl->get_name(); |
---|
152 | |
---|
153 | std::list< Declaration * > &memb = aggDecl->get_members(); |
---|
154 | |
---|
155 | if ( ! memb.empty() ) { |
---|
156 | output << " {" << endl; |
---|
157 | |
---|
158 | cur_indent += CodeGenerator::tabsize; |
---|
159 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
160 | output << indent; |
---|
161 | (*i)->accept( *this ); |
---|
162 | output << ";" << endl; |
---|
163 | } |
---|
164 | |
---|
165 | cur_indent -= CodeGenerator::tabsize; |
---|
166 | |
---|
167 | output << indent << "}"; |
---|
168 | } // if |
---|
169 | } |
---|
170 | |
---|
171 | void CodeGenerator::visit( StructDecl *structDecl ) { |
---|
172 | output << "struct "; |
---|
173 | handleAggregate( structDecl ); |
---|
174 | } |
---|
175 | |
---|
176 | void CodeGenerator::visit( UnionDecl *aggregateDecl ) { |
---|
177 | output << "union "; |
---|
178 | handleAggregate( aggregateDecl ); |
---|
179 | } |
---|
180 | |
---|
181 | void CodeGenerator::visit( EnumDecl *aggDecl ) { |
---|
182 | output << "enum "; |
---|
183 | |
---|
184 | if ( aggDecl->get_name() != "" ) |
---|
185 | output << aggDecl->get_name(); |
---|
186 | |
---|
187 | std::list< Declaration* > &memb = aggDecl->get_members(); |
---|
188 | |
---|
189 | if ( ! memb.empty() ) { |
---|
190 | output << " {" << endl; |
---|
191 | |
---|
192 | cur_indent += CodeGenerator::tabsize; |
---|
193 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
194 | ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
195 | assert( obj ); |
---|
196 | output << indent << mangleName( obj ); |
---|
197 | if ( obj->get_init() ) { |
---|
198 | output << " = "; |
---|
199 | obj->get_init()->accept( *this ); |
---|
200 | } // if |
---|
201 | output << "," << endl; |
---|
202 | } // for |
---|
203 | |
---|
204 | cur_indent -= CodeGenerator::tabsize; |
---|
205 | |
---|
206 | output << indent << "}"; |
---|
207 | } // if |
---|
208 | } |
---|
209 | |
---|
210 | void CodeGenerator::visit( TraitDecl *aggregateDecl ) {} |
---|
211 | |
---|
212 | void CodeGenerator::visit( TypedefDecl *typeDecl ) { |
---|
213 | output << "typedef "; |
---|
214 | output << genType( typeDecl->get_base(), typeDecl->get_name() ); |
---|
215 | } |
---|
216 | |
---|
217 | void CodeGenerator::visit( TypeDecl *typeDecl ) { |
---|
218 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, |
---|
219 | // still to be done |
---|
220 | output << "extern unsigned long " << typeDecl->get_name(); |
---|
221 | if ( typeDecl->get_base() ) { |
---|
222 | output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )"; |
---|
223 | } // if |
---|
224 | } |
---|
225 | |
---|
226 | void CodeGenerator::printDesignators( std::list< Expression * > & designators ) { |
---|
227 | typedef std::list< Expression * > DesignatorList; |
---|
228 | if ( designators.size() == 0 ) return; |
---|
229 | for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) { |
---|
230 | if ( dynamic_cast< NameExpr * >( *iter ) ) { |
---|
231 | // if expression is a name, then initializing aggregate member |
---|
232 | output << "."; |
---|
233 | (*iter)->accept( *this ); |
---|
234 | } else { |
---|
235 | // if not a simple name, it has to be a constant expression, i.e. an array designator |
---|
236 | output << "["; |
---|
237 | (*iter)->accept( *this ); |
---|
238 | output << "]"; |
---|
239 | } |
---|
240 | } |
---|
241 | output << " = "; |
---|
242 | } |
---|
243 | |
---|
244 | void CodeGenerator::visit( SingleInit *init ) { |
---|
245 | printDesignators( init->get_designators() ); |
---|
246 | init->get_value()->accept( *this ); |
---|
247 | } |
---|
248 | |
---|
249 | void CodeGenerator::visit( ListInit *init ) { |
---|
250 | printDesignators( init->get_designators() ); |
---|
251 | output << "{ "; |
---|
252 | if ( init->begin_initializers() == init->end_initializers() ) { |
---|
253 | // illegal to leave initializer list empty for scalar initializers, |
---|
254 | // but always legal to have 0 |
---|
255 | output << "0"; |
---|
256 | } else { |
---|
257 | genCommaList( init->begin_initializers(), init->end_initializers() ); |
---|
258 | } |
---|
259 | output << " }"; |
---|
260 | } |
---|
261 | |
---|
262 | void CodeGenerator::visit( Constant *constant ) { |
---|
263 | output << constant->get_value() ; |
---|
264 | } |
---|
265 | |
---|
266 | //*** Expressions |
---|
267 | void CodeGenerator::visit( ApplicationExpr *applicationExpr ) { |
---|
268 | extension( applicationExpr ); |
---|
269 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
270 | OperatorInfo opInfo; |
---|
271 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { |
---|
272 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
273 | switch ( opInfo.type ) { |
---|
274 | case OT_PREFIXASSIGN: |
---|
275 | case OT_POSTFIXASSIGN: |
---|
276 | case OT_INFIXASSIGN: |
---|
277 | case OT_CTOR: |
---|
278 | case OT_DTOR: |
---|
279 | { |
---|
280 | assert( arg != applicationExpr->get_args().end() ); |
---|
281 | if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { |
---|
282 | // remove & from first assignment/ctor argument |
---|
283 | *arg = addrExpr->get_arg(); |
---|
284 | } else { |
---|
285 | // no address-of operator, so must be a pointer - add dereference |
---|
286 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
287 | newExpr->get_args().push_back( *arg ); |
---|
288 | assert( (*arg)->get_results().size() == 1 ); |
---|
289 | Type * type = InitTweak::getPointerBase( (*arg)->get_results().front() ); |
---|
290 | assert( type ); |
---|
291 | newExpr->get_results().push_back( type ); |
---|
292 | *arg = newExpr; |
---|
293 | } // if |
---|
294 | break; |
---|
295 | } |
---|
296 | |
---|
297 | default: |
---|
298 | // do nothing |
---|
299 | ; |
---|
300 | } |
---|
301 | |
---|
302 | switch ( opInfo.type ) { |
---|
303 | case OT_INDEX: |
---|
304 | assert( applicationExpr->get_args().size() == 2 ); |
---|
305 | (*arg++)->accept( *this ); |
---|
306 | output << "["; |
---|
307 | (*arg)->accept( *this ); |
---|
308 | output << "]"; |
---|
309 | break; |
---|
310 | |
---|
311 | case OT_CALL: |
---|
312 | // there are no intrinsic definitions of the function call operator |
---|
313 | assert( false ); |
---|
314 | break; |
---|
315 | |
---|
316 | case OT_CTOR: |
---|
317 | case OT_DTOR: |
---|
318 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
319 | // the expression fed into a single parameter constructor or destructor |
---|
320 | // may contain side effects, so must still output this expression |
---|
321 | output << "("; |
---|
322 | (*arg++)->accept( *this ); |
---|
323 | output << ") /* " << opInfo.inputName << " */"; |
---|
324 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
325 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
326 | output << "("; |
---|
327 | (*arg++)->accept( *this ); |
---|
328 | output << opInfo.symbol; |
---|
329 | (*arg)->accept( *this ); |
---|
330 | output << ") /* " << opInfo.inputName << " */"; |
---|
331 | } else { |
---|
332 | // no constructors with 0 or more than 2 parameters |
---|
333 | assert( false ); |
---|
334 | } |
---|
335 | break; |
---|
336 | |
---|
337 | case OT_PREFIX: |
---|
338 | case OT_PREFIXASSIGN: |
---|
339 | assert( applicationExpr->get_args().size() == 1 ); |
---|
340 | output << "("; |
---|
341 | output << opInfo.symbol; |
---|
342 | (*arg)->accept( *this ); |
---|
343 | output << ")"; |
---|
344 | break; |
---|
345 | |
---|
346 | case OT_POSTFIX: |
---|
347 | case OT_POSTFIXASSIGN: |
---|
348 | assert( applicationExpr->get_args().size() == 1 ); |
---|
349 | (*arg)->accept( *this ); |
---|
350 | output << opInfo.symbol; |
---|
351 | break; |
---|
352 | |
---|
353 | |
---|
354 | case OT_INFIX: |
---|
355 | case OT_INFIXASSIGN: |
---|
356 | assert( applicationExpr->get_args().size() == 2 ); |
---|
357 | output << "("; |
---|
358 | (*arg++)->accept( *this ); |
---|
359 | output << opInfo.symbol; |
---|
360 | (*arg)->accept( *this ); |
---|
361 | output << ")"; |
---|
362 | break; |
---|
363 | |
---|
364 | case OT_CONSTANT: |
---|
365 | case OT_LABELADDRESS: |
---|
366 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
367 | assert( false ); |
---|
368 | } |
---|
369 | } else { |
---|
370 | varExpr->accept( *this ); |
---|
371 | output << "("; |
---|
372 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
373 | output << ")"; |
---|
374 | } // if |
---|
375 | } else { |
---|
376 | applicationExpr->get_function()->accept( *this ); |
---|
377 | output << "("; |
---|
378 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
379 | output << ")"; |
---|
380 | } // if |
---|
381 | } |
---|
382 | |
---|
383 | void CodeGenerator::visit( UntypedExpr *untypedExpr ) { |
---|
384 | extension( untypedExpr ); |
---|
385 | if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { |
---|
386 | OperatorInfo opInfo; |
---|
387 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
388 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); |
---|
389 | switch ( opInfo.type ) { |
---|
390 | case OT_INDEX: |
---|
391 | assert( untypedExpr->get_args().size() == 2 ); |
---|
392 | (*arg++)->accept( *this ); |
---|
393 | output << "["; |
---|
394 | (*arg)->accept( *this ); |
---|
395 | output << "]"; |
---|
396 | break; |
---|
397 | |
---|
398 | case OT_CALL: |
---|
399 | assert( false ); |
---|
400 | |
---|
401 | |
---|
402 | case OT_CTOR: |
---|
403 | case OT_DTOR: |
---|
404 | if ( untypedExpr->get_args().size() == 1 ) { |
---|
405 | // the expression fed into a single parameter constructor or destructor |
---|
406 | // may contain side effects, so must still output this expression |
---|
407 | output << "("; |
---|
408 | (*arg++)->accept( *this ); |
---|
409 | output << ") /* " << opInfo.inputName << " */"; |
---|
410 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
411 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
412 | output << "("; |
---|
413 | (*arg++)->accept( *this ); |
---|
414 | output << opInfo.symbol; |
---|
415 | (*arg)->accept( *this ); |
---|
416 | output << ") /* " << opInfo.inputName << " */"; |
---|
417 | } else { |
---|
418 | // no constructors with 0 or more than 2 parameters |
---|
419 | assert( false ); |
---|
420 | } |
---|
421 | break; |
---|
422 | |
---|
423 | case OT_PREFIX: |
---|
424 | case OT_PREFIXASSIGN: |
---|
425 | case OT_LABELADDRESS: |
---|
426 | assert( untypedExpr->get_args().size() == 1 ); |
---|
427 | output << "("; |
---|
428 | output << opInfo.symbol; |
---|
429 | (*arg)->accept( *this ); |
---|
430 | output << ")"; |
---|
431 | break; |
---|
432 | |
---|
433 | case OT_POSTFIX: |
---|
434 | case OT_POSTFIXASSIGN: |
---|
435 | assert( untypedExpr->get_args().size() == 1 ); |
---|
436 | (*arg)->accept( *this ); |
---|
437 | output << opInfo.symbol; |
---|
438 | break; |
---|
439 | |
---|
440 | case OT_INFIX: |
---|
441 | case OT_INFIXASSIGN: |
---|
442 | assert( untypedExpr->get_args().size() == 2 ); |
---|
443 | output << "("; |
---|
444 | (*arg++)->accept( *this ); |
---|
445 | output << opInfo.symbol; |
---|
446 | (*arg)->accept( *this ); |
---|
447 | output << ")"; |
---|
448 | break; |
---|
449 | |
---|
450 | case OT_CONSTANT: |
---|
451 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
452 | assert( false ); |
---|
453 | } |
---|
454 | } else { |
---|
455 | nameExpr->accept( *this ); |
---|
456 | output << "("; |
---|
457 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
458 | output << ")"; |
---|
459 | } // if |
---|
460 | } else { |
---|
461 | untypedExpr->get_function()->accept( *this ); |
---|
462 | output << "("; |
---|
463 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
464 | output << ")"; |
---|
465 | } // if |
---|
466 | } |
---|
467 | |
---|
468 | void CodeGenerator::visit( NameExpr *nameExpr ) { |
---|
469 | extension( nameExpr ); |
---|
470 | OperatorInfo opInfo; |
---|
471 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
472 | assert( opInfo.type == OT_CONSTANT ); |
---|
473 | output << opInfo.symbol; |
---|
474 | } else { |
---|
475 | output << nameExpr->get_name(); |
---|
476 | } // if |
---|
477 | } |
---|
478 | |
---|
479 | void CodeGenerator::visit( AddressExpr *addressExpr ) { |
---|
480 | extension( addressExpr ); |
---|
481 | output << "(&"; |
---|
482 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address |
---|
483 | if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { |
---|
484 | output << mangleName( variableExpr->get_var() ); |
---|
485 | } else { |
---|
486 | addressExpr->get_arg()->accept( *this ); |
---|
487 | } // if |
---|
488 | output << ")"; |
---|
489 | } |
---|
490 | |
---|
491 | void CodeGenerator::visit( CastExpr *castExpr ) { |
---|
492 | extension( castExpr ); |
---|
493 | output << "("; |
---|
494 | if ( castExpr->get_results().empty() ) { |
---|
495 | output << "(void)" ; |
---|
496 | } else if ( ! castExpr->get_results().front()->get_isLvalue() ) { |
---|
497 | // at least one result type of cast, but not an lvalue |
---|
498 | output << "("; |
---|
499 | output << genType( castExpr->get_results().front(), "" ); |
---|
500 | output << ")"; |
---|
501 | } else { |
---|
502 | // otherwise, the cast is to an lvalue type, so the cast |
---|
503 | // should be dropped, since the result of a cast is |
---|
504 | // never an lvalue in C |
---|
505 | } |
---|
506 | castExpr->get_arg()->accept( *this ); |
---|
507 | output << ")"; |
---|
508 | } |
---|
509 | |
---|
510 | void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { |
---|
511 | assert( false ); |
---|
512 | } |
---|
513 | |
---|
514 | void CodeGenerator::visit( MemberExpr *memberExpr ) { |
---|
515 | extension( memberExpr ); |
---|
516 | memberExpr->get_aggregate()->accept( *this ); |
---|
517 | output << "." << mangleName( memberExpr->get_member() ); |
---|
518 | } |
---|
519 | |
---|
520 | void CodeGenerator::visit( VariableExpr *variableExpr ) { |
---|
521 | extension( variableExpr ); |
---|
522 | OperatorInfo opInfo; |
---|
523 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { |
---|
524 | output << opInfo.symbol; |
---|
525 | } else { |
---|
526 | output << mangleName( variableExpr->get_var() ); |
---|
527 | } // if |
---|
528 | } |
---|
529 | |
---|
530 | void CodeGenerator::visit( ConstantExpr *constantExpr ) { |
---|
531 | assert( constantExpr->get_constant() ); |
---|
532 | extension( constantExpr ); |
---|
533 | constantExpr->get_constant()->accept( *this ); |
---|
534 | } |
---|
535 | |
---|
536 | void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { |
---|
537 | extension( sizeofExpr ); |
---|
538 | output << "sizeof("; |
---|
539 | if ( sizeofExpr->get_isType() ) { |
---|
540 | output << genType( sizeofExpr->get_type(), "" ); |
---|
541 | } else { |
---|
542 | sizeofExpr->get_expr()->accept( *this ); |
---|
543 | } // if |
---|
544 | output << ")"; |
---|
545 | } |
---|
546 | |
---|
547 | void CodeGenerator::visit( AlignofExpr *alignofExpr ) { |
---|
548 | extension( alignofExpr ); |
---|
549 | // use GCC extension to avoid bumping std to C11 |
---|
550 | output << "__alignof__("; |
---|
551 | if ( alignofExpr->get_isType() ) { |
---|
552 | output << genType( alignofExpr->get_type(), "" ); |
---|
553 | } else { |
---|
554 | alignofExpr->get_expr()->accept( *this ); |
---|
555 | } // if |
---|
556 | output << ")"; |
---|
557 | } |
---|
558 | |
---|
559 | void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) { |
---|
560 | assert( false && "UntypedOffsetofExpr should not reach code generation" ); |
---|
561 | } |
---|
562 | |
---|
563 | void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) { |
---|
564 | extension( offsetofExpr ); |
---|
565 | // use GCC builtin |
---|
566 | output << "__builtin_offsetof("; |
---|
567 | output << genType( offsetofExpr->get_type(), "" ); |
---|
568 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
569 | output << ")"; |
---|
570 | } |
---|
571 | |
---|
572 | void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) { |
---|
573 | assert( false && "OffsetPackExpr should not reach code generation" ); |
---|
574 | } |
---|
575 | |
---|
576 | void CodeGenerator::visit( LogicalExpr *logicalExpr ) { |
---|
577 | extension( logicalExpr ); |
---|
578 | output << "("; |
---|
579 | logicalExpr->get_arg1()->accept( *this ); |
---|
580 | if ( logicalExpr->get_isAnd() ) { |
---|
581 | output << " && "; |
---|
582 | } else { |
---|
583 | output << " || "; |
---|
584 | } // if |
---|
585 | logicalExpr->get_arg2()->accept( *this ); |
---|
586 | output << ")"; |
---|
587 | } |
---|
588 | |
---|
589 | void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { |
---|
590 | extension( conditionalExpr ); |
---|
591 | output << "("; |
---|
592 | conditionalExpr->get_arg1()->accept( *this ); |
---|
593 | output << " ? "; |
---|
594 | conditionalExpr->get_arg2()->accept( *this ); |
---|
595 | output << " : "; |
---|
596 | conditionalExpr->get_arg3()->accept( *this ); |
---|
597 | output << ")"; |
---|
598 | } |
---|
599 | |
---|
600 | void CodeGenerator::visit( CommaExpr *commaExpr ) { |
---|
601 | extension( commaExpr ); |
---|
602 | output << "("; |
---|
603 | commaExpr->get_arg1()->accept( *this ); |
---|
604 | output << " , "; |
---|
605 | commaExpr->get_arg2()->accept( *this ); |
---|
606 | output << ")"; |
---|
607 | } |
---|
608 | |
---|
609 | void CodeGenerator::visit( TupleExpr *tupleExpr ) {} |
---|
610 | |
---|
611 | void CodeGenerator::visit( TypeExpr *typeExpr ) {} |
---|
612 | |
---|
613 | void CodeGenerator::visit( AsmExpr *asmExpr ) { |
---|
614 | extension( asmExpr ); |
---|
615 | if ( asmExpr->get_inout() ) { |
---|
616 | output << "[ "; |
---|
617 | asmExpr->get_inout()->accept( *this ); |
---|
618 | output << " ] "; |
---|
619 | } // if |
---|
620 | asmExpr->get_constraint()->accept( *this ); |
---|
621 | output << " ( "; |
---|
622 | asmExpr->get_operand()->accept( *this ); |
---|
623 | output << " )"; |
---|
624 | } |
---|
625 | |
---|
626 | //*** Statements |
---|
627 | void CodeGenerator::visit( CompoundStmt *compoundStmt ) { |
---|
628 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
629 | output << "{" << endl; |
---|
630 | |
---|
631 | cur_indent += CodeGenerator::tabsize; |
---|
632 | |
---|
633 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
634 | output << indent << printLabels( (*i)->get_labels() ); |
---|
635 | (*i)->accept( *this ); |
---|
636 | |
---|
637 | output << endl; |
---|
638 | if ( wantSpacing( *i ) ) { |
---|
639 | output << endl; |
---|
640 | } |
---|
641 | } |
---|
642 | cur_indent -= CodeGenerator::tabsize; |
---|
643 | |
---|
644 | output << indent << "}"; |
---|
645 | } |
---|
646 | |
---|
647 | void CodeGenerator::visit( ExprStmt *exprStmt ) { |
---|
648 | assert( exprStmt ); |
---|
649 | // cast the top-level expression to void to reduce gcc warnings. |
---|
650 | Expression * expr = new CastExpr( exprStmt->get_expr() ); |
---|
651 | expr->accept( *this ); |
---|
652 | output << ";"; |
---|
653 | } |
---|
654 | |
---|
655 | void CodeGenerator::visit( AsmStmt *asmStmt ) { |
---|
656 | output << "asm "; |
---|
657 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
658 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
659 | output << "( "; |
---|
660 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); |
---|
661 | output << " : "; |
---|
662 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
663 | output << " : "; |
---|
664 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
665 | output << " : "; |
---|
666 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
667 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
668 | output << " : "; |
---|
669 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
670 | output << *begin++; |
---|
671 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
672 | output << ", "; |
---|
673 | } // for |
---|
674 | } // if |
---|
675 | output << " );" ; |
---|
676 | } |
---|
677 | |
---|
678 | void CodeGenerator::visit( IfStmt *ifStmt ) { |
---|
679 | output << "if ( "; |
---|
680 | ifStmt->get_condition()->accept( *this ); |
---|
681 | output << " ) "; |
---|
682 | |
---|
683 | ifStmt->get_thenPart()->accept( *this ); |
---|
684 | |
---|
685 | if ( ifStmt->get_elsePart() != 0) { |
---|
686 | output << " else "; |
---|
687 | ifStmt->get_elsePart()->accept( *this ); |
---|
688 | } // if |
---|
689 | } |
---|
690 | |
---|
691 | void CodeGenerator::visit( SwitchStmt *switchStmt ) { |
---|
692 | output << "switch ( " ; |
---|
693 | switchStmt->get_condition()->accept( *this ); |
---|
694 | output << " ) "; |
---|
695 | |
---|
696 | output << "{" << std::endl; |
---|
697 | cur_indent += CodeGenerator::tabsize; |
---|
698 | |
---|
699 | acceptAll( switchStmt->get_branches(), *this ); |
---|
700 | |
---|
701 | cur_indent -= CodeGenerator::tabsize; |
---|
702 | |
---|
703 | output << indent << "}"; |
---|
704 | } |
---|
705 | |
---|
706 | void CodeGenerator::visit( CaseStmt *caseStmt ) { |
---|
707 | output << indent; |
---|
708 | if ( caseStmt->isDefault()) { |
---|
709 | output << "default"; |
---|
710 | } else { |
---|
711 | output << "case "; |
---|
712 | caseStmt->get_condition()->accept( *this ); |
---|
713 | } // if |
---|
714 | output << ":\n"; |
---|
715 | |
---|
716 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
717 | |
---|
718 | cur_indent += CodeGenerator::tabsize; |
---|
719 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
720 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
721 | (*i)->accept( *this ); |
---|
722 | output << endl; |
---|
723 | } |
---|
724 | cur_indent -= CodeGenerator::tabsize; |
---|
725 | } |
---|
726 | |
---|
727 | void CodeGenerator::visit( BranchStmt *branchStmt ) { |
---|
728 | switch ( branchStmt->get_type()) { |
---|
729 | case BranchStmt::Goto: |
---|
730 | if ( ! branchStmt->get_target().empty() ) |
---|
731 | output << "goto " << branchStmt->get_target(); |
---|
732 | else { |
---|
733 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
734 | output << "goto *"; |
---|
735 | branchStmt->get_computedTarget()->accept( *this ); |
---|
736 | } // if |
---|
737 | } // if |
---|
738 | break; |
---|
739 | case BranchStmt::Break: |
---|
740 | output << "break"; |
---|
741 | break; |
---|
742 | case BranchStmt::Continue: |
---|
743 | output << "continue"; |
---|
744 | break; |
---|
745 | } |
---|
746 | output << ";"; |
---|
747 | } |
---|
748 | |
---|
749 | |
---|
750 | void CodeGenerator::visit( ReturnStmt *returnStmt ) { |
---|
751 | output << "return "; |
---|
752 | |
---|
753 | // xxx -- check for null expression; |
---|
754 | if ( returnStmt->get_expr() ) { |
---|
755 | returnStmt->get_expr()->accept( *this ); |
---|
756 | } // if |
---|
757 | output << ";"; |
---|
758 | } |
---|
759 | |
---|
760 | void CodeGenerator::visit( WhileStmt *whileStmt ) { |
---|
761 | if ( whileStmt->get_isDoWhile() ) { |
---|
762 | output << "do" ; |
---|
763 | } else { |
---|
764 | output << "while (" ; |
---|
765 | whileStmt->get_condition()->accept( *this ); |
---|
766 | output << ")"; |
---|
767 | } // if |
---|
768 | output << " "; |
---|
769 | |
---|
770 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); |
---|
771 | whileStmt->get_body()->accept( *this ); |
---|
772 | |
---|
773 | output << indent; |
---|
774 | |
---|
775 | if ( whileStmt->get_isDoWhile() ) { |
---|
776 | output << " while (" ; |
---|
777 | whileStmt->get_condition()->accept( *this ); |
---|
778 | output << ");"; |
---|
779 | } // if |
---|
780 | } |
---|
781 | |
---|
782 | void CodeGenerator::visit( ForStmt *forStmt ) { |
---|
783 | // initialization is always hoisted, so don't |
---|
784 | // bother doing anything with that |
---|
785 | output << "for (;"; |
---|
786 | |
---|
787 | if ( forStmt->get_condition() != 0 ) { |
---|
788 | forStmt->get_condition()->accept( *this ); |
---|
789 | } |
---|
790 | output << ";"; |
---|
791 | |
---|
792 | if ( forStmt->get_increment() != 0 ) { |
---|
793 | // cast the top-level expression to void to reduce gcc warnings. |
---|
794 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
795 | expr->accept( *this ); |
---|
796 | } |
---|
797 | output << ") "; |
---|
798 | |
---|
799 | if ( forStmt->get_body() != 0 ) { |
---|
800 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
801 | forStmt->get_body()->accept( *this ); |
---|
802 | } // if |
---|
803 | } |
---|
804 | |
---|
805 | void CodeGenerator::visit( NullStmt *nullStmt ) { |
---|
806 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
807 | output << "/* null statement */ ;"; |
---|
808 | } |
---|
809 | |
---|
810 | void CodeGenerator::visit( DeclStmt *declStmt ) { |
---|
811 | declStmt->get_decl()->accept( *this ); |
---|
812 | |
---|
813 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
814 | output << ";"; |
---|
815 | } // if |
---|
816 | } |
---|
817 | |
---|
818 | void CodeGenerator::handleStorageClass( Declaration *decl ) { |
---|
819 | switch ( decl->get_storageClass() ) { |
---|
820 | case DeclarationNode::Extern: |
---|
821 | output << "extern "; |
---|
822 | break; |
---|
823 | case DeclarationNode::Static: |
---|
824 | output << "static "; |
---|
825 | break; |
---|
826 | case DeclarationNode::Auto: |
---|
827 | // silently drop storage class |
---|
828 | break; |
---|
829 | case DeclarationNode::Register: |
---|
830 | output << "register "; |
---|
831 | break; |
---|
832 | case DeclarationNode::Inline: |
---|
833 | output << "inline "; |
---|
834 | break; |
---|
835 | case DeclarationNode::Fortran: |
---|
836 | output << "fortran "; |
---|
837 | break; |
---|
838 | case DeclarationNode::Noreturn: |
---|
839 | output << "_Noreturn "; |
---|
840 | break; |
---|
841 | case DeclarationNode::Threadlocal: |
---|
842 | output << "_Thread_local "; |
---|
843 | break; |
---|
844 | case DeclarationNode::NoStorageClass: |
---|
845 | break; |
---|
846 | } // switch |
---|
847 | } |
---|
848 | } // namespace CodeGen |
---|
849 | |
---|
850 | // Local Variables: // |
---|
851 | // tab-width: 4 // |
---|
852 | // mode: c++ // |
---|
853 | // compile-command: "make install" // |
---|
854 | // End: // |
---|