[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 | |
---|
[e45215c] | 290 | void CodeGenerator::printDesignators( std::list< Expression * > & designators ) { |
---|
| 291 | typedef std::list< Expression * > DesignatorList; |
---|
| 292 | if ( designators.size() == 0 ) return; |
---|
| 293 | for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) { |
---|
[8a4da06] | 294 | if ( dynamic_cast< NameExpr * >( *iter ) ) { |
---|
[f32c7f4] | 295 | // if expression is a name, then initializing aggregate member |
---|
| 296 | output << "."; |
---|
| 297 | (*iter)->accept( *this ); |
---|
| 298 | } else { |
---|
| 299 | // if not a simple name, it has to be a constant expression, i.e. an array designator |
---|
[e45215c] | 300 | output << "["; |
---|
| 301 | (*iter)->accept( *this ); |
---|
| 302 | output << "]"; |
---|
[3778cb2] | 303 | } // if |
---|
| 304 | } // for |
---|
[e45215c] | 305 | output << " = "; |
---|
| 306 | } |
---|
| 307 | |
---|
[8688ce1] | 308 | void CodeGenerator::visit( SingleInit * init ) { |
---|
[e45215c] | 309 | printDesignators( init->get_designators() ); |
---|
[51587aa] | 310 | init->get_value()->accept( *this ); |
---|
| 311 | } |
---|
| 312 | |
---|
[8688ce1] | 313 | void CodeGenerator::visit( ListInit * init ) { |
---|
[e45215c] | 314 | printDesignators( init->get_designators() ); |
---|
[6c4ff37] | 315 | output << "{ "; |
---|
[01b9928] | 316 | genCommaList( init->begin(), init->end() ); |
---|
[6c4ff37] | 317 | output << " }"; |
---|
[51587aa] | 318 | } |
---|
| 319 | |
---|
[7e003011] | 320 | void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){ |
---|
[e39241b] | 321 | assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); |
---|
| 322 | // xxx - generate something reasonable for constructor/destructor pairs |
---|
| 323 | output << "<ctorinit>"; |
---|
[fc638d2] | 324 | } |
---|
| 325 | |
---|
[8688ce1] | 326 | void CodeGenerator::visit( Constant * constant ) { |
---|
[6c4ff37] | 327 | output << constant->get_value() ; |
---|
[51587aa] | 328 | } |
---|
| 329 | |
---|
[4810867] | 330 | // *** Expressions |
---|
[8688ce1] | 331 | void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { |
---|
[e04ef3a] | 332 | extension( applicationExpr ); |
---|
[8688ce1] | 333 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
[51587aa] | 334 | OperatorInfo opInfo; |
---|
| 335 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { |
---|
| 336 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
| 337 | switch ( opInfo.type ) { |
---|
| 338 | case OT_PREFIXASSIGN: |
---|
| 339 | case OT_POSTFIXASSIGN: |
---|
| 340 | case OT_INFIXASSIGN: |
---|
[356189a] | 341 | case OT_CTOR: |
---|
[c2ce2350] | 342 | case OT_DTOR: |
---|
[51587aa] | 343 | { |
---|
| 344 | assert( arg != applicationExpr->get_args().end() ); |
---|
[8688ce1] | 345 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { |
---|
[356189a] | 346 | // remove & from first assignment/ctor argument |
---|
[51587aa] | 347 | *arg = addrExpr->get_arg(); |
---|
| 348 | } else { |
---|
[356189a] | 349 | // no address-of operator, so must be a pointer - add dereference |
---|
[066d77a] | 350 | // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared. |
---|
| 351 | // Since its arguments are modified here, this assertion most commonly triggers when the application |
---|
| 352 | // is visited multiple times. |
---|
[8688ce1] | 353 | UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
[51587aa] | 354 | newExpr->get_args().push_back( *arg ); |
---|
[906e24d] | 355 | Type * type = InitTweak::getPointerBase( (*arg)->get_result() ); |
---|
[066d77a] | 356 | assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." ); |
---|
[906e24d] | 357 | newExpr->set_result( type->clone() ); |
---|
[51587aa] | 358 | *arg = newExpr; |
---|
| 359 | } // if |
---|
| 360 | break; |
---|
| 361 | } |
---|
[71f4e4f] | 362 | |
---|
[51587aa] | 363 | default: |
---|
| 364 | // do nothing |
---|
| 365 | ; |
---|
[3778cb2] | 366 | } // switch |
---|
[71f4e4f] | 367 | |
---|
[51587aa] | 368 | switch ( opInfo.type ) { |
---|
| 369 | case OT_INDEX: |
---|
| 370 | assert( applicationExpr->get_args().size() == 2 ); |
---|
| 371 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 372 | output << "["; |
---|
[51587aa] | 373 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 374 | output << "]"; |
---|
[51587aa] | 375 | break; |
---|
[71f4e4f] | 376 | |
---|
[51587aa] | 377 | case OT_CALL: |
---|
[356189a] | 378 | // there are no intrinsic definitions of the function call operator |
---|
[51587aa] | 379 | assert( false ); |
---|
| 380 | break; |
---|
[71f4e4f] | 381 | |
---|
[f1e012b] | 382 | case OT_CTOR: |
---|
[c2ce2350] | 383 | case OT_DTOR: |
---|
[356189a] | 384 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 385 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 386 | // effects, so must still output this expression |
---|
[64071c2] | 387 | output << "("; |
---|
[356189a] | 388 | (*arg++)->accept( *this ); |
---|
[64071c2] | 389 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 390 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
[c2ce2350] | 391 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
[356189a] | 392 | output << "("; |
---|
| 393 | (*arg++)->accept( *this ); |
---|
| 394 | output << opInfo.symbol; |
---|
| 395 | (*arg)->accept( *this ); |
---|
[c2ce2350] | 396 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 397 | } else { |
---|
[c2ce2350] | 398 | // no constructors with 0 or more than 2 parameters |
---|
[356189a] | 399 | assert( false ); |
---|
[8688ce1] | 400 | } // if |
---|
[356189a] | 401 | break; |
---|
[f1e012b] | 402 | |
---|
[51587aa] | 403 | case OT_PREFIX: |
---|
| 404 | case OT_PREFIXASSIGN: |
---|
| 405 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 406 | output << "("; |
---|
| 407 | output << opInfo.symbol; |
---|
[51587aa] | 408 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 409 | output << ")"; |
---|
[51587aa] | 410 | break; |
---|
[71f4e4f] | 411 | |
---|
[51587aa] | 412 | case OT_POSTFIX: |
---|
| 413 | case OT_POSTFIXASSIGN: |
---|
| 414 | assert( applicationExpr->get_args().size() == 1 ); |
---|
| 415 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 416 | output << opInfo.symbol; |
---|
[51587aa] | 417 | break; |
---|
| 418 | |
---|
[f1e012b] | 419 | |
---|
[51587aa] | 420 | case OT_INFIX: |
---|
| 421 | case OT_INFIXASSIGN: |
---|
| 422 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 423 | output << "("; |
---|
[51587aa] | 424 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 425 | output << opInfo.symbol; |
---|
[51587aa] | 426 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 427 | output << ")"; |
---|
[51587aa] | 428 | break; |
---|
[71f4e4f] | 429 | |
---|
[51587aa] | 430 | case OT_CONSTANT: |
---|
[721f17a] | 431 | case OT_LABELADDRESS: |
---|
| 432 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[51587aa] | 433 | assert( false ); |
---|
[3778cb2] | 434 | } // switch |
---|
[17cd4eb] | 435 | } else { |
---|
[51587aa] | 436 | varExpr->accept( *this ); |
---|
[6c4ff37] | 437 | output << "("; |
---|
[51587aa] | 438 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 439 | output << ")"; |
---|
[17cd4eb] | 440 | } // if |
---|
[51587aa] | 441 | } else { |
---|
| 442 | applicationExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 443 | output << "("; |
---|
[51587aa] | 444 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 445 | output << ")"; |
---|
[51587aa] | 446 | } // if |
---|
| 447 | } |
---|
[71f4e4f] | 448 | |
---|
[8688ce1] | 449 | void CodeGenerator::visit( UntypedExpr * untypedExpr ) { |
---|
[e04ef3a] | 450 | extension( untypedExpr ); |
---|
[8688ce1] | 451 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { |
---|
[51587aa] | 452 | OperatorInfo opInfo; |
---|
| 453 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
| 454 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); |
---|
| 455 | switch ( opInfo.type ) { |
---|
| 456 | case OT_INDEX: |
---|
| 457 | assert( untypedExpr->get_args().size() == 2 ); |
---|
| 458 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 459 | output << "["; |
---|
[51587aa] | 460 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 461 | output << "]"; |
---|
[51587aa] | 462 | break; |
---|
[71f4e4f] | 463 | |
---|
[51587aa] | 464 | case OT_CALL: |
---|
[f1e012b] | 465 | assert( false ); |
---|
| 466 | |
---|
[c2ce2350] | 467 | case OT_CTOR: |
---|
| 468 | case OT_DTOR: |
---|
| 469 | if ( untypedExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 470 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 471 | // effects, so must still output this expression |
---|
[64071c2] | 472 | output << "("; |
---|
[c2ce2350] | 473 | (*arg++)->accept( *this ); |
---|
[64071c2] | 474 | output << ") /* " << opInfo.inputName << " */"; |
---|
[c2ce2350] | 475 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
| 476 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
| 477 | output << "("; |
---|
| 478 | (*arg++)->accept( *this ); |
---|
| 479 | output << opInfo.symbol; |
---|
| 480 | (*arg)->accept( *this ); |
---|
| 481 | output << ") /* " << opInfo.inputName << " */"; |
---|
| 482 | } else { |
---|
| 483 | // no constructors with 0 or more than 2 parameters |
---|
| 484 | assert( false ); |
---|
[3778cb2] | 485 | } // if |
---|
[51587aa] | 486 | break; |
---|
[71f4e4f] | 487 | |
---|
[51587aa] | 488 | case OT_PREFIX: |
---|
| 489 | case OT_PREFIXASSIGN: |
---|
[de62360d] | 490 | case OT_LABELADDRESS: |
---|
[51587aa] | 491 | assert( untypedExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 492 | output << "("; |
---|
| 493 | output << opInfo.symbol; |
---|
[51587aa] | 494 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 495 | output << ")"; |
---|
[51587aa] | 496 | break; |
---|
[71f4e4f] | 497 | |
---|
[51587aa] | 498 | case OT_POSTFIX: |
---|
| 499 | case OT_POSTFIXASSIGN: |
---|
| 500 | assert( untypedExpr->get_args().size() == 1 ); |
---|
| 501 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 502 | output << opInfo.symbol; |
---|
[51587aa] | 503 | break; |
---|
[71f4e4f] | 504 | |
---|
[51587aa] | 505 | case OT_INFIX: |
---|
| 506 | case OT_INFIXASSIGN: |
---|
| 507 | assert( untypedExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 508 | output << "("; |
---|
[51587aa] | 509 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 510 | output << opInfo.symbol; |
---|
[51587aa] | 511 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 512 | output << ")"; |
---|
[51587aa] | 513 | break; |
---|
[71f4e4f] | 514 | |
---|
[51587aa] | 515 | case OT_CONSTANT: |
---|
| 516 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
| 517 | assert( false ); |
---|
[3778cb2] | 518 | } // switch |
---|
[51587aa] | 519 | } else { |
---|
[8688ce1] | 520 | if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 |
---|
[ac911f4] | 521 | assert( untypedExpr->get_args().size() == 2 ); |
---|
| 522 | (*untypedExpr->get_args().begin())->accept( *this ); |
---|
| 523 | output << " ... "; |
---|
| 524 | (*--untypedExpr->get_args().end())->accept( *this ); |
---|
[057b34f] | 525 | } else { // builtin routines |
---|
| 526 | nameExpr->accept( *this ); |
---|
| 527 | output << "("; |
---|
| 528 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
| 529 | output << ")"; |
---|
[66d12f7] | 530 | } // if |
---|
[51587aa] | 531 | } // if |
---|
| 532 | } else { |
---|
| 533 | untypedExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 534 | output << "("; |
---|
[51587aa] | 535 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
[6c4ff37] | 536 | output << ")"; |
---|
[51587aa] | 537 | } // if |
---|
| 538 | } |
---|
[71f4e4f] | 539 | |
---|
[064e3ff] | 540 | void CodeGenerator::visit( RangeExpr * rangeExpr ) { |
---|
| 541 | rangeExpr->get_low()->accept( *this ); |
---|
| 542 | output << " ... "; |
---|
| 543 | rangeExpr->get_high()->accept( *this ); |
---|
| 544 | } |
---|
| 545 | |
---|
[8688ce1] | 546 | void CodeGenerator::visit( NameExpr * nameExpr ) { |
---|
[e04ef3a] | 547 | extension( nameExpr ); |
---|
[51587aa] | 548 | OperatorInfo opInfo; |
---|
| 549 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
| 550 | assert( opInfo.type == OT_CONSTANT ); |
---|
[6c4ff37] | 551 | output << opInfo.symbol; |
---|
[51587aa] | 552 | } else { |
---|
[6c4ff37] | 553 | output << nameExpr->get_name(); |
---|
[51587aa] | 554 | } // if |
---|
| 555 | } |
---|
[71f4e4f] | 556 | |
---|
[8688ce1] | 557 | void CodeGenerator::visit( AddressExpr * addressExpr ) { |
---|
[e04ef3a] | 558 | extension( addressExpr ); |
---|
[6c4ff37] | 559 | output << "(&"; |
---|
[51587aa] | 560 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address |
---|
[8688ce1] | 561 | if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { |
---|
[6c4ff37] | 562 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 563 | } else { |
---|
| 564 | addressExpr->get_arg()->accept( *this ); |
---|
| 565 | } // if |
---|
[6c4ff37] | 566 | output << ")"; |
---|
[51587aa] | 567 | } |
---|
| 568 | |
---|
[8688ce1] | 569 | void CodeGenerator::visit( CastExpr * castExpr ) { |
---|
[e04ef3a] | 570 | extension( castExpr ); |
---|
[803deb1] | 571 | output << "("; |
---|
[906e24d] | 572 | if ( castExpr->get_result()->isVoid() ) { |
---|
[803deb1] | 573 | output << "(void)" ; |
---|
[615a096] | 574 | } else if ( ! castExpr->get_result()->get_lvalue() ) { |
---|
[803deb1] | 575 | // at least one result type of cast, but not an lvalue |
---|
| 576 | output << "("; |
---|
[e39241b] | 577 | output << genType( castExpr->get_result(), "", pretty, genC ); |
---|
[71f4e4f] | 578 | output << ")"; |
---|
[803deb1] | 579 | } else { |
---|
[8e9cbb2] | 580 | // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is |
---|
[803deb1] | 581 | // never an lvalue in C |
---|
[3778cb2] | 582 | } // if |
---|
[803deb1] | 583 | castExpr->get_arg()->accept( *this ); |
---|
| 584 | output << ")"; |
---|
[51587aa] | 585 | } |
---|
[71f4e4f] | 586 | |
---|
[8688ce1] | 587 | void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { |
---|
[e39241b] | 588 | assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); |
---|
| 589 | extension( memberExpr ); |
---|
| 590 | memberExpr->get_aggregate()->accept( *this ); |
---|
[5f642e38] | 591 | output << "."; |
---|
| 592 | memberExpr->get_member()->accept( *this ); |
---|
[51587aa] | 593 | } |
---|
[71f4e4f] | 594 | |
---|
[8688ce1] | 595 | void CodeGenerator::visit( MemberExpr * memberExpr ) { |
---|
[e04ef3a] | 596 | extension( memberExpr ); |
---|
[51587aa] | 597 | memberExpr->get_aggregate()->accept( *this ); |
---|
[6c4ff37] | 598 | output << "." << mangleName( memberExpr->get_member() ); |
---|
[51587aa] | 599 | } |
---|
[71f4e4f] | 600 | |
---|
[8688ce1] | 601 | void CodeGenerator::visit( VariableExpr * variableExpr ) { |
---|
[e04ef3a] | 602 | extension( variableExpr ); |
---|
[51587aa] | 603 | OperatorInfo opInfo; |
---|
| 604 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { |
---|
[6c4ff37] | 605 | output << opInfo.symbol; |
---|
[51587aa] | 606 | } else { |
---|
[6c4ff37] | 607 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 608 | } // if |
---|
| 609 | } |
---|
[71f4e4f] | 610 | |
---|
[8688ce1] | 611 | void CodeGenerator::visit( ConstantExpr * constantExpr ) { |
---|
[51587aa] | 612 | assert( constantExpr->get_constant() ); |
---|
[e04ef3a] | 613 | extension( constantExpr ); |
---|
[51587aa] | 614 | constantExpr->get_constant()->accept( *this ); |
---|
| 615 | } |
---|
[71f4e4f] | 616 | |
---|
[8688ce1] | 617 | void CodeGenerator::visit( SizeofExpr * sizeofExpr ) { |
---|
[e04ef3a] | 618 | extension( sizeofExpr ); |
---|
[6c4ff37] | 619 | output << "sizeof("; |
---|
[51587aa] | 620 | if ( sizeofExpr->get_isType() ) { |
---|
[e39241b] | 621 | output << genType( sizeofExpr->get_type(), "", pretty, genC ); |
---|
[51587aa] | 622 | } else { |
---|
| 623 | sizeofExpr->get_expr()->accept( *this ); |
---|
| 624 | } // if |
---|
[6c4ff37] | 625 | output << ")"; |
---|
[51587aa] | 626 | } |
---|
[47534159] | 627 | |
---|
[8688ce1] | 628 | void CodeGenerator::visit( AlignofExpr * alignofExpr ) { |
---|
[47534159] | 629 | // use GCC extension to avoid bumping std to C11 |
---|
[8e9cbb2] | 630 | extension( alignofExpr ); |
---|
[47534159] | 631 | output << "__alignof__("; |
---|
[25a054f] | 632 | if ( alignofExpr->get_isType() ) { |
---|
[e39241b] | 633 | output << genType( alignofExpr->get_type(), "", pretty, genC ); |
---|
[47534159] | 634 | } else { |
---|
[25a054f] | 635 | alignofExpr->get_expr()->accept( *this ); |
---|
[47534159] | 636 | } // if |
---|
| 637 | output << ")"; |
---|
| 638 | } |
---|
[71f4e4f] | 639 | |
---|
[8688ce1] | 640 | void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { |
---|
[e39241b] | 641 | assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); |
---|
| 642 | output << "offsetof("; |
---|
| 643 | output << genType( offsetofExpr->get_type(), "", pretty, genC ); |
---|
| 644 | output << ", " << offsetofExpr->get_member(); |
---|
| 645 | output << ")"; |
---|
[2a4b088] | 646 | } |
---|
| 647 | |
---|
[8688ce1] | 648 | void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) { |
---|
[25a054f] | 649 | // use GCC builtin |
---|
| 650 | output << "__builtin_offsetof("; |
---|
[e39241b] | 651 | output << genType( offsetofExpr->get_type(), "", pretty, genC ); |
---|
[e551c69] | 652 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
[25a054f] | 653 | output << ")"; |
---|
| 654 | } |
---|
[d63eeb0] | 655 | |
---|
[8688ce1] | 656 | void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { |
---|
[e39241b] | 657 | assertf( ! genC, "OffsetPackExpr should not reach code generation." ); |
---|
| 658 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; |
---|
[afc1045] | 659 | } |
---|
[70a06f6] | 660 | |
---|
[8688ce1] | 661 | void CodeGenerator::visit( LogicalExpr * logicalExpr ) { |
---|
[e04ef3a] | 662 | extension( logicalExpr ); |
---|
[6c4ff37] | 663 | output << "("; |
---|
[51587aa] | 664 | logicalExpr->get_arg1()->accept( *this ); |
---|
| 665 | if ( logicalExpr->get_isAnd() ) { |
---|
[6c4ff37] | 666 | output << " && "; |
---|
[51587aa] | 667 | } else { |
---|
[6c4ff37] | 668 | output << " || "; |
---|
[51587aa] | 669 | } // if |
---|
| 670 | logicalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 671 | output << ")"; |
---|
[51587aa] | 672 | } |
---|
[71f4e4f] | 673 | |
---|
[8688ce1] | 674 | void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) { |
---|
[e04ef3a] | 675 | extension( conditionalExpr ); |
---|
[6c4ff37] | 676 | output << "("; |
---|
[51587aa] | 677 | conditionalExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 678 | output << " ? "; |
---|
[51587aa] | 679 | conditionalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 680 | output << " : "; |
---|
[51587aa] | 681 | conditionalExpr->get_arg3()->accept( *this ); |
---|
[6c4ff37] | 682 | output << ")"; |
---|
[51587aa] | 683 | } |
---|
[71f4e4f] | 684 | |
---|
[8688ce1] | 685 | void CodeGenerator::visit( CommaExpr * commaExpr ) { |
---|
[e04ef3a] | 686 | extension( commaExpr ); |
---|
[6c4ff37] | 687 | output << "("; |
---|
[51587aa] | 688 | commaExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 689 | output << " , "; |
---|
[51587aa] | 690 | commaExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 691 | output << ")"; |
---|
[51587aa] | 692 | } |
---|
[71f4e4f] | 693 | |
---|
[e39241b] | 694 | void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { |
---|
| 695 | assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); |
---|
[f975c65] | 696 | extension( tupleExpr ); |
---|
[e39241b] | 697 | output << "["; |
---|
| 698 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 699 | output << "]"; |
---|
| 700 | } |
---|
[907eccb] | 701 | |
---|
[e39241b] | 702 | void CodeGenerator::visit( TupleExpr * tupleExpr ) { |
---|
| 703 | assertf( ! genC, "TupleExpr should not reach code generation." ); |
---|
[f975c65] | 704 | extension( tupleExpr ); |
---|
[e39241b] | 705 | output << "["; |
---|
| 706 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 707 | output << "]"; |
---|
| 708 | } |
---|
[71f4e4f] | 709 | |
---|
[f975c65] | 710 | void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) { |
---|
| 711 | assertf( ! genC, "TupleIndexExpr should not reach code generation." ); |
---|
| 712 | extension( tupleExpr ); |
---|
| 713 | tupleExpr->get_tuple()->accept( *this ); |
---|
| 714 | output << "." << tupleExpr->get_index(); |
---|
| 715 | } |
---|
| 716 | |
---|
[e39241b] | 717 | void CodeGenerator::visit( TypeExpr * typeExpr ) { |
---|
| 718 | assertf( ! genC, "TypeExpr should not reach code generation." ); |
---|
| 719 | output<< genType( typeExpr->get_type(), "", pretty, genC ); |
---|
| 720 | } |
---|
[2b6c1e0] | 721 | |
---|
[8688ce1] | 722 | void CodeGenerator::visit( AsmExpr * asmExpr ) { |
---|
[7f5566b] | 723 | if ( asmExpr->get_inout() ) { |
---|
| 724 | output << "[ "; |
---|
| 725 | asmExpr->get_inout()->accept( *this ); |
---|
| 726 | output << " ] "; |
---|
| 727 | } // if |
---|
| 728 | asmExpr->get_constraint()->accept( *this ); |
---|
| 729 | output << " ( "; |
---|
| 730 | asmExpr->get_operand()->accept( *this ); |
---|
| 731 | output << " )"; |
---|
| 732 | } |
---|
| 733 | |
---|
[3c13c03] | 734 | void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) { |
---|
[fbcde64] | 735 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); |
---|
[e39241b] | 736 | output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")"; |
---|
[3c13c03] | 737 | compLitExpr->get_initializer()->accept( *this ); |
---|
| 738 | } |
---|
| 739 | |
---|
[6eb8948] | 740 | void CodeGenerator::visit( StmtExpr * stmtExpr ) { |
---|
[3c13c03] | 741 | std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); |
---|
[4810867] | 742 | output << lineDirective( stmtExpr) << "({" << std::endl; |
---|
[3c13c03] | 743 | cur_indent += CodeGenerator::tabsize; |
---|
| 744 | unsigned int numStmts = stmts.size(); |
---|
| 745 | unsigned int i = 0; |
---|
| 746 | for ( Statement * stmt : stmts ) { |
---|
[4810867] | 747 | output << lineDirective( stmt ) << indent; |
---|
| 748 | output << printLabels( stmt->get_labels() ); |
---|
[3c13c03] | 749 | if ( i+1 == numStmts ) { |
---|
| 750 | // last statement in a statement expression needs to be handled specially - |
---|
| 751 | // cannot cast to void, otherwise the expression statement has no value |
---|
| 752 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { |
---|
| 753 | exprStmt->get_expr()->accept( *this ); |
---|
| 754 | output << ";" << endl; |
---|
| 755 | ++i; |
---|
| 756 | break; |
---|
| 757 | } |
---|
| 758 | } |
---|
| 759 | stmt->accept( *this ); |
---|
| 760 | output << endl; |
---|
| 761 | if ( wantSpacing( stmt ) ) { |
---|
| 762 | output << endl; |
---|
| 763 | } // if |
---|
| 764 | ++i; |
---|
| 765 | } |
---|
| 766 | cur_indent -= CodeGenerator::tabsize; |
---|
| 767 | output << indent << "})"; |
---|
[6eb8948] | 768 | } |
---|
| 769 | |
---|
[4810867] | 770 | // *** Statements |
---|
[8688ce1] | 771 | void CodeGenerator::visit( CompoundStmt * compoundStmt ) { |
---|
[51587aa] | 772 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
[2b6c1e0] | 773 | output << "{" << endl; |
---|
[51587aa] | 774 | |
---|
[2b6c1e0] | 775 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 776 | |
---|
[7f5566b] | 777 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
[cda48b6] | 778 | output << indent << printLabels( (*i)->get_labels() ); |
---|
[7f5566b] | 779 | (*i)->accept( *this ); |
---|
[2b6c1e0] | 780 | |
---|
[6c4ff37] | 781 | output << endl; |
---|
[2b6c1e0] | 782 | if ( wantSpacing( *i ) ) { |
---|
| 783 | output << endl; |
---|
[3778cb2] | 784 | } // if |
---|
[8688ce1] | 785 | } // for |
---|
[71f4e4f] | 786 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 787 | |
---|
[cda48b6] | 788 | output << indent << "}"; |
---|
[51587aa] | 789 | } |
---|
| 790 | |
---|
[8688ce1] | 791 | void CodeGenerator::visit( ExprStmt * exprStmt ) { |
---|
[6c4ff37] | 792 | assert( exprStmt ); |
---|
[262f085f] | 793 | Expression * expr = exprStmt->get_expr(); |
---|
| 794 | if ( genC ) { |
---|
| 795 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 796 | expr = new CastExpr( expr ); |
---|
| 797 | } |
---|
[321a2481] | 798 | expr->accept( *this ); |
---|
| 799 | output << ";"; |
---|
[51587aa] | 800 | } |
---|
| 801 | |
---|
[8688ce1] | 802 | void CodeGenerator::visit( AsmStmt * asmStmt ) { |
---|
[7f5566b] | 803 | output << "asm "; |
---|
| 804 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
| 805 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
| 806 | output << "( "; |
---|
| 807 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); |
---|
| 808 | output << " : "; |
---|
| 809 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
| 810 | output << " : "; |
---|
| 811 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
| 812 | output << " : "; |
---|
| 813 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
| 814 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
| 815 | output << " : "; |
---|
| 816 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
| 817 | output << *begin++; |
---|
| 818 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
| 819 | output << ", "; |
---|
| 820 | } // for |
---|
| 821 | } // if |
---|
| 822 | output << " );" ; |
---|
| 823 | } |
---|
| 824 | |
---|
[e994912] | 825 | void CodeGenerator::visit( AsmDecl * asmDecl ) { |
---|
| 826 | output << "asm "; |
---|
| 827 | AsmStmt * asmStmt = asmDecl->get_stmt(); |
---|
| 828 | output << "( "; |
---|
| 829 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); |
---|
| 830 | output << " )" ; |
---|
| 831 | } |
---|
| 832 | |
---|
[8688ce1] | 833 | void CodeGenerator::visit( IfStmt * ifStmt ) { |
---|
[4810867] | 834 | output << lineDirective( ifStmt ); |
---|
[7f5566b] | 835 | output << "if ( "; |
---|
| 836 | ifStmt->get_condition()->accept( *this ); |
---|
| 837 | output << " ) "; |
---|
[51587aa] | 838 | |
---|
[7f5566b] | 839 | ifStmt->get_thenPart()->accept( *this ); |
---|
[51587aa] | 840 | |
---|
| 841 | if ( ifStmt->get_elsePart() != 0) { |
---|
[2b6c1e0] | 842 | output << " else "; |
---|
[7f5566b] | 843 | ifStmt->get_elsePart()->accept( *this ); |
---|
[51587aa] | 844 | } // if |
---|
| 845 | } |
---|
| 846 | |
---|
[8688ce1] | 847 | void CodeGenerator::visit( SwitchStmt * switchStmt ) { |
---|
[4810867] | 848 | output << lineDirective( switchStmt ); |
---|
[7f5566b] | 849 | output << "switch ( " ; |
---|
| 850 | switchStmt->get_condition()->accept( *this ); |
---|
| 851 | output << " ) "; |
---|
[71f4e4f] | 852 | |
---|
[2b6c1e0] | 853 | output << "{" << std::endl; |
---|
[6c4ff37] | 854 | cur_indent += CodeGenerator::tabsize; |
---|
[8688ce1] | 855 | acceptAll( switchStmt->get_statements(), *this ); |
---|
[6c4ff37] | 856 | cur_indent -= CodeGenerator::tabsize; |
---|
[cda48b6] | 857 | output << indent << "}"; |
---|
[51587aa] | 858 | } |
---|
| 859 | |
---|
[8688ce1] | 860 | void CodeGenerator::visit( CaseStmt * caseStmt ) { |
---|
[4810867] | 861 | output << lineDirective( caseStmt ); |
---|
[cda48b6] | 862 | output << indent; |
---|
[eb3261f] | 863 | if ( caseStmt->isDefault()) { |
---|
[2b6c1e0] | 864 | output << "default"; |
---|
[eb3261f] | 865 | } else { |
---|
[2b6c1e0] | 866 | output << "case "; |
---|
[7f5566b] | 867 | caseStmt->get_condition()->accept( *this ); |
---|
[17cd4eb] | 868 | } // if |
---|
[6c4ff37] | 869 | output << ":\n"; |
---|
[71f4e4f] | 870 | |
---|
[51587aa] | 871 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
| 872 | |
---|
[6c4ff37] | 873 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 874 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
[cda48b6] | 875 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
[7f5566b] | 876 | (*i)->accept( *this ); |
---|
[6c4ff37] | 877 | output << endl; |
---|
[3778cb2] | 878 | } // for |
---|
[6c4ff37] | 879 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 880 | } |
---|
| 881 | |
---|
[8688ce1] | 882 | void CodeGenerator::visit( BranchStmt * branchStmt ) { |
---|
[51587aa] | 883 | switch ( branchStmt->get_type()) { |
---|
| 884 | case BranchStmt::Goto: |
---|
| 885 | if ( ! branchStmt->get_target().empty() ) |
---|
[6c4ff37] | 886 | output << "goto " << branchStmt->get_target(); |
---|
[71f4e4f] | 887 | else { |
---|
[51587aa] | 888 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
[6c4ff37] | 889 | output << "goto *"; |
---|
[51587aa] | 890 | branchStmt->get_computedTarget()->accept( *this ); |
---|
| 891 | } // if |
---|
| 892 | } // if |
---|
| 893 | break; |
---|
| 894 | case BranchStmt::Break: |
---|
[6c4ff37] | 895 | output << "break"; |
---|
[51587aa] | 896 | break; |
---|
| 897 | case BranchStmt::Continue: |
---|
[6c4ff37] | 898 | output << "continue"; |
---|
[51587aa] | 899 | break; |
---|
[3778cb2] | 900 | } // switch |
---|
[2b6c1e0] | 901 | output << ";"; |
---|
[51587aa] | 902 | } |
---|
| 903 | |
---|
[8688ce1] | 904 | void CodeGenerator::visit( ReturnStmt * returnStmt ) { |
---|
[6c4ff37] | 905 | output << "return "; |
---|
[4b2589a] | 906 | maybeAccept( returnStmt->get_expr(), *this ); |
---|
[6c4ff37] | 907 | output << ";"; |
---|
[51587aa] | 908 | } |
---|
| 909 | |
---|
[daf1af8] | 910 | void CodeGenerator::visit( ThrowStmt * throwStmt ) { |
---|
| 911 | assertf( ! genC, "Throw statements should not reach code generation." ); |
---|
| 912 | |
---|
| 913 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? |
---|
| 914 | "throw" : "throwResume"); |
---|
| 915 | if (throwStmt->get_expr()) { |
---|
| 916 | output << " "; |
---|
| 917 | throwStmt->get_expr()->accept( *this ); |
---|
| 918 | } |
---|
| 919 | if (throwStmt->get_target()) { |
---|
| 920 | output << " _At "; |
---|
| 921 | throwStmt->get_target()->accept( *this ); |
---|
| 922 | } |
---|
| 923 | output << ";"; |
---|
| 924 | } |
---|
| 925 | |
---|
[8688ce1] | 926 | void CodeGenerator::visit( WhileStmt * whileStmt ) { |
---|
[321a2481] | 927 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 928 | output << "do" ; |
---|
[321a2481] | 929 | } else { |
---|
[6c4ff37] | 930 | output << "while (" ; |
---|
[7f5566b] | 931 | whileStmt->get_condition()->accept( *this ); |
---|
[6c4ff37] | 932 | output << ")"; |
---|
[51587aa] | 933 | } // if |
---|
[2b6c1e0] | 934 | output << " "; |
---|
[51587aa] | 935 | |
---|
[2b6c1e0] | 936 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); |
---|
[51587aa] | 937 | whileStmt->get_body()->accept( *this ); |
---|
| 938 | |
---|
[cda48b6] | 939 | output << indent; |
---|
[51587aa] | 940 | |
---|
| 941 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 942 | output << " while (" ; |
---|
[7f5566b] | 943 | whileStmt->get_condition()->accept( *this ); |
---|
[6c4ff37] | 944 | output << ");"; |
---|
[51587aa] | 945 | } // if |
---|
| 946 | } |
---|
| 947 | |
---|
[8688ce1] | 948 | void CodeGenerator::visit( ForStmt * forStmt ) { |
---|
[8e9cbb2] | 949 | // initialization is always hoisted, so don't bother doing anything with that |
---|
[145f1fc] | 950 | output << "for (;"; |
---|
[51587aa] | 951 | |
---|
[321a2481] | 952 | if ( forStmt->get_condition() != 0 ) { |
---|
[51587aa] | 953 | forStmt->get_condition()->accept( *this ); |
---|
[3778cb2] | 954 | } // if |
---|
[6c4ff37] | 955 | output << ";"; |
---|
[51587aa] | 956 | |
---|
[321a2481] | 957 | if ( forStmt->get_increment() != 0 ) { |
---|
| 958 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 959 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
| 960 | expr->accept( *this ); |
---|
[3778cb2] | 961 | } // if |
---|
[2b6c1e0] | 962 | output << ") "; |
---|
[51587aa] | 963 | |
---|
| 964 | if ( forStmt->get_body() != 0 ) { |
---|
[2b6c1e0] | 965 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
[51587aa] | 966 | forStmt->get_body()->accept( *this ); |
---|
| 967 | } // if |
---|
| 968 | } |
---|
| 969 | |
---|
[7e003011] | 970 | void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) { |
---|
[cda48b6] | 971 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
[6c4ff37] | 972 | output << "/* null statement */ ;"; |
---|
[51587aa] | 973 | } |
---|
| 974 | |
---|
[8688ce1] | 975 | void CodeGenerator::visit( DeclStmt * declStmt ) { |
---|
[51587aa] | 976 | declStmt->get_decl()->accept( *this ); |
---|
[71f4e4f] | 977 | |
---|
[51587aa] | 978 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
[6c4ff37] | 979 | output << ";"; |
---|
[51587aa] | 980 | } // if |
---|
| 981 | } |
---|
| 982 | |
---|
[dd020c0] | 983 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) { |
---|
[fb04321] | 984 | if ( decl->get_storageClasses().any() ) { |
---|
[6e8bd43] | 985 | decl->get_storageClasses().print( output ); |
---|
[a7c90d4] | 986 | } // if |
---|
[dd020c0] | 987 | } // CodeGenerator::handleStorageClass |
---|
[9facf3b] | 988 | |
---|
| 989 | std::string genName( DeclarationWithType * decl ) { |
---|
| 990 | CodeGen::OperatorInfo opInfo; |
---|
| 991 | if ( operatorLookup( decl->get_name(), opInfo ) ) { |
---|
| 992 | return opInfo.outputName; |
---|
| 993 | } else { |
---|
| 994 | return decl->get_name(); |
---|
| 995 | } // if |
---|
| 996 | } |
---|
[51b7345] | 997 | } // namespace CodeGen |
---|
[51587aa] | 998 | |
---|
| 999 | // Local Variables: // |
---|
| 1000 | // tab-width: 4 // |
---|
| 1001 | // mode: c++ // |
---|
| 1002 | // compile-command: "make install" // |
---|
| 1003 | // End: // |
---|