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