system/corennnnn
修订版 | aad2046b4d80e22ce1d1952cf700a4fe5379441f (tree) |
---|---|
时间 | 2009-05-12 02:01:13 |
作者 | Android (Google) Code Review <android-gerrit@goog...> |
Commiter | Android (Google) Code Review |
Merge change 1366
* changes:
@@ -20,11 +20,14 @@ | ||
20 | 20 | 3. This notice may not be removed or altered from any source distribution. |
21 | 21 | */ |
22 | 22 | |
23 | +#include <ctype.h> | |
24 | +#include <dlfcn.h> | |
23 | 25 | #include <stdarg.h> |
24 | 26 | #include <stdio.h> |
25 | 27 | #include <stdlib.h> |
26 | 28 | #include <string.h> |
27 | 29 | |
30 | +class compiler { | |
28 | 31 | /* vars: value of variables |
29 | 32 | loc : local variable index |
30 | 33 | glo : global variable index |
@@ -64,12 +67,12 @@ FILE* file; | ||
64 | 67 | #define TAG_TOK ' ' |
65 | 68 | #define TAG_MACRO 2 |
66 | 69 | |
67 | -pdef(t) | |
70 | +void pdef(int t) | |
68 | 71 | { |
69 | 72 | *(char *)dstk++ = t; |
70 | 73 | } |
71 | 74 | |
72 | -inp() | |
75 | +void inp() | |
73 | 76 | { |
74 | 77 | if (dptr) { |
75 | 78 | ch = *(char *)dptr++; |
@@ -82,13 +85,13 @@ inp() | ||
82 | 85 | /* printf("ch=%c 0x%x\n", ch, ch); */ |
83 | 86 | } |
84 | 87 | |
85 | -isid() | |
88 | +int isid() | |
86 | 89 | { |
87 | 90 | return isalnum(ch) | ch == '_'; |
88 | 91 | } |
89 | 92 | |
90 | 93 | /* read a character constant */ |
91 | -getq() | |
94 | +void getq() | |
92 | 95 | { |
93 | 96 | if (ch == '\\') { |
94 | 97 | inp(); |
@@ -97,7 +100,7 @@ getq() | ||
97 | 100 | } |
98 | 101 | } |
99 | 102 | |
100 | -next() | |
103 | +void next() | |
101 | 104 | { |
102 | 105 | int l, a; |
103 | 106 |
@@ -224,11 +227,11 @@ void error(char *fmt,...) | ||
224 | 227 | fprintf(stderr, "%d: ", ftell((FILE *)file)); |
225 | 228 | vfprintf(stderr, fmt, ap); |
226 | 229 | fprintf(stderr, "\n"); |
227 | - exit(1); | |
228 | 230 | va_end(ap); |
231 | + exit(1); | |
229 | 232 | } |
230 | 233 | |
231 | -void skip(c) | |
234 | +void skip(int c) | |
232 | 235 | { |
233 | 236 | if (tok != c) { |
234 | 237 | error("'%c' expected", c); |
@@ -236,7 +239,7 @@ void skip(c) | ||
236 | 239 | next(); |
237 | 240 | } |
238 | 241 | |
239 | -o(n) | |
242 | +void o(int n) | |
240 | 243 | { |
241 | 244 | /* cannot use unsigned, so we must do a hack */ |
242 | 245 | while (n && n != -1) { |
@@ -246,7 +249,7 @@ o(n) | ||
246 | 249 | } |
247 | 250 | |
248 | 251 | /* output a symbol and patch all calls to it */ |
249 | -gsym(t) | |
252 | +void gsym(int t) | |
250 | 253 | { |
251 | 254 | int n; |
252 | 255 | while (t) { |
@@ -261,7 +264,7 @@ gsym(t) | ||
261 | 264 | #define psym oad |
262 | 265 | |
263 | 266 | /* instruction + address */ |
264 | -oad(n, t) | |
267 | +int oad(int n, int t) | |
265 | 268 | { |
266 | 269 | o(n); |
267 | 270 | *(int *)ind = t; |
@@ -271,24 +274,24 @@ oad(n, t) | ||
271 | 274 | } |
272 | 275 | |
273 | 276 | /* load immediate value */ |
274 | -li(t) | |
277 | +int li(int t) | |
275 | 278 | { |
276 | 279 | oad(0xb8, t); /* mov $xx, %eax */ |
277 | 280 | } |
278 | 281 | |
279 | -gjmp(t) | |
282 | +int gjmp(int t) | |
280 | 283 | { |
281 | 284 | return psym(0xe9, t); |
282 | 285 | } |
283 | 286 | |
284 | 287 | /* l = 0: je, l == 1: jne */ |
285 | -gtst(l, t) | |
288 | +int gtst(int l, int t) | |
286 | 289 | { |
287 | 290 | o(0x0fc085); /* test %eax, %eax, je/jne xxx */ |
288 | 291 | return psym(0x84 + l, t); |
289 | 292 | } |
290 | 293 | |
291 | -gcmp(t) | |
294 | +int gcmp(int t) | |
292 | 295 | { |
293 | 296 | o(0xc139); /* cmp %eax,%ecx */ |
294 | 297 | li(0); |
@@ -297,14 +300,14 @@ gcmp(t) | ||
297 | 300 | o(0xc0); |
298 | 301 | } |
299 | 302 | |
300 | -gmov(l, t) | |
303 | +int gmov(int l, int t) | |
301 | 304 | { |
302 | 305 | o(l + 0x83); |
303 | 306 | oad((t < LOCAL) << 7 | 5, t); |
304 | 307 | } |
305 | 308 | |
306 | 309 | /* l is one if '=' parsing wanted (quick hack) */ |
307 | -unary(l) | |
310 | +void unary(int l) | |
308 | 311 | { |
309 | 312 | int n, t, a, c; |
310 | 313 |
@@ -375,7 +378,7 @@ unary(l) | ||
375 | 378 | n = *(int *)t; |
376 | 379 | /* forward reference: try dlsym */ |
377 | 380 | if (!n) |
378 | - n = dlsym(0, last_id); | |
381 | + n = (int) dlsym(0, (char*) last_id); | |
379 | 382 | if (tok == '=' & l) { |
380 | 383 | /* assignment */ |
381 | 384 | next(); |
@@ -426,7 +429,7 @@ unary(l) | ||
426 | 429 | } |
427 | 430 | } |
428 | 431 | |
429 | -sum(l) | |
432 | +void sum(int l) | |
430 | 433 | { |
431 | 434 | int t, n, a; |
432 | 435 |
@@ -468,19 +471,20 @@ sum(l) | ||
468 | 471 | } |
469 | 472 | } |
470 | 473 | |
471 | -expr() | |
474 | +void expr() | |
472 | 475 | { |
473 | 476 | sum(11); |
474 | 477 | } |
475 | 478 | |
476 | 479 | |
477 | -test_expr() | |
480 | +int test_expr() | |
478 | 481 | { |
479 | 482 | expr(); |
480 | 483 | return gtst(0, 0); |
481 | 484 | } |
482 | 485 | |
483 | -block(l) | |
486 | + | |
487 | +void block(int l) | |
484 | 488 | { |
485 | 489 | int a, n, t; |
486 | 490 |
@@ -524,7 +528,7 @@ block(l) | ||
524 | 528 | } |
525 | 529 | } |
526 | 530 | skip(')'); |
527 | - block(&a); | |
531 | + block((int) &a); | |
528 | 532 | gjmp(n - ind - 5); /* jmp */ |
529 | 533 | gsym(a); |
530 | 534 | } else if (tok == '{') { |
@@ -550,7 +554,7 @@ block(l) | ||
550 | 554 | } |
551 | 555 | |
552 | 556 | /* 'l' is true if local declarations */ |
553 | -decl(l) | |
557 | +void decl(int l) | |
554 | 558 | { |
555 | 559 | int a; |
556 | 560 |
@@ -599,7 +603,10 @@ decl(l) | ||
599 | 603 | } |
600 | 604 | } |
601 | 605 | |
602 | -main(int n, char** t) | |
606 | +public: | |
607 | +compiler(){} | |
608 | + | |
609 | +int compile(int n, char** t) | |
603 | 610 | { |
604 | 611 | file = stdin; |
605 | 612 | if (n-- > 1) { |
@@ -627,3 +634,10 @@ main(int n, char** t) | ||
627 | 634 | return (*(int (*)())*(int *)(vars + TOK_MAIN)) (n, t); |
628 | 635 | #endif |
629 | 636 | } |
637 | + | |
638 | +}; | |
639 | + | |
640 | +int main(int argc, char** argv) { | |
641 | + compiler c; | |
642 | + return c.compile(argc, argv); | |
643 | +} | |
\ No newline at end of file |
@@ -1,2 +1,2 @@ | ||
1 | 1 | #!/bin/sh |
2 | -gcc acc.c -DTEST -ldl -o tests/acc && tests/acc tests/otcc.c tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig | |
2 | +g++ acc.cpp -DTEST -ldl -o tests/acc && tests/acc tests/otcc.c tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig |