1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2009 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 "ci/ciInstance.hpp"
  28 #include "ci/ciStreams.hpp"
  29 #include "shark/sharkBuilder.hpp"
  30 #include "shark/sharkConstant.hpp"
  31 #include "shark/sharkValue.hpp"
  32 
  33 using namespace llvm;
  34 
  35 SharkConstant* SharkConstant::for_ldc(ciBytecodeStream *iter) {
  36   ciConstant constant = iter->get_constant();
  37   ciType *type = NULL;
  38   if (constant.basic_type() == T_OBJECT) {
  39     ciEnv *env = ciEnv::current();
  40     if (constant.as_object()->is_klass())
  41       type = env->Class_klass();
  42     else
  43       type = env->String_klass();
  44   }
  45   return new SharkConstant(constant, type);
  46 }
  47 
  48 SharkConstant* SharkConstant::for_field(ciBytecodeStream *iter) {
  49   bool will_link;
  50   ciField *field = iter->get_field(will_link);
  51   assert(will_link, "typeflow responsibility");
  52 
  53   return new SharkConstant(field->constant_value(), field->type());
  54 }
  55 
  56 SharkConstant::SharkConstant(ciConstant constant, ciType *type) {
  57   SharkValue *value = NULL;
  58 
  59   switch (constant.basic_type()) {
  60   case T_BOOLEAN:
  61   case T_BYTE:
  62   case T_CHAR:
  63   case T_SHORT:
  64   case T_INT:
  65     value = SharkValue::jint_constant(constant.as_int());
  66     break;
  67 
  68   case T_LONG:
  69     value = SharkValue::jlong_constant(constant.as_long());
  70     break;
  71 
  72   case T_FLOAT:
  73     value = SharkValue::jfloat_constant(constant.as_float());
  74     break;
  75 
  76   case T_DOUBLE:
  77     value = SharkValue::jdouble_constant(constant.as_double());
  78     break;
  79 
  80   case T_OBJECT:
  81   case T_ARRAY:
  82     break;
  83 
  84   case T_ILLEGAL:
  85     // out of memory
  86     _is_loaded = false;
  87     return;
  88 
  89   default:
  90     tty->print_cr("Unhandled type %s", type2name(constant.basic_type()));
  91     ShouldNotReachHere();
  92   }
  93 
  94   // Handle primitive types.  We create SharkValues for these
  95   // now; doing so doesn't emit any code, and it allows us to
  96   // delegate a bunch of stuff to the SharkValue code.
  97   if (value) {
  98     _value       = value;
  99     _is_loaded   = true;
 100     _is_nonzero  = value->zero_checked();
 101     _is_two_word = value->is_two_word();
 102     return;
 103   }
 104 
 105   // Handle reference types.  This is tricky because some
 106   // ciObjects are psuedo-objects that refer to oops which
 107   // have yet to be created.  We need to spot the unloaded
 108   // objects (which differ between ldc* and get*, thanks!)
 109   ciObject *object = constant.as_object();
 110   assert(type != NULL, "shouldn't be");
 111   if (object->is_klass()) {
 112     // The constant returned for a klass is the ciKlass
 113     // for the entry, but we want the java_mirror.
 114     ciKlass *klass = object->as_klass();
 115     if (!klass->is_loaded()) {
 116       _is_loaded = false;
 117       return;
 118     }
 119     object = klass->java_mirror();
 120   }
 121   if (object->is_null_object() || !object->can_be_constant()) {
 122     _is_loaded = false;
 123     return;
 124   }
 125 
 126   _value       = NULL;
 127   _object      = object;
 128   _type        = type;
 129   _is_loaded   = true;
 130   _is_nonzero  = true;
 131   _is_two_word = false;
 132 }