sharkBlock.cpp revision 5776:de6a9e811145
1/*
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3 * Copyright 2008, 2009, 2010 Red Hat, Inc.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26#include "precompiled.hpp"
27#include "interpreter/bytecodes.hpp"
28#include "shark/llvmHeaders.hpp"
29#include "shark/llvmValue.hpp"
30#include "shark/sharkBlock.hpp"
31#include "shark/sharkBuilder.hpp"
32#include "shark/sharkConstant.hpp"
33#include "shark/sharkState.hpp"
34#include "shark/sharkValue.hpp"
35#include "shark/shark_globals.hpp"
36#include "utilities/debug.hpp"
37
38using namespace llvm;
39
40void SharkBlock::parse_bytecode(int start, int limit) {
41  SharkValue *a, *b, *c, *d;
42  int i;
43
44  // Ensure the current state is initialized before we emit any code,
45  // so that any setup code for the state is at the start of the block
46  current_state();
47
48  // Parse the bytecodes
49  iter()->reset_to_bci(start);
50  while (iter()->next_bci() < limit) {
51    NOT_PRODUCT(a = b = c = d = NULL);
52    iter()->next();
53
54    if (SharkTraceBytecodes)
55      tty->print_cr("%4d: %s", bci(), Bytecodes::name(bc()));
56
57    if (has_trap() && trap_bci() == bci()) {
58      do_trap(trap_request());
59      return;
60    }
61
62    if (UseLoopSafepoints) {
63      // XXX if a lcmp is followed by an if_?? then C2 maybe-inserts
64      // the safepoint before the lcmp rather than before the if.
65      // Maybe we should do this too.  See parse2.cpp for details.
66      switch (bc()) {
67      case Bytecodes::_goto:
68      case Bytecodes::_ifnull:
69      case Bytecodes::_ifnonnull:
70      case Bytecodes::_if_acmpeq:
71      case Bytecodes::_if_acmpne:
72      case Bytecodes::_ifeq:
73      case Bytecodes::_ifne:
74      case Bytecodes::_iflt:
75      case Bytecodes::_ifle:
76      case Bytecodes::_ifgt:
77      case Bytecodes::_ifge:
78      case Bytecodes::_if_icmpeq:
79      case Bytecodes::_if_icmpne:
80      case Bytecodes::_if_icmplt:
81      case Bytecodes::_if_icmple:
82      case Bytecodes::_if_icmpgt:
83      case Bytecodes::_if_icmpge:
84        if (iter()->get_dest() <= bci())
85          maybe_add_backedge_safepoint();
86        break;
87
88      case Bytecodes::_goto_w:
89        if (iter()->get_far_dest() <= bci())
90          maybe_add_backedge_safepoint();
91        break;
92
93      case Bytecodes::_tableswitch:
94      case Bytecodes::_lookupswitch:
95        if (switch_default_dest() <= bci()) {
96          maybe_add_backedge_safepoint();
97          break;
98        }
99        int len = switch_table_length();
100        for (int i = 0; i < len; i++) {
101          if (switch_dest(i) <= bci()) {
102            maybe_add_backedge_safepoint();
103            break;
104          }
105        }
106        break;
107      }
108    }
109
110    switch (bc()) {
111    case Bytecodes::_nop:
112      break;
113
114    case Bytecodes::_aconst_null:
115      push(SharkValue::null());
116      break;
117
118    case Bytecodes::_iconst_m1:
119      push(SharkValue::jint_constant(-1));
120      break;
121    case Bytecodes::_iconst_0:
122      push(SharkValue::jint_constant(0));
123      break;
124    case Bytecodes::_iconst_1:
125      push(SharkValue::jint_constant(1));
126      break;
127    case Bytecodes::_iconst_2:
128      push(SharkValue::jint_constant(2));
129      break;
130    case Bytecodes::_iconst_3:
131      push(SharkValue::jint_constant(3));
132      break;
133    case Bytecodes::_iconst_4:
134      push(SharkValue::jint_constant(4));
135      break;
136    case Bytecodes::_iconst_5:
137      push(SharkValue::jint_constant(5));
138      break;
139
140    case Bytecodes::_lconst_0:
141      push(SharkValue::jlong_constant(0));
142      break;
143    case Bytecodes::_lconst_1:
144      push(SharkValue::jlong_constant(1));
145      break;
146
147    case Bytecodes::_fconst_0:
148      push(SharkValue::jfloat_constant(0));
149      break;
150    case Bytecodes::_fconst_1:
151      push(SharkValue::jfloat_constant(1));
152      break;
153    case Bytecodes::_fconst_2:
154      push(SharkValue::jfloat_constant(2));
155      break;
156
157    case Bytecodes::_dconst_0:
158      push(SharkValue::jdouble_constant(0));
159      break;
160    case Bytecodes::_dconst_1:
161      push(SharkValue::jdouble_constant(1));
162      break;
163
164    case Bytecodes::_bipush:
165      push(SharkValue::jint_constant(iter()->get_constant_u1()));
166      break;
167    case Bytecodes::_sipush:
168      push(SharkValue::jint_constant(iter()->get_constant_u2()));
169      break;
170
171    case Bytecodes::_ldc:
172    case Bytecodes::_ldc_w:
173    case Bytecodes::_ldc2_w: {
174      SharkConstant* constant = SharkConstant::for_ldc(iter());
175      assert(constant->is_loaded(), "trap should handle unloaded classes");
176      push(constant->value(builder()));
177      break;
178    }
179    case Bytecodes::_iload_0:
180    case Bytecodes::_lload_0:
181    case Bytecodes::_fload_0:
182    case Bytecodes::_dload_0:
183    case Bytecodes::_aload_0:
184      push(local(0));
185      break;
186    case Bytecodes::_iload_1:
187    case Bytecodes::_lload_1:
188    case Bytecodes::_fload_1:
189    case Bytecodes::_dload_1:
190    case Bytecodes::_aload_1:
191      push(local(1));
192      break;
193    case Bytecodes::_iload_2:
194    case Bytecodes::_lload_2:
195    case Bytecodes::_fload_2:
196    case Bytecodes::_dload_2:
197    case Bytecodes::_aload_2:
198      push(local(2));
199      break;
200    case Bytecodes::_iload_3:
201    case Bytecodes::_lload_3:
202    case Bytecodes::_fload_3:
203    case Bytecodes::_dload_3:
204    case Bytecodes::_aload_3:
205      push(local(3));
206      break;
207    case Bytecodes::_iload:
208    case Bytecodes::_lload:
209    case Bytecodes::_fload:
210    case Bytecodes::_dload:
211    case Bytecodes::_aload:
212      push(local(iter()->get_index()));
213      break;
214
215    case Bytecodes::_baload:
216      do_aload(T_BYTE);
217      break;
218    case Bytecodes::_caload:
219      do_aload(T_CHAR);
220      break;
221    case Bytecodes::_saload:
222      do_aload(T_SHORT);
223      break;
224    case Bytecodes::_iaload:
225      do_aload(T_INT);
226      break;
227    case Bytecodes::_laload:
228      do_aload(T_LONG);
229      break;
230    case Bytecodes::_faload:
231      do_aload(T_FLOAT);
232      break;
233    case Bytecodes::_daload:
234      do_aload(T_DOUBLE);
235      break;
236    case Bytecodes::_aaload:
237      do_aload(T_OBJECT);
238      break;
239
240    case Bytecodes::_istore_0:
241    case Bytecodes::_lstore_0:
242    case Bytecodes::_fstore_0:
243    case Bytecodes::_dstore_0:
244    case Bytecodes::_astore_0:
245      set_local(0, pop());
246      break;
247    case Bytecodes::_istore_1:
248    case Bytecodes::_lstore_1:
249    case Bytecodes::_fstore_1:
250    case Bytecodes::_dstore_1:
251    case Bytecodes::_astore_1:
252      set_local(1, pop());
253      break;
254    case Bytecodes::_istore_2:
255    case Bytecodes::_lstore_2:
256    case Bytecodes::_fstore_2:
257    case Bytecodes::_dstore_2:
258    case Bytecodes::_astore_2:
259      set_local(2, pop());
260      break;
261    case Bytecodes::_istore_3:
262    case Bytecodes::_lstore_3:
263    case Bytecodes::_fstore_3:
264    case Bytecodes::_dstore_3:
265    case Bytecodes::_astore_3:
266      set_local(3, pop());
267      break;
268    case Bytecodes::_istore:
269    case Bytecodes::_lstore:
270    case Bytecodes::_fstore:
271    case Bytecodes::_dstore:
272    case Bytecodes::_astore:
273      set_local(iter()->get_index(), pop());
274      break;
275
276    case Bytecodes::_bastore:
277      do_astore(T_BYTE);
278      break;
279    case Bytecodes::_castore:
280      do_astore(T_CHAR);
281      break;
282    case Bytecodes::_sastore:
283      do_astore(T_SHORT);
284      break;
285    case Bytecodes::_iastore:
286      do_astore(T_INT);
287      break;
288    case Bytecodes::_lastore:
289      do_astore(T_LONG);
290      break;
291    case Bytecodes::_fastore:
292      do_astore(T_FLOAT);
293      break;
294    case Bytecodes::_dastore:
295      do_astore(T_DOUBLE);
296      break;
297    case Bytecodes::_aastore:
298      do_astore(T_OBJECT);
299      break;
300
301    case Bytecodes::_pop:
302      xpop();
303      break;
304    case Bytecodes::_pop2:
305      xpop();
306      xpop();
307      break;
308    case Bytecodes::_swap:
309      a = xpop();
310      b = xpop();
311      xpush(a);
312      xpush(b);
313      break;
314    case Bytecodes::_dup:
315      a = xpop();
316      xpush(a);
317      xpush(a);
318      break;
319    case Bytecodes::_dup_x1:
320      a = xpop();
321      b = xpop();
322      xpush(a);
323      xpush(b);
324      xpush(a);
325      break;
326    case Bytecodes::_dup_x2:
327      a = xpop();
328      b = xpop();
329      c = xpop();
330      xpush(a);
331      xpush(c);
332      xpush(b);
333      xpush(a);
334      break;
335    case Bytecodes::_dup2:
336      a = xpop();
337      b = xpop();
338      xpush(b);
339      xpush(a);
340      xpush(b);
341      xpush(a);
342      break;
343    case Bytecodes::_dup2_x1:
344      a = xpop();
345      b = xpop();
346      c = xpop();
347      xpush(b);
348      xpush(a);
349      xpush(c);
350      xpush(b);
351      xpush(a);
352      break;
353    case Bytecodes::_dup2_x2:
354      a = xpop();
355      b = xpop();
356      c = xpop();
357      d = xpop();
358      xpush(b);
359      xpush(a);
360      xpush(d);
361      xpush(c);
362      xpush(b);
363      xpush(a);
364      break;
365
366    case Bytecodes::_arraylength:
367      do_arraylength();
368      break;
369
370    case Bytecodes::_getfield:
371      do_getfield();
372      break;
373    case Bytecodes::_getstatic:
374      do_getstatic();
375      break;
376    case Bytecodes::_putfield:
377      do_putfield();
378      break;
379    case Bytecodes::_putstatic:
380      do_putstatic();
381      break;
382
383    case Bytecodes::_iadd:
384      b = pop();
385      a = pop();
386      push(SharkValue::create_jint(
387        builder()->CreateAdd(a->jint_value(), b->jint_value()), false));
388      break;
389    case Bytecodes::_isub:
390      b = pop();
391      a = pop();
392      push(SharkValue::create_jint(
393        builder()->CreateSub(a->jint_value(), b->jint_value()), false));
394      break;
395    case Bytecodes::_imul:
396      b = pop();
397      a = pop();
398      push(SharkValue::create_jint(
399        builder()->CreateMul(a->jint_value(), b->jint_value()), false));
400      break;
401    case Bytecodes::_idiv:
402      do_idiv();
403      break;
404    case Bytecodes::_irem:
405      do_irem();
406      break;
407    case Bytecodes::_ineg:
408      a = pop();
409      push(SharkValue::create_jint(
410        builder()->CreateNeg(a->jint_value()), a->zero_checked()));
411      break;
412    case Bytecodes::_ishl:
413      b = pop();
414      a = pop();
415      push(SharkValue::create_jint(
416        builder()->CreateShl(
417          a->jint_value(),
418          builder()->CreateAnd(
419            b->jint_value(), LLVMValue::jint_constant(0x1f))), false));
420      break;
421    case Bytecodes::_ishr:
422      b = pop();
423      a = pop();
424      push(SharkValue::create_jint(
425        builder()->CreateAShr(
426          a->jint_value(),
427          builder()->CreateAnd(
428            b->jint_value(), LLVMValue::jint_constant(0x1f))), false));
429      break;
430    case Bytecodes::_iushr:
431      b = pop();
432      a = pop();
433      push(SharkValue::create_jint(
434        builder()->CreateLShr(
435          a->jint_value(),
436          builder()->CreateAnd(
437            b->jint_value(), LLVMValue::jint_constant(0x1f))), false));
438      break;
439    case Bytecodes::_iand:
440      b = pop();
441      a = pop();
442      push(SharkValue::create_jint(
443        builder()->CreateAnd(a->jint_value(), b->jint_value()), false));
444      break;
445    case Bytecodes::_ior:
446      b = pop();
447      a = pop();
448      push(SharkValue::create_jint(
449        builder()->CreateOr(a->jint_value(), b->jint_value()),
450        a->zero_checked() && b->zero_checked()));
451      break;
452    case Bytecodes::_ixor:
453      b = pop();
454      a = pop();
455      push(SharkValue::create_jint(
456        builder()->CreateXor(a->jint_value(), b->jint_value()), false));
457      break;
458
459    case Bytecodes::_ladd:
460      b = pop();
461      a = pop();
462      push(SharkValue::create_jlong(
463        builder()->CreateAdd(a->jlong_value(), b->jlong_value()), false));
464      break;
465    case Bytecodes::_lsub:
466      b = pop();
467      a = pop();
468      push(SharkValue::create_jlong(
469        builder()->CreateSub(a->jlong_value(), b->jlong_value()), false));
470      break;
471    case Bytecodes::_lmul:
472      b = pop();
473      a = pop();
474      push(SharkValue::create_jlong(
475        builder()->CreateMul(a->jlong_value(), b->jlong_value()), false));
476      break;
477    case Bytecodes::_ldiv:
478      do_ldiv();
479      break;
480    case Bytecodes::_lrem:
481      do_lrem();
482      break;
483    case Bytecodes::_lneg:
484      a = pop();
485      push(SharkValue::create_jlong(
486        builder()->CreateNeg(a->jlong_value()), a->zero_checked()));
487      break;
488    case Bytecodes::_lshl:
489      b = pop();
490      a = pop();
491      push(SharkValue::create_jlong(
492        builder()->CreateShl(
493          a->jlong_value(),
494          builder()->CreateIntCast(
495            builder()->CreateAnd(
496              b->jint_value(), LLVMValue::jint_constant(0x3f)),
497            SharkType::jlong_type(), true)), false));
498      break;
499    case Bytecodes::_lshr:
500      b = pop();
501      a = pop();
502      push(SharkValue::create_jlong(
503        builder()->CreateAShr(
504          a->jlong_value(),
505          builder()->CreateIntCast(
506            builder()->CreateAnd(
507              b->jint_value(), LLVMValue::jint_constant(0x3f)),
508            SharkType::jlong_type(), true)), false));
509      break;
510    case Bytecodes::_lushr:
511      b = pop();
512      a = pop();
513      push(SharkValue::create_jlong(
514        builder()->CreateLShr(
515          a->jlong_value(),
516          builder()->CreateIntCast(
517            builder()->CreateAnd(
518              b->jint_value(), LLVMValue::jint_constant(0x3f)),
519            SharkType::jlong_type(), true)), false));
520      break;
521    case Bytecodes::_land:
522      b = pop();
523      a = pop();
524      push(SharkValue::create_jlong(
525        builder()->CreateAnd(a->jlong_value(), b->jlong_value()), false));
526      break;
527    case Bytecodes::_lor:
528      b = pop();
529      a = pop();
530      push(SharkValue::create_jlong(
531        builder()->CreateOr(a->jlong_value(), b->jlong_value()),
532        a->zero_checked() && b->zero_checked()));
533      break;
534    case Bytecodes::_lxor:
535      b = pop();
536      a = pop();
537      push(SharkValue::create_jlong(
538        builder()->CreateXor(a->jlong_value(), b->jlong_value()), false));
539      break;
540
541    case Bytecodes::_fadd:
542      b = pop();
543      a = pop();
544      push(SharkValue::create_jfloat(
545        builder()->CreateFAdd(a->jfloat_value(), b->jfloat_value())));
546      break;
547    case Bytecodes::_fsub:
548      b = pop();
549      a = pop();
550      push(SharkValue::create_jfloat(
551        builder()->CreateFSub(a->jfloat_value(), b->jfloat_value())));
552      break;
553    case Bytecodes::_fmul:
554      b = pop();
555      a = pop();
556      push(SharkValue::create_jfloat(
557        builder()->CreateFMul(a->jfloat_value(), b->jfloat_value())));
558      break;
559    case Bytecodes::_fdiv:
560      b = pop();
561      a = pop();
562      push(SharkValue::create_jfloat(
563        builder()->CreateFDiv(a->jfloat_value(), b->jfloat_value())));
564      break;
565    case Bytecodes::_frem:
566      b = pop();
567      a = pop();
568      push(SharkValue::create_jfloat(
569        builder()->CreateFRem(a->jfloat_value(), b->jfloat_value())));
570      break;
571    case Bytecodes::_fneg:
572      a = pop();
573      push(SharkValue::create_jfloat(
574        builder()->CreateFNeg(a->jfloat_value())));
575      break;
576
577    case Bytecodes::_dadd:
578      b = pop();
579      a = pop();
580      push(SharkValue::create_jdouble(
581        builder()->CreateFAdd(a->jdouble_value(), b->jdouble_value())));
582      break;
583    case Bytecodes::_dsub:
584      b = pop();
585      a = pop();
586      push(SharkValue::create_jdouble(
587        builder()->CreateFSub(a->jdouble_value(), b->jdouble_value())));
588      break;
589    case Bytecodes::_dmul:
590      b = pop();
591      a = pop();
592      push(SharkValue::create_jdouble(
593        builder()->CreateFMul(a->jdouble_value(), b->jdouble_value())));
594      break;
595    case Bytecodes::_ddiv:
596      b = pop();
597      a = pop();
598      push(SharkValue::create_jdouble(
599        builder()->CreateFDiv(a->jdouble_value(), b->jdouble_value())));
600      break;
601    case Bytecodes::_drem:
602      b = pop();
603      a = pop();
604      push(SharkValue::create_jdouble(
605        builder()->CreateFRem(a->jdouble_value(), b->jdouble_value())));
606      break;
607    case Bytecodes::_dneg:
608      a = pop();
609      push(SharkValue::create_jdouble(
610        builder()->CreateFNeg(a->jdouble_value())));
611      break;
612
613    case Bytecodes::_iinc:
614      i = iter()->get_index();
615      set_local(
616        i,
617        SharkValue::create_jint(
618          builder()->CreateAdd(
619            LLVMValue::jint_constant(iter()->get_iinc_con()),
620            local(i)->jint_value()), false));
621      break;
622
623    case Bytecodes::_lcmp:
624      do_lcmp();
625      break;
626
627    case Bytecodes::_fcmpl:
628      do_fcmp(false, false);
629      break;
630    case Bytecodes::_fcmpg:
631      do_fcmp(false, true);
632      break;
633    case Bytecodes::_dcmpl:
634      do_fcmp(true, false);
635      break;
636    case Bytecodes::_dcmpg:
637      do_fcmp(true, true);
638      break;
639
640    case Bytecodes::_i2l:
641      a = pop();
642      push(SharkValue::create_jlong(
643        builder()->CreateIntCast(
644          a->jint_value(), SharkType::jlong_type(), true), a->zero_checked()));
645      break;
646    case Bytecodes::_i2f:
647      push(SharkValue::create_jfloat(
648        builder()->CreateSIToFP(
649          pop()->jint_value(), SharkType::jfloat_type())));
650      break;
651    case Bytecodes::_i2d:
652      push(SharkValue::create_jdouble(
653        builder()->CreateSIToFP(
654          pop()->jint_value(), SharkType::jdouble_type())));
655      break;
656
657    case Bytecodes::_l2i:
658      push(SharkValue::create_jint(
659        builder()->CreateIntCast(
660          pop()->jlong_value(), SharkType::jint_type(), true), false));
661      break;
662    case Bytecodes::_l2f:
663      push(SharkValue::create_jfloat(
664        builder()->CreateSIToFP(
665          pop()->jlong_value(), SharkType::jfloat_type())));
666      break;
667    case Bytecodes::_l2d:
668      push(SharkValue::create_jdouble(
669        builder()->CreateSIToFP(
670          pop()->jlong_value(), SharkType::jdouble_type())));
671      break;
672
673    case Bytecodes::_f2i:
674      push(SharkValue::create_jint(
675        builder()->CreateCall(
676          builder()->f2i(), pop()->jfloat_value()), false));
677      break;
678    case Bytecodes::_f2l:
679      push(SharkValue::create_jlong(
680        builder()->CreateCall(
681          builder()->f2l(), pop()->jfloat_value()), false));
682      break;
683    case Bytecodes::_f2d:
684      push(SharkValue::create_jdouble(
685        builder()->CreateFPExt(
686          pop()->jfloat_value(), SharkType::jdouble_type())));
687      break;
688
689    case Bytecodes::_d2i:
690      push(SharkValue::create_jint(
691        builder()->CreateCall(
692          builder()->d2i(), pop()->jdouble_value()), false));
693      break;
694    case Bytecodes::_d2l:
695      push(SharkValue::create_jlong(
696        builder()->CreateCall(
697          builder()->d2l(), pop()->jdouble_value()), false));
698      break;
699    case Bytecodes::_d2f:
700      push(SharkValue::create_jfloat(
701        builder()->CreateFPTrunc(
702          pop()->jdouble_value(), SharkType::jfloat_type())));
703      break;
704
705    case Bytecodes::_i2b:
706      push(SharkValue::create_jint(
707        builder()->CreateAShr(
708          builder()->CreateShl(
709            pop()->jint_value(),
710            LLVMValue::jint_constant(24)),
711          LLVMValue::jint_constant(24)), false));
712      break;
713    case Bytecodes::_i2c:
714      push(SharkValue::create_jint(
715        builder()->CreateAnd(
716            pop()->jint_value(),
717            LLVMValue::jint_constant(0xffff)), false));
718      break;
719    case Bytecodes::_i2s:
720      push(SharkValue::create_jint(
721        builder()->CreateAShr(
722          builder()->CreateShl(
723            pop()->jint_value(),
724            LLVMValue::jint_constant(16)),
725          LLVMValue::jint_constant(16)), false));
726      break;
727
728    case Bytecodes::_return:
729      do_return(T_VOID);
730      break;
731    case Bytecodes::_ireturn:
732      do_return(T_INT);
733      break;
734    case Bytecodes::_lreturn:
735      do_return(T_LONG);
736      break;
737    case Bytecodes::_freturn:
738      do_return(T_FLOAT);
739      break;
740    case Bytecodes::_dreturn:
741      do_return(T_DOUBLE);
742      break;
743    case Bytecodes::_areturn:
744      do_return(T_OBJECT);
745      break;
746
747    case Bytecodes::_athrow:
748      do_athrow();
749      break;
750
751    case Bytecodes::_goto:
752    case Bytecodes::_goto_w:
753      do_goto();
754      break;
755
756    case Bytecodes::_jsr:
757    case Bytecodes::_jsr_w:
758      do_jsr();
759      break;
760
761    case Bytecodes::_ret:
762      do_ret();
763      break;
764
765    case Bytecodes::_ifnull:
766      do_if(ICmpInst::ICMP_EQ, SharkValue::null(), pop());
767      break;
768    case Bytecodes::_ifnonnull:
769      do_if(ICmpInst::ICMP_NE, SharkValue::null(), pop());
770      break;
771    case Bytecodes::_if_acmpeq:
772      b = pop();
773      a = pop();
774      do_if(ICmpInst::ICMP_EQ, b, a);
775      break;
776    case Bytecodes::_if_acmpne:
777      b = pop();
778      a = pop();
779      do_if(ICmpInst::ICMP_NE, b, a);
780      break;
781    case Bytecodes::_ifeq:
782      do_if(ICmpInst::ICMP_EQ, SharkValue::jint_constant(0), pop());
783      break;
784    case Bytecodes::_ifne:
785      do_if(ICmpInst::ICMP_NE, SharkValue::jint_constant(0), pop());
786      break;
787    case Bytecodes::_iflt:
788      do_if(ICmpInst::ICMP_SLT, SharkValue::jint_constant(0), pop());
789      break;
790    case Bytecodes::_ifle:
791      do_if(ICmpInst::ICMP_SLE, SharkValue::jint_constant(0), pop());
792      break;
793    case Bytecodes::_ifgt:
794      do_if(ICmpInst::ICMP_SGT, SharkValue::jint_constant(0), pop());
795      break;
796    case Bytecodes::_ifge:
797      do_if(ICmpInst::ICMP_SGE, SharkValue::jint_constant(0), pop());
798      break;
799    case Bytecodes::_if_icmpeq:
800      b = pop();
801      a = pop();
802      do_if(ICmpInst::ICMP_EQ, b, a);
803      break;
804    case Bytecodes::_if_icmpne:
805      b = pop();
806      a = pop();
807      do_if(ICmpInst::ICMP_NE, b, a);
808      break;
809    case Bytecodes::_if_icmplt:
810      b = pop();
811      a = pop();
812      do_if(ICmpInst::ICMP_SLT, b, a);
813      break;
814    case Bytecodes::_if_icmple:
815      b = pop();
816      a = pop();
817      do_if(ICmpInst::ICMP_SLE, b, a);
818      break;
819    case Bytecodes::_if_icmpgt:
820      b = pop();
821      a = pop();
822      do_if(ICmpInst::ICMP_SGT, b, a);
823      break;
824    case Bytecodes::_if_icmpge:
825      b = pop();
826      a = pop();
827      do_if(ICmpInst::ICMP_SGE, b, a);
828      break;
829
830    case Bytecodes::_tableswitch:
831    case Bytecodes::_lookupswitch:
832      do_switch();
833      break;
834
835    case Bytecodes::_invokestatic:
836    case Bytecodes::_invokespecial:
837    case Bytecodes::_invokevirtual:
838    case Bytecodes::_invokeinterface:
839      do_call();
840      break;
841
842    case Bytecodes::_instanceof:
843      // This is a very common construct:
844      //
845      //  if (object instanceof Klass) {
846      //    something = (Klass) object;
847      //    ...
848      //  }
849      //
850      // which gets compiled to something like this:
851      //
852      //  28: aload 9
853      //  30: instanceof <Class Klass>
854      //  33: ifeq 52
855      //  36: aload 9
856      //  38: checkcast <Class Klass>
857      //
858      // Handling both bytecodes at once allows us
859      // to eliminate the checkcast.
860      if (iter()->next_bci() < limit &&
861          (iter()->next_bc() == Bytecodes::_ifeq ||
862           iter()->next_bc() == Bytecodes::_ifne) &&
863          (!UseLoopSafepoints ||
864           iter()->next_get_dest() > iter()->next_bci())) {
865        if (maybe_do_instanceof_if()) {
866          iter()->next();
867          if (SharkTraceBytecodes)
868            tty->print_cr("%4d: %s", bci(), Bytecodes::name(bc()));
869          break;
870        }
871      }
872      // fall through
873    case Bytecodes::_checkcast:
874      do_instance_check();
875      break;
876
877    case Bytecodes::_new:
878      do_new();
879      break;
880    case Bytecodes::_newarray:
881      do_newarray();
882      break;
883    case Bytecodes::_anewarray:
884      do_anewarray();
885      break;
886    case Bytecodes::_multianewarray:
887      do_multianewarray();
888      break;
889
890    case Bytecodes::_monitorenter:
891      do_monitorenter();
892      break;
893    case Bytecodes::_monitorexit:
894      do_monitorexit();
895      break;
896
897    default:
898      ShouldNotReachHere();
899    }
900  }
901}
902
903SharkState* SharkBlock::initial_current_state() {
904  return entry_state()->copy();
905}
906
907int SharkBlock::switch_default_dest() {
908  return iter()->get_dest_table(0);
909}
910
911int SharkBlock::switch_table_length() {
912  switch(bc()) {
913  case Bytecodes::_tableswitch:
914    return iter()->get_int_table(2) - iter()->get_int_table(1) + 1;
915
916  case Bytecodes::_lookupswitch:
917    return iter()->get_int_table(1);
918
919  default:
920    ShouldNotReachHere();
921  }
922}
923
924int SharkBlock::switch_key(int i) {
925  switch(bc()) {
926  case Bytecodes::_tableswitch:
927    return iter()->get_int_table(1) + i;
928
929  case Bytecodes::_lookupswitch:
930    return iter()->get_int_table(2 + 2 * i);
931
932  default:
933    ShouldNotReachHere();
934  }
935}
936
937int SharkBlock::switch_dest(int i) {
938  switch(bc()) {
939  case Bytecodes::_tableswitch:
940    return iter()->get_dest_table(i + 3);
941
942  case Bytecodes::_lookupswitch:
943    return iter()->get_dest_table(2 + 2 * i + 1);
944
945  default:
946    ShouldNotReachHere();
947  }
948}
949
950void SharkBlock::do_div_or_rem(bool is_long, bool is_rem) {
951  SharkValue *sb = pop();
952  SharkValue *sa = pop();
953
954  check_divide_by_zero(sb);
955
956  Value *a, *b, *p, *q;
957  if (is_long) {
958    a = sa->jlong_value();
959    b = sb->jlong_value();
960    p = LLVMValue::jlong_constant(0x8000000000000000LL);
961    q = LLVMValue::jlong_constant(-1);
962  }
963  else {
964    a = sa->jint_value();
965    b = sb->jint_value();
966    p = LLVMValue::jint_constant(0x80000000);
967    q = LLVMValue::jint_constant(-1);
968  }
969
970  BasicBlock *ip           = builder()->GetBlockInsertionPoint();
971  BasicBlock *special_case = builder()->CreateBlock(ip, "special_case");
972  BasicBlock *general_case = builder()->CreateBlock(ip, "general_case");
973  BasicBlock *done         = builder()->CreateBlock(ip, "done");
974
975  builder()->CreateCondBr(
976    builder()->CreateAnd(
977      builder()->CreateICmpEQ(a, p),
978      builder()->CreateICmpEQ(b, q)),
979    special_case, general_case);
980
981  builder()->SetInsertPoint(special_case);
982  Value *special_result;
983  if (is_rem) {
984    if (is_long)
985      special_result = LLVMValue::jlong_constant(0);
986    else
987      special_result = LLVMValue::jint_constant(0);
988  }
989  else {
990    special_result = a;
991  }
992  builder()->CreateBr(done);
993
994  builder()->SetInsertPoint(general_case);
995  Value *general_result;
996  if (is_rem)
997    general_result = builder()->CreateSRem(a, b);
998  else
999    general_result = builder()->CreateSDiv(a, b);
1000  builder()->CreateBr(done);
1001
1002  builder()->SetInsertPoint(done);
1003  PHINode *result;
1004  if (is_long)
1005    result = builder()->CreatePHI(SharkType::jlong_type(), 0, "result");
1006  else
1007    result = builder()->CreatePHI(SharkType::jint_type(), 0, "result");
1008  result->addIncoming(special_result, special_case);
1009  result->addIncoming(general_result, general_case);
1010
1011  if (is_long)
1012    push(SharkValue::create_jlong(result, false));
1013  else
1014    push(SharkValue::create_jint(result, false));
1015}
1016
1017void SharkBlock::do_field_access(bool is_get, bool is_field) {
1018  bool will_link;
1019  ciField *field = iter()->get_field(will_link);
1020  assert(will_link, "typeflow responsibility");
1021  assert(is_field != field->is_static(), "mismatch");
1022
1023  // Pop the value off the stack where necessary
1024  SharkValue *value = NULL;
1025  if (!is_get)
1026    value = pop();
1027
1028  // Find the object we're accessing, if necessary
1029  Value *object = NULL;
1030  if (is_field) {
1031    SharkValue *value = pop();
1032    check_null(value);
1033    object = value->generic_value();
1034  }
1035  if (is_get && field->is_constant() && field->is_static()) {
1036    SharkConstant *constant = SharkConstant::for_field(iter());
1037    if (constant->is_loaded())
1038      value = constant->value(builder());
1039  }
1040  if (!is_get || value == NULL) {
1041    if (!is_field) {
1042      object = builder()->CreateInlineOop(field->holder()->java_mirror());
1043    }
1044    BasicType   basic_type = field->type()->basic_type();
1045    Type *stack_type = SharkType::to_stackType(basic_type);
1046    Type *field_type = SharkType::to_arrayType(basic_type);
1047    Type *type = field_type;
1048    if (field->is_volatile()) {
1049      if (field_type == SharkType::jfloat_type()) {
1050        type = SharkType::jint_type();
1051      } else if (field_type == SharkType::jdouble_type()) {
1052        type = SharkType::jlong_type();
1053      }
1054    }
1055    Value *addr = builder()->CreateAddressOfStructEntry(
1056      object, in_ByteSize(field->offset_in_bytes()),
1057      PointerType::getUnqual(type),
1058      "addr");
1059
1060    // Do the access
1061    if (is_get) {
1062      Value* field_value;
1063      if (field->is_volatile()) {
1064        field_value = builder()->CreateAtomicLoad(addr);
1065        field_value = builder()->CreateBitCast(field_value, field_type);
1066      } else {
1067        field_value = builder()->CreateLoad(addr);
1068      }
1069      if (field_type != stack_type) {
1070        field_value = builder()->CreateIntCast(
1071          field_value, stack_type, basic_type != T_CHAR);
1072      }
1073
1074      value = SharkValue::create_generic(field->type(), field_value, false);
1075    }
1076    else {
1077      Value *field_value = value->generic_value();
1078
1079      if (field_type != stack_type) {
1080        field_value = builder()->CreateIntCast(
1081          field_value, field_type, basic_type != T_CHAR);
1082      }
1083
1084      if (field->is_volatile()) {
1085        field_value = builder()->CreateBitCast(field_value, type);
1086        builder()->CreateAtomicStore(field_value, addr);
1087      } else {
1088        builder()->CreateStore(field_value, addr);
1089      }
1090
1091      if (!field->type()->is_primitive_type()) {
1092        builder()->CreateUpdateBarrierSet(oopDesc::bs(), addr);
1093      }
1094    }
1095  }
1096
1097  // Push the value onto the stack where necessary
1098  if (is_get)
1099    push(value);
1100}
1101
1102void SharkBlock::do_lcmp() {
1103  Value *b = pop()->jlong_value();
1104  Value *a = pop()->jlong_value();
1105
1106  BasicBlock *ip   = builder()->GetBlockInsertionPoint();
1107  BasicBlock *ne   = builder()->CreateBlock(ip, "lcmp_ne");
1108  BasicBlock *lt   = builder()->CreateBlock(ip, "lcmp_lt");
1109  BasicBlock *gt   = builder()->CreateBlock(ip, "lcmp_gt");
1110  BasicBlock *done = builder()->CreateBlock(ip, "done");
1111
1112  BasicBlock *eq = builder()->GetInsertBlock();
1113  builder()->CreateCondBr(builder()->CreateICmpEQ(a, b), done, ne);
1114
1115  builder()->SetInsertPoint(ne);
1116  builder()->CreateCondBr(builder()->CreateICmpSLT(a, b), lt, gt);
1117
1118  builder()->SetInsertPoint(lt);
1119  builder()->CreateBr(done);
1120
1121  builder()->SetInsertPoint(gt);
1122  builder()->CreateBr(done);
1123
1124  builder()->SetInsertPoint(done);
1125  PHINode *result = builder()->CreatePHI(SharkType::jint_type(), 0, "result");
1126  result->addIncoming(LLVMValue::jint_constant(-1), lt);
1127  result->addIncoming(LLVMValue::jint_constant(0),  eq);
1128  result->addIncoming(LLVMValue::jint_constant(1),  gt);
1129
1130  push(SharkValue::create_jint(result, false));
1131}
1132
1133void SharkBlock::do_fcmp(bool is_double, bool unordered_is_greater) {
1134  Value *a, *b;
1135  if (is_double) {
1136    b = pop()->jdouble_value();
1137    a = pop()->jdouble_value();
1138  }
1139  else {
1140    b = pop()->jfloat_value();
1141    a = pop()->jfloat_value();
1142  }
1143
1144  BasicBlock *ip      = builder()->GetBlockInsertionPoint();
1145  BasicBlock *ordered = builder()->CreateBlock(ip, "ordered");
1146  BasicBlock *ge      = builder()->CreateBlock(ip, "fcmp_ge");
1147  BasicBlock *lt      = builder()->CreateBlock(ip, "fcmp_lt");
1148  BasicBlock *eq      = builder()->CreateBlock(ip, "fcmp_eq");
1149  BasicBlock *gt      = builder()->CreateBlock(ip, "fcmp_gt");
1150  BasicBlock *done    = builder()->CreateBlock(ip, "done");
1151
1152  builder()->CreateCondBr(
1153    builder()->CreateFCmpUNO(a, b),
1154    unordered_is_greater ? gt : lt, ordered);
1155
1156  builder()->SetInsertPoint(ordered);
1157  builder()->CreateCondBr(builder()->CreateFCmpULT(a, b), lt, ge);
1158
1159  builder()->SetInsertPoint(ge);
1160  builder()->CreateCondBr(builder()->CreateFCmpUGT(a, b), gt, eq);
1161
1162  builder()->SetInsertPoint(lt);
1163  builder()->CreateBr(done);
1164
1165  builder()->SetInsertPoint(gt);
1166  builder()->CreateBr(done);
1167
1168  builder()->SetInsertPoint(eq);
1169  builder()->CreateBr(done);
1170
1171  builder()->SetInsertPoint(done);
1172  PHINode *result = builder()->CreatePHI(SharkType::jint_type(), 0, "result");
1173  result->addIncoming(LLVMValue::jint_constant(-1), lt);
1174  result->addIncoming(LLVMValue::jint_constant(0),  eq);
1175  result->addIncoming(LLVMValue::jint_constant(1),  gt);
1176
1177  push(SharkValue::create_jint(result, false));
1178}
1179
1180void SharkBlock::emit_IR() {
1181  ShouldNotCallThis();
1182}
1183
1184SharkState* SharkBlock::entry_state() {
1185  ShouldNotCallThis();
1186}
1187
1188void SharkBlock::do_zero_check(SharkValue* value) {
1189  ShouldNotCallThis();
1190}
1191
1192void SharkBlock::maybe_add_backedge_safepoint() {
1193  ShouldNotCallThis();
1194}
1195
1196bool SharkBlock::has_trap() {
1197  return false;
1198}
1199
1200int SharkBlock::trap_request() {
1201  ShouldNotCallThis();
1202}
1203
1204int SharkBlock::trap_bci() {
1205  ShouldNotCallThis();
1206}
1207
1208void SharkBlock::do_trap(int trap_request) {
1209  ShouldNotCallThis();
1210}
1211
1212void SharkBlock::do_arraylength() {
1213  ShouldNotCallThis();
1214}
1215
1216void SharkBlock::do_aload(BasicType basic_type) {
1217  ShouldNotCallThis();
1218}
1219
1220void SharkBlock::do_astore(BasicType basic_type) {
1221  ShouldNotCallThis();
1222}
1223
1224void SharkBlock::do_return(BasicType type) {
1225  ShouldNotCallThis();
1226}
1227
1228void SharkBlock::do_athrow() {
1229  ShouldNotCallThis();
1230}
1231
1232void SharkBlock::do_goto() {
1233  ShouldNotCallThis();
1234}
1235
1236void SharkBlock::do_jsr() {
1237  ShouldNotCallThis();
1238}
1239
1240void SharkBlock::do_ret() {
1241  ShouldNotCallThis();
1242}
1243
1244void SharkBlock::do_if(ICmpInst::Predicate p, SharkValue* b, SharkValue* a) {
1245  ShouldNotCallThis();
1246}
1247
1248void SharkBlock::do_switch() {
1249  ShouldNotCallThis();
1250}
1251
1252void SharkBlock::do_call() {
1253  ShouldNotCallThis();
1254}
1255
1256void SharkBlock::do_instance_check() {
1257  ShouldNotCallThis();
1258}
1259
1260bool SharkBlock::maybe_do_instanceof_if() {
1261  ShouldNotCallThis();
1262}
1263
1264void SharkBlock::do_new() {
1265  ShouldNotCallThis();
1266}
1267
1268void SharkBlock::do_newarray() {
1269  ShouldNotCallThis();
1270}
1271
1272void SharkBlock::do_anewarray() {
1273  ShouldNotCallThis();
1274}
1275
1276void SharkBlock::do_multianewarray() {
1277  ShouldNotCallThis();
1278}
1279
1280void SharkBlock::do_monitorenter() {
1281  ShouldNotCallThis();
1282}
1283
1284void SharkBlock::do_monitorexit() {
1285  ShouldNotCallThis();
1286}
1287