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