| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // CodeGenerator.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Thu Feb 16 14:56:29 2017
 | 
|---|
| 13 | // Update Count     : 418
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <algorithm>
 | 
|---|
| 17 | #include <iostream>
 | 
|---|
| 18 | #include <cassert>
 | 
|---|
| 19 | #include <list>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "Parser/ParseNode.h"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "SynTree/Declaration.h"
 | 
|---|
| 24 | #include "SynTree/Expression.h"
 | 
|---|
| 25 | #include "SynTree/Initializer.h"
 | 
|---|
| 26 | #include "SynTree/Statement.h"
 | 
|---|
| 27 | #include "SynTree/Type.h"
 | 
|---|
| 28 | #include "SynTree/Attribute.h"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #include "Common/utility.h"
 | 
|---|
| 31 | #include "Common/UnimplementedError.h"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "CodeGenerator.h"
 | 
|---|
| 34 | #include "OperatorTable.h"
 | 
|---|
| 35 | #include "GenType.h"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "InitTweak/InitTweak.h"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | using namespace std;
 | 
|---|
| 40 | 
 | 
|---|
| 41 | namespace CodeGen {
 | 
|---|
| 42 |         int CodeGenerator::tabsize = 4;
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         // the kinds of statements that would ideally be followed by whitespace
 | 
|---|
| 45 |         bool wantSpacing( Statement * stmt) {
 | 
|---|
| 46 |                 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
 | 
|---|
| 47 |                         dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
 | 
|---|
| 48 |         }
 | 
|---|
| 49 | 
 | 
|---|
| 50 |         void CodeGenerator::extension( Expression * expr ) {
 | 
|---|
| 51 |                 if ( expr->get_extension() ) {
 | 
|---|
| 52 |                         output << "__extension__ ";
 | 
|---|
| 53 |                 } // if
 | 
|---|
| 54 |         } // extension
 | 
|---|
| 55 | 
 | 
|---|
| 56 |         void CodeGenerator::extension( Declaration * decl ) {
 | 
|---|
| 57 |                 if ( decl->get_extension() ) {
 | 
|---|
| 58 |                         output << "__extension__ ";
 | 
|---|
| 59 |                 } // if
 | 
|---|
| 60 |         } // extension
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         void CodeGenerator::asmName( DeclarationWithType * decl ) {
 | 
|---|
| 63 |                 if ( ConstantExpr * asmName = decl->get_asmName() ) {
 | 
|---|
| 64 |                         output << " asm ( " << asmName->get_constant()->get_value() << " )";
 | 
|---|
| 65 |                 } // if
 | 
|---|
| 66 |         } // extension
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
 | 
|---|
| 69 |           return output << string( cg.cur_indent, ' ' );
 | 
|---|
| 70 |         }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
 | 
|---|
| 73 |                 return indent( output );
 | 
|---|
| 74 |         }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
 | 
|---|
| 77 |                 labels = &l;
 | 
|---|
| 78 |                 return *this;
 | 
|---|
| 79 |         }
 | 
|---|
| 80 | 
 | 
|---|
| 81 |         ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {
 | 
|---|
| 82 |                 std::list< Label > & labs = *printLabels.labels;
 | 
|---|
| 83 |                 // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
 | 
|---|
| 84 |                 for ( Label & l : labs ) {
 | 
|---|
| 85 |                         output << l.get_name() + ": ";
 | 
|---|
| 86 |                         printLabels.cg.genAttributes( l.get_attributes() );
 | 
|---|
| 87 |                 } // for
 | 
|---|
| 88 |                 return output;
 | 
|---|
| 89 |         }
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         CodeGenerator::CodeGenerator( std::ostream & os, bool pretty ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ) {}
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp )
 | 
|---|
| 94 |                         : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
 | 
|---|
| 95 |                 //output << std::string( init );
 | 
|---|
| 96 |         }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         CodeGenerator::CodeGenerator( std::ostream & os, char * init, int indentation, bool infunp )
 | 
|---|
| 99 |                         : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
 | 
|---|
| 100 |                 //output << std::string( init );
 | 
|---|
| 101 |         }
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         string CodeGenerator::mangleName( DeclarationWithType * decl ) {
 | 
|---|
| 104 |                 if ( pretty ) return decl->get_name();
 | 
|---|
| 105 |                 if ( decl->get_mangleName() != "" ) {
 | 
|---|
| 106 |                         // need to incorporate scope level in order to differentiate names for destructors
 | 
|---|
| 107 |                         return decl->get_scopedMangleName();
 | 
|---|
| 108 |                 } else {
 | 
|---|
| 109 |                         return decl->get_name();
 | 
|---|
| 110 |                 } // if
 | 
|---|
| 111 |         }
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         void CodeGenerator::genAttributes( list< Attribute * > & attributes ) {
 | 
|---|
| 114 |           if ( attributes.empty() ) return;
 | 
|---|
| 115 |                 output << "__attribute__ ((";
 | 
|---|
| 116 |                 for ( list< Attribute * >::iterator attr( attributes.begin() );; ) {
 | 
|---|
| 117 |                         output << (*attr)->get_name();
 | 
|---|
| 118 |                         if ( ! (*attr)->get_parameters().empty() ) {
 | 
|---|
| 119 |                                 output << "(";
 | 
|---|
| 120 |                                 genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() );
 | 
|---|
| 121 |                                 output << ")";
 | 
|---|
| 122 |                         } // if
 | 
|---|
| 123 |                   if ( ++attr == attributes.end() ) break;
 | 
|---|
| 124 |                         output << ",";                                                          // separator
 | 
|---|
| 125 |                 } // for
 | 
|---|
| 126 |                 output << ")) ";
 | 
|---|
| 127 |         } // CodeGenerator::genAttributes
 | 
|---|
| 128 | 
 | 
|---|
| 129 | 
 | 
|---|
| 130 |         //*** Declarations
 | 
|---|
| 131 |         void CodeGenerator::visit( FunctionDecl * functionDecl ) {
 | 
|---|
| 132 |                 extension( functionDecl );
 | 
|---|
| 133 |                 genAttributes( functionDecl->get_attributes() );
 | 
|---|
| 134 | 
 | 
|---|
| 135 |                 handleStorageClass( functionDecl );
 | 
|---|
| 136 |                 if ( functionDecl->get_isInline() ) {
 | 
|---|
| 137 |                         output << "inline ";
 | 
|---|
| 138 |                 } // if
 | 
|---|
| 139 |                 if ( functionDecl->get_isNoreturn() ) {
 | 
|---|
| 140 |                         output << "_Noreturn ";
 | 
|---|
| 141 |                 } // if
 | 
|---|
| 142 |                 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty );
 | 
|---|
| 143 | 
 | 
|---|
| 144 |                 asmName( functionDecl );
 | 
|---|
| 145 | 
 | 
|---|
| 146 |                 // acceptAll( functionDecl->get_oldDecls(), *this );
 | 
|---|
| 147 |                 if ( functionDecl->get_statements() ) {
 | 
|---|
| 148 |                         functionDecl->get_statements()->accept( *this );
 | 
|---|
| 149 |                 } // if
 | 
|---|
| 150 |         }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |         void CodeGenerator::visit( ObjectDecl * objectDecl ) {
 | 
|---|
| 153 |                 extension( objectDecl );
 | 
|---|
| 154 |                 genAttributes( objectDecl->get_attributes() );
 | 
|---|
| 155 | 
 | 
|---|
| 156 |                 handleStorageClass( objectDecl );
 | 
|---|
| 157 |                 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty );
 | 
|---|
| 158 | 
 | 
|---|
| 159 |                 asmName( objectDecl );
 | 
|---|
| 160 | 
 | 
|---|
| 161 |                 if ( objectDecl->get_init() ) {
 | 
|---|
| 162 |                         output << " = ";
 | 
|---|
| 163 |                         objectDecl->get_init()->accept( *this );
 | 
|---|
| 164 |                 } // if
 | 
|---|
| 165 | 
 | 
|---|
| 166 |                 if ( objectDecl->get_bitfieldWidth() ) {
 | 
|---|
| 167 |                         output << ":";
 | 
|---|
| 168 |                         objectDecl->get_bitfieldWidth()->accept( *this );
 | 
|---|
| 169 |                 } // if
 | 
|---|
| 170 |         }
 | 
|---|
| 171 | 
 | 
|---|
| 172 |         void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {
 | 
|---|
| 173 |                 genAttributes( aggDecl->get_attributes() );
 | 
|---|
| 174 | 
 | 
|---|
| 175 |                 if ( aggDecl->get_name() != "" )
 | 
|---|
| 176 |                         output << aggDecl->get_name();
 | 
|---|
| 177 | 
 | 
|---|
| 178 |                 // std::list< Declaration * > & memb = aggDecl->get_members();
 | 
|---|
| 179 |                 // if ( ! memb.empty() ) {
 | 
|---|
| 180 |                 if ( aggDecl->has_body() ) {
 | 
|---|
| 181 |                         std::list< Declaration * > & memb = aggDecl->get_members();
 | 
|---|
| 182 |                         output << " {" << endl;
 | 
|---|
| 183 | 
 | 
|---|
| 184 |                         cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 185 |                         for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
 | 
|---|
| 186 |                                 output << indent;
 | 
|---|
| 187 |                                 (*i)->accept( *this );
 | 
|---|
| 188 |                                 output << ";" << endl;
 | 
|---|
| 189 |                         } // for
 | 
|---|
| 190 | 
 | 
|---|
| 191 |                         cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 192 | 
 | 
|---|
| 193 |                         output << indent << "}";
 | 
|---|
| 194 |                 } // if
 | 
|---|
| 195 |         }
 | 
|---|
| 196 | 
 | 
|---|
| 197 |         void CodeGenerator::visit( StructDecl * structDecl ) {
 | 
|---|
| 198 |                 extension( structDecl );
 | 
|---|
| 199 |                 output << "struct ";
 | 
|---|
| 200 |                 handleAggregate( structDecl );
 | 
|---|
| 201 |         }
 | 
|---|
| 202 | 
 | 
|---|
| 203 |         void CodeGenerator::visit( UnionDecl * unionDecl ) {
 | 
|---|
| 204 |                 extension( unionDecl );
 | 
|---|
| 205 |                 output << "union ";
 | 
|---|
| 206 |                 handleAggregate( unionDecl );
 | 
|---|
| 207 |         }
 | 
|---|
| 208 | 
 | 
|---|
| 209 |         void CodeGenerator::visit( EnumDecl * enumDecl ) {
 | 
|---|
| 210 |                 extension( enumDecl );
 | 
|---|
| 211 |                 output << "enum ";
 | 
|---|
| 212 |                 genAttributes( enumDecl->get_attributes() );
 | 
|---|
| 213 | 
 | 
|---|
| 214 |                 if ( enumDecl->get_name() != "" )
 | 
|---|
| 215 |                         output << enumDecl->get_name();
 | 
|---|
| 216 | 
 | 
|---|
| 217 |                 std::list< Declaration* > &memb = enumDecl->get_members();
 | 
|---|
| 218 | 
 | 
|---|
| 219 |                 if ( ! memb.empty() ) {
 | 
|---|
| 220 |                         output << " {" << endl;
 | 
|---|
| 221 | 
 | 
|---|
| 222 |                         cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 223 |                         for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
 | 
|---|
| 224 |                                 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
 | 
|---|
| 225 |                                 assert( obj );
 | 
|---|
| 226 |                                 output << indent << mangleName( obj );
 | 
|---|
| 227 |                                 if ( obj->get_init() ) {
 | 
|---|
| 228 |                                         output << " = ";
 | 
|---|
| 229 |                                         obj->get_init()->accept( *this );
 | 
|---|
| 230 |                                 } // if
 | 
|---|
| 231 |                                 output << "," << endl;
 | 
|---|
| 232 |                         } // for
 | 
|---|
| 233 | 
 | 
|---|
| 234 |                         cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 235 | 
 | 
|---|
| 236 |                         output << indent << "}";
 | 
|---|
| 237 |                 } // if
 | 
|---|
| 238 |         }
 | 
|---|
| 239 | 
 | 
|---|
| 240 |         void CodeGenerator::visit( TraitDecl * traitDecl ) {}
 | 
|---|
| 241 | 
 | 
|---|
| 242 |         void CodeGenerator::visit( TypedefDecl * typeDecl ) {
 | 
|---|
| 243 |                 assert( false && "Typedefs are removed and substituted in earlier passes." );
 | 
|---|
| 244 |                 //output << "typedef ";
 | 
|---|
| 245 |                 //output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty );
 | 
|---|
| 246 |         }
 | 
|---|
| 247 | 
 | 
|---|
| 248 |         void CodeGenerator::visit( TypeDecl * typeDecl ) {
 | 
|---|
| 249 |                 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
 | 
|---|
| 250 |                 // still to be done
 | 
|---|
| 251 |                 extension( typeDecl );
 | 
|---|
| 252 |                 output << "extern unsigned long " << typeDecl->get_name();
 | 
|---|
| 253 |                 if ( typeDecl->get_base() ) {
 | 
|---|
| 254 |                         output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty ) << " )";
 | 
|---|
| 255 |                 } // if
 | 
|---|
| 256 |         }
 | 
|---|
| 257 | 
 | 
|---|
| 258 |         void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
 | 
|---|
| 259 |                 typedef std::list< Expression * > DesignatorList;
 | 
|---|
| 260 |                 if ( designators.size() == 0 ) return;
 | 
|---|
| 261 |                 for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
 | 
|---|
| 262 |                         if ( dynamic_cast< NameExpr * >( *iter ) ) {
 | 
|---|
| 263 |                                 // if expression is a name, then initializing aggregate member
 | 
|---|
| 264 |                                 output << ".";
 | 
|---|
| 265 |                                 (*iter)->accept( *this );
 | 
|---|
| 266 |                         } else {
 | 
|---|
| 267 |                                 // if not a simple name, it has to be a constant expression, i.e. an array designator
 | 
|---|
| 268 |                                 output << "[";
 | 
|---|
| 269 |                                 (*iter)->accept( *this );
 | 
|---|
| 270 |                                 output << "]";
 | 
|---|
| 271 |                         } // if
 | 
|---|
| 272 |                 } // for
 | 
|---|
| 273 |                 output << " = ";
 | 
|---|
| 274 |         }
 | 
|---|
| 275 | 
 | 
|---|
| 276 |         void CodeGenerator::visit( SingleInit * init ) {
 | 
|---|
| 277 |                 printDesignators( init->get_designators() );
 | 
|---|
| 278 |                 init->get_value()->accept( *this );
 | 
|---|
| 279 |         }
 | 
|---|
| 280 | 
 | 
|---|
| 281 |         void CodeGenerator::visit( ListInit * init ) {
 | 
|---|
| 282 |                 printDesignators( init->get_designators() );
 | 
|---|
| 283 |                 output << "{ ";
 | 
|---|
| 284 |                 if ( init->begin() == init->end() ) {
 | 
|---|
| 285 |                         // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
 | 
|---|
| 286 |                         output << "0";
 | 
|---|
| 287 |                 } else {
 | 
|---|
| 288 |                         genCommaList( init->begin(), init->end() );
 | 
|---|
| 289 |                 } // if
 | 
|---|
| 290 |                 output << " }";
 | 
|---|
| 291 |         }
 | 
|---|
| 292 | 
 | 
|---|
| 293 |         void CodeGenerator::visit( ConstructorInit * init ){
 | 
|---|
| 294 |                 assertf( false, "ConstructorInit nodes should not make it to CodeGen." );
 | 
|---|
| 295 |         }
 | 
|---|
| 296 | 
 | 
|---|
| 297 |         void CodeGenerator::visit( Constant * constant ) {
 | 
|---|
| 298 |                 output << constant->get_value() ;
 | 
|---|
| 299 |         }
 | 
|---|
| 300 | 
 | 
|---|
| 301 |         //*** Expressions
 | 
|---|
| 302 |         void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
 | 
|---|
| 303 |                 extension( applicationExpr );
 | 
|---|
| 304 |                 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
 | 
|---|
| 305 |                         OperatorInfo opInfo;
 | 
|---|
| 306 |                         if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
 | 
|---|
| 307 |                                 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
 | 
|---|
| 308 |                                 switch ( opInfo.type ) {
 | 
|---|
| 309 |                                   case OT_PREFIXASSIGN:
 | 
|---|
| 310 |                                   case OT_POSTFIXASSIGN:
 | 
|---|
| 311 |                                   case OT_INFIXASSIGN:
 | 
|---|
| 312 |                                   case OT_CTOR:
 | 
|---|
| 313 |                                   case OT_DTOR:
 | 
|---|
| 314 |                                         {
 | 
|---|
| 315 |                                                 assert( arg != applicationExpr->get_args().end() );
 | 
|---|
| 316 |                                                 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
 | 
|---|
| 317 |                                                         // remove & from first assignment/ctor argument
 | 
|---|
| 318 |                                                         *arg = addrExpr->get_arg();
 | 
|---|
| 319 |                                                 } else {
 | 
|---|
| 320 |                                                         // no address-of operator, so must be a pointer - add dereference
 | 
|---|
| 321 |                                                         // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared.
 | 
|---|
| 322 |                                                         // Since its arguments are modified here, this assertion most commonly triggers when the application
 | 
|---|
| 323 |                                                         // is visited multiple times.
 | 
|---|
| 324 |                                                         UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
 | 
|---|
| 325 |                                                         newExpr->get_args().push_back( *arg );
 | 
|---|
| 326 |                                                         Type * type = InitTweak::getPointerBase( (*arg)->get_result() );
 | 
|---|
| 327 |                                                         assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." );
 | 
|---|
| 328 |                                                         newExpr->set_result( type->clone() );
 | 
|---|
| 329 |                                                         *arg = newExpr;
 | 
|---|
| 330 |                                                 } // if
 | 
|---|
| 331 |                                                 break;
 | 
|---|
| 332 |                                         }
 | 
|---|
| 333 | 
 | 
|---|
| 334 |                                   default:
 | 
|---|
| 335 |                                         // do nothing
 | 
|---|
| 336 |                                         ;
 | 
|---|
| 337 |                                 } // switch
 | 
|---|
| 338 | 
 | 
|---|
| 339 |                                 switch ( opInfo.type ) {
 | 
|---|
| 340 |                                   case OT_INDEX:
 | 
|---|
| 341 |                                         assert( applicationExpr->get_args().size() == 2 );
 | 
|---|
| 342 |                                         (*arg++)->accept( *this );
 | 
|---|
| 343 |                                         output << "[";
 | 
|---|
| 344 |                                         (*arg)->accept( *this );
 | 
|---|
| 345 |                                         output << "]";
 | 
|---|
| 346 |                                         break;
 | 
|---|
| 347 | 
 | 
|---|
| 348 |                                   case OT_CALL:
 | 
|---|
| 349 |                                         // there are no intrinsic definitions of the function call operator
 | 
|---|
| 350 |                                         assert( false );
 | 
|---|
| 351 |                                         break;
 | 
|---|
| 352 | 
 | 
|---|
| 353 |                                   case OT_CTOR:
 | 
|---|
| 354 |                                   case OT_DTOR:
 | 
|---|
| 355 |                                         if ( applicationExpr->get_args().size() == 1 ) {
 | 
|---|
| 356 |                                                 // the expression fed into a single parameter constructor or destructor may contain side
 | 
|---|
| 357 |                                                 // effects, so must still output this expression
 | 
|---|
| 358 |                                                 output << "(";
 | 
|---|
| 359 |                                                 (*arg++)->accept( *this );
 | 
|---|
| 360 |                                                 output << ") /* " << opInfo.inputName << " */";
 | 
|---|
| 361 |                                         } else if ( applicationExpr->get_args().size() == 2 ) {
 | 
|---|
| 362 |                                                 // intrinsic two parameter constructors are essentially bitwise assignment
 | 
|---|
| 363 |                                                 output << "(";
 | 
|---|
| 364 |                                                 (*arg++)->accept( *this );
 | 
|---|
| 365 |                                                 output << opInfo.symbol;
 | 
|---|
| 366 |                                                 (*arg)->accept( *this );
 | 
|---|
| 367 |                                                 output << ") /* " << opInfo.inputName << " */";
 | 
|---|
| 368 |                                         } else {
 | 
|---|
| 369 |                                                 // no constructors with 0 or more than 2 parameters
 | 
|---|
| 370 |                                                 assert( false );
 | 
|---|
| 371 |                                         } // if
 | 
|---|
| 372 |                                         break;
 | 
|---|
| 373 | 
 | 
|---|
| 374 |                                   case OT_PREFIX:
 | 
|---|
| 375 |                                   case OT_PREFIXASSIGN:
 | 
|---|
| 376 |                                         assert( applicationExpr->get_args().size() == 1 );
 | 
|---|
| 377 |                                         output << "(";
 | 
|---|
| 378 |                                         output << opInfo.symbol;
 | 
|---|
| 379 |                                         (*arg)->accept( *this );
 | 
|---|
| 380 |                                         output << ")";
 | 
|---|
| 381 |                                         break;
 | 
|---|
| 382 | 
 | 
|---|
| 383 |                                   case OT_POSTFIX:
 | 
|---|
| 384 |                                   case OT_POSTFIXASSIGN:
 | 
|---|
| 385 |                                         assert( applicationExpr->get_args().size() == 1 );
 | 
|---|
| 386 |                                         (*arg)->accept( *this );
 | 
|---|
| 387 |                                         output << opInfo.symbol;
 | 
|---|
| 388 |                                         break;
 | 
|---|
| 389 | 
 | 
|---|
| 390 | 
 | 
|---|
| 391 |                                   case OT_INFIX:
 | 
|---|
| 392 |                                   case OT_INFIXASSIGN:
 | 
|---|
| 393 |                                         assert( applicationExpr->get_args().size() == 2 );
 | 
|---|
| 394 |                                         output << "(";
 | 
|---|
| 395 |                                         (*arg++)->accept( *this );
 | 
|---|
| 396 |                                         output << opInfo.symbol;
 | 
|---|
| 397 |                                         (*arg)->accept( *this );
 | 
|---|
| 398 |                                         output << ")";
 | 
|---|
| 399 |                                         break;
 | 
|---|
| 400 | 
 | 
|---|
| 401 |                                   case OT_CONSTANT:
 | 
|---|
| 402 |                                   case OT_LABELADDRESS:
 | 
|---|
| 403 |                                         // there are no intrinsic definitions of 0/1 or label addresses as functions
 | 
|---|
| 404 |                                         assert( false );
 | 
|---|
| 405 |                                 } // switch
 | 
|---|
| 406 |                         } else {
 | 
|---|
| 407 |                                 varExpr->accept( *this );
 | 
|---|
| 408 |                                 output << "(";
 | 
|---|
| 409 |                                 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
 | 
|---|
| 410 |                                 output << ")";
 | 
|---|
| 411 |                         } // if
 | 
|---|
| 412 |                 } else {
 | 
|---|
| 413 |                         applicationExpr->get_function()->accept( *this );
 | 
|---|
| 414 |                         output << "(";
 | 
|---|
| 415 |                         genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
 | 
|---|
| 416 |                         output << ")";
 | 
|---|
| 417 |                 } // if
 | 
|---|
| 418 |         }
 | 
|---|
| 419 | 
 | 
|---|
| 420 |         void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
 | 
|---|
| 421 |                 extension( untypedExpr );
 | 
|---|
| 422 |                 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
 | 
|---|
| 423 |                         OperatorInfo opInfo;
 | 
|---|
| 424 |                         if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
 | 
|---|
| 425 |                                 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
 | 
|---|
| 426 |                                 switch ( opInfo.type ) {
 | 
|---|
| 427 |                                   case OT_INDEX:
 | 
|---|
| 428 |                                         assert( untypedExpr->get_args().size() == 2 );
 | 
|---|
| 429 |                                         (*arg++)->accept( *this );
 | 
|---|
| 430 |                                         output << "[";
 | 
|---|
| 431 |                                         (*arg)->accept( *this );
 | 
|---|
| 432 |                                         output << "]";
 | 
|---|
| 433 |                                         break;
 | 
|---|
| 434 | 
 | 
|---|
| 435 |                                   case OT_CALL:
 | 
|---|
| 436 |                                         assert( false );
 | 
|---|
| 437 | 
 | 
|---|
| 438 |                                   case OT_CTOR:
 | 
|---|
| 439 |                                   case OT_DTOR:
 | 
|---|
| 440 |                                         if ( untypedExpr->get_args().size() == 1 ) {
 | 
|---|
| 441 |                                                 // the expression fed into a single parameter constructor or destructor may contain side
 | 
|---|
| 442 |                                                 // effects, so must still output this expression
 | 
|---|
| 443 |                                                 output << "(";
 | 
|---|
| 444 |                                                 (*arg++)->accept( *this );
 | 
|---|
| 445 |                                                 output << ") /* " << opInfo.inputName << " */";
 | 
|---|
| 446 |                                         } else if ( untypedExpr->get_args().size() == 2 ) {
 | 
|---|
| 447 |                                                 // intrinsic two parameter constructors are essentially bitwise assignment
 | 
|---|
| 448 |                                                 output << "(";
 | 
|---|
| 449 |                                                 (*arg++)->accept( *this );
 | 
|---|
| 450 |                                                 output << opInfo.symbol;
 | 
|---|
| 451 |                                                 (*arg)->accept( *this );
 | 
|---|
| 452 |                                                 output << ") /* " << opInfo.inputName << " */";
 | 
|---|
| 453 |                                         } else {
 | 
|---|
| 454 |                                                 // no constructors with 0 or more than 2 parameters
 | 
|---|
| 455 |                                                 assert( false );
 | 
|---|
| 456 |                                         } // if
 | 
|---|
| 457 |                                         break;
 | 
|---|
| 458 | 
 | 
|---|
| 459 |                                   case OT_PREFIX:
 | 
|---|
| 460 |                                   case OT_PREFIXASSIGN:
 | 
|---|
| 461 |                                   case OT_LABELADDRESS:
 | 
|---|
| 462 |                                         assert( untypedExpr->get_args().size() == 1 );
 | 
|---|
| 463 |                                         output << "(";
 | 
|---|
| 464 |                                         output << opInfo.symbol;
 | 
|---|
| 465 |                                         (*arg)->accept( *this );
 | 
|---|
| 466 |                                         output << ")";
 | 
|---|
| 467 |                                         break;
 | 
|---|
| 468 | 
 | 
|---|
| 469 |                                   case OT_POSTFIX:
 | 
|---|
| 470 |                                   case OT_POSTFIXASSIGN:
 | 
|---|
| 471 |                                         assert( untypedExpr->get_args().size() == 1 );
 | 
|---|
| 472 |                                         (*arg)->accept( *this );
 | 
|---|
| 473 |                                         output << opInfo.symbol;
 | 
|---|
| 474 |                                         break;
 | 
|---|
| 475 | 
 | 
|---|
| 476 |                                   case OT_INFIX:
 | 
|---|
| 477 |                                   case OT_INFIXASSIGN:
 | 
|---|
| 478 |                                         assert( untypedExpr->get_args().size() == 2 );
 | 
|---|
| 479 |                                         output << "(";
 | 
|---|
| 480 |                                         (*arg++)->accept( *this );
 | 
|---|
| 481 |                                         output << opInfo.symbol;
 | 
|---|
| 482 |                                         (*arg)->accept( *this );
 | 
|---|
| 483 |                                         output << ")";
 | 
|---|
| 484 |                                         break;
 | 
|---|
| 485 | 
 | 
|---|
| 486 |                                   case OT_CONSTANT:
 | 
|---|
| 487 |                                         // there are no intrinsic definitions of 0 or 1 as functions
 | 
|---|
| 488 |                                         assert( false );
 | 
|---|
| 489 |                                 } // switch
 | 
|---|
| 490 |                         } else {
 | 
|---|
| 491 |                                 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
 | 
|---|
| 492 |                                         assert( untypedExpr->get_args().size() == 2 );
 | 
|---|
| 493 |                                         (*untypedExpr->get_args().begin())->accept( *this );
 | 
|---|
| 494 |                                         output << " ... ";
 | 
|---|
| 495 |                                         (*--untypedExpr->get_args().end())->accept( *this );
 | 
|---|
| 496 |                                 } else {                                                                // builtin routines
 | 
|---|
| 497 |                                         nameExpr->accept( *this );
 | 
|---|
| 498 |                                         output << "(";
 | 
|---|
| 499 |                                         genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
 | 
|---|
| 500 |                                         output << ")";
 | 
|---|
| 501 |                                 } // if
 | 
|---|
| 502 |                         } // if
 | 
|---|
| 503 |                 } else {
 | 
|---|
| 504 |                         untypedExpr->get_function()->accept( *this );
 | 
|---|
| 505 |                         output << "(";
 | 
|---|
| 506 |                         genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
 | 
|---|
| 507 |                         output << ")";
 | 
|---|
| 508 |                 } // if
 | 
|---|
| 509 |         }
 | 
|---|
| 510 | 
 | 
|---|
| 511 |         void CodeGenerator::visit( RangeExpr * rangeExpr ) {
 | 
|---|
| 512 |                 rangeExpr->get_low()->accept( *this );
 | 
|---|
| 513 |                 output << " ... ";
 | 
|---|
| 514 |                 rangeExpr->get_high()->accept( *this );
 | 
|---|
| 515 |         }
 | 
|---|
| 516 | 
 | 
|---|
| 517 |         void CodeGenerator::visit( NameExpr * nameExpr ) {
 | 
|---|
| 518 |                 extension( nameExpr );
 | 
|---|
| 519 |                 OperatorInfo opInfo;
 | 
|---|
| 520 |                 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
 | 
|---|
| 521 |                         assert( opInfo.type == OT_CONSTANT );
 | 
|---|
| 522 |                         output << opInfo.symbol;
 | 
|---|
| 523 |                 } else {
 | 
|---|
| 524 |                         output << nameExpr->get_name();
 | 
|---|
| 525 |                 } // if
 | 
|---|
| 526 |         }
 | 
|---|
| 527 | 
 | 
|---|
| 528 |         void CodeGenerator::visit( AddressExpr * addressExpr ) {
 | 
|---|
| 529 |                 extension( addressExpr );
 | 
|---|
| 530 |                 output << "(&";
 | 
|---|
| 531 |                 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
 | 
|---|
| 532 |                 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
 | 
|---|
| 533 |                         output << mangleName( variableExpr->get_var() );
 | 
|---|
| 534 |                 } else {
 | 
|---|
| 535 |                         addressExpr->get_arg()->accept( *this );
 | 
|---|
| 536 |                 } // if
 | 
|---|
| 537 |                 output << ")";
 | 
|---|
| 538 |         }
 | 
|---|
| 539 | 
 | 
|---|
| 540 |         void CodeGenerator::visit( CastExpr * castExpr ) {
 | 
|---|
| 541 |                 extension( castExpr );
 | 
|---|
| 542 |                 output << "(";
 | 
|---|
| 543 |                 if ( castExpr->get_result()->isVoid() ) {
 | 
|---|
| 544 |                         output << "(void)" ;
 | 
|---|
| 545 |                 } else if ( ! castExpr->get_result()->get_isLvalue() ) {
 | 
|---|
| 546 |                         // at least one result type of cast, but not an lvalue
 | 
|---|
| 547 |                         output << "(";
 | 
|---|
| 548 |                         output << genType( castExpr->get_result(), "", pretty );
 | 
|---|
| 549 |                         output << ")";
 | 
|---|
| 550 |                 } else {
 | 
|---|
| 551 |                         // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
 | 
|---|
| 552 |                         // never an lvalue in C
 | 
|---|
| 553 |                 } // if
 | 
|---|
| 554 |                 castExpr->get_arg()->accept( *this );
 | 
|---|
| 555 |                 output << ")";
 | 
|---|
| 556 |         }
 | 
|---|
| 557 | 
 | 
|---|
| 558 |         void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
 | 
|---|
| 559 |                 assert( false );
 | 
|---|
| 560 |         }
 | 
|---|
| 561 | 
 | 
|---|
| 562 |         void CodeGenerator::visit( MemberExpr * memberExpr ) {
 | 
|---|
| 563 |                 extension( memberExpr );
 | 
|---|
| 564 |                 memberExpr->get_aggregate()->accept( *this );
 | 
|---|
| 565 |                 output << "." << mangleName( memberExpr->get_member() );
 | 
|---|
| 566 |         }
 | 
|---|
| 567 | 
 | 
|---|
| 568 |         void CodeGenerator::visit( VariableExpr * variableExpr ) {
 | 
|---|
| 569 |                 extension( variableExpr );
 | 
|---|
| 570 |                 OperatorInfo opInfo;
 | 
|---|
| 571 |                 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
 | 
|---|
| 572 |                         output << opInfo.symbol;
 | 
|---|
| 573 |                 } else {
 | 
|---|
| 574 |                         output << mangleName( variableExpr->get_var() );
 | 
|---|
| 575 |                 } // if
 | 
|---|
| 576 |         }
 | 
|---|
| 577 | 
 | 
|---|
| 578 |         void CodeGenerator::visit( ConstantExpr * constantExpr ) {
 | 
|---|
| 579 |                 assert( constantExpr->get_constant() );
 | 
|---|
| 580 |                 extension( constantExpr );
 | 
|---|
| 581 |                 constantExpr->get_constant()->accept( *this );
 | 
|---|
| 582 |         }
 | 
|---|
| 583 | 
 | 
|---|
| 584 |         void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
 | 
|---|
| 585 |                 extension( sizeofExpr );
 | 
|---|
| 586 |                 output << "sizeof(";
 | 
|---|
| 587 |                 if ( sizeofExpr->get_isType() ) {
 | 
|---|
| 588 |                         output << genType( sizeofExpr->get_type(), "", pretty );
 | 
|---|
| 589 |                 } else {
 | 
|---|
| 590 |                         sizeofExpr->get_expr()->accept( *this );
 | 
|---|
| 591 |                 } // if
 | 
|---|
| 592 |                 output << ")";
 | 
|---|
| 593 |         }
 | 
|---|
| 594 | 
 | 
|---|
| 595 |         void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
 | 
|---|
| 596 |                 // use GCC extension to avoid bumping std to C11
 | 
|---|
| 597 |                 extension( alignofExpr );
 | 
|---|
| 598 |                 output << "__alignof__(";
 | 
|---|
| 599 |                 if ( alignofExpr->get_isType() ) {
 | 
|---|
| 600 |                         output << genType( alignofExpr->get_type(), "", pretty );
 | 
|---|
| 601 |                 } else {
 | 
|---|
| 602 |                         alignofExpr->get_expr()->accept( *this );
 | 
|---|
| 603 |                 } // if
 | 
|---|
| 604 |                 output << ")";
 | 
|---|
| 605 |         }
 | 
|---|
| 606 | 
 | 
|---|
| 607 |         void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
 | 
|---|
| 608 |                 assert( false && "UntypedOffsetofExpr should not reach code generation." );
 | 
|---|
| 609 |         }
 | 
|---|
| 610 | 
 | 
|---|
| 611 |         void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
 | 
|---|
| 612 |                 // use GCC builtin
 | 
|---|
| 613 |                 output << "__builtin_offsetof(";
 | 
|---|
| 614 |                 output << genType( offsetofExpr->get_type(), "", pretty );
 | 
|---|
| 615 |                 output << ", " << mangleName( offsetofExpr->get_member() );
 | 
|---|
| 616 |                 output << ")";
 | 
|---|
| 617 |         }
 | 
|---|
| 618 | 
 | 
|---|
| 619 |         void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
 | 
|---|
| 620 |                 assert( false && "OffsetPackExpr should not reach code generation." );
 | 
|---|
| 621 |         }
 | 
|---|
| 622 | 
 | 
|---|
| 623 |         void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
 | 
|---|
| 624 |                 extension( logicalExpr );
 | 
|---|
| 625 |                 output << "(";
 | 
|---|
| 626 |                 logicalExpr->get_arg1()->accept( *this );
 | 
|---|
| 627 |                 if ( logicalExpr->get_isAnd() ) {
 | 
|---|
| 628 |                         output << " && ";
 | 
|---|
| 629 |                 } else {
 | 
|---|
| 630 |                         output << " || ";
 | 
|---|
| 631 |                 } // if
 | 
|---|
| 632 |                 logicalExpr->get_arg2()->accept( *this );
 | 
|---|
| 633 |                 output << ")";
 | 
|---|
| 634 |         }
 | 
|---|
| 635 | 
 | 
|---|
| 636 |         void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
 | 
|---|
| 637 |                 extension( conditionalExpr );
 | 
|---|
| 638 |                 output << "(";
 | 
|---|
| 639 |                 conditionalExpr->get_arg1()->accept( *this );
 | 
|---|
| 640 |                 output << " ? ";
 | 
|---|
| 641 |                 conditionalExpr->get_arg2()->accept( *this );
 | 
|---|
| 642 |                 output << " : ";
 | 
|---|
| 643 |                 conditionalExpr->get_arg3()->accept( *this );
 | 
|---|
| 644 |                 output << ")";
 | 
|---|
| 645 |         }
 | 
|---|
| 646 | 
 | 
|---|
| 647 |         void CodeGenerator::visit( CommaExpr * commaExpr ) {
 | 
|---|
| 648 |                 extension( commaExpr );
 | 
|---|
| 649 |                 output << "(";
 | 
|---|
| 650 |                 commaExpr->get_arg1()->accept( *this );
 | 
|---|
| 651 |                 output << " , ";
 | 
|---|
| 652 |                 commaExpr->get_arg2()->accept( *this );
 | 
|---|
| 653 |                 output << ")";
 | 
|---|
| 654 |         }
 | 
|---|
| 655 | 
 | 
|---|
| 656 |         void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { assertf( false, "UntypedTupleExpr should not make it to Code Gen" ); }
 | 
|---|
| 657 | 
 | 
|---|
| 658 |         void CodeGenerator::visit( TupleExpr * tupleExpr ) { assertf( false, "TupleExpr should not make it to Code Gen" ); }
 | 
|---|
| 659 | 
 | 
|---|
| 660 |         void CodeGenerator::visit( TypeExpr * typeExpr ) {}
 | 
|---|
| 661 | 
 | 
|---|
| 662 |         void CodeGenerator::visit( AsmExpr * asmExpr ) {
 | 
|---|
| 663 |                 if ( asmExpr->get_inout() ) {
 | 
|---|
| 664 |                         output << "[ ";
 | 
|---|
| 665 |                         asmExpr->get_inout()->accept( *this );
 | 
|---|
| 666 |                         output << " ] ";
 | 
|---|
| 667 |                 } // if
 | 
|---|
| 668 |                 asmExpr->get_constraint()->accept( *this );
 | 
|---|
| 669 |                 output << " ( ";
 | 
|---|
| 670 |                 asmExpr->get_operand()->accept( *this );
 | 
|---|
| 671 |                 output << " )";
 | 
|---|
| 672 |         }
 | 
|---|
| 673 | 
 | 
|---|
| 674 |         void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
 | 
|---|
| 675 |                 assert( compLitExpr->get_type() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
 | 
|---|
| 676 |                 output << "(" << genType( compLitExpr->get_type(), "", pretty ) << ")";
 | 
|---|
| 677 |                 compLitExpr->get_initializer()->accept( *this );
 | 
|---|
| 678 |         }
 | 
|---|
| 679 | 
 | 
|---|
| 680 |         void CodeGenerator::visit( StmtExpr * stmtExpr ) {
 | 
|---|
| 681 |                 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
 | 
|---|
| 682 |                 output << "({" << std::endl;
 | 
|---|
| 683 |                 cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 684 |                 unsigned int numStmts = stmts.size();
 | 
|---|
| 685 |                 unsigned int i = 0;
 | 
|---|
| 686 |                 for ( Statement * stmt : stmts ) {
 | 
|---|
| 687 |                         output << indent << printLabels( stmt->get_labels() );
 | 
|---|
| 688 |                         if ( i+1 == numStmts ) {
 | 
|---|
| 689 |                                 // last statement in a statement expression needs to be handled specially -
 | 
|---|
| 690 |                                 // cannot cast to void, otherwise the expression statement has no value
 | 
|---|
| 691 |                                 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
 | 
|---|
| 692 |                                         exprStmt->get_expr()->accept( *this );
 | 
|---|
| 693 |                                         output << ";" << endl;
 | 
|---|
| 694 |                                         ++i;
 | 
|---|
| 695 |                                         break;
 | 
|---|
| 696 |                                 }
 | 
|---|
| 697 |                         }
 | 
|---|
| 698 |                         stmt->accept( *this );
 | 
|---|
| 699 |                         output << endl;
 | 
|---|
| 700 |                         if ( wantSpacing( stmt ) ) {
 | 
|---|
| 701 |                                 output << endl;
 | 
|---|
| 702 |                         } // if
 | 
|---|
| 703 |                         ++i;
 | 
|---|
| 704 |                 }
 | 
|---|
| 705 |                 cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 706 |                 output << indent << "})";
 | 
|---|
| 707 |         }
 | 
|---|
| 708 | 
 | 
|---|
| 709 |         //*** Statements
 | 
|---|
| 710 |         void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
 | 
|---|
| 711 |                 std::list<Statement*> ks = compoundStmt->get_kids();
 | 
|---|
| 712 |                 output << "{" << endl;
 | 
|---|
| 713 | 
 | 
|---|
| 714 |                 cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 715 | 
 | 
|---|
| 716 |                 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
 | 
|---|
| 717 |                         output << indent << printLabels( (*i)->get_labels() );
 | 
|---|
| 718 |                         (*i)->accept( *this );
 | 
|---|
| 719 | 
 | 
|---|
| 720 |                         output << endl;
 | 
|---|
| 721 |                         if ( wantSpacing( *i ) ) {
 | 
|---|
| 722 |                                 output << endl;
 | 
|---|
| 723 |                         } // if
 | 
|---|
| 724 |                 } // for
 | 
|---|
| 725 |                 cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 726 | 
 | 
|---|
| 727 |                 output << indent << "}";
 | 
|---|
| 728 |         }
 | 
|---|
| 729 | 
 | 
|---|
| 730 |         void CodeGenerator::visit( ExprStmt * exprStmt ) {
 | 
|---|
| 731 |                 assert( exprStmt );
 | 
|---|
| 732 |                 // cast the top-level expression to void to reduce gcc warnings.
 | 
|---|
| 733 |                 Expression * expr = new CastExpr( exprStmt->get_expr() );
 | 
|---|
| 734 |                 expr->accept( *this );
 | 
|---|
| 735 |                 output << ";";
 | 
|---|
| 736 |         }
 | 
|---|
| 737 | 
 | 
|---|
| 738 |         void CodeGenerator::visit( AsmStmt * asmStmt ) {
 | 
|---|
| 739 |                 output << "asm ";
 | 
|---|
| 740 |                 if ( asmStmt->get_voltile() ) output << "volatile ";
 | 
|---|
| 741 |                 if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
 | 
|---|
| 742 |                 output << "( ";
 | 
|---|
| 743 |                 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
 | 
|---|
| 744 |                 output << " : ";
 | 
|---|
| 745 |                 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
 | 
|---|
| 746 |                 output << " : ";
 | 
|---|
| 747 |                 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
 | 
|---|
| 748 |                 output << " : ";
 | 
|---|
| 749 |                 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
 | 
|---|
| 750 |                 if ( ! asmStmt->get_gotolabels().empty() ) {
 | 
|---|
| 751 |                         output << " : ";
 | 
|---|
| 752 |                         for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
 | 
|---|
| 753 |                                 output << *begin++;
 | 
|---|
| 754 |                                 if ( begin == asmStmt->get_gotolabels().end() ) break;
 | 
|---|
| 755 |                                 output << ", ";
 | 
|---|
| 756 |                         } // for
 | 
|---|
| 757 |                 } // if
 | 
|---|
| 758 |                 output << " );" ;
 | 
|---|
| 759 |         }
 | 
|---|
| 760 | 
 | 
|---|
| 761 |         void CodeGenerator::visit( AsmDecl * asmDecl ) {
 | 
|---|
| 762 |                 output << "asm ";
 | 
|---|
| 763 |                 AsmStmt * asmStmt = asmDecl->get_stmt();
 | 
|---|
| 764 |                 output << "( ";
 | 
|---|
| 765 |                 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
 | 
|---|
| 766 |                 output << " )" ;
 | 
|---|
| 767 |         }
 | 
|---|
| 768 | 
 | 
|---|
| 769 |         void CodeGenerator::visit( IfStmt * ifStmt ) {
 | 
|---|
| 770 |                 output << "if ( ";
 | 
|---|
| 771 |                 ifStmt->get_condition()->accept( *this );
 | 
|---|
| 772 |                 output << " ) ";
 | 
|---|
| 773 | 
 | 
|---|
| 774 |                 ifStmt->get_thenPart()->accept( *this );
 | 
|---|
| 775 | 
 | 
|---|
| 776 |                 if ( ifStmt->get_elsePart() != 0) {
 | 
|---|
| 777 |                         output << " else ";
 | 
|---|
| 778 |                         ifStmt->get_elsePart()->accept( *this );
 | 
|---|
| 779 |                 } // if
 | 
|---|
| 780 |         }
 | 
|---|
| 781 | 
 | 
|---|
| 782 |         void CodeGenerator::visit( SwitchStmt * switchStmt ) {
 | 
|---|
| 783 |                 output << "switch ( " ;
 | 
|---|
| 784 |                 switchStmt->get_condition()->accept( *this );
 | 
|---|
| 785 |                 output << " ) ";
 | 
|---|
| 786 | 
 | 
|---|
| 787 |                 output << "{" << std::endl;
 | 
|---|
| 788 |                 cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 789 |                 acceptAll( switchStmt->get_statements(), *this );
 | 
|---|
| 790 |                 cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 791 |                 output << indent << "}";
 | 
|---|
| 792 |         }
 | 
|---|
| 793 | 
 | 
|---|
| 794 |         void CodeGenerator::visit( CaseStmt * caseStmt ) {
 | 
|---|
| 795 |                 output << indent;
 | 
|---|
| 796 |                 if ( caseStmt->isDefault()) {
 | 
|---|
| 797 |                         output << "default";
 | 
|---|
| 798 |                 } else {
 | 
|---|
| 799 |                         output << "case ";
 | 
|---|
| 800 |                         caseStmt->get_condition()->accept( *this );
 | 
|---|
| 801 |                 } // if
 | 
|---|
| 802 |                 output << ":\n";
 | 
|---|
| 803 | 
 | 
|---|
| 804 |                 std::list<Statement *> sts = caseStmt->get_statements();
 | 
|---|
| 805 | 
 | 
|---|
| 806 |                 cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 807 |                 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
 | 
|---|
| 808 |                         output << indent << printLabels( (*i)->get_labels() )  ;
 | 
|---|
| 809 |                         (*i)->accept( *this );
 | 
|---|
| 810 |                         output << endl;
 | 
|---|
| 811 |                 } // for
 | 
|---|
| 812 |                 cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 813 |         }
 | 
|---|
| 814 | 
 | 
|---|
| 815 |         void CodeGenerator::visit( BranchStmt * branchStmt ) {
 | 
|---|
| 816 |                 switch ( branchStmt->get_type()) {
 | 
|---|
| 817 |                   case BranchStmt::Goto:
 | 
|---|
| 818 |                         if ( ! branchStmt->get_target().empty() )
 | 
|---|
| 819 |                                 output << "goto " << branchStmt->get_target();
 | 
|---|
| 820 |                         else {
 | 
|---|
| 821 |                                 if ( branchStmt->get_computedTarget() != 0 ) {
 | 
|---|
| 822 |                                         output << "goto *";
 | 
|---|
| 823 |                                         branchStmt->get_computedTarget()->accept( *this );
 | 
|---|
| 824 |                                 } // if
 | 
|---|
| 825 |                         } // if
 | 
|---|
| 826 |                         break;
 | 
|---|
| 827 |                   case BranchStmt::Break:
 | 
|---|
| 828 |                         output << "break";
 | 
|---|
| 829 |                         break;
 | 
|---|
| 830 |                   case BranchStmt::Continue:
 | 
|---|
| 831 |                         output << "continue";
 | 
|---|
| 832 |                         break;
 | 
|---|
| 833 |                 } // switch
 | 
|---|
| 834 |                 output << ";";
 | 
|---|
| 835 |         }
 | 
|---|
| 836 | 
 | 
|---|
| 837 | 
 | 
|---|
| 838 |         void CodeGenerator::visit( ReturnStmt * returnStmt ) {
 | 
|---|
| 839 |                 output << "return ";
 | 
|---|
| 840 |                 maybeAccept( returnStmt->get_expr(), *this );
 | 
|---|
| 841 |                 output << ";";
 | 
|---|
| 842 |         }
 | 
|---|
| 843 | 
 | 
|---|
| 844 |         void CodeGenerator::visit( WhileStmt * whileStmt ) {
 | 
|---|
| 845 |                 if ( whileStmt->get_isDoWhile() ) {
 | 
|---|
| 846 |                         output << "do" ;
 | 
|---|
| 847 |                 } else {
 | 
|---|
| 848 |                         output << "while (" ;
 | 
|---|
| 849 |                         whileStmt->get_condition()->accept( *this );
 | 
|---|
| 850 |                         output << ")";
 | 
|---|
| 851 |                 } // if
 | 
|---|
| 852 |                 output << " ";
 | 
|---|
| 853 | 
 | 
|---|
| 854 |                 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
 | 
|---|
| 855 |                 whileStmt->get_body()->accept( *this );
 | 
|---|
| 856 | 
 | 
|---|
| 857 |                 output << indent;
 | 
|---|
| 858 | 
 | 
|---|
| 859 |                 if ( whileStmt->get_isDoWhile() ) {
 | 
|---|
| 860 |                         output << " while (" ;
 | 
|---|
| 861 |                         whileStmt->get_condition()->accept( *this );
 | 
|---|
| 862 |                         output << ");";
 | 
|---|
| 863 |                 } // if
 | 
|---|
| 864 |         }
 | 
|---|
| 865 | 
 | 
|---|
| 866 |         void CodeGenerator::visit( ForStmt * forStmt ) {
 | 
|---|
| 867 |                 // initialization is always hoisted, so don't bother doing anything with that
 | 
|---|
| 868 |                 output << "for (;";
 | 
|---|
| 869 | 
 | 
|---|
| 870 |                 if ( forStmt->get_condition() != 0 ) {
 | 
|---|
| 871 |                         forStmt->get_condition()->accept( *this );
 | 
|---|
| 872 |                 } // if
 | 
|---|
| 873 |                 output << ";";
 | 
|---|
| 874 | 
 | 
|---|
| 875 |                 if ( forStmt->get_increment() != 0 ) {
 | 
|---|
| 876 |                         // cast the top-level expression to void to reduce gcc warnings.
 | 
|---|
| 877 |                         Expression * expr = new CastExpr( forStmt->get_increment() );
 | 
|---|
| 878 |                         expr->accept( *this );
 | 
|---|
| 879 |                 } // if
 | 
|---|
| 880 |                 output << ") ";
 | 
|---|
| 881 | 
 | 
|---|
| 882 |                 if ( forStmt->get_body() != 0 ) {
 | 
|---|
| 883 |                         output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
 | 
|---|
| 884 |                         forStmt->get_body()->accept( *this );
 | 
|---|
| 885 |                 } // if
 | 
|---|
| 886 |         }
 | 
|---|
| 887 | 
 | 
|---|
| 888 |         void CodeGenerator::visit( NullStmt * nullStmt ) {
 | 
|---|
| 889 |                 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
 | 
|---|
| 890 |                 output << "/* null statement */ ;";
 | 
|---|
| 891 |         }
 | 
|---|
| 892 | 
 | 
|---|
| 893 |         void CodeGenerator::visit( DeclStmt * declStmt ) {
 | 
|---|
| 894 |                 declStmt->get_decl()->accept( *this );
 | 
|---|
| 895 | 
 | 
|---|
| 896 |                 if ( doSemicolon( declStmt->get_decl() ) ) {
 | 
|---|
| 897 |                         output << ";";
 | 
|---|
| 898 |                 } // if
 | 
|---|
| 899 |         }
 | 
|---|
| 900 | 
 | 
|---|
| 901 |         void CodeGenerator::handleStorageClass( Declaration * decl ) {
 | 
|---|
| 902 |                 switch ( decl->get_storageClass() ) {
 | 
|---|
| 903 |                   case DeclarationNode::Extern:
 | 
|---|
| 904 |                         output << "extern ";
 | 
|---|
| 905 |                         break;
 | 
|---|
| 906 |                   case DeclarationNode::Static:
 | 
|---|
| 907 |                         output << "static ";
 | 
|---|
| 908 |                         break;
 | 
|---|
| 909 |                   case DeclarationNode::Auto:
 | 
|---|
| 910 |                         // silently drop storage class
 | 
|---|
| 911 |                         break;
 | 
|---|
| 912 |                   case DeclarationNode::Register:
 | 
|---|
| 913 |                         output << "register ";
 | 
|---|
| 914 |                         break;
 | 
|---|
| 915 |                   case DeclarationNode::Inline:
 | 
|---|
| 916 |                         output << "inline ";
 | 
|---|
| 917 |                         break;
 | 
|---|
| 918 |                   case DeclarationNode::Fortran:
 | 
|---|
| 919 |                         output << "fortran ";
 | 
|---|
| 920 |                         break;
 | 
|---|
| 921 |                   case DeclarationNode::Noreturn:
 | 
|---|
| 922 |                         output << "_Noreturn ";
 | 
|---|
| 923 |                         break;
 | 
|---|
| 924 |                   case DeclarationNode::Threadlocal:
 | 
|---|
| 925 |                         output << "_Thread_local ";
 | 
|---|
| 926 |                         break;
 | 
|---|
| 927 |                   case DeclarationNode::NoStorageClass:
 | 
|---|
| 928 |                         break;
 | 
|---|
| 929 |                 } // switch
 | 
|---|
| 930 |         }
 | 
|---|
| 931 | 
 | 
|---|
| 932 |         std::string genName( DeclarationWithType * decl ) {
 | 
|---|
| 933 |                 CodeGen::OperatorInfo opInfo;
 | 
|---|
| 934 |                 if ( operatorLookup( decl->get_name(), opInfo ) ) {
 | 
|---|
| 935 |                         return opInfo.outputName;
 | 
|---|
| 936 |                 } else {
 | 
|---|
| 937 |                         return decl->get_name();
 | 
|---|
| 938 |                 } // if
 | 
|---|
| 939 |         }
 | 
|---|
| 940 | } // namespace CodeGen
 | 
|---|
| 941 | 
 | 
|---|
| 942 | // Local Variables: //
 | 
|---|
| 943 | // tab-width: 4 //
 | 
|---|
| 944 | // mode: c++ //
 | 
|---|
| 945 | // compile-command: "make install" //
 | 
|---|
| 946 | // End: //
 | 
|---|