Skip to content

Commit

Permalink
add a higher-level IL to use as a target for source2il (#21)
Browse files Browse the repository at this point in the history
## Summary

Add the `L10` intermediate language, which is the first IL not
structured around basic blocks (i.e., continuations). The `pass10`
pass lowers it into the `L4` language.

## Details

### Motivation

Directly translating the source language into a language with explicit
basic blocks is hard. `L10` is intended to bridge the gap between the
source language and `L4`.

### Language

The name `L10` is chosen so that there's still space for more ILs. It
does away with the basic-block centered structure, instead providing
higher- level control-flow constructs such as `if`, `case`, and
`loop`.

A `try`/`except` like facility is currently missing, meaning that
exception handling (where `Raise` is used) cannot be expressed.

Some way to delimit lifetimes of locals is also missing. Locals are
currently treated as being alive for the whole duration, which can lead
to locals that have their address taken being kept alive longer than
necessary.

Apart from the new control-flow constructs, `L10` stays close to `L4`,
so that the lowering can focus on the splitting the procedure into
basic blocks.

---------

Co-authored-by: Saem Ghani <[email protected]>
  • Loading branch information
zerbina and saem authored Sep 5, 2024
1 parent a08f670 commit 47e1dda
Show file tree
Hide file tree
Showing 80 changed files with 2,117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ jobs:
# run the passtool for the highest-level language:
- name: "Check the grammar"
run: |
bin/passtool passes lang4
bin/passtool passes lang10
bin/passtool passes lang_source
47 changes: 47 additions & 0 deletions passes/lang10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## L10 Language

```grammar
.extends lang4
```

Continuations are gone, and locals are scoped to procedures:

```grammar
procdef -= (ProcDef <type_id> (Continuations <continuation>+))
procdef += (ProcDef <type_id> (Locals <type_id>*) <single_stmt>)
```

Control-flow statements appear in a normal statement context:

```grammar
single_stmt ::= (Stmts <stmt>+)
| <stmt>
choice -= (Choice <intVal> <goto>)
| (Choice <floatVal> <goto>)
| (Choice <intVal> <intVal> <goto>)
| (Choice <floatVal> <floatVal> <goto>)
choice += (Choice <intVal> <single_stmt>)
| (Choice <floatVal> <single_stmt>)
stmt += (Block <single_stmt>)
| (Loop <single_stmt>)
| (If <value> <single_stmt> <single_stmt>?)
| (Case <simple> <choice>+)
| (Raise <value>)
| (Unreachable)
```

*Future consideration:* If-then-else support could be removed, which would
reduce the IL's surface area and make translation slightly simpler.

`Break` is used to break out of an enclosing `Block`, `Loop`, `If`, or `Case`.
A depth value of '0' refers to the most enclosing block.

```grammar
stmt += (Break depth:<int>)
```

*Rationale:* allowing `Break` to target `If` and `Case` makes translation
simpler and removes the need for some `Block`s.
Loading

0 comments on commit 47e1dda

Please sign in to comment.