996
|
1 /***************************************************************************
|
|
2
|
|
3 NAME
|
|
4 ecto_b98.c - glue code for linking .b98 files with .i files
|
|
5
|
|
6 LICENSE TERMS
|
|
7 Copyright (C) 2008 Alex Smith
|
|
8
|
|
9 This program is free software; you can redistribute it and/or modify
|
|
10 it under the terms of the GNU General Public License as published by
|
|
11 the Free Software Foundation; either version 2 of the License, or
|
|
12 (at your option) any later version.
|
|
13
|
|
14 This program is distributed in the hope that it will be useful,
|
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 GNU General Public License for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with this program; if not, write to the Free Software
|
|
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22
|
|
23 Note that this code is designed to be linked against a GPLv3 library
|
|
24 (producing a GPLv3 output), and therefore it is recommended that
|
|
25 modifications to this code are compatible with GPLv3, although this
|
|
26 is not a legal requirement.
|
|
27
|
|
28 ***************************************************************************/
|
|
29
|
|
30 /* This is an expansion library designed to link with the Funge-98 interpreter
|
|
31 cfunge (when compiled as a library); this is glue code which hooks into the
|
|
32 INTERCAL control structure, and uses functions in glue code which hooks
|
|
33 into the Funge control structure in cfunge. The Befunge program itself that
|
|
34 is interpreted is stored in yet another .cio file, and accessed via the
|
|
35 extern variables ick_iffi_befungeString, ick_iffi_markerposns, and
|
|
36 ick_iffi_markercount. The name ecto_b98 is magical, and causes this library
|
|
37 to be included when a .b98 files is encountered on the command line (as
|
|
38 well as the cfunge library itself, which has the magical name
|
|
39 libick_ecto_b98.a).
|
|
40
|
|
41 The library itself works by using label names that have a special meaning
|
|
42 to ick, along with ICK_EC_FUNCs, to cause a compiled C-INTERCAL program to
|
|
43 also search in the Befunge program for line labels, COME FROMs, and NEXT
|
|
44 FROMs, as well as in the INTERCAL program itself. It also allows the
|
|
45 Befunge program to use INTERCAL control flow structures (even to itself),
|
|
46 by monitoring flags set in a Funge-98 fingerprint (IFFI, which accounts
|
|
47 for most of the variable names) and simulating the required structure
|
|
48 (such as FORGET or NEXT) somewhere where cfunge itself will be unaffected
|
|
49 and unaware of what is going on.
|
|
50
|
|
51 In general, INTERCAL control flow structures affect only the position and
|
|
52 delta of the IP in the Befunge program, not anything else. (In particular,
|
|
53 the Befunge stack is unaffected.)
|
|
54 */
|
|
55
|
|
56 #include <stdio.h>
|
|
57 #include <assert.h>
|
|
58 #include <ick_ec.h>
|
|
59
|
|
60 extern int ick_printflow;
|
|
61
|
|
62 struct ick_ipposdeltatype
|
|
63 {
|
|
64 long long ix, iy, dx, dy;
|
|
65 };
|
|
66
|
|
67 /* This is called from IFFI. Remember to keep matching
|
|
68 extern over there in sync. */
|
|
69 void ick_interpreter_main_loop(void);
|
|
70
|
|
71 /* This is just a forward decl. */
|
|
72 void ick_iffi_handle_control(void);
|
|
73
|
|
74 /* These are implemented in IFFI. */
|
|
75 extern void ick_interpreter_run(void);
|
|
76 extern void ick_iffi_interpreter_one_iteration(void);
|
|
77 extern void ick_save_ip_pos_delta(struct ick_ipposdeltatype*);
|
|
78 extern void ick_restore_ip_pos_delta(const struct ick_ipposdeltatype*);
|
|
79
|
|
80 unsigned short ick_iffi_forgetcount=0;
|
|
81 int ick_iffi_nexting=0;
|
|
82 int ick_iffi_comingfrom=0;
|
|
83 int ick_iffi_nextingfrom=0;
|
|
84 int ick_iffi_sucking=0;
|
|
85 int ick_iffi_resuming=0;
|
|
86 uint32_t ick_iffi_linelabel=0;
|
|
87 int ick_iffi_breakloop=0; /* becomes 1 when IFFI's loaded or mark-mode ends */
|
|
88 int ick_iffi_inmarkmode=0;
|
|
89
|
|
90 extern const unsigned char* ick_iffi_befungeString;
|
|
91 extern long long ick_iffi_markerposns[][2];
|
|
92 extern int ick_iffi_markercount;
|
|
93
|
|
94 ICK_EC_FUNC_START(ick_interpreter_main_loop)
|
|
95 {
|
|
96 ick_startup( {ick_interpreter_run();} ); /* could be put anywhere, is put here
|
|
97 for convenience */
|
|
98
|
|
99 /* we enter here when running at startup code and continue until IFFI is loaded */
|
|
100
|
|
101 if(0)
|
|
102 {
|
|
103 ick_l2_ICK_EC_PP_2: /* this will be automatically replaced with a valid label,
|
|
104 as will the next occurence of it, by ick when it does
|
|
105 linking (i.e. this is not an infinite loop). */
|
|
106 if(ick_global_linelabel != 0x70001ff1 &&
|
|
107 ick_global_linelabel != 0x70001ff2)
|
|
108 goto ick_l2_ICK_EC_PP_2;
|
|
109 /* If control reaches this point, a COME FROM or NEXT has just completed,
|
|
110 and the IP already in the correct place. Continue, outside markmode. */
|
|
111 ick_iffi_nexting = 0;
|
|
112 ick_iffi_comingfrom = 0;
|
|
113 ick_iffi_nextingfrom = 0;
|
|
114 ick_iffi_sucking = 0;
|
|
115 ick_iffi_inmarkmode = 0;
|
|
116 ick_global_checkmode = 0;
|
|
117 /* If a NEXT has just completed, do a suckpoint-check on that line label
|
|
118 (in case someone COMEs FROM the target of the NEXT). */
|
|
119 if(ick_global_linelabel == 0x70001ff2)
|
|
120 {
|
|
121 auto struct ick_ipposdeltatype ippd;
|
|
122 ick_save_ip_pos_delta(&ippd);
|
|
123 ick_iffi_sucking = 0;
|
|
124 ick_checksuckpoint(ick_iffi_linelabel); /* may not return */
|
|
125 ick_restore_ip_pos_delta(&ippd);
|
|
126 }
|
|
127 }
|
|
128
|
|
129 ick_iffi_iml_rerun:
|
|
130
|
|
131 ick_iffi_breakloop=0;
|
|
132 while(!ick_iffi_breakloop)
|
|
133 {
|
|
134 ick_iffi_interpreter_one_iteration();
|
|
135 if (ick_iffi_forgetcount && !ick_iffi_breakloop)
|
|
136 {
|
|
137 ick_forget(ick_iffi_forgetcount);
|
|
138 ick_iffi_forgetcount = 0;
|
|
139 }
|
|
140 }
|
|
141 /* There are several reasons why we could reach this point. Most of them,
|
|
142 we just fall off the end of the function and whatever called us handles
|
|
143 the program flow from there. However, if the loop broke due to a RESUME,
|
|
144 line label or NEXT, we need to handle it. */
|
|
145 if(ick_iffi_resuming)
|
|
146 {
|
|
147 unsigned short iifctemp = ick_iffi_forgetcount;
|
|
148 ick_iffi_resuming = 0;
|
|
149 ick_iffi_forgetcount = 0;
|
|
150 ick_resume(iifctemp); /* never returns */
|
|
151 }
|
|
152 if(ick_iffi_nexting)
|
|
153 {
|
|
154 /* To handle a NEXT, we need to save the relevant IP information (position
|
|
155 and delta, which are the only things saved/restored on the NEXT stack)
|
|
156 onto the C stack. This is done in a struct ick_ipposdatatype, using the
|
|
157 functions available for such save/restore. */
|
|
158 auto struct ick_ipposdeltatype ippd;
|
|
159 ick_save_ip_pos_delta(&ippd);
|
|
160 ick_iffi_nexting = 0;
|
|
161 ick_next(ick_iffi_linelabel); /* may not return */
|
|
162 ick_restore_ip_pos_delta(&ippd);
|
|
163 goto ick_iffi_iml_rerun; /* continue the main loop if we got here */
|
|
164 }
|
|
165 if(ick_iffi_sucking && ! ick_iffi_inmarkmode)
|
|
166 {
|
|
167 /* Handle a line-label encountered in normal execution. */
|
|
168 auto struct ick_ipposdeltatype ippd;
|
|
169 ick_save_ip_pos_delta(&ippd);
|
|
170 ick_iffi_sucking = 0;
|
|
171 ick_checksuckpoint(ick_iffi_linelabel); /* may not return */
|
|
172 ick_restore_ip_pos_delta(&ippd);
|
|
173 goto ick_iffi_iml_rerun; /* continue the main loop if we got here */
|
|
174 }
|
|
175 }
|
|
176 ICK_EC_FUNC_END
|
|
177
|
|
178 ICK_EC_FUNC_START(ick_iffi_handle_control)
|
|
179 {
|
|
180 static int recursing=0;
|
|
181 int markerno, dirno, nextcheck;
|
|
182 struct ick_ipposdeltatype ippd;
|
|
183
|
|
184 /* The label names here are magical internal identifiers. That means that this
|
|
185 is not in fact an infinite loop, but is instead a handler for sucking in
|
|
186 from line labels. The 0x70001ff1 is a magic number used specifically by
|
|
187 this code. */
|
|
188 if(0)
|
|
189 {
|
|
190 ick_l1_ICK_EC_PP_1:
|
|
191 if(ick_global_linelabel > 65535)
|
|
192 goto skipsearch; /* there can't be a match, don't check to avoid recursion */
|
|
193 nextcheck = 0;
|
|
194 if(ick_printflow)
|
|
195 fprintf(stderr,"[cfcheck:%d]\n",(int)ick_global_linelabel);
|
|
196 }
|
|
197
|
|
198 if(0)
|
|
199 {
|
|
200 ick_l2_ICK_EC_PP_2:
|
|
201 if(ick_global_linelabel > 65535)
|
|
202 goto skipsearch; /* there can't be a match, don't check to avoid recursion */
|
|
203 nextcheck = 1;
|
|
204 if(ick_printflow)
|
|
205 fprintf(stderr,"[llcheck:%d]\n",(int)ick_global_linelabel);
|
|
206 }
|
|
207
|
|
208 assert(!recursing);
|
|
209
|
|
210 recursing=1;
|
|
211
|
|
212 ick_save_ip_pos_delta(&ippd);
|
|
213
|
|
214 markerno=ick_iffi_markercount;
|
|
215 while(markerno--)
|
|
216 {
|
|
217 dirno=4;
|
|
218 while(dirno--)
|
|
219 {
|
|
220 struct ick_ipposdeltatype tippd;
|
|
221 tippd.ix=ick_iffi_markerposns[markerno][0];
|
|
222 tippd.iy=ick_iffi_markerposns[markerno][1];
|
|
223 tippd.dx=(dirno == 3 ? -1 : dirno == 1 ? 1 : 0);
|
|
224 tippd.dy=(dirno == 0 ? -1 : dirno == 2 ? 1 : 0);
|
|
225 if(ick_printflow)
|
|
226 fprintf(stderr, "Checking for %s at (%lld, %lld) going %c...\n",
|
|
227 nextcheck ? "line label" : "COME FROM/NEXT FROM",
|
|
228 tippd.ix, tippd.iy, dirno["^>v<"]);
|
|
229 tippd.ix += tippd.dx;
|
|
230 tippd.iy += tippd.dy;
|
|
231 ick_restore_ip_pos_delta(&tippd);
|
|
232 ick_iffi_inmarkmode = 1;
|
|
233 ick_local_checkmode = ick_global_checkmode;
|
|
234 ick_global_checkmode = 0;
|
|
235 ick_interpreter_main_loop();
|
|
236 ick_global_checkmode = ick_local_checkmode;
|
|
237 ick_iffi_inmarkmode = 0;
|
|
238 if((ick_iffi_nextingfrom||ick_iffi_comingfrom) &&
|
|
239 ick_iffi_linelabel == ick_global_linelabel && !nextcheck)
|
|
240 {
|
|
241 if(ick_printflow)
|
|
242 fprintf(stderr,"%s FROM found!\n",ick_iffi_comingfrom?"COME":"NEXT");
|
|
243 /* Error out on multiple COME FROM/NEXT FROM with the same target. */
|
|
244 if(ick_global_goto) ick_lose(ICK_IE555, -1, (char*)0);
|
|
245 ick_global_goto=0x70001FF1; /* we found a suckpoint */
|
|
246 if(ick_iffi_nextingfrom) ick_global_checkmode = 3; /* do a next from */
|
|
247 /* if it's a come-from, the checkmode is already correct */
|
|
248 ick_save_ip_pos_delta(&ippd);
|
|
249 }
|
|
250 if(ick_iffi_sucking && ick_iffi_linelabel == ick_global_linelabel &&
|
|
251 nextcheck)
|
|
252 {
|
|
253 if(ick_printflow)
|
|
254 fprintf(stderr,"Line label found!\n");
|
|
255 /* Another part of the code is NEXTing to this line label. */
|
|
256 ick_save_ip_pos_delta(&ippd);
|
|
257 nextcheck = 2; /* when this loop ends, goto the main loop */
|
|
258 }
|
|
259 ick_iffi_nextingfrom = 0;
|
|
260 ick_iffi_comingfrom = 0;
|
|
261 ick_iffi_sucking = 0;
|
|
262 }
|
|
263 }
|
|
264
|
|
265 if(ick_printflow) fprintf(stderr,"Checks finished.\n");
|
|
266
|
|
267 /* Set the IP to what we want it to be on resume, or otherwise return it to
|
|
268 its original value. (Returning it to its original value is unneccesary
|
|
269 but harmless.) */
|
|
270 ick_restore_ip_pos_delta(&ippd);
|
|
271 recursing=0;
|
|
272 ick_iffi_linelabel = ick_global_linelabel;
|
|
273 if(nextcheck == 2)
|
|
274 ick_dogoto(0x70001FF2,-1,0);
|
|
275 skipsearch:
|
|
276 if(nextcheck)
|
|
277 goto ick_l2_ICK_EC_PP_2;
|
|
278 else
|
|
279 goto ick_l1_ICK_EC_PP_1;
|
|
280 }
|
|
281 ICK_EC_FUNC_END
|