blob: cbfdd089d1a9df45402317cf5a68b93768e17cd8 [file] [log] [blame]
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001/* ----------------------------------------------------------------------- *
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 assembler directives
36 */
37
38#include "compiler.h"
39
40#include <stdlib.h>
41#include <string.h>
42#include <ctype.h>
43#include <limits.h>
44
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 "parser.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"
58
59static iflag_t get_cpu(char *value)
60{
61 iflag_t r;
62
63 iflag_clear_all(&r);
64
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");
104 }
105 return r;
106}
107
108static int get_bits(char *value)
109{
110 int i;
111
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;
119 }
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;
125 }
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;
131 }
132 return i;
133}
134
135static enum directives parse_directive_line(char **directive, char **value)
136{
137 char *p, *q, *buf;
138
139 buf = nasm_skip_spaces(*directive);
140
141 /*
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.
145 */
146 if (*buf != '[')
147 return D_none;
148 q = strchr(buf, ']');
149 if (!q)
150 return D_corrupt;
151
152 /*
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.
159 */
160 p = strchr(buf, ';');
161 if (p) {
162 if (p < q) /* ouch! somewhere inside */
163 return D_corrupt;
164 *p = '\0';
165 }
166
167 /* no brace, no trailing spaces */
168 *q = '\0';
169 nasm_zap_spaces_rev(--q);
170
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;
178
179 /* and value finally */
180 p = nasm_skip_spaces(++q);
181 *value = p;
182
183 return find_directive(*directive);
184}
185
186static void process_pragma(char *str)
187{
188 (void)str;
189}
190
191enum directives process_directives(char *directive)
192{
193 enum directives d;
194 char *value, *p, *q, *special;
195 struct tokenval tokval;
196 bool bad_param = false;
197 int pass2 = passn > 1 ? 2 : 1;
198
199 d = parse_directive_line(&directive, &value);
200
201 switch (d) {
202 case D_none:
203 return D_none; /* Not a directive */
204
205 case D_corrupt:
206 nasm_error(ERR_NONFATAL, "invalid directive line");
207 break;
208
209 default: /* It's a backend-specific directive */
210 if (ofmt->directive(d, value, pass2))
211 break;
212 /* else fall through */
213 case D_unknown:
214 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
215 "unrecognised directive [%s]", directive);
216 break;
217
218 case D_SEGMENT: /* [SEGMENT n] */
219 case D_SECTION:
220 {
221 int sb;
222 int32_t seg = ofmt->section(value, pass2, &sb);
223
224 if (seg == NO_SEG) {
225 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
226 "segment name `%s' not recognized", value);
227 } else {
228 in_absolute = false;
229 location.segment = seg;
230 }
231 break;
232 }
233
234 case D_SECTALIGN: /* [SECTALIGN n] */
235 {
236 expr *e;
237
238 if (*value) {
239 stdscan_reset();
240 stdscan_set(value);
241 tokval.t_type = TOKEN_INVALID;
242 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
243 if (e) {
244 uint64_t align = e->value;
245
246 if (!is_power2(e->value)) {
247 nasm_error(ERR_NONFATAL,
248 "segment alignment `%s' is not power of two",
249 value);
250 } else if (align > UINT64_C(0x7fffffff)) {
251 /*
252 * FIXME: Please make some sane message here
253 * ofmt should have some 'check' method which
254 * would report segment alignment bounds.
255 */
256 nasm_error(ERR_NONFATAL,
257 "absurdly large segment alignment `%s' (2^%d)",
258 value, ilog2_64(align));
259 }
260
261 /* callee should be able to handle all details */
262 if (location.segment != NO_SEG)
263 ofmt->sectalign(location.segment, align);
264 }
265 }
266 break;
267 }
268
269 case D_EXTERN: /* [EXTERN label:special] */
270 if (*value == '$')
271 value++; /* skip initial $ if present */
272 if (pass0 == 2) {
273 q = value;
274 while (*q && *q != ':')
275 q++;
276 if (*q == ':') {
277 *q++ = '\0';
278 ofmt->symdef(value, 0L, 0L, 3, q);
279 }
280 } else if (passn == 1) {
281 bool validid = true;
282 q = value;
283 if (!isidstart(*q))
284 validid = false;
285 while (*q && *q != ':') {
286 if (!isidchar(*q))
287 validid = false;
288 q++;
289 }
290 if (!validid) {
291 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
292 break;
293 }
294 if (*q == ':') {
295 *q++ = '\0';
296 special = q;
297 } else
298 special = NULL;
299 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
300 int temp = pass0;
301 pass0 = 1; /* fake pass 1 in labels.c */
302 declare_as_global(value, special);
303 define_label(value, seg_alloc(), 0L, NULL,
304 false, true);
305 pass0 = temp;
306 }
307 } /* else pass0 == 1 */
308 break;
309
310 case D_BITS: /* [BITS bits] */
311 globalbits = get_bits(value);
312 break;
313
314 case D_GLOBAL: /* [GLOBAL symbol:special] */
315 if (*value == '$')
316 value++; /* skip initial $ if present */
317 if (pass0 == 2) { /* pass 2 */
318 q = value;
319 while (*q && *q != ':')
320 q++;
321 if (*q == ':') {
322 *q++ = '\0';
323 ofmt->symdef(value, 0L, 0L, 3, q);
324 }
325 } else if (pass2 == 1) { /* pass == 1 */
326 bool validid = true;
327
328 q = value;
329 if (!isidstart(*q))
330 validid = false;
331 while (*q && *q != ':') {
332 if (!isidchar(*q))
333 validid = false;
334 q++;
335 }
336 if (!validid) {
337 nasm_error(ERR_NONFATAL,
338 "identifier expected after GLOBAL");
339 break;
340 }
341 if (*q == ':') {
342 *q++ = '\0';
343 special = q;
344 } else
345 special = NULL;
346 declare_as_global(value, special);
347 } /* pass == 1 */
348 break;
349
350 case D_COMMON: /* [COMMON symbol size:special] */
351 {
352 int64_t size;
353 bool rn_error;
354 bool validid;
355
356 if (*value == '$')
357 value++; /* skip initial $ if present */
358 p = value;
359 validid = true;
360 if (!isidstart(*p))
361 validid = false;
362 while (*p && !nasm_isspace(*p)) {
363 if (!isidchar(*p))
364 validid = false;
365 p++;
366 }
367 if (!validid) {
368 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
369 break;
370 }
371 if (*p) {
372 p = nasm_zap_spaces_fwd(p);
373 q = p;
374 while (*q && *q != ':')
375 q++;
376 if (*q == ':') {
377 *q++ = '\0';
378 special = q;
379 } else {
380 special = NULL;
381 }
382 size = readnum(p, &rn_error);
383 if (rn_error) {
384 nasm_error(ERR_NONFATAL,
385 "invalid size specified"
386 " in COMMON declaration");
387 break;
388 }
389 } else {
390 nasm_error(ERR_NONFATAL,
391 "no size specified in"
392 " COMMON declaration");
393 break;
394 }
395
396 if (pass0 < 2) {
397 define_common(value, seg_alloc(), size, special);
398 } else if (pass0 == 2) {
399 if (special)
400 ofmt->symdef(value, 0L, 0L, 3, special);
401 }
402 break;
403 }
404
405 case D_ABSOLUTE: /* [ABSOLUTE address] */
406 {
407 expr *e;
408
409 stdscan_reset();
410 stdscan_set(value);
411 tokval.t_type = TOKEN_INVALID;
412 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
413 if (e) {
414 if (!is_reloc(e))
415 nasm_error(pass0 ==
416 1 ? ERR_NONFATAL : ERR_PANIC,
417 "cannot use non-relocatable expression as "
418 "ABSOLUTE address");
419 else {
420 absolute.segment = reloc_seg(e);
421 absolute.offset = reloc_value(e);
422 }
423 } else if (passn == 1)
424 absolute.offset = 0x100; /* don't go near zero in case of / */
425 else
426 nasm_panic(0, "invalid ABSOLUTE address "
427 "in pass two");
428 in_absolute = true;
429 location.segment = NO_SEG;
430 break;
431 }
432
433 case D_DEBUG: /* [DEBUG] */
434 {
435 bool badid, overlong;
436 char debugid[128];
437
438 p = value;
439 q = debugid;
440 badid = overlong = false;
441 if (!isidstart(*p)) {
442 badid = true;
443 } else {
444 while (*p && !nasm_isspace(*p)) {
445 if (q >= debugid + sizeof debugid - 1) {
446 overlong = true;
447 break;
448 }
449 if (!isidchar(*p))
450 badid = true;
451 *q++ = *p++;
452 }
453 *q = 0;
454 }
455 if (badid) {
456 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
457 "identifier expected after DEBUG");
458 break;
459 }
460 if (overlong) {
461 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
462 "DEBUG identifier too long");
463 break;
464 }
465 p = nasm_skip_spaces(p);
466 if (pass0 == 2)
467 dfmt->debug_directive(debugid, p);
468 break;
469 }
470
471 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
472 {
473 enum warn_action { WID_OFF, WID_ON, WID_RESET };
474 enum warn_action action;
475 int i;
476
477 value = nasm_skip_spaces(value);
478 switch(*value) {
479 case '-': action = WID_OFF; value++; break;
480 case '+': action = WID_ON; value++; break;
481 case '*': action = WID_RESET; value++; break;
482 default: action = WID_ON; break;
483 }
484
485 for (i = 1; i <= ERR_WARN_MAX; i++)
486 if (!nasm_stricmp(value, warnings[i].name))
487 break;
488 if (i <= ERR_WARN_MAX) {
489 switch (action) {
490 case WID_OFF:
491 warning_on[i] = false;
492 break;
493 case WID_ON:
494 warning_on[i] = true;
495 break;
496 case WID_RESET:
497 warning_on[i] = warning_on_global[i];
498 break;
499 }
500 }
501 break;
502 }
503
504 case D_CPU: /* [CPU] */
505 cpu = get_cpu(value);
506 break;
507
508 case D_LIST: /* [LIST {+|-}] */
509 value = nasm_skip_spaces(value);
510 if (*value == '+') {
511 user_nolist = false;
512 } else {
513 if (*value == '-') {
514 user_nolist = true;
515 } else {
516 bad_param = true;
517 }
518 }
519 break;
520
521 case D_DEFAULT: /* [DEFAULT] */
522 stdscan_reset();
523 stdscan_set(value);
524 tokval.t_type = TOKEN_INVALID;
525 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
526 switch (tokval.t_integer) {
527 case S_REL:
528 globalrel = 1;
529 break;
530 case S_ABS:
531 globalrel = 0;
532 break;
533 case P_BND:
534 globalbnd = 1;
535 break;
536 case P_NOBND:
537 globalbnd = 0;
538 break;
539 default:
540 bad_param = true;
541 break;
542 }
543 } else {
544 bad_param = true;
545 }
546 break;
547
548 case D_FLOAT:
549 if (float_option(value)) {
550 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
551 "unknown 'float' directive: %s", value);
552 }
553 break;
554
555 case D_PRAGMA:
556 process_pragma(value);
557 break;
558 }
559
560
561 /* A common error message */
562 if (bad_param) {
563 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
564 directive);
565 }
566
567 return d;
568}