From c1e5c89824b26b40ce10a78c1f61bd1b853ec0f0 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Thu, 3 Nov 2011 10:19:43 +0100 Subject: [PATCH] introduce isl_args structure that constains isl_arg list and size of options Before, the size of the options structure of a submodule was stored inside the corresponding child option of the parent. This however, requires that the options structure of the submodule is visible to the parent so that it can compute the size of this structure. Moreover, if the size of this structure changes, then the code containing the child option, which may reside in a different module, would have to be recompiled. Now, we store the size of the options structure together with the list of option descriptions. In particular, it is stored in the same module as the options structure itself, so that this structure no longer needs to be exposed. Signed-off-by: Sven Verdoolaege --- bound.c | 12 +++++----- cat.c | 11 +++++----- closure.c | 2 +- include/isl/arg.h | 44 +++++++++++++++++++++++-------------- include/isl/ctx.h | 11 +++++----- include/isl/options.h | 4 +--- isl_arg.c | 61 +++++++++++++++++++++++++-------------------------- isl_ctx.c | 37 ++++++++++++++++--------------- isl_ctx_private.h | 2 +- isl_options.c | 7 +++--- pip.c | 11 +++++----- 11 files changed, 105 insertions(+), 97 deletions(-) diff --git a/bound.c b/bound.c index 71150029..3c1cbeab 100644 --- a/bound.c +++ b/bound.c @@ -10,15 +10,15 @@ struct bound_options { int continue_on_error; }; -struct isl_arg bound_options_arg[] = { -ISL_ARG_CHILD(struct bound_options, isl, "isl", isl_options_arg, "isl options") +ISL_ARGS_START(struct bound_options, bound_options_args) +ISL_ARG_CHILD(struct bound_options, isl, "isl", &isl_options_args, + "isl options") ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0, NULL) ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0, NULL) ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0, NULL) -ISL_ARG_END -}; +ISL_ARGS_END -ISL_ARG_DEF(bound_options, struct bound_options, bound_options_arg) +ISL_ARG_DEF(bound_options, struct bound_options, bound_options_args) static __isl_give isl_set *set_bounds(__isl_take isl_set *set) { @@ -238,7 +238,7 @@ int main(int argc, char **argv) assert(options); argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL); - ctx = isl_ctx_alloc_with_options(bound_options_arg, options); + ctx = isl_ctx_alloc_with_options(&bound_options_args, options); s = isl_stream_new_file(ctx, stdin); obj = isl_stream_read_obj(s); diff --git a/cat.c b/cat.c index f278e9c5..fea62286 100644 --- a/cat.c +++ b/cat.c @@ -17,14 +17,13 @@ struct cat_options { unsigned format; }; -struct isl_arg cat_options_arg[] = { -ISL_ARG_CHILD(struct cat_options, isl, "isl", isl_options_arg, "isl options") +ISL_ARGS_START(struct cat_options, cat_options_args) +ISL_ARG_CHILD(struct cat_options, isl, "isl", &isl_options_args, "isl options") ISL_ARG_CHOICE(struct cat_options, format, 0, "format", \ cat_format, ISL_FORMAT_ISL, "output format") -ISL_ARG_END -}; +ISL_ARGS_END -ISL_ARG_DEF(cat_options, struct cat_options, cat_options_arg) +ISL_ARG_DEF(cat_options, struct cat_options, cat_options_args) int main(int argc, char **argv) { @@ -38,7 +37,7 @@ int main(int argc, char **argv) assert(options); argc = cat_options_parse(options, argc, argv, ISL_ARG_ALL); - ctx = isl_ctx_alloc_with_options(cat_options_arg, options); + ctx = isl_ctx_alloc_with_options(&cat_options_args, options); s = isl_stream_new_file(ctx, stdin); obj = isl_stream_read_obj(s); diff --git a/closure.c b/closure.c index 5ebab950..fc50a78f 100644 --- a/closure.c +++ b/closure.c @@ -12,7 +12,7 @@ int main(int argc, char **argv) assert(options); argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL); - ctx = isl_ctx_alloc_with_options(isl_options_arg, options); + ctx = isl_ctx_alloc_with_options(&isl_options_args, options); map = isl_map_read_from_file(ctx, stdin); map = isl_map_transitive_closure(map, &exact); diff --git a/include/isl/arg.h b/include/isl/arg.h index 8c775f6d..68284929 100644 --- a/include/isl/arg.h +++ b/include/isl/arg.h @@ -45,6 +45,8 @@ enum isl_arg_type { isl_arg_version }; +struct isl_args; + struct isl_arg { enum isl_arg_type type; char short_name; @@ -86,8 +88,7 @@ struct isl_arg { const char *default_value; } str; struct { - struct isl_arg *child; - size_t size; + struct isl_args *child; } child; struct { void (*print_version)(void); @@ -99,6 +100,18 @@ struct isl_arg { } u; }; +struct isl_args { + size_t options_size; + struct isl_arg *args; +}; + +#define ISL_ARGS_START(s,name) \ + struct isl_arg name ## LIST[]; \ + struct isl_args name = { sizeof(s), name ## LIST }; \ + struct isl_arg name ## LIST[] = { +#define ISL_ARGS_END \ + { isl_arg_end } }; + #define ISL_ARG_ALIAS(l) { \ .type = isl_arg_alias, \ .long_name = l, \ @@ -216,18 +229,18 @@ struct isl_arg { }, #define ISL_ARG_STR(st,f,s,l,a,d,h) \ ISL_ARG_STR_F(st,f,s,l,a,d,h,0) -#define _ISL_ARG_CHILD(o,sz,l,c,h,fl) { \ +#define _ISL_ARG_CHILD(o,l,c,h,fl) { \ .type = isl_arg_child, \ .long_name = l, \ .offset = o, \ .help_msg = h, \ .flags = fl, \ - .u = { .child = { .child = c, .size = sz } } \ + .u = { .child = { .child = c } } \ }, #define ISL_ARG_CHILD(st,f,l,c,h) \ - _ISL_ARG_CHILD(offsetof(st, f),sizeof(*((st *)NULL)->f),l,c,h,0) + _ISL_ARG_CHILD(offsetof(st, f),l,c,h,0) #define ISL_ARG_GROUP_F(c,h,fl) \ - _ISL_ARG_CHILD(-1,0,NULL,c,h,fl) + _ISL_ARG_CHILD(-1,NULL,c,h,fl) #define ISL_ARG_GROUP(c,h) \ ISL_ARG_GROUP_F(c,h,0) #define ISL_ARG_FLAGS(st,f,s,l,c,d,h) { \ @@ -247,38 +260,37 @@ struct isl_arg { .type = isl_arg_version, \ .u = { .version = { .print_version = print } } \ }, -#define ISL_ARG_END { isl_arg_end } #define ISL_ARG_ALL (1 << 0) -void isl_arg_set_defaults(struct isl_arg *arg, void *opt); -void isl_arg_free(struct isl_arg *arg, void *opt); -int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt, +void isl_args_set_defaults(struct isl_args *args, void *opt); +void isl_args_free(struct isl_args *args, void *opt); +int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt, unsigned flags); -#define ISL_ARG_DECL(prefix,st,arg) \ -extern struct isl_arg arg[]; \ +#define ISL_ARG_DECL(prefix,st,args) \ +extern struct isl_args args; \ st *prefix ## _new_with_defaults(void); \ void prefix ## _free(st *opt); \ int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags); -#define ISL_ARG_DEF(prefix,st,arg) \ +#define ISL_ARG_DEF(prefix,st,args) \ st *prefix ## _new_with_defaults() \ { \ st *opt = (st *)calloc(1, sizeof(st)); \ if (opt) \ - isl_arg_set_defaults(arg, opt); \ + isl_args_set_defaults(&(args), opt); \ return opt; \ } \ \ void prefix ## _free(st *opt) \ { \ - isl_arg_free(arg, opt); \ + isl_args_free(&(args), opt); \ } \ \ int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags) \ { \ - return isl_arg_parse(arg, argc, argv, opt, flags); \ + return isl_args_parse(&(args), argc, argv, opt, flags); \ } #if defined(__cplusplus) diff --git a/include/isl/ctx.h b/include/isl/ctx.h index 71555771..3ddc451e 100644 --- a/include/isl/ctx.h +++ b/include/isl/ctx.h @@ -139,9 +139,10 @@ typedef struct isl_ctx isl_ctx; struct isl_options *isl_ctx_options(isl_ctx *ctx); -isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, __isl_take void *opt); +isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, + __isl_take void *opt); isl_ctx *isl_ctx_alloc(void); -void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_arg *arg); +void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args); int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags); void isl_ctx_ref(struct isl_ctx *ctx); void isl_ctx_deref(struct isl_ctx *ctx); @@ -151,13 +152,13 @@ void isl_ctx_abort(isl_ctx *ctx); void isl_ctx_resume(isl_ctx *ctx); int isl_ctx_aborted(isl_ctx *ctx); -#define ISL_ARG_CTX_DECL(prefix,st,arg) \ +#define ISL_ARG_CTX_DECL(prefix,st,args) \ st *isl_ctx_peek_ ## prefix(isl_ctx *ctx); -#define ISL_ARG_CTX_DEF(prefix,st,arg) \ +#define ISL_ARG_CTX_DEF(prefix,st,args) \ st *isl_ctx_peek_ ## prefix(isl_ctx *ctx) \ { \ - return (st *)isl_ctx_peek_options(ctx, arg); \ + return (st *)isl_ctx_peek_options(ctx, &(args)); \ } enum isl_error isl_ctx_last_error(isl_ctx *ctx); diff --git a/include/isl/options.h b/include/isl/options.h index d280ab30..8ef0f7f7 100644 --- a/include/isl/options.h +++ b/include/isl/options.h @@ -65,9 +65,7 @@ struct isl_options { int schedule_split_parallel; }; -ISL_ARG_DECL(isl_options, struct isl_options, isl_options_arg) - -extern struct isl_arg isl_options_arg[]; +ISL_ARG_DECL(isl_options, struct isl_options, isl_options_args) #if defined(__cplusplus) } diff --git a/isl_arg.c b/isl_arg.c index 75c22fc5..ce0dcce6 100644 --- a/isl_arg.c +++ b/isl_arg.c @@ -16,7 +16,6 @@ static struct isl_arg help_arg[] = { ISL_ARG_PHANTOM_BOOL('h', "help", NULL, "print this help, then exit") -ISL_ARG_END }; static void set_default_choice(struct isl_arg *arg, void *opt) @@ -43,12 +42,12 @@ static void set_default_child(struct isl_arg *arg, void *opt) if (arg->offset == (size_t) -1) child = opt; else { - child = calloc(1, arg->u.child.size); + child = calloc(1, arg->u.child.child->options_size); *(void **)(((char *)opt) + arg->offset) = child; } if (child) - isl_arg_set_defaults(arg->u.child.child, child); + isl_args_set_defaults(arg->u.child.child, child); } static void set_default_user(struct isl_arg *arg, void *opt) @@ -79,39 +78,39 @@ static void set_default_str(struct isl_arg *arg, void *opt) *(const char **)(((char *)opt) + arg->offset) = str; } -void isl_arg_set_defaults(struct isl_arg *arg, void *opt) +void isl_args_set_defaults(struct isl_args *args, void *opt) { int i; - for (i = 0; arg[i].type != isl_arg_end; ++i) { - switch (arg[i].type) { + for (i = 0; args->args[i].type != isl_arg_end; ++i) { + switch (args->args[i].type) { case isl_arg_choice: - set_default_choice(&arg[i], opt); + set_default_choice(&args->args[i], opt); break; case isl_arg_flags: - set_default_flags(&arg[i], opt); + set_default_flags(&args->args[i], opt); break; case isl_arg_bool: - set_default_bool(&arg[i], opt); + set_default_bool(&args->args[i], opt); break; case isl_arg_child: - set_default_child(&arg[i], opt); + set_default_child(&args->args[i], opt); break; case isl_arg_user: - set_default_user(&arg[i], opt); + set_default_user(&args->args[i], opt); break; case isl_arg_int: - set_default_int(&arg[i], opt); + set_default_int(&args->args[i], opt); break; case isl_arg_long: - set_default_long(&arg[i], opt); + set_default_long(&args->args[i], opt); break; case isl_arg_ulong: - set_default_ulong(&arg[i], opt); + set_default_ulong(&args->args[i], opt); break; case isl_arg_arg: case isl_arg_str: - set_default_str(&arg[i], opt); + set_default_str(&args->args[i], opt); break; case isl_arg_alias: case isl_arg_footer: @@ -130,9 +129,9 @@ static void free_args(struct isl_arg *arg, void *opt) switch (arg[i].type) { case isl_arg_child: if (arg[i].offset == (size_t) -1) - free_args(arg[i].u.child.child, opt); + free_args(arg[i].u.child.child->args, opt); else - isl_arg_free(arg[i].u.child.child, + isl_args_free(arg[i].u.child.child, *(void **)(((char *)opt) + arg[i].offset)); break; case isl_arg_arg: @@ -158,12 +157,12 @@ static void free_args(struct isl_arg *arg, void *opt) } } -void isl_arg_free(struct isl_arg *arg, void *opt) +void isl_args_free(struct isl_args *args, void *opt) { if (!opt) return; - free_args(arg, opt); + free_args(args->args, opt); free(opt); } @@ -535,7 +534,7 @@ static void print_help(struct isl_arg *arg, const char *prefix, void *opt) child = opt; else child = *(void **)(((char *) opt) + arg[i].offset); - print_help(arg[i].u.child.child, arg[i].long_name, child); + print_help(arg[i].u.child.child->args, arg[i].long_name, child); any = 1; } } @@ -562,7 +561,7 @@ static int any_version(struct isl_arg *decl) case isl_arg_version: return 1; case isl_arg_child: - if (any_version(decl[i].u.child.child)) + if (any_version(decl[i].u.child.child->args)) return 1; break; default: @@ -962,7 +961,7 @@ static int parse_child_option(struct isl_arg *decl, char **arg, child = *(void **)(((char *)opt) + decl->offset); prefix = decl->long_name; } - return parse_option(decl->u.child.child, arg, prefix, child); + return parse_option(decl->u.child.child->args, arg, prefix, child); } static int parse_option(struct isl_arg *decl, char **arg, @@ -1022,7 +1021,7 @@ static void print_version(struct isl_arg *decl) decl[i].u.version.print_version(); break; case isl_arg_child: - print_version(decl[i].u.child.child); + print_version(decl[i].u.child.child->args); break; default: break; @@ -1066,7 +1065,7 @@ static int next_arg(struct isl_arg *arg, int a) return -1; } -int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt, +int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt, unsigned flags) { int a = -1; @@ -1074,26 +1073,26 @@ int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt, int i; int n; - n = n_arg(arg); + n = n_arg(args->args); for (i = 1; i < argc; ++i) { if (strcmp(argv[i], "--help") == 0) - print_help_and_exit(arg, argv[0], opt); + print_help_and_exit(args->args, argv[0], opt); } for (i = 1; i < argc; ++i) { if ((strcmp(argv[i], "--version") == 0 || - strcmp(argv[i], "-V") == 0) && any_version(arg)) - print_version_and_exit(arg); + strcmp(argv[i], "-V") == 0) && any_version(args->args)) + print_version_and_exit(args->args); } while (argc > 1 + skip) { int parsed; if (argv[1 + skip][0] != '-') { - a = next_arg(arg, a); + a = next_arg(args->args, a); if (a >= 0) { char **p; - p = (char **)(((char *)opt)+arg[a].offset); + p = (char **)(((char *)opt)+args->args[a].offset); free(*p); *p = strdup(argv[1 + skip]); argc = drop_argument(argc, argv, 1 + skip, 1); @@ -1106,7 +1105,7 @@ int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt, ++skip; continue; } - parsed = parse_option(arg, &argv[1 + skip], NULL, opt); + parsed = parse_option(args->args, &argv[1 + skip], NULL, opt); if (parsed) argc = drop_argument(argc, argv, 1 + skip, parsed); else if (ISL_FL_ISSET(flags, ISL_ARG_ALL)) { diff --git a/isl_ctx.c b/isl_ctx.c index 43544caf..14910b89 100644 --- a/isl_ctx.c +++ b/isl_ctx.c @@ -10,20 +10,21 @@ #include #include -static struct isl_options *find_nested_options(struct isl_arg *arg, - void *opt, struct isl_arg *wanted) +static struct isl_options *find_nested_options(struct isl_args *args, + void *opt, struct isl_args *wanted) { int i; struct isl_options *options; - if (arg == wanted) + if (args == wanted) return opt; - for (i = 0; arg[i].type != isl_arg_end; ++i) { - if (arg[i].type != isl_arg_child) + for (i = 0; args->args[i].type != isl_arg_end; ++i) { + if (args->args[i].type != isl_arg_child) continue; - options = find_nested_options(arg[i].u.child.child, - *(void **)(((char *)opt) + arg->offset), wanted); + options = find_nested_options(args->args[i].u.child.child, + *(void **)(((char *)opt) + args->args[i].offset), + wanted); if (options) return options; } @@ -31,20 +32,20 @@ static struct isl_options *find_nested_options(struct isl_arg *arg, return NULL; } -static struct isl_options *find_nested_isl_options(struct isl_arg *arg, +static struct isl_options *find_nested_isl_options(struct isl_args *args, void *opt) { - return find_nested_options(arg, opt, isl_options_arg); + return find_nested_options(args, opt, &isl_options_args); } -void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_arg *arg) +void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args) { if (!ctx) return NULL; - return find_nested_options(ctx->user_arg, ctx->user_opt, arg); + return find_nested_options(ctx->user_args, ctx->user_opt, args); } -isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt) +isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt) { struct isl_ctx *ctx = NULL; struct isl_options *opt = NULL; @@ -53,7 +54,7 @@ isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt) if (!user_opt) return NULL; - opt = find_nested_isl_options(arg, user_opt); + opt = find_nested_isl_options(args, user_opt); if (!opt) { opt = isl_options_new_with_defaults(); if (!opt) @@ -72,7 +73,7 @@ isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt) if (!ctx->stats) goto error; - ctx->user_arg = arg; + ctx->user_args = args; ctx->user_opt = user_opt; ctx->opt_allocated = opt_allocated; ctx->opt = opt; @@ -99,7 +100,7 @@ isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt) return ctx; error: - isl_arg_free(arg, user_opt); + isl_args_free(args, user_opt); if (opt_allocated) isl_options_free(opt); free(ctx); @@ -112,7 +113,7 @@ struct isl_ctx *isl_ctx_alloc() opt = isl_options_new_with_defaults(); - return isl_ctx_alloc_with_options(isl_options_arg, opt); + return isl_ctx_alloc_with_options(&isl_options_args, opt); } void isl_ctx_ref(struct isl_ctx *ctx) @@ -138,7 +139,7 @@ void isl_ctx_free(struct isl_ctx *ctx) isl_int_clear(ctx->two); isl_int_clear(ctx->negone); isl_int_clear(ctx->normalize_gcd); - isl_arg_free(ctx->user_arg, ctx->user_opt); + isl_args_free(ctx->user_args, ctx->user_opt); if (ctx->opt_allocated) free(ctx->opt); free(ctx->stats); @@ -189,5 +190,5 @@ int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags) { if (!ctx) return -1; - return isl_arg_parse(ctx->user_arg, argc, argv, ctx->user_opt, flags); + return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags); } diff --git a/isl_ctx_private.h b/isl_ctx_private.h index ff8b233f..65267f80 100644 --- a/isl_ctx_private.h +++ b/isl_ctx_private.h @@ -8,7 +8,7 @@ struct isl_ctx { int opt_allocated; struct isl_options *opt; void *user_opt; - struct isl_arg *user_arg; + struct isl_args *user_args; isl_int zero; isl_int one; diff --git a/isl_options.c b/isl_options.c index 1948c2bf..520a875a 100644 --- a/isl_options.c +++ b/isl_options.c @@ -86,7 +86,7 @@ static void print_version(void) printf("%s", isl_version()); } -struct isl_arg isl_options_arg[] = { +ISL_ARGS_START(struct isl_options, isl_options_args) ISL_ARG_CHOICE(struct isl_options, lp_solver, 0, "lp-solver", \ isl_lp_solver_choice, ISL_LP_TAB, "lp solver to use") ISL_ARG_CHOICE(struct isl_options, ilp_solver, 0, "ilp-solver", \ @@ -128,7 +128,6 @@ ISL_ARG_BOOL(struct isl_options, schedule_split_parallel, 0, "schedule-split-parallel", 1, "split non-tilable bands with parallel schedules") ISL_ARG_VERSION(print_version) -ISL_ARG_END -}; +ISL_ARGS_END -ISL_ARG_DEF(isl_options, struct isl_options, isl_options_arg) +ISL_ARG_DEF(isl_options, struct isl_options, isl_options_args) diff --git a/pip.c b/pip.c index 9cbdf934..2770d1b4 100644 --- a/pip.c +++ b/pip.c @@ -52,15 +52,14 @@ struct isl_arg_choice pip_format[] = { {0} }; -struct isl_arg options_arg[] = { -ISL_ARG_CHILD(struct options, isl, "isl", isl_options_arg, "isl options") +ISL_ARGS_START(struct options, options_args) +ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options") ISL_ARG_BOOL(struct options, verify, 'T', "verify", 0, NULL) ISL_ARG_CHOICE(struct options, format, 0, "format", pip_format, FORMAT_SET, "output format") -ISL_ARG_END -}; +ISL_ARGS_END -ISL_ARG_DEF(options, struct options, options_arg) +ISL_ARG_DEF(options, struct options, options_args) static __isl_give isl_basic_set *set_bounds(__isl_take isl_basic_set *bset) { @@ -296,7 +295,7 @@ int main(int argc, char **argv) assert(options); argc = options_parse(options, argc, argv, ISL_ARG_ALL); - ctx = isl_ctx_alloc_with_options(options_arg, options); + ctx = isl_ctx_alloc_with_options(&options_args, options); context = isl_basic_set_read_from_file(ctx, stdin); assert(context); -- 2.11.4.GIT