1 package org.simantics.scl.compiler.elaboration.java;
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;
53 public class Builtins extends ConcreteModule {
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];
58 public static Builtins INSTANCE = new Builtins();
60 public static SCLValue Nothing;
61 public static SCLValue Just;
63 public static SCLValue EQUALS;
68 TVar A = Types.var(Kinds.STAR);
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,
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)));
99 addTypeDescriptor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class)));
101 addTypeDescriptor("Maybe", MaybeType.INSTANCE);
103 addTypeDescriptor("Variant", new StandardTypeConstructor(Types.VARIANT, Kinds.STAR, TypeDesc.forClass(Variant.class)));
105 addEffectConstructor("Proc", new EffectConstructor(Types.PROC));
106 addEffectConstructor("Exception", new EffectConstructor(Types.EXCEPTION));
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]));
115 Kind tupleKind = Kinds.STAR;
116 for(int arity=0;arity<=Constants.MAX_TUPLE_LENGTH;++arity) {
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);
128 String javaName = "org/simantics/scl/runtime/tuple/Tuple"+arity;
130 cons = new NoRepConstant(returnType);
131 typeConstructor.setConstructors(new Constructor(Locations.NO_LOCATION, typeConstructor,
132 Name.create(Types.BUILTIN, constructor.name),
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),
145 typeConstructor.isOpen = false;
146 SCLValue value = new SCLValue(Name.create(Types.BUILTIN, constructor.name), cons);
148 TUPLE_CONSTRUCTORS[arity] = value;
150 tupleKind = Kinds.arrow(Kinds.STAR, tupleKind);
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)
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)
170 Boolean.isOpen = false;
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)
183 addValue("Dynamic", DynamicConstructor.INSTANCE);
187 TypeClass VecCompC = new TypeClass(Locations.NO_LOCATION,
193 addTypeClass("VecComp", VecCompC);
195 addTypeDescriptor("Vector", new VectorType(Types.VECTOR));
196 addValue("getVector", new GetVector(Types.NO_EFFECTS, Types.VECTOR));
197 addValue("lengthVector", new LengthVector(Types.VECTOR));
198 //addValue("createVectorFromList", CreateVectorFromList.INSTANCE);
200 addTypeDescriptor("MVector", new VectorType(Types.MVECTOR));
201 addValue("createMVector", CreateMVector.INSTANCE);
202 addValue("createMVectorProto", CreateMVectorProto.INSTANCE);
203 addValue("getMVector", new GetVector(Types.PROC, Types.MVECTOR));
204 addValue("lengthMVector", new LengthVector(Types.MVECTOR));
205 addValue("freezeMVector", new FreezeMVector());
206 addValue("setMVector", SetMVector.INSTANCE);
210 addValue("fail", FailFunction.INSTANCE).documentation =
211 "Throws a runtime exeception with the given string as a description.";
216 TVar a = Types.var(Kinds.STAR);
217 TVar e = Types.var(Kinds.EFFECT);
218 SSAFunction runProcFunction = new SSAFunction(new TVar[] {a,e}, e, a);
219 Type parameterType = Types.functionE(Types.PUNIT, Types.union(new Type[] {Types.PROC,e}), a);
220 SSABlock block = new SSABlock(parameterType);
221 BoundVar[] parameters = block.getParameters();
223 BoundVar x = new BoundVar(a);
224 LetApply apply = new LetApply(x, Types.PROC, parameters[0].createOccurrence(),
225 new NoRepConstant(Types.PUNIT).createOccurrence()
227 block.addStatement(apply);
229 block.setExit(new Jump(runProcFunction.getReturnCont().createOccurrence(),
230 x.createOccurrence()));
232 runProcFunction.addBlock(block);
233 SCLConstant runProc = new SCLConstant(Names.Builtin_runProc, runProcFunction.getType());
234 runProc.setDefinition(runProcFunction);
235 runProc.setInlineArity(1, 0xffffffff);
236 runProc.setBase(new JavaStaticMethod("org/simantics/scl/runtime/procedure/Procedures",
237 "runProc", a, parameterType));
238 addValue("runProc", runProc);
246 TypeClass TypeableC = new TypeClass(Locations.NO_LOCATION,
249 Type.class.getName(),
252 TypeableC.documentation = "A class of types that can be reified with `typeOf` function.";
253 addTypeClass("Typeable", TypeableC);
255 /* data Type = TCon String String
258 final TCon Type = Types.con(Types.BUILTIN, "Type");
259 final TypeDesc TypeD = TypeDesc.forClass(Type.class);
260 addValue("TCon", new JavaStaticMethod(
261 Types.class.getName().replace('.', '/'),
263 TypeDesc.forClass(TCon.class), new TypeDesc[] {TypeDesc.STRING, TypeDesc.STRING},
264 Type, Types.STRING, Types.STRING));
265 addValue("TApply", new JavaStaticMethod(
266 "org/simantics/scl/compiler/types/Types",
268 TypeDesc.forClass(TApply.class), new TypeDesc[] {TypeD, TypeD},
270 addValue("TFun", new JavaStaticMethod(
271 "org/simantics/scl/compiler/types/Types",
273 TypeDesc.forClass(TFun.class), new TypeDesc[] {TypeD, TypeD, TypeD},
274 Type, Type, Type, Type));
275 addValue("TPure", new JavaStaticField(
276 "org/simantics/scl/compiler/types/Types",
279 TypeDesc.forClass(TUnion.class),
281 addValue("TUnion2", new JavaStaticMethod(
282 "org/simantics/scl/compiler/types/Types",
287 StandardTypeConstructor TypeC = new StandardTypeConstructor(Type, Kinds.STAR,
288 TypeDesc.forClass("org/simantics/scl/compiler/types/Type"));
291 TypeC.documentation = "Represents an SCL data type.";
292 addTypeDescriptor("Type", TypeC);
294 // typeOf :: Typeable a => a -> Type
295 addValue("typeOf", TypeOfConstant.INSTANCE)
296 .documentation = "Returns the type of the value given as a parameter.";
297 addValue("typeOfProxy", TypeOfProxyConstant.INSTANCE)
298 .documentation = "Returns the type of the type proxy given as a parameter.";
299 addValue("TypeProxy", TypeProxyConstant.INSTANCE);
302 // *** Serializable ***
305 /* class Serializable a
307 TypeClass SerializableC = new TypeClass(Locations.NO_LOCATION,
310 "org/simantics/databoard/binding/Binding",
313 SerializableC.documentation = "A class of types having a `Binding`";
314 addTypeClass("Serializable", SerializableC);
316 /* data TypeRep = TCon String
317 * | TApply TypeRep TypeRep
320 StandardTypeConstructor BindingC = new StandardTypeConstructor(Types.BINDING, Kinds.STAR_TO_STAR,
321 TypeDesc.forClass("org/simantics/databoard/binding/Binding"));
322 BindingC.setType(Types.BINDING, A);
323 BindingC.documentation = "`Binding` represents a data type in the form supported by Databoard library. " +
324 "It is used to serialize and deserialize values.";
325 addTypeDescriptor("Binding", BindingC);
327 // typeOf :: Typeable a => a -> TypeReps
328 addValue("binding", BindingConstant.INSTANCE)
329 .documentation = "Gives a binding for the required type.";
336 addRelation("Eq", EqRelation.INSTANCE);
337 addRelation("Optional", OptionalRelation.INSTANCE);
338 addRelation("Execute", new ExecuteRelation(0));
339 addRelation("Execute10", new ExecuteRelation(10));
343 EQUALS = new SCLValue(Names.Builtin_equals, EqualsFunction.INSTANCE);
344 EQUALS.setPrecedence(new Precedence(4, Associativity.NONASSOC));
346 addValue("hashCode", HashCodeFunction.INSTANCE);
351 StandardTypeConstructor branchPoint = new StandardTypeConstructor(Types.BRANCH_POINT, Kinds.STAR,
352 TypeDesc.forClass(BranchPoint.class));
353 addTypeDescriptor("BranchPoint", branchPoint);
355 addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
358 setParentClassLoader(getClass().getClassLoader());
362 addTypeDescriptor("CHRContext", new StandardTypeConstructor(Types.CHRContext, Kinds.STAR, TypeDesc.forClass(CHRContext.class)));
366 public Documentation getDocumentation() {
367 return documentation;
370 public static void flush() {
371 INSTANCE = new Builtins();