diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runtime/fib.c	Thu Oct 08 01:52:38 2009 -0400
@@ -0,0 +1,308 @@
+#include <stdio.h>
+#include "builtin.h"
+#include "object.h"
+#include "context.h"
+#include "func.h"
+#include "integer.h"
+#include "bool.h"
+
+typedef enum {
+	FUNC__LT_,
+	FUNC__LT__AT_Int32,
+	FUNC__MN_,
+	FUNC__MN__AT_Int32,
+	FUNC__PL_,
+	FUNC__PL__AT_Int32,
+	FUNC_Fib,
+	FUNC_If,
+	FUNC_If_AT_Yes_SP_No,
+	FUNC_Main,
+	FUNC_Print,
+	END
+} funcids;
+
+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;
+	resume = 0;
+_dispatch:
+switch(func)
+{
+Method(_LT_)
+	MethodDispatch(TYPE_INT32,_LT_,Int32)
+EndMethod
+
+MethodImplNoLocals(_LT_,Int32,TYPE_INT32,
+	NumParams 2)
+	
+	Param(1, TYPE_INT32)
+	
+	if(((t_Int32 *)cdata->params[0])->num < ((t_Int32 *)cdata->params[-1])->num)
+	{
+		release_ref(cdata->params[0]);
+		Ret(0, Yes)
+	} else {
+		release_ref(cdata->params[0]);
+		Ret(0, No)
+	}
+	
+	release_ref(cdata->params[-1]);
+EndFuncNoLocals
+
+Method(_MN_)
+	MethodDispatch(TYPE_INT32,_MN_,Int32)
+EndMethod
+
+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
+
+Method(_PL_)
+	MethodDispatch(TYPE_INT32,_PL_,Int32)
+EndMethod
+
+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
+	
+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];
+	FreeCall
+	
+	PrepCall(2)
+	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];
+	FreeCall
+	
+	if(lv_Fib->____result__0__1)
+	{
+		PrepCall(2)
+		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];
+		FreeCall
+	}
+	if(lv_Fib->____result__0__1)
+	{
+		PrepCall(2)
+		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];
+		FreeCall
+	}
+	if(lv_Fib->____result__0__0)
+	{
+		lv_Fib->out = add_ref(_const_Whole_SP_Number__1);
+	}
+	if(lv_Fib->____result__0__1)
+	{
+		PrepCall(1)
+		SetParam(0, add_ref(lv_Fib->____result__7__0))
+		Call(Fib,
+			NumParams 1, 5, Fib)
+		lv_Fib->____result__6__0 = cdata->params[0];
+		FreeCall
+	}
+	if(lv_Fib->____result__0__1)
+	{
+		PrepCall(1)
+		SetParam(0, add_ref(lv_Fib->____result__11__0))
+		Call(Fib,
+			NumParams 1, 6, Fib)
+		lv_Fib->____result__10__0 = cdata->params[0];
+		FreeCall
+	}
+	if(lv_Fib->____result__0__1)
+	{
+		PrepCall(2)
+		SetParam(0, add_ref(lv_Fib->____result__6__0))
+		SetParam(1, add_ref(lv_Fib->____result__10__0))
+		Call(_PL_,
+			NumParams 2, 7, Fib)
+		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, lv_Fib->out)
+EndFunc(Fib)
+
+Method(If)
+	MethodDispatch(TYPE_BOOLEAN,If,Yes_SP_No)
+EndMethod		
+		
+MethodImplNoLocals(If,Yes_SP_No,TYPE_BOOLEAN,
+	NumParams 1)
+	
+	if(((t_Boolean *)(cdata->params[0]))->val)
+	{
+		Ret(1, NULL)
+	} else {
+		Ret(1, cdata->params[0]);
+		Ret(0, NULL)
+	}
+EndFuncNoLocals
+
+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
+	
+	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)
+
+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
+
+case END:
+	return;
+}
+_exception:
+	puts("whoops, exception!");
+}
+
+int main(int argc, char ** argv)
+{
+	register_builtin_types();
+	
+	_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;
+}