/* * Created on Apr 12, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package fr.lifl.stc.stan.link.defaultLink; import fr.lifl.stc.stan.execution.Frame; import fr.lifl.stc.stan.execution.stack.JvmType; import fr.lifl.stc.stan.link.Link; /** * @author dorina * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class DefaultLink extends Link { static final long serialVersionUID = 0; private byte link; /* (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.link.Link#compatibleWith(fr.lifl.rd2p.jits.tools.ea.link.Link) */ //mask used to contruct a link (if reference link, then value included) private static final byte maskLinkType_constructLink[] = { (byte)0xF0, (byte)0xFF, (byte)0xFF}; //mask used to find a link (if reference link searched, then value link not included) private static final byte maskLinkType_findLink[] = { (byte)0xF0, (byte)0x0F, (byte)0xFF}; //whole, public, secret private static final byte maskFrom [] = {(byte)0xFF, (byte)0x33, 0, (byte)0xCC }; //{(byte)0x11111111, (byte)0x00110011 (byte)0x11001100,}; private static final byte maskTo [] = {(byte)0xFF, (byte)0x55, 0, (byte)0xAA }; //{(byte)0xFF, (byte)0x10101010, (byte)0x01010101 }; private static final byte transform[] = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x02, (byte)0x02, (byte)0x02, (byte)0x02, (byte)0x03, (byte)0x03, (byte)0x03, (byte)0x03, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x01, (byte)0x01, (byte)0x03, (byte)0x03, (byte)0x02, (byte)0x03, (byte)0x02, (byte)0x03, (byte)0x03, (byte)0x03, (byte)0x03, (byte)0x03, (byte)0x00, (byte)0x04, (byte)0x08, (byte)0x0C, (byte)0x00, (byte)0x04, (byte)0x08, (byte)0x0C, (byte)0x00, (byte)0x04, (byte)0x08, (byte)0x0C, (byte)0x00, (byte)0x04, (byte)0x08, (byte)0x0C, (byte)0x00, (byte)0x05, (byte)0x0A, (byte)0x0F, (byte)0x00, (byte)0x05, (byte)0x0A, (byte)0x0F, (byte)0x00, (byte)0x05, (byte)0x0A, (byte)0x0F, (byte)0x00, (byte)0x05, (byte)0x0A, (byte)0x0F, (byte)0x00, (byte)0x04, (byte)0x08, (byte)0x0C, (byte)0x01, (byte)0x05, (byte)0x09, (byte)0x0D, (byte)0x02, (byte)0x06, (byte)0x0A, (byte)0x0E, (byte)0x03, (byte)0x07, (byte)0x0B, (byte)0x0F, (byte)0x00, (byte)0x05, (byte)0x0A, (byte)0x0F, (byte)0x01, (byte)0x05, (byte)0x0B, (byte)0x0F, (byte)0x02, (byte)0x07, (byte)0x0A, (byte)0x0F, (byte)0x03, (byte)0x07, (byte)0x0B, (byte)0x0F, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x04, (byte)0x04, (byte)0x04, (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x0C, (byte)0x0C, (byte)0x0C, (byte)0x0C, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08, (byte)0x09, (byte)0x0A, (byte)0x0B, (byte)0x0C, (byte)0x0D, (byte)0x0E, (byte)0x0F, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x05, (byte)0x05, (byte)0x05, (byte)0x05, (byte)0x0A, (byte)0x0A, (byte)0x0A, (byte)0x0A, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x05, (byte)0x05, (byte)0x07, (byte)0x07, (byte)0x0A, (byte)0x0B, (byte)0x0A, (byte)0x0B, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x00, (byte)0x04, (byte)0x08, (byte)0x0C, (byte)0x04, (byte)0x04, (byte)0x0C, (byte)0x0C, (byte)0x08, (byte)0x0C, (byte)0x08, (byte)0x0C, (byte)0x0C, (byte)0x0C, (byte)0x0C, (byte)0x0C, (byte)0x00, (byte)0x05, (byte)0x0A, (byte)0x0F, (byte)0x04, (byte)0x05, (byte)0x0E, (byte)0x0F, (byte)0x08, (byte)0x0D, (byte)0x0A, (byte)0x0F, (byte)0x0C, (byte)0x0D, (byte)0x0E, (byte)0x0F, (byte)0x00, (byte)0x04, (byte)0x08, (byte)0x0C, (byte)0x05, (byte)0x05, (byte)0x0D, (byte)0x0D, (byte)0x0A, (byte)0x0E, (byte)0x0A, (byte)0x0E, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x00, (byte)0x05, (byte)0x0A, (byte)0x0F, (byte)0x05, (byte)0x05, (byte)0x0F, (byte)0x0F, (byte)0x0A, (byte)0x0F, (byte)0x0A, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x0F, (byte)0x0F}; static byte MASK_VALUE = (byte)0xF0; static byte MASK_REFERENCE = (byte)0x0F; public DefaultLink(){ this.link = 0; } public DefaultLink(byte b) { this.link = b; } public DefaultLink(int from, int to, int linkType){ //System.out.println("from = "+from+" to="+to+" linkType ="+linkType); //if we have W->W , we put SS, SP, PS and PP if(from == to && from == JvmType.WHOLE_PART) this.link = (byte)(0xFF & maskLinkType_constructLink[linkType]); else this.link = (byte)( maskLinkType_constructLink[linkType] & maskFrom[from] & maskTo[to] ); } public DefaultLink(int fromPart, int fromType, int toPart, int toType){ //if we have W->W , we put SS and SP, or we put SS, SP, PS and PP ? //if(fromPart == toPart && fromPart == JvmType.WHOLE_PART) // this.link = (byte)( maskLinkType_constructLink[fromType] & maskLinkType_constructLink[toType]); //else{ this.link = (byte)( maskLinkType_constructLink[fromType] & maskLinkType_constructLink[toType] & maskFrom[fromPart] & maskTo[toPart] ); //} } public boolean isEmpty(){ return this.getLink() == 0; } public boolean isLinkOfType(int typeLink){ return (this.link & maskLinkType_findLink[typeLink]) != 0; } /* public boolean isLinkToSecret(){ //FIXME return (this.getLink() & maskTo[JvmType.SECRET_PART]) != 0; } */ /* public boolean isLinkToSecretReference(){ return (this.getLink() & maskTo[JvmType.SECRET_PART] & maskLinkType_[Link.REFERENCE_LINK]) != 0; } public boolean isLinkToSecretValue(){ //FIXME return (this.getLink() & maskTo[JvmType.SECRET_PART] & maskLinkType_[Link.VALUE_LINK]) != 0; } */ public boolean isLinkFromPart(int part){ return (link & maskFrom[part]) != 0; } public boolean isLinkToPart(int part){ return (link & maskTo[part]) != 0; } //TODO is this ok? public boolean compatibleWith(Link l) { if( l == null){ if(!this.isEmpty()) return false; return true; } DefaultLink dl = (DefaultLink)l; return ( (this.link|dl.getLink()) == dl.getLink()); } /* (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.link.Link#makeAccept(fr.lifl.rd2p.jits.tools.ea.link.Link) */ public Link makeAccept(Link l) { return this.merge(l); } /* (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.link.Link#merge(fr.lifl.rd2p.jits.tools.ea.link.Link) */ public Link merge(Link l) { if(l == null || !merge(((DefaultLink)l).getLink())) return null; return this; } private boolean merge(byte b) { byte oldLink = this.getLink(); this.link |= b; return oldLink != this.link; } /* public boolean merge(int from, int to, int linkType){ byte newLink = (byte)( maskLinkType[linkType] & maskFrom[from] & maskTo[to] ); return merge(newLink); } */ /* public static Link createLink(int from, int to, int linkType){ byte newLink = (byte)( maskLinkType[linkType] & maskFrom[from] & maskTo[to] ); Link l = new DefaultLink(newLink); return l; } */ /* (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.link.Link#copyLink(fr.lifl.rd2p.jits.tools.ea.link.Link) */ public void copyLink(Link l) { if(l!=null) { this.setLink(((DefaultLink)l).getLink()); } } public boolean equals(Link l){ if(l==null) return false; return this.getLink() == ((DefaultLink)l).getLink(); } /* (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.link.Link#makePointTo(fr.lifl.rd2p.jits.tools.ea.link.Link,fr.lifl.rd2p.jits.tools.ea.link.Link) */ public Link makePointTo(Link from, Link to) { if(from==null || to==null) return null; byte oldLink = this.getLink(); DefaultLink dfrom = (DefaultLink)from; DefaultLink dto = (DefaultLink)to; //byte valT1 = getValue(dfrom.getLink()); byte refT1 = getReference(dfrom.getLink()); byte valT2 = getValue(dto.getLink()); byte refT2 = getReference(dto.getLink()); byte refT3 = op(refT1, refT2); byte valT3 = (byte)(op(refT1, valT2) | refT3 ) ; setLink( (byte) (this.getLink() | constructLink(valT3, refT3))); if( oldLink!=this.getLink()) return this; return null; } /* * * (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.link.Link#makePointedBy(fr.lifl.rd2p.jits.tools.ea.link.Link,fr.lifl.rd2p.jits.tools.ea.link.Link) */ public Link makePointedBy(Link from, Link to){ //Link from, Link to) { // TODO Auto-generated method stub if(to==null || from == null) return null; byte oldLink = this.getLink(); DefaultLink dfrom = (DefaultLink)from; DefaultLink dto = (DefaultLink)to; byte valT1 = getValue(dto.getLink()); byte refT1 = getReference(dto.getLink()); byte valT2 = getValue(dfrom.getLink()); byte refT2 = getReference(dfrom.getLink()); byte refT3 = op(refT2, refT1); byte valT3 = (byte)( op(valT2, valT1) | op(refT2, valT1) | op(valT2,refT1) | refT3 ) ; setLink( (byte) (this.getLink() | constructLink(valT3, refT3))); if( oldLink!=this.getLink()) return this; return null; } /** * * @param from * @param to * @param linkType * @return */ public JvmType getPointedPart(JvmType from, JvmType to, int linkType){ if(this.isEmpty()) return null; // System.out.println("from: "+from+" to:"+to+" linkType="+linkType+" this.link="+this.getLink()); byte b = (byte)(this.link & maskFrom[from.getPartType()]); if(linkType == Link.REFERENCE_LINK) b = getReference(b); else b = getValue(b); //1010 if points to secret int toPart = -1; //JvmType res = null; if((b & 0xA) !=0 ) { if((b & 0x5) != 0){ //res = to; toPart = JvmType.WHOLE_PART; } else{ //res = to toPart = JvmType.SECRET_PART; } } else if((b & 0x5) != 0) toPart = JvmType.PUBLIC_PART; if(toPart!=-1) return Frame.getPart(to, toPart); return null; } public Link makePointsToReturn(Link from, Link to, int typeReturn){ //ivert to int old_val = this.getLink(); to = new DefaultLink(DefaultLink.invert(((DefaultLink)to).getLink())); makePointedBy(from, to); /* boolean updated = false; if(from==null || to==null) return false; byte oldLink = this.getLink(); DefaultLink dfrom = (DefaultLink)from; DefaultLink dto = (DefaultLink)to; byte valT1 = getValue(dto.getLink()); byte refT1 = getReference(dto.getLink()); byte valT2 = getValue(dfrom.getLink()); byte refT2 = getReference(dfrom.getLink()); byte refT3 = op(refT2, refT1); byte valT3 = op(valT2, valT1);// | op(refT2, valT1) | op(valT2,refT1) | refT3 ) ; setLink( (byte) (this.getLink() | constructLink(valT3, refT3))); updated = ( oldLink!=this.getLink()); */ if(typeReturn == Link.VALUE_LINK) setLink( (byte)(getLink() & 0x50)); if(old_val != this.getLink()) return this; return null; } public Link makePointArray(Link from, Link to){ from = new DefaultLink(DefaultLink.invert(((DefaultLink)from).getLink())); return makePointTo(from, to); } /* (non-Javadoc) * @see fr.lifl.rd2p.jits.tools.ea.link.Link#applySignature(II Link) */ public Link applySignature(int from, int to, Link l) { byte newLink = ((DefaultLink)l).getLink(); byte newLinkVal = DefaultLink.getValue(newLink); byte newLinkRef = DefaultLink.getReference(newLink); if(from == JvmType.SECRET_PART){ if(to == JvmType.SECRET_PART){ if(newLinkVal != 0) newLinkVal = 0x8; if(newLinkRef != 0) newLinkRef = 0x8; } else{ byte ss = 0; byte sp = 0; if((newLinkVal & 0xA) !=0 ) ss = 0x8; if((newLinkVal & 0x5) != 0 ) sp = 0x4; newLinkVal = (byte)(ss | sp); ss = 0; sp = 0; if((newLinkRef & 0xA) !=0 ) ss = 0x8; if((newLinkRef & 0x5) != 0 ) sp = 0x4; newLinkRef = (byte)(ss | sp); /* byte ss = 0; byte ps = 0; if((newLinkVal & 0xC) !=0 ) ss = 0x8; if((newLinkVal & 0x3) != 0 ) ps = 0x2; newLinkVal = (byte)(ss | ps); ss = 0; ps = 0; if((newLinkRef & 0xC) !=0 ) ss = 0x8; if((newLinkRef & 0x3) != 0 ) ps = 0x2; newLinkRef = (byte)(ss | ps); */ } } else { if(to == JvmType.SECRET_PART) { byte ss = 0; byte ps = 0; if((newLinkVal & 0xC) !=0 ) ss = 0x8; if((newLinkVal & 0x3) != 0 ) ps = 0x2; newLinkVal = (byte)(ss | ps); ss = 0; ps = 0; if((newLinkRef & 0xC) !=0 ) ss = 0x8; if((newLinkRef & 0x3) != 0 ) ps = 0x2; newLinkRef = (byte)(ss | ps); /* byte ss = 0; byte sp = 0; if((newLinkVal & 0xA) !=0 ) ss = 0x8; if((newLinkVal & 0x5) != 0 ) sp = 0x4; newLinkVal = (byte)(ss | sp); ss = 0; sp = 0; if((newLinkRef & 0xA) !=0 ) ss = 0x8; if((newLinkRef & 0x5) != 0 ) sp = 0x4; newLinkRef = (byte)(ss | sp); */ } } newLink = DefaultLink.constructLink(newLinkVal, newLinkRef); if(merge(newLink)) return this; return null; } public void extendPublicReference(){ byte val = getValue(this.getLink()); byte ref = getReference(this.getLink()); byte res = ref; if((ref & 0x4)!=0) res |= 0xB; if((ref & 0x1)!=0) res |= 0x3; ref = res; val = (byte)(val | ref); this.setLink(constructLink(val, ref)); } public byte getLink(){ return link; } public void setLink(byte b){ this.link = b; } public void setLinkToMax(){ this.setLink((byte)0xFF); } public void setLinkToMin(){ this.setLink((byte)0); } public String toString() { if(this.link == (byte)0x0) return "-"; int ref = this.link & MASK_REFERENCE; int val = ref^((this.link>>4)&0x0F); return toStr(ref,"R")+toStr(val,"V"); //return ""+this.link; } private String toStr(int val, String mes) { if(val==0) return ""; if(val==0x0F) return mes+"tt"; String s = " "; if((val & 0x1)!=0) s+=mes+"pp"; if((val & 0x2)!=0) s+=mes+"ps"; if((val & 0x4)!=0) s+=mes+"sp"; if((val & 0x8)!=0) s+=mes+"ss"; return s; } public Link clone1() { DefaultLink df = new DefaultLink(this.getLink()); return df; } public Object clone() { DefaultLink df = new DefaultLink(this.getLink()); return df; } public static byte getReference(byte b){ return (byte)(b & MASK_REFERENCE); } public static byte getValue(byte b){ return (byte)(((b & MASK_VALUE) >> 4)&0x0F); } private static byte constructLink(byte val, byte ref) { return (byte) ((ref & MASK_REFERENCE) | ((val<<4) & MASK_VALUE)); } /** * res = 0xxxx * @param b1 * @param b2 * @return */ private static byte op(byte b1, byte b2) { //System.out.println("b1 = "+b1+" b2 = "+b2); return transform[getTransformIndex((byte)(b1<<4 | b2))]; } private static int getTransformIndex(byte i) { //System.out.println("i = "+i); if( i < 0) return 256 + i; return i; } //takes an octect and inverts the links: SP => PS, PS => SP, PP => PP, SS => SS private static byte invert4(byte b){ //PP and SS remain the same byte res = (byte)(b & 0x9); if((b & 0x4) != 0) res = (byte)(res | 0x2); if((b & 0x2) != 0) res = (byte)(res | 0x4); return res; } private static byte invert(byte b){ byte ref = DefaultLink.getReference(b); byte val = DefaultLink.getValue(b); ref = DefaultLink.invert4(ref); val = DefaultLink.invert4(val); return DefaultLink.constructLink(val, ref); } }