view CbC-examples/test1.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 5d30d517ebed
children
line wrap: on
line source

#define __environment _CbC_environment
#define __return _CbC_return

/*
    test for CbC converted __code from C
 */

#include <stdio.h>

extern void *malloc(size_t);

typedef void *stack;

void *stack0;      /* size of void* == 1 */

struct cont_save { /* General Return Continuation */
    __code (*ret)();
};

/*
    __code g(int,void *);
    __code f_g0(int ,int ,void *);
    __code f_g1(int,void *);
*/

struct f_g0_save {  /* Specialized Return Continuation */
    __code (*ret)();
    int ii,kk,jj;
};

__code g(int i,void *sp) {
    goto (* ((struct cont_save *)sp)->ret)(i+4,sp);
}

__code f_g1(int j,void *sp) {  /* Continuation  */
    int k;
    struct f_g0_save *c;

    c = sp;
    k = c->kk;
    sp += sizeof(struct f_g0_save);
    goto (* ((struct cont_save *)sp)->ret)(k+4+j,sp);
}

__code f(int i,void *sp) {
    int k,j;
    struct f_g0_save *c;
printf("#0047:f 0 sp: %x\n",sp-stack0);

    k = 3+i;

printf("#0051:f 1 sp: %x\n",sp-stack0);
    sp -= sizeof(struct f_g0_save);
printf("#0053:f 2 sp: %x\n",sp-stack0);
    c = sp;
    c->kk = k;
    c->ii = i;
    c->jj = j;
    c->ret = f_g1;
    goto g(i,sp);
}



struct f0_save {  /* Specialized Return Continuation */
	__code (*ret)();
	__code (*exit1)();
	void *exit1env;
	int jj;
};

__code f1(int i,void *sp) ;
__code f0(int i,int j,__code(*exit2)(), void *exit2env,void *sp)
{
	struct f0_save *c;
    printf("#0075:f0 1 sp: %x\n",sp-stack0);
	sp -= sizeof(struct f0_save);
    printf("#0077:f0 2 sp: %x\n",sp-stack0);
	c = sp;
	c->jj = j;
        c->exit1 = exit2;
        c->exit1env = exit2env;
	c->ret = f1;
    printf("#0083:f0 3 sp: %x\n",sp-stack0);
	goto f(i,sp);
}

__code print(int i,int j,__code (*exit1)(),void*exit1env);

__code f1(int i,void *sp) {
	int j;
	int *exit2env;
	__code (*exit2)();
	struct f0_save *c;

        c = sp;
        j = c->jj;
        exit2 = c->exit1;
        exit2env = c->exit1env;

	sp += sizeof(struct f0_save);
	goto print(i,j,exit2,exit2env);
}

int main( int ac, char *av[])
{
    int i,j;
    int *sp;

    // i = atoi(av[1]);
    i = 1;
    stack0 = ((char *)malloc(1024)+1024);
    sp = stack0;
    j = i;
    
    printf("#0115:sp: %x %x\n",sp-(int*)stack0,sizeof(*stack0));
    goto f0(i,j,__return,__environment,sp);
}

__code print(int i,int j,__code (*exit1)(),void*exit1env)
{
    printf("#0121:%d %d\n",i,j);
    goto (*exit1)(0,exit1env);
}