From: Bernhard Urban Date: Thu, 26 Apr 2012 12:41:01 +0000 (+0200) Subject: codegen: implement `iastore' and `iaload' X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=mate.git;a=commitdiff_plain;h=c0425d5d39668e59b104bb21166869757531a8dd codegen: implement `iastore' and `iaload' --- diff --git a/Makefile b/Makefile index 3bd63d4..79d3696 100644 --- 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) $< diff --git a/Mate/X86CodeGen.hs b/Mate/X86CodeGen.hs index c71494e..ff9ca2d 100644 --- a/Mate/X86CodeGen.hs +++ b/Mate/X86CodeGen.hs @@ -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) diff --git a/tests/Array1.java b/tests/Array1.java index 87cd795..074477a 100644 --- a/tests/Array1.java +++ b/tests/Array1.java @@ -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 } }