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.types.Types;
import org.simantics.scl.compiler.types.exceptions.MatchException;
import org.simantics.scl.compiler.types.util.MultiFunction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import gnu.trove.procedure.TObjectObjectProcedure;
import gnu.trove.procedure.TObjectProcedure;
public class CodeGeneration {
-
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(CodeGeneration.class);
+
public static final int OPTIMIZATION_PHASES = 2;
CompilationContext compilationContext;
}
public void simplifyValues() {
- //System.out.println("===== Simplify values =====");
+ //LOGGER.info("===== Simplify values =====");
Collection<SCLValue> values = module.getValues();
SimplificationContext simplificationContext = new SimplificationContext(compilationContext, validator);
- //System.out.println("-----------------------------------------------");
+ //LOGGER.info("-----------------------------------------------");
SCLValue[] valueArray = values.toArray(new SCLValue[values.size()]);
for(SCLValue value : valueArray) {
// Simplify
for(SCLValue value : valueArray) {
- //System.out.println("BEFORE " + value.getName() + " = " + value.getExpression());
+ //LOGGER.info("BEFORE " + value.getName() + " = " + value.getExpression());
value.getSimplifiedExpression(simplificationContext);
- //System.out.println("AFTER " + value.getName() + " = " + value.getExpression());
+ //LOGGER.info("AFTER " + value.getName() + " = " + value.getExpression());
}
}
public void convertToSSA() {
- ModuleWriter mw = new ModuleWriter(compilationContext.namingPolicy.getModuleClassName());
+ ModuleWriter mw = new ModuleWriter(compilationContext.namingPolicy.getModuleClassName(), compilationContext.lineLocator);
for(SCLValue value : module.getValues()) {
- //System.out.println(value.getName().name + " :: " + value.getType());
+ //LOGGER.info(value.getName().name + " :: " + value.getType());
Expression expression = value.getExpression();
if(expression == null)
continue;
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
if(constant.getTypeParameters().length > 0)
continue;
- //System.out.println(value.getName() + " <- " + constant.getValue().getName());
+ //LOGGER.info(value.getName() + " <- " + constant.getValue().getName());
value.setValue(constant.getValue().getValue());
value.setExpression(null); // HMM??
}*/
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(compilationContext.environment, w));
+ w.return_(expression.location, decomposed.body.toVal(compilationContext, w));
} catch(RuntimeException e) {
long location = value.getExpression().location;
if(location == Locations.NO_LOCATION)
public void optimizeSSA() {
if(SCLCompilerConfiguration.SHOW_SSA_BEFORE_OPTIMIZATION && SCLCompilerConfiguration.debugFilter(module.getName())) {
- System.out.println("=== SSA before optimization ====================================");
- System.out.println(ssaModule);
+ LOGGER.info("=== SSA before optimization ====================================");
+ LOGGER.info("{}", ssaModule);
}
if(SCLCompilerConfiguration.DEBUG)
ssaModule.validate();
int optCount = 0;
for(int phase=0;phase<OPTIMIZATION_PHASES;++phase) {
while(optCount++ < 100 && ssaModule.simplify(compilationContext.environment, phase)) {
- //System.out.println("simplify " + optCount);
- //System.out.println("================================================================");
- //System.out.println(ssaModule);
+ //LOGGER.info("simplify " + optCount);
+ //LOGGER.info("================================================================");
+ //LOGGER.info(ssaModule);
}
if(phase == 0)
ssaModule.saveInlinableDefinitions();
}
if(SCLCompilerConfiguration.SHOW_SSA_BEFORE_LAMBDA_LIFTING && SCLCompilerConfiguration.debugFilter(module.getName())) {
- System.out.println("=== SSA before lambda lifting ==================================");
- System.out.println(ssaModule);
+ LOGGER.info("=== SSA before lambda lifting ==================================");
+ LOGGER.info("{}", ssaModule);
}
ssaModule.lambdaLift(errorLog);
//ssaModule.validate();
public void generateCode() {
if(SCLCompilerConfiguration.SHOW_FINAL_SSA && SCLCompilerConfiguration.debugFilter(module.getName())) {
- System.out.println("=== Final SSA ==================================================");
- System.out.println(ssaModule);
+ LOGGER.info("=== Final SSA ==================================================");
+ LOGGER.info("{}", ssaModule);
}
try {
ssaModule.generateCode(moduleBuilder);
errorLog.log(e.getMessage());
}
if(SCLCompilerConfiguration.TRACE_MAX_METHOD_SIZE && moduleBuilder.getMethodSizeCounter() != null)
- System.out.println("[Max method size] " + module.getName() + ": " + moduleBuilder.getMethodSizeCounter());
+ LOGGER.info("[Max method size] " + module.getName() + ": " + moduleBuilder.getMethodSizeCounter());
classes = moduleBuilder.getClasses();
}
if(constructor.parameterTypes.length != 1) {
String javaName = MethodBuilderBase.getClassName(dataType.getTypeDesc());
if(SCLCompilerConfiguration.TRACE_METHOD_CREATION)
- System.out.println("Create class " + javaName);
+ LOGGER.info("Create class " + javaName);
ClassBuilder cf = new ClassBuilder(moduleBuilder, Opcodes.ACC_PUBLIC, javaName, "java/lang/Object");
cf.setSourceFile("_SCL_DataType");
CodeBuilderUtils.makeRecord(cf, constructor.name.name,
// Create supertype
{
if(SCLCompilerConfiguration.TRACE_METHOD_CREATION)
- System.out.println("Create class " + javaName);
+ LOGGER.info("Create class " + javaName);
ClassBuilder cf = new ClassBuilder(moduleBuilder,
Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC,
javaName, "java/lang/Object");
// Create constructors
for(Constructor constructor : dataType.constructors) {
if(SCLCompilerConfiguration.TRACE_METHOD_CREATION)
- System.out.println("Create class " + constructor.javaName);
+ LOGGER.info("Create class " + constructor.javaName);
ClassBuilder cf = new ClassBuilder(moduleBuilder, Opcodes.ACC_PUBLIC, constructor.javaName, javaName);
cf.setSourceFile("_SCL_DataType");
CodeBuilderUtils.makeRecord(cf, constructor.name.name,
final JavaTypeTranslator javaTypeTranslator = moduleBuilder.getJavaTypeTranslator();
if(SCLCompilerConfiguration.TRACE_METHOD_CREATION)
- System.out.println("Create class " + typeClass.javaName);
+ LOGGER.info("Create class " + typeClass.javaName);
final ClassBuilder cf = new ClassBuilder(moduleBuilder,
Opcodes.ACC_INTERFACE | Opcodes.ACC_PUBLIC,
typeClass.javaName, "java/lang/Object");
final JavaTypeTranslator javaTypeTranslator = moduleBuilder.getJavaTypeTranslator();
if(SCLCompilerConfiguration.TRACE_METHOD_CREATION)
- System.out.println("Create class " + instance.javaName);
+ LOGGER.info("Create class " + instance.javaName);
final ClassBuilder cb = new ClassBuilder(moduleBuilder, Opcodes.ACC_PUBLIC, instance.javaName, "java/lang/Object",
instance.typeClass.javaName);
cb.setSourceFile("_SCL_TypeClassInstance");
} catch (MatchException e) {
throw new InternalCompilerError("Method " + method.getName() + " has too high arity.");
}
- //System.out.println("Interface types: " + Arrays.toString(types));
+ //LOGGER.info("Interface types: " + Arrays.toString(types));
TypeDesc[] parameterTypeDescs = javaTypeTranslator.toTypeDescs(mfun.parameterTypes);
TypeDesc returnTypeDesc = javaTypeTranslator.toTypeDesc(mfun.returnType);
MethodBuilder mb = cb.addMethod(Opcodes.ACC_PUBLIC, method.getJavaName(),
MultiFunction mfun2;
try {
mfun2 = Types.matchFunction(Types.removeForAll(function.getType()), parameters.length);
- //System.out.println("Implementation types: " + Arrays.toString(functionTypes));
+ //LOGGER.info("Implementation types: " + Arrays.toString(functionTypes));
} catch (MatchException e) {
throw new InternalCompilerError(e);
}