Mercurial > repos > rhope
diff 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 |
line wrap: on
line diff
--- a/test/fib.c Tue Jun 01 01:13:54 2010 -0400 +++ b/test/fib.c Sun Jun 06 20:29:10 2010 -0400 @@ -4,117 +4,353 @@ #include "context.h" #include "func.h" #include "integer.h" +#include "bool.h" -typedef struct -{ - object * work1; - object * work2; - object * out; -} fib_t; +typedef enum { + FUNC__LT_, + FUNC__LT__AT_Int32, + FUNC__MN_, + FUNC__MN__AT_Int32, + FUNC__PL_, + FUNC__PL__AT_Int32, + FUNC_Fib, + FUNC_Fib_1, + FUNC_Fib_2, + FUNC_Fib_3, + FUNC_Fib_4, + FUNC_Fib_5, + FUNC_Fib_6, + FUNC_If, + FUNC_If_AT_Yes_SP_No, + FUNC_Main, + FUNC_Main_1, + FUNC_Main_2, + FUNC_Print, + END +} funcids; -object * const_1; -object * const_2; +#define DISPATCH switch(func) { \ + case FUNC__LT_: goto f__LT_;\ + case FUNC__LT__AT_Int32: goto f__LT__AT_Int32;\ + case FUNC__MN_: goto f__MN_;\ + case FUNC__MN__AT_Int32: goto f__MN__AT_Int32;\ + case FUNC__PL_: goto f__PL_;\ + case FUNC__PL__AT_Int32: goto f__PL__AT_Int32;\ + case FUNC_Fib: goto f_Fib;\ + case FUNC_Fib_1: goto r1_Fib;\ + case FUNC_Fib_2: goto r2_Fib;\ + case FUNC_Fib_3: goto r3_Fib;\ + case FUNC_Fib_4: goto r4_Fib;\ + case FUNC_Fib_5: goto r5_Fib;\ + case FUNC_Fib_6: goto r6_Fib;\ + case FUNC_If: goto f_If;\ + case FUNC_If_AT_Yes_SP_No: goto f_If_AT_Yes_SP_No;\ + case FUNC_Main: goto f_Main;\ + case FUNC_Main_1: goto r1_Main;\ + case FUNC_Main_2: goto r2_Main;\ + case FUNC_Print: goto f_Print;\ + case END: goto DO_END;\ +} + -Func(Fib, - NumParams 1, - CallSpace 2, - fib_t); - - locals->out = NULL; - +typedef struct { + struct object *____result__0__0; + struct object *____result__0__1; + struct object *____result__10__0; + struct object *____result__11__0; + struct object *____result__1__0; + struct object *____result__5__0; + struct object *____result__6__0; + struct object *____result__7__0; + struct object *out; +} lt_Fib; + +typedef struct { + struct object *____result__0__0; + struct object *____result__1__0; +} lt_Main; + +object * _const_Whole_SP_Number__1; +object * _const_Whole_SP_Number__2; +object * _const_Whole_SP_Number__30; + +void rhope(uint32_t func) +{ + uint16_t resume,idx; + context * ct; + calldata * cdata, *temp_cdata; +FuncDef(Fib) +FuncDef(Main) + + ct = new_context(); + cdata = alloc_cdata(ct, NULL, 0); + cdata->num_params = 0; + cdata->func = END; +DISPATCH +Method(_LT_) + MethodDispatch(TYPE_INT32,_LT_,Int32) +EndMethod + +MethodImplNoLocals(_LT_,Int32,TYPE_INT32, + NumParams 2) - call->params[0] = add_ref(cdata->params[0]); - call->params[1] = add_ref(const_2); - MCall(METHOD_LESS, 2) + Param(1, TYPE_INT32) - MCall(METHOD_IF, 1) - - if(call->params[0]) + if(((t_Int32 *)cdata->params[0])->num < ((t_Int32 *)cdata->params[-1])->num) { - release_ref(call->params[0]); - locals->out = add_ref(const_1); + release_ref(cdata->params[0]); + Ret(0, Yes) + } else { + release_ref(cdata->params[0]); + Ret(0, No) } - if(call->params[1]) + release_ref(cdata->params[-1]); +EndFuncNoLocals +DISPATCH + +Method(_MN_) + MethodDispatch(TYPE_INT32,_MN_,Int32) +EndMethod +DISPATCH + +MethodImplNoLocals(_MN_,Int32,TYPE_INT32, + NumParams 2) + + Param(1, TYPE_INT32) + + cdata->params[0] = copy_object(cdata->params[0]); + ((t_Int32 *)cdata->params[0])->num -= ((t_Int32 *)cdata->params[-1])->num; + + release_ref(cdata->params[-1]); +EndFuncNoLocals +DISPATCH + +Method(_PL_) + MethodDispatch(TYPE_INT32,_PL_,Int32) +EndMethod +DISPATCH + +MethodImplNoLocals(_PL_,Int32,TYPE_INT32, + NumParams 2) + + Param(1, TYPE_INT32) + + cdata->params[0] = copy_object(cdata->params[0]); + ((t_Int32 *)cdata->params[0])->num += ((t_Int32 *)cdata->params[-1])->num; + + release_ref(cdata->params[-1]); +EndFuncNoLocals +DISPATCH + +Func(Fib, + NumParams 1) + lv_Fib->____result__0__0 = NULL; + lv_Fib->____result__0__1 = NULL; + lv_Fib->____result__1__0 = NULL; + lv_Fib->____result__5__0 = NULL; + lv_Fib->____result__6__0 = NULL; + lv_Fib->____result__7__0 = NULL; + lv_Fib->____result__10__0 = NULL; + lv_Fib->____result__11__0 = NULL; + lv_Fib->out = NULL; + + PrepCall(2) + + SetParam(0, add_ref(cdata->lastframe->params[0])) + SetParam(1, add_ref(_const_Whole_SP_Number__2)) + Call(_LT_, + NumParams 2, 1, Fib) + lv_Fib->____result__1__0 = cdata->params[0]; + + SetParam(0, add_ref(lv_Fib->____result__1__0)) + Call(If, + NumParams 1, 2, Fib) + lv_Fib->____result__0__0 = cdata->params[0]; + lv_Fib->____result__0__1 = cdata->params[-1]; + + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(cdata->lastframe->params[0])) + SetParam(1, add_ref(_const_Whole_SP_Number__1)) + Call(_MN_, + NumParams 2, 3, Fib) + lv_Fib->____result__7__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(cdata->lastframe->params[0])) + SetParam(1, add_ref(_const_Whole_SP_Number__2)) + Call(_MN_, + NumParams 2, 4, Fib) + lv_Fib->____result__11__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__0) { - release_ref(call->params[1]); - call->params[0] = add_ref(cdata->params[0]); - call->params[1] = add_ref(const_1); - MCall(METHOD_SUB, 2) - locals->work1 = call->params[0]; - - call->params[0] = add_ref(cdata->params[0]); - call->params[1] = add_ref(const_2); - MCall(METHOD_SUB, 2) - locals->work2 = call->params[0]; - - call->params[0] = locals->work1; - Call(Fib, 1) - locals->work1 = call->params[0]; - - call->params[0] = locals->work2; - Call(Fib, 1) - locals->work2 = call->params[0]; - - if(!locals->out) + lv_Fib->out = add_ref(_const_Whole_SP_Number__1); + } + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(lv_Fib->____result__7__0)) + Call(Fib, + NumParams 1, 5, Fib) + lv_Fib->____result__6__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(lv_Fib->____result__11__0)) + Call(Fib, + NumParams 1, 6, Fib) + lv_Fib->____result__10__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__1) + { + if(lv_Fib->____result__0__0) + { + release_ref(lv_Fib->____result__0__0); + } + if(lv_Fib->____result__0__1) + { + release_ref(lv_Fib->____result__0__1); + } + if(lv_Fib->____result__1__0) + { + release_ref(lv_Fib->____result__1__0); + } + if(lv_Fib->____result__7__0) + { + release_ref(lv_Fib->____result__7__0); + } + if(lv_Fib->____result__11__0) { - release_ref(cdata->params[0]); - cdata->params[0] = locals->work1; - cdata->params[1] = locals->work2; - TMCall(METHOD_ADD, 2) + release_ref(lv_Fib->____result__11__0); } + TPrepCall(2) + SetParam(0, lv_Fib->____result__6__0) + SetParam(1, lv_Fib->____result__10__0) + TCall(_PL_, + NumParams 2) + //lv_Fib->____result__5__0 = cdata->params[0]; + } + FreeCall + if(lv_Fib->____result__0__1) + { + lv_Fib->out = add_ref(lv_Fib->____result__5__0); + } + if(lv_Fib->____result__0__0) + { + release_ref(lv_Fib->____result__0__0); + } + if(lv_Fib->____result__0__1) + { + release_ref(lv_Fib->____result__0__1); + } + if(lv_Fib->____result__1__0) + { + release_ref(lv_Fib->____result__1__0); + } + if(lv_Fib->____result__5__0) + { + release_ref(lv_Fib->____result__5__0); + } + if(lv_Fib->____result__6__0) + { + release_ref(lv_Fib->____result__6__0); + } + if(lv_Fib->____result__7__0) + { + release_ref(lv_Fib->____result__7__0); + } + if(lv_Fib->____result__10__0) + { + release_ref(lv_Fib->____result__10__0); + } + if(lv_Fib->____result__11__0) + { + release_ref(lv_Fib->____result__11__0); } release_ref(cdata->params[0]); - Ret(0, locals->out) + Ret(0, lv_Fib->out) +EndFunc(Fib) +DISPATCH + +Method(If) + MethodDispatch(TYPE_BOOLEAN,If,Yes_SP_No) +EndMethod + +MethodImplNoLocals(If,Yes_SP_No,TYPE_BOOLEAN, + NumParams 1) -EndFunc + if(((t_Boolean *)(cdata->params[0]))->val) + { + Ret(1, NULL) + } else { + Ret(1, cdata->params[0]); + Ret(0, NULL) + } +EndFuncNoLocals +DISPATCH -FuncNoLocals(Main, - NumParams 1, - CallSpace 2) +Func(Main, + NumParams 0) + + lv_Main->____result__0__0 = NULL; + lv_Main->____result__1__0 = NULL; + + PrepCall(1) + SetParam(0, add_ref(_const_Whole_SP_Number__30)) + Call(Fib, + NumParams 1, 1, Main) + lv_Main->____result__1__0 = cdata->params[0]; + FreeCall + + PrepCall(1) + SetParam(0, add_ref(lv_Main->____result__1__0)) + Call(Print, + NumParams 1, 2, Main) + lv_Main->____result__0__0 = cdata->params[0]; + FreeCall - TCall(Fib, 1) -EndFunc + if(lv_Main->____result__0__0) + { + release_ref(lv_Main->____result__0__0); + } + if(lv_Main->____result__1__0) + { + release_ref(lv_Main->____result__1__0); + } +EndFunc(Main) +DISPATCH -#ifdef _WIN32 - #include "windows.h" -#endif +FuncNoLocals(Print, + NumParams 1) + + if(get_blueprint(cdata->params[0])->type_id == TYPE_INT32) + { + printf("%d\n", ((t_Int32 *)(cdata->params[0]))->num); + } else { + puts("Don't know how to print this type"); + } + release_ref(cdata->params[0]); + Ret(0, make_Int32(0)) +EndFuncNoLocals +DISPATCH + +DO_END: + return; + +_exception: + puts("whoops, exception!"); +} int main(int argc, char ** argv) { - returntype ret; - calldata *cdata; - context * ct; - #ifdef _WIN32 - DWORD dur; - dur = timeGetTime(); - #endif - if(argc < 2) - { - puts("not enought arguments"); - return -1; - } register_builtin_types(); - ct = new_context(); - cdata = alloc_cdata(ct, 1); - cdata->num_params = 1; - cdata->resume = 0; - const_1 = make_Int32(1); - const_2 = make_Int32(2); - cdata->params[0] = make_Int32(atoi(argv[1])); - ret = f_Main(cdata); - while(ret == TAIL_RETURN) - ret = cdata->tail_func(cdata); - if(ret == EXCEPTION_RETURN) { - puts("Exception!"); - return -1; - } - printf("Result: %d\n", ((t_Int32 *)cdata->params[0])->num); - #ifdef _WIN32 - dur = timeGetTime()-dur; - printf("Took %d milliseconds\n", dur); - #endif + _const_Whole_SP_Number__1 = make_Int32(1); + _const_Whole_SP_Number__2 = make_Int32(2); + _const_Whole_SP_Number__30 = make_Int32(30); + + rhope(FUNC_Main); return 0; } -