Repository navigation
Expand file tree
/
Copy pathEx8_Roundtrip.lean
More file actions
404 lines (346 loc) · 21.2 KB
/
Copy pathEx8_Roundtrip.lean
File metadata and controls
404 lines (346 loc) · 21.2 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
-- Tutorial example 8: IR -> SystemVerilog -> IR, what is proved and what is not.
--
-- Every claim in docs/tutorial/md/Ch07c_VerifiedSynthesis.md §7c.10 is pinned here.
-- Part A: three expressions INSIDE the proved fragment `SFrag` (theorem `roundtrip_sem`).
-- Part B: one module that passes `seqCheck` (theorem `certified_forward_trace_module`).
-- Part C: things with NO theorem: expressions outside the fragment, text -> tree, the shipping
-- `partial def`s, and the XiangShan-class bugs.
--
-- Run from the repository root:
-- lake build Tools.VedU1Entry Tools.SVParser.RoundtripProof
-- lake env lean docs/research/examples/tutorial/Ex8_Roundtrip.lean ; echo "exit $?"
import Tools.SVParser.RoundtripProof
import Tools.VedU1Entry
open Sparkle.IR.AST Sparkle.IR.Semantics
open Tools.SVParser Tools.SVParser.EmitAst Tools.SVParser.RoundtripProof Tools.SVParser.EmitSem
open Tools.SVParser.Lower
/-! ## The width table, the environment, and one helper -/
/-- Widths of the names used below. `unknown` has none. -/
def wof : String → Option Nat
| "a" => some 8 | "b" => some 8 | "s" => some 1 | "w16" => some 16 | "w40" => some 40
| "d138" => some 138 | _ => none
/-- The width environment the theorems use: the table, 0 for an unknown name. -/
def we : WEnv := fun n => (wof n).getD 0
/-- One environment with every value inside its width. -/
def env : Env := fun n => match n with
| "a" => 200 | "b" => 100 | "s" => 0 | "w16" => 4660 | "w40" => 1 | _ => 0
theorem env_bounded : Bounded we env := by
intro n
simp only [we, env]
split <;> first | exact Nat.two_pow_pos _ | simp_all [wof]
/-- The shipping emitter's text for an expression. (`emitExpr` is a `partial def`.) -/
def textOf (e : Expr) : String := Sparkle.Backend.Verilog.emitExpr wof e
/-! ## A. PROVED: three expressions inside `SFrag` -/
/-- `a + b`, both 8 bits. -/
def eAdd : Expr := .op .add [.ref "a", .ref "b"]
/-- `s ? a : b`, both arms 8 bits. -/
def eMux : Expr := .op .mux [.ref "s", .ref "a", .ref "b"]
/-- `a[5:2]`, in range for an 8-bit `a`. -/
def eSlice : Expr := .slice (.ref "a") 5 2
-- The decidable mirror of `SFrag` accepts all three.
#guard sfragCheck wof eAdd
#guard sfragCheck wof eMux
#guard sfragCheck wof eSlice
-- The emitted SystemVerilog text.
#guard textOf eAdd == "(a + b)"
#guard textOf eMux == "(s ? a : b)"
#guard textOf eSlice == "a[5:2]"
-- `SFrag` itself, built from its constructors (no environment needed: the three expressions
-- read only declared names, and the side conditions are about widths and names).
-- NB: `decide` cannot prove `sanitizeName "a" = "a"` (it gets stuck inside `String.all`);
-- `simp [sanitizeName]` can, and that is what the `ref` constructors below use.
theorem sfrag_add (e' : Env) : SFrag wof we e' eAdd :=
.binop .add (by decide) (.ref "a" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl) (.ref "b" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl)
theorem sfrag_mux (e' : Env) : SFrag wof we e' eMux :=
.mux rfl (.ref "s" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl) (.ref "a" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl) (.ref "b" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl)
theorem sfrag_slice (e' : Env) : SFrag wof we e' eSlice :=
.sliceRefKeep "a" 5 2 (by simp [Sparkle.Backend.Verilog.sanitizeName]) (Or.inr (by decide +kernel)) (Or.inr (by decide +kernel))
(Or.inr rfl)
/-- `roundtrip_sem` applied to exactly `a + b`: emit it with the real `emitAstExpr`, lower it
back with `lowerT`; the result has the SAME width and the SAME value, in every bounded
environment. -/
theorem add_roundtrip (e' : Env) (_h : Bounded we e') :
∃ x, (emitAstExpr wof eAdd).bind lowerT = some x ∧ widthOf we x = widthOf we eAdd ∧
evalExpr we e' x = evalExpr we e' eAdd :=
roundtrip_sem (sfrag_add e')
theorem mux_roundtrip (e' : Env) (_h : Bounded we e') :
∃ x, (emitAstExpr wof eMux).bind lowerT = some x ∧ widthOf we x = widthOf we eMux ∧
evalExpr we e' x = evalExpr we e' eMux :=
roundtrip_sem (sfrag_mux e')
theorem slice_roundtrip (e' : Env) (_h : Bounded we e') :
∃ x, (emitAstExpr wof eSlice).bind lowerT = some x ∧ widthOf we x = widthOf we eSlice ∧
evalExpr we e' x = evalExpr we e' eSlice :=
roundtrip_sem (sfrag_slice e')
-- What the theorem says, with numbers. At `env` (a = 200, b = 100, s = 0):
-- a + b width 8, value 44 (300 mod 256)
-- s ? a : b width 8, value 100 (s = 0 picks b)
-- a[5:2] width 4, value 2 (200 = 0b11001000; bits 5..2 = 0b0010)
-- The lowered expression has the same width and value; here it is even the same tree.
#guard widthOf we eAdd == 8 && evalExpr we env eAdd == some 44
#guard widthOf we eMux == 8 && evalExpr we env eMux == some 100
#guard widthOf we eSlice == 4 && evalExpr we env eSlice == some 2
#guard (emitAstExpr wof eAdd).bind lowerT == some eAdd
#guard (emitAstExpr wof eMux).bind lowerT == some eMux
#guard (emitAstExpr wof eSlice).bind lowerT == some eSlice
/-- info: 'add_roundtrip' depends on axioms: [propext, Classical.choice, Quot.sound] -/
#guard_msgs in
#print axioms add_roundtrip
/-! ### A module that passes `seqCheck`: delay8 -/
open Sparkle.Core.Domain Sparkle.Core.Signal
def delay8 (x : Signal defaultDomain (BitVec 8)) : Signal defaultDomain (BitVec 8) :=
Signal.register 0#8 x
#ved_u1 delay8
open Sparkle.IR.Type in
/-- The module `#ved_u1 delay8` returned, as a compiled literal (`ved_mod` has no compiled code,
so `#guard` could not call it). The next line checks that the literal IS `delay8.ved_mod`. -/
def delayMod : Module :=
{ name := "delay8", parameters := [],
inputs := [{ name := "_gen_x", ty := .bitVector 8 }, { name := "clk", ty := .bit },
{ name := "rst", ty := .bit }],
outputs := [{ name := "out", ty := .bitVector 8 }],
wires := [{ name := "_gen_x", ty := .bitVector 8 }, { name := "_gen_out", ty := .bitVector 8 }],
body := [.register "_gen_out" "clk" ("rst", .synchronous) (.ref "_gen_x") 0,
.assign "out" (.ref "_gen_out")],
assertions := [], isPrimitive := false }
example : delay8.ved_mod = delayMod := by rfl
-- The check, run: `seqCheck` on delay8's body, with the module's own width table.
#guard seqCheck (moduleWof delayMod) (weOf (moduleWof delayMod)) delayMod.body
/-- The same fact as a proof term (`hchk` below). `decide` cannot do it (`sf4Check` and
`sanitizeName` do not reduce); `simp` unfolds them. -/
theorem delay8_seqCheck :
seqCheck (moduleWof delayMod) (weOf (moduleWof delayMod)) delayMod.body = true := by
simp [seqCheck, sf4Check, delayMod, moduleWof, weOf, Sparkle.Backend.Verilog.sanitizeName,
Sparkle.IR.Semantics.widthOf]
-- The SystemVerilog text the shipping emitter prints for the module.
#guard Sparkle.Backend.Verilog.emitModule delayMod ==
"// Generated by Sparkle HDL\n// Module: delay8\n\nmodule delay8 (\n input logic [7:0] _gen_x,\n input logic clk,\n input logic rst,\n output logic [7:0] out\n);\n\n logic [7:0] _gen_out = 8'h0;\n\n\n always_ff @(posedge clk) begin\n if (rst)\n _gen_out <= 8'd0;\n else\n _gen_out <= _gen_x;\n end\n\n assign out = _gen_out;\n\nendmodule\n"
/-- The seeding discipline for delay8 in a 5-cycle run: the stream `_gen_x` = 5, 9, 9, 3, 7 (the
tutorial waveform), every other input 0, the register from the state; EVERY value clipped to its
declared width (a name with no width is clipped to 0). The theorem needs this clipping because its
hypothesis `hseed` is about all states `st`, not only the reachable ones. `runModule` counts its
index DOWN (as in Ex7), so index `t` reads stream time `4 - t`. -/
def seed8 (t : Nat) (st : String → Nat) : Env := fun n =>
(if n == "_gen_x" then [5, 9, 9, 3, 7].getD (4 - t) 0
else if n == "_gen_out" then st n else 0) % 2 ^ weOf (moduleWof delayMod) n
/-- The hypothesis `hseed` of `certified_forward_trace_module`, written out for delay8: every
environment the seed produces is `Bounded`, for every time and every state. -/
theorem seed8_bounded : ∀ t st, Bounded (weOf (moduleWof delayMod)) (seed8 t st) :=
fun _ _ _ => Nat.mod_lt _ (Nat.two_pow_pos _)
/-- The theorem, for delay8, with its two hypotheses supplied:
`hchk` (`delay8_seqCheck`, the `seqCheck` above) and `hseed` (`seed8_bounded`). The conclusion: the
emitter produces the three programs `pairs regs mprog`, and for every cycle count `k`, every
state and every memory, the IR run equals the SystemVerilog-model run. -/
theorem delay8_certified :
∃ pairs regs mprog,
emitAssigns (moduleWof delayMod) delayMod.body = some pairs
∧ emitRegs (moduleWof delayMod) delayMod.body = some regs
∧ emitMemWrites (moduleWof delayMod) delayMod.body = some mprog
∧ ∀ (k : Nat) (st : String → Nat) (mems : MEnv),
runModule (weOf (moduleWof delayMod)) delayMod.body seed8 k st mems
= runModuleSV (moduleWof delayMod) pairs regs mprog seed8 k st mems :=
certified_forward_trace_module delay8_seqCheck seed8 seed8_bounded
/-- info: 'delay8_certified' depends on axioms: [propext, Classical.choice, Quot.sound] -/
#guard_msgs in
#print axioms delay8_certified
-- The two runs, side by side, 5 cycles, starting from the register at 0:
-- per cycle (`_gen_x`, `_gen_out`, `out`).
def irRun : Option (List (List Nat)) :=
(runModule (weOf (moduleWof delayMod)) delayMod.body seed8 5 (fun _ => 0) (fun _ _ => 0)).map
(·.map fun e => ["_gen_x", "_gen_out", "out"].map e)
def svRun : Option (List (List Nat)) := do
let pairs ← emitAssigns (moduleWof delayMod) delayMod.body
let regs ← emitRegs (moduleWof delayMod) delayMod.body
let mprog ← emitMemWrites (moduleWof delayMod) delayMod.body
let envs ← runModuleSV (moduleWof delayMod) pairs regs mprog seed8 5 (fun _ => 0) (fun _ _ => 0)
some (envs.map fun e => ["_gen_x", "_gen_out", "out"].map e)
-- t: 0 1 2 3 4
-- _gen_x: 5 9 9 3 7
-- _gen_out: 0 5 9 9 3
-- out: 0 5 9 9 3 (IR and SystemVerilog model: the same)
#guard irRun == some [[5, 0, 0], [9, 5, 5], [9, 9, 9], [3, 9, 9], [7, 3, 3]]
#guard svRun == irRun
-- This theorem and `delay8.ved_u1` are NOT one theorem. They read different width functions:
-- `ved_u1` uses `declWidth` (the wire table only), this one `weOf (moduleWof ·)` (wires, inputs
-- and outputs). They disagree on `out`, `clk` and `rst`:
#guard Tools.VedU1Contract.declWidth delayMod "out" == 0
#guard weOf (moduleWof delayMod) "out" == 8
#guard Tools.VedU1Contract.declWidth delayMod "clk" == 0
#guard weOf (moduleWof delayMod) "clk" == 1
/-! ## B. NOT PROVED: outside the fragment -/
/-- `~w40`: `not` of a 40-bit wire. `SFrag.not` needs width ≤ 32. -/
def eNot40 : Expr := .op .not [.ref "w40"]
/-- `s ? a : w16`: arms of width 8 and 16. -/
def eMuxW : Expr := .op .mux [.ref "s", .ref "a", .ref "w16"]
/-- `a[11:4]`: the select runs past bit 7 of an 8-bit `a`. -/
def eSliceOut : Expr := .slice (.ref "a") 11 4
/-- `~unknown`: `not` of a name with no declared width. -/
def eNotUnk : Expr := .op .not [.ref "unknown"]
-- The mirror of `SFrag` rejects all four.
#guard !sfragCheck wof eNot40
#guard !sfragCheck wof eMuxW
#guard !sfragCheck wof eSliceOut
#guard !sfragCheck wof eNotUnk
-- What the emitter prints for them anyway.
#guard textOf eNot40 == "(40'(w40 ^ 40'd1099511627775))"
#guard textOf eMuxW == "(s ? a : w16)"
#guard textOf eSliceOut == "8'((a) >> 4)"
#guard textOf eNotUnk == "~(unknown)"
-- `roundtrip_sem` has no case for them: it needs `SFrag`. The module-level theorem uses a
-- DIFFERENT predicate, `sf4Check`. It accepts `~w40` (no 32-bit limit) and rejects the other three:
#guard sf4Check wof we eNot40
#guard !sf4Check wof we eMuxW
#guard !sf4Check wof we eSliceOut
#guard !sf4Check wof we eNotUnk
-- (1) Mux with different arm widths. With s = 0 (`env`), the IR mux passes `w16` through
-- unmasked: its value is 4660, but the width it reports is the THEN arm's, 8. 4660 does not fit
-- 8 bits. The SystemVerilog model gives the ternary the wider width, 16. Same value, two widths:
-- exactly the mismatch `SFrag.mux` rules out by requiring equal arm widths.
#guard widthOf we eMuxW == 8 && evalExpr we env eMuxW == some 4660
#guard (emitAstExpr wof eMuxW).bind (fun sv => Tools.SVParser.SVSemantics.widthSV wof sv)
== some 16
-- So `4660 < 2 ^ 8` is false: the value is not bounded by the width the IR reports.
#guard !decide (4660 < 2 ^ widthOf we eMuxW)
-- What is not guaranteed: that the emitted ternary keeps the IR's width. No theorem says so.
-- (2) Out-of-range slice. The emitted text is a cast of a shift, not a part-select. On this input
-- the IR gives 12 (200 >> 4); the SV side is not evaluated here, and there is no theorem that
-- the two agree.
#guard evalExpr we env eSliceOut == some 12
-- (3) `~` of 40 bits: IR value of `~w40` at w40 = 1 is 2^40 - 2; the SystemVerilog model agrees.
#guard evalExpr we env eNot40 == some 1099511627774
#guard (emitAstExpr wof eNot40).bind (fun sv => Tools.SVParser.SVSemantics.evalSV wof env 0 sv)
== some 1099511627774
-- Agreement on one input. `roundtrip_sem` is silent here (the width ≤ 32 condition); the
-- module-level theorem covers it by `sf4Check`.
/-! ## C. NOT PROVED: text -> tree -/
-- The syntax tree is printed as TEXT and the text is parsed back by the shipping parser.
-- `roundtrip_sem` is about the tree before printing. Here is one real text and what the
-- parser makes of it:
#guard textOf eMux == "(s ? a : b)"
#guard (match parseExprString (textOf eMux) with
| .ok sv => emitAstExpr wof eMux == some sv
| .error _ => false)
-- That is a CHECK on this input (and on those in EmitAst.lean's `#guard` list). No theorem
-- says `parse (print t) = t`.
--
-- A concrete loss in that step: an asynchronous-reset register and a synchronous one print as
-- different text, and the parser makes the SAME tree of both: it keeps only the first
-- `posedge` of a sensitivity list.
open Sparkle.IR.Type in
def delayModAsync : Module :=
{ delayMod with
body := [.register "_gen_out" "clk" ("rst", .asynchronous) (.ref "_gen_x") 0,
.assign "out" (.ref "_gen_out")] }
#guard Sparkle.Backend.Verilog.emitModule delayModAsync != Sparkle.Backend.Verilog.emitModule delayMod
/-- Both texts parse, and to the same tree. A text that fails to parse is never "the same". -/
def sameTree (a b : String) : Bool :=
match Tools.SVParser.Parser.parseModuleFromString a, Tools.SVParser.Parser.parseModuleFromString b with
| .ok x, .ok y => toString (repr x) == toString (repr y)
| _, _ => false
#guard (Tools.SVParser.Parser.parseModuleFromString (Sparkle.Backend.Verilog.emitModule delayMod)).toOption.isSome
#guard sameTree (Sparkle.Backend.Verilog.emitModule delayModAsync) (Sparkle.Backend.Verilog.emitModule delayMod)
-- Controls: two texts that both fail to parse are not "the same tree" (comparing the two
-- `Option`s directly would say `none == none`), and a failing text never matches a good one.
#guard !sameTree "not verilog" "not verilog either"
#guard !sameTree "not verilog" (Sparkle.Backend.Verilog.emitModule delayMod)
-- (the reset KIND is re-detected later from the shape of the `if`; see EmitAst.lean M0 notes.)
/-! ## D. NOT PROVED: the shipping emitter and parser are `partial def`s -/
-- `Sparkle.Backend.Verilog.emitExpr` is `partial`: Lean makes it an opaque constant with no
-- unfolding equations, so no proof can compute with it (and `decide` gets stuck on it).
/-- info: opaque Sparkle.Backend.Verilog.emitExpr : (optParam (String → Option Nat) fun x => none) → Expr → String -/
#guard_msgs in
#print Sparkle.Backend.Verilog.emitExpr
/-- error: Unknown constant `Sparkle.Backend.Verilog.emitExpr.eq_1` -/
#guard_msgs (error) in
#check @Sparkle.Backend.Verilog.emitExpr.eq_1
-- The theorems are therefore about TOTAL twins (`emitAstExpr`, `lowerT`, `emitFrag`, ...). The
-- link from a twin to the shipping function is a list of `#guard`s: the ones in
-- `Tools/SVParser/EmitAst.lean` (about 50 expressions, each: parse (shipping text) = twin tree).
-- What they do NOT cover: every expression not on that list. Here is a wider sweep, written for
-- this file: all leaves, `not`/`neg` of a leaf, every pair for 12 binary operators, and every
-- mux over leaves.
def leaves : List Expr :=
[.ref "a", .ref "b", .ref "s", .ref "w16", .ref "w40", .ref "unknown", .const 3 8, .const 1 1]
def bins : List Operator :=
[.and, .or, .xor, .add, .sub, .mul, .eq, .lt_u, .shl, .shr, .lt_s, .asr]
def sweep : List Expr :=
leaves ++ leaves.map (fun x => .op .not [x]) ++ leaves.map (fun x => .op .neg [x]) ++
(bins.flatMap fun op => leaves.flatMap fun x => leaves.map fun y => .op op [x, y]) ++
(leaves.flatMap fun c => leaves.flatMap fun t => leaves.map fun f => .op .mux [c, t, f])
def textMatchesTwin (e : Expr) : Bool :=
match parseExprString (textOf e) with
| .ok sv => emitAstExpr wof e == some sv
| .error _ => false
#guard sweep.length == 1304
-- every one of the 1304 agrees (shipping text, parsed, equals the twin's tree) ...
#guard (sweep.filter textMatchesTwin).length == 1304
-- ... and the proved fragment covers only some of them:
#guard (sweep.filter (sfragCheck wof)).length == 679
-- The sweep is still finite: depth 1 over 8 leaves. It says nothing about deeper trees, other
-- widths, slices, concats, or the statement and module layers.
/-! ## E. The documented bugs of this class -/
-- `~` widened by context (ParserTest Test 58, XiangShan NCBUpstreamRXREQ). Verilog's `~` takes
-- the width of its CONTEXT. The fix prints a width-pinned form whenever the operand's width is
-- known:
#guard textOf (.op .not [.ref "a"]) == "(8'(a ^ 8'd255))"
#guard textOf (.op .not [.op .eq [.ref "a", .const 0 8]]) == "(1'((a == 8'd0) ^ 1'd1))"
-- ... and when the width is UNKNOWN, the emitter still falls back to the unpinned form:
#guard textOf eNotUnk == "~(unknown)"
-- That input is outside `SFrag` (the `not` case needs a known width) and outside `sf4Check`
-- (checked above). What guards it today: ParserTest Test 58 (one module; asserts that the
-- re-emitted text contains no `~(`), the XiangShan co-simulation and the yosys gate. Not a proof.
-- Dropped slice offset (ParserTest Tests 51, 57, 60) and the wide-concat copy (Test 52). Those
-- bugs were in the C simulator's wide-value code: for 51 and 57 the tests record that the IR and
-- the re-emitted Verilog were correct and only the JIT was wrong. So they are NOT in the layer
-- `roundtrip_sem` talks about. The slice offset at the IR level is covered: a part-select of a
-- 138-bit wire with `lo ≠ 0` is inside `SFrag` (`sliceRefKeep`) and the emitted text keeps it:
def eWide : Expr := .slice (.ref "d138") 137 69
#guard sfragCheck wof eWide
#guard textOf eWide == "d138[137:69]"
#guard (emitAstExpr wof eWide).bind lowerT == some eWide
theorem sfrag_wide (e' : Env) : SFrag wof we e' eWide :=
.sliceRefKeep "d138" 137 69 (by simp [Sparkle.Backend.Verilog.sanitizeName]) (Or.inr (by decide +kernel))
(Or.inr (by decide +kernel)) (Or.inr rfl)
theorem wide_roundtrip (e' : Env) (_h : Bounded we e') :
∃ x, (emitAstExpr wof eWide).bind lowerT = some x ∧ widthOf we x = widthOf we eWide ∧
evalExpr we e' x = evalExpr we e' eWide :=
roundtrip_sem (sfrag_wide e')
#guard widthOf we eWide == 69
-- The C simulator, which is where those bugs were, has no theorem at all: it is covered by the
-- co-simulation tests only.
-- Concat width (the MiscModule / HPTW class of bench/xiangshan/README.md: concat elements are
-- self-determined in Verilog, so an operator element must be pinned to its width). A concat
-- of `a & b` (8 bits) and `b`:
def eConcat : Expr := .concat [.op .and [.ref "a", .ref "b"], .ref "b"]
-- the operator element is printed with an explicit 8-bit cast:
#guard textOf eConcat == "{8'((a & b)), b}"
#guard sfragCheck wof eConcat
-- at a = 12, b = 10 (`env2`): width 16, value (12 & 10) << 8 | 10 = 8 * 256 + 10 = 2058
def env2 : Env := fun n => match n with | "a" => 12 | "b" => 10 | _ => 0
#guard widthOf we eConcat == 16 && evalExpr we env2 eConcat == some 2058
-- the lowered tree is a different tree (the cast comes back as a slice of a concat) with the
-- same width and value, as `roundtrip_sem` says:
#guard ((emitAstExpr wof eConcat).bind lowerT).map (fun x => (widthOf we x, evalExpr we env2 x))
== some (16, some 2058)
#guard ((emitAstExpr wof eConcat).bind lowerT) != some eConcat
theorem sfrag_concat (e' : Env) : SFrag wof we e' eConcat :=
.concatCons (fun _ _ h => ⟨8, by cases h; rfl, by decide, by cases h; rfl⟩)
(.binop .and (by decide) (.ref "a" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl)
(.ref "b" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl))
(.concatCons (fun _ _ h => by cases h)
(.ref "b" (by simp [Sparkle.Backend.Verilog.sanitizeName]) rfl) .concatNil)
theorem concat_roundtrip (e' : Env) (_h : Bounded we e') :
∃ x, (emitAstExpr wof eConcat).bind lowerT = some x ∧ widthOf we x = widthOf we eConcat ∧
evalExpr we e' x = evalExpr we e' eConcat :=
roundtrip_sem (sfrag_concat e')
-- The same shape with an operator element of UNKNOWN width is outside the fragment, and is
-- printed without the cast:
def eConcatUnk : Expr := .concat [.op .and [.ref "a", .ref "unknown"], .ref "b"]
#guard !sfragCheck wof eConcatUnk
#guard !sf4Check wof we eConcatUnk
#guard textOf eConcatUnk == "{(a & unknown), b}"
/-- info: 'concat_roundtrip' depends on axioms: [propext, Classical.choice, Quot.sound] -/
#guard_msgs in
#print axioms concat_roundtrip
/-- info: 'wide_roundtrip' depends on axioms: [propext, Classical.choice, Quot.sound] -/
#guard_msgs in
#print axioms wide_roundtrip