-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComp.fs
510 lines (411 loc) · 17.3 KB
/
Comp.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
(* File MicroC/Comp.fs
A compiler from micro-C, a sublanguage of the C language, to an
abstract machine. Direct (forwards) compilation without
optimization of jumps to jumps, tail-calls etc.
sestoft@itu.dk * 2009-09-23, 2011-11-10
A value is an integer; it may represent an integer or a pointer,
where a pointer is just an address in the store (of a variable or
pointer or the base address of an array).
The compile-time environment maps a global variable to a fixed
store address, and maps a local variable to an offset into the
current stack frame, relative to its bottom. The run-time store
maps a location to an integer. This freely permits pointer
arithmetics, as in real C. A compile-time function environment
maps a function name to a code label. In the generated code,
labels are replaced by absolute code addresses.
Expressions can have side effects. A function takes a list of
typed arguments and may optionally return a result.
Arrays can be one-dimensional and constant-size only. For
simplicity, we represent an array as a variable which holds the
address of the first array element. This is consistent with the
way array-type parameters are handled in C, but not with the way
array-type variables are handled. Actually, this was how B (the
predecessor of C) represented array variables.
The store behaves as a stack, so all data except global variables
are stack allocated: variables, function parameters and arrays.
*)
module Comp
open System.IO
open Absyn
open Machine
open Debug
open Backend
(* ------------------------------------------------------------------- *)
(* Simple environment operations *)
type 'data Env = (string * 'data) list
let rec lookup env x =
match env with
| [] -> failwith (x + " not found")
| (y, v) :: yr -> if x = y then v else lookup yr x
(* A global variable has an absolute address, a local one has an offset: *)
type Var =
| Glovar of int (* absolute address in stack *)
| Locvar of int (* address relative to bottom of frame *)
(* The variable environment keeps track of global and local variables, and
keeps track of next available offset for local variables *)
type VarEnv = (Var * typ) Env * int
(* The function environment maps function name to label and parameter decs *)
type Paramdecs = (typ * string) list
type FunEnv = (label * typ option * Paramdecs) Env
let isX86Instr = ref false
(* Bind declared variable in env and generate code to allocate it: *)
// kind : Glovar / Locvar
let rec allocateWithMsg (kind: int -> Var) (typ, x) (varEnv: VarEnv) =
let varEnv, instrs =
allocate (kind: int -> Var) (typ, x) (varEnv: VarEnv)
msg
<| "\nalloc\n"
+ sprintf "%A\n" varEnv
+ sprintf "%A\n" instrs
(varEnv, instrs)
and allocate (kind: int -> Var) (typ, x) (varEnv: VarEnv) : VarEnv * instr list =
msg $"allocate called!{(x, typ)}"
let (env, newloc) = varEnv
match typ with
| TypA (TypA _, _) -> raise (Failure "allocate: array of arrays not permitted")
| TypA (t, Some i) ->
let newEnv =
((x, (kind (newloc + i), typ)) :: env, newloc + i + 1) //数组内容占用 i个位置,数组变量占用1个位置
let code = [ INCSP i; GETSP; OFFSET(i - 1); SUB ]
// info (fun () -> printf "new varEnv: %A\n" newEnv)
(newEnv, code)
| _ ->
let newEnv =
((x, (kind (newloc), typ)) :: env, newloc + 1)
let code = [ INCSP 1 ]
// info (fun () -> printf "new varEnv: %A\n" newEnv) // 调试 显示分配后环境变化
(newEnv, code)
(* Bind declared parameters in env: *)
let bindParam (env, newloc) (typ, x) : VarEnv =
((x, (Locvar newloc, typ)) :: env, newloc + 1)
let bindParams paras ((env, newloc): VarEnv) : VarEnv = List.fold bindParam (env, newloc) paras
(* ------------------------------------------------------------------- *)
(* Build environments for global variables and functions *)
let makeGlobalEnvs (topdecs: topdec list) : VarEnv * FunEnv * instr list =
let rec addv decs varEnv funEnv =
msg $"\nGlobal funEnv:\n{funEnv}\n"
match decs with
| [] -> (varEnv, funEnv, [])
| dec :: decr ->
match dec with
| Vardec (typ, var) ->
let (varEnv1, code1) = allocateWithMsg Glovar (typ, var) varEnv
let (varEnvr, funEnvr, coder) = addv decr varEnv1 funEnv
(varEnvr, funEnvr, code1 @ coder)
| VardecAndAssign (typ, x, e) -> //= 返回环境
let (varEnv1, code1) = allocate Glovar (typ, x) varEnv
let (varEnvr, funEnvr, coder) = addv decr varEnv1 funEnv
(varEnvr, funEnvr, code1 @ coder)
| Fundec (tyOpt, f, xs, body) -> addv decr varEnv ((f, ($"{newLabel ()}_{f}", tyOpt, xs)) :: funEnv)
addv topdecs ([], 0) []
(*
生成 x86 代码,局部地址偏移 *8 ,因为 x86栈上 8个字节表示一个 堆栈的 slot槽位
栈式虚拟机 无须考虑,每个栈位保存一个变量
*)
let x86patch code =
if !isX86Instr then
code @ [ CSTI -8; MUL ] // x86 偏移地址*8
else
code
(* ------------------------------------------------------------------- *)
(* Compiling micro-C statements:
* stmt is the statement to compile
* varenv is the local and global variable environment
* funEnv is the global function environment
*)
let rec cStmt stmt (varEnv: VarEnv) (funEnv: FunEnv) : instr list =
match stmt with
| If (e, stmt1, stmt2) ->
let labelse = newLabel ()
let labend = newLabel ()
cExpr e varEnv funEnv
@ [ IFZERO labelse ]
@ cStmt stmt1 varEnv funEnv
@ [ GOTO labend ]
@ [ Label labelse ]
@ cStmt stmt2 varEnv funEnv
@ [ Label labend ]
| While (e, body) ->
let labbegin = newLabel ()
let labtest = newLabel ()
[ GOTO labtest; Label labbegin ]
@ cStmt body varEnv funEnv
@ [ Label labtest ]
@ cExpr e varEnv funEnv @ [ IFNZRO labbegin ]
| For(e1, e2, e3, body) ->
let labbegin = newLabel()
let labtest = newLabel()
cExpr e1 varEnv funEnv @ [INCSP -1]
@ [GOTO labtest; Label labbegin]
@ cStmt body varEnv funEnv
@ cExpr e3 varEnv funEnv @ [INCSP -1]
@ [Label labtest]
@ cExpr e2 varEnv funEnv
@ [IFNZRO labbegin]
| DoWhile (body, e) ->
let labbegin = newLabel ()
let labtest = newLabel ()
cStmt body varEnv funEnv
@[ GOTO labtest]
@[Label labbegin ]
@ cStmt body varEnv funEnv
@ [ Label labtest ]
@ cExpr e varEnv funEnv
@ [ IFNZRO labbegin ]
| DoUntil (body, e) ->
let labbegin = newLabel ()
let labtest = newLabel ()
cStmt body varEnv funEnv
@[ GOTO labtest]
@[Label labbegin ]
@ cStmt body varEnv funEnv
@ [ Label labtest ]
@ cExpr e varEnv funEnv
@ [ IFZERO labbegin ]
| Switch (e, cases) ->
let rec searchcases c =
match c with
| Case (e, body) :: tail ->
let labend = newLabel ()
let labfin = newLabel ()
[DUP]
@ cExpr e varEnv funEnv
@ [EQ]
@ [ IFZERO labend ]
@ cStmt body varEnv funEnv
@ [ GOTO labfin ]
@ [ Label labend ]
@ searchcases tail
@ [ Label labfin ]
| Default body :: [] ->
cStmt body varEnv funEnv
| [] -> []
cExpr e varEnv funEnv
@ searchcases cases
@[INCSP -1]
| Expr e -> cExpr e varEnv funEnv @ [ INCSP -1 ]
| Block stmts ->
let rec loop stmts varEnv =
match stmts with
| [] -> (snd varEnv, [])
| s1 :: sr ->
let (varEnv1, code1) = cStmtOrDec s1 varEnv funEnv
let (fdepthr, coder) = loop sr varEnv1
(fdepthr, code1 @ coder)
let (fdepthend, code) = loop stmts varEnv
code @ [ INCSP(snd varEnv - fdepthend) ]
| Return None -> [ RET(snd varEnv - 1) ]
| Return (Some e) -> cExpr e varEnv funEnv @ [ RET(snd varEnv) ]
and cStmtOrDec stmtOrDec (varEnv: VarEnv) (funEnv: FunEnv) : VarEnv * instr list =
match stmtOrDec with
| Stmt stmt -> (varEnv, cStmt stmt varEnv funEnv)
| Dec (typ, x) -> allocateWithMsg Locvar (typ, x) varEnv
| DecAndAssign (typ, x, e) -> //=给x赋值e
let (varEnv, code) = allocate Locvar (typ, x) varEnv
(varEnv,
code
@ (cExpr (Assign((AccVar x), e)) varEnv funEnv)
@ [ INCSP -1 ])//赋完值缩减栈
(* Compiling micro-C expressions:
* e is the expression to compile
* varEnv is the local and gloval variable environment
* funEnv is the global function environment
Net effect principle: if the compilation (cExpr e varEnv funEnv) of
expression e returns the instruction sequence instrs, then the
execution of instrs will leave the rvalue of expression e on the
stack top (and thus extend the current stack frame with one element).
*)
and cExpr (e: expr) (varEnv: VarEnv) (funEnv: FunEnv) : instr list =
match e with
| Access acc -> cAccess acc varEnv funEnv @ [ LDI ]
| Assign (acc, e) ->
cAccess acc varEnv funEnv
@ cExpr e varEnv funEnv @ [ STI ]
| CstI i -> [ CSTI i ]
| CstF i -> [ CSTF(System.BitConverter.ToInt32((System.BitConverter.GetBytes(float32 (i))), 0)) ] //=
| CstD i -> //=
[ CSTD(
System.BitConverter.ToInt32((System.BitConverter.GetBytes(i)), 4),
System.BitConverter.ToInt32((System.BitConverter.GetBytes(i)), 0)
) ]
| CstL i -> //=
[ CSTL(
System.BitConverter.ToInt32((System.BitConverter.GetBytes(i)), 4),
System.BitConverter.ToInt32((System.BitConverter.GetBytes(i)), 0)
) ]
| CstC i -> [ CSTC((int32) (System.BitConverter.ToInt16((System.BitConverter.GetBytes(char (i))), 0))) ] //=
| Addr acc -> cAccess acc varEnv funEnv
| Prim1 (ope, e1) ->
cExpr e1 varEnv funEnv
@ (match ope with
| "!" -> [ NOT ]
| "printi" -> [ PRINTI ]
| "printc" -> [ PRINTC ]
| _ -> raise (Failure "unknown primitive 1"))
| Prim2 (ope, e1, e2) ->
cExpr e1 varEnv funEnv
@ cExpr e2 varEnv funEnv
@ (match ope with
| "*" -> [ MUL ]
| "+" -> [ ADD ]
| "-" -> [ SUB ]
| "/" -> [ DIV ]
| "%" -> [ MOD ]
| "==" -> [ EQ ]
| "!=" -> [ EQ; NOT ]
| "<" -> [ LT ]
| ">=" -> [ LT; NOT ]
| ">" -> [ SWAP; LT ]
| "<=" -> [ SWAP; LT; NOT ]
| _ -> raise (Failure "unknown primitive 2"))
| Prim3 (e, e1, e2) ->
let labelse = newLabel ()
let labend = newLabel ()
cExpr e varEnv funEnv
@ [ IFZERO labelse ]
@ cExpr e1 varEnv funEnv
@ [ GOTO labend ]
@ [ Label labelse ]
@ cExpr e2 varEnv funEnv
@ [ Label labend ]
| Prim4 (ope, e1) ->
(match ope with
| "I++" -> //先找到变量地址,复制一份后取值,将原值与地址交换(保存一下原值),之后同++i,最后赋值后清除计算结果省空间
cAccess e1 varEnv funEnv
@[ DUP;LDI;SWAP;DUP;LDI;CSTI 1; ADD;STI;INCSP -1 ]
| "I--" -> //类同i++
cAccess e1 varEnv funEnv
@ [ DUP;LDI;SWAP;DUP;LDI;CSTI -1; ADD;STI;INCSP -1 ]
| "++I" -> //找到变量地址后dup一份,取值,加常量1,add操作,将计算结果赋值
cAccess e1 varEnv funEnv
@[ DUP;LDI;CSTI 1; ADD;STI ]
| "--I" -> //类同i--
cAccess (e1) varEnv funEnv
@ [ DUP;LDI;CSTI -1; ADD;STI ]
| _ -> raise (Failure "unknown primitive 4"))
| AssignPrim (ope, e1, e2) ->
cAccess e1 varEnv funEnv
@[DUP;LDI]
@ cExpr e2 varEnv funEnv
@ (match ope with
| "+=" -> [ ADD;STI ]
| "-=" -> [ SUB;STI ]
| "*=" -> [ MUL;STI ]
| "/=" -> [ DIV;STI ]
| _ -> raise (Failure "unknown AssignPrim"))
| Andalso (e1, e2) ->
let labend = newLabel ()
let labfalse = newLabel ()
cExpr e1 varEnv funEnv
@ [ IFZERO labfalse ]
@ cExpr e2 varEnv funEnv
@ [ GOTO labend
Label labfalse
CSTI 0
Label labend ]
| Orelse (e1, e2) ->
let labend = newLabel ()
let labtrue = newLabel ()
cExpr e1 varEnv funEnv
@ [ IFNZRO labtrue ]
@ cExpr e2 varEnv funEnv
@ [ GOTO labend
Label labtrue
CSTI 1
Label labend ]
| Call (f, es) -> callfun f es varEnv funEnv
(* Generate code to access variable, dereference pointer or index array.
The effect of the compiled code is to leave an lvalue on the stack. *)
and cAccess access varEnv funEnv : instr list =
match access with
| AccVar x ->
match lookup (fst varEnv) x with
// x86 虚拟机指令 需要知道是全局变量 [GVAR addr]
// 栈式虚拟机Stack VM 的全局变量的地址是 栈上的偏移 用 [CSTI addr] 表示
| Glovar addr, _ ->
if !isX86Instr then
[ GVAR addr ]
else
[ CSTI addr ]
| Locvar addr, _ -> [ GETBP; OFFSET addr; ADD ]
| AccDeref e ->
match e with
| Access _ -> (cExpr e varEnv funEnv)
| Addr _ -> (cExpr e varEnv funEnv)
| _ ->
printfn "WARN: x86 pointer arithmetic not support!"
(cExpr e varEnv funEnv)
| AccIndex (acc, idx) ->
cAccess acc varEnv funEnv
@ [ LDI ]
@ x86patch (cExpr idx varEnv funEnv) @ [ ADD ]
(* Generate code to evaluate a list es of expressions: *)
and cExprs es varEnv funEnv : instr list =
List.concat (List.map (fun e -> cExpr e varEnv funEnv) es)
(* Generate code to evaluate arguments es and then call function f: *)
and callfun f es varEnv funEnv : instr list =
let (labf, tyOpt, paramdecs) = lookup funEnv f
let argc = List.length es
if argc = List.length paramdecs then
cExprs es varEnv funEnv @ [ CALL(argc, labf) ]
else
raise (Failure(f + ": parameter/argument mismatch"))
(* Compile a complete micro-C program: globals, call to main, functions *)
let argc = ref 0
let cProgram (Prog topdecs) : instr list =
let _ = resetLabels ()
let ((globalVarEnv, _), funEnv, globalInit) = makeGlobalEnvs topdecs
let compilefun (tyOpt, f, xs, body) =
let (labf, _, paras) = lookup funEnv f
let paraNums = List.length paras
let (envf, fdepthf) = bindParams paras (globalVarEnv, 0)
let code = cStmt body (envf, fdepthf) funEnv
[ FLabel (paraNums, labf) ]
@ code @ [ RET(paraNums - 1) ]
let functions =
List.choose
(function
| Fundec (rTy, name, argTy, body) -> Some(compilefun (rTy, name, argTy, body))
| Vardec _ -> None)
topdecs
let (mainlab, _, mainparams) = lookup funEnv "main"
argc := List.length mainparams
globalInit
@ [ LDARGS !argc
CALL(!argc, mainlab)
STOP ]
@ List.concat functions
(* Compile a complete micro-C and write the resulting instruction list
to file fname; also, return the program as a list of instructions.
*)
let intsToFile (inss: int list) (fname: string) =
File.WriteAllText(fname, String.concat " " (List.map string inss))
let writeInstr fname instrs =
let ins =
String.concat "\n" (List.map string instrs)
File.WriteAllText(fname, ins)
printfn $"VM instructions saved in file:\n\t{fname}"
let compileToFile program fname =
msg <|sprintf "program:\n %A" program
let instrs = cProgram program
msg <| sprintf "\nStack VM instrs:\n %A\n" instrs
writeInstr (fname + ".ins") instrs
let bytecode = code2ints instrs
msg <| sprintf "Stack VM numeric code:\n %A\n" bytecode
// 面向 x86 的虚拟机指令 略有差异,主要是地址偏移的计算方式不同
// 单独生成 x86 的指令
// isX86Instr := true
// let x86instrs = cProgram program
// writeInstr (fname + ".insx86") x86instrs
// let x86asmlist = List.map emitx86 x86instrs
// let x86asmbody =
// List.fold (fun asm ins -> asm + ins) "" x86asmlist
// let x86asm =
// (x86header + beforeinit !argc + x86asmbody)
// printfn $"x86 assembly saved in file:\n\t{fname}.asm"
// File.WriteAllText(fname + ".asm", x86asm)
// // let deinstrs = decomp bytecode
// // printf "deinstrs: %A\n" deinstrs
intsToFile bytecode (fname + ".out")
instrs
(* Example programs are found in the files ex1.c, ex2.c, etc *)