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