output: remove ABSOLUTE handling, OUT_RAWDATA asserts
[nasm.git] / asm / pragma.c
blob71861a94555fb72f6fc2b690699f0dd71c8d356e
1 /* ----------------------------------------------------------------------- *
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
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.
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.
32 * ----------------------------------------------------------------------- */
35 * Parse and handle [pragma] directives. The preprocessor handles
36 * %pragma preproc directives separately, all other namespaces are
37 * simply converted to [pragma].
40 #include "compiler.h"
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <limits.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "assemble.h"
50 #include "error.h"
52 static enum directive_result asm_pragma(const struct pragma *pragma);
53 static enum directive_result limit_pragma(const struct pragma *pragma);
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:
61 * %pragma <facility> <opname> [<options>...]
63 * ... where "facility" can be either a generic facility or a backend
64 * name.
66 * The following names are currently reserved for global facilities;
67 * so far none of these have any defined pragmas at all:
69 * preproc - preprocessor
70 * limit - limit setting
71 * 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")
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.
85 * Adding default-suppressed warnings would, however, be a good idea
86 * at some point.
88 static struct pragma_facility global_pragmas[] =
90 { "asm", asm_pragma },
91 { "limit", limit_pragma },
92 { "list", NULL },
93 { "file", NULL },
94 { "input", NULL },
96 /* None of these should actually happen... */
97 { "preproc", NULL }, /* This shouldn't happen... */
98 { "output", NULL },
99 { "debug", NULL },
100 { "ignore", NULL },
101 { NULL, NULL }
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.)
111 static bool search_pragma_list(const struct pragma_facility *list,
112 const char *default_name,
113 struct pragma *pragma)
115 const struct pragma_facility *pf;
116 enum directive_result rv;
118 if (!list)
119 return false;
121 for (pf = list; pf->name; pf++) {
122 if (!nasm_stricmp(pragma->facility_name, pf->name))
123 goto found_it;
126 if (default_name && !nasm_stricmp(pragma->facility_name, default_name))
127 goto found_it;
129 return false;
131 found_it:
132 pragma->facility = pf;
134 /* If the handler is NULL all pragmas are unknown... */
135 if (pf->handler)
136 rv = pf->handler(pragma);
137 else
138 rv = DIRR_UNKNOWN;
140 switch (rv) {
141 case DIRR_UNKNOWN:
142 switch (pragma->opcode) {
143 case D_none:
144 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
145 "empty %%pragma %s", pragma->facility_name);
146 break;
147 default:
148 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_UNKNOWN_PRAGMA,
149 "unknown %%pragma %s %s",
150 pragma->facility_name, pragma->opname);
151 break;
153 break;
155 case DIRR_OK:
156 case DIRR_ERROR:
157 break; /* Nothing to do */
159 case DIRR_BADPARAM:
161 * This one is an error. Don't use it if forward compatibility
162 * would be compromised, as opposed to an inherent error.
164 nasm_error(ERR_NONFATAL, "bad argument to %%pragma %s %s",
165 pragma->facility_name, pragma->opname);
166 break;
168 default:
169 panic();
171 return true;
174 void process_pragma(char *str)
176 struct pragma pragma;
177 char *p;
179 nasm_zero(pragma);
181 pragma.facility_name = nasm_get_word(str, &p);
182 if (!pragma.facility_name) {
183 nasm_error(ERR_WARNING|ERR_PASS2|ERR_WARN_BAD_PRAGMA,
184 "empty pragma directive");
185 return; /* Empty pragma */
189 * The facility "ignore" means just that; don't even complain of
190 * the absence of an operation.
192 if (!nasm_stricmp(pragma.facility_name, "ignore"))
193 return;
196 * The "output" and "debug" facilities are aliases for the
197 * current output and debug formats, respectively.
199 if (!nasm_stricmp(pragma.facility_name, "output"))
200 pragma.facility_name = ofmt->shortname;
201 if (!nasm_stricmp(pragma.facility_name, "debug"))
202 pragma.facility_name = dfmt->shortname;
204 pragma.opname = nasm_get_word(p, &p);
205 if (!pragma.opname)
206 pragma.opcode = D_none;
207 else
208 pragma.opcode = directive_find(pragma.opname);
210 pragma.tail = nasm_trim_spaces(p);
212 /* Look for a global pragma namespace */
213 if (search_pragma_list(global_pragmas, NULL, &pragma))
214 return;
216 /* Look to see if it is an output backend pragma */
217 if (search_pragma_list(ofmt->pragmas, ofmt->shortname, &pragma))
218 return;
220 /* Look to see if it is a debug format pragma */
221 if (search_pragma_list(dfmt->pragmas, dfmt->shortname, &pragma))
222 return;
225 * Note: it would be nice to warn for an unknown namespace,
226 * but in order to do so we need to walk *ALL* the backends
227 * in order to make sure we aren't dealing with a pragma that
228 * is for another backend. On the other hand, that could
229 * also be a warning with a separate warning flag.
231 * Leave this for the future, however, the warning classes are
232 * already defined for future compatibility.
237 * Pragmas for the assembler proper
239 static enum directive_result asm_pragma(const struct pragma *pragma)
241 switch (pragma->opcode) {
242 case D_PREFIX:
243 case D_GPREFIX:
244 set_label_mangle(LM_GPREFIX, pragma->tail);
245 return DIRR_OK;
246 case D_SUFFIX:
247 case D_GSUFFIX:
248 set_label_mangle(LM_GSUFFIX, pragma->tail);
249 return DIRR_OK;
250 case D_LPREFIX:
251 set_label_mangle(LM_LPREFIX, pragma->tail);
252 return DIRR_OK;
253 case D_LSUFFIX:
254 set_label_mangle(LM_LSUFFIX, pragma->tail);
255 return DIRR_OK;
256 default:
257 return DIRR_UNKNOWN;
261 static enum directive_result limit_pragma(const struct pragma *pragma)
263 return nasm_set_limit(pragma->opname, pragma->tail);