Simplify symbol accessors in CFCMethod
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/f6f0d830 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/f6f0d830 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/f6f0d830 Branch: refs/heads/master Commit: f6f0d830890a28bedf428c9cfcf9b655aaeae07c Parents: 2452a90 Author: Nick Wellnhofer <[email protected]> Authored: Wed Dec 26 22:35:04 2012 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Fri Dec 28 22:23:10 2012 +0100 ---------------------------------------------------------------------- clownfish/compiler/perl/lib/Clownfish/CFC.xs | 36 ++-------- clownfish/compiler/src/CFCBindClass.c | 48 +++++-------- clownfish/compiler/src/CFCBindMethod.c | 33 ++------- clownfish/compiler/src/CFCDumpable.c | 18 +---- clownfish/compiler/src/CFCMethod.c | 76 +++++++-------------- clownfish/compiler/src/CFCMethod.h | 35 ++++------ clownfish/compiler/src/CFCPerlMethod.c | 24 +++---- 7 files changed, 88 insertions(+), 182 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/f6f0d830/clownfish/compiler/perl/lib/Clownfish/CFC.xs ---------------------------------------------------------------------- diff --git a/clownfish/compiler/perl/lib/Clownfish/CFC.xs b/clownfish/compiler/perl/lib/Clownfish/CFC.xs index 3a23bd7..415338e 100644 --- a/clownfish/compiler/perl/lib/Clownfish/CFC.xs +++ b/clownfish/compiler/perl/lib/Clownfish/CFC.xs @@ -884,47 +884,27 @@ ALIAS: short_typedef = 4 full_typedef = 5 CODE: - size_t size = 0; - switch (ix) { - case 1: - size = CFCMethod_short_method_sym(self, invoker, NULL, 0); - break; - case 2: - size = CFCMethod_full_method_sym(self, invoker, NULL, 0); - break; - case 3: - size = CFCMethod_full_offset_sym(self, invoker, NULL, 0); - break; - case 4: - size = CFCMethod_short_typedef(self, invoker, NULL, 0); - break; - case 5: - size = CFCMethod_full_typedef(self, invoker, NULL, 0); - break; - default: croak("Unexpected ix: %d", (int)ix); - } - RETVAL = newSV(size); - SvPOK_on(RETVAL); - char *buf = SvPVX(RETVAL); + char *buf; switch (ix) { case 1: - CFCMethod_short_method_sym(self, invoker, buf, size); + buf = CFCMethod_short_method_sym(self, invoker); break; case 2: - CFCMethod_full_method_sym(self, invoker, buf, size); + buf = CFCMethod_full_method_sym(self, invoker); break; case 3: - CFCMethod_full_offset_sym(self, invoker, buf, size); + buf = CFCMethod_full_offset_sym(self, invoker); break; case 4: - CFCMethod_short_typedef(self, invoker, buf, size); + buf = CFCMethod_short_typedef(self, invoker); break; case 5: - CFCMethod_full_typedef(self, invoker, buf, size); + buf = CFCMethod_full_typedef(self, invoker); break; default: croak("Unexpected ix: %d", (int)ix); } - SvCUR_set(RETVAL, strlen(buf)); + RETVAL = newSVpvn(buf, strlen(buf)); + FREEMEM(buf); OUTPUT: RETVAL void http://git-wip-us.apache.org/repos/asf/lucy/blob/f6f0d830/clownfish/compiler/src/CFCBindClass.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindClass.c b/clownfish/compiler/src/CFCBindClass.c index 50d9141..a053e5c 100644 --- a/clownfish/compiler/src/CFCBindClass.c +++ b/clownfish/compiler/src/CFCBindClass.c @@ -262,21 +262,16 @@ CFCBindClass_to_c_data(CFCBindClass *self) { for (int meth_num = 0; methods[meth_num] != NULL; meth_num++) { CFCMethod *method = methods[meth_num]; - size_t off_sym_size - = CFCMethod_full_offset_sym(method, client, NULL, 0); - char *full_offset_sym = (char*)MALLOCATE(off_sym_size); - CFCMethod_full_offset_sym(method, client, full_offset_sym, - off_sym_size); - char meth_num_str[20]; - sprintf(meth_num_str, "%d", meth_num); - + char *full_offset_sym = CFCMethod_full_offset_sym(method, client); // Create offset in bytes for the method from the top of the VTable // object. - offsets = CFCUtil_cat(offsets, "size_t ", full_offset_sym, " = ", - "offsetof(cfish_VTable, method_ptrs) + ", - meth_num_str, " * sizeof(cfish_method_t);\n", - NULL); + const char pattern[] = + "size_t %s = offsetof(cfish_VTable, method_ptrs)" + " + %d * sizeof(cfish_method_t);\n"; + char *offset = CFCUtil_sprintf(pattern, full_offset_sym, meth_num); + offsets = CFCUtil_cat(offsets, offset, NULL); FREEMEM(full_offset_sym); + FREEMEM(offset); } if (fresh_methods[0] != NULL) { @@ -562,31 +557,26 @@ S_short_names(CFCBindClass *self) { CFCMethod **methods = CFCClass_methods(client); for (int i = 0; methods[i] != NULL; i++) { CFCMethod *meth = methods[i]; + static const char pattern[] = " #define %s %s\n"; // Method invocation symbols. - size_t size = CFCMethod_short_method_sym(meth, client, NULL, 0); - char *short_sym = (char*)MALLOCATE(size); - CFCMethod_short_method_sym(meth, client, short_sym, size); - size = CFCMethod_full_method_sym(meth, client, NULL, 0); - char *full_sym = (char*)MALLOCATE(size); - CFCMethod_full_method_sym(meth, client, full_sym, size); - short_names = CFCUtil_cat(short_names, " #define ", short_sym, - " ", full_sym, "\n", NULL); + char *short_sym = CFCMethod_short_method_sym(meth, client); + char *full_sym = CFCMethod_full_method_sym(meth, client); + char *define_sym = CFCUtil_sprintf(pattern, short_sym, full_sym); + short_names = CFCUtil_cat(short_names, define_sym, NULL); FREEMEM(short_sym); FREEMEM(full_sym); + FREEMEM(define_sym); // Method typedefs. - size = CFCMethod_short_typedef(meth, client, NULL, 0); - char *short_typedef = (char*)MALLOCATE(size); - CFCMethod_short_typedef(meth, client, short_typedef, size); - size = CFCMethod_full_typedef(meth, client, NULL, 0); - char *full_typedef = (char*)MALLOCATE(size); - CFCMethod_full_typedef(meth, client, full_typedef, size); - short_names = CFCUtil_cat(short_names, " #define ", - short_typedef, " ", full_typedef, "\n", - NULL); + char *short_typedef = CFCMethod_short_typedef(meth, client); + char *full_typedef = CFCMethod_full_typedef(meth, client); + char *define_typedef = CFCUtil_sprintf(pattern, short_typedef, + full_typedef); + short_names = CFCUtil_cat(short_names, define_typedef, NULL); FREEMEM(short_typedef); FREEMEM(full_typedef); + FREEMEM(define_typedef); } } short_names = CFCUtil_cat(short_names, "#endif /* ", http://git-wip-us.apache.org/repos/asf/lucy/blob/f6f0d830/clownfish/compiler/src/CFCBindMethod.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindMethod.c b/clownfish/compiler/src/CFCBindMethod.c index c9ee631..eec71f1 100644 --- a/clownfish/compiler/src/CFCBindMethod.c +++ b/clownfish/compiler/src/CFCBindMethod.c @@ -67,13 +67,8 @@ S_final_method_def(CFCMethod *method, CFCClass *klass) { const char *arg_names = CFCParamList_name_list(CFCMethod_get_param_list(method)); - size_t meth_sym_size = CFCMethod_full_method_sym(method, klass, NULL, 0); - char *full_meth_sym = (char*)MALLOCATE(meth_sym_size); - CFCMethod_full_method_sym(method, klass, full_meth_sym, meth_sym_size); - - size_t offset_sym_size = CFCMethod_full_offset_sym(method, klass, NULL, 0); - char *full_offset_sym = (char*)MALLOCATE(offset_sym_size); - CFCMethod_full_offset_sym(method, klass, full_offset_sym, offset_sym_size); + char *full_meth_sym = CFCMethod_full_method_sym(method, klass); + char *full_offset_sym = CFCMethod_full_offset_sym(method, klass); const char pattern[] = "extern size_t %s;\n" @@ -98,17 +93,9 @@ S_virtual_method_def(CFCMethod *method, CFCClass *klass) { const char *visibility = CFCClass_included(klass) ? "CHY_IMPORT" : "CHY_EXPORT"; - size_t meth_sym_size = CFCMethod_full_method_sym(method, klass, NULL, 0); - char *full_meth_sym = (char*)MALLOCATE(meth_sym_size); - CFCMethod_full_method_sym(method, klass, full_meth_sym, meth_sym_size); - - size_t offset_sym_size = CFCMethod_full_offset_sym(method, klass, NULL, 0); - char *full_offset_sym = (char*)MALLOCATE(offset_sym_size); - CFCMethod_full_offset_sym(method, klass, full_offset_sym, offset_sym_size); - - size_t full_typedef_size = CFCMethod_full_typedef(method, klass, NULL, 0); - char *full_typedef = (char*)MALLOCATE(full_typedef_size); - CFCMethod_full_typedef(method, klass, full_typedef, full_typedef_size); + char *full_meth_sym = CFCMethod_full_method_sym(method, klass); + char *full_offset_sym = CFCMethod_full_offset_sym(method, klass); + char *full_typedef = CFCMethod_full_typedef(method, klass); // Prepare parameter lists, minus invoker. The invoker gets forced to // "self" later. @@ -151,11 +138,7 @@ char* CFCBindMeth_typedef_dec(struct CFCMethod *method, CFCClass *klass) { const char *params = CFCParamList_to_c(CFCMethod_get_param_list(method)); const char *ret_type = CFCType_to_c(CFCMethod_get_return_type(method)); - - size_t full_typedef_size = CFCMethod_full_typedef(method, klass, NULL, 0); - char *full_typedef = (char*)MALLOCATE(full_typedef_size); - CFCMethod_full_typedef(method, klass, full_typedef, full_typedef_size); - + char *full_typedef = CFCMethod_full_typedef(method, klass); char *buf = CFCUtil_sprintf("typedef %s\n(*%s)(%s);\n", ret_type, full_typedef, params); FREEMEM(full_typedef); @@ -173,9 +156,7 @@ CFCBindMeth_spec_def(CFCMethod *method) { full_override_sym = CFCMethod_full_override_sym(method); } - size_t offset_sym_size = CFCMethod_full_offset_sym(method, NULL, NULL, 0); - char *full_offset_sym = (char*)MALLOCATE(offset_sym_size); - CFCMethod_full_offset_sym(method, NULL, full_offset_sym, offset_sym_size); + char *full_offset_sym = CFCMethod_full_offset_sym(method, NULL); char pattern[] = " {\n" http://git-wip-us.apache.org/repos/asf/lucy/blob/f6f0d830/clownfish/compiler/src/CFCDumpable.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCDumpable.c b/clownfish/compiler/src/CFCDumpable.c index feec89b..14d6204 100644 --- a/clownfish/compiler/src/CFCDumpable.c +++ b/clownfish/compiler/src/CFCDumpable.c @@ -165,13 +165,8 @@ S_add_dump_method(CFCClass *klass) { const size_t BUF_SIZE = 400; char buf[BUF_SIZE]; - size_t full_typedef_size = CFCMethod_full_typedef(method, klass, NULL, 0); - char *full_typedef = (char*)MALLOCATE(full_typedef_size); - CFCMethod_full_typedef(method, klass, full_typedef, full_typedef_size); - - size_t full_meth_size = CFCMethod_full_method_sym(method, klass, NULL, 0); - char *full_meth = (char*)MALLOCATE(full_meth_size); - CFCMethod_full_method_sym(method, klass, full_meth, full_meth_size); + char *full_typedef = CFCMethod_full_typedef(method, klass); + char *full_meth = CFCMethod_full_method_sym(method, klass); if (parent && CFCClass_has_attribute(parent, "dumpable")) { const char pattern[] = @@ -226,13 +221,8 @@ S_add_load_method(CFCClass *klass) { const size_t BUF_SIZE = 400; char buf[BUF_SIZE]; - size_t full_typedef_size = CFCMethod_full_typedef(method, klass, NULL, 0); - char *full_typedef = (char*)MALLOCATE(full_typedef_size); - CFCMethod_full_typedef(method, klass, full_typedef, full_typedef_size); - - size_t full_meth_size = CFCMethod_full_method_sym(method, klass, NULL, 0); - char *full_meth = (char*)MALLOCATE(full_meth_size); - CFCMethod_full_method_sym(method, klass, full_meth, full_meth_size); + char *full_typedef = CFCMethod_full_typedef(method, klass); + char *full_meth = CFCMethod_full_method_sym(method, klass); if (parent && CFCClass_has_attribute(parent, "dumpable")) { const char pattern[] = http://git-wip-us.apache.org/repos/asf/lucy/blob/f6f0d830/clownfish/compiler/src/CFCMethod.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCMethod.c b/clownfish/compiler/src/CFCMethod.c index 329b109..6524a96 100644 --- a/clownfish/compiler/src/CFCMethod.c +++ b/clownfish/compiler/src/CFCMethod.c @@ -223,9 +223,8 @@ CFCMethod_finalize(CFCMethod *self) { return finalized; } -size_t -CFCMethod_short_method_sym(CFCMethod *self, CFCClass *invoker, char *buf, - size_t buf_size) { +static char* +S_short_method_sym(CFCMethod *self, CFCClass *invoker, const char *postfix) { const char *cnick; if (invoker) { cnick = CFCClass_get_cnick(invoker); @@ -233,16 +232,11 @@ CFCMethod_short_method_sym(CFCMethod *self, CFCClass *invoker, char *buf, else { cnick = CFCMethod_get_class_cnick(self); } - size_t needed = strlen(cnick) + 1 + strlen(self->macro_sym) + 1; - if (buf_size >= needed) { - sprintf(buf, "%s_%s", cnick, self->macro_sym); - } - return needed; + return CFCUtil_sprintf("%s_%s%s", cnick, self->macro_sym, postfix); } -size_t -CFCMethod_full_method_sym(CFCMethod *self, CFCClass *invoker, char *buf, - size_t buf_size) { +static char* +S_full_method_sym(CFCMethod *self, CFCClass *invoker, const char *postfix) { const char *Prefix; const char *cnick; if (invoker) { @@ -253,27 +247,23 @@ CFCMethod_full_method_sym(CFCMethod *self, CFCClass *invoker, char *buf, Prefix = CFCMethod_get_Prefix(self); cnick = CFCMethod_get_class_cnick(self); } - size_t needed = strlen(Prefix) - + strlen(cnick) - + 1 - + strlen(self->macro_sym) - + 1; - if (buf_size >= needed) { - sprintf(buf, "%s%s_%s", Prefix, cnick, self->macro_sym); - } - return needed; + return CFCUtil_sprintf("%s%s_%s%s", Prefix, cnick, self->macro_sym, + postfix); } -size_t -CFCMethod_full_offset_sym(CFCMethod *self, CFCClass *invoker, char *buf, - size_t buf_size) { - size_t needed = CFCMethod_full_method_sym(self, invoker, NULL, 0) - + strlen("_OFFSET"); - if (buf_size >= needed) { - CFCMethod_full_method_sym(self, invoker, buf, buf_size); - strcat(buf, "_OFFSET"); - } - return needed; +char* +CFCMethod_short_method_sym(CFCMethod *self, CFCClass *invoker) { + return S_short_method_sym(self, invoker, ""); +} + +char* +CFCMethod_full_method_sym(CFCMethod *self, CFCClass *invoker) { + return S_full_method_sym(self, invoker, ""); +} + +char* +CFCMethod_full_offset_sym(CFCMethod *self, CFCClass *invoker) { + return S_full_method_sym(self, invoker, "_OFFSET"); } const char* @@ -286,28 +276,14 @@ CFCMethod_micro_sym(CFCMethod *self) { return CFCSymbol_micro_sym((CFCSymbol*)self); } -size_t -CFCMethod_short_typedef(CFCMethod *self, CFCClass *invoker, char *buf, - size_t buf_size) { - size_t needed = CFCMethod_short_method_sym(self, invoker, NULL, 0) - + strlen("_t"); - if (buf_size >= needed) { - CFCMethod_short_method_sym(self, invoker, buf, buf_size); - strcat(buf, "_t"); - } - return needed; +char* +CFCMethod_short_typedef(CFCMethod *self, CFCClass *invoker) { + return S_short_method_sym(self, invoker, "_t"); } -size_t -CFCMethod_full_typedef(CFCMethod *self, CFCClass *invoker, char *buf, - size_t buf_size) { - size_t needed = CFCMethod_full_method_sym(self, invoker, NULL, 0) - + strlen("_t"); - if (buf_size >= needed) { - CFCMethod_full_method_sym(self, invoker, buf, buf_size); - strcat(buf, "_t"); - } - return needed; +char* +CFCMethod_full_typedef(CFCMethod *self, CFCClass *invoker) { + return S_full_method_sym(self, invoker, "_t"); } const char* http://git-wip-us.apache.org/repos/asf/lucy/blob/f6f0d830/clownfish/compiler/src/CFCMethod.h ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCMethod.h b/clownfish/compiler/src/CFCMethod.h index d39ac22..b7f01dd 100644 --- a/clownfish/compiler/src/CFCMethod.h +++ b/clownfish/compiler/src/CFCMethod.h @@ -110,11 +110,10 @@ CFCMethod_finalize(CFCMethod *self); * @param invoker Class for which the symbol is created. If invoker is NULL, * use the class where the method is defined. * - * @return the number of bytes which the symbol would occupy. + * @return the symbol. */ -size_t -CFCMethod_short_method_sym(CFCMethod *self, struct CFCClass *invoker, - char *buf, size_t buf_size); +char* +CFCMethod_short_method_sym(CFCMethod *self, struct CFCClass *invoker); /** * Create the fully-qualified symbol used to invoke the method, e.g. @@ -122,22 +121,20 @@ CFCMethod_short_method_sym(CFCMethod *self, struct CFCClass *invoker, * @param invoker Class for which the symbol is created. If invoker is NULL, * use the class where the method is defined. * - * @return the number of bytes which the symbol would occupy. + * @return the symbol. */ -size_t -CFCMethod_full_method_sym(CFCMethod *self, struct CFCClass *invoker, char *buf, - size_t buf_size); +char* +CFCMethod_full_method_sym(CFCMethod *self, struct CFCClass *invoker); /** Create the fully qualified name of the variable which stores the method's * vtable offset, e.g. "Crust_LobClaw_Pinch_OFFSET". * @param invoker Class for which the symbol is created. If invoker is NULL, * use the class where the method is defined. * - * @return the number of bytes which the symbol would occupy. + * @return the symbol. */ -size_t -CFCMethod_full_offset_sym(CFCMethod *self, struct CFCClass *invoker, char *buf, - size_t buf_size); +char* +CFCMethod_full_offset_sym(CFCMethod *self, struct CFCClass *invoker); const char* CFCMethod_get_macro_sym(CFCMethod *self); @@ -149,21 +146,19 @@ CFCMethod_micro_sym(CFCMethod *self); * @param invoker Class for which the symbol is created. If invoker is NULL, * use the class where the method is defined. * - * @return the number of bytes which the symbol would occupy. + * @return the symbol. */ -size_t -CFCMethod_short_typedef(CFCMethod *self, struct CFCClass *invoker, char *buf, - size_t buf_size); +char* +CFCMethod_short_typedef(CFCMethod *self, struct CFCClass *invoker); /** Create the fully-qualified typedef symbol, e.g. "Crust_Claw_Pinch_t". * @param invoker Class for which the symbol is created. If invoker is NULL, * use the class where the method is defined. * - * @return the number of bytes which the symbol would occupy. + * @return the symbol. */ -size_t -CFCMethod_full_typedef(CFCMethod *self, struct CFCClass *invoker, char *buf, - size_t buf_size); +char* +CFCMethod_full_typedef(CFCMethod *self, struct CFCClass *invoker); /** Returns the fully qualified name of the function which implements the * callback to the host in the event that a host method has been defined which http://git-wip-us.apache.org/repos/asf/lucy/blob/f6f0d830/clownfish/compiler/src/CFCPerlMethod.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCPerlMethod.c b/clownfish/compiler/src/CFCPerlMethod.c index 0a3de96..10bb9b2 100644 --- a/clownfish/compiler/src/CFCPerlMethod.c +++ b/clownfish/compiler/src/CFCPerlMethod.c @@ -168,20 +168,16 @@ S_xsub_body(CFCPerlMethod *self) { } // Extract the method function pointer. - size_t typedef_size - = CFCMethod_full_typedef(method, klass, NULL, 0); - char *full_typedef = (char*)MALLOCATE(typedef_size); - CFCMethod_full_typedef(method, klass, full_typedef, typedef_size); - - size_t meth_size - = CFCMethod_full_method_sym(method, klass, NULL, 0); - char *full_meth = (char*)MALLOCATE(meth_size); - CFCMethod_full_method_sym(method, klass, full_meth, meth_size); - body = CFCUtil_cat(body, full_typedef, " method = CFISH_METHOD_PTR(", - CFCClass_full_vtable_var(klass), ", ", - full_meth, ");\n ", NULL); + char *full_typedef = CFCMethod_full_typedef(method, klass); + char *full_meth = CFCMethod_full_method_sym(method, klass); + char *method_ptr + = CFCUtil_sprintf("%s method = CFISH_METHOD_PTR(%s, %s);\n ", + full_typedef, CFCClass_full_vtable_var(klass), + full_meth); + body = CFCUtil_cat(body, method_ptr, NULL); FREEMEM(full_typedef); FREEMEM(full_meth); + FREEMEM(method_ptr); // Compensate for functions which eat refcounts. for (int i = 0; arg_vars[i] != NULL; i++) { @@ -573,9 +569,7 @@ S_callback_refcount_mods(CFCMethod *method) { static char* S_invalid_callback_def(CFCMethod *method) { - size_t meth_sym_size = CFCMethod_full_method_sym(method, NULL, NULL, 0); - char *full_method_sym = (char*)MALLOCATE(meth_sym_size); - CFCMethod_full_method_sym(method, NULL, full_method_sym, meth_sym_size); + char *full_method_sym = CFCMethod_full_method_sym(method, NULL); const char *override_sym = CFCMethod_full_override_sym(method); CFCParamList *param_list = CFCMethod_get_param_list(method);
