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