996
|
1 #include <stdlib.h>
|
|
2 #include <stdio.h>
|
|
3 #include <string.h>
|
|
4
|
|
5 #include "c2m.h"
|
|
6 #include "egobfi.h"
|
|
7
|
|
8 #include "c2m/i386/i386.h"
|
|
9
|
|
10 #undef CHKPSZ
|
|
11 #define CHKPSZ(a) \
|
|
12 if ((a) >= plen) { \
|
|
13 void (*nprog)() = malloc(plen); \
|
|
14 if (nprog == NULL) { perror("malloc"); exit(1); } \
|
|
15 C2MJUMP(nprog) \
|
|
16 C2MTAIL() \
|
|
17 prog = nprog; \
|
|
18 pptr = 0; \
|
|
19 }
|
|
20
|
|
21 void mygetchar()
|
|
22 {
|
|
23 int input = getchar();
|
|
24 if (input == EOF) {
|
|
25 switch (eof) {
|
|
26 case 0:
|
|
27 *mptr = 0;
|
|
28 break;
|
|
29
|
|
30 case -1:
|
|
31 *mptr = -1;
|
|
32 break;
|
|
33 }
|
|
34 } else {
|
|
35 *mptr = (USEINT) input;
|
|
36 }
|
|
37 }
|
|
38
|
|
39 void myputchar()
|
|
40 {
|
|
41 putchar(*mptr);
|
|
42 fflush(stdout);
|
|
43 }
|
|
44
|
|
45 void mydebug()
|
|
46 {
|
|
47 int cur;
|
|
48 cur = mptr - mem - 10;
|
|
49 if (cur < 0) cur = 0;
|
|
50 printf("%d:", cur);
|
|
51 for (; cur < mptr - mem + 40; cur++) {
|
|
52 if (cur == mptr - mem) putchar('*');
|
|
53 CHKMSZ(mem + cur)
|
|
54 printf("%d|", (int) mem[cur]);
|
|
55 }
|
|
56 putchar('\n');
|
|
57 }
|
|
58
|
|
59 void mychkmem()
|
|
60 {
|
|
61 CHKMSZ(mptr)
|
|
62 }
|
|
63
|
|
64 void c2m()
|
|
65 {
|
|
66 /* optimize optimizes a chunk of code at ipptr */
|
|
67 int cnt, a;
|
|
68 int mptri = (int) &mptr;
|
|
69
|
|
70 if (pptr == 0) C2MHEAD()
|
|
71
|
|
72 while (iprog[ipptr]) {
|
|
73 CHKPSZ(pptr + 100)
|
|
74 /*printf("%d\n", pptr); */
|
|
75 switch(iprog[ipptr]) {
|
|
76 case '+':
|
|
77 case '-':
|
|
78 cnt = 0;
|
|
79 while (iprog[ipptr] == '+' ||
|
|
80 iprog[ipptr] == '-') {
|
|
81 if (iprog[ipptr] == '+') cnt++;
|
|
82 else cnt--;
|
|
83 ipptr++;
|
|
84 }
|
|
85
|
|
86 C2MADD(cnt)
|
|
87 break;
|
|
88
|
|
89 case '>':
|
|
90 case '<':
|
|
91 cnt = 0;
|
|
92 while (iprog[ipptr] == '>' ||
|
|
93 iprog[ipptr] == '<') {
|
|
94 if (iprog[ipptr] == '>') cnt++;
|
|
95 else cnt--;
|
|
96 ipptr++;
|
|
97 }
|
|
98
|
|
99 C2MRHT(cnt)
|
|
100 /*if (cnt > 0) C2MCALL(mychkmem)*/
|
|
101 break;
|
|
102
|
|
103 case '[':
|
|
104 /* special case 1: zero */
|
|
105 if (iprog[ipptr + 2] == ']' &&
|
|
106 (iprog[ipptr + 1] == '-' ||
|
|
107 (iprog[ipptr + 1] == '+' && wrapping))
|
|
108 ) {
|
|
109 C2MZERO()
|
|
110 ipptr += 3;
|
|
111 break;
|
|
112 }
|
|
113
|
|
114 /* start a suboptimize */
|
|
115 a = (int) (prog + pptr);
|
|
116 pptr += C2MLOS;
|
|
117 ipptr++;
|
|
118 c2m();
|
|
119
|
|
120 /* check for addto * /
|
|
121 if (pptr == a + 5 && optaddto(a)) {
|
|
122 ipptr++;
|
|
123 break;
|
|
124 }*/
|
|
125
|
|
126 /* make the jump */
|
|
127 C2MLO(a)
|
|
128 C2MLC(a)
|
|
129 ipptr++;
|
|
130 break;
|
|
131
|
|
132 case ']':
|
|
133 /* this optimization session is over */
|
|
134 return;
|
|
135
|
|
136 case '.':
|
|
137 /* not much we can optimize here ... */
|
|
138 C2MCALL(myputchar)
|
|
139 ipptr++;
|
|
140 break;
|
|
141
|
|
142 case ',':
|
|
143 /* here neither ... */
|
|
144 C2MCALL(mygetchar)
|
|
145 ipptr++;
|
|
146 break;
|
|
147
|
|
148 case '#':
|
|
149 /* still no optimizations! */
|
|
150 if (debug) C2MCALL(mydebug)
|
|
151 ipptr++;
|
|
152 break;
|
|
153
|
|
154 default:
|
|
155 ipptr++;
|
|
156 break;
|
|
157 }
|
|
158 }
|
|
159
|
|
160 /* if we finished making the optimized program, this is the end */
|
|
161 C2MTAIL()
|
|
162
|
|
163 tprog();
|
|
164 }
|