Skip to content

Commit fa3e7b8

Browse files
committed
move spec out
1 parent edc694d commit fa3e7b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+60
-54
lines changed

src/Optimize/Library.fs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,18 @@ type Call =
244244
Arg: Value[]
245245
Span: Span }
246246

247+
type Load =
248+
{ Target: int
249+
Base: Value
250+
Offset: Value }
251+
247252
/// Instrument
248253
type Instr =
249254
| Assign of Assign
250255
| Binary of Binary
251256
| Unary of Unary
252257
| Call of Call
253-
| Load
258+
| Load of Load
254259
| Store
255260
| Alloc
256261

@@ -260,7 +265,7 @@ type Instr =
260265
| Assign a -> a.Target
261266
| Unary n -> n.Target
262267
| Call c -> c.Target
263-
| Load -> failwith "Not Implemented"
268+
| Load l -> l.Target
264269
| Store -> failwith "Not Implemented"
265270
| Alloc -> failwith "Not Implemented"
266271

@@ -273,7 +278,9 @@ type Instr =
273278
| Assign a -> yield a.Value
274279
| Unary u -> yield u.Value
275280
| Call c -> yield! c.Arg
276-
| Load -> failwith "Not Implemented"
281+
| Load l ->
282+
yield l.Base
283+
yield l.Offset
277284
| Store -> failwith "Not Implemented"
278285
| Alloc -> failwith "Not Implemented"
279286
}
@@ -286,18 +293,17 @@ type Branch =
286293
One: int
287294
Span: Span }
288295

289-
type Switch =
296+
type Indirect =
290297
{ Value: Value
291-
Dest: (int * uint64)[]
292-
Default: int
298+
Dest: int[]
293299
Span: Span }
294300

295301
type Ret = { Value: Option<Value>; Span: Span }
296302

297303
type Transfer =
298304
| Jump of Jump
299305
| Branch of Branch
300-
| Switch of Switch
306+
| Indirect of Indirect
301307
| Return of Ret
302308
| Unreachable of Span
303309

@@ -308,11 +314,9 @@ type Transfer =
308314
| Branch b ->
309315
yield b.One
310316
yield b.Zero
311-
| Switch s ->
312-
for dest, _ in s.Dest do
317+
| Indirect s ->
318+
for dest in s.Dest do
313319
yield dest
314-
315-
yield s.Default
316320
| Return _
317321
| Unreachable _ -> ()
318322
}
@@ -341,7 +345,11 @@ type Block =
341345
use_ arg
342346

343347
def c.Target
344-
| Load -> failwith "Not Implemented"
348+
| Load l ->
349+
use_ l.Base
350+
use_ l.Offset
351+
352+
def l.Target
345353
| Store -> failwith "Not Implemented"
346354
| Alloc -> failwith "Not Implemented"
347355

@@ -351,7 +359,7 @@ type Block =
351359
match r.Value with
352360
| None -> ()
353361
| Some v -> use_ v
354-
| Switch s -> use_ s.Value
362+
| Indirect s -> use_ s.Value
355363
| Jump _
356364
| Unreachable _ -> ()
357365

@@ -379,7 +387,12 @@ type Block =
379387
{ c with
380388
Arg = Array.map use_ c.Arg
381389
Target = def c.Target }
382-
| Load -> failwith "Not Implemented"
390+
| Load l ->
391+
Load
392+
{ l with
393+
Base = use_ l.Base
394+
Offset = use_ l.Offset
395+
Target = def l.Target }
383396
| Store -> failwith "Not Implemented"
384397
| Alloc -> failwith "Not Implemented"
385398

@@ -395,7 +408,7 @@ type Block =
395408
| Some v -> Some(use_ v)
396409

397410
Return { r with Value = value }
398-
| Switch s -> Switch { s with Value = use_ s.Value }
411+
| Indirect s -> Indirect { s with Value = use_ s.Value }
399412
| Jump _
400413
| Unreachable _ -> this.Trans
401414

@@ -478,7 +491,7 @@ type Func =
478491
let v = valueToString n.Value
479492
$"! {v}"
480493
| Call c -> failwith "Not Implemented"
481-
| Load -> failwith "Not Implemented"
494+
| Load l -> failwith "Not Implemented"
482495
| Store -> failwith "Not Implemented"
483496
| Alloc -> failwith "Not Implemented"
484497

@@ -500,7 +513,7 @@ type Func =
500513
+ match r.Value with
501514
| Some i -> $" {valueToString i}"
502515
| None -> ""
503-
| Switch s -> failwith "Not Implemented"
516+
| Indirect s -> failwith "Not Implemented"
504517
| Unreachable _ -> "Unreachable"
505518

506519
tw.WriteLine trans |> ignore

src/Optimize/Lower/Env.fs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ type Env() =
126126
| Branch b ->
127127
this.AddEdge id b.One
128128
this.AddEdge id b.Zero
129-
| Switch s ->
130-
for t, _ in s.Dest do
129+
| Indirect s ->
130+
for t in s.Dest do
131131
this.AddEdge id t
132-
133-
this.AddEdge id s.Default
134132
| Return _
135133
| Unreachable _ -> ()
136134

src/Optimize/Pass/ADCE.fs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let adceImpl (f: Func) =
3434
if not prev then
3535
match f.Block[id].Trans with
3636
| Branch { Value = value }
37-
| Switch { Value = value } -> markValue value
37+
| Indirect { Value = value } -> markValue value
3838
| _ -> ()
3939

4040
let control = cdg[id].Pred
@@ -44,21 +44,13 @@ let adceImpl (f: Func) =
4444
| Branch b ->
4545
markBlock control
4646
markValue b.Value
47-
| Switch s ->
48-
markBlock control
49-
markValue s.Value
5047
| _ -> ()
5148

5249
match f.Block[id].Trans with
5350
| Jump j -> markBlock j.Target
5451
| Branch b ->
5552
markBlock b.Zero
5653
markBlock b.One
57-
| Switch s ->
58-
markBlock s.Default
59-
60-
for target, _ in s.Dest do
61-
markBlock target
6254
| _ -> ()
6355

6456
for (blockIdx, block) in Array.indexed f.Block do
@@ -74,7 +66,7 @@ let adceImpl (f: Func) =
7466
for instr in block.Instr do
7567
match instr with
7668
| Call _
77-
| Load
69+
| Load _
7870
| Store
7971
| Alloc ->
8072
markBlock blockIdx

src/Optimize/Pass/DCE.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let dceImpl (phiOnly: bool) (f: Func) =
3636
| Unary _
3737
| Binary _ -> true
3838
| Call _
39-
| Load
39+
| Load _
4040
| Store
4141
| Alloc -> false
4242
else

src/Optimize/Pass/SCCP.fs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type VarValue(count: int) =
2323
member this.Eval instr =
2424
match instr with
2525
| Call _
26-
| Load
26+
| Load _
2727
| Alloc -> Top
2828
| Assign v -> this.ValueOf v.Value
2929
| Binary b ->
@@ -112,8 +112,8 @@ let calc (var: Var[]) (param: int[]) (block: Block[]) (cfg: GraphNode[]) =
112112

113113
if not prevBlock then
114114
blockList.Add b.One |> ignore
115-
| Switch s ->
116-
for targetBlock, _ in s.Dest do
115+
| Indirect s ->
116+
for targetBlock in s.Dest do
117117
let prevBlock = blockReachable[targetBlock]
118118
blockReachable[targetBlock] <- true
119119

@@ -137,9 +137,9 @@ let calc (var: Var[]) (param: int[]) (block: Block[]) (cfg: GraphNode[]) =
137137

138138
if not prevBlock then
139139
blockList.Add b.One |> ignore
140-
| Switch s ->
141-
for targetBlock, targetValue in s.Dest do
142-
if v = targetValue then
140+
| Indirect s ->
141+
for targetValue, targetBlock in Array.indexed s.Dest do
142+
if int v = targetValue then
143143
let prevBlock = blockReachable[targetBlock]
144144
blockReachable[targetBlock] <- true
145145

@@ -281,19 +281,19 @@ let calc (var: Var[]) (param: int[]) (block: Block[]) (cfg: GraphNode[]) =
281281
if not prevBlock then
282282
blockList.Add b.One |> ignore
283283
// TODO: switch default
284-
| Switch s ->
284+
| Indirect s ->
285285
match varValue.ValueOf s.Value with
286286
| Bottom -> ()
287287
| Top ->
288-
for targetBlock, _ in s.Dest do
288+
for targetBlock in s.Dest do
289289
let prevBlock = blockReachable[targetBlock]
290290
blockReachable[targetBlock] <- true
291291

292292
if not prevBlock then
293293
blockList.Add targetBlock |> ignore
294294
| Known v ->
295-
for targetBlock, targetValue in s.Dest do
296-
if v = targetValue then
295+
for targetValue, targetBlock in Array.indexed s.Dest do
296+
if int v = targetValue then
297297
let prevBlock = blockReachable[targetBlock]
298298
blockReachable[targetBlock] <- true
299299

src/Optimize/SSA.fs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ type SSA(f: Func) =
130130
{ c with
131131
Arg = Array.map reUse c.Arg
132132
Target = reDef c.Target }
133-
| Load -> failwith "Not Implemented"
133+
| Load l ->
134+
Load
135+
{ l with
136+
Base = reUse l.Base
137+
Offset = reUse l.Offset
138+
Target = reDef l.Target }
134139
| Store -> failwith "Not Implemented"
135140
| Alloc -> failwith "Not Implemented"
136141

@@ -180,10 +185,10 @@ type SSA(f: Func) =
180185
Some value
181186

182187
Return { r with Value = value }
183-
| Switch s ->
188+
| Indirect s ->
184189
let value = reUse s.Value
185190
useInTrans value
186-
Switch { s with Value = value }
191+
Indirect { s with Value = value }
187192
| Jump _
188193
| Unreachable _ -> block.Trans
189194

@@ -271,12 +276,10 @@ type SSA(f: Func) =
271276
{ b with Zero = newBlock }
272277

273278
Branch b
274-
| Switch s ->
275-
let map (target, value) = mapBlock target, value
279+
| Indirect s ->
280+
let newDest = Array.map mapBlock s.Dest
276281

277-
let newDest = Array.map map s.Dest
278-
279-
Switch { s with Dest = newDest }
282+
Indirect { s with Dest = newDest }
280283

281284
block[idx] <- { block[idx] with Trans = newTx }
282285

src/Optimize/Util.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ let removeVarAndBlock (f: Func) (varValue: Option<Value>[]) (blockMap: int[]) =
317317
else
318318
Jump { Target = one; Span = b.Span }
319319

320-
| Switch _ -> failwith "Not Implemented"
320+
| Indirect _ -> failwith "Not Implemented"
321321
| Return r -> Return r
322322
| Unreachable r -> Unreachable r
323323

test/Optimize/Spec.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let getAllFile path =
3030

3131
let arch = Common.Target.X86_64
3232

33-
let specFile = getAllFile "/Spec"
33+
let specFile = getAllFile "/../Spec"
3434
let spec = specFile.Keys |> Seq.map (Array.create 1)
3535

3636
let lowerSnap = Snapshot("raw.flir")
@@ -54,10 +54,10 @@ let cfgShouldMatch (f: Func) =
5454
Assert.Contains(b.One, node.Succ)
5555
Assert.Contains(idx, f.CFG[b.Zero].Pred)
5656
Assert.Contains(idx, f.CFG[b.One].Pred)
57-
| Switch s ->
57+
| Indirect s ->
5858
Assert.Equal(s.Dest.Length, node.Succ.Length)
5959

60-
for dest, _ in s.Dest do
60+
for dest in s.Dest do
6161
Assert.Contains(dest, node.Succ)
6262
Assert.Contains(idx, f.CFG[dest].Pred)
6363
| Return _

0 commit comments

Comments
 (0)