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