Changeset 17cd4eb for translator/CodeGen
- Timestamp:
- Jan 7, 2015, 6:04:42 PM (11 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 0b8cd722
- Parents:
- d9a0e76
- Location:
- translator/CodeGen
- Files:
-
- 5 edited
-
CodeGenerator2.cc (modified) (1 diff)
-
CodeGenerator2.h (modified) (1 diff)
-
GenType.cc (modified) (2 diffs)
-
GenType.h (modified) (2 diffs)
-
Generate.cc (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
translator/CodeGen/CodeGenerator2.cc
rd9a0e76 r17cd4eb 20 20 21 21 namespace CodeGen { 22 23 int CodeGenerator2::tabsize = 4; 24 25 CodeGenerator2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before(os), after() { } 26 27 CodeGenerator2::CodeGenerator2(std::ostream &os, std::string init, int indent, bool infunp ) 28 : cur_indent( indent ), insideFunction( infunp ), before(os), after() 29 { 30 //before << std::string( init ); 31 } 32 33 CodeGenerator2::CodeGenerator2(std::ostream &os, char *init, int indent, bool infunp ) 34 : cur_indent( indent ), insideFunction( infunp ), before(os), after() 35 { 36 //before << std::string( init ); 37 } 38 39 CodeGenerator2::~CodeGenerator2() { 40 after.freeze( false ); 41 } 42 43 string 44 mangleName( DeclarationWithType *decl ) { 45 if( decl->get_mangleName() != "" ) { 46 return decl->get_mangleName(); 47 } else { 48 return decl->get_name(); 49 } 50 } 51 52 //*** Declarations 53 void CodeGenerator2::visit(FunctionDecl *functionDecl){ 54 55 handleStorageClass( functionDecl ); 56 before << genType( functionDecl->get_functionType(), mangleName( functionDecl ) ); 57 58 // how to get this to the Functype? 59 std::list< Declaration * > olds = functionDecl->get_oldDecls(); 60 if( ! olds.empty() ) { 61 before << " /* function has old declaration */"; 62 } 63 64 // acceptAll( functionDecl->get_oldDecls(), *this ); 65 if( functionDecl->get_statements() ) { 66 functionDecl->get_statements()->accept(*this); 67 } 68 } 69 70 void CodeGenerator2::visit(ObjectDecl *objectDecl){ 71 72 handleStorageClass( objectDecl ); 73 before << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 22 int CodeGenerator2::tabsize = 4; 23 24 CodeGenerator2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before( os ), after() { } 25 26 CodeGenerator2::CodeGenerator2( std::ostream &os, std::string init, int indent, bool infunp ) 27 : cur_indent( indent ), insideFunction( infunp ), before( os ) 28 { 29 //before << std::string( init ); 30 } 31 32 CodeGenerator2::CodeGenerator2( std::ostream &os, char *init, int indent, bool infunp ) 33 : cur_indent( indent ), insideFunction( infunp ), before( os ) 34 { 35 //before << std::string( init ); 36 } 37 38 string mangleName( DeclarationWithType *decl ) { 39 if ( decl->get_mangleName() != "" ) { 40 return decl->get_mangleName(); 41 } else { 42 return decl->get_name(); 43 } // if 44 } 45 46 //*** Declarations 47 void CodeGenerator2::visit( FunctionDecl *functionDecl ) { 48 handleStorageClass( functionDecl ); 49 before << genType( functionDecl->get_functionType(), mangleName( functionDecl ) ); 50 51 // how to get this to the Functype? 52 std::list< Declaration * > olds = functionDecl->get_oldDecls(); 53 if ( ! olds.empty() ) { 54 before << " /* function has old declaration */"; 55 } // if 56 57 // acceptAll( functionDecl->get_oldDecls(), *this ); 58 if ( functionDecl->get_statements() ) { 59 functionDecl->get_statements()->accept(*this ); 60 } // if 61 } 62 63 void CodeGenerator2::visit( ObjectDecl *objectDecl ) { 64 handleStorageClass( objectDecl ); 65 before << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 74 66 75 if( objectDecl->get_init() ) { 76 before << " = "; 77 objectDecl->get_init()->accept( *this ); 78 } 79 if( objectDecl->get_bitfieldWidth() ) { 80 before << ":"; 81 objectDecl->get_bitfieldWidth()->accept( *this ); 82 } 83 } 84 85 void 86 CodeGenerator2::handleAggregate( AggregateDecl *aggDecl ) 87 { 88 if ( aggDecl->get_name() != "" ) 89 before << aggDecl->get_name(); 67 if ( objectDecl->get_init() ) { 68 before << " = "; 69 objectDecl->get_init()->accept( *this ); 70 } // if 71 if ( objectDecl->get_bitfieldWidth() ) { 72 before << ":"; 73 objectDecl->get_bitfieldWidth()->accept( *this ); 74 } // if 75 } 76 77 void CodeGenerator2::handleAggregate( AggregateDecl *aggDecl ) { 78 if ( aggDecl->get_name() != "" ) 79 before << aggDecl->get_name(); 90 80 91 std::list< Declaration * > &memb = aggDecl->get_members(); 92 93 if( ! memb.empty() ){ 94 before << endl << string(cur_indent, ' ') << "{" << endl; 95 96 cur_indent += CodeGenerator2::tabsize; 97 for(std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++){ 98 before << string(cur_indent, ' '); 99 (*i)->accept(*this); 100 before << ";" << endl; 101 } 102 103 cur_indent -= CodeGenerator2::tabsize; 104 105 before << string(cur_indent, ' ') << "}"; 106 } 107 } 108 109 void CodeGenerator2::visit( StructDecl *structDecl ){ 110 before << "struct "; 111 handleAggregate( structDecl ); 112 } 113 114 void 115 CodeGenerator2::visit(UnionDecl *aggregateDecl) 116 { 117 before << "union "; 118 handleAggregate( aggregateDecl ); 119 } 120 121 void 122 CodeGenerator2::visit(EnumDecl *aggDecl) 123 { 124 before << "enum "; 125 126 if ( aggDecl->get_name() != "" ) 127 before << aggDecl->get_name(); 81 std::list< Declaration * > &memb = aggDecl->get_members(); 82 83 if ( ! memb.empty() ) { 84 before << endl << string( cur_indent, ' ' ) << "{" << endl; 85 86 cur_indent += CodeGenerator2::tabsize; 87 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 88 before << string( cur_indent, ' ' ); 89 (*i)->accept(*this ); 90 before << ";" << endl; 91 } 92 93 cur_indent -= CodeGenerator2::tabsize; 94 95 before << string( cur_indent, ' ' ) << "}"; 96 } // if 97 } 98 99 void CodeGenerator2::visit( StructDecl *structDecl ) { 100 before << "struct "; 101 handleAggregate( structDecl ); 102 } 103 104 void CodeGenerator2::visit( UnionDecl *aggregateDecl ) { 105 before << "union "; 106 handleAggregate( aggregateDecl ); 107 } 108 109 void CodeGenerator2::visit( EnumDecl *aggDecl ) { 110 before << "enum "; 111 112 if ( aggDecl->get_name() != "" ) 113 before << aggDecl->get_name(); 128 114 129 std::list< Declaration* > &memb = aggDecl->get_members(); 130 131 if( ! memb.empty() ){ 132 before << endl << "{" << endl; 133 134 cur_indent += CodeGenerator2::tabsize; 135 for(std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++){ 136 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 137 assert( obj ); 138 before << string(cur_indent, ' ') << mangleName( obj ); 139 if( obj->get_init() ) { 140 before << " = "; 141 obj->get_init()->accept(*this); 115 std::list< Declaration* > &memb = aggDecl->get_members(); 116 117 if ( ! memb.empty() ) { 118 before << endl << "{" << endl; 119 120 cur_indent += CodeGenerator2::tabsize; 121 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 122 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 123 assert( obj ); 124 before << string( cur_indent, ' ' ) << mangleName( obj ); 125 if ( obj->get_init() ) { 126 before << " = "; 127 obj->get_init()->accept(*this ); 128 } // if 129 before << "," << endl; 130 } 131 132 cur_indent -= CodeGenerator2::tabsize; 133 134 before << "}" << endl; 135 } // if 136 } 137 138 void CodeGenerator2::visit( ContextDecl *aggregateDecl ) {} 139 140 void CodeGenerator2::visit( TypedefDecl *typeDecl ) { 141 before << "typedef "; 142 before << genType( typeDecl->get_base(), typeDecl->get_name() ); 143 } 144 145 void CodeGenerator2::visit( TypeDecl *typeDecl ) { 146 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, 147 // still to be done 148 before << "extern unsigned long " << typeDecl->get_name(); 149 if ( typeDecl->get_base() ) { 150 before << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )"; 151 } // if 152 } 153 154 void CodeGenerator2::visit( SingleInit *init ) { 155 init->get_value()->accept( *this ); 156 } 157 158 void CodeGenerator2::visit( ListInit *init ) { 159 before << "{ "; 160 genCommaList( init->begin_initializers(), init->end_initializers() ); 161 before << " }"; 162 } 163 164 void CodeGenerator2::visit( Constant *constant ) { 165 before << constant->get_value() ; 166 } 167 168 //*** Expressions 169 void CodeGenerator2::visit( ApplicationExpr *applicationExpr ) { 170 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 171 OperatorInfo opInfo; 172 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { 173 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); 174 switch ( opInfo.type ) { 175 case OT_PREFIXASSIGN: 176 case OT_POSTFIXASSIGN: 177 case OT_INFIXASSIGN: 178 { 179 assert( arg != applicationExpr->get_args().end() ); 180 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 181 182 *arg = addrExpr->get_arg(); 183 } else { 184 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 185 newExpr->get_args().push_back( *arg ); 186 *arg = newExpr; 187 } // if 188 break; 189 } 190 191 default: 192 // do nothing 193 ; 194 } 195 196 switch ( opInfo.type ) { 197 case OT_INDEX: 198 assert( applicationExpr->get_args().size() == 2 ); 199 (*arg++)->accept( *this ); 200 before << "["; 201 (*arg)->accept( *this ); 202 before << "]"; 203 break; 204 205 case OT_CALL: 206 // there are no intrinsic definitions of the function call operator 207 assert( false ); 208 break; 209 210 case OT_PREFIX: 211 case OT_PREFIXASSIGN: 212 assert( applicationExpr->get_args().size() == 1 ); 213 before << "("; 214 before << opInfo.symbol; 215 (*arg)->accept( *this ); 216 before << ")"; 217 break; 218 219 case OT_POSTFIX: 220 case OT_POSTFIXASSIGN: 221 assert( applicationExpr->get_args().size() == 1 ); 222 (*arg)->accept( *this ); 223 before << opInfo.symbol; 224 break; 225 226 case OT_INFIX: 227 case OT_INFIXASSIGN: 228 assert( applicationExpr->get_args().size() == 2 ); 229 before << "("; 230 (*arg++)->accept( *this ); 231 before << opInfo.symbol; 232 (*arg)->accept( *this ); 233 before << ")"; 234 break; 235 236 case OT_CONSTANT: 237 // there are no intrinsic definitions of 0 or 1 as functions 238 assert( false ); 239 } 240 } else { 241 varExpr->accept( *this ); 242 before << "("; 243 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 244 before << ")"; 245 } // if 246 } else { 247 applicationExpr->get_function()->accept( *this ); 248 before << "("; 249 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 250 before << ")"; 251 } // if 252 } 253 254 void CodeGenerator2::visit( UntypedExpr *untypedExpr ) { 255 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 256 OperatorInfo opInfo; 257 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { 258 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); 259 switch ( opInfo.type ) { 260 case OT_INDEX: 261 assert( untypedExpr->get_args().size() == 2 ); 262 (*arg++)->accept( *this ); 263 before << "["; 264 (*arg)->accept( *this ); 265 before << "]"; 266 break; 267 268 case OT_CALL: 269 assert( false ); 270 break; 271 272 case OT_PREFIX: 273 case OT_PREFIXASSIGN: 274 assert( untypedExpr->get_args().size() == 1 ); 275 before << "("; 276 before << opInfo.symbol; 277 (*arg)->accept( *this ); 278 before << ")"; 279 break; 280 281 case OT_POSTFIX: 282 case OT_POSTFIXASSIGN: 283 assert( untypedExpr->get_args().size() == 1 ); 284 (*arg)->accept( *this ); 285 before << opInfo.symbol; 286 break; 287 288 case OT_INFIX: 289 case OT_INFIXASSIGN: 290 assert( untypedExpr->get_args().size() == 2 ); 291 before << "("; 292 (*arg++)->accept( *this ); 293 before << opInfo.symbol; 294 (*arg)->accept( *this ); 295 before << ")"; 296 break; 297 298 case OT_CONSTANT: 299 // there are no intrinsic definitions of 0 or 1 as functions 300 assert( false ); 301 } 302 } else { 303 nameExpr->accept( *this ); 304 before << "("; 305 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 306 before << ")"; 307 } // if 308 } else { 309 untypedExpr->get_function()->accept( *this ); 310 before << "("; 311 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 312 before << ")"; 313 } // if 314 } 315 316 void CodeGenerator2::visit( NameExpr *nameExpr ) { 317 OperatorInfo opInfo; 318 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { 319 assert( opInfo.type == OT_CONSTANT ); 320 before << opInfo.symbol; 321 } else { 322 before << nameExpr->get_name(); 323 } // if 324 } 325 326 void CodeGenerator2::visit( AddressExpr *addressExpr ) { 327 before << "(&"; 328 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 329 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 330 before << mangleName( variableExpr->get_var() ); 331 } else { 332 addressExpr->get_arg()->accept( *this ); 333 } // if 334 before << ")"; 335 } 336 337 void CodeGenerator2::visit( CastExpr *castExpr ) { 338 before << "(("; 339 if ( castExpr->get_results().empty() ) { 340 before << "void" ; 341 } else { 342 before << genType( castExpr->get_results().front(), "" ); 343 } // if 344 before << ")"; 345 castExpr->get_arg()->accept( *this ); 346 before << ")"; 347 } 348 349 void CodeGenerator2::visit( UntypedMemberExpr *memberExpr ) { 350 assert( false ); 351 } 352 353 void CodeGenerator2::visit( MemberExpr *memberExpr ) { 354 memberExpr->get_aggregate()->accept( *this ); 355 before << "." << mangleName( memberExpr->get_member() ); 356 } 357 358 void CodeGenerator2::visit( VariableExpr *variableExpr ) { 359 OperatorInfo opInfo; 360 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { 361 before << opInfo.symbol; 362 } else { 363 before << mangleName( variableExpr->get_var() ); 364 } // if 365 } 366 367 void CodeGenerator2::visit( ConstantExpr *constantExpr ) { 368 assert( constantExpr->get_constant() ); 369 constantExpr->get_constant()->accept( *this ); 370 } 371 372 void CodeGenerator2::visit( SizeofExpr *sizeofExpr ) { 373 before << "sizeof("; 374 if ( sizeofExpr->get_isType() ) { 375 before << genType( sizeofExpr->get_type(), "" ); 376 } else { 377 sizeofExpr->get_expr()->accept( *this ); 378 } // if 379 before << ")"; 380 } 381 382 void CodeGenerator2::visit( LogicalExpr *logicalExpr ) { 383 before << "("; 384 logicalExpr->get_arg1()->accept( *this ); 385 if ( logicalExpr->get_isAnd() ) { 386 before << " && "; 387 } else { 388 before << " || "; 389 } // if 390 logicalExpr->get_arg2()->accept( *this ); 391 before << ")"; 392 } 393 394 void CodeGenerator2::visit( ConditionalExpr *conditionalExpr ) { 395 before << "("; 396 conditionalExpr->get_arg1()->accept( *this ); 397 before << " ? "; 398 conditionalExpr->get_arg2()->accept( *this ); 399 before << " : "; 400 conditionalExpr->get_arg3()->accept( *this ); 401 before << ")"; 402 } 403 404 void CodeGenerator2::visit( CommaExpr *commaExpr ) { 405 before << "("; 406 commaExpr->get_arg1()->accept( *this ); 407 before << " , "; 408 commaExpr->get_arg2()->accept( *this ); 409 before << ")"; 410 } 411 412 void CodeGenerator2::visit( TupleExpr *tupleExpr ) {} 413 414 void CodeGenerator2::visit( TypeExpr *typeExpr ) {} 415 416 417 //*** Statements 418 void CodeGenerator2::visit( CompoundStmt *compoundStmt ) { 419 std::list<Statement*> ks = compoundStmt->get_kids(); 420 421 before << endl << string( cur_indent, ' ' ) << "{" << endl; 422 423 cur_indent += CodeGenerator2::tabsize; 424 425 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) { 426 before << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ; 427 (*i)->accept(*this ); 428 shift_left(); 429 before << endl; 142 430 } 143 before << "," << endl; 144 } 145 146 cur_indent -= CodeGenerator2::tabsize; 147 148 before << "}" << endl; 149 } 150 } 151 152 void 153 CodeGenerator2::visit(ContextDecl *aggregateDecl) 154 { 155 } 156 157 void 158 CodeGenerator2::visit(TypedefDecl *typeDecl) 159 { 160 before << "typedef "; 161 before << genType( typeDecl->get_base(), typeDecl->get_name() ); 162 } 163 164 void 165 CodeGenerator2::visit(TypeDecl *typeDecl) 166 { 167 // really, we should mutate this into something that isn't a TypeDecl 168 // but that requires large-scale changes, still to be done 169 before << "extern unsigned long " << typeDecl->get_name(); 170 if( typeDecl->get_base() ) { 171 before << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )"; 172 } 173 } 174 175 //*** Initializer 176 void 177 CodeGenerator2::visit(SingleInit *init) 178 { 179 init->get_value()->accept( *this ); 180 } 181 182 void 183 CodeGenerator2::visit(ListInit *init) 184 { 185 before << "{ "; 186 genCommaList( init->begin_initializers(), init->end_initializers() ); 187 before << " }"; 188 } 189 190 //*** Constant 191 void CodeGenerator2::visit(Constant *constant) { 192 before << constant->get_value() ; 193 } 194 195 //*** Expressions 196 void 197 CodeGenerator2::visit(ApplicationExpr *applicationExpr) 198 { 199 if( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 200 OperatorInfo opInfo; 201 if( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { 202 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); 203 switch( opInfo.type ) { 204 case OT_PREFIXASSIGN: 205 case OT_POSTFIXASSIGN: 206 case OT_INFIXASSIGN: 207 { 208 assert( arg != applicationExpr->get_args().end() ); 209 if( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 210 211 *arg = addrExpr->get_arg(); 212 } else { 213 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 214 newExpr->get_args().push_back( *arg ); 215 *arg = newExpr; 216 } 217 break; 218 } 219 220 default: 221 // do nothing 222 ; 223 } 224 225 switch( opInfo.type ) { 226 case OT_INDEX: 227 assert( applicationExpr->get_args().size() == 2 ); 228 (*arg++)->accept( *this ); 229 before << "["; 230 (*arg)->accept( *this ); 231 before << "]"; 232 break; 233 234 case OT_CALL: 235 // there are no intrinsic definitions of the function call operator 236 assert( false ); 237 break; 238 239 case OT_PREFIX: 240 case OT_PREFIXASSIGN: 241 assert( applicationExpr->get_args().size() == 1 ); 242 before << "("; 243 before << opInfo.symbol; 244 (*arg)->accept( *this ); 245 before << ")"; 246 break; 247 248 case OT_POSTFIX: 249 case OT_POSTFIXASSIGN: 250 assert( applicationExpr->get_args().size() == 1 ); 251 (*arg)->accept( *this ); 252 before << opInfo.symbol; 253 break; 254 255 case OT_INFIX: 256 case OT_INFIXASSIGN: 257 assert( applicationExpr->get_args().size() == 2 ); 258 before << "("; 259 (*arg++)->accept( *this ); 260 before << opInfo.symbol; 261 (*arg)->accept( *this ); 262 before << ")"; 263 break; 264 265 case OT_CONSTANT: 266 // there are no intrinsic definitions of 0 or 1 as functions 267 assert( false ); 268 } 269 } else { 270 varExpr->accept( *this ); 271 before << "("; 272 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 273 before << ")"; 274 } 275 } else { 276 applicationExpr->get_function()->accept( *this ); 277 before << "("; 278 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 279 before << ")"; 280 } 281 } 282 283 void 284 CodeGenerator2::visit(UntypedExpr *untypedExpr) 285 { 286 if( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 287 OperatorInfo opInfo; 288 if( operatorLookup( nameExpr->get_name(), opInfo ) ) { 289 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); 290 switch( opInfo.type ) { 291 case OT_INDEX: 292 assert( untypedExpr->get_args().size() == 2 ); 293 (*arg++)->accept( *this ); 294 before << "["; 295 (*arg)->accept( *this ); 296 before << "]"; 297 break; 298 299 case OT_CALL: 300 assert( false ); 301 break; 302 303 case OT_PREFIX: 304 case OT_PREFIXASSIGN: 305 assert( untypedExpr->get_args().size() == 1 ); 306 before << "("; 307 before << opInfo.symbol; 308 (*arg)->accept( *this ); 309 before << ")"; 310 break; 311 312 case OT_POSTFIX: 313 case OT_POSTFIXASSIGN: 314 assert( untypedExpr->get_args().size() == 1 ); 315 (*arg)->accept( *this ); 316 before << opInfo.symbol; 317 break; 318 319 case OT_INFIX: 320 case OT_INFIXASSIGN: 321 assert( untypedExpr->get_args().size() == 2 ); 322 before << "("; 323 (*arg++)->accept( *this ); 324 before << opInfo.symbol; 325 (*arg)->accept( *this ); 326 before << ")"; 327 break; 328 329 case OT_CONSTANT: 330 // there are no intrinsic definitions of 0 or 1 as functions 331 assert( false ); 332 } 333 } else { 334 nameExpr->accept( *this ); 335 before << "("; 336 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 337 before << ")"; 338 } 339 } else { 340 untypedExpr->get_function()->accept( *this ); 341 before << "("; 342 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 343 before << ")"; 344 } 345 } 346 347 void 348 CodeGenerator2::visit(NameExpr *nameExpr) 349 { 350 OperatorInfo opInfo; 351 if( operatorLookup( nameExpr->get_name(), opInfo ) ) { 352 assert( opInfo.type == OT_CONSTANT ); 353 before << opInfo.symbol; 354 } else { 355 before << nameExpr->get_name(); 356 } 357 } 358 359 void 360 CodeGenerator2::visit(AddressExpr *addressExpr) 361 { 362 before << "(&"; 363 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 364 if( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 365 before << mangleName( variableExpr->get_var() ); 366 } else { 367 addressExpr->get_arg()->accept( *this ); 368 } 369 before << ")"; 370 } 371 372 void 373 CodeGenerator2::visit(CastExpr *castExpr) 374 { 375 before << "(("; 376 if( castExpr->get_results().empty() ) { 377 before << "void" ; 378 } else { 379 before << genType( castExpr->get_results().front(), "" ); 380 } 381 before << ")"; 382 castExpr->get_arg()->accept( *this ); 383 before << ")"; 384 } 385 386 void 387 CodeGenerator2::visit(UntypedMemberExpr *memberExpr) 388 { 389 assert( false ); 390 } 391 392 void 393 CodeGenerator2::visit(MemberExpr *memberExpr) 394 { 395 memberExpr->get_aggregate()->accept( *this ); 396 before << "." << mangleName( memberExpr->get_member() ); 397 } 398 399 void 400 CodeGenerator2::visit(VariableExpr *variableExpr) 401 { 402 OperatorInfo opInfo; 403 if( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { 404 before << opInfo.symbol; 405 } else { 406 before << mangleName( variableExpr->get_var() ); 407 } 408 } 409 410 void 411 CodeGenerator2::visit(ConstantExpr *constantExpr) 412 { 413 assert( constantExpr->get_constant() ); 414 constantExpr->get_constant()->accept( *this ); 415 } 416 417 void 418 CodeGenerator2::visit(SizeofExpr *sizeofExpr) 419 { 420 before << "sizeof("; 421 if( sizeofExpr->get_isType() ) { 422 before << genType( sizeofExpr->get_type(), "" ); 423 } else { 424 sizeofExpr->get_expr()->accept( *this ); 425 } 426 before << ")"; 427 } 428 429 void 430 CodeGenerator2::visit(LogicalExpr *logicalExpr) 431 { 432 before << "("; 433 logicalExpr->get_arg1()->accept( *this ); 434 if( logicalExpr->get_isAnd() ) { 435 before << " && "; 436 } else { 437 before << " || "; 438 } 439 logicalExpr->get_arg2()->accept( *this ); 440 before << ")"; 441 } 442 443 void 444 CodeGenerator2::visit(ConditionalExpr *conditionalExpr) 445 { 446 before << "("; 447 conditionalExpr->get_arg1()->accept( *this ); 448 before << " ? "; 449 conditionalExpr->get_arg2()->accept( *this ); 450 before << " : "; 451 conditionalExpr->get_arg3()->accept( *this ); 452 before << ")"; 453 } 454 455 void 456 CodeGenerator2::visit(CommaExpr *commaExpr) 457 { 458 before << "("; 459 commaExpr->get_arg1()->accept( *this ); 460 before << " , "; 461 commaExpr->get_arg2()->accept( *this ); 462 before << ")"; 463 } 464 465 void 466 CodeGenerator2::visit(TupleExpr *tupleExpr) 467 { 468 } 469 470 void 471 CodeGenerator2::visit(TypeExpr *typeExpr) 472 { 473 } 474 475 476 //*** Statements 477 void CodeGenerator2::visit(CompoundStmt *compoundStmt){ 478 std::list<Statement*> ks = compoundStmt->get_kids(); 479 480 before << endl << string(cur_indent,' ') << "{" << endl; 481 482 cur_indent += CodeGenerator2::tabsize; 483 484 for(std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++){ 485 before << string(cur_indent, ' ') << printLabels( (*i)->get_labels() ) ; 486 (*i)->accept(*this); 487 shift_left(); 488 before << endl; 489 } 490 cur_indent -= CodeGenerator2::tabsize; 491 492 before << string(cur_indent,' ') << "}" << endl; 493 } 494 495 void CodeGenerator2::visit(ExprStmt *exprStmt){ 496 if(exprStmt != 0){ 497 exprStmt->get_expr()->accept( *this ); 498 shift_left(); 499 before << ";" ; 500 } 501 } 502 503 void CodeGenerator2::visit(IfStmt *ifStmt){ 504 before << "if ("; 505 ifStmt->get_condition()->accept(*this); 506 after << ")" << endl ; 507 shift_left(); 508 509 cur_indent += CodeGenerator2::tabsize; 510 before << string(cur_indent, ' '); 511 ifStmt->get_thenPart()->accept(*this); 512 cur_indent -= CodeGenerator2::tabsize; 513 shift_left(); before << endl; 514 515 if(ifStmt->get_elsePart() != 0){ 516 before << string(cur_indent, ' ') << " else " << endl ; 517 518 cur_indent += CodeGenerator2::tabsize; 519 ifStmt->get_elsePart()->accept(*this); 520 cur_indent -= CodeGenerator2::tabsize; 521 } 522 } 523 524 void CodeGenerator2::visit(SwitchStmt *switchStmt){ 525 //before << /* "\r" << */ string(cur_indent, ' ') << CodeGenerator2::printLabels( switchStmt->get_labels() ) 526 before << "switch (" ; 527 switchStmt->get_condition()->accept(*this); 528 after << ")" << std::endl ; 529 shift_left(); 530 531 before << string(cur_indent, ' ') << "{" << std::endl; 532 cur_indent += CodeGenerator2::tabsize; 533 534 std::list< Statement * > stmts = switchStmt->get_branches(); 535 bool lastBreak = false; 536 537 // horrible, horrible hack 538 if( dynamic_cast<BranchStmt *>(stmts.back()) != 0 ) { 539 lastBreak = true; 540 stmts.pop_back(); 541 } 542 acceptAll(stmts, *this ); 543 if ( lastBreak ) { 544 Statement *st = switchStmt->get_branches().back(); 545 before << CodeGenerator2::printLabels(st->get_labels()); 546 st->accept(*this); 547 } 431 cur_indent -= CodeGenerator2::tabsize; 432 433 before << string( cur_indent, ' ' ) << "}" << endl; 434 } 435 436 void CodeGenerator2::visit( ExprStmt *exprStmt ) { 437 if ( exprStmt != 0 ) { 438 exprStmt->get_expr()->accept( *this ); 439 shift_left(); 440 before << ";" ; 441 } // if 442 } 443 444 void CodeGenerator2::visit( IfStmt *ifStmt ) { 445 before << "if ("; 446 ifStmt->get_condition()->accept(*this ); 447 after += ")\n"; 448 shift_left(); 449 450 cur_indent += CodeGenerator2::tabsize; 451 before << string( cur_indent, ' ' ); 452 ifStmt->get_thenPart()->accept(*this ); 453 cur_indent -= CodeGenerator2::tabsize; 454 shift_left(); before << endl; 455 456 if ( ifStmt->get_elsePart() != 0) { 457 before << string( cur_indent, ' ' ) << " else " << endl ; 458 459 cur_indent += CodeGenerator2::tabsize; 460 ifStmt->get_elsePart()->accept(*this ); 461 cur_indent -= CodeGenerator2::tabsize; 462 } // if 463 } 464 465 void CodeGenerator2::visit( SwitchStmt *switchStmt ) { 466 //before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( switchStmt->get_labels() ) 467 before << "switch (" ; 468 switchStmt->get_condition()->accept(*this ); 469 after += ")\n"; 470 shift_left(); 471 472 before << string( cur_indent, ' ' ) << "{" << std::endl; 473 cur_indent += CodeGenerator2::tabsize; 474 475 std::list< Statement * > stmts = switchStmt->get_branches(); 476 bool lastBreak = false; 477 478 // horrible, horrible hack 479 if ( dynamic_cast<BranchStmt *>( stmts.back()) != 0 ) { 480 lastBreak = true; 481 stmts.pop_back(); 482 } // if 483 acceptAll( stmts, *this ); 484 if ( lastBreak ) { 485 Statement *st = switchStmt->get_branches().back(); 486 before << CodeGenerator2::printLabels( st->get_labels()); 487 st->accept(*this ); 488 } // if 548 489 549 cur_indent -= CodeGenerator2::tabsize; 550 551 before << /* "\r" << */ string(cur_indent, ' ') << "}" << endl ; 552 } 553 554 void CodeGenerator2::visit(CaseStmt *caseStmt){ 555 before << string(cur_indent, ' '); 556 if (caseStmt->isDefault()) 557 before << "default " ; 558 else { 559 before << "case " ; 560 caseStmt->get_condition()->accept(*this); 561 } 562 after << ":" << std::endl ; 563 shift_left(); 564 565 std::list<Statement *> sts = caseStmt->get_statements(); 566 567 cur_indent += CodeGenerator2::tabsize; 568 for(std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++){ 569 before << /* "\r" << */ string(cur_indent, ' ') << printLabels( (*i)->get_labels() ) ; 570 (*i)->accept(*this); 571 shift_left(); 572 before << ";" << endl; 573 } 574 cur_indent -= CodeGenerator2::tabsize; 575 } 576 577 void CodeGenerator2::visit(BranchStmt *branchStmt){ 578 switch(branchStmt->get_type()){ 579 case BranchStmt::Goto: 580 if( ! branchStmt->get_target().empty() ) 581 before << "goto " << branchStmt->get_target(); 582 else { 583 if ( branchStmt->get_computedTarget() != 0 ) { 584 before << "goto *"; 585 branchStmt->get_computedTarget()->accept(*this); 490 cur_indent -= CodeGenerator2::tabsize; 491 492 before << /* "\r" << */ string( cur_indent, ' ' ) << "}" << endl ; 493 } 494 495 void CodeGenerator2::visit( CaseStmt *caseStmt ) { 496 before << string( cur_indent, ' ' ); 497 if ( caseStmt->isDefault()) 498 before << "default " ; 499 else { 500 before << "case " ; 501 caseStmt->get_condition()->accept(*this ); 502 } // if 503 after += ":\n"; 504 shift_left(); 505 506 std::list<Statement *> sts = caseStmt->get_statements(); 507 508 cur_indent += CodeGenerator2::tabsize; 509 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 510 before << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ; 511 (*i)->accept(*this ); 512 shift_left(); 513 before << ";" << endl; 586 514 } 587 } 588 break; 589 case BranchStmt::Break: 590 before << "break"; 591 break; 592 case BranchStmt::Continue: 593 before << "continue"; 594 break; 595 } 596 before << ";"; 597 } 598 599 600 void CodeGenerator2::visit(ReturnStmt *returnStmt){ 601 before << "return "; 602 603 // xxx -- check for null expression; 604 if ( returnStmt->get_expr() ){ 605 returnStmt->get_expr()->accept( *this ); 606 } 607 after << ";"; 608 } 609 610 void CodeGenerator2::visit(WhileStmt *whileStmt){ 611 if( whileStmt->get_isDoWhile() ) 612 before << "do" ; 613 else { 614 before << "while(" ; 615 whileStmt->get_condition()->accept(*this); 616 after << ")"; 617 } 618 after << "{" << endl ; 619 shift_left(); 620 621 whileStmt->get_body()->accept( *this ); 622 623 before << /* "\r" << */ string(cur_indent, ' ') << "}" ; 624 625 if( whileStmt->get_isDoWhile() ){ 626 before << " while(" ; 627 whileStmt->get_condition()->accept(*this); 628 after << ");"; 629 } 630 631 after << endl; 632 } 633 634 void CodeGenerator2::visit(ForStmt *forStmt){ 635 before << "for ("; 636 637 if( forStmt->get_initialization() != 0 ) 638 forStmt->get_initialization()->accept( *this ); 639 else 640 before << ";"; 641 shift_left(); 642 643 if( forStmt->get_condition() != 0 ) 644 forStmt->get_condition()->accept( *this ); 645 shift_left(); before << ";"; 646 647 if( forStmt->get_increment() != 0 ) 648 forStmt->get_increment()->accept( *this ); 649 shift_left(); before << ")" << endl; 650 651 if( forStmt->get_body() != 0 ){ 652 cur_indent += CodeGenerator2::tabsize; 653 before << string(cur_indent, ' ') << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() ); 654 forStmt->get_body()->accept( *this ); 655 cur_indent -= CodeGenerator2::tabsize; 656 } 657 } 658 659 void CodeGenerator2::visit(NullStmt *nullStmt){ 660 //before << /* "\r" << */ string(cur_indent, ' ') << CodeGenerator2::printLabels( nullStmt->get_labels() ); 661 before << "/* null statement */ ;"; 662 } 663 664 void CodeGenerator2::visit(DeclStmt *declStmt){ 665 declStmt->get_decl()->accept( *this ); 515 cur_indent -= CodeGenerator2::tabsize; 516 } 517 518 void CodeGenerator2::visit( BranchStmt *branchStmt ) { 519 switch ( branchStmt->get_type()) { 520 case BranchStmt::Goto: 521 if ( ! branchStmt->get_target().empty() ) 522 before << "goto " << branchStmt->get_target(); 523 else { 524 if ( branchStmt->get_computedTarget() != 0 ) { 525 before << "goto *"; 526 branchStmt->get_computedTarget()->accept(*this ); 527 } // if 528 } // if 529 break; 530 case BranchStmt::Break: 531 before << "break"; 532 break; 533 case BranchStmt::Continue: 534 before << "continue"; 535 break; 536 } 537 before << ";"; 538 } 539 540 541 void CodeGenerator2::visit( ReturnStmt *returnStmt ) { 542 before << "return "; 543 544 // xxx -- check for null expression; 545 if ( returnStmt->get_expr() ) { 546 returnStmt->get_expr()->accept( *this ); 547 } // if 548 after += ";"; 549 } 550 551 void CodeGenerator2::visit( WhileStmt *whileStmt ) { 552 if ( whileStmt->get_isDoWhile() ) 553 before << "do" ; 554 else { 555 before << "while(" ; 556 whileStmt->get_condition()->accept(*this ); 557 after += ")"; 558 } // if 559 after += "{\n"; 560 shift_left(); 561 562 whileStmt->get_body()->accept( *this ); 563 564 before << /* "\r" << */ string( cur_indent, ' ' ) << "}" ; 565 566 if ( whileStmt->get_isDoWhile() ) { 567 before << " while(" ; 568 whileStmt->get_condition()->accept(*this ); 569 after += ");"; 570 } // if 571 572 after += "\n"; 573 } 574 575 void CodeGenerator2::visit( ForStmt *forStmt ) { 576 before << "for ("; 577 578 if ( forStmt->get_initialization() != 0 ) 579 forStmt->get_initialization()->accept( *this ); 580 else 581 before << ";"; 582 shift_left(); 583 584 if ( forStmt->get_condition() != 0 ) 585 forStmt->get_condition()->accept( *this ); 586 shift_left(); before << ";"; 587 588 if ( forStmt->get_increment() != 0 ) 589 forStmt->get_increment()->accept( *this ); 590 shift_left(); before << ")" << endl; 591 592 if ( forStmt->get_body() != 0 ) { 593 cur_indent += CodeGenerator2::tabsize; 594 before << string( cur_indent, ' ' ) << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() ); 595 forStmt->get_body()->accept( *this ); 596 cur_indent -= CodeGenerator2::tabsize; 597 } // if 598 } 599 600 void CodeGenerator2::visit( NullStmt *nullStmt ) { 601 //before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( nullStmt->get_labels() ); 602 before << "/* null statement */ ;"; 603 } 604 605 void CodeGenerator2::visit( DeclStmt *declStmt ) { 606 declStmt->get_decl()->accept( *this ); 666 607 667 if( doSemicolon( declStmt->get_decl() ) ) { 668 after << ";"; 669 } 670 shift_left(); 671 } 672 673 std::string CodeGenerator2::printLabels ( std::list< Label > &l ) { 674 std::string str(""); 675 l.unique(); 676 677 for( std::list< Label >::iterator i = l.begin(); i != l.end(); i++) 678 str += *i + ": "; 679 680 return str; 681 } 682 683 void CodeGenerator2::shift_left(){ 684 before << string(after.str(), after.pcount()); 685 686 after.freeze( false ); 687 after.seekp(0); 688 } 689 690 void 691 CodeGenerator2::handleStorageClass( Declaration *decl ) 692 { 693 switch( decl->get_storageClass() ) { 694 case Declaration::NoStorageClass: 695 break; 696 case Declaration::Auto: 697 break; 698 case Declaration::Static: 699 before << "static "; 700 break; 701 case Declaration::Extern: 702 before << "extern "; 703 break; 704 case Declaration::Register: 705 before << "register "; 706 break; 707 case Declaration::Fortran: 708 before << "fortran "; 709 break; 710 } 711 } 712 608 if ( doSemicolon( declStmt->get_decl() ) ) { 609 after += ";"; 610 } // if 611 shift_left(); 612 } 613 614 std::string CodeGenerator2::printLabels( std::list< Label > &l ) { 615 std::string str( "" ); 616 l.unique(); 617 618 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ ) 619 str += *i + ": "; 620 621 return str; 622 } 623 624 void CodeGenerator2::shift_left() { 625 before << after; 626 after = ""; 627 } 628 629 void CodeGenerator2::handleStorageClass( Declaration *decl ) { 630 switch ( decl->get_storageClass() ) { 631 case Declaration::NoStorageClass: 632 break; 633 case Declaration::Auto: 634 break; 635 case Declaration::Static: 636 before << "static "; 637 break; 638 case Declaration::Extern: 639 before << "extern "; 640 break; 641 case Declaration::Register: 642 before << "register "; 643 break; 644 case Declaration::Fortran: 645 before << "fortran "; 646 break; 647 } 648 } 713 649 } // namespace CodeGen 714 715 -
translator/CodeGen/CodeGenerator2.h
rd9a0e76 r17cd4eb 10 10 11 11 namespace CodeGen { 12 class CodeGenerator2 : public Visitor { 13 public: 14 static int tabsize; 12 15 13 class CodeGenerator2 : public Visitor 14 { 15 public: 16 static int tabsize; 16 CodeGenerator2( std::ostream &os ); 17 CodeGenerator2( std::ostream &os, std::string, int indent = 0, bool infun = false ); 18 CodeGenerator2( std::ostream &os, char *, int indent = 0, bool infun = false ); 17 19 18 CodeGenerator2( std::ostream &os ); 19 CodeGenerator2(std::ostream &os, std::string, int indent = 0, bool infun = false ); 20 CodeGenerator2(std::ostream &os, char *, int indent = 0, bool infun = false ); 20 CodeGenerator2( CodeGenerator2 & ); 21 21 22 CodeGenerator2( CodeGenerator2 & ); 22 //*** Declaration 23 virtual void visit( StructDecl * ); 24 virtual void visit( FunctionDecl * ); 25 virtual void visit( ObjectDecl * ); 26 virtual void visit( UnionDecl *aggregateDecl ); 27 virtual void visit( EnumDecl *aggregateDecl ); 28 virtual void visit( ContextDecl *aggregateDecl ); 29 virtual void visit( TypedefDecl *typeDecl ); 30 virtual void visit( TypeDecl *typeDecl ); 23 31 24 virtual ~CodeGenerator2(); 32 //*** Initializer 33 virtual void visit( SingleInit * ); 34 virtual void visit( ListInit * ); 25 35 26 //*** Declaration 27 virtual void visit(StructDecl *); 28 virtual void visit(FunctionDecl *); 29 virtual void visit(ObjectDecl *); 30 virtual void visit(UnionDecl *aggregateDecl); 31 virtual void visit(EnumDecl *aggregateDecl); 32 virtual void visit(ContextDecl *aggregateDecl); 33 virtual void visit(TypedefDecl *typeDecl); 34 virtual void visit(TypeDecl *typeDecl); 36 //*** Constant 37 virtual void visit( Constant * ); 35 38 36 //*** Initializer 37 virtual void visit(SingleInit *); 38 virtual void visit(ListInit *); 39 //*** Expression 40 virtual void visit( ApplicationExpr *applicationExpr ); 41 virtual void visit( UntypedExpr *untypedExpr ); 42 virtual void visit( NameExpr *nameExpr ); 43 virtual void visit( AddressExpr *addressExpr ); 44 virtual void visit( CastExpr *castExpr ); 45 virtual void visit( UntypedMemberExpr *memberExpr ); 46 virtual void visit( MemberExpr *memberExpr ); 47 virtual void visit( VariableExpr *variableExpr ); 48 virtual void visit( ConstantExpr *constantExpr ); 49 virtual void visit( SizeofExpr *sizeofExpr ); 50 virtual void visit( LogicalExpr *logicalExpr ); 51 virtual void visit( ConditionalExpr *conditionalExpr ); 52 virtual void visit( CommaExpr *commaExpr ); 53 virtual void visit( TupleExpr *tupleExpr ); 54 virtual void visit( TypeExpr *typeExpr ); 39 55 40 //*** Constant 41 virtual void visit(Constant *); 56 //*** Statements 57 virtual void visit( CompoundStmt * ); 58 virtual void visit( ExprStmt * ); 59 virtual void visit( IfStmt * ); 60 virtual void visit( SwitchStmt * ); 61 virtual void visit( CaseStmt * ); 62 virtual void visit( BranchStmt * ); 63 virtual void visit( ReturnStmt * ); 64 virtual void visit( WhileStmt * ); 65 virtual void visit( ForStmt * ); 66 virtual void visit( NullStmt * ); 67 virtual void visit( DeclStmt * ); 42 68 43 //*** Expression 44 virtual void visit(ApplicationExpr *applicationExpr); 45 virtual void visit(UntypedExpr *untypedExpr); 46 virtual void visit(NameExpr *nameExpr); 47 virtual void visit(AddressExpr *addressExpr); 48 virtual void visit(CastExpr *castExpr); 49 virtual void visit(UntypedMemberExpr *memberExpr); 50 virtual void visit(MemberExpr *memberExpr); 51 virtual void visit(VariableExpr *variableExpr); 52 virtual void visit(ConstantExpr *constantExpr); 53 virtual void visit(SizeofExpr *sizeofExpr); 54 virtual void visit(LogicalExpr *logicalExpr); 55 virtual void visit(ConditionalExpr *conditionalExpr); 56 virtual void visit(CommaExpr *commaExpr); 57 virtual void visit(TupleExpr *tupleExpr); 58 virtual void visit(TypeExpr *typeExpr); 69 std::string get_string( void ); 70 void add_string_left( std::string s ) { before << s; } 71 void shift_left(); 72 template< class Iterator > void genCommaList( Iterator begin, Iterator end ); 73 private: 74 int cur_indent; 75 bool insideFunction; 76 std::ostream &before; 77 std::string after; 59 78 60 //*** Statements 61 virtual void visit(CompoundStmt *); 62 virtual void visit(ExprStmt *); 63 virtual void visit(IfStmt *); 64 virtual void visit(SwitchStmt *); 65 virtual void visit(CaseStmt *); 66 virtual void visit(BranchStmt *); 67 virtual void visit(ReturnStmt *); 68 virtual void visit(WhileStmt *); 69 virtual void visit(ForStmt *); 70 virtual void visit(NullStmt *); 71 virtual void visit(DeclStmt *); 72 73 std::string get_string(void); 74 void add_string_left(std::string s) { before << s; } 75 void shift_left(); 76 template< class Iterator > void genCommaList( Iterator begin, Iterator end ); 77 78 private: 79 int cur_indent; 80 bool insideFunction; 81 std::ostream &before; 82 std::ostrstream after; 83 84 static std::string printLabels ( std::list < Label > & ); 85 void handleStorageClass( Declaration *decl ); 86 void handleAggregate( AggregateDecl *aggDecl ); 87 void handleTypedef( NamedTypeDecl *namedType ); 79 static std::string printLabels ( std::list < Label > & ); 80 void handleStorageClass( Declaration *decl ); 81 void handleAggregate( AggregateDecl *aggDecl ); 82 void handleTypedef( NamedTypeDecl *namedType ); 88 83 89 84 }; 90 85 91 template< class Iterator > 92 void 93 CodeGenerator2::genCommaList( Iterator begin, Iterator end ) 94 { 95 if( begin == end ) return; 96 97 for( ;; ) { 98 (*begin++)->accept( *this ); 99 100 if( begin == end ) return; 101 102 before << ", "; 86 template< class Iterator > 87 void CodeGenerator2::genCommaList( Iterator begin, Iterator end ) { 88 if ( begin == end ) return; 89 90 for ( ;; ) { 91 (*begin++)->accept( *this ); 92 if ( begin == end ) return; 93 before << ", "; 94 } 103 95 } 104 }105 96 106 inline bool107 doSemicolon( Declaration* decl ) 108 { 109 if( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { 110 return !func->get_statements();97 inline bool doSemicolon( Declaration* decl ) { 98 if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { 99 return !func->get_statements(); 100 } 101 return true; 111 102 } 112 return true;113 }114 115 103 } // namespace CodeGen 116 104 -
translator/CodeGen/GenType.cc
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: GenType.cc,v 1.6 2005/08/29 20:14:12 rcbilson Exp $5 *6 */7 8 1 #include <strstream> 9 2 #include <cassert> … … 16 9 17 10 namespace CodeGen { 11 class GenType : public Visitor { 12 public: 13 GenType( const std::string &typeString ); 14 std::string get_typeString() const { return typeString; } 15 void set_typeString( const std::string &newValue ) { typeString = newValue; } 16 17 virtual void visit( FunctionType *funcType ); 18 virtual void visit( VoidType *voidType ); 19 virtual void visit( BasicType *basicType ); 20 virtual void visit( PointerType *pointerType ); 21 virtual void visit( ArrayType *arrayType ); 22 virtual void visit( StructInstType *structInst ); 23 virtual void visit( UnionInstType *unionInst ); 24 virtual void visit( EnumInstType *enumInst ); 25 virtual void visit( TypeInstType *typeInst ); 26 27 private: 28 void handleQualifiers( Type *type ); 29 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); 30 31 std::string typeString; 32 }; 18 33 19 class GenType : public Visitor 20 { 21 public: 22 GenType( const std::string &typeString ); 23 std::string get_typeString() const { return typeString; } 24 void set_typeString( const std::string &newValue ) { typeString = newValue; } 34 std::string genType( Type *type, const std::string &baseString ) { 35 GenType gt( baseString ); 36 type->accept( gt ); 37 return gt.get_typeString(); 38 } 39 40 GenType::GenType( const std::string &typeString ) : typeString( typeString ) {} 41 42 void GenType::visit( VoidType *voidType ) { 43 typeString = "void " + typeString; 44 handleQualifiers( voidType ); 45 } 46 47 void GenType::visit( BasicType *basicType ) { 48 BasicType::Kind kind = basicType->get_kind(); 49 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); 50 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; 51 handleQualifiers( basicType ); 52 } 53 54 void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { 55 std::ostrstream os; 56 if ( typeString != "" ) { 57 if ( typeString[ 0 ] == '*' ) { 58 os << "(" << typeString << ")"; 59 } else { 60 os << typeString; 61 } // if 62 } // if 63 os << "["; 64 65 if ( isStatic ) { 66 os << "static "; 67 } // if 68 if ( qualifiers.isConst ) { 69 os << "const "; 70 } // if 71 if ( qualifiers.isVolatile ) { 72 os << "volatile "; 73 } // if 74 if ( qualifiers.isRestrict ) { 75 os << "__restrict "; 76 } // if 77 if ( isVarLen ) { 78 os << "*"; 79 } // if 80 if ( dimension != 0 ) { 81 CodeGenerator2 cg( os ); 82 dimension->accept( cg ); 83 } // if 84 os << "]"; 85 86 typeString = std::string( os.str(), os.pcount() ); 25 87 26 virtual void visit( FunctionType *funcType ); 27 virtual void visit( VoidType *voidType ); 28 virtual void visit( BasicType *basicType ); 29 virtual void visit( PointerType *pointerType ); 30 virtual void visit( ArrayType *arrayType ); 31 virtual void visit( StructInstType *structInst ); 32 virtual void visit( UnionInstType *unionInst ); 33 virtual void visit( EnumInstType *enumInst ); 34 virtual void visit( TypeInstType *typeInst ); 88 base->accept( *this ); 89 } 90 91 void GenType::visit( PointerType *pointerType ) { 92 assert( pointerType->get_base() != 0); 93 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { 94 genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); 95 } else { 96 handleQualifiers( pointerType ); 97 if ( typeString[ 0 ] == '?' ) { 98 typeString = "* " + typeString; 99 } else { 100 typeString = "*" + typeString; 101 } // if 102 pointerType->get_base()->accept( *this ); 103 } // if 104 } 105 106 void GenType::visit( ArrayType *arrayType ){ 107 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); 108 } 109 110 void GenType::visit( FunctionType *funcType ) { 111 std::ostrstream os; 112 113 if ( typeString != "" ) { 114 if ( typeString[ 0 ] == '*' ) { 115 os << "(" << typeString << ")"; 116 } else { 117 os << typeString; 118 } // if 119 } // if 35 120 36 private: 37 void handleQualifiers( Type *type ); 38 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); 121 /************* parameters ***************/ 122 123 const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); 124 125 if ( pars.empty() ) { 126 if ( funcType->get_isVarArgs() ) { 127 os << "()"; 128 } else { 129 os << "(void)"; 130 } // if 131 } else { 132 CodeGenerator2 cg( os ); 133 os << "(" ; 134 135 cg.genCommaList( pars.begin(), pars.end() ); 136 137 if ( funcType->get_isVarArgs() ){ 138 os << ", ..."; 139 } // if 140 os << ")"; 141 } // if 39 142 40 std::string typeString; 41 }; 143 typeString = std::string( os.str(), os.pcount() ); 42 144 43 std::string 44 genType( Type *type, const std::string &baseString ) 45 { 46 GenType gt( baseString ); 47 type->accept( gt ); 48 return gt.get_typeString(); 49 } 145 if ( funcType->get_returnVals().size() == 0 ) { 146 typeString = "void " + typeString; 147 } else { 148 funcType->get_returnVals().front()->get_type()->accept( *this ); 149 } // if 150 } 50 151 51 GenType::GenType( const std::string &typeString ) 52 : typeString( typeString ) 53 { 54 }152 void GenType::visit( StructInstType *structInst ) { 153 typeString = "struct " + structInst->get_name() + " " + typeString; 154 handleQualifiers( structInst ); 155 } 55 156 56 void GenType::visit(VoidType *voidType){57 typeString = "void" + typeString;58 handleQualifiers( voidType);59 }157 void GenType::visit( UnionInstType *unionInst ) { 158 typeString = "union " + unionInst->get_name() + " " + typeString; 159 handleQualifiers( unionInst ); 160 } 60 161 61 void GenType::visit(BasicType *basicType) 62 { 63 std::string typeWords; 64 switch(basicType->get_kind()){ 65 case BasicType::Bool: 66 typeWords = "_Bool"; 67 break; 68 case BasicType::Char: 69 case BasicType::SignedChar: 70 typeWords = "char"; 71 break; 72 case BasicType::UnsignedChar: 73 typeWords = "unsigned char"; 74 break; 75 case BasicType::ShortSignedInt: 76 typeWords = "short"; 77 break; 78 case BasicType::ShortUnsignedInt: 79 typeWords = "short unsigned"; 80 break; 81 case BasicType::SignedInt: 82 typeWords = "int"; 83 break; 84 case BasicType::UnsignedInt: 85 typeWords = "unsigned int"; 86 break; 87 case BasicType::LongSignedInt: 88 typeWords = "long int"; 89 break; 90 case BasicType::LongUnsignedInt: 91 typeWords = "long unsigned int"; 92 break; 93 case BasicType::LongLongSignedInt: 94 typeWords = "long long int"; 95 break; 96 case BasicType::LongLongUnsignedInt: 97 typeWords = "long long unsigned int"; 98 break; 99 case BasicType::Float: 100 typeWords = "float"; 101 break; 102 case BasicType::Double: 103 typeWords = "double"; 104 break; 105 case BasicType::LongDouble: 106 typeWords = "long double"; 107 break; 108 case BasicType::FloatComplex: 109 typeWords = "float _Complex"; 110 break; 111 case BasicType::DoubleComplex: 112 typeWords = "double _Complex"; 113 break; 114 case BasicType::LongDoubleComplex: 115 typeWords = "long double _Complex"; 116 break; 117 case BasicType::FloatImaginary: 118 typeWords = "float _Imaginary"; 119 break; 120 case BasicType::DoubleImaginary: 121 typeWords = "double _Imaginary"; 122 break; 123 case BasicType::LongDoubleImaginary: 124 typeWords = "long double _Imaginary"; 125 break; 126 default: 127 assert( false ); 128 } 129 typeString = typeWords + " " + typeString; 130 handleQualifiers( basicType ); 131 } 162 void GenType::visit( EnumInstType *enumInst ) { 163 typeString = "enum " + enumInst->get_name() + " " + typeString; 164 handleQualifiers( enumInst ); 165 } 132 166 133 void 134 GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) 135 { 136 std::ostrstream os; 137 if( typeString != "" ) { 138 if( typeString[ 0 ] == '*' ) { 139 os << "(" << typeString << ")"; 140 } else { 141 os << typeString; 167 void GenType::visit( TypeInstType *typeInst ) { 168 typeString = typeInst->get_name() + " " + typeString; 169 handleQualifiers( typeInst ); 142 170 } 143 }144 os << "[";145 if( isStatic ) {146 os << "static ";147 }148 if( qualifiers.isConst ) {149 os << "const ";150 }151 if( qualifiers.isVolatile ) {152 os << "volatile ";153 }154 if( isVarLen ) {155 os << "*";156 }157 if( dimension != 0 ) {158 CodeGenerator2 cg( os );159 dimension->accept( cg );160 }161 os << "]";162 163 typeString = std::string( os.str(), os.pcount() );164 165 base->accept ( *this );166 }167 171 168 void GenType::visit(PointerType *pointerType) {169 assert(pointerType->get_base() != 0); 170 if( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { 171 genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); 172 } else{173 handleQualifiers( pointerType );174 if( typeString[ 0 ] == '?' ) { 175 typeString = "* " + typeString; 176 } else { 177 typeString = "*" + typeString; 172 void GenType::handleQualifiers( Type *type ) { 173 if ( type->get_isConst() ) { 174 typeString = "const " + typeString; 175 } // if 176 if ( type->get_isVolatile() ) { 177 typeString = "volatile " + typeString; 178 } // if 179 if ( type->get_isRestrict() ) { 180 typeString = "__restrict " + typeString; 181 } // if 178 182 } 179 pointerType->get_base()->accept ( *this );180 }181 }182 183 void GenType::visit(ArrayType *arrayType){184 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );185 }186 187 void GenType::visit(FunctionType *funcType) {188 std::ostrstream os;189 190 if( typeString != "" ) {191 if( typeString[ 0 ] == '*' ) {192 os << "(" << typeString << ")";193 } else {194 os << typeString;195 }196 }197 198 /************* parameters ***************/199 200 const std::list<DeclarationWithType*> &pars = funcType->get_parameters();201 202 if( pars.empty() ) {203 if( funcType->get_isVarArgs() ) {204 os << "()";205 } else {206 os << "(void)";207 }208 } else {209 CodeGenerator2 cg( os );210 os << "(" ;211 212 cg.genCommaList( pars.begin(), pars.end() );213 214 if( funcType->get_isVarArgs() ){215 os << ", ...";216 }217 os << ")";218 }219 220 typeString = std::string( os.str(), os.pcount() );221 222 if( funcType->get_returnVals().size() == 0 ) {223 typeString = "void " + typeString;224 } else {225 226 funcType->get_returnVals().front()->get_type()->accept( *this );227 228 }229 230 }231 232 void GenType::visit( StructInstType *structInst )233 {234 typeString = "struct " + structInst->get_name() + " " + typeString;235 handleQualifiers( structInst );236 }237 238 void239 GenType::visit( UnionInstType *unionInst )240 {241 typeString = "union " + unionInst->get_name() + " " + typeString;242 handleQualifiers( unionInst );243 }244 245 void246 GenType::visit( EnumInstType *enumInst )247 {248 typeString = "enum " + enumInst->get_name() + " " + typeString;249 handleQualifiers( enumInst );250 }251 252 void253 GenType::visit( TypeInstType *typeInst )254 {255 typeString = typeInst->get_name() + " " + typeString;256 handleQualifiers( typeInst );257 }258 259 void260 GenType::handleQualifiers( Type *type )261 {262 if( type->get_isConst() ) {263 typeString = "const " + typeString;264 }265 if( type->get_isVolatile() ) {266 typeString = "volatile " + typeString;267 }268 if( type->get_isRestrict() ) {269 typeString = "__restrict " + typeString;270 }271 }272 273 183 } // namespace CodeGen -
translator/CodeGen/GenType.h
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: GenType.h,v 1.2 2005/08/29 20:14:12 rcbilson Exp $5 *6 */7 8 1 #ifndef CODEGEN_GENTYPE_H 9 2 #define CODEGEN_GENTYPE_H … … 13 6 14 7 namespace CodeGen { 15 16 std::string genType( Type *type, const std::string &baseString ); 17 8 std::string genType( Type *type, const std::string &baseString ); 18 9 } // namespace CodeGen 19 10 20 #endif / * #ifndef CODEGEN_GENTYPE_H */11 #endif // CODEGEN_GENTYPE_H -
translator/CodeGen/Generate.cc
rd9a0e76 r17cd4eb 12 12 13 13 namespace CodeGen { 14 void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) { 15 CodeGen::CodeGenerator2 cgv( os ); 14 16 15 void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) { 16 17 CodeGen::CodeGenerator2 cgv( os ); 18 19 for(std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end(); i++) 20 { 21 if( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || !LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) { 22 (*i)->accept(cgv); 23 cgv.shift_left(); 24 if( doSemicolon( *i ) ) { 25 os << ";"; 26 } 27 os << std::endl; 28 } 29 } 30 31 } 32 17 for ( std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end(); i++ ) { 18 if ( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) { 19 (*i)->accept(cgv); 20 cgv.shift_left(); 21 if ( doSemicolon( *i ) ) { 22 os << ";"; 23 } // if 24 os << std::endl; 25 } // if 26 } // for 27 } 33 28 } // namespace CodeGen 34 35
Note:
See TracChangeset
for help on using the changeset viewer.