| 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.h --
 | 
|---|
| 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 : Fri Mar 12 18:35:38 2021
 | 
|---|
| 13 | // Update Count     : 63
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #pragma once
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <list>                   // for list
 | 
|---|
| 19 | #include <ostream>                // for ostream, operator<<
 | 
|---|
| 20 | #include <string>                 // for string
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "CodeGen/Options.h"      // for Options
 | 
|---|
| 23 | #include "Common/Indenter.h"      // for Indenter
 | 
|---|
| 24 | #include "Common/PassVisitor.h"   // for PassVisitor
 | 
|---|
| 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
 | 
|---|
| 28 | 
 | 
|---|
| 29 | namespace CodeGen {
 | 
|---|
| 30 |         struct CodeGenerator : public WithShortCircuiting, public WithGuards, public WithVisitorRef<CodeGenerator> {
 | 
|---|
| 31 |                 static int tabsize;
 | 
|---|
| 32 | 
 | 
|---|
| 33 |                 CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false, bool printExprTypes = false );
 | 
|---|
| 34 |                 CodeGenerator( std::ostream &os, const Options &options );
 | 
|---|
| 35 | 
 | 
|---|
| 36 |                 //*** Turn off visit_children for all nodes
 | 
|---|
| 37 |                 void previsit( BaseSyntaxNode * );
 | 
|---|
| 38 | 
 | 
|---|
| 39 |                 //*** Error for unhandled node types
 | 
|---|
| 40 |                 void postvisit( BaseSyntaxNode * );
 | 
|---|
| 41 | 
 | 
|---|
| 42 |                 //*** print type for all expressions
 | 
|---|
| 43 |                 void previsit( Expression * node );
 | 
|---|
| 44 | 
 | 
|---|
| 45 |                 //*** Declaration
 | 
|---|
| 46 |                 void postvisit( StructDecl * );
 | 
|---|
| 47 |                 void postvisit( FunctionDecl * );
 | 
|---|
| 48 |                 void postvisit( ObjectDecl * );
 | 
|---|
| 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 );
 | 
|---|
| 55 | 
 | 
|---|
| 56 |                 //*** Initializer
 | 
|---|
| 57 |                 void postvisit( Designation * );
 | 
|---|
| 58 |                 void postvisit( SingleInit * );
 | 
|---|
| 59 |                 void postvisit( ListInit * );
 | 
|---|
| 60 |                 void postvisit( ConstructorInit * );
 | 
|---|
| 61 | 
 | 
|---|
| 62 |                 //*** Constant
 | 
|---|
| 63 |                 void postvisit( Constant * );
 | 
|---|
| 64 | 
 | 
|---|
| 65 |                 //*** Expression
 | 
|---|
| 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 );
 | 
|---|
| 73 |                 void postvisit( KeywordCastExpr * castExpr );
 | 
|---|
| 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 );
 | 
|---|
| 94 |                 void postvisit( AsmExpr * );
 | 
|---|
| 95 |                 void postvisit( StmtExpr * );
 | 
|---|
| 96 |                 void postvisit( ConstructorExpr * );
 | 
|---|
| 97 |                 void postvisit( DeletedExpr * );
 | 
|---|
| 98 |                 void postvisit( DefaultArgExpr * );
 | 
|---|
| 99 |                 void postvisit( GenericExpr * );
 | 
|---|
| 100 | 
 | 
|---|
| 101 |                 //*** Statements
 | 
|---|
| 102 |                 void postvisit( CompoundStmt * );
 | 
|---|
| 103 |                 void postvisit( ExprStmt * );
 | 
|---|
| 104 |                 void postvisit( AsmStmt * );
 | 
|---|
| 105 |                 void postvisit( DirectiveStmt * );
 | 
|---|
| 106 |                 void postvisit( AsmDecl * );                                    // special: statement in declaration context
 | 
|---|
| 107 |                 void postvisit( DirectiveDecl * );                              // special: statement in declaration context
 | 
|---|
| 108 |                 void postvisit( IfStmt * );
 | 
|---|
| 109 |                 void postvisit( SwitchStmt * );
 | 
|---|
| 110 |                 void postvisit( CaseStmt * );
 | 
|---|
| 111 |                 void postvisit( BranchStmt * );
 | 
|---|
| 112 |                 void postvisit( ReturnStmt * );
 | 
|---|
| 113 |                 void postvisit( ThrowStmt * );
 | 
|---|
| 114 |                 void postvisit( CatchStmt * );
 | 
|---|
| 115 |                 void postvisit( WaitForStmt * );
 | 
|---|
| 116 |                 void postvisit( WithStmt * );
 | 
|---|
| 117 |                 void postvisit( WhileStmt * );
 | 
|---|
| 118 |                 void postvisit( ForStmt * );
 | 
|---|
| 119 |                 void postvisit( NullStmt * );
 | 
|---|
| 120 |                 void postvisit( DeclStmt * );
 | 
|---|
| 121 |                 void postvisit( ImplicitCtorDtorStmt * );
 | 
|---|
| 122 | 
 | 
|---|
| 123 |                 void genAttributes( std::list< Attribute * > & attributes );
 | 
|---|
| 124 | 
 | 
|---|
| 125 |                 template< class Iterator > void genCommaList( Iterator begin, Iterator end );
 | 
|---|
| 126 | 
 | 
|---|
| 127 |                 struct LabelPrinter {
 | 
|---|
| 128 |                         LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {}
 | 
|---|
| 129 |                         LabelPrinter & operator()( std::list< Label > & l );
 | 
|---|
| 130 |                         CodeGenerator & cg;
 | 
|---|
| 131 |                         std::list< Label > * labels;
 | 
|---|
| 132 |                 };
 | 
|---|
| 133 | 
 | 
|---|
| 134 |                 void asmName( DeclarationWithType *decl );
 | 
|---|
| 135 | 
 | 
|---|
| 136 |                 void extension( Expression *expr );
 | 
|---|
| 137 |                 void extension( Declaration *decl );
 | 
|---|
| 138 | 
 | 
|---|
| 139 |                 void updateLocation( BaseSyntaxNode const * to );
 | 
|---|
| 140 |                 struct LineEnder {
 | 
|---|
| 141 |                         CodeGenerator & cg;
 | 
|---|
| 142 |                         LineEnder( CodeGenerator & cg ) : cg( cg ) {}
 | 
|---|
| 143 |                         std::ostream & operator()(std::ostream &) const;
 | 
|---|
| 144 |                 };
 | 
|---|
| 145 |           private:
 | 
|---|
| 146 |                 Indenter indent;
 | 
|---|
| 147 |                 std::ostream & output;
 | 
|---|
| 148 |                 LabelPrinter printLabels;
 | 
|---|
| 149 |                 Options options;
 | 
|---|
| 150 |           public:
 | 
|---|
| 151 |                 LineEnder endl;
 | 
|---|
| 152 |           private:
 | 
|---|
| 153 | 
 | 
|---|
| 154 |                 CodeLocation currentLocation;
 | 
|---|
| 155 |                 void updateLocation( CodeLocation const & to );
 | 
|---|
| 156 | 
 | 
|---|
| 157 |                 void handleStorageClass( DeclarationWithType *decl );
 | 
|---|
| 158 |                 void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
 | 
|---|
| 159 |                 void handleTypedef( NamedTypeDecl *namedType );
 | 
|---|
| 160 |                 std::string mangleName( DeclarationWithType * decl );
 | 
|---|
| 161 |         }; // CodeGenerator
 | 
|---|
| 162 | 
 | 
|---|
| 163 |         template< class Iterator >
 | 
|---|
| 164 |         void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
 | 
|---|
| 165 |                 if ( begin == end ) return;
 | 
|---|
| 166 |                 for ( ;; ) {
 | 
|---|
| 167 |                         (*begin++)->accept( *visitor );
 | 
|---|
| 168 |                         if ( begin == end ) break;
 | 
|---|
| 169 |                         output << ", ";                                                         // separator
 | 
|---|
| 170 |                 } // for
 | 
|---|
| 171 |         } // genCommaList
 | 
|---|
| 172 | 
 | 
|---|
| 173 |         inline bool doSemicolon( Declaration* decl ) {
 | 
|---|
| 174 |                 if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
 | 
|---|
| 175 |                         return ! func->get_statements();
 | 
|---|
| 176 |                 } // if
 | 
|---|
| 177 |                 return true;
 | 
|---|
| 178 |         } // doSemicolon
 | 
|---|
| 179 | 
 | 
|---|
| 180 |         /// returns C-compatible name of declaration
 | 
|---|
| 181 |         std::string genName( DeclarationWithType * decl );
 | 
|---|
| 182 | 
 | 
|---|
| 183 |         inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) {
 | 
|---|
| 184 |                 return endl( os );
 | 
|---|
| 185 |         }
 | 
|---|
| 186 | } // namespace CodeGen
 | 
|---|
| 187 | 
 | 
|---|
| 188 | // Local Variables: //
 | 
|---|
| 189 | // tab-width: 4 //
 | 
|---|
| 190 | // mode: c++ //
 | 
|---|
| 191 | // compile-command: "make install" //
 | 
|---|
| 192 | // End: //
 | 
|---|