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