# HG changeset patch # User kent # Date 1254222916 -32400 # Node ID 959d4c8c8abcdb9815d496498dad533c4eaec372 # Parent 9de9dad105d48a4a591d064af305b7ae387ef9c1 add conv.c conv1.c diff -r 9de9dad105d4 -r 959d4c8c8abc CbC-examples/conv.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CbC-examples/conv.c Tue Sep 29 20:15:16 2009 +0900 @@ -0,0 +1,92 @@ +#include "stdio.h" + +f0(int i) { + int k,j; + k = 3+i; + j = g0(i+3); + return k+4+j; +} + +g0(int i) { + return i+4; +} + + +typedef void *stack; + +__code f_g0(int i,int k,stack sp) ; + +struct cont_interface { // General Return Continuation + __code (*ret)(int, void*); +}; + +__code f(int i,stack sp) { + int k,j; + k = 3+i; + goto f_g0(i,k,sp); +} + +struct f_g0_interface { // Specialized Return Continuation + __code (*ret)(); + int i_,k_,j_; +}; + +__code f_g1(int j,stack sp); +__code g(int i,stack sp) ; + +__code f_g0(int i,int k,stack sp) { // Caller + struct f_g0_interface *c = + (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface)); + + c->ret = f_g1; + c->k_ = k; + c->i_ = i; + + goto g(i+3,sp); +} + +__code f_g1(int j,stack sp) { // Continuation + struct f_g0_interface *c = (struct f_g0_interface *)sp; + int k = c->k_; + sp += sizeof(struct f_g0_interface); + goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp); +} + +__code g(int i,stack sp) { + goto (( (struct cont_interface *)sp)->ret)(i+4,sp); +} + +struct main_continuation { // General Return Continuation + __code (*ret)(int, void*); + __code (*main_ret)(int, void*); + void *env; +}; + +__code main_return(int i,stack sp) { + printf("#0061:%d\n",i); + goto (( (struct main_continuation *)sp)->main_ret)(0, + ((struct main_continuation *)sp)->env); +} + +#define STACK_SIZE 2048 +char main_stack[STACK_SIZE]; +#define stack_last (&main_stack[STACK_SIZE]) + +typedef __code (*return_type)(int, void*); +int +main(int argc, char **argv) +{ + struct main_continuation *cont; + stack sp = stack_last; + + printf("#0075:%d\n",f0(233)); + + sp -= sizeof(*cont); + cont = (struct main_continuation *)sp; + cont->ret = main_return; + cont->main_ret = (return_type) _CbC_return; + cont->env = _CbC_environment; + goto f(233,sp); +} + +/* end */ diff -r 9de9dad105d4 -r 959d4c8c8abc CbC-examples/conv1.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CbC-examples/conv1.c Tue Sep 29 20:15:16 2009 +0900 @@ -0,0 +1,225 @@ +#include "stdio.h" + +static int loop; + +#if 1 // def __micro_c__ +#define CC_ONLY 0 +#else +#define CC_ONLY 1 +#endif + +/* classical function call case (0) */ + +f0(int i) { + int k,j; + k = 3+i; + j = g0(i+3); + return k+4+j; +} + +g0(int i) { + return h0(i+4)+i; +} + +h0(int i) { + return i+4; +} + +#if !CC_ONLY + +/* straight conversion case (1) */ + +typedef char *stack; + +struct cont_interface { // General Return Continuation + __code (*ret)(int, void *); +}; + +__code f(int i,stack sp) { + int k,j; + k = 3+i; + goto f_g0(i,k,sp); +} + +struct f_g0_interface { // Specialized Return Continuation + __code (*ret)(int, void *); + int i_,k_,j_; +}; + +__code f_g1(int j,stack sp); + +__code f_g0(int i,int k,stack sp) { // Caller + struct f_g0_interface *c = + (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface)); + + c->ret = f_g1; + c->k_ = k; + c->i_ = i; + + goto g(i+3,sp); +} + +__code f_g1(int j,stack sp) { // Continuation + struct f_g0_interface *c = (struct f_g0_interface *)sp; + int k = c->k_; + sp+=sizeof(struct f_g0_interface); + c = (struct f_g0_interface *)sp; + goto (c->ret)(k+4+j,sp); +} + +__code g_h1(int j,stack sp); + +__code g(int i,stack sp) { // Caller + struct f_g0_interface *c = + (struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface)); + + c->ret = g_h1; + c->i_ = i; + + goto h(i+3,sp); +} + +__code g_h1(int j,stack sp) { // Continuation + struct f_g0_interface *c = (struct f_g0_interface *)sp; + int i = c->i_; + sp+=sizeof(struct f_g0_interface); + c = (struct f_g0_interface *)sp; + goto (c->ret)(j+i,sp); +} + +__code h(int i,stack sp) { + struct f_g0_interface *c = (struct f_g0_interface *)sp; + goto (c->ret)(i+4,sp); +} + +struct main_continuation { // General Return Continuation + __code (*ret)(); + __code (*main_ret)(); + void *env; +}; + +__code main_return(int i,stack sp) { + if (loop-->0) + goto f(233,sp); + printf("#0103:%d\n",i); + goto (( (struct main_continuation *)sp)->main_ret)(0, + ((struct main_continuation *)sp)->env); +} + +/* little optimzation without stack continuation (2) */ + +__code f2(int i,char *sp) { + int k,j; + k = 3+i; + goto g2(i,k,i+3,sp); +} + +__code g2(int i,int k,int j,char *sp) { + j = j+4; + goto h2(i,k+4+j,sp); +} + +__code h2_1(int i,int k,int j,char *sp) { + goto main_return2(i+j,sp); +} + +__code h2(int i,int k,char *sp) { + goto h2_1(i,k,i+4,sp); +} + +__code main_return2(int i,stack sp) { + if (loop-->0) + goto f2(233,sp); + printf("#0132:%d\n",i); + goto (( (struct main_continuation *)sp)->main_ret)(0, + ((struct main_continuation *)sp)->env); +} + +/* little optimizaed case (3) */ + +__code f2_1(int i,char *sp) { + int k,j; + k = 3+i; + goto g2_1(k,i+3,sp); +} + +__code g2_1(int k,int i,char *sp) { + goto h2_11(k,i+4,sp); +} + +__code f2_0_1(int k,int j,char *sp); +__code h2_1_1(int i,int k,int j,char *sp) { + goto f2_0_1(k,i+j,sp); +} + +__code h2_11(int i,int k,char *sp) { + goto h2_1_1(i,k,i+4,sp); +} + +__code f2_0_1(int k,int j,char *sp) { + goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp); +} + +__code main_return2_1(int i,stack sp) { + if (loop-->0) + goto f2_1(233,sp); + printf("#0165:%d\n",i); + goto (( (struct main_continuation *)sp)->main_ret)(0, + ((struct main_continuation *)sp)->env); +} + +#define STACK_SIZE 2048 +char main_stack[STACK_SIZE]; +#define stack_last (main_stack+STACK_SIZE) + +#endif + +#define LOOP_COUNT 10000000 + +main(int ac,char *av[]) +{ +#if !CC_ONLY + struct main_continuation *cont; + stack sp = stack_last; +#endif + int sw; + int j; + if (ac==2) sw = atoi(av[1]); + else sw=3; + + if (sw==0) { + for(loop=0;loopret = main_return; + cont->main_ret = _CbC_return; + cont->env = _CbC_environment; + goto f(233,sp); + } else if (sw==2) { + loop = LOOP_COUNT; + sp -= sizeof(*cont); + cont = (struct main_continuation *)sp; + cont->ret = main_return2; + cont->main_ret = _CbC_return; + cont->env = _CbC_environment; + goto f2(233,sp); + } else if (sw==3) { + loop = LOOP_COUNT; + sp -= sizeof(*cont); + cont = (struct main_continuation *)sp; + cont->ret = main_return2_1; + cont->main_ret = _CbC_return; + cont->env = _CbC_environment; + goto f2_1(233,sp); +#endif + } +return 0; +} + +/* end */ diff -r 9de9dad105d4 -r 959d4c8c8abc CbC-examples/test_func2code.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CbC-examples/test_func2code.c Tue Sep 29 20:15:16 2009 +0900 @@ -0,0 +1,43 @@ +#include +#include + +#define DEBUG 1 +#ifdef DEBUG + #define log(f, args...) \ + fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) +#else + #define log(f, args...) ; +#endif + +__code +exitter(int a) +{ + exit(0); +} + +__code +cs0(int x, int y) +{ + log("x = %d, y = %d.\n", x, y); + log("will exit with code %d.\n", x*y); + goto exitter(x*y); +} + +void +continuation(int a) +{ + log("go code segment cs0\n"); + goto cs0(a, a*20); + log("Error: continuation reachs bad region.\n"); +} + +int +main(int argc, char **argv) +{ + int a; + if (argc>2) { + a = atoi(argv[1]); + } + + continuation(20); +} diff -r 9de9dad105d4 -r 959d4c8c8abc CbC-examples/test_para4.c --- a/CbC-examples/test_para4.c Thu Sep 24 14:29:28 2009 +0900 +++ b/CbC-examples/test_para4.c Tue Sep 29 20:15:16 2009 +0900 @@ -18,6 +18,29 @@ exit(a); } +__code NOINLINE cs0(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, + int a2, int b2, int c2, int d2, int e2, int f2, int g2, int h2, int i2, int j2){ +#if DPRINT + //printf("cs0 : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d.\n", a, b, c, d, e, f, g); + printf("cs0 : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", + a, b, c, d, e, f, g, h, i, j); + printf("cs0 : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", + a2, b2, c2, d2, e2, f2, g2, h2, i2, j2); +#endif + goto cs_exit( (int)(10*a + 10*b + 10*i2 + 10*j2) ); +} + +__code NOINLINE cs_goto(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, + int a2, int b2, int c2, int d2, int e2, int f2, int g2, int h2, int i2, int j2){ +#if DPRINT + printf("cs_goto : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", + a, b, c, d, e, f, g, h, i, j); + printf("cs_goto : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", + a2, b2, c2, d2, e2, f2, g2, h2, i2, j2); +#endif + goto cs0(b, c, d, e, f, g, h, i, j, a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, a); +} + int function(double l, float m, int n){ int a=10, b=20, c=30, d=40, e=50, f=60, g=70, h=80, i=90, j=100; int a2=110, b2=120, c2=130, d2=140, e2=150, f2=160, g2=170, h2=180, i2=190, j2=200; @@ -33,29 +56,6 @@ goto cs_goto(a, b, c, d, e, f, g, h, i, j, a2, b2, c2, d2, e2, f2, g2, h2, i2, j2); } -__code NOINLINE cs_goto(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, - int a2, int b2, int c2, int d2, int e2, int f2, int g2, int h2, int i2, int j2){ -#if DPRINT - printf("cs_goto : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", - a, b, c, d, e, f, g, h, i, j); - printf("cs_goto : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", - a2, b2, c2, d2, e2, f2, g2, h2, i2, j2); -#endif - goto cs0(b, c, d, e, f, g, h, i, j, a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, a); -} - -__code NOINLINE cs0(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, - int a2, int b2, int c2, int d2, int e2, int f2, int g2, int h2, int i2, int j2){ -#if DPRINT - //printf("cs0 : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d.\n", a, b, c, d, e, f, g); - printf("cs0 : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", - a, b, c, d, e, f, g, h, i, j); - printf("cs0 : a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n", - a2, b2, c2, d2, e2, f2, g2, h2, i2, j2); -#endif - goto cs_exit( (int)(10*a + 10*b + 10*i2 + 10*j2) ); -} - int main(int argc, char **argv){ diff -r 9de9dad105d4 -r 959d4c8c8abc gcc/c-parser.c --- a/gcc/c-parser.c Thu Sep 24 14:29:28 2009 +0900 +++ b/gcc/c-parser.c Tue Sep 29 20:15:16 2009 +0900 @@ -3795,13 +3795,13 @@ tree env = NULL_TREE; // if (c_parser_next_token_is (parser, CPP_COMMA)) // env = c_parser_cbc_make_env(parser); - if (CbC_IS_CODE_SEGMENT(TREE_TYPE(current_function_decl))) + //if (CbC_IS_CODE_SEGMENT(TREE_TYPE(current_function_decl))) { CbC_IS_CbC_GOTO (expr.value) = 1; CALL_EXPR_TAILCALL (expr.value) = 1; } add_stmt(expr.value); - CbC_HAVE_CbC_GOTO (current_function_decl) = 1; + //CbC_HAVE_CbC_GOTO (current_function_decl) = 1; // should be match with function type? stmt = c_finish_return (0); } diff -r 9de9dad105d4 -r 959d4c8c8abc gcc/calls.c --- a/gcc/calls.c Thu Sep 24 14:29:28 2009 +0900 +++ b/gcc/calls.c Tue Sep 29 20:15:16 2009 +0900 @@ -2384,17 +2384,12 @@ #ifndef noCbC if ( fntype - //&& CbC_IS_CODE_SEGMENT (fntype) - //&& fndecl - //&& CbC_IS_CODE_SEGMENT (fndecl) - //&& CbC_IS_CODE_SEGMENT(TREE_TYPE(fndecl)) - //&& CALL_EXPR_TAILCALL (exp) && CbC_IS_CbC_GOTO (exp) // it's better? than CALL_EXPR_TAILCALL() + && CbC_IS_CODE_SEGMENT (TREE_TYPE (current_function_decl)) ) { - // fprintf(stderr, "\n\tgoto code segment.\n"); - args_size.constant = CbC_ARGS_SIZE; + args_size.constant = CbC_PRETENDED_STACK_SIZE; return expand_cbc_goto(exp, target, fndecl, funtype, fntype, addr, ignore, flags, num_actuals, args, &args_size, args_so_far, @@ -2402,16 +2397,23 @@ preferred_stack_boundary, preferred_unit_stack_boundary, structure_value_addr, old_inhibit_defer_pop); } - else if ( fndecl&&CbC_HAVE_CbC_GOTO(fndecl) ) + else if ( CbC_IS_CbC_GOTO (exp) ) + { + // TODO: 関数からコードセグメントへの遷移 + char *name_callee = IDENTIFIER_POINTER(DECL_NAME(fndecl)); + warning(0, "no warning: code segment `%s' has been called from a function.", name_callee); + args_size.constant = CbC_PRETENDED_STACK_SIZE; + } + else if ( fndecl && CbC_IS_CODE_SEGMENT (TREE_TYPE (fndecl)) ) { - char *name = IDENTIFIER_POINTER(DECL_NAME(fndecl)); - fprintf(stderr, "\nCbC: function %s having CbCgoto statement has been `CALLED'.\n", name); - //args_size.constant = CbC_ARGS_SIZE; + // 警告コードセグメントを関数呼び出し + char *name= IDENTIFIER_POINTER(DECL_NAME(fndecl)); + warning (0, "code segment `%s' has been \"called\" instead \"goto\".", name); } - else if ( fntype&&CbC_IS_CODE_SEGMENT(fntype) ) + else if (CbC_IS_CODE_SEGMENT(TREE_TYPE (current_function_decl)) ) { - char *name = IDENTIFIER_POINTER(DECL_NAME(fndecl)); - fprintf(stderr, "\nCbC error: codesegment %s has been `CALLED!'.\n", name); + // code segment内部からの関数呼び出し。なんも問題ない。 + warning (0, "no warning: normal call from a code segment."); } #endif @@ -2486,17 +2488,16 @@ pattern, do not round up, since we'll be re-using whatever space our caller provided. */ #ifndef noCbC - if ( ( fntype && CbC_IS_CODE_SEGMENT(fntype) ) - || ( fndecl && CbC_HAVE_CbC_GOTO(fndecl) ) ) + if ( fntype && CbC_IS_CODE_SEGMENT(fntype) ) { - unadjusted_args_size = args_size.constant; - adjusted_args_size.constant = CbC_ARGS_SIZE; - compute_argument_block_size (reg_parm_stack_space, - &adjusted_args_size, - fndecl, fntype, - (pass == 0 ? 0 - : preferred_stack_boundary)); - } + unadjusted_args_size = args_size.constant; + adjusted_args_size.constant = CbC_PRETENDED_STACK_SIZE; + compute_argument_block_size (reg_parm_stack_space, + &adjusted_args_size, + fndecl, fntype, + (pass == 0 ? 0 + : preferred_stack_boundary)); + } else #endif { diff -r 9de9dad105d4 -r 959d4c8c8abc gcc/cbc-goto.h --- a/gcc/cbc-goto.h Thu Sep 24 14:29:28 2009 +0900 +++ b/gcc/cbc-goto.h Tue Sep 29 20:15:16 2009 +0900 @@ -1,5 +1,3 @@ - -#define CbC_ARGS_SIZE 256 static void preexpand_argument_expr (struct arg_data *, int); @@ -200,7 +198,7 @@ //if ( !(current_function_decl&&CbC_IS_CODE_SEGMENT(current_function_decl)) ) { HOST_WIDE_INT padding; - padding = CbC_ARGS_SIZE - + padding = CbC_PRETENDED_STACK_SIZE - (crtl->args.size - crtl->args.pretend_args_size); if (0&&padding > 0) anti_adjust_stack (GEN_INT (padding)); diff -r 9de9dad105d4 -r 959d4c8c8abc gcc/cbc-tree-debug.c --- a/gcc/cbc-tree-debug.c Thu Sep 24 14:29:28 2009 +0900 +++ b/gcc/cbc-tree-debug.c Tue Sep 29 20:15:16 2009 +0900 @@ -7,19 +7,25 @@ tree -_debug_tree_type (tree t) +_TREE_TYPE (tree t) { return TREE_TYPE (t); } enum tree_code -_debug_tree_code (tree t) +_TREE_CODE (tree t) { return TREE_CODE (t); } tree -_debug_tree_chain (tree t) +_TREE_CHAIN (tree t) { return TREE_CHAIN (t); } + +tree +_DECL_NAME (tree t) +{ + return DECL_NAME (t); +} diff -r 9de9dad105d4 -r 959d4c8c8abc gcc/cbc-tree.h --- a/gcc/cbc-tree.h Thu Sep 24 14:29:28 2009 +0900 +++ b/gcc/cbc-tree.h Tue Sep 29 20:15:16 2009 +0900 @@ -1,17 +1,20 @@ + +//#define CbC_PRETENDED_STACK_SIZE 256 +#define CbC_PRETENDED_STACK_SIZE 1024 + + + /* Set if the fntype is code segment on CbC language. */ // flag3,5,6 has been used by c-tree.h #define CbC_IS_CODE_SEGMENT(TYPE) TYPE_LANG_FLAG_5 ( FUNCTION_TYPE_CHECK(TYPE)) -#define CbC_HAVE_CbC_GOTO(EXP) DECL_LANG_FLAG_4 (FUNCTION_DECL_CHECK (EXP)) /* Set if the CALL_EXPR NODE is goto statement on CbC language. */ -//#define CbC_IS_CbC_GOTO(NODE) (CALL_EXPR_CHECK(NODE)->common.lang_flag_5) - -// old difinition -//#define CbC_IS_CODE_SEGMENT(EXP) DECL_LANG_FLAG_7 (FUNCTION_DECL_CHECK (EXP)) -//#define CbC_IS_CODE_SEGMENT(NODE) (TYPE_CHECK (NODE)->type.lang_flag_5) #define CbC_IS_CbC_GOTO(NODE) TREE_LANG_FLAG_5 (CALL_EXPR_CHECK(NODE)) -#define CALL_EXPR_CBC_GOTO(NODE) TREE_LANG_FLAG_5 (CALL_EXPR_CHECK(NODE)) +#define CALL_EXPR_CbC_GOTO(NODE) TREE_LANG_FLAG_5 (CALL_EXPR_CHECK(NODE)) extern tree cbc_return_f; extern tree cbc_env; extern location_t cbc_return; + + + diff -r 9de9dad105d4 -r 959d4c8c8abc gcc/cfgexpand.c --- a/gcc/cfgexpand.c Thu Sep 24 14:29:28 2009 +0900 +++ b/gcc/cfgexpand.c Tue Sep 29 20:15:16 2009 +0900 @@ -239,7 +239,7 @@ CALL_CANNOT_INLINE_P (t) = gimple_call_cannot_inline_p (stmt); CALL_EXPR_VA_ARG_PACK (t) = gimple_call_va_arg_pack_p (stmt); #ifndef noCbC - CALL_EXPR_CBC_GOTO (t) = gimple_call_cbc_goto_p (stmt); + CALL_EXPR_CbC_GOTO (t) = gimple_call_cbc_goto_p (stmt); #endif /* If the call has a LHS then create a MODIFY_EXPR to hold it. */ diff -r 9de9dad105d4 -r 959d4c8c8abc gcc/gimple.c --- a/gcc/gimple.c Thu Sep 24 14:29:28 2009 +0900 +++ b/gcc/gimple.c Tue Sep 29 20:15:16 2009 +0900 @@ -364,7 +364,7 @@ gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t)); gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t)); #ifndef noCbC - gimple_call_set_cbc_goto (call, CALL_EXPR_CBC_GOTO (t)); + gimple_call_set_cbc_goto (call, CALL_EXPR_CbC_GOTO (t)); #endif return call;