Browse Source

More work on ExpressionVisitors and added String add method to JavaWriter

kindlich 6 years ago
parent
commit
7c777c2f71
No known key found for this signature in database

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

35
 		LONG_GET_MIN_VALUE.setTag(JavaFieldInfo.class, new JavaFieldInfo(jLong, "MIN_VALUE", "J"));
35
 		LONG_GET_MIN_VALUE.setTag(JavaFieldInfo.class, new JavaFieldInfo(jLong, "MIN_VALUE", "J"));
36
 		LONG_GET_MAX_VALUE.setTag(JavaFieldInfo.class, new JavaFieldInfo(jLong, "MAX_VALUE", "J"));
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
 		// TODO: STRING_ADD_STRING
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
 		implement(INT_TO_UINT, writer -> {});
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
 		INT_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jInteger, "toString", "(I)Ljava/lang/String;", true));
109
 		INT_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jInteger, "toString", "(I)Ljava/lang/String;", true));
108
 		
110
 		
109
 		implement(LONG_TO_BYTE, writer -> { writer.l2i(); writer.i2b(); });
111
 		implement(LONG_TO_BYTE, writer -> { writer.l2i(); writer.i2b(); });
110
 		implement(LONG_TO_SBYTE, writer -> { writer.l2i(); writer.i2b(); });
112
 		implement(LONG_TO_SBYTE, writer -> { writer.l2i(); writer.i2b(); });
111
 		implement(LONG_TO_SHORT, writer -> { writer.l2i(); writer.i2s(); });
113
 		implement(LONG_TO_SHORT, writer -> { writer.l2i(); writer.i2s(); });
112
 		implement(LONG_TO_USHORT, writer -> { writer.l2i(); writer.i2s(); });
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
 		implement(LONG_TO_ULONG, writer -> {});
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
 		implement(LONG_TO_CHAR, writer -> { writer.l2i(); writer.i2s(); });
120
 		implement(LONG_TO_CHAR, writer -> { writer.l2i(); writer.i2s(); });
119
 		LONG_TO_STRING.setTag(JavaMethodInfo.class, new JavaMethodInfo(jLong, "toString", "(J)Ljava/lang/String;", true));
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 View File

1
 package org.openzen.zenscript.javabytecode.compiler;
1
 package org.openzen.zenscript.javabytecode.compiler;
2
 
2
 
3
+import org.objectweb.asm.Type;
4
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
3
 import org.openzen.zenscript.codemodel.expression.*;
5
 import org.openzen.zenscript.codemodel.expression.*;
4
 import org.openzen.zenscript.codemodel.member.DefinitionMember;
6
 import org.openzen.zenscript.codemodel.member.DefinitionMember;
5
 import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
7
 import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
9
 import org.openzen.zenscript.shared.CompileException;
11
 import org.openzen.zenscript.shared.CompileException;
10
 import org.openzen.zenscript.shared.CompileExceptionCode;
12
 import org.openzen.zenscript.shared.CompileExceptionCode;
11
 
13
 
14
+import java.util.Map;
15
+
12
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
16
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
13
 
17
 
14
     private JavaWriter javaWriter;
18
     private JavaWriter javaWriter;
24
 
28
 
25
     @Override
29
     @Override
26
     public Void visitArray(ArrayExpression expression) {
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
         return null;
40
         return null;
28
     }
41
     }
29
 
42
 
194
 
207
 
195
     @Override
208
     @Override
196
     public Void visitGetField(GetFieldExpression expression) {
209
     public Void visitGetField(GetFieldExpression expression) {
197
-        if (!checkAndExecuteFieldInfo(expression.field, false))
210
+        if (!checkAndGetFieldInfo(expression.field, false))
198
             throw new IllegalStateException("Missing field info on a field member!");
211
             throw new IllegalStateException("Missing field info on a field member!");
199
         return null;
212
         return null;
200
     }
213
     }
213
 
226
 
214
     @Override
227
     @Override
215
     public Void visitGetStaticField(GetStaticFieldExpression expression) {
228
     public Void visitGetStaticField(GetStaticFieldExpression expression) {
216
-        if (!checkAndExecuteFieldInfo(expression.field, true))
229
+        if (!checkAndGetFieldInfo(expression.field, true))
217
             throw new IllegalStateException("Missing field info on a field member!");
230
             throw new IllegalStateException("Missing field info on a field member!");
218
         return null;
231
         return null;
219
     }
232
     }
225
 
238
 
226
     @Override
239
     @Override
227
     public Void visitInterfaceCast(InterfaceCastExpression expression) {
240
     public Void visitInterfaceCast(InterfaceCastExpression expression) {
241
+        expression.value.accept(this);
242
+        javaWriter.checkCast(expression.type.accept(JavaTypeClassVisitor.INSTANCE));
228
         return null;
243
         return null;
229
     }
244
     }
230
 
245
 
231
     @Override
246
     @Override
232
     public Void visitIs(IsExpression expression) {
247
     public Void visitIs(IsExpression expression) {
248
+        expression.value.accept(this);
249
+        javaWriter.instanceOf(Type.getDescriptor(expression.isType.accept(JavaTypeClassVisitor.INSTANCE)));
233
         return null;
250
         return null;
234
     }
251
     }
235
 
252
 
236
     @Override
253
     @Override
237
-    public Void visitMakeConst(MakeConstExpression expression) {
254
+    public Void visitMakeConst(MakeConstExpression expression)
255
+    {
256
+
238
         return null;
257
         return null;
239
     }
258
     }
240
 
259
 
241
     @Override
260
     @Override
242
     public Void visitMap(MapExpression expression) {
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
         return null;
272
         return null;
244
     }
273
     }
245
 
274
 
246
     @Override
275
     @Override
247
     public Void visitNew(NewExpression expression) {
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
         return null;
288
         return null;
249
     }
289
     }
250
 
290
 
251
     @Override
291
     @Override
252
     public Void visitNot(NotExpression expression) {
292
     public Void visitNot(NotExpression expression) {
293
+        expression.value.accept(this);
294
+        javaWriter.iNeg();
253
         return null;
295
         return null;
254
     }
296
     }
255
 
297
 
256
     @Override
298
     @Override
257
     public Void visitNull(NullExpression expression) {
299
     public Void visitNull(NullExpression expression) {
300
+        javaWriter.aConstNull();
258
         return null;
301
         return null;
259
     }
302
     }
260
 
303
 
270
 
313
 
271
     @Override
314
     @Override
272
     public Void visitSetField(SetFieldExpression expression) {
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
         return null;
321
         return null;
274
     }
322
     }
275
 
323
 
280
 
328
 
281
     @Override
329
     @Override
282
     public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
330
     public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
283
-        if(expression.variable.isFinal)
331
+        if (expression.variable.isFinal)
284
             throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final variable!");
332
             throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final variable!");
285
         expression.value.accept(this);
333
         expression.value.accept(this);
286
         final JavaLocalVariableInfo tag = expression.variable.getTag(JavaLocalVariableInfo.class);
334
         final JavaLocalVariableInfo tag = expression.variable.getTag(JavaLocalVariableInfo.class);
292
 
340
 
293
     @Override
341
     @Override
294
     public Void visitSetStaticField(SetStaticFieldExpression expression) {
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
         return null;
348
         return null;
296
     }
349
     }
297
 
350
 
367
 
420
 
368
     //TODO: Should isStatic go to the fieldInfo or stay here?
421
     //TODO: Should isStatic go to the fieldInfo or stay here?
369
     //Will return true if a JavaFieldInfo.class tag exists, and will compile that tag
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
         JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
436
         JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
372
         if (fieldInfo == null)
437
         if (fieldInfo == null)
373
             return false;
438
             return false;
374
         if (isStatic) {
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
         } else {
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
         return true;
444
         return true;
386
     }
445
     }

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

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

Loading…
Cancel
Save