[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 * );
|
---|
[51587aa] | 90 |
|
---|
| 91 | //*** Statements
|
---|
[9857e8d] | 92 | void postvisit( CompoundStmt * );
|
---|
| 93 | void postvisit( ExprStmt * );
|
---|
| 94 | void postvisit( AsmStmt * );
|
---|
| 95 | void postvisit( AsmDecl * ); // special: statement in declaration context
|
---|
| 96 | void postvisit( IfStmt * );
|
---|
| 97 | void postvisit( SwitchStmt * );
|
---|
| 98 | void postvisit( CaseStmt * );
|
---|
| 99 | void postvisit( BranchStmt * );
|
---|
| 100 | void postvisit( ReturnStmt * );
|
---|
| 101 | void postvisit( ThrowStmt * );
|
---|
[e4ea10b7] | 102 | void postvisit( CatchStmt * );
|
---|
| 103 | void postvisit( WaitForStmt * );
|
---|
[55d6e8de] | 104 | void postvisit( WithStmt * );
|
---|
[9857e8d] | 105 | void postvisit( WhileStmt * );
|
---|
| 106 | void postvisit( ForStmt * );
|
---|
| 107 | void postvisit( NullStmt * );
|
---|
| 108 | void postvisit( DeclStmt * );
|
---|
| 109 | void postvisit( ImplicitCtorDtorStmt * );
|
---|
[7baed7d] | 110 |
|
---|
| 111 | void genAttributes( std::list< Attribute * > & attributes );
|
---|
[51587aa] | 112 |
|
---|
| 113 | template< class Iterator > void genCommaList( Iterator begin, Iterator end );
|
---|
[cda48b6] | 114 |
|
---|
[888cbe4] | 115 | struct LabelPrinter {
|
---|
| 116 | LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {}
|
---|
| 117 | LabelPrinter & operator()( std::list< Label > & l );
|
---|
| 118 | CodeGenerator & cg;
|
---|
| 119 | std::list< Label > * labels;
|
---|
[cda48b6] | 120 | };
|
---|
[e04ef3a] | 121 |
|
---|
[58dd019] | 122 | void asmName( DeclarationWithType *decl );
|
---|
| 123 |
|
---|
[8e9cbb2] | 124 | void extension( Expression *expr );
|
---|
| 125 | void extension( Declaration *decl );
|
---|
[0dd18fd] | 126 |
|
---|
| 127 | void updateLocation( BaseSyntaxNode const * to );
|
---|
[d22e90f] | 128 | struct LineEnder {
|
---|
| 129 | CodeGenerator & cg;
|
---|
| 130 | LineEnder( CodeGenerator & cg ) : cg( cg ) {}
|
---|
| 131 | std::ostream & operator()(std::ostream &) const;
|
---|
| 132 | };
|
---|
[51587aa] | 133 | private:
|
---|
[cda48b6] | 134 | Indenter indent;
|
---|
[d22e90f] | 135 | std::ostream & output;
|
---|
[888cbe4] | 136 | LabelPrinter printLabels;
|
---|
[35b1bf4] | 137 | bool pretty = false; // pretty print
|
---|
[e39241b] | 138 | bool genC = false; // true if output has to be C code
|
---|
[c850687] | 139 | bool lineMarks = false;
|
---|
[d22e90f] | 140 | public:
|
---|
| 141 | LineEnder endl;
|
---|
| 142 | private:
|
---|
[51587aa] | 143 |
|
---|
[8bafacc] | 144 | CodeLocation currentLocation;
|
---|
| 145 | void updateLocation( CodeLocation const & to );
|
---|
| 146 |
|
---|
[dd020c0] | 147 | void handleStorageClass( DeclarationWithType *decl );
|
---|
[5f642e38] | 148 | void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
|
---|
[51587aa] | 149 | void handleTypedef( NamedTypeDecl *namedType );
|
---|
[486341f] | 150 | std::string mangleName( DeclarationWithType * decl );
|
---|
[66d12f7] | 151 | }; // CodeGenerator
|
---|
[7baed7d] | 152 |
|
---|
[51587aa] | 153 | template< class Iterator >
|
---|
[6c4ff37] | 154 | void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
|
---|
[44a81853] | 155 | if ( begin == end ) return;
|
---|
[51587aa] | 156 | for ( ;; ) {
|
---|
[9857e8d] | 157 | (*begin++)->accept( *visitor );
|
---|
[44a81853] | 158 | if ( begin == end ) break;
|
---|
| 159 | output << ", "; // separator
|
---|
[51587aa] | 160 | } // for
|
---|
[44a81853] | 161 | } // genCommaList
|
---|
[7baed7d] | 162 |
|
---|
[51587aa] | 163 | inline bool doSemicolon( Declaration* decl ) {
|
---|
| 164 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
|
---|
| 165 | return ! func->get_statements();
|
---|
| 166 | } // if
|
---|
| 167 | return true;
|
---|
[44a81853] | 168 | } // doSemicolon
|
---|
[9facf3b] | 169 |
|
---|
| 170 | /// returns C-compatible name of declaration
|
---|
| 171 | std::string genName( DeclarationWithType * decl );
|
---|
[d22e90f] | 172 |
|
---|
| 173 | inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
|
---|
| 174 | return endl( os );
|
---|
| 175 | }
|
---|
[51b73452] | 176 | } // namespace CodeGen
|
---|
| 177 |
|
---|
[51587aa] | 178 | // Local Variables: //
|
---|
| 179 | // tab-width: 4 //
|
---|
| 180 | // mode: c++ //
|
---|
| 181 | // compile-command: "make install" //
|
---|
| 182 | // End: //
|
---|