diff options
author | H. Peter Anvin <hpa@linux.intel.com> | 2017-03-07 21:32:37 -0800 |
---|---|---|
committer | H. Peter Anvin <hpa@linux.intel.com> | 2017-03-07 21:32:37 -0800 |
commit | a6e26d9cca91ee2b008ece9f7298740ed0b2edf6 (patch) | |
tree | a631af93fb92cb0d44fbde23a840d101b857e3c0 /asm/pragma.c | |
parent | bb4524cf9a10419fa241894a742c45f7019d2eda (diff) | |
download | nasm-a6e26d9cca91ee2b008ece9f7298740ed0b2edf6.tar.gz |
Add a generic pragma-handling infrastructure
Add infrastructure for handling %pragmas with a variety of namespaces,
etc., etc...
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Diffstat (limited to 'asm/pragma.c')
-rw-r--r-- | asm/pragma.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/asm/pragma.c b/asm/pragma.c new file mode 100644 index 00000000..d0518e8b --- /dev/null +++ b/asm/pragma.c @@ -0,0 +1,176 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 1996-2017 The NASM Authors - All Rights Reserved + * See the file AUTHORS included with the NASM distribution for + * the specific copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following + * conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * ----------------------------------------------------------------------- */ + +/* + * Parse and handle [pragma] directives. The preprocessor handles + * %pragma preproc directives separately, all other namespaces are + * simply converted to [pragma]. + */ + +#include "compiler.h" + +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include <limits.h> + +#include "nasm.h" +#include "nasmlib.h" +#include "error.h" + +/* + * Handle [pragma] directives. [pragma] is generally produced by + * the %pragma preprocessor directive, which simply passes on any + * string that it finds *except* %pragma preproc. The idea is + * that pragmas are of the form: + * + * %pragma <facility> <opname> [<options>...] + * + * ... where "facility" can be either a generic facility or a backend + * name. + * + * The following names are currently reserved for global facilities; + * so far none of these have any defined pragmas at all: + * + * preproc - preprocessor + * asm - assembler + * list - listing generator + * file - generic file handling + * input - input file handling + * output - backend-independent output handling + * debug - backend-independent debug handling + * ignore - dummy pragma (can be used to "comment out") + * + * This function should generally not error out if it doesn't understand + * what a pragma is for, for unknown arguments, etc; the whole point of + * a pragma is that future releases might add new ones that should be + * ignored rather than be an error. Erroring out is acceptable for + * known pragmas suffering from parsing errors and so on. + * + * Adding default-suppressed warnings would, however, be a good idea + * at some point. + */ +static struct pragma_facility global_pragmas[] = +{ + { "preproc", NULL }, /* This shouldn't happen... */ + { "asm", NULL }, + { "list", NULL }, + { "file", NULL }, + { "input", NULL }, + { "output", NULL }, + { "debug", NULL }, + { "ignore", NULL }, + { NULL, NULL } +}; + +/* + * Search a pragma list for a known pragma facility and if so, invoke + * the handler. Return true if processing is complete. + * The "default name", if set, matches the final NULL entry (used + * for backends, so multiple backends can share the same list under + * some circumstances.) + */ +static bool search_pragma_list(const struct pragma_facility *list, + const char *default_name, + struct pragma *pragma) +{ + const struct pragma_facility *pf; + + if (!list) + return false; + + for (pf = list; pf->name; pf++) { + if (!nasm_stricmp(pragma->facility_name, pf->name)) + goto found_it; + } + + if (default_name && !nasm_stricmp(pragma->facility_name, default_name)) + goto found_it; + + return false; + +found_it: + if (!pf->handler) + return true; + + pragma->facility = pf; + pf->handler(pragma); + return true; +} + +void process_pragma(char *str) +{ + struct pragma pragma; + char *p; + + nasm_zero(&pragma); + + pragma.facility_name = nasm_get_word(str, &p); + if (!pragma.facility_name) { + nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA, + "empty pragma directive"); + return; /* Empty pragma */ + } + + pragma.operation = nasm_get_word(p, &p); + if (!pragma.operation) { + nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA, + "pragma directive contains only facility namespace"); + return; /* Facility name only */ + } + + pragma.tail = nasm_skip_spaces(p); + + /* Look for a global pragma namespace */ + if (search_pragma_list(global_pragmas, NULL, &pragma)) + return; + + /* Look to see if it is an output backend pragma */ + if (search_pragma_list(ofmt->pragmas, ofmt->shortname, &pragma)) + return; + + /* Look to see if it is a debug format pragma */ + if (search_pragma_list(dfmt->pragmas, dfmt->shortname, &pragma)) + return; + + /* + * Note: it would be nice to warn for an unknown namespace, + * but in order to do so we need to walk *ALL* the backends + * in order to make sure we aren't dealing with a pragma that + * is for another backend. On the other hand, that could + * also be a warning with a separate warning flag. + * + * Leave this for the future, however, the warning classes are + * already defined for future compatibility. + */ +} |