comparison gcc/c-parser.c @ 10:26e357f6275f

add cbc_make_nested_function
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Tue, 08 Sep 2009 21:04:27 +0900
parents ddf292032e7d
children 8f2e43f937f3
comparison
equal deleted inserted replaced
9:ddf292032e7d 10:26e357f6275f
57 #include "vec.h" 57 #include "vec.h"
58 #include "target.h" 58 #include "target.h"
59 #include "cgraph.h" 59 #include "cgraph.h"
60 #ifndef noCbC 60 #ifndef noCbC
61 #include "cbc-tree.h" 61 #include "cbc-tree.h"
62 static tree cbc_make_nested_function (location_t);
62 #endif 63 #endif
63 64
64 65
65 /* Initialization routine for this file. */ 66 /* Initialization routine for this file. */
66 67
3793 { 3794 {
3794 TREE_TYPE(expr.value) = void_type_node; 3795 TREE_TYPE(expr.value) = void_type_node;
3795 tree env = NULL_TREE; 3796 tree env = NULL_TREE;
3796 // if (c_parser_next_token_is (parser, CPP_COMMA)) 3797 // if (c_parser_next_token_is (parser, CPP_COMMA))
3797 // env = c_parser_cbc_make_env(parser); 3798 // env = c_parser_cbc_make_env(parser);
3798 CbC_IS_CbC_GOTO (expr.value) = 1; 3799 if (CbC_IS_CODE_SEGMENT(TREE_TYPE(current_function_decl)))
3799 CALL_EXPR_TAILCALL (expr.value) = 1; 3800 {
3801 CbC_IS_CbC_GOTO (expr.value) = 1;
3802 CALL_EXPR_TAILCALL (expr.value) = 1;
3803 }
3800 add_stmt(expr.value); 3804 add_stmt(expr.value);
3801 CbC_HAVE_CbC_GOTO (current_function_decl) = 1; 3805 CbC_HAVE_CbC_GOTO (current_function_decl) = 1;
3802 // should be match with function type? 3806 // should be match with function type?
3803 stmt = c_finish_return (0); 3807 stmt = c_finish_return (0);
3804 } 3808 }
5601 } 5605 }
5602 break; 5606 break;
5603 case RID_CbC_RET: 5607 case RID_CbC_RET:
5604 case RID_RETURN: 5608 case RID_RETURN:
5605 5609
5606 if (cbc_return_f==0) { tree retval; 5610 #if 0
5611 if (cbc_return_f==0)
5612 { tree retval;
5607 5613
5608 /* 5614 /*
5609 Generates something like... 5615 Generates something like...
5610 5616
5611 int retval = 1; 5617 int retval = 1;
5692 5698
5693 TREE_SIDE_EFFECTS (stmt) = 1; 5699 TREE_SIDE_EFFECTS (stmt) = 1;
5694 expr.value = c_finish_stmt_expr (stmt); 5700 expr.value = c_finish_stmt_expr (stmt);
5695 expr.original_code = ERROR_MARK; 5701 expr.original_code = ERROR_MARK;
5696 5702
5697 } else { 5703
5704 }
5705 else
5706 {
5698 //tree label = lookup_label(c_parser_peek_token (parser)->value); 5707 //tree label = lookup_label(c_parser_peek_token (parser)->value);
5699 //TREE_USED(label) = 1; 5708 //TREE_USED(label) = 1;
5700 //expr.value = build1(ADDR_EXPR, ptr_type_node, label); 5709 //expr.value = build1(ADDR_EXPR, ptr_type_node, label);
5701 expr.value = build1(ADDR_EXPR, ptr_type_node, return_label1); 5710 expr.value = build1(ADDR_EXPR, ptr_type_node, return_label1);
5702 c_parser_consume_token (parser); 5711 c_parser_consume_token (parser);
5703 } 5712 }
5704 break; 5713 #else //by KENT.
5705 #endif 5714 {
5715 c_parser_consume_token (parser);
5716
5717 tree stmt = c_begin_stmt_expr ();
5718 cbc_return_f = c_parser_peek_token (parser)->value;
5719 location_t location = c_parser_peek_token (parser)->location;
5720
5721 //add_stmt (cbc_make_nested_function (location));
5722 tree nested_func = cbc_make_nested_function (location);
5723 add_stmt (build_stmt (DECL_EXPR, nested_func));
5724
5725 tree value = build_addr (nested_func , current_function_decl);
5726 SET_EXPR_LOCATION (value, location);
5727 add_stmt (value);
5728
5729 TREE_SIDE_EFFECTS (stmt) = 1;
5730 expr.value = c_finish_stmt_expr (stmt);
5731 expr.original_code = ERROR_MARK;
5732 }
5733
5734 #endif //0
5735 break;
5736 #endif //noCbC
5706 default: 5737 default:
5707 c_parser_error (parser, "expected expression"); 5738 c_parser_error (parser, "expected expression");
5708 expr.value = error_mark_node; 5739 expr.value = error_mark_node;
5709 expr.original_code = ERROR_MARK; 5740 expr.original_code = ERROR_MARK;
5710 break; 5741 break;
5730 expr.value = error_mark_node; 5761 expr.value = error_mark_node;
5731 expr.original_code = ERROR_MARK; 5762 expr.original_code = ERROR_MARK;
5732 break; 5763 break;
5733 } 5764 }
5734 return c_parser_postfix_expression_after_primary (parser, expr); 5765 return c_parser_postfix_expression_after_primary (parser, expr);
5766 }
5767
5768 static tree
5769 cbc_make_nested_function (location_t loc)
5770 {
5771
5772 /*
5773 * void __return_func(int _retval, void *fp){
5774 * retval = _retval;
5775 * goto exit0;
5776 * }
5777 */
5778
5779 tree fnbody;
5780 struct c_declarator *declarator;
5781 tree ident;
5782 struct c_arg_info *args;
5783 struct c_declspecs *specs;
5784 struct c_typespec t;
5785
5786 { /* nested function's parameters (int _retval,void *fp) */
5787 struct c_declspecs *_retval_specs,
5788 *_envp_specs;
5789 struct c_declarator*_retval_decl,
5790 *_envp_decl;
5791 struct c_parm *_retval,
5792 *_envp;
5793 tree _retval_ident,
5794 _envp_ident;
5795 struct c_typespec t;
5796
5797 push_scope ();
5798 declare_parm_level ();
5799
5800 _retval_specs = build_null_declspecs();
5801 t.kind = ctsk_resword;
5802 //t.spec = integer_type_node;
5803 t.spec = get_identifier("int");
5804 declspecs_add_type (_retval_specs, t);
5805 finish_declspecs (_retval_specs);
5806 _retval_decl = build_id_declarator (get_identifier ("_retval"));
5807 _retval = build_c_parm (_retval_specs, NULL_TREE, _retval_decl);
5808
5809 push_parm_decl(_retval);
5810
5811 _envp_specs = build_null_declspecs();
5812 t.kind = ctsk_resword;
5813 t.spec = get_identifier("void");
5814 declspecs_add_type (_envp_specs, t);
5815 finish_declspecs (_envp_specs);
5816 _envp_decl = build_id_declarator (get_identifier ("_envp"));
5817 _envp_decl = make_pointer_declarator (build_null_declspecs (), _envp_decl);
5818 _envp = build_c_parm (_envp_specs, NULL_TREE, _envp_decl);
5819
5820 push_parm_decl(_envp);
5821
5822 args = get_parm_info(false);
5823 pop_scope();
5824 }
5825
5826 t.kind = ctsk_resword;
5827 t.spec = get_identifier("void");
5828 specs = build_null_declspecs();
5829 declspecs_add_type (specs, t);
5830 finish_declspecs (specs);
5831
5832 /* make nested function. */
5833 //ident = build_decl (/*TODO:*/VAR_DECL, get_identifier ("_cbc_internal_return"), specs->type);
5834 declarator = build_id_declarator (get_identifier ("_cbc_internal_return"));
5835 //declarator->id_loc = ;
5836 declarator = build_function_declarator (args, declarator);
5837
5838 c_push_function_context ();
5839
5840 if (!start_function (specs, declarator, NULL_TREE))
5841 {
5842 c_pop_function_context();
5843 gcc_assert (0);
5844 }
5845 store_parm_decls ();
5846
5847
5848 /* start compound statement. */
5849 tree cstmt = c_begin_compound_stmt (true);
5850
5851 tree lhs = build_external_ref (get_identifier("retval"), false, loc);
5852 tree rhs = build_external_ref (get_identifier("_retval"), false, loc);
5853 add_stmt (build_modify_expr (loc, lhs, NOP_EXPR, rhs));
5854 tree stmt = c_finish_goto_label (get_identifier ("_cbc_exit0"));
5855
5856 /* end compound statement. */
5857 fnbody = c_end_compound_stmt (cstmt, true);
5858 TREE_SIDE_EFFECTS (cstmt) = 1;
5859
5860 /* finish declaration of nested function. */
5861 tree decl = current_function_decl;
5862 add_stmt (fnbody);
5863 finish_function ();
5864 c_pop_function_context ();
5865
5866 //add_stmt (build_stmt (DECL_EXPR, decl));
5867 //return build_stmt (DECL_EXPR, decl);
5868 return decl;
5735 } 5869 }
5736 5870
5737 /* Parse a postfix expression after a parenthesized type name: the 5871 /* Parse a postfix expression after a parenthesized type name: the
5738 brace-enclosed initializer of a compound literal, possibly followed 5872 brace-enclosed initializer of a compound literal, possibly followed
5739 by some postfix operators. This is separate because it is not 5873 by some postfix operators. This is separate because it is not