Changeset 56fcd77 for src/GenPoly
- Timestamp:
- Dec 14, 2015, 4:06:26 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- db0b3ce
- Parents:
- cdec5af
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
rcdec5af r56fcd77 14 14 // 15 15 16 #include <map>17 16 #include <set> 18 #include <sstream>19 17 #include <stack> 20 18 #include <string> … … 22 20 #include <algorithm> 23 21 #include <cassert> 24 #include <utility>25 22 26 23 #include "Box.h" … … 54 51 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ); 55 52 56 /// Cache for layout struct decls57 class LayoutStructDecls {58 public:59 /// Gets the layout struct declaration for a given number of members (0 for non-generic); creates declarations as needed.60 /// Declarations are allocated with "new", not freed on LayoutStructDecls destruction61 StructDecl* get( int members );62 /// adds all struct declarations to the translation unit63 void addToTranslationUnit( std::list< Declaration* >& translationUnit );64 private:65 /// Layout struct declarations, indexed by number of members in the generic struct (0 for non-generic)66 std::map< int, StructDecl* > decls;67 };68 69 53 /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call 70 54 class Pass1 : public PolyMutator { 71 55 public: 72 Pass1( LayoutStructDecls &layoutDecls);56 Pass1(); 73 57 virtual Expression *mutate( ApplicationExpr *appExpr ); 74 58 virtual Expression *mutate( AddressExpr *addrExpr ); … … 104 88 bool useRetval; 105 89 UniqueName tempNamer; 106 LayoutStructDecls &layoutDecls;107 90 }; 108 91 … … 110 93 class Pass2 : public PolyMutator { 111 94 public: 112 Pass2( LayoutStructDecls &layoutDecls );113 95 template< typename DeclClass > 114 96 DeclClass *handleDecl( DeclClass *decl, Type *type ); … … 123 105 124 106 std::map< UniqueId, std::string > adapterName; 125 LayoutStructDecls &layoutDecls;126 107 }; 127 108 … … 129 110 class Pass3 : public PolyMutator { 130 111 public: 131 Pass3( LayoutStructDecls &layoutDecls );132 112 template< typename DeclClass > 133 113 DeclClass *handleDecl( DeclClass *decl, Type *type ); … … 140 120 virtual Type *mutate( FunctionType *funcType ); 141 121 private: 142 LayoutStructDecls &layoutDecls;143 122 }; 144 123 … … 155 134 156 135 void box( std::list< Declaration *>& translationUnit ) { 157 LayoutStructDecls layoutStructs; 158 Pass1 pass1( layoutStructs ); 159 Pass2 pass2( layoutStructs ); 160 Pass3 pass3( layoutStructs ); 136 Pass1 pass1; 137 Pass2 pass2; 138 Pass3 pass3; 161 139 mutateAll( translationUnit, pass1 ); 162 140 mutateAll( translationUnit, pass2 ); 163 141 mutateAll( translationUnit, pass3 ); 164 layoutStructs.addToTranslationUnit( translationUnit );165 }166 167 //////////////////////////////////// LayoutStructDecls //////////////////////////////////////////////168 169 namespace {170 StructDecl* LayoutStructDecls::get( int members ) {171 // Attempt to read declaration already in map172 std::map< int, StructDecl* >::iterator decl = decls.find( members );173 if ( decl != decls.end() ) return decl->second;174 175 // Insert if not found176 std::string member_str;177 if ( members > 0 ) {178 std::stringstream ss;179 ss << members;180 member_str = ss.str();181 }182 StructDecl *newDecl = new StructDecl( std::string("_layout") + member_str );183 184 newDecl->get_members().push_back(185 new ObjectDecl( "size", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ) );186 newDecl->get_members().push_back(187 new ObjectDecl( "align", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ) );188 if ( members > 0 ) {189 ArrayType *aType =190 new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ),191 new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), member_str ) ), false, false );192 newDecl->get_members().push_back(193 new ObjectDecl( "offsets", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, aType, 0 ) );194 }195 196 decls.insert( std::make_pair( members, newDecl ) );197 198 return newDecl;199 }200 201 void LayoutStructDecls::addToTranslationUnit( std::list< Declaration* >& translationUnit ) {202 for ( std::map< int, StructDecl* >::reverse_iterator decl = decls.rbegin(); decl != decls.rend(); ++decl ) {203 translationUnit.push_front( decl->second );204 }205 }206 142 } 207 143 … … 245 181 } 246 182 247 Pass1::Pass1( LayoutStructDecls &layoutDecls ) 248 : useRetval( false ), tempNamer( "_temp" ), layoutDecls( layoutDecls ) { 183 Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) { 249 184 adapters.push(AdapterMap()); 250 185 } … … 374 309 Type *concrete = env->lookup( tyParm->first ); 375 310 if ( concrete ) { 311 // TODO add alignment 376 312 arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) ); 377 313 arg++; … … 1000 936 ////////////////////////////////////////// Pass2 //////////////////////////////////////////////////// 1001 937 1002 Pass2::Pass2( LayoutStructDecls &layoutDecls ) : layoutDecls(layoutDecls) {}1003 1004 938 void Pass2::addAdapters( FunctionType *functionType ) { 1005 939 std::list< DeclarationWithType *> ¶mList = functionType->get_parameters(); … … 1074 1008 std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin(); 1075 1009 std::list< DeclarationWithType *> inferredParams; 1010 // TODO add alignment 1076 1011 ObjectDecl *newObj = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 ); 1077 1012 // ObjectDecl *newFunPtr = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ); … … 1104 1039 ////////////////////////////////////////// Pass3 //////////////////////////////////////////////////// 1105 1040 1106 Pass3::Pass3( LayoutStructDecls &layoutDecls ) : layoutDecls(layoutDecls) {}1107 1108 1041 template< typename DeclClass > 1109 1042 DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
Note: See TracChangeset
for help on using the changeset viewer.