Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.interop/src/com/oracle/truffle/interop/ForeignAccessArguments.java @ 21371:2c2c85ce7f4d
Move VM-independent code out of HotSpot project
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Thu, 14 May 2015 15:56:31 -0700 |
parents | ab451b6de9f8 |
children |
rev | line source |
---|---|
21013
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
1 /* |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
4 * |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. Oracle designates this |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
8 * particular file as subject to the "Classpath" exception as provided |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
9 * by Oracle in the LICENSE file that accompanied this code. |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
10 * |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
15 * accompanied this code). |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
16 * |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU General Public License version |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
20 * |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
22 * or visit www.oracle.com if you need additional information or have any |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
23 * questions. |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
24 */ |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
25 package com.oracle.truffle.interop; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
26 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
27 import com.oracle.truffle.api.*; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
28 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
29 public final class ForeignAccessArguments { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
30 public static final Object[] EMPTY_ARGUMENTS_ARRAY = new Object[0]; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
31 public static final int RECEIVER_INDEX = 0; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
32 public static final int RUNTIME_ARGUMENT_COUNT = 1; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
33 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
34 public static Object[] create(Object receiver) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
35 return new Object[]{receiver}; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
36 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
37 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
38 public static Object[] create(Object receiver, Object[] arguments) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
39 Object[] objectArguments = new Object[RUNTIME_ARGUMENT_COUNT + arguments.length]; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
40 objectArguments[RECEIVER_INDEX] = receiver; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
41 arraycopy(arguments, 0, objectArguments, RUNTIME_ARGUMENT_COUNT, arguments.length); |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
42 return objectArguments; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
43 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
44 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
45 public static Object getArgument(Object[] arguments, int index) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
46 return arguments[RUNTIME_ARGUMENT_COUNT + index]; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
47 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
48 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
49 public static Object getReceiver(Object[] arguments) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
50 return arguments[RECEIVER_INDEX]; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
51 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
52 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
53 public static Object[] extractUserArguments(Object[] arguments) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
54 return copyOfRange(arguments, RUNTIME_ARGUMENT_COUNT, arguments.length); |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
55 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
56 |
21167
ab451b6de9f8
Interop: introduce separate global language scope
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
21013
diff
changeset
|
57 public static Object[] extractUserArguments(int skip, Object[] arguments) { |
ab451b6de9f8
Interop: introduce separate global language scope
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
21013
diff
changeset
|
58 return copyOfRange(arguments, RUNTIME_ARGUMENT_COUNT + skip, arguments.length); |
ab451b6de9f8
Interop: introduce separate global language scope
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
21013
diff
changeset
|
59 } |
ab451b6de9f8
Interop: introduce separate global language scope
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
21013
diff
changeset
|
60 |
21013
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
61 public static int getUserArgumentCount(Object[] arguments) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
62 return arguments.length - RUNTIME_ARGUMENT_COUNT; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
63 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
64 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
65 private static Object[] copyOfRange(Object[] original, int from, int to) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
66 int newLength = to - from; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
67 if (newLength < 0) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
68 CompilerDirectives.transferToInterpreter(); |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
69 throw new IllegalArgumentException(from + " > " + to); |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
70 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
71 Object[] copy = new Object[newLength]; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
72 arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
73 return copy; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
74 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
75 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
76 private static void arraycopy(Object[] src, int srcPos, Object[] dest, int destPos, int length) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
77 for (int i = 0; i < length; i++) { |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
78 dest[destPos + i] = src[srcPos + i]; |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
79 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
80 } |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
81 |
556b6a4b36b2
Interop: add truffle.interop
Matthias Grimmer <grimmer@ssw.jku.at>
parents:
diff
changeset
|
82 } |