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.ClassConstant;
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.ThrowFunction;
23 import org.simantics.scl.compiler.constants.singletons.TypeOfConstant;
24 import org.simantics.scl.compiler.constants.singletons.TypeOfProxyConstant;
25 import org.simantics.scl.compiler.constants.singletons.TypeProxyConstant;
26 import org.simantics.scl.compiler.elaboration.fundeps.Fundep;
27 import org.simantics.scl.compiler.elaboration.modules.Documentation;
28 import org.simantics.scl.compiler.elaboration.modules.PrivateProperty;
29 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
30 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
31 import org.simantics.scl.compiler.errors.Locations;
32 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
33 import org.simantics.scl.compiler.internal.codegen.references.BoundVar;
34 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
35 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
36 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Jump;
37 import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply;
38 import org.simantics.scl.compiler.internal.codegen.types.MaybeType;
39 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
40 import org.simantics.scl.compiler.internal.codegen.types.VectorType;
41 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
42 import org.simantics.scl.compiler.module.ConcreteModule;
43 import org.simantics.scl.compiler.types.TApply;
44 import org.simantics.scl.compiler.types.TCon;
45 import org.simantics.scl.compiler.types.TFun;
46 import org.simantics.scl.compiler.types.TPred;
47 import org.simantics.scl.compiler.types.TUnion;
48 import org.simantics.scl.compiler.types.TVar;
49 import org.simantics.scl.compiler.types.Type;
50 import org.simantics.scl.compiler.types.Types;
51 import org.simantics.scl.compiler.types.kinds.Kind;
52 import org.simantics.scl.compiler.types.kinds.Kinds;
53 import org.simantics.scl.runtime.chr.CHRContext;
54 import org.simantics.scl.runtime.profiling.BranchPoint;
56 public class Builtins extends ConcreteModule {
58 public static final SCLValue[] TUPLE_CONSTRUCTORS = new SCLValue[Constants.MAX_TUPLE_LENGTH+1];
59 public static final SCLValue[] LIST_CONSTRUCTORS = new SCLValue[Constants.MAX_LIST_LITERAL_LENGTH+1];
61 public static Builtins INSTANCE = new Builtins();
63 public static SCLValue Nothing;
64 public static SCLValue Just;
66 public static SCLValue EQUALS;
71 TVar A = Types.var(Kinds.STAR);
73 StandardTypeConstructor Boolean = new StandardTypeConstructor(Types.BOOLEAN, Kinds.STAR, TypeDesc.BOOLEAN);
74 Boolean.documentation = "Data type representing truth values `True` and `False`.";
75 addTypeDescriptor("Boolean", Boolean);
76 addTypeDescriptor("Byte", new StandardTypeConstructor(Types.BYTE, Kinds.STAR, TypeDesc.BYTE,
77 "8-bit signed integer"));
78 addTypeDescriptor("Character", new StandardTypeConstructor(Types.CHARACTER, Kinds.STAR, TypeDesc.CHAR,
79 "16-bit Unicode character."));
80 addTypeDescriptor("Short", new StandardTypeConstructor(Types.SHORT, Kinds.STAR, TypeDesc.SHORT,
81 "16-bit signed integer"));
82 addTypeDescriptor("Integer", new StandardTypeConstructor(Types.INTEGER, Kinds.STAR, TypeDesc.INT,
83 "32-bit signed integer"));
84 addTypeDescriptor("Long", new StandardTypeConstructor(Types.LONG, Kinds.STAR, TypeDesc.LONG,
85 "64-bit signed integer"));
86 addTypeDescriptor("Float", new StandardTypeConstructor(Types.FLOAT, Kinds.STAR, TypeDesc.FLOAT,
87 "32-bit floating point number"));
88 addTypeDescriptor("Double", new StandardTypeConstructor(Types.DOUBLE, Kinds.STAR, TypeDesc.DOUBLE,
89 "64-bit floating point number"));
90 addTypeDescriptor("String", new StandardTypeConstructor(Types.STRING, Kinds.STAR, TypeDesc.STRING,
93 addTypeDescriptor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class)));
95 addTypeDescriptor("Maybe", MaybeType.INSTANCE);
97 addTypeDescriptor("Variant", new StandardTypeConstructor(Types.VARIANT, Kinds.STAR, TypeDesc.forClass(Variant.class)));
99 addEffectConstructor("Proc", new EffectConstructor(Types.PROC));
100 addEffectConstructor("Exception", new EffectConstructor(Types.EXCEPTION));
102 //addTypeDescriptor("->", new StandardTypeConstructor(Kinds.STAR_TO_STAR_TO_STAR, Constants.FUNCTION));
103 addTypeDescriptor("[]", new StandardTypeConstructor(Types.LIST, Kinds.STAR_TO_STAR, Constants.LIST));
104 addTypeDescriptor("@", new StandardTypeConstructor(Types.PUNIT, Kinds.STAR, Constants.TUPLE[0]));
105 addTypeDescriptor("TypeProxy", new StandardTypeConstructor(Types.TYPE_PROXY, Kinds.STAR_TO_STAR, Constants.TUPLE[0]));
109 Kind tupleKind = Kinds.STAR;
110 for(int arity=0;arity<=Constants.MAX_TUPLE_LENGTH;++arity) {
112 TVar[] vars = new TVar[arity];
113 for(int i=0;i<vars.length;++i)
114 vars[i] = Types.var(Kinds.STAR);
115 TCon constructor = Types.tupleConstructor(arity);
116 StandardTypeConstructor typeConstructor =
117 new StandardTypeConstructor(constructor, tupleKind, Constants.TUPLE[arity]);
118 addTypeDescriptor(constructor.name, typeConstructor);
119 Type returnType = Types.apply(constructor, vars);
120 typeConstructor.setType(constructor, vars);
122 String javaName = "org/simantics/scl/runtime/tuple/Tuple"+arity;
124 cons = new NoRepConstant(returnType);
125 typeConstructor.setConstructors(new Constructor(Locations.NO_LOCATION, typeConstructor,
126 Name.create(Types.BUILTIN, constructor.name),
131 cons = new SCLConstructor(constructor.name,
132 javaName, vars, 0, returnType, vars);
133 typeConstructor.setConstructors(
134 new Constructor(Locations.NO_LOCATION, typeConstructor,
135 Name.create(Types.BUILTIN, constructor.name),
139 typeConstructor.isOpen = false;
140 SCLValue value = new SCLValue(Name.create(Types.BUILTIN, constructor.name), cons);
142 TUPLE_CONSTRUCTORS[arity] = value;
144 tupleKind = Kinds.arrow(Kinds.STAR, tupleKind);
149 for(int arity=0;arity<=Constants.MAX_LIST_LITERAL_LENGTH;++arity) {
150 SCLValue value = addValue("_list_literal_" + arity + "_",
151 arity == 0 ? new EmptyListConstructor() :
152 new ListConstructor(arity)
154 value.addProperty(PrivateProperty.INSTANCE);
155 LIST_CONSTRUCTORS[arity] = value;
160 SCLValue True = addValue("True", new BooleanConstant(true));
161 SCLValue False = addValue("False", new BooleanConstant(false));
162 Boolean.setConstructors(
163 new Constructor(Locations.NO_LOCATION, Boolean, False.getName(), Type.EMPTY_ARRAY, null),
164 new Constructor(Locations.NO_LOCATION, Boolean, True.getName(), Type.EMPTY_ARRAY, null)
166 Boolean.isOpen = false;
170 Nothing = addValue("Nothing", NothingConstant.INSTANCE);
171 Just = addValue("Just", JustConstant.INSTANCE);
172 MaybeType.INSTANCE.setConstructors(
173 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Nothing.getName(), Type.EMPTY_ARRAY, null),
174 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Just.getName(), new Type[] {MaybeType.INSTANCE.parameters[0]}, null)
179 addValue("Dynamic", DynamicConstructor.INSTANCE);
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);
204 StandardTypeConstructor ClassC = new StandardTypeConstructor(Types.CLASS, Kinds.STAR_TO_STAR,
205 TypeDesc.forClass("java/lang/Class"));
206 ClassC.setType(Types.CLASS, A);
207 addTypeDescriptor("Class", ClassC);
208 addValue("classObject", ClassConstant.INSTANCE);
212 addValue("fail", FailFunction.INSTANCE).documentation =
213 "Throws a runtime exeception with the given string as a description.";
214 addValue("throw", ThrowFunction.INSTANCE).documentation =
215 "Throws a given exception.";
220 TVar a = Types.var(Kinds.STAR);
221 TVar e = Types.var(Kinds.EFFECT);
222 SSAFunction runProcFunction = new SSAFunction(new TVar[] {a,e}, e, a);
223 Type parameterType = Types.functionE(Types.PUNIT, Types.union(new Type[] {Types.PROC,e}), a);
224 SSABlock block = new SSABlock(parameterType);
225 BoundVar[] parameters = block.getParameters();
227 BoundVar x = new BoundVar(a);
228 LetApply apply = new LetApply(x, Types.PROC, parameters[0].createOccurrence(),
229 new NoRepConstant(Types.PUNIT).createOccurrence()
231 block.addStatement(apply);
233 block.setExit(new Jump(runProcFunction.getReturnCont().createOccurrence(),
234 x.createOccurrence()));
236 runProcFunction.addBlock(block);
237 SCLConstant runProc = new SCLConstant(Names.Builtin_runProc, runProcFunction.getType());
238 runProc.setDefinition(runProcFunction);
239 runProc.setInlineArity(1, 0xffffffff);
240 runProc.setBase(new JavaStaticMethod("org/simantics/scl/runtime/procedure/Procedures",
241 "runProc", a, parameterType));
242 addValue("runProc", runProc);
250 TypeClass TypeableC = new TypeClass(Locations.NO_LOCATION,
253 Type.class.getName(),
256 TypeableC.documentation = "A class of types that can be reified with `typeOf` function.";
257 addTypeClass("Typeable", TypeableC);
259 /* data Type = TCon String String
262 final TCon Type = Types.con(Types.BUILTIN, "Type");
263 final TypeDesc TypeD = TypeDesc.forClass(Type.class);
264 addValue("TCon", new JavaStaticMethod(
265 Types.class.getName().replace('.', '/'),
267 TypeDesc.forClass(TCon.class), new TypeDesc[] {TypeDesc.STRING, TypeDesc.STRING},
268 Type, Types.STRING, Types.STRING));
269 addValue("TApply", new JavaStaticMethod(
270 "org/simantics/scl/compiler/types/Types",
272 TypeDesc.forClass(TApply.class), new TypeDesc[] {TypeD, TypeD},
274 addValue("TFun", new JavaStaticMethod(
275 "org/simantics/scl/compiler/types/Types",
277 TypeDesc.forClass(TFun.class), new TypeDesc[] {TypeD, TypeD, TypeD},
278 Type, Type, Type, Type));
279 addValue("TPure", new JavaStaticField(
280 "org/simantics/scl/compiler/types/Types",
283 TypeDesc.forClass(TUnion.class),
285 addValue("TUnion2", new JavaStaticMethod(
286 "org/simantics/scl/compiler/types/Types",
290 addValue("TUnion3", new JavaStaticMethod(
291 "org/simantics/scl/compiler/types/Types",
294 Type, Type, Type, Type));
296 StandardTypeConstructor TypeC = new StandardTypeConstructor(Type, Kinds.STAR,
297 TypeDesc.forClass("org/simantics/scl/compiler/types/Type"));
300 TypeC.documentation = "Represents an SCL data type.";
301 addTypeDescriptor("Type", TypeC);
303 // typeOf :: Typeable a => a -> Type
304 addValue("typeOf", TypeOfConstant.INSTANCE)
305 .documentation = "Returns the type of the value given as a parameter.";
306 addValue("typeOfProxy", TypeOfProxyConstant.INSTANCE)
307 .documentation = "Returns the type of the type proxy given as a parameter.";
308 addValue("TypeProxy", TypeProxyConstant.INSTANCE);
311 // *** Serializable ***
314 /* class Serializable a
316 TypeClass SerializableC = new TypeClass(Locations.NO_LOCATION,
319 "org/simantics/databoard/binding/Binding",
322 SerializableC.documentation = "A class of types having a `Binding`";
323 addTypeClass("Serializable", SerializableC);
325 /* data TypeRep = TCon String
326 * | TApply TypeRep TypeRep
329 StandardTypeConstructor BindingC = new StandardTypeConstructor(Types.BINDING, Kinds.STAR_TO_STAR,
330 TypeDesc.forClass("org/simantics/databoard/binding/Binding"));
331 BindingC.setType(Types.BINDING, A);
332 BindingC.documentation = "`Binding` represents a data type in the form supported by Databoard library. " +
333 "It is used to serialize and deserialize values.";
334 addTypeDescriptor("Binding", BindingC);
336 // typeOf :: Typeable a => a -> TypeReps
337 addValue("binding", BindingConstant.INSTANCE)
338 .documentation = "Gives a binding for the required type.";
345 addRelation("Eq", EqRelation.INSTANCE);
346 addRelation("Optional", OptionalRelation.INSTANCE);
347 addRelation("Execute", new ExecuteRelation(0));
348 addRelation("Execute10", new ExecuteRelation(10));
352 EQUALS = new SCLValue(Names.Builtin_equals, EqualsFunction.INSTANCE);
353 EQUALS.setPrecedence(new Precedence(4, Associativity.NONASSOC));
355 addValue("hashCode", HashCodeFunction.INSTANCE);
360 StandardTypeConstructor branchPoint = new StandardTypeConstructor(Types.BRANCH_POINT, Kinds.STAR,
361 TypeDesc.forClass(BranchPoint.class));
362 addTypeDescriptor("BranchPoint", branchPoint);
364 addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
367 setParentClassLoader(getClass().getClassLoader());
371 addTypeDescriptor("CHRContext", new StandardTypeConstructor(Types.CHRContext, Kinds.STAR, TypeDesc.forClass(CHRContext.class)));
375 public Documentation getDocumentation() {
376 return documentation;
379 public static void flush() {
380 INSTANCE = new Builtins();