sharkConstant.cpp 4.0 KB
Newer Older
1
/*
2
 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Copyright 2009 Red Hat, Inc.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

26 27 28 29 30 31
#include "precompiled.hpp"
#include "ci/ciInstance.hpp"
#include "ci/ciStreams.hpp"
#include "shark/sharkBuilder.hpp"
#include "shark/sharkConstant.hpp"
#include "shark/sharkValue.hpp"
32 33 34 35 36 37 38 39

using namespace llvm;

SharkConstant* SharkConstant::for_ldc(ciBytecodeStream *iter) {
  ciConstant constant = iter->get_constant();
  ciType *type = NULL;
  if (constant.basic_type() == T_OBJECT) {
    ciEnv *env = ciEnv::current();
T
twisti 已提交
40 41 42 43 44 45

    assert(constant.as_object()->klass() == env->String_klass()
           || constant.as_object()->klass() == env->Class_klass()
           || constant.as_object()->klass()->is_subtype_of(env->MethodType_klass())
           || constant.as_object()->klass()->is_subtype_of(env->MethodHandle_klass()), "should be");

46
    type = constant.as_object()->klass();
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  }
  return new SharkConstant(constant, type);
}

SharkConstant* SharkConstant::for_field(ciBytecodeStream *iter) {
  bool will_link;
  ciField *field = iter->get_field(will_link);
  assert(will_link, "typeflow responsibility");

  return new SharkConstant(field->constant_value(), field->type());
}

SharkConstant::SharkConstant(ciConstant constant, ciType *type) {
  SharkValue *value = NULL;

  switch (constant.basic_type()) {
  case T_BOOLEAN:
  case T_BYTE:
  case T_CHAR:
  case T_SHORT:
  case T_INT:
    value = SharkValue::jint_constant(constant.as_int());
    break;

  case T_LONG:
    value = SharkValue::jlong_constant(constant.as_long());
    break;

  case T_FLOAT:
    value = SharkValue::jfloat_constant(constant.as_float());
    break;

  case T_DOUBLE:
    value = SharkValue::jdouble_constant(constant.as_double());
    break;

  case T_OBJECT:
  case T_ARRAY:
    break;

  case T_ILLEGAL:
    // out of memory
    _is_loaded = false;
    return;

  default:
    tty->print_cr("Unhandled type %s", type2name(constant.basic_type()));
    ShouldNotReachHere();
  }

  // Handle primitive types.  We create SharkValues for these
  // now; doing so doesn't emit any code, and it allows us to
  // delegate a bunch of stuff to the SharkValue code.
  if (value) {
    _value       = value;
    _is_loaded   = true;
    _is_nonzero  = value->zero_checked();
    _is_two_word = value->is_two_word();
    return;
  }

  // Handle reference types.  This is tricky because some
  // ciObjects are psuedo-objects that refer to oops which
  // have yet to be created.  We need to spot the unloaded
  // objects (which differ between ldc* and get*, thanks!)
  ciObject *object = constant.as_object();
  assert(type != NULL, "shouldn't be");
114 115 116 117

  if ((! object->is_null_object()) && object->klass() == ciEnv::current()->Class_klass()) {
    ciKlass *klass = object->klass();
    if (! klass->is_loaded()) {
118 119 120 121
      _is_loaded = false;
      return;
    }
  }
122 123

  if (object->is_null_object() || ! object->can_be_constant() || ! object->is_loaded()) {
124 125 126 127 128 129 130 131 132 133 134
    _is_loaded = false;
    return;
  }

  _value       = NULL;
  _object      = object;
  _type        = type;
  _is_loaded   = true;
  _is_nonzero  = true;
  _is_two_word = false;
}