This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathX86.fs
390 lines (332 loc) · 8.79 KB
/
X86.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
module X86
(* assembler syntax --------------------------------------------------------- *)
#nowarn "62"
type lbl = string
type quad = int64
type imm =
| Lit of quad
| Lbl of lbl
(* arguments: rdi, rsi, rdx, rcx, r09, r08
callee-save rbx, rbp, r12-r15 *)
type reg =
| Rip
| Rax
| Rbx
| Rcx
| Rdx
| Rsi
| Rdi
| Rbp
| Rsp
| R08
| R09
| R10
| R11
| R12
| R13
| R14
| R15
type operand =
| Imm of imm (* immediate *)
| Reg of reg (* register *)
| Ind1 of imm (* indirect: displacement *)
| Ind2 of reg (* indirect: (%reg) *)
| Ind3 of (imm * reg) (* indirect: displacement(%reg) *)
(* Condition Codes *)
type cnd =
| Eq
| Neq
| Gt
| Ge
| Lt
| Le
type opcode =
| Movq
| Pushq
| Popq
| Leaq
| Incq
| Decq
| Negq
| Notq
| Addq
| Subq
| Imulq
| Xorq
| Orq
| Andq
| Shlq
| Sarq
| Shrq
| Jmp
| J of cnd
| Cmpq
| Set of cnd
| Callq
| Retq
type ins = opcode * (operand list)
type data =
| Asciz of string
| Quad of imm
type asm =
| Text of ins list (* code *)
| Data of data list (* data *)
(* labeled blocks of data or code *)
type elem = { lbl: lbl; globals: bool; asm: asm }
type prog = elem list
(* pretty printing ----------------------------------------------------------- *)
let string_of_reg : reg -> string =
function
| Rip -> "%rip"
| Rax -> "%rax"
| Rbx -> "%rbx"
| Rcx -> "%rcx"
| Rdx -> "%rdx"
| Rsi -> "%rsi"
| Rdi -> "%rdi"
| Rbp -> "%rbp"
| Rsp -> "%rsp"
| R08 -> "%r8 "
| R09 -> "%r9 "
| R10 -> "%r10"
| R11 -> "%r11"
| R12 -> "%r12"
| R13 -> "%r13"
| R14 -> "%r14"
| R15 -> "%r15"
let string_of_byte_reg : reg -> string =
function
| Rip -> failwith "%rip used as byte register"
| Rax -> "%al"
| Rbx -> "%bl"
| Rcx -> "%cl"
| Rdx -> "%dl"
| Rsi -> "%sil"
| Rdi -> "%dil"
| Rbp -> "%bpl"
| Rsp -> "%spl"
| R08 -> "%r8b"
| R09 -> "%r9b"
| R10 -> "%r10b"
| R11 -> "%r11b"
| R12 -> "%r12b"
| R13 -> "%r13b"
| R14 -> "%r14b"
| R15 -> "%r15b"
let string_of_lbl (l: lbl) : string = l
let string_of_imm : imm -> string =
function
| Lit i -> string (int64 i)
| Lbl l -> string_of_lbl l
let string_of_operand : operand -> string =
function
| Imm i -> "$" ^ string_of_imm i
| Reg r -> string_of_reg r
| Ind1 i -> string_of_imm i
| Ind2 r -> "(" ^ string_of_reg r ^ ")"
| Ind3 (i, r) -> string_of_imm i ^ "(" ^ string_of_reg r ^ ")"
let string_of_byte_operand : operand -> string =
function
| Imm i -> "$" ^ string_of_imm i
| Reg r -> string_of_byte_reg r
| Ind1 i -> string_of_imm i
| Ind2 r -> "(" ^ string_of_reg r ^ ")"
| Ind3 (i, r) -> string_of_imm i ^ "(" ^ string_of_reg r ^ ")"
let string_of_jmp_operand : operand -> string =
function
| Imm i -> string_of_imm i
| Reg r -> "*" ^ string_of_reg r
| Ind1 i -> "*" ^ string_of_imm i
| Ind2 r -> "*" ^ "(" ^ string_of_reg r ^ ")"
| Ind3 (i, r) ->
"*"
^ string_of_imm i ^ "(" ^ string_of_reg r ^ ")"
let string_of_cnd : cnd -> string =
function
| Eq -> "e"
| Neq -> "ne"
| Gt -> "g"
| Ge -> "ge"
| Lt -> "l"
| Le -> "le"
let string_of_opcode : opcode -> string =
function
| Movq -> "movq"
| Pushq -> "pushq"
| Popq -> "popq"
| Leaq -> "leaq"
| Incq -> "incq"
| Decq -> "decq"
| Negq -> "negq"
| Notq -> "notq"
| Addq -> "addq"
| Subq -> "subq"
| Imulq -> "imulq"
| Xorq -> "xorq"
| Orq -> "orq"
| Andq -> "andq"
| Shlq -> "shlq"
| Sarq -> "sarq"
| Shrq -> "shrq"
| Jmp -> "jmp"
| J c -> "j" ^ string_of_cnd c
| Cmpq -> "cmpq"
| Set c -> "set" ^ string_of_cnd c
| Callq -> "callq"
| Retq -> "retq"
let map_concat s f l = String.concat s (List.map f l)
let string_of_shift op =
function
| [ Imm i; dst ] as args ->
" "
^ string_of_opcode op
^ " " ^ map_concat ", " string_of_operand args
| [ Reg Rcx; dst ] -> Printf.sprintf " %s %%cl, %s" (string_of_opcode op) (string_of_operand dst)
| args ->
failwith (
Printf.sprintf "shift instruction has invalid operands: %s\n" (map_concat ", " string_of_operand args)
)
let string_of_ins (op, args) : string =
match op with
| Shlq
| Sarq
| Shrq -> string_of_shift op args
| _ ->
let f =
match op with
| J _
| Jmp
| Callq -> string_of_jmp_operand
| Set _ -> string_of_byte_operand
| _ -> string_of_operand
" "
^ string_of_opcode op ^ " " ^ map_concat ", " f args
let escaped s =
System.Text.RegularExpressions.Regex.Escape s
let string_of_data : data -> string =
function
| Asciz s -> " .asciz " ^ "\"" ^ escaped s ^ "\""
// | Asciz s -> "\t.asciz\t" ^ "\"" ^ (String.escaped s) ^ "\""
| Quad i -> " .quad " ^ string_of_imm i
let string_of_asm : asm -> string =
function
| Text is -> "\t.text\n" ^ map_concat "\n" string_of_ins is
| Data ds -> "\t.data\n" ^ map_concat "\n" string_of_data ds
let string_of_elem
{ lbl = lbl
globals = globals
asm = asm }
: string =
let sec, body =
match asm with
| Text is -> "\t.text\n", map_concat "\n" string_of_ins is
| Data ds -> "\t.data\n", map_concat "\n" string_of_data ds
let glb =
if globals then
" .globl " ^ string_of_lbl lbl ^ "\n"
else
"" in
sec ^ glb ^ string_of_lbl lbl ^ ":\n" ^ body
let string_of_prog (p: prog) : string =
// String.concat "\n" <| List.map string_of_elem p
String.concat "\n" (List.map string_of_elem p)
(* examples ------------------------------------------------------------------ *)
(* This module defines some convenient notations that can help when
* writing x86 assembly AST by hand. *)
// open Platform
// let prefix label =
// if Platform.isLinux then
// label
// else
// "_" + label
module Asm =
let (!&) i =
Imm(Lit(int64 i)) (* int64 constants *)
let (!&&) l = Imm(Lbl l) (* label constants *)
let (!@) r = Reg r (* registers *)
(* helper functions for building blocks of data or code *)
let data l ds =
{ lbl = l
globals = true
asm = Data ds }
let text l is =
{ lbl = l
globals = false
asm = Text is }
let gtext l is =
// { lbl = prefix l; globals = true; asm = Text is }
{ lbl = l
globals = true
asm = Text is }
(* Example X86 assembly program (written as OCaml AST).
Note: OS X does not allow "absolute" addressing (i.e. using
Ind1 (Lbl "lbl_name") as a source operand of Movq).
So this program causes an error when assembled using gcc.
*)
open Asm
let p1 : prog =
[ text
"foo"
[ Xorq, [ !@Rax; !@Rax ]
Movq, [ !&100; !@Rax ]
Retq, [] ]
gtext
"_program"
[ Xorq, [ !@Rax; !@Rax ]
Movq, [ Ind1(Lbl "baz"); !@Rax ]
Retq, [] ]
data "baz" [ Quad(Lit 99L) ]
data "quux" [ Asciz "Hello, world!" ] ]
(* This example uses "rip-relative" addressing to load the
global value (99L) stored at label "baz". *)
let p2 : prog =
[ text
"foo"
[ Xorq, [ !@Rax; !@Rax ]
Movq, [ !&100; !@Rax ]
Retq, [] ]
gtext
"_program"
[ Xorq, [ !@Rax; !@Rax ]
Movq, [ Ind3(Lbl "baz", Rip); !@Rax ]
Retq, [] ]
data "baz" [ Quad(Lit 99L) ]
data "quux" [ Asciz "Hello, world!" ] ]
(* This x86 program computes [n] factorial via a loop.
Note that the argument [n] is a meta-level variable.
That means that p3 is really a "template" that, given
an OCaml integer [n] produces assembly code to
compute [n] factorial.
*)
let p3 (n: int) : prog =
[ gtext
"_program"
[ Movq, [ !&1; !@Rax ]
Movq, [ !&n; !@Rdi ] ]
text
"loop"
[ Cmpq, [ !&0; !@Rdi ]
J Eq, [ !&& "exit" ]
Imulq, [ !@Rdi; !@Rax ]
Decq, [ !@Rdi ]
Jmp, [ !&& "loop" ] ]
text "exit" [ Retq, [] ] ]
(* This x86 program computes [n] factorial via recursion.
As above, [n] is a meta-level argument.
*)
let p4 (n: int) : prog =
[ text
"fac"
[ Movq, [ !@Rsi; !@Rax ]
Cmpq, [ !&1; !@Rdi ]
J Eq, [ !&& "exit" ]
Imulq, [ !@Rdi; !@Rsi ]
Decq, [ !@Rdi ]
Callq, [ !&& "fac" ] ]
text "exit" [ Retq, [] ]
gtext
"_program"
[ Movq, [ !&n; !@Rdi ]
Movq, [ !&1; !@Rsi ]
Callq, [ !&& "fac" ]
Retq, [] ] ]