10554
|
1 /* ----------------------------------------------------------------------- *
|
|
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"
|
|
49 #include "assemble.h"
|
|
50 #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 */
|
|
84 static struct pragma_facility global_pragmas[] =
|
|
85 {
|
|
86 { "preproc", NULL }, /* This shouldn't happen... */
|
|
87 { "asm", NULL },
|
|
88 { "list", NULL },
|
|
89 { "file", NULL },
|
|
90 { "input", NULL },
|
|
91 { "output", NULL },
|
|
92 { "debug", NULL },
|
|
93 { "ignore", NULL },
|
|
94 { NULL, NULL }
|
|
95 };
|
|
96
|
|
97 /*
|
|
98 * Search a pragma list for a known pragma facility and if so, invoke
|
|
99 * the handler. Return true if processing is complete.
|
|
100 * The "default name", if set, matches the final NULL entry (used
|
|
101 * for backends, so multiple backends can share the same list under
|
|
102 * some circumstances.)
|
|
103 */
|
|
104 static bool search_pragma_list(const struct pragma_facility *list,
|
|
105 const char *default_name,
|
|
106 struct pragma *pragma)
|
|
107 {
|
|
108 const struct pragma_facility *pf;
|
|
109 enum directive_result rv;
|
|
110
|
|
111 if (!list)
|
|
112 return false;
|
|
113
|
|
114 for (pf = list; pf->name; pf++) {
|
|
115 if (!nasm_stricmp(pragma->facility_name, pf->name))
|
|
116 goto found_it;
|
|
117 }
|
|
118
|
|
119 if (default_name && !nasm_stricmp(pragma->facility_name, default_name))
|
|
120 goto found_it;
|
|
121
|
|
122 return false;
|
|
123
|
|
124 found_it:
|
|
125 pragma->facility = pf;
|
|
126
|
|
127 /* If the handler is NULL all pragmas are unknown... */
|
|
128 if (pf->handler)
|
|
129 rv = pf->handler(pragma);
|
|
130 else
|
|
131 rv = DIRR_UNKNOWN;
|
|
132
|
|
133 switch (rv) {
|
|
134 case DIRR_UNKNOWN:
|
|
135 switch (pragma->opcode) {
|
|
136 case D_none:
|
|
137 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
|
|
138 "empty %%pragma %s", pragma->facility_name);
|
|
139 break;
|
|
140 default:
|
|
141 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_UNKNOWN_PRAGMA,
|
|
142 "unknown %%pragma %s %s",
|
|
143 pragma->facility_name, pragma->opname);
|
|
144 break;
|
|
145 }
|
|
146 break;
|
|
147
|
|
148 case DIRR_OK:
|
|
149 case DIRR_ERROR:
|
|
150 break; /* Nothing to do */
|
|
151
|
|
152 case DIRR_BADPARAM:
|
|
153 /*
|
|
154 * This one is an error. Don't use it if forward compatibility
|
|
155 * would be compromised, as opposed to an inherent error.
|
|
156 */
|
|
157 nasm_error(ERR_NONFATAL, "bad argument to %%pragma %s %s",
|
|
158 pragma->facility_name, pragma->opname);
|
|
159 break;
|
|
160
|
|
161 default:
|
|
162 panic();
|
|
163 }
|
|
164 return true;
|
|
165 }
|
|
166
|
|
167 void process_pragma(char *str)
|
|
168 {
|
|
169 struct pragma pragma;
|
|
170 char *p;
|
|
171
|
|
172 nasm_zero(&pragma);
|
|
173
|
|
174 pragma.facility_name = nasm_get_word(str, &p);
|
|
175 if (!pragma.facility_name) {
|
|
176 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
|
|
177 "empty pragma directive");
|
|
178 return; /* Empty pragma */
|
|
179 }
|
|
180
|
|
181 /*
|
|
182 * The facility "ignore" means just that; don't even complain of
|
|
183 * the absence of an operation.
|
|
184 */
|
|
185 if (!nasm_stricmp(pragma.facility_name, "ignore"))
|
|
186 return;
|
|
187
|
|
188 pragma.opname = nasm_get_word(p, &p);
|
|
189 if (!pragma.opname)
|
|
190 pragma.opcode = D_none;
|
|
191 else
|
|
192 pragma.opcode = find_directive(pragma.opname);
|
|
193
|
|
194 pragma.tail = nasm_skip_spaces(p);
|
|
195
|
|
196 /* Look for a global pragma namespace */
|
|
197 if (search_pragma_list(global_pragmas, NULL, &pragma))
|
|
198 return;
|
|
199
|
|
200 /* Look to see if it is an output backend pragma */
|
|
201 if (search_pragma_list(ofmt->pragmas, ofmt->shortname, &pragma))
|
|
202 return;
|
|
203
|
|
204 /* Look to see if it is a debug format pragma */
|
|
205 if (search_pragma_list(dfmt->pragmas, dfmt->shortname, &pragma))
|
|
206 return;
|
|
207
|
|
208 /*
|
|
209 * Note: it would be nice to warn for an unknown namespace,
|
|
210 * but in order to do so we need to walk *ALL* the backends
|
|
211 * in order to make sure we aren't dealing with a pragma that
|
|
212 * is for another backend. On the other hand, that could
|
|
213 * also be a warning with a separate warning flag.
|
|
214 *
|
|
215 * Leave this for the future, however, the warning classes are
|
|
216 * already defined for future compatibility.
|
|
217 */
|
|
218 }
|