[51587aa] | 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 | // |
---|
[71f4e4f] | 7 | // CodeGenerator.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[de8d7fb1] | 11 | // Last Modified By : Peter A. Buhr |
---|
[6180274] | 12 | // Last Modified On : Wed Feb 2 20:30:30 2022 |
---|
| 13 | // Update Count : 541 |
---|
[51587aa] | 14 | // |
---|
[3268a58] | 15 | #include "CodeGenerator.h" |
---|
[51587aa] | 16 | |
---|
[bf2438c] | 17 | #include <cassert> // for assert, assertf |
---|
| 18 | #include <list> // for _List_iterator, list, list<>::it... |
---|
[51b7345] | 19 | |
---|
[bf2438c] | 20 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 21 | #include "Common/utility.h" // for CodeLocation, toString |
---|
| 22 | #include "GenType.h" // for genType |
---|
| 23 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
| 24 | #include "OperatorTable.h" // for OperatorInfo, operatorLookup |
---|
[07de76b] | 25 | #include "SynTree/LinkageSpec.h" // for Spec, Intrinsic |
---|
[bf2438c] | 26 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 27 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
| 28 | #include "SynTree/Constant.h" // for Constant |
---|
| 29 | #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl |
---|
| 30 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica... |
---|
| 31 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
| 32 | #include "SynTree/Label.h" // for Label, operator<< |
---|
| 33 | #include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt |
---|
| 34 | #include "SynTree/Type.h" // for Type, Type::StorageClasses, Func... |
---|
[10a7775] | 35 | |
---|
[51b7345] | 36 | using namespace std; |
---|
| 37 | |
---|
| 38 | namespace CodeGen { |
---|
[6c4ff37] | 39 | int CodeGenerator::tabsize = 4; |
---|
[51587aa] | 40 | |
---|
[60a8062] | 41 | // The kinds of statements that would ideally be followed by whitespace. |
---|
[2b6c1e0] | 42 | bool wantSpacing( Statement * stmt) { |
---|
| 43 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || |
---|
[3b0bc16] | 44 | dynamic_cast< WhileDoStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt ); |
---|
[2b6c1e0] | 45 | } |
---|
| 46 | |
---|
[8688ce1] | 47 | void CodeGenerator::extension( Expression * expr ) { |
---|
[8e9cbb2] | 48 | if ( expr->get_extension() ) { |
---|
| 49 | output << "__extension__ "; |
---|
| 50 | } // if |
---|
| 51 | } // extension |
---|
| 52 | |
---|
[8688ce1] | 53 | void CodeGenerator::extension( Declaration * decl ) { |
---|
[8e9cbb2] | 54 | if ( decl->get_extension() ) { |
---|
| 55 | output << "__extension__ "; |
---|
| 56 | } // if |
---|
| 57 | } // extension |
---|
| 58 | |
---|
[58dd019] | 59 | void CodeGenerator::asmName( DeclarationWithType * decl ) { |
---|
[e612146c] | 60 | if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) { |
---|
[58dd019] | 61 | output << " asm ( " << asmName->get_constant()->get_value() << " )"; |
---|
| 62 | } // if |
---|
| 63 | } // extension |
---|
| 64 | |
---|
[888cbe4] | 65 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { |
---|
| 66 | labels = &l; |
---|
| 67 | return *this; |
---|
| 68 | } |
---|
| 69 | |
---|
[8688ce1] | 70 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) { |
---|
[888cbe4] | 71 | std::list< Label > & labs = *printLabels.labels; |
---|
| 72 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter? |
---|
| 73 | for ( Label & l : labs ) { |
---|
| 74 | output << l.get_name() + ": "; |
---|
| 75 | printLabels.cg.genAttributes( l.get_attributes() ); |
---|
[8688ce1] | 76 | } // for |
---|
[888cbe4] | 77 | return output; |
---|
| 78 | } |
---|
| 79 | |
---|
[60a8062] | 80 | // Using updateLocation at the beginning of a node and endl within a node should become the method of formating. |
---|
[8bafacc] | 81 | void CodeGenerator::updateLocation( CodeLocation const & to ) { |
---|
[d22e90f] | 82 | // skip if linemarks shouldn't appear or if codelocation is unset |
---|
[42a36d9] | 83 | if ( !options.lineMarks || to.isUnset() ) return; |
---|
[d22e90f] | 84 | |
---|
| 85 | if ( currentLocation.followedBy( to, 0 ) ) { |
---|
[8bafacc] | 86 | return; |
---|
| 87 | } else if ( currentLocation.followedBy( to, 1 ) ) { |
---|
| 88 | output << "\n" << indent; |
---|
[d48e529] | 89 | currentLocation.first_line += 1; |
---|
[8bafacc] | 90 | } else if ( currentLocation.followedBy( to, 2 ) ) { |
---|
| 91 | output << "\n\n" << indent; |
---|
[d48e529] | 92 | currentLocation.first_line += 2; |
---|
[8bafacc] | 93 | } else { |
---|
[d48e529] | 94 | output << "\n# " << to.first_line << " \"" << to.filename |
---|
[60a8062] | 95 | << "\"\n" << indent; |
---|
[8bafacc] | 96 | currentLocation = to; |
---|
| 97 | } |
---|
| 98 | output << std::flush; |
---|
| 99 | } |
---|
[c850687] | 100 | |
---|
[8bafacc] | 101 | void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) { |
---|
| 102 | updateLocation( to->location ); |
---|
[c850687] | 103 | } |
---|
| 104 | |
---|
[d22e90f] | 105 | // replace endl |
---|
| 106 | ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const { |
---|
| 107 | // if ( !cg.lineMarks ) { |
---|
| 108 | // os << "\n" << cg.indent << std::flush; |
---|
| 109 | // } |
---|
| 110 | os << "\n" << std::flush; |
---|
| 111 | cg.currentLocation.first_line++; |
---|
| 112 | // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/"; |
---|
| 113 | return os; |
---|
[c850687] | 114 | } |
---|
| 115 | |
---|
[4e5e6cc] | 116 | CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( 0, CodeGenerator::tabsize ), output( os ), printLabels( *this ), options( pretty, genC, lineMarks, printExprTypes ), endl( *this ) {} |
---|
| 117 | CodeGenerator::CodeGenerator( std::ostream & os, const Options &options ) : indent( 0, CodeGenerator::tabsize ), output( os ), printLabels( *this ), options(options), endl( *this ) {} |
---|
[cda48b6] | 118 | |
---|
[486341f] | 119 | string CodeGenerator::mangleName( DeclarationWithType * decl ) { |
---|
[13073be] | 120 | // GCC builtins should always be printed unmangled |
---|
[42a36d9] | 121 | if ( options.pretty || decl->linkage.is_gcc_builtin ) return decl->name; |
---|
[16ba4a6] | 122 | if ( LinkageSpec::isMangled(decl->linkage) && decl->mangleName != "" ) { |
---|
[f326f99] | 123 | // need to incorporate scope level in order to differentiate names for destructors |
---|
| 124 | return decl->get_scopedMangleName(); |
---|
[51587aa] | 125 | } else { |
---|
[13073be] | 126 | return decl->name; |
---|
[51587aa] | 127 | } // if |
---|
| 128 | } |
---|
[94b4364] | 129 | |
---|
[44a81853] | 130 | void CodeGenerator::genAttributes( list< Attribute * > & attributes ) { |
---|
[60a8062] | 131 | if ( attributes.empty() ) return; |
---|
[44a81853] | 132 | output << "__attribute__ (("; |
---|
| 133 | for ( list< Attribute * >::iterator attr( attributes.begin() );; ) { |
---|
[85b2300] | 134 | output << (*attr)->name; |
---|
| 135 | if ( ! (*attr)->parameters.empty() ) { |
---|
[44a81853] | 136 | output << "("; |
---|
[85b2300] | 137 | genCommaList( (*attr)->parameters.begin(), (*attr)->parameters.end() ); |
---|
[44a81853] | 138 | output << ")"; |
---|
| 139 | } // if |
---|
[60a8062] | 140 | if ( ++attr == attributes.end() ) break; |
---|
[44a81853] | 141 | output << ","; // separator |
---|
| 142 | } // for |
---|
| 143 | output << ")) "; |
---|
| 144 | } // CodeGenerator::genAttributes |
---|
[7baed7d] | 145 | |
---|
[9857e8d] | 146 | // *** BaseSyntaxNode |
---|
[d22e90f] | 147 | void CodeGenerator::previsit( BaseSyntaxNode * node ) { |
---|
[9857e8d] | 148 | // turn off automatic recursion for all nodes, to allow each visitor to |
---|
| 149 | // precisely control the order in which its children are visited. |
---|
| 150 | visit_children = false; |
---|
[d22e90f] | 151 | updateLocation( node ); |
---|
[9857e8d] | 152 | } |
---|
| 153 | |
---|
| 154 | // *** BaseSyntaxNode |
---|
| 155 | void CodeGenerator::postvisit( BaseSyntaxNode * node ) { |
---|
| 156 | std::stringstream ss; |
---|
| 157 | node->print( ss ); |
---|
| 158 | assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() ); |
---|
| 159 | } |
---|
[7baed7d] | 160 | |
---|
[5f08961d] | 161 | // *** Expression |
---|
| 162 | void CodeGenerator::previsit( Expression * node ) { |
---|
| 163 | previsit( (BaseSyntaxNode *)node ); |
---|
| 164 | GuardAction( [this, node](){ |
---|
[60a8062] | 165 | if ( options.printExprTypes && node->result ) { |
---|
| 166 | output << " /* " << genType( node->result, "", options ) << " */ "; |
---|
| 167 | } |
---|
| 168 | } ); |
---|
[5f08961d] | 169 | } |
---|
| 170 | |
---|
[4810867] | 171 | // *** Declarations |
---|
[9857e8d] | 172 | void CodeGenerator::postvisit( FunctionDecl * functionDecl ) { |
---|
[3ed994e] | 173 | // deleted decls should never be used, so don't print them |
---|
[42a36d9] | 174 | if ( functionDecl->isDeleted && options.genC ) return; |
---|
[8e9cbb2] | 175 | extension( functionDecl ); |
---|
[7baed7d] | 176 | genAttributes( functionDecl->get_attributes() ); |
---|
| 177 | |
---|
[51587aa] | 178 | handleStorageClass( functionDecl ); |
---|
[6e8bd43] | 179 | functionDecl->get_funcSpec().print( output ); |
---|
[dd020c0] | 180 | |
---|
[da09ba1] | 181 | Options subOptions = options; |
---|
[76f7fc7] | 182 | subOptions.anonymousUnused = functionDecl->has_body(); |
---|
[da09ba1] | 183 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), subOptions ); |
---|
[51587aa] | 184 | |
---|
[58dd019] | 185 | asmName( functionDecl ); |
---|
| 186 | |
---|
[51587aa] | 187 | if ( functionDecl->get_statements() ) { |
---|
[9857e8d] | 188 | functionDecl->get_statements()->accept( *visitor ); |
---|
[51587aa] | 189 | } // if |
---|
[3ed994e] | 190 | if ( functionDecl->isDeleted ) { |
---|
| 191 | output << " = void"; |
---|
| 192 | } |
---|
[51587aa] | 193 | } |
---|
| 194 | |
---|
[9857e8d] | 195 | void CodeGenerator::postvisit( ObjectDecl * objectDecl ) { |
---|
[3ed994e] | 196 | // deleted decls should never be used, so don't print them |
---|
[42a36d9] | 197 | if ( objectDecl->isDeleted && options.genC ) return; |
---|
[de8d7fb1] | 198 | |
---|
| 199 | // gcc allows an empty declarator (no name) for bit-fields and C states: 6.7.2.1 Structure and union specifiers, |
---|
| 200 | // point 4, page 113: If the (bit field) value is zero, the declaration shall have no declarator. For anything |
---|
| 201 | // else, the anonymous name refers to the anonymous object for plan9 inheritance. |
---|
| 202 | if ( objectDecl->get_name().empty() && options.genC && ! objectDecl->get_bitfieldWidth() ) { |
---|
[e39241b] | 203 | // only generate an anonymous name when generating C code, otherwise it clutters the output too much |
---|
[d9c8a59] | 204 | static UniqueName name = { "__anonymous_object" }; |
---|
| 205 | objectDecl->set_name( name.newName() ); |
---|
[de8d7fb1] | 206 | // Stops unused parameter warnings. |
---|
| 207 | if ( options.anonymousUnused ) { |
---|
| 208 | objectDecl->attributes.push_back( new Attribute( "unused" ) ); |
---|
| 209 | } |
---|
[d9c8a59] | 210 | } |
---|
| 211 | |
---|
[8e9cbb2] | 212 | extension( objectDecl ); |
---|
[f9cebb5] | 213 | genAttributes( objectDecl->get_attributes() ); |
---|
| 214 | |
---|
[51587aa] | 215 | handleStorageClass( objectDecl ); |
---|
[42a36d9] | 216 | output << genType( objectDecl->get_type(), mangleName( objectDecl ), options.pretty, options.genC ); |
---|
[71f4e4f] | 217 | |
---|
[58dd019] | 218 | asmName( objectDecl ); |
---|
| 219 | |
---|
[51587aa] | 220 | if ( objectDecl->get_init() ) { |
---|
[6c4ff37] | 221 | output << " = "; |
---|
[9857e8d] | 222 | objectDecl->get_init()->accept( *visitor ); |
---|
[51587aa] | 223 | } // if |
---|
[3ed994e] | 224 | if ( objectDecl->isDeleted ) { |
---|
| 225 | output << " = void"; |
---|
| 226 | } |
---|
[3778cb2] | 227 | |
---|
[51587aa] | 228 | if ( objectDecl->get_bitfieldWidth() ) { |
---|
[6c4ff37] | 229 | output << ":"; |
---|
[9857e8d] | 230 | objectDecl->get_bitfieldWidth()->accept( *visitor ); |
---|
[51587aa] | 231 | } // if |
---|
| 232 | } |
---|
| 233 | |
---|
[5f642e38] | 234 | void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) { |
---|
[42a36d9] | 235 | if( ! aggDecl->parameters.empty() && ! options.genC ) { |
---|
[e39241b] | 236 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
| 237 | output << "forall("; |
---|
[0b3b2ae] | 238 | genCommaList( aggDecl->parameters.begin(), aggDecl->parameters.end() ); |
---|
[e39241b] | 239 | output << ")" << endl; |
---|
[e6cee92] | 240 | output << indent; |
---|
[e39241b] | 241 | } |
---|
| 242 | |
---|
[593370c] | 243 | output << kind; |
---|
[0b3b2ae] | 244 | genAttributes( aggDecl->attributes ); |
---|
| 245 | output << aggDecl->name; |
---|
[71f4e4f] | 246 | |
---|
[2c57025] | 247 | if ( aggDecl->has_body() ) { |
---|
[0b3b2ae] | 248 | std::list< Declaration * > & memb = aggDecl->members; |
---|
[94b4364] | 249 | output << " {" << endl; |
---|
[51587aa] | 250 | |
---|
[f7cb0bc] | 251 | ++indent; |
---|
[3778cb2] | 252 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { |
---|
[87e08e24] | 253 | output << indent; |
---|
[9857e8d] | 254 | (*i)->accept( *visitor ); |
---|
[6c4ff37] | 255 | output << ";" << endl; |
---|
[3778cb2] | 256 | } // for |
---|
[51587aa] | 257 | |
---|
[f7cb0bc] | 258 | --indent; |
---|
[51587aa] | 259 | |
---|
[cda48b6] | 260 | output << indent << "}"; |
---|
[17cd4eb] | 261 | } // if |
---|
[51587aa] | 262 | } |
---|
[17cd4eb] | 263 | |
---|
[9857e8d] | 264 | void CodeGenerator::postvisit( StructDecl * structDecl ) { |
---|
[8e9cbb2] | 265 | extension( structDecl ); |
---|
[5f642e38] | 266 | handleAggregate( structDecl, "struct " ); |
---|
[51587aa] | 267 | } |
---|
[17cd4eb] | 268 | |
---|
[9857e8d] | 269 | void CodeGenerator::postvisit( UnionDecl * unionDecl ) { |
---|
[8e9cbb2] | 270 | extension( unionDecl ); |
---|
[5f642e38] | 271 | handleAggregate( unionDecl, "union " ); |
---|
[51587aa] | 272 | } |
---|
[71f4e4f] | 273 | |
---|
[9857e8d] | 274 | void CodeGenerator::postvisit( EnumDecl * enumDecl ) { |
---|
[8e9cbb2] | 275 | extension( enumDecl ); |
---|
| 276 | std::list< Declaration* > &memb = enumDecl->get_members(); |
---|
[d8e2a09] | 277 | if (enumDecl->base && ! memb.empty()) { |
---|
[30d91e4] | 278 | unsigned long long last_val = -1; |
---|
[51587aa] | 279 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
[8688ce1] | 280 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
[51587aa] | 281 | assert( obj ); |
---|
[4390fb6] | 282 | output << "static const "; |
---|
| 283 | output << genType(enumDecl->base, "", options) << " "; |
---|
| 284 | output << mangleName( obj ) << " "; |
---|
| 285 | output << " = "; |
---|
[30d91e4] | 286 | output << "(" << genType(enumDecl->base, "", options) << ")"; |
---|
| 287 | if ( (BasicType *)(enumDecl->base) && ((BasicType *)(enumDecl->base))->isWholeNumber() ) { |
---|
| 288 | if ( obj->get_init() ) { |
---|
| 289 | obj->get_init()->accept( *visitor ); |
---|
| 290 | last_val = ((ConstantExpr *)(((SingleInit *)(obj->init))->value))->constant.get_ival(); |
---|
| 291 | } else { |
---|
| 292 | output << ++last_val; |
---|
| 293 | } // if |
---|
[4390fb6] | 294 | } else { |
---|
[30d91e4] | 295 | if ( obj->get_init() ) { |
---|
[b99fd56] | 296 | obj->get_init()->accept( *visitor ); |
---|
[4390fb6] | 297 | } else { |
---|
[30d91e4] | 298 | // Should not reach here! |
---|
[4390fb6] | 299 | } |
---|
[30d91e4] | 300 | } |
---|
[4390fb6] | 301 | output << ";" << endl; |
---|
[51587aa] | 302 | } // for |
---|
[4390fb6] | 303 | } else { |
---|
| 304 | output << "enum "; |
---|
| 305 | genAttributes( enumDecl->get_attributes() ); |
---|
| 306 | |
---|
| 307 | output << enumDecl->get_name(); |
---|
| 308 | |
---|
| 309 | if ( ! memb.empty() ) { |
---|
| 310 | output << " {" << endl; |
---|
| 311 | |
---|
| 312 | ++indent; |
---|
| 313 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
| 314 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
| 315 | assert( obj ); |
---|
| 316 | output << indent << mangleName( obj ); |
---|
| 317 | if ( obj->get_init() ) { |
---|
| 318 | output << " = "; |
---|
| 319 | obj->get_init()->accept( *visitor ); |
---|
| 320 | } // if |
---|
| 321 | output << "," << endl; |
---|
| 322 | } // for |
---|
[f7cb0bc] | 323 | --indent; |
---|
[cda48b6] | 324 | output << indent << "}"; |
---|
[4390fb6] | 325 | } // if |
---|
[51587aa] | 326 | } // if |
---|
| 327 | } |
---|
[71f4e4f] | 328 | |
---|
[9857e8d] | 329 | void CodeGenerator::postvisit( TraitDecl * traitDecl ) { |
---|
[42a36d9] | 330 | assertf( ! options.genC, "TraitDecls should not reach code generation." ); |
---|
[a984e65] | 331 | extension( traitDecl ); |
---|
| 332 | handleAggregate( traitDecl, "trait " ); |
---|
| 333 | } |
---|
[71f4e4f] | 334 | |
---|
[9857e8d] | 335 | void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { |
---|
[42a36d9] | 336 | assertf( ! options.genC, "Typedefs are removed and substituted in earlier passes." ); |
---|
[e39241b] | 337 | output << "typedef "; |
---|
[42a36d9] | 338 | output << genType( typeDecl->get_base(), typeDecl->get_name(), options ) << endl; |
---|
[51587aa] | 339 | } |
---|
[71f4e4f] | 340 | |
---|
[9857e8d] | 341 | void CodeGenerator::postvisit( TypeDecl * typeDecl ) { |
---|
[42a36d9] | 342 | assertf( ! options.genC, "TypeDecls should not reach code generation." ); |
---|
[ab4bff5] | 343 | output << typeDecl->genTypeString() << " " << typeDecl->name; |
---|
[f0ecf9b] | 344 | if ( typeDecl->sized ) { |
---|
[ab4bff5] | 345 | output << " | sized(" << typeDecl->name << ")"; |
---|
[a0c7dc36] | 346 | } |
---|
[ab4bff5] | 347 | if ( ! typeDecl->assertions.empty() ) { |
---|
[a0c7dc36] | 348 | output << " | { "; |
---|
[ab4bff5] | 349 | for ( DeclarationWithType * assert : typeDecl->assertions ) { |
---|
| 350 | assert->accept( *visitor ); |
---|
| 351 | output << "; "; |
---|
| 352 | } |
---|
[a0c7dc36] | 353 | output << " }"; |
---|
[e39241b] | 354 | } |
---|
[51587aa] | 355 | } |
---|
| 356 | |
---|
[92fea32] | 357 | void CodeGenerator::postvisit( StaticAssertDecl * assertDecl ) { |
---|
| 358 | output << "_Static_assert("; |
---|
| 359 | assertDecl->condition->accept( *visitor ); |
---|
| 360 | output << ", "; |
---|
| 361 | assertDecl->message->accept( *visitor ); |
---|
| 362 | output << ")"; |
---|
| 363 | } |
---|
| 364 | |
---|
[9857e8d] | 365 | void CodeGenerator::postvisit( Designation * designation ) { |
---|
[e4d829b] | 366 | std::list< Expression * > designators = designation->get_designators(); |
---|
[e45215c] | 367 | if ( designators.size() == 0 ) return; |
---|
[e4d829b] | 368 | for ( Expression * des : designators ) { |
---|
[62423350] | 369 | if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) { |
---|
| 370 | // if expression is a NameExpr or VariableExpr, then initializing aggregate member |
---|
| 371 | output << "."; |
---|
[9857e8d] | 372 | des->accept( *visitor ); |
---|
[e4d829b] | 373 | } else { |
---|
[3e54399] | 374 | // otherwise, it has to be a ConstantExpr or CastExpr, initializing array element |
---|
[62423350] | 375 | output << "["; |
---|
[9857e8d] | 376 | des->accept( *visitor ); |
---|
[62423350] | 377 | output << "]"; |
---|
[3778cb2] | 378 | } // if |
---|
| 379 | } // for |
---|
[3eb1653] | 380 | output << " = "; |
---|
[e45215c] | 381 | } |
---|
| 382 | |
---|
[9857e8d] | 383 | void CodeGenerator::postvisit( SingleInit * init ) { |
---|
| 384 | init->get_value()->accept( *visitor ); |
---|
[51587aa] | 385 | } |
---|
| 386 | |
---|
[9857e8d] | 387 | void CodeGenerator::postvisit( ListInit * init ) { |
---|
[e4d829b] | 388 | auto initBegin = init->begin(); |
---|
| 389 | auto initEnd = init->end(); |
---|
| 390 | auto desigBegin = init->get_designations().begin(); |
---|
| 391 | auto desigEnd = init->get_designations().end(); |
---|
| 392 | |
---|
[6c4ff37] | 393 | output << "{ "; |
---|
[e4d829b] | 394 | for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { |
---|
[9857e8d] | 395 | (*desigBegin)->accept( *visitor ); |
---|
| 396 | (*initBegin)->accept( *visitor ); |
---|
[e4d829b] | 397 | ++initBegin, ++desigBegin; |
---|
| 398 | if ( initBegin != initEnd ) { |
---|
| 399 | output << ", "; |
---|
| 400 | } |
---|
| 401 | } |
---|
[6c4ff37] | 402 | output << " }"; |
---|
[e4d829b] | 403 | assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() ); |
---|
[51587aa] | 404 | } |
---|
| 405 | |
---|
[d22e90f] | 406 | void CodeGenerator::postvisit( ConstructorInit * init ){ |
---|
[42a36d9] | 407 | assertf( ! options.genC, "ConstructorInit nodes should not reach code generation." ); |
---|
[e6cee92] | 408 | // pseudo-output for constructor/destructor pairs |
---|
[d22e90f] | 409 | output << "<ctorinit>{" << endl << ++indent << "ctor: "; |
---|
[9857e8d] | 410 | maybeAccept( init->get_ctor(), *visitor ); |
---|
[d22e90f] | 411 | output << ", " << endl << indent << "dtor: "; |
---|
[9857e8d] | 412 | maybeAccept( init->get_dtor(), *visitor ); |
---|
[d22e90f] | 413 | output << endl << --indent << "}"; |
---|
[fc638d2] | 414 | } |
---|
| 415 | |
---|
[9857e8d] | 416 | void CodeGenerator::postvisit( Constant * constant ) { |
---|
[d7312ac] | 417 | output << constant->get_value(); |
---|
[51587aa] | 418 | } |
---|
| 419 | |
---|
[4810867] | 420 | // *** Expressions |
---|
[9857e8d] | 421 | void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) { |
---|
[e04ef3a] | 422 | extension( applicationExpr ); |
---|
[8688ce1] | 423 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
[60a8062] | 424 | const OperatorInfo * opInfo; |
---|
| 425 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && ( opInfo = operatorLookup( varExpr->get_var()->get_name() ) ) ) { |
---|
[51587aa] | 426 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
[60a8062] | 427 | switch ( opInfo->type ) { |
---|
[51587aa] | 428 | case OT_INDEX: |
---|
| 429 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[9857e8d] | 430 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 431 | output << "["; |
---|
[9857e8d] | 432 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 433 | output << "]"; |
---|
[51587aa] | 434 | break; |
---|
[71f4e4f] | 435 | |
---|
[51587aa] | 436 | case OT_CALL: |
---|
[356189a] | 437 | // there are no intrinsic definitions of the function call operator |
---|
[51587aa] | 438 | assert( false ); |
---|
| 439 | break; |
---|
[71f4e4f] | 440 | |
---|
[f1e012b] | 441 | case OT_CTOR: |
---|
[c2ce2350] | 442 | case OT_DTOR: |
---|
[356189a] | 443 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 444 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 445 | // effects, so must still output this expression |
---|
[64071c2] | 446 | output << "("; |
---|
[9857e8d] | 447 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 448 | output << ") /* " << opInfo->inputName << " */"; |
---|
[356189a] | 449 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
[c2ce2350] | 450 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
[356189a] | 451 | output << "("; |
---|
[9857e8d] | 452 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 453 | output << opInfo->symbol; |
---|
[9857e8d] | 454 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 455 | output << ") /* " << opInfo->inputName << " */"; |
---|
[356189a] | 456 | } else { |
---|
[c2ce2350] | 457 | // no constructors with 0 or more than 2 parameters |
---|
[356189a] | 458 | assert( false ); |
---|
[8688ce1] | 459 | } // if |
---|
[356189a] | 460 | break; |
---|
[f1e012b] | 461 | |
---|
[51587aa] | 462 | case OT_PREFIX: |
---|
| 463 | case OT_PREFIXASSIGN: |
---|
| 464 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 465 | output << "("; |
---|
[60a8062] | 466 | output << opInfo->symbol; |
---|
[9857e8d] | 467 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 468 | output << ")"; |
---|
[51587aa] | 469 | break; |
---|
[71f4e4f] | 470 | |
---|
[51587aa] | 471 | case OT_POSTFIX: |
---|
| 472 | case OT_POSTFIXASSIGN: |
---|
| 473 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[9857e8d] | 474 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 475 | output << opInfo->symbol; |
---|
[51587aa] | 476 | break; |
---|
| 477 | |
---|
[f1e012b] | 478 | |
---|
[51587aa] | 479 | case OT_INFIX: |
---|
| 480 | case OT_INFIXASSIGN: |
---|
| 481 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 482 | output << "("; |
---|
[9857e8d] | 483 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 484 | output << opInfo->symbol; |
---|
[9857e8d] | 485 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 486 | output << ")"; |
---|
[51587aa] | 487 | break; |
---|
[71f4e4f] | 488 | |
---|
[51587aa] | 489 | case OT_CONSTANT: |
---|
[721f17a] | 490 | case OT_LABELADDRESS: |
---|
| 491 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[51587aa] | 492 | assert( false ); |
---|
[3778cb2] | 493 | } // switch |
---|
[17cd4eb] | 494 | } else { |
---|
[9857e8d] | 495 | varExpr->accept( *visitor ); |
---|
[6c4ff37] | 496 | output << "("; |
---|
[51587aa] | 497 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 498 | output << ")"; |
---|
[17cd4eb] | 499 | } // if |
---|
[51587aa] | 500 | } else { |
---|
[9857e8d] | 501 | applicationExpr->get_function()->accept( *visitor ); |
---|
[6c4ff37] | 502 | output << "("; |
---|
[51587aa] | 503 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 504 | output << ")"; |
---|
[51587aa] | 505 | } // if |
---|
| 506 | } |
---|
[71f4e4f] | 507 | |
---|
[9857e8d] | 508 | void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) { |
---|
[e04ef3a] | 509 | extension( untypedExpr ); |
---|
[22bc276] | 510 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) { |
---|
[60a8062] | 511 | const OperatorInfo * opInfo = operatorLookup( nameExpr->name ); |
---|
| 512 | if ( opInfo ) { |
---|
[22bc276] | 513 | std::list< Expression* >::iterator arg = untypedExpr->args.begin(); |
---|
[60a8062] | 514 | switch ( opInfo->type ) { |
---|
[51587aa] | 515 | case OT_INDEX: |
---|
[22bc276] | 516 | assert( untypedExpr->args.size() == 2 ); |
---|
[9857e8d] | 517 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 518 | output << "["; |
---|
[9857e8d] | 519 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 520 | output << "]"; |
---|
[51587aa] | 521 | break; |
---|
[71f4e4f] | 522 | |
---|
[51587aa] | 523 | case OT_CALL: |
---|
[f1e012b] | 524 | assert( false ); |
---|
| 525 | |
---|
[c2ce2350] | 526 | case OT_CTOR: |
---|
| 527 | case OT_DTOR: |
---|
[22bc276] | 528 | if ( untypedExpr->args.size() == 1 ) { |
---|
[8e9cbb2] | 529 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 530 | // effects, so must still output this expression |
---|
[64071c2] | 531 | output << "("; |
---|
[9857e8d] | 532 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 533 | output << ") /* " << opInfo->inputName << " */"; |
---|
[c2ce2350] | 534 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
| 535 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
| 536 | output << "("; |
---|
[9857e8d] | 537 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 538 | output << opInfo->symbol; |
---|
[9857e8d] | 539 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 540 | output << ") /* " << opInfo->inputName << " */"; |
---|
[c2ce2350] | 541 | } else { |
---|
| 542 | // no constructors with 0 or more than 2 parameters |
---|
[42a36d9] | 543 | assertf( ! options.genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." ); |
---|
[4e8949f] | 544 | output << "("; |
---|
| 545 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 546 | output << opInfo->symbol << "{ "; |
---|
[22bc276] | 547 | genCommaList( arg, untypedExpr->args.end() ); |
---|
[60a8062] | 548 | output << "}) /* " << opInfo->inputName << " */"; |
---|
[3778cb2] | 549 | } // if |
---|
[51587aa] | 550 | break; |
---|
[71f4e4f] | 551 | |
---|
[51587aa] | 552 | case OT_PREFIX: |
---|
| 553 | case OT_PREFIXASSIGN: |
---|
[de62360d] | 554 | case OT_LABELADDRESS: |
---|
[22bc276] | 555 | assert( untypedExpr->args.size() == 1 ); |
---|
[6c4ff37] | 556 | output << "("; |
---|
[60a8062] | 557 | output << opInfo->symbol; |
---|
[9857e8d] | 558 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 559 | output << ")"; |
---|
[51587aa] | 560 | break; |
---|
[71f4e4f] | 561 | |
---|
[51587aa] | 562 | case OT_POSTFIX: |
---|
| 563 | case OT_POSTFIXASSIGN: |
---|
[22bc276] | 564 | assert( untypedExpr->args.size() == 1 ); |
---|
[9857e8d] | 565 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 566 | output << opInfo->symbol; |
---|
[51587aa] | 567 | break; |
---|
[71f4e4f] | 568 | |
---|
[51587aa] | 569 | case OT_INFIX: |
---|
| 570 | case OT_INFIXASSIGN: |
---|
[22bc276] | 571 | assert( untypedExpr->args.size() == 2 ); |
---|
[6c4ff37] | 572 | output << "("; |
---|
[9857e8d] | 573 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 574 | output << opInfo->symbol; |
---|
[9857e8d] | 575 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 576 | output << ")"; |
---|
[51587aa] | 577 | break; |
---|
[71f4e4f] | 578 | |
---|
[51587aa] | 579 | case OT_CONSTANT: |
---|
| 580 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
| 581 | assert( false ); |
---|
[3778cb2] | 582 | } // switch |
---|
[51587aa] | 583 | } else { |
---|
[22bc276] | 584 | // builtin routines |
---|
| 585 | nameExpr->accept( *visitor ); |
---|
| 586 | output << "("; |
---|
| 587 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
| 588 | output << ")"; |
---|
[51587aa] | 589 | } // if |
---|
| 590 | } else { |
---|
[22bc276] | 591 | untypedExpr->function->accept( *visitor ); |
---|
[6c4ff37] | 592 | output << "("; |
---|
[22bc276] | 593 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
[6c4ff37] | 594 | output << ")"; |
---|
[51587aa] | 595 | } // if |
---|
| 596 | } |
---|
[71f4e4f] | 597 | |
---|
[9857e8d] | 598 | void CodeGenerator::postvisit( RangeExpr * rangeExpr ) { |
---|
[22bc276] | 599 | rangeExpr->low->accept( *visitor ); |
---|
[064e3ff] | 600 | output << " ... "; |
---|
[22bc276] | 601 | rangeExpr->high->accept( *visitor ); |
---|
[064e3ff] | 602 | } |
---|
| 603 | |
---|
[9857e8d] | 604 | void CodeGenerator::postvisit( NameExpr * nameExpr ) { |
---|
[e04ef3a] | 605 | extension( nameExpr ); |
---|
[60a8062] | 606 | const OperatorInfo * opInfo = operatorLookup( nameExpr->name ); |
---|
| 607 | if ( opInfo ) { |
---|
| 608 | if ( opInfo->type == OT_CONSTANT ) { |
---|
| 609 | output << opInfo->symbol; |
---|
[5ea7a22] | 610 | } else { |
---|
[60a8062] | 611 | output << opInfo->outputName; |
---|
[5ea7a22] | 612 | } |
---|
[51587aa] | 613 | } else { |
---|
[6c4ff37] | 614 | output << nameExpr->get_name(); |
---|
[51587aa] | 615 | } // if |
---|
| 616 | } |
---|
[71f4e4f] | 617 | |
---|
[6e50a6b] | 618 | void CodeGenerator::postvisit( DimensionExpr * dimensionExpr ) { |
---|
| 619 | extension( dimensionExpr ); |
---|
| 620 | output << "/*non-type*/" << dimensionExpr->get_name(); |
---|
| 621 | } |
---|
| 622 | |
---|
[9857e8d] | 623 | void CodeGenerator::postvisit( AddressExpr * addressExpr ) { |
---|
[e04ef3a] | 624 | extension( addressExpr ); |
---|
[6c4ff37] | 625 | output << "(&"; |
---|
[9857e8d] | 626 | addressExpr->arg->accept( *visitor ); |
---|
[6c4ff37] | 627 | output << ")"; |
---|
[51587aa] | 628 | } |
---|
| 629 | |
---|
[9857e8d] | 630 | void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) { |
---|
[5809461] | 631 | extension( addressExpr ); |
---|
| 632 | output << "(&&" << addressExpr->arg << ")"; |
---|
| 633 | } |
---|
| 634 | |
---|
[9857e8d] | 635 | void CodeGenerator::postvisit( CastExpr * castExpr ) { |
---|
[e04ef3a] | 636 | extension( castExpr ); |
---|
[803deb1] | 637 | output << "("; |
---|
[906e24d] | 638 | if ( castExpr->get_result()->isVoid() ) { |
---|
[d7312ac] | 639 | output << "(void)"; |
---|
[d104b02] | 640 | } else { |
---|
| 641 | // at least one result type of cast. |
---|
| 642 | // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write |
---|
| 643 | // an lvalue cast, this has been taken out. |
---|
[803deb1] | 644 | output << "("; |
---|
[42a36d9] | 645 | output << genType( castExpr->get_result(), "", options ); |
---|
[71f4e4f] | 646 | output << ")"; |
---|
[3778cb2] | 647 | } // if |
---|
[da9d79b] | 648 | castExpr->arg->accept( *visitor ); |
---|
| 649 | output << ")"; |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | void CodeGenerator::postvisit( KeywordCastExpr * castExpr ) { |
---|
[42a36d9] | 653 | assertf( ! options.genC, "KeywordCast should not reach code generation." ); |
---|
[da9d79b] | 654 | extension( castExpr ); |
---|
| 655 | output << "((" << castExpr->targetString() << " &)"; |
---|
| 656 | castExpr->arg->accept( *visitor ); |
---|
[803deb1] | 657 | output << ")"; |
---|
[51587aa] | 658 | } |
---|
[71f4e4f] | 659 | |
---|
[9857e8d] | 660 | void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) { |
---|
[42a36d9] | 661 | assertf( ! options.genC, "VirtualCastExpr should not reach code generation." ); |
---|
[a5f0529] | 662 | extension( castExpr ); |
---|
| 663 | output << "(virtual "; |
---|
[9857e8d] | 664 | castExpr->get_arg()->accept( *visitor ); |
---|
[a5f0529] | 665 | output << ")"; |
---|
| 666 | } |
---|
| 667 | |
---|
[9857e8d] | 668 | void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) { |
---|
[42a36d9] | 669 | assertf( ! options.genC, "UntypedMemberExpr should not reach code generation." ); |
---|
[e39241b] | 670 | extension( memberExpr ); |
---|
[9857e8d] | 671 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
[5f642e38] | 672 | output << "."; |
---|
[9857e8d] | 673 | memberExpr->get_member()->accept( *visitor ); |
---|
[51587aa] | 674 | } |
---|
[71f4e4f] | 675 | |
---|
[9857e8d] | 676 | void CodeGenerator::postvisit( MemberExpr * memberExpr ) { |
---|
[e04ef3a] | 677 | extension( memberExpr ); |
---|
[9857e8d] | 678 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
[6c4ff37] | 679 | output << "." << mangleName( memberExpr->get_member() ); |
---|
[51587aa] | 680 | } |
---|
[71f4e4f] | 681 | |
---|
[9857e8d] | 682 | void CodeGenerator::postvisit( VariableExpr * variableExpr ) { |
---|
[e04ef3a] | 683 | extension( variableExpr ); |
---|
[60a8062] | 684 | const OperatorInfo * opInfo; |
---|
[b99fd56] | 685 | if( dynamic_cast<ZeroType*>( variableExpr->get_var()->get_type() ) ) { |
---|
| 686 | output << "0"; |
---|
| 687 | } else if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && (opInfo = operatorLookup( variableExpr->get_var()->get_name() )) && opInfo->type == OT_CONSTANT ) { |
---|
[60a8062] | 688 | output << opInfo->symbol; |
---|
[51587aa] | 689 | } else { |
---|
[b99fd56] | 690 | // if (dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type()) |
---|
[4390fb6] | 691 | // && dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type())->baseEnum->base) { |
---|
| 692 | // output << '(' <<genType(dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type())->baseEnum->base, "", options) << ')'; |
---|
| 693 | // } |
---|
[6c4ff37] | 694 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 695 | } // if |
---|
| 696 | } |
---|
[71f4e4f] | 697 | |
---|
[9857e8d] | 698 | void CodeGenerator::postvisit( ConstantExpr * constantExpr ) { |
---|
[51587aa] | 699 | assert( constantExpr->get_constant() ); |
---|
[e04ef3a] | 700 | extension( constantExpr ); |
---|
[9857e8d] | 701 | constantExpr->get_constant()->accept( *visitor ); |
---|
[51587aa] | 702 | } |
---|
[71f4e4f] | 703 | |
---|
[9857e8d] | 704 | void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) { |
---|
[e04ef3a] | 705 | extension( sizeofExpr ); |
---|
[6c4ff37] | 706 | output << "sizeof("; |
---|
[51587aa] | 707 | if ( sizeofExpr->get_isType() ) { |
---|
[42a36d9] | 708 | output << genType( sizeofExpr->get_type(), "", options ); |
---|
[51587aa] | 709 | } else { |
---|
[9857e8d] | 710 | sizeofExpr->get_expr()->accept( *visitor ); |
---|
[51587aa] | 711 | } // if |
---|
[6c4ff37] | 712 | output << ")"; |
---|
[51587aa] | 713 | } |
---|
[47534159] | 714 | |
---|
[9857e8d] | 715 | void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) { |
---|
[47534159] | 716 | // use GCC extension to avoid bumping std to C11 |
---|
[8e9cbb2] | 717 | extension( alignofExpr ); |
---|
[47534159] | 718 | output << "__alignof__("; |
---|
[25a054f] | 719 | if ( alignofExpr->get_isType() ) { |
---|
[42a36d9] | 720 | output << genType( alignofExpr->get_type(), "", options ); |
---|
[47534159] | 721 | } else { |
---|
[9857e8d] | 722 | alignofExpr->get_expr()->accept( *visitor ); |
---|
[47534159] | 723 | } // if |
---|
| 724 | output << ")"; |
---|
| 725 | } |
---|
[71f4e4f] | 726 | |
---|
[9857e8d] | 727 | void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) { |
---|
[42a36d9] | 728 | assertf( ! options.genC, "UntypedOffsetofExpr should not reach code generation." ); |
---|
[e39241b] | 729 | output << "offsetof("; |
---|
[42a36d9] | 730 | output << genType( offsetofExpr->get_type(), "", options ); |
---|
[e39241b] | 731 | output << ", " << offsetofExpr->get_member(); |
---|
| 732 | output << ")"; |
---|
[2a4b088] | 733 | } |
---|
| 734 | |
---|
[9857e8d] | 735 | void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) { |
---|
[25a054f] | 736 | // use GCC builtin |
---|
| 737 | output << "__builtin_offsetof("; |
---|
[42a36d9] | 738 | output << genType( offsetofExpr->get_type(), "", options ); |
---|
[e551c69] | 739 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
[25a054f] | 740 | output << ")"; |
---|
| 741 | } |
---|
[d63eeb0] | 742 | |
---|
[9857e8d] | 743 | void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) { |
---|
[42a36d9] | 744 | assertf( ! options.genC, "OffsetPackExpr should not reach code generation." ); |
---|
| 745 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", options ) << ")"; |
---|
[afc1045] | 746 | } |
---|
[70a06f6] | 747 | |
---|
[9857e8d] | 748 | void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) { |
---|
[e04ef3a] | 749 | extension( logicalExpr ); |
---|
[6c4ff37] | 750 | output << "("; |
---|
[9857e8d] | 751 | logicalExpr->get_arg1()->accept( *visitor ); |
---|
[51587aa] | 752 | if ( logicalExpr->get_isAnd() ) { |
---|
[6c4ff37] | 753 | output << " && "; |
---|
[51587aa] | 754 | } else { |
---|
[6c4ff37] | 755 | output << " || "; |
---|
[51587aa] | 756 | } // if |
---|
[9857e8d] | 757 | logicalExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 758 | output << ")"; |
---|
[51587aa] | 759 | } |
---|
[71f4e4f] | 760 | |
---|
[9857e8d] | 761 | void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) { |
---|
[e04ef3a] | 762 | extension( conditionalExpr ); |
---|
[6c4ff37] | 763 | output << "("; |
---|
[9857e8d] | 764 | conditionalExpr->get_arg1()->accept( *visitor ); |
---|
[6c4ff37] | 765 | output << " ? "; |
---|
[9857e8d] | 766 | conditionalExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 767 | output << " : "; |
---|
[9857e8d] | 768 | conditionalExpr->get_arg3()->accept( *visitor ); |
---|
[6c4ff37] | 769 | output << ")"; |
---|
[51587aa] | 770 | } |
---|
[71f4e4f] | 771 | |
---|
[9857e8d] | 772 | void CodeGenerator::postvisit( CommaExpr * commaExpr ) { |
---|
[e04ef3a] | 773 | extension( commaExpr ); |
---|
[6c4ff37] | 774 | output << "("; |
---|
[42a36d9] | 775 | if ( options.genC ) { |
---|
[8a6cf7e] | 776 | // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings. |
---|
| 777 | commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); |
---|
| 778 | } |
---|
[9857e8d] | 779 | commaExpr->get_arg1()->accept( *visitor ); |
---|
[6c4ff37] | 780 | output << " , "; |
---|
[9857e8d] | 781 | commaExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 782 | output << ")"; |
---|
[51587aa] | 783 | } |
---|
[71f4e4f] | 784 | |
---|
[9857e8d] | 785 | void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) { |
---|
[42a36d9] | 786 | assertf( ! options.genC, "TupleAssignExpr should not reach code generation." ); |
---|
[9857e8d] | 787 | tupleExpr->stmtExpr->accept( *visitor ); |
---|
[d104b02] | 788 | } |
---|
| 789 | |
---|
[9857e8d] | 790 | void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) { |
---|
[42a36d9] | 791 | assertf( ! options.genC, "UntypedTupleExpr should not reach code generation." ); |
---|
[f975c65] | 792 | extension( tupleExpr ); |
---|
[e39241b] | 793 | output << "["; |
---|
| 794 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 795 | output << "]"; |
---|
| 796 | } |
---|
[907eccb] | 797 | |
---|
[9857e8d] | 798 | void CodeGenerator::postvisit( TupleExpr * tupleExpr ) { |
---|
[42a36d9] | 799 | assertf( ! options.genC, "TupleExpr should not reach code generation." ); |
---|
[f975c65] | 800 | extension( tupleExpr ); |
---|
[e39241b] | 801 | output << "["; |
---|
| 802 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 803 | output << "]"; |
---|
| 804 | } |
---|
[71f4e4f] | 805 | |
---|
[9857e8d] | 806 | void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) { |
---|
[42a36d9] | 807 | assertf( ! options.genC, "TupleIndexExpr should not reach code generation." ); |
---|
[f975c65] | 808 | extension( tupleExpr ); |
---|
[9857e8d] | 809 | tupleExpr->get_tuple()->accept( *visitor ); |
---|
[f975c65] | 810 | output << "." << tupleExpr->get_index(); |
---|
| 811 | } |
---|
| 812 | |
---|
[9857e8d] | 813 | void CodeGenerator::postvisit( TypeExpr * typeExpr ) { |
---|
[42a36d9] | 814 | // if ( options.genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; |
---|
| 815 | // assertf( ! options.genC, "TypeExpr should not reach code generation." ); |
---|
| 816 | if ( ! options.genC ) { |
---|
| 817 | output << genType( typeExpr->get_type(), "", options ); |
---|
[e4d829b] | 818 | } |
---|
[e39241b] | 819 | } |
---|
[2b6c1e0] | 820 | |
---|
[9857e8d] | 821 | void CodeGenerator::postvisit( AsmExpr * asmExpr ) { |
---|
[665f432] | 822 | if ( !asmExpr->inout.empty() ) { |
---|
[7f5566b] | 823 | output << "[ "; |
---|
[665f432] | 824 | output << asmExpr->inout; |
---|
[7f5566b] | 825 | output << " ] "; |
---|
| 826 | } // if |
---|
[665f432] | 827 | asmExpr->constraint->accept( *visitor ); |
---|
[7f5566b] | 828 | output << " ( "; |
---|
[665f432] | 829 | asmExpr->operand->accept( *visitor ); |
---|
[7f5566b] | 830 | output << " )"; |
---|
| 831 | } |
---|
| 832 | |
---|
[9857e8d] | 833 | void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) { |
---|
[fbcde64] | 834 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); |
---|
[42a36d9] | 835 | output << "(" << genType( compLitExpr->get_result(), "", options ) << ")"; |
---|
[9857e8d] | 836 | compLitExpr->get_initializer()->accept( *visitor ); |
---|
[3c13c03] | 837 | } |
---|
| 838 | |
---|
[9857e8d] | 839 | void CodeGenerator::postvisit( UniqueExpr * unqExpr ) { |
---|
[42a36d9] | 840 | assertf( ! options.genC, "Unique expressions should not reach code generation." ); |
---|
[d104b02] | 841 | output << "unq<" << unqExpr->get_id() << ">{ "; |
---|
[9857e8d] | 842 | unqExpr->get_expr()->accept( *visitor ); |
---|
[d104b02] | 843 | output << " }"; |
---|
| 844 | } |
---|
| 845 | |
---|
[9857e8d] | 846 | void CodeGenerator::postvisit( StmtExpr * stmtExpr ) { |
---|
[44b4114] | 847 | std::list< Statement * > & stmts = stmtExpr->statements->kids; |
---|
[d22e90f] | 848 | output << "({" << endl; |
---|
[f7cb0bc] | 849 | ++indent; |
---|
[3c13c03] | 850 | unsigned int numStmts = stmts.size(); |
---|
| 851 | unsigned int i = 0; |
---|
| 852 | for ( Statement * stmt : stmts ) { |
---|
[058f549] | 853 | output << indent << printLabels( stmt->get_labels() ); |
---|
[3c13c03] | 854 | if ( i+1 == numStmts ) { |
---|
| 855 | // last statement in a statement expression needs to be handled specially - |
---|
| 856 | // cannot cast to void, otherwise the expression statement has no value |
---|
| 857 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { |
---|
[44b4114] | 858 | exprStmt->expr->accept( *visitor ); |
---|
[3c13c03] | 859 | output << ";" << endl; |
---|
| 860 | ++i; |
---|
| 861 | break; |
---|
| 862 | } |
---|
| 863 | } |
---|
[9857e8d] | 864 | stmt->accept( *visitor ); |
---|
[3c13c03] | 865 | output << endl; |
---|
| 866 | if ( wantSpacing( stmt ) ) { |
---|
| 867 | output << endl; |
---|
| 868 | } // if |
---|
| 869 | ++i; |
---|
| 870 | } |
---|
[f7cb0bc] | 871 | --indent; |
---|
[3c13c03] | 872 | output << indent << "})"; |
---|
[6eb8948] | 873 | } |
---|
| 874 | |
---|
[4e8949f] | 875 | void CodeGenerator::postvisit( ConstructorExpr * expr ) { |
---|
[42a36d9] | 876 | assertf( ! options.genC, "Unique expressions should not reach code generation." ); |
---|
[4e8949f] | 877 | expr->callExpr->accept( *visitor ); |
---|
| 878 | } |
---|
| 879 | |
---|
[44b4114] | 880 | void CodeGenerator::postvisit( DeletedExpr * expr ) { |
---|
[42a36d9] | 881 | assertf( ! options.genC, "Deleted expressions should not reach code generation." ); |
---|
[44b4114] | 882 | expr->expr->accept( *visitor ); |
---|
| 883 | } |
---|
| 884 | |
---|
[0f79853] | 885 | void CodeGenerator::postvisit( DefaultArgExpr * arg ) { |
---|
[42a36d9] | 886 | assertf( ! options.genC, "Default argument expressions should not reach code generation." ); |
---|
[0f79853] | 887 | arg->expr->accept( *visitor ); |
---|
| 888 | } |
---|
| 889 | |
---|
[d807ca28] | 890 | void CodeGenerator::postvisit( GenericExpr * expr ) { |
---|
[42a36d9] | 891 | assertf( ! options.genC, "C11 _Generic expressions should not reach code generation." ); |
---|
[d807ca28] | 892 | output << "_Generic("; |
---|
| 893 | expr->control->accept( *visitor ); |
---|
| 894 | output << ", "; |
---|
| 895 | unsigned int numAssocs = expr->associations.size(); |
---|
| 896 | unsigned int i = 0; |
---|
| 897 | for ( GenericExpr::Association & assoc : expr->associations ) { |
---|
| 898 | if (assoc.isDefault) { |
---|
| 899 | output << "default: "; |
---|
| 900 | } else { |
---|
[42a36d9] | 901 | output << genType( assoc.type, "", options ) << ": "; |
---|
[d807ca28] | 902 | } |
---|
| 903 | assoc.expr->accept( *visitor ); |
---|
| 904 | if ( i+1 != numAssocs ) { |
---|
| 905 | output << ", "; |
---|
| 906 | } |
---|
| 907 | i++; |
---|
| 908 | } |
---|
| 909 | output << ")"; |
---|
| 910 | } |
---|
| 911 | |
---|
| 912 | |
---|
[4810867] | 913 | // *** Statements |
---|
[9857e8d] | 914 | void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) { |
---|
[51587aa] | 915 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
[2b6c1e0] | 916 | output << "{" << endl; |
---|
[51587aa] | 917 | |
---|
[f7cb0bc] | 918 | ++indent; |
---|
[51587aa] | 919 | |
---|
[7f5566b] | 920 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
[cda48b6] | 921 | output << indent << printLabels( (*i)->get_labels() ); |
---|
[9857e8d] | 922 | (*i)->accept( *visitor ); |
---|
[2b6c1e0] | 923 | |
---|
[6c4ff37] | 924 | output << endl; |
---|
[2b6c1e0] | 925 | if ( wantSpacing( *i ) ) { |
---|
| 926 | output << endl; |
---|
[3778cb2] | 927 | } // if |
---|
[8688ce1] | 928 | } // for |
---|
[f7cb0bc] | 929 | --indent; |
---|
[51587aa] | 930 | |
---|
[cda48b6] | 931 | output << indent << "}"; |
---|
[51587aa] | 932 | } |
---|
| 933 | |
---|
[9857e8d] | 934 | void CodeGenerator::postvisit( ExprStmt * exprStmt ) { |
---|
[6c4ff37] | 935 | assert( exprStmt ); |
---|
[42a36d9] | 936 | if ( options.genC ) { |
---|
[262f085f] | 937 | // cast the top-level expression to void to reduce gcc warnings. |
---|
[8a6cf7e] | 938 | exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); |
---|
[262f085f] | 939 | } |
---|
[9857e8d] | 940 | exprStmt->get_expr()->accept( *visitor ); |
---|
[3eb1653] | 941 | output << ";"; |
---|
[51587aa] | 942 | } |
---|
| 943 | |
---|
[9857e8d] | 944 | void CodeGenerator::postvisit( AsmStmt * asmStmt ) { |
---|
[7f5566b] | 945 | output << "asm "; |
---|
| 946 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
| 947 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
| 948 | output << "( "; |
---|
[9857e8d] | 949 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
[7f5566b] | 950 | output << " : "; |
---|
| 951 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
| 952 | output << " : "; |
---|
| 953 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
| 954 | output << " : "; |
---|
| 955 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
| 956 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
| 957 | output << " : "; |
---|
| 958 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
| 959 | output << *begin++; |
---|
| 960 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
| 961 | output << ", "; |
---|
| 962 | } // for |
---|
| 963 | } // if |
---|
[d7312ac] | 964 | output << " );"; |
---|
[7f5566b] | 965 | } |
---|
| 966 | |
---|
[9857e8d] | 967 | void CodeGenerator::postvisit( AsmDecl * asmDecl ) { |
---|
[e994912] | 968 | output << "asm "; |
---|
| 969 | AsmStmt * asmStmt = asmDecl->get_stmt(); |
---|
| 970 | output << "( "; |
---|
[9857e8d] | 971 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
[d7312ac] | 972 | output << " )"; |
---|
[e994912] | 973 | } |
---|
| 974 | |
---|
[2d019af] | 975 | void CodeGenerator::postvisit( DirectiveDecl * directiveDecl ) { |
---|
| 976 | output << endl << directiveDecl->get_stmt()->directive; // endl prevents spaces before directive |
---|
| 977 | } |
---|
| 978 | |
---|
[cc32d83] | 979 | void CodeGenerator::postvisit( DirectiveStmt * dirStmt ) { |
---|
[d7312ac] | 980 | output << endl << dirStmt->directive; // endl prevents spaces before directive |
---|
[cc32d83] | 981 | } |
---|
| 982 | |
---|
[9857e8d] | 983 | void CodeGenerator::postvisit( IfStmt * ifStmt ) { |
---|
[7f5566b] | 984 | output << "if ( "; |
---|
[9857e8d] | 985 | ifStmt->get_condition()->accept( *visitor ); |
---|
[7f5566b] | 986 | output << " ) "; |
---|
[51587aa] | 987 | |
---|
[3b0bc16] | 988 | ifStmt->get_then()->accept( *visitor ); |
---|
[51587aa] | 989 | |
---|
[3b0bc16] | 990 | if ( ifStmt->get_else() != 0) { |
---|
[2b6c1e0] | 991 | output << " else "; |
---|
[3b0bc16] | 992 | ifStmt->get_else()->accept( *visitor ); |
---|
[51587aa] | 993 | } // if |
---|
| 994 | } |
---|
| 995 | |
---|
[9857e8d] | 996 | void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { |
---|
[d7312ac] | 997 | output << "switch ( "; |
---|
[9857e8d] | 998 | switchStmt->get_condition()->accept( *visitor ); |
---|
[7f5566b] | 999 | output << " ) "; |
---|
[71f4e4f] | 1000 | |
---|
[d22e90f] | 1001 | output << "{" << endl; |
---|
[f7cb0bc] | 1002 | ++indent; |
---|
[9857e8d] | 1003 | acceptAll( switchStmt->get_statements(), *visitor ); |
---|
[f7cb0bc] | 1004 | --indent; |
---|
[cda48b6] | 1005 | output << indent << "}"; |
---|
[51587aa] | 1006 | } |
---|
| 1007 | |
---|
[9857e8d] | 1008 | void CodeGenerator::postvisit( CaseStmt * caseStmt ) { |
---|
[8bafacc] | 1009 | updateLocation( caseStmt ); |
---|
[1dcd9554] | 1010 | output << indent; |
---|
[eb3261f] | 1011 | if ( caseStmt->isDefault()) { |
---|
[2b6c1e0] | 1012 | output << "default"; |
---|
[eb3261f] | 1013 | } else { |
---|
[2b6c1e0] | 1014 | output << "case "; |
---|
[9857e8d] | 1015 | caseStmt->get_condition()->accept( *visitor ); |
---|
[17cd4eb] | 1016 | } // if |
---|
[d22e90f] | 1017 | output << ":" << endl; |
---|
[71f4e4f] | 1018 | |
---|
[51587aa] | 1019 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
| 1020 | |
---|
[f7cb0bc] | 1021 | ++indent; |
---|
[51587aa] | 1022 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
[d7312ac] | 1023 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
[9857e8d] | 1024 | (*i)->accept( *visitor ); |
---|
[6c4ff37] | 1025 | output << endl; |
---|
[3778cb2] | 1026 | } // for |
---|
[f7cb0bc] | 1027 | --indent; |
---|
[51587aa] | 1028 | } |
---|
| 1029 | |
---|
[9857e8d] | 1030 | void CodeGenerator::postvisit( BranchStmt * branchStmt ) { |
---|
[51587aa] | 1031 | switch ( branchStmt->get_type()) { |
---|
| 1032 | case BranchStmt::Goto: |
---|
| 1033 | if ( ! branchStmt->get_target().empty() ) |
---|
[6c4ff37] | 1034 | output << "goto " << branchStmt->get_target(); |
---|
[71f4e4f] | 1035 | else { |
---|
[51587aa] | 1036 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
[6c4ff37] | 1037 | output << "goto *"; |
---|
[9857e8d] | 1038 | branchStmt->get_computedTarget()->accept( *visitor ); |
---|
[51587aa] | 1039 | } // if |
---|
| 1040 | } // if |
---|
| 1041 | break; |
---|
| 1042 | case BranchStmt::Break: |
---|
[6c4ff37] | 1043 | output << "break"; |
---|
[51587aa] | 1044 | break; |
---|
| 1045 | case BranchStmt::Continue: |
---|
[6c4ff37] | 1046 | output << "continue"; |
---|
[51587aa] | 1047 | break; |
---|
[7c2a7b6] | 1048 | case BranchStmt::FallThrough: |
---|
| 1049 | case BranchStmt::FallThroughDefault: |
---|
[42a36d9] | 1050 | assertf( ! options.genC, "fallthru should not reach code generation." ); |
---|
[60a8062] | 1051 | output << "fallthru"; |
---|
[7c2a7b6] | 1052 | break; |
---|
[6180274] | 1053 | default: ; // prevent warning |
---|
[3778cb2] | 1054 | } // switch |
---|
[7c2a7b6] | 1055 | // print branch target for labelled break/continue/fallthru in debug mode |
---|
[42a36d9] | 1056 | if ( ! options.genC && branchStmt->get_type() != BranchStmt::Goto ) { |
---|
[7c2a7b6] | 1057 | if ( ! branchStmt->get_target().empty() ) { |
---|
| 1058 | output << " " << branchStmt->get_target(); |
---|
| 1059 | } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) { |
---|
| 1060 | output << " default"; |
---|
| 1061 | } |
---|
| 1062 | } |
---|
[2b6c1e0] | 1063 | output << ";"; |
---|
[51587aa] | 1064 | } |
---|
| 1065 | |
---|
[9857e8d] | 1066 | void CodeGenerator::postvisit( ReturnStmt * returnStmt ) { |
---|
[6c4ff37] | 1067 | output << "return "; |
---|
[9857e8d] | 1068 | maybeAccept( returnStmt->get_expr(), *visitor ); |
---|
[6c4ff37] | 1069 | output << ";"; |
---|
[51587aa] | 1070 | } |
---|
| 1071 | |
---|
[9857e8d] | 1072 | void CodeGenerator::postvisit( ThrowStmt * throwStmt ) { |
---|
[42a36d9] | 1073 | assertf( ! options.genC, "Throw statements should not reach code generation." ); |
---|
[daf1af8] | 1074 | |
---|
| 1075 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? |
---|
[60a8062] | 1076 | "throw" : "throwResume"); |
---|
[daf1af8] | 1077 | if (throwStmt->get_expr()) { |
---|
| 1078 | output << " "; |
---|
[9857e8d] | 1079 | throwStmt->get_expr()->accept( *visitor ); |
---|
[daf1af8] | 1080 | } |
---|
| 1081 | if (throwStmt->get_target()) { |
---|
| 1082 | output << " _At "; |
---|
[9857e8d] | 1083 | throwStmt->get_target()->accept( *visitor ); |
---|
[daf1af8] | 1084 | } |
---|
| 1085 | output << ";"; |
---|
| 1086 | } |
---|
[e4ea10b7] | 1087 | void CodeGenerator::postvisit( CatchStmt * stmt ) { |
---|
[42a36d9] | 1088 | assertf( ! options.genC, "Catch statements should not reach code generation." ); |
---|
[e4ea10b7] | 1089 | |
---|
| 1090 | output << ((stmt->get_kind() == CatchStmt::Terminate) ? |
---|
[60a8062] | 1091 | "catch" : "catchResume"); |
---|
[e4ea10b7] | 1092 | output << "( "; |
---|
| 1093 | stmt->decl->accept( *visitor ); |
---|
| 1094 | output << " ) "; |
---|
| 1095 | |
---|
| 1096 | if( stmt->cond ) { |
---|
| 1097 | output << "if/when(?) ("; |
---|
| 1098 | stmt->cond->accept( *visitor ); |
---|
| 1099 | output << ") "; |
---|
| 1100 | } |
---|
| 1101 | stmt->body->accept( *visitor ); |
---|
| 1102 | } |
---|
| 1103 | |
---|
| 1104 | void CodeGenerator::postvisit( WaitForStmt * stmt ) { |
---|
[42a36d9] | 1105 | assertf( ! options.genC, "Waitfor statements should not reach code generation." ); |
---|
[e4ea10b7] | 1106 | |
---|
| 1107 | bool first = true; |
---|
| 1108 | for( auto & clause : stmt->clauses ) { |
---|
| 1109 | if(first) { output << "or "; first = false; } |
---|
| 1110 | if( clause.condition ) { |
---|
| 1111 | output << "when("; |
---|
| 1112 | stmt->timeout.condition->accept( *visitor ); |
---|
| 1113 | output << ") "; |
---|
| 1114 | } |
---|
| 1115 | output << "waitfor("; |
---|
| 1116 | clause.target.function->accept( *visitor ); |
---|
| 1117 | for( Expression * expr : clause.target.arguments ) { |
---|
| 1118 | output << ","; |
---|
| 1119 | expr->accept( *visitor ); |
---|
| 1120 | } |
---|
| 1121 | output << ") "; |
---|
| 1122 | clause.statement->accept( *visitor ); |
---|
| 1123 | } |
---|
| 1124 | |
---|
| 1125 | if( stmt->timeout.statement ) { |
---|
| 1126 | output << "or "; |
---|
| 1127 | if( stmt->timeout.condition ) { |
---|
| 1128 | output << "when("; |
---|
| 1129 | stmt->timeout.condition->accept( *visitor ); |
---|
| 1130 | output << ") "; |
---|
| 1131 | } |
---|
| 1132 | output << "timeout("; |
---|
| 1133 | stmt->timeout.time->accept( *visitor ); |
---|
| 1134 | output << ") "; |
---|
| 1135 | stmt->timeout.statement->accept( *visitor ); |
---|
| 1136 | } |
---|
| 1137 | |
---|
| 1138 | if( stmt->orelse.statement ) { |
---|
| 1139 | output << "or "; |
---|
| 1140 | if( stmt->orelse.condition ) { |
---|
| 1141 | output << "when("; |
---|
| 1142 | stmt->orelse.condition->accept( *visitor ); |
---|
| 1143 | output << ")"; |
---|
| 1144 | } |
---|
| 1145 | output << "else "; |
---|
| 1146 | stmt->orelse.statement->accept( *visitor ); |
---|
| 1147 | } |
---|
| 1148 | } |
---|
| 1149 | |
---|
[55d6e8de] | 1150 | void CodeGenerator::postvisit( WithStmt * with ) { |
---|
[42a36d9] | 1151 | if ( ! options.genC ) { |
---|
[55d6e8de] | 1152 | output << "with ( "; |
---|
| 1153 | genCommaList( with->exprs.begin(), with->exprs.end() ); |
---|
| 1154 | output << " ) "; |
---|
| 1155 | } |
---|
| 1156 | with->stmt->accept( *visitor ); |
---|
| 1157 | } |
---|
[daf1af8] | 1158 | |
---|
[3b0bc16] | 1159 | void CodeGenerator::postvisit( WhileDoStmt * whileDoStmt ) { |
---|
| 1160 | if ( whileDoStmt->get_isDoWhile() ) { |
---|
[d7312ac] | 1161 | output << "do"; |
---|
[321a2481] | 1162 | } else { |
---|
[d7312ac] | 1163 | output << "while ("; |
---|
[3b0bc16] | 1164 | whileDoStmt->get_condition()->accept( *visitor ); |
---|
[6c4ff37] | 1165 | output << ")"; |
---|
[51587aa] | 1166 | } // if |
---|
[2b6c1e0] | 1167 | output << " "; |
---|
[51587aa] | 1168 | |
---|
[3b0bc16] | 1169 | output << CodeGenerator::printLabels( whileDoStmt->get_body()->get_labels() ); |
---|
| 1170 | whileDoStmt->get_body()->accept( *visitor ); |
---|
[51587aa] | 1171 | |
---|
[cda48b6] | 1172 | output << indent; |
---|
[51587aa] | 1173 | |
---|
[3b0bc16] | 1174 | if ( whileDoStmt->get_isDoWhile() ) { |
---|
[d7312ac] | 1175 | output << " while ("; |
---|
[3b0bc16] | 1176 | whileDoStmt->get_condition()->accept( *visitor ); |
---|
[6c4ff37] | 1177 | output << ");"; |
---|
[51587aa] | 1178 | } // if |
---|
| 1179 | } |
---|
| 1180 | |
---|
[9857e8d] | 1181 | void CodeGenerator::postvisit( ForStmt * forStmt ) { |
---|
[8e9cbb2] | 1182 | // initialization is always hoisted, so don't bother doing anything with that |
---|
[145f1fc] | 1183 | output << "for (;"; |
---|
[51587aa] | 1184 | |
---|
[321a2481] | 1185 | if ( forStmt->get_condition() != 0 ) { |
---|
[9857e8d] | 1186 | forStmt->get_condition()->accept( *visitor ); |
---|
[3778cb2] | 1187 | } // if |
---|
[6c4ff37] | 1188 | output << ";"; |
---|
[51587aa] | 1189 | |
---|
[321a2481] | 1190 | if ( forStmt->get_increment() != 0 ) { |
---|
| 1191 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 1192 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
[9857e8d] | 1193 | expr->accept( *visitor ); |
---|
[3778cb2] | 1194 | } // if |
---|
[2b6c1e0] | 1195 | output << ") "; |
---|
[51587aa] | 1196 | |
---|
| 1197 | if ( forStmt->get_body() != 0 ) { |
---|
[2b6c1e0] | 1198 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
[9857e8d] | 1199 | forStmt->get_body()->accept( *visitor ); |
---|
[51587aa] | 1200 | } // if |
---|
| 1201 | } |
---|
| 1202 | |
---|
[9857e8d] | 1203 | void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) { |
---|
[cda48b6] | 1204 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
[6c4ff37] | 1205 | output << "/* null statement */ ;"; |
---|
[51587aa] | 1206 | } |
---|
| 1207 | |
---|
[9857e8d] | 1208 | void CodeGenerator::postvisit( DeclStmt * declStmt ) { |
---|
| 1209 | declStmt->get_decl()->accept( *visitor ); |
---|
[71f4e4f] | 1210 | |
---|
[51587aa] | 1211 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
[6c4ff37] | 1212 | output << ";"; |
---|
[51587aa] | 1213 | } // if |
---|
| 1214 | } |
---|
| 1215 | |
---|
[9857e8d] | 1216 | void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) { |
---|
[42a36d9] | 1217 | assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." ); |
---|
[9857e8d] | 1218 | stmt->callStmt->accept( *visitor ); |
---|
| 1219 | } |
---|
| 1220 | |
---|
[6cebfef] | 1221 | void CodeGenerator::postvisit( MutexStmt * stmt ) { |
---|
| 1222 | assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." ); |
---|
| 1223 | stmt->stmt->accept( *visitor ); |
---|
| 1224 | } |
---|
| 1225 | |
---|
[dd020c0] | 1226 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) { |
---|
[fb04321] | 1227 | if ( decl->get_storageClasses().any() ) { |
---|
[6e8bd43] | 1228 | decl->get_storageClasses().print( output ); |
---|
[a7c90d4] | 1229 | } // if |
---|
[dd020c0] | 1230 | } // CodeGenerator::handleStorageClass |
---|
[9facf3b] | 1231 | |
---|
| 1232 | std::string genName( DeclarationWithType * decl ) { |
---|
[60a8062] | 1233 | const OperatorInfo * opInfo = operatorLookup( decl->get_name() ); |
---|
| 1234 | if ( opInfo ) { |
---|
| 1235 | return opInfo->outputName; |
---|
[9facf3b] | 1236 | } else { |
---|
| 1237 | return decl->get_name(); |
---|
| 1238 | } // if |
---|
| 1239 | } |
---|
[51b7345] | 1240 | } // namespace CodeGen |
---|
[51587aa] | 1241 | |
---|
| 1242 | // Local Variables: // |
---|
| 1243 | // tab-width: 4 // |
---|
| 1244 | // mode: c++ // |
---|
| 1245 | // compile-command: "make install" // |
---|
| 1246 | // End: // |
---|