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 | // Autogen.h -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Sun May 17 21:53:34 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Tue May 19 16:49:43 2015 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef AUTOGEN_H |
---|
17 | #define AUTOGEN_H |
---|
18 | |
---|
19 | #include <string> |
---|
20 | #include "SynTree/Statement.h" |
---|
21 | #include "SynTree/Expression.h" |
---|
22 | #include "SynTree/Declaration.h" |
---|
23 | #include "SynTree/Initializer.h" |
---|
24 | #include "InitTweak/InitTweak.h" |
---|
25 | |
---|
26 | namespace SymTab { |
---|
27 | /// Generates assignment operators, constructors, and destructor for aggregate types as required |
---|
28 | void autogenerateRoutines( std::list< Declaration * > &translationUnit ); |
---|
29 | |
---|
30 | /// returns true if obj's name is the empty string and it has a bitfield width |
---|
31 | bool isUnnamedBitfield( ObjectDecl * obj ); |
---|
32 | |
---|
33 | /// size_t type - set when size_t typedef is seen. Useful in a few places, |
---|
34 | /// such as in determining array dimension type |
---|
35 | extern Type * SizeType; |
---|
36 | |
---|
37 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. |
---|
38 | template< typename OutputIterator > |
---|
39 | void genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool forward = true ); |
---|
40 | |
---|
41 | /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Should only be called with non-array types. |
---|
42 | template< typename OutputIterator > |
---|
43 | void genScalarCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out ) { |
---|
44 | // want to be able to generate assignment, ctor, and dtor generically, |
---|
45 | // so fname is either ?=?, ?{}, or ^?{} |
---|
46 | UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) ); |
---|
47 | |
---|
48 | // do something special for unnamed members |
---|
49 | fExpr->get_args().push_back( new AddressExpr( dstParam ) ); |
---|
50 | |
---|
51 | Statement * listInit = srcParam.buildListInit( fExpr ); |
---|
52 | if ( listInit ) { |
---|
53 | *out++ = listInit; |
---|
54 | } |
---|
55 | |
---|
56 | std::list< Expression * > args = *++srcParam; |
---|
57 | fExpr->get_args().splice( fExpr->get_args().end(), args ); |
---|
58 | /* if ( srcParam ) { |
---|
59 | // xxx - |
---|
60 | // make srcParam more complicated |
---|
61 | // if srcParam contains |
---|
62 | fExpr->get_args().push_back( srcParam ); |
---|
63 | } |
---|
64 | */ |
---|
65 | *out++ = new ExprStmt( noLabels, fExpr ); |
---|
66 | } |
---|
67 | |
---|
68 | /// Store in out a loop which calls fname on each element of the array with srcParam and dstParam as arguments. |
---|
69 | /// If forward is true, loop goes from 0 to N-1, else N-1 to 0 |
---|
70 | template< typename OutputIterator > |
---|
71 | void genArrayCall( InitTweak::InitExpander & srcParam, Expression *dstParam, const std::string & fname, OutputIterator out, ArrayType *array, bool forward = true ) { |
---|
72 | static UniqueName indexName( "_index" ); |
---|
73 | |
---|
74 | // for a flexible array member nothing is done -- user must define own assignment |
---|
75 | if ( ! array->get_dimension() ) return ; |
---|
76 | |
---|
77 | Expression * begin, * end, * update, * cmp; |
---|
78 | if ( forward ) { |
---|
79 | // generate: for ( int i = 0; i < 0; ++i ) |
---|
80 | begin = new NameExpr( "0" ); |
---|
81 | end = array->get_dimension()->clone(); |
---|
82 | cmp = new NameExpr( "?<?" ); |
---|
83 | update = new NameExpr( "++?" ); |
---|
84 | } else { |
---|
85 | // generate: for ( int i = N-1; i >= 0; --i ) |
---|
86 | begin = new UntypedExpr( new NameExpr( "?-?" ) ); |
---|
87 | ((UntypedExpr*)begin)->get_args().push_back( array->get_dimension()->clone() ); |
---|
88 | ((UntypedExpr*)begin)->get_args().push_back( new NameExpr( "1" ) ); |
---|
89 | end = new NameExpr( "0" ); |
---|
90 | cmp = new NameExpr( "?>=?" ); |
---|
91 | update = new NameExpr( "--?" ); |
---|
92 | } |
---|
93 | |
---|
94 | ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), NULL ); |
---|
95 | |
---|
96 | UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
97 | init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) ); |
---|
98 | init->get_args().push_back( begin ); |
---|
99 | index->set_init( new SingleInit( init, std::list<Expression*>() ) ); |
---|
100 | |
---|
101 | UntypedExpr *cond = new UntypedExpr( cmp ); |
---|
102 | cond->get_args().push_back( new VariableExpr( index ) ); |
---|
103 | cond->get_args().push_back( end ); |
---|
104 | |
---|
105 | UntypedExpr *inc = new UntypedExpr( update ); |
---|
106 | inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) ); |
---|
107 | |
---|
108 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?[?]" ) ); |
---|
109 | dstIndex->get_args().push_back( dstParam ); |
---|
110 | dstIndex->get_args().push_back( new VariableExpr( index ) ); |
---|
111 | dstParam = dstIndex; |
---|
112 | |
---|
113 | // srcParam must keep track of the array indices to build the |
---|
114 | // source parameter and/or array list initializer |
---|
115 | srcParam.addArrayIndex( new VariableExpr( index ), array->get_dimension()->clone() ); |
---|
116 | |
---|
117 | // if ( srcParam ) { |
---|
118 | // UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) ); |
---|
119 | // srcIndex->get_args().push_back( srcParam ); |
---|
120 | // srcIndex->get_args().push_back( new VariableExpr( index ) ); |
---|
121 | // srcParam = srcIndex; |
---|
122 | // } |
---|
123 | |
---|
124 | // for stmt's body, eventually containing call |
---|
125 | CompoundStmt * body = new CompoundStmt( noLabels ); |
---|
126 | genCall( srcParam, dstParam, fname, back_inserter( body->get_kids() ), array->get_base(), forward ); |
---|
127 | |
---|
128 | // block containing for stmt and index variable |
---|
129 | std::list<Statement *> initList; |
---|
130 | CompoundStmt * block = new CompoundStmt( noLabels ); |
---|
131 | block->get_kids().push_back( new DeclStmt( noLabels, index ) ); |
---|
132 | block->get_kids().push_back( new ForStmt( noLabels, initList, cond, inc, body ) ); |
---|
133 | |
---|
134 | *out++ = block; |
---|
135 | } |
---|
136 | |
---|
137 | template< typename OutputIterator > |
---|
138 | void genCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, Type * type, bool forward ) { |
---|
139 | if ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
140 | genArrayCall( srcParam, dstParam, fname, out, at, forward ); |
---|
141 | } else { |
---|
142 | genScalarCall( srcParam, dstParam, fname, out ); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /// inserts into out a generated call expression to function fname with arguments dstParam |
---|
147 | /// and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. decl is the |
---|
148 | /// object being constructed. The function wraps constructor and destructor calls in an |
---|
149 | /// ImplicitCtorDtorStmt node. |
---|
150 | template< typename OutputIterator > |
---|
151 | void genImplicitCall( InitTweak::InitExpander & srcParam, Expression * dstParam, const std::string & fname, OutputIterator out, DeclarationWithType * decl, bool forward = true ) { |
---|
152 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( decl ); |
---|
153 | assert( obj ); |
---|
154 | // unnamed bit fields are not copied as they cannot be accessed |
---|
155 | if ( isUnnamedBitfield( obj ) ) return; |
---|
156 | |
---|
157 | std::list< Statement * > stmts; |
---|
158 | genCall( srcParam, dstParam, fname, back_inserter( stmts ), obj->get_type(), forward ); |
---|
159 | |
---|
160 | // currently genCall should produce at most one element, but if that changes then the next line needs to be updated to grab the statement which contains the call |
---|
161 | assert( stmts.size() <= 1 ); |
---|
162 | if ( stmts.size() == 1 ) { |
---|
163 | Statement * callStmt = stmts.front(); |
---|
164 | if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && obj->get_bitfieldWidth() == NULL ) ) ) { |
---|
165 | // implicitly generated ctor/dtor calls should be wrapped |
---|
166 | // so that later passes are aware they were generated. |
---|
167 | // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield, |
---|
168 | // because this causes the address to be taken at codegen, which is illegal in C. |
---|
169 | callStmt = new ImplicitCtorDtorStmt( callStmt ); |
---|
170 | } |
---|
171 | *out++ = callStmt; |
---|
172 | } |
---|
173 | } |
---|
174 | } // namespace SymTab |
---|
175 | #endif // AUTOGEN_H |
---|
176 | |
---|