diff options
author | redbrain <redbrain@gcc.gnu.org> | 2012-04-19 18:12:21 +0100 |
---|---|---|
committer | redbrain <redbrain@gcc.gnu.org> | 2012-04-19 18:12:21 +0100 |
commit | 30903ba9eb9e6b3f3d926041cfc438c71377d6f6 (patch) | |
tree | 7b354ef88fc891de196333aece719d8a088896ab | |
parent | 381d5e664c825841aed30644bf53aabc731aebb6 (diff) |
mid changes to argument passing
-rw-r--r-- | gcc/python/py-runtime.c | 22 | ||||
-rw-r--r-- | gcc/python/py-runtime.def | 14 | ||||
-rw-r--r-- | gcc/python/py-runtime.h | 3 | ||||
-rw-r--r-- | libgpython/include/gpython/objects.h | 1 | ||||
-rw-r--r-- | libgpython/include/gpython/runtime.h | 4 | ||||
-rw-r--r-- | libgpython/runtime/py-obj_class.c | 2 | ||||
-rw-r--r-- | libgpython/runtime/py-obj_classmethod.c | 114 | ||||
-rw-r--r-- | libgpython/runtime/py-obj_staticmethod.c | 2 | ||||
-rw-r--r-- | libgpython/runtime/py-runtime.c | 60 |
9 files changed, 210 insertions, 12 deletions
diff --git a/gcc/python/py-runtime.c b/gcc/python/py-runtime.c index b09a2645aaa..c9213fac8b5 100644 --- a/gcc/python/py-runtime.c +++ b/gcc/python/py-runtime.c @@ -144,7 +144,27 @@ tree GPY_RR_fold_func_decl (tree identifier, tree func) ptr_type_node, NULL_TREE); tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, - get_identifier ("gpy_rr_fold_class_decl"), + get_identifier ("gpy_rr_fold_staticmethod_decl"), + fntype); + tree restype = TREE_TYPE (fndecl); + tree resdecl = build_decl (BUILTINS_LOCATION, RESULT_DECL, NULL_TREE, + restype); + DECL_CONTEXT (resdecl) = fndecl; + DECL_RESULT (fndecl) = resdecl; + DECL_EXTERNAL (fndecl) = 1; + TREE_PUBLIC (fndecl) = 1; + + return build_call_expr (fndecl, 2, identifier, build_fold_addr_expr (func)); +} + +tree GPY_RR_fold_classmethod_decl (tree identifier, tree func) +{ + tree fntype = build_function_type_list (gpy_object_type_ptr, + gpy_const_char_ptr, + ptr_type_node, + NULL_TREE); + tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, + get_identifier ("gpy_rr_fold_classmethod_decl"), fntype); tree restype = TREE_TYPE (fndecl); tree resdecl = build_decl (BUILTINS_LOCATION, RESULT_DECL, NULL_TREE, diff --git a/gcc/python/py-runtime.def b/gcc/python/py-runtime.def index 0fb82f34bd7..81484017119 100644 --- a/gcc/python/py-runtime.def +++ b/gcc/python/py-runtime.def @@ -70,7 +70,19 @@ py-runtime = { code = GPY_RR_fold_func_decl; arguments = "tree identifier, tree func"; proto = "tree, tree"; comment = "/* Fold func into decl <identifier><fndcel> */"; - function_identifier = "gpy_rr_fold_class_decl"; + function_identifier = "gpy_rr_fold_staticmethod_decl"; + fntype = "gpy_object_type_ptr, + gpy_const_char_ptr, + ptr_type_node, + NULL_TREE"; + build_call = build_call_expr; + build_call_args = "fndecl, 2, identifier, build_fold_addr_expr (func)"; + }; +py-runtime = { code = GPY_RR_fold_classmethod_decl; + arguments = "tree identifier, tree func"; + proto = "tree, tree"; + comment = "/* Fold func into decl <identifier><fndcel> */"; + function_identifier = "gpy_rr_fold_classmethod_decl"; fntype = "gpy_object_type_ptr, gpy_const_char_ptr, ptr_type_node, diff --git a/gcc/python/py-runtime.h b/gcc/python/py-runtime.h index 2ceb8f2622a..02a6165a76c 100644 --- a/gcc/python/py-runtime.h +++ b/gcc/python/py-runtime.h @@ -51,6 +51,9 @@ extern tree GPY_RR_fold_class_decl (tree, tree, tree); /* Fold func into decl <identifier><fndcel> */ extern tree GPY_RR_fold_func_decl (tree, tree); +/* Fold func into decl <identifier><fndcel> */ +extern tree GPY_RR_fold_classmethod_decl (tree, tree); + /* Fold integer into Int object via Int (x) */ extern tree GPY_RR_fold_integer (tree); diff --git a/libgpython/include/gpython/objects.h b/libgpython/include/gpython/objects.h index 741221e872e..114a551c676 100644 --- a/libgpython/include/gpython/objects.h +++ b/libgpython/include/gpython/objects.h @@ -121,5 +121,6 @@ extern gpy_object_t * gpy_create_object_decl (gpy_typedef_t *, void *); extern unsigned char * gpy_object_staticmethod_getaddr (gpy_object_t *); extern void gpy_obj_integer_mod_init (gpy_vector_t * const); +extern void gpy_obj_staticmethod_mod_init (gpy_vector_t * const); #endif //__GCC_OBJECTS_H__ diff --git a/libgpython/include/gpython/runtime.h b/libgpython/include/gpython/runtime.h index c01e98c8e1b..cf049237667 100644 --- a/libgpython/include/gpython/runtime.h +++ b/libgpython/include/gpython/runtime.h @@ -17,9 +17,7 @@ along with GCC; see the file COPYING3. If not see #ifndef __GCC_RUNTIME_H__ #define __GCC_RUNTIME_H__ -extern gpy_vector_t * gpy_primitives; -extern gpy_object_t ** gpy_globl_runtime_stack; - extern gpy_object_t * gpy_rr_fold_functor_decl (const char *, unsigned char *); +extern unsigned char * gpy_rr_eval_attrib_reference (gpy_object_t *, const char *); #endif //__GCC_RUNTIME_H__ diff --git a/libgpython/runtime/py-obj_class.c b/libgpython/runtime/py-obj_class.c index 408e40da349..d002f8b2739 100644 --- a/libgpython/runtime/py-obj_class.c +++ b/libgpython/runtime/py-obj_class.c @@ -29,6 +29,7 @@ along with GCC; see the file COPYING3. If not see #include <gpython/gpython.h> #include <gpython/vectors.h> #include <gpython/objects.h> +#include <gpython/runtime.h> typedef void (*__field_init_ptr)(void *); @@ -80,6 +81,7 @@ gpy_object_t * gpy_object_classobj_new (gpy_typedef_t * type, unsigned char * codeaddr = gpy_object_staticmethod_getaddr (field_init); gpy_assert (codeaddr); + debug ("calling the __init__!\n"); __field_init_ptr c = (__field_init_ptr)codeaddr; c (self); diff --git a/libgpython/runtime/py-obj_classmethod.c b/libgpython/runtime/py-obj_classmethod.c index e69de29bb2d..815c017b886 100644 --- a/libgpython/runtime/py-obj_classmethod.c +++ b/libgpython/runtime/py-obj_classmethod.c @@ -0,0 +1,114 @@ +/* This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#include <gmp.h> +#include <mpfr.h> + +#include <gpython/gpython.h> +#include <gpython/vectors.h> +#include <gpython/objects.h> + +struct gpy_object_classmethod_t { + const unsigned char * code; + const char * identifier; + unsigned int nargs; +}; + +gpy_object_t * gpy_object_classmethod_new (gpy_typedef_t * type, + gpy_object_t ** args) +{ + gpy_object_t * retval = NULL_OBJECT; + + bool check = gpy_args_check_fmt (args, "s,p,i."); + gpy_assert (check); + + char * id = gpy_args_lit_parse_string (args[0]); + unsigned char * code_addr = gpy_args_lit_parse_pointer (args[1]); + int nargs = gpy_args_lit_parse_int (args[2]); + + struct gpy_object_classmethod_t * self = gpy_malloc (type->state_size); + self->identifier = id; + self->code = code_addr; + self->nargs = nargs; + + retval = gpy_create_object_decl (type, self); + + return retval; +} + +/* free's the object state not the */ +void gpy_object_classmethod_dealloc (gpy_object_t * self) +{ + gpy_assert (self->T == TYPE_OBJECT_DECL); + gpy_object_state_t * object_state = self->o.object_state; + + gpy_free (object_state->state); + object_state->state = NULL; +} + +void gpy_object_classmethod_print (gpy_object_t * self, FILE *fd, bool newline) +{ + fprintf (fd, "class method instance <%p> ", (void *)self); + if (newline) + fprintf (fd, "\n"); +} + +gpy_object_t * gpy_object_classmethod_call (gpy_object_t * self, + gpy_object_t ** args) +{ + gpy_object_t * retval = NULL_OBJECT; + gpy_assert (self->T == TYPE_OBJECT_DECL); + + struct gpy_object_classmethod_t * state = self->o.object_state->state; + if (!state->code) + { + fndecl fnptr = (fndecl)state->code; + fnptr (args); + } + return retval; +} + +unsigned char * gpy_object_classmethod_getaddr (gpy_object_t * self) +{ + gpy_object_state_t * state = self->o.object_state; + struct gpy_object_classmethod_t * s = state->state; + return s->code; +} + +static struct gpy_typedef_t class_functor_obj = { + "classmethod", + sizeof (struct gpy_object_classmethod_t), + &gpy_object_classmethod_new, + &gpy_object_classmethod_dealloc, + &gpy_object_classmethod_print, + &gpy_object_classmethod_call, + NULL, + NULL +}; + +void gpy_obj_classmethod_mod_init (gpy_vector_t * const vec) +{ + gpy_vec_push (vec, &class_functor_obj); +} diff --git a/libgpython/runtime/py-obj_staticmethod.c b/libgpython/runtime/py-obj_staticmethod.c index b81075d8f09..0f287e18f9e 100644 --- a/libgpython/runtime/py-obj_staticmethod.c +++ b/libgpython/runtime/py-obj_staticmethod.c @@ -109,7 +109,7 @@ static struct gpy_typedef_t functor_obj = { NULL }; -void gpy_obj_functor_mod_init (gpy_vector_t * const vec) +void gpy_obj_staticmethod_mod_init (gpy_vector_t * const vec) { gpy_vec_push (vec, &functor_obj); } diff --git a/libgpython/runtime/py-runtime.c b/libgpython/runtime/py-runtime.c index 2f234db8eb3..a518ba9db69 100644 --- a/libgpython/runtime/py-runtime.c +++ b/libgpython/runtime/py-runtime.c @@ -46,8 +46,9 @@ static void gpy_rr_init_primitives (void) { gpy_obj_integer_mod_init (__GPY_GLOBL_PRIMITIVES); - gpy_obj_functor_mod_init (__GPY_GLOBL_PRIMITIVES); + gpy_obj_staticmethod_mod_init (__GPY_GLOBL_PRIMITIVES); gpy_obj_class_mod_init (__GPY_GLOBL_PRIMITIVES); + gpy_obj_classmethod_mod_init (__GPY_GLOBL_PRIMITIVES); } static @@ -80,7 +81,7 @@ void gpy_rr_extend_runtime_stack (int nslots) __GPY_GLOBL_RR_STACK = gpy_realloc (__GPY_GLOBL_RR_STACK, size); __GPY_GLOBL_RR_STACK_POINTER = __GPY_GLOBL_RR_STACK; - __GPY_GLOBL_RR_STACK_POINTER += 3+nslots; + __GPY_GLOBL_RR_STACK2_POINTER += 3+nslots; } void gpy_rr_init_runtime (void) @@ -104,7 +105,7 @@ gpy_object_attrib_t * gpy_rr_fold_attribute (const char * identifier, attrib->identifier = identifier; if (code_addr) { - gpy_object_t * f = gpy_rr_fold_functor_decl (identifier, code_addr); + gpy_object_t * f = gpy_rr_fold_classmethod_decl (identifier, code_addr); attrib->addr = f; } else @@ -181,8 +182,8 @@ gpy_object_t * gpy_rr_fold_class_decl (gpy_object_attrib_t ** attribs, return retval; } -gpy_object_t * gpy_rr_fold_functor_decl (const char * identifier, - unsigned char * code_addr) +gpy_object_t * gpy_rr_fold_staticmethod_decl (const char * identifier, + unsigned char * code_addr) { gpy_object_t * retval = NULL_OBJECT; @@ -218,13 +219,58 @@ gpy_object_t * gpy_rr_fold_functor_decl (const char * identifier, retval = def->tp_new (def, args); gpy_free (args); - debug ("initilized function object <%p> to <%s>!\n", + debug ("initilized staticmethod object <%p> to <%s>!\n", (void*)retval, identifier); gpy_assert (retval->T == TYPE_OBJECT_DECL); return retval; } +gpy_object_t * gpy_rr_fold_classmethod_decl (const char * identifier, + unsigned char * code_addr) +{ + gpy_object_t * retval = NULL_OBJECT; + + gpy_object_t ** args = (gpy_object_t **) + gpy_calloc (4, sizeof(gpy_object_t*)); + + gpy_literal_t i; + i.type = TYPE_STRING; + i.literal.string = (char *)identifier; + + gpy_literal_t p; + p.type = TYPE_ADDR; + p.literal.addr = code_addr; + + gpy_literal_t n; + n.type = TYPE_INTEGER; + n.literal.integer = 0; + + gpy_object_t a1 = { .T = TYPE_OBJECT_LIT, .o.literal = &i }; + gpy_object_t a2 = { .T = TYPE_OBJECT_LIT, .o.literal = &p }; + gpy_object_t a3 = { .T = TYPE_OBJECT_LIT, .o.literal = &n }; + gpy_object_t a4 = { .T = TYPE_NULL, .o.literal = NULL }; + + args[0] = &a1; + args[1] = &a2; + args[2] = &a3; + args[3] = &a4; + + gpy_typedef_t * def = (gpy_typedef_t *) + __GPY_GLOBL_PRIMITIVES->vector[1]; + gpy_assert (def); + + retval = def->tp_new (def, args); + gpy_free (args); + + debug ("initilized classmethod object <%p> to <%s>!\n", + (void*)retval, identifier); + gpy_assert (retval->T == TYPE_OBJECT_DECL); + + return retval; +} + + gpy_object_t * gpy_rr_fold_call (gpy_object_t * decl, int nargs, ...) { gpy_object_t * retval = NULL_OBJECT; @@ -243,6 +289,8 @@ gpy_object_t * gpy_rr_fold_call (gpy_object_t * decl, int nargs, ...) return retval; } + + unsigned char * gpy_rr_eval_attrib_reference (gpy_object_t * base, const char * attrib) { |