blob: e7dba760e5272c202799af9f5b30c861c99a24ca [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
H. Peter Anvin98578072018-06-01 18:02:54 -070052static enum directive_result asm_pragma(const struct pragma *pragma);
53
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
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 */
86static struct pragma_facility global_pragmas[] =
87{
H. Peter Anvin98578072018-06-01 18:02:54 -070088 { "asm", asm_pragma },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080089 { "list", NULL },
90 { "file", NULL },
91 { "input", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070092
93 /* None of these should actually happen... */
94 { "preproc", NULL }, /* This shouldn't happen... */
H. Peter Anvina6e26d92017-03-07 21:32:37 -080095 { "output", NULL },
H. Peter Anvinb7136482018-05-30 14:42:06 -070096 { "debug", NULL },
H. Peter Anvina6e26d92017-03-07 21:32:37 -080097 { "ignore", NULL },
98 { NULL, NULL }
99};
100
101/*
102 * Search a pragma list for a known pragma facility and if so, invoke
103 * the handler. Return true if processing is complete.
104 * The "default name", if set, matches the final NULL entry (used
105 * for backends, so multiple backends can share the same list under
106 * some circumstances.)
107 */
108static bool search_pragma_list(const struct pragma_facility *list,
109 const char *default_name,
110 struct pragma *pragma)
111{
112 const struct pragma_facility *pf;
H. Peter Anvin87534252017-03-08 20:28:13 -0800113 enum directive_result rv;
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800114
115 if (!list)
116 return false;
117
118 for (pf = list; pf->name; pf++) {
119 if (!nasm_stricmp(pragma->facility_name, pf->name))
120 goto found_it;
121 }
122
123 if (default_name && !nasm_stricmp(pragma->facility_name, default_name))
124 goto found_it;
125
126 return false;
127
128found_it:
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800129 pragma->facility = pf;
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800130
H. Peter Anvin87534252017-03-08 20:28:13 -0800131 /* If the handler is NULL all pragmas are unknown... */
132 if (pf->handler)
133 rv = pf->handler(pragma);
134 else
135 rv = DIRR_UNKNOWN;
136
137 switch (rv) {
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800138 case DIRR_UNKNOWN:
139 switch (pragma->opcode) {
140 case D_none:
141 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
142 "empty %%pragma %s", pragma->facility_name);
143 break;
144 default:
145 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_UNKNOWN_PRAGMA,
146 "unknown %%pragma %s %s",
147 pragma->facility_name, pragma->opname);
148 break;
149 }
150 break;
151
152 case DIRR_OK:
153 case DIRR_ERROR:
154 break; /* Nothing to do */
155
156 case DIRR_BADPARAM:
157 /*
158 * This one is an error. Don't use it if forward compatibility
159 * would be compromised, as opposed to an inherent error.
160 */
161 nasm_error(ERR_NONFATAL, "bad argument to %%pragma %s %s",
162 pragma->facility_name, pragma->opname);
163 break;
164
165 default:
166 panic();
167 }
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800168 return true;
169}
170
171void process_pragma(char *str)
172{
173 struct pragma pragma;
174 char *p;
175
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700176 nasm_zero(pragma);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800177
178 pragma.facility_name = nasm_get_word(str, &p);
179 if (!pragma.facility_name) {
180 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
181 "empty pragma directive");
182 return; /* Empty pragma */
183 }
184
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800185 /*
186 * The facility "ignore" means just that; don't even complain of
187 * the absence of an operation.
188 */
189 if (!nasm_stricmp(pragma.facility_name, "ignore"))
190 return;
191
H. Peter Anvinb7136482018-05-30 14:42:06 -0700192 /*
193 * The "output" and "debug" facilities are aliases for the
194 * current output and debug formats, respectively.
195 */
196 if (!nasm_stricmp(pragma.facility_name, "output"))
197 pragma.facility_name = ofmt->shortname;
198 if (!nasm_stricmp(pragma.facility_name, "debug"))
199 pragma.facility_name = dfmt->shortname;
200
H. Peter Anvind9493fa2017-03-08 20:05:41 -0800201 pragma.opname = nasm_get_word(p, &p);
202 if (!pragma.opname)
203 pragma.opcode = D_none;
204 else
H. Peter Anvin5253f582017-04-03 00:09:58 -0700205 pragma.opcode = directive_find(pragma.opname);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800206
H. Peter Anvin98578072018-06-01 18:02:54 -0700207 pragma.tail = nasm_trim_spaces(p);
H. Peter Anvina6e26d92017-03-07 21:32:37 -0800208
209 /* Look for a global pragma namespace */
210 if (search_pragma_list(global_pragmas, NULL, &pragma))
211 return;
212
213 /* Look to see if it is an output backend pragma */
214 if (search_pragma_list(ofmt->pragmas, ofmt->shortname, &pragma))
215 return;
216
217 /* Look to see if it is a debug format pragma */
218 if (search_pragma_list(dfmt->pragmas, dfmt->shortname, &pragma))
219 return;
220
221 /*
222 * Note: it would be nice to warn for an unknown namespace,
223 * but in order to do so we need to walk *ALL* the backends
224 * in order to make sure we aren't dealing with a pragma that
225 * is for another backend. On the other hand, that could
226 * also be a warning with a separate warning flag.
227 *
228 * Leave this for the future, however, the warning classes are
229 * already defined for future compatibility.
230 */
231}
H. Peter Anvin98578072018-06-01 18:02:54 -0700232
233/*
234 * Pragmas for the assembler proper
235 */
236static enum directive_result asm_pragma(const struct pragma *pragma)
237{
238 switch (pragma->opcode) {
239 case D_PREFIX:
240 case D_GPREFIX:
241 set_label_mangle(LM_GPREFIX, pragma->tail);
242 return DIRR_OK;
243 case D_SUFFIX:
244 case D_GSUFFIX:
245 set_label_mangle(LM_GSUFFIX, pragma->tail);
246 return DIRR_OK;
247 case D_LPREFIX:
248 set_label_mangle(LM_LPREFIX, pragma->tail);
249 return DIRR_OK;
250 case D_LSUFFIX:
251 set_label_mangle(LM_LSUFFIX, pragma->tail);
252 return DIRR_OK;
253 default:
254 return DIRR_UNKNOWN;
255 }
256}