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.ThrowFunction;
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 final SCLValue[] TUPLE_CONSTRUCTORS = new SCLValue[Constants.MAX_TUPLE_LENGTH+1];
57 public static final SCLValue[] LIST_CONSTRUCTORS = new SCLValue[Constants.MAX_LIST_LITERAL_LENGTH+1];
59 public static 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));
107 addEffectConstructor("Exception", new EffectConstructor(Types.EXCEPTION));
109 //addTypeDescriptor("->", new StandardTypeConstructor(Kinds.STAR_TO_STAR_TO_STAR, Constants.FUNCTION));
110 addTypeDescriptor("[]", new StandardTypeConstructor(Types.LIST, Kinds.STAR_TO_STAR, Constants.LIST));
111 addTypeDescriptor("@", new StandardTypeConstructor(Types.PUNIT, Kinds.STAR, Constants.TUPLE[0]));
112 addTypeDescriptor("TypeProxy", new StandardTypeConstructor(Types.TYPE_PROXY, Kinds.STAR_TO_STAR, Constants.TUPLE[0]));
116 Kind tupleKind = Kinds.STAR;
117 for(int arity=0;arity<=Constants.MAX_TUPLE_LENGTH;++arity) {
119 TVar[] vars = new TVar[arity];
120 for(int i=0;i<vars.length;++i)
121 vars[i] = Types.var(Kinds.STAR);
122 TCon constructor = Types.tupleConstructor(arity);
123 StandardTypeConstructor typeConstructor =
124 new StandardTypeConstructor(constructor, tupleKind, Constants.TUPLE[arity]);
125 addTypeDescriptor(constructor.name, typeConstructor);
126 Type returnType = Types.apply(constructor, vars);
127 typeConstructor.setType(constructor, vars);
129 String javaName = "org/simantics/scl/runtime/tuple/Tuple"+arity;
131 cons = new NoRepConstant(returnType);
132 typeConstructor.setConstructors(new Constructor(Locations.NO_LOCATION, typeConstructor,
133 Name.create(Types.BUILTIN, constructor.name),
138 cons = new SCLConstructor(constructor.name,
139 javaName, vars, 0, returnType, vars);
140 typeConstructor.setConstructors(
141 new Constructor(Locations.NO_LOCATION, typeConstructor,
142 Name.create(Types.BUILTIN, constructor.name),
146 typeConstructor.isOpen = false;
147 SCLValue value = new SCLValue(Name.create(Types.BUILTIN, constructor.name), cons);
149 TUPLE_CONSTRUCTORS[arity] = value;
151 tupleKind = Kinds.arrow(Kinds.STAR, tupleKind);
156 for(int arity=0;arity<=Constants.MAX_LIST_LITERAL_LENGTH;++arity) {
157 LIST_CONSTRUCTORS[arity] = addValue("_list_literal_" + arity + "_",
158 arity == 0 ? new EmptyListConstructor() :
159 new ListConstructor(arity)
165 SCLValue True = addValue("True", new BooleanConstant(true));
166 SCLValue False = addValue("False", new BooleanConstant(false));
167 Boolean.setConstructors(
168 new Constructor(Locations.NO_LOCATION, Boolean, False.getName(), Type.EMPTY_ARRAY, null),
169 new Constructor(Locations.NO_LOCATION, Boolean, True.getName(), Type.EMPTY_ARRAY, null)
171 Boolean.isOpen = false;
175 Nothing = addValue("Nothing", NothingConstant.INSTANCE);
176 Just = addValue("Just", JustConstant.INSTANCE);
177 MaybeType.INSTANCE.setConstructors(
178 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Nothing.getName(), Type.EMPTY_ARRAY, null),
179 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Just.getName(), new Type[] {MaybeType.INSTANCE.parameters[0]}, null)
184 addValue("Dynamic", DynamicConstructor.INSTANCE);
188 TypeClass VecCompC = new TypeClass(Locations.NO_LOCATION,
194 addTypeClass("VecComp", VecCompC);
196 addTypeDescriptor("Vector", new VectorType(Types.VECTOR));
197 addValue("getVector", new GetVector(Types.NO_EFFECTS, Types.VECTOR));
198 addValue("lengthVector", new LengthVector(Types.VECTOR));
199 //addValue("createVectorFromList", CreateVectorFromList.INSTANCE);
201 addTypeDescriptor("MVector", new VectorType(Types.MVECTOR));
202 addValue("createMVector", CreateMVector.INSTANCE);
203 addValue("createMVectorProto", CreateMVectorProto.INSTANCE);
204 addValue("getMVector", new GetVector(Types.PROC, Types.MVECTOR));
205 addValue("lengthMVector", new LengthVector(Types.MVECTOR));
206 addValue("freezeMVector", new FreezeMVector());
207 addValue("setMVector", SetMVector.INSTANCE);
211 addValue("fail", FailFunction.INSTANCE).documentation =
212 "Throws a runtime exeception with the given string as a description.";
213 addValue("throw", ThrowFunction.INSTANCE).documentation =
214 "Throws a given exception.";
219 TVar a = Types.var(Kinds.STAR);
220 TVar e = Types.var(Kinds.EFFECT);
221 SSAFunction runProcFunction = new SSAFunction(new TVar[] {a,e}, e, a);
222 Type parameterType = Types.functionE(Types.PUNIT, Types.union(new Type[] {Types.PROC,e}), a);
223 SSABlock block = new SSABlock(parameterType);
224 BoundVar[] parameters = block.getParameters();
226 BoundVar x = new BoundVar(a);
227 LetApply apply = new LetApply(x, Types.PROC, parameters[0].createOccurrence(),
228 new NoRepConstant(Types.PUNIT).createOccurrence()
230 block.addStatement(apply);
232 block.setExit(new Jump(runProcFunction.getReturnCont().createOccurrence(),
233 x.createOccurrence()));
235 runProcFunction.addBlock(block);
236 SCLConstant runProc = new SCLConstant(Names.Builtin_runProc, runProcFunction.getType());
237 runProc.setDefinition(runProcFunction);
238 runProc.setInlineArity(1, 0xffffffff);
239 runProc.setBase(new JavaStaticMethod("org/simantics/scl/runtime/procedure/Procedures",
240 "runProc", a, parameterType));
241 addValue("runProc", runProc);
249 TypeClass TypeableC = new TypeClass(Locations.NO_LOCATION,
252 Type.class.getName(),
255 TypeableC.documentation = "A class of types that can be reified with `typeOf` function.";
256 addTypeClass("Typeable", TypeableC);
258 /* data Type = TCon String String
261 final TCon Type = Types.con(Types.BUILTIN, "Type");
262 final TypeDesc TypeD = TypeDesc.forClass(Type.class);
263 addValue("TCon", new JavaStaticMethod(
264 Types.class.getName().replace('.', '/'),
266 TypeDesc.forClass(TCon.class), new TypeDesc[] {TypeDesc.STRING, TypeDesc.STRING},
267 Type, Types.STRING, Types.STRING));
268 addValue("TApply", new JavaStaticMethod(
269 "org/simantics/scl/compiler/types/Types",
271 TypeDesc.forClass(TApply.class), new TypeDesc[] {TypeD, TypeD},
273 addValue("TFun", new JavaStaticMethod(
274 "org/simantics/scl/compiler/types/Types",
276 TypeDesc.forClass(TFun.class), new TypeDesc[] {TypeD, TypeD, TypeD},
277 Type, Type, Type, Type));
278 addValue("TPure", new JavaStaticField(
279 "org/simantics/scl/compiler/types/Types",
282 TypeDesc.forClass(TUnion.class),
284 addValue("TUnion2", new JavaStaticMethod(
285 "org/simantics/scl/compiler/types/Types",
289 addValue("TUnion3", new JavaStaticMethod(
290 "org/simantics/scl/compiler/types/Types",
293 Type, Type, Type, Type));
295 StandardTypeConstructor TypeC = new StandardTypeConstructor(Type, Kinds.STAR,
296 TypeDesc.forClass("org/simantics/scl/compiler/types/Type"));
299 TypeC.documentation = "Represents an SCL data type.";
300 addTypeDescriptor("Type", TypeC);
302 // typeOf :: Typeable a => a -> Type
303 addValue("typeOf", TypeOfConstant.INSTANCE)
304 .documentation = "Returns the type of the value given as a parameter.";
305 addValue("typeOfProxy", TypeOfProxyConstant.INSTANCE)
306 .documentation = "Returns the type of the type proxy given as a parameter.";
307 addValue("TypeProxy", TypeProxyConstant.INSTANCE);
310 // *** Serializable ***
313 /* class Serializable a
315 TypeClass SerializableC = new TypeClass(Locations.NO_LOCATION,
318 "org/simantics/databoard/binding/Binding",
321 SerializableC.documentation = "A class of types having a `Binding`";
322 addTypeClass("Serializable", SerializableC);
324 /* data TypeRep = TCon String
325 * | TApply TypeRep TypeRep
328 StandardTypeConstructor BindingC = new StandardTypeConstructor(Types.BINDING, Kinds.STAR_TO_STAR,
329 TypeDesc.forClass("org/simantics/databoard/binding/Binding"));
330 BindingC.setType(Types.BINDING, A);
331 BindingC.documentation = "`Binding` represents a data type in the form supported by Databoard library. " +
332 "It is used to serialize and deserialize values.";
333 addTypeDescriptor("Binding", BindingC);
335 // typeOf :: Typeable a => a -> TypeReps
336 addValue("binding", BindingConstant.INSTANCE)
337 .documentation = "Gives a binding for the required type.";
344 addRelation("Eq", EqRelation.INSTANCE);
345 addRelation("Optional", OptionalRelation.INSTANCE);
346 addRelation("Execute", new ExecuteRelation(0));
347 addRelation("Execute10", new ExecuteRelation(10));
351 EQUALS = new SCLValue(Names.Builtin_equals, EqualsFunction.INSTANCE);
352 EQUALS.setPrecedence(new Precedence(4, Associativity.NONASSOC));
354 addValue("hashCode", HashCodeFunction.INSTANCE);
359 StandardTypeConstructor branchPoint = new StandardTypeConstructor(Types.BRANCH_POINT, Kinds.STAR,
360 TypeDesc.forClass(BranchPoint.class));
361 addTypeDescriptor("BranchPoint", branchPoint);
363 addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
366 setParentClassLoader(getClass().getClassLoader());
370 addTypeDescriptor("CHRContext", new StandardTypeConstructor(Types.CHRContext, Kinds.STAR, TypeDesc.forClass(CHRContext.class)));
374 public Documentation getDocumentation() {
375 return documentation;
378 public static void flush() {
379 INSTANCE = new Builtins();