2 * Value/Parameter type functions
4 * Copyright (C) 2001-2007 Peter Johnson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 #include "libyasm-stdint.h"
41 yasm_call_directive(const yasm_directive *directive, yasm_object *object,
42 yasm_valparamhead *valparams,
43 yasm_valparamhead *objext_valparams, unsigned long line)
47 if ((directive->flags & (YASM_DIR_ARG_REQUIRED|YASM_DIR_ID_REQUIRED)) &&
48 (!valparams || !yasm_vps_first(valparams))) {
49 yasm_error_set(YASM_ERROR_SYNTAX,
50 N_("directive `%s' requires an argument"),
55 vp = yasm_vps_first(valparams);
56 if ((directive->flags & YASM_DIR_ID_REQUIRED) &&
57 vp->type != YASM_PARAM_ID) {
58 yasm_error_set(YASM_ERROR_SYNTAX,
59 N_("directive `%s' requires an identifier parameter"),
64 directive->handler(object, valparams, objext_valparams, line);
68 yasm_vp_create_id(/*@keep@*/ char *v, /*@keep@*/ char *p, int id_prefix)
70 yasm_valparam *r = yasm_xmalloc(sizeof(yasm_valparam));
72 r->type = YASM_PARAM_ID;
74 r->id_prefix = (char)id_prefix;
79 yasm_vp_create_string(/*@keep@*/ char *v, /*@keep@*/ char *p)
81 yasm_valparam *r = yasm_xmalloc(sizeof(yasm_valparam));
83 r->type = YASM_PARAM_STRING;
90 yasm_vp_create_expr(/*@keep@*/ char *v, /*@keep@*/ yasm_expr *p)
92 yasm_valparam *r = yasm_xmalloc(sizeof(yasm_valparam));
94 r->type = YASM_PARAM_EXPR;
100 /*@null@*/ /*@only@*/ yasm_expr *
101 yasm_vp_expr(const yasm_valparam *vp, yasm_symtab *symtab, unsigned long line)
107 return yasm_expr_create_ident(yasm_expr_sym(
108 yasm_symtab_use(symtab, yasm_vp_id(vp), line)), line);
109 case YASM_PARAM_EXPR:
110 return yasm_expr_copy(vp->param.e);
116 /*@null@*/ /*@dependent@*/ const char *
117 yasm_vp_string(const yasm_valparam *vp)
124 case YASM_PARAM_STRING:
125 return vp->param.str;
131 /*@null@*/ /*@dependent@*/ const char *
132 yasm_vp_id(const yasm_valparam *vp)
136 if (vp->type == YASM_PARAM_ID) {
137 if (vp->param.id[0] == vp->id_prefix)
138 return &vp->param.id[1];
146 yasm_vps_delete(yasm_valparamhead *headp)
148 yasm_valparam *cur, *next;
150 cur = STAILQ_FIRST(headp);
152 next = STAILQ_NEXT(cur, link);
154 yasm_xfree(cur->val);
157 yasm_xfree(cur->param.id);
159 case YASM_PARAM_STRING:
160 yasm_xfree(cur->param.str);
162 case YASM_PARAM_EXPR:
163 yasm_expr_destroy(cur->param.e);
173 yasm_vps_print(const yasm_valparamhead *headp, FILE *f)
175 const yasm_valparam *vp;
178 fprintf(f, "(none)");
182 yasm_vps_foreach(vp, headp) {
184 fprintf(f, "(\"%s\",", vp->val);
186 fprintf(f, "((nil),");
189 fprintf(f, "%s", vp->param.id);
191 case YASM_PARAM_STRING:
192 fprintf(f, "\"%s\"", vp->param.str);
194 case YASM_PARAM_EXPR:
195 yasm_expr_print(vp->param.e, f);
199 if (yasm_vps_next(vp))
205 yasm_vps_create(void)
207 yasm_valparamhead *headp = yasm_xmalloc(sizeof(yasm_valparamhead));
208 yasm_vps_initialize(headp);
213 yasm_vps_destroy(yasm_valparamhead *headp)
215 yasm_vps_delete(headp);
220 yasm_dir_helper(void *obj, yasm_valparam *vp_first, unsigned long line,
221 const yasm_dir_help *help, size_t nhelp, void *data,
222 int (*helper_valparam) (void *obj, yasm_valparam *vp,
223 unsigned long line, void *data))
225 yasm_valparam *vp = vp_first;
237 if (!vp->val && (s = yasm_vp_id(vp))) {
238 for (i=0; i<nhelp; i++) {
239 if (help[i].needsparam == 0 &&
240 yasm__strcasecmp(s, help[i].name) == 0) {
241 if (help[i].helper(obj, vp, line,
242 ((char *)data)+help[i].off,
250 } else if (vp->val) {
251 for (i=0; i<nhelp; i++) {
252 if (help[i].needsparam == 1 &&
253 yasm__strcasecmp(vp->val, help[i].name) == 0) {
254 if (help[i].helper(obj, vp, line,
255 ((char *)data)+help[i].off,
266 int final = helper_valparam(obj, vp, line, data);
272 } while((vp = yasm_vps_next(vp)));
278 yasm_dir_helper_flag_or(void *obj, yasm_valparam *vp, unsigned long line,
279 void *d, uintptr_t flag)
281 unsigned long *flags = (unsigned long *)d;
287 yasm_dir_helper_flag_and(void *obj, yasm_valparam *vp, unsigned long line,
288 void *d, uintptr_t flag)
290 unsigned long *flags = (unsigned long *)d;
296 yasm_dir_helper_flag_set(void *obj, yasm_valparam *vp, unsigned long line,
297 void *d, uintptr_t flag)
299 unsigned long *flags = (unsigned long *)d;
305 yasm_dir_helper_expr(void *obj, yasm_valparam *vp, unsigned long line,
306 void *data, uintptr_t arg)
308 yasm_object *object = (yasm_object *)obj;
309 yasm_expr **expr = (yasm_expr **)data;
312 yasm_expr_destroy(*expr);
313 if (!(*expr = yasm_vp_expr(vp, object->symtab, line))) {
314 yasm_error_set(YASM_ERROR_VALUE, N_("argument to `%s' is not an expression"),
322 yasm_dir_helper_intn(void *obj, yasm_valparam *vp, unsigned long line,
323 void *data, uintptr_t arg)
325 yasm_object *object = (yasm_object *)obj;
326 /*@only@*/ /*@null@*/ yasm_expr *e;
327 /*@dependent@*/ /*@null@*/ yasm_intnum *local;
328 yasm_intnum **intn = (yasm_intnum **)data;
331 yasm_intnum_destroy(*intn);
332 if (!(e = yasm_vp_expr(vp, object->symtab, line)) ||
333 !(local = yasm_expr_get_intnum(&e, 0))) {
334 yasm_error_set(YASM_ERROR_NOT_CONSTANT,
335 N_("argument to `%s' is not an integer"),
338 yasm_expr_destroy(e);
341 *intn = yasm_intnum_copy(local);
342 yasm_expr_destroy(e);
347 yasm_dir_helper_string(void *obj, yasm_valparam *vp, unsigned long line,
348 void *data, uintptr_t arg)
350 /*@dependent@*/ /*@null@*/ const char *local;
351 char **s = (char **)data;
355 if (!(local = yasm_vp_string(vp))) {
356 yasm_error_set(YASM_ERROR_VALUE,
357 N_("argument to `%s' is not a string or identifier"),
361 *s = yasm__xstrdup(local);
366 yasm_dir_helper_valparam_warn(void *obj, yasm_valparam *vp,
367 unsigned long line, void *data)
372 yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"),
377 if ((s = yasm_vp_id(vp)))
378 yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), s);
379 else if (vp->type == YASM_PARAM_STRING)
380 yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized string qualifier"));
382 yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized numeric qualifier"));