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