Download Refinement and Theorem Proving*

Transcript
(case op
(add (ISA-add rc ra rb ISA))
;; REGS[rc] := REGS[ra] + REGS[rb]
(sub (ISA-sub rc ra rb ISA))
;; REGS[rc] := REGS[ra] - REGS[rb]
(mul (ISA-mul rc ra rb ISA))
;; REGS[rc] := REGS[ra] * REGS[rb]
(load (ISA-load rc ra ISA))
;; REGS[rc] := MEM[ra]
(loadi (ISA-loadi rc ra ISA))
;; REGS[rc] := MEM[REGS[ra]]
(store (ISA-store ra rb ISA))
;; MEM[REGS[ra]] := REGS[rb]
(bez (ISA-bez ra rb ISA))
;; REGS[ra]=0 -> pc:=pc+REGS[rb]
(jump (ISA-jump ra ISA))
;; pc:=REGS[ra]
(otherwise (ISA-default ISA))))))
The ISA-step function uses a book (library) for reasoning about records. One
of the functions exported by the records books is g (get), where (g f r) gets
the value of field f from record r. Therefore, ISA-step fetches the instruction
in the instruction memory (the field :imem of ISA state ISA) referenced by the
program counter (the field :pc of ISA state ISA) and stores this in inst, which
itself is a record consisting of fields :opcode, :rc, :ra, and :rb. Then, based
on the opcode, the appropriate action is taken. For example, in the case of an
add instruction, the next ISA state is (ISA-add rc ra rb ISA), where ISA-add
provides the semantics of add instructions. The definition of ISA-add is given
below
(defun ISA-add (rc ra rb ISA)
(seq-isa nil
:pc
(1+ (g :pc ISA))
:regs (add-rc ra rb rc (g :regs ISA))
:imem (g :imem ISA)
:dmem (g :dmem ISA)))
(defun add-rc (ra rb rc regs)
(seq regs rc (+ (g ra regs)
(g rb regs))))
The macro seq-isa is used to construct an ISA state whose four fields have
the given values. Notice that the program counter is incremented and the register
file is updated by setting the value of register rc to the sum of the values in
registers ra and rb. This happens in fuction add-rc, where seq is a macro that
given a record, r, and a collection of field/value pairs, updates the fields in r
with the given values and returns the result.