blob: 8cfbafb89999cf2081f78b589a2f0fd381a171cf [file] [log] [blame]
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001/* ----------------------------------------------------------------------- *
2 *
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -07003 * Copyright 1996-2019 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
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080042#include "nctype.h"
H. Peter Anvina6e26d92017-03-07 21:32:37 -080043
44#include "nasm.h"
45#include "nasmlib.h"
Cyrill Gorcunov48541332017-03-08 11:39:42 +030046#include "assemble.h"
H. Peter Anvina6e26d92017-03-07 21:32:37 -080047#include "error.h"
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -070048#include "listing.h"
H. Peter Anvina6e26d92017-03-07 21:32:37 -080049
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070050static enum directive_result output_pragma(const struct pragma *pragma);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070051static enum directive_result limit_pragma(const struct pragma *pragma);
H. Peter Anvin98578072018-06-01 18:02:54 -070052
H. Peter Anvina6e26d92017-03-07 21:32:37 -080053/*
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 Anvin987dc9c2018-06-12 13:50:37 -070068 * limit - limit setting
H. Peter Anvina6e26d92017-03-07 21:32:37 -080069 * 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 */
86static struct pragma_facility global_pragmas[] =
87{
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070088 { "asm", NULL },
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070089 { "limit", limit_pragma },
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -070090 { "list", list_pragma },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080091 { "file", NULL },
92 { "input", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070093
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070094 /* None of these should actually happen due to special handling */
95 { "preproc", NULL }, /* Handled in the preprocessor by necessity */
H. Peter Anvina6e26d92017-03-07 21:32:37 -080096 { "output", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070097 { "debug", NULL },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080098 { "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 */
109static bool search_pragma_list(const struct pragma_facility *list,
110 const char *default_name,
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700111 pragma_handler generic_handler,
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800112 struct pragma *pragma)
113{
114 const struct pragma_facility *pf;
H. Peter Anvin87534252017-03-08 20:28:13 -0800115 enum directive_result rv;
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800116
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
130found_it:
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800131 pragma->facility = pf;
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800132
H. Peter Anvin87534252017-03-08 20:28:13 -0800133 /* 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 Anvinf7be8b32018-06-18 11:32:17 -0700139 /* Is there an additional, applicable generic handler? */
140 if (rv == DIRR_UNKNOWN && generic_handler)
141 rv = generic_handler(pragma);
142
H. Peter Anvin87534252017-03-08 20:28:13 -0800143 switch (rv) {
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800144 case DIRR_UNKNOWN:
145 switch (pragma->opcode) {
146 case D_none:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800147 /*!
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700148 *!pragma-bad [off] empty or malformed %pragma
149 *!=bad-pragma
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800150 *! warns about a malformed or otherwise unparsable
151 *! \c{%pragma} directive.
152 */
H. Peter Anvin (Intel)873ceee2019-08-06 19:18:36 -0700153 nasm_warn(ERR_PASS2|WARN_PRAGMA_BAD,
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 /*!
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700158 *!pragma-unknown [off] unknown %pragma facility or directive
159 *!=unknown-pragma
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800160 *! warns about an unknown \c{%pragma} directive.
161 *! This is not yet implemented for most cases.
162 */
H. Peter Anvin (Intel)873ceee2019-08-06 19:18:36 -0700163 nasm_warn(ERR_PASS2|WARN_PRAGMA_UNKNOWN,
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800164 "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 Anvina6e26d92017-03-07 21:32:37 -0800186 return true;
187}
188
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800189/* This warning message is intended for future use */
190/*!
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700191 *!pragma-na [off] %pragma not applicable to this compilation
192 *!=not-my-pragma
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800193 *! 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 Anvina6e26d92017-03-07 21:32:37 -0800197void process_pragma(char *str)
198{
199 struct pragma pragma;
200 char *p;
201
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700202 nasm_zero(pragma);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800203
204 pragma.facility_name = nasm_get_word(str, &p);
205 if (!pragma.facility_name) {
H. Peter Anvin (Intel)873ceee2019-08-06 19:18:36 -0700206 nasm_warn(ERR_PASS2|WARN_PRAGMA_BAD,
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800207 "empty pragma directive");
208 return; /* Empty pragma */
209 }
210
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800211 /*
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 Anvinb7136482018-05-30 14:42:06 -0700218 /*
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 Anvind9493fa2017-03-08 20:05:41 -0800227 pragma.opname = nasm_get_word(p, &p);
228 if (!pragma.opname)
229 pragma.opcode = D_none;
230 else
H. Peter Anvin5253f582017-04-03 00:09:58 -0700231 pragma.opcode = directive_find(pragma.opname);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800232
H. Peter Anvin98578072018-06-01 18:02:54 -0700233 pragma.tail = nasm_trim_spaces(p);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800234
235 /* Look for a global pragma namespace */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700236 if (search_pragma_list(global_pragmas, NULL, NULL, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800237 return;
238
239 /* Look to see if it is an output backend pragma */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700240 if (search_pragma_list(ofmt->pragmas, ofmt->shortname,
241 output_pragma, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800242 return;
243
244 /* Look to see if it is a debug format pragma */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700245 if (search_pragma_list(dfmt->pragmas, dfmt->shortname, NULL, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800246 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 Anvin98578072018-06-01 18:02:54 -0700259
260/*
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700261 * 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 Anvin98578072018-06-01 18:02:54 -0700263 */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700264static enum directive_result output_pragma(const struct pragma *pragma)
H. Peter Anvin98578072018-06-01 18:02:54 -0700265{
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 Anvin987dc9c2018-06-12 13:50:37 -0700285
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700286/*
287 * %pragma limit to set resource limits
288 */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700289static enum directive_result limit_pragma(const struct pragma *pragma)
290{
291 return nasm_set_limit(pragma->opname, pragma->tail);
292}