compile optimization
This commit is contained in:
@@ -10,12 +10,11 @@ Mcode is a JSON-based intermediate representation that can be interpreted direct
|
||||
## Pipeline
|
||||
|
||||
```
|
||||
Source → Tokenize → Parse (AST) → Mcode (JSON) → Interpret
|
||||
→ Compile to Mach (planned)
|
||||
→ Compile to native (planned)
|
||||
Source → Tokenize → Parse (AST) → Fold → Mcode (JSON) → Streamline → Interpret
|
||||
→ QBE → Native
|
||||
```
|
||||
|
||||
Mcode is produced by the `JS_Mcode` compiler pass, which emits a cJSON tree. The mcode interpreter walks this tree directly, dispatching on instruction name strings.
|
||||
Mcode is produced by `mcode.cm`, which lowers the folded AST to JSON instruction arrays. The streamline optimizer (`streamline.cm`) then eliminates redundant operations. The result can be interpreted by `mcode.c`, or lowered to QBE IL by `qbe_emit.cm` for native compilation. See [Compilation Pipeline](pipeline.md) for the full overview.
|
||||
|
||||
## JSMCode Structure
|
||||
|
||||
@@ -44,16 +43,37 @@ struct JSMCode {
|
||||
|
||||
## Instruction Format
|
||||
|
||||
Each instruction is a JSON array. The first element is the instruction name (string), followed by operands:
|
||||
Each instruction is a JSON array. The first element is the instruction name (string), followed by operands (typically `[op, dest, ...args, line, col]`):
|
||||
|
||||
```json
|
||||
["LOADK", 0, 42]
|
||||
["ADD", 2, 0, 1]
|
||||
["JMPFALSE", 3, "else_label"]
|
||||
["CALL", 0, 2, 1]
|
||||
["access", 3, 5, 1, 9]
|
||||
["load_index", 10, 4, 9, 5, 11]
|
||||
["store_dynamic", 4, 11, 12, 6, 3]
|
||||
["frame", 15, 14, 1, 7, 7]
|
||||
["setarg", 15, 0, 16, 7, 7]
|
||||
["invoke", 15, 13, 7, 7]
|
||||
```
|
||||
|
||||
The instruction set mirrors the Mach VM opcodes — same operations, same register semantics, but with string dispatch instead of numeric opcodes.
|
||||
### Typed Load/Store
|
||||
|
||||
Memory operations come in typed variants for optimization:
|
||||
|
||||
- `load_index dest, obj, idx` — array element by integer index
|
||||
- `load_field dest, obj, key` — record property by string key
|
||||
- `load_dynamic dest, obj, key` — unknown; dispatches at runtime
|
||||
- `store_index obj, val, idx` — array element store
|
||||
- `store_field obj, val, key` — record property store
|
||||
- `store_dynamic obj, val, key` — unknown; dispatches at runtime
|
||||
|
||||
The compiler selects the appropriate variant based on `type_tag` and `access_kind` annotations from parse and fold.
|
||||
|
||||
### Decomposed Calls
|
||||
|
||||
Function calls are split into separate instructions:
|
||||
|
||||
- `frame dest, fn, argc` — allocate call frame
|
||||
- `setarg frame, idx, val` — set argument
|
||||
- `invoke frame, result` — execute the call
|
||||
|
||||
## Labels
|
||||
|
||||
|
||||
Reference in New Issue
Block a user