codegen: implement `iastore' and `iaload'
authorBernhard Urban <lewurm@gmail.com>
Thu, 26 Apr 2012 12:41:01 +0000 (14:41 +0200)
committerBernhard Urban <lewurm@gmail.com>
Thu, 26 Apr 2012 12:41:01 +0000 (14:41 +0200)
Makefile
Mate/X86CodeGen.hs
tests/Array1.java

index 3bd63d44968acf5b880a854708854791f8731996..79d36964c2ab981a857b5b901909a3daf355dd19 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -64,6 +64,8 @@ test: mate $(CLASS_FILES)
        @printf "should be: %s\n" "Hello World"
        ./$< tests/Strings1 | egrep -c -e "^okay :-\)"
        @printf "should be: %d\n" 3
+       ./$< tests/Array1 | grep "printstream"
+       @printf "should be:   0x%08x 0x%08x\n" 0x264 0x8
 
 %.class: %.java
        $(JAVAC) $<
index c71494e74ecdad5ea6bee96746adbf80b9cb5a5a..ff9ca2da67521554b794660d0cfeca2f3ee6e8ce 100644 (file)
@@ -11,7 +11,7 @@ import qualified Data.Set as S
 import qualified Data.ByteString.Lazy as B
 import Control.Monad
 
-import Foreign
+import Foreign hiding (xor)
 import Foreign.C.Types
 
 import Text.Printf
@@ -234,6 +234,17 @@ emitFromBB method cls hmap =  do
         call (trapaddr - w32_calladdr)
         add esp (4 :: Word32)
     emit DUP = push (Disp 0, esp)
+    emit IASTORE = do
+        pop eax -- value
+        pop ebx -- offset
+        add ebx (1 :: Word32)
+        pop ecx -- aref
+        mov (ecx, ebx, S4) eax
+    emit IALOAD = do
+        pop ebx -- offset
+        add ebx (1 :: Word32)
+        pop ecx -- aref
+        push (ecx, ebx, S4)
     emit ARRAYLENGTH = do
         pop eax
         push (Disp 0, eax)
@@ -307,6 +318,7 @@ emitFromBB method cls hmap =  do
     emit IADD = do pop ebx; pop eax; add eax ebx; push eax
     emit ISUB = do pop ebx; pop eax; sub eax ebx; push eax
     emit IMUL = do pop ebx; pop eax; mul ebx; push eax
+    emit IXOR = do pop ebx; pop eax; xor eax ebx; push eax
     emit (IINC x imm) = do
         add (Disp (cArgs x), ebp) (s8_w32 imm)
 
index 87cd7954ef7a01b75f6afa2fbb134680756a436f..074477ae4ef773367c2748c65f28f54169528f4c 100644 (file)
@@ -3,6 +3,14 @@ package tests;
 public class Array1 {
        public static void main(String []args) {
                int []arr = new int[0x8];
-               System.out.printf(arr.length);
+               int sum = 0;
+               for (int i = 0; i < 0x8; i++) {
+                       arr[i] = (i + 1) * 0x11;
+               }
+               for (int i = 0; i < 0x8; i++) {
+                       sum += arr[i];
+               }
+               System.out.printf(sum); // 0x264
+               System.out.printf(arr.length); // 0x8
        }
 }