-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathASyntax.hs
More file actions
2038 lines (1791 loc) · 79.5 KB
/
Copy pathASyntax.hs
File metadata and controls
2038 lines (1791 loc) · 79.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
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 CPP #-}
{-# LANGUAGE PatternGuards #-}
module ASyntax(
APackage(..),
getAPackageFieldInfos,
getAPackageClocks,
getAPackageInputs,
getAPackageParamsPortsAndInouts,
apkgIsMCD,
apkgExposesClkOrRst,
AId,
AMethodId,
AType(..),
ASize,
ARule(..),
AAssumption(..),
ARuleId,
APred,
AIFace(..),
AInput,
AMethodInput,
AAbstractInput(..),
AOutput,
AClock(..),
AReset(..),
AInout(..),
AClockDomain,
mkOutputWire,
mkIfcInoutN,
AExpr(..),
ADef(..),
ASPackage(..),
ASPSignalInfo(..),
ASPMethodInfo(..),
ASPCommentInfo(..),
AStateOut,
AVInst(..),
getSpecialOutputs,
getOutputClockWires,
getOutputClockPorts,
getOutputResetPorts,
getIfcInoutPorts,
ASchedule(..),
AScheduler(..),
AAction(..),
ANoInlineFun(..),
ARuleDescr,
aTZero,
aTBool,
aSBool,
aXSBool,
aRuleName,
aRulePred,
aTNat,
aTAction,
aTClock,
aTReset,
aTInout,
aTInout_,
isStringType,
isSizedString,
isUnsizedString,
dropSize,
unifyStringTypes,
isTupleType,
getArrayElemType,
getArraySize,
aIfaceProps,
aIfaceResType,
aIfaceResIds,
aIfaceArgs,
aIfaceArgSize,
aIfaceRules,
aIfaceRulesImpl,
aIfaceSchedNames,
aIfacePred,
aiface_vname,
aiface_argnames_width,
aIfaceMethods,
aIfaceHasAction,
aTrue,
aFalse,
isTrue,
isFalse,
aNoReset,
cmpASInt,
getSchedulerIds,
dropScheduleIds,
dropSchedulerIds,
aNat,
AForeignCall(..),
AForeignBlock,
PPrintExpand(..),
pPrintExpandFlags,
ppeString,
ppeAPackage,
mkMethId,
mkMethStr,
mkMethArgStr,
mkMethResStr,
splitPortNums,
isMethId,
MethodPart(..),
getParams,
getPorts,
getClocks,
getResets,
getInouts,
getInstArgs,
defaultAId,
binOp,
mkCFCondWireInstId,
PExpandContext, defContext, bContext, pContext
) where
#if defined(__GLASGOW_HASKELL__) && (__GLASGOW_HASKELL__ >= 804)
import Prelude hiding ((<>))
#endif
import Data.List(nub, intersect, (\\), partition)
-- import IntegerUtil(integerFormatPref)
import Eval
import PPrint
import IntLit
import Id
import IdPrint
import PreIds(idPrimAction, idClock, idReset, idInout, idInout_, idPreludeRead, idNoReset)
import Prim
import ErrorUtil(internalError)
import Backend
import Pragma
import PreStrings(fsDollar, fsUnderscore, fsEnable, fs_port)
import FStringCompat
-- import Position(noPosition)
import Position
import Data.Maybe
import Util(itos)
import VModInfo
import Wires
import ProofObligation(ProofObligation, MsgFn)
import Flags
import qualified Data.Map as M
import ISyntax(IType)
import InstNodes(InstTree)
-- packages converted from ISyntax
data APackage = APackage {
-- package name
apkg_name :: AId,
-- module wrapped around a non-inlined function
apkg_is_wrapped :: Bool,
-- whether module compilation was specific to the chosen backend
apkg_backend :: Maybe Backend,
-- size parameters (names in verilog)
apkg_size_params :: [AId],
-- package inputs (ports and parameters)
-- in the order described by wireinfo
-- (i.e. clock, reset, inouts, provided method arguments,
-- module arg ports, module arg parameters)
apkg_inputs :: [AAbstractInput],
-- table of different clock domains
apkg_clock_domains :: [AClockDomain],
-- description of external wires (e.g. clock and reset)
apkg_external_wires :: VWireInfo,
-- table of port names to source types
apkg_external_wire_types :: M.Map VName IType,
-- table of resets
apkg_reset_list :: [(ResetId, AReset)],
-- state elements (Verilog instances)
apkg_state_instances :: [AVInst],
-- local defs, in dependency-sorted order?
apkg_local_defs :: [ADef],
-- rules, in unspecified order
apkg_rules :: [ARule],
-- relationships among rule names
apkg_schedule_pragmas :: [ASchedulePragma],
-- interface methods
apkg_interface :: [AIFace],
-- comments on submodule instantiations
apkg_inst_comments :: [(Id, [String])],
-- instantiation tree of rules and methods
apkg_inst_tree :: InstTree,
-- proof obligations which have not yet been discharged
apkg_proof_obligations :: [(ProofObligation AExpr, MsgFn)]
} deriving (Eq, Show)
instance NFData APackage where
rnf (APackage n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13 n14 n15 n16 n17) =
rnf17 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13 n14 n15 n16 n17
getAPackageFieldInfos :: APackage -> [VFieldInfo]
getAPackageFieldInfos = map aif_fieldinfo . apkg_interface
getAPackageClocks :: APackage -> [AClock]
getAPackageClocks APackage { apkg_clock_domains = acds } = concatMap snd acds
getAPackageInputs :: APackage -> [(AAbstractInput, VArgInfo)]
getAPackageInputs apkg =
let
-- get the two fields
inputs = apkg_inputs apkg
arginfos = wArgs (apkg_external_wires apkg)
-- check that they are the same length
inputs_length = length (apkg_inputs apkg)
arginfos_length = length arginfos
args_with_info = zip inputs arginfos
in
if (inputs_length /= arginfos_length)
then internalError ("getAPackageInputs: " ++
"length inputs != length arginfos: " ++
ppReadable (inputs, arginfos))
else args_with_info
-- returns the input ports, separated into params, ports and inouts
-- (note that this converts abstract inputs to port inputs)
getAPackageParamsPortsAndInouts :: APackage -> ([AInput], [AInput], [AInput])
getAPackageParamsPortsAndInouts apkg =
let args_with_info = getAPackageInputs apkg
drop_info (xs, ys, zs) = (map fst xs, map fst ys, map fst zs)
cvtToPorts (xs, ys, zs) =
(concatMap absInputToPorts xs,
concatMap absInputToPorts ys,
concatMap absInputToPorts zs)
(params, rest) = partition (isParam . snd) args_with_info
(inouts,ports) = partition (isInout . snd) rest
in cvtToPorts $ drop_info $ (params, ports, inouts)
apkgIsMCD :: APackage -> Bool
apkgIsMCD apkg =
let domains = apkg_clock_domains apkg
clocks = concatMap snd domains
resets = apkg_reset_list apkg
in (length domains /= 1) ||
(length clocks /= 1) ||
(length resets > 1)
apkgExposesClkOrRst :: APackage -> Bool
apkgExposesClkOrRst apkg =
let isClkOrRst (AIClock {}) = True
isClkOrRst (AIReset {}) = True
isClkOrRst _ = False
in any isClkOrRst (apkg_interface apkg)
-- a scheduled package
-- rules and interface methods have turned into logic connected to state instances
data ASPackage = ASPackage {
-- package name
aspkg_name :: AId,
-- module wrapped around a pure function with pragma no-inline
aspkg_is_wrapped :: Bool,
-- parameters (names in Verilog)
-- (i.e. module args generated as params, size parameters)
-- XXX there are no size parameters because we don't support
-- XXX synthesis of size-polymorphic modules
aspkg_parameters :: [AInput],
-- package outputs (output clocks/resets, method results/RDY)
aspkg_outputs :: [AOutput],
-- package inputs (input clocks/resets, method args/EN, module args)
-- (i.e. clock, reset, method arguments, module args as ports)
aspkg_inputs :: [AInput],
-- package inouts (module args and provided interface)
aspkg_inouts :: [AInput],
-- state elements (Verilog instances)
aspkg_state_instances :: [AVInst],
-- state element outputs (wires coming out of state elements)
aspkg_state_outputs :: [AStateOut],
-- defs (all sorts)
aspkg_values :: [ADef],
-- inout defs
aspkg_inout_values :: [ADef],
-- foreign function calls (grouped by clock domain)
aspkg_foreign_calls :: [AForeignBlock],
-- wire ports from inlined submodules (RWire and CReg)
-- which shouldn't be unecessarily inlined away
aspkg_inlined_ports :: [AId],
-- info about which Ids are for what purpose
aspkg_signal_info :: ASPSignalInfo,
-- user comments to be included in the output RTL
aspkg_comment_info :: ASPCommentInfo
}
deriving (Eq, Show)
instance NFData ASPackage where
rnf (ASPackage n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13 n14) =
rnf14 n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 n11 n12 n13 n14
data ASPSignalInfo = ASPSignalInfo {
-- input params, ports, clocks, and resets are all in one list
-- (can use isParam etc to filter it out)
aspsi_inputs :: [AId],
-- the interface output clocks (and possibly empty list of gates)
aspsi_output_clks :: [(AId,[AId])],
aspsi_output_rsts :: [AId],
aspsi_ifc_iots :: [AId],
-- the ifc methods
aspsi_methods :: [ASPMethodInfo],
-- inline submodule info (RWire and CReg)
-- XXX this somewhat duplicates the aspkg_inlined_ports field
-- * instance name
-- * module name as String
-- * list of ports that became signals
aspsi_inlined_ports :: [(AId, String, [AId])],
-- rule scheduling signals
-- relation from rule name to its CAN_FIRE and WILL_FIRE signals
aspsi_rule_sched :: [(AId, [AId])],
-- mux selectors
-- ids of defs created in AState for the selectors to submod muxes
aspsi_mux_selectors :: [AId],
-- mux values
-- ids of defs created in AState for the values to submod muxes
aspsi_mux_values :: [AId],
-- submodule enables
-- ids of defs created in AState for the enables to submod methods
aspsi_submod_enables :: [AId]
}
deriving (Eq, Show)
instance PPrint ASPSignalInfo where
pPrint d p aspsi = text "ASPSignalInfo = " <> braces
(text "Inputs" <+> pPrint d p (aspsi_inputs aspsi)
$+$ text "Output Clocks" <+> pPrint d p (aspsi_output_clks aspsi)
$+$ text "Output Resets" <+> pPrint d p (aspsi_output_rsts aspsi)
$+$ text "Inouts" <+> pPrint d p (aspsi_ifc_iots aspsi)
$+$ text "Methods" <+> pPrint d p (aspsi_methods aspsi)
-- $+$ text "Inlined Ports" <+> pPrint d p (aspsi_inlined_ports aspsi)
)
-- relation from method name to
-- type of method (value, action, actionvalue) as string
-- ports for RDY, EN, val, args
-- names of the associated rules (for methods with actions)
data ASPMethodInfo = ASPMethodInfo {
aspm_name :: AId,
aspm_type :: String,
aspm_mrdyid :: Maybe AId,
aspm_menableid :: Maybe AId,
aspm_resultids :: [AId],
aspm_inputs :: [AId],
aspm_assocrules :: [AId]
}
deriving (Eq, Show)
instance PPrint ASPMethodInfo where
pPrint d p aspmi = text "method:" <+> pPrint d p( aspm_name aspmi)
<+> text (aspm_type aspmi) <> equals <>
braces ( pPrint d 0 (aspm_mrdyid aspmi) <+>
pPrint d 0 (aspm_menableid aspmi) <+>
pPrint d 0 (aspm_resultids aspmi) $+$
pPrint d 0 (aspm_inputs aspmi) $+$
pPrint d 0 (aspm_assocrules aspmi) )
instance NFData ASPMethodInfo where
rnf (ASPMethodInfo n t mr me mres ins assoc) = rnf7 n t mr me mres ins assoc
instance NFData ASPSignalInfo where
rnf (ASPSignalInfo ins oclks orsts iots meths iports rsched muxsels muxvals senables) =
rnf10 ins oclks orsts iots meths iports rsched muxsels muxvals senables
data ASPCommentInfo = ASPCommentInfo {
-- comments on submodule instantiations
aspci_submod_insts :: [(Id, [String])],
-- comments on rules
aspci_rules :: [(AId, [String])]
-- comments on methods
-- aspsi_methods :: ...
}
deriving (Eq, Show)
instance NFData ASPCommentInfo where
rnf (ASPCommentInfo insts rules) = rnf2 insts rules
-- parallel rule groups; total order on state
-- (first rule in the list writes, present only if there are state conflicts)
data ASchedule = ASchedule {
asch_scheduler :: [AScheduler],
-- list of ruleids is REVERSE ordering for execution
asch_rev_exec_order :: [ARuleId]
}
deriving (Eq, Show)
instance NFData ASchedule where
rnf (ASchedule sched order) = rnf2 sched order
newtype AScheduler =
-- esposito: (r,f) s.t.
-- f is the list of conditions for which
-- rule r should not fire when enabled
-- f is expressed as a
-- list of rules that conflict with the rule r.
-- In the future, f should be a list of list of rules,
-- where the sublists are a list of rules which when
-- enabled should disable the firing of r.
-- So [[a,b],[c],[d,e,f]] = !(ab) && !c && !(def)
ASchedEsposito [(ARuleId, [ARuleId])]
deriving (Eq, Show)
instance NFData AScheduler where
rnf (ASchedEsposito fs) = rnf fs
getSchedulerIds :: AScheduler -> [ARuleId]
getSchedulerIds (ASchedEsposito fs) = map fst fs
dropScheduleIds :: [ARuleId] -> ASchedule -> ASchedule
dropScheduleIds dropIds (ASchedule gs order) = (ASchedule gs' order')
where order' = order \\ dropIds
gs' = map (dropSchedulerIds dropIds) gs
dropSchedulerIds :: [ARuleId] -> AScheduler -> AScheduler
dropSchedulerIds dropIds sch =
let result = dropSchedulerIds' dropIds sch in
-- paranoia check
if (not . null) (dropIds `intersect` getSchedulerIds result) then
internalError("ASyntax.dropSchedulerIds " ++ ppReadable dropIds ++
"\n" ++ ppReadable sch)
else result
dropSchedulerIds' :: [ARuleId] -> AScheduler -> AScheduler
dropSchedulerIds' dropIds (ASchedEsposito fs) = ASchedEsposito fs'
where fs' = [(rid, rs') | (rid, rs) <- fs,
not (rid `elem` dropIds),
let rs' = rs \\ dropIds]
type AId = Id
type AMethodId = AId
data AType =
-- Bit k
ATBit {
atb_size :: ASize
}
-- sized or unsized string
| ATString {
ats_maybe_size :: Maybe ASize
}
-- Verilog real number
| ATReal
-- PrimArray
| ATArray {
atr_length :: ASize,
atr_elem_type :: AType
}
-- Tuple type, for methods with multiple return values
| ATTuple {
att_elem_types :: [AType]
}
-- abstract type, PrimAction, Interface, Clock, ..
-- (can take size parameters as arguments)
| ATAbstract {
ata_id :: AId,
ata_sizes :: [ASize]
}
deriving (Eq, Ord, Show)
instance NFData AType where
rnf (ATBit sz) = rnf sz
rnf (ATString msz) = rnf msz
rnf ATReal = ()
rnf (ATArray len typ) = rnf2 len typ
rnf (ATTuple typs) = rnf typs
rnf (ATAbstract aid args) = rnf2 aid args
instance HasPosition AType where
getPosition (ATAbstract {ata_id = id}) = getPosition id
getPosition _ = noPosition
aTZero, aTBool, aTNat :: AType
aTZero = ATBit 0
aTBool = ATBit 1
aTNat = ATBit 32
aTAction, aTClock, aTReset :: AType
aTAction = ATAbstract idPrimAction []
aTClock = ATAbstract idClock []
aTReset = ATAbstract idReset []
aTInout, aTInout_ :: ASize -> AType
aTInout n = ATAbstract idInout [n]
aTInout_ n = ATAbstract idInout_ [n]
-- Useful routines for working with ATString types
isStringType :: AType -> Bool
isStringType (ATString _) = True
isStringType _ = False
isUnsizedString :: AType -> Bool
isUnsizedString (ATString Nothing) = True
isUnsizedString _ = False
isSizedString :: AType -> Bool
isSizedString (ATString (Just n)) = True
isSizedString _ = False
dropSize :: AType -> AType
dropSize (ATString (Just _)) = ATString Nothing
dropSize x = x
unifyStringTypes :: [AType] -> AType
unifyStringTypes [] = internalError "unifyStringTypes given an empty list"
unifyStringTypes (t:ts) | isUnsizedString t = t
| otherwise = helper t ts
where helper t [] = t
helper t (t1:ts) | t /= t1 = dropSize t
| otherwise = helper t ts
isTupleType :: AType -> Bool
isTupleType (ATTuple _) = True
isTupleType _ = False
type ASize = Integer
getArrayElemType :: AType -> AType
getArrayElemType (ATArray _ t) = t
getArrayElemType t = internalError ("getArrayElemType: " ++ ppReadable t)
getArraySize :: AType -> ASize
getArraySize (ATArray sz _) = sz
getArraySize t = internalError ("getArraySize: " ++ ppReadable t)
-- ----------
-- module inputs
-- This a module input (or inout) port at the hardware level.
-- This is how module inputs are expressed post AState.
-- It is also how method inputs are currently represented.
-- (If we want to support method inputs which are synthesized to multiple
-- ports, such as structs with a port for each field, then we would change
-- then to be AAbstractInput.)
type AInput = (AId, AType)
-- One source-language method argument, decomposed into the hardware input
-- ports it occupies (a single port for an unsplit argument, several for a
-- split struct/tuple). A method's arguments are then a list of these groups.
type AMethodInput = [AInput]
-- These are abstract inputs (including inouts), which can map to one or more
-- hardware ports. These are used in APackage for module arg inputs, prior to
-- being converted to AInput in AState.
data AAbstractInput =
-- simple input using one port
AAI_Port AInput |
-- clock osc and maybe gate
AAI_Clock AId (Maybe AId) |
AAI_Reset AId |
AAI_Inout AId Integer
deriving (Eq, Show)
instance NFData AAbstractInput where
rnf (AAI_Port p) = rnf p
rnf (AAI_Clock osc mgate) = rnf2 osc mgate
rnf (AAI_Reset wire) = rnf wire
rnf (AAI_Inout wire sz) = rnf2 wire sz
absInputToPorts :: AAbstractInput -> [AInput]
absInputToPorts (AAI_Port p) = [p]
absInputToPorts (AAI_Clock osc Nothing) = [(osc, aTBool)]
absInputToPorts (AAI_Clock osc (Just gate)) = [(osc, aTBool), (gate, aTBool)]
absInputToPorts (AAI_Reset r) = [(r,aTBool)]
absInputToPorts (AAI_Inout r n) = [(r,ATAbstract {ata_id = idInout_, ata_sizes = [n]})]
-- ----------
-- Verilog instance output: name and type
type AStateOut = (AId, AType)
-- module outputs (export list): types can be seen from module definition
type AOutput = (AId, AType)
data AVInst = AVInst {
avi_vname :: AId, -- name of Verilog instance
avi_type :: AType, -- type (like ATAbstract "Prelude.VReg")
avi_user_import :: Bool, -- whether it is a foreign module
-- This field records the types/sizes of method inputs and outputs.
-- XXX This list corresponds to vFields in the VModInfo, but cannot be
-- XXX stored there, because VModInfo is created before types are known.
-- There is a triple for each method in vFields of VModInfo.
-- The first component lists the types of the ports arising from each
-- method argument. The second is maybe the type of the EN, and the
-- third is the types of the output ports arising from the method result.
-- NOTE: These are the output language types (i.e. ATBit n)
avi_meth_types :: [([[AType]], Maybe AType, [AType])],
-- This field maps source-language types to their corresponding ports
avi_port_types :: M.Map VName IType,
avi_vmi :: VModInfo, -- Verilog names, conflict info, etc.
avi_iargs :: [AExpr], -- list of instantiation arguments
-- The number of used copies of each method (up to the max multiplicity)
-- To ensure correlation, it is a pair of the method id to its
-- number of used copies. (The total multiplicity is in the VFieldInfo.)
avi_iarray :: [(AId, Integer)]
}
deriving (Eq, Show)
-- Return output clock and reset wires (including "outhigh" gate ports).
-- Note even though all special wires (so far) have type ATBit 1,
-- it is controllable here.
-- Returns the state-element-output id, the ASyntax type,
-- and the Verilog name of the output port to connect to.
getSpecialOutputs :: AVInst -> [(AId, AType, VPort)]
getSpecialOutputs avi =
let
extractClkPorts (_, osc_port, Nothing) = [osc_port]
extractClkPorts (_, osc_port, Just gate_port) = [osc_port, gate_port]
-- throw away the association with the clock/reset name
clk_ports = concatMap extractClkPorts (getOutputClockPorts avi)
rst_ports = map snd (getOutputResetPorts avi)
iot_ports = map snd (getIfcInoutPorts avi)
in
-- nub because special wires (e.g. an oscillator)
-- can theoretically be reused
nub (clk_ports ++ rst_ports ++ iot_ports)
-- Does not return clock gates ports which are "outhigh"
getOutputClockWires :: AVInst ->
[(AId, -- Clock Id
AId, -- Osc
Maybe AId)] -- Gate
getOutputClockWires avi =
let
vmi = avi_vmi avi
out_clocks = [id | (Clock id) <- vFields vmi]
mkOscWire osc_name = mkOutputWireId (avi_vname avi) osc_name
mkGateWire gate_name = mkOutputWireId (avi_vname avi) gate_name
clock_wires clk_id =
case (lookupOutputClockWires clk_id vmi) of
(osc_name, Nothing) ->
(clk_id, mkOscWire osc_name, Nothing)
(osc_name, Just gate_name) ->
(clk_id, mkOscWire osc_name, Just (mkGateWire gate_name))
in
map clock_wires out_clocks
getOutputClockPorts :: AVInst ->
[(AId, -- Clock Id
(AId, AType, VPort), -- Osc
Maybe (AId, AType, VPort))] -- Gate
getOutputClockPorts avi =
let
vmi = avi_vmi avi
out_clocks = [id | (Clock id) <- vFields vmi]
mkOscPort clk_name =
(mkOutputWireId (avi_vname avi) clk_name,
ATBit 1,
(clk_name, [VPclock]))
mkGatePort (clk_gate_name, portprops) =
(mkOutputWireId (avi_vname avi) clk_gate_name,
ATBit 1,
(clk_gate_name, (VPclockgate:portprops)))
clock_ports id =
case (lookupOutputClockPorts id vmi) of
(clk_name, Nothing) ->
(id, mkOscPort clk_name, Nothing)
(clk_name, Just clk_gate_vport) ->
(id, mkOscPort clk_name, Just (mkGatePort clk_gate_vport))
in
map clock_ports out_clocks
getOutputResetPorts :: AVInst -> [(AId, (AId, AType, VPort))]
getOutputResetPorts avi =
let
vmi = avi_vmi avi
output_resets = [id | (Reset id) <- vFields vmi]
mkResetPort rst_name = (mkOutputWireId (avi_vname avi) rst_name,
ATBit 1,
(rst_name, [VPreset]))
reset_ports id =
let rst_name = lookupOutputResetPort id vmi
in (id, mkResetPort rst_name)
in
map reset_ports output_resets
getIfcInoutPorts :: AVInst -> [(AId, (AId, AType, VPort))]
getIfcInoutPorts avi =
let
vmi = avi_vmi avi
res_types = map (\ (_,_,rs) -> rs) (avi_meth_types avi)
ifc_inouts = [(id,vn,ty)
| (Inout id vn _ _, rs) <- zip (vFields vmi) res_types,
let ty = case rs of
[r] -> r
_ -> error ("ASyntax.unknown inout " ++ ppReadable id)]
mkInoutPort ty vname = (mkOutputWireId (avi_vname avi) vname,
ty,
(vname, [VPinout]))
inout_ports (id,vn,ty) = (id, mkInoutPort ty vn)
in
map inout_ports ifc_inouts
{-
vNParam :: VModInfo -> Integer
vNParam (VModInfo _ _ _ as _ _ _) = length [x | Param x <- as]
-}
getParams :: AVInst -> [AExpr]
getParams avi = [e | (i, e) <- getInstArgs avi, isParam i]
getPorts :: AVInst -> [AExpr]
getPorts avi = [e | (i, e) <- getInstArgs avi, isPort i]
getInstArgs :: AVInst -> [(VArgInfo, AExpr)]
getInstArgs avi = zip (vArgs vi) es
where vi = avi_vmi avi
es = avi_iargs avi
getClocks :: AVInst -> [AExpr]
getClocks avi = [e | (i,e) <- getInstArgs avi, isClock i]
getResets :: AVInst -> [AExpr]
getResets avi = [e | (i,e) <- getInstArgs avi, isReset i]
getInouts :: AVInst -> [AExpr]
getInouts avi = [e | (i,e) <- getInstArgs avi, isInout i]
-- local definition
data ADef = ADef {
adef_objid :: AId,
adef_type :: AType,
adef_expr :: AExpr,
adef_props :: [DefProp]
}
deriving (Eq, Ord, Show)
instance HasPosition ADef where
getPosition adef = getPosition (adef_objid adef )
instance NFData ADef where
rnf (ADef id typ expr props) = rnf4 id typ expr props
-- last id has original rule if this one comes from a split; Nothing otherwise
-- it's only used as an optimization; it's safe to put Nothing there
data ARule =
ARule {
arule_id :: ARuleId, -- rule name with a suffix
arule_pragmas :: [RulePragma], -- rule pragmas,
-- e.g., no-implicit-conditions
arule_descr :: ARuleDescr, -- string that describes the rule
arule_wprops :: WireProps, -- clock domain and reset information
arule_pred :: APred, -- rule predicate (CAN_FIRE)
arule_actions :: [AAction], -- rule body (actions)
arule_assumps :: [AAssumption], -- assumptions that should hold after this rule executes
arule_parent :: (Maybe ARuleId) -- if this rule came from a split,
-- Just parent rule name
}
deriving (Eq, Show)
instance NFData ARule where
rnf (ARule rid prags descr wprops pred acts assumps mparent) =
rnf8 rid prags descr wprops pred acts assumps mparent
type ARuleDescr = String
type ARuleId = Id
type APred = AExpr
aRuleName :: ARule -> ARuleId
aRuleName = arule_id
aRulePred :: ARule -> AExpr
aRulePred = arule_pred
data AAssumption =
AAssumption {
assump_property :: AExpr, -- property we've assumed holds
assump_actions :: [AAction] -- actions to take if the property does NOT hold
-- cannot include method calls
}
deriving (Eq, Show)
instance NFData AAssumption where
rnf (AAssumption prop acts) = rnf2 prop acts
-- the APred is the implicit condition to the scheduler
data AIFace = AIDef { aif_name :: AId,
aif_inputs :: [AMethodInput],
aif_props :: WireProps,
aif_pred :: APred,
aif_value :: ADef,
aif_fieldinfo :: VFieldInfo,
-- value methods have their own assumptions
-- because there is no rule to attach it to
aif_assumps :: [AAssumption] }
| AIAction { aif_inputs :: [AMethodInput],
aif_props :: WireProps,
aif_pred :: APred,
aif_name :: AId,
aif_body :: [ARule],
aif_fieldinfo :: VFieldInfo }
| AIActionValue { aif_inputs :: [AMethodInput],
aif_props :: WireProps,
aif_pred :: APred,
aif_name :: AId,
aif_body :: [ARule],
aif_value :: ADef,
aif_fieldinfo :: VFieldInfo }
-- trivial aif_inputs, props, pred?
| AIClock { aif_name :: AId,
aif_clock :: AClock,
aif_fieldinfo :: VFieldInfo }
| AIReset { aif_name :: AId,
aif_reset :: AReset,
aif_fieldinfo :: VFieldInfo }
| AIInout { aif_name :: AId,
aif_inout :: AInout,
aif_fieldinfo :: VFieldInfo }
deriving (Eq, Show)
instance NFData AIFace where
rnf (AIDef name ins props pred val finfo assumps) =
rnf7 name ins props pred val finfo assumps
rnf (AIAction ins props pred name body finfo) =
rnf6 ins props pred name body finfo
rnf (AIActionValue ins props pred name body val finfo) =
rnf7 ins props pred name body val finfo
rnf (AIClock name clk finfo) = rnf3 name clk finfo
rnf (AIReset name rst finfo) = rnf3 name rst finfo
rnf (AIInout name inout finfo) = rnf3 name inout finfo
aiface_vname :: AIFace -> String
aiface_vname i = getIdString (vf_name (aif_fieldinfo i))
-- wire properties
aIfaceProps :: AIFace -> WireProps
aIfaceProps (AIDef { aif_props = p }) = p
aIfaceProps (AIAction { aif_props = p }) = p
aIfaceProps (AIActionValue { aif_props = p }) = p
aIfaceProps _ = emptyWireProps
-- The result type of an interface method, which may be a tuple for multiple output ports.
aIfaceResType :: AIFace -> AType
-- XXX should be ATAction?
aIfaceResType (AIAction { }) = ATBit 0
aIfaceResType (AIDef { aif_value = (ADef _ t _ _)}) = t
aIfaceResType (AIActionValue { aif_value = (ADef _ t _ _)}) = t
-- should not need type of clock or reset
aIfaceResType x = internalError ("aIfaceResType: " ++ show x)
aIfaceResIds :: AIFace -> [AId]
aIfaceResIds (AIDef {aif_name=id}) = [id]
aIfaceResIds (AIActionValue {aif_name=id}) = [id]
aIfaceResIds _ = []
-- Source-language argument groups (each group is one method argument, which
-- may decompose to multiple hardware ports).
aIfaceArgGroups :: AIFace -> [AMethodInput]
aIfaceArgGroups (AIClock {}) = []
aIfaceArgGroups (AIReset {}) = []
aIfaceArgGroups (AIInout {}) = []
aIfaceArgGroups f = aif_inputs f
-- Flat list of hardware-level input ports, in order, expanding multi-port
-- argument groups.
aIfaceArgs :: AIFace -> [AInput]
aIfaceArgs = concat . aIfaceArgGroups
-- associate the internal and external names and width of AIFace args
aiface_argnames_width :: AIFace -> [(AId, String, Integer)]
aiface_argnames_width (AIClock {}) = []
aiface_argnames_width (AIReset {}) = []
aiface_argnames_width aif =
let ports = aIfaceArgs aif
in zip3 (map fst ports)
(map (getVNameString . fst) (vfMethodArgPorts (aif_fieldinfo aif)))
(map aIfaceArgSize ports)
aIfaceArgSize :: AInput -> Integer
aIfaceArgSize (_, ATBit size) = size
aIfaceArgSize x = internalError ("aIfaceArgSize: " ++ show x)
aIfaceRules :: AIFace -> [ARule]
aIfaceRules (AIAction { aif_body = rs}) = rs
aIfaceRules (AIActionValue { aif_body = rs}) = rs
aIfaceRules _ = []
aIfaceRulesImpl :: AIFace -> [(ADef, ARule)]
aIfaceRulesImpl (AIAction { aif_name = i,
aif_body = rs }) = map (addRdyToARule rdyId) rs
where rdyId = mkRdyId i
aIfaceRulesImpl (AIActionValue { aif_name = i,
aif_body = rs }) = map (addRdyToARule rdyId) rs
where rdyId = mkRdyId i
aIfaceRulesImpl _ = []
addRdyToARule :: Id -> ARule -> (ADef, ARule)
addRdyToARule rdyId r0@(ARule { arule_id = ri, arule_pred = e }) = (d, r)
where di = mkRdyId (mkRdyId ri)
d = ADef di aTBool
(APrim di aTBool PrimBAnd [e, ASDef aTBool rdyId]) []
r = r0 { arule_pred = (ASDef aTBool di) }
-- The names of value methods and action/actionvalue rules
aIfaceSchedNames :: AIFace -> [ARuleId]
aIfaceSchedNames (AIAction { aif_body = rs}) = map arule_id rs
aIfaceSchedNames (AIActionValue { aif_body = rs}) = map arule_id rs
aIfaceSchedNames (AIDef { aif_name = i }) = [i]
aIfaceSchedNames _ = []
aIfacePred :: AIFace -> APred
aIfacePred ifc@(AIDef {}) = aif_pred ifc
aIfacePred ifc@(AIAction {}) = aif_pred ifc
aIfacePred ifc@(AIActionValue {}) = aif_pred ifc
aIfacePred ifc@(AIClock {}) =
internalError ("aIfacePred: unexpected clock: " ++ ppReadable ifc)
aIfacePred ifc@(AIReset {}) =
internalError ("aIfacePred: unexpected reset: " ++ ppReadable ifc)
aIfacePred ifc@(AIInout {}) =
internalError ("aIfacePred: unexpected inout: " ++ ppReadable ifc)
aIfaceMethods :: [AIFace] -> [AIFace]
aIfaceMethods is =
let getMethod (AIClock {}) = Nothing
getMethod (AIReset {}) = Nothing
getMethod (AIInout {}) = Nothing
getMethod i = Just i
in mapMaybe getMethod is
aIfaceHasAction :: AIFace -> Bool
aIfaceHasAction (AIAction {}) = True
aIfaceHasAction (AIActionValue {}) = True
aIfaceHasAction _ = False
-- note no types because they are implicitly action
data AAction
= ACall { -- state method call
aact_objid :: AId,
acall_methid :: AMethodId,
aact_args :: [AExpr] -- first element of the list is the condition
}
| AFCall {
aact_objid :: AId, -- foreign function call
afcall_fun :: String,
afcall_isC :: Bool,
aact_args :: [AExpr], -- first element of the list is the condition
-- is it an action inserted by BSC to check an assumption
aact_assump :: Bool
}
-- action part of a foreign ActionValue function
| ATaskAction {
aact_objid :: AId,
ataskact_fun :: String,
ataskact_isC :: Bool,
ataskact_cookie :: Integer, -- correlation cookie
aact_args :: [AExpr], -- first element is the condition
-- the temporary to set, spliced in later
ataskact_temp :: (Maybe Id),
-- include the return value type for easy reference,
-- and to support foreign functions with ignored values
ataskact_value_type :: AType,
-- is it an action inserted by BSC to check an assumption
aact_assump :: Bool
}
deriving (Eq, Ord, Show)
instance NFData AAction where
rnf (ACall oid mid args) = rnf3 oid mid args
rnf (AFCall oid fun isC args assump) = rnf5 oid fun isC args assump
rnf (ATaskAction oid fun isC cookie args temp vtyp assump) =
rnf8 oid fun isC cookie args temp vtyp assump
data AClock = AClock {
aclock_osc :: AExpr, -- must be of type ATBit 1
aclock_gate :: AExpr -- must be of type ATBit 1
}
deriving(Eq, Ord, Show)
-- the Ord instance has little meaning - it is used for Maps and the like
-- the Eq instance should be accurate
-- (same oscillator and gate ==> same clock)
-- though it may not catch aliasing
instance PPrint AClock where
pPrint d p (AClock osc gate) =
(text "{ osc: ") <+>
(pPrint d p osc) <+>
(text "gate: ") <+>
(pPrint d p gate) <+>
(text "}")
instance NFData AClock where
rnf (AClock osc gate) = rnf2 osc gate
mkOutputWireId :: AId -> VName -> AId