Skip to content

Commit f5a25c5

Browse files
authored
Merge pull request #720 from CakeML/flat-to-clos
Compile flaLang directly to closLang
2 parents 3d28c48 + d3a5540 commit f5a25c5

File tree

74 files changed

+10012
-13657
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+10012
-13657
lines changed

compiler/backend/Holmakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
INCLUDES = $(HOLDIR)/examples/machine-code/multiword\
22
$(CAKEMLDIR)/misc $(CAKEMLDIR)/semantics $(CAKEMLDIR)/semantics/proofs\
33
$(CAKEMLDIR)/basis/pure\
4+
pattern_matching\
45
../encoders/asm reg_alloc reachability
56

67
all: $(DEFAULT_TARGETS) README.md

compiler/backend/README.md

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,14 @@ replaces it with an alloc call with 0.
178178
[flat_elimScript.sml](flat_elimScript.sml):
179179
Implementation for flatLang dead-code elimination.
180180

181-
[flat_exh_matchScript.sml](flat_exh_matchScript.sml):
182-
This compiler phase ensures that all pattern matches are exhaustive.
181+
[flat_patternScript.sml](flat_patternScript.sml):
182+
Interface between flatLang and pattern compiler.
183183

184-
[flat_reorder_matchScript.sml](flat_reorder_matchScript.sml):
185-
This compiler phase reorders patterns in pattern matches to improve
186-
code quality.
187-
188-
[flat_to_patScript.sml](flat_to_patScript.sml):
189-
This phase performs pattern-match compilation.
184+
[flat_to_closScript.sml](flat_to_closScript.sml):
185+
Compilation from flatLang to closLang. This compiler phase converts
186+
explicit variable names of flatLang to de Bruijn indexing of
187+
closLang. It also makes all division-by-zero and out-of-bounds
188+
exceptions raised explicitly.
190189

191190
[flat_uncheck_ctorsScript.sml](flat_uncheck_ctorsScript.sml):
192191
This compiler phase replaces tuples with constructors (with tag 0).
@@ -220,17 +219,8 @@ compiler configuration.
220219
[mips](mips):
221220
This directory contains the mips-specific part of the compiler backend.
222221

223-
[patLangScript.sml](patLangScript.sml):
224-
The patLang intermediate language follows immediately after
225-
pattern-match compilation from flatLang. The patLang language
226-
differs from earlier languages in that it uses de Bruijn indices
227-
for variable names.
228-
229-
[pat_to_closScript.sml](pat_to_closScript.sml):
230-
The translation from patLang to closLang is very simple.
231-
Its main purpose is simplifying the semantics of some operations,
232-
for example to explicitly raise an exception for Div so the semantics
233-
in closLang can make more assumptions about the arguments.
222+
[pattern_matching](pattern_matching):
223+
The CakeML pattern matching expressions compiler
234224

235225
[presLangScript.sml](presLangScript.sml):
236226
Functions for converting various intermediate languages

compiler/backend/backendComputeLib.sml

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ val add_backend_compset = computeLib.extend_compset
2626
[computeLib.Tys
2727
[ (* ---- configurations ---- *)
2828
``:source_to_flat$config``
29+
,``:flat_pattern$config``
2930
,``:clos_to_bvl$config``
3031
,``:bvl_to_bvi$config``
3132
,``:data_to_word$config``
@@ -72,16 +73,29 @@ val add_backend_compset = computeLib.extend_compset
7273
[ (* ---- source_to_flat ---- *)
7374
flatLangTheory.bool_id_def
7475
,flatLangTheory.Bool_def
76+
,miscTheory.enumerate_def
7577
]
7678
,computeLib.Defs (theory_computes "source_to_flat")
7779
(* ---- flat_elim ---- *)
7880
,computeLib.Defs (theory_computes "flat_elim")
81+
,computeLib.Defs (theory_computes "flat_pattern")
82+
,computeLib.Defs (theory_computes "flatLang")
83+
,computeLib.Defs (theory_computes "pattern_semantics")
84+
,computeLib.Defs (theory_computes "pattern_comp")
7985
,computeLib.Defs (theory_computes "reachable_spt")
8086
,computeLib.Tys
8187
[``:flatLang$op``
8288
,``:flatLang$pat``
8389
,``:flatLang$exp``
8490
,``:flatLang$dec``
91+
,``:pattern_semantics$pat``
92+
,``:pattern_semantics$dTest``
93+
,``:pattern_semantics$dGuard``
94+
,``:pattern_semantics$dTree``
95+
,``:pattern_semantics$term``
96+
,``:pattern_common$position``
97+
,``:pattern_common$pmatchResult``
98+
,``:pattern_common$matchResult``
8599
,``:source_to_flat$environment``
86100
,``:source_to_flat$next_indices``
87101
,``:source_to_flat$config``
@@ -93,27 +107,15 @@ val add_backend_compset = computeLib.extend_compset
93107

94108
,computeLib.Defs (theory_computes "flat_uncheck_ctors")
95109

96-
,computeLib.Tys
97-
[ (* ---- patLang ---- *)
98-
``:patLang$exp``
99-
,``:patLang$op``
100-
]
110+
,computeLib.Defs (theory_computes "flat_to_clos")
101111

102-
(* ---- flat_to_pat ---- *)
103-
,computeLib.Defs
104-
[flat_to_patTheory.Bool_def
105-
,flat_to_patTheory.isBool_def
106-
,flat_to_patTheory.sIf_def
107-
,flat_to_patTheory.pure_op_op_eqn (* could put this in the compute set and avoid listing explicitly *)
108-
,flat_to_patTheory.pure_op_def
109-
,flat_to_patTheory.pure_def
110-
,flat_to_patTheory.ground_def
111-
,flat_to_patTheory.sLet_def
112-
,flat_to_patTheory.Let_Els_compute
113-
,flat_to_patTheory.compile_pat_def
114-
,flat_to_patTheory.compile_row_def
115-
,flat_to_patTheory.compile_exp_def
116-
,flat_to_patTheory.compile_def
112+
,computeLib.Tys
113+
[``:closLang$exp``
114+
,``:closLang$op``
115+
,``:clos_known$val_approx``
116+
,``:clos_known$globalOpt``
117+
,``:clos_known$inliningDecision``
118+
,``:clos_known$config``
117119
]
118120

119121
,computeLib.Tys
@@ -128,13 +130,6 @@ val add_backend_compset = computeLib.extend_compset
128130
,computeLib.Defs
129131
[closLangTheory.pure_def
130132
,closLangTheory.pure_op_def
131-
(* ---- pat_to_clos ---- *)
132-
,pat_to_closTheory.dest_WordToInt_def
133-
,pat_to_closTheory.CopyByteStr_def
134-
,pat_to_closTheory.CopyByteAw8_def
135-
,pat_to_closTheory.vector_tag_def
136-
,pat_to_closTheory.compile_def
137-
(*,pat_to_closTheory.pat_tag_shift_def*)
138133
(* ---- clos_mti ---- *)
139134
,clos_mtiTheory.intro_multi_def
140135
,clos_mtiTheory.collect_args_def

compiler/backend/backendScript.sml

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
open preamble
88
source_to_flatTheory
9-
flat_to_patTheory
10-
pat_to_closTheory
9+
flat_to_closTheory
1110
clos_to_bvlTheory
1211
bvl_to_bviTheory
1312
bvi_to_dataTheory
@@ -46,12 +45,9 @@ val compile_tap_def = Define`
4645
let td = tap_flat c.tap_conf p [] in
4746
let _ = empty_ffi (strlit "finished: source_to_flat") in
4847
let c = c with source_conf := c' in
49-
let p = flat_to_pat$compile p in
50-
let td = tap_pat c.tap_conf p td in
51-
let _ = empty_ffi (strlit "finished: flat_to_pat") in
52-
let p = MAP pat_to_clos$compile p in
48+
let p = flat_to_clos$compile_decs p in
5349
let td = tap_clos c.tap_conf p td in
54-
let _ = empty_ffi (strlit "finished: pat_to_clos") in
50+
let _ = empty_ffi (strlit "finished: flat_to_clos") in
5551
let (c',p) = clos_to_bvl$compile c.clos_conf p in
5652
let c = c with clos_conf := c' in
5753
let _ = empty_ffi (strlit "finished: clos_to_bvl") in
@@ -87,16 +83,10 @@ val to_flat_def = Define`
8783
let c = c with source_conf := c' in
8884
(c,p)`;
8985

90-
val to_pat_def = Define`
91-
to_pat c p =
92-
let (c,p) = to_flat c p in
93-
let p = flat_to_pat$compile p in
94-
(c,p)`;
95-
9686
val to_clos_def = Define`
9787
to_clos c p =
98-
let (c,p) = to_pat c p in
99-
let p = MAP pat_to_clos$compile p in
88+
let (c,p) = to_flat c p in
89+
let p = flat_to_clos$compile_decs p in
10090
(c,p)`;
10191

10292
val to_bvl_def = Define`
@@ -160,7 +150,6 @@ Proof
160150
to_bvi_def,
161151
to_bvl_def,
162152
to_clos_def,
163-
to_pat_def,
164153
to_flat_def] >>
165154
unabbrev_all_tac >>
166155
rpt (CHANGED_TAC (srw_tac[][] >> full_simp_tac(srw_ss())[] >> srw_tac[][] >> rev_full_simp_tac(srw_ss())[]))
@@ -215,15 +204,10 @@ val from_clos_def = Define`
215204
let c = c with clos_conf := c' in
216205
from_bvl c p`;
217206

218-
val from_pat_def = Define`
219-
from_pat c p =
220-
let p = MAP pat_to_clos$compile p in
221-
from_clos c p`;
222-
223207
val from_flat_def = Define`
224208
from_flat c p =
225-
let p = flat_to_pat$compile p in
226-
from_pat c p`;
209+
let p = flat_to_clos$compile_decs p in
210+
from_clos c p`;
227211

228212
val from_source_def = Define`
229213
from_source c p =
@@ -243,7 +227,6 @@ Proof
243227
from_bvi_def,
244228
from_bvl_def,
245229
from_clos_def,
246-
from_pat_def,
247230
from_flat_def] >>
248231
unabbrev_all_tac >>
249232
rpt (CHANGED_TAC (srw_tac[][] >> full_simp_tac(srw_ss())[] >> srw_tac[][] >> rev_full_simp_tac(srw_ss())[]))
@@ -307,7 +290,6 @@ Proof
307290
to_bvi_def,
308291
to_bvl_def,
309292
to_clos_def,
310-
to_pat_def,
311293
to_flat_def,to_livesets_def] >>
312294
fs[compile_def,compile_tap_def]>>
313295
pairarg_tac>>
@@ -352,7 +334,6 @@ Proof
352334
to_bvi_def,
353335
to_bvl_def,
354336
to_clos_def,
355-
to_pat_def,
356337
to_flat_def,to_livesets_def] >>
357338
unabbrev_all_tac>>fs[]>>
358339
rpt(rfs[]>>fs[])
@@ -370,7 +351,7 @@ Theorem to_data_change_config:
370351
bvl_conf := c1'.bvl_conf |>,
371352
prog')
372353
Proof
373-
rw[to_data_def,to_bvi_def,to_bvl_def,to_clos_def,to_pat_def,to_flat_def]
354+
rw[to_data_def,to_bvi_def,to_bvl_def,to_clos_def,to_flat_def]
374355
\\ rpt (pairarg_tac \\ fs[]) \\ rw[] \\ fs[] \\ rfs[] \\ rveq \\ fs[] \\ rfs[] \\ rveq \\ fs[]
375356
\\ simp[config_component_equality]
376357
QED

compiler/backend/bvl_to_bviScript.sml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ val ConcatByte_location_eq = save_thm("ConcatByte_location_eq",
120120
val AllocGlobal_code_def = Define`
121121
AllocGlobal_code = (0:num,
122122
Let [Op GlobalsPtr []]
123-
(Let [Op Deref [Op (Const 0) []; Var 0]]
123+
(Let [Op El [Op (Const 0) []; Var 0]]
124124
(Let [Op Update [Op Add [Var 0; Op(Const 1)[]]; Op (Const 0) []; Var 1]]
125125
(Let [Op Length [Var 2]]
126126
(If (Op Less [Var 0; Var 2]) (Var 1)
@@ -130,7 +130,7 @@ val AllocGlobal_code_def = Define`
130130

131131
val CopyGlobals_code_def = Define`
132132
CopyGlobals_code = (3:num, (* ptr to new array, ptr to old array, index to copy *)
133-
Let [Op Update [Op Deref [Var 2; Var 1]; Var 2; Var 0]]
133+
Let [Op Update [Op El [Var 2; Var 1]; Var 2; Var 0]]
134134
(If (Op Equal [Op(Const 0)[]; Var 3]) (Var 0)
135135
(Call 0 (SOME CopyGlobals_location) [Var 1; Var 2; Op Sub [Op(Const 1)[];Var 3]] NONE)))`;
136136

@@ -209,7 +209,7 @@ local val compile_op_quotation = `
209209
dtcase op of
210210
| Const i => (dtcase c1 of [] => compile_int i
211211
| _ => Let [Op (Const 0) c1] (compile_int i))
212-
| Global n => Op Deref (c1++[compile_int(&(n+1)); Op GlobalsPtr []])
212+
| Global n => Op El (c1++[compile_int(&(n+1)); Op GlobalsPtr []])
213213
| SetGlobal n => Op Update (c1++[compile_int(&(n+1)); Op GlobalsPtr []])
214214
| AllocGlobal =>
215215
(dtcase c1 of [] => Call 0 (SOME AllocGlobal_location) [] NONE

compiler/backend/closLangScript.sml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ val _ = Datatype `
2525
indicating the first element, and how many, should be
2626
copied into the end of the new block. The fourth
2727
argument is the total size of the new block. *)
28-
| El (* read Block field index *)
28+
| El (* read Block field index or loads a value from a reference *)
2929
| LengthBlock (* get length of Block *)
3030
| Length (* get length of reference *)
3131
| LengthByte (* get length of byte array *)
@@ -43,9 +43,9 @@ val _ = Datatype `
4343
| LengthByteVec (* get length of ByteVector *)
4444
| DerefByteVec (* load a byte from a ByteVector *)
4545
| TagLenEq num num (* check Block's tag and length *)
46+
| LenEq num (* check Block's length *)
4647
| TagEq num (* check Block's tag *)
4748
| Ref (* makes a reference *)
48-
| Deref (* loads a value from a reference *)
4949
| Update (* updates a reference *)
5050
| Label num (* constructs a CodePtr *)
5151
| FFI string (* calls the FFI *)

compiler/backend/clos_to_bvlScript.sml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ val code_for_recc_case_def = Define `
126126
code_for_recc_case n num_args (c:bvl$exp) =
127127
(num_args + 1,
128128
Let [mk_el (Var num_args) (mk_const 2)]
129-
(Let (GENLIST (\a. Var (a + 1)) num_args ++ GENLIST (\i. Op Deref [mk_const i; Var 0]) n) c))`;
129+
(Let (GENLIST (\a. Var (a + 1)) num_args ++ GENLIST (\i. Op El [mk_const i; Var 0]) n) c))`;
130130

131131
val build_aux_def = Define `
132132
(build_aux i [] aux = (i:num,aux)) /\

0 commit comments

Comments
 (0)