H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 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 | |
H. Peter Anvin | c2f3f26 | 2018-12-27 12:37:25 -0800 | [diff] [blame] | 42 | #include "nctype.h" |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 43 | |
| 44 | #include "nasm.h" |
| 45 | #include "nasmlib.h" |
Cyrill Gorcunov | 4854133 | 2017-03-08 11:39:42 +0300 | [diff] [blame] | 46 | #include "assemble.h" |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 47 | #include "error.h" |
| 48 | |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 49 | static enum directive_result output_pragma(const struct pragma *pragma); |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 50 | static enum directive_result limit_pragma(const struct pragma *pragma); |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 51 | |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 52 | /* |
| 53 | * Handle [pragma] directives. [pragma] is generally produced by |
| 54 | * the %pragma preprocessor directive, which simply passes on any |
| 55 | * string that it finds *except* %pragma preproc. The idea is |
| 56 | * that pragmas are of the form: |
| 57 | * |
| 58 | * %pragma <facility> <opname> [<options>...] |
| 59 | * |
| 60 | * ... where "facility" can be either a generic facility or a backend |
| 61 | * name. |
| 62 | * |
| 63 | * The following names are currently reserved for global facilities; |
| 64 | * so far none of these have any defined pragmas at all: |
| 65 | * |
| 66 | * preproc - preprocessor |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 67 | * limit - limit setting |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 68 | * asm - assembler |
| 69 | * list - listing generator |
| 70 | * file - generic file handling |
| 71 | * input - input file handling |
| 72 | * output - backend-independent output handling |
| 73 | * debug - backend-independent debug handling |
| 74 | * ignore - dummy pragma (can be used to "comment out") |
| 75 | * |
| 76 | * This function should generally not error out if it doesn't understand |
| 77 | * what a pragma is for, for unknown arguments, etc; the whole point of |
| 78 | * a pragma is that future releases might add new ones that should be |
| 79 | * ignored rather than be an error. Erroring out is acceptable for |
| 80 | * known pragmas suffering from parsing errors and so on. |
| 81 | * |
| 82 | * Adding default-suppressed warnings would, however, be a good idea |
| 83 | * at some point. |
| 84 | */ |
| 85 | static struct pragma_facility global_pragmas[] = |
| 86 | { |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 87 | { "asm", NULL }, |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 88 | { "limit", limit_pragma }, |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 89 | { "list", NULL }, |
| 90 | { "file", NULL }, |
| 91 | { "input", NULL }, |
H. Peter Anvin | b713648 | 2018-05-30 14:42:06 -0700 | [diff] [blame] | 92 | |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 93 | /* None of these should actually happen due to special handling */ |
| 94 | { "preproc", NULL }, /* Handled in the preprocessor by necessity */ |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 95 | { "output", NULL }, |
H. Peter Anvin | b713648 | 2018-05-30 14:42:06 -0700 | [diff] [blame] | 96 | { "debug", NULL }, |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 97 | { "ignore", NULL }, |
| 98 | { NULL, NULL } |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Search a pragma list for a known pragma facility and if so, invoke |
| 103 | * the handler. Return true if processing is complete. |
| 104 | * The "default name", if set, matches the final NULL entry (used |
| 105 | * for backends, so multiple backends can share the same list under |
| 106 | * some circumstances.) |
| 107 | */ |
| 108 | static bool search_pragma_list(const struct pragma_facility *list, |
| 109 | const char *default_name, |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 110 | pragma_handler generic_handler, |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 111 | struct pragma *pragma) |
| 112 | { |
| 113 | const struct pragma_facility *pf; |
H. Peter Anvin | 8753425 | 2017-03-08 20:28:13 -0800 | [diff] [blame] | 114 | enum directive_result rv; |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 115 | |
| 116 | if (!list) |
| 117 | return false; |
| 118 | |
| 119 | for (pf = list; pf->name; pf++) { |
| 120 | if (!nasm_stricmp(pragma->facility_name, pf->name)) |
| 121 | goto found_it; |
| 122 | } |
| 123 | |
| 124 | if (default_name && !nasm_stricmp(pragma->facility_name, default_name)) |
| 125 | goto found_it; |
| 126 | |
| 127 | return false; |
| 128 | |
| 129 | found_it: |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 130 | pragma->facility = pf; |
H. Peter Anvin | d9493fa | 2017-03-08 20:05:41 -0800 | [diff] [blame] | 131 | |
H. Peter Anvin | 8753425 | 2017-03-08 20:28:13 -0800 | [diff] [blame] | 132 | /* If the handler is NULL all pragmas are unknown... */ |
| 133 | if (pf->handler) |
| 134 | rv = pf->handler(pragma); |
| 135 | else |
| 136 | rv = DIRR_UNKNOWN; |
| 137 | |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 138 | /* Is there an additional, applicable generic handler? */ |
| 139 | if (rv == DIRR_UNKNOWN && generic_handler) |
| 140 | rv = generic_handler(pragma); |
| 141 | |
H. Peter Anvin | 8753425 | 2017-03-08 20:28:13 -0800 | [diff] [blame] | 142 | switch (rv) { |
H. Peter Anvin | d9493fa | 2017-03-08 20:05:41 -0800 | [diff] [blame] | 143 | case DIRR_UNKNOWN: |
| 144 | switch (pragma->opcode) { |
| 145 | case D_none: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 146 | /*! |
| 147 | *!bad-pragma [off] empty or malformed %pragma |
| 148 | *! warns about a malformed or otherwise unparsable |
| 149 | *! \c{%pragma} directive. |
| 150 | */ |
H. Peter Anvin (Intel) | df4d342 | 2018-12-12 17:48:38 -0800 | [diff] [blame] | 151 | nasm_error(ERR_WARNING|ERR_PASS2|WARN_BAD_PRAGMA, |
H. Peter Anvin | d9493fa | 2017-03-08 20:05:41 -0800 | [diff] [blame] | 152 | "empty %%pragma %s", pragma->facility_name); |
| 153 | break; |
| 154 | default: |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 155 | /*! |
| 156 | *!unknown-pragma [off] unknown %pragma facility or directive |
| 157 | *! warns about an unknown \c{%pragma} directive. |
| 158 | *! This is not yet implemented for most cases. |
| 159 | */ |
H. Peter Anvin (Intel) | df4d342 | 2018-12-12 17:48:38 -0800 | [diff] [blame] | 160 | nasm_error(ERR_WARNING|ERR_PASS2|WARN_UNKNOWN_PRAGMA, |
H. Peter Anvin | d9493fa | 2017-03-08 20:05:41 -0800 | [diff] [blame] | 161 | "unknown %%pragma %s %s", |
| 162 | pragma->facility_name, pragma->opname); |
| 163 | break; |
| 164 | } |
| 165 | break; |
| 166 | |
| 167 | case DIRR_OK: |
| 168 | case DIRR_ERROR: |
| 169 | break; /* Nothing to do */ |
| 170 | |
| 171 | case DIRR_BADPARAM: |
| 172 | /* |
| 173 | * This one is an error. Don't use it if forward compatibility |
| 174 | * would be compromised, as opposed to an inherent error. |
| 175 | */ |
| 176 | nasm_error(ERR_NONFATAL, "bad argument to %%pragma %s %s", |
| 177 | pragma->facility_name, pragma->opname); |
| 178 | break; |
| 179 | |
| 180 | default: |
| 181 | panic(); |
| 182 | } |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 186 | /* This warning message is intended for future use */ |
| 187 | /*! |
| 188 | *!not-my-pragma [off] %pragma not applicable to this compilation |
| 189 | *! warns about a \c{%pragma} directive which is not applicable to |
| 190 | *! this particular assembly session. This is not yet implemented. |
| 191 | */ |
| 192 | |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 193 | void process_pragma(char *str) |
| 194 | { |
| 195 | struct pragma pragma; |
| 196 | char *p; |
| 197 | |
H. Peter Anvin | e886c0e | 2017-03-31 14:56:17 -0700 | [diff] [blame] | 198 | nasm_zero(pragma); |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 199 | |
| 200 | pragma.facility_name = nasm_get_word(str, &p); |
| 201 | if (!pragma.facility_name) { |
H. Peter Anvin (Intel) | df4d342 | 2018-12-12 17:48:38 -0800 | [diff] [blame] | 202 | nasm_error(ERR_WARNING|ERR_PASS2|WARN_BAD_PRAGMA, |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 203 | "empty pragma directive"); |
| 204 | return; /* Empty pragma */ |
| 205 | } |
| 206 | |
H. Peter Anvin | d9493fa | 2017-03-08 20:05:41 -0800 | [diff] [blame] | 207 | /* |
| 208 | * The facility "ignore" means just that; don't even complain of |
| 209 | * the absence of an operation. |
| 210 | */ |
| 211 | if (!nasm_stricmp(pragma.facility_name, "ignore")) |
| 212 | return; |
| 213 | |
H. Peter Anvin | b713648 | 2018-05-30 14:42:06 -0700 | [diff] [blame] | 214 | /* |
| 215 | * The "output" and "debug" facilities are aliases for the |
| 216 | * current output and debug formats, respectively. |
| 217 | */ |
| 218 | if (!nasm_stricmp(pragma.facility_name, "output")) |
| 219 | pragma.facility_name = ofmt->shortname; |
| 220 | if (!nasm_stricmp(pragma.facility_name, "debug")) |
| 221 | pragma.facility_name = dfmt->shortname; |
| 222 | |
H. Peter Anvin | d9493fa | 2017-03-08 20:05:41 -0800 | [diff] [blame] | 223 | pragma.opname = nasm_get_word(p, &p); |
| 224 | if (!pragma.opname) |
| 225 | pragma.opcode = D_none; |
| 226 | else |
H. Peter Anvin | 5253f58 | 2017-04-03 00:09:58 -0700 | [diff] [blame] | 227 | pragma.opcode = directive_find(pragma.opname); |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 228 | |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 229 | pragma.tail = nasm_trim_spaces(p); |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 230 | |
| 231 | /* Look for a global pragma namespace */ |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 232 | if (search_pragma_list(global_pragmas, NULL, NULL, &pragma)) |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 233 | return; |
| 234 | |
| 235 | /* Look to see if it is an output backend pragma */ |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 236 | if (search_pragma_list(ofmt->pragmas, ofmt->shortname, |
| 237 | output_pragma, &pragma)) |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 238 | return; |
| 239 | |
| 240 | /* Look to see if it is a debug format pragma */ |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 241 | if (search_pragma_list(dfmt->pragmas, dfmt->shortname, NULL, &pragma)) |
H. Peter Anvin | a6e26d9 | 2017-03-07 21:32:37 -0800 | [diff] [blame] | 242 | return; |
| 243 | |
| 244 | /* |
| 245 | * Note: it would be nice to warn for an unknown namespace, |
| 246 | * but in order to do so we need to walk *ALL* the backends |
| 247 | * in order to make sure we aren't dealing with a pragma that |
| 248 | * is for another backend. On the other hand, that could |
| 249 | * also be a warning with a separate warning flag. |
| 250 | * |
| 251 | * Leave this for the future, however, the warning classes are |
| 252 | * already defined for future compatibility. |
| 253 | */ |
| 254 | } |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 255 | |
| 256 | /* |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 257 | * Generic pragmas that apply to all output backends; these are handled |
| 258 | * specially so they can be made selective based on the output format. |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 259 | */ |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 260 | static enum directive_result output_pragma(const struct pragma *pragma) |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 261 | { |
| 262 | switch (pragma->opcode) { |
| 263 | case D_PREFIX: |
| 264 | case D_GPREFIX: |
| 265 | set_label_mangle(LM_GPREFIX, pragma->tail); |
| 266 | return DIRR_OK; |
| 267 | case D_SUFFIX: |
| 268 | case D_GSUFFIX: |
| 269 | set_label_mangle(LM_GSUFFIX, pragma->tail); |
| 270 | return DIRR_OK; |
| 271 | case D_LPREFIX: |
| 272 | set_label_mangle(LM_LPREFIX, pragma->tail); |
| 273 | return DIRR_OK; |
| 274 | case D_LSUFFIX: |
| 275 | set_label_mangle(LM_LSUFFIX, pragma->tail); |
| 276 | return DIRR_OK; |
| 277 | default: |
| 278 | return DIRR_UNKNOWN; |
| 279 | } |
| 280 | } |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 281 | |
H. Peter Anvin | f7be8b3 | 2018-06-18 11:32:17 -0700 | [diff] [blame] | 282 | /* |
| 283 | * %pragma limit to set resource limits |
| 284 | */ |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 285 | static enum directive_result limit_pragma(const struct pragma *pragma) |
| 286 | { |
| 287 | return nasm_set_limit(pragma->opname, pragma->tail); |
| 288 | } |