forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiFileParser.hs
More file actions
435 lines (404 loc) · 14.5 KB
/
HiFileParser.hs
File metadata and controls
435 lines (404 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module HiFileParser
( Interface(..)
, List(..)
, Dictionary(..)
, Module(..)
, Usage(..)
, Dependencies(..)
, getInterface
, fromFile
) where
{- HLINT ignore "Reduce duplication" -}
import Control.Monad (replicateM, replicateM_)
import Data.Binary (Get, Word32)
import Data.Binary.Get (Decoder (..), bytesRead,
getByteString, getInt64be,
getWord32be, getWord64be,
getWord8, lookAhead,
runGetIncremental, skip)
import Data.Bool (bool)
import Data.ByteString.Lazy.Internal (defaultChunkSize)
import Data.Char (chr)
import Data.Functor (void, ($>))
import Data.List (find)
import Data.Maybe (catMaybes)
import Data.Semigroup ((<>))
import qualified Data.Vector as V
import GHC.IO.IOMode (IOMode (..))
import Numeric (showHex)
import RIO.ByteString as B (ByteString, hGetSome, null)
import System.IO (withBinaryFile)
type IsBoot = Bool
type ModuleName = ByteString
newtype List a = List
{ unList :: [a]
} deriving newtype (Show)
newtype Dictionary = Dictionary
{ unDictionary :: V.Vector ByteString
} deriving newtype (Show)
newtype Module = Module
{ unModule :: ModuleName
} deriving newtype (Show)
newtype Usage = Usage
{ unUsage :: FilePath
} deriving newtype (Show)
data Dependencies = Dependencies
{ dmods :: List (ModuleName, IsBoot)
, dpkgs :: List (ModuleName, Bool)
, dorphs :: List Module
, dfinsts :: List Module
, dplugins :: List ModuleName
} deriving (Show)
data Interface = Interface
{ deps :: Dependencies
, usage :: List Usage
} deriving (Show)
-- | Read a block prefixed with its length
withBlockPrefix :: Get a -> Get a
withBlockPrefix f = getWord32be *> f
getBool :: Get Bool
getBool = toEnum . fromIntegral <$> getWord8
getString :: Get String
getString = fmap (chr . fromIntegral) . unList <$> getList getWord32be
getMaybe :: Get a -> Get (Maybe a)
getMaybe f = bool (pure Nothing) (Just <$> f) =<< getBool
getList :: Get a -> Get (List a)
getList f = do
i <- getWord8
l <-
if i == 0xff
then getWord32be
else pure (fromIntegral i :: Word32)
List <$> replicateM (fromIntegral l) f
getTuple :: Get a -> Get b -> Get (a, b)
getTuple f g = (,) <$> f <*> g
getByteStringSized :: Get ByteString
getByteStringSized = do
size <- getInt64be
getByteString (fromIntegral size)
getDictionary :: Int -> Get Dictionary
getDictionary ptr = do
offset <- bytesRead
skip $ ptr - fromIntegral offset
size <- fromIntegral <$> getInt64be
Dictionary <$> V.replicateM size getByteStringSized
getCachedBS :: Dictionary -> Get ByteString
getCachedBS d = go =<< getWord32be
where
go i =
case unDictionary d V.!? fromIntegral i of
Just bs -> pure bs
Nothing -> fail $ "Invalid dictionary index: " <> show i
getFP :: Get ()
getFP = void $ getWord64be *> getWord64be
getInterface721 :: Dictionary -> Get Interface
getInterface721 d = do
void getModule
void getBool
replicateM_ 2 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = getCachedBS d *> (Module <$> getCachedBS d)
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
pure (List [])
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface741 :: Dictionary -> Get Interface
getInterface741 d = do
void getModule
void getBool
replicateM_ 3 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = getCachedBS d *> (Module <$> getCachedBS d)
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
pure (List [])
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
2 -> Just . Usage <$> getString <* getWord64be <* getWord64be
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface761 :: Dictionary -> Get Interface
getInterface761 d = do
void getModule
void getBool
replicateM_ 3 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = getCachedBS d *> (Module <$> getCachedBS d)
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
pure (List [])
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
2 -> Just . Usage <$> getString <* getWord64be <* getWord64be
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface781 :: Dictionary -> Get Interface
getInterface781 d = do
void getModule
void getBool
replicateM_ 3 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = getCachedBS d *> (Module <$> getCachedBS d)
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
pure (List [])
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
2 -> Just . Usage <$> getString <* getFP
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface801 :: Dictionary -> Get Interface
getInterface801 d = do
void getModule
void getWord8
replicateM_ 3 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = getCachedBS d *> (Module <$> getCachedBS d)
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
pure (List [])
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
2 -> Just . Usage <$> getString <* getFP
3 -> getModule *> getFP $> Nothing
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface821 :: Dictionary -> Get Interface
getInterface821 d = do
void getModule
void $ getMaybe getModule
void getWord8
replicateM_ 3 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = do
idType <- getWord8
case idType of
0 -> void $ getCachedBS d
_ ->
void $
getCachedBS d *> getList (getTuple (getCachedBS d) getModule)
Module <$> getCachedBS d
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
pure (List [])
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
2 -> Just . Usage <$> getString <* getFP
3 -> getModule *> getFP $> Nothing
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface841 :: Dictionary -> Get Interface
getInterface841 d = do
void getModule
void $ getMaybe getModule
void getWord8
replicateM_ 5 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = do
idType <- getWord8
case idType of
0 -> void $ getCachedBS d
_ ->
void $
getCachedBS d *> getList (getTuple (getCachedBS d) getModule)
Module <$> getCachedBS d
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
pure (List [])
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
2 -> Just . Usage <$> getString <* getFP
3 -> getModule *> getFP $> Nothing
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface861 :: Dictionary -> Get Interface
getInterface861 d = do
void getModule
void $ getMaybe getModule
void getWord8
replicateM_ 6 getFP
void getBool
void getBool
Interface <$> getDependencies <*> getUsage
where
getModule = do
idType <- getWord8
case idType of
0 -> void $ getCachedBS d
_ ->
void $
getCachedBS d *> getList (getTuple (getCachedBS d) getModule)
Module <$> getCachedBS d
getDependencies =
withBlockPrefix $
Dependencies <$> getList (getTuple (getCachedBS d) getBool) <*>
getList (getTuple (getCachedBS d) getBool) <*>
getList getModule <*>
getList getModule <*>
getList (getCachedBS d)
getUsage = withBlockPrefix $ List . catMaybes . unList <$> getList go
where
go :: Get (Maybe Usage)
go = do
usageType <- getWord8
case usageType of
0 -> getModule *> getFP *> getBool $> Nothing
1 ->
getCachedBS d *> getFP *> getMaybe getFP *>
getList (getTuple (getWord8 *> getCachedBS d) getFP) *>
getBool $> Nothing
2 -> Just . Usage <$> getString <* getFP
3 -> getModule *> getFP $> Nothing
_ -> fail $ "Invalid usageType: " <> show usageType
getInterface :: Get Interface
getInterface = do
magic <- getWord32be
case magic of
-- x32
0x1face -> void getWord32be
-- x64
0x1face64 -> void getWord64be
invalidMagic -> fail $ "Invalid magic: " <> showHex invalidMagic ""
-- ghc version
version <- getString
-- way
void getString
-- dict_ptr
dictPtr <- getWord32be
-- dict
dict <- lookAhead $ getDictionary $ fromIntegral dictPtr
-- symtable_ptr
void getWord32be
let versions =
[ ("8061", getInterface861)
, ("8041", getInterface841)
, ("8021", getInterface821)
, ("8001", getInterface801)
, ("7081", getInterface781)
, ("7061", getInterface761)
, ("7041", getInterface741)
, ("7021", getInterface721)
]
case snd <$> find ((version >=) . fst) versions of
Just f -> f dict
Nothing -> fail $ "Unsupported version: " <> version
fromFile :: FilePath -> IO (Either String Interface)
fromFile fp = withBinaryFile fp ReadMode go
where
go h =
let feed (Done _ _ iface) = pure $ Right iface
feed (Fail _ _ msg) = pure $ Left msg
feed (Partial k) = do
chunk <- hGetSome h defaultChunkSize
feed $ k $ if B.null chunk then Nothing else Just chunk
in feed $ runGetIncremental getInterface