/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/ReflectionGetCallerClassNode.java

https://github.com/dain/graal · Java · 105 lines · 59 code · 11 blank · 35 comment · 11 complexity · 85d126cb3599aeb9901759251290f28c MD5 · raw file

  1. /*
  2. * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. package com.oracle.graal.hotspot.replacements;
  24. import static com.oracle.graal.compiler.GraalCompiler.*;
  25. import com.oracle.graal.api.meta.*;
  26. import com.oracle.graal.compiler.common.*;
  27. import com.oracle.graal.graph.*;
  28. import com.oracle.graal.graph.spi.*;
  29. import com.oracle.graal.hotspot.meta.*;
  30. import com.oracle.graal.nodes.*;
  31. import com.oracle.graal.nodes.spi.*;
  32. import com.oracle.graal.replacements.nodes.*;
  33. public class ReflectionGetCallerClassNode extends MacroStateSplitNode implements Canonicalizable, Lowerable {
  34. public ReflectionGetCallerClassNode(Invoke invoke) {
  35. super(invoke);
  36. }
  37. @Override
  38. public Node canonical(CanonicalizerTool tool) {
  39. ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess());
  40. if (callerClassNode != null) {
  41. return callerClassNode;
  42. }
  43. return this;
  44. }
  45. @Override
  46. public void lower(LoweringTool tool) {
  47. ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess());
  48. if (callerClassNode != null) {
  49. graph().replaceFixedWithFloating(this, graph().addOrUniqueWithInputs(callerClassNode));
  50. } else {
  51. InvokeNode invoke = createInvoke();
  52. graph().replaceFixedWithFixed(this, invoke);
  53. invoke.lower(tool);
  54. }
  55. }
  56. /**
  57. * If inlining is deep enough this method returns a {@link ConstantNode} of the caller class by
  58. * walking the the stack.
  59. *
  60. * @param metaAccess
  61. * @return ConstantNode of the caller class, or null
  62. */
  63. private ConstantNode getCallerClassNode(MetaAccessProvider metaAccess) {
  64. if (!shouldIntrinsify(getTargetMethod())) {
  65. return null;
  66. }
  67. // Walk back up the frame states to find the caller at the required depth.
  68. FrameState state = stateAfter();
  69. // Cf. JVM_GetCallerClass
  70. // NOTE: Start the loop at depth 1 because the current frame state does
  71. // not include the Reflection.getCallerClass() frame.
  72. for (int n = 1; state != null; state = state.outerFrameState(), n++) {
  73. HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) state.method();
  74. switch (n) {
  75. case 0:
  76. throw GraalInternalError.shouldNotReachHere("current frame state does not include the Reflection.getCallerClass frame");
  77. case 1:
  78. // Frame 0 and 1 must be caller sensitive (see JVM_GetCallerClass).
  79. if (!method.isCallerSensitive()) {
  80. return null; // bail-out; let JVM_GetCallerClass do the work
  81. }
  82. break;
  83. default:
  84. if (!method.ignoredBySecurityStackWalk()) {
  85. // We have reached the desired frame; return the holder class.
  86. HotSpotResolvedObjectType callerClass = method.getDeclaringClass();
  87. return ConstantNode.forConstant(HotSpotObjectConstant.forObject(callerClass.mirror()), metaAccess);
  88. }
  89. break;
  90. }
  91. }
  92. return null; // bail-out; let JVM_GetCallerClass do the work
  93. }
  94. }