Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.codegen.test/src/com/oracle/truffle/api/codegen/test/BinaryOperationTest.java @ 8246:3862508afe2f
Updated @NodeClass tests.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Wed, 06 Mar 2013 18:33:05 +0100 |
parents | |
children | aad7e9f4f71c |
comparison
equal
deleted
inserted
replaced
8245:703c09f8640c | 8246:3862508afe2f |
---|---|
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 | |
27 public class BinaryOperationTest { | |
28 | |
29 static int convertInt(Object value) { | |
30 if (value instanceof Number) { | |
31 return ((Number) value).intValue(); | |
32 } else if (value instanceof String) { | |
33 return Integer.parseInt((String) value); | |
34 } | |
35 throw new RuntimeException("Invalid datatype"); | |
36 } | |
37 | |
38 @NodeClass(BinaryNode.class) | |
39 abstract static class BinaryNode extends ValueNode { | |
40 | |
41 @Child protected ValueNode leftNode; | |
42 @Child protected ValueNode rightNode; | |
43 | |
44 public BinaryNode(ValueNode left, ValueNode right) { | |
45 this.leftNode = left; | |
46 this.rightNode = right; | |
47 } | |
48 | |
49 public BinaryNode(BinaryNode prev) { | |
50 this(prev.leftNode, prev.rightNode); | |
51 } | |
52 | |
53 @Specialization | |
54 int add(int left, int right) { | |
55 return left + right; | |
56 } | |
57 | |
58 @Generic | |
59 int add(Object left, Object right) { | |
60 return convertInt(left) + convertInt(right); | |
61 } | |
62 | |
63 @Specialization | |
64 int sub(int left, int right) { | |
65 return left + right; | |
66 } | |
67 | |
68 @Generic | |
69 int sub(Object left, Object right) { | |
70 return convertInt(left) + convertInt(right); | |
71 } | |
72 | |
73 } | |
74 | |
75 } |