[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
|
---|
[6180274] | 12 | // Last Modified On : Wed Feb 2 20:30:30 2022
|
---|
| 13 | // Update Count : 541
|
---|
[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 ) ||
|
---|
[3b0bc16] | 44 | dynamic_cast< WhileDoStmt * >( 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 );
|
---|
[786c438] | 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 << " = ";
|
---|
[f135b50] | 293 | // In progress
|
---|
[9857e8d] | 294 | obj->get_init()->accept( *visitor );
|
---|
[51587aa] | 295 | } // if
|
---|
[6c4ff37] | 296 | output << "," << endl;
|
---|
[51587aa] | 297 | } // for
|
---|
| 298 |
|
---|
[f7cb0bc] | 299 | --indent;
|
---|
[51587aa] | 300 |
|
---|
[cda48b6] | 301 | output << indent << "}";
|
---|
[51587aa] | 302 | } // if
|
---|
| 303 | }
|
---|
[71f4e4f] | 304 |
|
---|
[9857e8d] | 305 | void CodeGenerator::postvisit( TraitDecl * traitDecl ) {
|
---|
[42a36d9] | 306 | assertf( ! options.genC, "TraitDecls should not reach code generation." );
|
---|
[a984e65] | 307 | extension( traitDecl );
|
---|
| 308 | handleAggregate( traitDecl, "trait " );
|
---|
| 309 | }
|
---|
[71f4e4f] | 310 |
|
---|
[9857e8d] | 311 | void CodeGenerator::postvisit( TypedefDecl * typeDecl ) {
|
---|
[42a36d9] | 312 | assertf( ! options.genC, "Typedefs are removed and substituted in earlier passes." );
|
---|
[e39241b] | 313 | output << "typedef ";
|
---|
[42a36d9] | 314 | output << genType( typeDecl->get_base(), typeDecl->get_name(), options ) << endl;
|
---|
[51587aa] | 315 | }
|
---|
[71f4e4f] | 316 |
|
---|
[9857e8d] | 317 | void CodeGenerator::postvisit( TypeDecl * typeDecl ) {
|
---|
[42a36d9] | 318 | assertf( ! options.genC, "TypeDecls should not reach code generation." );
|
---|
[ab4bff5] | 319 | output << typeDecl->genTypeString() << " " << typeDecl->name;
|
---|
[f0ecf9b] | 320 | if ( typeDecl->sized ) {
|
---|
[ab4bff5] | 321 | output << " | sized(" << typeDecl->name << ")";
|
---|
[a0c7dc36] | 322 | }
|
---|
[ab4bff5] | 323 | if ( ! typeDecl->assertions.empty() ) {
|
---|
[a0c7dc36] | 324 | output << " | { ";
|
---|
[ab4bff5] | 325 | for ( DeclarationWithType * assert : typeDecl->assertions ) {
|
---|
| 326 | assert->accept( *visitor );
|
---|
| 327 | output << "; ";
|
---|
| 328 | }
|
---|
[a0c7dc36] | 329 | output << " }";
|
---|
[e39241b] | 330 | }
|
---|
[51587aa] | 331 | }
|
---|
| 332 |
|
---|
[92fea32] | 333 | void CodeGenerator::postvisit( StaticAssertDecl * assertDecl ) {
|
---|
| 334 | output << "_Static_assert(";
|
---|
| 335 | assertDecl->condition->accept( *visitor );
|
---|
| 336 | output << ", ";
|
---|
| 337 | assertDecl->message->accept( *visitor );
|
---|
| 338 | output << ")";
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[9857e8d] | 341 | void CodeGenerator::postvisit( Designation * designation ) {
|
---|
[e4d829b] | 342 | std::list< Expression * > designators = designation->get_designators();
|
---|
[e45215c] | 343 | if ( designators.size() == 0 ) return;
|
---|
[e4d829b] | 344 | for ( Expression * des : designators ) {
|
---|
[62423350] | 345 | if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
|
---|
| 346 | // if expression is a NameExpr or VariableExpr, then initializing aggregate member
|
---|
| 347 | output << ".";
|
---|
[9857e8d] | 348 | des->accept( *visitor );
|
---|
[e4d829b] | 349 | } else {
|
---|
[3e54399] | 350 | // otherwise, it has to be a ConstantExpr or CastExpr, initializing array element
|
---|
[62423350] | 351 | output << "[";
|
---|
[9857e8d] | 352 | des->accept( *visitor );
|
---|
[62423350] | 353 | output << "]";
|
---|
[3778cb2] | 354 | } // if
|
---|
| 355 | } // for
|
---|
[e45215c] | 356 | output << " = ";
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[9857e8d] | 359 | void CodeGenerator::postvisit( SingleInit * init ) {
|
---|
| 360 | init->get_value()->accept( *visitor );
|
---|
[51587aa] | 361 | }
|
---|
| 362 |
|
---|
[9857e8d] | 363 | void CodeGenerator::postvisit( ListInit * init ) {
|
---|
[e4d829b] | 364 | auto initBegin = init->begin();
|
---|
| 365 | auto initEnd = init->end();
|
---|
| 366 | auto desigBegin = init->get_designations().begin();
|
---|
| 367 | auto desigEnd = init->get_designations().end();
|
---|
| 368 |
|
---|
[6c4ff37] | 369 | output << "{ ";
|
---|
[e4d829b] | 370 | for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
|
---|
[9857e8d] | 371 | (*desigBegin)->accept( *visitor );
|
---|
| 372 | (*initBegin)->accept( *visitor );
|
---|
[e4d829b] | 373 | ++initBegin, ++desigBegin;
|
---|
| 374 | if ( initBegin != initEnd ) {
|
---|
| 375 | output << ", ";
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
[6c4ff37] | 378 | output << " }";
|
---|
[e4d829b] | 379 | assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
|
---|
[51587aa] | 380 | }
|
---|
| 381 |
|
---|
[d22e90f] | 382 | void CodeGenerator::postvisit( ConstructorInit * init ){
|
---|
[42a36d9] | 383 | assertf( ! options.genC, "ConstructorInit nodes should not reach code generation." );
|
---|
[e6cee92] | 384 | // pseudo-output for constructor/destructor pairs
|
---|
[d22e90f] | 385 | output << "<ctorinit>{" << endl << ++indent << "ctor: ";
|
---|
[9857e8d] | 386 | maybeAccept( init->get_ctor(), *visitor );
|
---|
[d22e90f] | 387 | output << ", " << endl << indent << "dtor: ";
|
---|
[9857e8d] | 388 | maybeAccept( init->get_dtor(), *visitor );
|
---|
[d22e90f] | 389 | output << endl << --indent << "}";
|
---|
[fc638d2] | 390 | }
|
---|
| 391 |
|
---|
[9857e8d] | 392 | void CodeGenerator::postvisit( Constant * constant ) {
|
---|
[d7312ac] | 393 | output << constant->get_value();
|
---|
[51587aa] | 394 | }
|
---|
| 395 |
|
---|
[4810867] | 396 | // *** Expressions
|
---|
[9857e8d] | 397 | void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) {
|
---|
[e04ef3a] | 398 | extension( applicationExpr );
|
---|
[8688ce1] | 399 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
|
---|
[60a8062] | 400 | const OperatorInfo * opInfo;
|
---|
| 401 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && ( opInfo = operatorLookup( varExpr->get_var()->get_name() ) ) ) {
|
---|
[51587aa] | 402 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
|
---|
[60a8062] | 403 | switch ( opInfo->type ) {
|
---|
[51587aa] | 404 | case OT_INDEX:
|
---|
| 405 | assert( applicationExpr->get_args().size() == 2 );
|
---|
[9857e8d] | 406 | (*arg++)->accept( *visitor );
|
---|
[6c4ff37] | 407 | output << "[";
|
---|
[9857e8d] | 408 | (*arg)->accept( *visitor );
|
---|
[6c4ff37] | 409 | output << "]";
|
---|
[51587aa] | 410 | break;
|
---|
[71f4e4f] | 411 |
|
---|
[51587aa] | 412 | case OT_CALL:
|
---|
[356189a] | 413 | // there are no intrinsic definitions of the function call operator
|
---|
[51587aa] | 414 | assert( false );
|
---|
| 415 | break;
|
---|
[71f4e4f] | 416 |
|
---|
[f1e012b] | 417 | case OT_CTOR:
|
---|
[c2ce2350] | 418 | case OT_DTOR:
|
---|
[356189a] | 419 | if ( applicationExpr->get_args().size() == 1 ) {
|
---|
[8e9cbb2] | 420 | // the expression fed into a single parameter constructor or destructor may contain side
|
---|
| 421 | // effects, so must still output this expression
|
---|
[64071c2] | 422 | output << "(";
|
---|
[9857e8d] | 423 | (*arg++)->accept( *visitor );
|
---|
[60a8062] | 424 | output << ") /* " << opInfo->inputName << " */";
|
---|
[356189a] | 425 | } else if ( applicationExpr->get_args().size() == 2 ) {
|
---|
[c2ce2350] | 426 | // intrinsic two parameter constructors are essentially bitwise assignment
|
---|
[356189a] | 427 | output << "(";
|
---|
[9857e8d] | 428 | (*arg++)->accept( *visitor );
|
---|
[60a8062] | 429 | output << opInfo->symbol;
|
---|
[9857e8d] | 430 | (*arg)->accept( *visitor );
|
---|
[60a8062] | 431 | output << ") /* " << opInfo->inputName << " */";
|
---|
[356189a] | 432 | } else {
|
---|
[c2ce2350] | 433 | // no constructors with 0 or more than 2 parameters
|
---|
[356189a] | 434 | assert( false );
|
---|
[8688ce1] | 435 | } // if
|
---|
[356189a] | 436 | break;
|
---|
[f1e012b] | 437 |
|
---|
[51587aa] | 438 | case OT_PREFIX:
|
---|
| 439 | case OT_PREFIXASSIGN:
|
---|
| 440 | assert( applicationExpr->get_args().size() == 1 );
|
---|
[6c4ff37] | 441 | output << "(";
|
---|
[60a8062] | 442 | output << opInfo->symbol;
|
---|
[9857e8d] | 443 | (*arg)->accept( *visitor );
|
---|
[6c4ff37] | 444 | output << ")";
|
---|
[51587aa] | 445 | break;
|
---|
[71f4e4f] | 446 |
|
---|
[51587aa] | 447 | case OT_POSTFIX:
|
---|
| 448 | case OT_POSTFIXASSIGN:
|
---|
| 449 | assert( applicationExpr->get_args().size() == 1 );
|
---|
[9857e8d] | 450 | (*arg)->accept( *visitor );
|
---|
[60a8062] | 451 | output << opInfo->symbol;
|
---|
[51587aa] | 452 | break;
|
---|
| 453 |
|
---|
[f1e012b] | 454 |
|
---|
[51587aa] | 455 | case OT_INFIX:
|
---|
| 456 | case OT_INFIXASSIGN:
|
---|
| 457 | assert( applicationExpr->get_args().size() == 2 );
|
---|
[6c4ff37] | 458 | output << "(";
|
---|
[9857e8d] | 459 | (*arg++)->accept( *visitor );
|
---|
[60a8062] | 460 | output << opInfo->symbol;
|
---|
[9857e8d] | 461 | (*arg)->accept( *visitor );
|
---|
[6c4ff37] | 462 | output << ")";
|
---|
[51587aa] | 463 | break;
|
---|
[71f4e4f] | 464 |
|
---|
[51587aa] | 465 | case OT_CONSTANT:
|
---|
[721f17a] | 466 | case OT_LABELADDRESS:
|
---|
| 467 | // there are no intrinsic definitions of 0/1 or label addresses as functions
|
---|
[51587aa] | 468 | assert( false );
|
---|
[3778cb2] | 469 | } // switch
|
---|
[17cd4eb] | 470 | } else {
|
---|
[9857e8d] | 471 | varExpr->accept( *visitor );
|
---|
[6c4ff37] | 472 | output << "(";
|
---|
[51587aa] | 473 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 474 | output << ")";
|
---|
[17cd4eb] | 475 | } // if
|
---|
[51587aa] | 476 | } else {
|
---|
[9857e8d] | 477 | applicationExpr->get_function()->accept( *visitor );
|
---|
[6c4ff37] | 478 | output << "(";
|
---|
[51587aa] | 479 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 480 | output << ")";
|
---|
[51587aa] | 481 | } // if
|
---|
| 482 | }
|
---|
[71f4e4f] | 483 |
|
---|
[9857e8d] | 484 | void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) {
|
---|
[e04ef3a] | 485 | extension( untypedExpr );
|
---|
[22bc276] | 486 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) {
|
---|
[60a8062] | 487 | const OperatorInfo * opInfo = operatorLookup( nameExpr->name );
|
---|
| 488 | if ( opInfo ) {
|
---|
[22bc276] | 489 | std::list< Expression* >::iterator arg = untypedExpr->args.begin();
|
---|
[60a8062] | 490 | switch ( opInfo->type ) {
|
---|
[51587aa] | 491 | case OT_INDEX:
|
---|
[22bc276] | 492 | assert( untypedExpr->args.size() == 2 );
|
---|
[9857e8d] | 493 | (*arg++)->accept( *visitor );
|
---|
[6c4ff37] | 494 | output << "[";
|
---|
[9857e8d] | 495 | (*arg)->accept( *visitor );
|
---|
[6c4ff37] | 496 | output << "]";
|
---|
[51587aa] | 497 | break;
|
---|
[71f4e4f] | 498 |
|
---|
[51587aa] | 499 | case OT_CALL:
|
---|
[f1e012b] | 500 | assert( false );
|
---|
| 501 |
|
---|
[c2ce2350] | 502 | case OT_CTOR:
|
---|
| 503 | case OT_DTOR:
|
---|
[22bc276] | 504 | if ( untypedExpr->args.size() == 1 ) {
|
---|
[8e9cbb2] | 505 | // the expression fed into a single parameter constructor or destructor may contain side
|
---|
| 506 | // effects, so must still output this expression
|
---|
[64071c2] | 507 | output << "(";
|
---|
[9857e8d] | 508 | (*arg++)->accept( *visitor );
|
---|
[60a8062] | 509 | output << ") /* " << opInfo->inputName << " */";
|
---|
[c2ce2350] | 510 | } else if ( untypedExpr->get_args().size() == 2 ) {
|
---|
| 511 | // intrinsic two parameter constructors are essentially bitwise assignment
|
---|
| 512 | output << "(";
|
---|
[9857e8d] | 513 | (*arg++)->accept( *visitor );
|
---|
[60a8062] | 514 | output << opInfo->symbol;
|
---|
[9857e8d] | 515 | (*arg)->accept( *visitor );
|
---|
[60a8062] | 516 | output << ") /* " << opInfo->inputName << " */";
|
---|
[c2ce2350] | 517 | } else {
|
---|
| 518 | // no constructors with 0 or more than 2 parameters
|
---|
[42a36d9] | 519 | assertf( ! options.genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." );
|
---|
[4e8949f] | 520 | output << "(";
|
---|
| 521 | (*arg++)->accept( *visitor );
|
---|
[60a8062] | 522 | output << opInfo->symbol << "{ ";
|
---|
[22bc276] | 523 | genCommaList( arg, untypedExpr->args.end() );
|
---|
[60a8062] | 524 | output << "}) /* " << opInfo->inputName << " */";
|
---|
[3778cb2] | 525 | } // if
|
---|
[51587aa] | 526 | break;
|
---|
[71f4e4f] | 527 |
|
---|
[51587aa] | 528 | case OT_PREFIX:
|
---|
| 529 | case OT_PREFIXASSIGN:
|
---|
[de62360d] | 530 | case OT_LABELADDRESS:
|
---|
[22bc276] | 531 | assert( untypedExpr->args.size() == 1 );
|
---|
[6c4ff37] | 532 | output << "(";
|
---|
[60a8062] | 533 | output << opInfo->symbol;
|
---|
[9857e8d] | 534 | (*arg)->accept( *visitor );
|
---|
[6c4ff37] | 535 | output << ")";
|
---|
[51587aa] | 536 | break;
|
---|
[71f4e4f] | 537 |
|
---|
[51587aa] | 538 | case OT_POSTFIX:
|
---|
| 539 | case OT_POSTFIXASSIGN:
|
---|
[22bc276] | 540 | assert( untypedExpr->args.size() == 1 );
|
---|
[9857e8d] | 541 | (*arg)->accept( *visitor );
|
---|
[60a8062] | 542 | output << opInfo->symbol;
|
---|
[51587aa] | 543 | break;
|
---|
[71f4e4f] | 544 |
|
---|
[51587aa] | 545 | case OT_INFIX:
|
---|
| 546 | case OT_INFIXASSIGN:
|
---|
[22bc276] | 547 | assert( untypedExpr->args.size() == 2 );
|
---|
[6c4ff37] | 548 | output << "(";
|
---|
[9857e8d] | 549 | (*arg++)->accept( *visitor );
|
---|
[60a8062] | 550 | output << opInfo->symbol;
|
---|
[9857e8d] | 551 | (*arg)->accept( *visitor );
|
---|
[6c4ff37] | 552 | output << ")";
|
---|
[51587aa] | 553 | break;
|
---|
[71f4e4f] | 554 |
|
---|
[51587aa] | 555 | case OT_CONSTANT:
|
---|
| 556 | // there are no intrinsic definitions of 0 or 1 as functions
|
---|
| 557 | assert( false );
|
---|
[3778cb2] | 558 | } // switch
|
---|
[51587aa] | 559 | } else {
|
---|
[22bc276] | 560 | // builtin routines
|
---|
| 561 | nameExpr->accept( *visitor );
|
---|
| 562 | output << "(";
|
---|
| 563 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
|
---|
| 564 | output << ")";
|
---|
[51587aa] | 565 | } // if
|
---|
| 566 | } else {
|
---|
[22bc276] | 567 | untypedExpr->function->accept( *visitor );
|
---|
[6c4ff37] | 568 | output << "(";
|
---|
[22bc276] | 569 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
|
---|
[6c4ff37] | 570 | output << ")";
|
---|
[51587aa] | 571 | } // if
|
---|
| 572 | }
|
---|
[71f4e4f] | 573 |
|
---|
[9857e8d] | 574 | void CodeGenerator::postvisit( RangeExpr * rangeExpr ) {
|
---|
[22bc276] | 575 | rangeExpr->low->accept( *visitor );
|
---|
[064e3ff] | 576 | output << " ... ";
|
---|
[22bc276] | 577 | rangeExpr->high->accept( *visitor );
|
---|
[064e3ff] | 578 | }
|
---|
| 579 |
|
---|
[9857e8d] | 580 | void CodeGenerator::postvisit( NameExpr * nameExpr ) {
|
---|
[e04ef3a] | 581 | extension( nameExpr );
|
---|
[60a8062] | 582 | const OperatorInfo * opInfo = operatorLookup( nameExpr->name );
|
---|
| 583 | if ( opInfo ) {
|
---|
| 584 | if ( opInfo->type == OT_CONSTANT ) {
|
---|
| 585 | output << opInfo->symbol;
|
---|
[5ea7a22] | 586 | } else {
|
---|
[60a8062] | 587 | output << opInfo->outputName;
|
---|
[5ea7a22] | 588 | }
|
---|
[51587aa] | 589 | } else {
|
---|
[6c4ff37] | 590 | output << nameExpr->get_name();
|
---|
[51587aa] | 591 | } // if
|
---|
| 592 | }
|
---|
[71f4e4f] | 593 |
|
---|
[6e50a6b] | 594 | void CodeGenerator::postvisit( DimensionExpr * dimensionExpr ) {
|
---|
| 595 | extension( dimensionExpr );
|
---|
| 596 | output << "/*non-type*/" << dimensionExpr->get_name();
|
---|
| 597 | }
|
---|
| 598 |
|
---|
[9857e8d] | 599 | void CodeGenerator::postvisit( AddressExpr * addressExpr ) {
|
---|
[e04ef3a] | 600 | extension( addressExpr );
|
---|
[6c4ff37] | 601 | output << "(&";
|
---|
[9857e8d] | 602 | addressExpr->arg->accept( *visitor );
|
---|
[6c4ff37] | 603 | output << ")";
|
---|
[51587aa] | 604 | }
|
---|
| 605 |
|
---|
[9857e8d] | 606 | void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) {
|
---|
[5809461] | 607 | extension( addressExpr );
|
---|
| 608 | output << "(&&" << addressExpr->arg << ")";
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[9857e8d] | 611 | void CodeGenerator::postvisit( CastExpr * castExpr ) {
|
---|
[e04ef3a] | 612 | extension( castExpr );
|
---|
[803deb1] | 613 | output << "(";
|
---|
[906e24d] | 614 | if ( castExpr->get_result()->isVoid() ) {
|
---|
[d7312ac] | 615 | output << "(void)";
|
---|
[d104b02] | 616 | } else {
|
---|
| 617 | // at least one result type of cast.
|
---|
| 618 | // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write
|
---|
| 619 | // an lvalue cast, this has been taken out.
|
---|
[803deb1] | 620 | output << "(";
|
---|
[42a36d9] | 621 | output << genType( castExpr->get_result(), "", options );
|
---|
[71f4e4f] | 622 | output << ")";
|
---|
[3778cb2] | 623 | } // if
|
---|
[da9d79b] | 624 | castExpr->arg->accept( *visitor );
|
---|
| 625 | output << ")";
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | void CodeGenerator::postvisit( KeywordCastExpr * castExpr ) {
|
---|
[42a36d9] | 629 | assertf( ! options.genC, "KeywordCast should not reach code generation." );
|
---|
[da9d79b] | 630 | extension( castExpr );
|
---|
| 631 | output << "((" << castExpr->targetString() << " &)";
|
---|
| 632 | castExpr->arg->accept( *visitor );
|
---|
[803deb1] | 633 | output << ")";
|
---|
[51587aa] | 634 | }
|
---|
[71f4e4f] | 635 |
|
---|
[9857e8d] | 636 | void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) {
|
---|
[42a36d9] | 637 | assertf( ! options.genC, "VirtualCastExpr should not reach code generation." );
|
---|
[a5f0529] | 638 | extension( castExpr );
|
---|
| 639 | output << "(virtual ";
|
---|
[9857e8d] | 640 | castExpr->get_arg()->accept( *visitor );
|
---|
[a5f0529] | 641 | output << ")";
|
---|
| 642 | }
|
---|
| 643 |
|
---|
[9857e8d] | 644 | void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) {
|
---|
[42a36d9] | 645 | assertf( ! options.genC, "UntypedMemberExpr should not reach code generation." );
|
---|
[e39241b] | 646 | extension( memberExpr );
|
---|
[9857e8d] | 647 | memberExpr->get_aggregate()->accept( *visitor );
|
---|
[5f642e38] | 648 | output << ".";
|
---|
[9857e8d] | 649 | memberExpr->get_member()->accept( *visitor );
|
---|
[51587aa] | 650 | }
|
---|
[71f4e4f] | 651 |
|
---|
[9857e8d] | 652 | void CodeGenerator::postvisit( MemberExpr * memberExpr ) {
|
---|
[e04ef3a] | 653 | extension( memberExpr );
|
---|
[9857e8d] | 654 | memberExpr->get_aggregate()->accept( *visitor );
|
---|
[6c4ff37] | 655 | output << "." << mangleName( memberExpr->get_member() );
|
---|
[51587aa] | 656 | }
|
---|
[71f4e4f] | 657 |
|
---|
[9857e8d] | 658 | void CodeGenerator::postvisit( VariableExpr * variableExpr ) {
|
---|
[e04ef3a] | 659 | extension( variableExpr );
|
---|
[60a8062] | 660 | const OperatorInfo * opInfo;
|
---|
| 661 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && (opInfo = operatorLookup( variableExpr->get_var()->get_name() )) && opInfo->type == OT_CONSTANT ) {
|
---|
| 662 | output << opInfo->symbol;
|
---|
[51587aa] | 663 | } else {
|
---|
[3e54399] | 664 | if (dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type())
|
---|
| 665 | && dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type())->baseEnum->base) {
|
---|
| 666 | output << '(' <<genType(dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type())->baseEnum->base, "", options) << ')';
|
---|
| 667 | }
|
---|
[6c4ff37] | 668 | output << mangleName( variableExpr->get_var() );
|
---|
[51587aa] | 669 | } // if
|
---|
| 670 | }
|
---|
[71f4e4f] | 671 |
|
---|
[9857e8d] | 672 | void CodeGenerator::postvisit( ConstantExpr * constantExpr ) {
|
---|
[51587aa] | 673 | assert( constantExpr->get_constant() );
|
---|
[e04ef3a] | 674 | extension( constantExpr );
|
---|
[9857e8d] | 675 | constantExpr->get_constant()->accept( *visitor );
|
---|
[51587aa] | 676 | }
|
---|
[71f4e4f] | 677 |
|
---|
[9857e8d] | 678 | void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) {
|
---|
[e04ef3a] | 679 | extension( sizeofExpr );
|
---|
[6c4ff37] | 680 | output << "sizeof(";
|
---|
[51587aa] | 681 | if ( sizeofExpr->get_isType() ) {
|
---|
[42a36d9] | 682 | output << genType( sizeofExpr->get_type(), "", options );
|
---|
[51587aa] | 683 | } else {
|
---|
[9857e8d] | 684 | sizeofExpr->get_expr()->accept( *visitor );
|
---|
[51587aa] | 685 | } // if
|
---|
[6c4ff37] | 686 | output << ")";
|
---|
[51587aa] | 687 | }
|
---|
[47534159] | 688 |
|
---|
[9857e8d] | 689 | void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) {
|
---|
[47534159] | 690 | // use GCC extension to avoid bumping std to C11
|
---|
[8e9cbb2] | 691 | extension( alignofExpr );
|
---|
[47534159] | 692 | output << "__alignof__(";
|
---|
[25a054f] | 693 | if ( alignofExpr->get_isType() ) {
|
---|
[42a36d9] | 694 | output << genType( alignofExpr->get_type(), "", options );
|
---|
[47534159] | 695 | } else {
|
---|
[9857e8d] | 696 | alignofExpr->get_expr()->accept( *visitor );
|
---|
[47534159] | 697 | } // if
|
---|
| 698 | output << ")";
|
---|
| 699 | }
|
---|
[71f4e4f] | 700 |
|
---|
[9857e8d] | 701 | void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) {
|
---|
[42a36d9] | 702 | assertf( ! options.genC, "UntypedOffsetofExpr should not reach code generation." );
|
---|
[e39241b] | 703 | output << "offsetof(";
|
---|
[42a36d9] | 704 | output << genType( offsetofExpr->get_type(), "", options );
|
---|
[e39241b] | 705 | output << ", " << offsetofExpr->get_member();
|
---|
| 706 | output << ")";
|
---|
[2a4b088] | 707 | }
|
---|
| 708 |
|
---|
[9857e8d] | 709 | void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) {
|
---|
[25a054f] | 710 | // use GCC builtin
|
---|
| 711 | output << "__builtin_offsetof(";
|
---|
[42a36d9] | 712 | output << genType( offsetofExpr->get_type(), "", options );
|
---|
[e551c69] | 713 | output << ", " << mangleName( offsetofExpr->get_member() );
|
---|
[25a054f] | 714 | output << ")";
|
---|
| 715 | }
|
---|
[d63eeb0] | 716 |
|
---|
[9857e8d] | 717 | void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) {
|
---|
[42a36d9] | 718 | assertf( ! options.genC, "OffsetPackExpr should not reach code generation." );
|
---|
| 719 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", options ) << ")";
|
---|
[afc1045] | 720 | }
|
---|
[70a06f6] | 721 |
|
---|
[9857e8d] | 722 | void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) {
|
---|
[e04ef3a] | 723 | extension( logicalExpr );
|
---|
[6c4ff37] | 724 | output << "(";
|
---|
[9857e8d] | 725 | logicalExpr->get_arg1()->accept( *visitor );
|
---|
[51587aa] | 726 | if ( logicalExpr->get_isAnd() ) {
|
---|
[6c4ff37] | 727 | output << " && ";
|
---|
[51587aa] | 728 | } else {
|
---|
[6c4ff37] | 729 | output << " || ";
|
---|
[51587aa] | 730 | } // if
|
---|
[9857e8d] | 731 | logicalExpr->get_arg2()->accept( *visitor );
|
---|
[6c4ff37] | 732 | output << ")";
|
---|
[51587aa] | 733 | }
|
---|
[71f4e4f] | 734 |
|
---|
[9857e8d] | 735 | void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) {
|
---|
[e04ef3a] | 736 | extension( conditionalExpr );
|
---|
[6c4ff37] | 737 | output << "(";
|
---|
[9857e8d] | 738 | conditionalExpr->get_arg1()->accept( *visitor );
|
---|
[6c4ff37] | 739 | output << " ? ";
|
---|
[9857e8d] | 740 | conditionalExpr->get_arg2()->accept( *visitor );
|
---|
[6c4ff37] | 741 | output << " : ";
|
---|
[9857e8d] | 742 | conditionalExpr->get_arg3()->accept( *visitor );
|
---|
[6c4ff37] | 743 | output << ")";
|
---|
[51587aa] | 744 | }
|
---|
[71f4e4f] | 745 |
|
---|
[9857e8d] | 746 | void CodeGenerator::postvisit( CommaExpr * commaExpr ) {
|
---|
[e04ef3a] | 747 | extension( commaExpr );
|
---|
[6c4ff37] | 748 | output << "(";
|
---|
[42a36d9] | 749 | if ( options.genC ) {
|
---|
[8a6cf7e] | 750 | // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
|
---|
| 751 | commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
|
---|
| 752 | }
|
---|
[9857e8d] | 753 | commaExpr->get_arg1()->accept( *visitor );
|
---|
[6c4ff37] | 754 | output << " , ";
|
---|
[9857e8d] | 755 | commaExpr->get_arg2()->accept( *visitor );
|
---|
[6c4ff37] | 756 | output << ")";
|
---|
[51587aa] | 757 | }
|
---|
[71f4e4f] | 758 |
|
---|
[9857e8d] | 759 | void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) {
|
---|
[42a36d9] | 760 | assertf( ! options.genC, "TupleAssignExpr should not reach code generation." );
|
---|
[9857e8d] | 761 | tupleExpr->stmtExpr->accept( *visitor );
|
---|
[d104b02] | 762 | }
|
---|
| 763 |
|
---|
[9857e8d] | 764 | void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) {
|
---|
[42a36d9] | 765 | assertf( ! options.genC, "UntypedTupleExpr should not reach code generation." );
|
---|
[f975c65] | 766 | extension( tupleExpr );
|
---|
[e39241b] | 767 | output << "[";
|
---|
| 768 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
|
---|
| 769 | output << "]";
|
---|
| 770 | }
|
---|
[907eccb] | 771 |
|
---|
[9857e8d] | 772 | void CodeGenerator::postvisit( TupleExpr * tupleExpr ) {
|
---|
[42a36d9] | 773 | assertf( ! options.genC, "TupleExpr should not reach code generation." );
|
---|
[f975c65] | 774 | extension( tupleExpr );
|
---|
[e39241b] | 775 | output << "[";
|
---|
| 776 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
|
---|
| 777 | output << "]";
|
---|
| 778 | }
|
---|
[71f4e4f] | 779 |
|
---|
[9857e8d] | 780 | void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) {
|
---|
[42a36d9] | 781 | assertf( ! options.genC, "TupleIndexExpr should not reach code generation." );
|
---|
[f975c65] | 782 | extension( tupleExpr );
|
---|
[9857e8d] | 783 | tupleExpr->get_tuple()->accept( *visitor );
|
---|
[f975c65] | 784 | output << "." << tupleExpr->get_index();
|
---|
| 785 | }
|
---|
| 786 |
|
---|
[9857e8d] | 787 | void CodeGenerator::postvisit( TypeExpr * typeExpr ) {
|
---|
[42a36d9] | 788 | // if ( options.genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
|
---|
| 789 | // assertf( ! options.genC, "TypeExpr should not reach code generation." );
|
---|
| 790 | if ( ! options.genC ) {
|
---|
| 791 | output << genType( typeExpr->get_type(), "", options );
|
---|
[e4d829b] | 792 | }
|
---|
[e39241b] | 793 | }
|
---|
[2b6c1e0] | 794 |
|
---|
[9857e8d] | 795 | void CodeGenerator::postvisit( AsmExpr * asmExpr ) {
|
---|
[665f432] | 796 | if ( !asmExpr->inout.empty() ) {
|
---|
[7f5566b] | 797 | output << "[ ";
|
---|
[665f432] | 798 | output << asmExpr->inout;
|
---|
[7f5566b] | 799 | output << " ] ";
|
---|
| 800 | } // if
|
---|
[665f432] | 801 | asmExpr->constraint->accept( *visitor );
|
---|
[7f5566b] | 802 | output << " ( ";
|
---|
[665f432] | 803 | asmExpr->operand->accept( *visitor );
|
---|
[7f5566b] | 804 | output << " )";
|
---|
| 805 | }
|
---|
| 806 |
|
---|
[9857e8d] | 807 | void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) {
|
---|
[fbcde64] | 808 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
|
---|
[42a36d9] | 809 | output << "(" << genType( compLitExpr->get_result(), "", options ) << ")";
|
---|
[9857e8d] | 810 | compLitExpr->get_initializer()->accept( *visitor );
|
---|
[3c13c03] | 811 | }
|
---|
| 812 |
|
---|
[9857e8d] | 813 | void CodeGenerator::postvisit( UniqueExpr * unqExpr ) {
|
---|
[42a36d9] | 814 | assertf( ! options.genC, "Unique expressions should not reach code generation." );
|
---|
[d104b02] | 815 | output << "unq<" << unqExpr->get_id() << ">{ ";
|
---|
[9857e8d] | 816 | unqExpr->get_expr()->accept( *visitor );
|
---|
[d104b02] | 817 | output << " }";
|
---|
| 818 | }
|
---|
| 819 |
|
---|
[9857e8d] | 820 | void CodeGenerator::postvisit( StmtExpr * stmtExpr ) {
|
---|
[44b4114] | 821 | std::list< Statement * > & stmts = stmtExpr->statements->kids;
|
---|
[d22e90f] | 822 | output << "({" << endl;
|
---|
[f7cb0bc] | 823 | ++indent;
|
---|
[3c13c03] | 824 | unsigned int numStmts = stmts.size();
|
---|
| 825 | unsigned int i = 0;
|
---|
| 826 | for ( Statement * stmt : stmts ) {
|
---|
[058f549] | 827 | output << indent << printLabels( stmt->get_labels() );
|
---|
[3c13c03] | 828 | if ( i+1 == numStmts ) {
|
---|
| 829 | // last statement in a statement expression needs to be handled specially -
|
---|
| 830 | // cannot cast to void, otherwise the expression statement has no value
|
---|
| 831 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
|
---|
[44b4114] | 832 | exprStmt->expr->accept( *visitor );
|
---|
[3c13c03] | 833 | output << ";" << endl;
|
---|
| 834 | ++i;
|
---|
| 835 | break;
|
---|
| 836 | }
|
---|
| 837 | }
|
---|
[9857e8d] | 838 | stmt->accept( *visitor );
|
---|
[3c13c03] | 839 | output << endl;
|
---|
| 840 | if ( wantSpacing( stmt ) ) {
|
---|
| 841 | output << endl;
|
---|
| 842 | } // if
|
---|
| 843 | ++i;
|
---|
| 844 | }
|
---|
[f7cb0bc] | 845 | --indent;
|
---|
[3c13c03] | 846 | output << indent << "})";
|
---|
[6eb8948] | 847 | }
|
---|
| 848 |
|
---|
[4e8949f] | 849 | void CodeGenerator::postvisit( ConstructorExpr * expr ) {
|
---|
[42a36d9] | 850 | assertf( ! options.genC, "Unique expressions should not reach code generation." );
|
---|
[4e8949f] | 851 | expr->callExpr->accept( *visitor );
|
---|
| 852 | }
|
---|
| 853 |
|
---|
[44b4114] | 854 | void CodeGenerator::postvisit( DeletedExpr * expr ) {
|
---|
[42a36d9] | 855 | assertf( ! options.genC, "Deleted expressions should not reach code generation." );
|
---|
[44b4114] | 856 | expr->expr->accept( *visitor );
|
---|
| 857 | }
|
---|
| 858 |
|
---|
[0f79853] | 859 | void CodeGenerator::postvisit( DefaultArgExpr * arg ) {
|
---|
[42a36d9] | 860 | assertf( ! options.genC, "Default argument expressions should not reach code generation." );
|
---|
[0f79853] | 861 | arg->expr->accept( *visitor );
|
---|
| 862 | }
|
---|
| 863 |
|
---|
[d807ca28] | 864 | void CodeGenerator::postvisit( GenericExpr * expr ) {
|
---|
[42a36d9] | 865 | assertf( ! options.genC, "C11 _Generic expressions should not reach code generation." );
|
---|
[d807ca28] | 866 | output << "_Generic(";
|
---|
| 867 | expr->control->accept( *visitor );
|
---|
| 868 | output << ", ";
|
---|
| 869 | unsigned int numAssocs = expr->associations.size();
|
---|
| 870 | unsigned int i = 0;
|
---|
| 871 | for ( GenericExpr::Association & assoc : expr->associations ) {
|
---|
| 872 | if (assoc.isDefault) {
|
---|
| 873 | output << "default: ";
|
---|
| 874 | } else {
|
---|
[42a36d9] | 875 | output << genType( assoc.type, "", options ) << ": ";
|
---|
[d807ca28] | 876 | }
|
---|
| 877 | assoc.expr->accept( *visitor );
|
---|
| 878 | if ( i+1 != numAssocs ) {
|
---|
| 879 | output << ", ";
|
---|
| 880 | }
|
---|
| 881 | i++;
|
---|
| 882 | }
|
---|
| 883 | output << ")";
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 |
|
---|
[4810867] | 887 | // *** Statements
|
---|
[9857e8d] | 888 | void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) {
|
---|
[51587aa] | 889 | std::list<Statement*> ks = compoundStmt->get_kids();
|
---|
[2b6c1e0] | 890 | output << "{" << endl;
|
---|
[51587aa] | 891 |
|
---|
[f7cb0bc] | 892 | ++indent;
|
---|
[51587aa] | 893 |
|
---|
[7f5566b] | 894 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
|
---|
[cda48b6] | 895 | output << indent << printLabels( (*i)->get_labels() );
|
---|
[9857e8d] | 896 | (*i)->accept( *visitor );
|
---|
[2b6c1e0] | 897 |
|
---|
[6c4ff37] | 898 | output << endl;
|
---|
[2b6c1e0] | 899 | if ( wantSpacing( *i ) ) {
|
---|
| 900 | output << endl;
|
---|
[3778cb2] | 901 | } // if
|
---|
[8688ce1] | 902 | } // for
|
---|
[f7cb0bc] | 903 | --indent;
|
---|
[51587aa] | 904 |
|
---|
[cda48b6] | 905 | output << indent << "}";
|
---|
[51587aa] | 906 | }
|
---|
| 907 |
|
---|
[9857e8d] | 908 | void CodeGenerator::postvisit( ExprStmt * exprStmt ) {
|
---|
[6c4ff37] | 909 | assert( exprStmt );
|
---|
[42a36d9] | 910 | if ( options.genC ) {
|
---|
[262f085f] | 911 | // cast the top-level expression to void to reduce gcc warnings.
|
---|
[8a6cf7e] | 912 | exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
|
---|
[262f085f] | 913 | }
|
---|
[9857e8d] | 914 | exprStmt->get_expr()->accept( *visitor );
|
---|
[321a2481] | 915 | output << ";";
|
---|
[51587aa] | 916 | }
|
---|
| 917 |
|
---|
[9857e8d] | 918 | void CodeGenerator::postvisit( AsmStmt * asmStmt ) {
|
---|
[7f5566b] | 919 | output << "asm ";
|
---|
| 920 | if ( asmStmt->get_voltile() ) output << "volatile ";
|
---|
| 921 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
|
---|
| 922 | output << "( ";
|
---|
[9857e8d] | 923 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
|
---|
[7f5566b] | 924 | output << " : ";
|
---|
| 925 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
|
---|
| 926 | output << " : ";
|
---|
| 927 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
|
---|
| 928 | output << " : ";
|
---|
| 929 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
|
---|
| 930 | if ( ! asmStmt->get_gotolabels().empty() ) {
|
---|
| 931 | output << " : ";
|
---|
| 932 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
|
---|
| 933 | output << *begin++;
|
---|
| 934 | if ( begin == asmStmt->get_gotolabels().end() ) break;
|
---|
| 935 | output << ", ";
|
---|
| 936 | } // for
|
---|
| 937 | } // if
|
---|
[d7312ac] | 938 | output << " );";
|
---|
[7f5566b] | 939 | }
|
---|
| 940 |
|
---|
[9857e8d] | 941 | void CodeGenerator::postvisit( AsmDecl * asmDecl ) {
|
---|
[e994912] | 942 | output << "asm ";
|
---|
| 943 | AsmStmt * asmStmt = asmDecl->get_stmt();
|
---|
| 944 | output << "( ";
|
---|
[9857e8d] | 945 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
|
---|
[d7312ac] | 946 | output << " )";
|
---|
[e994912] | 947 | }
|
---|
| 948 |
|
---|
[2d019af] | 949 | void CodeGenerator::postvisit( DirectiveDecl * directiveDecl ) {
|
---|
| 950 | output << endl << directiveDecl->get_stmt()->directive; // endl prevents spaces before directive
|
---|
| 951 | }
|
---|
| 952 |
|
---|
[cc32d83] | 953 | void CodeGenerator::postvisit( DirectiveStmt * dirStmt ) {
|
---|
[d7312ac] | 954 | output << endl << dirStmt->directive; // endl prevents spaces before directive
|
---|
[cc32d83] | 955 | }
|
---|
| 956 |
|
---|
[9857e8d] | 957 | void CodeGenerator::postvisit( IfStmt * ifStmt ) {
|
---|
[7f5566b] | 958 | output << "if ( ";
|
---|
[9857e8d] | 959 | ifStmt->get_condition()->accept( *visitor );
|
---|
[7f5566b] | 960 | output << " ) ";
|
---|
[51587aa] | 961 |
|
---|
[3b0bc16] | 962 | ifStmt->get_then()->accept( *visitor );
|
---|
[51587aa] | 963 |
|
---|
[3b0bc16] | 964 | if ( ifStmt->get_else() != 0) {
|
---|
[2b6c1e0] | 965 | output << " else ";
|
---|
[3b0bc16] | 966 | ifStmt->get_else()->accept( *visitor );
|
---|
[51587aa] | 967 | } // if
|
---|
| 968 | }
|
---|
| 969 |
|
---|
[9857e8d] | 970 | void CodeGenerator::postvisit( SwitchStmt * switchStmt ) {
|
---|
[d7312ac] | 971 | output << "switch ( ";
|
---|
[9857e8d] | 972 | switchStmt->get_condition()->accept( *visitor );
|
---|
[7f5566b] | 973 | output << " ) ";
|
---|
[71f4e4f] | 974 |
|
---|
[d22e90f] | 975 | output << "{" << endl;
|
---|
[f7cb0bc] | 976 | ++indent;
|
---|
[9857e8d] | 977 | acceptAll( switchStmt->get_statements(), *visitor );
|
---|
[f7cb0bc] | 978 | --indent;
|
---|
[cda48b6] | 979 | output << indent << "}";
|
---|
[51587aa] | 980 | }
|
---|
| 981 |
|
---|
[9857e8d] | 982 | void CodeGenerator::postvisit( CaseStmt * caseStmt ) {
|
---|
[8bafacc] | 983 | updateLocation( caseStmt );
|
---|
[1dcd9554] | 984 | output << indent;
|
---|
[eb3261f] | 985 | if ( caseStmt->isDefault()) {
|
---|
[2b6c1e0] | 986 | output << "default";
|
---|
[eb3261f] | 987 | } else {
|
---|
[2b6c1e0] | 988 | output << "case ";
|
---|
[9857e8d] | 989 | caseStmt->get_condition()->accept( *visitor );
|
---|
[17cd4eb] | 990 | } // if
|
---|
[d22e90f] | 991 | output << ":" << endl;
|
---|
[71f4e4f] | 992 |
|
---|
[51587aa] | 993 | std::list<Statement *> sts = caseStmt->get_statements();
|
---|
| 994 |
|
---|
[f7cb0bc] | 995 | ++indent;
|
---|
[51587aa] | 996 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
|
---|
[d7312ac] | 997 | output << indent << printLabels( (*i)->get_labels() ) ;
|
---|
[9857e8d] | 998 | (*i)->accept( *visitor );
|
---|
[6c4ff37] | 999 | output << endl;
|
---|
[3778cb2] | 1000 | } // for
|
---|
[f7cb0bc] | 1001 | --indent;
|
---|
[51587aa] | 1002 | }
|
---|
| 1003 |
|
---|
[9857e8d] | 1004 | void CodeGenerator::postvisit( BranchStmt * branchStmt ) {
|
---|
[51587aa] | 1005 | switch ( branchStmt->get_type()) {
|
---|
| 1006 | case BranchStmt::Goto:
|
---|
| 1007 | if ( ! branchStmt->get_target().empty() )
|
---|
[6c4ff37] | 1008 | output << "goto " << branchStmt->get_target();
|
---|
[71f4e4f] | 1009 | else {
|
---|
[51587aa] | 1010 | if ( branchStmt->get_computedTarget() != 0 ) {
|
---|
[6c4ff37] | 1011 | output << "goto *";
|
---|
[9857e8d] | 1012 | branchStmt->get_computedTarget()->accept( *visitor );
|
---|
[51587aa] | 1013 | } // if
|
---|
| 1014 | } // if
|
---|
| 1015 | break;
|
---|
| 1016 | case BranchStmt::Break:
|
---|
[6c4ff37] | 1017 | output << "break";
|
---|
[51587aa] | 1018 | break;
|
---|
| 1019 | case BranchStmt::Continue:
|
---|
[6c4ff37] | 1020 | output << "continue";
|
---|
[51587aa] | 1021 | break;
|
---|
[7c2a7b6] | 1022 | case BranchStmt::FallThrough:
|
---|
| 1023 | case BranchStmt::FallThroughDefault:
|
---|
[42a36d9] | 1024 | assertf( ! options.genC, "fallthru should not reach code generation." );
|
---|
[60a8062] | 1025 | output << "fallthru";
|
---|
[7c2a7b6] | 1026 | break;
|
---|
[6180274] | 1027 | default: ; // prevent warning
|
---|
[3778cb2] | 1028 | } // switch
|
---|
[7c2a7b6] | 1029 | // print branch target for labelled break/continue/fallthru in debug mode
|
---|
[42a36d9] | 1030 | if ( ! options.genC && branchStmt->get_type() != BranchStmt::Goto ) {
|
---|
[7c2a7b6] | 1031 | if ( ! branchStmt->get_target().empty() ) {
|
---|
| 1032 | output << " " << branchStmt->get_target();
|
---|
| 1033 | } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) {
|
---|
| 1034 | output << " default";
|
---|
| 1035 | }
|
---|
| 1036 | }
|
---|
[2b6c1e0] | 1037 | output << ";";
|
---|
[51587aa] | 1038 | }
|
---|
| 1039 |
|
---|
[9857e8d] | 1040 | void CodeGenerator::postvisit( ReturnStmt * returnStmt ) {
|
---|
[6c4ff37] | 1041 | output << "return ";
|
---|
[9857e8d] | 1042 | maybeAccept( returnStmt->get_expr(), *visitor );
|
---|
[6c4ff37] | 1043 | output << ";";
|
---|
[51587aa] | 1044 | }
|
---|
| 1045 |
|
---|
[9857e8d] | 1046 | void CodeGenerator::postvisit( ThrowStmt * throwStmt ) {
|
---|
[42a36d9] | 1047 | assertf( ! options.genC, "Throw statements should not reach code generation." );
|
---|
[daf1af8] | 1048 |
|
---|
| 1049 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
|
---|
[60a8062] | 1050 | "throw" : "throwResume");
|
---|
[daf1af8] | 1051 | if (throwStmt->get_expr()) {
|
---|
| 1052 | output << " ";
|
---|
[9857e8d] | 1053 | throwStmt->get_expr()->accept( *visitor );
|
---|
[daf1af8] | 1054 | }
|
---|
| 1055 | if (throwStmt->get_target()) {
|
---|
| 1056 | output << " _At ";
|
---|
[9857e8d] | 1057 | throwStmt->get_target()->accept( *visitor );
|
---|
[daf1af8] | 1058 | }
|
---|
| 1059 | output << ";";
|
---|
| 1060 | }
|
---|
[e4ea10b7] | 1061 | void CodeGenerator::postvisit( CatchStmt * stmt ) {
|
---|
[42a36d9] | 1062 | assertf( ! options.genC, "Catch statements should not reach code generation." );
|
---|
[e4ea10b7] | 1063 |
|
---|
| 1064 | output << ((stmt->get_kind() == CatchStmt::Terminate) ?
|
---|
[60a8062] | 1065 | "catch" : "catchResume");
|
---|
[e4ea10b7] | 1066 | output << "( ";
|
---|
| 1067 | stmt->decl->accept( *visitor );
|
---|
| 1068 | output << " ) ";
|
---|
| 1069 |
|
---|
| 1070 | if( stmt->cond ) {
|
---|
| 1071 | output << "if/when(?) (";
|
---|
| 1072 | stmt->cond->accept( *visitor );
|
---|
| 1073 | output << ") ";
|
---|
| 1074 | }
|
---|
| 1075 | stmt->body->accept( *visitor );
|
---|
| 1076 | }
|
---|
| 1077 |
|
---|
| 1078 | void CodeGenerator::postvisit( WaitForStmt * stmt ) {
|
---|
[42a36d9] | 1079 | assertf( ! options.genC, "Waitfor statements should not reach code generation." );
|
---|
[e4ea10b7] | 1080 |
|
---|
| 1081 | bool first = true;
|
---|
| 1082 | for( auto & clause : stmt->clauses ) {
|
---|
| 1083 | if(first) { output << "or "; first = false; }
|
---|
| 1084 | if( clause.condition ) {
|
---|
| 1085 | output << "when(";
|
---|
| 1086 | stmt->timeout.condition->accept( *visitor );
|
---|
| 1087 | output << ") ";
|
---|
| 1088 | }
|
---|
| 1089 | output << "waitfor(";
|
---|
| 1090 | clause.target.function->accept( *visitor );
|
---|
| 1091 | for( Expression * expr : clause.target.arguments ) {
|
---|
| 1092 | output << ",";
|
---|
| 1093 | expr->accept( *visitor );
|
---|
| 1094 | }
|
---|
| 1095 | output << ") ";
|
---|
| 1096 | clause.statement->accept( *visitor );
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 | if( stmt->timeout.statement ) {
|
---|
| 1100 | output << "or ";
|
---|
| 1101 | if( stmt->timeout.condition ) {
|
---|
| 1102 | output << "when(";
|
---|
| 1103 | stmt->timeout.condition->accept( *visitor );
|
---|
| 1104 | output << ") ";
|
---|
| 1105 | }
|
---|
| 1106 | output << "timeout(";
|
---|
| 1107 | stmt->timeout.time->accept( *visitor );
|
---|
| 1108 | output << ") ";
|
---|
| 1109 | stmt->timeout.statement->accept( *visitor );
|
---|
| 1110 | }
|
---|
| 1111 |
|
---|
| 1112 | if( stmt->orelse.statement ) {
|
---|
| 1113 | output << "or ";
|
---|
| 1114 | if( stmt->orelse.condition ) {
|
---|
| 1115 | output << "when(";
|
---|
| 1116 | stmt->orelse.condition->accept( *visitor );
|
---|
| 1117 | output << ")";
|
---|
| 1118 | }
|
---|
| 1119 | output << "else ";
|
---|
| 1120 | stmt->orelse.statement->accept( *visitor );
|
---|
| 1121 | }
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
[55d6e8de] | 1124 | void CodeGenerator::postvisit( WithStmt * with ) {
|
---|
[42a36d9] | 1125 | if ( ! options.genC ) {
|
---|
[55d6e8de] | 1126 | output << "with ( ";
|
---|
| 1127 | genCommaList( with->exprs.begin(), with->exprs.end() );
|
---|
| 1128 | output << " ) ";
|
---|
| 1129 | }
|
---|
| 1130 | with->stmt->accept( *visitor );
|
---|
| 1131 | }
|
---|
[daf1af8] | 1132 |
|
---|
[3b0bc16] | 1133 | void CodeGenerator::postvisit( WhileDoStmt * whileDoStmt ) {
|
---|
| 1134 | if ( whileDoStmt->get_isDoWhile() ) {
|
---|
[d7312ac] | 1135 | output << "do";
|
---|
[321a2481] | 1136 | } else {
|
---|
[d7312ac] | 1137 | output << "while (";
|
---|
[3b0bc16] | 1138 | whileDoStmt->get_condition()->accept( *visitor );
|
---|
[6c4ff37] | 1139 | output << ")";
|
---|
[51587aa] | 1140 | } // if
|
---|
[2b6c1e0] | 1141 | output << " ";
|
---|
[51587aa] | 1142 |
|
---|
[3b0bc16] | 1143 | output << CodeGenerator::printLabels( whileDoStmt->get_body()->get_labels() );
|
---|
| 1144 | whileDoStmt->get_body()->accept( *visitor );
|
---|
[51587aa] | 1145 |
|
---|
[cda48b6] | 1146 | output << indent;
|
---|
[51587aa] | 1147 |
|
---|
[3b0bc16] | 1148 | if ( whileDoStmt->get_isDoWhile() ) {
|
---|
[d7312ac] | 1149 | output << " while (";
|
---|
[3b0bc16] | 1150 | whileDoStmt->get_condition()->accept( *visitor );
|
---|
[6c4ff37] | 1151 | output << ");";
|
---|
[51587aa] | 1152 | } // if
|
---|
| 1153 | }
|
---|
| 1154 |
|
---|
[9857e8d] | 1155 | void CodeGenerator::postvisit( ForStmt * forStmt ) {
|
---|
[8e9cbb2] | 1156 | // initialization is always hoisted, so don't bother doing anything with that
|
---|
[145f1fc] | 1157 | output << "for (;";
|
---|
[51587aa] | 1158 |
|
---|
[321a2481] | 1159 | if ( forStmt->get_condition() != 0 ) {
|
---|
[9857e8d] | 1160 | forStmt->get_condition()->accept( *visitor );
|
---|
[3778cb2] | 1161 | } // if
|
---|
[6c4ff37] | 1162 | output << ";";
|
---|
[51587aa] | 1163 |
|
---|
[321a2481] | 1164 | if ( forStmt->get_increment() != 0 ) {
|
---|
| 1165 | // cast the top-level expression to void to reduce gcc warnings.
|
---|
| 1166 | Expression * expr = new CastExpr( forStmt->get_increment() );
|
---|
[9857e8d] | 1167 | expr->accept( *visitor );
|
---|
[3778cb2] | 1168 | } // if
|
---|
[2b6c1e0] | 1169 | output << ") ";
|
---|
[51587aa] | 1170 |
|
---|
| 1171 | if ( forStmt->get_body() != 0 ) {
|
---|
[2b6c1e0] | 1172 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
|
---|
[9857e8d] | 1173 | forStmt->get_body()->accept( *visitor );
|
---|
[51587aa] | 1174 | } // if
|
---|
| 1175 | }
|
---|
| 1176 |
|
---|
[9857e8d] | 1177 | void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) {
|
---|
[cda48b6] | 1178 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
|
---|
[6c4ff37] | 1179 | output << "/* null statement */ ;";
|
---|
[51587aa] | 1180 | }
|
---|
| 1181 |
|
---|
[9857e8d] | 1182 | void CodeGenerator::postvisit( DeclStmt * declStmt ) {
|
---|
| 1183 | declStmt->get_decl()->accept( *visitor );
|
---|
[71f4e4f] | 1184 |
|
---|
[51587aa] | 1185 | if ( doSemicolon( declStmt->get_decl() ) ) {
|
---|
[6c4ff37] | 1186 | output << ";";
|
---|
[51587aa] | 1187 | } // if
|
---|
| 1188 | }
|
---|
| 1189 |
|
---|
[9857e8d] | 1190 | void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) {
|
---|
[42a36d9] | 1191 | assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." );
|
---|
[9857e8d] | 1192 | stmt->callStmt->accept( *visitor );
|
---|
| 1193 | }
|
---|
| 1194 |
|
---|
[6cebfef] | 1195 | void CodeGenerator::postvisit( MutexStmt * stmt ) {
|
---|
| 1196 | assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." );
|
---|
| 1197 | stmt->stmt->accept( *visitor );
|
---|
| 1198 | }
|
---|
| 1199 |
|
---|
[dd020c0] | 1200 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
|
---|
[fb04321] | 1201 | if ( decl->get_storageClasses().any() ) {
|
---|
[6e8bd43] | 1202 | decl->get_storageClasses().print( output );
|
---|
[a7c90d4] | 1203 | } // if
|
---|
[dd020c0] | 1204 | } // CodeGenerator::handleStorageClass
|
---|
[9facf3b] | 1205 |
|
---|
| 1206 | std::string genName( DeclarationWithType * decl ) {
|
---|
[60a8062] | 1207 | const OperatorInfo * opInfo = operatorLookup( decl->get_name() );
|
---|
| 1208 | if ( opInfo ) {
|
---|
| 1209 | return opInfo->outputName;
|
---|
[9facf3b] | 1210 | } else {
|
---|
| 1211 | return decl->get_name();
|
---|
| 1212 | } // if
|
---|
| 1213 | }
|
---|
[51b73452] | 1214 | } // namespace CodeGen
|
---|
[51587aa] | 1215 |
|
---|
[50377a4] | 1216 |
|
---|
| 1217 | unsigned Indenter::tabsize = 2;
|
---|
| 1218 |
|
---|
[e149f77] | 1219 | std::ostream & operator<<( std::ostream & out, const BaseSyntaxNode * node ) {
|
---|
| 1220 | if ( node ) {
|
---|
| 1221 | node->print( out );
|
---|
| 1222 | } else {
|
---|
| 1223 | out << "nullptr";
|
---|
| 1224 | }
|
---|
| 1225 | return out;
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
[51587aa] | 1228 | // Local Variables: //
|
---|
| 1229 | // tab-width: 4 //
|
---|
| 1230 | // mode: c++ //
|
---|
| 1231 | // compile-command: "make install" //
|
---|
| 1232 | // End: //
|
---|