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