e412a11689f708e3dd27679a82d468173743a9d8
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / JavaModule.java
1 package org.simantics.scl.compiler.elaboration.java;
2
3
4 import org.simantics.scl.compiler.constants.DoubleConstant;
5 import org.simantics.scl.compiler.constants.FloatConstant;
6 import org.simantics.scl.compiler.constants.FunctionValue;
7 import org.simantics.scl.compiler.constants.IntegerConstant;
8 import org.simantics.scl.compiler.constants.JavaComparisonOperation;
9 import org.simantics.scl.compiler.constants.JavaComparisonToZeroOperation;
10 import org.simantics.scl.compiler.constants.JavaConversionOperation;
11 import org.simantics.scl.compiler.constants.JavaMathOperation;
12 import org.simantics.scl.compiler.constants.JavaMethod;
13 import org.simantics.scl.compiler.constants.LongConstant;
14 import org.simantics.scl.compiler.constants.ShortConstant;
15 import org.simantics.scl.compiler.constants.singletons.UnsafeCoerce;
16 import org.simantics.scl.compiler.elaboration.expressions.EApplyType;
17 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
18 import org.simantics.scl.compiler.elaboration.expressions.Expression;
19 import org.simantics.scl.compiler.module.ConcreteModule;
20 import org.simantics.scl.compiler.types.TVar;
21 import org.simantics.scl.compiler.types.Types;
22 import org.simantics.scl.compiler.types.kinds.Kinds;
23
24 public class JavaModule extends ConcreteModule {
25
26     public static final JavaModule INSTANCE = new JavaModule();
27     
28     public static final String MODULE_NAME = "JavaBuiltin";
29
30     private JavaModule() {
31         super(MODULE_NAME);
32         addValue("dconst_0", new DoubleConstant(0.0));
33         addValue("dconst_1", new DoubleConstant(1.0));
34         addValue("fconst_0", new FloatConstant(0.0f));
35         addValue("fconst_1", new FloatConstant(1.0f));
36         addValue("iconst_0", IntegerConstant.ZERO);
37         addValue("iconst_1", IntegerConstant.ONE);
38         addValue("sconst_0", ShortConstant.ZERO);
39         addValue("sconst_1", ShortConstant.ONE);
40         addValue("lconst_0", new LongConstant(0L));
41         addValue("lconst_1", new LongConstant(1L));
42         
43         // Mathematical operations
44         for(JavaMathOperation operation : JavaMathOperation.OPCODES)
45             addValue(operation.getMnemonic(), operation);
46         addValue("cadd", JavaMathOperation.CADD);
47         addValue("csub", JavaMathOperation.CSUB);
48         
49         addValue("sadd", JavaMathOperation.SADD);
50         addValue("ssub", JavaMathOperation.SSUB);
51         addValue("smul", JavaMathOperation.SMUL);
52         addValue("sdiv", JavaMathOperation.SDIV);
53         addValue("srem", JavaMathOperation.SREM);
54         addValue("sneg", JavaMathOperation.SNEG);
55         addValue("sand", JavaMathOperation.SAND);
56         addValue("sor", JavaMathOperation.SOR);
57         addValue("sxor", JavaMathOperation.SXOR);
58         
59         addValue("badd", JavaMathOperation.BADD);
60         addValue("bsub", JavaMathOperation.BSUB);
61         addValue("bmul", JavaMathOperation.BMUL);
62         addValue("bdiv", JavaMathOperation.BDIV);
63         addValue("brem", JavaMathOperation.BREM);
64         addValue("bneg", JavaMathOperation.BNEG);
65         addValue("band", JavaMathOperation.BAND);
66         addValue("bor", JavaMathOperation.BOR);
67         addValue("bxor", JavaMathOperation.BXOR);
68         
69         // Conversions
70         for(JavaConversionOperation operation : JavaConversionOperation.OPCODES)
71             addValue(operation.getMnemonic(), operation);
72         
73         // Comparison operations
74         addValue("icmpeq", new JavaComparisonOperation("==", Types.INTEGER));
75         addValue("icmpne", new JavaComparisonOperation("!=", Types.INTEGER));
76         addValue("icmplt", new JavaComparisonOperation("<", Types.INTEGER));
77         addValue("icmple", new JavaComparisonOperation("<=", Types.INTEGER));
78         addValue("icmpgt", new JavaComparisonOperation(">", Types.INTEGER));
79         addValue("icmpge", new JavaComparisonOperation(">=", Types.INTEGER));
80         
81         addValue("lcmpeq", new JavaComparisonOperation("==", Types.LONG));
82         addValue("lcmpne", new JavaComparisonOperation("!=", Types.LONG));
83         addValue("lcmplt", new JavaComparisonOperation("<", Types.LONG));
84         addValue("lcmple", new JavaComparisonOperation("<=", Types.LONG));
85         addValue("lcmpgt", new JavaComparisonOperation(">", Types.LONG));
86         addValue("lcmpge", new JavaComparisonOperation(">=", Types.LONG));     
87         
88         addValue("bcmpeq", new JavaComparisonOperation("==", Types.BYTE));
89         addValue("bcmpne", new JavaComparisonOperation("!=", Types.BYTE));
90         addValue("bcmplt", new JavaComparisonOperation("<", Types.BYTE));
91         addValue("bcmple", new JavaComparisonOperation("<=", Types.BYTE));
92         addValue("bcmpgt", new JavaComparisonOperation(">", Types.BYTE));
93         addValue("bcmpge", new JavaComparisonOperation(">=", Types.BYTE));
94         
95         addValue("scmpeq", new JavaComparisonOperation("==", Types.SHORT));
96         addValue("scmpne", new JavaComparisonOperation("!=", Types.SHORT));
97         addValue("scmplt", new JavaComparisonOperation("<", Types.SHORT));
98         addValue("scmple", new JavaComparisonOperation("<=", Types.SHORT));
99         addValue("scmpgt", new JavaComparisonOperation(">", Types.SHORT));
100         addValue("scmpge", new JavaComparisonOperation(">=", Types.SHORT));
101         
102         addValue("fcmpeq", new JavaComparisonOperation("==", Types.FLOAT));
103         addValue("fcmpne", new JavaComparisonOperation("!=", Types.FLOAT));
104         addValue("fcmplt", new JavaComparisonOperation("<", Types.FLOAT));
105         addValue("fcmple", new JavaComparisonOperation("<=", Types.FLOAT));
106         addValue("fcmpgt", new JavaComparisonOperation(">", Types.FLOAT));
107         addValue("fcmpge", new JavaComparisonOperation(">=", Types.FLOAT));    
108         
109         addValue("dcmpeq", new JavaComparisonOperation("==", Types.DOUBLE));
110         addValue("dcmpne", new JavaComparisonOperation("!=", Types.DOUBLE));
111         addValue("dcmplt", new JavaComparisonOperation("<", Types.DOUBLE));
112         addValue("dcmple", new JavaComparisonOperation("<=", Types.DOUBLE));
113         addValue("dcmpgt", new JavaComparisonOperation(">", Types.DOUBLE));
114         addValue("dcmpge", new JavaComparisonOperation(">=", Types.DOUBLE));        
115
116         addValue("ccmpeq", new JavaComparisonOperation("==", Types.CHARACTER));
117         addValue("ccmpne", new JavaComparisonOperation("!=", Types.CHARACTER));
118         addValue("ccmplt", new JavaComparisonOperation("<", Types.CHARACTER));
119         addValue("ccmple", new JavaComparisonOperation("<=", Types.CHARACTER));
120         addValue("ccmpgt", new JavaComparisonOperation(">", Types.CHARACTER));
121         addValue("ccmpge", new JavaComparisonOperation(">=", Types.CHARACTER));
122         
123         // Comparison to zero operations
124         addValue("ifeq", new JavaComparisonToZeroOperation("=="));
125         addValue("ifne", new JavaComparisonToZeroOperation("!="));
126         addValue("iflt", new JavaComparisonToZeroOperation("<"));
127         addValue("ifle", new JavaComparisonToZeroOperation("<="));
128         addValue("ifgt", new JavaComparisonToZeroOperation(">"));
129         addValue("ifge", new JavaComparisonToZeroOperation(">="));
130         
131         TVar A = Types.var(Kinds.STAR);
132         
133         addValue("unsafeCoerce", UnsafeCoerce.INSTANCE);
134         addValue("equals", new JavaMethod(true, 
135                 "java/lang/Object", "equals", Types.NO_EFFECTS, Types.BOOLEAN, A, A));
136         addValue("hashCode", new JavaMethod(true, 
137                 "java/lang/Object", "hashCode", Types.NO_EFFECTS, Types.INTEGER, A));
138         addValue("toString", new JavaMethod(true, 
139                 "java/lang/Object", "toString", Types.NO_EFFECTS, Types.STRING, A));
140         
141         setParentClassLoader(getClass().getClassLoader());
142     }
143     
144     static Expression createLiteral(FunctionValue value) {
145         Expression result = new ELiteral(value);
146         for(TVar var : value.getTypeParameters())
147             result = new EApplyType(result, var);
148         return result;
149     }
150     
151 }
152