From 278b926c045bbed76e91b4c0e0ad90b11b4c0641 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 15 Aug 2016 19:07:32 +0300 Subject: [PATCH] Fix support of primitive arrays --- .../src/main/java/org/teavm/wasm/Example.java | 5 ++ .../wasm/generate/WasmGenerationVisitor.java | 90 ++++++++++++++++--- 2 files changed, 85 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/teavm/wasm/Example.java b/core/src/main/java/org/teavm/wasm/Example.java index b6d64ed92..1e58a67d8 100644 --- a/core/src/main/java/org/teavm/wasm/Example.java +++ b/core/src/main/java/org/teavm/wasm/Example.java @@ -45,6 +45,11 @@ public final class Example { WasmRuntime.print((Object) new Derived2() instanceof Derived1 ? 1 : 0); WasmRuntime.print((Object) new Derived2() instanceof A ? 1 : 0); WasmRuntime.print(new A(23) instanceof Base ? 1 : 0); + + byte[] bytes = { 5, 6, 10, 15 }; + for (byte bt : bytes) { + WasmRuntime.print(bt); + } } private static Base instance(int index) { diff --git a/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java b/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java index 94b952032..9995ac850 100644 --- a/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java +++ b/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java @@ -406,7 +406,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { storeField(lhs.getQualified(), lhs.getField(), statement.getRightValue()); } else if (left instanceof SubscriptExpr) { SubscriptExpr lhs = (SubscriptExpr) left; - storeArrayItem(lhs.getArray(), lhs.getIndex(), statement.getRightValue()); + storeArrayItem(lhs, statement.getRightValue()); } else { throw new UnsupportedOperationException("This expression is not supported yet"); } @@ -446,10 +446,34 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { } } - private void storeArrayItem(Expr array, Expr index, Expr rightValue) { - WasmExpression ptr = getArrayElementPointer(array, index); + private void storeArrayItem(SubscriptExpr leftValue, Expr rightValue) { + WasmExpression ptr = getArrayElementPointer(leftValue); rightValue.acceptVisitor(this); - result = new WasmStoreInt32(4, ptr, result, WasmInt32Subtype.INT32); + + switch (leftValue.getType()) { + case BYTE: + result = new WasmStoreInt32(1, ptr, result, WasmInt32Subtype.INT8); + break; + case SHORT: + result = new WasmStoreInt32(2, ptr, result, WasmInt32Subtype.INT16); + break; + case CHAR: + result = new WasmStoreInt32(2, ptr, result, WasmInt32Subtype.UINT16); + break; + case INT: + case OBJECT: + result = new WasmStoreInt32(4, ptr, result, WasmInt32Subtype.INT32); + break; + case LONG: + result = new WasmStoreInt64(8, ptr, result, WasmInt64Subtype.INT64); + break; + case FLOAT: + result = new WasmStoreFloat32(4, ptr, result); + break; + case DOUBLE: + result = new WasmStoreFloat64(8, ptr, result); + break; + } } @Override @@ -518,19 +542,65 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { @Override public void visit(SubscriptExpr expr) { - WasmExpression ptr = getArrayElementPointer(expr.getArray(), expr.getIndex()); - result = new WasmLoadInt32(4, ptr, WasmInt32Subtype.INT32); + WasmExpression ptr = getArrayElementPointer(expr); + switch (expr.getType()) { + case BYTE: + result = new WasmLoadInt32(1, ptr, WasmInt32Subtype.INT8); + break; + case SHORT: + result = new WasmLoadInt32(2, ptr, WasmInt32Subtype.INT16); + break; + case CHAR: + result = new WasmLoadInt32(2, ptr, WasmInt32Subtype.UINT16); + break; + case INT: + case OBJECT: + result = new WasmLoadInt32(4, ptr, WasmInt32Subtype.INT32); + break; + case LONG: + result = new WasmLoadInt64(8, ptr, WasmInt64Subtype.INT64); + break; + case FLOAT: + result = new WasmLoadFloat32(4, ptr); + break; + case DOUBLE: + result = new WasmLoadFloat64(8, ptr); + break; + } } - private WasmExpression getArrayElementPointer(Expr arrayExpr, Expr indexExpr) { - arrayExpr.acceptVisitor(this); + private WasmExpression getArrayElementPointer(SubscriptExpr expr) { + expr.getArray().acceptVisitor(this); WasmExpression array = result; - indexExpr.acceptVisitor(this); + expr.getIndex().acceptVisitor(this); WasmExpression index = result; + int size = -1; + switch (expr.getType()) { + case BYTE: + size = 0; + break; + case SHORT: + case CHAR: + size = 1; + break; + case INT: + case FLOAT: + case OBJECT: + size = 2; + break; + case LONG: + case DOUBLE: + size = 3; + break; + } + int base = classGenerator.getClassSize(RuntimeArray.class.getName()); array = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.ADD, array, new WasmInt32Constant(base)); - index = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.SHL, index, new WasmInt32Constant(2)); + if (size != 0) { + index = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.SHL, index, + new WasmInt32Constant(size)); + } return new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.ADD, array, index); }