Documentation

CslibFrontier.Computability.Program.Assemble

Assembling structured programs #

A structured program (Code) assembles into a flat list of instructions (Code.assemble) whose conditionals and loops are tests and jumps to absolute labels. A machine supplies a FlatMachine: its instructions, among them the data instructions, an unconditional jump and a jump on each test, and its step relation, with the four facts about these that assembly relies on. Every structured execution is then a run of the assembled program with exactly the same number of instructions (Exec.runs), so bounds proved with the rules of Program.Structured are bounds on the flat machine.

Main definitions #

Main results #

References #

structure Cslib.Program.FlatMachine (I : InstructionSet) :
Type (max (max (max (i + 1) u) v) w)

The flat instructions of a machine with instruction set I, and how it steps. A state is a label and the data. The four step facts say that the instructions named here behave as data instructions, jumps and tests; the machine may have other instructions, such as a halt.

Assemble a structured program to be placed at label pc.

@[simp]
theorem Cslib.Program.Code.length_assemble {I : InstructionSet} (F : FlatMachine I) (c : Code I.Op I.Test) (pc : ℕ) :
(assemble F c pc).length = c.size

Assembling c at any label gives c.size instructions.

def Cslib.Program.Contains {α : Type u_1} (p : List α) (base : ℕ) (q : List α) :

p contains q from label base.

theorem Cslib.Program.Contains.append_left {α : Type u_1} {p q₁ q₂ : List α} {base : ℕ} (h : Contains p base (q₁ ++ q₂)) :
Contains p base q₁

Retain the left part of the contained block.

theorem Cslib.Program.Contains.append_right {α : Type u_1} {p q₁ q₂ : List α} {base : ℕ} (h : Contains p base (q₁ ++ q₂)) :
Contains p (base + q₁.length) q₂

Retain the right part and advance the starting label.

theorem Cslib.Program.Contains.cons_tail {α : Type u_1} {p q : List α} {base : ℕ} {j : α} (h : Contains p base (j :: q)) :
Contains p (base + 1) q

Drop the first instruction of the contained block.

theorem Cslib.Program.Contains.head {α : Type u_1} {p q : List α} {base : ℕ} {j : α} (h : Contains p base (j :: q)) :
p[base]? = some j

Fetch the first instruction of the contained block.

theorem Cslib.Program.Exec.runs {I : InstructionSet} (F : FlatMachine I) {c : Code I.Op I.Test} {x y : I.Data} {n : ℕ} (h : Exec I c x y n) (pre post : List F.Instr) :

Assembly is correct. A structured execution is a run of the assembled program placed after any prefix and before any suffix, from its first instruction to the label after it, with exactly the same number of instructions.