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