H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame^] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
| 3 | * Copyright 1996-2017 The NASM Authors - All Rights Reserved |
| 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * Parse and handle [pragma] directives. The preprocessor handles |
| 36 | * %pragma preproc directives separately, all other namespaces are |
| 37 | * simply converted to [pragma]. |
| 38 | */ |
| 39 | |
| 40 | #include "compiler.h" |
| 41 | |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | #include <ctype.h> |
| 45 | #include <limits.h> |
| 46 | |
| 47 | #include "nasm.h" |
| 48 | #include "nasmlib.h" |
| 49 | #include "error.h" |
| 50 | |
| 51 | /* |
| 52 | * Handle [pragma] directives. [pragma] is generally produced by |
| 53 | * the %pragma preprocessor directive, which simply passes on any |
| 54 | * string that it finds *except* %pragma preproc. The idea is |
| 55 | * that pragmas are of the form: |
| 56 | * |
| 57 | * %pragma <facility> <opname> [<options>...] |
| 58 | * |
| 59 | * ... where "facility" can be either a generic facility or a backend |
| 60 | * name. |
| 61 | * |
| 62 | * The following names are currently reserved for global facilities; |
| 63 | * so far none of these have any defined pragmas at all: |
| 64 | * |
| 65 | * preproc - preprocessor |
| 66 | * asm - assembler |
| 67 | * list - listing generator |
| 68 | * file - generic file handling |
| 69 | * input - input file handling |
| 70 | * output - backend-independent output handling |
| 71 | * debug - backend-independent debug handling |
| 72 | * ignore - dummy pragma (can be used to "comment out") |
| 73 | * |
| 74 | * This function should generally not error out if it doesn't understand |
| 75 | * what a pragma is for, for unknown arguments, etc; the whole point of |
| 76 | * a pragma is that future releases might add new ones that should be |
| 77 | * ignored rather than be an error. Erroring out is acceptable for |
| 78 | * known pragmas suffering from parsing errors and so on. |
| 79 | * |
| 80 | * Adding default-suppressed warnings would, however, be a good idea |
| 81 | * at some point. |
| 82 | */ |
| 83 | static struct pragma_facility global_pragmas[] = |
| 84 | { |
| 85 | { "preproc", NULL }, /* This shouldn't happen... */ |
| 86 | { "asm", NULL }, |
| 87 | { "list", NULL }, |
| 88 | { "file", NULL }, |
| 89 | { "input", NULL }, |
| 90 | { "output", NULL }, |
| 91 | { "debug", NULL }, |
| 92 | { "ignore", NULL }, |
| 93 | { NULL, NULL } |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * Search a pragma list for a known pragma facility and if so, invoke |
| 98 | * the handler. Return true if processing is complete. |
| 99 | * The "default name", if set, matches the final NULL entry (used |
| 100 | * for backends, so multiple backends can share the same list under |
| 101 | * some circumstances.) |
| 102 | */ |
| 103 | static bool search_pragma_list(const struct pragma_facility *list, |
| 104 | const char *default_name, |
| 105 | struct pragma *pragma) |
| 106 | { |
| 107 | const struct pragma_facility *pf; |
| 108 | |
| 109 | if (!list) |
| 110 | return false; |
| 111 | |
| 112 | for (pf = list; pf->name; pf++) { |
| 113 | if (!nasm_stricmp(pragma->facility_name, pf->name)) |
| 114 | goto found_it; |
| 115 | } |
| 116 | |
| 117 | if (default_name && !nasm_stricmp(pragma->facility_name, default_name)) |
| 118 | goto found_it; |
| 119 | |
| 120 | return false; |
| 121 | |
| 122 | found_it: |
| 123 | if (!pf->handler) |
| 124 | return true; |
| 125 | |
| 126 | pragma->facility = pf; |
| 127 | pf->handler(pragma); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | void process_pragma(char *str) |
| 132 | { |
| 133 | struct pragma pragma; |
| 134 | char *p; |
| 135 | |
| 136 | nasm_zero(&pragma); |
| 137 | |
| 138 | pragma.facility_name = nasm_get_word(str, &p); |
| 139 | if (!pragma.facility_name) { |
| 140 | nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA, |
| 141 | "empty pragma directive"); |
| 142 | return; /* Empty pragma */ |
| 143 | } |
| 144 | |
| 145 | pragma.operation = nasm_get_word(p, &p); |
| 146 | if (!pragma.operation) { |
| 147 | nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA, |
| 148 | "pragma directive contains only facility namespace"); |
| 149 | return; /* Facility name only */ |
| 150 | } |
| 151 | |
| 152 | pragma.tail = nasm_skip_spaces(p); |
| 153 | |
| 154 | /* Look for a global pragma namespace */ |
| 155 | if (search_pragma_list(global_pragmas, NULL, &pragma)) |
| 156 | return; |
| 157 | |
| 158 | /* Look to see if it is an output backend pragma */ |
| 159 | if (search_pragma_list(ofmt->pragmas, ofmt->shortname, &pragma)) |
| 160 | return; |
| 161 | |
| 162 | /* Look to see if it is a debug format pragma */ |
| 163 | if (search_pragma_list(dfmt->pragmas, dfmt->shortname, &pragma)) |
| 164 | return; |
| 165 | |
| 166 | /* |
| 167 | * Note: it would be nice to warn for an unknown namespace, |
| 168 | * but in order to do so we need to walk *ALL* the backends |
| 169 | * in order to make sure we aren't dealing with a pragma that |
| 170 | * is for another backend. On the other hand, that could |
| 171 | * also be a warning with a separate warning flag. |
| 172 | * |
| 173 | * Leave this for the future, however, the warning classes are |
| 174 | * already defined for future compatibility. |
| 175 | */ |
| 176 | } |