blob: 363e593c67bd7d6f8bc08428f73eba99c709baef [file] [log] [blame]
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001/* ----------------------------------------------------------------------- *
2 *
H. Peter Anvinf7be8b32018-06-18 11:32:17 -07003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvina6e26d92017-03-07 21:32:37 -08004 * 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>
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080043#include "nctype.h"
H. Peter Anvina6e26d92017-03-07 21:32:37 -080044#include <limits.h>
45
46#include "nasm.h"
47#include "nasmlib.h"
Cyrill Gorcunov48541332017-03-08 11:39:42 +030048#include "assemble.h"
H. Peter Anvina6e26d92017-03-07 21:32:37 -080049#include "error.h"
50
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070051static enum directive_result output_pragma(const struct pragma *pragma);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070052static enum directive_result limit_pragma(const struct pragma *pragma);
H. Peter Anvin98578072018-06-01 18:02:54 -070053
H. Peter Anvina6e26d92017-03-07 21:32:37 -080054/*
55 * Handle [pragma] directives. [pragma] is generally produced by
56 * the %pragma preprocessor directive, which simply passes on any
57 * string that it finds *except* %pragma preproc. The idea is
58 * that pragmas are of the form:
59 *
60 * %pragma <facility> <opname> [<options>...]
61 *
62 * ... where "facility" can be either a generic facility or a backend
63 * name.
64 *
65 * The following names are currently reserved for global facilities;
66 * so far none of these have any defined pragmas at all:
67 *
68 * preproc - preprocessor
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070069 * limit - limit setting
H. Peter Anvina6e26d92017-03-07 21:32:37 -080070 * asm - assembler
71 * list - listing generator
72 * file - generic file handling
73 * input - input file handling
74 * output - backend-independent output handling
75 * debug - backend-independent debug handling
76 * ignore - dummy pragma (can be used to "comment out")
77 *
78 * This function should generally not error out if it doesn't understand
79 * what a pragma is for, for unknown arguments, etc; the whole point of
80 * a pragma is that future releases might add new ones that should be
81 * ignored rather than be an error. Erroring out is acceptable for
82 * known pragmas suffering from parsing errors and so on.
83 *
84 * Adding default-suppressed warnings would, however, be a good idea
85 * at some point.
86 */
87static struct pragma_facility global_pragmas[] =
88{
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070089 { "asm", NULL },
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070090 { "limit", limit_pragma },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080091 { "list", NULL },
92 { "file", NULL },
93 { "input", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070094
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070095 /* None of these should actually happen due to special handling */
96 { "preproc", NULL }, /* Handled in the preprocessor by necessity */
H. Peter Anvina6e26d92017-03-07 21:32:37 -080097 { "output", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070098 { "debug", NULL },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080099 { "ignore", NULL },
100 { NULL, NULL }
101};
102
103/*
104 * Search a pragma list for a known pragma facility and if so, invoke
105 * the handler. Return true if processing is complete.
106 * The "default name", if set, matches the final NULL entry (used
107 * for backends, so multiple backends can share the same list under
108 * some circumstances.)
109 */
110static bool search_pragma_list(const struct pragma_facility *list,
111 const char *default_name,
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700112 pragma_handler generic_handler,
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800113 struct pragma *pragma)
114{
115 const struct pragma_facility *pf;
H. Peter Anvin87534252017-03-08 20:28:13 -0800116 enum directive_result rv;
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800117
118 if (!list)
119 return false;
120
121 for (pf = list; pf->name; pf++) {
122 if (!nasm_stricmp(pragma->facility_name, pf->name))
123 goto found_it;
124 }
125
126 if (default_name && !nasm_stricmp(pragma->facility_name, default_name))
127 goto found_it;
128
129 return false;
130
131found_it:
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800132 pragma->facility = pf;
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800133
H. Peter Anvin87534252017-03-08 20:28:13 -0800134 /* If the handler is NULL all pragmas are unknown... */
135 if (pf->handler)
136 rv = pf->handler(pragma);
137 else
138 rv = DIRR_UNKNOWN;
139
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700140 /* Is there an additional, applicable generic handler? */
141 if (rv == DIRR_UNKNOWN && generic_handler)
142 rv = generic_handler(pragma);
143
H. Peter Anvin87534252017-03-08 20:28:13 -0800144 switch (rv) {
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800145 case DIRR_UNKNOWN:
146 switch (pragma->opcode) {
147 case D_none:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800148 /*!
149 *!bad-pragma [off] empty or malformed %pragma
150 *! warns about a malformed or otherwise unparsable
151 *! \c{%pragma} directive.
152 */
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800153 nasm_error(ERR_WARNING|ERR_PASS2|WARN_BAD_PRAGMA,
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800154 "empty %%pragma %s", pragma->facility_name);
155 break;
156 default:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800157 /*!
158 *!unknown-pragma [off] unknown %pragma facility or directive
159 *! warns about an unknown \c{%pragma} directive.
160 *! This is not yet implemented for most cases.
161 */
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800162 nasm_error(ERR_WARNING|ERR_PASS2|WARN_UNKNOWN_PRAGMA,
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800163 "unknown %%pragma %s %s",
164 pragma->facility_name, pragma->opname);
165 break;
166 }
167 break;
168
169 case DIRR_OK:
170 case DIRR_ERROR:
171 break; /* Nothing to do */
172
173 case DIRR_BADPARAM:
174 /*
175 * This one is an error. Don't use it if forward compatibility
176 * would be compromised, as opposed to an inherent error.
177 */
178 nasm_error(ERR_NONFATAL, "bad argument to %%pragma %s %s",
179 pragma->facility_name, pragma->opname);
180 break;
181
182 default:
183 panic();
184 }
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800185 return true;
186}
187
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800188/* This warning message is intended for future use */
189/*!
190 *!not-my-pragma [off] %pragma not applicable to this compilation
191 *! warns about a \c{%pragma} directive which is not applicable to
192 *! this particular assembly session. This is not yet implemented.
193 */
194
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800195void process_pragma(char *str)
196{
197 struct pragma pragma;
198 char *p;
199
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700200 nasm_zero(pragma);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800201
202 pragma.facility_name = nasm_get_word(str, &p);
203 if (!pragma.facility_name) {
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800204 nasm_error(ERR_WARNING|ERR_PASS2|WARN_BAD_PRAGMA,
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800205 "empty pragma directive");
206 return; /* Empty pragma */
207 }
208
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800209 /*
210 * The facility "ignore" means just that; don't even complain of
211 * the absence of an operation.
212 */
213 if (!nasm_stricmp(pragma.facility_name, "ignore"))
214 return;
215
H. Peter Anvinb7136482018-05-30 14:42:06 -0700216 /*
217 * The "output" and "debug" facilities are aliases for the
218 * current output and debug formats, respectively.
219 */
220 if (!nasm_stricmp(pragma.facility_name, "output"))
221 pragma.facility_name = ofmt->shortname;
222 if (!nasm_stricmp(pragma.facility_name, "debug"))
223 pragma.facility_name = dfmt->shortname;
224
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800225 pragma.opname = nasm_get_word(p, &p);
226 if (!pragma.opname)
227 pragma.opcode = D_none;
228 else
H. Peter Anvin5253f582017-04-03 00:09:58 -0700229 pragma.opcode = directive_find(pragma.opname);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800230
H. Peter Anvin98578072018-06-01 18:02:54 -0700231 pragma.tail = nasm_trim_spaces(p);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800232
233 /* Look for a global pragma namespace */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700234 if (search_pragma_list(global_pragmas, NULL, NULL, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800235 return;
236
237 /* Look to see if it is an output backend pragma */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700238 if (search_pragma_list(ofmt->pragmas, ofmt->shortname,
239 output_pragma, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800240 return;
241
242 /* Look to see if it is a debug format pragma */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700243 if (search_pragma_list(dfmt->pragmas, dfmt->shortname, NULL, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800244 return;
245
246 /*
247 * Note: it would be nice to warn for an unknown namespace,
248 * but in order to do so we need to walk *ALL* the backends
249 * in order to make sure we aren't dealing with a pragma that
250 * is for another backend. On the other hand, that could
251 * also be a warning with a separate warning flag.
252 *
253 * Leave this for the future, however, the warning classes are
254 * already defined for future compatibility.
255 */
256}
H. Peter Anvin98578072018-06-01 18:02:54 -0700257
258/*
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700259 * Generic pragmas that apply to all output backends; these are handled
260 * specially so they can be made selective based on the output format.
H. Peter Anvin98578072018-06-01 18:02:54 -0700261 */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700262static enum directive_result output_pragma(const struct pragma *pragma)
H. Peter Anvin98578072018-06-01 18:02:54 -0700263{
264 switch (pragma->opcode) {
265 case D_PREFIX:
266 case D_GPREFIX:
267 set_label_mangle(LM_GPREFIX, pragma->tail);
268 return DIRR_OK;
269 case D_SUFFIX:
270 case D_GSUFFIX:
271 set_label_mangle(LM_GSUFFIX, pragma->tail);
272 return DIRR_OK;
273 case D_LPREFIX:
274 set_label_mangle(LM_LPREFIX, pragma->tail);
275 return DIRR_OK;
276 case D_LSUFFIX:
277 set_label_mangle(LM_LSUFFIX, pragma->tail);
278 return DIRR_OK;
279 default:
280 return DIRR_UNKNOWN;
281 }
282}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700283
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700284/*
285 * %pragma limit to set resource limits
286 */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700287static enum directive_result limit_pragma(const struct pragma *pragma)
288{
289 return nasm_set_limit(pragma->opname, pragma->tail);
290}