blob: cbe0cc7bc2aa2c37f57ae164255e5b9123fb60ed [file] [log] [blame]
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
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
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
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
67 * asm - assembler
68 * list - listing generator
69 * file - generic file handling
70 * input - input file handling
71 * output - backend-independent output handling
72 * debug - backend-independent debug handling
73 * ignore - dummy pragma (can be used to "comment out")
74 *
75 * This function should generally not error out if it doesn't understand
76 * what a pragma is for, for unknown arguments, etc; the whole point of
77 * a pragma is that future releases might add new ones that should be
78 * ignored rather than be an error. Erroring out is acceptable for
79 * known pragmas suffering from parsing errors and so on.
80 *
81 * Adding default-suppressed warnings would, however, be a good idea
82 * at some point.
83 */
84static struct pragma_facility global_pragmas[] =
85{
H. Peter Anvina6e26d92017-03-07 21:32:37 -080086 { "asm", NULL },
87 { "list", NULL },
88 { "file", NULL },
89 { "input", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070090
91 /* None of these should actually happen... */
92 { "preproc", NULL }, /* This shouldn't happen... */
H. Peter Anvina6e26d92017-03-07 21:32:37 -080093 { "output", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070094 { "debug", NULL },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080095 { "ignore", NULL },
96 { NULL, NULL }
97};
98
99/*
100 * Search a pragma list for a known pragma facility and if so, invoke
101 * the handler. Return true if processing is complete.
102 * The "default name", if set, matches the final NULL entry (used
103 * for backends, so multiple backends can share the same list under
104 * some circumstances.)
105 */
106static bool search_pragma_list(const struct pragma_facility *list,
107 const char *default_name,
108 struct pragma *pragma)
109{
110 const struct pragma_facility *pf;
H. Peter Anvin87534252017-03-08 20:28:13 -0800111 enum directive_result rv;
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800112
113 if (!list)
114 return false;
115
116 for (pf = list; pf->name; pf++) {
117 if (!nasm_stricmp(pragma->facility_name, pf->name))
118 goto found_it;
119 }
120
121 if (default_name && !nasm_stricmp(pragma->facility_name, default_name))
122 goto found_it;
123
124 return false;
125
126found_it:
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800127 pragma->facility = pf;
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800128
H. Peter Anvin87534252017-03-08 20:28:13 -0800129 /* If the handler is NULL all pragmas are unknown... */
130 if (pf->handler)
131 rv = pf->handler(pragma);
132 else
133 rv = DIRR_UNKNOWN;
134
135 switch (rv) {
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800136 case DIRR_UNKNOWN:
137 switch (pragma->opcode) {
138 case D_none:
139 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
140 "empty %%pragma %s", pragma->facility_name);
141 break;
142 default:
143 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_UNKNOWN_PRAGMA,
144 "unknown %%pragma %s %s",
145 pragma->facility_name, pragma->opname);
146 break;
147 }
148 break;
149
150 case DIRR_OK:
151 case DIRR_ERROR:
152 break; /* Nothing to do */
153
154 case DIRR_BADPARAM:
155 /*
156 * This one is an error. Don't use it if forward compatibility
157 * would be compromised, as opposed to an inherent error.
158 */
159 nasm_error(ERR_NONFATAL, "bad argument to %%pragma %s %s",
160 pragma->facility_name, pragma->opname);
161 break;
162
163 default:
164 panic();
165 }
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800166 return true;
167}
168
169void process_pragma(char *str)
170{
171 struct pragma pragma;
172 char *p;
173
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700174 nasm_zero(pragma);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800175
176 pragma.facility_name = nasm_get_word(str, &p);
177 if (!pragma.facility_name) {
178 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
179 "empty pragma directive");
180 return; /* Empty pragma */
181 }
182
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800183 /*
184 * The facility "ignore" means just that; don't even complain of
185 * the absence of an operation.
186 */
187 if (!nasm_stricmp(pragma.facility_name, "ignore"))
188 return;
189
H. Peter Anvinb7136482018-05-30 14:42:06 -0700190 /*
191 * The "output" and "debug" facilities are aliases for the
192 * current output and debug formats, respectively.
193 */
194 if (!nasm_stricmp(pragma.facility_name, "output"))
195 pragma.facility_name = ofmt->shortname;
196 if (!nasm_stricmp(pragma.facility_name, "debug"))
197 pragma.facility_name = dfmt->shortname;
198
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800199 pragma.opname = nasm_get_word(p, &p);
200 if (!pragma.opname)
201 pragma.opcode = D_none;
202 else
H. Peter Anvin5253f582017-04-03 00:09:58 -0700203 pragma.opcode = directive_find(pragma.opname);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800204
205 pragma.tail = nasm_skip_spaces(p);
206
207 /* Look for a global pragma namespace */
208 if (search_pragma_list(global_pragmas, NULL, &pragma))
209 return;
210
211 /* Look to see if it is an output backend pragma */
212 if (search_pragma_list(ofmt->pragmas, ofmt->shortname, &pragma))
213 return;
214
215 /* Look to see if it is a debug format pragma */
216 if (search_pragma_list(dfmt->pragmas, dfmt->shortname, &pragma))
217 return;
218
219 /*
220 * Note: it would be nice to warn for an unknown namespace,
221 * but in order to do so we need to walk *ALL* the backends
222 * in order to make sure we aren't dealing with a pragma that
223 * is for another backend. On the other hand, that could
224 * also be a warning with a separate warning flag.
225 *
226 * Leave this for the future, however, the warning classes are
227 * already defined for future compatibility.
228 */
229}