]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/CodeGeneration.java
SCL compiler generates line numbers to bytecode
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / compilation / CodeGeneration.java
index 470b8e9f0701c0418fb08bf02757e0b4717616d5..082fa2f6dc553d1b36e2b64c6152329f46b87725 100644 (file)
@@ -17,6 +17,7 @@ import org.simantics.scl.compiler.constants.ThisConstant;
 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;
@@ -25,8 +26,8 @@ import org.simantics.scl.compiler.elaboration.modules.SCLValueProperty;
 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;
@@ -37,7 +38,6 @@ import org.simantics.scl.compiler.internal.codegen.utils.ClassBuilder;
 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;
@@ -61,10 +61,8 @@ public class CodeGeneration {
     
     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;
@@ -75,28 +73,22 @@ public class CodeGeneration {
     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()]);
         
@@ -116,7 +108,7 @@ public class CodeGeneration {
     }
     
     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();
@@ -124,8 +116,6 @@ public class CodeGeneration {
                 continue;
 
             Name name = value.getName();
-            DecomposedExpression decomposed = 
-                    DecomposedExpression.decompose(expression);
             
             SCLConstant constant = new SCLConstant(name, value.getType());
             value.setValue(constant);            
@@ -135,13 +125,18 @@ public class CodeGeneration {
                     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
@@ -165,7 +160,7 @@ public class CodeGeneration {
                     continue;
      
                 DecomposedExpression decomposed = 
-                        DecomposedExpression.decompose(expression);
+                        DecomposedExpression.decompose(errorLog, expression);
     
                 CodeWriter w = mw.createFunction((SCLConstant)value.getValue(),
                         decomposed.typeParameters,
@@ -177,9 +172,12 @@ public class CodeGeneration {
                 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;
             }
         }
@@ -191,7 +189,7 @@ public class CodeGeneration {
     }
     
     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);            
         }
@@ -199,7 +197,7 @@ public class CodeGeneration {
         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);        
@@ -207,7 +205,7 @@ public class CodeGeneration {
             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);            
         }
@@ -219,7 +217,7 @@ public class CodeGeneration {
     }
     
     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);
         }
@@ -247,7 +245,7 @@ public class CodeGeneration {
                     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);
                 }
@@ -274,7 +272,7 @@ public class CodeGeneration {
                     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);
                 }
@@ -379,7 +377,7 @@ public class CodeGeneration {
                 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;