BR3392439: make sure to update globalbits if appropriate
[nasm.git] / asm / directiv.c
blob354b591a5ad9f35e11d18eae0bef29ea7ffcdad1
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 assembler directives
38 #include "compiler.h"
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <limits.h>
45 #include "nasm.h"
46 #include "nasmlib.h"
47 #include "error.h"
48 #include "float.h"
49 #include "stdscan.h"
50 #include "preproc.h"
51 #include "eval.h"
52 #include "assemble.h"
53 #include "outform.h"
54 #include "listing.h"
55 #include "labels.h"
56 #include "iflag.h"
58 static iflag_t get_cpu(char *value)
60 iflag_t r;
62 iflag_clear_all(&r);
64 if (!strcmp(value, "8086"))
65 iflag_set(&r, IF_8086);
66 else if (!strcmp(value, "186"))
67 iflag_set(&r, IF_186);
68 else if (!strcmp(value, "286"))
69 iflag_set(&r, IF_286);
70 else if (!strcmp(value, "386"))
71 iflag_set(&r, IF_386);
72 else if (!strcmp(value, "486"))
73 iflag_set(&r, IF_486);
74 else if (!strcmp(value, "586") ||
75 !nasm_stricmp(value, "pentium"))
76 iflag_set(&r, IF_PENT);
77 else if (!strcmp(value, "686") ||
78 !nasm_stricmp(value, "ppro") ||
79 !nasm_stricmp(value, "pentiumpro") ||
80 !nasm_stricmp(value, "p2"))
81 iflag_set(&r, IF_P6);
82 else if (!nasm_stricmp(value, "p3") ||
83 !nasm_stricmp(value, "katmai"))
84 iflag_set(&r, IF_KATMAI);
85 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
86 !nasm_stricmp(value, "willamette"))
87 iflag_set(&r, IF_WILLAMETTE);
88 else if (!nasm_stricmp(value, "prescott"))
89 iflag_set(&r, IF_PRESCOTT);
90 else if (!nasm_stricmp(value, "x64") ||
91 !nasm_stricmp(value, "x86-64"))
92 iflag_set(&r, IF_X86_64);
93 else if (!nasm_stricmp(value, "ia64") ||
94 !nasm_stricmp(value, "ia-64") ||
95 !nasm_stricmp(value, "itanium")||
96 !nasm_stricmp(value, "itanic") ||
97 !nasm_stricmp(value, "merced"))
98 iflag_set(&r, IF_IA64);
99 else {
100 iflag_set(&r, IF_PLEVEL);
101 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
102 "unknown 'cpu' type");
104 return r;
107 static int get_bits(char *value)
109 int i;
111 if ((i = atoi(value)) == 16)
112 return i; /* set for a 16-bit segment */
113 else if (i == 32) {
114 if (iflag_ffs(&cpu) < IF_386) {
115 nasm_error(ERR_NONFATAL,
116 "cannot specify 32-bit segment on processor below a 386");
117 i = 16;
119 } else if (i == 64) {
120 if (iflag_ffs(&cpu) < IF_X86_64) {
121 nasm_error(ERR_NONFATAL,
122 "cannot specify 64-bit segment on processor below an x86-64");
123 i = 16;
125 } else {
126 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
127 "`%s' is not a valid segment size; must be 16, 32 or 64",
128 value);
129 i = 16;
131 return i;
134 static enum directive parse_directive_line(char **directive, char **value)
136 char *p, *q, *buf;
138 buf = nasm_skip_spaces(*directive);
141 * It should be enclosed in [ ].
142 * XXX: we don't check there is nothing else on the remainder of the
143 * line, except a possible comment.
145 if (*buf != '[')
146 return D_none;
147 q = strchr(buf, ']');
148 if (!q)
149 return D_corrupt;
152 * Strip off the comments. XXX: this doesn't account for quoted
153 * strings inside a directive. We should really strip the
154 * comments in generic code, not here. While we're at it, it
155 * would be better to pass the backend a series of tokens instead
156 * of a raw string, and actually process quoted strings for it,
157 * like of like argv is handled in C.
159 p = strchr(buf, ';');
160 if (p) {
161 if (p < q) /* ouch! somewhere inside */
162 return D_corrupt;
163 *p = '\0';
166 /* no brace, no trailing spaces */
167 *q = '\0';
168 nasm_zap_spaces_rev(--q);
170 /* directive */
171 p = nasm_skip_spaces(++buf);
172 q = nasm_skip_word(p);
173 if (!q)
174 return D_corrupt; /* sigh... no value there */
175 *q = '\0';
176 *directive = p;
178 /* and value finally */
179 p = nasm_skip_spaces(++q);
180 *value = p;
182 return directive_find(*directive);
186 * Process a line from the assembler and try to handle it if it
187 * is a directive. Return true if the line was handled (including
188 * if it was an error), false otherwise.
190 bool process_directives(char *directive)
192 enum directive d;
193 char *value, *p, *q, *special;
194 struct tokenval tokval;
195 bool bad_param = false;
196 int pass2 = passn > 1 ? 2 : 1;
198 d = parse_directive_line(&directive, &value);
200 switch (d) {
201 case D_none:
202 return D_none; /* Not a directive */
204 case D_corrupt:
205 nasm_error(ERR_NONFATAL, "invalid directive line");
206 break;
208 default: /* It's a backend-specific directive */
209 switch (ofmt->directive(d, value, pass2)) {
210 case DIRR_UNKNOWN:
211 goto unknown;
212 case DIRR_OK:
213 case DIRR_ERROR:
214 break;
215 case DIRR_BADPARAM:
216 bad_param = true;
217 break;
218 default:
219 panic();
221 break;
223 case D_unknown:
224 unknown:
225 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
226 "unrecognised directive [%s]", directive);
227 break;
229 case D_SEGMENT: /* [SEGMENT n] */
230 case D_SECTION:
232 int sb = globalbits;
233 int32_t seg = ofmt->section(value, pass2, &sb);
235 if (seg == NO_SEG) {
236 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
237 "segment name `%s' not recognized", value);
238 } else {
239 in_absolute = false;
240 location.segment = seg;
241 globalbits = sb;
243 break;
246 case D_SECTALIGN: /* [SECTALIGN n] */
248 expr *e;
250 if (*value) {
251 stdscan_reset();
252 stdscan_set(value);
253 tokval.t_type = TOKEN_INVALID;
254 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
255 if (e) {
256 uint64_t align = e->value;
258 if (!is_power2(e->value)) {
259 nasm_error(ERR_NONFATAL,
260 "segment alignment `%s' is not power of two",
261 value);
262 } else if (align > UINT64_C(0x7fffffff)) {
264 * FIXME: Please make some sane message here
265 * ofmt should have some 'check' method which
266 * would report segment alignment bounds.
268 nasm_error(ERR_NONFATAL,
269 "absurdly large segment alignment `%s' (2^%d)",
270 value, ilog2_64(align));
273 /* callee should be able to handle all details */
274 if (location.segment != NO_SEG)
275 ofmt->sectalign(location.segment, align);
278 break;
281 case D_EXTERN: /* [EXTERN label:special] */
282 if (*value == '$')
283 value++; /* skip initial $ if present */
284 if (pass0 == 2) {
285 q = value;
286 while (*q && *q != ':')
287 q++;
288 if (*q == ':') {
289 *q++ = '\0';
290 ofmt->symdef(value, 0L, 0L, 3, q);
292 } else if (passn == 1) {
293 bool validid = true;
294 q = value;
295 if (!isidstart(*q))
296 validid = false;
297 while (*q && *q != ':') {
298 if (!isidchar(*q))
299 validid = false;
300 q++;
302 if (!validid) {
303 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
304 break;
306 if (*q == ':') {
307 *q++ = '\0';
308 special = q;
309 } else
310 special = NULL;
311 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
312 int temp = pass0;
313 pass0 = 1; /* fake pass 1 in labels.c */
314 declare_as_global(value, special);
315 define_label(value, seg_alloc(), 0L, NULL,
316 false, true);
317 pass0 = temp;
319 } /* else pass0 == 1 */
320 break;
322 case D_BITS: /* [BITS bits] */
323 globalbits = get_bits(value);
324 break;
326 case D_GLOBAL: /* [GLOBAL symbol:special] */
327 if (*value == '$')
328 value++; /* skip initial $ if present */
329 if (pass0 == 2) { /* pass 2 */
330 q = value;
331 while (*q && *q != ':')
332 q++;
333 if (*q == ':') {
334 *q++ = '\0';
335 ofmt->symdef(value, 0L, 0L, 3, q);
337 } else if (pass2 == 1) { /* pass == 1 */
338 bool validid = true;
340 q = value;
341 if (!isidstart(*q))
342 validid = false;
343 while (*q && *q != ':') {
344 if (!isidchar(*q))
345 validid = false;
346 q++;
348 if (!validid) {
349 nasm_error(ERR_NONFATAL,
350 "identifier expected after GLOBAL");
351 break;
353 if (*q == ':') {
354 *q++ = '\0';
355 special = q;
356 } else
357 special = NULL;
358 declare_as_global(value, special);
359 } /* pass == 1 */
360 break;
362 case D_COMMON: /* [COMMON symbol size:special] */
364 int64_t size;
365 bool rn_error;
366 bool validid;
368 if (*value == '$')
369 value++; /* skip initial $ if present */
370 p = value;
371 validid = true;
372 if (!isidstart(*p))
373 validid = false;
374 while (*p && !nasm_isspace(*p)) {
375 if (!isidchar(*p))
376 validid = false;
377 p++;
379 if (!validid) {
380 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
381 break;
383 if (*p) {
384 p = nasm_zap_spaces_fwd(p);
385 q = p;
386 while (*q && *q != ':')
387 q++;
388 if (*q == ':') {
389 *q++ = '\0';
390 special = q;
391 } else {
392 special = NULL;
394 size = readnum(p, &rn_error);
395 if (rn_error) {
396 nasm_error(ERR_NONFATAL,
397 "invalid size specified"
398 " in COMMON declaration");
399 break;
401 } else {
402 nasm_error(ERR_NONFATAL,
403 "no size specified in"
404 " COMMON declaration");
405 break;
408 if (pass0 < 2) {
409 define_common(value, seg_alloc(), size, special);
410 } else if (pass0 == 2) {
411 if (special)
412 ofmt->symdef(value, 0L, 0L, 3, special);
414 break;
417 case D_ABSOLUTE: /* [ABSOLUTE address] */
419 expr *e;
421 stdscan_reset();
422 stdscan_set(value);
423 tokval.t_type = TOKEN_INVALID;
424 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
425 if (e) {
426 if (!is_reloc(e))
427 nasm_error(pass0 ==
428 1 ? ERR_NONFATAL : ERR_PANIC,
429 "cannot use non-relocatable expression as "
430 "ABSOLUTE address");
431 else {
432 absolute.segment = reloc_seg(e);
433 absolute.offset = reloc_value(e);
435 } else if (passn == 1)
436 absolute.offset = 0x100; /* don't go near zero in case of / */
437 else
438 nasm_panic(0, "invalid ABSOLUTE address "
439 "in pass two");
440 in_absolute = true;
441 location.segment = NO_SEG;
442 break;
445 case D_DEBUG: /* [DEBUG] */
447 bool badid, overlong;
448 char debugid[128];
450 p = value;
451 q = debugid;
452 badid = overlong = false;
453 if (!isidstart(*p)) {
454 badid = true;
455 } else {
456 while (*p && !nasm_isspace(*p)) {
457 if (q >= debugid + sizeof debugid - 1) {
458 overlong = true;
459 break;
461 if (!isidchar(*p))
462 badid = true;
463 *q++ = *p++;
465 *q = 0;
467 if (badid) {
468 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
469 "identifier expected after DEBUG");
470 break;
472 if (overlong) {
473 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
474 "DEBUG identifier too long");
475 break;
477 p = nasm_skip_spaces(p);
478 if (pass0 == 2)
479 dfmt->debug_directive(debugid, p);
480 break;
483 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
484 if (!set_warning_status(value)) {
485 nasm_error(ERR_WARNING|ERR_WARN_UNK_WARNING,
486 "unknown warning option: %s", value);
488 break;
490 case D_CPU: /* [CPU] */
491 cpu = get_cpu(value);
492 break;
494 case D_LIST: /* [LIST {+|-}] */
495 value = nasm_skip_spaces(value);
496 if (*value == '+') {
497 user_nolist = false;
498 } else {
499 if (*value == '-') {
500 user_nolist = true;
501 } else {
502 bad_param = true;
505 break;
507 case D_DEFAULT: /* [DEFAULT] */
508 stdscan_reset();
509 stdscan_set(value);
510 tokval.t_type = TOKEN_INVALID;
511 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
512 switch (tokval.t_integer) {
513 case S_REL:
514 globalrel = 1;
515 break;
516 case S_ABS:
517 globalrel = 0;
518 break;
519 case P_BND:
520 globalbnd = 1;
521 break;
522 case P_NOBND:
523 globalbnd = 0;
524 break;
525 default:
526 bad_param = true;
527 break;
529 } else {
530 bad_param = true;
532 break;
534 case D_FLOAT:
535 if (float_option(value)) {
536 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
537 "unknown 'float' directive: %s", value);
539 break;
541 case D_PRAGMA:
542 process_pragma(value);
543 break;
547 /* A common error message */
548 if (bad_param) {
549 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
550 directive);
553 return d != D_none;