travis: weirdpaste.i now has better line directives
[nasm.git] / asm / directiv.c
blob53422098368f40492dc1db374e0ab0cb9a7587a8
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2019 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 "nctype.h"
42 #include "nasm.h"
43 #include "nasmlib.h"
44 #include "ilog2.h"
45 #include "error.h"
46 #include "floats.h"
47 #include "stdscan.h"
48 #include "preproc.h"
49 #include "eval.h"
50 #include "assemble.h"
51 #include "outform.h"
52 #include "listing.h"
53 #include "labels.h"
54 #include "iflag.h"
56 struct cpunames {
57 const char *name;
58 unsigned int level;
59 /* Eventually a table of features */
62 static iflag_t get_cpu(const char *value)
64 iflag_t r;
65 const struct cpunames *cpu;
66 static const struct cpunames cpunames[] = {
67 { "8086", IF_8086 },
68 { "186", IF_186 },
69 { "286", IF_286 },
70 { "386", IF_386 },
71 { "486", IF_486 },
72 { "586", IF_PENT },
73 { "pentium", IF_PENT },
74 { "pentiummmx", IF_PENT },
75 { "686", IF_P6 },
76 { "p6", IF_P6 },
77 { "ppro", IF_P6 },
78 { "pentiumpro", IF_P6 },
79 { "p2", IF_P6 }, /* +MMX */
80 { "pentiumii", IF_P6 },
81 { "p3", IF_KATMAI },
82 { "katmai", IF_KATMAI },
83 { "p4", IF_WILLAMETTE },
84 { "willamette", IF_WILLAMETTE },
85 { "prescott", IF_PRESCOTT },
86 { "x64", IF_X86_64 },
87 { "x86-64", IF_X86_64 },
88 { "ia64", IF_IA64 },
89 { "ia-64", IF_IA64 },
90 { "itanium", IF_IA64 },
91 { "itanic", IF_IA64 },
92 { "merced", IF_IA64 },
93 { "nehalem", IF_NEHALEM },
94 { "westmere", IF_WESTMERE },
95 { "sandybridge", IF_SANDYBRIDGE },
96 { "ivybridge", IF_FUTURE },
97 { "any", IF_ANY },
98 { "all", IF_ANY },
99 { "default", IF_PLEVEL },
100 { NULL, IF_PLEVEL } /* Error and final default entry */
103 iflag_clear_all(&r);
105 for (cpu = cpunames; cpu->name; cpu++) {
106 if (!nasm_stricmp(value, cpu->name))
107 break;
110 if (!cpu->name)
111 nasm_nonfatal("unknown 'cpu' type '%s'", value);
113 iflag_set_cpu(&r, cpu->level);
114 return r;
117 static int get_bits(const char *value)
119 int i = atoi(value);
121 switch (i) {
122 case 16:
123 break; /* Always safe */
124 case 32:
125 if (!iflag_cpu_level_ok(&cpu, IF_386)) {
126 nasm_nonfatal("cannot specify 32-bit segment on processor below a 386");
127 i = 16;
129 break;
130 case 64:
131 if (!iflag_cpu_level_ok(&cpu, IF_X86_64)) {
132 nasm_nonfatal("cannot specify 64-bit segment on processor below an x86-64");
133 i = 16;
135 break;
136 default:
137 nasm_nonfatal("`%s' is not a valid segment size; must be 16, 32 or 64",
138 value);
139 i = 16;
140 break;
142 return i;
145 static enum directive parse_directive_line(char **directive, char **value)
147 char *p, *q, *buf;
149 buf = nasm_skip_spaces(*directive);
152 * It should be enclosed in [ ].
153 * XXX: we don't check there is nothing else on the remainder of the
154 * line, except a possible comment.
156 if (*buf != '[')
157 return D_none;
158 q = strchr(buf, ']');
159 if (!q)
160 return D_corrupt;
163 * Strip off the comments. XXX: this doesn't account for quoted
164 * strings inside a directive. We should really strip the
165 * comments in generic code, not here. While we're at it, it
166 * would be better to pass the backend a series of tokens instead
167 * of a raw string, and actually process quoted strings for it,
168 * like of like argv is handled in C.
170 p = strchr(buf, ';');
171 if (p) {
172 if (p < q) /* ouch! somewhere inside */
173 return D_corrupt;
174 *p = '\0';
177 /* no brace, no trailing spaces */
178 *q = '\0';
179 nasm_zap_spaces_rev(--q);
181 /* directive */
182 p = nasm_skip_spaces(++buf);
183 q = nasm_skip_word(p);
184 if (!q)
185 return D_corrupt; /* sigh... no value there */
186 *q = '\0';
187 *directive = p;
189 /* and value finally */
190 p = nasm_skip_spaces(++q);
191 *value = p;
193 return directive_find(*directive);
197 * Process a line from the assembler and try to handle it if it
198 * is a directive. Return true if the line was handled (including
199 * if it was an error), false otherwise.
201 bool process_directives(char *directive)
203 enum directive d;
204 char *value, *p, *q, *special;
205 struct tokenval tokval;
206 bool bad_param = false;
207 enum label_type type;
209 d = parse_directive_line(&directive, &value);
211 switch (d) {
212 case D_none:
213 return D_none; /* Not a directive */
215 case D_corrupt:
216 nasm_nonfatal("invalid directive line");
217 break;
219 default: /* It's a backend-specific directive */
220 switch (ofmt->directive(d, value)) {
221 case DIRR_UNKNOWN:
222 goto unknown;
223 case DIRR_OK:
224 case DIRR_ERROR:
225 break;
226 case DIRR_BADPARAM:
227 bad_param = true;
228 break;
229 default:
230 panic();
232 break;
234 case D_unknown:
235 unknown:
236 nasm_nonfatal("unrecognized directive [%s]", directive);
237 break;
239 case D_SEGMENT: /* [SEGMENT n] */
240 case D_SECTION:
242 int sb = globalbits;
243 int32_t seg = ofmt->section(value, &sb);
245 if (seg == NO_SEG) {
246 nasm_nonfatal("segment name `%s' not recognized", value);
247 } else {
248 globalbits = sb;
249 switch_segment(seg);
251 break;
254 case D_SECTALIGN: /* [SECTALIGN n] */
256 expr *e;
258 if (*value) {
259 stdscan_reset();
260 stdscan_set(value);
261 tokval.t_type = TOKEN_INVALID;
262 e = evaluate(stdscan, NULL, &tokval, NULL, true, NULL);
263 if (e) {
264 uint64_t align = e->value;
266 if (!is_power2(e->value)) {
267 nasm_nonfatal("segment alignment `%s' is not power of two",
268 value);
269 } else if (align > UINT64_C(0x7fffffff)) {
271 * FIXME: Please make some sane message here
272 * ofmt should have some 'check' method which
273 * would report segment alignment bounds.
275 nasm_nonfatal("absurdly large segment alignment `%s' (2^%d)",
276 value, ilog2_64(align));
279 /* callee should be able to handle all details */
280 if (location.segment != NO_SEG)
281 ofmt->sectalign(location.segment, align);
284 break;
287 case D_BITS: /* [BITS bits] */
288 globalbits = get_bits(value);
289 break;
291 case D_GLOBAL: /* [GLOBAL|STATIC|EXTERN|COMMON symbol:special] */
292 type = LBL_GLOBAL;
293 goto symdef;
294 case D_STATIC:
295 type = LBL_STATIC;
296 goto symdef;
297 case D_EXTERN:
298 type = LBL_EXTERN;
299 goto symdef;
300 case D_REQUIRED:
301 type = LBL_REQUIRED;
302 goto symdef;
303 case D_COMMON:
304 type = LBL_COMMON;
305 goto symdef;
307 symdef:
309 bool validid = true;
310 int64_t size = 0;
311 char *sizestr;
312 bool rn_error;
314 if (*value == '$')
315 value++; /* skip initial $ if present */
317 q = value;
318 if (!nasm_isidstart(*q)) {
319 validid = false;
320 } else {
321 q++;
322 while (*q && *q != ':' && !nasm_isspace(*q)) {
323 if (!nasm_isidchar(*q))
324 validid = false;
325 q++;
328 if (!validid) {
329 nasm_nonfatal("identifier expected after %s, got `%s'",
330 directive, value);
331 break;
334 if (nasm_isspace(*q)) {
335 *q++ = '\0';
336 sizestr = q = nasm_skip_spaces(q);
337 q = strchr(q, ':');
338 } else {
339 sizestr = NULL;
342 if (q && *q == ':') {
343 *q++ = '\0';
344 special = q;
345 } else {
346 special = NULL;
349 if (type == LBL_COMMON) {
350 if (sizestr)
351 size = readnum(sizestr, &rn_error);
352 if (!sizestr || rn_error)
353 nasm_nonfatal("%s size specified in common declaration",
354 sizestr ? "invalid" : "no");
355 } else if (sizestr) {
356 nasm_nonfatal("invalid syntax in %s declaration", directive);
359 if (!declare_label(value, type, special))
360 break;
362 if (type == LBL_COMMON || type == LBL_EXTERN || type == LBL_REQUIRED)
363 define_label(value, 0, size, false);
365 break;
368 case D_ABSOLUTE: /* [ABSOLUTE address] */
370 expr *e;
372 stdscan_reset();
373 stdscan_set(value);
374 tokval.t_type = TOKEN_INVALID;
375 e = evaluate(stdscan, NULL, &tokval, NULL, true, NULL);
376 if (e) {
377 if (!is_reloc(e)) {
378 nasm_nonfatal("cannot use non-relocatable expression as "
379 "ABSOLUTE address");
380 } else {
381 absolute.segment = reloc_seg(e);
382 absolute.offset = reloc_value(e);
384 } else if (pass_first()) {
385 absolute.offset = 0x100; /* don't go near zero in case of / */
386 } else {
387 nasm_nonfatal("invalid ABSOLUTE address");
389 in_absolute = true;
390 location.segment = NO_SEG;
391 location.offset = absolute.offset;
392 break;
395 case D_DEBUG: /* [DEBUG] */
397 bool badid, overlong;
398 char debugid[128];
400 p = value;
401 q = debugid;
402 badid = overlong = false;
403 if (!nasm_isidstart(*p)) {
404 badid = true;
405 } else {
406 while (*p && !nasm_isspace(*p)) {
407 if (q >= debugid + sizeof debugid - 1) {
408 overlong = true;
409 break;
411 if (!nasm_isidchar(*p))
412 badid = true;
413 *q++ = *p++;
415 *q = 0;
417 if (badid) {
418 nasm_nonfatal("identifier expected after DEBUG");
419 break;
421 if (overlong) {
422 nasm_nonfatal("DEBUG identifier too long");
423 break;
425 p = nasm_skip_spaces(p);
426 if (pass_final())
427 dfmt->debug_directive(debugid, p);
428 break;
431 case D_WARNING: /* [WARNING {push|pop|{+|-|*}warn-name}] */
432 value = nasm_skip_spaces(value);
433 if ((*value | 0x20) == 'p') {
434 if (!nasm_stricmp(value, "push"))
435 push_warnings();
436 else if (!nasm_stricmp(value, "pop"))
437 pop_warnings();
439 set_warning_status(value);
440 break;
442 case D_CPU: /* [CPU] */
443 cpu = get_cpu(value);
444 break;
446 case D_LIST: /* [LIST {+|-}] */
447 value = nasm_skip_spaces(value);
448 if (*value == '+') {
449 user_nolist = false;
450 } else {
451 if (*value == '-') {
452 user_nolist = true;
453 } else {
454 bad_param = true;
457 break;
459 case D_DEFAULT: /* [DEFAULT] */
460 stdscan_reset();
461 stdscan_set(value);
462 tokval.t_type = TOKEN_INVALID;
463 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
464 switch (tokval.t_integer) {
465 case S_REL:
466 globalrel = 1;
467 break;
468 case S_ABS:
469 globalrel = 0;
470 break;
471 case P_BND:
472 globalbnd = 1;
473 break;
474 case P_NOBND:
475 globalbnd = 0;
476 break;
477 default:
478 bad_param = true;
479 break;
481 } else {
482 bad_param = true;
484 break;
486 case D_FLOAT:
487 if (float_option(value)) {
488 nasm_nonfatal("unknown 'float' directive: %s", value);
490 break;
492 case D_PRAGMA:
493 process_pragma(value);
494 break;
498 /* A common error message */
499 if (bad_param) {
500 nasm_nonfatal("invalid parameter to [%s] directive", directive);
503 return d != D_none;