PrettyPrint.hs 18.8 KB
Newer Older
1
2
3
4
5
6
7
8
{-# LANGUAGE NoImplicitPrelude, FlexibleInstances #-}

module Mu.PrettyPrint
  ( PrettyPrint(..)
  ) where

import Prelude
       (Maybe(..), Show(..), String, unwords, map, concat, unlines, error,
9
        reverse, ($), (.), (++), init, last, concatMap)
10
11

import Data.Char (toUpper)
12
import Mu.AST
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import Text.Printf (printf)
import Control.Applicative ((<$>))
import Control.Monad (Monad(..), mapM)
import Control.Monad.Trans.Reader (runReader, Reader, ask, local)

class PrettyPrint a  where
    ppFormat :: a -> Reader String String
    pp :: a -> String
    pp t = runReader (ppFormat t) ""

instance (PrettyPrint a) =>
         PrettyPrint [a] where
    ppFormat t = do
        ind <- ask
        lst <- (mapM ppFormat t)
        return $ ind ++ (unwords lst)

30
instance PrettyPrint Type where
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
    ppFormat uType = do
        ind <- ask
        return $
            ind ++
            (case uType of
                 MuInt len -> printf "int<%d>" len
                 MuFloat -> "float"
                 MuDouble -> "double"
                 Ref t -> printf "ref<%s>" (pp t)
                 IRef t -> printf "iref<%s>" (pp t)
                 WeakRef t -> printf "weakref<%s>" (pp t)
                 UPtr t -> printf "uptr<%s>" (pp t)
                 Struct lst -> printf "struct<%s>" (unwords $ map pp lst)
                 Array t len -> printf "array<%s %d>" (pp t) len
                 Hybrid lst t ->
                     printf "hybrid<%s %s>" (unwords $ map pp lst) (pp t)
                 Void -> "void"
                 ThreadRef -> "threadref"
                 StackRef -> "stackref"
                 FrameCursorRef -> "framecursorref"
                 TagRef64 -> "tagref64"
                 Vector t len -> printf "vector<%s %d>" (pp t) len
                 FuncRef sig -> printf "funcref<%s>" (pp sig)
                 UFuncPtr sig -> printf "ufuncptr<%s>" (pp sig))

instance PrettyPrint CallConvention where
    ppFormat conv = do
        ind <- ask
        return $
            ind ++
            (case conv of
62
                 MuCallConvention -> "#DEFAULT"
63
                 ForeignCallConvention s -> '#' : s)
64

65
instance PrettyPrint VarName where
66
    ppFormat (VarName iD) = do
67
        ind <- ask
68
        return $ printf "%s%s%s" ind "@" $ show iD
69

70
71
instance PrettyPrint TypedefName where
    ppFormat (TypedefName name) = do
72
        ind <- ask
73
        return $ ind ++ ('@' : show name)
74

75
76
instance PrettyPrint SignatureName where
    ppFormat (SignatureName name) = do
77
        ind <- ask
78
        return $ ind ++ ('@' : show name)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

instance PrettyPrint ExceptionClause where
    ppFormat (ExceptionClause nor exc) = do
        ind <- ask
        return $ printf "%sEXC(%s %s)" ind (pp nor) (pp exc)

instance PrettyPrint WPExceptionClause where
    ppFormat (WPExceptionClause dest) = do
        ind <- ask
        return $ printf "%sWPEXC(%s)" ind (pp dest)

instance PrettyPrint KeepAliveClause where
    ppFormat (KeepAlive vars) = do
        ind <- ask
        return $ printf "%sKEEPALIVE(%s)" ind (pp vars)

instance PrettyPrint DestinationClause where
    ppFormat (DestinationClause dest args) = do
        ind <- ask
98
        return $ printf "%s%s(%s)" ind ('%' : show dest) (pp args)
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

instance PrettyPrint Flag where
    ppFormat (Flag s) = do
        ind <- ask
        return $ ind ++ ('#' : s)

instance PrettyPrint BinaryOp where
    ppFormat op = do
        ind <- ask
        return $ ind ++ (map toUpper $ show op)

instance PrettyPrint ConvertOp where
    ppFormat op = ask >>= (return . (++ (show op)))

instance PrettyPrint CompareOp where
    ppFormat op = ask >>= (return . (++ (show op)))

instance PrettyPrint AtomicRMWOp where
    ppFormat op = ask >>= (return . (++ (show op)))

instance PrettyPrint MemoryOrder where
    ppFormat op = ask >>= (return . (++ (show op)))

instance PrettyPrint Expression where
    ppFormat expr = do
        ind <- ask
        return $
            ind ++
            (case expr of
                 BinaryOperation op t1 v1 v2 exec ->
                     printf
                         "%s <%s> %s %s%s"
                         (pp op)
                         (pp t1)
                         (pp v1)
                         (pp v2)
                         (printMaybe pp exec)
                 CompareOperation op t1 v1 v2 ->
                     printf "%s <%s> %s %s" (pp op) (pp t1) (pp v1) (pp v2)
                 ConvertOperation op t1 v1 v2 exec ->
                     printf
                         "%s <%s %s> %s%s"
                         (pp op)
                         (pp t1)
                         (pp v1)
                         (pp v2)
                         (printMaybe pp exec)
                 AtomicRMWOperation f1 memOrd op t1 v1 v2 exec ->
                     printf
                         "ATOMICRMW%s %s %s <%s> %s %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (pp memOrd)
                         (pp op)
                         (pp t1)
                         (pp v1)
                         (pp v2)
                         (printMaybe pp exec)
                 CmpXchg f1 f2 memOrd1 memOrd2 t1 v1 v2 v3 exec ->
                     printf
                         "CMPXCHG%s%s %s %s <%s> %s %s %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (if f2
                              then " WEAK"
                              else "")
                         (pp memOrd1)
                         (pp memOrd2)
                         (pp t1)
                         (pp v1)
                         (pp v2)
                         (pp v3)
                         (printMaybe pp exec)
                 Fence ord -> printf "FENCE %s" (pp ord)
                 New t1 exec -> printf "NEW <%s>%s" (pp t1) (printMaybe pp exec)
                 NewHybrid t1 t2 v1 exec ->
                     printf
                         "NEWHYBRID <%s %s> %s%s"
                         (pp t1)
                         (pp t2)
                         (pp v1)
                         (printMaybe pp exec)
                 Alloca t1 exec ->
                     printf "ALLOCA <%s>%s" (pp t1) (printMaybe pp exec)
                 AllocaHybrid t1 t2 v1 exec ->
                     printf
                         "ALLOCAHYBRID <%s %s> %s%s"
                         (pp t1)
                         (pp t2)
                         (pp v1)
                         (printMaybe pp exec)
                 Return lst -> printf "RET %s" (printArgList lst)
                 Throw v1 -> printf "THROW %s" (pp v1)
                 Call s1 v1 lst exec alive ->
                     printf
                         "CALL <%s> %s %s%s%s"
                         (pp s1)
                         (pp v1)
                         (printArgList lst)
                         (printMaybe pp exec)
                         (printMaybe pp alive)
                 CCall conv t1 s1 v1 argLst exec alive ->
                     printf
                         "CCALL %s <%s %s> %s %s%s%s"
                         (pp conv)
                         (pp t1)
                         (pp s1)
                         (pp v1)
                         (printArgList argLst)
                         (printMaybe pp exec)
                         (printMaybe pp alive)
                 TailCall s1 v1 lst ->
                     printf
                         "TAILCALL <%s> %s %s"
                         (pp s1)
                         (pp v1)
                         (printArgList lst)
                 Branch1 dest -> printf "BRANCH %s" (pp dest)
                 Branch2 cond d1 d2 ->
                     printf "BRANCH2 %s %s %s" (pp cond) (pp d1) (pp d2)
                 WatchPoint name iD lst dis ena exec alive ->
                     printf
                         "[%s] WATCHPOINT %d %s %s %s%s%s"
                         (pp name)
                         iD
                         (printTypeList lst)
                         (pp dis)
                         (pp ena)
                         (printMaybe pp exec)
                         (printMaybe pp alive)
                 Trap name lst exec alive ->
                     printf
                         "[%s] TRAP %s%s%s"
                         (pp name)
                         (printTypeList lst)
                         (printMaybe pp exec)
                         (printMaybe pp alive)
                 WPBranch iD dis ena ->
                     printf "WPBRANCH %d %s %s" iD (pp dis) (pp ena)
                 Switch t1 v1 dest blocks ->
                     printf
                         "SWITCH <%s> %s %s {\n%s}"
                         (pp t1)
                         (pp v1)
                         (pp dest)
                         (concat $
                          runReader (mapM printBlock blocks) (ind ++ "\t"))
                 SwapStack v1 csClause nsClause exec alive ->
                     printf
                         "SWAPSTACK %s %s %s%s%s"
                         (pp v1)
                         (pp csClause)
                         (pp nsClause)
                         (printMaybe pp exec)
                         (printMaybe pp alive)
                 NewThread v1 nsClause exec ->
                     printf
                         "NEWTHREAD %s %s%s"
                         (pp v1)
                         (pp nsClause)
                         (printMaybe pp exec)
262
                 Comminst inst fLst tLst vLst exec alive ->
263
264
                     printf
                         "COMMINST @%s%s%s%s%s%s%s"
265
266
267
268
269
                         (show inst)
                         (printFlagList fLst)
                         (printTypeList tLst)
                         --(printSigList sLst)
                         (printArgList vLst)
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
                         (printMaybe pp exec)
                         (printMaybe pp alive)
                 Load f1 memOrd t1 v1 exec ->
                     printf
                         "LOAD%s%s <%s> %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (printMaybe pp memOrd)
                         (pp t1)
                         (pp v1)
                         (printMaybe pp exec)
                 Store f1 memOrd t1 v1 v2 exec ->
                     printf
                         "STORE%s%s <%s> %s %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (printMaybe pp memOrd)
                         (pp t1)
                         (pp v1)
                         (pp v2)
                         (printMaybe pp exec)
293
                 {-
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
                 ExtractValueS t1 index v1 exec ->
                     printf
                         "EXTRACTVALUE <%s %d> %s%s"
                         (pp t1)
                         index
                         (pp v1)
                         (printMaybe pp exec)
                 InsertValueS t1 index v1 v2 exec ->
                     printf
                         "INSERTVALUE <%s %d> %s %s%s"
                         (pp t1)
                         index
                         (pp v1)
                         (pp v2)
                         (printMaybe pp exec)
309
310
                 -}
                 ExtractValue t1 index v1 ->
311
                     printf
312
                         "EXTRACTVALUE <%s %s> %s"
313
                         (pp t1)
314
                         index
315
                         (pp v1)
316
                 InsertValue t1 index v1 newVal ->
317
                     printf
318
                         "INSERTVALUE <%s %s> %s %s"
319
                         (pp t1)
320
                         index
321
                         (pp v1)
322
323
                         (pp newVal)
                 ShuffleVector t1 t2 v1 v2 mask ->
324
                     printf
325
                         "SHUFFLEVECTOR <%s %s> %s %s"
326
327
328
329
                         (pp t1)
                         (pp t2)
                         (pp v1)
                         (pp v2)
330
331
                         (pp mask)
                 GetIRef t1 v1 ->
332
                     printf
333
                         "GETIREF <%s> %s"
334
335
                         (pp t1)
                         (pp v1)
336
                 GetFieldIRef f1 t1 index v1 ->
337
338
339
340
341
342
343
344
                     printf
                         "GETFIELDIREF%s <%s %d> %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (pp t1)
                         index
                         (pp v1)
345
                 GetElemIRef f1 t1 t2 v1 v2 ->
346
347
348
349
350
351
352
353
354
                     printf
                         "GETELEMIREF%s <%s %s> %s %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (pp t1)
                         (pp t2)
                         (pp v1)
                         (pp v2)
355
                 ShiftIRef f1 t1 t2 v1 v2 ->
356
357
358
359
360
361
362
363
364
                     printf
                         "SHIFTIREF%s <%s %s> %s %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (pp t1)
                         (pp t2)
                         (pp v1)
                         (pp v2)
365
                 GetVarPartIRef f1 t1 v1 ->
366
367
368
369
370
371
372
                     printf
                         "GETVARPARTIREF%s <%s> %s%s"
                         (if f1
                              then " PTR"
                              else "")
                         (pp t1)
                         (pp v1)
373
374
                 --Comment str -> printf "//%s" str
                 )
375
      where
376
        printTypeList :: [TypedefName] -> String
377
        printTypeList lst = "<" ++ pp lst ++ ">"
378
379
        --printSigList :: [FuncSig] -> String
        --printSigList lst = "<[" ++ (pp lst) ++ "]>"
380
        printArgList :: [VarName] -> String
381
382
383
384
385
386
387
388
        printArgList lst = "(" ++ (pp lst) ++ ")"
        printFlagList :: [Flag] -> String
        printFlagList lst = "[" ++ (unwords $ map pp lst) ++ "]"
        printMaybe :: (a -> String) -> Maybe a -> String
        printMaybe f mVal =
            case mVal of
                Nothing -> ""
                Just val -> ' ' : f val
389
        printBlock :: (VarName, DestinationClause) -> Reader String String
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
        printBlock (arg, destClause) = do
            ind <- ask
            return $ printf "%s%s %s\n" ind (pp arg) (pp destClause)

instance PrettyPrint CurStackClause where
    ppFormat clause = do
        ind <- ask
        return $
            ind ++
            (case clause of
                 RetWith lst -> printf "RET_WITH <%s>" (pp lst)
                 KillOld -> "KILL_OLD")

instance PrettyPrint NewStackClause where
    ppFormat clause = do
        ind <- ask
        return $
            ind ++
            (case clause of
                 PassValues tLst vLst ->
                     printf "PASS_VALUES <%s> (%s)" (pp tLst) (pp vLst)
                 ThrowExc exc -> printf "THROW_EXC %s" (pp exc))

413
414
instance PrettyPrint a => PrettyPrint (Assigned a) where
    ppFormat ((:=) vars expr) = do
415
416
417
418
419
        ind <- ask
        case vars of
            [] -> return $ ind ++ pp expr
            [var] -> return $ printf "%s%s = %s" ind (pp var) (pp expr)
            _ -> return $ printf "%s%s = %s" ind (printArgList vars) (pp expr)
420
                where printArgList :: [VarName] -> String
421
422
                      printArgList lst = "(" ++ (pp lst) ++ ")"

423
424
instance PrettyPrint Definition where
    ppFormat defn = do
425
        ind <- ask
426
427
        case defn of
            Constant n ty con -> return $
428
                printf "%s.const %s <%s> = %s" ind (show n) (pp ty) (pp con)
429

430
            {-
431
            Constdefn var@(VarName _ dType) val ->
432
433
                return $
                printf "%s.const %s <%s> = %s" ind (pp var) (pp dType) val
434
            -}
435
436
437
438

            TypeDefinition n ty ->
                return $ printf "%s.typedef %s = %s" ind (show n) (pp ty)

439
            SignatureDefinition var tLst ret ->
440
441
442
443
444
445
446
                return $
                printf
                    "%s.funcsig %s = %s -> %s"
                    ind
                    (pp var)
                    (printSig tLst)
                    (printSig ret)
447
                where printSig :: [TypedefName] -> String
448
                      printSig lst = "(" ++ (pp lst) ++ ")"
449
450
            FunctionDefinition name ver sig firstBlock restBlock -> do
                pBody <- local (++ "\t") (mapM ppFormat $ reverse $ firstBlock : restBlock)
451
452
453
454
                return $
                    printf
                        "%s.funcdef @%s VERSION %s <%s> {\n%s\t}"
                        ind
455
456
                        (show name)
                        ('%' : show ver)
457
458
                        (pp sig)
                        (unlines pBody)
459
            {-
460
461
            Functiondefn name sig ->
                return $ printf "%s.funcdefn @%s = <%s>" ind name (pp sig)
462
463
464
465
            -}
            GlobalCell var uType ->
                return $ printf "%s.global %s <%s>" ind (show var) (show uType)
            ExposedFunction name fName cconv cookie ->
466
467
468
469
                return $
                printf
                    "%s.expose @%s = @%s <%s> %s"
                    ind
470
471
                    (show name)
                    (show fName)
472
                    (pp cconv)
473
474
475
476
477
478
479
480
481
482
483
484
485
486
                    (show cookie)

instance PrettyPrint ConstConstructor where
    ppFormat con = do
        ind <- ask
        return $ ind ++ (case con of
            IntCtor x -> show x
            FloatCtor x -> show x
            DoubleCtor x -> show x
            ListCtor xs -> (case xs of
                [] -> "{}"
                _  -> "{" ++ (concatMap (\x -> pp x ++ " ") $ init xs) ++ (pp $ last xs) ++ "}")
            NullCtor -> "NULL"
            ExternCtor bs -> "EXTERN \"" ++ (show bs) ++ "\"")
487
488
489

instance PrettyPrint BasicBlock where
    ppFormat (BasicBlock name params exec instructions term) = do
490
491
        -- Why. Is. This. Here. v
        --_ <- error "This happened"
492
493
494
495
496
497
498
        ind <- ask
        blocks <- local (++ "\t") (printBlocks $ reverse instructions)
        termInst <- local (++ "\t") (ppFormat term)
        return $
            printf
                "%s%s (%s)%s:\n%s%s"
                ind
499
                ('%' : show name)
500
501
502
503
504
                (printParams params)
                (printExec exec)
                (blocks)
                (termInst)
      where
505
        printParams :: [(VarName, TypedefName)] -> String
506
        printParams lst =
507
508
            unwords $ map (\(name, t1) -> printf "<%s> %s" (pp t1) (pp name)) lst
        printBlocks :: [Assigned Expression] -> Reader String String
509
        printBlocks lst = unlines <$> (mapM ppFormat lst)
510
        printExec :: Maybe VarName -> String
511
512
513
514
515
        printExec e =
            case e of
                Nothing -> ""
                Just exc -> printf " [%s]" (pp exc)

516
517
instance PrettyPrint Bundle where
    ppFormat (Bundle prog) = unlines <$> mapM ppFormat prog