GithubHelp home page GithubHelp logo

Comments (2)

RyanGlScott avatar RyanGlScott commented on September 15, 2024

I'm interested in looking at this, as mkX64Disassembler is proving to be a space bottleneck in large SAW proofs. I'm not sure if running mkX64Disassembler at compile time is necessarily the way to go, however. I experimented with this on the rgs/compile-time-disassembly branch, but that will cause GHC to eat up all of your memory before it can ever finish compiling Flexdis86.DefaultParser.

An alternative approach that @travitch proposed is to build the disassembler table incrementally, one instruction at a time. Similar tricks have been used in pate, but for initializing memory. It looks like the key function in flexdis86 which is responsible for table creation is mkOpcodeTable:

-- We calculate all allowed prefixes for the instruction in the first
-- argument. This simplifies parsing at the cost of extra space.
mkOpcodeTable :: [Def] -> ParserGen OpcodeTable
mkOpcodeTable defs = go [] (concatMap allPrefixedOpcodes defs)
where -- Recursive function that generates opcode table by parsing
-- opcodes in first element of list.
go :: -- Opcode bytes parsed so far.
[Word8]
-- Potential opcode definitions with the remaining opcode
-- bytes each potential definition expects.
-> [([Word8], (Prefixes, Def))]
-> ParserGen OpcodeTable
go seen l
-- If we have parsed all the opcodes expected by the remaining
-- definitions.
| all opcodeDone l = do
case l of
_ | all (expectsModRM.snd.snd) l -> do
tbl <- checkRequiredReg (snd <$> l)
case tbl of
RegTable v -> pure $! ReadModRMTable v
RegUnchecked m -> pure $! ReadModRMUnchecked m
[([],(pfx, d))] -> assert (not (expectsModRM d)) $
return $! SkipModRM pfx d
_ -> error $ "mkOpcodeTable: ambiguous operators " ++ show l
-- If we still have opcodes to parse, check that all definitions
-- expect at least one more opcode, and generate table for next
-- opcode match.
| otherwise = assert (all (not.opcodeDone) l) $ do
let v = partitionBy l
g i = go (fromIntegral i:seen) (v V.! i)
tbl <- V.generateM 256 g
pure $! OpcodeTable tbl
-- Return whether opcode parsing is done.
opcodeDone :: ([Word8], a) -> Bool
opcodeDone (remaining,_) = null remaining

It's not entirely obvious at a first glance what the best approach is for making this incremental. For starters, the OpcodeTable data type isn't just a flat Vector, so it's unclear how to map opcodes to instructions cleanly:

data OpcodeTable
= OpcodeTable !NextOpcodeTable
| SkipModRM !Prefixes !Def
| ReadModRMTable !(V.Vector ModTable)
| ReadModRMUnchecked !ModTable
deriving (Generic)
instance DS.NFData OpcodeTable
-- | A NextOpcodeTable describes a table of parsers to read based on the bytes.
type NextOpcodeTable = V.Vector OpcodeTable

@travitch, do you have any thoughts on a possible design here?

from flexdis86.

travitch avatar travitch commented on September 15, 2024

Moving the discussion of parse table sizes to #40 because solving that is orthogonal to whether or not we compute the tables at compile time

from flexdis86.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.