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