-
Notifications
You must be signed in to change notification settings - Fork 14
/
PP.hs
1254 lines (1069 loc) · 47.7 KB
/
PP.hs
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Rank2Types #-}
-- |
-- Module : Text.LLVM.PP
-- Copyright : Trevor Elliott 2011-2016
-- License : BSD3
--
-- Maintainer : awesomelyawesome@gmail.com
-- Stability : experimental
-- Portability : unknown
--
-- This is the pretty-printer for llvm assembly versions 3.6 and lower.
--
module Text.LLVM.PP where
import Text.LLVM.AST
import Text.LLVM.Triple.AST (TargetTriple)
import Text.LLVM.Triple.Print (printTriple)
import Control.Applicative ((<|>))
import Data.Bits ( shiftR, (.&.) )
import Data.Char (isAlphaNum,isAscii,isDigit,isPrint,ord,toUpper)
import Data.List (intersperse)
import qualified Data.Map as Map
import Data.Maybe (catMaybes,fromMaybe,isJust)
import Numeric (showHex)
import Text.PrettyPrint.HughesPJ
import Data.Int
import Prelude hiding ((<>))
-- Pretty-printer Config -------------------------------------------------------
type LLVM = ?config :: Config
-- | The differences between various versions of the llvm textual AST.
data Config = Config { cfgLoadImplicitType :: Bool
-- ^ True when the type of the result of a load is
-- derived from its pointer argument, or supplied
-- implicitly.
, cfgGEPImplicitType :: Bool
-- ^ True when the type of the result of the GEP
-- instruction is implied.
, cfgUseDILocation :: Bool
}
withConfig :: Config -> (LLVM => a) -> a
withConfig cfg body = let ?config = cfg in body
ppLLVM, ppLLVM35, ppLLVM36, ppLLVM37, ppLLVM38 :: (LLVM => a) -> a
ppLLVM = ppLLVM38
ppLLVM35 = ppLLVM36
ppLLVM36 = withConfig Config { cfgLoadImplicitType = True
, cfgGEPImplicitType = True
, cfgUseDILocation = False
}
ppLLVM37 = withConfig Config { cfgLoadImplicitType = False
, cfgGEPImplicitType = False
, cfgUseDILocation = True
}
ppLLVM38 = withConfig Config { cfgLoadImplicitType = False
, cfgGEPImplicitType = False
, cfgUseDILocation = True
}
checkConfig :: LLVM => (Config -> Bool) -> Bool
checkConfig p = p ?config
-- Modules ---------------------------------------------------------------------
ppModule :: LLVM => Module -> Doc
ppModule m = foldr ($+$) empty
$ ppSourceName (modSourceName m)
: ppTargetTriple (modTriple m)
: ppDataLayout (modDataLayout m)
: ppInlineAsm (modInlineAsm m)
: concat [ map ppTypeDecl (modTypes m)
, map ppGlobal (modGlobals m)
, map ppGlobalAlias (modAliases m)
, map ppDeclare (modDeclares m)
, map ppDefine (modDefines m)
, map ppNamedMd (modNamedMd m)
, map ppUnnamedMd (modUnnamedMd m)
, map ppComdat (Map.toList (modComdat m))
]
-- Source filename -------------------------------------------------------------
ppSourceName :: Maybe String -> Doc
ppSourceName Nothing = empty
ppSourceName (Just sn) = "source_filename" <+> char '=' <+> doubleQuotes (text sn)
-- Metadata --------------------------------------------------------------------
ppNamedMd :: NamedMd -> Doc
ppNamedMd nm =
sep [ ppMetadata (text (nmName nm)) <+> char '='
, ppMetadata (braces (commas (map (ppMetadata . int) (nmValues nm)))) ]
ppUnnamedMd :: LLVM => UnnamedMd -> Doc
ppUnnamedMd um =
sep [ ppMetadata (int (umIndex um)) <+> char '='
, distinct <+> ppValMd (umValues um) ]
where
distinct | umDistinct um = "distinct"
| otherwise = empty
-- Aliases ---------------------------------------------------------------------
ppGlobalAlias :: LLVM => GlobalAlias -> Doc
ppGlobalAlias g = ppSymbol (aliasName g)
<+> char '='
<+> ppMaybe ppLinkage (aliasLinkage g)
<+> ppMaybe ppVisibility (aliasVisibility g)
<+> body
where
val = aliasTarget g
body = case val of
ValSymbol _sym -> ppType (aliasType g) <+> ppValue val
_ -> ppValue val
-- Target triple ---------------------------------------------------------------
-- | Pretty print a 'TargetTriple'
ppTargetTriple :: TargetTriple -> Doc
ppTargetTriple triple = "target" <+> "triple" <+> char '='
<+> doubleQuotes (text (printTriple triple))
-- Data Layout -----------------------------------------------------------------
-- | Pretty print a data layout specification.
ppDataLayout :: DataLayout -> Doc
ppDataLayout [] = empty
ppDataLayout ls = "target" <+> "datalayout" <+> char '='
<+> doubleQuotes (hcat (intersperse (char '-') (map ppLayoutSpec ls)))
-- | Pretty print a single layout specification.
ppLayoutSpec :: LayoutSpec -> Doc
ppLayoutSpec ls =
case ls of
BigEndian -> char 'E'
LittleEndian -> char 'e'
PointerSize 0 sz abi pref -> char 'p' <> char ':' <> ppLayoutBody sz abi pref
PointerSize n sz abi pref -> char 'p' <> int n <> char ':'
<> ppLayoutBody sz abi pref
IntegerSize sz abi pref -> char 'i' <> ppLayoutBody sz abi pref
VectorSize sz abi pref -> char 'v' <> ppLayoutBody sz abi pref
FloatSize sz abi pref -> char 'f' <> ppLayoutBody sz abi pref
StackObjSize sz abi pref -> char 's' <> ppLayoutBody sz abi pref
AggregateSize sz abi pref -> char 'a' <> ppLayoutBody sz abi pref
NativeIntSize szs ->
char 'n' <> hcat (punctuate (char ':') (map int szs))
StackAlign a -> char 'S' <> int a
Mangling m -> char 'm' <> char ':' <> ppMangling m
-- | Pretty-print the common case for data layout specifications.
ppLayoutBody :: Int -> Int -> Maybe Int -> Doc
ppLayoutBody size abi mb = int size <> char ':' <> int abi <> pref
where
pref = case mb of
Nothing -> empty
Just p -> char ':' <> int p
ppMangling :: Mangling -> Doc
ppMangling ElfMangling = char 'e'
ppMangling MipsMangling = char 'm'
ppMangling MachOMangling = char 'o'
ppMangling WindowsCoffMangling = char 'w'
-- Inline Assembly -------------------------------------------------------------
-- | Pretty-print the inline assembly block.
ppInlineAsm :: InlineAsm -> Doc
ppInlineAsm = foldr ($+$) empty . map ppLine
where
ppLine l = "module asm" <+> doubleQuotes (text l)
-- Identifiers -----------------------------------------------------------------
ppIdent :: Ident -> Doc
ppIdent (Ident n)
| validIdentifier n = char '%' <> text n
| otherwise = char '%' <> ppStringLiteral n
-- | According to the LLVM Language Reference Manual, the regular
-- expression for LLVM identifiers is "[-a-zA-Z$._][-a-zA-Z$._0-9]".
-- Identifiers may also be strings of one or more decimal digits.
validIdentifier :: String -> Bool
validIdentifier [] = False
validIdentifier s@(c0 : cs)
| isDigit c0 = all isDigit cs
| otherwise = all isIdentChar s
where
isIdentChar :: Char -> Bool
isIdentChar c = isAlphaNum c || c `elem` ("-$._" :: [Char])
-- Symbols ---------------------------------------------------------------------
ppSymbol :: Symbol -> Doc
ppSymbol (Symbol n)
| validIdentifier n = char '@' <> text n
| otherwise = char '@' <> ppStringLiteral n
-- Types -----------------------------------------------------------------------
ppPrimType :: PrimType -> Doc
ppPrimType Label = "label"
ppPrimType Void = "void"
ppPrimType (Integer i) = char 'i' <> integer (toInteger i)
ppPrimType (FloatType ft) = ppFloatType ft
ppPrimType X86mmx = "x86mmx"
ppPrimType Metadata = "metadata"
ppFloatType :: FloatType -> Doc
ppFloatType Half = "half"
ppFloatType Float = "float"
ppFloatType Double = "double"
ppFloatType Fp128 = "fp128"
ppFloatType X86_fp80 = "x86_fp80"
ppFloatType PPC_fp128 = "ppc_fp128"
ppType :: Type -> Doc
ppType (PrimType pt) = ppPrimType pt
ppType (Alias i) = ppIdent i
ppType (Array len ty) = brackets (integral len <+> char 'x' <+> ppType ty)
ppType (PtrTo ty) = ppType ty <> char '*'
ppType PtrOpaque = "ptr"
ppType (Struct ts) = structBraces (commas (map ppType ts))
ppType (PackedStruct ts) = angles (structBraces (commas (map ppType ts)))
ppType (FunTy r as va) = ppType r <> ppArgList va (map ppType as)
ppType (Vector len pt) = angles (integral len <+> char 'x' <+> ppType pt)
ppType Opaque = "opaque"
ppTypeDecl :: TypeDecl -> Doc
ppTypeDecl td = ppIdent (typeName td) <+> char '='
<+> "type" <+> ppType (typeValue td)
-- Declarations ----------------------------------------------------------------
ppGlobal :: LLVM => Global -> Doc
ppGlobal g = ppSymbol (globalSym g) <+> char '='
<+> ppTheGlobalAttrs (globalAttrs g)
<+> ppType (globalType g) <+> ppMaybe ppValue (globalValue g)
<> ppAlign (globalAlign g)
<> ppAttachedMetadata (Map.toList (globalMetadata g))
where
isStruct | Just (ValStruct {}) <- globalValue g = True
| otherwise = False
ppTheGlobalAttrs | isStruct = ppStructGlobalAttrs
| otherwise = ppGlobalAttrs
ppGlobalAttrs :: GlobalAttrs -> Doc
ppGlobalAttrs ga
-- LLVM 3.8 does not emit or parse linkage information w/ hidden visibility
| Just HiddenVisibility <- gaVisibility ga =
ppVisibility HiddenVisibility <+> constant
| otherwise = ppMaybe ppLinkage (gaLinkage ga) <+> ppMaybe ppVisibility (gaVisibility ga) <+> constant
where
constant | gaConstant ga = "constant"
| otherwise = "global"
ppStructGlobalAttrs :: GlobalAttrs -> Doc
ppStructGlobalAttrs ga
-- LLVM 3.8 does not emit or parse external linkage for
-- global structs
| Just External <- gaLinkage ga,
Just DefaultVisibility <- gaVisibility ga
= constant
| otherwise = ppGlobalAttrs ga
where
constant | gaConstant ga = "constant"
| otherwise = "global"
ppDeclare :: Declare -> Doc
ppDeclare d = "declare"
<+> ppMaybe ppLinkage (decLinkage d)
<+> ppMaybe ppVisibility (decVisibility d)
<+> ppType (decRetType d)
<+> ppSymbol (decName d)
<> ppArgList (decVarArgs d) (map ppType (decArgs d))
<+> hsep (ppFunAttr <$> decAttrs d)
<> maybe empty ((char ' ' <>) . ppComdatName) (decComdat d)
ppComdatName :: String -> Doc
ppComdatName s = "comdat" <> parens (char '$' <> text s)
ppComdat :: (String,SelectionKind) -> Doc
ppComdat (n,k) = ppComdatName n <+> char '=' <+> text "comdat" <+> ppSelectionKind k
ppSelectionKind :: SelectionKind -> Doc
ppSelectionKind k =
case k of
ComdatAny -> "any"
ComdatExactMatch -> "exactmatch"
ComdatLargest -> "largest"
ComdatNoDuplicates -> "noduplicates"
ComdatSameSize -> "samesize"
ppDefine :: LLVM => Define -> Doc
ppDefine d = "define"
<+> ppMaybe ppLinkage (defLinkage d)
<+> ppMaybe ppVisibility (defVisibility d)
<+> ppType (defRetType d)
<+> ppSymbol (defName d)
<> ppArgList (defVarArgs d) (map (ppTyped ppIdent) (defArgs d))
<+> hsep (ppFunAttr <$> defAttrs d)
<+> ppMaybe (\s -> "section" <+> doubleQuotes (text s)) (defSection d)
<+> ppMaybe (\gc -> "gc" <+> ppGC gc) (defGC d)
<+> ppMds (defMetadata d)
<+> char '{'
$+$ vcat (map ppBasicBlock (defBody d))
$+$ char '}'
where
ppMds mdm =
case Map.toList mdm of
[] -> empty
mds -> hsep [ "!" <> text k <+> ppValMd md | (k, md) <- mds ]
-- FunAttr ---------------------------------------------------------------------
ppFunAttr :: FunAttr -> Doc
ppFunAttr a =
case a of
AlignStack w -> text "alignstack" <> parens (int w)
Alwaysinline -> text "alwaysinline"
Builtin -> text "builtin"
Cold -> text "cold"
Inlinehint -> text "inlinehint"
Jumptable -> text "jumptable"
Minsize -> text "minsize"
Naked -> text "naked"
Nobuiltin -> text "nobuiltin"
Noduplicate -> text "noduplicate"
Noimplicitfloat -> text "noimplicitfloat"
Noinline -> text "noinline"
Nonlazybind -> text "nonlazybind"
Noredzone -> text "noredzone"
Noreturn -> text "noreturn"
Nounwind -> text "nounwind"
Optnone -> text "optnone"
Optsize -> text "optsize"
Readnone -> text "readnone"
Readonly -> text "readonly"
ReturnsTwice -> text "returns_twice"
SanitizeAddress -> text "sanitize_address"
SanitizeMemory -> text "sanitize_memory"
SanitizeThread -> text "sanitize_thread"
SSP -> text "ssp"
SSPreq -> text "sspreq"
SSPstrong -> text "sspstrong"
UWTable -> text "uwtable"
-- Basic Blocks ----------------------------------------------------------------
ppLabelDef :: BlockLabel -> Doc
ppLabelDef (Named (Ident l)) = text l <> char ':'
ppLabelDef (Anon i) = char ';' <+> "<label>:" <+> int i
ppLabel :: BlockLabel -> Doc
ppLabel (Named l) = ppIdent l
ppLabel (Anon i) = char '%' <> int i
ppBasicBlock :: LLVM => BasicBlock -> Doc
ppBasicBlock bb = ppMaybe ppLabelDef (bbLabel bb)
$+$ nest 2 (vcat (map ppStmt (bbStmts bb)))
-- Statements ------------------------------------------------------------------
ppStmt :: LLVM => Stmt -> Doc
ppStmt stmt = case stmt of
Result var i mds -> ppIdent var <+> char '=' <+> ppInstr i
<> ppAttachedMetadata mds
Effect i mds -> ppInstr i <> ppAttachedMetadata mds
ppAttachedMetadata :: LLVM => [(String,ValMd)] -> Doc
ppAttachedMetadata mds
| null mds = empty
| otherwise = comma <+> commas (map step mds)
where
step (l,md) = ppMetadata (text l) <+> ppValMd md
-- Linkage ---------------------------------------------------------------------
ppLinkage :: Linkage -> Doc
ppLinkage linkage = case linkage of
Private -> "private"
LinkerPrivate -> "linker_private"
LinkerPrivateWeak -> "linker_private_weak"
LinkerPrivateWeakDefAuto -> "linker_private_weak_def_auto"
Internal -> "internal"
AvailableExternally -> "available_externally"
Linkonce -> "linkonce"
Weak -> "weak"
Common -> "common"
Appending -> "appending"
ExternWeak -> "extern_weak"
LinkonceODR -> "linkonce_ddr"
WeakODR -> "weak_odr"
External -> "external"
DLLImport -> "dllimport"
DLLExport -> "dllexport"
ppVisibility :: Visibility -> Doc
ppVisibility v = case v of
DefaultVisibility -> "default"
HiddenVisibility -> "hidden"
ProtectedVisibility -> "protected"
ppGC :: GC -> Doc
ppGC = doubleQuotes . text . getGC
-- Expressions -----------------------------------------------------------------
ppTyped :: (a -> Doc) -> Typed a -> Doc
ppTyped fmt ty = ppType (typedType ty) <+> fmt (typedValue ty)
ppSignBits :: Bool -> Bool -> Doc
ppSignBits nuw nsw = opt nuw "nuw" <+> opt nsw "nsw"
ppExact :: Bool -> Doc
ppExact e = opt e "exact"
ppArithOp :: ArithOp -> Doc
ppArithOp (Add nuw nsw) = "add" <+> ppSignBits nuw nsw
ppArithOp FAdd = "fadd"
ppArithOp (Sub nuw nsw) = "sub" <+> ppSignBits nuw nsw
ppArithOp FSub = "fsub"
ppArithOp (Mul nuw nsw) = "mul" <+> ppSignBits nuw nsw
ppArithOp FMul = "fmul"
ppArithOp (UDiv e) = "udiv" <+> ppExact e
ppArithOp (SDiv e) = "sdiv" <+> ppExact e
ppArithOp FDiv = "fdiv"
ppArithOp URem = "urem"
ppArithOp SRem = "srem"
ppArithOp FRem = "frem"
ppUnaryArithOp :: UnaryArithOp -> Doc
ppUnaryArithOp FNeg = "fneg"
ppBitOp :: BitOp -> Doc
ppBitOp (Shl nuw nsw) = "shl" <+> ppSignBits nuw nsw
ppBitOp (Lshr e) = "lshr" <+> ppExact e
ppBitOp (Ashr e) = "ashr" <+> ppExact e
ppBitOp And = "and"
ppBitOp Or = "or"
ppBitOp Xor = "xor"
ppConvOp :: ConvOp -> Doc
ppConvOp Trunc = "trunc"
ppConvOp ZExt = "zext"
ppConvOp SExt = "sext"
ppConvOp FpTrunc = "fptrunc"
ppConvOp FpExt = "fpext"
ppConvOp FpToUi = "fptoui"
ppConvOp FpToSi = "fptosi"
ppConvOp UiToFp = "uitofp"
ppConvOp SiToFp = "sitofp"
ppConvOp PtrToInt = "ptrtoint"
ppConvOp IntToPtr = "inttoptr"
ppConvOp BitCast = "bitcast"
ppAtomicOrdering :: AtomicOrdering -> Doc
ppAtomicOrdering Unordered = text "unordered"
ppAtomicOrdering Monotonic = text "monotonic"
ppAtomicOrdering Acquire = text "acquire"
ppAtomicOrdering Release = text "release"
ppAtomicOrdering AcqRel = text "acq_rel"
ppAtomicOrdering SeqCst = text "seq_cst"
ppAtomicOp :: AtomicRWOp -> Doc
ppAtomicOp AtomicXchg = "xchg"
ppAtomicOp AtomicAdd = "add"
ppAtomicOp AtomicSub = "sub"
ppAtomicOp AtomicAnd = "and"
ppAtomicOp AtomicNand = "nand"
ppAtomicOp AtomicOr = "or"
ppAtomicOp AtomicXor = "xor"
ppAtomicOp AtomicMax = "max"
ppAtomicOp AtomicMin = "min"
ppAtomicOp AtomicUMax = "umax"
ppAtomicOp AtomicUMin = "umin"
ppScope :: Maybe String -> Doc
ppScope Nothing = empty
ppScope (Just s) = "syncscope" <> parens (doubleQuotes (text s))
ppInstr :: LLVM => Instr -> Doc
ppInstr instr = case instr of
Ret tv -> "ret" <+> ppTyped ppValue tv
RetVoid -> "ret void"
Arith op l r -> ppArithOp op <+> ppTyped ppValue l
<> comma <+> ppValue r
UnaryArith op a -> ppUnaryArithOp op <+> ppTyped ppValue a
Bit op l r -> ppBitOp op <+> ppTyped ppValue l
<> comma <+> ppValue r
Conv op a ty -> ppConvOp op <+> ppTyped ppValue a
<+> "to" <+> ppType ty
Call tc ty f args -> ppCall tc ty f args
CallBr ty f args u es -> ppCallBr ty f args u es
Alloca ty len align -> ppAlloca ty len align
Load ptr mo ma -> ppLoad ptr mo ma
Store a ptr mo ma -> ppStore a ptr mo ma
Fence scope order -> "fence" <+> ppScope scope <+> ppAtomicOrdering order
CmpXchg w v p a n s o o' -> "cmpxchg" <+> opt w "weak"
<+> opt v "volatile"
<+> ppTyped ppValue p
<> comma <+> ppTyped ppValue a
<> comma <+> ppTyped ppValue n
<+> ppScope s
<+> ppAtomicOrdering o
<+> ppAtomicOrdering o'
AtomicRW v op p a s o -> "atomicrmw"
<+> opt v "volatile"
<+> ppAtomicOp op
<+> ppTyped ppValue p
<> comma <+> ppTyped ppValue a
<+> ppScope s
<+> ppAtomicOrdering o
ICmp op l r -> "icmp" <+> ppICmpOp op
<+> ppTyped ppValue l <> comma <+> ppValue r
FCmp op l r -> "fcmp" <+> ppFCmpOp op
<+> ppTyped ppValue l <> comma <+> ppValue r
Phi ty vls -> "phi" <+> ppType ty
<+> commas (map ppPhiArg vls)
Select c t f -> "select" <+> ppTyped ppValue c
<> comma <+> ppTyped ppValue t
<> comma <+> ppTyped ppValue (f <$ t)
ExtractValue v is -> "extractvalue" <+> ppTyped ppValue v
<> comma <+> (commas (map integral is))
InsertValue a v is -> "insertvalue" <+> ppTyped ppValue a
<> comma <+> ppTyped ppValue v
<> comma <+> commas (map integral is)
ShuffleVector a b m -> "shufflevector" <+> ppTyped ppValue a
<> comma <+> ppTyped ppValue (b <$ a)
<> comma <+> ppTyped ppValue m
GEP ib ptr ixs -> ppGEP ib ptr ixs
Comment str -> char ';' <+> text str
Jump i -> "br"
<+> ppTypedLabel i
Br c t f -> "br" <+> ppTyped ppValue c
<> comma <+> ppType (PrimType Label)
<+> ppLabel t
<> comma <+> ppType (PrimType Label)
<+> ppLabel f
Invoke ty f args to uw -> ppInvoke ty f args to uw
Unreachable -> "unreachable"
Unwind -> "unwind"
VaArg al t -> "va_arg" <+> ppTyped ppValue al
<> comma <+> ppType t
ExtractElt v i -> "extractelement"
<+> ppTyped ppValue v
<> comma <+> ppVectorIndex i
InsertElt v e i -> "insertelement"
<+> ppTyped ppValue v
<> comma <+> ppTyped ppValue e
<> comma <+> ppVectorIndex i
IndirectBr d ls -> "indirectbr"
<+> ppTyped ppValue d
<> comma <+> commas (map ppTypedLabel ls)
Switch c d ls -> "switch"
<+> ppTyped ppValue c
<> comma <+> ppTypedLabel d
<+> char '['
$$ nest 2 (vcat (map (ppSwitchEntry (typedType c)) ls))
$$ char ']'
LandingPad ty mfn c cs ->
case mfn of
Just fn -> "landingpad"
<+> ppType ty
<+> "personality"
<+> ppTyped ppValue fn
$$ nest 2 (ppClauses c cs)
Nothing -> "landingpad"
<+> ppType ty
$$ nest 2 (ppClauses c cs)
Resume tv -> "resume" <+> ppTyped ppValue tv
Freeze tv -> "freeze" <+> ppTyped ppValue tv
ppLoad :: LLVM => Typed (Value' BlockLabel) -> Maybe AtomicOrdering -> Maybe Align -> Doc
ppLoad ptr mo ma =
"load" <+> (if isAtomic then "atomic" else empty)
<+> (if isImplicit then empty else explicit)
<+> ppTyped ppValue ptr
<+> ordering
<> ppAlign ma
where
isAtomic = isJust mo
isImplicit = checkConfig cfgLoadImplicitType
ordering =
case mo of
Just ao -> ppAtomicOrdering ao
_ -> empty
explicit =
case typedType ptr of
PtrTo ty -> ppType ty <> comma
ty -> ppType ty <> comma
ppStore :: LLVM
=> Typed (Value' BlockLabel)
-> Typed (Value' BlockLabel)
-> Maybe AtomicOrdering
-> Maybe Align
-> Doc
ppStore ptr val mo ma =
"store" <+> (if isJust mo then "atomic" else empty)
<+> ppTyped ppValue ptr <> comma
<+> ppTyped ppValue val
<+> case mo of
Just ao -> ppAtomicOrdering ao
_ -> empty
<> ppAlign ma
ppClauses :: LLVM => Bool -> [Clause] -> Doc
ppClauses isCleanup cs = vcat (cleanup : map ppClause cs)
where
cleanup | isCleanup = "cleanup"
| otherwise = empty
ppClause :: LLVM => Clause -> Doc
ppClause c = case c of
Catch tv -> "catch" <+> ppTyped ppValue tv
Filter tv -> "filter" <+> ppTyped ppValue tv
ppTypedLabel :: BlockLabel -> Doc
ppTypedLabel i = ppType (PrimType Label) <+> ppLabel i
ppSwitchEntry :: Type -> (Integer,BlockLabel) -> Doc
ppSwitchEntry ty (i,l) = ppType ty <+> integer i <> comma <+> ppTypedLabel l
ppVectorIndex :: LLVM => Value -> Doc
ppVectorIndex i = ppType (PrimType (Integer 32)) <+> ppValue i
ppAlign :: Maybe Align -> Doc
ppAlign Nothing = empty
ppAlign (Just align) = comma <+> "align" <+> int align
ppAlloca :: LLVM => Type -> Maybe (Typed Value) -> Maybe Int -> Doc
ppAlloca ty mbLen mbAlign = "alloca" <+> ppType ty <> len <> align
where
len = fromMaybe empty $ do
l <- mbLen
return (comma <+> ppTyped ppValue l)
align = fromMaybe empty $ do
a <- mbAlign
return (comma <+> "align" <+> int a)
ppCall :: LLVM => Bool -> Type -> Value -> [Typed Value] -> Doc
ppCall tc ty f args
| tc = "tail" <+> body
| otherwise = body
where
body = "call" <+> ppCallSym ty f
<> parens (commas (map (ppTyped ppValue) args))
-- | Note that the textual syntax changed in LLVM 10 (@callbr@ was introduced in
-- LLVM 9).
ppCallBr :: LLVM => Type -> Value -> [Typed Value] -> BlockLabel -> [BlockLabel] -> Doc
ppCallBr ty f args to indirectDests =
"callbr"
<+> ppCallSym res f <> parens (commas (map (ppTyped ppValue) args))
<+> "to" <+> ppLab to <+> brackets (commas (map ppLab indirectDests))
where
ppLab l = ppType (PrimType Label) <+> ppLabel l
res =
case ty of
PtrTo (FunTy r _ _) -> r
_ -> PrimType Void
-- | Print out the @<ty>|<fnty> <fnptrval>@ portion of a @call@, @callbr@, or
-- @invoke@ instruction, where:
--
-- * @<ty>@ is the return type.
--
-- * @<fnty>@ is the overall function type.
--
-- * @<fnptrval>@ is a pointer value, where the memory it points to is treated
-- as a value of type @<fnty>@.
--
-- The LLVM Language Reference Manual indicates that either @<ty>@ or @<fnty>@
-- can be used, but in practice, @<ty>@ is typically preferred unless the
-- function type involves varargs. We adopt the same convention here.
ppCallSym :: LLVM => Type -> Value -> Doc
ppCallSym ty val = pp_ty <+> ppValue val
where
pp_ty =
case ty of
FunTy res args va
| va
-> ppType res <+> ppArgList va (map ppType args)
| otherwise
-> ppType res
_ -> ppType ty
ppGEP :: LLVM => Bool -> Typed Value -> [Typed Value] -> Doc
ppGEP ib ptr ixs = "getelementptr" <+> inbounds
<+> (if isImplicit then empty else explicit)
<+> commas (map (ppTyped ppValue) (ptr:ixs))
where
isImplicit = checkConfig cfgGEPImplicitType
explicit =
case typedType ptr of
PtrTo ty -> ppType ty <> comma
ty -> ppType ty <> comma
inbounds | ib = "inbounds"
| otherwise = empty
ppInvoke :: LLVM => Type -> Value -> [Typed Value] -> BlockLabel -> BlockLabel -> Doc
ppInvoke ty f args to uw = body
where
body = "invoke" <+> ppCallSym ty f
<> parens (commas (map (ppTyped ppValue) args))
<+> "to" <+> ppType (PrimType Label) <+> ppLabel to
<+> "unwind" <+> ppType (PrimType Label) <+> ppLabel uw
ppPhiArg :: LLVM => (Value,BlockLabel) -> Doc
ppPhiArg (v,l) = char '[' <+> ppValue v <> comma <+> ppLabel l <+> char ']'
ppICmpOp :: ICmpOp -> Doc
ppICmpOp Ieq = "eq"
ppICmpOp Ine = "ne"
ppICmpOp Iugt = "ugt"
ppICmpOp Iuge = "uge"
ppICmpOp Iult = "ult"
ppICmpOp Iule = "ule"
ppICmpOp Isgt = "sgt"
ppICmpOp Isge = "sge"
ppICmpOp Islt = "slt"
ppICmpOp Isle = "sle"
ppFCmpOp :: FCmpOp -> Doc
ppFCmpOp Ffalse = "false"
ppFCmpOp Foeq = "oeq"
ppFCmpOp Fogt = "ogt"
ppFCmpOp Foge = "oge"
ppFCmpOp Folt = "olt"
ppFCmpOp Fole = "ole"
ppFCmpOp Fone = "one"
ppFCmpOp Ford = "ord"
ppFCmpOp Fueq = "ueq"
ppFCmpOp Fugt = "ugt"
ppFCmpOp Fuge = "uge"
ppFCmpOp Fult = "ult"
ppFCmpOp Fule = "ule"
ppFCmpOp Fune = "une"
ppFCmpOp Funo = "uno"
ppFCmpOp Ftrue = "true"
ppValue' :: LLVM => (i -> Doc) -> Value' i -> Doc
ppValue' pp val = case val of
ValInteger i -> integer i
ValBool b -> ppBool b
ValFloat i -> float i
ValDouble i -> double i
ValFP80 (FP80_LongDouble e s) ->
-- shown as 0xK<<20-hex-digits>>, per
-- https://llvm.org/docs/LangRef.html#simple-constants
let pad n | n < 0x10 = shows (0::Int) . showHex n
| otherwise = showHex n
fld v i = pad ((v `shiftR` (i * 8)) .&. 0xff)
in "0xK" <> text (foldr (fld e) (foldr (fld s) "" $ reverse [0..7::Int]) [1, 0])
ValIdent i -> ppIdent i
ValSymbol s -> ppSymbol s
ValNull -> "null"
ValArray ty es -> brackets
$ commas (map (ppTyped (ppValue' pp) . Typed ty) es)
ValVector ty es -> angles $ commas
$ map (ppTyped (ppValue' pp) . Typed ty) es
ValStruct fs -> structBraces (commas (map (ppTyped (ppValue' pp)) fs))
ValPackedStruct fs -> angles
$ structBraces (commas (map (ppTyped (ppValue' pp)) fs))
ValString s -> char 'c' <> ppStringLiteral (map (toEnum . fromIntegral) s)
ValConstExpr ce -> ppConstExpr' pp ce
ValUndef -> "undef"
ValLabel l -> pp l
ValZeroInit -> "zeroinitializer"
ValAsm s a i c -> ppAsm s a i c
ValMd m -> ppValMd' pp m
ValPoison -> "poison"
ppValue :: LLVM => Value -> Doc
ppValue = ppValue' ppLabel
ppValMd' :: LLVM => (i -> Doc) -> ValMd' i -> Doc
ppValMd' pp m = case m of
ValMdString str -> ppMetadata (ppStringLiteral str)
ValMdValue tv -> ppTyped (ppValue' pp) tv
ValMdRef i -> ppMetadata (int i)
ValMdNode vs -> ppMetadataNode' pp vs
ValMdLoc l -> ppDebugLoc' pp l
ValMdDebugInfo di -> ppDebugInfo' pp di
ppValMd :: LLVM => ValMd -> Doc
ppValMd = ppValMd' ppLabel
ppDebugLoc' :: LLVM => (i -> Doc) -> DebugLoc' i -> Doc
ppDebugLoc' pp dl = (if cfgUseDILocation ?config then "!DILocation"
else "!MDLocation")
<> parens (commas [ "line:" <+> integral (dlLine dl)
, "column:" <+> integral (dlCol dl)
, "scope:" <+> ppValMd' pp (dlScope dl)
] <> mbIA <> mbImplicit)
where
mbIA = case dlIA dl of
Just md -> comma <+> "inlinedAt:" <+> ppValMd' pp md
Nothing -> empty
mbImplicit = if dlImplicit dl then comma <+> "implicit" else empty
ppDebugLoc :: LLVM => DebugLoc -> Doc
ppDebugLoc = ppDebugLoc' ppLabel
ppTypedValMd :: LLVM => ValMd -> Doc
ppTypedValMd = ppTyped ppValMd . Typed (PrimType Metadata)
ppMetadata :: Doc -> Doc
ppMetadata body = char '!' <> body
ppMetadataNode' :: LLVM => (i -> Doc) -> [Maybe (ValMd' i)] -> Doc
ppMetadataNode' pp vs = ppMetadata (braces (commas (map arg vs)))
where arg = maybe ("null") (ppValMd' pp)
ppMetadataNode :: LLVM => [Maybe ValMd] -> Doc
ppMetadataNode = ppMetadataNode' ppLabel
ppStringLiteral :: String -> Doc
ppStringLiteral = doubleQuotes . text . concatMap escape
where
escape c | c == '"' || c == '\\' = '\\' : showHex (fromEnum c) ""
| isAscii c && isPrint c = [c]
| otherwise = '\\' : pad (ord c)
pad n | n < 0x10 = '0' : map toUpper (showHex n "")
| otherwise = map toUpper (showHex n "")
ppAsm :: Bool -> Bool -> String -> String -> Doc
ppAsm s a i c =
"asm" <+> sideeffect <+> alignstack
<+> ppStringLiteral i <> comma <+> ppStringLiteral c
where
sideeffect | s = "sideeffect"
| otherwise = empty
alignstack | a = "alignstack"
| otherwise = empty
ppConstExpr' :: LLVM => (i -> Doc) -> ConstExpr' i -> Doc
ppConstExpr' pp expr =
case expr of
ConstGEP inb _mix mp ixs ->
"getelementptr"
<+> opt inb "inbounds"
<+> parens (mcommas ((ppType <$> mp) : (map (pure . ppTyp') ixs)))
ConstConv op tv t -> ppConvOp op <+> parens (ppTyp' tv <+> "to" <+> ppType t)
ConstSelect c l r ->
"select" <+> parens (commas [ ppTyp' c, ppTyp' l , ppTyp' r])
ConstBlockAddr t l -> "blockaddress" <+> parens (ppVal' (typedValue t) <> comma <+> pp l)
ConstFCmp op a b -> "fcmp" <+> ppFCmpOp op <+> ppTupleT a b
ConstICmp op a b -> "icmp" <+> ppICmpOp op <+> ppTupleT a b
ConstArith op a b -> ppArithOp op <+> ppTuple a b
ConstUnaryArith op a -> ppUnaryArithOp op <+> ppTyp' a
ConstBit op a b -> ppBitOp op <+> ppTuple a b
where ppTuple a b = parens $ ppTyped ppVal' a <> comma <+> ppVal' b
ppTupleT a b = parens $ ppTyped ppVal' a <> comma <+> ppTyp' b
ppVal' = ppValue' pp
ppTyp' = ppTyped ppVal'
ppConstExpr :: LLVM => ConstExpr -> Doc
ppConstExpr = ppConstExpr' ppLabel
-- DWARF Debug Info ------------------------------------------------------------
ppDebugInfo' :: LLVM => (i -> Doc) -> DebugInfo' i -> Doc
ppDebugInfo' pp di = case di of
DebugInfoBasicType bt -> ppDIBasicType bt
DebugInfoCompileUnit cu -> ppDICompileUnit' pp cu
DebugInfoCompositeType ct -> ppDICompositeType' pp ct
DebugInfoDerivedType dt -> ppDIDerivedType' pp dt
DebugInfoEnumerator nm v u -> ppDIEnumerator nm v u
DebugInfoExpression e -> ppDIExpression e
DebugInfoFile f -> ppDIFile f
DebugInfoGlobalVariable gv -> ppDIGlobalVariable' pp gv
DebugInfoGlobalVariableExpression gv -> ppDIGlobalVariableExpression' pp gv
DebugInfoLexicalBlock lb -> ppDILexicalBlock' pp lb
DebugInfoLexicalBlockFile lbf -> ppDILexicalBlockFile' pp lbf
DebugInfoLocalVariable lv -> ppDILocalVariable' pp lv
DebugInfoSubprogram sp -> ppDISubprogram' pp sp
DebugInfoSubrange sr -> ppDISubrange sr
DebugInfoSubroutineType st -> ppDISubroutineType' pp st
DebugInfoNameSpace ns -> ppDINameSpace' pp ns
DebugInfoTemplateTypeParameter dttp -> ppDITemplateTypeParameter' pp dttp
DebugInfoTemplateValueParameter dtvp -> ppDITemplateValueParameter' pp dtvp
DebugInfoImportedEntity diip -> ppDIImportedEntity' pp diip
DebugInfoLabel dil -> ppDILabel' pp dil
DebugInfoArgList args -> ppDIArgList' pp args
ppDebugInfo :: LLVM => DebugInfo -> Doc
ppDebugInfo = ppDebugInfo' ppLabel
ppDIImportedEntity' :: LLVM => (i -> Doc) -> DIImportedEntity' i -> Doc
ppDIImportedEntity' pp ie = "!DIImportedEntity"
<> parens (mcommas [ pure ("tag:" <+> integral (diieTag ie))
, (("scope:" <+>) . ppValMd' pp) <$> diieScope ie
, (("entity:" <+>) . ppValMd' pp) <$> diieEntity ie
, (("file:" <+>) . ppValMd' pp) <$> diieFile ie
, pure ("line:" <+> integral (diieLine ie))
, (("name:" <+>) . text) <$> diieName ie
])
ppDIImportedEntity :: LLVM => DIImportedEntity -> Doc
ppDIImportedEntity = ppDIImportedEntity' ppLabel
ppDILabel' :: LLVM => (i -> Doc) -> DILabel' i -> Doc
ppDILabel' pp ie = "!DILabel"
<> parens (mcommas [ (("scope:" <+>) . ppValMd' pp) <$> dilScope ie
, pure ("name:" <+> text (dilName ie))
, (("file:" <+>) . ppValMd' pp) <$> dilFile ie
, pure ("line:" <+> integral (dilLine ie))
])
ppDILabel :: LLVM => DILabel -> Doc
ppDILabel = ppDILabel' ppLabel
ppDINameSpace' :: LLVM => (i -> Doc) -> DINameSpace' i -> Doc
ppDINameSpace' pp ns = "!DINameSpace"
<> parens (mcommas [ ("name:" <+>) . text <$> (dinsName ns)
, pure ("scope:" <+> ppValMd' pp (dinsScope ns))
, pure ("file:" <+> ppValMd' pp (dinsFile ns))
, pure ("line:" <+> integral (dinsLine ns))
])
ppDINameSpace :: LLVM => DINameSpace -> Doc
ppDINameSpace = ppDINameSpace' ppLabel
ppDITemplateTypeParameter' :: LLVM => (i -> Doc) -> DITemplateTypeParameter' i -> Doc
ppDITemplateTypeParameter' pp tp = "!DITemplateTypeParameter"
<> parens (mcommas [ ("name:" <+>) . text <$> dittpName tp
, ("type:" <+>) . ppValMd' pp <$> dittpType tp
])
ppDITemplateTypeParameter :: LLVM => DITemplateTypeParameter -> Doc
ppDITemplateTypeParameter = ppDITemplateTypeParameter' ppLabel
ppDITemplateValueParameter' :: LLVM => (i -> Doc) -> DITemplateValueParameter' i -> Doc
ppDITemplateValueParameter' pp vp = "!DITemplateValueParameter"
<> parens (mcommas [ pure ("tag:" <+> integral (ditvpTag vp))
, ("name:" <+>) . text <$> ditvpName vp
, ("type:" <+>) . ppValMd' pp <$> ditvpType vp
, pure ("value:" <+> ppValMd' pp (ditvpValue vp))
])
ppDITemplateValueParameter :: LLVM => DITemplateValueParameter -> Doc
ppDITemplateValueParameter = ppDITemplateValueParameter' ppLabel
ppDIBasicType :: DIBasicType -> Doc
ppDIBasicType bt = "!DIBasicType"
<> parens (commas [ "tag:" <+> integral (dibtTag bt)
, "name:" <+> doubleQuotes (text (dibtName bt))
, "size:" <+> integral (dibtSize bt)
, "align:" <+> integral (dibtAlign bt)
, "encoding:" <+> integral (dibtEncoding bt)
] <> mbFlags)
where
mbFlags = case dibtFlags bt of
Just flags -> comma <+> "flags:" <+> integral flags
Nothing -> empty
ppDICompileUnit' :: LLVM => (i -> Doc) -> DICompileUnit' i -> Doc
ppDICompileUnit' pp cu = "!DICompileUnit"
<> parens (mcommas
[ pure ("language:" <+> integral (dicuLanguage cu))