Mercurial > repos > rhope
comparison test/fib.c @ 66:d4b44ae2e34a
New variant of C backend works now
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 06 Jun 2010 20:29:10 -0400 |
parents | b71bab34ec65 |
children |
comparison
equal
deleted
inserted
replaced
65:1db811fa4744 | 66:d4b44ae2e34a |
---|---|
2 #include "builtin.h" | 2 #include "builtin.h" |
3 #include "object.h" | 3 #include "object.h" |
4 #include "context.h" | 4 #include "context.h" |
5 #include "func.h" | 5 #include "func.h" |
6 #include "integer.h" | 6 #include "integer.h" |
7 | 7 #include "bool.h" |
8 typedef struct | 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_Fib_1, | |
18 FUNC_Fib_2, | |
19 FUNC_Fib_3, | |
20 FUNC_Fib_4, | |
21 FUNC_Fib_5, | |
22 FUNC_Fib_6, | |
23 FUNC_If, | |
24 FUNC_If_AT_Yes_SP_No, | |
25 FUNC_Main, | |
26 FUNC_Main_1, | |
27 FUNC_Main_2, | |
28 FUNC_Print, | |
29 END | |
30 } funcids; | |
31 | |
32 #define DISPATCH switch(func) { \ | |
33 case FUNC__LT_: goto f__LT_;\ | |
34 case FUNC__LT__AT_Int32: goto f__LT__AT_Int32;\ | |
35 case FUNC__MN_: goto f__MN_;\ | |
36 case FUNC__MN__AT_Int32: goto f__MN__AT_Int32;\ | |
37 case FUNC__PL_: goto f__PL_;\ | |
38 case FUNC__PL__AT_Int32: goto f__PL__AT_Int32;\ | |
39 case FUNC_Fib: goto f_Fib;\ | |
40 case FUNC_Fib_1: goto r1_Fib;\ | |
41 case FUNC_Fib_2: goto r2_Fib;\ | |
42 case FUNC_Fib_3: goto r3_Fib;\ | |
43 case FUNC_Fib_4: goto r4_Fib;\ | |
44 case FUNC_Fib_5: goto r5_Fib;\ | |
45 case FUNC_Fib_6: goto r6_Fib;\ | |
46 case FUNC_If: goto f_If;\ | |
47 case FUNC_If_AT_Yes_SP_No: goto f_If_AT_Yes_SP_No;\ | |
48 case FUNC_Main: goto f_Main;\ | |
49 case FUNC_Main_1: goto r1_Main;\ | |
50 case FUNC_Main_2: goto r2_Main;\ | |
51 case FUNC_Print: goto f_Print;\ | |
52 case END: goto DO_END;\ | |
53 } | |
54 | |
55 | |
56 typedef struct { | |
57 struct object *____result__0__0; | |
58 struct object *____result__0__1; | |
59 struct object *____result__10__0; | |
60 struct object *____result__11__0; | |
61 struct object *____result__1__0; | |
62 struct object *____result__5__0; | |
63 struct object *____result__6__0; | |
64 struct object *____result__7__0; | |
65 struct object *out; | |
66 } lt_Fib; | |
67 | |
68 typedef struct { | |
69 struct object *____result__0__0; | |
70 struct object *____result__1__0; | |
71 } lt_Main; | |
72 | |
73 object * _const_Whole_SP_Number__1; | |
74 object * _const_Whole_SP_Number__2; | |
75 object * _const_Whole_SP_Number__30; | |
76 | |
77 void rhope(uint32_t func) | |
9 { | 78 { |
10 object * work1; | 79 uint16_t resume,idx; |
11 object * work2; | 80 context * ct; |
12 object * out; | 81 calldata * cdata, *temp_cdata; |
13 } fib_t; | 82 FuncDef(Fib) |
14 | 83 FuncDef(Main) |
15 object * const_1; | 84 |
16 object * const_2; | 85 ct = new_context(); |
17 | 86 cdata = alloc_cdata(ct, NULL, 0); |
87 cdata->num_params = 0; | |
88 cdata->func = END; | |
89 DISPATCH | |
90 Method(_LT_) | |
91 MethodDispatch(TYPE_INT32,_LT_,Int32) | |
92 EndMethod | |
93 | |
94 MethodImplNoLocals(_LT_,Int32,TYPE_INT32, | |
95 NumParams 2) | |
96 | |
97 Param(1, TYPE_INT32) | |
98 | |
99 if(((t_Int32 *)cdata->params[0])->num < ((t_Int32 *)cdata->params[-1])->num) | |
100 { | |
101 release_ref(cdata->params[0]); | |
102 Ret(0, Yes) | |
103 } else { | |
104 release_ref(cdata->params[0]); | |
105 Ret(0, No) | |
106 } | |
107 | |
108 release_ref(cdata->params[-1]); | |
109 EndFuncNoLocals | |
110 DISPATCH | |
111 | |
112 Method(_MN_) | |
113 MethodDispatch(TYPE_INT32,_MN_,Int32) | |
114 EndMethod | |
115 DISPATCH | |
116 | |
117 MethodImplNoLocals(_MN_,Int32,TYPE_INT32, | |
118 NumParams 2) | |
119 | |
120 Param(1, TYPE_INT32) | |
121 | |
122 cdata->params[0] = copy_object(cdata->params[0]); | |
123 ((t_Int32 *)cdata->params[0])->num -= ((t_Int32 *)cdata->params[-1])->num; | |
124 | |
125 release_ref(cdata->params[-1]); | |
126 EndFuncNoLocals | |
127 DISPATCH | |
128 | |
129 Method(_PL_) | |
130 MethodDispatch(TYPE_INT32,_PL_,Int32) | |
131 EndMethod | |
132 DISPATCH | |
133 | |
134 MethodImplNoLocals(_PL_,Int32,TYPE_INT32, | |
135 NumParams 2) | |
136 | |
137 Param(1, TYPE_INT32) | |
138 | |
139 cdata->params[0] = copy_object(cdata->params[0]); | |
140 ((t_Int32 *)cdata->params[0])->num += ((t_Int32 *)cdata->params[-1])->num; | |
141 | |
142 release_ref(cdata->params[-1]); | |
143 EndFuncNoLocals | |
144 DISPATCH | |
145 | |
18 Func(Fib, | 146 Func(Fib, |
19 NumParams 1, | 147 NumParams 1) |
20 CallSpace 2, | 148 lv_Fib->____result__0__0 = NULL; |
21 fib_t); | 149 lv_Fib->____result__0__1 = NULL; |
22 | 150 lv_Fib->____result__1__0 = NULL; |
23 locals->out = NULL; | 151 lv_Fib->____result__5__0 = NULL; |
24 | 152 lv_Fib->____result__6__0 = NULL; |
25 | 153 lv_Fib->____result__7__0 = NULL; |
26 call->params[0] = add_ref(cdata->params[0]); | 154 lv_Fib->____result__10__0 = NULL; |
27 call->params[1] = add_ref(const_2); | 155 lv_Fib->____result__11__0 = NULL; |
28 MCall(METHOD_LESS, 2) | 156 lv_Fib->out = NULL; |
29 | 157 |
30 MCall(METHOD_IF, 1) | 158 PrepCall(2) |
31 | 159 |
32 if(call->params[0]) | 160 SetParam(0, add_ref(cdata->lastframe->params[0])) |
33 { | 161 SetParam(1, add_ref(_const_Whole_SP_Number__2)) |
34 release_ref(call->params[0]); | 162 Call(_LT_, |
35 locals->out = add_ref(const_1); | 163 NumParams 2, 1, Fib) |
36 } | 164 lv_Fib->____result__1__0 = cdata->params[0]; |
37 | 165 |
38 if(call->params[1]) | 166 SetParam(0, add_ref(lv_Fib->____result__1__0)) |
39 { | 167 Call(If, |
40 release_ref(call->params[1]); | 168 NumParams 1, 2, Fib) |
41 call->params[0] = add_ref(cdata->params[0]); | 169 lv_Fib->____result__0__0 = cdata->params[0]; |
42 call->params[1] = add_ref(const_1); | 170 lv_Fib->____result__0__1 = cdata->params[-1]; |
43 MCall(METHOD_SUB, 2) | 171 |
44 locals->work1 = call->params[0]; | 172 if(lv_Fib->____result__0__1) |
173 { | |
174 SetParam(0, add_ref(cdata->lastframe->params[0])) | |
175 SetParam(1, add_ref(_const_Whole_SP_Number__1)) | |
176 Call(_MN_, | |
177 NumParams 2, 3, Fib) | |
178 lv_Fib->____result__7__0 = cdata->params[0]; | |
179 } | |
180 if(lv_Fib->____result__0__1) | |
181 { | |
182 SetParam(0, add_ref(cdata->lastframe->params[0])) | |
183 SetParam(1, add_ref(_const_Whole_SP_Number__2)) | |
184 Call(_MN_, | |
185 NumParams 2, 4, Fib) | |
186 lv_Fib->____result__11__0 = cdata->params[0]; | |
187 } | |
188 if(lv_Fib->____result__0__0) | |
189 { | |
190 lv_Fib->out = add_ref(_const_Whole_SP_Number__1); | |
191 } | |
192 if(lv_Fib->____result__0__1) | |
193 { | |
194 SetParam(0, add_ref(lv_Fib->____result__7__0)) | |
195 Call(Fib, | |
196 NumParams 1, 5, Fib) | |
197 lv_Fib->____result__6__0 = cdata->params[0]; | |
198 } | |
199 if(lv_Fib->____result__0__1) | |
200 { | |
201 SetParam(0, add_ref(lv_Fib->____result__11__0)) | |
202 Call(Fib, | |
203 NumParams 1, 6, Fib) | |
204 lv_Fib->____result__10__0 = cdata->params[0]; | |
205 } | |
206 if(lv_Fib->____result__0__1) | |
207 { | |
208 if(lv_Fib->____result__0__0) | |
209 { | |
210 release_ref(lv_Fib->____result__0__0); | |
211 } | |
212 if(lv_Fib->____result__0__1) | |
213 { | |
214 release_ref(lv_Fib->____result__0__1); | |
215 } | |
216 if(lv_Fib->____result__1__0) | |
217 { | |
218 release_ref(lv_Fib->____result__1__0); | |
219 } | |
220 if(lv_Fib->____result__7__0) | |
221 { | |
222 release_ref(lv_Fib->____result__7__0); | |
223 } | |
224 if(lv_Fib->____result__11__0) | |
225 { | |
226 release_ref(lv_Fib->____result__11__0); | |
227 } | |
228 TPrepCall(2) | |
229 SetParam(0, lv_Fib->____result__6__0) | |
230 SetParam(1, lv_Fib->____result__10__0) | |
231 TCall(_PL_, | |
232 NumParams 2) | |
233 //lv_Fib->____result__5__0 = cdata->params[0]; | |
234 } | |
235 FreeCall | |
236 if(lv_Fib->____result__0__1) | |
237 { | |
238 lv_Fib->out = add_ref(lv_Fib->____result__5__0); | |
239 } | |
240 if(lv_Fib->____result__0__0) | |
241 { | |
242 release_ref(lv_Fib->____result__0__0); | |
243 } | |
244 if(lv_Fib->____result__0__1) | |
245 { | |
246 release_ref(lv_Fib->____result__0__1); | |
247 } | |
248 if(lv_Fib->____result__1__0) | |
249 { | |
250 release_ref(lv_Fib->____result__1__0); | |
251 } | |
252 if(lv_Fib->____result__5__0) | |
253 { | |
254 release_ref(lv_Fib->____result__5__0); | |
255 } | |
256 if(lv_Fib->____result__6__0) | |
257 { | |
258 release_ref(lv_Fib->____result__6__0); | |
259 } | |
260 if(lv_Fib->____result__7__0) | |
261 { | |
262 release_ref(lv_Fib->____result__7__0); | |
263 } | |
264 if(lv_Fib->____result__10__0) | |
265 { | |
266 release_ref(lv_Fib->____result__10__0); | |
267 } | |
268 if(lv_Fib->____result__11__0) | |
269 { | |
270 release_ref(lv_Fib->____result__11__0); | |
271 } | |
272 release_ref(cdata->params[0]); | |
273 Ret(0, lv_Fib->out) | |
274 EndFunc(Fib) | |
275 DISPATCH | |
276 | |
277 Method(If) | |
278 MethodDispatch(TYPE_BOOLEAN,If,Yes_SP_No) | |
279 EndMethod | |
45 | 280 |
46 call->params[0] = add_ref(cdata->params[0]); | 281 MethodImplNoLocals(If,Yes_SP_No,TYPE_BOOLEAN, |
47 call->params[1] = add_ref(const_2); | 282 NumParams 1) |
48 MCall(METHOD_SUB, 2) | 283 |
49 locals->work2 = call->params[0]; | 284 if(((t_Boolean *)(cdata->params[0]))->val) |
50 | 285 { |
51 call->params[0] = locals->work1; | 286 Ret(1, NULL) |
52 Call(Fib, 1) | 287 } else { |
53 locals->work1 = call->params[0]; | 288 Ret(1, cdata->params[0]); |
54 | 289 Ret(0, NULL) |
55 call->params[0] = locals->work2; | 290 } |
56 Call(Fib, 1) | 291 EndFuncNoLocals |
57 locals->work2 = call->params[0]; | 292 DISPATCH |
58 | 293 |
59 if(!locals->out) | 294 Func(Main, |
60 { | 295 NumParams 0) |
61 release_ref(cdata->params[0]); | 296 |
62 cdata->params[0] = locals->work1; | 297 lv_Main->____result__0__0 = NULL; |
63 cdata->params[1] = locals->work2; | 298 lv_Main->____result__1__0 = NULL; |
64 TMCall(METHOD_ADD, 2) | 299 |
65 } | 300 PrepCall(1) |
301 SetParam(0, add_ref(_const_Whole_SP_Number__30)) | |
302 Call(Fib, | |
303 NumParams 1, 1, Main) | |
304 lv_Main->____result__1__0 = cdata->params[0]; | |
305 FreeCall | |
306 | |
307 PrepCall(1) | |
308 SetParam(0, add_ref(lv_Main->____result__1__0)) | |
309 Call(Print, | |
310 NumParams 1, 2, Main) | |
311 lv_Main->____result__0__0 = cdata->params[0]; | |
312 FreeCall | |
313 | |
314 if(lv_Main->____result__0__0) | |
315 { | |
316 release_ref(lv_Main->____result__0__0); | |
317 } | |
318 if(lv_Main->____result__1__0) | |
319 { | |
320 release_ref(lv_Main->____result__1__0); | |
321 } | |
322 EndFunc(Main) | |
323 DISPATCH | |
324 | |
325 FuncNoLocals(Print, | |
326 NumParams 1) | |
327 | |
328 if(get_blueprint(cdata->params[0])->type_id == TYPE_INT32) | |
329 { | |
330 printf("%d\n", ((t_Int32 *)(cdata->params[0]))->num); | |
331 } else { | |
332 puts("Don't know how to print this type"); | |
66 } | 333 } |
67 release_ref(cdata->params[0]); | 334 release_ref(cdata->params[0]); |
68 Ret(0, locals->out) | 335 Ret(0, make_Int32(0)) |
69 | 336 EndFuncNoLocals |
70 EndFunc | 337 DISPATCH |
71 | 338 |
72 FuncNoLocals(Main, | 339 DO_END: |
73 NumParams 1, | 340 return; |
74 CallSpace 2) | 341 |
75 | 342 _exception: |
76 TCall(Fib, 1) | 343 puts("whoops, exception!"); |
77 EndFunc | 344 } |
78 | |
79 #ifdef _WIN32 | |
80 #include "windows.h" | |
81 #endif | |
82 | 345 |
83 int main(int argc, char ** argv) | 346 int main(int argc, char ** argv) |
84 { | 347 { |
85 returntype ret; | |
86 calldata *cdata; | |
87 context * ct; | |
88 #ifdef _WIN32 | |
89 DWORD dur; | |
90 dur = timeGetTime(); | |
91 #endif | |
92 if(argc < 2) | |
93 { | |
94 puts("not enought arguments"); | |
95 return -1; | |
96 } | |
97 register_builtin_types(); | 348 register_builtin_types(); |
98 ct = new_context(); | 349 |
99 cdata = alloc_cdata(ct, 1); | 350 _const_Whole_SP_Number__1 = make_Int32(1); |
100 cdata->num_params = 1; | 351 _const_Whole_SP_Number__2 = make_Int32(2); |
101 cdata->resume = 0; | 352 _const_Whole_SP_Number__30 = make_Int32(30); |
102 | 353 |
103 const_1 = make_Int32(1); | 354 rhope(FUNC_Main); |
104 const_2 = make_Int32(2); | |
105 cdata->params[0] = make_Int32(atoi(argv[1])); | |
106 ret = f_Main(cdata); | |
107 while(ret == TAIL_RETURN) | |
108 ret = cdata->tail_func(cdata); | |
109 if(ret == EXCEPTION_RETURN) { | |
110 puts("Exception!"); | |
111 return -1; | |
112 } | |
113 printf("Result: %d\n", ((t_Int32 *)cdata->params[0])->num); | |
114 #ifdef _WIN32 | |
115 dur = timeGetTime()-dur; | |
116 printf("Took %d milliseconds\n", dur); | |
117 #endif | |
118 return 0; | 355 return 0; |
119 } | 356 } |
120 |