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