1 4 package gnu.bytecode; 5 6 public class Scope 7 { 8 9 Scope parent; 10 Scope nextSibling; 11 Scope firstChild, lastChild; 12 13 14 boolean preserved; 15 16 Label start; 17 Label end; 18 Variable vars; 19 Variable last_var; 20 21 public Scope() 22 { 23 } 24 25 public Scope (Label start, Label end) 26 { 27 this.start = start; 28 this.end = end; 29 } 30 31 public final Variable firstVar () { return vars; } 33 34 public VarEnumerator allVars () { return new VarEnumerator (this); } 35 36 37 public void linkChild (Scope parent) 38 { 39 this.parent = parent; 40 if (parent == null) 41 return; 42 if (parent.lastChild == null) 43 parent.firstChild = this; 44 else 45 parent.lastChild.nextSibling = this; 46 parent.lastChild = this; 47 } 48 49 public Variable addVariable (CodeAttr code, Type type, String name) 50 { 51 Variable var = new Variable(name, type); 52 addVariable (code, var); 53 return var; 54 } 55 56 public void addVariable (Variable var) 57 { 58 if (last_var == null) 59 vars = var; 60 else 61 last_var.next = var; 62 last_var = var; 63 var.scope = this; 64 } 65 66 67 public void addVariableAfter (Variable prev, Variable var) 68 { 69 if (prev == null) 70 { var.next = vars; 72 vars = var; 73 } 74 else 75 { 76 var.next = prev.next; 77 prev.next = var; 78 } 79 if (last_var == prev) 80 last_var = var; 81 if (var.next == var) 82 throw new Error ("cycle"); 83 var.scope = this; 84 } 85 86 public void addVariable (CodeAttr code, Variable var) 87 { 88 addVariable (var); 89 if (var.isSimple() && code != null) 90 var.allocateLocal(code); 91 } 92 93 97 public Variable getVariable(int index) { 98 Variable var = vars; 99 while (--index >= 0) 100 var = var.next; 101 return var; 102 } 103 104 static boolean equals (byte[] name1, byte[] name2) { 105 if (name1.length != name2.length) 106 return false; 107 if (name1 == name2) 108 return true; 109 for (int i = name1.length; --i >= 0; ) 110 if (name1[i] != name2[i]) 111 return false; 112 return true; 113 } 114 115 public void setStartPC(CodeAttr code) 116 { 117 start = code.getLabel(); 118 } 119 120 125 public Variable lookup (String name) { 126 for (Variable var = vars; var != null; var = var.next) { 127 if (name.equals(var.name)) 128 return var; 129 } 130 return null; 131 } 132 133 136 void freeLocals (CodeAttr code) 137 { 138 if (preserved) 139 return; 140 for (Variable var = vars; var != null; var = var.next) 141 { 142 if (var.isSimple () && ! var.dead ()) 143 var.freeLocal(code); 144 } 145 for (Scope child = firstChild; child != null; child = child.nextSibling) 146 { 147 if (child.preserved) 148 { 149 child.preserved = false; 150 child.freeLocals(code); 151 } 152 } 153 } 154 }; 155 | Popular Tags |