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