import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
import org.simantics.scl.compiler.elaboration.expressions.Expression;
import org.simantics.scl.compiler.elaboration.macros.StandardMacroRule;
+import org.simantics.scl.compiler.elaboration.modules.DerivedProperty;
import org.simantics.scl.compiler.elaboration.modules.InlineProperty;
import org.simantics.scl.compiler.elaboration.modules.MethodImplementation;
import org.simantics.scl.compiler.elaboration.modules.PrivateProperty;
import org.simantics.scl.compiler.elaboration.modules.TypeClass;
import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
import org.simantics.scl.compiler.elaboration.modules.TypeClassMethod;
-import org.simantics.scl.compiler.environment.Environment;
import org.simantics.scl.compiler.errors.ErrorLog;
+import org.simantics.scl.compiler.errors.Locations;
import org.simantics.scl.compiler.internal.codegen.references.IVal;
import org.simantics.scl.compiler.internal.codegen.references.Val;
import org.simantics.scl.compiler.internal.codegen.ssa.SSAModule;
import org.simantics.scl.compiler.internal.codegen.utils.CodeBuilderUtils;
import org.simantics.scl.compiler.internal.codegen.utils.CodeBuildingException;
import org.simantics.scl.compiler.internal.codegen.utils.Constants;
-import org.simantics.scl.compiler.internal.codegen.utils.JavaNamingPolicy;
import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilderBase;
import org.simantics.scl.compiler.internal.codegen.utils.ModuleBuilder;
public static final int OPTIMIZATION_PHASES = 2;
+ CompilationContext compilationContext;
ErrorLog errorLog;
- Environment environment;
- JavaNamingPolicy namingPolicy;
- JavaTypeTranslator javaTypeTranslator;
JavaReferenceValidator<Object, Object, Object, Object> validator;
ConcreteModule module;
ModuleBuilder moduleBuilder;
Map<String, byte[]> classes;
@SuppressWarnings("unchecked")
- public CodeGeneration(ErrorLog errorLog,
- Environment environment,
- JavaNamingPolicy namingPolicy, JavaTypeTranslator javaTypeTranslator,
+ public CodeGeneration(CompilationContext compilationContext,
JavaReferenceValidator<?, ?, ?, ?> javaReferenceValidator,
- ConcreteModule module) {
- this.errorLog = errorLog;
- this.environment = environment;
- this.namingPolicy = namingPolicy;
- this.javaTypeTranslator = javaTypeTranslator;
+ ConcreteModule module) {
+ this.compilationContext = compilationContext;
+ this.errorLog = compilationContext.errorLog;
this.module = module;
this.validator = (JavaReferenceValidator<Object, Object, Object, Object>) javaReferenceValidator;
- moduleBuilder = new ModuleBuilder(namingPolicy, javaTypeTranslator);
+ moduleBuilder = new ModuleBuilder(compilationContext.namingPolicy, compilationContext.javaTypeTranslator);
}
public void simplifyValues() {
//System.out.println("===== Simplify values =====");
Collection<SCLValue> values = module.getValues();
- SimplificationContext simplificationContext =
- new SimplificationContext(environment, errorLog,
- javaTypeTranslator, validator);
+ SimplificationContext simplificationContext = new SimplificationContext(compilationContext, validator);
//System.out.println("-----------------------------------------------");
SCLValue[] valueArray = values.toArray(new SCLValue[values.size()]);
}
public void convertToSSA() {
- ModuleWriter mw = new ModuleWriter(namingPolicy.getModuleClassName());
+ ModuleWriter mw = new ModuleWriter(compilationContext.namingPolicy.getModuleClassName(), compilationContext.lineLocator);
for(SCLValue value : module.getValues()) {
//System.out.println(value.getName().name + " :: " + value.getType());
Expression expression = value.getExpression();
continue;
Name name = value.getName();
- DecomposedExpression decomposed =
- DecomposedExpression.decompose(expression);
SCLConstant constant = new SCLConstant(name, value.getType());
value.setValue(constant);
decomposed.typeParameters,
decomposed.returnType,
decomposed.parameterTypes));*/
+ boolean isDerived = false;
for(SCLValueProperty prop : value.getProperties()) {
if(prop instanceof InlineProperty) {
InlineProperty inlineProperty = (InlineProperty)prop;
constant.setInlineArity(inlineProperty.arity, inlineProperty.phaseMask);
}
else if(prop == PrivateProperty.INSTANCE)
- constant.setPrivate(true);
+ constant.setPrivate(!isDerived);
+ else if(prop == DerivedProperty.INSTANCE) {
+ constant.setPrivate(false);
+ isDerived = true;
+ }
}
}
// This is quite hackish optimization that can be possibly removed when
continue;
DecomposedExpression decomposed =
- DecomposedExpression.decompose(expression);
+ DecomposedExpression.decompose(errorLog, expression);
CodeWriter w = mw.createFunction((SCLConstant)value.getValue(),
decomposed.typeParameters,
IVal[] parameterVals = w.getParameters();
for(int i=0;i<decomposed.parameters.length;++i)
decomposed.parameters[i].setVal(parameterVals[i]);
- w.return_(decomposed.body.toVal(environment, w));
+ w.return_(expression.location, decomposed.body.toVal(compilationContext, w));
} catch(RuntimeException e) {
- errorLog.setExceptionPosition(value.getExpression().location);
+ long location = value.getExpression().location;
+ if(location == Locations.NO_LOCATION)
+ location = value.definitionLocation;
+ errorLog.setExceptionPosition(location);
throw e;
}
}
}
public void optimizeSSA() {
- if(SCLCompilerConfiguration.SHOW_SSA_BEFORE_OPTIMIZATION) {
+ if(SCLCompilerConfiguration.SHOW_SSA_BEFORE_OPTIMIZATION && SCLCompilerConfiguration.debugFilter(module.getName())) {
System.out.println("=== SSA before optimization ====================================");
System.out.println(ssaModule);
}
ssaModule.validate();
int optCount = 0;
for(int phase=0;phase<OPTIMIZATION_PHASES;++phase) {
- while(optCount++ < 100 && ssaModule.simplify(environment, phase)) {
+ while(optCount++ < 100 && ssaModule.simplify(compilationContext.environment, phase)) {
//System.out.println("simplify " + optCount);
//System.out.println("================================================================");
//System.out.println(ssaModule);
if(phase == 0)
ssaModule.saveInlinableDefinitions();
}
- if(SCLCompilerConfiguration.SHOW_SSA_BEFORE_LAMBDA_LIFTING) {
+ if(SCLCompilerConfiguration.SHOW_SSA_BEFORE_LAMBDA_LIFTING && SCLCompilerConfiguration.debugFilter(module.getName())) {
System.out.println("=== SSA before lambda lifting ==================================");
System.out.println(ssaModule);
}
}
public void generateCode() {
- if(SCLCompilerConfiguration.SHOW_FINAL_SSA) {
+ if(SCLCompilerConfiguration.SHOW_FINAL_SSA && SCLCompilerConfiguration.debugFilter(module.getName())) {
System.out.println("=== Final SSA ==================================================");
System.out.println(ssaModule);
}
cf.setSourceFile("_SCL_DataType");
CodeBuilderUtils.makeRecord(cf, constructor.name.name,
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "c",
- javaTypeTranslator.toTypeDescs(constructor.parameterTypes),
+ compilationContext.javaTypeTranslator.toTypeDescs(constructor.parameterTypes),
true);
moduleBuilder.addClass(cf);
}
cf.setSourceFile("_SCL_DataType");
CodeBuilderUtils.makeRecord(cf, constructor.name.name,
Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "c",
- javaTypeTranslator.toTypeDescs(constructor.parameterTypes),
+ compilationContext.javaTypeTranslator.toTypeDescs(constructor.parameterTypes),
true);
moduleBuilder.addClass(cf);
}
MethodImplementation implementation =
instance.methodImplementations.get(method.getName());
if(implementation.isDefault) {
- IVal function = environment.getValue(implementation.name).getValue();
+ IVal function = compilationContext.environment.getValue(implementation.name).getValue();
Val[] parameters = new Val[method.getArity() + 1];
MultiFunction mfun2;