trapping code: transition from native to haskell rts PoC
authorBernhard Urban <lewurm@gmail.com>
Sun, 18 Mar 2012 04:19:43 +0000 (05:19 +0100)
committerBernhard Urban <lewurm@gmail.com>
Sun, 18 Mar 2012 04:19:43 +0000 (05:19 +0100)
Makefile
Mate.hs
trap.c [new file with mode: 0644]

index 8965f9a14eb43c198cb081565f19ef0abb2f4f21..7bc23cfc42fe8bf023ba07e6cb825eab0ecbd8b9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,8 +4,8 @@ all: mate Test.class
 %.class: %.java
        javac $<
 
-mate: Mate.hs
-       ghc --make -O2 $< -o $@
+mate: Mate.hs trap.c
+       ghc --make -Wall -O2 $^ -o $@
 
 clean:
        rm -f *.hi *.o mate
diff --git a/Mate.hs b/Mate.hs
index 600e29b1dd31f42e514c31fb8973e9b27949c3df..3fcfca5c04e4c2056adfc4028083a565cfd06079 100644 (file)
--- a/Mate.hs
+++ b/Mate.hs
@@ -31,6 +31,12 @@ import Harpy.X86Disassembler
 foreign import ccall "dynamic"
    code_void :: FunPtr (CInt -> IO CInt) -> (CInt -> IO CInt)
 
+foreign import ccall "getaddr"
+  getaddr :: CUInt
+
+foreign import ccall "callertrap"
+  callertrap :: IO ()
+
 
 $(callDecl "callAsWord32" [t|Word32|])
 
@@ -59,7 +65,7 @@ main = do
 
 runstuff :: Ptr Int32 -> B.ByteString -> IO ()
 runstuff env bytecode = do
-          let emittedcode = compile $ codeInstructions $ decodeMethod bytecode
+          let emittedcode = (compile (fromIntegral getaddr)) $ codeInstructions $ decodeMethod bytecode
           (_, Right ((entryPtr, endOffset), disasm)) <- runCodeGen emittedcode env ()
           printf "entry point: 0x%08x\n" ((fromIntegral $ ptrToIntPtr entryPtr) :: Int)
 
@@ -88,6 +94,9 @@ runstuff env bytecode = do
           Right newdisasm <- disassembleBlock entryPtr endOffset
           mapM_ (putStrLn . showAtt) $ newdisasm
 
+          let addr :: Int; addr = (fromIntegral getaddr :: Int)
+          printf "getaddr: 0x%08x\n" addr
+
           return ()
 
 
@@ -100,10 +109,15 @@ exitCode = do mov esp ebp
               pop ebp
               ret
 
-compile :: [J.Instruction] -> CodeGen (Ptr Int32) s ((Ptr Word8, Int), [Instruction])
-compile insn = do
+compile :: Word32 -> [J.Instruction] -> CodeGen (Ptr Int32) s ((Ptr Word8, Int), [Instruction])
+compile trapaddr insn = do
   entryCode
   mapM compile_ins insn
+  push eax
+  mov ecx (trapaddr :: Word32)
+  call ecx
+  -- call trapaddr -- Y U DON'T WORK? (ask mr. gdb for help)
+  pop eax
   exitCode
   d <- disassemble
   c <- getEntryPoint
diff --git a/trap.c b/trap.c
new file mode 100644 (file)
index 0000000..a1e5ddc
--- /dev/null
+++ b/trap.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+void callertrap(void)
+{
+       char buf[5];
+       unsigned int *ptr = (unsigned int) (buf + 1);
+
+       printf("callertrap by 0x%08x\n", *(ptr + 4));
+       /* TODO:
+        * call magic haskell function
+        * with environment information */
+}
+
+unsigned int getaddr(void)
+{
+       return (unsigned int) callertrap;
+}