From cb978f2211a1dad0eb2da1716073996c20f78f0d Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Sun, 18 Mar 2012 05:19:43 +0100 Subject: [PATCH] trapping code: transition from native to haskell rts --- Makefile | 4 ++-- Mate.hs | 20 +++++++++++++++++--- trap.c | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 trap.c diff --git a/Makefile b/Makefile index 8965f9a..7bc23cf 100644 --- 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 600e29b..3fcfca5 100644 --- 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 index 0000000..a1e5ddc --- /dev/null +++ b/trap.c @@ -0,0 +1,17 @@ +#include + +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; +} -- 2.25.1