| [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 | //
 | 
|---|
| [7baed7d] | 7 | // CodeGenerator.h --
 | 
|---|
| [51587aa] | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| [c850687] | 11 | // Last Modified By : Andrew Beach
 | 
|---|
| [0dd18fd] | 12 | // Last Modified On : Fri Aug 18 15:40:00 2017
 | 
|---|
 | 13 | // Update Count     : 56
 | 
|---|
| [51587aa] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [6b0b624] | 16 | #pragma once
 | 
|---|
| [51b73452] | 17 | 
 | 
|---|
| [bf2438c] | 18 | #include <list>                   // for list
 | 
|---|
 | 19 | #include <ostream>                // for ostream, operator<<
 | 
|---|
 | 20 | #include <string>                 // for string
 | 
|---|
| [51b73452] | 21 | 
 | 
|---|
| [e6cee92] | 22 | #include "Common/Indenter.h"      // for Indenter
 | 
|---|
| [9857e8d] | 23 | #include "Common/PassVisitor.h"   // for PassVisitor
 | 
|---|
| [bf2438c] | 24 | #include "SynTree/Declaration.h"  // for DeclarationWithType (ptr only), Fun...
 | 
|---|
 | 25 | #include "SynTree/Visitor.h"      // for Visitor
 | 
|---|
 | 26 | #include "SynTree/SynTree.h"      // for Visitor Nodes
 | 
|---|
| [c850687] | 27 | 
 | 
|---|
| [51b73452] | 28 | namespace CodeGen {
 | 
|---|
| [9857e8d] | 29 |         struct CodeGenerator : public WithShortCircuiting, public WithVisitorRef<CodeGenerator> {
 | 
|---|
 | 30 |           static int tabsize;
 | 
|---|
| [51587aa] | 31 | 
 | 
|---|
| [c850687] | 32 |                 CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false );
 | 
|---|
| [51587aa] | 33 | 
 | 
|---|
| [9857e8d] | 34 |                 //*** Turn off visit_children for all nodes
 | 
|---|
 | 35 |                 void previsit( BaseSyntaxNode * );
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 |                 //*** Error for unhandled node types
 | 
|---|
 | 38 |                 void postvisit( BaseSyntaxNode * );
 | 
|---|
 | 39 | 
 | 
|---|
| [51587aa] | 40 |                 //*** Declaration
 | 
|---|
| [9857e8d] | 41 |                 void postvisit( StructDecl * );
 | 
|---|
 | 42 |                 void postvisit( FunctionDecl * );
 | 
|---|
 | 43 |                 void postvisit( ObjectDecl * );
 | 
|---|
 | 44 |                 void postvisit( UnionDecl *aggregateDecl );
 | 
|---|
 | 45 |                 void postvisit( EnumDecl *aggregateDecl );
 | 
|---|
 | 46 |                 void postvisit( TraitDecl *aggregateDecl );
 | 
|---|
 | 47 |                 void postvisit( TypedefDecl *typeDecl );
 | 
|---|
 | 48 |                 void postvisit( TypeDecl *typeDecl );
 | 
|---|
| [51587aa] | 49 | 
 | 
|---|
 | 50 |                 //*** Initializer
 | 
|---|
| [9857e8d] | 51 |                 void postvisit( Designation * );
 | 
|---|
 | 52 |                 void postvisit( SingleInit * );
 | 
|---|
 | 53 |                 void postvisit( ListInit * );
 | 
|---|
 | 54 |                 void postvisit( ConstructorInit * );
 | 
|---|
| [51587aa] | 55 | 
 | 
|---|
 | 56 |                 //*** Constant
 | 
|---|
| [9857e8d] | 57 |                 void postvisit( Constant * );
 | 
|---|
| [51587aa] | 58 | 
 | 
|---|
 | 59 |                 //*** Expression
 | 
|---|
| [9857e8d] | 60 |                 void postvisit( ApplicationExpr *applicationExpr );
 | 
|---|
 | 61 |                 void postvisit( UntypedExpr *untypedExpr );
 | 
|---|
 | 62 |                 void postvisit( RangeExpr * rangeExpr );
 | 
|---|
 | 63 |                 void postvisit( NameExpr *nameExpr );
 | 
|---|
 | 64 |                 void postvisit( AddressExpr *addressExpr );
 | 
|---|
 | 65 |                 void postvisit( LabelAddressExpr *addressExpr );
 | 
|---|
 | 66 |                 void postvisit( CastExpr *castExpr );
 | 
|---|
 | 67 |                 void postvisit( VirtualCastExpr *castExpr );
 | 
|---|
 | 68 |                 void postvisit( UntypedMemberExpr *memberExpr );
 | 
|---|
 | 69 |                 void postvisit( MemberExpr *memberExpr );
 | 
|---|
 | 70 |                 void postvisit( VariableExpr *variableExpr );
 | 
|---|
 | 71 |                 void postvisit( ConstantExpr *constantExpr );
 | 
|---|
 | 72 |                 void postvisit( SizeofExpr *sizeofExpr );
 | 
|---|
 | 73 |                 void postvisit( AlignofExpr *alignofExpr );
 | 
|---|
 | 74 |                 void postvisit( UntypedOffsetofExpr *offsetofExpr );
 | 
|---|
 | 75 |                 void postvisit( OffsetofExpr *offsetofExpr );
 | 
|---|
 | 76 |                 void postvisit( OffsetPackExpr *offsetPackExpr );
 | 
|---|
 | 77 |                 void postvisit( LogicalExpr *logicalExpr );
 | 
|---|
 | 78 |                 void postvisit( ConditionalExpr *conditionalExpr );
 | 
|---|
 | 79 |                 void postvisit( CommaExpr *commaExpr );
 | 
|---|
 | 80 |                 void postvisit( CompoundLiteralExpr *compLitExpr );
 | 
|---|
 | 81 |                 void postvisit( UniqueExpr * );
 | 
|---|
 | 82 |                 void postvisit( TupleAssignExpr * tupleExpr );
 | 
|---|
 | 83 |                 void postvisit( UntypedTupleExpr *tupleExpr );
 | 
|---|
 | 84 |                 void postvisit( TupleExpr *tupleExpr );
 | 
|---|
 | 85 |                 void postvisit( TupleIndexExpr * tupleExpr );
 | 
|---|
 | 86 |                 void postvisit( TypeExpr *typeExpr );
 | 
|---|
 | 87 |                 void postvisit( AsmExpr * );
 | 
|---|
 | 88 |                 void postvisit( StmtExpr * );
 | 
|---|
| [4e8949f] | 89 |                 void postvisit( ConstructorExpr * );
 | 
|---|
| [44b4114] | 90 |                 void postvisit( DeletedExpr * );
 | 
|---|
| [51587aa] | 91 | 
 | 
|---|
 | 92 |                 //*** Statements
 | 
|---|
| [9857e8d] | 93 |                 void postvisit( CompoundStmt * );
 | 
|---|
 | 94 |                 void postvisit( ExprStmt * );
 | 
|---|
 | 95 |                 void postvisit( AsmStmt * );
 | 
|---|
 | 96 |                 void postvisit( AsmDecl * );                            // special: statement in declaration context
 | 
|---|
 | 97 |                 void postvisit( IfStmt * );
 | 
|---|
 | 98 |                 void postvisit( SwitchStmt * );
 | 
|---|
 | 99 |                 void postvisit( CaseStmt * );
 | 
|---|
 | 100 |                 void postvisit( BranchStmt * );
 | 
|---|
 | 101 |                 void postvisit( ReturnStmt * );
 | 
|---|
 | 102 |                 void postvisit( ThrowStmt * );
 | 
|---|
| [e4ea10b7] | 103 |                 void postvisit( CatchStmt * );
 | 
|---|
 | 104 |                 void postvisit( WaitForStmt * );
 | 
|---|
| [55d6e8de] | 105 |                 void postvisit( WithStmt * );
 | 
|---|
| [9857e8d] | 106 |                 void postvisit( WhileStmt * );
 | 
|---|
 | 107 |                 void postvisit( ForStmt * );
 | 
|---|
 | 108 |                 void postvisit( NullStmt * );
 | 
|---|
 | 109 |                 void postvisit( DeclStmt * );
 | 
|---|
 | 110 |                 void postvisit( ImplicitCtorDtorStmt * );
 | 
|---|
| [7baed7d] | 111 | 
 | 
|---|
 | 112 |                 void genAttributes( std::list< Attribute * > & attributes );
 | 
|---|
| [51587aa] | 113 | 
 | 
|---|
 | 114 |                 template< class Iterator > void genCommaList( Iterator begin, Iterator end );
 | 
|---|
| [cda48b6] | 115 | 
 | 
|---|
| [888cbe4] | 116 |                 struct LabelPrinter {
 | 
|---|
 | 117 |                         LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {}
 | 
|---|
 | 118 |                         LabelPrinter & operator()( std::list< Label > & l );
 | 
|---|
 | 119 |                         CodeGenerator & cg;
 | 
|---|
 | 120 |                         std::list< Label > * labels;
 | 
|---|
| [cda48b6] | 121 |                 };
 | 
|---|
| [e04ef3a] | 122 | 
 | 
|---|
| [58dd019] | 123 |                 void asmName( DeclarationWithType *decl );
 | 
|---|
 | 124 | 
 | 
|---|
| [8e9cbb2] | 125 |                 void extension( Expression *expr );
 | 
|---|
 | 126 |                 void extension( Declaration *decl );
 | 
|---|
| [0dd18fd] | 127 | 
 | 
|---|
 | 128 |                 void updateLocation( BaseSyntaxNode const * to );
 | 
|---|
| [d22e90f] | 129 |                 struct LineEnder {
 | 
|---|
 | 130 |                         CodeGenerator & cg;
 | 
|---|
 | 131 |                         LineEnder( CodeGenerator & cg ) : cg( cg ) {}
 | 
|---|
 | 132 |                         std::ostream & operator()(std::ostream &) const;
 | 
|---|
 | 133 |                 };
 | 
|---|
| [51587aa] | 134 |           private:
 | 
|---|
| [cda48b6] | 135 |                 Indenter indent;
 | 
|---|
| [d22e90f] | 136 |                 std::ostream & output;
 | 
|---|
| [888cbe4] | 137 |                 LabelPrinter printLabels;
 | 
|---|
| [35b1bf4] | 138 |                 bool pretty = false;  // pretty print
 | 
|---|
| [e39241b] | 139 |                 bool genC = false;    // true if output has to be C code
 | 
|---|
| [c850687] | 140 |                 bool lineMarks = false;
 | 
|---|
| [d22e90f] | 141 |         public:
 | 
|---|
 | 142 |                 LineEnder endl;
 | 
|---|
 | 143 |         private:
 | 
|---|
| [51587aa] | 144 | 
 | 
|---|
| [8bafacc] | 145 |                 CodeLocation currentLocation;
 | 
|---|
 | 146 |                 void updateLocation( CodeLocation const & to );
 | 
|---|
 | 147 | 
 | 
|---|
| [dd020c0] | 148 |                 void handleStorageClass( DeclarationWithType *decl );
 | 
|---|
| [5f642e38] | 149 |                 void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
 | 
|---|
| [51587aa] | 150 |                 void handleTypedef( NamedTypeDecl *namedType );
 | 
|---|
| [486341f] | 151 |                 std::string mangleName( DeclarationWithType * decl );
 | 
|---|
| [66d12f7] | 152 |         }; // CodeGenerator
 | 
|---|
| [7baed7d] | 153 | 
 | 
|---|
| [51587aa] | 154 |         template< class Iterator >
 | 
|---|
| [6c4ff37] | 155 |         void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
 | 
|---|
| [44a81853] | 156 |           if ( begin == end ) return;
 | 
|---|
| [51587aa] | 157 |                 for ( ;; ) {
 | 
|---|
| [9857e8d] | 158 |                         (*begin++)->accept( *visitor );
 | 
|---|
| [44a81853] | 159 |                   if ( begin == end ) break;
 | 
|---|
 | 160 |                         output << ", ";                                                         // separator
 | 
|---|
| [51587aa] | 161 |                 } // for
 | 
|---|
| [44a81853] | 162 |         } // genCommaList
 | 
|---|
| [7baed7d] | 163 | 
 | 
|---|
| [51587aa] | 164 |         inline bool doSemicolon( Declaration* decl ) {
 | 
|---|
 | 165 |                 if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
 | 
|---|
 | 166 |                         return ! func->get_statements();
 | 
|---|
 | 167 |                 } // if
 | 
|---|
 | 168 |                 return true;
 | 
|---|
| [44a81853] | 169 |         } // doSemicolon
 | 
|---|
| [9facf3b] | 170 | 
 | 
|---|
 | 171 |         /// returns C-compatible name of declaration
 | 
|---|
 | 172 |         std::string genName( DeclarationWithType * decl );
 | 
|---|
| [d22e90f] | 173 | 
 | 
|---|
 | 174 |         inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
 | 
|---|
 | 175 |                 return endl( os );
 | 
|---|
 | 176 |         }
 | 
|---|
| [51b73452] | 177 | } // namespace CodeGen
 | 
|---|
 | 178 | 
 | 
|---|
| [51587aa] | 179 | // Local Variables: //
 | 
|---|
 | 180 | // tab-width: 4 //
 | 
|---|
 | 181 | // mode: c++ //
 | 
|---|
 | 182 | // compile-command: "make install" //
 | 
|---|
 | 183 | // End: //
 | 
|---|