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