Browse Source

Added unboxingTypeVisitor and fixed primitives in Iterators

kindlich 5 years ago
parent
commit
8f71b442fc
No known key found for this signature in database

+ 1
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/CompilerUtils.java View File

@@ -28,7 +28,7 @@ public class CompilerUtils {
28 28
 	}
29 29
 
30 30
 	public static boolean isLarge(StoredType type) {
31
-		return type.type == BasicTypeID.DOUBLE || type.type == BasicTypeID.DOUBLE;
31
+		return type.type == BasicTypeID.DOUBLE || type.type == BasicTypeID.LONG;
32 32
 	}
33 33
 	
34 34
 	public static int calcAccess(int modifiers) {

+ 0
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaBoxingTypeVisitor.java View File

@@ -60,7 +60,6 @@ public class JavaBoxingTypeVisitor implements TypeVisitorWithContext<StoredType,
60 60
 			default:
61 61
 				return null;
62 62
 		}
63
-		writer.dup();
64 63
 		
65 64
 		if (method != null)
66 65
 			writer.invokeStatic(method);

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

@@ -155,16 +155,20 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
155 155
 	private static final JavaMethod SHARED_ADDREF = JavaMethod.getNativeVirtual(JavaClass.SHARED, "addRef", "()V");
156 156
 	private static final JavaMethod SHARED_RELEASE = JavaMethod.getNativeVirtual(JavaClass.SHARED, "release", "()V");
157 157
 
158
-	protected final JavaWriter javaWriter;
159
-	private final JavaCapturedExpressionVisitor capturedExpressionVisitor = new JavaCapturedExpressionVisitor(this);
160
-	private final JavaBytecodeContext context;
161
-	private final JavaCompiledModule module;
158
+	final JavaWriter javaWriter;
159
+    private final JavaBoxingTypeVisitor boxingTypeVisitor;
160
+    private final JavaUnboxingTypeVisitor unboxingTypeVisitor;
161
+    private final JavaCapturedExpressionVisitor capturedExpressionVisitor = new JavaCapturedExpressionVisitor(this);
162
+    private final JavaBytecodeContext context;
163
+    private final JavaCompiledModule module;
162 164
 
163 165
 	public JavaExpressionVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaWriter javaWriter) {
164 166
 		this.javaWriter = javaWriter;
165 167
 		this.context = context;
166 168
 		this.module = module;
167
-	}
169
+        boxingTypeVisitor = new JavaBoxingTypeVisitor(javaWriter);
170
+        unboxingTypeVisitor = new JavaUnboxingTypeVisitor(javaWriter);
171
+    }
168 172
 
169 173
 	@Override
170 174
 	public Void visitAndAnd(AndAndExpression expression) {
@@ -1665,7 +1669,9 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
1665 1669
 		javaWriter.ifNonNull(end);
1666 1670
 		javaWriter.pop();
1667 1671
 		expression.right.accept(this);
1672
+		expression.right.type.type.accept(expression.right.type, boxingTypeVisitor);
1668 1673
 		javaWriter.label(end);
1674
+		expression.type.type.accept(expression.type, unboxingTypeVisitor);
1669 1675
 		return null;
1670 1676
 	}
1671 1677
 
@@ -2335,11 +2341,14 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
2335 2341
 		javaWriter.newObject("java/util/HashMap");
2336 2342
 		javaWriter.dup();
2337 2343
 		javaWriter.invokeSpecial("java/util/HashMap", "<init>", "()V");
2344
+        final AssocTypeID type = (AssocTypeID) expression.type.type;
2338 2345
 		for (int i = 0; i < expression.keys.length; i++) {
2339 2346
 			javaWriter.dup();
2340 2347
 			expression.keys[i].accept(this);
2341
-			expression.values[i].accept(this);
2342
-			javaWriter.invokeInterface(MAP_PUT);
2348
+            type.keyType.type.accept(type.keyType, boxingTypeVisitor);
2349
+            expression.values[i].accept(this);
2350
+            type.valueType.type.accept(type.valueType, boxingTypeVisitor);
2351
+            javaWriter.invokeInterface(MAP_PUT);
2343 2352
 			javaWriter.pop();
2344 2353
 		}
2345 2354
 		return null;
@@ -4142,7 +4151,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
4142 4151
 	public Void visitWrapOptional(WrapOptionalExpression expression) {
4143 4152
 		//Does nothing if not required to be wrapped
4144 4153
 		expression.value.accept(this);
4145
-		expression.value.type.type.accept(expression.value.type, new JavaBoxingTypeVisitor(javaWriter));
4154
+		expression.value.type.type.accept(expression.value.type, boxingTypeVisitor);
4146 4155
 		return null;
4147 4156
 	}
4148 4157
 

+ 39
- 14
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaForeachWriter.java View File

@@ -7,10 +7,11 @@ import org.openzen.zenscript.codemodel.statement.VarStatement;
7 7
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
8 8
 import org.openzen.zenscript.javashared.JavaClass;
9 9
 import org.openzen.zenscript.javashared.JavaMethod;
10
+import org.openzen.zenscript.javashared.JavaModifiers;
10 11
 
11 12
 import java.util.HashMap;
12 13
 import java.util.Map;
13
-
14
+@SuppressWarnings("Duplicates")
14 15
 public class JavaForeachWriter {
15 16
 
16 17
 	private final JavaWriter javaWriter;
@@ -19,6 +20,7 @@ public class JavaForeachWriter {
19 20
 	private final Label startLabel;
20 21
 	private final Label endLabel;
21 22
 	private final JavaStatementVisitor statementVisitor;
23
+	private final JavaUnboxingTypeVisitor unboxingTypeVisitor;
22 24
 
23 25
 	public JavaForeachWriter(JavaStatementVisitor statementVisitor, VarStatement[] variables, Statement content, Label start, Label end) {
24 26
 		this.statementVisitor = statementVisitor;
@@ -27,7 +29,8 @@ public class JavaForeachWriter {
27 29
 		this.content = content;
28 30
 		this.startLabel = start;
29 31
 		this.endLabel = end;
30
-	}
32
+        this.unboxingTypeVisitor = new JavaUnboxingTypeVisitor(this.javaWriter);
33
+    }
31 34
 
32 35
 	public void visitIntRange() {
33 36
 		javaWriter.dup();
@@ -55,8 +58,7 @@ public class JavaForeachWriter {
55 58
 	}
56 59
 
57 60
 	public void visitStringCharacterIterator() {
58
-		//TODO UNTESTED!
59
-		javaWriter.invokeSpecial("java/lang/String", "toCharArray()", "()[C");
61
+        javaWriter.invokeVirtual(JavaMethod.getVirtual(JavaClass.STRING, "toCharArray", "()[C", JavaModifiers.PUBLIC));
60 62
 		handleArray(javaWriter.local(int.class), javaWriter.getLocalVariable(variables[0].variable));
61 63
 	}
62 64
 
@@ -66,12 +68,12 @@ public class JavaForeachWriter {
66 68
 
67 69
 		javaWriter.label(startLabel);
68 70
 		javaWriter.dup();
69
-		javaWriter.dup();
70 71
 		javaWriter.arrayLength();
71
-		javaWriter.loadInt(z);
72
-
73
-		javaWriter.ifICmpLE(endLabel);
74
-		javaWriter.loadInt(z);
72
+        javaWriter.loadInt(z);
73
+        
74
+        javaWriter.ifICmpLE(endLabel);
75
+        javaWriter.dup();
76
+        javaWriter.loadInt(z);
75 77
 
76 78
 
77 79
 		javaWriter.arrayLoad(arrayTypeInfo.type);
@@ -85,7 +87,21 @@ public class JavaForeachWriter {
85 87
 	}
86 88
 
87 89
 	public void visitAssocKeyIterator() {
88
-		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
90
+	    javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.MAP, "keySet", "()Ljava/util/Set;", 0));
91
+        javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.COLLECTION, "iterator", "()Ljava/util/Iterator;", 0));
92
+        
93
+        javaWriter.label(startLabel);
94
+        javaWriter.dup();
95
+        javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.ITERATOR, "hasNext", "()Z", 0));
96
+        javaWriter.ifEQ(endLabel);
97
+        javaWriter.dup();
98
+        javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.ITERATOR, "next", "()Ljava/lang/Object;", 0));
99
+        
100
+        final JavaLocalVariableInfo keyVariable = javaWriter.getLocalVariable(variables[0].variable);
101
+        this.downCast(0, keyVariable.type);
102
+        javaWriter.store(keyVariable.type, keyVariable.local);
103
+        
104
+        content.accept(statementVisitor);
89 105
 	}
90 106
 
91 107
 	public void visitAssocKeyValueIterator() {
@@ -96,6 +112,7 @@ public class JavaForeachWriter {
96 112
 		javaWriter.dup();
97 113
 		javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.ITERATOR, "hasNext", "()Z", 0));
98 114
 		javaWriter.ifEQ(endLabel);
115
+		javaWriter.dup();
99 116
 		javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.ITERATOR, "next", "()Ljava/lang/Object;", 0));
100 117
 		javaWriter.checkCast(Type.getType(Map.Entry.class));
101 118
 		javaWriter.dup(false);
@@ -105,13 +122,21 @@ public class JavaForeachWriter {
105 122
 		final JavaLocalVariableInfo valueVariable = javaWriter.getLocalVariable(variables[1].variable);
106 123
 
107 124
 		javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.fromInternalName("java/util/Map$Entry", JavaClass.Kind.INTERFACE), "getKey", "()Ljava/lang/Object;", 0));
125
+		this.downCast(0, keyVariable.type);
108 126
 		javaWriter.store(keyVariable.type, keyVariable.local);
109
-
127
+		
110 128
 		javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.fromInternalName("java/util/Map$Entry", JavaClass.Kind.INTERFACE), "getValue", "()Ljava/lang/Object;", 0));
129
+        this.downCast(1, valueVariable.type);
111 130
 		javaWriter.store(valueVariable.type, valueVariable.local);
131
+		
112 132
 		content.accept(statementVisitor);
113
-
114
-
115
-		//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
116 133
 	}
134
+	
135
+	private void downCast(int typeNumber, Type t) {
136
+        if(CompilerUtils.isPrimitive(variables[typeNumber].type.type)) {
137
+            variables[typeNumber].type.type.accept(variables[typeNumber].type, unboxingTypeVisitor);
138
+        } else {
139
+            javaWriter.checkCast(t);
140
+        }
141
+    }
117 142
 }

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

@@ -148,6 +148,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
148 148
 		
149 149
 		javaWriter.goTo(start);
150 150
 		javaWriter.label(end);
151
+		javaWriter.pop();
151 152
 		return false;
152 153
 	}
153 154
 

+ 137
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaUnboxingTypeVisitor.java View File

@@ -0,0 +1,137 @@
1
+package org.openzen.zenscript.javabytecode.compiler;
2
+
3
+import org.objectweb.asm.Type;
4
+import org.openzen.zenscript.codemodel.type.*;
5
+import org.openzen.zenscript.javashared.JavaClass;
6
+import org.openzen.zenscript.javashared.JavaMethod;
7
+
8
+public class JavaUnboxingTypeVisitor implements TypeVisitorWithContext<StoredType, Void, RuntimeException> {
9
+    
10
+    private static final JavaMethod UNBOX_BOOLEAN = JavaMethod.getNativeVirtual(JavaClass.BOOLEAN, "booleanValue", "()Z");
11
+    private static final JavaMethod UNBOX_BYTE = JavaMethod.getNativeVirtual(JavaClass.BYTE, "byteValue", "()B");
12
+    private static final JavaMethod UNBOX_SHORT = JavaMethod.getNativeVirtual(JavaClass.SHORT, "shortValue", "()S");
13
+    private static final JavaMethod UNBOX_INTEGER = JavaMethod.getNativeVirtual(JavaClass.INTEGER, "intValue", "()I");
14
+    private static final JavaMethod UNBOX_LONG = JavaMethod.getNativeVirtual(JavaClass.LONG, "longValue", "()J");
15
+    private static final JavaMethod UNBOX_FLOAT = JavaMethod.getNativeVirtual(JavaClass.FLOAT, "floatValue", "()F");
16
+    private static final JavaMethod UNBOX_DOUBLE = JavaMethod.getNativeVirtual(JavaClass.DOUBLE, "doubleValue", "()D");
17
+    private static final JavaMethod UNBOX_CHARACTER = JavaMethod.getNativeVirtual(JavaClass.CHARACTER, "charValue", "()C");
18
+    
19
+    private final JavaWriter writer;
20
+    
21
+    public JavaUnboxingTypeVisitor(JavaWriter writer) {
22
+        this.writer = writer;
23
+    }
24
+    
25
+    
26
+    @Override
27
+    public Void visitBasic(StoredType context, BasicTypeID basic) throws RuntimeException {
28
+        final JavaMethod method;
29
+        
30
+        switch(basic) {
31
+            case BOOL:
32
+                writer.checkCast(JavaClass.BOOLEAN.internalName);
33
+                method = UNBOX_BOOLEAN;
34
+                break;
35
+            case BYTE:
36
+            case SBYTE:
37
+                writer.checkCast(JavaClass.BYTE.internalName);
38
+                method = UNBOX_BYTE;
39
+                break;
40
+            case SHORT:
41
+            case USHORT:
42
+                writer.checkCast(JavaClass.SHORT.internalName);
43
+                method = UNBOX_SHORT;
44
+                break;
45
+            case INT:
46
+            case UINT:
47
+                writer.checkCast(JavaClass.INTEGER.internalName);
48
+                method = UNBOX_INTEGER;
49
+                break;
50
+            case LONG:
51
+            case ULONG:
52
+            case USIZE:
53
+                writer.checkCast(JavaClass.LONG.internalName);
54
+                method = UNBOX_LONG;
55
+                break;
56
+            case FLOAT:
57
+                writer.checkCast(JavaClass.FLOAT.internalName);
58
+                method = UNBOX_FLOAT;
59
+                break;
60
+            case DOUBLE:
61
+                writer.checkCast(JavaClass.DOUBLE.internalName);
62
+                method = UNBOX_DOUBLE;
63
+                break;
64
+            case CHAR:
65
+                writer.checkCast(JavaClass.CHARACTER.internalName);
66
+                method = UNBOX_CHARACTER;
67
+                break;
68
+            case VOID:
69
+            case UNDETERMINED:
70
+            case NULL:
71
+            default:
72
+                return null;
73
+        }
74
+        writer.invokeVirtual(method);
75
+        return null;
76
+    }
77
+    
78
+    @Override
79
+    public Void visitString(StoredType context, StringTypeID string) throws RuntimeException {
80
+        //NO-OP
81
+        return null;
82
+    }
83
+    
84
+    @Override
85
+    public Void visitArray(StoredType context, ArrayTypeID array) throws RuntimeException {
86
+        //NO-OP
87
+        return null;
88
+    }
89
+    
90
+    @Override
91
+    public Void visitAssoc(StoredType context, AssocTypeID assoc) throws RuntimeException {
92
+        //NO-OP
93
+        return null;
94
+    }
95
+    
96
+    @Override
97
+    public Void visitGenericMap(StoredType context, GenericMapTypeID map) throws RuntimeException {
98
+        //NO-OP
99
+        return null;
100
+    }
101
+    
102
+    @Override
103
+    public Void visitIterator(StoredType context, IteratorTypeID iterator) throws RuntimeException {
104
+        //NO-OP
105
+        return null;
106
+    }
107
+    
108
+    @Override
109
+    public Void visitFunction(StoredType context, FunctionTypeID function) throws RuntimeException {
110
+        //NO-OP
111
+        return null;
112
+    }
113
+    
114
+    @Override
115
+    public Void visitDefinition(StoredType context, DefinitionTypeID definition) throws RuntimeException {
116
+        //NO-OP
117
+        return null;
118
+    }
119
+    
120
+    @Override
121
+    public Void visitGeneric(StoredType context, GenericTypeID generic) throws RuntimeException {
122
+        //NO-OP
123
+        return null;
124
+    }
125
+    
126
+    @Override
127
+    public Void visitRange(StoredType context, RangeTypeID range) throws RuntimeException {
128
+        //NO-OP
129
+        return null;
130
+    }
131
+    
132
+    @Override
133
+    public Void visitOptional(StoredType context, OptionalTypeID type) throws RuntimeException {
134
+        //NO-OP
135
+        return null;
136
+    }
137
+}

Loading…
Cancel
Save