author | joehw |
Wed, 26 Jun 2019 05:49:59 +0000 | |
changeset 55496 | 8e0ae3830fca |
parent 48409 | 5ab69533994b |
permissions | -rw-r--r-- |
6 | 1 |
/* |
55496 | 2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
6 | 3 |
*/ |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
4 |
/* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* the License. You may obtain a copy of the License at |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
14 |
* Unless required by applicable law or agreed to in writing, software |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
17 |
* See the License for the specific language governing permissions and |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
18 |
* limitations under the License. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
19 |
*/ |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
20 |
|
6 | 21 |
package com.sun.org.apache.bcel.internal.util; |
22 |
||
46174 | 23 |
import java.io.PrintWriter; |
24 |
import java.util.ArrayList; |
|
25 |
import java.util.HashMap; |
|
26 |
import java.util.List; |
|
27 |
import java.util.Locale; |
|
28 |
import java.util.Map; |
|
29 |
||
30 |
import com.sun.org.apache.bcel.internal.Const; |
|
6 | 31 |
import com.sun.org.apache.bcel.internal.classfile.Utility; |
46174 | 32 |
import com.sun.org.apache.bcel.internal.generic.AllocationInstruction; |
33 |
import com.sun.org.apache.bcel.internal.generic.ArrayInstruction; |
|
34 |
import com.sun.org.apache.bcel.internal.generic.ArrayType; |
|
35 |
import com.sun.org.apache.bcel.internal.generic.BranchHandle; |
|
36 |
import com.sun.org.apache.bcel.internal.generic.BranchInstruction; |
|
37 |
import com.sun.org.apache.bcel.internal.generic.CHECKCAST; |
|
38 |
import com.sun.org.apache.bcel.internal.generic.CPInstruction; |
|
39 |
import com.sun.org.apache.bcel.internal.generic.CodeExceptionGen; |
|
40 |
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; |
|
41 |
import com.sun.org.apache.bcel.internal.generic.ConstantPushInstruction; |
|
42 |
import com.sun.org.apache.bcel.internal.generic.EmptyVisitor; |
|
43 |
import com.sun.org.apache.bcel.internal.generic.FieldInstruction; |
|
44 |
import com.sun.org.apache.bcel.internal.generic.IINC; |
|
45 |
import com.sun.org.apache.bcel.internal.generic.INSTANCEOF; |
|
46 |
import com.sun.org.apache.bcel.internal.generic.Instruction; |
|
47 |
import com.sun.org.apache.bcel.internal.generic.InstructionConst; |
|
48 |
import com.sun.org.apache.bcel.internal.generic.InstructionHandle; |
|
49 |
import com.sun.org.apache.bcel.internal.generic.InvokeInstruction; |
|
50 |
import com.sun.org.apache.bcel.internal.generic.LDC; |
|
51 |
import com.sun.org.apache.bcel.internal.generic.LDC2_W; |
|
52 |
import com.sun.org.apache.bcel.internal.generic.LocalVariableInstruction; |
|
53 |
import com.sun.org.apache.bcel.internal.generic.MULTIANEWARRAY; |
|
54 |
import com.sun.org.apache.bcel.internal.generic.MethodGen; |
|
55 |
import com.sun.org.apache.bcel.internal.generic.NEWARRAY; |
|
56 |
import com.sun.org.apache.bcel.internal.generic.ObjectType; |
|
57 |
import com.sun.org.apache.bcel.internal.generic.RET; |
|
58 |
import com.sun.org.apache.bcel.internal.generic.ReturnInstruction; |
|
59 |
import com.sun.org.apache.bcel.internal.generic.Select; |
|
60 |
import com.sun.org.apache.bcel.internal.generic.Type; |
|
6 | 61 |
|
62 |
/** |
|
63 |
* Factory creates il.append() statements, and sets instruction targets. |
|
64 |
* A helper class for BCELifier. |
|
65 |
* |
|
66 |
* @see BCELifier |
|
55496 | 67 |
* @version $Id$ |
68 |
* @LastModified: Jun 2019 |
|
6 | 69 |
*/ |
70 |
class BCELFactory extends EmptyVisitor { |
|
71 |
||
46174 | 72 |
private static final String CONSTANT_PREFIX = Const.class.getSimpleName()+"."; |
73 |
private final MethodGen _mg; |
|
74 |
private final PrintWriter _out; |
|
75 |
private final ConstantPoolGen _cp; |
|
76 |
||
6 | 77 |
|
46174 | 78 |
BCELFactory(final MethodGen mg, final PrintWriter out) { |
79 |
_mg = mg; |
|
80 |
_cp = mg.getConstantPool(); |
|
81 |
_out = out; |
|
82 |
} |
|
6 | 83 |
|
46174 | 84 |
private final Map<Instruction, InstructionHandle> branch_map = new HashMap<>(); |
6 | 85 |
|
86 |
||
46174 | 87 |
public void start() { |
88 |
if (!_mg.isAbstract() && !_mg.isNative()) { |
|
89 |
for (InstructionHandle ih = _mg.getInstructionList().getStart(); ih != null; ih = ih |
|
90 |
.getNext()) { |
|
91 |
final Instruction i = ih.getInstruction(); |
|
92 |
if (i instanceof BranchInstruction) { |
|
93 |
branch_map.put(i, ih); // memorize container |
|
94 |
} |
|
95 |
if (ih.hasTargeters()) { |
|
96 |
if (i instanceof BranchInstruction) { |
|
97 |
_out.println(" InstructionHandle ih_" + ih.getPosition() + ";"); |
|
98 |
} else { |
|
99 |
_out.print(" InstructionHandle ih_" + ih.getPosition() + " = "); |
|
100 |
} |
|
101 |
} else { |
|
102 |
_out.print(" "); |
|
103 |
} |
|
104 |
if (!visitInstruction(i)) { |
|
105 |
i.accept(this); |
|
106 |
} |
|
107 |
} |
|
108 |
updateBranchTargets(); |
|
109 |
updateExceptionHandlers(); |
|
6 | 110 |
} |
46174 | 111 |
} |
6 | 112 |
|
113 |
||
46174 | 114 |
private boolean visitInstruction( final Instruction i ) { |
115 |
final short opcode = i.getOpcode(); |
|
116 |
if ((InstructionConst.getInstruction(opcode) != null) |
|
117 |
&& !(i instanceof ConstantPushInstruction) && !(i instanceof ReturnInstruction)) { // Handled below |
|
118 |
_out.println("il.append(InstructionConst." |
|
119 |
+ i.getName().toUpperCase(Locale.ENGLISH) + ");"); |
|
120 |
return true; |
|
121 |
} |
|
122 |
return false; |
|
6 | 123 |
} |
124 |
||
125 |
||
46174 | 126 |
@Override |
127 |
public void visitLocalVariableInstruction( final LocalVariableInstruction i ) { |
|
128 |
final short opcode = i.getOpcode(); |
|
129 |
final Type type = i.getType(_cp); |
|
130 |
if (opcode == Const.IINC) { |
|
131 |
_out.println("il.append(new IINC(" + i.getIndex() + ", " + ((IINC) i).getIncrement() |
|
132 |
+ "));"); |
|
133 |
} else { |
|
134 |
final String kind = (opcode < Const.ISTORE) ? "Load" : "Store"; |
|
135 |
_out.println("il.append(_factory.create" + kind + "(" + BCELifier.printType(type) |
|
136 |
+ ", " + i.getIndex() + "));"); |
|
137 |
} |
|
138 |
} |
|
139 |
||
6 | 140 |
|
46174 | 141 |
@Override |
142 |
public void visitArrayInstruction( final ArrayInstruction i ) { |
|
143 |
final short opcode = i.getOpcode(); |
|
144 |
final Type type = i.getType(_cp); |
|
145 |
final String kind = (opcode < Const.IASTORE) ? "Load" : "Store"; |
|
146 |
_out.println("il.append(_factory.createArray" + kind + "(" + BCELifier.printType(type) |
|
147 |
+ "));"); |
|
6 | 148 |
} |
46174 | 149 |
|
6 | 150 |
|
46174 | 151 |
@Override |
152 |
public void visitFieldInstruction( final FieldInstruction i ) { |
|
153 |
final short opcode = i.getOpcode(); |
|
154 |
final String class_name = i.getReferenceType(_cp).getSignature(); |
|
155 |
final String field_name = i.getFieldName(_cp); |
|
156 |
final Type type = i.getFieldType(_cp); |
|
157 |
_out.println("il.append(_factory.createFieldAccess(\"" + class_name + "\", \"" + field_name |
|
158 |
+ "\", " + BCELifier.printType(type) + ", " + CONSTANT_PREFIX |
|
159 |
+ Const.getOpcodeName(opcode).toUpperCase(Locale.ENGLISH) + "));"); |
|
160 |
} |
|
6 | 161 |
|
162 |
||
46174 | 163 |
@Override |
164 |
public void visitInvokeInstruction( final InvokeInstruction i ) { |
|
165 |
final short opcode = i.getOpcode(); |
|
166 |
final String class_name = i.getReferenceType(_cp).getSignature(); |
|
167 |
final String method_name = i.getMethodName(_cp); |
|
168 |
final Type type = i.getReturnType(_cp); |
|
169 |
final Type[] arg_types = i.getArgumentTypes(_cp); |
|
170 |
_out.println("il.append(_factory.createInvoke(\"" + class_name + "\", \"" + method_name |
|
171 |
+ "\", " + BCELifier.printType(type) + ", " |
|
172 |
+ BCELifier.printArgumentTypes(arg_types) + ", " + CONSTANT_PREFIX |
|
173 |
+ Const.getOpcodeName(opcode).toUpperCase(Locale.ENGLISH) + "));"); |
|
174 |
} |
|
6 | 175 |
|
176 |
||
46174 | 177 |
@Override |
47477
115ed64c7822
8181155: Fix lint warnings in JAXP repo: fallthrough and static
joehw
parents:
47216
diff
changeset
|
178 |
@SuppressWarnings("fallthrough") // by design for case Const.ANEWARRAY |
46174 | 179 |
public void visitAllocationInstruction( final AllocationInstruction i ) { |
180 |
Type type; |
|
181 |
if (i instanceof CPInstruction) { |
|
182 |
type = ((CPInstruction) i).getType(_cp); |
|
183 |
} else { |
|
184 |
type = ((NEWARRAY) i).getType(); |
|
185 |
} |
|
186 |
final short opcode = ((Instruction) i).getOpcode(); |
|
187 |
int dim = 1; |
|
188 |
switch (opcode) { |
|
189 |
case Const.NEW: |
|
190 |
_out.println("il.append(_factory.createNew(\"" + ((ObjectType) type).getClassName() |
|
191 |
+ "\"));"); |
|
192 |
break; |
|
193 |
case Const.MULTIANEWARRAY: |
|
194 |
dim = ((MULTIANEWARRAY) i).getDimensions(); |
|
195 |
//$FALL-THROUGH$ |
|
196 |
case Const.ANEWARRAY: |
|
197 |
case Const.NEWARRAY: |
|
198 |
if (type instanceof ArrayType) { |
|
199 |
type = ((ArrayType) type).getBasicType(); |
|
200 |
} |
|
201 |
_out.println("il.append(_factory.createNewArray(" + BCELifier.printType(type) |
|
202 |
+ ", (short) " + dim + "));"); |
|
203 |
break; |
|
204 |
default: |
|
205 |
throw new RuntimeException("Oops: " + opcode); |
|
206 |
} |
|
6 | 207 |
} |
208 |
||
209 |
||
46174 | 210 |
private void createConstant( final Object value ) { |
211 |
String embed = value.toString(); |
|
212 |
if (value instanceof String) { |
|
213 |
embed = '"' + Utility.convertString(embed) + '"'; |
|
214 |
} else if (value instanceof Character) { |
|
215 |
embed = "(char)0x" + Integer.toHexString(((Character) value).charValue()); |
|
216 |
} else if (value instanceof Float) { |
|
217 |
embed += "f"; |
|
218 |
} else if (value instanceof Long) { |
|
219 |
embed += "L"; |
|
220 |
} else if (value instanceof ObjectType) { |
|
221 |
final ObjectType ot = (ObjectType) value; |
|
222 |
embed = "new ObjectType(\""+ot.getClassName()+"\")"; |
|
223 |
} |
|
6 | 224 |
|
46174 | 225 |
_out.println("il.append(new PUSH(_cp, " + embed + "));"); |
226 |
} |
|
6 | 227 |
|
228 |
||
46174 | 229 |
@Override |
230 |
public void visitLDC( final LDC i ) { |
|
231 |
createConstant(i.getValue(_cp)); |
|
232 |
} |
|
6 | 233 |
|
234 |
||
46174 | 235 |
@Override |
236 |
public void visitLDC2_W( final LDC2_W i ) { |
|
237 |
createConstant(i.getValue(_cp)); |
|
238 |
} |
|
6 | 239 |
|
240 |
||
46174 | 241 |
@Override |
242 |
public void visitConstantPushInstruction( final ConstantPushInstruction i ) { |
|
243 |
createConstant(i.getValue()); |
|
244 |
} |
|
6 | 245 |
|
246 |
||
46174 | 247 |
@Override |
248 |
public void visitINSTANCEOF( final INSTANCEOF i ) { |
|
249 |
final Type type = i.getType(_cp); |
|
250 |
_out.println("il.append(new INSTANCEOF(_cp.addClass(" + BCELifier.printType(type) + ")));"); |
|
251 |
} |
|
6 | 252 |
|
253 |
||
46174 | 254 |
@Override |
255 |
public void visitCHECKCAST( final CHECKCAST i ) { |
|
256 |
final Type type = i.getType(_cp); |
|
257 |
_out.println("il.append(_factory.createCheckCast(" + BCELifier.printType(type) + "));"); |
|
258 |
} |
|
6 | 259 |
|
260 |
||
46174 | 261 |
@Override |
262 |
public void visitReturnInstruction( final ReturnInstruction i ) { |
|
263 |
final Type type = i.getType(_cp); |
|
264 |
_out.println("il.append(_factory.createReturn(" + BCELifier.printType(type) + "));"); |
|
6 | 265 |
} |
266 |
||
46174 | 267 |
// Memorize BranchInstructions that need an update |
268 |
private final List<BranchInstruction> branches = new ArrayList<>(); |
|
6 | 269 |
|
270 |
||
46174 | 271 |
@Override |
272 |
public void visitBranchInstruction( final BranchInstruction bi ) { |
|
273 |
final BranchHandle bh = (BranchHandle) branch_map.get(bi); |
|
274 |
final int pos = bh.getPosition(); |
|
275 |
final String name = bi.getName() + "_" + pos; |
|
276 |
if (bi instanceof Select) { |
|
277 |
final Select s = (Select) bi; |
|
278 |
branches.add(bi); |
|
279 |
final StringBuilder args = new StringBuilder("new int[] { "); |
|
280 |
final int[] matchs = s.getMatchs(); |
|
281 |
for (int i = 0; i < matchs.length; i++) { |
|
282 |
args.append(matchs[i]); |
|
283 |
if (i < matchs.length - 1) { |
|
284 |
args.append(", "); |
|
285 |
} |
|
286 |
} |
|
287 |
args.append(" }"); |
|
288 |
_out.print("Select " + name + " = new " + bi.getName().toUpperCase(Locale.ENGLISH) |
|
289 |
+ "(" + args + ", new InstructionHandle[] { "); |
|
290 |
for (int i = 0; i < matchs.length; i++) { |
|
291 |
_out.print("null"); |
|
292 |
if (i < matchs.length - 1) { |
|
293 |
_out.print(", "); |
|
294 |
} |
|
295 |
} |
|
296 |
_out.println(" }, null);"); |
|
297 |
} else { |
|
298 |
final int t_pos = bh.getTarget().getPosition(); |
|
299 |
String target; |
|
300 |
if (pos > t_pos) { |
|
301 |
target = "ih_" + t_pos; |
|
302 |
} else { |
|
303 |
branches.add(bi); |
|
304 |
target = "null"; |
|
305 |
} |
|
306 |
_out.println(" BranchInstruction " + name + " = _factory.createBranchInstruction(" |
|
307 |
+ CONSTANT_PREFIX + bi.getName().toUpperCase(Locale.ENGLISH) + ", " + target |
|
308 |
+ ");"); |
|
6 | 309 |
} |
46174 | 310 |
if (bh.hasTargeters()) { |
311 |
_out.println(" ih_" + pos + " = il.append(" + name + ");"); |
|
312 |
} else { |
|
313 |
_out.println(" il.append(" + name + ");"); |
|
314 |
} |
|
6 | 315 |
} |
316 |
||
46174 | 317 |
|
318 |
@Override |
|
319 |
public void visitRET( final RET i ) { |
|
320 |
_out.println("il.append(new RET(" + i.getIndex() + ")));"); |
|
321 |
} |
|
322 |
||
6 | 323 |
|
46174 | 324 |
private void updateBranchTargets() { |
325 |
for (final BranchInstruction bi : branches) { |
|
326 |
final BranchHandle bh = (BranchHandle) branch_map.get(bi); |
|
327 |
final int pos = bh.getPosition(); |
|
328 |
final String name = bi.getName() + "_" + pos; |
|
329 |
int t_pos = bh.getTarget().getPosition(); |
|
330 |
_out.println(" " + name + ".setTarget(ih_" + t_pos + ");"); |
|
331 |
if (bi instanceof Select) { |
|
332 |
final InstructionHandle[] ihs = ((Select) bi).getTargets(); |
|
333 |
for (int j = 0; j < ihs.length; j++) { |
|
334 |
t_pos = ihs[j].getPosition(); |
|
335 |
_out.println(" " + name + ".setTarget(" + j + ", ih_" + t_pos + ");"); |
|
336 |
} |
|
337 |
} |
|
338 |
} |
|
6 | 339 |
} |
46174 | 340 |
|
341 |
||
342 |
private void updateExceptionHandlers() { |
|
343 |
final CodeExceptionGen[] handlers = _mg.getExceptionHandlers(); |
|
344 |
for (final CodeExceptionGen h : handlers) { |
|
345 |
final String type = (h.getCatchType() == null) ? "null" : BCELifier.printType(h |
|
346 |
.getCatchType()); |
|
347 |
_out.println(" method.addExceptionHandler(" + "ih_" + h.getStartPC().getPosition() |
|
348 |
+ ", " + "ih_" + h.getEndPC().getPosition() + ", " + "ih_" |
|
349 |
+ h.getHandlerPC().getPosition() + ", " + type + ");"); |
|
350 |
} |
|
351 |
} |
|
6 | 352 |
} |