Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BytecodeTrimmer |
|
| 2.6666666666666665;2.667 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.nabla.automatic.trimming; | |
18 | ||
19 | import org.objectweb.asm.tree.AbstractInsnNode; | |
20 | import org.objectweb.asm.tree.InsnList; | |
21 | ||
22 | /** Base class for code trimmers. | |
23 | */ | |
24 | public abstract class BytecodeTrimmer { | |
25 | ||
26 | /** Current instructions window. */ | |
27 | private AbstractInsnNode[] currentWindow; | |
28 | ||
29 | /** Simple constructor. | |
30 | * @param lookahead number of lookahead instructions | |
31 | */ | |
32 | 3 | protected BytecodeTrimmer(final int lookahead) { |
33 | 3 | currentWindow = new AbstractInsnNode[lookahead]; |
34 | 3 | } |
35 | ||
36 | /** Trim a list of instructions. | |
37 | * @param instructions list of instructions to optimize | |
38 | */ | |
39 | public void trim(final InsnList instructions) { | |
40 | ||
41 | // set up lookahead instructions | |
42 | 195 | currentWindow[0] = instructions.get(0); |
43 | 650 | for (int i = 1; (i < currentWindow.length) && (currentWindow[i - 1] != null); ++i) { |
44 | 455 | currentWindow[i] = currentWindow[i - 1].getNext(); |
45 | } | |
46 | ||
47 | // trim the whole instructions set using a sliding window | |
48 | 5435 | while (currentWindow[currentWindow.length - 1] != null) { |
49 | ||
50 | // trim current window | |
51 | 5240 | final boolean updated = trimWindow(instructions, currentWindow); |
52 | ||
53 | 5240 | if (!updated) { |
54 | // slide the window ourselves one instruction forward | |
55 | 17144 | for (int i = 1; i < currentWindow.length; ++i) { |
56 | 11958 | currentWindow[i - 1] = currentWindow[i]; |
57 | } | |
58 | 5186 | if (currentWindow[currentWindow.length - 1] != null) { |
59 | 5186 | currentWindow[currentWindow.length - 1] = currentWindow[currentWindow.length - 1].getNext(); |
60 | } | |
61 | } | |
62 | ||
63 | 5240 | } |
64 | ||
65 | 195 | } |
66 | ||
67 | /** Trim the current window of lookahead instructions. | |
68 | * @param instructions complete instructions list of instructions to trim | |
69 | * @param window current instructions window (belongs to the list) | |
70 | * @return true if instructions and window have been updated and are ready | |
71 | * for next iteration | |
72 | */ | |
73 | protected abstract boolean trimWindow(InsnList instructions, | |
74 | AbstractInsnNode[] window); | |
75 | ||
76 | } |