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 (!strcmp(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;
213 d
= parse_directive_line(&directive
, &value
);
217 return D_none
; /* Not a directive */
220 nasm_error(ERR_NONFATAL
, "invalid directive line");
223 default: /* It's a backend-specific directive */
224 switch (ofmt
->directive(d
, value
, pass2
)) {
240 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
241 "unrecognised directive [%s]", directive
);
244 case D_SEGMENT
: /* [SEGMENT n] */
248 int32_t seg
= ofmt
->section(value
, pass2
, &sb
);
251 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
252 "segment name `%s' not recognized", value
);
255 location
.segment
= seg
;
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_EXTERN
: /* [EXTERN label:special] */
298 value
++; /* skip initial $ if present */
301 while (*q
&& *q
!= ':')
305 ofmt
->symdef(value
, 0L, 0L, 3, q
);
307 } else if (passn
== 1) {
312 while (*q
&& *q
!= ':') {
318 nasm_error(ERR_NONFATAL
, "identifier expected after EXTERN");
326 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
328 pass0
= 1; /* fake pass 1 in labels.c */
329 declare_as_global(value
, special
);
330 define_label(value
, seg_alloc(), 0L, NULL
,
334 } /* else pass0 == 1 */
337 case D_BITS
: /* [BITS bits] */
338 globalbits
= get_bits(value
);
341 case D_GLOBAL
: /* [GLOBAL symbol:special] */
343 value
++; /* skip initial $ if present */
344 if (pass0
== 2) { /* pass 2 */
346 while (*q
&& *q
!= ':')
350 ofmt
->symdef(value
, 0L, 0L, 3, q
);
352 } else if (pass2
== 1) { /* pass == 1 */
358 while (*q
&& *q
!= ':') {
364 nasm_error(ERR_NONFATAL
,
365 "identifier expected after GLOBAL");
373 declare_as_global(value
, special
);
377 case D_COMMON
: /* [COMMON symbol size:special] */
384 value
++; /* skip initial $ if present */
389 while (*p
&& !nasm_isspace(*p
)) {
395 nasm_error(ERR_NONFATAL
, "identifier expected after COMMON");
399 p
= nasm_zap_spaces_fwd(p
);
401 while (*q
&& *q
!= ':')
409 size
= readnum(p
, &rn_error
);
411 nasm_error(ERR_NONFATAL
,
412 "invalid size specified"
413 " in COMMON declaration");
417 nasm_error(ERR_NONFATAL
,
418 "no size specified in"
419 " COMMON declaration");
424 define_common(value
, seg_alloc(), size
, special
);
425 } else if (pass0
== 2) {
427 ofmt
->symdef(value
, 0L, 0L, 3, special
);
432 case D_ABSOLUTE
: /* [ABSOLUTE address] */
438 tokval
.t_type
= TOKEN_INVALID
;
439 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
443 1 ? ERR_NONFATAL
: ERR_PANIC
,
444 "cannot use non-relocatable expression as "
447 absolute
.segment
= reloc_seg(e
);
448 absolute
.offset
= reloc_value(e
);
450 } else if (passn
== 1)
451 absolute
.offset
= 0x100; /* don't go near zero in case of / */
453 nasm_panic(0, "invalid ABSOLUTE address "
456 location
.segment
= NO_SEG
;
460 case D_DEBUG
: /* [DEBUG] */
462 bool badid
, overlong
;
467 badid
= overlong
= false;
468 if (!isidstart(*p
)) {
471 while (*p
&& !nasm_isspace(*p
)) {
472 if (q
>= debugid
+ sizeof debugid
- 1) {
483 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
484 "identifier expected after DEBUG");
488 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
489 "DEBUG identifier too long");
492 p
= nasm_skip_spaces(p
);
494 dfmt
->debug_directive(debugid
, p
);
498 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
499 if (!set_warning_status(value
)) {
500 nasm_error(ERR_WARNING
|ERR_WARN_UNK_WARNING
,
501 "unknown warning option: %s", value
);
505 case D_CPU
: /* [CPU] */
506 cpu
= get_cpu(value
);
509 case D_LIST
: /* [LIST {+|-}] */
510 value
= nasm_skip_spaces(value
);
522 case D_DEFAULT
: /* [DEFAULT] */
525 tokval
.t_type
= TOKEN_INVALID
;
526 if (stdscan(NULL
, &tokval
) != TOKEN_INVALID
) {
527 switch (tokval
.t_integer
) {
550 if (float_option(value
)) {
551 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
552 "unknown 'float' directive: %s", value
);
557 process_pragma(value
);
562 /* A common error message */
564 nasm_error(ERR_NONFATAL
, "invalid parameter to [%s] directive",