-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc6_linux.c
670 lines (640 loc) · 28.6 KB
/
c6_linux.c
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// c6.c - A mini compiler derived from the c4 by Robert Swierczek
// 修改者: 陳鍾誠 (模組化+中文註解+目的檔處理)
// char, int, and pointer types
// if, while, return, and expression statements
// just enough features to allow self-compilation and a bit more
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <fcntl.h>
#define int long long // c6 要求 int 與處理器位址同樣長度,因此將 int 定義為 64 位元整數。
char *p, *lp, // current position in source code (p: 目前原始碼指標, lp: 上一行原始碼指標)
*data,*datap, // data/bss pointer (資料段機器碼指標)
*op; // 指令字串列表
int *e, *le, *code, // current position in emitted code (e: 目前機器碼指標, le: 上一行機器碼指標)
*id, // currently parsed identifier (id: 目前的 id)
*sym, // symbol table (simple list of identifiers) (符號表)
tk, // current token (目前 token)
ival, // current token value (目前的 token 值)
ty, // current expression type (目前的運算式型態)
loc, // local variable offset (區域變數的位移)
line, // current line number (目前行號)
src, // print source and assembly flag (印出原始碼)
debug, // print executed instructions (印出執行指令 -- 除錯模式)
o_run, // 執行目的檔
o_save, // 反組譯目的檔
o_dump; // 傾印目的檔
int fd, poolsz, *idmain;
int *pc, *bp, *sp, codeLen, dataLen;
// tokens and classes (operators last and in precedence order) (按優先權順序排列)
enum { // token : 0-127 直接用該字母表達, 128 以後用代號。
Num = 128, Fun, Sys, Glo, Loc, Id,
Char, Else, Enum, If, Int, Return, Sizeof, While,
Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak
};
// opcodes (機器碼的 op)
enum { LEA ,IMM ,ADDR,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,
OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,
OPEN,READ,WRIT,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT };
// types (支援型態,只有 int, char, pointer)
enum { CHAR, INT, PTR };
// 因為沒有 struct,所以使用 offset 代替,例如 id[Tk] 代表 id.Tk (token), id[Hash] 代表 id.Hash, id[Name] 代表 id.Name, .....
// identifier offsets (since we can't create an ident struct)
enum { Tk, Hash, Name, Class, Type, Val, HClass, HType, HVal, Idsz }; // HClass, HType, HVal 是暫存的備份 ???
int stepInstr(int *p) {
// 傳回下一個指令大小:ADJ 之前有一個參數,之後沒有參數。
if (*++p <= ADJ) return 2; else return 1;
}
void printInstr(int *p, int *code, char *data) {
int ir, arg;
// 印出下一個指令
ir = *++p;
printf(" %4X:%X %8.4s", p-code, p, &op[ir * 5]);
if (ir <= ADJ) { // ADJ 之前的指令有一個參數
arg = *++p;
if (ir==JSR || ir==JMP || ir==BZ || ir==BNZ) {
if (arg==0) printf("0?\n"); else printf(" %X:%X\n", (int*)arg-code, (int*)arg);
} else if (ir==ADDR)
printf(" %X:%X\n", (char*)arg-data, arg);
else
printf(" %d\n", arg);
} else { // ADJ 之後的指令沒有任何參數
printf("\n");
}
}
void next() {
char *pp;
// 詞彙解析 lexer
while (tk = *p) {
++p;
if (tk == '\n') { // 換行
if (src) {
printf("%d: %.*s", line, p - lp, lp); // 印出該行
lp = p; // lp = p = 新一行的原始碼開頭
while (le < e) { // 印出上一行的所有目的碼
printInstr(le, code, data);
le = le + stepInstr(le);
}
}
++line;
}
else if (tk == '#') { // 取得 #include <stdio.h> 這類的一整行
while (*p != 0 && *p != '\n') ++p;
}
else if ((tk >= 'a' && tk <= 'z') || (tk >= 'A' && tk <= 'Z') || tk == '_') { // 取得變數名稱
pp = p - 1;
while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_')
tk = tk * 147 + *p++; // 計算雜湊值
tk = (tk << 6) + (p - pp); // 符號表的雜湊位址 ??
id = sym;
while (id[Tk]) { // 檢查是否碰撞 ?
if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; } // 沒碰撞就傳回 token
id = id + Idsz; // 碰撞,前進到下一格。
}
id[Name] = (int)pp; // id.Name = ptr(變數名稱)
id[Hash] = tk; // id.Hash = 雜湊值
tk = id[Tk] = Id; // token = id.Tk = Id
return;
}
else if (tk >= '0' && tk <= '9') { // 取得數字串
if (ival = tk - '0') { while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0'; } // 十進位
else if (*p == 'x' || *p == 'X') { // 十六進位
while ((tk = *++p) && ((tk >= '0' && tk <= '9') || (tk >= 'a' && tk <= 'f') || (tk >= 'A' && tk <= 'F'))) // 16 進位
ival = ival * 16 + (tk & 15) + (tk >= 'A' ? 9 : 0);
}
else { while (*p >= '0' && *p <= '7') ival = ival * 8 + *p++ - '0'; } // 八進位
tk = Num; // token = Number
return;
}
else if (tk == '/') {
if (*p == '/') { // 註解
++p;
while (*p != 0 && *p != '\n') ++p; // 略過註解
}
else { // 除法
tk = Div;
return;
}
}
else if (tk == '\'' || tk == '"') { // 字元或字串
pp = datap;
while (*p != 0 && *p != tk) {
if ((ival = *p++) == '\\') {
if ((ival = *p++) == 'n') ival = '\n'; // 處理 \n 的特殊情況
}
if (tk == '"') *datap++ = ival; // 把字串塞到資料段裏
}
++p;
if (tk == '"') ival = (int)pp; else tk = Num; // (若是字串) ? (ival = 字串 (在資料段中的) 指標) : (字元值)
return;
} // 以下為運算元 =+-!<>|&^%*[?~, ++, --, !=, <=, >=, ||, &&, ~ ;{}()],:
else if (tk == '=') { if (*p == '=') { ++p; tk = Eq; } else tk = Assign; return; }
else if (tk == '+') { if (*p == '+') { ++p; tk = Inc; } else tk = Add; return; }
else if (tk == '-') { if (*p == '-') { ++p; tk = Dec; } else tk = Sub; return; }
else if (tk == '!') { if (*p == '=') { ++p; tk = Ne; } return; }
else if (tk == '<') { if (*p == '=') { ++p; tk = Le; } else if (*p == '<') { ++p; tk = Shl; } else tk = Lt; return; }
else if (tk == '>') { if (*p == '=') { ++p; tk = Ge; } else if (*p == '>') { ++p; tk = Shr; } else tk = Gt; return; }
else if (tk == '|') { if (*p == '|') { ++p; tk = Lor; } else tk = Or; return; }
else if (tk == '&') { if (*p == '&') { ++p; tk = Lan; } else tk = And; return; }
else if (tk == '^') { tk = Xor; return; }
else if (tk == '%') { tk = Mod; return; }
else if (tk == '*') { tk = Mul; return; }
else if (tk == '[') { tk = Brak; return; }
else if (tk == '?') { tk = Cond; return; }
else if (tk == '~' || tk == ';' || tk == '{' || tk == '}' || tk == '(' || tk == ')' || tk == ']' || tk == ',' || tk == ':') return;
}
}
void expr(int lev) {
int t, *d;
// 運算式 expression, 其中 lev 代表優先等級
if (!tk) { printf("%d: unexpected eof in expression\n", line); exit(-1); } // EOF
else if (tk == Num) { *++e = IMM; *++e = ival; next(); ty = INT; } // 數值
else if (tk == '"') { // 字串
*++e = ADDR; *++e = ival; next();
while (tk == '"') next();
datap = (char *)((int)datap + sizeof(int) & -sizeof(int)); ty = PTR; // 用 int 為大小對齊 ??
}
else if (tk == Sizeof) { // 處理 sizeof(type) ,其中 type 可能為 char, int 或 ptr
next(); if (tk == '(') next(); else { printf("%d: open paren expected in sizeof\n", line); exit(-1); }
ty = INT; if (tk == Int) next(); else if (tk == Char) { next(); ty = CHAR; }
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk == ')') next(); else { printf("%d: close paren expected in sizeof\n", line); exit(-1); }
*++e = IMM; *++e = (ty == CHAR) ? sizeof(char) : sizeof(int);
ty = INT;
}
else if (tk == Id) { // 處理 id ...
d = id; next();
if (tk == '(') { // id (args) ,這是 call
next();
t = 0;
while (tk != ')') { expr(Assign); *++e = PSH; ++t; if (tk == ',') next(); } // 推入參數
next();
// d[Class] 可能為 Num = 128, Fun, Sys, Glo, Loc, ...
if (d[Class] == Sys) *++e = d[Val]; // token 是系統呼叫,直接呼叫之...
else if (d[Class] == Fun) { *++e = JSR; *++e = d[Val]; } // token 是自訂函數,用 JSR : jump to subroutine 指令呼叫
else { printf("%d: bad function call\n", line); exit(-1); }
if (t) { *++e = ADJ; *++e = t; } // 有參數,要調整堆疊 (ADJ : stack adjust)
ty = d[Type];
}
else if (d[Class] == Num) { *++e = IMM; *++e = d[Val]; ty = INT; } // 該 id 是數值
else {
if (d[Class] == Loc) { *++e = LEA; *++e = loc - d[Val]; } // 該 id 是區域變數,載入區域變數 (LEA : load local address)
else if (d[Class] == Glo) { *++e = IMM; *++e = d[Val]; } // 該 id 是全域變數,載入該全域變數 (IMM : load global address or immediate 載入全域變數或立即值)
else { printf("%d: undefined variable\n", line); exit(-1); }
*++e = ((ty = d[Type]) == CHAR) ? LC : LI; // LI : load int, LC : load char
}
}
else if (tk == '(') { // (E) : 有括號的運算式 ...
next();
if (tk == Int || tk == Char) {
t = (tk == Int) ? INT : CHAR; next();
while (tk == Mul) { next(); t = t + PTR; }
if (tk == ')') next(); else { printf("%d: bad cast\n", line); exit(-1); }
expr(Inc); // 處理 ++, -- 的情況
ty = t;
}
else {
expr(Assign); // 處理 (E) 中的 E (E 運算式必須能處理 (t=x) op y 的情況,所以用 expr(Assign))
if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); }
}
}
else if (tk == Mul) { // * 乘法
next(); expr(Inc);
if (ty > INT) ty = ty - PTR; else { printf("%d: bad dereference\n", line); exit(-1); }
*++e = (ty == CHAR) ? LC : LI;
}
else if (tk == And) { // & AND
next(); expr(Inc);
if (*e == LC || *e == LI) --e; else { printf("%d: bad address-of\n", line); exit(-1); }
ty = ty + PTR;
}
else if (tk == '!') { next(); expr(Inc); *++e = PSH; *++e = IMM; *++e = 0; *++e = EQ; ty = INT; } // NOT
else if (tk == '~') { next(); expr(Inc); *++e = PSH; *++e = IMM; *++e = -1; *++e = XOR; ty = INT; } // Logical NOT
else if (tk == Add) { next(); expr(Inc); ty = INT; }
else if (tk == Sub) {
next(); *++e = IMM;
if (tk == Num) { *++e = -ival; next(); } else { *++e = -1; *++e = PSH; expr(Inc); *++e = MUL; } // -Num or -E
ty = INT;
}
else if (tk == Inc || tk == Dec) { // ++ or --
t = tk; next(); expr(Inc);
if (*e == LC) { *e = PSH; *++e = LC; }
else if (*e == LI) { *e = PSH; *++e = LI; }
else { printf("%d: bad lvalue in pre-increment\n", line); exit(-1); }
*++e = PSH;
*++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char);
*++e = (t == Inc) ? ADD : SUB;
*++e = (ty == CHAR) ? SC : SI;
}
else { printf("%d: bad expression\n", line); exit(-1); }
// 參考: https://en.wikipedia.org/wiki/Operator-precedence_parser, https://www.cnblogs.com/rubylouvre/archive/2012/09/08/2657682.html https://web.archive.org/web/20151223215421/http://hall.org.ua/halls/wizzard/pdf/Vaughan.Pratt.TDOP.pdf
while (tk >= lev) { // "precedence climbing" or "Top Down Operator Precedence" method
t = ty;
if (tk == Assign) {
next();
if (*e == LC || *e == LI) *e = PSH; else { printf("%d: bad lvalue in assignment\n", line); exit(-1); }
expr(Assign); *++e = ((ty = t) == CHAR) ? SC : SI;
}
else if (tk == Cond) {
next();
*++e = BZ; d = ++e;
expr(Assign);
if (tk == ':') next(); else { printf("%d: conditional missing colon\n", line); exit(-1); }
*d = (int)(e + 3); *++e = JMP; d = ++e;
expr(Cond);
*d = (int)(e + 1);
}
else if (tk == Lor) { next(); *++e = BNZ; d = ++e; expr(Lan); *d = (int)(e + 1); ty = INT; }
else if (tk == Lan) { next(); *++e = BZ; d = ++e; expr(Or); *d = (int)(e + 1); ty = INT; }
else if (tk == Or) { next(); *++e = PSH; expr(Xor); *++e = OR; ty = INT; }
else if (tk == Xor) { next(); *++e = PSH; expr(And); *++e = XOR; ty = INT; }
else if (tk == And) { next(); *++e = PSH; expr(Eq); *++e = AND; ty = INT; }
else if (tk == Eq) { next(); *++e = PSH; expr(Lt); *++e = EQ; ty = INT; }
else if (tk == Ne) { next(); *++e = PSH; expr(Lt); *++e = NE; ty = INT; }
else if (tk == Lt) { next(); *++e = PSH; expr(Shl); *++e = LT; ty = INT; }
else if (tk == Gt) { next(); *++e = PSH; expr(Shl); *++e = GT; ty = INT; }
else if (tk == Le) { next(); *++e = PSH; expr(Shl); *++e = LE; ty = INT; }
else if (tk == Ge) { next(); *++e = PSH; expr(Shl); *++e = GE; ty = INT; }
else if (tk == Shl) { next(); *++e = PSH; expr(Add); *++e = SHL; ty = INT; }
else if (tk == Shr) { next(); *++e = PSH; expr(Add); *++e = SHR; ty = INT; }
else if (tk == Add) {
next(); *++e = PSH; expr(Mul);
if ((ty = t) > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; }
*++e = ADD;
}
else if (tk == Sub) {
next(); *++e = PSH; expr(Mul);
if (t > PTR && t == ty) { *++e = SUB; *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = DIV; ty = INT; }
else if ((ty = t) > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; *++e = SUB; }
else *++e = SUB;
}
else if (tk == Mul) { next(); *++e = PSH; expr(Inc); *++e = MUL; ty = INT; }
else if (tk == Div) { next(); *++e = PSH; expr(Inc); *++e = DIV; ty = INT; }
else if (tk == Mod) { next(); *++e = PSH; expr(Inc); *++e = MOD; ty = INT; }
else if (tk == Inc || tk == Dec) {
if (*e == LC) { *e = PSH; *++e = LC; }
else if (*e == LI) { *e = PSH; *++e = LI; }
else { printf("%d: bad lvalue in post-increment\n", line); exit(-1); }
*++e = PSH; *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char);
*++e = (tk == Inc) ? ADD : SUB;
*++e = (ty == CHAR) ? SC : SI;
*++e = PSH; *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char);
*++e = (tk == Inc) ? SUB : ADD;
next();
}
else if (tk == Brak) {
next(); *++e = PSH; expr(Assign);
if (tk == ']') next(); else { printf("%d: close bracket expected\n", line); exit(-1); }
if (t > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; }
else if (t < PTR) { printf("%d: pointer type expected\n", line); exit(-1); }
*++e = ADD;
*++e = ((ty = t - PTR) == CHAR) ? LC : LI;
}
else { printf("%d: compiler error tk=%d\n", line, tk); exit(-1); }
}
}
void stmt() {
int *a, *b;
// 陳述 statement
if (tk == If) { // if 語句
next();
if (tk == '(') next(); else { printf("%d: open paren expected\n", line); exit(-1); }
expr(Assign);
if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); }
*++e = BZ; b = ++e;
stmt();
if (tk == Else) { // else 語句
*b = (int)(e + 3); *++e = JMP; b = ++e;
next();
stmt();
}
*b = (int)(e + 1);
}
else if (tk == While) { // while 語句
next();
a = e + 1;
if (tk == '(') next(); else { printf("%d: open paren expected\n", line); exit(-1); }
expr(Assign);
if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); }
*++e = BZ; b = ++e;
stmt();
*++e = JMP; *++e = (int)a;
*b = (int)(e + 1);
}
else if (tk == Return) { // return 語句
next();
if (tk != ';') expr(Assign);
*++e = LEV;
if (tk == ';') next(); else { printf("%d: semicolon expected\n", line); exit(-1); }
}
else if (tk == '{') { // 區塊 {...}
next();
while (tk != '}') stmt();
next();
}
else if (tk == ';') { // ; 空陳述
next();
}
else { // 指定 assign
expr(Assign);
if (tk == ';') next(); else { printf("%d: semicolon expected\n", line); exit(-1); }
}
}
int prog() {
int bt, i;
// 編譯整個程式 Program
line = 1;
next();
while (tk) {
bt = INT; // basetype
if (tk == Int) next();
else if (tk == Char) { next(); bt = CHAR; }
else if (tk == Enum) { // enum Id? {... 列舉
next();
if (tk != '{') next(); // 略過 Id
if (tk == '{') {
next();
i = 0; // 紀錄 enum 的目前值
while (tk != '}') {
if (tk != Id) { printf("%d: bad enum identifier %d\n", line, tk); return -1; }
next();
if (tk == Assign) { // 有 Id=Num 的情況
next();
if (tk != Num) { printf("%d: bad enum initializer\n", line); return -1; }
i = ival;
next();
}
id[Class] = Num; id[Type] = INT; id[Val] = i++;
if (tk == ',') next();
}
next();
}
}
while (tk != ';' && tk != '}') { // 掃描直到區塊結束
ty = bt;
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk != Id) { printf("%d: bad global declaration\n", line); return -1; }
if (id[Class]) { printf("%d: duplicate global definition\n", line); return -1; } // id.Class 已經存在,重複宣告了!
next();
id[Type] = ty;
if (tk == '(') { // function 函數定義 ex: int f( ...
id[Class] = Fun;
id[Val] = (int)(e + 1);
next(); i = 0;
while (tk != ')') { // 掃描參數直到 ...)
ty = INT;
if (tk == Int) next();
else if (tk == Char) { next(); ty = CHAR; }
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk != Id) { printf("%d: bad parameter declaration\n", line); return -1; }
if (id[Class] == Loc) { printf("%d: duplicate parameter definition\n", line); return -1; } // 這裡的 id 會指向 hash 搜尋過的 symTable 裏的那個 (在 next 裏處理的),所以若是該 id 已經是 Local,那麼就重複了!
// 把 id.Class, id.Type, id.Val 暫存到 id.HClass, id.HType, id.Hval ,因為 Local 優先於 Global
id[HClass] = id[Class]; id[Class] = Loc;
id[HType] = id[Type]; id[Type] = ty;
id[HVal] = id[Val]; id[Val] = i++;
next();
if (tk == ',') next();
}
next();
if (tk != '{') { printf("%d: bad function definition\n", line); return -1; } // BODY 開始 {...
loc = ++i;
next();
while (tk == Int || tk == Char) { // 宣告
bt = (tk == Int) ? INT : CHAR;
next();
while (tk != ';') {
ty = bt;
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk != Id) { printf("%d: bad local declaration\n", line); return -1; }
if (id[Class] == Loc) { printf("%d: duplicate local definition\n", line); return -1; }
// 把 id.Class, id.Type, id.Val 暫存到 id.HClass, id.HType, id.Hval ,因為 Local 優先於 Global
id[HClass] = id[Class]; id[Class] = Loc;
id[HType] = id[Type]; id[Type] = ty;
id[HVal] = id[Val]; id[Val] = ++i;
next();
if (tk == ',') next();
}
next();
}
*++e = ENT; *++e = i - loc;
while (tk != '}') stmt();
*++e = LEV;
id = sym; // unwind symbol table locals (把被區域變數隱藏掉的那些 Local id 還原,恢復全域變數的符號定義)
while (id[Tk]) {
if (id[Class] == Loc) {
id[Class] = id[HClass];
id[Type] = id[HType];
id[Val] = id[HVal];
}
id = id + Idsz;
}
}
else {
id[Class] = Glo;
id[Val] = (int)datap;
datap = datap + sizeof(int);
}
if (tk == ',') next();
}
next();
}
return 0;
}
int compile(int fd) {
int i, *t;
// 編譯器
p = "char else enum if int return sizeof while "
"open read write close printf malloc free memset memcmp exit void main";
i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table
i = OPEN; while (i <= EXIT) { next(); id[Class] = Sys; id[Type] = INT; id[Val] = i++; } // add library to symbol table
next(); id[Tk] = Char; // handle void type
next(); idmain = id; // keep track of main
if (!(lp = p = malloc(poolsz))) { printf("could not malloc(%d) source area\n", poolsz); return -1; }
if ((i = read(fd, p, poolsz-1)) <= 0) { printf("read() returned %d\n", i); return -1; }
p[i] = 0; // 設定程式 p 字串結束符號 \0
return prog();
}
int run(int *pc, int *bp, int *sp) {
int a, cycle; // a: 累積器, cycle: 執行指令數
int i, *t; // temps
// 虛擬機 => pc: 程式計數器, sp: 堆疊暫存器, bp: 框架暫存器
cycle = 0;
while (1) {
i = *pc++; ++cycle;
if (debug) {
printInstr(pc-2, code, data); // pc-2, 因為已經 pc++ 過了,而 printInstr 又是落後一個的情況。
}
if (i == LEA) a = (int)(bp + *pc++); // load local address 載入區域變數
else if (i == IMM) a = *pc++; // load immediate 載入立即值
else if (i == ADDR) { a = *pc; pc++; } // load address 載入位址
else if (i == JMP) pc = (int *)*pc; // jump 躍躍指令
else if (i == JSR) { *--sp = (int)(pc + 1); pc = (int *)*pc; } // jump to subroutine 跳到副程式
else if (i == BZ) pc = a ? pc + 1 : (int *)*pc; // branch if zero if (a==0) goto m[pc]
else if (i == BNZ) pc = a ? (int *)*pc : pc + 1; // branch if not zero if (a!=0) goto m[pc]
else if (i == ENT) { *--sp = (int)bp; bp = sp; sp = sp - *pc++; } // enter subroutine 進入副程式
else if (i == ADJ) sp = sp + *pc++; // stack adjust 調整堆疊
else if (i == LEV) { sp = bp; bp = (int *)*sp++; pc = (int *)*sp++; } // leave subroutine 離開副程式
else if (i == LI) a = *(int *)a; // load int 載入整數
else if (i == LC) a = *(char *)a; // load char 載入字元
else if (i == SI) *(int *)*sp++ = a; // store int 儲存整數
else if (i == SC) a = *(char *)*sp++ = a; // store char 儲存字元
else if (i == PSH) *--sp = a; // push 推入堆疊
else if (i == OR) a = *sp++ | a; // a = a OR *sp
else if (i == XOR) a = *sp++ ^ a; // a = a XOR *sp
else if (i == AND) a = *sp++ & a; // ...
else if (i == EQ) a = *sp++ == a;
else if (i == NE) a = *sp++ != a;
else if (i == LT) a = *sp++ < a;
else if (i == GT) a = *sp++ > a;
else if (i == LE) a = *sp++ <= a;
else if (i == GE) a = *sp++ >= a;
else if (i == SHL) a = *sp++ << a;
else if (i == SHR) a = *sp++ >> a;
else if (i == ADD) a = *sp++ + a;
else if (i == SUB) a = *sp++ - a;
else if (i == MUL) a = *sp++ * a;
else if (i == DIV) a = *sp++ / a;
else if (i == MOD) a = *sp++ % a;
else if (i == OPEN) a = open((char *)sp[1], *sp); // 開檔
else if (i == READ) a = read(sp[2], (char *)sp[1], *sp); // 讀檔
else if (i == WRIT) a = write(sp[2], (char *)sp[1], *sp); // 寫檔
else if (i == CLOS) a = close(*sp); // 關檔
else if (i == PRTF) { t = sp + pc[1]; a = printf((char *)t[-1], t[-2], t[-3], t[-4], t[-5], t[-6]); } // printf("....", a, b, c, d, e)
else if (i == MALC) a = (int)malloc(*sp); // 分配記憶體
else if (i == FREE) free((void *)*sp); // 釋放記憶體
else if (i == MSET) a = (int)memset((char *)sp[2], sp[1], *sp); // 設定記憶體
else if (i == MCMP) a = memcmp((char *)sp[2], (char *)sp[1], *sp); // 比較記憶體
else if (i == EXIT) { printf("exit(%d) cycle = %d\n", *sp, cycle); return *sp; } // EXIT 離開
else { printf("unknown instruction = %d! cycle = %d\n", i, cycle); return -1; } // 錯誤處理
}
}
int vm(int argc, char **argv) {
int *t;
// 虛擬機: setup stack
bp = sp = (int *)((int)sp + poolsz);
*--sp = EXIT; // call exit if main returns
*--sp = PSH; t = sp;
*--sp = argc; // 把 argc,argv 放入堆疊,這樣 main(argc,argv) 才能取得到
*--sp = (int)argv;
*--sp = (int)t; // 推入返回點,於是最後 RET 時會跳回 t=sp 指定的位址,接著呼叫 EXIT 離開。
return run(pc, bp, sp);
}
int obj_relocate(int *code, int codeLen, int *pcode1, char *pdata1, int *pcode2, char *pdata2) {
int *p, ir;
// 程式段機器碼重定位
p=code;
while (p<code+codeLen) {
ir=*++p;
if (ir <= ADJ) { // ADJ 之前的指令,有一個參數
++p;
if (ir == ADDR) // 資料位址,重定位
*p = (int)(pdata2+((char*)*p-pdata1));
else if (ir==JSR || ir==JMP || ir==BZ || ir==BNZ) // 跳躍指令,重定位
*p = (int)(pcode2+((int*)*p-pcode1));
}
}
}
int obj_dump(int *entry, int *code, int codeLen, char *data, int dataLen) {
int *p, ir, arg, step;
char *dp;
// 印出目的碼
printf("entry: 0x%X\n", entry);
printf("code: start=0x%X length=0x%X\n", code, codeLen);
p=code;
while (p<code+codeLen) {
printInstr(p, code, data);
p = p+stepInstr(p);
}
printf("data: start=0x%X length=0x%X\n", data, dataLen);
dp = data;
while (dp<data+dataLen) {
printf("%c", *dp++);
}
printf("\n");
}
int obj_save(char *oFile, int *entry, int *code, int codeLen, char *data, int dataLen) {
int fd, len;
// 儲存目的檔
fd = open(oFile, 0101501, 0666); // Windows: O_BINARY|O_CREAT|O_WRONLY|O_TRUNC=0101401 // Linux: O_CREAT|O_WRONLY|O_TRUNC=01101
if (fd == -1) return -1;
write(fd, &entry, sizeof(int));
write(fd, &code, sizeof(int));
write(fd, &codeLen, sizeof(int));
write(fd, &data, sizeof(int));
write(fd, &dataLen, sizeof(int));
write(fd, code, codeLen*sizeof(int));
write(fd, data, dataLen);
close(fd);
}
int obj_load(int fd) {
int *codex, *entry, len;
char *datax;
// 載入目的檔
read(fd, &entry, sizeof(int));
read(fd, &codex, sizeof(int));
read(fd, &codeLen, sizeof(int));
read(fd, &datax, sizeof(int));
read(fd, &dataLen, sizeof(int));
len = read(fd, code, codeLen*sizeof(int));
if (len != codeLen*sizeof(int)) {
printf("obj_load:read code fail, len(%d)!=size(%d)\n", len, codeLen*sizeof(int));
exit(1);
}
len = read(fd, data, dataLen);
obj_relocate(code, codeLen, codex, datax, code, data);
pc = code + (entry-codex);
}
int main(int argc, char **argv) {
char *iFile, *oFile, *narg;
// 主程式
--argc; ++argv; // 略過程式名稱 ./c6
if (argc > 0 && **argv == '-' && (*argv)[1] == 's') { src = 1; --argc; ++argv; }
if (argc > 0 && **argv == '-' && (*argv)[1] == 'd') { debug = 1; --argc; ++argv; }
if (argc > 0 && **argv == '-' && (*argv)[1] == 'r') { o_run = 1; --argc; ++argv; }
if (argc > 0 && **argv == '-' && (*argv)[1] == 'u') { o_dump = 1; --argc; ++argv; }
if (argc < 1) { printf("usage: c6 [-s] [-d] [-r] [-u] in_file [-o] out_file...\n"); return -1; }
iFile = *argv;
if (argc > 1) {
narg = *(argv+1);
if (*narg == '-' && narg[1] == 'o') {
o_save = 1;
oFile = *(argv+2);
}
}
if ((fd = open(iFile, 0100000)) < 0) { // 0100000 代表以 BINARY mode 開啟 (Windows 中預設為 TEXT mode)
printf("could not open(%s)\n", iFile);
return -1;
}
poolsz = 256*1024; // 最大記憶體大小 (程式碼/資料/堆疊/符號表)
if (!(sym = malloc(poolsz))) { printf("could not malloc(%d) symbol area\n", poolsz); return -1; } // 符號段
if (!(code = le = e = malloc(poolsz))) { printf("could not malloc(%d) text area\n", poolsz); return -1; } // 程式段
if (!(data = datap = malloc(poolsz))) { printf("could not malloc(%d) data area\n", poolsz); return -1; } // 資料段
if (!(sp = malloc(poolsz))) { printf("could not malloc(%d) stack area\n", poolsz); return -1; } // 堆疊段
memset(sym, 0, poolsz);
memset(e, 0, poolsz);
memset(data, 0, poolsz);
op = "LEA ,IMM ,ADDR,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
"OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
"OPEN,READ,WRIT,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT,";
if (o_dump) { // -u: 印出目的檔
obj_load(fd);
obj_dump(pc, code, codeLen, data, dataLen);
return 0;
}
if (o_run) { // -r: 執行目的檔
obj_load(fd);
vm(argc, argv);
return 0;
}
if (compile(fd)==-1) return -1; // 編譯
if (!(pc = (int *)idmain[Val])) { printf("main() not defined\n"); return -1; }
if (src) return 0; // 編譯並列印,不執行
if (o_save) { // -o 輸出目的檔,但不執行
obj_save(oFile, pc, code, e-code, data, datap-data);
printf("Compile %s success!\nOutput: %s\n", iFile, oFile);
return 0;
}
close(fd);
vm(argc, argv); // 用虛擬機執行編譯出來的碼
}