source: src/SymTab/Autogen.h @ 5f98ce5

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglerjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 5f98ce5 was 5f98ce5, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

hoist non-constexpr array dimension into const variable in case of side effects

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