A structured language and a cost-exact compiler #
Instr.effect P ins c: the effect of a straight-line instruction on the state, leaving the program counter unchanged (noneon a division by zero or on a control instruction).step_eq_effectrelates it tostep.Stmt: skip, straight-line instruction, sequence,if a < b,while a < b(on natural or on real registers).compile s base : Program,codeSize s, with(compile s base).length = codeSize s.Layout and exact costs (all costs count executed machine instructions):
skipemits no code and costs zero.instr iemits[i]and costs one.seq s temitscompile s base ++ compile t (base + codeSize s). Its size and its cost are the sums of the two constituent sizes and costs.ifLtN a b t eputs the branch atbase, compileseatbase + 1, and jumps pasttafterwards. The branch target and start oftarebase + 2 + codeSize e. The size iscodeSize t + codeSize e + 2. The true branch costsk_t + 1and the false branch costsk_e + 2. The real-register variant uses the same layout withbrLtR.whileLtN a b bodybranches tobase + 2on a true test and jumps to the exit otherwise. The body ends with a jump back tobase. The size iscodeSize body + 3. The exit costs two steps and each iteration costsk_body + 2. The real-register variant usesbrLtR.
A loop that iterates
mtimes with body costsk₁, …, kₘcosts exactly(k₁ + 2) + ⋯ + (kₘ + 2) + 2.Exec P W s c c' k: big-step cost semantics counting exactly the machine instructions the compiled code executes. Every configuration at which an instruction runs, plus the final one, is required to beWordsBounded W, anddivRis never executed with a zero divisor.Execnever changes the program counter (Exec.pc_eq) and is insensitive to it (Exec.setPc).Simulation (
Exec.runsTo): ifcompile s basesits atbaseinsideq, then runningqfor exactlyksteps fromcwithpc := baseends atc'withpc := base + codeSize s, every visited configurationWordsBounded W.Top level (
runsWithin_of_exec):Exec P W s initConfig c' kgivesRunsWithin (compile s 0 ++ [halt]) P W (k + 1) c'.output.Per-instruction rules
Exec.constN, …, and loop rules with a potential (Exec.whileLtN_potential,Exec.whileLtR_potential, and the_boundedvariants).
The effect of a straight-line instruction on the state; the program counter is left
unchanged. none for a division by zero and for control instructions.
Equations
- One or more equations did not get rendered due to their size.
- WordRAM.Instr.effect P (WordRAM.Instr.constN r k) x✝ = some { pc := x✝.pc, nreg := WordRAM.upd x✝.nreg r k, rreg := x✝.rreg, nmem := x✝.nmem, rmem := x✝.rmem, output := x✝.output }
- WordRAM.Instr.effect P (WordRAM.Instr.constR r z) x✝ = some { pc := x✝.pc, nreg := x✝.nreg, rreg := WordRAM.upd x✝.rreg r ↑z, nmem := x✝.nmem, rmem := x✝.rmem, output := x✝.output }
- WordRAM.Instr.effect P (WordRAM.Instr.outN a) x✝ = some { pc := x✝.pc, nreg := x✝.nreg, rreg := x✝.rreg, nmem := x✝.nmem, rmem := x✝.rmem, output := x✝.output ++ [x✝.nreg a] }
- WordRAM.Instr.effect P WordRAM.Instr.halt x✝ = none
- WordRAM.Instr.effect P (WordRAM.Instr.jump l) x✝ = none
- WordRAM.Instr.effect P (WordRAM.Instr.brLtN a b l) x✝ = none
- WordRAM.Instr.effect P (WordRAM.Instr.brLtR a b l) x✝ = none
Instances For
The effect is insensitive to the program counter.
The structured language #
Structured statements over straight-line instructions.
- skip : Stmt
No code.
- instr
(i : Instr)
: Stmt
One straight-line instruction (
Instr.IsStraightis required by the semantics). - seq
(s t : Stmt)
: Stmt
Sequencing.
- ifLtN
(a b : ℕ)
(thenS elseS : Stmt)
: Stmt
if nreg a < nreg b then thenS else elseS. - ifLtR
(a b : ℕ)
(thenS elseS : Stmt)
: Stmt
if rreg a < rreg b then thenS else elseS. - whileLtN
(a b : ℕ)
(body : Stmt)
: Stmt
while nreg a < nreg b do body. - whileLtR
(a b : ℕ)
(body : Stmt)
: Stmt
while rreg a < rreg b do body.
Instances For
Number of machine instructions in the compiled code.
Equations
- WordRAM.Machine.Stmt.skip.codeSize = 0
- (WordRAM.Machine.Stmt.instr i).codeSize = 1
- (s.seq t).codeSize = s.codeSize + t.codeSize
- (WordRAM.Machine.Stmt.ifLtN a b t e).codeSize = t.codeSize + e.codeSize + 2
- (WordRAM.Machine.Stmt.ifLtR a b t e).codeSize = t.codeSize + e.codeSize + 2
- (WordRAM.Machine.Stmt.whileLtN a b body).codeSize = body.codeSize + 3
- (WordRAM.Machine.Stmt.whileLtR a b body).codeSize = body.codeSize + 3
Instances For
Compile a statement to machine code placed at address base.
Equations
- One or more equations did not get rendered due to their size.
- WordRAM.Machine.Stmt.skip.compile x✝ = []
- (WordRAM.Machine.Stmt.instr i).compile x✝ = [i]
- (s.seq t).compile x✝ = s.compile x✝ ++ t.compile (x✝ + s.codeSize)
- (WordRAM.Machine.Stmt.whileLtN a b body).compile x✝ = WordRAM.Instr.brLtN a b (x✝ + 2) :: WordRAM.Instr.jump (x✝ + 3 + body.codeSize) :: body.compile (x✝ + 2) ++ [WordRAM.Instr.jump x✝]
- (WordRAM.Machine.Stmt.whileLtR a b body).compile x✝ = WordRAM.Instr.brLtR a b (x✝ + 2) :: WordRAM.Instr.jump (x✝ + 3 + body.codeSize) :: body.compile (x✝ + 2) ++ [WordRAM.Instr.jump x✝]
Instances For
The compiled code has exactly codeSize instructions.
Big-step cost semantics #
Exec P W s c c' k: from state c, the statement s runs to completion in state c'
executing exactly k machine instructions of its compiled code. Every configuration at
which an instruction runs, and the final configuration, is WordsBounded W; divR is never
executed with a zero divisor (the effect must be some). The program counter is carried
along unchanged.
- skip {P : Input} {W : ℕ} {c : Config} : WordsBounded W c → Exec P W Stmt.skip c c 0
- instr {P : Input} {W : ℕ} {ins : Instr} {c c' : Config} : ins.IsStraight → Instr.effect P ins c = some c' → WordsBounded W c → WordsBounded W c' → Exec P W (Stmt.instr ins) c c' 1
- seq {P : Input} {W : ℕ} {s t : Stmt} {c c₁ c₂ : Config} {k₁ k₂ : ℕ} : Exec P W s c c₁ k₁ → Exec P W t c₁ c₂ k₂ → Exec P W (s.seq t) c c₂ (k₁ + k₂)
- ifLtN_true {P : Input} {W a b : ℕ} {t e : Stmt} {c c' : Config} {k : ℕ} : c.nreg a < c.nreg b → Exec P W t c c' k → Exec P W (Stmt.ifLtN a b t e) c c' (k + 1)
- ifLtN_false {P : Input} {W a b : ℕ} {t e : Stmt} {c c' : Config} {k : ℕ} : ¬c.nreg a < c.nreg b → Exec P W e c c' k → Exec P W (Stmt.ifLtN a b t e) c c' (k + 2)
- ifLtR_true {P : Input} {W a b : ℕ} {t e : Stmt} {c c' : Config} {k : ℕ} : c.rreg a < c.rreg b → Exec P W t c c' k → Exec P W (Stmt.ifLtR a b t e) c c' (k + 1)
- ifLtR_false {P : Input} {W a b : ℕ} {t e : Stmt} {c c' : Config} {k : ℕ} : ¬c.rreg a < c.rreg b → Exec P W e c c' k → Exec P W (Stmt.ifLtR a b t e) c c' (k + 2)
- whileLtN_false {P : Input} {W a b : ℕ} {body : Stmt} {c : Config} : ¬c.nreg a < c.nreg b → WordsBounded W c → Exec P W (Stmt.whileLtN a b body) c c 2
- whileLtN_true {P : Input} {W a b : ℕ} {body : Stmt} {c c₁ c' : Config} {k₁ k₂ : ℕ} : c.nreg a < c.nreg b → Exec P W body c c₁ k₁ → Exec P W (Stmt.whileLtN a b body) c₁ c' k₂ → Exec P W (Stmt.whileLtN a b body) c c' (k₁ + 2 + k₂)
- whileLtR_false {P : Input} {W a b : ℕ} {body : Stmt} {c : Config} : ¬c.rreg a < c.rreg b → WordsBounded W c → Exec P W (Stmt.whileLtR a b body) c c 2
- whileLtR_true {P : Input} {W a b : ℕ} {body : Stmt} {c c₁ c' : Config} {k₁ k₂ : ℕ} : c.rreg a < c.rreg b → Exec P W body c c₁ k₁ → Exec P W (Stmt.whileLtR a b body) c₁ c' k₂ → Exec P W (Stmt.whileLtR a b body) c c' (k₁ + 2 + k₂)
Instances For
The starting configuration of an execution is word-bounded.
The final configuration of an execution is word-bounded.
Per-instruction rules #
Each rule gives the exact next state, cost 1, and the side conditions: the written natural
value must be ≤ W (for inputSize, constN, addN, mulN, readIn, rand), and divR
needs a nonzero divisor.
Blocks #
Prepend one instruction to a block: cost k + 1.
Loops with a potential #
Generic loop rule. test is the loop test, loop the loop statement, with the two
loop rules as hypotheses. Given an invariant I (implying the word bound) and a potential
Φ such that from any I-state with the test true one iteration of the body executes with
cost k, re-establishes I, and satisfies Φ c' + k + 2 ≤ Φ c, the whole loop executes from
any I-state with total cost ≤ Φ c + 2 and ends in an I-state with the test false.
Loop rule with a potential for whileLtN a b body: total cost ≤ Φ c + 2.
Loop rule with a potential for whileLtR a b body: total cost ≤ Φ c + 2.
Loop rule with a bounded body cost B and a strictly decreasing measure μ: total cost
≤ (B + 2) * μ c + 2.
whileLtN with a bounded body cost B and a strictly decreasing measure μ.
whileLtR with a bounded body cost B and a strictly decreasing measure μ.
Loop rule with a ghost state: the invariant I a and the potential Φ a may depend on a
value a : α that the configuration does not store (for example the set of vertices processed
so far); each iteration may change it. Total cost ≤ Φ a c + 2.
Simulation #
Two configurations differing only in an equal program counter are equal.
Simulation. If compile s base sits at base inside q, an execution
Exec P W s c c' k is realised by running q for exactly k steps from c with
pc := base, ending at c' with pc := base + codeSize s, every visited configuration
WordsBounded W.
Top level. An execution of s from the initial configuration gives a
RunsWithin for the program compile s 0 ++ [halt] with k + 1 steps.
Top level, with the output named.