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