Makefile.in: fix typo in pathname
[nasm.git] / asm / directiv.c
blob937f17af0b76e8031d30aeeeff68674417aee976
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 "ilog2.h"
48 #include "error.h"
49 #include "float.h"
50 #include "stdscan.h"
51 #include "preproc.h"
52 #include "eval.h"
53 #include "assemble.h"
54 #include "outform.h"
55 #include "listing.h"
56 #include "labels.h"
57 #include "iflag.h"
59 static iflag_t get_cpu(char *value)
61 iflag_t r;
63 iflag_clear_all(&r);
65 if (!strcmp(value, "8086"))
66 iflag_set(&r, IF_8086);
67 else if (!strcmp(value, "186"))
68 iflag_set(&r, IF_186);
69 else if (!strcmp(value, "286"))
70 iflag_set(&r, IF_286);
71 else if (!strcmp(value, "386"))
72 iflag_set(&r, IF_386);
73 else if (!strcmp(value, "486"))
74 iflag_set(&r, IF_486);
75 else if (!strcmp(value, "586") ||
76 !nasm_stricmp(value, "pentium"))
77 iflag_set(&r, IF_PENT);
78 else if (!strcmp(value, "686") ||
79 !nasm_stricmp(value, "ppro") ||
80 !nasm_stricmp(value, "pentiumpro") ||
81 !nasm_stricmp(value, "p2"))
82 iflag_set(&r, IF_P6);
83 else if (!nasm_stricmp(value, "p3") ||
84 !nasm_stricmp(value, "katmai"))
85 iflag_set(&r, IF_KATMAI);
86 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
87 !nasm_stricmp(value, "willamette"))
88 iflag_set(&r, IF_WILLAMETTE);
89 else if (!nasm_stricmp(value, "prescott"))
90 iflag_set(&r, IF_PRESCOTT);
91 else if (!nasm_stricmp(value, "x64") ||
92 !nasm_stricmp(value, "x86-64"))
93 iflag_set(&r, IF_X86_64);
94 else if (!nasm_stricmp(value, "ia64") ||
95 !nasm_stricmp(value, "ia-64") ||
96 !nasm_stricmp(value, "itanium")||
97 !nasm_stricmp(value, "itanic") ||
98 !nasm_stricmp(value, "merced"))
99 iflag_set(&r, IF_IA64);
100 else {
101 iflag_set(&r, IF_PLEVEL);
102 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
103 "unknown 'cpu' type");
105 return r;
108 static int get_bits(char *value)
110 int i;
112 if ((i = atoi(value)) == 16)
113 return i; /* set for a 16-bit segment */
114 else if (i == 32) {
115 if (iflag_ffs(&cpu) < IF_386) {
116 nasm_error(ERR_NONFATAL,
117 "cannot specify 32-bit segment on processor below a 386");
118 i = 16;
120 } else if (i == 64) {
121 if (iflag_ffs(&cpu) < IF_X86_64) {
122 nasm_error(ERR_NONFATAL,
123 "cannot specify 64-bit segment on processor below an x86-64");
124 i = 16;
126 } else {
127 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
128 "`%s' is not a valid segment size; must be 16, 32 or 64",
129 value);
130 i = 16;
132 return i;
135 static enum directive parse_directive_line(char **directive, char **value)
137 char *p, *q, *buf;
139 buf = nasm_skip_spaces(*directive);
142 * It should be enclosed in [ ].
143 * XXX: we don't check there is nothing else on the remainder of the
144 * line, except a possible comment.
146 if (*buf != '[')
147 return D_none;
148 q = strchr(buf, ']');
149 if (!q)
150 return D_corrupt;
153 * Strip off the comments. XXX: this doesn't account for quoted
154 * strings inside a directive. We should really strip the
155 * comments in generic code, not here. While we're at it, it
156 * would be better to pass the backend a series of tokens instead
157 * of a raw string, and actually process quoted strings for it,
158 * like of like argv is handled in C.
160 p = strchr(buf, ';');
161 if (p) {
162 if (p < q) /* ouch! somewhere inside */
163 return D_corrupt;
164 *p = '\0';
167 /* no brace, no trailing spaces */
168 *q = '\0';
169 nasm_zap_spaces_rev(--q);
171 /* directive */
172 p = nasm_skip_spaces(++buf);
173 q = nasm_skip_word(p);
174 if (!q)
175 return D_corrupt; /* sigh... no value there */
176 *q = '\0';
177 *directive = p;
179 /* and value finally */
180 p = nasm_skip_spaces(++q);
181 *value = p;
183 return directive_find(*directive);
187 * Process a line from the assembler and try to handle it if it
188 * is a directive. Return true if the line was handled (including
189 * if it was an error), false otherwise.
191 bool process_directives(char *directive)
193 enum directive d;
194 char *value, *p, *q, *special;
195 struct tokenval tokval;
196 bool bad_param = false;
197 int pass2 = passn > 1 ? 2 : 1;
199 d = parse_directive_line(&directive, &value);
201 switch (d) {
202 case D_none:
203 return D_none; /* Not a directive */
205 case D_corrupt:
206 nasm_error(ERR_NONFATAL, "invalid directive line");
207 break;
209 default: /* It's a backend-specific directive */
210 switch (ofmt->directive(d, value, pass2)) {
211 case DIRR_UNKNOWN:
212 goto unknown;
213 case DIRR_OK:
214 case DIRR_ERROR:
215 break;
216 case DIRR_BADPARAM:
217 bad_param = true;
218 break;
219 default:
220 panic();
222 break;
224 case D_unknown:
225 unknown:
226 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
227 "unrecognised directive [%s]", directive);
228 break;
230 case D_SEGMENT: /* [SEGMENT n] */
231 case D_SECTION:
233 int sb = globalbits;
234 int32_t seg = ofmt->section(value, pass2, &sb);
236 if (seg == NO_SEG) {
237 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
238 "segment name `%s' not recognized", value);
239 } else {
240 in_absolute = false;
241 location.segment = seg;
242 globalbits = sb;
244 break;
247 case D_SECTALIGN: /* [SECTALIGN n] */
249 expr *e;
251 if (*value) {
252 stdscan_reset();
253 stdscan_set(value);
254 tokval.t_type = TOKEN_INVALID;
255 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
256 if (e) {
257 uint64_t align = e->value;
259 if (!is_power2(e->value)) {
260 nasm_error(ERR_NONFATAL,
261 "segment alignment `%s' is not power of two",
262 value);
263 } else if (align > UINT64_C(0x7fffffff)) {
265 * FIXME: Please make some sane message here
266 * ofmt should have some 'check' method which
267 * would report segment alignment bounds.
269 nasm_error(ERR_NONFATAL,
270 "absurdly large segment alignment `%s' (2^%d)",
271 value, ilog2_64(align));
274 /* callee should be able to handle all details */
275 if (location.segment != NO_SEG)
276 ofmt->sectalign(location.segment, align);
279 break;
282 case D_EXTERN: /* [EXTERN label:special] */
283 if (*value == '$')
284 value++; /* skip initial $ if present */
285 if (pass0 == 2) {
286 q = value;
287 while (*q && *q != ':')
288 q++;
289 if (*q == ':') {
290 *q++ = '\0';
291 ofmt->symdef(value, 0L, 0L, 3, q);
293 } else if (passn == 1) {
294 bool validid = true;
295 q = value;
296 if (!isidstart(*q))
297 validid = false;
298 while (*q && *q != ':') {
299 if (!isidchar(*q))
300 validid = false;
301 q++;
303 if (!validid) {
304 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
305 break;
307 if (*q == ':') {
308 *q++ = '\0';
309 special = q;
310 } else
311 special = NULL;
312 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
313 int temp = pass0;
314 pass0 = 1; /* fake pass 1 in labels.c */
315 declare_as_global(value, special);
316 define_label(value, seg_alloc(), 0L, NULL,
317 false, true);
318 pass0 = temp;
320 } /* else pass0 == 1 */
321 break;
323 case D_BITS: /* [BITS bits] */
324 globalbits = get_bits(value);
325 break;
327 case D_GLOBAL: /* [GLOBAL symbol:special] */
328 if (*value == '$')
329 value++; /* skip initial $ if present */
330 if (pass0 == 2) { /* pass 2 */
331 q = value;
332 while (*q && *q != ':')
333 q++;
334 if (*q == ':') {
335 *q++ = '\0';
336 ofmt->symdef(value, 0L, 0L, 3, q);
338 } else if (pass2 == 1) { /* pass == 1 */
339 bool validid = true;
341 q = value;
342 if (!isidstart(*q))
343 validid = false;
344 while (*q && *q != ':') {
345 if (!isidchar(*q))
346 validid = false;
347 q++;
349 if (!validid) {
350 nasm_error(ERR_NONFATAL,
351 "identifier expected after GLOBAL");
352 break;
354 if (*q == ':') {
355 *q++ = '\0';
356 special = q;
357 } else
358 special = NULL;
359 declare_as_global(value, special);
360 } /* pass == 1 */
361 break;
363 case D_COMMON: /* [COMMON symbol size:special] */
365 int64_t size;
366 bool rn_error;
367 bool validid;
369 if (*value == '$')
370 value++; /* skip initial $ if present */
371 p = value;
372 validid = true;
373 if (!isidstart(*p))
374 validid = false;
375 while (*p && !nasm_isspace(*p)) {
376 if (!isidchar(*p))
377 validid = false;
378 p++;
380 if (!validid) {
381 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
382 break;
384 if (*p) {
385 p = nasm_zap_spaces_fwd(p);
386 q = p;
387 while (*q && *q != ':')
388 q++;
389 if (*q == ':') {
390 *q++ = '\0';
391 special = q;
392 } else {
393 special = NULL;
395 size = readnum(p, &rn_error);
396 if (rn_error) {
397 nasm_error(ERR_NONFATAL,
398 "invalid size specified"
399 " in COMMON declaration");
400 break;
402 } else {
403 nasm_error(ERR_NONFATAL,
404 "no size specified in"
405 " COMMON declaration");
406 break;
409 if (pass0 < 2) {
410 define_common(value, seg_alloc(), size, special);
411 } else if (pass0 == 2) {
412 if (special)
413 ofmt->symdef(value, 0L, 0L, 3, special);
415 break;
418 case D_ABSOLUTE: /* [ABSOLUTE address] */
420 expr *e;
422 stdscan_reset();
423 stdscan_set(value);
424 tokval.t_type = TOKEN_INVALID;
425 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
426 if (e) {
427 if (!is_reloc(e))
428 nasm_error(pass0 ==
429 1 ? ERR_NONFATAL : ERR_PANIC,
430 "cannot use non-relocatable expression as "
431 "ABSOLUTE address");
432 else {
433 absolute.segment = reloc_seg(e);
434 absolute.offset = reloc_value(e);
436 } else if (passn == 1)
437 absolute.offset = 0x100; /* don't go near zero in case of / */
438 else
439 nasm_panic(0, "invalid ABSOLUTE address "
440 "in pass two");
441 in_absolute = true;
442 location.segment = NO_SEG;
443 break;
446 case D_DEBUG: /* [DEBUG] */
448 bool badid, overlong;
449 char debugid[128];
451 p = value;
452 q = debugid;
453 badid = overlong = false;
454 if (!isidstart(*p)) {
455 badid = true;
456 } else {
457 while (*p && !nasm_isspace(*p)) {
458 if (q >= debugid + sizeof debugid - 1) {
459 overlong = true;
460 break;
462 if (!isidchar(*p))
463 badid = true;
464 *q++ = *p++;
466 *q = 0;
468 if (badid) {
469 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
470 "identifier expected after DEBUG");
471 break;
473 if (overlong) {
474 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
475 "DEBUG identifier too long");
476 break;
478 p = nasm_skip_spaces(p);
479 if (pass0 == 2)
480 dfmt->debug_directive(debugid, p);
481 break;
484 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
485 if (!set_warning_status(value)) {
486 nasm_error(ERR_WARNING|ERR_WARN_UNK_WARNING,
487 "unknown warning option: %s", value);
489 break;
491 case D_CPU: /* [CPU] */
492 cpu = get_cpu(value);
493 break;
495 case D_LIST: /* [LIST {+|-}] */
496 value = nasm_skip_spaces(value);
497 if (*value == '+') {
498 user_nolist = false;
499 } else {
500 if (*value == '-') {
501 user_nolist = true;
502 } else {
503 bad_param = true;
506 break;
508 case D_DEFAULT: /* [DEFAULT] */
509 stdscan_reset();
510 stdscan_set(value);
511 tokval.t_type = TOKEN_INVALID;
512 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
513 switch (tokval.t_integer) {
514 case S_REL:
515 globalrel = 1;
516 break;
517 case S_ABS:
518 globalrel = 0;
519 break;
520 case P_BND:
521 globalbnd = 1;
522 break;
523 case P_NOBND:
524 globalbnd = 0;
525 break;
526 default:
527 bad_param = true;
528 break;
530 } else {
531 bad_param = true;
533 break;
535 case D_FLOAT:
536 if (float_option(value)) {
537 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
538 "unknown 'float' directive: %s", value);
540 break;
542 case D_PRAGMA:
543 process_pragma(value);
544 break;
548 /* A common error message */
549 if (bad_param) {
550 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
551 directive);
554 return d != D_none;