Skip to content

Commit 8af1a46

Browse files
committed
[notes] compilation with fall-through
1 parent 46a7852 commit 8af1a46

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

notes/compilation.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,45 @@ e.g. when assigning to a boolean variable:
406406

407407
Ldone:
408408
```
409+
410+
411+
Compiling booleans with fall-through
412+
------------------------------------
413+
414+
The `compileBool(e, Ltrue, Lfalse)` function does not utilize fall-through
415+
in many cases, and thus uses more labels than strictly needed.
416+
417+
An alternative is a `compileBool(e, Lfalse)` that produces code that would jump to `Lfalse`
418+
whenever `e` evaluates to `false`, and fall through otherwise.
419+
420+
```haskell
421+
compileBool(EFalse, l):
422+
goto l
423+
424+
compileBool(ETrue, l):
425+
-- emit nothing
426+
427+
compileBool(EAnd e₁ e₂, l):
428+
compileBool(e₁, l)
429+
compileBool(e₂, l)
430+
431+
-- if we had negation:
432+
compileBool(ENot e, l):
433+
lTrue newLabel
434+
compileBool(e, lTrue)
435+
goto l
436+
lTrue:
437+
438+
compileBool(EOr e₁ e₂, l):
439+
lFalse, lTrue <- newLabel
440+
compileBool(e₁, lFalse)
441+
goto lTrue
442+
lFalse:
443+
compileBool(e₂, l)
444+
lTrue:
445+
446+
compileBool(ELt int e₁ e₂, l):
447+
compileExp(e₁)
448+
compileExp(e₂)
449+
if_icmpge l
450+
```

0 commit comments

Comments
 (0)