[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 |
---|
[6e300d9] | 11 | // Last Modified By : Andrew Beach |
---|
[daf1af8] | 12 | // Last Modified On : Thu Jun 8 16:00:00 2017 |
---|
| 13 | // Update Count : 485 |
---|
[51587aa] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <algorithm> |
---|
| 17 | #include <iostream> |
---|
| 18 | #include <cassert> |
---|
| 19 | #include <list> |
---|
| 20 | |
---|
[68cd1ce] | 21 | #include "Parser/ParseNode.h" |
---|
| 22 | |
---|
[e8032b0] | 23 | #include "SynTree/Declaration.h" |
---|
[51b7345] | 24 | #include "SynTree/Expression.h" |
---|
| 25 | #include "SynTree/Initializer.h" |
---|
[68cd1ce] | 26 | #include "SynTree/Statement.h" |
---|
[e8032b0] | 27 | #include "SynTree/Type.h" |
---|
[7baed7d] | 28 | #include "SynTree/Attribute.h" |
---|
[51b7345] | 29 | |
---|
[d3b7937] | 30 | #include "Common/utility.h" |
---|
| 31 | #include "Common/UnimplementedError.h" |
---|
[51b7345] | 32 | |
---|
[a61fea9a] | 33 | #include "CodeGenerator.h" |
---|
[51b7345] | 34 | #include "OperatorTable.h" |
---|
| 35 | #include "GenType.h" |
---|
| 36 | |
---|
[10a7775] | 37 | #include "InitTweak/InitTweak.h" |
---|
| 38 | |
---|
[51b7345] | 39 | using namespace std; |
---|
| 40 | |
---|
| 41 | namespace CodeGen { |
---|
[6c4ff37] | 42 | int CodeGenerator::tabsize = 4; |
---|
[51587aa] | 43 | |
---|
[145f1fc] | 44 | // the kinds of statements that would ideally be followed by whitespace |
---|
[2b6c1e0] | 45 | bool wantSpacing( Statement * stmt) { |
---|
| 46 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || |
---|
[08061589] | 47 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt ); |
---|
[2b6c1e0] | 48 | } |
---|
| 49 | |
---|
[8688ce1] | 50 | void CodeGenerator::extension( Expression * expr ) { |
---|
[8e9cbb2] | 51 | if ( expr->get_extension() ) { |
---|
| 52 | output << "__extension__ "; |
---|
| 53 | } // if |
---|
| 54 | } // extension |
---|
| 55 | |
---|
[8688ce1] | 56 | void CodeGenerator::extension( Declaration * decl ) { |
---|
[8e9cbb2] | 57 | if ( decl->get_extension() ) { |
---|
| 58 | output << "__extension__ "; |
---|
| 59 | } // if |
---|
| 60 | } // extension |
---|
| 61 | |
---|
[58dd019] | 62 | void CodeGenerator::asmName( DeclarationWithType * decl ) { |
---|
| 63 | if ( ConstantExpr * asmName = decl->get_asmName() ) { |
---|
| 64 | output << " asm ( " << asmName->get_constant()->get_value() << " )"; |
---|
| 65 | } // if |
---|
| 66 | } // extension |
---|
| 67 | |
---|
[888cbe4] | 68 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { |
---|
| 69 | labels = &l; |
---|
| 70 | return *this; |
---|
| 71 | } |
---|
| 72 | |
---|
[8688ce1] | 73 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) { |
---|
[888cbe4] | 74 | std::list< Label > & labs = *printLabels.labels; |
---|
| 75 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter? |
---|
| 76 | for ( Label & l : labs ) { |
---|
| 77 | output << l.get_name() + ": "; |
---|
| 78 | printLabels.cg.genAttributes( l.get_attributes() ); |
---|
[8688ce1] | 79 | } // for |
---|
[888cbe4] | 80 | return output; |
---|
| 81 | } |
---|
| 82 | |
---|
[c850687] | 83 | CodeGenerator::LineMarker::LineMarker( |
---|
| 84 | CodeLocation const & loc, bool toPrint) : |
---|
| 85 | loc(loc), toPrint(toPrint) |
---|
| 86 | {} |
---|
| 87 | |
---|
| 88 | CodeGenerator::LineMarker CodeGenerator::lineDirective( |
---|
| 89 | BaseSyntaxNode const * node) { |
---|
| 90 | return LineMarker(node->location, lineMarks); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | std::ostream & operator<<(std::ostream & out, |
---|
| 94 | CodeGenerator::LineMarker const & marker) { |
---|
| 95 | if (marker.toPrint && marker.loc.isSet()) { |
---|
| 96 | return out << "\n# " << marker.loc.linenumber << " \"" |
---|
| 97 | << marker.loc.filename << "\"\n"; |
---|
| 98 | } else if (marker.toPrint) { |
---|
| 99 | return out << "\n/* Missing CodeLocation */\n"; |
---|
| 100 | } else { |
---|
| 101 | return out; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
[f7cb0bc] | 105 | CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} |
---|
[cda48b6] | 106 | |
---|
[486341f] | 107 | string CodeGenerator::mangleName( DeclarationWithType * decl ) { |
---|
[35b1bf4] | 108 | if ( pretty ) return decl->get_name(); |
---|
[51587aa] | 109 | if ( decl->get_mangleName() != "" ) { |
---|
[f326f99] | 110 | // need to incorporate scope level in order to differentiate names for destructors |
---|
| 111 | return decl->get_scopedMangleName(); |
---|
[51587aa] | 112 | } else { |
---|
| 113 | return decl->get_name(); |
---|
| 114 | } // if |
---|
| 115 | } |
---|
[94b4364] | 116 | |
---|
[44a81853] | 117 | void CodeGenerator::genAttributes( list< Attribute * > & attributes ) { |
---|
| 118 | if ( attributes.empty() ) return; |
---|
| 119 | output << "__attribute__ (("; |
---|
| 120 | for ( list< Attribute * >::iterator attr( attributes.begin() );; ) { |
---|
| 121 | output << (*attr)->get_name(); |
---|
| 122 | if ( ! (*attr)->get_parameters().empty() ) { |
---|
| 123 | output << "("; |
---|
| 124 | genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() ); |
---|
| 125 | output << ")"; |
---|
| 126 | } // if |
---|
| 127 | if ( ++attr == attributes.end() ) break; |
---|
| 128 | output << ","; // separator |
---|
| 129 | } // for |
---|
| 130 | output << ")) "; |
---|
| 131 | } // CodeGenerator::genAttributes |
---|
[7baed7d] | 132 | |
---|
| 133 | |
---|
[4810867] | 134 | // *** Declarations |
---|
[8688ce1] | 135 | void CodeGenerator::visit( FunctionDecl * functionDecl ) { |
---|
[8e9cbb2] | 136 | extension( functionDecl ); |
---|
[7baed7d] | 137 | genAttributes( functionDecl->get_attributes() ); |
---|
| 138 | |
---|
[51587aa] | 139 | handleStorageClass( functionDecl ); |
---|
[6e8bd43] | 140 | functionDecl->get_funcSpec().print( output ); |
---|
[dd020c0] | 141 | |
---|
[e39241b] | 142 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC ); |
---|
[51587aa] | 143 | |
---|
[58dd019] | 144 | asmName( functionDecl ); |
---|
| 145 | |
---|
[51587aa] | 146 | // acceptAll( functionDecl->get_oldDecls(), *this ); |
---|
| 147 | if ( functionDecl->get_statements() ) { |
---|
[7f5566b] | 148 | functionDecl->get_statements()->accept( *this ); |
---|
[51587aa] | 149 | } // if |
---|
| 150 | } |
---|
| 151 | |
---|
[8688ce1] | 152 | void CodeGenerator::visit( ObjectDecl * objectDecl ) { |
---|
[e39241b] | 153 | if (objectDecl->get_name().empty() && genC ) { |
---|
| 154 | // only generate an anonymous name when generating C code, otherwise it clutters the output too much |
---|
[d9c8a59] | 155 | static UniqueName name = { "__anonymous_object" }; |
---|
| 156 | objectDecl->set_name( name.newName() ); |
---|
| 157 | } |
---|
| 158 | |
---|
[8e9cbb2] | 159 | extension( objectDecl ); |
---|
[f9cebb5] | 160 | genAttributes( objectDecl->get_attributes() ); |
---|
| 161 | |
---|
[51587aa] | 162 | handleStorageClass( objectDecl ); |
---|
[e39241b] | 163 | output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC ); |
---|
[71f4e4f] | 164 | |
---|
[58dd019] | 165 | asmName( objectDecl ); |
---|
| 166 | |
---|
[51587aa] | 167 | if ( objectDecl->get_init() ) { |
---|
[6c4ff37] | 168 | output << " = "; |
---|
[51587aa] | 169 | objectDecl->get_init()->accept( *this ); |
---|
| 170 | } // if |
---|
[3778cb2] | 171 | |
---|
[51587aa] | 172 | if ( objectDecl->get_bitfieldWidth() ) { |
---|
[6c4ff37] | 173 | output << ":"; |
---|
[51587aa] | 174 | objectDecl->get_bitfieldWidth()->accept( *this ); |
---|
| 175 | } // if |
---|
| 176 | } |
---|
| 177 | |
---|
[5f642e38] | 178 | void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) { |
---|
[c0aa336] | 179 | genAttributes( aggDecl->get_attributes() ); |
---|
[35b1bf4] | 180 | |
---|
[e39241b] | 181 | if( ! aggDecl->get_parameters().empty() && ! genC ) { |
---|
| 182 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
| 183 | output << "forall("; |
---|
| 184 | genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() ); |
---|
| 185 | output << ")" << endl; |
---|
| 186 | } |
---|
| 187 | |
---|
[29cf9c8] | 188 | output << kind; |
---|
[51587aa] | 189 | if ( aggDecl->get_name() != "" ) |
---|
[6c4ff37] | 190 | output << aggDecl->get_name(); |
---|
[71f4e4f] | 191 | |
---|
[2c57025] | 192 | if ( aggDecl->has_body() ) { |
---|
| 193 | std::list< Declaration * > & memb = aggDecl->get_members(); |
---|
[94b4364] | 194 | output << " {" << endl; |
---|
[51587aa] | 195 | |
---|
[f7cb0bc] | 196 | ++indent; |
---|
[3778cb2] | 197 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { |
---|
[4810867] | 198 | output << lineDirective( *i ) << indent; |
---|
[7f5566b] | 199 | (*i)->accept( *this ); |
---|
[6c4ff37] | 200 | output << ";" << endl; |
---|
[3778cb2] | 201 | } // for |
---|
[51587aa] | 202 | |
---|
[f7cb0bc] | 203 | --indent; |
---|
[51587aa] | 204 | |
---|
[cda48b6] | 205 | output << indent << "}"; |
---|
[17cd4eb] | 206 | } // if |
---|
[51587aa] | 207 | } |
---|
[17cd4eb] | 208 | |
---|
[8688ce1] | 209 | void CodeGenerator::visit( StructDecl * structDecl ) { |
---|
[8e9cbb2] | 210 | extension( structDecl ); |
---|
[5f642e38] | 211 | handleAggregate( structDecl, "struct " ); |
---|
[51587aa] | 212 | } |
---|
[17cd4eb] | 213 | |
---|
[8688ce1] | 214 | void CodeGenerator::visit( UnionDecl * unionDecl ) { |
---|
[8e9cbb2] | 215 | extension( unionDecl ); |
---|
[5f642e38] | 216 | handleAggregate( unionDecl, "union " ); |
---|
[51587aa] | 217 | } |
---|
[71f4e4f] | 218 | |
---|
[8688ce1] | 219 | void CodeGenerator::visit( EnumDecl * enumDecl ) { |
---|
[8e9cbb2] | 220 | extension( enumDecl ); |
---|
[4810867] | 221 | output << lineDirective ( enumDecl ); |
---|
[6c4ff37] | 222 | output << "enum "; |
---|
[c0aa336] | 223 | genAttributes( enumDecl->get_attributes() ); |
---|
[51587aa] | 224 | |
---|
[8e9cbb2] | 225 | if ( enumDecl->get_name() != "" ) |
---|
| 226 | output << enumDecl->get_name(); |
---|
[71f4e4f] | 227 | |
---|
[8e9cbb2] | 228 | std::list< Declaration* > &memb = enumDecl->get_members(); |
---|
[51587aa] | 229 | |
---|
| 230 | if ( ! memb.empty() ) { |
---|
[cda48b6] | 231 | output << " {" << endl; |
---|
[51587aa] | 232 | |
---|
[f7cb0bc] | 233 | ++indent; |
---|
[51587aa] | 234 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
[8688ce1] | 235 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
[51587aa] | 236 | assert( obj ); |
---|
[4810867] | 237 | output << lineDirective( obj ) << indent << mangleName( obj ); |
---|
[51587aa] | 238 | if ( obj->get_init() ) { |
---|
[6c4ff37] | 239 | output << " = "; |
---|
[7f5566b] | 240 | obj->get_init()->accept( *this ); |
---|
[51587aa] | 241 | } // if |
---|
[6c4ff37] | 242 | output << "," << endl; |
---|
[51587aa] | 243 | } // for |
---|
| 244 | |
---|
[f7cb0bc] | 245 | --indent; |
---|
[51587aa] | 246 | |
---|
[cda48b6] | 247 | output << indent << "}"; |
---|
[51587aa] | 248 | } // if |
---|
| 249 | } |
---|
[71f4e4f] | 250 | |
---|
[7e003011] | 251 | void CodeGenerator::visit( __attribute__((unused)) TraitDecl * traitDecl ) {} |
---|
[71f4e4f] | 252 | |
---|
[8688ce1] | 253 | void CodeGenerator::visit( TypedefDecl * typeDecl ) { |
---|
[e39241b] | 254 | assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); |
---|
[4810867] | 255 | output << lineDirective( typeDecl ); |
---|
[e39241b] | 256 | output << "typedef "; |
---|
| 257 | output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; |
---|
[51587aa] | 258 | } |
---|
[71f4e4f] | 259 | |
---|
[8688ce1] | 260 | void CodeGenerator::visit( TypeDecl * typeDecl ) { |
---|
[e39241b] | 261 | if ( genC ) { |
---|
| 262 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, |
---|
| 263 | // still to be done |
---|
| 264 | extension( typeDecl ); |
---|
| 265 | output << "extern unsigned long " << typeDecl->get_name(); |
---|
| 266 | if ( typeDecl->get_base() ) { |
---|
| 267 | output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )"; |
---|
| 268 | } // if |
---|
| 269 | } else { |
---|
[bdd0755] | 270 | output << typeDecl->genTypeString() << " " << typeDecl->get_name(); |
---|
| 271 | if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) { |
---|
| 272 | output << " | sized(" << typeDecl->get_name() << ")"; |
---|
| 273 | } |
---|
[e39241b] | 274 | if ( ! typeDecl->get_assertions().empty() ) { |
---|
| 275 | output << " | { "; |
---|
| 276 | genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() ); |
---|
| 277 | output << " }"; |
---|
| 278 | } |
---|
| 279 | } |
---|
[51587aa] | 280 | } |
---|
| 281 | |
---|
[e4d829b] | 282 | void CodeGenerator::visit( Designation * designation ) { |
---|
| 283 | std::list< Expression * > designators = designation->get_designators(); |
---|
[e45215c] | 284 | if ( designators.size() == 0 ) return; |
---|
[e4d829b] | 285 | for ( Expression * des : designators ) { |
---|
[62423350] | 286 | if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) { |
---|
| 287 | // if expression is a NameExpr or VariableExpr, then initializing aggregate member |
---|
| 288 | output << "."; |
---|
[e4d829b] | 289 | des->accept( *this ); |
---|
| 290 | } else { |
---|
[62423350] | 291 | // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt |
---|
| 292 | output << "["; |
---|
[e4d829b] | 293 | des->accept( *this ); |
---|
[62423350] | 294 | output << "]"; |
---|
[3778cb2] | 295 | } // if |
---|
| 296 | } // for |
---|
[e45215c] | 297 | output << " = "; |
---|
| 298 | } |
---|
| 299 | |
---|
[8688ce1] | 300 | void CodeGenerator::visit( SingleInit * init ) { |
---|
[51587aa] | 301 | init->get_value()->accept( *this ); |
---|
| 302 | } |
---|
| 303 | |
---|
[8688ce1] | 304 | void CodeGenerator::visit( ListInit * init ) { |
---|
[e4d829b] | 305 | auto initBegin = init->begin(); |
---|
| 306 | auto initEnd = init->end(); |
---|
| 307 | auto desigBegin = init->get_designations().begin(); |
---|
| 308 | auto desigEnd = init->get_designations().end(); |
---|
| 309 | |
---|
[6c4ff37] | 310 | output << "{ "; |
---|
[e4d829b] | 311 | for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { |
---|
| 312 | (*desigBegin)->accept( *this ); |
---|
| 313 | (*initBegin)->accept( *this ); |
---|
| 314 | ++initBegin, ++desigBegin; |
---|
| 315 | if ( initBegin != initEnd ) { |
---|
| 316 | output << ", "; |
---|
| 317 | } |
---|
| 318 | } |
---|
[6c4ff37] | 319 | output << " }"; |
---|
[e4d829b] | 320 | assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() ); |
---|
[51587aa] | 321 | } |
---|
| 322 | |
---|
[7e003011] | 323 | void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){ |
---|
[e39241b] | 324 | assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); |
---|
| 325 | // xxx - generate something reasonable for constructor/destructor pairs |
---|
| 326 | output << "<ctorinit>"; |
---|
[fc638d2] | 327 | } |
---|
| 328 | |
---|
[8688ce1] | 329 | void CodeGenerator::visit( Constant * constant ) { |
---|
[6c4ff37] | 330 | output << constant->get_value() ; |
---|
[51587aa] | 331 | } |
---|
| 332 | |
---|
[4810867] | 333 | // *** Expressions |
---|
[8688ce1] | 334 | void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { |
---|
[e04ef3a] | 335 | extension( applicationExpr ); |
---|
[8688ce1] | 336 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
[51587aa] | 337 | OperatorInfo opInfo; |
---|
| 338 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { |
---|
| 339 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
| 340 | switch ( opInfo.type ) { |
---|
| 341 | case OT_PREFIXASSIGN: |
---|
| 342 | case OT_POSTFIXASSIGN: |
---|
| 343 | case OT_INFIXASSIGN: |
---|
[356189a] | 344 | case OT_CTOR: |
---|
[c2ce2350] | 345 | case OT_DTOR: |
---|
[51587aa] | 346 | { |
---|
| 347 | assert( arg != applicationExpr->get_args().end() ); |
---|
[8688ce1] | 348 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { |
---|
[356189a] | 349 | // remove & from first assignment/ctor argument |
---|
[51587aa] | 350 | *arg = addrExpr->get_arg(); |
---|
| 351 | } else { |
---|
[356189a] | 352 | // no address-of operator, so must be a pointer - add dereference |
---|
[066d77a] | 353 | // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared. |
---|
| 354 | // Since its arguments are modified here, this assertion most commonly triggers when the application |
---|
| 355 | // is visited multiple times. |
---|
[8688ce1] | 356 | UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
[51587aa] | 357 | newExpr->get_args().push_back( *arg ); |
---|
[906e24d] | 358 | Type * type = InitTweak::getPointerBase( (*arg)->get_result() ); |
---|
[066d77a] | 359 | assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." ); |
---|
[906e24d] | 360 | newExpr->set_result( type->clone() ); |
---|
[51587aa] | 361 | *arg = newExpr; |
---|
| 362 | } // if |
---|
| 363 | break; |
---|
| 364 | } |
---|
[71f4e4f] | 365 | |
---|
[51587aa] | 366 | default: |
---|
| 367 | // do nothing |
---|
| 368 | ; |
---|
[3778cb2] | 369 | } // switch |
---|
[71f4e4f] | 370 | |
---|
[51587aa] | 371 | switch ( opInfo.type ) { |
---|
| 372 | case OT_INDEX: |
---|
| 373 | assert( applicationExpr->get_args().size() == 2 ); |
---|
| 374 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 375 | output << "["; |
---|
[51587aa] | 376 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 377 | output << "]"; |
---|
[51587aa] | 378 | break; |
---|
[71f4e4f] | 379 | |
---|
[51587aa] | 380 | case OT_CALL: |
---|
[356189a] | 381 | // there are no intrinsic definitions of the function call operator |
---|
[51587aa] | 382 | assert( false ); |
---|
| 383 | break; |
---|
[71f4e4f] | 384 | |
---|
[f1e012b] | 385 | case OT_CTOR: |
---|
[c2ce2350] | 386 | case OT_DTOR: |
---|
[356189a] | 387 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 388 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 389 | // effects, so must still output this expression |
---|
[64071c2] | 390 | output << "("; |
---|
[356189a] | 391 | (*arg++)->accept( *this ); |
---|
[64071c2] | 392 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 393 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
[c2ce2350] | 394 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
[356189a] | 395 | output << "("; |
---|
| 396 | (*arg++)->accept( *this ); |
---|
| 397 | output << opInfo.symbol; |
---|
| 398 | (*arg)->accept( *this ); |
---|
[c2ce2350] | 399 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 400 | } else { |
---|
[c2ce2350] | 401 | // no constructors with 0 or more than 2 parameters |
---|
[356189a] | 402 | assert( false ); |
---|
[8688ce1] | 403 | } // if |
---|
[356189a] | 404 | break; |
---|
[f1e012b] | 405 | |
---|
[51587aa] | 406 | case OT_PREFIX: |
---|
| 407 | case OT_PREFIXASSIGN: |
---|
| 408 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 409 | output << "("; |
---|
| 410 | output << opInfo.symbol; |
---|
[51587aa] | 411 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 412 | output << ")"; |
---|
[51587aa] | 413 | break; |
---|
[71f4e4f] | 414 | |
---|
[51587aa] | 415 | case OT_POSTFIX: |
---|
| 416 | case OT_POSTFIXASSIGN: |
---|
| 417 | assert( applicationExpr->get_args().size() == 1 ); |
---|
| 418 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 419 | output << opInfo.symbol; |
---|
[51587aa] | 420 | break; |
---|
| 421 | |
---|
[f1e012b] | 422 | |
---|
[51587aa] | 423 | case OT_INFIX: |
---|
| 424 | case OT_INFIXASSIGN: |
---|
| 425 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 426 | output << "("; |
---|
[51587aa] | 427 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 428 | output << opInfo.symbol; |
---|
[51587aa] | 429 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 430 | output << ")"; |
---|
[51587aa] | 431 | break; |
---|
[71f4e4f] | 432 | |
---|
[51587aa] | 433 | case OT_CONSTANT: |
---|
[721f17a] | 434 | case OT_LABELADDRESS: |
---|
| 435 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[51587aa] | 436 | assert( false ); |
---|
[3778cb2] | 437 | } // switch |
---|
[17cd4eb] | 438 | } else { |
---|
[51587aa] | 439 | varExpr->accept( *this ); |
---|
[6c4ff37] | 440 | output << "("; |
---|
[51587aa] | 441 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 442 | output << ")"; |
---|
[17cd4eb] | 443 | } // if |
---|
[51587aa] | 444 | } else { |
---|
| 445 | applicationExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 446 | output << "("; |
---|
[51587aa] | 447 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 448 | output << ")"; |
---|
[51587aa] | 449 | } // if |
---|
| 450 | } |
---|
[71f4e4f] | 451 | |
---|
[8688ce1] | 452 | void CodeGenerator::visit( UntypedExpr * untypedExpr ) { |
---|
[e04ef3a] | 453 | extension( untypedExpr ); |
---|
[8688ce1] | 454 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { |
---|
[51587aa] | 455 | OperatorInfo opInfo; |
---|
| 456 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
| 457 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); |
---|
| 458 | switch ( opInfo.type ) { |
---|
| 459 | case OT_INDEX: |
---|
| 460 | assert( untypedExpr->get_args().size() == 2 ); |
---|
| 461 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 462 | output << "["; |
---|
[51587aa] | 463 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 464 | output << "]"; |
---|
[51587aa] | 465 | break; |
---|
[71f4e4f] | 466 | |
---|
[51587aa] | 467 | case OT_CALL: |
---|
[f1e012b] | 468 | assert( false ); |
---|
| 469 | |
---|
[c2ce2350] | 470 | case OT_CTOR: |
---|
| 471 | case OT_DTOR: |
---|
| 472 | if ( untypedExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 473 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 474 | // effects, so must still output this expression |
---|
[64071c2] | 475 | output << "("; |
---|
[c2ce2350] | 476 | (*arg++)->accept( *this ); |
---|
[64071c2] | 477 | output << ") /* " << opInfo.inputName << " */"; |
---|
[c2ce2350] | 478 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
| 479 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
| 480 | output << "("; |
---|
| 481 | (*arg++)->accept( *this ); |
---|
| 482 | output << opInfo.symbol; |
---|
| 483 | (*arg)->accept( *this ); |
---|
| 484 | output << ") /* " << opInfo.inputName << " */"; |
---|
| 485 | } else { |
---|
| 486 | // no constructors with 0 or more than 2 parameters |
---|
| 487 | assert( false ); |
---|
[3778cb2] | 488 | } // if |
---|
[51587aa] | 489 | break; |
---|
[71f4e4f] | 490 | |
---|
[51587aa] | 491 | case OT_PREFIX: |
---|
| 492 | case OT_PREFIXASSIGN: |
---|
[de62360d] | 493 | case OT_LABELADDRESS: |
---|
[51587aa] | 494 | assert( untypedExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 495 | output << "("; |
---|
| 496 | output << opInfo.symbol; |
---|
[51587aa] | 497 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 498 | output << ")"; |
---|
[51587aa] | 499 | break; |
---|
[71f4e4f] | 500 | |
---|
[51587aa] | 501 | case OT_POSTFIX: |
---|
| 502 | case OT_POSTFIXASSIGN: |
---|
| 503 | assert( untypedExpr->get_args().size() == 1 ); |
---|
| 504 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 505 | output << opInfo.symbol; |
---|
[51587aa] | 506 | break; |
---|
[71f4e4f] | 507 | |
---|
[51587aa] | 508 | case OT_INFIX: |
---|
| 509 | case OT_INFIXASSIGN: |
---|
| 510 | assert( untypedExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 511 | output << "("; |
---|
[51587aa] | 512 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 513 | output << opInfo.symbol; |
---|
[51587aa] | 514 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 515 | output << ")"; |
---|
[51587aa] | 516 | break; |
---|
[71f4e4f] | 517 | |
---|
[51587aa] | 518 | case OT_CONSTANT: |
---|
| 519 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
| 520 | assert( false ); |
---|
[3778cb2] | 521 | } // switch |
---|
[51587aa] | 522 | } else { |
---|
[8688ce1] | 523 | if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 |
---|
[ac911f4] | 524 | assert( untypedExpr->get_args().size() == 2 ); |
---|
| 525 | (*untypedExpr->get_args().begin())->accept( *this ); |
---|
| 526 | output << " ... "; |
---|
| 527 | (*--untypedExpr->get_args().end())->accept( *this ); |
---|
[057b34f] | 528 | } else { // builtin routines |
---|
| 529 | nameExpr->accept( *this ); |
---|
| 530 | output << "("; |
---|
| 531 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
| 532 | output << ")"; |
---|
[66d12f7] | 533 | } // if |
---|
[51587aa] | 534 | } // if |
---|
| 535 | } else { |
---|
| 536 | untypedExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 537 | output << "("; |
---|
[51587aa] | 538 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
[6c4ff37] | 539 | output << ")"; |
---|
[51587aa] | 540 | } // if |
---|
| 541 | } |
---|
[71f4e4f] | 542 | |
---|
[064e3ff] | 543 | void CodeGenerator::visit( RangeExpr * rangeExpr ) { |
---|
| 544 | rangeExpr->get_low()->accept( *this ); |
---|
| 545 | output << " ... "; |
---|
| 546 | rangeExpr->get_high()->accept( *this ); |
---|
| 547 | } |
---|
| 548 | |
---|
[8688ce1] | 549 | void CodeGenerator::visit( NameExpr * nameExpr ) { |
---|
[e04ef3a] | 550 | extension( nameExpr ); |
---|
[51587aa] | 551 | OperatorInfo opInfo; |
---|
| 552 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
| 553 | assert( opInfo.type == OT_CONSTANT ); |
---|
[6c4ff37] | 554 | output << opInfo.symbol; |
---|
[51587aa] | 555 | } else { |
---|
[6c4ff37] | 556 | output << nameExpr->get_name(); |
---|
[51587aa] | 557 | } // if |
---|
| 558 | } |
---|
[71f4e4f] | 559 | |
---|
[8688ce1] | 560 | void CodeGenerator::visit( AddressExpr * addressExpr ) { |
---|
[e04ef3a] | 561 | extension( addressExpr ); |
---|
[6c4ff37] | 562 | output << "(&"; |
---|
[51587aa] | 563 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address |
---|
[8688ce1] | 564 | if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { |
---|
[6c4ff37] | 565 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 566 | } else { |
---|
| 567 | addressExpr->get_arg()->accept( *this ); |
---|
| 568 | } // if |
---|
[6c4ff37] | 569 | output << ")"; |
---|
[51587aa] | 570 | } |
---|
| 571 | |
---|
[8688ce1] | 572 | void CodeGenerator::visit( CastExpr * castExpr ) { |
---|
[e04ef3a] | 573 | extension( castExpr ); |
---|
[803deb1] | 574 | output << "("; |
---|
[906e24d] | 575 | if ( castExpr->get_result()->isVoid() ) { |
---|
[803deb1] | 576 | output << "(void)" ; |
---|
[615a096] | 577 | } else if ( ! castExpr->get_result()->get_lvalue() ) { |
---|
[803deb1] | 578 | // at least one result type of cast, but not an lvalue |
---|
| 579 | output << "("; |
---|
[e39241b] | 580 | output << genType( castExpr->get_result(), "", pretty, genC ); |
---|
[71f4e4f] | 581 | output << ")"; |
---|
[803deb1] | 582 | } else { |
---|
[8e9cbb2] | 583 | // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is |
---|
[803deb1] | 584 | // never an lvalue in C |
---|
[3778cb2] | 585 | } // if |
---|
[803deb1] | 586 | castExpr->get_arg()->accept( *this ); |
---|
| 587 | output << ")"; |
---|
[51587aa] | 588 | } |
---|
[71f4e4f] | 589 | |
---|
[8688ce1] | 590 | void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { |
---|
[e39241b] | 591 | assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); |
---|
| 592 | extension( memberExpr ); |
---|
| 593 | memberExpr->get_aggregate()->accept( *this ); |
---|
[5f642e38] | 594 | output << "."; |
---|
| 595 | memberExpr->get_member()->accept( *this ); |
---|
[51587aa] | 596 | } |
---|
[71f4e4f] | 597 | |
---|
[8688ce1] | 598 | void CodeGenerator::visit( MemberExpr * memberExpr ) { |
---|
[e04ef3a] | 599 | extension( memberExpr ); |
---|
[51587aa] | 600 | memberExpr->get_aggregate()->accept( *this ); |
---|
[6c4ff37] | 601 | output << "." << mangleName( memberExpr->get_member() ); |
---|
[51587aa] | 602 | } |
---|
[71f4e4f] | 603 | |
---|
[8688ce1] | 604 | void CodeGenerator::visit( VariableExpr * variableExpr ) { |
---|
[e04ef3a] | 605 | extension( variableExpr ); |
---|
[51587aa] | 606 | OperatorInfo opInfo; |
---|
| 607 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { |
---|
[6c4ff37] | 608 | output << opInfo.symbol; |
---|
[51587aa] | 609 | } else { |
---|
[6c4ff37] | 610 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 611 | } // if |
---|
| 612 | } |
---|
[71f4e4f] | 613 | |
---|
[8688ce1] | 614 | void CodeGenerator::visit( ConstantExpr * constantExpr ) { |
---|
[51587aa] | 615 | assert( constantExpr->get_constant() ); |
---|
[e04ef3a] | 616 | extension( constantExpr ); |
---|
[51587aa] | 617 | constantExpr->get_constant()->accept( *this ); |
---|
| 618 | } |
---|
[71f4e4f] | 619 | |
---|
[8688ce1] | 620 | void CodeGenerator::visit( SizeofExpr * sizeofExpr ) { |
---|
[e04ef3a] | 621 | extension( sizeofExpr ); |
---|
[6c4ff37] | 622 | output << "sizeof("; |
---|
[51587aa] | 623 | if ( sizeofExpr->get_isType() ) { |
---|
[e39241b] | 624 | output << genType( sizeofExpr->get_type(), "", pretty, genC ); |
---|
[51587aa] | 625 | } else { |
---|
| 626 | sizeofExpr->get_expr()->accept( *this ); |
---|
| 627 | } // if |
---|
[6c4ff37] | 628 | output << ")"; |
---|
[51587aa] | 629 | } |
---|
[47534159] | 630 | |
---|
[8688ce1] | 631 | void CodeGenerator::visit( AlignofExpr * alignofExpr ) { |
---|
[47534159] | 632 | // use GCC extension to avoid bumping std to C11 |
---|
[8e9cbb2] | 633 | extension( alignofExpr ); |
---|
[47534159] | 634 | output << "__alignof__("; |
---|
[25a054f] | 635 | if ( alignofExpr->get_isType() ) { |
---|
[e39241b] | 636 | output << genType( alignofExpr->get_type(), "", pretty, genC ); |
---|
[47534159] | 637 | } else { |
---|
[25a054f] | 638 | alignofExpr->get_expr()->accept( *this ); |
---|
[47534159] | 639 | } // if |
---|
| 640 | output << ")"; |
---|
| 641 | } |
---|
[71f4e4f] | 642 | |
---|
[8688ce1] | 643 | void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { |
---|
[e39241b] | 644 | assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); |
---|
| 645 | output << "offsetof("; |
---|
| 646 | output << genType( offsetofExpr->get_type(), "", pretty, genC ); |
---|
| 647 | output << ", " << offsetofExpr->get_member(); |
---|
| 648 | output << ")"; |
---|
[2a4b088] | 649 | } |
---|
| 650 | |
---|
[8688ce1] | 651 | void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) { |
---|
[25a054f] | 652 | // use GCC builtin |
---|
| 653 | output << "__builtin_offsetof("; |
---|
[e39241b] | 654 | output << genType( offsetofExpr->get_type(), "", pretty, genC ); |
---|
[e551c69] | 655 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
[25a054f] | 656 | output << ")"; |
---|
| 657 | } |
---|
[d63eeb0] | 658 | |
---|
[8688ce1] | 659 | void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { |
---|
[e39241b] | 660 | assertf( ! genC, "OffsetPackExpr should not reach code generation." ); |
---|
| 661 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; |
---|
[afc1045] | 662 | } |
---|
[70a06f6] | 663 | |
---|
[8688ce1] | 664 | void CodeGenerator::visit( LogicalExpr * logicalExpr ) { |
---|
[e04ef3a] | 665 | extension( logicalExpr ); |
---|
[6c4ff37] | 666 | output << "("; |
---|
[51587aa] | 667 | logicalExpr->get_arg1()->accept( *this ); |
---|
| 668 | if ( logicalExpr->get_isAnd() ) { |
---|
[6c4ff37] | 669 | output << " && "; |
---|
[51587aa] | 670 | } else { |
---|
[6c4ff37] | 671 | output << " || "; |
---|
[51587aa] | 672 | } // if |
---|
| 673 | logicalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 674 | output << ")"; |
---|
[51587aa] | 675 | } |
---|
[71f4e4f] | 676 | |
---|
[8688ce1] | 677 | void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) { |
---|
[e04ef3a] | 678 | extension( conditionalExpr ); |
---|
[6c4ff37] | 679 | output << "("; |
---|
[51587aa] | 680 | conditionalExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 681 | output << " ? "; |
---|
[51587aa] | 682 | conditionalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 683 | output << " : "; |
---|
[51587aa] | 684 | conditionalExpr->get_arg3()->accept( *this ); |
---|
[6c4ff37] | 685 | output << ")"; |
---|
[51587aa] | 686 | } |
---|
[71f4e4f] | 687 | |
---|
[8688ce1] | 688 | void CodeGenerator::visit( CommaExpr * commaExpr ) { |
---|
[e04ef3a] | 689 | extension( commaExpr ); |
---|
[6c4ff37] | 690 | output << "("; |
---|
[51587aa] | 691 | commaExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 692 | output << " , "; |
---|
[51587aa] | 693 | commaExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 694 | output << ")"; |
---|
[51587aa] | 695 | } |
---|
[71f4e4f] | 696 | |
---|
[e39241b] | 697 | void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { |
---|
| 698 | assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); |
---|
[f975c65] | 699 | extension( tupleExpr ); |
---|
[e39241b] | 700 | output << "["; |
---|
| 701 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 702 | output << "]"; |
---|
| 703 | } |
---|
[907eccb] | 704 | |
---|
[e39241b] | 705 | void CodeGenerator::visit( TupleExpr * tupleExpr ) { |
---|
| 706 | assertf( ! genC, "TupleExpr should not reach code generation." ); |
---|
[f975c65] | 707 | extension( tupleExpr ); |
---|
[e39241b] | 708 | output << "["; |
---|
| 709 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 710 | output << "]"; |
---|
| 711 | } |
---|
[71f4e4f] | 712 | |
---|
[f975c65] | 713 | void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) { |
---|
| 714 | assertf( ! genC, "TupleIndexExpr should not reach code generation." ); |
---|
| 715 | extension( tupleExpr ); |
---|
| 716 | tupleExpr->get_tuple()->accept( *this ); |
---|
| 717 | output << "." << tupleExpr->get_index(); |
---|
| 718 | } |
---|
| 719 | |
---|
[e39241b] | 720 | void CodeGenerator::visit( TypeExpr * typeExpr ) { |
---|
[e4d829b] | 721 | // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; |
---|
| 722 | // assertf( ! genC, "TypeExpr should not reach code generation." ); |
---|
| 723 | if ( ! genC ) { |
---|
| 724 | output<< genType( typeExpr->get_type(), "", pretty, genC ); |
---|
| 725 | } |
---|
[e39241b] | 726 | } |
---|
[2b6c1e0] | 727 | |
---|
[8688ce1] | 728 | void CodeGenerator::visit( AsmExpr * asmExpr ) { |
---|
[7f5566b] | 729 | if ( asmExpr->get_inout() ) { |
---|
| 730 | output << "[ "; |
---|
| 731 | asmExpr->get_inout()->accept( *this ); |
---|
| 732 | output << " ] "; |
---|
| 733 | } // if |
---|
| 734 | asmExpr->get_constraint()->accept( *this ); |
---|
| 735 | output << " ( "; |
---|
| 736 | asmExpr->get_operand()->accept( *this ); |
---|
| 737 | output << " )"; |
---|
| 738 | } |
---|
| 739 | |
---|
[3c13c03] | 740 | void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) { |
---|
[fbcde64] | 741 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); |
---|
[e39241b] | 742 | output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")"; |
---|
[3c13c03] | 743 | compLitExpr->get_initializer()->accept( *this ); |
---|
| 744 | } |
---|
| 745 | |
---|
[6eb8948] | 746 | void CodeGenerator::visit( StmtExpr * stmtExpr ) { |
---|
[3c13c03] | 747 | std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); |
---|
[4810867] | 748 | output << lineDirective( stmtExpr) << "({" << std::endl; |
---|
[f7cb0bc] | 749 | ++indent; |
---|
[3c13c03] | 750 | unsigned int numStmts = stmts.size(); |
---|
| 751 | unsigned int i = 0; |
---|
| 752 | for ( Statement * stmt : stmts ) { |
---|
[4810867] | 753 | output << lineDirective( stmt ) << indent; |
---|
| 754 | output << printLabels( stmt->get_labels() ); |
---|
[3c13c03] | 755 | if ( i+1 == numStmts ) { |
---|
| 756 | // last statement in a statement expression needs to be handled specially - |
---|
| 757 | // cannot cast to void, otherwise the expression statement has no value |
---|
| 758 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { |
---|
| 759 | exprStmt->get_expr()->accept( *this ); |
---|
| 760 | output << ";" << endl; |
---|
| 761 | ++i; |
---|
| 762 | break; |
---|
| 763 | } |
---|
| 764 | } |
---|
| 765 | stmt->accept( *this ); |
---|
| 766 | output << endl; |
---|
| 767 | if ( wantSpacing( stmt ) ) { |
---|
| 768 | output << endl; |
---|
| 769 | } // if |
---|
| 770 | ++i; |
---|
| 771 | } |
---|
[f7cb0bc] | 772 | --indent; |
---|
[3c13c03] | 773 | output << indent << "})"; |
---|
[6eb8948] | 774 | } |
---|
| 775 | |
---|
[4810867] | 776 | // *** Statements |
---|
[8688ce1] | 777 | void CodeGenerator::visit( CompoundStmt * compoundStmt ) { |
---|
[51587aa] | 778 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
[2b6c1e0] | 779 | output << "{" << endl; |
---|
[51587aa] | 780 | |
---|
[f7cb0bc] | 781 | ++indent; |
---|
[51587aa] | 782 | |
---|
[7f5566b] | 783 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
[cda48b6] | 784 | output << indent << printLabels( (*i)->get_labels() ); |
---|
[7f5566b] | 785 | (*i)->accept( *this ); |
---|
[2b6c1e0] | 786 | |
---|
[6c4ff37] | 787 | output << endl; |
---|
[2b6c1e0] | 788 | if ( wantSpacing( *i ) ) { |
---|
| 789 | output << endl; |
---|
[3778cb2] | 790 | } // if |
---|
[8688ce1] | 791 | } // for |
---|
[f7cb0bc] | 792 | --indent; |
---|
[51587aa] | 793 | |
---|
[cda48b6] | 794 | output << indent << "}"; |
---|
[51587aa] | 795 | } |
---|
| 796 | |
---|
[8688ce1] | 797 | void CodeGenerator::visit( ExprStmt * exprStmt ) { |
---|
[6c4ff37] | 798 | assert( exprStmt ); |
---|
[262f085f] | 799 | Expression * expr = exprStmt->get_expr(); |
---|
| 800 | if ( genC ) { |
---|
| 801 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 802 | expr = new CastExpr( expr ); |
---|
| 803 | } |
---|
[321a2481] | 804 | expr->accept( *this ); |
---|
| 805 | output << ";"; |
---|
[51587aa] | 806 | } |
---|
| 807 | |
---|
[8688ce1] | 808 | void CodeGenerator::visit( AsmStmt * asmStmt ) { |
---|
[7f5566b] | 809 | output << "asm "; |
---|
| 810 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
| 811 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
| 812 | output << "( "; |
---|
| 813 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); |
---|
| 814 | output << " : "; |
---|
| 815 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
| 816 | output << " : "; |
---|
| 817 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
| 818 | output << " : "; |
---|
| 819 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
| 820 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
| 821 | output << " : "; |
---|
| 822 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
| 823 | output << *begin++; |
---|
| 824 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
| 825 | output << ", "; |
---|
| 826 | } // for |
---|
| 827 | } // if |
---|
| 828 | output << " );" ; |
---|
| 829 | } |
---|
| 830 | |
---|
[e994912] | 831 | void CodeGenerator::visit( AsmDecl * asmDecl ) { |
---|
| 832 | output << "asm "; |
---|
| 833 | AsmStmt * asmStmt = asmDecl->get_stmt(); |
---|
| 834 | output << "( "; |
---|
| 835 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); |
---|
| 836 | output << " )" ; |
---|
| 837 | } |
---|
| 838 | |
---|
[8688ce1] | 839 | void CodeGenerator::visit( IfStmt * ifStmt ) { |
---|
[4810867] | 840 | output << lineDirective( ifStmt ); |
---|
[7f5566b] | 841 | output << "if ( "; |
---|
| 842 | ifStmt->get_condition()->accept( *this ); |
---|
| 843 | output << " ) "; |
---|
[51587aa] | 844 | |
---|
[7f5566b] | 845 | ifStmt->get_thenPart()->accept( *this ); |
---|
[51587aa] | 846 | |
---|
| 847 | if ( ifStmt->get_elsePart() != 0) { |
---|
[2b6c1e0] | 848 | output << " else "; |
---|
[7f5566b] | 849 | ifStmt->get_elsePart()->accept( *this ); |
---|
[51587aa] | 850 | } // if |
---|
| 851 | } |
---|
| 852 | |
---|
[8688ce1] | 853 | void CodeGenerator::visit( SwitchStmt * switchStmt ) { |
---|
[4810867] | 854 | output << lineDirective( switchStmt ); |
---|
[7f5566b] | 855 | output << "switch ( " ; |
---|
| 856 | switchStmt->get_condition()->accept( *this ); |
---|
| 857 | output << " ) "; |
---|
[71f4e4f] | 858 | |
---|
[2b6c1e0] | 859 | output << "{" << std::endl; |
---|
[f7cb0bc] | 860 | ++indent; |
---|
[8688ce1] | 861 | acceptAll( switchStmt->get_statements(), *this ); |
---|
[f7cb0bc] | 862 | --indent; |
---|
[cda48b6] | 863 | output << indent << "}"; |
---|
[51587aa] | 864 | } |
---|
| 865 | |
---|
[8688ce1] | 866 | void CodeGenerator::visit( CaseStmt * caseStmt ) { |
---|
[4810867] | 867 | output << lineDirective( caseStmt ); |
---|
[cda48b6] | 868 | output << indent; |
---|
[eb3261f] | 869 | if ( caseStmt->isDefault()) { |
---|
[2b6c1e0] | 870 | output << "default"; |
---|
[eb3261f] | 871 | } else { |
---|
[2b6c1e0] | 872 | output << "case "; |
---|
[7f5566b] | 873 | caseStmt->get_condition()->accept( *this ); |
---|
[17cd4eb] | 874 | } // if |
---|
[6c4ff37] | 875 | output << ":\n"; |
---|
[71f4e4f] | 876 | |
---|
[51587aa] | 877 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
| 878 | |
---|
[f7cb0bc] | 879 | ++indent; |
---|
[51587aa] | 880 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
[cda48b6] | 881 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
[7f5566b] | 882 | (*i)->accept( *this ); |
---|
[6c4ff37] | 883 | output << endl; |
---|
[3778cb2] | 884 | } // for |
---|
[f7cb0bc] | 885 | --indent; |
---|
[51587aa] | 886 | } |
---|
| 887 | |
---|
[8688ce1] | 888 | void CodeGenerator::visit( BranchStmt * branchStmt ) { |
---|
[51587aa] | 889 | switch ( branchStmt->get_type()) { |
---|
| 890 | case BranchStmt::Goto: |
---|
| 891 | if ( ! branchStmt->get_target().empty() ) |
---|
[6c4ff37] | 892 | output << "goto " << branchStmt->get_target(); |
---|
[71f4e4f] | 893 | else { |
---|
[51587aa] | 894 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
[6c4ff37] | 895 | output << "goto *"; |
---|
[51587aa] | 896 | branchStmt->get_computedTarget()->accept( *this ); |
---|
| 897 | } // if |
---|
| 898 | } // if |
---|
| 899 | break; |
---|
| 900 | case BranchStmt::Break: |
---|
[6c4ff37] | 901 | output << "break"; |
---|
[51587aa] | 902 | break; |
---|
| 903 | case BranchStmt::Continue: |
---|
[6c4ff37] | 904 | output << "continue"; |
---|
[51587aa] | 905 | break; |
---|
[3778cb2] | 906 | } // switch |
---|
[2b6c1e0] | 907 | output << ";"; |
---|
[51587aa] | 908 | } |
---|
| 909 | |
---|
[8688ce1] | 910 | void CodeGenerator::visit( ReturnStmt * returnStmt ) { |
---|
[6c4ff37] | 911 | output << "return "; |
---|
[4b2589a] | 912 | maybeAccept( returnStmt->get_expr(), *this ); |
---|
[6c4ff37] | 913 | output << ";"; |
---|
[51587aa] | 914 | } |
---|
| 915 | |
---|
[daf1af8] | 916 | void CodeGenerator::visit( ThrowStmt * throwStmt ) { |
---|
| 917 | assertf( ! genC, "Throw statements should not reach code generation." ); |
---|
| 918 | |
---|
| 919 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? |
---|
| 920 | "throw" : "throwResume"); |
---|
| 921 | if (throwStmt->get_expr()) { |
---|
| 922 | output << " "; |
---|
| 923 | throwStmt->get_expr()->accept( *this ); |
---|
| 924 | } |
---|
| 925 | if (throwStmt->get_target()) { |
---|
| 926 | output << " _At "; |
---|
| 927 | throwStmt->get_target()->accept( *this ); |
---|
| 928 | } |
---|
| 929 | output << ";"; |
---|
| 930 | } |
---|
| 931 | |
---|
[8688ce1] | 932 | void CodeGenerator::visit( WhileStmt * whileStmt ) { |
---|
[321a2481] | 933 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 934 | output << "do" ; |
---|
[321a2481] | 935 | } else { |
---|
[6c4ff37] | 936 | output << "while (" ; |
---|
[7f5566b] | 937 | whileStmt->get_condition()->accept( *this ); |
---|
[6c4ff37] | 938 | output << ")"; |
---|
[51587aa] | 939 | } // if |
---|
[2b6c1e0] | 940 | output << " "; |
---|
[51587aa] | 941 | |
---|
[2b6c1e0] | 942 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); |
---|
[51587aa] | 943 | whileStmt->get_body()->accept( *this ); |
---|
| 944 | |
---|
[cda48b6] | 945 | output << indent; |
---|
[51587aa] | 946 | |
---|
| 947 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 948 | output << " while (" ; |
---|
[7f5566b] | 949 | whileStmt->get_condition()->accept( *this ); |
---|
[6c4ff37] | 950 | output << ");"; |
---|
[51587aa] | 951 | } // if |
---|
| 952 | } |
---|
| 953 | |
---|
[8688ce1] | 954 | void CodeGenerator::visit( ForStmt * forStmt ) { |
---|
[8e9cbb2] | 955 | // initialization is always hoisted, so don't bother doing anything with that |
---|
[145f1fc] | 956 | output << "for (;"; |
---|
[51587aa] | 957 | |
---|
[321a2481] | 958 | if ( forStmt->get_condition() != 0 ) { |
---|
[51587aa] | 959 | forStmt->get_condition()->accept( *this ); |
---|
[3778cb2] | 960 | } // if |
---|
[6c4ff37] | 961 | output << ";"; |
---|
[51587aa] | 962 | |
---|
[321a2481] | 963 | if ( forStmt->get_increment() != 0 ) { |
---|
| 964 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 965 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
| 966 | expr->accept( *this ); |
---|
[3778cb2] | 967 | } // if |
---|
[2b6c1e0] | 968 | output << ") "; |
---|
[51587aa] | 969 | |
---|
| 970 | if ( forStmt->get_body() != 0 ) { |
---|
[2b6c1e0] | 971 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
[51587aa] | 972 | forStmt->get_body()->accept( *this ); |
---|
| 973 | } // if |
---|
| 974 | } |
---|
| 975 | |
---|
[7e003011] | 976 | void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) { |
---|
[cda48b6] | 977 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
[6c4ff37] | 978 | output << "/* null statement */ ;"; |
---|
[51587aa] | 979 | } |
---|
| 980 | |
---|
[8688ce1] | 981 | void CodeGenerator::visit( DeclStmt * declStmt ) { |
---|
[51587aa] | 982 | declStmt->get_decl()->accept( *this ); |
---|
[71f4e4f] | 983 | |
---|
[51587aa] | 984 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
[6c4ff37] | 985 | output << ";"; |
---|
[51587aa] | 986 | } // if |
---|
| 987 | } |
---|
| 988 | |
---|
[dd020c0] | 989 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) { |
---|
[fb04321] | 990 | if ( decl->get_storageClasses().any() ) { |
---|
[6e8bd43] | 991 | decl->get_storageClasses().print( output ); |
---|
[a7c90d4] | 992 | } // if |
---|
[dd020c0] | 993 | } // CodeGenerator::handleStorageClass |
---|
[9facf3b] | 994 | |
---|
| 995 | std::string genName( DeclarationWithType * decl ) { |
---|
| 996 | CodeGen::OperatorInfo opInfo; |
---|
| 997 | if ( operatorLookup( decl->get_name(), opInfo ) ) { |
---|
| 998 | return opInfo.outputName; |
---|
| 999 | } else { |
---|
| 1000 | return decl->get_name(); |
---|
| 1001 | } // if |
---|
| 1002 | } |
---|
[51b7345] | 1003 | } // namespace CodeGen |
---|
[51587aa] | 1004 | |
---|
| 1005 | // Local Variables: // |
---|
| 1006 | // tab-width: 4 // |
---|
| 1007 | // mode: c++ // |
---|
| 1008 | // compile-command: "make install" // |
---|
| 1009 | // End: // |
---|