-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComp.fs
467 lines (367 loc) · 15.1 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
(* 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.
[email protected] * 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 StackMachine
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
ex1.c下面的的全局声明
int g ;
int h[3]
构造的环境如下:
h 是整型数组,长度为 3,g是整数,下一个空闲位置是 5
([("h", (Glovar 4, TypA (TypI, Some 3)));
("g", (Glovar 0, TypI))], 5)
实际存储布局如下:
(0,0)(1,0)(2,0)(3,0) (4,1) ......
*)
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)}"
// newloc 下个空闲存储位置
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 varEnv:\n{varEnv}\n"
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)
| 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.Value 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 ]
// add by tianhuiwen
| DoWhile (body, e) ->
let labbegin = newLabel ()
let labtest = newLabel ()
[ GOTO labbegin; Label labbegin ]
@ cStmt body varEnv funEnv
@ [ Label labtest ]
@ cExpr e varEnv funEnv
@ [ IFNZRO labbegin ]
| DoUntil (body, e) ->
let labbegin = newLabel ()
let labtest = newLabel ()
[ GOTO labbegin; Label labbegin ]
@ cStmt body varEnv funEnv
@ [ Label labtest ]
@ cExpr e varEnv funEnv
@ [ IFZERO labbegin ]
| Until (e, body) ->
let labbegin = newLabel ()
let labtest = newLabel ()
[ GOTO labtest; Label labbegin ]
@ cStmt body varEnv funEnv
@ [ Label labtest ]
@ cExpr e varEnv funEnv
@ [ IFZERO labbegin ]
| Switch (e, cases) ->
let rec matchcase c =
match c with
| Case (e1, body) :: tail ->
let labend = newLabel ()
let labfin = newLabel ()
[DUP]
@ cExpr e1 varEnv funEnv
@ [EQ]
@ [ IFZERO labend ]
@ cStmt body varEnv funEnv
@ [ GOTO labfin ]
@ [ Label labend ]
@ matchcase tail
@ [ Label labfin ]
| [] -> []
cExpr e varEnv funEnv
@ matchcase cases
@[INCSP -1]
| Expr e -> cExpr e varEnv funEnv @ [ INCSP -1 ]
// add by tianhuiwen
| 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
(* 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 ]
| PreInc acc -> cAccess acc varEnv funEnv @ [DUP] @ [LDI] @ [CSTI 1] @ [ADD] @ [STI]
| PreDec acc -> cAccess acc varEnv funEnv @ [DUP] @ [LDI] @ [CSTI 1] @ [SUB] @ [STI]
| CstI i -> [ CSTI i ]
| 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(cond, e1, e2) ->
let labelse = newLabel ()
let labend = newLabel ()
cExpr cond varEnv funEnv
@ [ IFZERO labelse ]
@ cExpr e1 varEnv funEnv
@ [ GOTO labend ]
@ [ Label labelse ]
@ cExpr e2 varEnv funEnv
@ [ Label labend ]
| 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] 表示
// F# ! 操作符 取引用类型的值
| Glovar addr, _ ->
if isX86Instr.Value 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.Value <- List.length mainparams
globalInit
@ [ LDARGS argc.Value
CALL(argc.Value, 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.Value <- 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.Value + 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 *)