1 /*
2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_VM_PRIMS_METHODCOMPARATOR_HPP
26 #define SHARE_VM_PRIMS_METHODCOMPARATOR_HPP
27 
28 #include "interpreter/bytecodeStream.hpp"
29 #include "oops/constantPool.hpp"
30 #include "oops/method.hpp"
31 
32 class BciMap;
33 
34 // methodComparator provides an interface for determining if methods of
35 // different versions of classes are equivalent or switchable
36 
37 class MethodComparator {
38  private:
39   static BytecodeStream *_s_old, *_s_new;
40   static ConstantPool* _old_cp;
41   static ConstantPool* _new_cp;
42   static BciMap *_bci_map;
43   static bool _switchable_test;
44   static GrowableArray<int> *_fwd_jmps;
45 
46   static bool args_same(Bytecodes::Code c_old, Bytecodes::Code c_new);
47   static bool pool_constants_same(int cpi_old, int cpi_new);
48   static int check_stack_and_locals_size(Method* old_method, Method* new_method);
49 
50  public:
51   // Check if the new method is equivalent to the old one modulo constant pool (EMCP).
52   // Intuitive definition: two versions of the same method are EMCP, if they don't differ
53   // on the source code level. Practically, we check whether the only difference between
54   // method versions is some constantpool indices embedded into the bytecodes, and whether
55   // these indices eventually point to the same constants for both method versions.
56   static bool methods_EMCP(Method* old_method, Method* new_method);
57 
58   static bool methods_switchable(Method* old_method, Method* new_method, BciMap &bci_map);
59 };
60 
61 
62 // ByteCode Index Map. For two versions of the same method, where the new version may contain
63 // fragments not found in the old version, provides a mapping from an index of a bytecode in
64 // the old method to the index of the same bytecode in the new method.
65 
66 class BciMap {
67  private:
68   int *_old_bci, *_new_st_bci, *_new_end_bci;
69   int _cur_size, _cur_pos;
70   int _pos;
71 
72  public:
BciMap()73   BciMap() {
74     _cur_size = 50;
75     _old_bci = (int*) malloc(sizeof(int) * _cur_size);
76     _new_st_bci = (int*) malloc(sizeof(int) * _cur_size);
77     _new_end_bci = (int*) malloc(sizeof(int) * _cur_size);
78     _cur_pos = 0;
79   }
80 
~BciMap()81   ~BciMap() {
82     free(_old_bci);
83     free(_new_st_bci);
84     free(_new_end_bci);
85   }
86 
87   // Store the position of an added fragment, e.g.
88   //
89   //                              |<- old_bci
90   // -----------------------------------------
91   // Old method   |invokevirtual 5|aload 1|...
92   // -----------------------------------------
93   //
94   //                                 |<- new_st_bci          |<- new_end_bci
95   // --------------------------------------------------------------------
96   // New method       |invokevirual 5|aload 2|invokevirtual 6|aload 1|...
97   // --------------------------------------------------------------------
98   //                                 ^^^^^^^^^^^^^^^^^^^^^^^^
99   //                                    Added fragment
100 
store_fragment_location(int old_bci,int new_st_bci,int new_end_bci)101   void store_fragment_location(int old_bci, int new_st_bci, int new_end_bci) {
102     if (_cur_pos == _cur_size) {
103       _cur_size += 10;
104       _old_bci = (int*) realloc(_old_bci, sizeof(int) * _cur_size);
105       _new_st_bci = (int*) realloc(_new_st_bci, sizeof(int) * _cur_size);
106       _new_end_bci = (int*) realloc(_new_end_bci, sizeof(int) * _cur_size);
107     }
108     _old_bci[_cur_pos] = old_bci;
109     _new_st_bci[_cur_pos] = new_st_bci;
110     _new_end_bci[_cur_pos] = new_end_bci;
111     _cur_pos++;
112   }
113 
new_bci_for_old(int old_bci)114   int new_bci_for_old(int old_bci) {
115     if (_cur_pos == 0 || old_bci < _old_bci[0]) return old_bci;
116     _pos = 1;
117     while (_pos < _cur_pos && old_bci >= _old_bci[_pos])
118       _pos++;
119     return _new_end_bci[_pos-1] + (old_bci - _old_bci[_pos-1]);
120   }
121 
122   // Test if two indexes - one in the old method and another in the new one - correspond
123   // to the same bytecode
old_and_new_locations_same(int old_dest_bci,int new_dest_bci)124   bool old_and_new_locations_same(int old_dest_bci, int new_dest_bci) {
125     if (new_bci_for_old(old_dest_bci) == new_dest_bci)
126       return true;
127     else if (_old_bci[_pos-1] == old_dest_bci)
128       return (new_dest_bci == _new_st_bci[_pos-1]);
129     else return false;
130   }
131 };
132 
133 #endif // SHARE_VM_PRIMS_METHODCOMPARATOR_HPP
134