Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/BuiltinFunctionTest.java @ 8238:0b48dc5f37c3
Added truffle.api.codegen.test project with a BultinFunctionTest.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Fri, 01 Mar 2013 17:05:14 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
8237:6b74ffe38183 | 8238:0b48dc5f37c3 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, 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.truffle.api.codegen.test; | |
24 | |
25 import com.oracle.truffle.api.codegen.*; | |
26 import com.oracle.truffle.api.nodes.*; | |
27 | |
28 /** | |
29 * Generated code at {@link BuiltinFunctionTestFactory}. | |
30 */ | |
31 public class BuiltinFunctionTest { | |
32 | |
33 @TypeSystemReference(SimpleTypes.class) | |
34 abstract static class ValueNode extends Node { | |
35 | |
36 abstract int executeInt() throws UnexpectedResultException; | |
37 | |
38 abstract String executeString() throws UnexpectedResultException; | |
39 | |
40 abstract Object execute(); | |
41 | |
42 } | |
43 | |
44 abstract static class FunctionNode extends ValueNode { | |
45 | |
46 @Children ValueNode[] parameters; | |
47 | |
48 FunctionNode(ValueNode[] parameters) { | |
49 this.parameters = adoptChildren(parameters); | |
50 } | |
51 | |
52 FunctionNode(FunctionNode prev) { | |
53 this(prev.parameters); | |
54 } | |
55 } | |
56 | |
57 static int convertInt(Object value) { | |
58 if (value instanceof Number) { | |
59 return ((Number) value).intValue(); | |
60 } else if (value instanceof String) { | |
61 return Integer.parseInt((String) value); | |
62 } | |
63 throw new RuntimeException("Invalid datatype"); | |
64 } | |
65 | |
66 abstract static class MathAbsNode extends FunctionNode { | |
67 | |
68 MathAbsNode(ValueNode[] children) { | |
69 super(children); | |
70 } | |
71 | |
72 MathAbsNode(MathAbsNode prev) { | |
73 super(prev); | |
74 } | |
75 | |
76 @Specialization | |
77 int doInt(int value1) { | |
78 return Math.abs(value1); | |
79 } | |
80 | |
81 @Generic | |
82 int doGeneric(Object value0) { | |
83 return doInt(convertInt(value0)); | |
84 } | |
85 } | |
86 | |
87 abstract static class StringThisNode extends ValueNode { | |
88 | |
89 @Override | |
90 final String executeString() { | |
91 return (String) execute(); | |
92 } | |
93 | |
94 } | |
95 | |
96 @ExecuteChildren({"thisNode", "parameters"}) | |
97 abstract static class InstanceFunctionNode extends FunctionNode { | |
98 | |
99 @Child StringThisNode thisNode; | |
100 | |
101 InstanceFunctionNode(StringThisNode thisNode, ValueNode[] parameters) { | |
102 super(parameters); | |
103 this.thisNode = thisNode; | |
104 } | |
105 | |
106 InstanceFunctionNode(InstanceFunctionNode prev) { | |
107 this(prev.thisNode, prev.parameters); | |
108 } | |
109 } | |
110 | |
111 abstract static class StringSubstrNode extends InstanceFunctionNode { | |
112 | |
113 StringSubstrNode(StringThisNode thisNode, ValueNode[] parameters) { | |
114 super(thisNode, parameters); | |
115 } | |
116 | |
117 StringSubstrNode(StringSubstrNode prev) { | |
118 super(prev); | |
119 } | |
120 | |
121 @Specialization | |
122 String doInt(String thisValue, int beginIndex, int endIndex) { | |
123 return thisValue.substring(beginIndex, endIndex); | |
124 } | |
125 | |
126 @Generic | |
127 String doGeneric(String thisValue, Object beginIndex, Object endIndex) { | |
128 return thisValue.substring(convertInt(beginIndex), convertInt(endIndex)); | |
129 } | |
130 } | |
131 | |
132 } |