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