add isl_aff_mod_val
[platform/upstream/isl.git] / isl_ctx.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the MIT license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <isl_ctx_private.h>
11 #include <isl/vec.h>
12 #include <isl_options_private.h>
13
14 #define __isl_calloc(type,size)         ((type *)calloc(1, size))
15 #define __isl_calloc_type(type)         __isl_calloc(type,sizeof(type))
16
17 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
18         const char *file, int line)
19 {
20         if (!ctx)
21                 return;
22
23         isl_ctx_set_error(ctx, error);
24
25         switch (ctx->opt->on_error) {
26         case ISL_ON_ERROR_WARN:
27                 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
28                 return;
29         case ISL_ON_ERROR_CONTINUE:
30                 return;
31         case ISL_ON_ERROR_ABORT:
32                 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
33                 abort();
34                 return;
35         }
36 }
37
38 static struct isl_options *find_nested_options(struct isl_args *args,
39         void *opt, struct isl_args *wanted)
40 {
41         int i;
42         struct isl_options *options;
43
44         if (args == wanted)
45                 return opt;
46
47         for (i = 0; args->args[i].type != isl_arg_end; ++i) {
48                 if (args->args[i].type != isl_arg_child)
49                         continue;
50                 options = find_nested_options(args->args[i].u.child.child,
51                             *(void **)(((char *)opt) + args->args[i].offset),
52                             wanted);
53                 if (options)
54                         return options;
55         }
56
57         return NULL;
58 }
59
60 static struct isl_options *find_nested_isl_options(struct isl_args *args,
61         void *opt)
62 {
63         return find_nested_options(args, opt, &isl_options_args);
64 }
65
66 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
67 {
68         if (!ctx)
69                 return NULL;
70         if (args == &isl_options_args)
71                 return ctx->opt;
72         return find_nested_options(ctx->user_args, ctx->user_opt, args);
73 }
74
75 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
76 {
77         struct isl_ctx *ctx = NULL;
78         struct isl_options *opt = NULL;
79         int opt_allocated = 0;
80
81         if (!user_opt)
82                 return NULL;
83
84         opt = find_nested_isl_options(args, user_opt);
85         if (!opt) {
86                 opt = isl_options_new_with_defaults();
87                 if (!opt)
88                         goto error;
89                 opt_allocated = 1;
90         }
91
92         ctx = __isl_calloc_type(struct isl_ctx);
93         if (!ctx)
94                 goto error;
95
96         if (isl_hash_table_init(ctx, &ctx->id_table, 0))
97                 goto error;
98
99         ctx->stats = isl_calloc_type(ctx, struct isl_stats);
100         if (!ctx->stats)
101                 goto error;
102
103         ctx->user_args = args;
104         ctx->user_opt = user_opt;
105         ctx->opt_allocated = opt_allocated;
106         ctx->opt = opt;
107         ctx->ref = 0;
108
109         isl_int_init(ctx->zero);
110         isl_int_set_si(ctx->zero, 0);
111
112         isl_int_init(ctx->one);
113         isl_int_set_si(ctx->one, 1);
114
115         isl_int_init(ctx->two);
116         isl_int_set_si(ctx->two, 2);
117
118         isl_int_init(ctx->negone);
119         isl_int_set_si(ctx->negone, -1);
120
121         isl_int_init(ctx->normalize_gcd);
122
123         ctx->n_cached = 0;
124         ctx->n_miss = 0;
125
126         ctx->error = isl_error_none;
127
128         return ctx;
129 error:
130         isl_args_free(args, user_opt);
131         if (opt_allocated)
132                 isl_options_free(opt);
133         free(ctx);
134         return NULL;
135 }
136
137 struct isl_ctx *isl_ctx_alloc()
138 {
139         struct isl_options *opt;
140
141         opt = isl_options_new_with_defaults();
142
143         return isl_ctx_alloc_with_options(&isl_options_args, opt);
144 }
145
146 void isl_ctx_ref(struct isl_ctx *ctx)
147 {
148         ctx->ref++;
149 }
150
151 void isl_ctx_deref(struct isl_ctx *ctx)
152 {
153         isl_assert(ctx, ctx->ref > 0, return);
154         ctx->ref--;
155 }
156
157 void isl_ctx_free(struct isl_ctx *ctx)
158 {
159         if (!ctx)
160                 return;
161         if (ctx->ref != 0)
162                 isl_die(ctx, isl_error_invalid,
163                         "isl_ctx freed, but some objects still reference it",
164                         return);
165
166         isl_hash_table_clear(&ctx->id_table);
167         isl_blk_clear_cache(ctx);
168         isl_int_clear(ctx->zero);
169         isl_int_clear(ctx->one);
170         isl_int_clear(ctx->two);
171         isl_int_clear(ctx->negone);
172         isl_int_clear(ctx->normalize_gcd);
173         isl_args_free(ctx->user_args, ctx->user_opt);
174         if (ctx->opt_allocated)
175                 isl_options_free(ctx->opt);
176         free(ctx->stats);
177         free(ctx);
178 }
179
180 struct isl_options *isl_ctx_options(isl_ctx *ctx)
181 {
182         if (!ctx)
183                 return NULL;
184         return ctx->opt;
185 }
186
187 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
188 {
189         return ctx->error;
190 }
191
192 void isl_ctx_reset_error(isl_ctx *ctx)
193 {
194         ctx->error = isl_error_none;
195 }
196
197 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
198 {
199         if (ctx)
200                 ctx->error = error;
201 }
202
203 void isl_ctx_abort(isl_ctx *ctx)
204 {
205         if (ctx)
206                 ctx->abort = 1;
207 }
208
209 void isl_ctx_resume(isl_ctx *ctx)
210 {
211         if (ctx)
212                 ctx->abort = 0;
213 }
214
215 int isl_ctx_aborted(isl_ctx *ctx)
216 {
217         return ctx ? ctx->abort : -1;
218 }
219
220 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
221 {
222         if (!ctx)
223                 return -1;
224         return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
225 }