1 /*
   2  * Copyright (c) 2019, 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 
  24 
  25 package org.graalvm.compiler.serviceprovider;
  26 
  27 import java.util.ArrayList;
  28 
  29 import jdk.vm.ci.code.BytecodePosition;
  30 
  31 class SpeculationEncodingAdapter implements SpeculationReasonGroup.SpeculationContextObject.Visitor {
  32     private ArrayList<Object> objects;
  33 
  34     SpeculationEncodingAdapter() {
  35         this.objects = null;
  36     }
  37 
  38     Object[] flatten(Object[] context) {
  39         boolean flatten = false;
  40         for (Object c : context) {
  41             if (c instanceof SpeculationReasonGroup.SpeculationContextObject || (c != null && c.getClass() == BytecodePosition.class)) {
  42                 // Needs extra work to flatten the representation
  43                 flatten = true;
  44                 break;
  45             }
  46         }
  47         if (!flatten) {
  48             return context;
  49         }
  50         objects = new ArrayList<>();
  51         for (Object c : context) {
  52             if (c instanceof SpeculationReasonGroup.SpeculationContextObject) {
  53                 SpeculationReasonGroup.SpeculationContextObject sco = (SpeculationReasonGroup.SpeculationContextObject) c;
  54                 // These are compiler objects which all have the same class
  55                 // loader so the class name uniquely identifies the class.
  56                 objects.add(c.getClass().getName());
  57                 sco.accept(this);
  58             } else if (c != null && c.getClass() == BytecodePosition.class) {
  59                 BytecodePosition p = (BytecodePosition) c;
  60                 objects.add(c.getClass().getName());
  61                 while (p != null) {
  62                     visitInt(p.getBCI());
  63                     objects.add(p.getMethod());
  64                     p = p.getCaller();
  65                 }
  66             } else {
  67                 objects.add(c);
  68             }
  69         }
  70         return objects.toArray();
  71     }
  72 
  73     @Override
  74     public void visitBoolean(boolean v) {
  75         objects.add((byte) (v ? 1 : 0));
  76     }
  77 
  78     @Override
  79     public void visitByte(byte v) {
  80         objects.add(v);
  81     }
  82 
  83     @Override
  84     public void visitChar(char v) {
  85         objects.add(v);
  86     }
  87 
  88     @Override
  89     public void visitShort(short v) {
  90         objects.add(v);
  91     }
  92 
  93     @Override
  94     public void visitInt(int v) {
  95         objects.add(v);
  96     }
  97 
  98     @Override
  99     public void visitLong(long v) {
 100         objects.add(v);
 101     }
 102 
 103     @Override
 104     public void visitFloat(float v) {
 105         objects.add(Float.floatToRawIntBits(v));
 106     }
 107 
 108     @Override
 109     public void visitDouble(double v) {
 110         objects.add(Double.doubleToRawLongBits(v));
 111     }
 112 
 113     @Override
 114     public void visitObject(Object v) {
 115         if (v == null) {
 116             objects.add(0);
 117         } else {
 118             objects.add(v);
 119         }
 120     }
 121 }