1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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 static iflag_t
get_cpu(char *value
)
65 if (!strcmp(value
, "8086"))
66 iflag_set(&r
, IF_8086
);
67 else if (!strcmp(value
, "186"))
68 iflag_set(&r
, IF_186
);
69 else if (!strcmp(value
, "286"))
70 iflag_set(&r
, IF_286
);
71 else if (!strcmp(value
, "386"))
72 iflag_set(&r
, IF_386
);
73 else if (!strcmp(value
, "486"))
74 iflag_set(&r
, IF_486
);
75 else if (!strcmp(value
, "586") ||
76 !nasm_stricmp(value
, "pentium"))
77 iflag_set(&r
, IF_PENT
);
78 else if (!strcmp(value
, "686") ||
79 !nasm_stricmp(value
, "ppro") ||
80 !nasm_stricmp(value
, "pentiumpro") ||
81 !nasm_stricmp(value
, "p2"))
83 else if (!nasm_stricmp(value
, "p3") ||
84 !nasm_stricmp(value
, "katmai"))
85 iflag_set(&r
, IF_KATMAI
);
86 else if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
87 !nasm_stricmp(value
, "willamette"))
88 iflag_set(&r
, IF_WILLAMETTE
);
89 else if (!nasm_stricmp(value
, "prescott"))
90 iflag_set(&r
, IF_PRESCOTT
);
91 else if (!nasm_stricmp(value
, "x64") ||
92 !nasm_stricmp(value
, "x86-64"))
93 iflag_set(&r
, IF_X86_64
);
94 else if (!nasm_stricmp(value
, "ia64") ||
95 !nasm_stricmp(value
, "ia-64") ||
96 !nasm_stricmp(value
, "itanium")||
97 !nasm_stricmp(value
, "itanic") ||
98 !nasm_stricmp(value
, "merced"))
99 iflag_set(&r
, IF_IA64
);
101 iflag_set(&r
, IF_PLEVEL
);
102 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
103 "unknown 'cpu' type");
108 static int get_bits(char *value
)
112 if ((i
= atoi(value
)) == 16)
113 return i
; /* set for a 16-bit segment */
115 if (iflag_ffs(&cpu
) < IF_386
) {
116 nasm_error(ERR_NONFATAL
,
117 "cannot specify 32-bit segment on processor below a 386");
120 } else if (i
== 64) {
121 if (iflag_ffs(&cpu
) < IF_X86_64
) {
122 nasm_error(ERR_NONFATAL
,
123 "cannot specify 64-bit segment on processor below an x86-64");
127 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
128 "`%s' is not a valid segment size; must be 16, 32 or 64",
135 static enum directive
parse_directive_line(char **directive
, char **value
)
139 buf
= nasm_skip_spaces(*directive
);
142 * It should be enclosed in [ ].
143 * XXX: we don't check there is nothing else on the remainder of the
144 * line, except a possible comment.
148 q
= strchr(buf
, ']');
153 * Strip off the comments. XXX: this doesn't account for quoted
154 * strings inside a directive. We should really strip the
155 * comments in generic code, not here. While we're at it, it
156 * would be better to pass the backend a series of tokens instead
157 * of a raw string, and actually process quoted strings for it,
158 * like of like argv is handled in C.
160 p
= strchr(buf
, ';');
162 if (p
< q
) /* ouch! somewhere inside */
167 /* no brace, no trailing spaces */
169 nasm_zap_spaces_rev(--q
);
172 p
= nasm_skip_spaces(++buf
);
173 q
= nasm_skip_word(p
);
175 return D_corrupt
; /* sigh... no value there */
179 /* and value finally */
180 p
= nasm_skip_spaces(++q
);
183 return directive_find(*directive
);
187 * Process a line from the assembler and try to handle it if it
188 * is a directive. Return true if the line was handled (including
189 * if it was an error), false otherwise.
191 bool process_directives(char *directive
)
194 char *value
, *p
, *q
, *special
;
195 struct tokenval tokval
;
196 bool bad_param
= false;
197 int pass2
= passn
> 1 ? 2 : 1;
199 /* used in the externdef case. Moved here because of ISO C90. */
203 d
= parse_directive_line(&directive
, &value
);
207 return D_none
; /* Not a directive */
210 nasm_error(ERR_NONFATAL
, "invalid directive line");
213 default: /* It's a backend-specific directive */
214 switch (ofmt
->directive(d
, value
, pass2
)) {
230 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
231 "unrecognised directive [%s]", directive
);
234 case D_SEGMENT
: /* [SEGMENT n] */
238 int32_t seg
= ofmt
->section(value
, pass2
, &sb
);
241 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
242 "segment name `%s' not recognized", value
);
245 location
.segment
= seg
;
251 if(passn
== 2) { /* wait for local symbols to be declared */
253 value
++; /* skip initial $ if present */
255 while (*q
&& *q
!= ':')
259 nasm_error(ERR_DEBUG
, "Extern definition of symbol `%s'", value
);
263 if(lookup_label(value
, &lblsegment
, &lbloffset
)) {
264 nasm_error(ERR_DEBUG
, "Label locally declared, set label to global.");
265 declare_as_global(value
, 0);
267 nasm_error(ERR_DEBUG
, "Label was not found locally, introduce as extern!");
268 goto externdef_behave_extern
;
273 case D_SECTALIGN
: /* [SECTALIGN n] */
280 tokval
.t_type
= TOKEN_INVALID
;
281 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
283 uint64_t align
= e
->value
;
285 if (!is_power2(e
->value
)) {
286 nasm_error(ERR_NONFATAL
,
287 "segment alignment `%s' is not power of two",
289 } else if (align
> UINT64_C(0x7fffffff)) {
291 * FIXME: Please make some sane message here
292 * ofmt should have some 'check' method which
293 * would report segment alignment bounds.
295 nasm_error(ERR_NONFATAL
,
296 "absurdly large segment alignment `%s' (2^%d)",
297 value
, ilog2_64(align
));
300 /* callee should be able to handle all details */
301 if (location
.segment
!= NO_SEG
)
302 ofmt
->sectalign(location
.segment
, align
);
308 externdef_behave_extern
: /* goto mark for externdef */
309 case D_EXTERN
: /* [EXTERN label:special] */
311 value
++; /* skip initial $ if present */
314 while (*q
&& *q
!= ':')
318 ofmt
->symdef(value
, 0L, 0L, 3, q
);
320 } else if (passn
== 2) { /* changed value from 1 to 2, hope this will cause no trouble */
325 while (*q
&& *q
!= ':') {
331 nasm_error(ERR_NONFATAL
, "identifier expected after EXTERN");
339 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
341 pass0
= 1; /* fake pass 1 in labels.c */
342 declare_as_global(value
, special
);
343 define_label(value
, seg_alloc(), 0L, NULL
,
347 } /* else pass0 == 1 */
350 case D_BITS
: /* [BITS bits] */
351 globalbits
= get_bits(value
);
354 case D_GLOBAL
: /* [GLOBAL symbol:special] */
356 value
++; /* skip initial $ if present */
357 if (pass0
== 2) { /* pass 2 */
359 while (*q
&& *q
!= ':')
363 ofmt
->symdef(value
, 0L, 0L, 3, q
);
365 } else if (pass2
== 1) { /* pass == 1 */
371 while (*q
&& *q
!= ':') {
377 nasm_error(ERR_NONFATAL
,
378 "identifier expected after GLOBAL");
386 declare_as_global(value
, special
);
390 case D_COMMON
: /* [COMMON symbol size:special] */
397 value
++; /* skip initial $ if present */
402 while (*p
&& !nasm_isspace(*p
)) {
408 nasm_error(ERR_NONFATAL
, "identifier expected after COMMON");
412 p
= nasm_zap_spaces_fwd(p
);
414 while (*q
&& *q
!= ':')
422 size
= readnum(p
, &rn_error
);
424 nasm_error(ERR_NONFATAL
,
425 "invalid size specified"
426 " in COMMON declaration");
430 nasm_error(ERR_NONFATAL
,
431 "no size specified in"
432 " COMMON declaration");
437 define_common(value
, seg_alloc(), size
, special
);
438 } else if (pass0
== 2) {
440 ofmt
->symdef(value
, 0L, 0L, 3, special
);
445 case D_ABSOLUTE
: /* [ABSOLUTE address] */
451 tokval
.t_type
= TOKEN_INVALID
;
452 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, NULL
);
456 1 ? ERR_NONFATAL
: ERR_PANIC
,
457 "cannot use non-relocatable expression as "
460 absolute
.segment
= reloc_seg(e
);
461 absolute
.offset
= reloc_value(e
);
463 } else if (passn
== 1)
464 absolute
.offset
= 0x100; /* don't go near zero in case of / */
466 nasm_panic(0, "invalid ABSOLUTE address "
469 location
.segment
= NO_SEG
;
473 case D_DEBUG
: /* [DEBUG] */
475 bool badid
, overlong
;
480 badid
= overlong
= false;
481 if (!isidstart(*p
)) {
484 while (*p
&& !nasm_isspace(*p
)) {
485 if (q
>= debugid
+ sizeof debugid
- 1) {
496 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
497 "identifier expected after DEBUG");
501 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
502 "DEBUG identifier too long");
505 p
= nasm_skip_spaces(p
);
507 dfmt
->debug_directive(debugid
, p
);
511 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
512 if (!set_warning_status(value
)) {
513 nasm_error(ERR_WARNING
|ERR_WARN_UNK_WARNING
,
514 "unknown warning option: %s", value
);
518 case D_CPU
: /* [CPU] */
519 cpu
= get_cpu(value
);
522 case D_LIST
: /* [LIST {+|-}] */
523 value
= nasm_skip_spaces(value
);
535 case D_DEFAULT
: /* [DEFAULT] */
538 tokval
.t_type
= TOKEN_INVALID
;
539 if (stdscan(NULL
, &tokval
) != TOKEN_INVALID
) {
540 switch (tokval
.t_integer
) {
563 if (float_option(value
)) {
564 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_PANIC
,
565 "unknown 'float' directive: %s", value
);
570 process_pragma(value
);
575 /* A common error message */
577 nasm_error(ERR_NONFATAL
, "invalid parameter to [%s] directive",