history.cxx

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright (C) 2006, Johns Hopkins University.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or
00007  * without modification, are permitted provided that the following
00008  * conditions are met:
00009  *
00010  *   - Redistributions of source code must contain the above 
00011  *     copyright notice, this list of conditions, and the following
00012  *     disclaimer. 
00013  *
00014  *   - Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions, and the following
00016  *     disclaimer in the documentation and/or other materials 
00017  *     provided with the distribution.
00018  *
00019  *   - Neither the names of the copyright holders nor the names of any
00020  *     of any contributors may be used to endorse or promote products
00021  *     derived from this software without specific prior written
00022  *     permission. 
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00027  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  **************************************************************************/
00037 
00038 #if 0
00040 //     Type system history                     //
00042 // -------------- !!! DO  NOT DELETE !!!   ----------------- //
00043 // This is an older version of Type Specialize. 
00044 // It HAS IMPORTANT COMMENTS that no longer apply, but need to be 
00045 // reviewed whenever a change is necessary.
00046 // DO NOT DELETE.
00047 // // The following function has the distinction of being the most
00048 // // debugged (and most vulnerale) function.
00049 GCPtr<Type>
00050 Type::TypeSpecialize(CVector<GCPtr <Type> >& ftvs,
00051                      CVector<GCPtr <Type> >& nftvs)
00052 {
00053   Type *t = getType();
00054   Type *theType = new Type(t->kind, t->ast);
00055   theType->arrlen = t->arrlen;
00056   theType->defAst = t->defAst;
00057   theType->myContainer = t->myContainer;
00058   GCPtr<Type> retType = theType;
00059   
00060   //std::cout << "To Specialize " << '`' << ast->s << '\''
00061   //        << this->asString()  << std::endl;  
00062 #if 0  
00063   // NO ARBITRARY RECURSION FOR NOW
00064   if((t->kind != ty_fix) && (t->mark & MARK)) {
00065     Type *tv = new Type(ty_tvar, t->ast);
00066     tv->link = t;
00067     GCPtr<Type> fix = new Type(ty_fix, t->ast);
00068     fix->components.append(new comp(tv));
00069     retType = fix;
00070     return retType;
00071   }  
00072   ...    
00073   t->mark |= MARK;
00074   ...
00075 #endif
00076 
00077   if(t->sp != NULL)
00078     retType = t->sp;
00079   else {    
00080     t->sp = retType;
00081 
00082     switch(t->kind) {
00083     case ty_fix:
00084       {
00085         Type *tv = new Type(ty_tvar, t->ast);
00086         Type *sp = t->components[0]->typ->getType();
00087         tv->link = sp;
00088         retType = tv;
00089         break;
00090       }
00091  
00092     case ty_tvar:
00093       {
00094         size_t i=0;
00095         for(i=0; i<ftvs.size(); i++) {
00096           Type *ftv = ftvs[i]->getType();
00097           //assert(ftv->kind == ty_tvar);
00098           if(ftv->kind == ty_tvar && t->uniqueID == ftv->uniqueID) {
00099             theType->link = nftvs[i]; // ***
00100             break;
00101           }
00102         }
00103 
00104         // Instead of ***, was:
00105         //      if(nftvs[i]->link == NULL)
00106         //        nftvs[i]->link = theType;     
00107         //      else
00108         //        retType = nftvs[i]->link;
00109 
00110 
00111         // If the variable was NOT in ftv list, then 
00112         // we should link it to the original, in order to honor
00113         // variable capture
00114         if(i == ftvs.size())
00115           theType->link = t;
00116    
00117         // THE FOLLOWING IS WRONG !!! 
00118         // This will not honor variable capture
00119         // if(theType->link == NULL) {
00120         //   GCPtr<Type> mytype = theType;
00121         //   ftvs.append(t); 
00122         //   nftvs.append(mytype);       
00123         // }
00124         break;
00125       }
00126 
00127     default:
00128       {      
00129         /* Deal with Type-args */
00130         for(size_t i=0; i<t->typeArgs.size(); i++) {
00131           Type *arg = t->typeArgs[i]->getType();
00132           Type *newArg = new Type(ty_tvar, arg->ast);
00133 
00134           if(arg->kind != ty_tvar) {
00135             newArg->link = arg;
00136           }
00137           else {
00138             for(size_t j=0; j<ftvs.size(); j++) {
00139               Type *ftv = ftvs[j]->getType();
00140               //assert(ftv->kind == ty_tvar);
00141               if(ftv->kind == ty_tvar  && arg->uniqueID == ftv->uniqueID) {
00142                 newArg->link = nftvs[j];    
00143                 break;
00144               }
00145             }
00146  
00147             //  std::cout << "arg = " << arg->asString() 
00148             //            << " ftvs = ";
00149             //  for(size_t j=0; j<ftvs.size(); j++) 
00150             //    std::cout << " " << ftvs[j]->asString();        
00151             //  std::cout << std::endl;
00152             // The HISTORICAL assert.
00153             // assert(newArg->link != NULL);
00154             // This has been removed because, if I have:
00155             // (define c12  (lambda (clArg:(list 'd)) clArg))
00156             // when time comes to specialize 
00157             //  To Specialize `clArg'(list 'a101)
00158             //  arg = 'a101 ftvs = 
00159             // clArg is in the environment, but has NOT been 
00160             // generalized YET.
00161             // So, in order to honor variable capture, we must do:
00162             if(newArg->link == NULL)
00163               newArg->link = arg;         
00164           } 
00165           theType->typeArgs.append(newArg);
00166         }
00167    
00168    
00169         /* Deal with Components */
00170         for(size_t i=0; i<t->components.size(); i++) {
00171           comp *nComp = 
00172             new comp(t->components[i]->name,
00173                      t->components[i]->typ->TypeSpecialize(ftvs, nftvs));
00174           theType->components.append(nComp);
00175         }
00176         break;
00177       }      
00178     }
00179     t->sp =  NULL;
00180   }
00181   //   std::cout << "\t Specialized " << '`' << ast->s << '\''
00182   //                << *this << " to " << *retType
00183   //                << std::endl;
00184  
00185   return retType;
00186 }
00187  
00188 Old Super constraint Handling:
00189     case at_super:
00190       { 
00191         for(size_t d = 0; d < tcdecl->children.size(); d++) {
00192           AST *tcApp = tcdecl->children[d];
00193           TYPEINFER(tcApp, gamma, instEnv, impTypes, isVP, 
00194                     sigma->tcc, uflags, trail, USE_MODE, 
00195                     TI_TYP_EXP | TI_TYP_APP | TI_TCC_SUB);
00196           
00197           if(!errFree)
00198             break;
00199                   
00200           AST *superIdent = tcApp->children[0];
00201           AST *superAST = superIdent->symbolDef;        
00202 
00203           // Make sure that the superclass declarations are
00204           // acyclic (form a ADG)
00205           if(!superDAG(superAST, ident)) {
00206             errStream << tcdecl->loc << ": "
00207                       << "super-class declarations must form a DAG,"
00208                       << " but this declaration forms a loop"
00209                       << std::endl;
00210             errFree = false;
00211           }
00212         
00213           if(!errFree)
00214             break;
00215 
00216           Typeclass *sup = superIdent->symType->getType();      
00217           sup->flags |= TY_CT_SUBSUMED;
00218           sigma->tcc->addPred(sup);
00219         }
00220         break;
00221       }
00222 
00223  
00224 // VERBOSE VERSION OF COLLECTALLFTVSWRTGAMMA
00225 static void
00226 collectftvsWrtGamma(Type *typ,
00227                     CVector<Type *>& tvs,
00228                     const Environment<TypeScheme> *gamma,
00229                     std::string pad = "")
00230 {   
00231   Type *t = typ->getType();
00232 
00233   if(t->mark & MARK2)
00234     return;
00235 
00236   t->mark |= MARK2;
00237 
00238   std::string myPad = pad;
00239   pad += "  ";
00240   //std::cout << myPad <<"Marked  " << t->asString() << std::endl;
00241 
00242   if(t->kind == ty_tvar) {
00243     assert(t->components.size() == 0);
00244     if(!boundInGamma(t, gamma) && !(tvs.contains(t))) 
00245       tvs.append(t);
00246   }
00247   else {
00248     for(size_t i=0; i < t->components.size(); i++) {
00249       if(!(t->mark & MARK2)) {
00250         std::cerr << myPad << t->asString() << ": comp = " << i 
00251                   << " MARK IS CLEAR" 
00252                   << std::endl;
00253         assert(false);
00254       }
00255       collectftvsWrtGamma(t->components[i]->typ, tvs, gamma, pad);
00256     }
00257 
00258     for(size_t i=0; i < t->typeArgs.size(); i++) {
00259       if(!(t->mark & MARK2)) {
00260         std::cerr << myPad << t->asString() << ": arg = " << i 
00261                   << " MARK IS CLEAR" 
00262                   << std::endl;
00263         assert(false);
00264       }
00265       collectftvsWrtGamma(t->typeArgs[i], tvs, gamma, pad);
00266     }
00267 
00268     if(t->fnDeps)
00269       for(size_t i=0; i < t->fnDeps->size(); i++) {
00270         if(!(t->mark & MARK2)) {
00271           std::cerr << myPad << t->asString() << ": fnDep = " << i 
00272                     << " MARK IS CLEAR" 
00273                     << std::endl;
00274           assert(false);
00275         }
00276         collectftvsWrtGamma(FNDEP(t)[i], tvs, gamma, pad);
00277       }    
00278   }
00279 
00280   //std::cout << myPad << "Cleared " << t->asString() << std::endl;
00281   t->mark &= ~MARK2;
00282 }
00283 
00284 
00285 // Old Instance checking for disjointness
00286   if(!mySigma->tau->equals(hisSigma->tau))
00287     return false;
00288 
00289   if(mySigma->tcc == NULL)
00290     return true;
00291 
00292   if(hisSigma->tcc == NULL)
00293     return true;
00294   
00295   CVector<Type *> ftvs;
00296   collectAllftvs(mySigma->tau, ftvs);
00297   
00298   bool uniquefound = false;
00299   for(size_t i=0; i < mySigma->tcc->pred.size(); i++) {
00300     Typeclass *myPred = mySigma->tcc->pred[i];
00301     
00302     bool found = false;
00303     for(size_t j=0; j < hisSigma->tcc->pred.size(); j++) {
00304       Typeclass *hisPred = hisSigma->tcc->pred[j];      
00305       if(myPred->equals(hisPred)) {
00306         found = true;
00307         break;
00308       }
00309     }
00310 
00311     if(!found) {
00312       uniquefound = true ;
00313       break;
00314     }
00315   }
00316 
00317   if(!uniquefound)
00318     return true;
00319 
00320   uniquefound = false;
00321   for(size_t i=0; i < hisSigma->tcc->pred.size(); i++) {
00322     Typeclass *hisPred = hisSigma->tcc->pred[i];
00323     
00324     bool found = false;
00325     for(size_t j=0; j < mySigma->tcc->pred.size(); j++) {
00326       Typeclass *myPred = mySigma->tcc->pred[j];      
00327       if(hisPred->equals(myPred)) {
00328         found = true;
00329         break;
00330       }
00331     }
00332 
00333     if(!found) {
00334       uniquefound = true;
00335       break;
00336     }
00337   }
00338 
00339   if(!uniquefound)
00340     return true;
00341   else
00342     return false;
00343   
00344 // Old Additional FnDep checking while solving predicates.
00345 
00346   if(!errFree)
00347     return false;
00348   
00349   for(size_t i=0; i < tcc->pred.size(); i++) {
00350     Typeclass *pred1 = tcc->pred[i];
00351     
00352     for(size_t j=i+1; j < tcc->pred.size(); j++) {
00353       Typeclass *pred2 = tcc->pred[i];
00354     
00355       if(pred1->defAst != pred2->defAst)
00356         break;
00357 
00358       if((pred1->fnDeps != NULL) && (pred2->fnDeps != NULL)) {
00359         assert(pred1->fnDeps->size() == pred2->fnDeps->size());\
00360 
00361         for(size_t fd = 0; fd < pred1->fnDeps->size(); fd++) {
00362           Type *fnDep1 = FNDEP(pred1)[fd];
00363           Type *fnDep2 = FNDEP(pred2)[fd];
00364           
00365           assert(fnDep1->defAst == fnDep2->defAst);
00366 
00367           Type *fnDep1domain = fnDep1->components[0]->typ->getType();
00368           Type *fnDep2domain = fnDep2->components[0]->typ->getType();
00369 
00370           if(fnDep1domain->equals(fnDep2domain)) {
00371             sherpa::CVector<Type *> trail;
00372             AST *errAst = new AST(at_Null, errLoc);
00373             CHKERR(errFree, unify(errStream, trail, NULL, errAst, 
00374                                   fnDep1, fnDep2, 0));
00375             if(!errFree) {
00376               errStream << errLoc << ": "
00377                         << "The following leads to a contradiction:\n"
00378                         << pred1->asString() << "\n"
00379                         << pred2->asString() << "\n"
00380                         << fnDep1->asString() << "\n"
00381                         << fnDep2->asString() 
00382                         << std::endl;
00383               errFree = false;
00384               break;
00385             }
00386           }
00387         }       
00388       }
00389     }    
00390   }
00391 
00392 
00393 bool 
00394 Typeclass::TCCspecialized)
00395 {
00396   if(link)
00397     return getType()->TCCspecialized();
00398   
00399   CVector<Type *> closure;
00400   CVector<Type *> tvs;
00401 
00402   for(size_t i=0; i < typeArgs.size(); i++) {
00403     Type *arg = typeArgs[i]->getType();
00404     if(!arg->isTvar())
00405       closure.append(arg);
00406     else
00407       tvs.append(arg);
00408   }
00409 
00410   if(fnDeps)
00411     close(closure, *fnDeps);
00412 
00413   for(size_t i=0; i < tvs.size(); i++)
00414     if(!closure.contains(tvs[i]))
00415       return false;
00416 
00417   return true;
00418 }
00419 
00420 
00422 
00423 
00424 // New Polyinstantiator: Old FQN lookup scheme
00425 {
00426   AST *mod = uoc->ast->children[0];
00427   for(size_t c = 0; c < mod->children.size(); c++) {
00428     AST *form = mod->children[c];
00429     
00430     if(form->astType != at_define && form->astType != at_proclaim)
00431     continue;
00432     
00433     AST *ident = form->getID();
00434     
00435     if (ident->fqn.asString() == fqn) {
00436       if (ident->defn)
00437         ident = ident->defn;
00438       
00439       AST *defForm = ident->defForm;
00440         
00441       return defForm;
00442     }
00443   }
00444 }
00445 
00446 
00447 
00449       /* Need to emit:
00450          (let ((_tmp  (ALLOC-CLOSURE closureEnv Fn)))
00451          (SET!-CLOSURE (clenvName fv1 ... fvn)  lamName)
00452          _tmp) */
00453       
00454       
00455 
00456       AST *clTmp = AST::genSym(ast, "CLS");
00457       AST *clPat = new AST(at_identPattern, clTmp->loc, clTmp);
00458       AST *fnType = ast->symType->asAST(ast->loc);
00459       assert(fnType->children.size() == 2);
00460       fnType->children[0] = cl_convert_ast(fnType->children[0],
00461                                            outAsts, shouldHoist);
00462       fnType->children[1] = cl_convert_ast(fnType->children[1],
00463                                            outAsts, shouldHoist);
00464       
00465       //AST *fnType = cl_convert_ast(ast->symType->asAST(ast->loc),
00466       //                               outAsts, shouldHoist);
00467       AST *allocClosure = new AST(at_allocClosure, ast->loc,
00468                                   fnType);
00469       AST *clLb = new AST(at_letbinding, clPat->loc, 
00470                           clPat, allocClosure);
00471       AST *clLbs = new AST(at_letbindings, clLb->loc, clLb);
00472       
00473       AST *envApp = new AST(at_struct_apply, ast->loc);
00474       envApp->addChild(clenvName->Use());
00475       
00476       if(freeVars.size() > 0)
00477         for (size_t fv = 0; fv < freeVars.size(); fv++)
00478           envApp->addChild(freeVars[fv]->Use());            
00479       else
00480         envApp->addChild(new AST(at_unit, envApp->loc));
00481       
00482       AST *setClosure = new AST(at_set_closure, ast->loc, 
00483                                 clTmp->Use(),
00484                                 envApp, lamName->Use());
00485       AST *begin = new AST(at_begin, ast->loc, setClosure,
00486                            clTmp->Use());
00487       AST *clLet = new AST(at_let, ast->loc, clLbs, begin,
00488                            new AST(at_constraints, ast->loc)); 
00489       ast = clLet;
00490       SHAPDEBUG ast->PrettyPrint(ast);   
00491 
00492       if(ast->children[0]->astType == at_identPattern &&
00493          (ast->children[1]->astType == at_lambda))
00494         hoistChildren = HOISTALL;    
00495 
00496           if (ast->symbolDef->Flags2 & ID_IS_RECBOUND) {
00497             ast->Flags2 |= (ID_NEEDS_HEAPIFY|ID_IS_CLOSED);
00498             ast->symbolDef->Flags2 |= (ID_NEEDS_HEAPIFY|ID_IS_CAPTURED);
00499           }
00500       
00501 
00503   for(size_t i=0; i < UocInfo::ifList.size(); i++) {
00504     if(&*((UocInfo::ifList)[i]) == this) 
00505       std::cout << "My IF no. is " << i << std::endl; 
00506   }
00507 
00508   for(size_t i=0; i < UocInfo::srcList.size(); i++) {
00509     if(&*((UocInfo::srcList)[i]) == this) 
00510       std::cout << "My SRC no. is " << i << std::endl; 
00511   }
00512 
00513 
00514 
00515   std::cout << "SIZE of Bindings = " 
00516             << "[" << &*env << "] "
00517             << env->bindings.size()
00518             << std::endl;
00519   for (size_t i = 0; i < env->bindings.size(); i++) {
00520     std::cout << "Binding: " 
00521               << env->bindings[i]->nm
00522               << std::endl;
00523   }
00524 
00525 
00526 
00528 
00529 /* To handle the special case of Literals in bitfields*/
00530       
00531 if(t1->Isize == 0) {
00532   t1->Isize = t2->Isize;
00533   break;
00534  }
00535  else if(t2->Isize == 0) {
00536    t2->Isize = t1->Isize;
00537    break;
00538  }
00539 
00541       Old Character Printing:
00542       std::streamsize w = out.ostrm.width();
00543       char fillChar = out.ostrm.fill('0');
00544       
00545       out.ostrm << right;
00546       out.ostrm << oct;
00547 
00548       out << "'\\";
00549       out.ostrm.width(3);
00550       out << ast->litValue.c;
00551       out << "'";
00552 
00553       out.ostrm << dec;
00554       out.ostrm << left;
00555       out.ostrm.width(w);
00556       out.ostrm.fill(fillChar);
00557 
00559 
00560       GCPtr<AST> bindings = ast->children[0];
00561       GCPtr<AST> letbinding = bindings->children[0];
00562       GCPtr<AST> ident = letbinding->children[0]->children[0];
00563       GCPtr<AST> binds = letbinding->children[1]->children[0];
00564       GCPtr<AST> exprs = ast->children[1]->children[1];
00565       GCPtr<AST> body = letbinding->children[1]->children[1];
00566       out << "(" << ast->atKwd() << " ";
00567       BitcP(out, ident, showTypes);
00568       out << " ";
00569 
00570       assert(binds->children.size() == exprs->children.size());      
00571       out << "(";
00572       for(size_t i=0; i<binds->children.size(); i++) {
00573         out << "(";
00574         BitcP(out, binds->children[i], showTypes);
00575         out << " ";
00576         BitcP(out, exprs->children[i], showTypes);
00577         out << ") ";
00578       }
00579       out << ")";
00580 
00581       BitcP(out, body, showTypes);
00582 
00583       out << ")";
00584 
00585    Old Structure Union Type Printing:
00586 
00587       if (ast->astType == at_defunion ||
00588           ast->astType == at_declunion) 
00589         out << "(union ";
00590       else
00591         out << "(struct ";
00592 
00593       // FIX: for debugging purposes, this should be showing the 
00594       // post-inference type in some cases
00595       if(ast->children[1]->children.size() > 0) {
00596         out << "(";
00597         BitcP(out, ast->children[0], false);
00598         out << " ";
00599         BitcP(out, ast->children[1], false);
00600         out << ")";
00601       }
00602       else {
00603         BitcP(out, ast->children[0], false);
00604       }
00605 
00606       BitcP(out, ast->children[2], false);
00607 
00608       if (ast->children.size() > 4) {
00609         out << " ";
00610         BitcP(out, ast->children[4], false);
00611       }
00612 
00613       out << ")";
00614       break;
00615 
00616   Old exception Type Printing:
00617     if(ast->children.size() > 1) {
00618         out << "(";
00619         BitcP(out, ast->children[0], false);
00620         out << " ";
00621 
00622         for(size_t i=1; i<ast->children.size(); i++) {
00623           out << " ";
00624           BitcP(out, ast->children[i], false);
00625         }
00626         out << ")";
00627       }
00628       else {
00629         BitcP(out, ast->children[0], false);
00630       }
00631 
00633 
00634 void
00635 UocremEnv(std::ostream& errStream, AST *ast,
00636           UocInfo *uoc)
00637 {
00638   switch(ast->astType) {
00639   case at_ident: //declValue only produces idents
00640     uoc->env->removeBinding(ast->s);
00641     uoc->gamma->removeBinding(ast->s);
00642     break;
00643     
00644   case at_identPattern:
00645     uoc->env->removeBinding(ast->children[0]->s);
00646     uoc->gamma->removeBinding(ast->children[0]->s);
00647     break;
00648 
00649   default:
00650     errStream << ast->loc << ": "
00651               << "Internal Compiler Error." 
00652               << "Unexpected Binding Pattern type " 
00653               << ast->astTypeName()
00654               << " obtained by remEnv() routine."
00655               << std::endl;
00656     break;
00657   }
00658 }
00659 
00660 #endif

Generated on Sat Feb 4 23:59:28 2012 for BitC Compiler by  doxygen 1.4.7