Переглянути джерело

More work on ExpressionVisitors and added String add method to JavaWriter

kindlich 6 роки тому
джерело
коміт
7c777c2f71
Не вдалося знайти GPG ключ що відповідає даному підпису

+ 73
- 71
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java Переглянути файл

@@ -35,86 +35,88 @@ public class JavaCompiler {
35 35
 		LONG_GET_MIN_VALUE.setTag(JavaFieldInfo.class, new JavaFieldInfo(jLong, "MIN_VALUE", "J"));
36 36
 		LONG_GET_MAX_VALUE.setTag(JavaFieldInfo.class, new JavaFieldInfo(jLong, "MAX_VALUE", "J"));
37 37
 		
38
-		implement(BOOL_NOT, writer -> writer.iNot());
39
-		
40
-		implement(BYTE_NOT, writer -> writer.iNot());
41
-		implement(SBYTE_NOT, writer -> writer.iNot());
42
-		implement(SHORT_NOT, writer -> writer.iNot());
43
-		implement(USHORT_NOT, writer -> writer.iNot());
44
-		implement(INT_NOT, writer -> writer.iNot());
45
-		implement(UINT_NOT, writer -> writer.iNot());
46
-		implement(LONG_NOT, writer -> writer.lNot());
47
-		implement(ULONG_NOT, writer -> writer.lNot());
48
-		
49
-		implement(BYTE_NEG, writer -> writer.iNeg());
50
-		implement(SBYTE_NEG, writer -> writer.iNeg());
51
-		implement(SHORT_NEG, writer -> writer.iNeg());
52
-		implement(USHORT_NEG, writer -> writer.iNeg());
53
-		implement(INT_NEG, writer -> writer.iNeg());
54
-		implement(UINT_NEG, writer -> writer.iNeg());
55
-		implement(LONG_NEG, writer -> writer.lNeg());
56
-		implement(ULONG_NEG, writer -> writer.lNeg());
57
-		implement(FLOAT_NEG, writer -> writer.fNeg());
58
-		implement(DOUBLE_NEG, writer -> writer.dNeg());
59
-		
60
-		implement(BYTE_ADD_BYTE, writer -> writer.iAdd());
61
-		implement(SBYTE_ADD_SBYTE, writer -> writer.iAdd());
62
-		implement(SHORT_ADD_SHORT, writer -> writer.iAdd());
63
-		implement(USHORT_ADD_USHORT, writer -> writer.iAdd());
64
-		implement(INT_ADD_INT, writer -> writer.iAdd());
65
-		implement(UINT_ADD_UINT, writer -> writer.iAdd());
66
-		implement(LONG_ADD_LONG, writer -> writer.lAdd());
67
-		implement(ULONG_ADD_ULONG, writer -> writer.lAdd());
68
-		implement(FLOAT_ADD_FLOAT, writer -> writer.fAdd());
69
-		implement(DOUBLE_ADD_DOUBLE, writer -> writer.dAdd());
38
+		implement(BOOL_NOT, JavaWriter::iNot);
39
+		
40
+		implement(BYTE_NOT, JavaWriter::iNot);
41
+		implement(SBYTE_NOT, JavaWriter::iNot);
42
+		implement(SHORT_NOT, JavaWriter::iNot);
43
+		implement(USHORT_NOT, JavaWriter::iNot);
44
+		implement(INT_NOT, JavaWriter::iNot);
45
+		implement(UINT_NOT, JavaWriter::iNot);
46
+		implement(LONG_NOT, JavaWriter::lNot);
47
+		implement(ULONG_NOT, JavaWriter::lNot);
48
+		
49
+		implement(BYTE_NEG, JavaWriter::iNeg);
50
+		implement(SBYTE_NEG, JavaWriter::iNeg);
51
+		implement(SHORT_NEG, JavaWriter::iNeg);
52
+		implement(USHORT_NEG, JavaWriter::iNeg);
53
+		implement(INT_NEG, JavaWriter::iNeg);
54
+		implement(UINT_NEG, JavaWriter::iNeg);
55
+		implement(LONG_NEG, JavaWriter::lNeg);
56
+		implement(ULONG_NEG, JavaWriter::lNeg);
57
+		implement(FLOAT_NEG, JavaWriter::fNeg);
58
+		implement(DOUBLE_NEG, JavaWriter::dNeg);
59
+		
60
+		implement(BYTE_ADD_BYTE, JavaWriter::iAdd);
61
+		implement(SBYTE_ADD_SBYTE, JavaWriter::iAdd);
62
+		implement(SHORT_ADD_SHORT, JavaWriter::iAdd);
63
+		implement(USHORT_ADD_USHORT, JavaWriter::iAdd);
64
+		implement(INT_ADD_INT, JavaWriter::iAdd);
65
+		implement(UINT_ADD_UINT, JavaWriter::iAdd);
66
+		implement(LONG_ADD_LONG, JavaWriter::lAdd);
67
+		implement(ULONG_ADD_ULONG, JavaWriter::lAdd);
68
+		implement(FLOAT_ADD_FLOAT, JavaWriter::fAdd);
69
+		implement(DOUBLE_ADD_DOUBLE, JavaWriter::dAdd);
70 70
 		// TODO: STRING_ADD_STRING
71
-		
72
-		implement(BYTE_SUB_BYTE, writer -> writer.iSub());
73
-		implement(SBYTE_SUB_SBYTE, writer -> writer.iSub());
74
-		implement(SHORT_SUB_SHORT, writer -> writer.iSub());
75
-		implement(USHORT_SUB_USHORT, writer -> writer.iSub());
76
-		implement(INT_SUB_INT, writer -> writer.iSub());
77
-		implement(UINT_SUB_UINT, writer -> writer.iSub());
78
-		implement(LONG_SUB_LONG, writer -> writer.lSub());
79
-		implement(ULONG_SUB_ULONG, writer -> writer.lSub());
80
-		implement(FLOAT_SUB_FLOAT, writer -> writer.fSub());
81
-		implement(DOUBLE_SUB_DOUBLE, writer -> writer.dSub());
82
-		
83
-		implement(BYTE_MUL_BYTE, writer -> writer.iMul());
84
-		implement(SBYTE_MUL_SBYTE, writer -> writer.iMul());
85
-		implement(SHORT_MUL_SHORT, writer -> writer.iMul());
86
-		implement(USHORT_MUL_USHORT, writer -> writer.iMul());
87
-		implement(INT_MUL_INT, writer -> writer.iMul());
88
-		implement(UINT_MUL_UINT, writer -> writer.iMul());
89
-		implement(LONG_MUL_LONG, writer -> writer.lMul());
90
-		implement(ULONG_MUL_ULONG, writer -> writer.lMul());
91
-		implement(FLOAT_MUL_FLOAT, writer -> writer.fMul());
92
-		implement(DOUBLE_MUL_DOUBLE, writer -> writer.dMul());
93
-		
94
-		implement(INT_DIV_INT, writer -> writer.iDiv());
95
-		implement(INT_MOD_INT, writer -> writer.iRem());
96
-		
97
-		implement(INT_TO_BYTE, writer -> writer.i2b());
98
-		implement(INT_TO_SBYTE, writer -> writer.i2b());
99
-		implement(INT_TO_SHORT, writer -> writer.i2s());
100
-		implement(INT_TO_USHORT, writer -> writer.i2s());
71
+
72
+		implement(STRING_ADD_STRING, JavaWriter::stringAdd);
73
+		
74
+		implement(BYTE_SUB_BYTE, JavaWriter::iSub);
75
+		implement(SBYTE_SUB_SBYTE, JavaWriter::iSub);
76
+		implement(SHORT_SUB_SHORT, JavaWriter::iSub);
77
+		implement(USHORT_SUB_USHORT, JavaWriter::iSub);
78
+		implement(INT_SUB_INT, JavaWriter::iSub);
79
+		implement(UINT_SUB_UINT, JavaWriter::iSub);
80
+		implement(LONG_SUB_LONG, JavaWriter::lSub);
81
+		implement(ULONG_SUB_ULONG, JavaWriter::lSub);
82
+		implement(FLOAT_SUB_FLOAT, JavaWriter::fSub);
83
+		implement(DOUBLE_SUB_DOUBLE, JavaWriter::dSub);
84
+		
85
+		implement(BYTE_MUL_BYTE, JavaWriter::iMul);
86
+		implement(SBYTE_MUL_SBYTE, JavaWriter::iMul);
87
+		implement(SHORT_MUL_SHORT, JavaWriter::iMul);
88
+		implement(USHORT_MUL_USHORT, JavaWriter::iMul);
89
+		implement(INT_MUL_INT, JavaWriter::iMul);
90
+		implement(UINT_MUL_UINT, JavaWriter::iMul);
91
+		implement(LONG_MUL_LONG, JavaWriter::lMul);
92
+		implement(ULONG_MUL_ULONG, JavaWriter::lMul);
93
+		implement(FLOAT_MUL_FLOAT, JavaWriter::fMul);
94
+		implement(DOUBLE_MUL_DOUBLE, JavaWriter::dMul);
95
+		
96
+		implement(INT_DIV_INT, JavaWriter::iDiv);
97
+		implement(INT_MOD_INT, JavaWriter::iRem);
98
+		
99
+		implement(INT_TO_BYTE, JavaWriter::i2b);
100
+		implement(INT_TO_SBYTE, JavaWriter::i2b);
101
+		implement(INT_TO_SHORT, JavaWriter::i2s);
102
+		implement(INT_TO_USHORT, JavaWriter::i2s);
101 103
 		implement(INT_TO_UINT, writer -> {});
102
-		implement(INT_TO_LONG, writer -> writer.i2l());
103
-		implement(INT_TO_ULONG, writer -> writer.i2l());
104
-		implement(INT_TO_FLOAT, writer -> writer.i2f());
105
-		implement(INT_TO_DOUBLE, writer -> writer.i2d());
106
-		implement(INT_TO_CHAR, writer -> writer.i2s());
104
+		implement(INT_TO_LONG, JavaWriter::i2l);
105
+		implement(INT_TO_ULONG, JavaWriter::i2l);
106
+		implement(INT_TO_FLOAT, JavaWriter::i2f);
107
+		implement(INT_TO_DOUBLE, JavaWriter::i2d);
108
+		implement(INT_TO_CHAR, JavaWriter::i2s);
107 109
 		INT_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jInteger, "toString", "(I)Ljava/lang/String;", true));
108 110
 		
109 111
 		implement(LONG_TO_BYTE, writer -> { writer.l2i(); writer.i2b(); });
110 112
 		implement(LONG_TO_SBYTE, writer -> { writer.l2i(); writer.i2b(); });
111 113
 		implement(LONG_TO_SHORT, writer -> { writer.l2i(); writer.i2s(); });
112 114
 		implement(LONG_TO_USHORT, writer -> { writer.l2i(); writer.i2s(); });
113
-		implement(LONG_TO_INT, writer -> writer.l2i());
114
-		implement(LONG_TO_UINT, writer -> writer.l2i());
115
+		implement(LONG_TO_INT, JavaWriter::l2i);
116
+		implement(LONG_TO_UINT, JavaWriter::l2i);
115 117
 		implement(LONG_TO_ULONG, writer -> {});
116
-		implement(LONG_TO_FLOAT, writer -> writer.l2f());
117
-		implement(LONG_TO_DOUBLE, writer -> writer.l2d());
118
+		implement(LONG_TO_FLOAT, JavaWriter::l2f);
119
+		implement(LONG_TO_DOUBLE, JavaWriter::l2d);
118 120
 		implement(LONG_TO_CHAR, writer -> { writer.l2i(); writer.i2s(); });
119 121
 		LONG_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jLong, "toString", "(J)Ljava/lang/String;", true));
120 122
 		

+ 72
- 13
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java Переглянути файл

@@ -1,5 +1,7 @@
1 1
 package org.openzen.zenscript.javabytecode.compiler;
2 2
 
3
+import org.objectweb.asm.Type;
4
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
3 5
 import org.openzen.zenscript.codemodel.expression.*;
4 6
 import org.openzen.zenscript.codemodel.member.DefinitionMember;
5 7
 import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
@@ -9,6 +11,8 @@ import org.openzen.zenscript.javabytecode.JavaMethodInfo;
9 11
 import org.openzen.zenscript.shared.CompileException;
10 12
 import org.openzen.zenscript.shared.CompileExceptionCode;
11 13
 
14
+import java.util.Map;
15
+
12 16
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
13 17
 
14 18
     private JavaWriter javaWriter;
@@ -24,6 +28,15 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
24 28
 
25 29
     @Override
26 30
     public Void visitArray(ArrayExpression expression) {
31
+        javaWriter.constant(expression.expressions.length);
32
+        Type type = Type.getType(expression.type.accept(JavaTypeClassVisitor.INSTANCE).getComponentType());
33
+        javaWriter.newArray(type);
34
+        for (int i = 0; i < expression.expressions.length; i++) {
35
+            javaWriter.dup();
36
+            javaWriter.constant(i);
37
+            expression.expressions[i].accept(this);
38
+            javaWriter.arrayStore(type);
39
+        }
27 40
         return null;
28 41
     }
29 42
 
@@ -194,7 +207,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
194 207
 
195 208
     @Override
196 209
     public Void visitGetField(GetFieldExpression expression) {
197
-        if (!checkAndExecuteFieldInfo(expression.field, false))
210
+        if (!checkAndGetFieldInfo(expression.field, false))
198 211
             throw new IllegalStateException("Missing field info on a field member!");
199 212
         return null;
200 213
     }
@@ -213,7 +226,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
213 226
 
214 227
     @Override
215 228
     public Void visitGetStaticField(GetStaticFieldExpression expression) {
216
-        if (!checkAndExecuteFieldInfo(expression.field, true))
229
+        if (!checkAndGetFieldInfo(expression.field, true))
217 230
             throw new IllegalStateException("Missing field info on a field member!");
218 231
         return null;
219 232
     }
@@ -225,36 +238,66 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
225 238
 
226 239
     @Override
227 240
     public Void visitInterfaceCast(InterfaceCastExpression expression) {
241
+        expression.value.accept(this);
242
+        javaWriter.checkCast(expression.type.accept(JavaTypeClassVisitor.INSTANCE));
228 243
         return null;
229 244
     }
230 245
 
231 246
     @Override
232 247
     public Void visitIs(IsExpression expression) {
248
+        expression.value.accept(this);
249
+        javaWriter.instanceOf(Type.getDescriptor(expression.isType.accept(JavaTypeClassVisitor.INSTANCE)));
233 250
         return null;
234 251
     }
235 252
 
236 253
     @Override
237
-    public Void visitMakeConst(MakeConstExpression expression) {
254
+    public Void visitMakeConst(MakeConstExpression expression)
255
+    {
256
+
238 257
         return null;
239 258
     }
240 259
 
241 260
     @Override
242 261
     public Void visitMap(MapExpression expression) {
262
+        javaWriter.newObject(expression.type.accept(JavaTypeClassVisitor.INSTANCE));
263
+        javaWriter.dup();
264
+        javaWriter.invokeSpecial("java/util/Map", "<init>", "()V");
265
+        for (int i = 0; i < expression.keys.length; i++) {
266
+            javaWriter.dup();
267
+            expression.keys[i].accept(this);
268
+            expression.values[i].accept(this);
269
+            javaWriter.invokeInterface(Map.class, "put", Object.class, Object.class, Object.class);
270
+            javaWriter.pop();
271
+        }
243 272
         return null;
244 273
     }
245 274
 
246 275
     @Override
247 276
     public Void visitNew(NewExpression expression) {
277
+        String type = Type.getDescriptor(expression.type.accept(JavaTypeClassVisitor.INSTANCE));
278
+        javaWriter.newObject(type);
279
+        javaWriter.dup();
280
+        StringBuilder signatureBuilder = new StringBuilder("(");
281
+        for (Expression argument : expression.arguments.arguments) {
282
+            argument.accept(this);
283
+            signatureBuilder.append(Type.getDescriptor(argument.type.accept(JavaTypeClassVisitor.INSTANCE)));
284
+        }
285
+        signatureBuilder.append(")V");
286
+        javaWriter.invokeSpecial(type, "<init>", signatureBuilder.toString());
287
+
248 288
         return null;
249 289
     }
250 290
 
251 291
     @Override
252 292
     public Void visitNot(NotExpression expression) {
293
+        expression.value.accept(this);
294
+        javaWriter.iNeg();
253 295
         return null;
254 296
     }
255 297
 
256 298
     @Override
257 299
     public Void visitNull(NullExpression expression) {
300
+        javaWriter.aConstNull();
258 301
         return null;
259 302
     }
260 303
 
@@ -270,6 +313,11 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
270 313
 
271 314
     @Override
272 315
     public Void visitSetField(SetFieldExpression expression) {
316
+        if (expression.field.isFinal)
317
+            throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final field!");
318
+        expression.value.accept(this);
319
+        if (!checkAndPutFieldInfo(expression.field, false))
320
+            throw new IllegalStateException("Missing field info on a field member!");
273 321
         return null;
274 322
     }
275 323
 
@@ -280,7 +328,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
280 328
 
281 329
     @Override
282 330
     public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
283
-        if(expression.variable.isFinal)
331
+        if (expression.variable.isFinal)
284 332
             throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final variable!");
285 333
         expression.value.accept(this);
286 334
         final JavaLocalVariableInfo tag = expression.variable.getTag(JavaLocalVariableInfo.class);
@@ -292,6 +340,11 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
292 340
 
293 341
     @Override
294 342
     public Void visitSetStaticField(SetStaticFieldExpression expression) {
343
+        if (expression.field.isFinal)
344
+            throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final field!");
345
+        expression.value.accept(this);
346
+        if (!checkAndPutFieldInfo(expression.field, true))
347
+            throw new IllegalStateException("Missing field info on a field member!");
295 348
         return null;
296 349
     }
297 350
 
@@ -367,20 +420,26 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
367 420
 
368 421
     //TODO: Should isStatic go to the fieldInfo or stay here?
369 422
     //Will return true if a JavaFieldInfo.class tag exists, and will compile that tag
370
-    private boolean checkAndExecuteFieldInfo(DefinitionMember field, boolean isStatic) {
423
+    private boolean checkAndPutFieldInfo(DefinitionMember field, boolean isStatic) {
424
+        JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
425
+        if (fieldInfo == null)
426
+            return false;
427
+        if (isStatic) {
428
+            getJavaWriter().putStaticField(fieldInfo.javaClass.internalClassName, fieldInfo.name, fieldInfo.signature);
429
+        } else {
430
+            getJavaWriter().putField(fieldInfo.javaClass.internalClassName, fieldInfo.name, fieldInfo.signature);
431
+        }
432
+        return true;
433
+    }
434
+
435
+    private boolean checkAndGetFieldInfo(DefinitionMember field, boolean isStatic) {
371 436
         JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
372 437
         if (fieldInfo == null)
373 438
             return false;
374 439
         if (isStatic) {
375
-            getJavaWriter().getStaticField(
376
-                    fieldInfo.javaClass.internalClassName,
377
-                    fieldInfo.name,
378
-                    fieldInfo.signature);
440
+            getJavaWriter().getStaticField(fieldInfo.javaClass.internalClassName, fieldInfo.name, fieldInfo.signature);
379 441
         } else {
380
-            getJavaWriter().getField(
381
-                    fieldInfo.javaClass.internalClassName,
382
-                    fieldInfo.name,
383
-                    fieldInfo.signature);
442
+            getJavaWriter().getField(fieldInfo.javaClass.internalClassName, fieldInfo.name, fieldInfo.signature);
384 443
         }
385 444
         return true;
386 445
     }

+ 4
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java Переглянути файл

@@ -1064,4 +1064,8 @@ public class JavaWriter {
1064 1064
 
1065 1065
         return labelNames.get(lbl);
1066 1066
     }
1067
+
1068
+    public void stringAdd() {
1069
+        invokeVirtual(String.class, "concat", String.class, String.class);
1070
+    }
1067 1071
 }

Завантаження…
Відмінити
Зберегти