]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java
6df643ea38e17f1e51f2689d0288b7f379d02718
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / Builtins.java
1 package org.simantics.scl.compiler.elaboration.java;
2
3 import org.cojen.classfile.TypeDesc;
4 import org.simantics.databoard.binding.mutable.Variant;
5 import org.simantics.scl.compiler.common.datatypes.Constructor;
6 import org.simantics.scl.compiler.common.names.Name;
7 import org.simantics.scl.compiler.common.names.Names;
8 import org.simantics.scl.compiler.common.precedence.Associativity;
9 import org.simantics.scl.compiler.common.precedence.Precedence;
10 import org.simantics.scl.compiler.constants.BooleanConstant;
11 import org.simantics.scl.compiler.constants.Constant;
12 import org.simantics.scl.compiler.constants.JavaStaticField;
13 import org.simantics.scl.compiler.constants.JavaStaticMethod;
14 import org.simantics.scl.compiler.constants.NoRepConstant;
15 import org.simantics.scl.compiler.constants.SCLConstant;
16 import org.simantics.scl.compiler.constants.SCLConstructor;
17 import org.simantics.scl.compiler.constants.singletons.BindingConstant;
18 import org.simantics.scl.compiler.constants.singletons.FailFunction;
19 import org.simantics.scl.compiler.constants.singletons.JustConstant;
20 import org.simantics.scl.compiler.constants.singletons.NothingConstant;
21 import org.simantics.scl.compiler.constants.singletons.TypeOfConstant;
22 import org.simantics.scl.compiler.constants.singletons.TypeOfProxyConstant;
23 import org.simantics.scl.compiler.constants.singletons.TypeProxyConstant;
24 import org.simantics.scl.compiler.elaboration.fundeps.Fundep;
25 import org.simantics.scl.compiler.elaboration.modules.Documentation;
26 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
27 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
28 import org.simantics.scl.compiler.errors.Locations;
29 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
30 import org.simantics.scl.compiler.internal.codegen.references.BoundVar;
31 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
32 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
33 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Jump;
34 import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply;
35 import org.simantics.scl.compiler.internal.codegen.types.MaybeType;
36 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
37 import org.simantics.scl.compiler.internal.codegen.types.VectorType;
38 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
39 import org.simantics.scl.compiler.module.ConcreteModule;
40 import org.simantics.scl.compiler.types.TApply;
41 import org.simantics.scl.compiler.types.TCon;
42 import org.simantics.scl.compiler.types.TFun;
43 import org.simantics.scl.compiler.types.TPred;
44 import org.simantics.scl.compiler.types.TUnion;
45 import org.simantics.scl.compiler.types.TVar;
46 import org.simantics.scl.compiler.types.Type;
47 import org.simantics.scl.compiler.types.Types;
48 import org.simantics.scl.compiler.types.kinds.Kind;
49 import org.simantics.scl.compiler.types.kinds.Kinds;
50 import org.simantics.scl.runtime.chr.CHRContext;
51 import org.simantics.scl.runtime.profiling.BranchPoint;
52
53 public class Builtins extends ConcreteModule {
54
55     public static final SCLValue[] TUPLE_CONSTRUCTORS = new SCLValue[Constants.MAX_TUPLE_LENGTH+1];
56     public static final SCLValue[] LIST_CONSTRUCTORS = new SCLValue[Constants.MAX_LIST_LITERAL_LENGTH+1];
57     
58     public static Builtins INSTANCE = new Builtins();
59     
60     public static SCLValue Nothing;
61     public static SCLValue Just;
62     
63     public static SCLValue EQUALS;
64     
65     private Builtins() {      
66         super(Types.BUILTIN);
67         
68         TVar A = Types.var(Kinds.STAR);
69         
70         StandardTypeConstructor Boolean = new StandardTypeConstructor(Types.BOOLEAN, Kinds.STAR, TypeDesc.BOOLEAN);
71         Boolean.documentation = "Data type representing truth values `True` and `False`.";
72         addTypeDescriptor("Boolean", Boolean);
73         addTypeDescriptor("Byte", new StandardTypeConstructor(Types.BYTE, Kinds.STAR, TypeDesc.BYTE,
74                 "8-bit signed integer"));
75         addTypeDescriptor("Character", new StandardTypeConstructor(Types.CHARACTER, Kinds.STAR, TypeDesc.CHAR,
76                 "16-bit Unicode character."));
77         addTypeDescriptor("Short", new StandardTypeConstructor(Types.SHORT, Kinds.STAR, TypeDesc.SHORT,
78                 "16-bit signed integer"));
79         addTypeDescriptor("Integer", new StandardTypeConstructor(Types.INTEGER, Kinds.STAR, TypeDesc.INT,
80                 "32-bit signed integer"));
81         addTypeDescriptor("Long", new StandardTypeConstructor(Types.LONG, Kinds.STAR, TypeDesc.LONG,
82                 "64-bit signed integer"));
83         addTypeDescriptor("Float", new StandardTypeConstructor(Types.FLOAT, Kinds.STAR, TypeDesc.FLOAT,
84                 "32-bit floating point number"));
85         addTypeDescriptor("Double", new StandardTypeConstructor(Types.DOUBLE, Kinds.STAR, TypeDesc.DOUBLE,
86                 "64-bit floating point number"));
87         addTypeDescriptor("String", new StandardTypeConstructor(Types.STRING, Kinds.STAR, TypeDesc.STRING,
88                 "Unicode string"));
89         
90         addTypeDescriptor("BooleanArray", new StandardTypeConstructor(Types.BOOLEAN_ARRAY, Kinds.STAR, TypeDesc.forClass(boolean[].class)));
91         addTypeDescriptor("ByteArray", new StandardTypeConstructor(Types.BYTE_ARRAY, Kinds.STAR, TypeDesc.forClass(byte[].class)));
92         addTypeDescriptor("CharacterArray", new StandardTypeConstructor(Types.CHARACTER_ARRAY, Kinds.STAR, TypeDesc.forClass(char[].class)));
93         addTypeDescriptor("ShortArray", new StandardTypeConstructor(Types.SHORT_ARRAY, Kinds.STAR, TypeDesc.forClass(short[].class)));
94         addTypeDescriptor("IntegerArray", new StandardTypeConstructor(Types.INTEGER_ARRAY, Kinds.STAR, TypeDesc.forClass(int[].class)));
95         addTypeDescriptor("LongArray", new StandardTypeConstructor(Types.LONG_ARRAY, Kinds.STAR, TypeDesc.forClass(long[].class)));
96         addTypeDescriptor("FloatArray", new StandardTypeConstructor(Types.FLOAT_ARRAY, Kinds.STAR, TypeDesc.forClass(float[].class)));
97         addTypeDescriptor("DoubleArray", new StandardTypeConstructor(Types.DOUBLE_ARRAY, Kinds.STAR, TypeDesc.forClass(double[].class)));
98         
99         addTypeDescriptor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class)));
100         
101         addTypeDescriptor("Maybe", MaybeType.INSTANCE);
102         
103         addTypeDescriptor("Variant", new StandardTypeConstructor(Types.VARIANT, Kinds.STAR, TypeDesc.forClass(Variant.class)));
104         
105         addEffectConstructor("Proc", new EffectConstructor(Types.PROC));
106         
107         //addTypeDescriptor("->", new StandardTypeConstructor(Kinds.STAR_TO_STAR_TO_STAR, Constants.FUNCTION));
108         addTypeDescriptor("[]", new StandardTypeConstructor(Types.LIST, Kinds.STAR_TO_STAR, Constants.LIST));        
109         addTypeDescriptor("@", new StandardTypeConstructor(Types.PUNIT, Kinds.STAR, Constants.TUPLE[0]));
110         addTypeDescriptor("TypeProxy", new StandardTypeConstructor(Types.TYPE_PROXY, Kinds.STAR_TO_STAR, Constants.TUPLE[0]));
111         
112         // *** Tuples ***
113         
114         Kind tupleKind = Kinds.STAR;
115         for(int arity=0;arity<=Constants.MAX_TUPLE_LENGTH;++arity) {
116             if(arity != 1) {
117                 TVar[] vars = new TVar[arity];
118                 for(int i=0;i<vars.length;++i)
119                     vars[i] = Types.var(Kinds.STAR);
120                 TCon constructor = Types.tupleConstructor(arity);
121                 StandardTypeConstructor typeConstructor = 
122                         new StandardTypeConstructor(constructor, tupleKind, Constants.TUPLE[arity]);
123                 addTypeDescriptor(constructor.name, typeConstructor);
124                 Type returnType = Types.apply(constructor, vars);
125                 typeConstructor.setType(constructor, vars);
126                 Constant cons;
127                 String javaName = "org/simantics/scl/runtime/tuple/Tuple"+arity;
128                 if(arity == 0) {
129                     cons = new NoRepConstant(returnType);
130                     typeConstructor.setConstructors(new Constructor(Locations.NO_LOCATION, typeConstructor,
131                             Name.create(Types.BUILTIN, constructor.name), 
132                             vars, javaName)
133                             );
134                 }
135                 else { 
136                     cons = new SCLConstructor(constructor.name, 
137                             javaName, vars, 0, returnType, vars);
138                     typeConstructor.setConstructors(
139                             new Constructor(Locations.NO_LOCATION, typeConstructor, 
140                                     Name.create(Types.BUILTIN, constructor.name), 
141                                     vars, javaName)
142                             );
143                 }
144                 typeConstructor.isOpen = false;
145                 SCLValue value = new SCLValue(Name.create(Types.BUILTIN, constructor.name), cons);
146                 addValue(value);
147                 TUPLE_CONSTRUCTORS[arity] = value;
148             }
149             tupleKind = Kinds.arrow(Kinds.STAR, tupleKind);
150         }
151         
152         // *** Lists ***
153         
154         for(int arity=0;arity<=Constants.MAX_LIST_LITERAL_LENGTH;++arity) {
155             LIST_CONSTRUCTORS[arity] = addValue("_list_literal_" + arity + "_",
156                     arity == 0 ? new EmptyListConstructor() : 
157                         new ListConstructor(arity)
158                     );
159         }
160         
161         // *** Boolean ***
162         
163         SCLValue True = addValue("True", new BooleanConstant(true));
164         SCLValue False = addValue("False", new BooleanConstant(false));
165         Boolean.setConstructors(
166                 new Constructor(Locations.NO_LOCATION, Boolean, False.getName(), Type.EMPTY_ARRAY, null),
167                 new Constructor(Locations.NO_LOCATION, Boolean, True.getName(), Type.EMPTY_ARRAY, null)
168                 );
169         Boolean.isOpen = false;
170         
171         // *** Maybe ***
172         
173         Nothing = addValue("Nothing", NothingConstant.INSTANCE);
174         Just = addValue("Just", JustConstant.INSTANCE);
175         MaybeType.INSTANCE.setConstructors(
176                 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Nothing.getName(), Type.EMPTY_ARRAY, null),
177                 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Just.getName(), new Type[] {MaybeType.INSTANCE.parameters[0]}, null)
178                 );
179
180         // *** Dynamic ***
181         
182         addValue("Dynamic", DynamicConstructor.INSTANCE);
183         
184         // *** Vector ***
185         
186         TypeClass VecCompC = new TypeClass(Locations.NO_LOCATION, 
187                 TPred.EMPTY_ARRAY, 
188                 Types.VEC_COMP, 
189                 "java/lang/Class",
190                 new TVar[] {A},
191                 Fundep.EMPTY_ARRAY);
192         addTypeClass("VecComp", VecCompC);
193         
194         addTypeDescriptor("Vector", new VectorType(Types.VECTOR));
195         addValue("getVector", new GetVector(Types.NO_EFFECTS, Types.VECTOR));
196         addValue("lengthVector", new LengthVector(Types.VECTOR));
197         //addValue("createVectorFromList", CreateVectorFromList.INSTANCE);
198         
199         addTypeDescriptor("MVector", new VectorType(Types.MVECTOR));
200         addValue("createMVector", CreateMVector.INSTANCE);
201         addValue("createMVectorProto", CreateMVectorProto.INSTANCE);
202         addValue("getMVector", new GetVector(Types.PROC, Types.MVECTOR));
203         addValue("lengthMVector", new LengthVector(Types.MVECTOR));
204         addValue("freezeMVector", new FreezeMVector());
205         addValue("setMVector", SetMVector.INSTANCE);       
206         
207         // *** fail ***
208         
209         addValue("fail", FailFunction.INSTANCE).documentation =
210                 "Throws a runtime exeception with the given string as a description.";
211                 
212         // *** runProc ***
213         
214         {
215             TVar a = Types.var(Kinds.STAR);
216             TVar e = Types.var(Kinds.EFFECT);
217             SSAFunction runProcFunction = new SSAFunction(new TVar[] {a,e}, e, a);
218             Type parameterType = Types.functionE(Types.PUNIT, Types.union(new Type[] {Types.PROC,e}), a);
219             SSABlock block = new SSABlock(parameterType);
220             BoundVar[] parameters = block.getParameters();
221             
222             BoundVar x = new BoundVar(a);
223             LetApply apply = new LetApply(x, Types.PROC, parameters[0].createOccurrence(), 
224                     new NoRepConstant(Types.PUNIT).createOccurrence()
225                     );
226             block.addStatement(apply);
227             
228             block.setExit(new Jump(runProcFunction.getReturnCont().createOccurrence(), 
229                     x.createOccurrence()));
230             
231             runProcFunction.addBlock(block);
232             SCLConstant runProc = new SCLConstant(Names.Builtin_runProc, runProcFunction.getType());
233             runProc.setDefinition(runProcFunction);            
234             runProc.setInlineArity(1, 0xffffffff);
235             runProc.setBase(new JavaStaticMethod("org/simantics/scl/runtime/procedure/Procedures",
236                     "runProc", a, parameterType));
237             addValue("runProc", runProc);
238         }
239         
240         // *** Typeable ***
241         
242         {
243             /* class Typeable a 
244              */
245             TypeClass TypeableC = new TypeClass(Locations.NO_LOCATION, 
246                     TPred.EMPTY_ARRAY, 
247                     Types.TYPEABLE, 
248                     Type.class.getName(),
249                     new TVar[] {A},
250                     Fundep.EMPTY_ARRAY);
251             TypeableC.documentation = "A class of types that can be reified with `typeOf` function.";
252             addTypeClass("Typeable", TypeableC);
253         
254             /* data Type = TCon String String
255              *           | TApply Type Type
256              */
257             final TCon Type = Types.con(Types.BUILTIN, "Type");
258             final TypeDesc TypeD = TypeDesc.forClass(Type.class);
259             addValue("TCon", new JavaStaticMethod(
260                     Types.class.getName().replace('.', '/'),
261                     "con",
262                     TypeDesc.forClass(TCon.class), new TypeDesc[] {TypeDesc.STRING, TypeDesc.STRING},
263                     Type, Types.STRING, Types.STRING)); 
264             addValue("TApply", new JavaStaticMethod(
265                     "org/simantics/scl/compiler/types/Types",
266                     "apply",
267                     TypeDesc.forClass(TApply.class), new TypeDesc[] {TypeD, TypeD},
268                     Type, Type, Type));
269             addValue("TFun", new JavaStaticMethod(
270                     "org/simantics/scl/compiler/types/Types",
271                     "functionE",
272                     TypeDesc.forClass(TFun.class), new TypeDesc[] {TypeD, TypeD, TypeD},
273                     Type, Type, Type, Type));
274             addValue("TPure", new JavaStaticField(
275                     "org/simantics/scl/compiler/types/Types",
276                     "NO_EFFECTS",
277                     Types.NO_EFFECTS,
278                     TypeDesc.forClass(TUnion.class),
279                     Type, -1));
280             addValue("TUnion2", new JavaStaticMethod(
281                     "org/simantics/scl/compiler/types/Types",
282                     "union",
283                     Types.NO_EFFECTS,
284                     Type, Type, Type));
285
286             StandardTypeConstructor TypeC = new StandardTypeConstructor(Type, Kinds.STAR, 
287                     TypeDesc.forClass("org/simantics/scl/compiler/types/Type"));
288             TypeC.setType(Type);
289             TypeC.isOpen = true;
290             TypeC.documentation = "Represents an SCL data type.";
291             addTypeDescriptor("Type", TypeC);
292
293             // typeOf :: Typeable a => a -> Type
294             addValue("typeOf", TypeOfConstant.INSTANCE)
295             .documentation = "Returns the type of the value given as a parameter.";
296             addValue("typeOfProxy", TypeOfProxyConstant.INSTANCE)
297             .documentation = "Returns the type of the type proxy given as a parameter.";
298             addValue("TypeProxy", TypeProxyConstant.INSTANCE);
299         }
300         
301         // *** Serializable ***
302         
303         {
304             /* class Serializable a 
305              */
306             TypeClass SerializableC = new TypeClass(Locations.NO_LOCATION, 
307                     TPred.EMPTY_ARRAY, 
308                     Types.SERIALIZABLE, 
309                     "org/simantics/databoard/binding/Binding",
310                     new TVar[] {A},
311                     Fundep.EMPTY_ARRAY);
312             SerializableC.documentation = "A class of types having a `Binding`";
313             addTypeClass("Serializable", SerializableC);
314         
315             /* data TypeRep = TCon String
316              *              | TApply TypeRep TypeRep
317              */           
318
319             StandardTypeConstructor BindingC = new StandardTypeConstructor(Types.BINDING, Kinds.STAR_TO_STAR, 
320                     TypeDesc.forClass("org/simantics/databoard/binding/Binding"));
321             BindingC.setType(Types.BINDING, A);
322             BindingC.documentation = "`Binding` represents a data type in the form supported by Databoard library. " + 
323                     "It is used to serialize and deserialize values.";
324             addTypeDescriptor("Binding", BindingC);
325             
326             // typeOf :: Typeable a => a -> TypeReps
327             addValue("binding", BindingConstant.INSTANCE)
328             .documentation = "Gives a binding for the required type.";
329             
330         }
331         
332         // Relations
333         
334         {
335             addRelation("Eq", EqRelation.INSTANCE);
336             addRelation("Optional", OptionalRelation.INSTANCE);
337             addRelation("Execute", new ExecuteRelation(0));
338             addRelation("Execute10", new ExecuteRelation(10));
339         }
340         
341         {
342             EQUALS = new SCLValue(Names.Builtin_equals, EqualsFunction.INSTANCE);
343             EQUALS.setPrecedence(new Precedence(4, Associativity.NONASSOC));
344             addValue(EQUALS);
345             addValue("hashCode", HashCodeFunction.INSTANCE);
346         }
347         
348         // Coverage
349         {
350             StandardTypeConstructor branchPoint = new StandardTypeConstructor(Types.BRANCH_POINT, Kinds.STAR,
351                     TypeDesc.forClass(BranchPoint.class)); 
352             addTypeDescriptor("BranchPoint", branchPoint);
353             
354             addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
355         }
356         
357         setParentClassLoader(getClass().getClassLoader());
358         
359         // CHR
360         
361         addTypeDescriptor("CHRContext", new StandardTypeConstructor(Types.CHRContext, Kinds.STAR, TypeDesc.forClass(CHRContext.class)));
362     }
363     
364     @Override
365     public Documentation getDocumentation() {
366         return documentation;
367     }
368
369         public static void flush() {
370                 INSTANCE = new Builtins();
371         }
372
373 }