sharkConstant.cpp revision 5776:de6a9e811145
126135Swpaul/*
226135Swpaul * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
326135Swpaul * Copyright 2009 Red Hat, Inc.
426135Swpaul * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
526135Swpaul *
626135Swpaul * This code is free software; you can redistribute it and/or modify it
726135Swpaul * under the terms of the GNU General Public License version 2 only, as
826135Swpaul * published by the Free Software Foundation.
926135Swpaul *
1026135Swpaul * This code is distributed in the hope that it will be useful, but WITHOUT
1126135Swpaul * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1226135Swpaul * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1326135Swpaul * version 2 for more details (a copy is included in the LICENSE file that
1426135Swpaul * accompanied this code).
1526135Swpaul *
1626135Swpaul * You should have received a copy of the GNU General Public License version
1726135Swpaul * 2 along with this work; if not, write to the Free Software Foundation,
1826135Swpaul * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1926135Swpaul *
2026135Swpaul * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2126135Swpaul * or visit www.oracle.com if you need additional information or have any
2226135Swpaul * questions.
2326135Swpaul *
2426135Swpaul */
2526135Swpaul
2626135Swpaul#include "precompiled.hpp"
2726135Swpaul#include "ci/ciInstance.hpp"
2826135Swpaul#include "ci/ciStreams.hpp"
2926135Swpaul#include "shark/sharkBuilder.hpp"
3026135Swpaul#include "shark/sharkConstant.hpp"
3126135Swpaul#include "shark/sharkValue.hpp"
3226135Swpaul
3326135Swpaulusing namespace llvm;
3426135Swpaul
3526135SwpaulSharkConstant* SharkConstant::for_ldc(ciBytecodeStream *iter) {
3626135Swpaul  ciConstant constant = iter->get_constant();
3726135Swpaul  ciType *type = NULL;
3826135Swpaul  if (constant.basic_type() == T_OBJECT) {
3926135Swpaul    ciEnv *env = ciEnv::current();
4026135Swpaul
4126135Swpaul    assert(constant.as_object()->klass() == env->String_klass()
4226135Swpaul           || constant.as_object()->klass() == env->Class_klass()
4326135Swpaul           || constant.as_object()->klass()->is_subtype_of(env->MethodType_klass())
4426135Swpaul           || constant.as_object()->klass()->is_subtype_of(env->MethodHandle_klass()), "should be");
4526135Swpaul
4626135Swpaul    type = constant.as_object()->klass();
4726135Swpaul  }
4826135Swpaul  return new SharkConstant(constant, type);
4926135Swpaul}
5026135Swpaul
5126135SwpaulSharkConstant* SharkConstant::for_field(ciBytecodeStream *iter) {
5226135Swpaul  bool will_link;
5326135Swpaul  ciField *field = iter->get_field(will_link);
5426135Swpaul  assert(will_link, "typeflow responsibility");
5526135Swpaul
5626135Swpaul  return new SharkConstant(field->constant_value(), field->type());
5726135Swpaul}
5826135Swpaul
5926135SwpaulSharkConstant::SharkConstant(ciConstant constant, ciType *type) {
6026135Swpaul  SharkValue *value = NULL;
6126135Swpaul
62114601Sobrien  switch (constant.basic_type()) {
6326135Swpaul  case T_BOOLEAN:
6430762Scharnier  case T_BYTE:
6530762Scharnier  case T_CHAR:
6626135Swpaul  case T_SHORT:
6730762Scharnier  case T_INT:
68114601Sobrien    value = SharkValue::jint_constant(constant.as_int());
69114601Sobrien    break;
7026135Swpaul
7126135Swpaul  case T_LONG:
7226135Swpaul    value = SharkValue::jlong_constant(constant.as_long());
7326135Swpaul    break;
7426135Swpaul
7526135Swpaul  case T_FLOAT:
7626135Swpaul    value = SharkValue::jfloat_constant(constant.as_float());
7730762Scharnier    break;
7830762Scharnier
7926135Swpaul  case T_DOUBLE:
8026135Swpaul    value = SharkValue::jdouble_constant(constant.as_double());
8130762Scharnier    break;
8226135Swpaul
8374880Swpaul  case T_OBJECT:
8426135Swpaul  case T_ARRAY:
8526135Swpaul    break;
8626135Swpaul
8726135Swpaul  case T_ILLEGAL:
8874880Swpaul    // out of memory
8974880Swpaul    _is_loaded = false;
9030762Scharnier    return;
9174880Swpaul
9230762Scharnier  default:
9390868Smike    tty->print_cr("Unhandled type %s", type2name(constant.basic_type()));
9430762Scharnier    ShouldNotReachHere();
9574880Swpaul  }
9626135Swpaul
9726135Swpaul  // Handle primitive types.  We create SharkValues for these
9826135Swpaul  // now; doing so doesn't emit any code, and it allows us to
9926135Swpaul  // delegate a bunch of stuff to the SharkValue code.
10026135Swpaul  if (value) {
10126135Swpaul    _value       = value;
10226135Swpaul    _is_loaded   = true;
10326135Swpaul    _is_nonzero  = value->zero_checked();
10426135Swpaul    _is_two_word = value->is_two_word();
10526135Swpaul    return;
10626135Swpaul  }
10726135Swpaul
10826135Swpaul  // Handle reference types.  This is tricky because some
10926135Swpaul  // ciObjects are psuedo-objects that refer to oops which
11026135Swpaul  // have yet to be created.  We need to spot the unloaded
11126135Swpaul  // objects (which differ between ldc* and get*, thanks!)
11226135Swpaul  ciObject *object = constant.as_object();
11326135Swpaul  assert(type != NULL, "shouldn't be");
11426135Swpaul
11590298Sdes  if ((! object->is_null_object()) && object->klass() == ciEnv::current()->Class_klass()) {
11690298Sdes    ciKlass *klass = object->klass();
11726135Swpaul    if (! klass->is_loaded()) {
11826135Swpaul      _is_loaded = false;
11926135Swpaul      return;
12026135Swpaul    }
12126135Swpaul  }
12226135Swpaul
12326135Swpaul  if (object->is_null_object() || ! object->can_be_constant() || ! object->is_loaded()) {
12426135Swpaul    _is_loaded = false;
12526135Swpaul    return;
12626135Swpaul  }
12726135Swpaul
12826135Swpaul  _value       = NULL;
12926135Swpaul  _object      = object;
13026135Swpaul  _type        = type;
13126135Swpaul  _is_loaded   = true;
13295658Sdes  _is_nonzero  = true;
13395658Sdes  _is_two_word = false;
13495658Sdes}
13595658Sdes