Changeset a32b204 for translator/ResolvExpr/ConversionCost.cc
- Timestamp:
- May 17, 2015, 1:19:35 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:
- 0dd3a2f
- Parents:
- b87a5ed
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/ResolvExpr/ConversionCost.cc
rb87a5ed ra32b204 1 /* 2 * This file is part of the Cforall project 3 * 4 * $Id: ConversionCost.cc,v 1.11 2005/08/29 20:14:15 rcbilson Exp $ 5 * 6 */ 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 // ConversionCost.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Sun May 17 07:06:19 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun May 17 07:22:19 2015 13 // Update Count : 4 14 // 7 15 8 16 #include "ConversionCost.h" … … 12 20 #include "SymTab/Indexer.h" 13 21 14 15 22 namespace ResolvExpr { 16 17 const Cost Cost::zero = Cost( 0, 0, 0 ); 18 const Cost Cost::infinity = Cost( -1, -1, -1 ); 19 20 Cost 21 conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) 22 { 23 if( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { 24 EqvClass eqvClass; 25 NamedTypeDecl *namedType; 23 const Cost Cost::zero = Cost( 0, 0, 0 ); 24 const Cost Cost::infinity = Cost( -1, -1, -1 ); 25 26 Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { 27 if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { 28 EqvClass eqvClass; 29 NamedTypeDecl *namedType; 26 30 /// std::cout << "type inst " << destAsTypeInst->get_name(); 27 if( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {28 29 } else if( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {31 if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { 32 return conversionCost( src, eqvClass.type, indexer, env ); 33 } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) { 30 34 /// std::cout << " found" << std::endl; 31 32 33 34 if( type->get_base() ) {35 36 } 37 } 35 TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); 36 // all typedefs should be gone by this point 37 assert( type ); 38 if ( type->get_base() ) { 39 return conversionCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 ); 40 } // if 41 } // if 38 42 /// std::cout << " not found" << std::endl; 39 } 43 } // if 40 44 /// std::cout << "src is "; 41 45 /// src->print( std::cout ); … … 44 48 /// std::cout << std::endl << "env is" << std::endl; 45 49 /// env.print( std::cout, 8 ); 46 if( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {50 if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { 47 51 /// std::cout << "compatible!" << std::endl; 48 return Cost( 0, 0, 0 ); 49 } else if( dynamic_cast< VoidType* >( dest ) ) { 50 return Cost( 0, 0, 1 ); 51 } else { 52 ConversionCost converter( dest, indexer, env ); 53 src->accept( converter ); 54 if( converter.get_cost() == Cost::infinity ) { 55 return Cost::infinity; 56 } else { 57 return converter.get_cost() + Cost( 0, 0, 0 ); 58 } 59 } 60 } 61 62 ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) 63 : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) 64 { 65 } 52 return Cost( 0, 0, 0 ); 53 } else if ( dynamic_cast< VoidType* >( dest ) ) { 54 return Cost( 0, 0, 1 ); 55 } else { 56 ConversionCost converter( dest, indexer, env ); 57 src->accept( converter ); 58 if ( converter.get_cost() == Cost::infinity ) { 59 return Cost::infinity; 60 } else { 61 return converter.get_cost() + Cost( 0, 0, 0 ); 62 } // if 63 } // if 64 } 65 66 ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) 67 : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) { 68 } 66 69 67 70 /* 68 Old69 ===70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 New89 ===90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 71 Old 72 === 73 Double 74 | 75 Float 76 | 77 ULong 78 / \ 79 UInt Long 80 \ / 81 Int 82 | 83 Ushort 84 | 85 Short 86 | 87 Uchar 88 / \ 89 Schar Char 90 91 New 92 === 93 +-----LongDoubleComplex--+ 94 LongDouble--+ | +-LongDoubleImag 95 | +---DoubleComplex---+ | 96 Double------+ | +----DoubleImag 97 | +-FloatComplex-+ | 98 Float---------+ +-------FloatImag 99 | 100 ULongLong 101 | 102 LongLong 103 | 104 ULong 105 / \ 106 UInt Long 107 \ / 108 Int 109 | 110 Ushort 111 | 112 Short 113 | 114 Uchar 115 / \ 116 Schar Char 117 \ / 118 Bool 116 119 */ 117 120 118 static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = 119 { 120 /* Src \ Dest: Bool Char SChar UChar Short UShort Int UInt Long ULong LLong ULLong Float Double LDbl FCplex DCplex LDCplex FImag DImag LDImag */ 121 /* Bool */ { 0, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12, 11, 12, 13, -1, -1, -1 }, 122 /* Char */ { -1, 0, -1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 }, 123 /* SChar */ { -1, -1, 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 }, 124 /* UChar */ { -1, -1, -1, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 9, 10, 11, -1, -1, -1 }, 125 /* Short */ { -1, -1, -1, -1, 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 8, 9, 10, -1, -1, -1 }, 126 /* UShort */ { -1, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 7, 8, 9, -1, -1, -1 }, 127 /* Int */ { -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 3, 4, 5, 6, 7, 6, 7, 8, -1, -1, -1 }, 128 /* UInt */ { -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 }, 129 /* Long */ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 }, 130 /* ULong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 4, 5, 6, -1, -1, -1 }, 131 /* LLong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 3, 4, 5, -1, -1, -1 }, 132 /* ULLong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 2, 3, 4, -1, -1, -1 }, 133 /* Float */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, 2, 3, -1, -1, -1 }, 134 /* Double */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 2, -1, -1, -1 }, 135 /* LDbl */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, -1, -1 }, 136 /* FCplex */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, -1, -1 }, 137 /* DCplex */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, -1 }, 138 /* LDCplex */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 }, 139 /* FImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 0, 1, 2 }, 140 /* DImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, -1, 0, 1 }, 141 /* LDImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 0 } 142 }; 143 144 void 145 ConversionCost::visit(VoidType *voidType) 146 { 147 cost = Cost::infinity; 148 } 149 150 void 151 ConversionCost::visit(BasicType *basicType) 152 { 153 if( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { 154 int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ]; 155 if( tableResult == -1 ) { 156 cost = Cost( 1, 0, 0 ); 157 } else { 158 cost = Cost( 0, 0, tableResult ); 159 } 160 } 161 } 162 163 void 164 ConversionCost::visit(PointerType *pointerType) 165 { 166 if( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { 167 if( pointerType->get_base()->get_qualifiers() <= destAsPtr->get_base()->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { 168 cost = Cost( 0, 0, 1 ); 169 } else { 170 int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env ); 171 if( assignResult < 0 ) { 172 cost = Cost( 0, 0, 1 ); 173 } else if( assignResult > 0 ) { 174 cost = Cost( 1, 0, 0 ); 175 } 176 } 177 } 178 } 179 180 void 181 ConversionCost::visit(ArrayType *arrayType) 182 { 183 } 184 185 void 186 ConversionCost::visit(FunctionType *functionType) 187 { 188 } 189 190 void 191 ConversionCost::visit(StructInstType *inst) 192 { 193 if( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { 194 if( inst->get_name() == destAsInst->get_name() ) { 195 cost = Cost::zero; 196 } 197 } 198 } 199 200 void 201 ConversionCost::visit(UnionInstType *inst) 202 { 203 if( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { 204 if( inst->get_name() == destAsInst->get_name() ) { 205 cost = Cost::zero; 206 } 207 } 208 } 209 210 void 211 ConversionCost::visit(EnumInstType *inst) 212 { 213 static Type::Qualifiers q; 214 static BasicType integer( q, BasicType::SignedInt ); 215 integer.accept( *this ); 216 if( cost < Cost( 1, 0, 0 ) ) { 217 cost.incSafe(); 218 } 219 } 220 221 void 222 ConversionCost::visit(ContextInstType *inst) 223 { 224 } 225 226 void 227 ConversionCost::visit(TypeInstType *inst) 228 { 229 EqvClass eqvClass; 230 NamedTypeDecl *namedType; 231 if( env.lookup( inst->get_name(), eqvClass ) ) { 232 cost = conversionCost( eqvClass.type, dest, indexer, env ); 233 } else if( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) { 234 if( inst->get_name() == destAsInst->get_name() ) { 235 cost = Cost::zero; 236 } 237 } else if( ( namedType = indexer.lookupType( inst->get_name() ) ) ) { 238 TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); 239 // all typedefs should be gone by this point 240 assert( type ); 241 if( type->get_base() ) { 242 cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost( 0, 0, 1 ); 243 } 244 } 245 } 246 247 void 248 ConversionCost::visit(TupleType *tupleType) 249 { 250 Cost c; 251 if( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { 252 std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); 253 std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); 254 while( srcIt != tupleType->get_types().end() ) { 255 Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env ); 256 if( newCost == Cost::infinity ) { 257 return; 258 } 259 c += newCost; 260 } 261 if( destIt != destAsTuple->get_types().end() ) { 262 cost = Cost::infinity; 263 } else { 264 cost = c; 265 } 266 } 267 } 268 121 static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = 122 { 123 /* Src \ Dest: Bool Char SChar UChar Short UShort Int UInt Long ULong LLong ULLong Float Double LDbl FCplex DCplex LDCplex FImag DImag LDImag */ 124 /* Bool */ { 0, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12, 11, 12, 13, -1, -1, -1 }, 125 /* Char */ { -1, 0, -1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 }, 126 /* SChar */ { -1, -1, 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 }, 127 /* UChar */ { -1, -1, -1, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 9, 10, 11, -1, -1, -1 }, 128 /* Short */ { -1, -1, -1, -1, 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 8, 9, 10, -1, -1, -1 }, 129 /* UShort */{ -1, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 7, 8, 9, -1, -1, -1 }, 130 /* Int */ { -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 3, 4, 5, 6, 7, 6, 7, 8, -1, -1, -1 }, 131 /* UInt */ { -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 }, 132 /* Long */ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 }, 133 /* ULong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 4, 5, 6, -1, -1, -1 }, 134 /* LLong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 3, 4, 5, -1, -1, -1 }, 135 /* ULLong */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 2, 3, 4, -1, -1, -1 }, 136 /* Float */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, 2, 3, -1, -1, -1 }, 137 /* Double */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 2, -1, -1, -1 }, 138 /* LDbl */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, -1, -1 }, 139 /* FCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, -1, -1 }, 140 /* DCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, -1 }, 141 /* LDCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 }, 142 /* FImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 0, 1, 2 }, 143 /* DImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, -1, 0, 1 }, 144 /* LDImag */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 0 } 145 }; 146 147 void ConversionCost::visit(VoidType *voidType) { 148 cost = Cost::infinity; 149 } 150 151 void ConversionCost::visit(BasicType *basicType) { 152 if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { 153 int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ]; 154 if ( tableResult == -1 ) { 155 cost = Cost( 1, 0, 0 ); 156 } else { 157 cost = Cost( 0, 0, tableResult ); 158 } // if 159 } // if 160 } 161 162 void ConversionCost::visit(PointerType *pointerType) { 163 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { 164 if ( pointerType->get_base()->get_qualifiers() <= destAsPtr->get_base()->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { 165 cost = Cost( 0, 0, 1 ); 166 } else { 167 int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env ); 168 if ( assignResult < 0 ) { 169 cost = Cost( 0, 0, 1 ); 170 } else if ( assignResult > 0 ) { 171 cost = Cost( 1, 0, 0 ); 172 } // if 173 } // if 174 } // if 175 } 176 177 void ConversionCost::visit(ArrayType *arrayType) { 178 } 179 180 void ConversionCost::visit(FunctionType *functionType) { 181 } 182 183 void ConversionCost::visit(StructInstType *inst) { 184 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { 185 if ( inst->get_name() == destAsInst->get_name() ) { 186 cost = Cost::zero; 187 } // if 188 } // if 189 } 190 191 void ConversionCost::visit(UnionInstType *inst) { 192 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { 193 if ( inst->get_name() == destAsInst->get_name() ) { 194 cost = Cost::zero; 195 } // if 196 } // if 197 } 198 199 void ConversionCost::visit(EnumInstType *inst) { 200 static Type::Qualifiers q; 201 static BasicType integer( q, BasicType::SignedInt ); 202 integer.accept( *this ); 203 if ( cost < Cost( 1, 0, 0 ) ) { 204 cost.incSafe(); 205 } // if 206 } 207 208 void ConversionCost::visit(ContextInstType *inst) { 209 } 210 211 void ConversionCost::visit(TypeInstType *inst) { 212 EqvClass eqvClass; 213 NamedTypeDecl *namedType; 214 if ( env.lookup( inst->get_name(), eqvClass ) ) { 215 cost = conversionCost( eqvClass.type, dest, indexer, env ); 216 } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) { 217 if ( inst->get_name() == destAsInst->get_name() ) { 218 cost = Cost::zero; 219 } 220 } else if ( ( namedType = indexer.lookupType( inst->get_name() ) ) ) { 221 TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); 222 // all typedefs should be gone by this point 223 assert( type ); 224 if ( type->get_base() ) { 225 cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost( 0, 0, 1 ); 226 } // if 227 } // if 228 } 229 230 void ConversionCost::visit(TupleType *tupleType) { 231 Cost c; 232 if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { 233 std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); 234 std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); 235 while ( srcIt != tupleType->get_types().end() ) { 236 Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env ); 237 if ( newCost == Cost::infinity ) { 238 return; 239 } // if 240 c += newCost; 241 } // while 242 if ( destIt != destAsTuple->get_types().end() ) { 243 cost = Cost::infinity; 244 } else { 245 cost = c; 246 } // if 247 } // if 248 } 269 249 } // namespace ResolvExpr 250 251 // Local Variables: // 252 // tab-width: 4 // 253 // mode: c++ // 254 // compile-command: "make install" // 255 // End: //
Note: See TracChangeset
for help on using the changeset viewer.