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
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
59 /* Eventually a table of features */
62 static iflag_t
get_cpu(const char *value
)
65 const struct cpunames
*cpu
;
66 static const struct cpunames cpunames
[] = {
73 { "pentium", IF_PENT
},
74 { "pentiummmx", IF_PENT
},
78 { "pentiumpro", IF_P6
},
79 { "p2", IF_P6
}, /* +MMX */
80 { "pentiumii", IF_P6
},
82 { "katmai", IF_KATMAI
},
83 { "p4", IF_WILLAMETTE
},
84 { "willamette", IF_WILLAMETTE
},
85 { "prescott", IF_PRESCOTT
},
87 { "x86-64", IF_X86_64
},
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
},
99 { "default", IF_PLEVEL
},
100 { NULL
, IF_PLEVEL
} /* Error and final default entry */
105 for (cpu
= cpunames
; cpu
->name
; cpu
++) {
106 if (!nasm_stricmp(value
, cpu
->name
))
111 nasm_nonfatal("unknown 'cpu' type '%s'", value
);
113 iflag_set_cpu(&r
, cpu
->level
);
117 static int get_bits(const char *value
)
123 break; /* Always safe */
125 if (!iflag_cpu_level_ok(&cpu
, IF_386
)) {
126 nasm_nonfatal("cannot specify 32-bit segment on processor below a 386");
131 if (!iflag_cpu_level_ok(&cpu
, IF_X86_64
)) {
132 nasm_nonfatal("cannot specify 64-bit segment on processor below an x86-64");
137 nasm_nonfatal("`%s' is not a valid segment size; must be 16, 32 or 64",
145 static enum directive
parse_directive_line(char **directive
, char **value
)
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.
158 q
= strchr(buf
, ']');
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
, ';');
172 if (p
< q
) /* ouch! somewhere inside */
177 /* no brace, no trailing spaces */
179 nasm_zap_spaces_rev(--q
);
182 p
= nasm_skip_spaces(++buf
);
183 q
= nasm_skip_word(p
);
185 return D_corrupt
; /* sigh... no value there */
189 /* and value finally */
190 p
= nasm_skip_spaces(++q
);
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
)
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
);
213 return D_none
; /* Not a directive */
216 nasm_nonfatal("invalid directive line");
219 default: /* It's a backend-specific directive */
220 switch (ofmt
->directive(d
, value
)) {
236 nasm_nonfatal("unrecognized directive [%s]", directive
);
239 case D_SEGMENT
: /* [SEGMENT n] */
243 int32_t seg
= ofmt
->section(value
, &sb
);
246 nasm_nonfatal("segment name `%s' not recognized", value
);
254 case D_SECTALIGN
: /* [SECTALIGN n] */
261 tokval
.t_type
= TOKEN_INVALID
;
262 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, true, NULL
);
264 uint64_t align
= e
->value
;
266 if (!is_power2(e
->value
)) {
267 nasm_nonfatal("segment alignment `%s' is not power of two",
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
);
287 case D_BITS
: /* [BITS bits] */
288 globalbits
= get_bits(value
);
291 case D_GLOBAL
: /* [GLOBAL|STATIC|EXTERN|COMMON symbol:special] */
315 value
++; /* skip initial $ if present */
318 if (!nasm_isidstart(*q
)) {
322 while (*q
&& *q
!= ':' && !nasm_isspace(*q
)) {
323 if (!nasm_isidchar(*q
))
329 nasm_nonfatal("identifier expected after %s, got `%s'",
334 if (nasm_isspace(*q
)) {
336 sizestr
= q
= nasm_skip_spaces(q
);
342 if (q
&& *q
== ':') {
349 if (type
== LBL_COMMON
) {
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
))
362 if (type
== LBL_COMMON
|| type
== LBL_EXTERN
|| type
== LBL_REQUIRED
)
363 define_label(value
, 0, size
, false);
368 case D_ABSOLUTE
: /* [ABSOLUTE address] */
374 tokval
.t_type
= TOKEN_INVALID
;
375 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, true, NULL
);
378 nasm_nonfatal("cannot use non-relocatable expression as "
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 / */
387 nasm_nonfatal("invalid ABSOLUTE address");
390 location
.segment
= NO_SEG
;
391 location
.offset
= absolute
.offset
;
395 case D_DEBUG
: /* [DEBUG] */
397 bool badid
, overlong
;
402 badid
= overlong
= false;
403 if (!nasm_isidstart(*p
)) {
406 while (*p
&& !nasm_isspace(*p
)) {
407 if (q
>= debugid
+ sizeof debugid
- 1) {
411 if (!nasm_isidchar(*p
))
418 nasm_nonfatal("identifier expected after DEBUG");
422 nasm_nonfatal("DEBUG identifier too long");
425 p
= nasm_skip_spaces(p
);
427 dfmt
->debug_directive(debugid
, p
);
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"))
436 else if (!nasm_stricmp(value
, "pop"))
439 set_warning_status(value
);
442 case D_CPU
: /* [CPU] */
443 cpu
= get_cpu(value
);
446 case D_LIST
: /* [LIST {+|-}] */
447 value
= nasm_skip_spaces(value
);
459 case D_DEFAULT
: /* [DEFAULT] */
462 tokval
.t_type
= TOKEN_INVALID
;
463 if (stdscan(NULL
, &tokval
) != TOKEN_INVALID
) {
464 switch (tokval
.t_integer
) {
487 if (float_option(value
)) {
488 nasm_nonfatal("unknown 'float' directive: %s", value
);
493 process_pragma(value
);
498 /* A common error message */
500 nasm_nonfatal("invalid parameter to [%s] directive", directive
);