annotate graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SimpleLanguage.java @ 12752:71991b7a0f14

SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Nov 2013 21:34:44 +0100
parents 7fee8bd5d2bd
children 69d2e4baa215
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
1 /*
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
4 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
8 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
13 * accompanied this code).
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
14 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
18 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
21 * questions.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
22 */
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
23 package com.oracle.truffle.sl;
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
24
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
25 import java.io.*;
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
26
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
27 import javax.script.*;
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
28
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
29 import com.oracle.truffle.api.*;
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
30 import com.oracle.truffle.api.impl.*;
7503
31da1716950f Updated truffle-sl for the changed operation code generation.
Christian Humer <christian.humer@gmail.com>
parents: 7292
diff changeset
31 import com.oracle.truffle.api.nodes.*;
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
32 import com.oracle.truffle.sl.runtime.*;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
33
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
34 public class SimpleLanguage {
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
35
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
36 private static final Object[] NO_ARGUMENTS = new Object[0];
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
37
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
38 public static void main(String[] args) throws IOException {
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
39 run(new FileInputStream(args[0]), System.out, 10, true);
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
40 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
41
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
42 public static void run(InputStream input, PrintStream printOutput, int repeats, boolean log) {
8463
b2161263e9f2 Cleanup unit tests : they should not print to stdout/stderr
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7503
diff changeset
43 if (log) {
8959
7fee8bd5d2bd Remove some System.(out|err).print...
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 8463
diff changeset
44 // CheckStyle: stop system..print check
8463
b2161263e9f2 Cleanup unit tests : they should not print to stdout/stderr
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7503
diff changeset
45 System.out.printf("== running on %s\n", Truffle.getRuntime().getName());
8959
7fee8bd5d2bd Remove some System.(out|err).print...
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 8463
diff changeset
46 // CheckStyle: resume system..print check
8463
b2161263e9f2 Cleanup unit tests : they should not print to stdout/stderr
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 7503
diff changeset
47 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
48
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
49 SLContext context = new SLContext(printOutput);
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
50 SLScript script;
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
51 try {
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
52 script = SLScript.create(context, input);
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
53 } catch (ScriptException e) {
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
54 // TODO temporary hack
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
55 throw new RuntimeException(e);
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
56 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
57
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
58 if (log) {
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
59 printScript(script);
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
60 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
61 try {
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
62 for (int i = 0; i < repeats; i++) {
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
63 long start = System.nanoTime();
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
64 Object result = script.run(NO_ARGUMENTS);
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
65 long end = System.nanoTime();
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
66
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
67 if (result != null) {
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
68 printOutput.println(result);
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
69 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
70 if (log) {
8959
7fee8bd5d2bd Remove some System.(out|err).print...
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 8463
diff changeset
71 // CheckStyle: stop system..print check
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
72 System.out.printf("== iteration %d: %.3f ms\n", (i + 1), (end - start) / 1000000.0);
8959
7fee8bd5d2bd Remove some System.(out|err).print...
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 8463
diff changeset
73 // CheckStyle: resume system..print check
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
74 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
75 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
76
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
77 } finally {
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
78 if (log) {
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
79 printScript(script);
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
80 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
81 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
82 }
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
83
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
84 private static void printScript(SLScript script) {
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
85 NodeUtil.printTree(System.out, ((DefaultCallTarget) script.getMain()).getRootNode());
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 8959
diff changeset
86 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
87 }