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
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
62 /* Eventually a table of features */
65 static iflag_t
get_cpu(const char *value
)
68 const struct cpunames
*cpu
;
69 static const struct cpunames cpunames
[] = {
76 { "pentium", IF_PENT
},
77 { "pentiummmx", IF_PENT
},
81 { "pentiumpro", IF_P6
},
82 { "p2", IF_P6
}, /* +MMX */
83 { "pentiumii", IF_P6
},
85 { "katmai", IF_KATMAI
},
86 { "p4", IF_WILLAMETTE
},
87 { "willamette", IF_WILLAMETTE
},
88 { "prescott", IF_PRESCOTT
},
90 { "x86-64", IF_X86_64
},
93 { "itanium", IF_IA64
},
94 { "itanic", IF_IA64
},
95 { "merced", IF_IA64
},
97 { "default", IF_PLEVEL
},
99 { NULL
, IF_PLEVEL
} /* Error and final default entry */
104 for (cpu
= cpunames
; cpu
->name
; cpu
++) {
105 if (!nasm_stricmp(value
, cpu
->name
))
110 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
111 "unknown 'cpu' type '%s'", value
);
114 iflag_set_cpu(&r
, cpu
->level
);
118 static int get_bits(const char *value
)
124 break; /* Always safe */
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");
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");
140 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
141 "`%s' is not a valid segment size; must be 16, 32 or 64",
149 static enum directive
parse_directive_line(char **directive
, char **value
)
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.
162 q
= strchr(buf
, ']');
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
, ';');
176 if (p
< q
) /* ouch! somewhere inside */
181 /* no brace, no trailing spaces */
183 nasm_zap_spaces_rev(--q
);
186 p
= nasm_skip_spaces(++buf
);
187 q
= nasm_skip_word(p
);
189 return D_corrupt
; /* sigh... no value there */
193 /* and value finally */
194 p
= nasm_skip_spaces(++q
);
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
)
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
);
218 return D_none
; /* Not a directive */
221 nasm_error(ERR_NONFATAL
, "invalid directive line");
224 default: /* It's a backend-specific directive */
225 switch (ofmt
->directive(d
, value
, pass2
)) {
241 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
242 "unrecognised directive [%s]", directive
);
245 case D_SEGMENT
: /* [SEGMENT n] */
249 int32_t seg
= ofmt
->section(value
, pass2
, &sb
);
252 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
253 "segment name `%s' not recognized", value
);
261 case D_SECTALIGN
: /* [SECTALIGN n] */
268 tokval
.t_type
= TOKEN_INVALID
;
269 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
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",
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
);
296 case D_BITS
: /* [BITS bits] */
297 globalbits
= get_bits(value
);
300 case D_GLOBAL
: /* [GLOBAL|STATIC|EXTERN|COMMON symbol:special] */
321 value
++; /* skip initial $ if present */
324 if (!isidstart(*q
)) {
328 while (*q
&& *q
!= ':' && !nasm_isspace(*q
)) {
335 nasm_error(ERR_NONFATAL
,
336 "identifier expected after %s, got `%s'",
341 if (nasm_isspace(*q
)) {
343 sizestr
= q
= nasm_skip_spaces(q
);
349 if (q
&& *q
== ':') {
356 if (type
== LBL_COMMON
) {
358 size
= readnum(sizestr
, &rn_error
);
359 if (!sizestr
|| rn_error
)
360 nasm_error(ERR_NONFATAL
,
361 "%s size specified in common declaration",
362 sizestr
? "invalid" : "no");
363 } else if (sizestr
) {
364 nasm_error(ERR_NONFATAL
, "invalid syntax in %s declaration",
368 if (!declare_label(value
, type
, special
))
371 if (type
== LBL_COMMON
|| type
== LBL_EXTERN
)
372 define_label(value
, 0, size
, false);
377 case D_ABSOLUTE
: /* [ABSOLUTE address] */
383 tokval
.t_type
= TOKEN_INVALID
;
384 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
388 1 ? ERR_NONFATAL
: ERR_PANIC
,
389 "cannot use non-relocatable expression as "
392 absolute
.segment
= reloc_seg(e
);
393 absolute
.offset
= reloc_value(e
);
395 } else if (passn
== 1)
396 absolute
.offset
= 0x100; /* don't go near zero in case of / */
398 nasm_panic("invalid ABSOLUTE address "
401 location
.segment
= NO_SEG
;
402 location
.offset
= absolute
.offset
;
406 case D_DEBUG
: /* [DEBUG] */
408 bool badid
, overlong
;
413 badid
= overlong
= false;
414 if (!isidstart(*p
)) {
417 while (*p
&& !nasm_isspace(*p
)) {
418 if (q
>= debugid
+ sizeof debugid
- 1) {
429 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
430 "identifier expected after DEBUG");
434 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
435 "DEBUG identifier too long");
438 p
= nasm_skip_spaces(p
);
440 dfmt
->debug_directive(debugid
, p
);
444 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
445 if (!set_warning_status(value
)) {
446 nasm_error(ERR_WARNING
|ERR_WARN_UNK_WARNING
,
447 "unknown warning option: %s", value
);
451 case D_CPU
: /* [CPU] */
452 cpu
= get_cpu(value
);
455 case D_LIST
: /* [LIST {+|-}] */
456 value
= nasm_skip_spaces(value
);
468 case D_DEFAULT
: /* [DEFAULT] */
471 tokval
.t_type
= TOKEN_INVALID
;
472 if (stdscan(NULL
, &tokval
) != TOKEN_INVALID
) {
473 switch (tokval
.t_integer
) {
496 if (float_option(value
)) {
497 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
498 "unknown 'float' directive: %s", value
);
503 process_pragma(value
);
508 /* A common error message */
510 nasm_error(ERR_NONFATAL
, "invalid parameter to [%s] directive",