comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/NodeFactory.java @ 7503:31da1716950f

Updated truffle-sl for the changed operation code generation.
author Christian Humer <christian.humer@gmail.com>
date Fri, 18 Jan 2013 13:29:14 +0100
parents 213c1297a814
children 5e3d1a68664e
comparison
equal deleted inserted replaced
7502:6343a09b2ec1 7503:31da1716950f
26 import java.math.*; 26 import java.math.*;
27 import java.util.*; 27 import java.util.*;
28 28
29 import com.oracle.truffle.api.frame.*; 29 import com.oracle.truffle.api.frame.*;
30 import com.oracle.truffle.sl.nodes.*; 30 import com.oracle.truffle.sl.nodes.*;
31 import com.oracle.truffle.sl.ops.*; 31 import com.oracle.truffle.sl.nodes.ArithmeticNodeFactory.*;
32 import com.oracle.truffle.sl.types.*;
33 32
34 public class NodeFactory { 33 public class NodeFactory {
35 34
36 private final HashMap<String, FunctionDefinitionNode> functions; 35 private final HashMap<String, FunctionDefinitionNode> functions;
37 private final PrintStream printOutput; 36 private final PrintStream printOutput;
47 public FunctionDefinitionNode findFunction(String name) { 46 public FunctionDefinitionNode findFunction(String name) {
48 return functions.get(name); 47 return functions.get(name);
49 } 48 }
50 49
51 public void startFunction() { 50 public void startFunction() {
52 frameDescriptor = new FrameDescriptor(TypesGen.TYPES); 51 frameDescriptor = new FrameDescriptor(SLTypesGen.SLTYPES);
53 } 52 }
54 53
55 public void createFunction(StatementNode body, String name) { 54 public void createFunction(StatementNode body, String name) {
56 functions.put(name, new FunctionDefinitionNode(body, frameDescriptor, name, returnValue)); 55 functions.put(name, new FunctionDefinitionNode(body, frameDescriptor, name, returnValue));
57 } 56 }
58 57
59 public TypedNode createLocal(String name) { 58 public TypedNode createLocal(String name) {
60 return ReadLocalOpFactory.create(frameDescriptor.findOrAddFrameSlot(name)); 59 return ReadLocalNodeFactory.create(frameDescriptor.findOrAddFrameSlot(name));
61 } 60 }
62 61
63 public TypedNode createStringLiteral(String value) { 62 public TypedNode createStringLiteral(String value) {
64 return StringLiteralFactory.create(value); 63 return StringLiteralNodeFactory.create(value);
65 } 64 }
66 65
67 public StatementNode createAssignment(String name, TypedNode right) { 66 public StatementNode createAssignment(String name, TypedNode right) {
68 return WriteLocalOpFactory.create(right, frameDescriptor.findOrAddFrameSlot(name)); 67 return WriteLocalNodeFactory.create(frameDescriptor.findOrAddFrameSlot(name), right);
69 } 68 }
70 69
71 public StatementNode createPrint(List<TypedNode> expressions) { 70 public StatementNode createPrint(List<TypedNode> expressions) {
72 return new PrintNode(expressions, printOutput); 71 if (expressions.size() >= 1) {
72 StatementNode[] nodes = new StatementNode[expressions.size() + 1];
73 for (int i = 0; i < expressions.size(); i++) {
74 nodes[i] = PrintNodeFactory.create(expressions.get(i), printOutput);
75 }
76 nodes[expressions.size()] = new PrintLineNode(printOutput);
77 return new BlockNode(nodes);
78 } else {
79 return new BlockNode(new StatementNode[]{new PrintLineNode(printOutput)});
80 }
73 } 81 }
74 82
75 public StatementNode createWhile(ConditionNode condition, StatementNode body) { 83 public StatementNode createWhile(ConditionNode condition, StatementNode body) {
76 return new WhileNode(condition, body); 84 return new WhileNode(condition, body);
77 } 85 }
81 } 89 }
82 90
83 public TypedNode createBinary(String operation, TypedNode left, TypedNode right) { 91 public TypedNode createBinary(String operation, TypedNode left, TypedNode right) {
84 switch (operation) { 92 switch (operation) {
85 case "+": 93 case "+":
86 return AddOpFactory.create(left, right); 94 return AddNodeFactory.create(left, right);
87 case "*": 95 case "*":
88 return MulOpFactory.create(left, right); 96 return MulNodeFactory.create(left, right);
97 case "/":
98 return DivNodeFactory.create(left, right);
99 case "-":
100 return SubNodeFactory.create(left, right);
89 case "<": 101 case "<":
90 return LessThanOpFactory.create(left, right); 102 return LessThanNodeFactory.create(left, right);
91 case "&&": 103 case "&&":
92 return LogicalAndOpFactory.create(left, right); 104 return LogicalAndNodeFactory.create(left, right);
93 default: 105 default:
94 throw new RuntimeException("unexpected operation: " + operation); 106 throw new RuntimeException("unexpected operation: " + operation);
95 } 107 }
96 } 108 }
97 109
98 public TypedNode createNumericLiteral(String value) { 110 public TypedNode createNumericLiteral(String value) {
99 try { 111 try {
100 return IntegerLiteralFactory.create(Integer.parseInt(value)); 112 return IntegerLiteralNodeFactory.create(Integer.parseInt(value));
101 } catch (NumberFormatException ex) { 113 } catch (NumberFormatException ex) {
102 return BigIntegerLiteralFactory.create(new BigInteger(value)); 114 return BigIntegerLiteralNodeFactory.create(new BigInteger(value));
103 } 115 }
104 } 116 }
105 117
106 public TypedNode createTime() { 118 public TypedNode createTime() {
107 return TimeOpFactory.create(); 119 return TimeNodeFactory.create();
108 } 120 }
109 121
110 public StatementNode createReturn(TypedNode value) { 122 public StatementNode createReturn(TypedNode value) {
111 FrameSlot slot = frameDescriptor.findOrAddFrameSlot("<retval>"); 123 FrameSlot slot = frameDescriptor.findOrAddFrameSlot("<retval>");
112 if (returnValue == null) { 124 if (returnValue == null) {
113 returnValue = ReadLocalOpFactory.create(slot); 125 returnValue = ReadLocalNodeFactory.create(slot);
114 } 126 }
115 StatementNode write = WriteLocalOpFactory.create(value, slot); 127 StatementNode write = WriteLocalNodeFactory.create(slot, value);
116 return new ReturnNode(write); 128 return new ReturnNode(write);
117 } 129 }
118 } 130 }