From 77ae76eea9df695b66834b8df2a1060c7dad3eca Mon Sep 17 00:00:00 2001 From: Jack Palevich Date: Sun, 10 May 2009 19:59:24 -0700 Subject: [PATCH] converted to C++ Base address of constant table changed, so had to update the "-orig" files. --- libacc/{acc.c => acc.cpp} | 62 ++++++++++++++++++++++-------------- libacc/test | 2 +- libacc/tests/hello.out-orig | Bin 40 -> 40 bytes libacc/tests/otcc.out-orig | Bin 8591 -> 8591 bytes 4 files changed, 39 insertions(+), 25 deletions(-) rename libacc/{acc.c => acc.cpp} (96%) diff --git a/libacc/acc.c b/libacc/acc.cpp similarity index 96% rename from libacc/acc.c rename to libacc/acc.cpp index 4aa2b3cc2..c2c38c49e 100644 --- a/libacc/acc.c +++ b/libacc/acc.cpp @@ -20,11 +20,14 @@ 3. This notice may not be removed or altered from any source distribution. */ +#include +#include #include #include #include #include +class compiler { /* vars: value of variables loc : local variable index glo : global variable index @@ -64,12 +67,12 @@ FILE* file; #define TAG_TOK ' ' #define TAG_MACRO 2 -pdef(t) +void pdef(int t) { *(char *)dstk++ = t; } -inp() +void inp() { if (dptr) { ch = *(char *)dptr++; @@ -82,13 +85,13 @@ inp() /* printf("ch=%c 0x%x\n", ch, ch); */ } -isid() +int isid() { return isalnum(ch) | ch == '_'; } /* read a character constant */ -getq() +void getq() { if (ch == '\\') { inp(); @@ -97,7 +100,7 @@ getq() } } -next() +void next() { int l, a; @@ -224,11 +227,11 @@ void error(char *fmt,...) fprintf(stderr, "%d: ", ftell((FILE *)file)); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); - exit(1); va_end(ap); + exit(1); } -void skip(c) +void skip(int c) { if (tok != c) { error("'%c' expected", c); @@ -236,7 +239,7 @@ void skip(c) next(); } -o(n) +void o(int n) { /* cannot use unsigned, so we must do a hack */ while (n && n != -1) { @@ -246,7 +249,7 @@ o(n) } /* output a symbol and patch all calls to it */ -gsym(t) +void gsym(int t) { int n; while (t) { @@ -261,7 +264,7 @@ gsym(t) #define psym oad /* instruction + address */ -oad(n, t) +int oad(int n, int t) { o(n); *(int *)ind = t; @@ -271,24 +274,24 @@ oad(n, t) } /* load immediate value */ -li(t) +int li(int t) { oad(0xb8, t); /* mov $xx, %eax */ } -gjmp(t) +int gjmp(int t) { return psym(0xe9, t); } /* l = 0: je, l == 1: jne */ -gtst(l, t) +int gtst(int l, int t) { o(0x0fc085); /* test %eax, %eax, je/jne xxx */ return psym(0x84 + l, t); } -gcmp(t) +int gcmp(int t) { o(0xc139); /* cmp %eax,%ecx */ li(0); @@ -297,14 +300,14 @@ gcmp(t) o(0xc0); } -gmov(l, t) +int gmov(int l, int t) { o(l + 0x83); oad((t < LOCAL) << 7 | 5, t); } /* l is one if '=' parsing wanted (quick hack) */ -unary(l) +void unary(int l) { int n, t, a, c; @@ -375,7 +378,7 @@ unary(l) n = *(int *)t; /* forward reference: try dlsym */ if (!n) - n = dlsym(0, last_id); + n = (int) dlsym(0, (char*) last_id); if (tok == '=' & l) { /* assignment */ next(); @@ -426,7 +429,7 @@ unary(l) } } -sum(l) +void sum(int l) { int t, n, a; @@ -468,19 +471,20 @@ sum(l) } } -expr() +void expr() { sum(11); } -test_expr() +int test_expr() { expr(); return gtst(0, 0); } -block(l) + +void block(int l) { int a, n, t; @@ -524,7 +528,7 @@ block(l) } } skip(')'); - block(&a); + block((int) &a); gjmp(n - ind - 5); /* jmp */ gsym(a); } else if (tok == '{') { @@ -550,7 +554,7 @@ block(l) } /* 'l' is true if local declarations */ -decl(l) +void decl(int l) { int a; @@ -599,7 +603,10 @@ decl(l) } } -main(int n, char** t) +public: +compiler(){} + +int compile(int n, char** t) { file = stdin; if (n-- > 1) { @@ -627,3 +634,10 @@ main(int n, char** t) return (*(int (*)())*(int *)(vars + TOK_MAIN)) (n, t); #endif } + +}; + +int main(int argc, char** argv) { + compiler c; + return c.compile(argc, argv); +} \ No newline at end of file diff --git a/libacc/test b/libacc/test index eb4f383be..b4cef93c6 100755 --- a/libacc/test +++ b/libacc/test @@ -1,2 +1,2 @@ #!/bin/sh -gcc acc.c -DTEST -ldl -o tests/acc && tests/acc tests/otcc.c tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig +g++ acc.cpp -DTEST -ldl -o tests/acc && tests/acc tests/otcc.c tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig diff --git a/libacc/tests/hello.out-orig b/libacc/tests/hello.out-orig index bb178c079d8e2f7d575a9d4d2c3973f94ddbce2f..1fb7bf5cf7b41e1550da4e07b4817180ee6a5471 100644 GIT binary patch literal 40 mcmWITeA@Vi0SFr3umH&&3RLkryDM@d!xxRLkryDM@d!xxPly|36wjB;Brqja&lQ&{b;uDWz(s|C}Pve5 z`@MhPJGpRj;qa34eQPs<&oym38fgovhT9o}thuH>|0JX_4UNvZ)aoJ8=c#QHRqAZS zF_ZXnfiEAf9+Tf0pfB#%VA0w%#BLYGtCU1G2(9y!`-&mDK%H*?tKJZ%Zzq+lKt^fMs!2^u|% zglnp`$sV}oZaVVo<(lv}Y*fn-H776p_7%xU%*Yx&sMC;v>$;I+1oFRZl$NOAXM|h= zGcgV_o?RCQsaMXi$FsuyuL`)xjqMKdoU!#~<96heG^;tt{XyFJ=(^_xDu%81n^vCW zy(va_gG4&0Pnf$?xFzG$!ids-XwJgvuVxjPkF(78#~)lFapzCZ^V))(xkm3v`t%Di zBqTWBAbC|Xxl{Zaay`%M)R}dVQ=e_^iHtR&O8*oX+|KX7pj>{)dOVRdn3hf_?n6ii z6T5)VC7xoH+h)PmKO~xv%nvo$FuWknpiD~vXGe}M^jM0)NI5umKOHJYVRv=7#xezy ze)ccV^B;zAS|gKtaoaX)Ks-;GG?@{QL^eM7#q;`S7`Z5fCZT5Q6C)PuI>?Zl(8=XmR_M+n^E}2Ts5{@(4I{jE zOS<*UO)*#;$sPxOmi@vXrlU{|Sxc|!;BHSG;~lJ)4E+vPS?;HxV~$CAzm3jF zh2n)HV*3E_^+Fc-eqjN4y+EHU`K+ER?kK_}G#D0+{&PiYmaUH$Z{d2wffFEO#h;J` zkAo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