varargs: make printf working
authorBernhard Urban <lewurm@gmail.com>
Thu, 26 Apr 2012 15:10:17 +0000 (17:10 +0200)
committerBernhard Urban <lewurm@gmail.com>
Thu, 26 Apr 2012 15:44:04 +0000 (17:44 +0200)
although it's still a hack: WOOOT

Makefile
Mate/X86CodeGen.hs
ffi/native.c
java/io/PrintStream.java
tests/VarArgs1.java [new file with mode: 0644]

index f170acc3076c011bc105e12cc79137329e1e94ce..87aca3b6201941f6e8a9ae1ab9ffec5fbac91138 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -68,6 +68,7 @@ test: mate $(CLASS_FILES)
        @printf "should be:   0x%08x 0x%08x\n" 0x264 0x8
        ./$< tests/Integer1 | grep mainresult
        @printf "should be:  0x%08x\n" 0x1337
+       ./$< tests/VarArgs1 | grep mainresult
 
 %.class: %.java
        $(JAVAC) $<
index 59ccb420d4a6cd929c8bb31f7780740c2d519275..1eb9057f7eb1dd559f67a6631b90841dd26ced1b 100644 (file)
@@ -234,12 +234,14 @@ emitFromBB method cls hmap =  do
         call (trapaddr - w32_calladdr)
         add esp (4 :: Word32)
     emit DUP = push (Disp 0, esp)
+    emit AASTORE = emit IASTORE
     emit IASTORE = do
         pop eax -- value
         pop ebx -- offset
         add ebx (1 :: Word32)
         pop ecx -- aref
         mov (ecx, ebx, S4) eax
+    emit AALOAD = emit IALOAD
     emit IALOAD = do
         pop ebx -- offset
         add ebx (1 :: Word32)
@@ -248,6 +250,7 @@ emitFromBB method cls hmap =  do
     emit ARRAYLENGTH = do
         pop eax
         push (Disp 0, eax)
+    emit (ANEWARRAY _) = emit (NEWARRAY 10) -- 10 == T_INT
     emit (NEWARRAY typ) = do
         let tsize = case decodeS (0 :: Integer) (B.pack [typ]) of
                     T_INT -> 4
@@ -280,6 +283,7 @@ emitFromBB method cls hmap =  do
     emit (ICONST_0) = push (0 :: Word32)
     emit (ICONST_1) = push (1 :: Word32)
     emit (ICONST_2) = push (2 :: Word32)
+    emit (ICONST_3) = push (3 :: Word32)
     emit (ICONST_4) = push (4 :: Word32)
     emit (ICONST_5) = push (5 :: Word32)
     emit (ALOAD_ x) = emit (ILOAD_ x)
index a9aa2277ce021f5295dff0846775a2262a7637b8..2972a18a34e70aa1037ca4804a910896df8f27c4 100644 (file)
@@ -1,5 +1,29 @@
 #include <stdio.h>
 
+static char ascii(char s) {
+       if(s < 0x20) return '.';
+       if(s > 0x7E) return '.';
+       return s;
+}
+
+void hexdump(void *d, int len) {
+       unsigned char *data;
+       int i, off;
+       data = (unsigned char*)d;
+       for (off=0; off<len; off += 16) {
+               printf("%08x  ",off);
+               for(i=0; i<16; i++)
+                       if((i+off)>=len) printf("   ");
+                       else printf("%02x ",data[off+i]);
+
+               printf(" ");
+               for(i=0; i<16; i++)
+                       if((i+off)>=len) printf(" ");
+                       else printf("%c",ascii(data[off+i]));
+               printf("\n");
+       }
+}
+
 void tests_Native1__printSomething____V(void)
 {
        printf("printSomething: woot \\o/\n");
@@ -24,3 +48,39 @@ void java_io_PrintStream__println___Ljava_lang_String__V(const char *a)
 {
        printf("%s\n", a);
 }
+
+struct integer {
+       unsigned int method_table_ptr;
+       int value;
+};
+
+void java_io_PrintStream__printf_1___Ljava_lang_String_Ljava_lang_Object__V
+       (struct integer *a1, const char *fmt)
+{
+       printf(fmt, a1->value);
+}
+
+void java_io_PrintStream__printf_2___Ljava_lang_String_Ljava_lang_Object_Ljava_lang_Object__V
+       (struct integer *a2, struct integer *a1, const char *fmt)
+{
+       printf(fmt, a1->value, a2->value);
+}
+
+void
+java_io_PrintStream__printf_3___Ljava_lang_String_Ljava_lang_Object_Ljava_lang_Object_Ljava_lang_Object__V
+       (struct integer *a3, struct integer *a2, struct integer *a1, const char *fmt)
+{
+       printf(fmt, a1->value, a2->value, a3->value);
+}
+
+void java_io_PrintStream__printf_4___Ljava_lang_String_Ljava_lang_Object_Ljava_lang_Object_Ljava_lang_Object_Ljava_lang_Object__V
+       (struct integer *a4, struct integer *a3, struct integer *a2, struct integer *a1, const char *fmt)
+{
+       printf(fmt, a1->value, a2->value, a3->value, a4->value);
+}
+
+void java_io_PrintStream__printf_5___Ljava_lang_String_Ljava_lang_Object_Ljava_lang_Object_Ljava_lang_Object_Ljava_lang_Object_Ljava_lang_Object__V
+       (struct integer *a5, struct integer *a4, struct integer *a3, struct integer *a2, struct integer *a1, const char *fmt)
+{
+       printf(fmt, a1->value, a2->value, a3->value, a4->value, a5->value);
+}
index 89fe534f952cc98b5dd9bfe1915e3ad38ada2a4a..0e7dafdfdb61ed6e0a7417dd546615144bfbf925 100644 (file)
@@ -1,6 +1,29 @@
 package java.io;
 
 public class PrintStream {
-       public native void printf(int a);
+       public PrintStream printf(String format, Object... args) {
+               /* temporary workaround ;-) */
+               int len = args.length;
+               if (len == 0) {
+                       this.println(format);
+               } else if (len == 1) {
+                       this.printf_1(format, args[0]);
+               } else if (len == 2) {
+                       this.printf_2(format, args[0], args[1]);
+               } else if (len == 3) {
+                       this.printf_3(format, args[0], args[1], args[2]);
+               } else if (len == 4) {
+                       this.printf_4(format, args[0], args[1], args[2], args[3]);
+               } else if (len == 5) {
+                       this.printf_5(format, args[0], args[1], args[2], args[3], args[4]);
+               }
+               return this;
+       }
+
+       public native void printf_1(String a, Object b);
+       public native void printf_2(String a, Object b, Object c);
+       public native void printf_3(String a, Object b, Object c, Object d);
+       public native void printf_4(String a, Object b, Object c, Object d, Object e);
+       public native void printf_5(String a, Object b, Object c, Object d, Object e, Object f);
        public native void println(String a);
 }
diff --git a/tests/VarArgs1.java b/tests/VarArgs1.java
new file mode 100644 (file)
index 0000000..c6f2f90
--- /dev/null
@@ -0,0 +1,12 @@
+package tests;
+
+public class VarArgs1 {
+       public static void main(String []args) {
+               System.out.printf("result: nice0r\n");
+               System.out.printf("result: 0x%08x\n", 0x1337);
+               System.out.printf("result: 0x%08x 0x%08x\n", 0x1337, 0x1122);
+               System.out.printf("result: 0x%08x 0x%08x 0x%08x\n", 0x1, 0x2, 0x3);
+               System.out.printf("result: 0x%08x 0x%08x 0x%08x 0x%08x\n", 0x1, 0x2, 0x3, 0x4);
+               System.out.printf("result: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n", 0x1, 0x2, 0x3, 0x4, 0x5);
+       }
+}