Mon Jan 16 10:04:55 EST 2012
Goal: get at all (packed) struct definitions to generate Lua tables
for translating between binary storage and table representation.
Where to start in ? I'm looking at the source, and the first thing
that looks familiar is this from AstAnalysis.hs :
analyseAST :: (MonadTrav m) => CTranslUnit -> m GlobalDecls
This uses MonadTrav class. A predefined instance of this is
Trav, which is parameterized by a user state.
For now I'm using this, inside the IO monad for getting at the file
info :: (CTranslUnit -> b) -> IO b
decls :: IO (Trav () GlobalDecls)
decls = info analyseAST
Here info parses a C file from the file system into CTranslUnit using
readFile, parseC, initPos and decls is the output of the analysis
using a Trav monad without user state.
So this gives C file to GlobalDecls dat structure.
gObjs :: Map Ident IdentDecl
gTags :: Map SUERef TagDef
gTypeDefs :: Map Ident TypeDef
Getting at the keys of gObjs and gTypeDefs:
decls tu = fst ds where
Right ds = runTrav_ $ analyseAST tu
objIdents = keys . gObjs . decls
typeDefIdents = keys . gTypeDefs . decls
These don't seem to have structure definitions. That because they are
in SUERef, which names anonymous types.
tagSUErefs = keys . gTags . decls
As a list of strings:
onFile ((Prelude.map show) . tagSUErefs)
Next: write a function that filters out all packed structures.
That was relatively straightforward. However, trying to actually map
that data to something exportable (size + basic data types like int /
float) actually requires evaluation of C code in the most general
case. So solving the problem in a generic way seems quite a hassle.
Practically I run into initializations through enums, which show up as
CVar. This probably needs access to a global symbol table.
It does give me the impression that for many C analysis tasks, a C
parser is not enough. An interpretation step is necessary, which can
probably best be left to a C compiler!
So: almost there. More after the break :)