Extended test file.
[nasm/externdefs2.git] / asm / directiv.c
blob20f03e18c1f69309dff813b26e47d28830c09624
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 /* used in the externdef case. Moved here because of ISO C90. */
200 int32_t lblsegment;
201 int64_t lbloffset;
203 d = parse_directive_line(&directive, &value);
205 switch (d) {
206 case D_none:
207 return D_none; /* Not a directive */
209 case D_corrupt:
210 nasm_error(ERR_NONFATAL, "invalid directive line");
211 break;
213 default: /* It's a backend-specific directive */
214 switch (ofmt->directive(d, value, pass2)) {
215 case DIRR_UNKNOWN:
216 goto unknown;
217 case DIRR_OK:
218 case DIRR_ERROR:
219 break;
220 case DIRR_BADPARAM:
221 bad_param = true;
222 break;
223 default:
224 panic();
226 break;
228 case D_unknown:
229 unknown:
230 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
231 "unrecognised directive [%s]", directive);
232 break;
234 case D_SEGMENT: /* [SEGMENT n] */
235 case D_SECTION:
237 int sb;
238 int32_t seg = ofmt->section(value, pass2, &sb);
240 if (seg == NO_SEG) {
241 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
242 "segment name `%s' not recognized", value);
243 } else {
244 in_absolute = false;
245 location.segment = seg;
247 break;
250 case D_EXTERNDEF:
251 if(passn == 2) { /* wait for local symbols to be declared */
252 if (*value == '$')
253 value++; /* skip initial $ if present */
254 q = value;
255 while (*q && *q != ':')
256 q++;
257 if (*q == ':')
258 *q++ = '\0';
259 nasm_error(ERR_DEBUG, "Extern definition of symbol `%s'", value);
263 if(lookup_label(value, &lblsegment, &lbloffset)) {
264 nasm_error(ERR_DEBUG, "Label locally declared, set label to global.");
265 declare_as_global(value, 0);
266 } else {
267 nasm_error(ERR_DEBUG, "Label was not found locally, introduce as extern!");
268 goto externdef_behave_extern;
271 break;
273 case D_SECTALIGN: /* [SECTALIGN n] */
275 expr *e;
277 if (*value) {
278 stdscan_reset();
279 stdscan_set(value);
280 tokval.t_type = TOKEN_INVALID;
281 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
282 if (e) {
283 uint64_t align = e->value;
285 if (!is_power2(e->value)) {
286 nasm_error(ERR_NONFATAL,
287 "segment alignment `%s' is not power of two",
288 value);
289 } else if (align > UINT64_C(0x7fffffff)) {
291 * FIXME: Please make some sane message here
292 * ofmt should have some 'check' method which
293 * would report segment alignment bounds.
295 nasm_error(ERR_NONFATAL,
296 "absurdly large segment alignment `%s' (2^%d)",
297 value, ilog2_64(align));
300 /* callee should be able to handle all details */
301 if (location.segment != NO_SEG)
302 ofmt->sectalign(location.segment, align);
305 break;
308 externdef_behave_extern: /* goto mark for externdef */
309 case D_EXTERN: /* [EXTERN label:special] */
310 if (*value == '$')
311 value++; /* skip initial $ if present */
312 if (pass0 == 2) {
313 q = value;
314 while (*q && *q != ':')
315 q++;
316 if (*q == ':') {
317 *q++ = '\0';
318 ofmt->symdef(value, 0L, 0L, 3, q);
320 } else if (passn == 2) { /* changed value from 1 to 2, hope this will cause no trouble */
321 bool validid = true;
322 q = value;
323 if (!isidstart(*q))
324 validid = false;
325 while (*q && *q != ':') {
326 if (!isidchar(*q))
327 validid = false;
328 q++;
330 if (!validid) {
331 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
332 break;
334 if (*q == ':') {
335 *q++ = '\0';
336 special = q;
337 } else
338 special = NULL;
339 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
340 int temp = pass0;
341 pass0 = 1; /* fake pass 1 in labels.c */
342 declare_as_global(value, special);
343 define_label(value, seg_alloc(), 0L, NULL,
344 false, true);
345 pass0 = temp;
347 } /* else pass0 == 1 */
348 break;
350 case D_BITS: /* [BITS bits] */
351 globalbits = get_bits(value);
352 break;
354 case D_GLOBAL: /* [GLOBAL symbol:special] */
355 if (*value == '$')
356 value++; /* skip initial $ if present */
357 if (pass0 == 2) { /* pass 2 */
358 q = value;
359 while (*q && *q != ':')
360 q++;
361 if (*q == ':') {
362 *q++ = '\0';
363 ofmt->symdef(value, 0L, 0L, 3, q);
365 } else if (pass2 == 1) { /* pass == 1 */
366 bool validid = true;
368 q = value;
369 if (!isidstart(*q))
370 validid = false;
371 while (*q && *q != ':') {
372 if (!isidchar(*q))
373 validid = false;
374 q++;
376 if (!validid) {
377 nasm_error(ERR_NONFATAL,
378 "identifier expected after GLOBAL");
379 break;
381 if (*q == ':') {
382 *q++ = '\0';
383 special = q;
384 } else
385 special = NULL;
386 declare_as_global(value, special);
387 } /* pass == 1 */
388 break;
390 case D_COMMON: /* [COMMON symbol size:special] */
392 int64_t size;
393 bool rn_error;
394 bool validid;
396 if (*value == '$')
397 value++; /* skip initial $ if present */
398 p = value;
399 validid = true;
400 if (!isidstart(*p))
401 validid = false;
402 while (*p && !nasm_isspace(*p)) {
403 if (!isidchar(*p))
404 validid = false;
405 p++;
407 if (!validid) {
408 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
409 break;
411 if (*p) {
412 p = nasm_zap_spaces_fwd(p);
413 q = p;
414 while (*q && *q != ':')
415 q++;
416 if (*q == ':') {
417 *q++ = '\0';
418 special = q;
419 } else {
420 special = NULL;
422 size = readnum(p, &rn_error);
423 if (rn_error) {
424 nasm_error(ERR_NONFATAL,
425 "invalid size specified"
426 " in COMMON declaration");
427 break;
429 } else {
430 nasm_error(ERR_NONFATAL,
431 "no size specified in"
432 " COMMON declaration");
433 break;
436 if (pass0 < 2) {
437 define_common(value, seg_alloc(), size, special);
438 } else if (pass0 == 2) {
439 if (special)
440 ofmt->symdef(value, 0L, 0L, 3, special);
442 break;
445 case D_ABSOLUTE: /* [ABSOLUTE address] */
447 expr *e;
449 stdscan_reset();
450 stdscan_set(value);
451 tokval.t_type = TOKEN_INVALID;
452 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
453 if (e) {
454 if (!is_reloc(e))
455 nasm_error(pass0 ==
456 1 ? ERR_NONFATAL : ERR_PANIC,
457 "cannot use non-relocatable expression as "
458 "ABSOLUTE address");
459 else {
460 absolute.segment = reloc_seg(e);
461 absolute.offset = reloc_value(e);
463 } else if (passn == 1)
464 absolute.offset = 0x100; /* don't go near zero in case of / */
465 else
466 nasm_panic(0, "invalid ABSOLUTE address "
467 "in pass two");
468 in_absolute = true;
469 location.segment = NO_SEG;
470 break;
473 case D_DEBUG: /* [DEBUG] */
475 bool badid, overlong;
476 char debugid[128];
478 p = value;
479 q = debugid;
480 badid = overlong = false;
481 if (!isidstart(*p)) {
482 badid = true;
483 } else {
484 while (*p && !nasm_isspace(*p)) {
485 if (q >= debugid + sizeof debugid - 1) {
486 overlong = true;
487 break;
489 if (!isidchar(*p))
490 badid = true;
491 *q++ = *p++;
493 *q = 0;
495 if (badid) {
496 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
497 "identifier expected after DEBUG");
498 break;
500 if (overlong) {
501 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
502 "DEBUG identifier too long");
503 break;
505 p = nasm_skip_spaces(p);
506 if (pass0 == 2)
507 dfmt->debug_directive(debugid, p);
508 break;
511 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
512 if (!set_warning_status(value)) {
513 nasm_error(ERR_WARNING|ERR_WARN_UNK_WARNING,
514 "unknown warning option: %s", value);
516 break;
518 case D_CPU: /* [CPU] */
519 cpu = get_cpu(value);
520 break;
522 case D_LIST: /* [LIST {+|-}] */
523 value = nasm_skip_spaces(value);
524 if (*value == '+') {
525 user_nolist = false;
526 } else {
527 if (*value == '-') {
528 user_nolist = true;
529 } else {
530 bad_param = true;
533 break;
535 case D_DEFAULT: /* [DEFAULT] */
536 stdscan_reset();
537 stdscan_set(value);
538 tokval.t_type = TOKEN_INVALID;
539 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
540 switch (tokval.t_integer) {
541 case S_REL:
542 globalrel = 1;
543 break;
544 case S_ABS:
545 globalrel = 0;
546 break;
547 case P_BND:
548 globalbnd = 1;
549 break;
550 case P_NOBND:
551 globalbnd = 0;
552 break;
553 default:
554 bad_param = true;
555 break;
557 } else {
558 bad_param = true;
560 break;
562 case D_FLOAT:
563 if (float_option(value)) {
564 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
565 "unknown 'float' directive: %s", value);
567 break;
569 case D_PRAGMA:
570 process_pragma(value);
571 break;
575 /* A common error message */
576 if (bad_param) {
577 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
578 directive);
581 return d != D_none;