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