blob: 85e8c9cd0d3577c1da8fd22e1164507e44498e7c [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>
43#include <string.h>
44#include <ctype.h>
45#include <limits.h>
46
47#include "nasm.h"
48#include "nasmlib.h"
Cyrill Gorcunov48541332017-03-08 11:39:42 +030049#include "assemble.h"
H. Peter Anvina6e26d92017-03-07 21:32:37 -080050#include "error.h"
51
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070052static enum directive_result output_pragma(const struct pragma *pragma);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070053static enum directive_result limit_pragma(const struct pragma *pragma);
H. Peter Anvin98578072018-06-01 18:02:54 -070054
H. Peter Anvina6e26d92017-03-07 21:32:37 -080055/*
56 * Handle [pragma] directives. [pragma] is generally produced by
57 * the %pragma preprocessor directive, which simply passes on any
58 * string that it finds *except* %pragma preproc. The idea is
59 * that pragmas are of the form:
60 *
61 * %pragma <facility> <opname> [<options>...]
62 *
63 * ... where "facility" can be either a generic facility or a backend
64 * name.
65 *
66 * The following names are currently reserved for global facilities;
67 * so far none of these have any defined pragmas at all:
68 *
69 * preproc - preprocessor
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070070 * limit - limit setting
H. Peter Anvina6e26d92017-03-07 21:32:37 -080071 * asm - assembler
72 * list - listing generator
73 * file - generic file handling
74 * input - input file handling
75 * output - backend-independent output handling
76 * debug - backend-independent debug handling
77 * ignore - dummy pragma (can be used to "comment out")
78 *
79 * This function should generally not error out if it doesn't understand
80 * what a pragma is for, for unknown arguments, etc; the whole point of
81 * a pragma is that future releases might add new ones that should be
82 * ignored rather than be an error. Erroring out is acceptable for
83 * known pragmas suffering from parsing errors and so on.
84 *
85 * Adding default-suppressed warnings would, however, be a good idea
86 * at some point.
87 */
88static struct pragma_facility global_pragmas[] =
89{
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070090 { "asm", NULL },
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070091 { "limit", limit_pragma },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080092 { "list", NULL },
93 { "file", NULL },
94 { "input", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070095
H. Peter Anvinf7be8b32018-06-18 11:32:17 -070096 /* None of these should actually happen due to special handling */
97 { "preproc", NULL }, /* Handled in the preprocessor by necessity */
H. Peter Anvina6e26d92017-03-07 21:32:37 -080098 { "output", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070099 { "debug", NULL },
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800100 { "ignore", NULL },
101 { NULL, NULL }
102};
103
104/*
105 * Search a pragma list for a known pragma facility and if so, invoke
106 * the handler. Return true if processing is complete.
107 * The "default name", if set, matches the final NULL entry (used
108 * for backends, so multiple backends can share the same list under
109 * some circumstances.)
110 */
111static bool search_pragma_list(const struct pragma_facility *list,
112 const char *default_name,
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700113 pragma_handler generic_handler,
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800114 struct pragma *pragma)
115{
116 const struct pragma_facility *pf;
H. Peter Anvin87534252017-03-08 20:28:13 -0800117 enum directive_result rv;
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800118
119 if (!list)
120 return false;
121
122 for (pf = list; pf->name; pf++) {
123 if (!nasm_stricmp(pragma->facility_name, pf->name))
124 goto found_it;
125 }
126
127 if (default_name && !nasm_stricmp(pragma->facility_name, default_name))
128 goto found_it;
129
130 return false;
131
132found_it:
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800133 pragma->facility = pf;
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800134
H. Peter Anvin87534252017-03-08 20:28:13 -0800135 /* If the handler is NULL all pragmas are unknown... */
136 if (pf->handler)
137 rv = pf->handler(pragma);
138 else
139 rv = DIRR_UNKNOWN;
140
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700141 /* Is there an additional, applicable generic handler? */
142 if (rv == DIRR_UNKNOWN && generic_handler)
143 rv = generic_handler(pragma);
144
H. Peter Anvin87534252017-03-08 20:28:13 -0800145 switch (rv) {
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800146 case DIRR_UNKNOWN:
147 switch (pragma->opcode) {
148 case D_none:
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800149 nasm_error(ERR_WARNING|ERR_PASS2|WARN_BAD_PRAGMA,
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800150 "empty %%pragma %s", pragma->facility_name);
151 break;
152 default:
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800153 nasm_error(ERR_WARNING|ERR_PASS2|WARN_UNKNOWN_PRAGMA,
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800154 "unknown %%pragma %s %s",
155 pragma->facility_name, pragma->opname);
156 break;
157 }
158 break;
159
160 case DIRR_OK:
161 case DIRR_ERROR:
162 break; /* Nothing to do */
163
164 case DIRR_BADPARAM:
165 /*
166 * This one is an error. Don't use it if forward compatibility
167 * would be compromised, as opposed to an inherent error.
168 */
169 nasm_error(ERR_NONFATAL, "bad argument to %%pragma %s %s",
170 pragma->facility_name, pragma->opname);
171 break;
172
173 default:
174 panic();
175 }
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800176 return true;
177}
178
179void process_pragma(char *str)
180{
181 struct pragma pragma;
182 char *p;
183
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700184 nasm_zero(pragma);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800185
186 pragma.facility_name = nasm_get_word(str, &p);
187 if (!pragma.facility_name) {
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800188 nasm_error(ERR_WARNING|ERR_PASS2|WARN_BAD_PRAGMA,
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800189 "empty pragma directive");
190 return; /* Empty pragma */
191 }
192
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800193 /*
194 * The facility "ignore" means just that; don't even complain of
195 * the absence of an operation.
196 */
197 if (!nasm_stricmp(pragma.facility_name, "ignore"))
198 return;
199
H. Peter Anvinb7136482018-05-30 14:42:06 -0700200 /*
201 * The "output" and "debug" facilities are aliases for the
202 * current output and debug formats, respectively.
203 */
204 if (!nasm_stricmp(pragma.facility_name, "output"))
205 pragma.facility_name = ofmt->shortname;
206 if (!nasm_stricmp(pragma.facility_name, "debug"))
207 pragma.facility_name = dfmt->shortname;
208
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800209 pragma.opname = nasm_get_word(p, &p);
210 if (!pragma.opname)
211 pragma.opcode = D_none;
212 else
H. Peter Anvin5253f582017-04-03 00:09:58 -0700213 pragma.opcode = directive_find(pragma.opname);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800214
H. Peter Anvin98578072018-06-01 18:02:54 -0700215 pragma.tail = nasm_trim_spaces(p);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800216
217 /* Look for a global pragma namespace */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700218 if (search_pragma_list(global_pragmas, NULL, NULL, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800219 return;
220
221 /* Look to see if it is an output backend pragma */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700222 if (search_pragma_list(ofmt->pragmas, ofmt->shortname,
223 output_pragma, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800224 return;
225
226 /* Look to see if it is a debug format pragma */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700227 if (search_pragma_list(dfmt->pragmas, dfmt->shortname, NULL, &pragma))
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800228 return;
229
230 /*
231 * Note: it would be nice to warn for an unknown namespace,
232 * but in order to do so we need to walk *ALL* the backends
233 * in order to make sure we aren't dealing with a pragma that
234 * is for another backend. On the other hand, that could
235 * also be a warning with a separate warning flag.
236 *
237 * Leave this for the future, however, the warning classes are
238 * already defined for future compatibility.
239 */
240}
H. Peter Anvin98578072018-06-01 18:02:54 -0700241
242/*
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700243 * Generic pragmas that apply to all output backends; these are handled
244 * specially so they can be made selective based on the output format.
H. Peter Anvin98578072018-06-01 18:02:54 -0700245 */
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700246static enum directive_result output_pragma(const struct pragma *pragma)
H. Peter Anvin98578072018-06-01 18:02:54 -0700247{
248 switch (pragma->opcode) {
249 case D_PREFIX:
250 case D_GPREFIX:
251 set_label_mangle(LM_GPREFIX, pragma->tail);
252 return DIRR_OK;
253 case D_SUFFIX:
254 case D_GSUFFIX:
255 set_label_mangle(LM_GSUFFIX, pragma->tail);
256 return DIRR_OK;
257 case D_LPREFIX:
258 set_label_mangle(LM_LPREFIX, pragma->tail);
259 return DIRR_OK;
260 case D_LSUFFIX:
261 set_label_mangle(LM_LSUFFIX, pragma->tail);
262 return DIRR_OK;
263 default:
264 return DIRR_UNKNOWN;
265 }
266}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700267
H. Peter Anvinf7be8b32018-06-18 11:32:17 -0700268/*
269 * %pragma limit to set resource limits
270 */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700271static enum directive_result limit_pragma(const struct pragma *pragma)
272{
273 return nasm_set_limit(pragma->opname, pragma->tail);
274}