Cleanup of label renaming infrastructure, add subsection support
[nasm.git] / asm / directiv.c
blobb7c919d23778f80bf2f4dfb45560f278778a3e14
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 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 struct cpunames {
60 const char *name;
61 unsigned int level;
62 /* Eventually a table of features */
65 static iflag_t get_cpu(const char *value)
67 iflag_t r;
68 const struct cpunames *cpu;
69 static const struct cpunames cpunames[] = {
70 { "8086", IF_8086 },
71 { "186", IF_186 },
72 { "286", IF_286 },
73 { "386", IF_386 },
74 { "486", IF_486 },
75 { "586", IF_PENT },
76 { "pentium", IF_PENT },
77 { "pentiummmx", IF_PENT },
78 { "686", IF_P6 },
79 { "p6", IF_P6 },
80 { "ppro", IF_P6 },
81 { "pentiumpro", IF_P6 },
82 { "p2", IF_P6 }, /* +MMX */
83 { "pentiumii", IF_P6 },
84 { "p3", IF_KATMAI },
85 { "katmai", IF_KATMAI },
86 { "p4", IF_WILLAMETTE },
87 { "willamette", IF_WILLAMETTE },
88 { "prescott", IF_PRESCOTT },
89 { "x64", IF_X86_64 },
90 { "x86-64", IF_X86_64 },
91 { "ia64", IF_IA64 },
92 { "ia-64", IF_IA64 },
93 { "itanium", IF_IA64 },
94 { "itanic", IF_IA64 },
95 { "merced", IF_IA64 },
96 { "any", IF_PLEVEL },
97 { "default", IF_PLEVEL },
98 { "all", IF_PLEVEL },
99 { NULL, IF_PLEVEL } /* Error and final default entry */
102 iflag_clear_all(&r);
104 for (cpu = cpunames; cpu->name; cpu++) {
105 if (!strcmp(value, cpu->name))
106 break;
109 if (!cpu->name) {
110 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
111 "unknown 'cpu' type '%s'", value);
114 iflag_set_cpu(&r, cpu->level);
115 return r;
118 static int get_bits(const char *value)
120 int i = atoi(value);
122 switch (i) {
123 case 16:
124 break; /* Always safe */
125 case 32:
126 if (!iflag_cpu_level_ok(&cpu, IF_386)) {
127 nasm_error(ERR_NONFATAL,
128 "cannot specify 32-bit segment on processor below a 386");
129 i = 16;
131 break;
132 case 64:
133 if (!iflag_cpu_level_ok(&cpu, IF_X86_64)) {
134 nasm_error(ERR_NONFATAL,
135 "cannot specify 64-bit segment on processor below an x86-64");
136 i = 16;
138 break;
139 default:
140 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
141 "`%s' is not a valid segment size; must be 16, 32 or 64",
142 value);
143 i = 16;
144 break;
146 return i;
149 static enum directive parse_directive_line(char **directive, char **value)
151 char *p, *q, *buf;
153 buf = nasm_skip_spaces(*directive);
156 * It should be enclosed in [ ].
157 * XXX: we don't check there is nothing else on the remainder of the
158 * line, except a possible comment.
160 if (*buf != '[')
161 return D_none;
162 q = strchr(buf, ']');
163 if (!q)
164 return D_corrupt;
167 * Strip off the comments. XXX: this doesn't account for quoted
168 * strings inside a directive. We should really strip the
169 * comments in generic code, not here. While we're at it, it
170 * would be better to pass the backend a series of tokens instead
171 * of a raw string, and actually process quoted strings for it,
172 * like of like argv is handled in C.
174 p = strchr(buf, ';');
175 if (p) {
176 if (p < q) /* ouch! somewhere inside */
177 return D_corrupt;
178 *p = '\0';
181 /* no brace, no trailing spaces */
182 *q = '\0';
183 nasm_zap_spaces_rev(--q);
185 /* directive */
186 p = nasm_skip_spaces(++buf);
187 q = nasm_skip_word(p);
188 if (!q)
189 return D_corrupt; /* sigh... no value there */
190 *q = '\0';
191 *directive = p;
193 /* and value finally */
194 p = nasm_skip_spaces(++q);
195 *value = p;
197 return directive_find(*directive);
201 * Process a line from the assembler and try to handle it if it
202 * is a directive. Return true if the line was handled (including
203 * if it was an error), false otherwise.
205 bool process_directives(char *directive)
207 enum directive d;
208 char *value, *p, *q, *special;
209 struct tokenval tokval;
210 bool bad_param = false;
211 int pass2 = passn > 1 ? 2 : 1;
212 enum label_type type;
214 d = parse_directive_line(&directive, &value);
216 switch (d) {
217 case D_none:
218 return D_none; /* Not a directive */
220 case D_corrupt:
221 nasm_error(ERR_NONFATAL, "invalid directive line");
222 break;
224 default: /* It's a backend-specific directive */
225 switch (ofmt->directive(d, value, pass2)) {
226 case DIRR_UNKNOWN:
227 goto unknown;
228 case DIRR_OK:
229 case DIRR_ERROR:
230 break;
231 case DIRR_BADPARAM:
232 bad_param = true;
233 break;
234 default:
235 panic();
237 break;
239 case D_unknown:
240 unknown:
241 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
242 "unrecognised directive [%s]", directive);
243 break;
245 case D_SEGMENT: /* [SEGMENT n] */
246 case D_SECTION:
248 int sb = globalbits;
249 int32_t seg = ofmt->section(value, pass2, &sb);
251 if (seg == NO_SEG) {
252 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
253 "segment name `%s' not recognized", value);
254 } else {
255 globalbits = sb;
256 switch_segment(seg);
258 break;
261 case D_SECTALIGN: /* [SECTALIGN n] */
263 expr *e;
265 if (*value) {
266 stdscan_reset();
267 stdscan_set(value);
268 tokval.t_type = TOKEN_INVALID;
269 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
270 if (e) {
271 uint64_t align = e->value;
273 if (!is_power2(e->value)) {
274 nasm_error(ERR_NONFATAL,
275 "segment alignment `%s' is not power of two",
276 value);
277 } else if (align > UINT64_C(0x7fffffff)) {
279 * FIXME: Please make some sane message here
280 * ofmt should have some 'check' method which
281 * would report segment alignment bounds.
283 nasm_error(ERR_NONFATAL,
284 "absurdly large segment alignment `%s' (2^%d)",
285 value, ilog2_64(align));
288 /* callee should be able to handle all details */
289 if (location.segment != NO_SEG)
290 ofmt->sectalign(location.segment, align);
293 break;
296 case D_BITS: /* [BITS bits] */
297 globalbits = get_bits(value);
298 break;
300 case D_GLOBAL: /* [GLOBAL|STATIC|EXTERN|COMMON symbol:special] */
301 type = LBL_GLOBAL;
302 goto symdef;
303 case D_STATIC:
304 type = LBL_STATIC;
305 goto symdef;
306 case D_EXTERN:
307 type = LBL_EXTERN;
308 goto symdef;
309 case D_COMMON:
310 type = LBL_COMMON;
311 goto symdef;
313 symdef:
315 bool validid = true;
316 int64_t size = 0;
317 char *sizestr;
318 bool rn_error;
320 q = value;
321 if (!isidstart(*q))
322 validid = false;
323 while (*q && *q != ':' && !nasm_isspace(*q)) {
324 if (!isidchar(*q))
325 validid = false;
326 q++;
328 if (!validid) {
329 nasm_error(ERR_NONFATAL,
330 "identifier expected after %s", directive);
331 break;
334 if (nasm_isspace(*q)) {
335 sizestr = q = nasm_zap_spaces_fwd(q);
336 q = strchr(q, ':');
337 } else {
338 sizestr = NULL;
341 if (*q == ':') {
342 *q++ = '\0';
343 special = q;
344 } else {
345 special = NULL;
348 if (type == LBL_COMMON) {
349 if (sizestr)
350 size = readnum(sizestr, &rn_error);
351 if (!sizestr || rn_error)
352 nasm_error(ERR_NONFATAL,
353 "%s size specified in common declaration",
354 sizestr ? "invalid" : "no");
355 } else if (sizestr) {
356 nasm_error(ERR_NONFATAL, "invalid syntax in %s declaration",
357 directive);
360 if (*value == '$')
361 value++; /* skip initial $ if present */
363 if (!declare_label(value, type, special))
364 break;
366 if (type == LBL_COMMON || type == LBL_EXTERN)
367 define_label(value, 0, size, false);
369 break;
372 case D_ABSOLUTE: /* [ABSOLUTE address] */
374 expr *e;
376 stdscan_reset();
377 stdscan_set(value);
378 tokval.t_type = TOKEN_INVALID;
379 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
380 if (e) {
381 if (!is_reloc(e))
382 nasm_error(pass0 ==
383 1 ? ERR_NONFATAL : ERR_PANIC,
384 "cannot use non-relocatable expression as "
385 "ABSOLUTE address");
386 else {
387 absolute.segment = reloc_seg(e);
388 absolute.offset = reloc_value(e);
390 } else if (passn == 1)
391 absolute.offset = 0x100; /* don't go near zero in case of / */
392 else
393 nasm_panic(0, "invalid ABSOLUTE address "
394 "in pass two");
395 in_absolute = true;
396 location.segment = NO_SEG;
397 break;
400 case D_DEBUG: /* [DEBUG] */
402 bool badid, overlong;
403 char debugid[128];
405 p = value;
406 q = debugid;
407 badid = overlong = false;
408 if (!isidstart(*p)) {
409 badid = true;
410 } else {
411 while (*p && !nasm_isspace(*p)) {
412 if (q >= debugid + sizeof debugid - 1) {
413 overlong = true;
414 break;
416 if (!isidchar(*p))
417 badid = true;
418 *q++ = *p++;
420 *q = 0;
422 if (badid) {
423 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
424 "identifier expected after DEBUG");
425 break;
427 if (overlong) {
428 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
429 "DEBUG identifier too long");
430 break;
432 p = nasm_skip_spaces(p);
433 if (pass0 == 2)
434 dfmt->debug_directive(debugid, p);
435 break;
438 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
439 if (!set_warning_status(value)) {
440 nasm_error(ERR_WARNING|ERR_WARN_UNK_WARNING,
441 "unknown warning option: %s", value);
443 break;
445 case D_CPU: /* [CPU] */
446 cpu = get_cpu(value);
447 break;
449 case D_LIST: /* [LIST {+|-}] */
450 value = nasm_skip_spaces(value);
451 if (*value == '+') {
452 user_nolist = false;
453 } else {
454 if (*value == '-') {
455 user_nolist = true;
456 } else {
457 bad_param = true;
460 break;
462 case D_DEFAULT: /* [DEFAULT] */
463 stdscan_reset();
464 stdscan_set(value);
465 tokval.t_type = TOKEN_INVALID;
466 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
467 switch (tokval.t_integer) {
468 case S_REL:
469 globalrel = 1;
470 break;
471 case S_ABS:
472 globalrel = 0;
473 break;
474 case P_BND:
475 globalbnd = 1;
476 break;
477 case P_NOBND:
478 globalbnd = 0;
479 break;
480 default:
481 bad_param = true;
482 break;
484 } else {
485 bad_param = true;
487 break;
489 case D_FLOAT:
490 if (float_option(value)) {
491 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
492 "unknown 'float' directive: %s", value);
494 break;
496 case D_PRAGMA:
497 process_pragma(value);
498 break;
502 /* A common error message */
503 if (bad_param) {
504 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
505 directive);
508 return d != D_none;