Browse Source

- Changed the way static is determined in an expression writer

- Minor code improvements; make better use of JavaMethodInfo
Stan Hebben 6 years ago
parent
commit
3ef3a8d6cf

+ 10
- 10
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.javabytecode;
6
 package org.openzen.zenscript.javabytecode;
7
 
7
 
8
-import java.util.ArrayList;
9
 import java.util.HashMap;
8
 import java.util.HashMap;
10
-import java.util.List;
11
 import java.util.Map;
9
 import java.util.Map;
12
 
10
 
13
 import org.objectweb.asm.ClassWriter;
11
 import org.objectweb.asm.ClassWriter;
106
 		implement(INT_TO_FLOAT, JavaWriter::i2f);
104
 		implement(INT_TO_FLOAT, JavaWriter::i2f);
107
 		implement(INT_TO_DOUBLE, JavaWriter::i2d);
105
 		implement(INT_TO_DOUBLE, JavaWriter::i2d);
108
 		implement(INT_TO_CHAR, JavaWriter::i2s);
106
 		implement(INT_TO_CHAR, JavaWriter::i2s);
109
-		INT_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jInteger, "toString", "(I)Ljava/lang/String;", true));
107
+		INT_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jInteger, "toString", "(I)Ljava/lang/String;", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC));
110
 		
108
 		
111
 		implement(LONG_TO_BYTE, writer -> { writer.l2i(); writer.i2b(); });
109
 		implement(LONG_TO_BYTE, writer -> { writer.l2i(); writer.i2b(); });
112
 		implement(LONG_TO_SBYTE, writer -> { writer.l2i(); writer.i2b(); });
110
 		implement(LONG_TO_SBYTE, writer -> { writer.l2i(); writer.i2b(); });
118
 		implement(LONG_TO_FLOAT, JavaWriter::l2f);
116
 		implement(LONG_TO_FLOAT, JavaWriter::l2f);
119
 		implement(LONG_TO_DOUBLE, JavaWriter::l2d);
117
 		implement(LONG_TO_DOUBLE, JavaWriter::l2d);
120
 		implement(LONG_TO_CHAR, writer -> { writer.l2i(); writer.i2s(); });
118
 		implement(LONG_TO_CHAR, writer -> { writer.l2i(); writer.i2s(); });
121
-		LONG_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jLong, "toString", "(J)Ljava/lang/String;", true));
119
+		LONG_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jLong, "toString", "(J)Ljava/lang/String;", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC));
122
 		
120
 		
123
-		FLOAT_BITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jFloat, "floatToRawIntBits", "(F)I", true));
124
-		DOUBLE_BITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jDouble, "doubleToRawLongBits", "(D)J", true));
125
-		FLOAT_FROMBITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jFloat, "intBitsToFloat", "(I)F", true));
126
-		DOUBLE_FROMBITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jDouble, "longBitsToDouble", "(J)D", true));
121
+		FLOAT_BITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jFloat, "floatToRawIntBits", "(F)I", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC));
122
+		DOUBLE_BITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jDouble, "doubleToRawLongBits", "(D)J", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC));
123
+		FLOAT_FROMBITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jFloat, "intBitsToFloat", "(I)F", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC));
124
+		DOUBLE_FROMBITS.setTag(JavaMethodInfo.class, new JavaMethodInfo(jDouble, "longBitsToDouble", "(J)D", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC));
127
 	}
125
 	}
128
 	
126
 	
129
 	private static void implement(DefinitionMember member, JavaBytecodeImplementation implementation) {
127
 	private static void implement(DefinitionMember member, JavaBytecodeImplementation implementation) {
184
 		// (TODO: can we break very long scripts into smaller methods? for the extreme scripts)
182
 		// (TODO: can we break very long scripts into smaller methods? for the extreme scripts)
185
 		final JavaClassWriter visitor = scriptBlocks.get(methodName);
183
 		final JavaClassWriter visitor = scriptBlocks.get(methodName);
186
 		visitor.hasRun = true;
184
 		visitor.hasRun = true;
187
-		final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(new JavaWriter(visitor, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "run", "()V", null, null));
185
+		JavaMethodInfo method = new JavaMethodInfo(new JavaClassInfo(methodName), "run", "()V", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);
186
+		final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(new JavaWriter(visitor, method, null, null));
188
 		statementVisitor.start();
187
 		statementVisitor.start();
189
 		for (Statement statement : script.statements) {
188
 		for (Statement statement : script.statements) {
190
 			statement.accept(statementVisitor);
189
 			statement.accept(statementVisitor);
198
 			throw new IllegalStateException("Already finished!");
197
 			throw new IllegalStateException("Already finished!");
199
 		finished = true;
198
 		finished = true;
200
 		
199
 		
201
-		final JavaWriter runWriter = new JavaWriter(scriptsClassWriter, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "run", "()V", null, null);
200
+		JavaMethodInfo runMethod = new JavaMethodInfo(new JavaClassInfo("Scripts"), "run", "()V", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC);
201
+		final JavaWriter runWriter = new JavaWriter(scriptsClassWriter, runMethod, null, null);
202
 		runWriter.start();
202
 		runWriter.start();
203
 		for (Map.Entry<String, JavaClassWriter> entry : scriptBlocks.entrySet()) {
203
 		for (Map.Entry<String, JavaClassWriter> entry : scriptBlocks.entrySet()) {
204
 			final String owner = entry.getKey();
204
 			final String owner = entry.getKey();

+ 11
- 9
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaMethodInfo.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.javabytecode;
6
 package org.openzen.zenscript.javabytecode;
7
 
7
 
8
+import org.objectweb.asm.Opcodes;
9
+
8
 /**
10
 /**
9
  *
11
  *
10
  * @author Hoofdgebruiker
12
  * @author Hoofdgebruiker
12
 public class JavaMethodInfo {
14
 public class JavaMethodInfo {
13
 	public final JavaClassInfo javaClass;
15
 	public final JavaClassInfo javaClass;
14
 	public final String name;
16
 	public final String name;
15
-	public final String signature;
16
-	public final boolean isStatic;
17
-	
18
-	public JavaMethodInfo(JavaClassInfo javaClass, String name, String signature) {
19
-		this(javaClass, name, signature, false);
20
-	}
17
+	public final String descriptor;
18
+	public final int modifiers;
21
 	
19
 	
22
-	public JavaMethodInfo(JavaClassInfo javaClass, String name, String signature, boolean isStatic) {
20
+	public JavaMethodInfo(JavaClassInfo javaClass, String name, String signature, int modifiers) {
23
 		this.javaClass = javaClass;
21
 		this.javaClass = javaClass;
24
 		this.name = name;
22
 		this.name = name;
25
-		this.signature = signature;
26
-		this.isStatic = isStatic;
23
+		this.descriptor = signature;
24
+		this.modifiers = modifiers;
25
+	}
26
+	
27
+	public boolean isStatic() {
28
+		return (modifiers & Opcodes.ACC_STATIC) > 0;
27
 	}
29
 	}
28
 }
30
 }

+ 9
- 24
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

16
 
16
 
17
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
17
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
18
     private final JavaWriter javaWriter;
18
     private final JavaWriter javaWriter;
19
-    private final boolean isInit;
20
-
21
-
22
-    public JavaExpressionVisitor(final JavaWriter javaWriter) {
23
-        this(javaWriter, false);
24
-    }
25
-
26
-    public JavaExpressionVisitor(JavaWriter javaWriter, boolean isInit) {
19
+	
20
+    public JavaExpressionVisitor(JavaWriter javaWriter) {
27
         this.javaWriter = javaWriter;
21
         this.javaWriter = javaWriter;
28
-        this.isInit = isInit;
29
     }
22
     }
30
 
23
 
31
     private static Class<?> getForEquals(ITypeID id) {
24
     private static Class<?> getForEquals(ITypeID id) {
104
         }
97
         }
105
         //TODO: Test with actual static method
98
         //TODO: Test with actual static method
106
         final JavaMethodInfo info = expression.member.getTag(JavaMethodInfo.class);
99
         final JavaMethodInfo info = expression.member.getTag(JavaMethodInfo.class);
107
-        javaWriter.invokeStatic(info.javaClass.internalClassName, info.name, info.signature);
100
+        javaWriter.invokeStatic(info.javaClass.internalClassName, info.name, info.descriptor);
108
         return null;
101
         return null;
109
     }
102
     }
110
 
103
 
459
     @Override
452
     @Override
460
     public Void visitSetField(SetFieldExpression expression) {
453
     public Void visitSetField(SetFieldExpression expression) {
461
         javaWriter.loadObject(0);
454
         javaWriter.loadObject(0);
462
-        if (expression.field.isFinal() && !isInit)
463
-            throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final field!");
464
         expression.value.accept(this);
455
         expression.value.accept(this);
465
         if (!checkAndPutFieldInfo(expression.field, false))
456
         if (!checkAndPutFieldInfo(expression.field, false))
466
             throw new IllegalStateException("Missing field info on a field member!");
457
             throw new IllegalStateException("Missing field info on a field member!");
469
 
460
 
470
     @Override
461
     @Override
471
     public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
462
     public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
472
-        //TODO is static?
473
-        final boolean isStatic = false;
474
         expression.value.accept(this);
463
         expression.value.accept(this);
475
-        javaWriter.store(expression.type.accept(JavaTypeVisitor.INSTANCE), isStatic ? expression.parameter.index : expression.parameter.index + 1);
464
+        javaWriter.store(expression.type.accept(JavaTypeVisitor.INSTANCE), javaWriter.method.isStatic() ? expression.parameter.index : expression.parameter.index + 1);
476
         return null;
465
         return null;
477
     }
466
     }
478
 
467
 
479
     @Override
468
     @Override
480
     public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
469
     public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
481
-        if (expression.variable.isFinal)
482
-            throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final variable!");
483
         expression.value.accept(this);
470
         expression.value.accept(this);
484
         Label label = new Label();
471
         Label label = new Label();
485
         javaWriter.label(label);
472
         javaWriter.label(label);
549
         JavaMethodInfo methodInfo = member.getTag(JavaMethodInfo.class);
536
         JavaMethodInfo methodInfo = member.getTag(JavaMethodInfo.class);
550
         if (methodInfo == null)
537
         if (methodInfo == null)
551
             return false;
538
             return false;
552
-        if (methodInfo.isStatic) {
553
-            getJavaWriter().invokeStatic(
554
-                    methodInfo.javaClass.internalClassName,
539
+        if (methodInfo.isStatic()) {
540
+            getJavaWriter().invokeStatic(methodInfo.javaClass.internalClassName,
555
                     methodInfo.name,
541
                     methodInfo.name,
556
-                    methodInfo.signature);
542
+                    methodInfo.descriptor);
557
         } else {
543
         } else {
558
-            getJavaWriter().invokeVirtual(
559
-                    methodInfo.javaClass.internalClassName,
544
+            getJavaWriter().invokeVirtual(methodInfo.javaClass.internalClassName,
560
                     methodInfo.name,
545
                     methodInfo.name,
561
-                    methodInfo.signature);
546
+                    methodInfo.descriptor);
562
         }
547
         }
563
         return true;
548
         return true;
564
     }
549
     }

+ 3
- 10
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java View File

8
 public class JavaStatementVisitor implements StatementVisitor<Void> {
8
 public class JavaStatementVisitor implements StatementVisitor<Void> {
9
     private final JavaWriter javaWriter;
9
     private final JavaWriter javaWriter;
10
     public final JavaExpressionVisitor expressionVisitor;
10
     public final JavaExpressionVisitor expressionVisitor;
11
-    private final boolean isInit;
12
-
13
-    public JavaStatementVisitor(final JavaWriter javaWriter) {
14
-        this(javaWriter, false);
15
-    }
16
-
11
+	
17
     /**
12
     /**
18
      * @param javaWriter the method writer that compiles the statement
13
      * @param javaWriter the method writer that compiles the statement
19
-     * @param isInit is the method a class initializer
20
      */
14
      */
21
-    public JavaStatementVisitor(JavaWriter javaWriter, boolean isInit) {
15
+    public JavaStatementVisitor(JavaWriter javaWriter) {
22
         this.javaWriter = javaWriter;
16
         this.javaWriter = javaWriter;
23
-        this.expressionVisitor = new JavaExpressionVisitor(javaWriter, isInit);
24
-        this.isInit = isInit;
17
+        this.expressionVisitor = new JavaExpressionVisitor(javaWriter);
25
     }
18
     }
26
 
19
 
27
     @Override
20
     @Override

+ 10
- 5
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java View File

12
 import java.util.Map;
12
 import java.util.Map;
13
 
13
 
14
 import static org.objectweb.asm.Opcodes.*;
14
 import static org.objectweb.asm.Opcodes.*;
15
+import org.openzen.zenscript.javabytecode.JavaMethodInfo;
15
 
16
 
16
 public class JavaWriter {
17
 public class JavaWriter {
18
+	public final JavaMethodInfo method;
19
+	
17
     private final LocalVariablesSorter visitor;
20
     private final LocalVariablesSorter visitor;
18
     private final List<JavaLocalVariableInfo> localVariableInfos = new ArrayList<>();
21
     private final List<JavaLocalVariableInfo> localVariableInfos = new ArrayList<>();
19
     private boolean debug = false;
22
     private boolean debug = false;
21
     private int labelIndex = 1;
24
     private int labelIndex = 1;
22
     private Map<Label, String> labelNames = new HashMap<>();
25
     private Map<Label, String> labelNames = new HashMap<>();
23
 
26
 
24
-    public JavaWriter(ClassVisitor visitor, boolean nameVariables, int access, String name, String description, String signature, String[] exceptions, String... annotations) {
25
-        final MethodVisitor methodVisitor = visitor.visitMethod(access, name, description, signature, exceptions);
27
+    public JavaWriter(ClassVisitor visitor, boolean nameVariables, JavaMethodInfo method, String signature, String[] exceptions, String... annotations) {
28
+		this.method = method;
29
+		
30
+        final MethodVisitor methodVisitor = visitor.visitMethod(method.modifiers, method.name, method.descriptor, signature, exceptions);
26
 
31
 
27
         for (String annotation : annotations) {
32
         for (String annotation : annotations) {
28
             methodVisitor.visitAnnotation(annotation, true).visitEnd();
33
             methodVisitor.visitAnnotation(annotation, true).visitEnd();
29
         }
34
         }
30
 
35
 
31
-        this.visitor = new LocalVariablesSorter(access, description, methodVisitor);
36
+        this.visitor = new LocalVariablesSorter(method.modifiers, method.descriptor, methodVisitor);
32
         this.nameVariables = nameVariables;
37
         this.nameVariables = nameVariables;
33
     }
38
     }
34
 
39
 
35
-    public JavaWriter(ClassVisitor visitor, int access, String name, String description, String signature, String[] exceptions, String... annotations) {
36
-        this(visitor, true, access, name, description, signature, exceptions, annotations);
40
+    public JavaWriter(ClassVisitor visitor, JavaMethodInfo method, String signature, String[] exceptions, String... annotations) {
41
+        this(visitor, true, method, signature, exceptions, annotations);
37
     }
42
     }
38
 
43
 
39
     private static String signature(Class aClass) {
44
     private static String signature(Class aClass) {

+ 20
- 19
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaDefinitionVisitor.java View File

7
 import org.openzen.zenscript.codemodel.definition.*;
7
 import org.openzen.zenscript.codemodel.definition.*;
8
 import org.openzen.zenscript.codemodel.expression.Expression;
8
 import org.openzen.zenscript.codemodel.expression.Expression;
9
 import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
9
 import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
10
-import org.openzen.zenscript.codemodel.generic.TypeParameter;
11
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
10
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
12
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
11
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
13
 import org.openzen.zenscript.codemodel.member.FieldMember;
12
 import org.openzen.zenscript.codemodel.member.FieldMember;
20
 import org.openzen.zenscript.javabytecode.JavaClassInfo;
19
 import org.openzen.zenscript.javabytecode.JavaClassInfo;
21
 import org.openzen.zenscript.javabytecode.JavaEnumInfo;
20
 import org.openzen.zenscript.javabytecode.JavaEnumInfo;
22
 import org.openzen.zenscript.javabytecode.JavaMethodInfo;
21
 import org.openzen.zenscript.javabytecode.JavaMethodInfo;
23
-import org.openzen.zenscript.javabytecode.TestIsStaticInfo;
24
 import org.openzen.zenscript.javabytecode.compiler.*;
22
 import org.openzen.zenscript.javabytecode.compiler.*;
25
 
23
 
26
 import java.util.Iterator;
24
 import java.util.Iterator;
45
         else
43
         else
46
             superType = Type.getType(definition.superType.accept(JavaTypeClassVisitor.INSTANCE));
44
             superType = Type.getType(definition.superType.accept(JavaTypeClassVisitor.INSTANCE));
47
 
45
 
46
+		JavaClassInfo toClass = new JavaClassInfo(definition.name);
48
         JavaClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
47
         JavaClassWriter writer = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
49
 
48
 
50
         //TODO: Calculate signature from generic parameters
49
         //TODO: Calculate signature from generic parameters
56
         for (IDefinitionMember member : definition.members) {
55
         for (IDefinitionMember member : definition.members) {
57
             if (member instanceof ConstructorMember) {
56
             if (member instanceof ConstructorMember) {
58
                 final ConstructorMember constructorMember = (ConstructorMember) member;
57
                 final ConstructorMember constructorMember = (ConstructorMember) member;
59
-
60
-
61
                 constructorMember.body.add(0, new ExpressionStatement(constructorMember.position, new Expression(constructorMember.position, BasicTypeID.VOID) {
58
                 constructorMember.body.add(0, new ExpressionStatement(constructorMember.position, new Expression(constructorMember.position, BasicTypeID.VOID) {
62
                     @Override
59
                     @Override
63
                     public <T> T accept(ExpressionVisitor<T> visitor) {
60
                     public <T> T accept(ExpressionVisitor<T> visitor) {
79
                     }
76
                     }
80
                 }));
77
                 }));
81
             }
78
             }
82
-            member.accept(new JavaMemberVisitor(writer, definition.name));
83
-
84
-
79
+			
80
+            member.accept(new JavaMemberVisitor(writer, toClass));
85
         }
81
         }
86
         writer.visitEnd();
82
         writer.visitEnd();
87
         return writer.toByteArray();
83
         return writer.toByteArray();
89
 
85
 
90
     @Override
86
     @Override
91
     public byte[] visitInterface(InterfaceDefinition definition) {
87
     public byte[] visitInterface(InterfaceDefinition definition) {
88
+		JavaClassInfo toClass = new JavaClassInfo(definition.name);
92
         ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
89
         ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
93
 
90
 
94
         //TODO: Calculate signature from generic parameters
91
         //TODO: Calculate signature from generic parameters
96
         String signature = null;
93
         String signature = null;
97
         writer.visit(Opcodes.V1_8, definition.modifiers | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT, definition.name, signature, Type.getInternalName(Object.class), null);
94
         writer.visit(Opcodes.V1_8, definition.modifiers | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT, definition.name, signature, Type.getInternalName(Object.class), null);
98
         for (IDefinitionMember member : definition.members) {
95
         for (IDefinitionMember member : definition.members) {
99
-            member.accept(new JavaMemberVisitor(writer, definition.name));
96
+            member.accept(new JavaMemberVisitor(writer, toClass));
100
         }
97
         }
101
         writer.visitEnd();
98
         writer.visitEnd();
102
         return writer.toByteArray();
99
         return writer.toByteArray();
123
             }
120
             }
124
         }
121
         }
125
 
122
 
126
-        final JavaWriter clinitWriter = new JavaWriter(writer, true, Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
127
-        final JavaStatementVisitor clinitVisitor = new JavaStatementVisitor(clinitWriter, true);
123
+		JavaClassInfo toClass = new JavaClassInfo(definition.name);
124
+		JavaMethodInfo clinitInfo = new JavaMethodInfo(toClass, "<clinit>", "()V", Opcodes.ACC_STATIC);
125
+        final JavaWriter clinitWriter = new JavaWriter(writer, clinitInfo, null, null);
126
+        final JavaStatementVisitor clinitVisitor = new JavaStatementVisitor(clinitWriter);
128
         clinitVisitor.start();
127
         clinitVisitor.start();
129
         int constantCount = 0;
128
         int constantCount = 0;
130
         for (IDefinitionMember member : definition.members) {
129
         for (IDefinitionMember member : definition.members) {
151
                 ++constantCount;
150
                 ++constantCount;
152
                 member.setTag(JavaEnumInfo.class, new JavaEnumInfo(clinitVisitor));
151
                 member.setTag(JavaEnumInfo.class, new JavaEnumInfo(clinitVisitor));
153
             }
152
             }
154
-            member.accept(new JavaMemberVisitor(writer, definition.name));
153
+            member.accept(new JavaMemberVisitor(writer, toClass));
155
         }
154
         }
156
 
155
 
157
         clinitWriter.constant(constantCount);
156
         clinitWriter.constant(constantCount);
171
         //Enum Stuff(required!)
170
         //Enum Stuff(required!)
172
         writer.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC, "$VALUES", "[L" + definition.name + ";", null, null).visitEnd();
171
         writer.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC, "$VALUES", "[L" + definition.name + ";", null, null).visitEnd();
173
 
172
 
174
-        JavaWriter valuesWriter = new JavaWriter(writer, true, Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "values", "()[L" + definition.name + ";", null, null);
173
+		JavaMethodInfo valuesMethodInfo = new JavaMethodInfo(toClass, "values", "()[L" + definition.name + ";", Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC);
174
+        JavaWriter valuesWriter = new JavaWriter(writer, true, valuesMethodInfo, null, null);
175
         valuesWriter.start();
175
         valuesWriter.start();
176
         valuesWriter.getStaticField(definition.name, "$VALUES", "[L" + definition.name + ";");
176
         valuesWriter.getStaticField(definition.name, "$VALUES", "[L" + definition.name + ";");
177
         valuesWriter.invokeVirtual("[L" + definition.name + ";", "clone", "()Ljava/lang/Object;");
177
         valuesWriter.invokeVirtual("[L" + definition.name + ";", "clone", "()Ljava/lang/Object;");
179
         valuesWriter.returnObject();
179
         valuesWriter.returnObject();
180
         valuesWriter.end();
180
         valuesWriter.end();
181
 
181
 
182
-
183
-        JavaWriter valueOfWriter = new JavaWriter(writer, true, Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "valueOf", "(Ljava/lang/String;)L" + definition.name + ";", null, null);
182
+		JavaMethodInfo valueOfMethodInfo = new JavaMethodInfo(toClass, "valueOf", "(Ljava/lang/String;)L" + definition.name + ";", Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC);
183
+        JavaWriter valueOfWriter = new JavaWriter(writer, true, valueOfMethodInfo, null, null);
184
         valueOfWriter.start();
184
         valueOfWriter.start();
185
         valueOfWriter.invokeStatic("java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
185
         valueOfWriter.invokeStatic("java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
186
         valueOfWriter.loadObject(0);
186
         valueOfWriter.loadObject(0);
202
     @Override
202
     @Override
203
     public byte[] visitFunction(FunctionDefinition definition) {
203
     public byte[] visitFunction(FunctionDefinition definition) {
204
         final String signature = CompilerUtils.calcSign(definition.header, false);
204
         final String signature = CompilerUtils.calcSign(definition.header, false);
205
-        final JavaWriter writer = new JavaWriter(outerWriter, true, CompilerUtils.calcAccess(definition.modifiers) | Opcodes.ACC_STATIC, definition.name, CompilerUtils.calcDesc(definition.header, false), signature, null);
205
+		
206
+		final JavaClassInfo toClass = new JavaClassInfo(CompilerUtils.calcClasName(definition.position));
207
+		final JavaMethodInfo methodInfo = new JavaMethodInfo(toClass, definition.name, CompilerUtils.calcDesc(definition.header, false), CompilerUtils.calcAccess(definition.modifiers) | Opcodes.ACC_STATIC);
208
+
209
+		final JavaWriter writer = new JavaWriter(outerWriter, true, methodInfo, signature, null);
206
         final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(writer);
210
         final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(writer);
207
         statementVisitor.start();
211
         statementVisitor.start();
208
         final Iterator<Statement> statementIterator = definition.statements.iterator();
212
         final Iterator<Statement> statementIterator = definition.statements.iterator();
222
                     writer.aConstNull();
226
                     writer.aConstNull();
223
                 writer.returnType(type.accept(JavaTypeVisitor.INSTANCE));
227
                 writer.returnType(type.accept(JavaTypeVisitor.INSTANCE));
224
             }
228
             }
225
-
226
         }
229
         }
227
 
230
 
228
         statementVisitor.end();
231
         statementVisitor.end();
229
-
230
-        final JavaMethodInfo methodInfo = new JavaMethodInfo(new JavaClassInfo(CompilerUtils.calcClasName(definition.position)), definition.name, signature, true);
231
-
232
+		
232
         definition.setTag(JavaMethodInfo.class, methodInfo);
233
         definition.setTag(JavaMethodInfo.class, methodInfo);
233
         definition.caller.setTag(JavaMethodInfo.class, methodInfo);
234
         definition.caller.setTag(JavaMethodInfo.class, methodInfo);
234
         return null;
235
         return null;

+ 22
- 13
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaMemberVisitor.java View File

6
 import org.objectweb.asm.Type;
6
 import org.objectweb.asm.Type;
7
 import org.openzen.zenscript.codemodel.FunctionParameter;
7
 import org.openzen.zenscript.codemodel.FunctionParameter;
8
 import org.openzen.zenscript.codemodel.Modifiers;
8
 import org.openzen.zenscript.codemodel.Modifiers;
9
-import org.openzen.zenscript.codemodel.expression.ConstructorThisCallExpression;
10
 import org.openzen.zenscript.codemodel.expression.Expression;
9
 import org.openzen.zenscript.codemodel.expression.Expression;
11
 import org.openzen.zenscript.codemodel.member.*;
10
 import org.openzen.zenscript.codemodel.member.*;
12
-import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
13
 import org.openzen.zenscript.codemodel.statement.Statement;
11
 import org.openzen.zenscript.codemodel.statement.Statement;
14
 import org.openzen.zenscript.javabytecode.JavaClassInfo;
12
 import org.openzen.zenscript.javabytecode.JavaClassInfo;
15
 import org.openzen.zenscript.javabytecode.JavaEnumInfo;
13
 import org.openzen.zenscript.javabytecode.JavaEnumInfo;
20
 public class JavaMemberVisitor implements MemberVisitor<Void> {
18
 public class JavaMemberVisitor implements MemberVisitor<Void> {
21
 
19
 
22
     private final ClassWriter writer;
20
     private final ClassWriter writer;
23
-    private final String className;
21
+	private final JavaClassInfo toClass;
24
 
22
 
25
-    public JavaMemberVisitor(ClassWriter writer, String className) {
23
+    public JavaMemberVisitor(ClassWriter writer, JavaClassInfo toClass) {
26
         this.writer = writer;
24
         this.writer = writer;
27
-        this.className = className;
25
+		this.toClass = toClass;
28
     }
26
     }
29
 
27
 
30
     @Override
28
     @Override
34
         String signature = null;
32
         String signature = null;
35
         final String descriptor = Type.getDescriptor(member.type.accept(JavaTypeClassVisitor.INSTANCE));
33
         final String descriptor = Type.getDescriptor(member.type.accept(JavaTypeClassVisitor.INSTANCE));
36
         writer.visitField(member.modifiers, member.name, descriptor, signature, null).visitEnd();
34
         writer.visitField(member.modifiers, member.name, descriptor, signature, null).visitEnd();
37
-        member.setTag(JavaFieldInfo.class, new JavaFieldInfo(new JavaClassInfo(className), member.name, descriptor));
35
+        member.setTag(JavaFieldInfo.class, new JavaFieldInfo(toClass, member.name, descriptor));
38
         return null;
36
         return null;
39
     }
37
     }
40
 
38
 
41
     @Override
39
     @Override
42
     public Void visitConstructor(ConstructorMember member) {
40
     public Void visitConstructor(ConstructorMember member) {
41
+        final boolean isEnum = member.hasTag(JavaEnumInfo.class);
42
+		String descriptor = CompilerUtils.calcDesc(member.header, isEnum);
43
+		final JavaMethodInfo method = new JavaMethodInfo(toClass, "<init>", descriptor, isEnum ? Opcodes.ACC_PRIVATE : CompilerUtils.calcAccess(member.modifiers));
44
+		
43
         final Label constructorStart = new Label();
45
         final Label constructorStart = new Label();
44
         final Label constructorEnd = new Label();
46
         final Label constructorEnd = new Label();
45
-        final boolean isEnum = member.hasTag(JavaEnumInfo.class);
46
-        final JavaWriter constructorWriter = new JavaWriter(writer, isEnum ? Opcodes.ACC_PRIVATE : member.modifiers, "<init>", CompilerUtils.calcDesc(member.header, isEnum), CompilerUtils.calcSign(member.header, isEnum), null);
47
+        final JavaWriter constructorWriter = new JavaWriter(writer, method, CompilerUtils.calcSign(member.header, isEnum), null);
47
         constructorWriter.label(constructorStart);
48
         constructorWriter.label(constructorStart);
48
         for (FunctionParameter parameter : member.header.parameters) {
49
         for (FunctionParameter parameter : member.header.parameters) {
49
             if(isEnum)
50
             if(isEnum)
50
                 parameter.index += 2;
51
                 parameter.index += 2;
51
             constructorWriter.nameVariable(parameter.index + 1, parameter.name, constructorStart, constructorEnd, Type.getType(parameter.type.accept(JavaTypeClassVisitor.INSTANCE)));
52
             constructorWriter.nameVariable(parameter.index + 1, parameter.name, constructorStart, constructorEnd, Type.getType(parameter.type.accept(JavaTypeClassVisitor.INSTANCE)));
52
         }
53
         }
53
-        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(constructorWriter, true);
54
+        final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(constructorWriter);
54
         statementVisitor.start();
55
         statementVisitor.start();
55
 
56
 
56
         for (Statement statement : member.body) {
57
         for (Statement statement : member.body) {
63
 
64
 
64
     @Override
65
     @Override
65
     public Void visitMethod(MethodMember member) {
66
     public Void visitMethod(MethodMember member) {
67
+        final boolean isAbstract = member.body == null || member.body.isEmpty() || Modifiers.isAbstract(member.modifiers);
68
+		int modifiers = (isAbstract ? Opcodes.ACC_ABSTRACT : 0)
69
+				| (member.isStatic() ? Opcodes.ACC_STATIC : 0)
70
+				| CompilerUtils.calcAccess(member.modifiers);
71
+		final JavaMethodInfo method = new JavaMethodInfo(
72
+				toClass,
73
+				member.name,
74
+				CompilerUtils.calcSign(member.header, false),
75
+				modifiers);
76
+		
66
         final Label methodStart = new Label();
77
         final Label methodStart = new Label();
67
         final Label methodEnd = new Label();
78
         final Label methodEnd = new Label();
68
-        final boolean isAbstract = member.body == null || member.body.isEmpty() || Modifiers.isAbstract(member.modifiers);
69
-        final JavaWriter methodWriter = new JavaWriter(writer, isAbstract ? member.modifiers | Opcodes.ACC_ABSTRACT : member.modifiers, member.name, CompilerUtils.calcDesc(member.header, false), CompilerUtils.calcSign(member.header, false), null);
79
+        final JavaWriter methodWriter = new JavaWriter(writer, method, CompilerUtils.calcSign(member.header, false), null);
70
         methodWriter.label(methodStart);
80
         methodWriter.label(methodStart);
71
         for (final FunctionParameter parameter : member.header.parameters) {
81
         for (final FunctionParameter parameter : member.header.parameters) {
72
             methodWriter.nameParameter(0, parameter.name);
82
             methodWriter.nameParameter(0, parameter.name);
86
             statementVisitor.end();
96
             statementVisitor.end();
87
         }
97
         }
88
 
98
 
89
-        member.setTag(JavaMethodInfo.class, new JavaMethodInfo(new JavaClassInfo(className), member.name, CompilerUtils.calcSign(member.header, false), member.isStatic()));
90
-
99
+        member.setTag(JavaMethodInfo.class, method);
91
         return null;
100
         return null;
92
     }
101
     }
93
 
102
 

+ 3
- 2
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java View File

10
 import java.util.HashMap;
10
 import java.util.HashMap;
11
 import java.util.List;
11
 import java.util.List;
12
 import java.util.Map;
12
 import java.util.Map;
13
+import org.objectweb.asm.Opcodes;
13
 import org.openzen.zenscript.codemodel.FunctionHeader;
14
 import org.openzen.zenscript.codemodel.FunctionHeader;
14
 import org.openzen.zenscript.codemodel.FunctionParameter;
15
 import org.openzen.zenscript.codemodel.FunctionParameter;
15
 import org.openzen.zenscript.codemodel.Modifiers;
16
 import org.openzen.zenscript.codemodel.Modifiers;
46
 	
47
 	
47
 	public GlobalRegistry(ZSPackage globals) {
48
 	public GlobalRegistry(ZSPackage globals) {
48
 		JavaClassInfo jPrintStream = new JavaClassInfo("java/io/PrintStream");
49
 		JavaClassInfo jPrintStream = new JavaClassInfo("java/io/PrintStream");
49
-		PRINTSTREAM_PRINTLN.setTag(JavaMethodInfo.class, new JavaMethodInfo(jPrintStream, "println", "(Ljava/lang/String;)V"));
50
+		PRINTSTREAM_PRINTLN.setTag(JavaMethodInfo.class, new JavaMethodInfo(jPrintStream, "println", "(Ljava/lang/String;)V", Opcodes.ACC_PUBLIC));
50
 		
51
 		
51
 		JavaClassInfo jSystem = new JavaClassInfo("java/lang/System");
52
 		JavaClassInfo jSystem = new JavaClassInfo("java/lang/System");
52
 		SYSTEM_OUT.setTag(JavaFieldInfo.class, new JavaFieldInfo(jSystem, "out", "Ljava/io/PrintStream;"));
53
 		SYSTEM_OUT.setTag(JavaFieldInfo.class, new JavaFieldInfo(jSystem, "out", "Ljava/io/PrintStream;"));
68
 			JavaClassInfo myClassInfo = new JavaClassInfo("my/test/MyClass");
69
 			JavaClassInfo myClassInfo = new JavaClassInfo("my/test/MyClass");
69
 			
70
 			
70
 			MethodMember member = new MethodMember(CodePosition.NATIVE, myClassDefinition, Modifiers.PUBLIC, "test", new FunctionHeader(BasicTypeID.STRING));
71
 			MethodMember member = new MethodMember(CodePosition.NATIVE, myClassDefinition, Modifiers.PUBLIC, "test", new FunctionHeader(BasicTypeID.STRING));
71
-			member.setTag(JavaMethodInfo.class, new JavaMethodInfo(myClassInfo, "test", "()Ljava/lang/String;"));
72
+			member.setTag(JavaMethodInfo.class, new JavaMethodInfo(myClassInfo, "test", "()Ljava/lang/String;", Opcodes.ACC_PUBLIC));
72
 			myClassDefinition.addMember(member);
73
 			myClassDefinition.addMember(member);
73
 			
74
 			
74
 			packageMyPackage.register(myClassDefinition);
75
 			packageMyPackage.register(myClassDefinition);

Loading…
Cancel
Save