Entry 479

Haskell generation of assembly code via Harpy

   

Submitted by Austin Seipp on Oct. 9, 2007 at 8:08 a.m.
Language: Haskell. Code size: 1.1 KB.

import Harpy.X86Disassembler
import Control.Monad.Trans
import Harpy.CodeGenMonad
import Harpy.X86Assembler
import System.Environment
import System.IO
import Foreign

stuff :: CodeGen e s ()
stuff = do
  ensureBufferSize (10*16)  -- setup

  push ebp
  mov ebp esp
  push ecx
  mov ecx (Disp 8,ebp)
  add ecx (1 :: Word32)
  mov eax ecx
  pop ecx
  mov esp ebp
  pop ebp
  ret

$(callDecl "callStuff" [t|Word32 -> Word32|])

run f = do
  stuff
  io $ putStr "Number: "
  i <- io readLn
  x <- callStuff i
  io $ putStr ("Result: ") >> print x
  d <- disassemble
  io $ putStrLn "Disassembly: " >> mapM_ (putStrLn . f) d
    where
      io = liftIO

main = do
  a   <- getArgs
  fmt <- case a of
    []    -> return showIntel
    (x:_) -> checkArg x

  hSetBuffering stdout NoBuffering
  (_,r) <- runCodeGen (run fmt) () ()
  case r of
    Right _ -> return ()
    Left e  -> putStrLn (show e)
 where
   checkArg x
       | x == "-att"   = return showAtt
       | x == "-intel" = return showIntel
       | otherwise     = return showIntel

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).