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