comparison runtime/fib.c @ 56:d2f9b0a9403d

Initial experiment with goto and switch
author Mike Pavone <pavone@retrodev.com>
date Thu, 08 Oct 2009 01:52:38 -0400
parents
children 2174878a6e4b
comparison
equal deleted inserted replaced
38:7f05bbe82f24 56:d2f9b0a9403d
1 #include <stdio.h>
2 #include "builtin.h"
3 #include "object.h"
4 #include "context.h"
5 #include "func.h"
6 #include "integer.h"
7 #include "bool.h"
8
9 typedef enum {
10 FUNC__LT_,
11 FUNC__LT__AT_Int32,
12 FUNC__MN_,
13 FUNC__MN__AT_Int32,
14 FUNC__PL_,
15 FUNC__PL__AT_Int32,
16 FUNC_Fib,
17 FUNC_If,
18 FUNC_If_AT_Yes_SP_No,
19 FUNC_Main,
20 FUNC_Print,
21 END
22 } funcids;
23
24 typedef struct {
25 struct object *____result__0__0;
26 struct object *____result__0__1;
27 struct object *____result__10__0;
28 struct object *____result__11__0;
29 struct object *____result__1__0;
30 struct object *____result__5__0;
31 struct object *____result__6__0;
32 struct object *____result__7__0;
33 struct object *out;
34 } lt_Fib;
35
36 typedef struct {
37 struct object *____result__0__0;
38 struct object *____result__1__0;
39 } lt_Main;
40
41 object * _const_Whole_SP_Number__1;
42 object * _const_Whole_SP_Number__2;
43 object * _const_Whole_SP_Number__30;
44
45 void rhope(uint32_t func)
46 {
47 uint16_t resume,idx;
48 context * ct;
49 calldata * cdata, *temp_cdata;
50 FuncDef(Fib)
51 FuncDef(Main)
52
53 ct = new_context();
54 cdata = alloc_cdata(ct, NULL, 0);
55 cdata->num_params = 0;
56 cdata->func = END;
57 resume = 0;
58 _dispatch:
59 switch(func)
60 {
61 Method(_LT_)
62 MethodDispatch(TYPE_INT32,_LT_,Int32)
63 EndMethod
64
65 MethodImplNoLocals(_LT_,Int32,TYPE_INT32,
66 NumParams 2)
67
68 Param(1, TYPE_INT32)
69
70 if(((t_Int32 *)cdata->params[0])->num < ((t_Int32 *)cdata->params[-1])->num)
71 {
72 release_ref(cdata->params[0]);
73 Ret(0, Yes)
74 } else {
75 release_ref(cdata->params[0]);
76 Ret(0, No)
77 }
78
79 release_ref(cdata->params[-1]);
80 EndFuncNoLocals
81
82 Method(_MN_)
83 MethodDispatch(TYPE_INT32,_MN_,Int32)
84 EndMethod
85
86 MethodImplNoLocals(_MN_,Int32,TYPE_INT32,
87 NumParams 2)
88
89 Param(1, TYPE_INT32)
90
91 cdata->params[0] = copy_object(cdata->params[0]);
92 ((t_Int32 *)cdata->params[0])->num -= ((t_Int32 *)cdata->params[-1])->num;
93
94 release_ref(cdata->params[-1]);
95 EndFuncNoLocals
96
97 Method(_PL_)
98 MethodDispatch(TYPE_INT32,_PL_,Int32)
99 EndMethod
100
101 MethodImplNoLocals(_PL_,Int32,TYPE_INT32,
102 NumParams 2)
103
104 Param(1, TYPE_INT32)
105
106 cdata->params[0] = copy_object(cdata->params[0]);
107 ((t_Int32 *)cdata->params[0])->num += ((t_Int32 *)cdata->params[-1])->num;
108
109 release_ref(cdata->params[-1]);
110 EndFuncNoLocals
111
112 Func(Fib,
113 NumParams 1)
114 lv_Fib->____result__0__0 = NULL;
115 lv_Fib->____result__0__1 = NULL;
116 lv_Fib->____result__1__0 = NULL;
117 lv_Fib->____result__5__0 = NULL;
118 lv_Fib->____result__6__0 = NULL;
119 lv_Fib->____result__7__0 = NULL;
120 lv_Fib->____result__10__0 = NULL;
121 lv_Fib->____result__11__0 = NULL;
122 lv_Fib->out = NULL;
123
124 PrepCall(2)
125 SetParam(0, add_ref(cdata->lastframe->params[0]))
126 SetParam(1, add_ref(_const_Whole_SP_Number__2))
127 Call(_LT_,
128 NumParams 2, 1, Fib)
129 lv_Fib->____result__1__0 = cdata->params[0];
130 FreeCall
131
132 PrepCall(2)
133 SetParam(0, add_ref(lv_Fib->____result__1__0))
134 Call(If,
135 NumParams 1, 2, Fib)
136 lv_Fib->____result__0__0 = cdata->params[0];
137 lv_Fib->____result__0__1 = cdata->params[-1];
138 FreeCall
139
140 if(lv_Fib->____result__0__1)
141 {
142 PrepCall(2)
143 SetParam(0, add_ref(cdata->lastframe->params[0]))
144 SetParam(1, add_ref(_const_Whole_SP_Number__1))
145 Call(_MN_,
146 NumParams 2, 3, Fib)
147 lv_Fib->____result__7__0 = cdata->params[0];
148 FreeCall
149 }
150 if(lv_Fib->____result__0__1)
151 {
152 PrepCall(2)
153 SetParam(0, add_ref(cdata->lastframe->params[0]))
154 SetParam(1, add_ref(_const_Whole_SP_Number__2))
155 Call(_MN_,
156 NumParams 2, 4, Fib)
157 lv_Fib->____result__11__0 = cdata->params[0];
158 FreeCall
159 }
160 if(lv_Fib->____result__0__0)
161 {
162 lv_Fib->out = add_ref(_const_Whole_SP_Number__1);
163 }
164 if(lv_Fib->____result__0__1)
165 {
166 PrepCall(1)
167 SetParam(0, add_ref(lv_Fib->____result__7__0))
168 Call(Fib,
169 NumParams 1, 5, Fib)
170 lv_Fib->____result__6__0 = cdata->params[0];
171 FreeCall
172 }
173 if(lv_Fib->____result__0__1)
174 {
175 PrepCall(1)
176 SetParam(0, add_ref(lv_Fib->____result__11__0))
177 Call(Fib,
178 NumParams 1, 6, Fib)
179 lv_Fib->____result__10__0 = cdata->params[0];
180 FreeCall
181 }
182 if(lv_Fib->____result__0__1)
183 {
184 PrepCall(2)
185 SetParam(0, add_ref(lv_Fib->____result__6__0))
186 SetParam(1, add_ref(lv_Fib->____result__10__0))
187 Call(_PL_,
188 NumParams 2, 7, Fib)
189 lv_Fib->____result__5__0 = cdata->params[0];
190 FreeCall
191 }
192 if(lv_Fib->____result__0__1)
193 {
194 lv_Fib->out = add_ref(lv_Fib->____result__5__0);
195 }
196 if(lv_Fib->____result__0__0)
197 {
198 release_ref(lv_Fib->____result__0__0);
199 }
200 if(lv_Fib->____result__0__1)
201 {
202 release_ref(lv_Fib->____result__0__1);
203 }
204 if(lv_Fib->____result__1__0)
205 {
206 release_ref(lv_Fib->____result__1__0);
207 }
208 if(lv_Fib->____result__5__0)
209 {
210 release_ref(lv_Fib->____result__5__0);
211 }
212 if(lv_Fib->____result__6__0)
213 {
214 release_ref(lv_Fib->____result__6__0);
215 }
216 if(lv_Fib->____result__7__0)
217 {
218 release_ref(lv_Fib->____result__7__0);
219 }
220 if(lv_Fib->____result__10__0)
221 {
222 release_ref(lv_Fib->____result__10__0);
223 }
224 if(lv_Fib->____result__11__0)
225 {
226 release_ref(lv_Fib->____result__11__0);
227 }
228 release_ref(cdata->params[0]);
229 Ret(0, lv_Fib->out)
230 EndFunc(Fib)
231
232 Method(If)
233 MethodDispatch(TYPE_BOOLEAN,If,Yes_SP_No)
234 EndMethod
235
236 MethodImplNoLocals(If,Yes_SP_No,TYPE_BOOLEAN,
237 NumParams 1)
238
239 if(((t_Boolean *)(cdata->params[0]))->val)
240 {
241 Ret(1, NULL)
242 } else {
243 Ret(1, cdata->params[0]);
244 Ret(0, NULL)
245 }
246 EndFuncNoLocals
247
248 Func(Main,
249 NumParams 0)
250
251 lv_Main->____result__0__0 = NULL;
252 lv_Main->____result__1__0 = NULL;
253
254 PrepCall(1)
255 SetParam(0, add_ref(_const_Whole_SP_Number__30))
256 Call(Fib,
257 NumParams 1, 1, Main)
258 lv_Main->____result__1__0 = cdata->params[0];
259 FreeCall
260
261 PrepCall(1)
262 SetParam(0, add_ref(lv_Main->____result__1__0))
263 Call(Print,
264 NumParams 1, 2, Main)
265 lv_Main->____result__0__0 = cdata->params[0];
266 FreeCall
267
268 if(lv_Main->____result__0__0)
269 {
270 release_ref(lv_Main->____result__0__0);
271 }
272 if(lv_Main->____result__1__0)
273 {
274 release_ref(lv_Main->____result__1__0);
275 }
276 EndFunc(Main)
277
278 FuncNoLocals(Print,
279 NumParams 1)
280
281 if(get_blueprint(cdata->params[0])->type_id == TYPE_INT32)
282 {
283 printf("%d\n", ((t_Int32 *)(cdata->params[0]))->num);
284 } else {
285 puts("Don't know how to print this type");
286 }
287 release_ref(cdata->params[0]);
288 Ret(0, make_Int32(0))
289 EndFuncNoLocals
290
291 case END:
292 return;
293 }
294 _exception:
295 puts("whoops, exception!");
296 }
297
298 int main(int argc, char ** argv)
299 {
300 register_builtin_types();
301
302 _const_Whole_SP_Number__1 = make_Int32(1);
303 _const_Whole_SP_Number__2 = make_Int32(2);
304 _const_Whole_SP_Number__30 = make_Int32(30);
305
306 rhope(FUNC_Main);
307 return 0;
308 }