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