comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLNodeFactory.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 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/NodeFactory.java@5151a7588384
children 06afa0db90b3
comparison
equal deleted inserted replaced
12712:882a0aadfed6 12752:71991b7a0f14
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.sl;
24
25 import java.math.*;
26 import java.util.*;
27
28 import com.oracle.truffle.api.*;
29 import com.oracle.truffle.api.frame.*;
30 import com.oracle.truffle.api.nodes.*;
31 import com.oracle.truffle.sl.nodes.ArithmeticNodeFactory.AddNodeFactory;
32 import com.oracle.truffle.sl.nodes.ArithmeticNodeFactory.DivNodeFactory;
33 import com.oracle.truffle.sl.nodes.ArithmeticNodeFactory.MulNodeFactory;
34 import com.oracle.truffle.sl.nodes.ArithmeticNodeFactory.SubNodeFactory;
35 import com.oracle.truffle.sl.nodes.*;
36 import com.oracle.truffle.sl.parser.*;
37 import com.oracle.truffle.sl.runtime.*;
38
39 public class SLNodeFactory {
40
41 private final SLContext context;
42
43 private Parser parser;
44 private FrameDescriptor frameDescriptor;
45 private TypedNode returnValue;
46
47 private Source source;
48 private String currentFunctionName;
49
50 public SLNodeFactory(SLContext context) {
51 this.context = context;
52 }
53
54 public void setSource(Source source) {
55 this.source = source;
56 }
57
58 public void setParser(Parser parser) {
59 this.parser = parser;
60 }
61
62 public CallTarget findFunction(String name) {
63 return context.getFunctionRegistry().lookup(name);
64 }
65
66 public void startFunction() {
67 frameDescriptor = new FrameDescriptor();
68 }
69
70 public void createFunction(StatementNode body, String name, String[] parameterNames) {
71 context.getFunctionRegistry().register(name, FunctionRootNode.createFunction(body, frameDescriptor, name, returnValue, parameterNames));
72 this.currentFunctionName = name;
73 this.returnValue = null;
74 }
75
76 private <T extends Node> T assignSource(T node) {
77 node.assignSourceSection(ParserUtils.createSourceSection(source, currentFunctionName, parser));
78 return node;
79 }
80
81 public TypedNode createLocal(String name) {
82 return assignSource(new ReadUninitializedNode(context, frameDescriptor.findOrAddFrameSlot(name, FrameSlotKind.Int)));
83 }
84
85 public TypedNode createStringLiteral(String value) {
86 return assignSource(new StringLiteralNode(value));
87 }
88
89 public TypedNode createAssignment(TypedNode read, TypedNode assignment) {
90 FrameSlot slot = ((ReadUninitializedNode) read).getSlot();
91 return assignSource(WriteLocalNodeFactory.create(slot, assignment));
92 }
93
94 public StatementNode createWhile(ConditionNode condition, StatementNode body) {
95 return assignSource(new WhileNode(condition, body));
96 }
97
98 public StatementNode createBlock(List<StatementNode> statements) {
99 return assignSource(new BlockNode(statements.toArray(new StatementNode[statements.size()])));
100 }
101
102 public TypedNode createCall(TypedNode function, TypedNode[] parameters) {
103 return assignSource(CallNode.create(function, parameters));
104 }
105
106 public TypedNode createBinary(String operation, TypedNode left, TypedNode right) {
107 TypedNode binary;
108 switch (operation) {
109 case "+":
110 binary = AddNodeFactory.create(left, right);
111 break;
112 case "*":
113 binary = MulNodeFactory.create(left, right);
114 break;
115 case "/":
116 binary = DivNodeFactory.create(left, right);
117 break;
118 case "-":
119 binary = SubNodeFactory.create(left, right);
120 break;
121 case "<":
122 binary = LessThanNodeFactory.create(left, right);
123 break;
124 case "&&":
125 binary = LogicalAndNodeFactory.create(left, right);
126 break;
127 default:
128 throw new RuntimeException("unexpected operation: " + operation);
129 }
130 return assignSource(binary);
131 }
132
133 public TypedNode createNumericLiteral(String value) {
134 try {
135 return assignSource(new IntegerLiteralNode(Integer.parseInt(value)));
136 } catch (NumberFormatException ex) {
137 return assignSource(new BigIntegerLiteralNode(new BigInteger(value)));
138 }
139 }
140
141 public StatementNode createReturn(TypedNode value) {
142 FrameSlot slot = frameDescriptor.findOrAddFrameSlot("<retval>", FrameSlotKind.Int);
143 if (returnValue == null) {
144 returnValue = ReadLocalNodeFactory.create(slot);
145 }
146 StatementNode write = WriteLocalNodeFactory.create(slot, value);
147 return assignSource(new ReturnNode(write));
148 }
149
150 public TypedNode createTernary(TypedNode condition, TypedNode thenPart, TypedNode elsePart) {
151 return assignSource(TernaryNodeFactory.create(condition, thenPart, elsePart));
152 }
153
154 public StatementNode createIf(ConditionNode condition, StatementNode then, StatementNode elseNode) {
155 return assignSource(IfNodeFactory.create(then, elseNode, condition));
156 }
157
158 }