[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 | //
|
---|
[6c4ff37] | 7 | // CodeGenerator.cc --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[eb3261f] | 11 | // Last Modified By : Rob Schluntz
|
---|
| 12 | // Last Modified On : Thu Jun 04 15:00:00 2015
|
---|
| 13 | // Update Count : 125
|
---|
[51587aa] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 16 | #include <algorithm>
|
---|
| 17 | #include <iostream>
|
---|
| 18 | #include <cassert>
|
---|
| 19 | #include <list>
|
---|
| 20 |
|
---|
| 21 | #include "SynTree/Type.h"
|
---|
| 22 | #include "SynTree/Declaration.h"
|
---|
| 23 | #include "SynTree/Statement.h"
|
---|
| 24 | #include "SynTree/Expression.h"
|
---|
| 25 | #include "SynTree/Initializer.h"
|
---|
| 26 |
|
---|
| 27 | #include "utility.h"
|
---|
| 28 | #include "UnimplementedError.h"
|
---|
| 29 |
|
---|
[a61fea9a] | 30 | #include "CodeGenerator.h"
|
---|
[51b73452] | 31 | #include "OperatorTable.h"
|
---|
| 32 | #include "GenType.h"
|
---|
| 33 |
|
---|
| 34 | using namespace std;
|
---|
| 35 |
|
---|
| 36 | namespace CodeGen {
|
---|
[6c4ff37] | 37 | int CodeGenerator::tabsize = 4;
|
---|
[51587aa] | 38 |
|
---|
[2b6c1e0] | 39 | // the kinds of statements that would ideally be separated by more whitespace
|
---|
| 40 | bool wantSpacing( Statement * stmt) {
|
---|
| 41 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
|
---|
| 42 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[6c4ff37] | 45 | CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
|
---|
[51587aa] | 46 |
|
---|
[6c4ff37] | 47 | CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
|
---|
| 48 | : cur_indent( indent ), insideFunction( infunp ), output( os ) {
|
---|
| 49 | //output << std::string( init );
|
---|
[51587aa] | 50 | }
|
---|
| 51 |
|
---|
[6c4ff37] | 52 | CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
|
---|
| 53 | : cur_indent( indent ), insideFunction( infunp ), output( os ) {
|
---|
| 54 | //output << std::string( init );
|
---|
[51587aa] | 55 | }
|
---|
| 56 |
|
---|
| 57 | string mangleName( DeclarationWithType *decl ) {
|
---|
| 58 | if ( decl->get_mangleName() != "" ) {
|
---|
| 59 | return decl->get_mangleName();
|
---|
| 60 | } else {
|
---|
| 61 | return decl->get_name();
|
---|
| 62 | } // if
|
---|
| 63 | }
|
---|
[51b73452] | 64 |
|
---|
[51587aa] | 65 | //*** Declarations
|
---|
[6c4ff37] | 66 | void CodeGenerator::visit( FunctionDecl *functionDecl ) {
|
---|
[51587aa] | 67 | handleStorageClass( functionDecl );
|
---|
[f38c8d9] | 68 | if ( functionDecl->get_isInline() ) {
|
---|
[6c4ff37] | 69 | output << "inline ";
|
---|
[f38c8d9] | 70 | } // if
|
---|
[6c4ff37] | 71 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
|
---|
[51587aa] | 72 |
|
---|
| 73 | // how to get this to the Functype?
|
---|
| 74 | std::list< Declaration * > olds = functionDecl->get_oldDecls();
|
---|
| 75 | if ( ! olds.empty() ) {
|
---|
[6c4ff37] | 76 | output << " /* function has old declaration */";
|
---|
[51587aa] | 77 | } // if
|
---|
| 78 |
|
---|
| 79 | // acceptAll( functionDecl->get_oldDecls(), *this );
|
---|
| 80 | if ( functionDecl->get_statements() ) {
|
---|
| 81 | functionDecl->get_statements()->accept(*this );
|
---|
| 82 | } // if
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[6c4ff37] | 85 | void CodeGenerator::visit( ObjectDecl *objectDecl ) {
|
---|
[51587aa] | 86 | handleStorageClass( objectDecl );
|
---|
[6c4ff37] | 87 | output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
|
---|
[51587aa] | 88 |
|
---|
| 89 | if ( objectDecl->get_init() ) {
|
---|
[6c4ff37] | 90 | output << " = ";
|
---|
[51587aa] | 91 | objectDecl->get_init()->accept( *this );
|
---|
| 92 | } // if
|
---|
| 93 | if ( objectDecl->get_bitfieldWidth() ) {
|
---|
[6c4ff37] | 94 | output << ":";
|
---|
[51587aa] | 95 | objectDecl->get_bitfieldWidth()->accept( *this );
|
---|
| 96 | } // if
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[6c4ff37] | 99 | void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
|
---|
[51587aa] | 100 | if ( aggDecl->get_name() != "" )
|
---|
[6c4ff37] | 101 | output << aggDecl->get_name();
|
---|
[51587aa] | 102 |
|
---|
| 103 | std::list< Declaration * > &memb = aggDecl->get_members();
|
---|
| 104 |
|
---|
| 105 | if ( ! memb.empty() ) {
|
---|
[6c4ff37] | 106 | output << endl << string( cur_indent, ' ' ) << "{" << endl;
|
---|
[51587aa] | 107 |
|
---|
[6c4ff37] | 108 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 109 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
---|
[6c4ff37] | 110 | output << string( cur_indent, ' ' );
|
---|
[51587aa] | 111 | (*i)->accept(*this );
|
---|
[6c4ff37] | 112 | output << ";" << endl;
|
---|
[51587aa] | 113 | }
|
---|
| 114 |
|
---|
[6c4ff37] | 115 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 116 |
|
---|
[6c4ff37] | 117 | output << string( cur_indent, ' ' ) << "}";
|
---|
[17cd4eb] | 118 | } // if
|
---|
[51587aa] | 119 | }
|
---|
[17cd4eb] | 120 |
|
---|
[6c4ff37] | 121 | void CodeGenerator::visit( StructDecl *structDecl ) {
|
---|
| 122 | output << "struct ";
|
---|
[51587aa] | 123 | handleAggregate( structDecl );
|
---|
| 124 | }
|
---|
[17cd4eb] | 125 |
|
---|
[6c4ff37] | 126 | void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
|
---|
| 127 | output << "union ";
|
---|
[51587aa] | 128 | handleAggregate( aggregateDecl );
|
---|
| 129 | }
|
---|
[51b73452] | 130 |
|
---|
[6c4ff37] | 131 | void CodeGenerator::visit( EnumDecl *aggDecl ) {
|
---|
| 132 | output << "enum ";
|
---|
[51587aa] | 133 |
|
---|
| 134 | if ( aggDecl->get_name() != "" )
|
---|
[6c4ff37] | 135 | output << aggDecl->get_name();
|
---|
[51587aa] | 136 |
|
---|
| 137 | std::list< Declaration* > &memb = aggDecl->get_members();
|
---|
| 138 |
|
---|
| 139 | if ( ! memb.empty() ) {
|
---|
[6c4ff37] | 140 | output << endl << "{" << endl;
|
---|
[51587aa] | 141 |
|
---|
[6c4ff37] | 142 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 143 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
---|
| 144 | ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
|
---|
| 145 | assert( obj );
|
---|
[6c4ff37] | 146 | output << string( cur_indent, ' ' ) << mangleName( obj );
|
---|
[51587aa] | 147 | if ( obj->get_init() ) {
|
---|
[6c4ff37] | 148 | output << " = ";
|
---|
[51587aa] | 149 | obj->get_init()->accept(*this );
|
---|
| 150 | } // if
|
---|
[6c4ff37] | 151 | output << "," << endl;
|
---|
[51587aa] | 152 | } // for
|
---|
| 153 |
|
---|
[6c4ff37] | 154 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 155 |
|
---|
[6c4ff37] | 156 | output << "}" << endl;
|
---|
[51587aa] | 157 | } // if
|
---|
| 158 | }
|
---|
[51b73452] | 159 |
|
---|
[6c4ff37] | 160 | void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
|
---|
[51587aa] | 161 |
|
---|
[6c4ff37] | 162 | void CodeGenerator::visit( TypedefDecl *typeDecl ) {
|
---|
| 163 | output << "typedef ";
|
---|
| 164 | output << genType( typeDecl->get_base(), typeDecl->get_name() );
|
---|
[51587aa] | 165 | }
|
---|
[51b73452] | 166 |
|
---|
[6c4ff37] | 167 | void CodeGenerator::visit( TypeDecl *typeDecl ) {
|
---|
[51587aa] | 168 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
|
---|
| 169 | // still to be done
|
---|
[6c4ff37] | 170 | output << "extern unsigned long " << typeDecl->get_name();
|
---|
[51587aa] | 171 | if ( typeDecl->get_base() ) {
|
---|
[6c4ff37] | 172 | output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
|
---|
[51587aa] | 173 | } // if
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[6c4ff37] | 176 | void CodeGenerator::visit( SingleInit *init ) {
|
---|
[51587aa] | 177 | init->get_value()->accept( *this );
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[6c4ff37] | 180 | void CodeGenerator::visit( ListInit *init ) {
|
---|
| 181 | output << "{ ";
|
---|
[51587aa] | 182 | genCommaList( init->begin_initializers(), init->end_initializers() );
|
---|
[6c4ff37] | 183 | output << " }";
|
---|
[51587aa] | 184 | }
|
---|
| 185 |
|
---|
[6c4ff37] | 186 | void CodeGenerator::visit( Constant *constant ) {
|
---|
| 187 | output << constant->get_value() ;
|
---|
[51587aa] | 188 | }
|
---|
| 189 |
|
---|
| 190 | //*** Expressions
|
---|
[6c4ff37] | 191 | void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
|
---|
[51587aa] | 192 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
|
---|
| 193 | OperatorInfo opInfo;
|
---|
| 194 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
|
---|
| 195 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
|
---|
| 196 | switch ( opInfo.type ) {
|
---|
| 197 | case OT_PREFIXASSIGN:
|
---|
| 198 | case OT_POSTFIXASSIGN:
|
---|
| 199 | case OT_INFIXASSIGN:
|
---|
| 200 | {
|
---|
| 201 | assert( arg != applicationExpr->get_args().end() );
|
---|
| 202 | if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
|
---|
| 203 |
|
---|
| 204 | *arg = addrExpr->get_arg();
|
---|
| 205 | } else {
|
---|
| 206 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
| 207 | newExpr->get_args().push_back( *arg );
|
---|
| 208 | *arg = newExpr;
|
---|
| 209 | } // if
|
---|
| 210 | break;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | default:
|
---|
| 214 | // do nothing
|
---|
| 215 | ;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | switch ( opInfo.type ) {
|
---|
| 219 | case OT_INDEX:
|
---|
| 220 | assert( applicationExpr->get_args().size() == 2 );
|
---|
| 221 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 222 | output << "[";
|
---|
[51587aa] | 223 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 224 | output << "]";
|
---|
[51587aa] | 225 | break;
|
---|
| 226 |
|
---|
| 227 | case OT_CALL:
|
---|
| 228 | // there are no intrinsic definitions of the function call operator
|
---|
| 229 | assert( false );
|
---|
| 230 | break;
|
---|
| 231 |
|
---|
| 232 | case OT_PREFIX:
|
---|
| 233 | case OT_PREFIXASSIGN:
|
---|
| 234 | assert( applicationExpr->get_args().size() == 1 );
|
---|
[6c4ff37] | 235 | output << "(";
|
---|
| 236 | output << opInfo.symbol;
|
---|
[51587aa] | 237 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 238 | output << ")";
|
---|
[51587aa] | 239 | break;
|
---|
| 240 |
|
---|
| 241 | case OT_POSTFIX:
|
---|
| 242 | case OT_POSTFIXASSIGN:
|
---|
| 243 | assert( applicationExpr->get_args().size() == 1 );
|
---|
| 244 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 245 | output << opInfo.symbol;
|
---|
[51587aa] | 246 | break;
|
---|
| 247 |
|
---|
| 248 | case OT_INFIX:
|
---|
| 249 | case OT_INFIXASSIGN:
|
---|
| 250 | assert( applicationExpr->get_args().size() == 2 );
|
---|
[6c4ff37] | 251 | output << "(";
|
---|
[51587aa] | 252 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 253 | output << opInfo.symbol;
|
---|
[51587aa] | 254 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 255 | output << ")";
|
---|
[51587aa] | 256 | break;
|
---|
| 257 |
|
---|
| 258 | case OT_CONSTANT:
|
---|
| 259 | // there are no intrinsic definitions of 0 or 1 as functions
|
---|
| 260 | assert( false );
|
---|
| 261 | }
|
---|
[17cd4eb] | 262 | } else {
|
---|
[51587aa] | 263 | varExpr->accept( *this );
|
---|
[6c4ff37] | 264 | output << "(";
|
---|
[51587aa] | 265 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 266 | output << ")";
|
---|
[17cd4eb] | 267 | } // if
|
---|
[51587aa] | 268 | } else {
|
---|
| 269 | applicationExpr->get_function()->accept( *this );
|
---|
[6c4ff37] | 270 | output << "(";
|
---|
[51587aa] | 271 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 272 | output << ")";
|
---|
[51587aa] | 273 | } // if
|
---|
| 274 | }
|
---|
[51b73452] | 275 |
|
---|
[6c4ff37] | 276 | void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
|
---|
[51587aa] | 277 | if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
|
---|
| 278 | OperatorInfo opInfo;
|
---|
| 279 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
| 280 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
|
---|
| 281 | switch ( opInfo.type ) {
|
---|
| 282 | case OT_INDEX:
|
---|
| 283 | assert( untypedExpr->get_args().size() == 2 );
|
---|
| 284 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 285 | output << "[";
|
---|
[51587aa] | 286 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 287 | output << "]";
|
---|
[51587aa] | 288 | break;
|
---|
| 289 |
|
---|
| 290 | case OT_CALL:
|
---|
| 291 | assert( false );
|
---|
| 292 | break;
|
---|
| 293 |
|
---|
| 294 | case OT_PREFIX:
|
---|
| 295 | case OT_PREFIXASSIGN:
|
---|
| 296 | assert( untypedExpr->get_args().size() == 1 );
|
---|
[6c4ff37] | 297 | output << "(";
|
---|
| 298 | output << opInfo.symbol;
|
---|
[51587aa] | 299 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 300 | output << ")";
|
---|
[51587aa] | 301 | break;
|
---|
| 302 |
|
---|
| 303 | case OT_POSTFIX:
|
---|
| 304 | case OT_POSTFIXASSIGN:
|
---|
| 305 | assert( untypedExpr->get_args().size() == 1 );
|
---|
| 306 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 307 | output << opInfo.symbol;
|
---|
[51587aa] | 308 | break;
|
---|
[51b73452] | 309 |
|
---|
[51587aa] | 310 | case OT_INFIX:
|
---|
| 311 | case OT_INFIXASSIGN:
|
---|
| 312 | assert( untypedExpr->get_args().size() == 2 );
|
---|
[6c4ff37] | 313 | output << "(";
|
---|
[51587aa] | 314 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 315 | output << opInfo.symbol;
|
---|
[51587aa] | 316 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 317 | output << ")";
|
---|
[51587aa] | 318 | break;
|
---|
| 319 |
|
---|
| 320 | case OT_CONSTANT:
|
---|
| 321 | // there are no intrinsic definitions of 0 or 1 as functions
|
---|
| 322 | assert( false );
|
---|
| 323 | }
|
---|
| 324 | } else {
|
---|
| 325 | nameExpr->accept( *this );
|
---|
[6c4ff37] | 326 | output << "(";
|
---|
[51587aa] | 327 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
[6c4ff37] | 328 | output << ")";
|
---|
[51587aa] | 329 | } // if
|
---|
| 330 | } else {
|
---|
| 331 | untypedExpr->get_function()->accept( *this );
|
---|
[6c4ff37] | 332 | output << "(";
|
---|
[51587aa] | 333 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
[6c4ff37] | 334 | output << ")";
|
---|
[51587aa] | 335 | } // if
|
---|
| 336 | }
|
---|
[51b73452] | 337 |
|
---|
[6c4ff37] | 338 | void CodeGenerator::visit( NameExpr *nameExpr ) {
|
---|
[51587aa] | 339 | OperatorInfo opInfo;
|
---|
| 340 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
| 341 | assert( opInfo.type == OT_CONSTANT );
|
---|
[6c4ff37] | 342 | output << opInfo.symbol;
|
---|
[51587aa] | 343 | } else {
|
---|
[6c4ff37] | 344 | output << nameExpr->get_name();
|
---|
[51587aa] | 345 | } // if
|
---|
| 346 | }
|
---|
[51b73452] | 347 |
|
---|
[6c4ff37] | 348 | void CodeGenerator::visit( AddressExpr *addressExpr ) {
|
---|
| 349 | output << "(&";
|
---|
[51587aa] | 350 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
|
---|
| 351 | if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
|
---|
[6c4ff37] | 352 | output << mangleName( variableExpr->get_var() );
|
---|
[51587aa] | 353 | } else {
|
---|
| 354 | addressExpr->get_arg()->accept( *this );
|
---|
| 355 | } // if
|
---|
[6c4ff37] | 356 | output << ")";
|
---|
[51587aa] | 357 | }
|
---|
| 358 |
|
---|
[6c4ff37] | 359 | void CodeGenerator::visit( CastExpr *castExpr ) {
|
---|
| 360 | output << "((";
|
---|
[51587aa] | 361 | if ( castExpr->get_results().empty() ) {
|
---|
[6c4ff37] | 362 | output << "void" ;
|
---|
[51587aa] | 363 | } else {
|
---|
[6c4ff37] | 364 | output << genType( castExpr->get_results().front(), "" );
|
---|
[51587aa] | 365 | } // if
|
---|
[6c4ff37] | 366 | output << ")";
|
---|
[51587aa] | 367 | castExpr->get_arg()->accept( *this );
|
---|
[6c4ff37] | 368 | output << ")";
|
---|
[51587aa] | 369 | }
|
---|
[51b73452] | 370 |
|
---|
[6c4ff37] | 371 | void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
|
---|
[51587aa] | 372 | assert( false );
|
---|
| 373 | }
|
---|
[51b73452] | 374 |
|
---|
[6c4ff37] | 375 | void CodeGenerator::visit( MemberExpr *memberExpr ) {
|
---|
[51587aa] | 376 | memberExpr->get_aggregate()->accept( *this );
|
---|
[6c4ff37] | 377 | output << "." << mangleName( memberExpr->get_member() );
|
---|
[51587aa] | 378 | }
|
---|
[51b73452] | 379 |
|
---|
[6c4ff37] | 380 | void CodeGenerator::visit( VariableExpr *variableExpr ) {
|
---|
[51587aa] | 381 | OperatorInfo opInfo;
|
---|
| 382 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
|
---|
[6c4ff37] | 383 | output << opInfo.symbol;
|
---|
[51587aa] | 384 | } else {
|
---|
[6c4ff37] | 385 | output << mangleName( variableExpr->get_var() );
|
---|
[51587aa] | 386 | } // if
|
---|
| 387 | }
|
---|
[51b73452] | 388 |
|
---|
[6c4ff37] | 389 | void CodeGenerator::visit( ConstantExpr *constantExpr ) {
|
---|
[51587aa] | 390 | assert( constantExpr->get_constant() );
|
---|
| 391 | constantExpr->get_constant()->accept( *this );
|
---|
| 392 | }
|
---|
[51b73452] | 393 |
|
---|
[6c4ff37] | 394 | void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
|
---|
| 395 | output << "sizeof(";
|
---|
[51587aa] | 396 | if ( sizeofExpr->get_isType() ) {
|
---|
[6c4ff37] | 397 | output << genType( sizeofExpr->get_type(), "" );
|
---|
[51587aa] | 398 | } else {
|
---|
| 399 | sizeofExpr->get_expr()->accept( *this );
|
---|
| 400 | } // if
|
---|
[6c4ff37] | 401 | output << ")";
|
---|
[51587aa] | 402 | }
|
---|
[51b73452] | 403 |
|
---|
[6c4ff37] | 404 | void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
|
---|
| 405 | output << "(";
|
---|
[51587aa] | 406 | logicalExpr->get_arg1()->accept( *this );
|
---|
| 407 | if ( logicalExpr->get_isAnd() ) {
|
---|
[6c4ff37] | 408 | output << " && ";
|
---|
[51587aa] | 409 | } else {
|
---|
[6c4ff37] | 410 | output << " || ";
|
---|
[51587aa] | 411 | } // if
|
---|
| 412 | logicalExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 413 | output << ")";
|
---|
[51587aa] | 414 | }
|
---|
[51b73452] | 415 |
|
---|
[6c4ff37] | 416 | void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
|
---|
| 417 | output << "(";
|
---|
[51587aa] | 418 | conditionalExpr->get_arg1()->accept( *this );
|
---|
[6c4ff37] | 419 | output << " ? ";
|
---|
[51587aa] | 420 | conditionalExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 421 | output << " : ";
|
---|
[51587aa] | 422 | conditionalExpr->get_arg3()->accept( *this );
|
---|
[6c4ff37] | 423 | output << ")";
|
---|
[51587aa] | 424 | }
|
---|
[51b73452] | 425 |
|
---|
[6c4ff37] | 426 | void CodeGenerator::visit( CommaExpr *commaExpr ) {
|
---|
| 427 | output << "(";
|
---|
[51587aa] | 428 | commaExpr->get_arg1()->accept( *this );
|
---|
[6c4ff37] | 429 | output << " , ";
|
---|
[51587aa] | 430 | commaExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 431 | output << ")";
|
---|
[51587aa] | 432 | }
|
---|
[51b73452] | 433 |
|
---|
[6c4ff37] | 434 | void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
|
---|
[51b73452] | 435 |
|
---|
[6c4ff37] | 436 | void CodeGenerator::visit( TypeExpr *typeExpr ) {}
|
---|
[2b6c1e0] | 437 |
|
---|
[51587aa] | 438 | //*** Statements
|
---|
[6c4ff37] | 439 | void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
|
---|
[51587aa] | 440 | std::list<Statement*> ks = compoundStmt->get_kids();
|
---|
[2b6c1e0] | 441 | output << "{" << endl;
|
---|
[51587aa] | 442 |
|
---|
[2b6c1e0] | 443 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 444 |
|
---|
| 445 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) {
|
---|
[2b6c1e0] | 446 | output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
|
---|
[51587aa] | 447 | (*i)->accept(*this );
|
---|
[2b6c1e0] | 448 |
|
---|
[6c4ff37] | 449 | output << endl;
|
---|
[2b6c1e0] | 450 | if ( wantSpacing( *i ) ) {
|
---|
| 451 | output << endl;
|
---|
| 452 | }
|
---|
[51587aa] | 453 | }
|
---|
[6c4ff37] | 454 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 455 |
|
---|
[2b6c1e0] | 456 | output << string( cur_indent, ' ' ) << "}";
|
---|
[51587aa] | 457 | }
|
---|
| 458 |
|
---|
[6c4ff37] | 459 | void CodeGenerator::visit( ExprStmt *exprStmt ) {
|
---|
| 460 | // I don't see why this check is necessary.
|
---|
| 461 | // If this starts to cause problems then put it back in,
|
---|
| 462 | // with an explanation
|
---|
| 463 | assert( exprStmt );
|
---|
| 464 |
|
---|
| 465 | // if ( exprStmt != 0 ) {
|
---|
| 466 | exprStmt->get_expr()->accept( *this );
|
---|
| 467 | output << ";" ;
|
---|
| 468 | // } // if
|
---|
[51587aa] | 469 | }
|
---|
| 470 |
|
---|
[6c4ff37] | 471 | void CodeGenerator::visit( IfStmt *ifStmt ) {
|
---|
| 472 | output << "if (";
|
---|
[51587aa] | 473 | ifStmt->get_condition()->accept(*this );
|
---|
[2b6c1e0] | 474 | output << ") ";
|
---|
[51587aa] | 475 |
|
---|
| 476 | ifStmt->get_thenPart()->accept(*this );
|
---|
| 477 |
|
---|
| 478 | if ( ifStmt->get_elsePart() != 0) {
|
---|
[2b6c1e0] | 479 | output << " else ";
|
---|
[51587aa] | 480 | ifStmt->get_elsePart()->accept(*this );
|
---|
| 481 | } // if
|
---|
| 482 | }
|
---|
| 483 |
|
---|
[6c4ff37] | 484 | void CodeGenerator::visit( SwitchStmt *switchStmt ) {
|
---|
| 485 | output << "switch (" ;
|
---|
[51587aa] | 486 | switchStmt->get_condition()->accept(*this );
|
---|
[2b6c1e0] | 487 | output << ") ";
|
---|
[6c4ff37] | 488 |
|
---|
[2b6c1e0] | 489 | output << "{" << std::endl;
|
---|
[6c4ff37] | 490 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 491 |
|
---|
[6c4ff37] | 492 | acceptAll( switchStmt->get_branches(), *this );
|
---|
[51587aa] | 493 |
|
---|
[6c4ff37] | 494 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 495 |
|
---|
[2b6c1e0] | 496 | output << string( cur_indent, ' ' ) << "}";
|
---|
[51587aa] | 497 | }
|
---|
| 498 |
|
---|
[6c4ff37] | 499 | void CodeGenerator::visit( CaseStmt *caseStmt ) {
|
---|
| 500 | output << string( cur_indent, ' ' );
|
---|
[eb3261f] | 501 | if ( caseStmt->isDefault()) {
|
---|
[2b6c1e0] | 502 | output << "default";
|
---|
[eb3261f] | 503 | } else {
|
---|
[2b6c1e0] | 504 | output << "case ";
|
---|
[51587aa] | 505 | caseStmt->get_condition()->accept(*this );
|
---|
[17cd4eb] | 506 | } // if
|
---|
[6c4ff37] | 507 | output << ":\n";
|
---|
| 508 |
|
---|
[51587aa] | 509 | std::list<Statement *> sts = caseStmt->get_statements();
|
---|
| 510 |
|
---|
[6c4ff37] | 511 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 512 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
|
---|
[eb3261f] | 513 | output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ;
|
---|
[51587aa] | 514 | (*i)->accept(*this );
|
---|
[6c4ff37] | 515 | output << endl;
|
---|
[51587aa] | 516 | }
|
---|
[6c4ff37] | 517 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 518 | }
|
---|
| 519 |
|
---|
[6c4ff37] | 520 | void CodeGenerator::visit( BranchStmt *branchStmt ) {
|
---|
[51587aa] | 521 | switch ( branchStmt->get_type()) {
|
---|
| 522 | case BranchStmt::Goto:
|
---|
| 523 | if ( ! branchStmt->get_target().empty() )
|
---|
[6c4ff37] | 524 | output << "goto " << branchStmt->get_target();
|
---|
[51587aa] | 525 | else {
|
---|
| 526 | if ( branchStmt->get_computedTarget() != 0 ) {
|
---|
[6c4ff37] | 527 | output << "goto *";
|
---|
[51587aa] | 528 | branchStmt->get_computedTarget()->accept( *this );
|
---|
| 529 | } // if
|
---|
| 530 | } // if
|
---|
| 531 | break;
|
---|
| 532 | case BranchStmt::Break:
|
---|
[6c4ff37] | 533 | output << "break";
|
---|
[51587aa] | 534 | break;
|
---|
| 535 | case BranchStmt::Continue:
|
---|
[6c4ff37] | 536 | output << "continue";
|
---|
[51587aa] | 537 | break;
|
---|
| 538 | }
|
---|
[2b6c1e0] | 539 | output << ";";
|
---|
[51587aa] | 540 | }
|
---|
| 541 |
|
---|
| 542 |
|
---|
[6c4ff37] | 543 | void CodeGenerator::visit( ReturnStmt *returnStmt ) {
|
---|
| 544 | output << "return ";
|
---|
[51587aa] | 545 |
|
---|
| 546 | // xxx -- check for null expression;
|
---|
| 547 | if ( returnStmt->get_expr() ) {
|
---|
| 548 | returnStmt->get_expr()->accept( *this );
|
---|
| 549 | } // if
|
---|
[6c4ff37] | 550 | output << ";";
|
---|
[51587aa] | 551 | }
|
---|
| 552 |
|
---|
[6c4ff37] | 553 | void CodeGenerator::visit( WhileStmt *whileStmt ) {
|
---|
[51587aa] | 554 | if ( whileStmt->get_isDoWhile() )
|
---|
[6c4ff37] | 555 | output << "do" ;
|
---|
[51587aa] | 556 | else {
|
---|
[6c4ff37] | 557 | output << "while (" ;
|
---|
[51587aa] | 558 | whileStmt->get_condition()->accept(*this );
|
---|
[6c4ff37] | 559 | output << ")";
|
---|
[51587aa] | 560 | } // if
|
---|
[2b6c1e0] | 561 | output << " ";
|
---|
[51587aa] | 562 |
|
---|
[2b6c1e0] | 563 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
|
---|
[51587aa] | 564 | whileStmt->get_body()->accept( *this );
|
---|
| 565 |
|
---|
[eb3261f] | 566 | output << string( cur_indent, ' ' );
|
---|
[51587aa] | 567 |
|
---|
| 568 | if ( whileStmt->get_isDoWhile() ) {
|
---|
[6c4ff37] | 569 | output << " while (" ;
|
---|
[51587aa] | 570 | whileStmt->get_condition()->accept(*this );
|
---|
[6c4ff37] | 571 | output << ");";
|
---|
[51587aa] | 572 | } // if
|
---|
| 573 | }
|
---|
| 574 |
|
---|
[6c4ff37] | 575 | void CodeGenerator::visit( ForStmt *forStmt ) {
|
---|
| 576 | output << "for (";
|
---|
[51587aa] | 577 |
|
---|
| 578 | if ( forStmt->get_initialization() != 0 )
|
---|
| 579 | forStmt->get_initialization()->accept( *this );
|
---|
| 580 | else
|
---|
[6c4ff37] | 581 | output << ";";
|
---|
| 582 |
|
---|
[51587aa] | 583 | if ( forStmt->get_condition() != 0 )
|
---|
| 584 | forStmt->get_condition()->accept( *this );
|
---|
[6c4ff37] | 585 | output << ";";
|
---|
[51587aa] | 586 |
|
---|
| 587 | if ( forStmt->get_increment() != 0 )
|
---|
| 588 | forStmt->get_increment()->accept( *this );
|
---|
[2b6c1e0] | 589 | output << ") ";
|
---|
[51587aa] | 590 |
|
---|
| 591 | if ( forStmt->get_body() != 0 ) {
|
---|
[2b6c1e0] | 592 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
|
---|
[51587aa] | 593 | forStmt->get_body()->accept( *this );
|
---|
| 594 | } // if
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[6c4ff37] | 597 | void CodeGenerator::visit( NullStmt *nullStmt ) {
|
---|
[eb3261f] | 598 | //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
|
---|
[6c4ff37] | 599 | output << "/* null statement */ ;";
|
---|
[51587aa] | 600 | }
|
---|
| 601 |
|
---|
[6c4ff37] | 602 | void CodeGenerator::visit( DeclStmt *declStmt ) {
|
---|
[51587aa] | 603 | declStmt->get_decl()->accept( *this );
|
---|
| 604 |
|
---|
| 605 | if ( doSemicolon( declStmt->get_decl() ) ) {
|
---|
[6c4ff37] | 606 | output << ";";
|
---|
[51587aa] | 607 | } // if
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[6c4ff37] | 610 | std::string CodeGenerator::printLabels( std::list< Label > &l ) {
|
---|
[51587aa] | 611 | std::string str( "" );
|
---|
[6c4ff37] | 612 | l.unique(); // assumes a sorted list. Why not use set?
|
---|
[51587aa] | 613 |
|
---|
| 614 | for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
|
---|
| 615 | str += *i + ": ";
|
---|
| 616 |
|
---|
| 617 | return str;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[6c4ff37] | 620 | void CodeGenerator::handleStorageClass( Declaration *decl ) {
|
---|
[51587aa] | 621 | switch ( decl->get_storageClass() ) {
|
---|
| 622 | case Declaration::NoStorageClass:
|
---|
| 623 | break;
|
---|
| 624 | case Declaration::Extern:
|
---|
[6c4ff37] | 625 | output << "extern ";
|
---|
[51587aa] | 626 | break;
|
---|
| 627 | case Declaration::Static:
|
---|
[6c4ff37] | 628 | output << "static ";
|
---|
[51587aa] | 629 | break;
|
---|
| 630 | case Declaration::Auto:
|
---|
| 631 | // silently drop storage class
|
---|
| 632 | break;
|
---|
| 633 | case Declaration::Register:
|
---|
[6c4ff37] | 634 | output << "register ";
|
---|
[51587aa] | 635 | break;
|
---|
[f38c8d9] | 636 | case Declaration::Inline:
|
---|
| 637 | // handled as special via isInline flag (FIX)
|
---|
| 638 | break;
|
---|
| 639 | case Declaration::Fortran:
|
---|
| 640 | // not handled
|
---|
| 641 | break;
|
---|
[843054c2] | 642 | } // switch
|
---|
[51587aa] | 643 | }
|
---|
[51b73452] | 644 | } // namespace CodeGen
|
---|
[51587aa] | 645 |
|
---|
| 646 | // Local Variables: //
|
---|
| 647 | // tab-width: 4 //
|
---|
| 648 | // mode: c++ //
|
---|
| 649 | // compile-command: "make install" //
|
---|
| 650 | // End: //
|
---|