src/HOL/IMP/ASM.thy
author nipkow
Sun, 04 Nov 2012 18:38:18 +0100
changeset 50007 56f269baae76
parent 49191 3601bf546775
child 53015 a1119cf551e8
permissions -rw-r--r--
executable true liveness analysis incl an approximating version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45266
13b5fb92b9f5 tuned text
nipkow
parents: 45257
diff changeset
     1
header "Stack Machine and Compilation"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     2
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     3
theory ASM imports AExp begin
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     4
45266
13b5fb92b9f5 tuned text
nipkow
parents: 45257
diff changeset
     5
subsection "Stack Machine"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     6
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
     7
text_raw{*\snip{ASMinstrdef}{0}{1}{% *}
45257
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
     8
datatype instr = LOADI val | LOAD vname | ADD
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
     9
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    10
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    11
text_raw{*\snip{ASMstackdef}{1}{2}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    12
type_synonym stack = "val list"
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    13
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    14
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    15
abbreviation "hd2 xs == hd(tl xs)"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    16
abbreviation "tl2 xs == tl(tl xs)"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    17
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    18
text{* \noindent Abbreviations are transparent: they are unfolded after
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    19
parsing and folded back again before printing. Internally, they do not
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    20
exist.*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    21
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    22
text_raw{*\snip{ASMexeconedef}{0}{1}{% *}
45257
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    23
fun exec1 :: "instr \<Rightarrow> state \<Rightarrow> stack \<Rightarrow> stack" where
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    24
"exec1 (LOADI n) _ stk  =  n # stk" |
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    25
"exec1 (LOAD x) s stk  =  s(x) # stk" |
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    26
"exec1  ADD _ stk  =  (hd2 stk + hd stk) # tl2 stk"
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    27
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    28
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    29
text_raw{*\snip{ASMexecdef}{1}{2}{% *}
45257
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    30
fun exec :: "instr list \<Rightarrow> state \<Rightarrow> stack \<Rightarrow> stack" where
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    31
"exec [] _ stk = stk" |
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    32
"exec (i#is) s stk = exec is s (exec1 i s stk)"
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    33
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    34
50007
56f269baae76 executable true liveness analysis incl an approximating version
nipkow
parents: 49191
diff changeset
    35
value "exec [LOADI 5, LOAD ''y'', ADD] <''x'' := 42, ''y'' := 43> [50]"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    36
45257
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    37
lemma exec_append[simp]:
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    38
  "exec (is1@is2) s stk = exec is2 s (exec is1 s stk)"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44036
diff changeset
    39
apply(induction is1 arbitrary: stk)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    40
apply (auto)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    41
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    42
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    43
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    44
subsection "Compilation"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    45
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    46
text_raw{*\snip{ASMcompdef}{0}{2}{% *}
45257
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    47
fun comp :: "aexp \<Rightarrow> instr list" where
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    48
"comp (N n) = [LOADI n]" |
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    49
"comp (V x) = [LOAD x]" |
45275
7f6c2db48b71 tuned text
nipkow
parents: 45266
diff changeset
    50
"comp (Plus e\<^isub>1 e\<^isub>2) = comp e\<^isub>1 @ comp e\<^isub>2 @ [ADD]"
49191
3601bf546775 tuned latex
nipkow
parents: 45275
diff changeset
    51
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    52
45257
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    53
value "comp (Plus (Plus (V ''x'') (N 1)) (V ''z''))"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    54
45257
12063e071d92 renamed in ASM
nipkow
parents: 45222
diff changeset
    55
theorem exec_comp: "exec (comp a) s stk = aval a s # stk"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44036
diff changeset
    56
apply(induction a arbitrary: stk)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    57
apply (auto)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    58
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    59
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    60
end