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