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