1 package org.simantics.scl.compiler.internal.codegen.writer;
3 import gnu.trove.map.hash.THashMap;
7 import org.simantics.scl.compiler.common.names.Name;
8 import org.simantics.scl.compiler.constants.Constant;
9 import org.simantics.scl.compiler.constants.JavaStaticField;
10 import org.simantics.scl.compiler.constants.SCLConstant;
11 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
12 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
13 import org.simantics.scl.compiler.internal.codegen.ssa.SSAModule;
14 import org.simantics.scl.compiler.types.TVar;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.runtime.tuple.Tuple2;
18 public class ModuleWriter {
22 String moduleClassName;
23 THashMap<Tuple2, JavaStaticField> externalConstantMap =
24 new THashMap<Tuple2, JavaStaticField>();
25 int externalConstantId = 0;
27 public ModuleWriter(String moduleClassName) {
28 this.module = new SSAModule();
29 this.moduleClassName = moduleClassName;
32 public CodeWriter createFunction(SCLConstant constant, TVar[] typeParameters,
33 Type effect, Type returnType, Type[] parameterTypes) {
34 Name name = constant.getName();
35 SSAFunction function = new SSAFunction(typeParameters, effect, returnType);
36 SSABlock block = new SSABlock(parameterTypes);
37 function.addBlock(block);
38 function.setTarget(constant);
39 constant.setDefinition(function);
40 module.put(name, constant);
41 return new CodeWriter(this, block);
45 public String toString() {
46 return module.toString();
49 public SSAModule getModule() {
53 public Constant getExternalConstant(Object value, Type type) {
54 Tuple2 tuple = new Tuple2(value, type);
55 if(externalConstantMap.contains(tuple))
56 return externalConstantMap.get(tuple);
59 String name = "externalConstant" + externalConstantId;
60 JavaStaticField constant = new JavaStaticField(moduleClassName, name, type, -1);
61 externalConstantMap.put(tuple, constant);
62 module.addStaticField(new Tuple2(name, type));
67 public ExternalConstant[] getExternalConstants() {
68 ExternalConstant[] result = new ExternalConstant[externalConstantMap.size()];
70 for(Map.Entry<Tuple2, JavaStaticField> entry : externalConstantMap.entrySet()) {
71 result[i++] = new ExternalConstant(entry.getValue().getFieldName(), entry.getKey().c0);