1 /*
2  * Copyright (c) 1997, 2020, 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_INTERPRETER_TEMPLATETABLE_HPP
26 #define SHARE_INTERPRETER_TEMPLATETABLE_HPP
27 
28 #include "interpreter/bytecodes.hpp"
29 #include "memory/allocation.hpp"
30 #include "runtime/frame.hpp"
31 #include "utilities/macros.hpp"
32 
33 #ifndef ZERO
34 // All the necessary definitions used for (bytecode) template generation. Instead of
35 // spreading the implementation functionality for each bytecode in the interpreter
36 // and the snippet generator, a template is assigned to each bytecode which can be
37 // used to generate the bytecode's implementation if needed.
38 
39 class InterpreterMacroAssembler;
40 
41 // A Template describes the properties of a code template for a given bytecode
42 // and provides a generator to generate the code template.
43 
44 class Template {
45  private:
46   enum Flags {
47     uses_bcp_bit,                                // set if template needs the bcp pointing to bytecode
48     does_dispatch_bit,                           // set if template dispatches on its own
49     calls_vm_bit,                                // set if template calls the vm
50     wide_bit                                     // set if template belongs to a wide instruction
51   };
52 
53   typedef void (*generator)(int arg);
54 
55   int       _flags;                              // describes interpreter template properties (bcp unknown)
56   TosState  _tos_in;                             // tos cache state before template execution
57   TosState  _tos_out;                            // tos cache state after  template execution
58   generator _gen;                                // template code generator
59   int       _arg;                                // argument for template code generator
60 
61   void      initialize(int flags, TosState tos_in, TosState tos_out, generator gen, int arg);
62 
63   friend class TemplateTable;
64 
65  public:
66   Bytecodes::Code bytecode() const;
is_valid() const67   bool      is_valid() const                     { return _gen != NULL; }
uses_bcp() const68   bool      uses_bcp() const                     { return (_flags & (1 << uses_bcp_bit     )) != 0; }
does_dispatch() const69   bool      does_dispatch() const                { return (_flags & (1 << does_dispatch_bit)) != 0; }
calls_vm() const70   bool      calls_vm() const                     { return (_flags & (1 << calls_vm_bit     )) != 0; }
is_wide() const71   bool      is_wide() const                      { return (_flags & (1 << wide_bit         )) != 0; }
tos_in() const72   TosState  tos_in() const                       { return _tos_in; }
tos_out() const73   TosState  tos_out() const                      { return _tos_out; }
74   void      generate(InterpreterMacroAssembler* masm);
75 };
76 
77 
78 // The TemplateTable defines all Templates and provides accessor functions
79 // to get the template for a given bytecode.
80 
81 class TemplateTable: AllStatic {
82  public:
83   enum Operation { add, sub, mul, div, rem, _and, _or, _xor, shl, shr, ushr };
84   enum Condition { equal, not_equal, less, less_equal, greater, greater_equal };
85   enum CacheByte { f1_byte = 1, f2_byte = 2 };  // byte_no codes
86   enum RewriteControl { may_rewrite, may_not_rewrite };  // control for fast code under CDS
87 
88  private:
89   static Template        _template_table     [Bytecodes::number_of_codes];
90   static Template        _template_table_wide[Bytecodes::number_of_codes];
91 
92   static Template*       _desc;                  // the current template to be generated
bytecode()93   static Bytecodes::Code bytecode()              { return _desc->bytecode(); }
94  public:
95   //%note templates_1
96   static InterpreterMacroAssembler* _masm;       // the assembler used when generating templates
97 
98  private:
99 
100   // special registers
101   static inline Address at_bcp(int offset);
102 
103   // helpers
104   static void unimplemented_bc();
105   static void patch_bytecode(Bytecodes::Code bc, Register bc_reg,
106                              Register temp_reg, bool load_bc_into_bc_reg = true, int byte_no = -1);
107 
108   // C calls
109   static void call_VM(Register oop_result, address entry_point);
110   static void call_VM(Register oop_result, address entry_point, Register arg_1);
111   static void call_VM(Register oop_result, address entry_point, Register arg_1, Register arg_2);
112   static void call_VM(Register oop_result, address entry_point, Register arg_1, Register arg_2, Register arg_3);
113 
114   // these overloadings are not presently used on SPARC:
115   static void call_VM(Register oop_result, Register last_java_sp, address entry_point);
116   static void call_VM(Register oop_result, Register last_java_sp, address entry_point, Register arg_1);
117   static void call_VM(Register oop_result, Register last_java_sp, address entry_point, Register arg_1, Register arg_2);
118   static void call_VM(Register oop_result, Register last_java_sp, address entry_point, Register arg_1, Register arg_2, Register arg_3);
119 
120   // bytecodes
121   static void nop();
122 
123   static void aconst_null();
124   static void iconst(int value);
125   static void lconst(int value);
126   static void fconst(int value);
127   static void dconst(int value);
128 
129   static void bipush();
130   static void sipush();
131   static void ldc(bool wide);
132   static void ldc2_w();
133   static void fast_aldc(bool wide);
134 
135   static void locals_index(Register reg, int offset = 1);
136   static void iload();
137   static void fast_iload();
138   static void fast_iload2();
139   static void fast_icaload();
140   static void lload();
141   static void fload();
142   static void dload();
143   static void aload();
144 
145   static void locals_index_wide(Register reg);
146   static void wide_iload();
147   static void wide_lload();
148   static void wide_fload();
149   static void wide_dload();
150   static void wide_aload();
151 
152   static void iaload();
153   static void laload();
154   static void faload();
155   static void daload();
156   static void aaload();
157   static void baload();
158   static void caload();
159   static void saload();
160 
161   static void iload(int n);
162   static void lload(int n);
163   static void fload(int n);
164   static void dload(int n);
165   static void aload(int n);
166   static void aload_0();
167   static void nofast_aload_0();
168   static void nofast_iload();
169   static void iload_internal(RewriteControl rc = may_rewrite);
170   static void aload_0_internal(RewriteControl rc = may_rewrite);
171 
172   static void istore();
173   static void lstore();
174   static void fstore();
175   static void dstore();
176   static void astore();
177 
178   static void wide_istore();
179   static void wide_lstore();
180   static void wide_fstore();
181   static void wide_dstore();
182   static void wide_astore();
183 
184   static void iastore();
185   static void lastore();
186   static void fastore();
187   static void dastore();
188   static void aastore();
189   static void bastore();
190   static void castore();
191   static void sastore();
192 
193   static void istore(int n);
194   static void lstore(int n);
195   static void fstore(int n);
196   static void dstore(int n);
197   static void astore(int n);
198 
199   static void pop();
200   static void pop2();
201   static void dup();
202   static void dup_x1();
203   static void dup_x2();
204   static void dup2();
205   static void dup2_x1();
206   static void dup2_x2();
207   static void swap();
208 
209   static void iop2(Operation op);
210   static void lop2(Operation op);
211   static void fop2(Operation op);
212   static void dop2(Operation op);
213 
214   static void idiv();
215   static void irem();
216 
217   static void lmul();
218   static void ldiv();
219   static void lrem();
220   static void lshl();
221   static void lshr();
222   static void lushr();
223 
224   static void ineg();
225   static void lneg();
226   static void fneg();
227   static void dneg();
228 
229   static void iinc();
230   static void wide_iinc();
231   static void convert();
232   static void lcmp();
233 
234   static void float_cmp (bool is_float, int unordered_result);
235   static void float_cmp (int unordered_result);
236   static void double_cmp(int unordered_result);
237 
238   static void branch(bool is_jsr, bool is_wide);
239   static void if_0cmp   (Condition cc);
240   static void if_icmp   (Condition cc);
241   static void if_nullcmp(Condition cc);
242   static void if_acmp   (Condition cc);
243 
244   static void _goto();
245   static void jsr();
246   static void ret();
247   static void wide_ret();
248 
249   static void goto_w();
250   static void jsr_w();
251 
252   static void tableswitch();
253   static void lookupswitch();
254   static void fast_linearswitch();
255   static void fast_binaryswitch();
256 
257   static void _return(TosState state);
258 
259   static void resolve_cache_and_index(int byte_no,       // one of 1,2,11
260                                       Register cache,    // output for CP cache
261                                       Register index,    // output for CP index
262                                       size_t index_size); // one of 1,2,4
263   static void load_invoke_cp_cache_entry(int byte_no,
264                                          Register method,
265                                          Register itable_index,
266                                          Register flags,
267                                          bool is_invokevirtual,
268                                          bool is_virtual_final,
269                                          bool is_invokedynamic);
270   static void load_field_cp_cache_entry(Register obj,
271                                         Register cache,
272                                         Register index,
273                                         Register offset,
274                                         Register flags,
275                                         bool is_static);
276   static void invokevirtual(int byte_no);
277   static void invokespecial(int byte_no);
278   static void invokestatic(int byte_no);
279   static void invokeinterface(int byte_no);
280   static void invokedynamic(int byte_no);
281   static void invokehandle(int byte_no);
282   static void fast_invokevfinal(int byte_no);
283 
284   static void getfield_or_static(int byte_no, bool is_static, RewriteControl rc = may_rewrite);
285   static void putfield_or_static(int byte_no, bool is_static, RewriteControl rc = may_rewrite);
286 
287   static void getfield(int byte_no);
288   static void putfield(int byte_no);
289   static void nofast_getfield(int byte_no);
290   static void nofast_putfield(int byte_no);
291   static void getstatic(int byte_no);
292   static void putstatic(int byte_no);
293   static void pop_and_check_object(Register obj);
294   static void condy_helper(Label& Done);  // shared by ldc instances
295 
296   static void _new();
297   static void newarray();
298   static void anewarray();
299   static void arraylength();
300   static void checkcast();
301   static void instanceof();
302 
303   static void athrow();
304 
305   static void monitorenter();
306   static void monitorexit();
307 
308   static void wide();
309   static void multianewarray();
310 
311   static void fast_xaccess(TosState state);
312   static void fast_accessfield(TosState state);
313   static void fast_storefield(TosState state);
314 
315   static void _breakpoint();
316 
317   static void shouldnotreachhere();
318 
319   // jvmti support
320   static void jvmti_post_field_access(Register cache, Register index, bool is_static, bool has_tos);
321   static void jvmti_post_field_mod(Register cache, Register index, bool is_static);
322   static void jvmti_post_fast_field_mod();
323 
324   // debugging of TemplateGenerator
325   static void transition(TosState tos_in, TosState tos_out);// checks if in/out states expected by template generator correspond to table entries
326 
327   // initialization helpers
328   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(            ), char filler );
329   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(int arg     ), int arg     );
330   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(bool arg    ), bool arg    );
331   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(TosState tos), TosState tos);
332   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(Operation op), Operation op);
333   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(Condition cc), Condition cc);
334 
335   friend class Template;
336 
337   // InterpreterMacroAssembler::is_a(), etc., need TemplateTable::call_VM().
338   friend class InterpreterMacroAssembler;
339 
340  public:
341   // Initialization
342   static void initialize();
343 
344   // Templates
template_for(Bytecodes::Code code)345   static Template* template_for     (Bytecodes::Code code)  { Bytecodes::check     (code); return &_template_table     [code]; }
template_for_wide(Bytecodes::Code code)346   static Template* template_for_wide(Bytecodes::Code code)  { Bytecodes::wide_check(code); return &_template_table_wide[code]; }
347 
348   // Platform specifics
349 #include CPU_HEADER(templateTable)
350 
351 };
352 #endif /* !ZERO */
353 
354 #endif // SHARE_INTERPRETER_TEMPLATETABLE_HPP
355