lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [1/5] git commit: Declare variables for XS args
Date Sun, 20 Jul 2014 11:01:51 GMT
Repository: lucy-clownfish
Updated Branches:
  refs/heads/master fc58f3571 -> 5e3a4105c


Declare variables for XS args

Also prefix the variable names with "arg_" to avoid clashes with other
local variable names.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/918e37e3
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/918e37e3
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/918e37e3

Branch: refs/heads/master
Commit: 918e37e317c06cc5e596d7499f223247dc8cacd3
Parents: fc58f35
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Sat Jul 19 19:25:59 2014 +0200
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Sat Jul 19 21:08:03 2014 +0200

----------------------------------------------------------------------
 compiler/src/CFCPerlConstructor.c | 20 +++++---
 compiler/src/CFCPerlMethod.c      | 93 +++++++++++++++++++---------------
 compiler/src/CFCPerlSub.c         | 48 +++++++++++++++---
 compiler/src/CFCPerlSub.h         | 10 ++++
 4 files changed, 117 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/918e37e3/compiler/src/CFCPerlConstructor.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlConstructor.c b/compiler/src/CFCPerlConstructor.c
index eeaec24..63db008 100644
--- a/compiler/src/CFCPerlConstructor.c
+++ b/compiler/src/CFCPerlConstructor.c
@@ -91,9 +91,10 @@ char*
 CFCPerlConstructor_xsub_def(CFCPerlConstructor *self) {
     const char *c_name = self->sub.c_name;
     CFCParamList *param_list = self->sub.param_list;
-    const char   *name_list  = CFCParamList_name_list(param_list);
+    char         *name_list  = CFCPerlSub_arg_name_list((CFCPerlSub*)self);
     CFCVariable **arg_vars   = CFCParamList_get_variables(param_list);
     const char   *func_sym   = CFCFunction_full_func_sym(self->init_func);
+    char *arg_decls    = CFCPerlSub_arg_declarations((CFCPerlSub*)self);
     char *allot_params = CFCPerlSub_build_allot_params((CFCPerlSub*)self);
     CFCVariable *self_var       = arg_vars[0];
     CFCType     *self_type      = CFCVariable_get_type(self_var);
@@ -106,8 +107,9 @@ CFCPerlConstructor_xsub_def(CFCPerlConstructor *self) {
         CFCType *type = CFCVariable_get_type(var);
         if (CFCType_is_object(type) && CFCType_decremented(type)) {
             const char *name = CFCVariable_micro_sym(var);
-            refcount_mods = CFCUtil_cat(refcount_mods, "\n    CFISH_INCREF(",
-                                        name, ");", NULL);
+            refcount_mods
+                = CFCUtil_cat(refcount_mods, "\n    CFISH_INCREF(arg_", name,
+                              ");", NULL);
         }
     }
 
@@ -115,6 +117,8 @@ CFCPerlConstructor_xsub_def(CFCPerlConstructor *self) {
         "XS(%s);\n"
         "XS(%s) {\n"
         "    dXSARGS;\n"
+        "    %s arg_self;\n"
+        "%s"
         "    CFISH_UNUSED_VAR(cv);\n"
         "    if (items < 1) { CFISH_THROW(CFISH_ERR, \"Usage: %%s(class_name, ...)\",
 GvNAME(CvGV(cv))); }\n"
         "    SP -= items;\n"
@@ -122,7 +126,7 @@ CFCPerlConstructor_xsub_def(CFCPerlConstructor *self) {
         "    %s\n"
         // Create "self" last, so that earlier exceptions while fetching
         // params don't trigger a bad invocation of DESTROY.
-        "    %s self = (%s)XSBind_new_blank_obj(ST(0));%s\n"
+        "    arg_self = (%s)XSBind_new_blank_obj(ST(0));%s\n"
         "\n"
         "    %s retval = %s(%s);\n"
         "    if (retval) {\n"
@@ -136,12 +140,14 @@ CFCPerlConstructor_xsub_def(CFCPerlConstructor *self) {
         "    XSRETURN(1);\n"
         "}\n\n";
     char *xsub_def
-        = CFCUtil_sprintf(pattern, c_name, c_name, allot_params, self_type_str,
-                          self_type_str, refcount_mods, self_type_str,
-                          func_sym, name_list);
+        = CFCUtil_sprintf(pattern, c_name, c_name, self_type_str, arg_decls,
+                          allot_params, self_type_str, refcount_mods,
+                          self_type_str, func_sym, name_list);
 
     FREEMEM(refcount_mods);
+    FREEMEM(arg_decls);
     FREEMEM(allot_params);
+    FREEMEM(name_list);
 
     return xsub_def;
 }

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/918e37e3/compiler/src/CFCPerlMethod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlMethod.c b/compiler/src/CFCPerlMethod.c
index d9d8255..a574b93 100644
--- a/compiler/src/CFCPerlMethod.c
+++ b/compiler/src/CFCPerlMethod.c
@@ -153,11 +153,11 @@ CFCPerlMethod_xsub_def(CFCPerlMethod *self) {
 
 static char*
 S_xsub_body(CFCPerlMethod *self) {
-    CFCMethod    *method        = self->method;
-    CFCParamList *param_list    = CFCMethod_get_param_list(method);
-    CFCVariable **arg_vars      = CFCParamList_get_variables(param_list);
-    const char   *name_list     = CFCParamList_name_list(param_list);
-    char *body = CFCUtil_strdup("");
+    CFCMethod    *method     = self->method;
+    CFCParamList *param_list = CFCMethod_get_param_list(method);
+    CFCVariable **arg_vars   = CFCParamList_get_variables(param_list);
+    char         *name_list  = CFCPerlSub_arg_name_list((CFCPerlSub*)self);
+    char         *body       = CFCUtil_strdup("");
 
     CFCParcel *parcel = CFCMethod_get_parcel(method);
     const char *class_name = CFCMethod_get_class_name(method);
@@ -183,7 +183,7 @@ S_xsub_body(CFCPerlMethod *self) {
         CFCVariable *var = arg_vars[i];
         CFCType     *type = CFCVariable_get_type(var);
         if (CFCType_is_object(type) && CFCType_decremented(type)) {
-            body = CFCUtil_cat(body, "CFISH_INCREF(",
+            body = CFCUtil_cat(body, "CFISH_INCREF(arg_",
                                CFCVariable_micro_sym(var), ");\n    ", NULL);
         }
     }
@@ -214,6 +214,8 @@ S_xsub_body(CFCPerlMethod *self) {
         FREEMEM(assignment);
     }
 
+    FREEMEM(name_list);
+
     return body;
 }
 
@@ -226,8 +228,8 @@ S_self_assign_statement(CFCPerlMethod *self, CFCType *type) {
         CFCUtil_die("Not an object type: %s", type_c);
     }
     const char *class_var = CFCType_get_class_var(type);
-    char pattern[] = "%s self = (%s)XSBind_sv_to_cfish_obj(ST(0), %s, NULL);";
-    char *statement = CFCUtil_sprintf(pattern, type_c, type_c, class_var);
+    char pattern[] = "arg_self = (%s)XSBind_sv_to_cfish_obj(ST(0), %s, NULL);";
+    char *statement = CFCUtil_sprintf(pattern, type_c, class_var);
 
     return statement;
 }
@@ -239,15 +241,19 @@ S_xsub_def_labeled_params(CFCPerlMethod *self) {
     CFCVariable **arg_vars   = CFCParamList_get_variables(param_list);
     CFCVariable *self_var    = arg_vars[0];
     CFCType     *self_type   = CFCVariable_get_type(self_var);
+    const char  *self_type_c    = CFCType_to_c(self_type);
     const char  *self_micro_sym = CFCVariable_micro_sym(self_var);
-    char *self_assign = S_self_assign_statement(self, self_type);
+    char *arg_decls    = CFCPerlSub_arg_declarations((CFCPerlSub*)self);
+    char *self_assign  = S_self_assign_statement(self, self_type);
     char *allot_params = CFCPerlSub_build_allot_params((CFCPerlSub*)self);
-    char *body = S_xsub_body(self);
+    char *body         = S_xsub_body(self);
 
     char pattern[] =
         "XS(%s);\n"
         "XS(%s) {\n"
         "    dXSARGS;\n"
+        "    %s arg_self;\n"
+        "%s"
         "    CFISH_UNUSED_VAR(cv);\n"
         "    if (items < 1) { CFISH_THROW(CFISH_ERR, \"Usage: %%s(%s, ...)\",  GvNAME(CvGV(cv)));
}\n"
         "    SP -= items;\n"
@@ -260,9 +266,10 @@ S_xsub_def_labeled_params(CFCPerlMethod *self) {
         "    %s\n"
         "}\n";
     char *xsub_def
-        = CFCUtil_sprintf(pattern, c_name, c_name, self_micro_sym,
-                          allot_params, self_assign, body);
+        = CFCUtil_sprintf(pattern, c_name, c_name, self_type_c, arg_decls,
+                          self_micro_sym, allot_params, self_assign, body);
 
+    FREEMEM(arg_decls);
     FREEMEM(self_assign);
     FREEMEM(allot_params);
     FREEMEM(body);
@@ -274,9 +281,14 @@ S_xsub_def_positional_args(CFCPerlMethod *self) {
     CFCMethod *method = self->method;
     CFCParamList *param_list = CFCMethod_get_param_list(method);
     CFCVariable **arg_vars = CFCParamList_get_variables(param_list);
+    CFCVariable *self_var    = arg_vars[0];
+    CFCType     *self_type   = CFCVariable_get_type(self_var);
+    const char  *self_type_c = CFCType_to_c(self_type);
     const char **arg_inits = CFCParamList_get_initial_values(param_list);
     unsigned num_vars = (unsigned)CFCParamList_num_vars(param_list);
-    char *body = S_xsub_body(self);
+    char *arg_decls   = CFCPerlSub_arg_declarations((CFCPerlSub*)self);
+    char *self_assign = S_self_assign_statement(self, self_type);
+    char *body        = S_xsub_body(self);
 
     // Determine how many args are truly required and build an error check.
     unsigned min_required = 0;
@@ -312,65 +324,64 @@ S_xsub_def_positional_args(CFCPerlMethod *self) {
 
     // Var assignments.
     char *var_assignments = CFCUtil_strdup("");
-    for (unsigned i = 0; i < num_vars; i++) {
+    for (unsigned i = 1; i < num_vars; i++) {
         CFCVariable *var = arg_vars[i];
         const char  *val = arg_inits[i];
         const char  *var_name = CFCVariable_micro_sym(var);
         CFCType     *var_type = CFCVariable_get_type(var);
         const char  *type_c   = CFCType_to_c(var_type);
 
-        if (i == 0) {    // self
-            char *statement = S_self_assign_statement(self, var_type);
-            var_assignments = CFCUtil_cat(var_assignments, statement, NULL);
+        char perl_stack_var[30];
+        sprintf(perl_stack_var, "ST(%u)", i);
+        char *conversion
+            = CFCPerlTypeMap_from_perl(var_type, perl_stack_var);
+        if (!conversion) {
+            CFCUtil_die("Can't map type '%s'", type_c);
+        }
+        if (val) {
+            char pattern[] =
+                "\n    arg_%s = ( items >= %u && XSBind_sv_defined(ST(%u)) )"
+                " ? %s : %s;";
+            char *statement = CFCUtil_sprintf(pattern, var_name, i, i,
+                                              conversion, val);
+            var_assignments
+                = CFCUtil_cat(var_assignments, statement, NULL);
             FREEMEM(statement);
         }
         else {
-            char perl_stack_var[30];
-            sprintf(perl_stack_var, "ST(%u)", i);
-            char *conversion
-                = CFCPerlTypeMap_from_perl(var_type, perl_stack_var);
-            if (!conversion) {
-                CFCUtil_die("Can't map type '%s'", type_c);
-            }
-            if (val) {
-                char pattern[] =
-                    "\n    %s %s = ( items >= %u && XSBind_sv_defined(ST(%u))
)"
-                    " ? %s : %s;";
-                char *statement = CFCUtil_sprintf(pattern, type_c, var_name, i,
-                                                  i, conversion, val);
-                var_assignments
-                    = CFCUtil_cat(var_assignments, statement, NULL);
-                FREEMEM(statement);
-            }
-            else {
-                var_assignments
-                    = CFCUtil_cat(var_assignments, "\n    ", type_c, " ",
-                                  var_name, " = ", conversion, ";", NULL);
-            }
-            FREEMEM(conversion);
+            var_assignments
+                = CFCUtil_cat(var_assignments, "\n    arg_", var_name, " = ",
+                              conversion, ";", NULL);
         }
+        FREEMEM(conversion);
     }
 
     char pattern[] =
         "XS(%s);\n"
         "XS(%s) {\n"
         "    dXSARGS;\n"
+        "    %s arg_self;\n"
+        "%s"
         "    CFISH_UNUSED_VAR(cv);\n"
         "    SP -= items;\n"
         "    %s;\n"
         "\n"
         "    /* Extract vars from Perl stack. */\n"
         "    %s\n"
+        "    %s\n"
         "\n"
         "    /* Execute */\n"
         "    %s\n"
         "}\n";
     char *xsub
         = CFCUtil_sprintf(pattern, self->sub.c_name, self->sub.c_name,
-                          num_args_check, var_assignments, body);
+                          self_type_c, arg_decls, num_args_check, self_assign,
+                          var_assignments, body);
 
     FREEMEM(num_args_check);
     FREEMEM(var_assignments);
+    FREEMEM(arg_decls);
+    FREEMEM(self_assign);
     FREEMEM(body);
     return xsub;
 }

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/918e37e3/compiler/src/CFCPerlSub.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlSub.c b/compiler/src/CFCPerlSub.c
index ba5e84d..48afd34 100644
--- a/compiler/src/CFCPerlSub.c
+++ b/compiler/src/CFCPerlSub.c
@@ -155,7 +155,7 @@ S_allot_params_arg(CFCType *type, const char *label, int required) {
         const char *allocation = use_sv_buffer
                                  ? "alloca(cfish_SStr_size())"
                                  : "NULL";
-        const char pattern[] = "ALLOT_OBJ(&%s, \"%s\", %u, %s, %s, %s)";
+        const char pattern[] = "ALLOT_OBJ(&arg_%s, \"%s\", %u, %s, %s, %s)";
         char *arg = CFCUtil_sprintf(pattern, label, label, label_len,
                                     req_string, class_var, allocation);
         return arg;
@@ -165,7 +165,7 @@ S_allot_params_arg(CFCType *type, const char *label, int required) {
             const char *prim_type = prim_type_to_allot_macro[i].prim_type;
             if (strcmp(prim_type, type_c_string) == 0) {
                 const char *allot = prim_type_to_allot_macro[i].allot_macro;
-                char pattern[] = "%s(&%s, \"%s\", %u, %s)";
+                char pattern[] = "%s(&arg_%s, \"%s\", %u, %s)";
                 char *arg = CFCUtil_sprintf(pattern, allot, label, label,
                                             label_len, req_string);
                 return arg;
@@ -178,6 +178,42 @@ S_allot_params_arg(CFCType *type, const char *label, int required) {
 }
 
 char*
+CFCPerlSub_arg_declarations(CFCPerlSub *self) {
+    CFCParamList *param_list = self->param_list;
+    CFCVariable **arg_vars   = CFCParamList_get_variables(param_list);
+    size_t        num_vars   = CFCParamList_num_vars(param_list);
+    char         *decls      = CFCUtil_strdup("");
+
+    // Declare variables.
+    for (size_t i = 1; i < num_vars; i++) {
+        CFCVariable *arg_var  = arg_vars[i];
+        CFCType     *type     = CFCVariable_get_type(arg_var);
+        const char  *type_str = CFCType_to_c(type);
+        const char  *var_name = CFCVariable_micro_sym(arg_var);
+        decls = CFCUtil_cat(decls, "    ", type_str, " arg_", var_name,
+                            ";\n", NULL);
+    }
+
+    return decls;
+}
+
+char*
+CFCPerlSub_arg_name_list(CFCPerlSub *self) {
+    CFCParamList  *param_list = self->param_list;
+    CFCVariable  **arg_vars   = CFCParamList_get_variables(param_list);
+    size_t        num_vars   = CFCParamList_num_vars(param_list);
+    char          *name_list  = CFCUtil_strdup("arg_self");
+
+    for (int i = 1; i < num_vars; i++) {
+        CFCVariable *arg_var  = arg_vars[i];
+        const char  *var_name = CFCVariable_micro_sym(arg_vars[i]);
+        name_list = CFCUtil_cat(name_list, ", arg_", var_name, NULL);
+    }
+
+    return name_list;
+}
+
+char*
 CFCPerlSub_build_allot_params(CFCPerlSub *self) {
     CFCParamList *param_list = self->param_list;
     CFCVariable **arg_vars   = CFCParamList_get_variables(param_list);
@@ -187,16 +223,16 @@ CFCPerlSub_build_allot_params(CFCPerlSub *self) {
 
     // Declare variables and assign default values.
     for (size_t i = 1; i < num_vars; i++) {
-        CFCVariable *arg_var = arg_vars[i];
-        const char  *val     = arg_inits[i];
-        const char  *local_c = CFCVariable_local_c(arg_var);
+        CFCVariable *arg_var  = arg_vars[i];
+        const char  *val      = arg_inits[i];
+        const char  *var_name = CFCVariable_micro_sym(arg_var);
         if (val == NULL) {
             CFCType *arg_type = CFCVariable_get_type(arg_var);
             val = CFCType_is_object(arg_type)
                   ? "NULL"
                   : "0";
         }
-        allot_params = CFCUtil_cat(allot_params, local_c, " = ", val,
+        allot_params = CFCUtil_cat(allot_params, "arg_", var_name, " = ", val,
                                    ";\n    ", NULL);
     }
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/918e37e3/compiler/src/CFCPerlSub.h
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlSub.h b/compiler/src/CFCPerlSub.h
index 6bc846c..54d1917 100644
--- a/compiler/src/CFCPerlSub.h
+++ b/compiler/src/CFCPerlSub.h
@@ -71,6 +71,16 @@ CFCPerlSub_destroy(CFCPerlSub *self);
 char*
 CFCPerlSub_params_hash_def(CFCPerlSub *self);
 
+/** Generate C declarations for the variables holding the arguments.
+ */
+char*
+CFCPerlSub_arg_declarations(CFCPerlSub *self);
+
+/** Create a comma-separated list of argument names prefixed by "arg_".
+ */
+char*
+CFCPerlSub_arg_name_list(CFCPerlSub *self);
+
 /** Generate code which will invoke XSBind_allot_params() to parse labeled
  * parameters supplied to an XSUB.
  */


Mime
View raw message