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.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;
54 public class Builtins extends ConcreteModule {
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];
59 public static final Builtins INSTANCE = new Builtins();
61 public static SCLValue Nothing;
62 public static SCLValue Just;
64 public static SCLValue EQUALS;
69 TVar A = Types.var(Kinds.STAR);
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,
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)));
100 addTypeDescriptor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class)));
102 addTypeDescriptor("Maybe", MaybeType.INSTANCE);
104 addTypeDescriptor("Variant", new StandardTypeConstructor(Types.VARIANT, Kinds.STAR, TypeDesc.forClass(Variant.class)));
106 addEffectConstructor("Proc", new EffectConstructor(Types.PROC));
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 TypeClass VecCompC = new TypeClass(Locations.NO_LOCATION,
189 addTypeClass("VecComp", VecCompC);
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);
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);
206 addValue("fail", FailFunction.INSTANCE).documentation =
207 "Throws a runtime exeception with the given string as a description.";
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();
219 BoundVar x = new BoundVar(a);
220 LetApply apply = new LetApply(x, Types.PROC, parameters[0].createOccurrence(),
221 new NoRepConstant(Types.PUNIT).createOccurrence()
223 block.addStatement(apply);
225 block.setExit(new Jump(runProcFunction.getReturnCont().createOccurrence(),
226 x.createOccurrence()));
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);
242 TypeClass TypeableC = new TypeClass(Locations.NO_LOCATION,
245 Type.class.getName(),
248 TypeableC.documentation = "A class of types that can be reified with `typeOf` function.";
249 addTypeClass("Typeable", TypeableC);
251 /* data Type = TCon String String
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('.', '/'),
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",
264 TypeDesc.forClass(TApply.class), new TypeDesc[] {TypeD, TypeD},
266 addValue("TFun", new JavaStaticMethod(
267 "org/simantics/scl/compiler/types/Types",
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",
275 TypeDesc.forClass(TUnion.class),
277 addValue("TUnion2", new JavaStaticMethod(
278 "org/simantics/scl/compiler/types/Types",
283 StandardTypeConstructor TypeC = new StandardTypeConstructor(Type, Kinds.STAR,
284 TypeDesc.forClass("org/simantics/scl/compiler/types/Type"));
287 TypeC.documentation = "Represents an SCL data type.";
288 addTypeDescriptor("Type", TypeC);
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);
298 // *** Serializable ***
301 /* class Serializable a
303 TypeClass SerializableC = new TypeClass(Locations.NO_LOCATION,
306 "org/simantics/databoard/binding/Binding",
309 SerializableC.documentation = "A class of types having a `Binding`";
310 addTypeClass("Serializable", SerializableC);
312 /* data TypeRep = TCon String
313 * | TApply TypeRep TypeRep
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);
323 // typeOf :: Typeable a => a -> TypeReps
324 addValue("binding", BindingConstant.INSTANCE)
325 .documentation = "Gives a binding for the required type.";
332 addRelation("Eq", EqRelation.INSTANCE);
333 addRelation("Optional", OptionalRelation.INSTANCE);
334 addRelation("Execute", new ExecuteRelation(0));
335 addRelation("Execute10", new ExecuteRelation(10));
339 EQUALS = new SCLValue(Names.Builtin_equals, EqualsFunction.INSTANCE);
340 EQUALS.setPrecedence(new Precedence(4, Associativity.NONASSOC));
342 addValue("hashCode", HashCodeFunction.INSTANCE);
347 StandardTypeConstructor branchPoint = new StandardTypeConstructor(Types.BRANCH_POINT, Kinds.STAR,
348 TypeDesc.forClass(BranchPoint.class));
349 addTypeDescriptor("BranchPoint", branchPoint);
351 addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
354 setParentClassLoader(getClass().getClassLoader());
358 addTypeDescriptor("CHRContext", new StandardTypeConstructor(Types.CHRContext, Kinds.STAR, TypeDesc.forClass(CHRContext.class)));
362 public Documentation getDocumentation() {
363 return documentation;