Debian Codegraph @

src/Assembly/Syntax.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import NArith.
3 From Coq Require Import String.
4 From Coq Require Import List.
5 From Coq Require Import Derive.
6 Require Import Crypto.Util.Prod.
7 Require Import Crypto.Util.Option.
8 Require Import Crypto.Util.Bool.Reflect.
9 Require Import Crypto.Util.Listable.
10 Require Import Crypto.Util.ListUtil.
11 Require Crypto.Util.Tuple.
12 Require Crypto.Util.OptionList.
13 Import ListNotations.
14
15 Local Open Scope list_scope.
16
17 Local Set Implicit Arguments.
18 Local Set Primitive Projections.
19
20 Inductive REG :=
21 | rax | rcx | rdx | rbx | rsp | rbp | rsi | rdi | r8 | r9 | r10 | r11 | r12 | r13 | r14 | r15
22 | eax | ecx | edx | ebx | esp | ebp | esi | edi | r8d | r9d | r10d | r11d | r12d | r13d | r14d | r15d
23 | ax | cx | dx | bx | sp | bp | si | di | r8w | r9w | r10w | r11w | r12w | r13w | r14w | r15w
24 | ah | al | ch | cl | dh | dl | bh | bl | spl | bpl | sil | dil | r8b | r9b | r10b | r11b | r12b | r13b | r14b | r15b
25 .
26
27 Derive REG_Listable SuchThat (@FinitelyListable REG REG_Listable) As REG_FinitelyListable.
28 Proof. prove_ListableDerive. Qed.
29 Global Existing Instances REG_Listable REG_FinitelyListable.
30 Definition REG_beq : REG -> REG -> bool := eqb_of_listable.
31 Definition REG_dec_bl : forall x y, REG_beq x y = true -> x = y := eqb_of_listable_bl.
32 Definition REG_dec_lb : forall x y, x = y -> REG_beq x y = true := eqb_of_listable_lb.
33 Definition REG_eq_dec : forall x y : REG, {x = y} + {x <> y} := eq_dec_of_listable.
34
35 Definition CONST := Z.
36 Coercion CONST_of_Z (x : Z) : CONST := x.
37
38 Inductive AccessSize := byte | word | dword | qword.
39
40 Derive AccessSize_Listable SuchThat (@FinitelyListable AccessSize AccessSize_Listable) As AccessSize_FinitelyListable.
41 Proof. prove_ListableDerive. Qed.
42 Global Existing Instances AccessSize_Listable AccessSize_FinitelyListable.
43 Definition AccessSize_beq : AccessSize -> AccessSize -> bool := eqb_of_listable.
44 Definition AccessSize_dec_bl : forall x y, AccessSize_beq x y = true -> x = y := eqb_of_listable_bl.
45 Definition AccessSize_dec_lb : forall x y, x = y -> AccessSize_beq x y = true := eqb_of_listable_lb.
46 Definition AccessSize_eq_dec : forall x y : AccessSize, {x = y} + {x <> y} := eq_dec_of_listable.
47
48 Coercion bits_of_AccessSize (x : AccessSize) : N
49 := match x with
50 | byte => 8
51 | word => 16
52 | dword => 32
53 | qword => 64
54 end.
55
56 Local Set Boolean Equality Schemes.
57 Local Set Decidable Equality Schemes.
58 Variant rip_relative_kind := explicitly_rip_relative | implicitly_rip_relative | not_rip_relative.
59 Local Unset Boolean Equality Schemes.
60 Local Unset Decidable Equality Schemes.
61 Global Coercion bool_of_rip_relative_kind (x : rip_relative_kind) : bool :=
62 match x with
63 | explicitly_rip_relative => true
64 | implicitly_rip_relative => true
65 | not_rip_relative => false
66 end.
67 Record MEM := { mem_bits_access_size : option AccessSize ; mem_base_reg : option REG ; mem_scale_reg : option (Z * REG) ; mem_base_label : option string ; mem_offset : option Z ; rip_relative : rip_relative_kind }.
68
69 Definition mem_of_reg (r : REG) : MEM :=
70 {| mem_base_reg := Some r ; mem_offset := None ; mem_scale_reg := None ; mem_bits_access_size := None ; mem_base_label := None ; rip_relative := not_rip_relative |}.
71
72 Inductive FLAG := CF | PF | AF | ZF | SF | OF.
73
74 Derive FLAG_Listable SuchThat (@FinitelyListable FLAG FLAG_Listable) As FLAG_FinitelyListable.
75 Proof. prove_ListableDerive. Qed.
76 Global Existing Instances FLAG_Listable FLAG_FinitelyListable.
77 Definition FLAG_beq : FLAG -> FLAG -> bool := eqb_of_listable.
78 Definition FLAG_dec_bl : forall x y, FLAG_beq x y = true -> x = y := eqb_of_listable_bl.
79 Definition FLAG_dec_lb : forall x y, x = y -> FLAG_beq x y = true := eqb_of_listable_lb.
80 Definition FLAG_eq_dec : forall x y : FLAG, {x = y} + {x <> y} := eq_dec_of_listable.
81
82 Inductive OpPrefix :=
83 | rep
84 | repz
85 | repnz
86 .
87
88 Derive OpPrefix_Listable SuchThat (@FinitelyListable OpPrefix OpPrefix_Listable) As OpPrefix_FinitelyListable.
89 Proof. prove_ListableDerive. Qed.
90 Global Existing Instances OpPrefix_Listable OpPrefix_FinitelyListable.
91 Definition OpPrefix_beq : OpPrefix -> OpPrefix -> bool := eqb_of_listable.
92 Definition OpPrefix_dec_bl : forall x y, OpPrefix_beq x y = true -> x = y := eqb_of_listable_bl.
93 Definition OpPrefix_dec_lb : forall x y, x = y -> OpPrefix_beq x y = true := eqb_of_listable_lb.
94 Definition OpPrefix_eq_dec : forall x y : OpPrefix, {x = y} + {x <> y} := eq_dec_of_listable.
95
96 Inductive OpCode :=
97 | adc
98 | adcx
99 | add
100 | adox
101 | and
102 | bzhi
103 | call
104 | clc
105 | cmovb
106 | cmovc
107 | cmove (* Conditional move if equal *)
108 | cmovne (* Conditional move if not equal *)
109 | cmovnz
110 | cmovo
111 | cmp
112 | db
113 | dw
114 | dd
115 | dq
116 | dec
117 | imul
118 | inc
119 | je
120 | jmp
121 | lea
122 | leave (* Function epilogue instruction *)
123 | mov
124 | movabs (* Move absolute value into register *)
125 | movdqa (* Move aligned packed data *)
126 | movdqu (* Move unaligned packed data *)
127 | movq (* Move quadword *)
128 | movd (* Move doubleword *)
129 | movsx (* Move with sign extension *)
130 | movups (* Move unaligned packed single-precision floating-point values *)
131 | movzx
132 | mul
133 | mulx
134 | neg (* Two's complement negation *)
135 | nop (* No operation *)
136 | not (* Bitwise NOT *)
137 | or
138 | paddq (* Add packed quadword integers *)
139 | pop
140 | psubq (* Subtract packed quadword integers *)
141 | pshufd (* Shuffle packed doublewords *)
142 | pshufw (* Shuffle packed words *)
143 | punpcklqdq (* Unpack and interleave low quadwords *)
144 | punpckhqdq (* Unpack and interleave high quadwords *)
145 | pslld (* Shift packed single-precision floating-point values left *)
146 | psrld (* Shift packed single-precision floating-point values right *)
147 | pand (* Bitwise AND *)
148 | pandn (* Bitwise AND NOT *)
149 | por (* Bitwise OR *)
150 | pxor (* Bitwise XOR *)
151 | psrad (* Shift packed signed integers right arithmetic *)
152 | push
153 | rcr
154 | ret
155 | rol (* Rotate left *)
156 | ror (* Rotate right *)
157 | sal (* Shift arithmetic left (functionally equivalent to shl) *)
158 | sar
159 | sbb
160 | setc
161 | sete (* Set byte if equal *)
162 | setne (* Set byte if not equal *)
163 | seto
164 | shl
165 | shlx
166 | shld
167 | shr
168 | shrx
169 | shrd
170 | sub
171 | test
172 | xchg
173 | xor
174 .
175
176 Derive OpCode_Listable SuchThat (@FinitelyListable OpCode OpCode_Listable) As OpCode_FinitelyListable.
177 Proof. prove_ListableDerive. Qed.
178 Global Existing Instances OpCode_Listable OpCode_FinitelyListable.
179 Definition OpCode_beq : OpCode -> OpCode -> bool := eqb_of_listable.
180 Definition OpCode_dec_bl : forall x y, OpCode_beq x y = true -> x = y := eqb_of_listable_bl.
181 Definition OpCode_dec_lb : forall x y, x = y -> OpCode_beq x y = true := eqb_of_listable_lb.
182 Definition OpCode_eq_dec : forall x y : OpCode, {x = y} + {x <> y} := eq_dec_of_listable.
183
184 Definition accesssize_of_declaration (opc : OpCode) : option AccessSize :=
185 match opc with
186 | db => Some byte
187 | dd => Some dword
188 | dq => Some qword
189 | dw => Some word
190 | adc
191 | adcx
192 | add
193 | adox
194 | and
195 | bzhi
196 | call
197 | clc
198 | cmovb
199 | cmovc
200 | cmove
201 | cmovne
202 | cmovnz
203 | cmovo
204 | cmp
205 | dec
206 | imul
207 | inc
208 | je
209 | jmp
210 | lea
211 | leave
212 | mov
213 | movabs
214 | movdqa
215 | movdqu
216 | movq
217 | movd
218 | movsx
219 | movups
220 | movzx
221 | mul
222 | mulx
223 | neg
224 | nop
225 | not
226 | or
227 | paddq
228 | pop
229 | psubq
230 | pshufd
231 | pshufw
232 | punpcklqdq
233 | punpckhqdq
234 | pslld
235 | psrld
236 | pand
237 | pandn
238 | por
239 | pxor
240 | psrad
241 | push
242 | rcr
243 | ret
244 | rol
245 | ror
246 | sal
247 | sar
248 | sbb
249 | setc
250 | sete
251 | setne
252 | seto
253 | shl
254 | shlx
255 | shld
256 | shr
257 | shrx
258 | shrd
259 | sub
260 | test
261 | xchg
262 | xor
263 => None
264 end.
265
266 Record JUMP_LABEL := { jump_near : bool ; label_name : string }.
267
268 Inductive ARG := reg (r : REG) | mem (m : MEM) | const (c : CONST) | label (l : JUMP_LABEL).
269 Coercion reg : REG >-> ARG.
270 Coercion mem : MEM >-> ARG.
271 Coercion const : CONST >-> ARG.
272
273 Record NormalInstruction := { prefix : option OpPrefix ; op : OpCode ; args : list ARG }.
274
275 Inductive RawLine :=
276 | SECTION (name : string)
277 | GLOBAL (name : string)
278 | LABEL (name : string)
279 | ALIGN (amount : string)
280 | DEFAULT_REL
281 | EMPTY
282 | INSTR (instr : NormalInstruction)
283 | DIRECTIVE (d : string)
284 | ASCII_ (null_terminated : bool) (s : string)
285 .
286 Notation ASCII := (ASCII_ false).
287 Notation ASCIZ := (ASCII_ true).
288 Coercion INSTR : NormalInstruction >-> RawLine.
289 Record Line := { indent : string ; rawline :> RawLine ; pre_comment_whitespace : string ; comment : option string ; line_number : N}.
290 Definition Lines := list Line.
291
292 Definition reg_size (r : REG) : N :=
293 match r with
294 |( rax | rcx | rdx | rbx | rsp | rbp | rsi | rdi | r8 | r9 | r10 | r11 | r12 | r13 | r14 | r15 )
295 => 64
296 |( eax | ecx | edx | ebx | esp | ebp | esi | edi | r8d | r9d | r10d | r11d | r12d | r13d | r14d | r15d)
297 => 32
298 |( ax | cx | dx | bx | sp | bp | si | di | r8w | r9w | r10w | r11w | r12w | r13w | r14w | r15w)
299 => 16
300 |(ah | al | ch | cl | dh | dl | bh | bl | spl | bpl | sil | dil | r8b | r9b | r10b | r11b | r12b | r13b | r14b | r15b)
301 => 8
302 end.
303
304 Definition standalone_operand_size (x : ARG) : option N :=
305 match x with
306 | reg r => Some (reg_size r)
307 | mem m => option_map bits_of_AccessSize m.(mem_bits_access_size)
308 | const c => None
309 | label _ => None
310 end%N.
311
312 Definition opcode_size (op : OpCode) :=
313 match op with
314 | seto | setc => Some 8
315 | ret | nop => Some 64 (* irrelevant? *)
316 | clc => Some 1 (* irrelevant? *)
317 | _ => None
318 end%N.
319
320 Definition operation_size instr :=
321 match opcode_size instr.(op) with
322 | Some s => Some s | None =>
323 let argsizes := List.map standalone_operand_size instr.(args) in
324 match OptionList.Option.List.lift argsizes with
325 | Some szs => match szs with
326 | nil => None (* unspecified *)
327 | _ => Some (List.fold_right N.max 0%N szs) (* fully specified *)
328 end
329 | _ => match OptionList.Option.List.map id argsizes with
330 | nil => None (* unspecified *)
331 | szs =>
332 let m := List.fold_right N.max 0%N szs in
333 let n := List.fold_right N.min m szs in
334 if N.eqb m n (* uniquely inferred from annotations *)
335 then Some n
336 else None (* inference needed but ambiguous *)
337 end
338 end
339 end.
340
341 Definition operand_size (x : ARG) (operation_size : N) : N :=
342 match standalone_operand_size x with
343 | Some s => s
344 | None => operation_size
345 end.
346
347 Definition reg_offset (r : REG) : N :=
348 match r with
349 |(ah | ch | dh | bh )
350 => 8
351 | _ => 0
352 end.
353
354 Definition widest_register_of (r : REG) : REG :=
355 match r with
356 | ((al | ah) | ax | eax | rax) => rax
357 | ((cl | ch) | cx | ecx | rcx) => rcx
358 | ((dl | dh) | dx | edx | rdx) => rdx
359 | ((bl | bh) | bx | ebx | rbx) => rbx
360 | (spl | sp | esp | rsp) => rsp
361 | (bpl | bp | ebp | rbp) => rbp
362 | (sil | si | esi | rsi) => rsi
363 | (dil | di | edi | rdi) => rdi
364 | (r8b | r8w | r8d | r8) => r8
365 | (r9b | r9w | r9d | r9) => r9
366 | (r10b | r10w | r10d | r10) => r10
367 | (r11b | r11w | r11d | r11) => r11
368 | (r12b | r12w | r12d | r12) => r12
369 | (r13b | r13w | r13d | r13) => r13
370 | (r14b | r14w | r14d | r14) => r14
371 | (r15b | r15w | r15d | r15) => r15
372 end.
373
374 Definition widest_registers := Eval lazy in List.filter (fun x => REG_beq x (widest_register_of x)) (list_all REG).
375
376 Definition wide_reg_index_pairs := Eval lazy in List.map (fun '(n, r) => (N.of_nat n, r)) (List.enumerate widest_registers).
377
378 Definition eta_reg {A} : (REG -> A) -> (REG -> A).
379 Proof.
380 intros f r; pose (f r) as fr; destruct r.
381 all: let v := eval cbv in fr in exact v.
382 Defined.
383
384 Definition reg_index (r : REG) : N := Eval lazy in
385 eta_reg (fun r =>
386 Option.value
387 (option_map (@fst _ _) (find (fun '(n, r') => REG_beq (widest_register_of r) r') wide_reg_index_pairs))
388 0%N)
389 r.
390
391 Definition widest_register_of_index_opt (n : N) : option REG
392 := List.nth_error (List.map (@snd _ _) wide_reg_index_pairs) (N.to_nat n).
393
394 (** convenience printing function *)
395 Definition widest_register_of_index (n : N) : REG
396 := Option.value (widest_register_of_index_opt n) rax.
397
398 Definition widest_reg_size_of (r : REG) : N :=
399 reg_size (widest_register_of_index (reg_index r)).
400
401 Definition index_and_shift_and_bitcount_of_reg (r : REG) :=
402 (reg_index r, reg_offset r, reg_size r).
403
404 Definition overlapping_registers (r : REG) : list REG := Eval lazy in eta_reg
405 (fun r => List.filter (fun r' => REG_beq (widest_register_of r) (widest_register_of r')) (list_all REG))
406 r.
407
408 Definition reg_of_index_and_shift_and_bitcount_opt :=
409 fun '(index, offset, size) =>
410 (wr <- widest_register_of_index_opt index;
411 let rs := overlapping_registers wr in
412 List.find (fun r => ((reg_size r =? size) && (reg_offset r =? offset))%N%bool) rs)%option.
413
414 Definition reg_of_index_and_shift_and_bitcount :=
415 fun '(index, offset, size) =>
416 match reg_of_index_and_shift_and_bitcount_opt (index, offset, size) with
417 | Some r => r
418 | None => widest_register_of_index index
419 end.
420
421 Class assembly_program_options := {
422 default_rel : bool ;
423 }.