From 43956341b5219a74d2ce9f2ace38695528e265bd Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Sun, 20 May 2012 21:37:08 +0200 Subject: [PATCH] array: char array support --- Makefile | 1 + Mate/Utilities.hs | 3 ++- Mate/X86CodeGen.hs | 17 +++++++++++++++++ java/lang/Character.java | 13 +++++++++++++ tests/CharArray1.java | 17 +++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 java/lang/Character.java create mode 100644 tests/CharArray1.java diff --git a/Makefile b/Makefile index 48a1b40..8d91b35 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ COMPILEF = $(basename $@).compile @if [ -f $(COMPILEF) ]; \ then $(SHELL) $(COMPILEF); \ else $(JAVAC) $<; fi + @echo "JAVAC $<" ffi/native.o: ffi/native.c ghc -Wall -O2 -c $< -o $@ diff --git a/Mate/Utilities.hs b/Mate/Utilities.hs index 4ae0bac..c76d680 100644 --- a/Mate/Utilities.hs +++ b/Mate/Utilities.hs @@ -44,8 +44,9 @@ methodHaveReturnValue :: Class Direct -> Word16 -> Bool methodHaveReturnValue cls idx = case ret of ReturnsVoid -> False; (Returns IntType) -> True; + (Returns (Array _ _)) -> True (Returns (ObjectType _)) -> True; - _ -> error "methodHaveReturnValue: todo" + _ -> error $ "methodHaveReturnValue: todo: " ++ show ret where nt = case constsPool cls M.! idx of (CMethod _ nt') -> nt' diff --git a/Mate/X86CodeGen.hs b/Mate/X86CodeGen.hs index a44a5d0..724cfc4 100644 --- a/Mate/X86CodeGen.hs +++ b/Mate/X86CodeGen.hs @@ -4,6 +4,7 @@ #include "debug.h" module Mate.X86CodeGen where +import Prelude hiding (and) import Data.Binary import Data.BinaryState import Data.Int @@ -181,12 +182,23 @@ emitFromBB method cls hmap = do add ebx (1 :: Word32) pop ecx -- aref mov (ecx, ebx, S4) eax + emit CASTORE = do + pop eax -- value + pop ebx -- offset + add ebx (1 :: Word32) + pop ecx -- aref + mov (ecx, ebx, S1) eax -- TODO(bernhard): char is two byte emit AALOAD = emit IALOAD emit IALOAD = do pop ebx -- offset add ebx (1 :: Word32) pop ecx -- aref push (ecx, ebx, S4) + emit CALOAD = do + pop ebx -- offset + add ebx (1 :: Word32) + pop ecx -- aref + push (ecx, ebx, S1) -- TODO(bernhard): char is two byte emit ARRAYLENGTH = do pop eax push (Disp 0, eax) @@ -194,6 +206,7 @@ emitFromBB method cls hmap = do emit (NEWARRAY typ) = do let tsize = case decodeS (0 :: Integer) (B.pack [typ]) of T_INT -> 4 + T_CHAR -> 2 _ -> error "newarray: type not implemented yet" -- get length from stack, but leave it there mov eax (Disp 0, esp) @@ -218,6 +231,10 @@ emitFromBB method cls hmap = do mtable <- liftIO $ getMethodTable objname mov (Disp 0, eax) mtable emit (CHECKCAST _) = nop -- TODO(bernhard): ... + emit I2C = do + pop eax + and eax (0x000000ff :: Word32) + push eax emit (BIPUSH val) = push (fromIntegral val :: Word32) emit (SIPUSH val) = push (fromIntegral (fromIntegral val :: Int16) :: Word32) emit (ICONST_0) = push (0 :: Word32) diff --git a/java/lang/Character.java b/java/lang/Character.java new file mode 100644 index 0000000..c314b14 --- /dev/null +++ b/java/lang/Character.java @@ -0,0 +1,13 @@ +package java.lang; + +public class Character { + char value; + + public Character(char a) { + this.value = a; + } + + public static Character valueOf(char a) { + return new Character(a); + } +} diff --git a/tests/CharArray1.java b/tests/CharArray1.java new file mode 100644 index 0000000..4250d2c --- /dev/null +++ b/tests/CharArray1.java @@ -0,0 +1,17 @@ +package tests; + +public class CharArray1 { + static char[] foo() { + char[] val = new char[10]; + for (int i = 0; i < 10; i++) + val[i] = (char) (i + 0x30); + return val; + } + + public static void main(String []args) { + char [] ar = foo(); + for (int i = 0; i < 10; i++) + System.out.printf("%c", ar[i]); + System.out.printf("\n"); + } +} -- 2.25.1