<commit_before>module Simulator.Util
  ( decodeRegs
  , readReg
  , regSetter
  , formatCpuState
  , formatInstr
  , putStrLn
  ) where

import Control.Lens
import Control.Monad.IO.Class
import Data.Bits
import Data.Maybe
import Data.Word
import Prelude hiding (putStrLn)
import Simulator.Types
import Text.Printf
import qualified Prelude as P

decodeRegs :: Word8 -> (Word8, Word8)
decodeRegs w = (shiftR w 4, w .&. 0x0F)

fun r = fromJust . lookup r

readReg :: Word8 -> CpuState -> Word32
readReg r = fun r [(0, _eax), (1, _ecx), (2, _edx), (3, _ebx),
                   (4, _esp), (5, _ebp), (6, _esi), (7, _edi)]

regSetter :: RegId -> ASetter' CpuState Word32
regSetter r = fun r [(0, eax), (1, ecx), (2, edx), (3, ebx),
                     (4, esp), (5, ebp), (6, esi), (7, edi)]

showReg :: RegId -> String
showReg r = fun r [(0, "%eax"), (1, "%ecx"), (2, "%edx"), (3, "%ebx"),
                   (4, "%esp"), (5, "%ebp"), (6, "%esi"), (7, "%edi")]

showHex :: PrintfArg a => a -> String
showHex w = printf "0x%08x" w

showFlag :: Bool -> String
showFlag True = "1"
showFlag _    = "0"

formatCpuState :: CpuState -> String
formatCpuState CpuState {..} =
  unlines
    [ unwords ["PC:", f _pc, "ZF:", g _zF, "SF:", g _sF,
               "OF:", g _oF, "STAT:", show _stat],
      unwords ["%eax:", f _eax, "%ecx:", f _ecx, "%edx:", f _edx, "%ebx:", f _ebx,
               "%esi:", f _esi, "%edi:", f _edi, "%esp:", f _esp, "%ebp:", f _ebp]
    ]
  where
  f = showHex
  g = showFlag

formatInstr :: Instr -> String<commit_msg>Trivial<commit_after>module Simulator.Util
  ( decodeRegs
  , readReg
  , regSetter
  , formatCpuState
  , formatInstr
  , putStrLn
  ) where

import Control.Lens
import Control.Monad.IO.Class
import Data.Bits
import Data.Maybe
import Data.Word
import Prelude hiding (putStrLn)
import Simulator.Types
import Text.Printf
import qualified Prelude as P

decodeRegs :: Word8 -> (Word8, Word8)
decodeRegs w = (shiftR w 4, w .&. 0x0F)

fun r = fromJust . lookup r

readReg :: Word8 -> CpuState -> Word32
readReg r = fun r [(0, _eax), (1, _ecx), (2, _edx), (3, _ebx),
                   (4, _esp), (5, _ebp), (6, _esi), (7, _edi)]

regSetter :: RegId -> ASetter' CpuState Word32
regSetter r = fun r [(0, eax), (1, ecx), (2, edx), (3, ebx),
                     (4, esp), (5, ebp), (6, esi), (7, edi)]

showReg :: RegId -> String
showReg r = fun r [(0, "%eax"), (1, "%ecx"), (2, "%edx"), (3, "%ebx"),
                   (4, "%esp"), (5, "%ebp"), (6, "%esi"), (7, "%edi")]

showHex :: PrintfArg a => a -> String
showHex w = printf "0x%08x" w

showFlag :: Bool -> String
showFlag True = "1"
showFlag _    = "0"

formatCpuState :: CpuState -> String
formatCpuState CpuState {..} =
  unlines
    [ unwords ["PC:", f _pc, "ZF:", g _zF, "SF:", g _sF,
               "OF:", g _oF, "STAT:", show _stat],
      unwords ["%eax:", f _eax, "%ecx:", f _ecx, "%edx:", f _edx, "%ebx:", f _ebx],
      unwords ["%esi:", f _esi, "%edi:", f _edi, "%esp:", f _esp, "%ebp:", f _ebp]
    ]
  where
  f = showHex
  g = showFlag

formatInstr :: Instr -> String
{'fraction_non_alphanumeric': 0.19131570355860267, 'fraction_numerical': 0.02611818478615736, 'mean_word_length': 2.903184713375796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6515951', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1211, 'n_words': 386}