finish mir def

This commit is contained in:
sfja 2025-02-03 22:34:45 +01:00
parent 81d8e32a04
commit b70ba48e0a
2 changed files with 77 additions and 19 deletions

View File

@ -1,4 +1,11 @@
import { Span } from "../diagnostics.ts";
import { IdBase } from "../ids.ts";
export type Block = {
id: BlockId;
stmts: Stmt[];
terminator?: Terminator;
};
export type Stmt = {
kind: StmtKind;
@ -6,22 +13,37 @@ export type Stmt = {
export type StmtKind =
| { tag: "error" }
| { tag: "assign" } & AssignStmt
| { tag: "fake_read" } & FakeReadStmt
| { tag: "deinit" } & DeinitStmt
| { tag: "live" } & LiveStmt
| { tag: "dead" } & DeadStmt
| { tag: "mention" } & MentionStmt;
| { tag: "assign"; place: Place; rval: RVal }
| { tag: "fake_read"; place: Place }
| { tag: "deinit"; place: Place }
| { tag: "live"; local: LocalId }
| { tag: "dead"; local: LocalId }
| { tag: "mention"; place: Place };
export type AssignStmt = { place: Place; rval: RVal };
export type FakeReadStmt = { place: Place };
export type DeinitStmt = { place: Place };
export type LiveStmt = { local: Local };
export type DeadStmt = { local: Local };
export type MentionStmt = { place: Place };
export type Terminator = {
kind: TerminatorKind;
};
export type TerminatorKind =
| { tag: "goto"; target: BlockId }
| {
tag: "switch";
discr: Operand;
targets: SwitchTarget[];
otherwise: BlockId;
}
| { tag: "return" }
| { tag: "unreachable" }
| { tag: "drop"; place: Place; target: BlockId }
| { tag: "call"; func: Operand; args: Operand[]; dest: Operand };
export type SwitchTarget = {
value: number;
target: BlockId;
};
export type Place = {
local: Local;
local: LocalId;
proj: ProjElem[];
};
@ -30,11 +52,48 @@ export type ProjElem =
| { tag: "deref" }
| { tag: "repeat" }
| { tag: "field"; fieldIdx: number }
| { tag: "index": local: Local }
| { tag: }
| { tag: "index"; local: LocalId }
| { tag: "downcast"; variantIdx: number };
// https://doc.rust-lang.org/beta/nightly-rustc/rustc_middle/mir/enum.Rvalue.html
export type RVal = {};
export type RVal =
| { tag: "use"; operand: Operand }
| { tag: "repeat"; operand: Operand; length: Const }
| { tag: "ref"; place: Place; mut: boolean }
| { tag: "ptr"; place: Place; mut: boolean }
| { tag: "binary"; binaryType: BinaryType; left: Operand; right: Operand }
| { tag: "unary"; unaryType: UnaryType; operand: Operand };
export type Local = {};
export type BinaryType =
| "add"
| "sub"
| "mul"
| "div"
| "rem"
| "xor"
| "and"
| "or"
| "shl"
| "shr"
| "eq"
| "ne"
| "lt"
| "lte"
| "gt"
| "gte";
export type UnaryType = "not" | "neg";
export type Operand =
| { tag: "copy"; place: Place }
| { tag: "move"; place: Place }
| { tag: "const"; val: Const };
export type Const =
| { tag: "null" }
| { tag: "int"; value: number }
| { tag: "bool"; value: boolean }
| { tag: "string"; value: string };
export type LocalId = IdBase & { readonly _: unique symbol };
export type BlockId = IdBase & { readonly _: unique symbol };

View File

@ -1,6 +1,5 @@
import { Ctx } from "../ctx.ts";
import { Ctx, File } from "../ctx.ts";
import { Pos, Span } from "../diagnostics.ts";
import { File } from "../ids.ts";
import { ControlFlow, range } from "../util.ts";
import { Token, TokenIter } from "./token.ts";