Fix global variables without declarations
[nasm.git] / asm / directiv.c
blobcbfdd089d1a9df45402317cf5a68b93768e17cd8
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
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 <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <limits.h>
45 #include "nasm.h"
46 #include "nasmlib.h"
47 #include "error.h"
48 #include "float.h"
49 #include "stdscan.h"
50 #include "preproc.h"
51 /* #include "parser.h" */
52 #include "eval.h"
53 #include "assemble.h"
54 #include "outform.h"
55 #include "listing.h"
56 #include "labels.h"
57 #include "iflag.h"
59 static iflag_t get_cpu(char *value)
61 iflag_t r;
63 iflag_clear_all(&r);
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"))
82 iflag_set(&r, IF_P6);
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);
100 else {
101 iflag_set(&r, IF_PLEVEL);
102 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
103 "unknown 'cpu' type");
105 return r;
108 static int get_bits(char *value)
110 int i;
112 if ((i = atoi(value)) == 16)
113 return i; /* set for a 16-bit segment */
114 else if (i == 32) {
115 if (iflag_ffs(&cpu) < IF_386) {
116 nasm_error(ERR_NONFATAL,
117 "cannot specify 32-bit segment on processor below a 386");
118 i = 16;
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");
124 i = 16;
126 } else {
127 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
128 "`%s' is not a valid segment size; must be 16, 32 or 64",
129 value);
130 i = 16;
132 return i;
135 static enum directives parse_directive_line(char **directive, char **value)
137 char *p, *q, *buf;
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.
146 if (*buf != '[')
147 return D_none;
148 q = strchr(buf, ']');
149 if (!q)
150 return D_corrupt;
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, ';');
161 if (p) {
162 if (p < q) /* ouch! somewhere inside */
163 return D_corrupt;
164 *p = '\0';
167 /* no brace, no trailing spaces */
168 *q = '\0';
169 nasm_zap_spaces_rev(--q);
171 /* directive */
172 p = nasm_skip_spaces(++buf);
173 q = nasm_skip_word(p);
174 if (!q)
175 return D_corrupt; /* sigh... no value there */
176 *q = '\0';
177 *directive = p;
179 /* and value finally */
180 p = nasm_skip_spaces(++q);
181 *value = p;
183 return find_directive(*directive);
186 static void process_pragma(char *str)
188 (void)str;
191 enum directives process_directives(char *directive)
193 enum directives d;
194 char *value, *p, *q, *special;
195 struct tokenval tokval;
196 bool bad_param = false;
197 int pass2 = passn > 1 ? 2 : 1;
199 d = parse_directive_line(&directive, &value);
201 switch (d) {
202 case D_none:
203 return D_none; /* Not a directive */
205 case D_corrupt:
206 nasm_error(ERR_NONFATAL, "invalid directive line");
207 break;
209 default: /* It's a backend-specific directive */
210 if (ofmt->directive(d, value, pass2))
211 break;
212 /* else fall through */
213 case D_unknown:
214 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
215 "unrecognised directive [%s]", directive);
216 break;
218 case D_SEGMENT: /* [SEGMENT n] */
219 case D_SECTION:
221 int sb;
222 int32_t seg = ofmt->section(value, pass2, &sb);
224 if (seg == NO_SEG) {
225 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
226 "segment name `%s' not recognized", value);
227 } else {
228 in_absolute = false;
229 location.segment = seg;
231 break;
234 case D_SECTALIGN: /* [SECTALIGN n] */
236 expr *e;
238 if (*value) {
239 stdscan_reset();
240 stdscan_set(value);
241 tokval.t_type = TOKEN_INVALID;
242 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
243 if (e) {
244 uint64_t align = e->value;
246 if (!is_power2(e->value)) {
247 nasm_error(ERR_NONFATAL,
248 "segment alignment `%s' is not power of two",
249 value);
250 } else if (align > UINT64_C(0x7fffffff)) {
252 * FIXME: Please make some sane message here
253 * ofmt should have some 'check' method which
254 * would report segment alignment bounds.
256 nasm_error(ERR_NONFATAL,
257 "absurdly large segment alignment `%s' (2^%d)",
258 value, ilog2_64(align));
261 /* callee should be able to handle all details */
262 if (location.segment != NO_SEG)
263 ofmt->sectalign(location.segment, align);
266 break;
269 case D_EXTERN: /* [EXTERN label:special] */
270 if (*value == '$')
271 value++; /* skip initial $ if present */
272 if (pass0 == 2) {
273 q = value;
274 while (*q && *q != ':')
275 q++;
276 if (*q == ':') {
277 *q++ = '\0';
278 ofmt->symdef(value, 0L, 0L, 3, q);
280 } else if (passn == 1) {
281 bool validid = true;
282 q = value;
283 if (!isidstart(*q))
284 validid = false;
285 while (*q && *q != ':') {
286 if (!isidchar(*q))
287 validid = false;
288 q++;
290 if (!validid) {
291 nasm_error(ERR_NONFATAL, "identifier expected after EXTERN");
292 break;
294 if (*q == ':') {
295 *q++ = '\0';
296 special = q;
297 } else
298 special = NULL;
299 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
300 int temp = pass0;
301 pass0 = 1; /* fake pass 1 in labels.c */
302 declare_as_global(value, special);
303 define_label(value, seg_alloc(), 0L, NULL,
304 false, true);
305 pass0 = temp;
307 } /* else pass0 == 1 */
308 break;
310 case D_BITS: /* [BITS bits] */
311 globalbits = get_bits(value);
312 break;
314 case D_GLOBAL: /* [GLOBAL symbol:special] */
315 if (*value == '$')
316 value++; /* skip initial $ if present */
317 if (pass0 == 2) { /* pass 2 */
318 q = value;
319 while (*q && *q != ':')
320 q++;
321 if (*q == ':') {
322 *q++ = '\0';
323 ofmt->symdef(value, 0L, 0L, 3, q);
325 } else if (pass2 == 1) { /* pass == 1 */
326 bool validid = true;
328 q = value;
329 if (!isidstart(*q))
330 validid = false;
331 while (*q && *q != ':') {
332 if (!isidchar(*q))
333 validid = false;
334 q++;
336 if (!validid) {
337 nasm_error(ERR_NONFATAL,
338 "identifier expected after GLOBAL");
339 break;
341 if (*q == ':') {
342 *q++ = '\0';
343 special = q;
344 } else
345 special = NULL;
346 declare_as_global(value, special);
347 } /* pass == 1 */
348 break;
350 case D_COMMON: /* [COMMON symbol size:special] */
352 int64_t size;
353 bool rn_error;
354 bool validid;
356 if (*value == '$')
357 value++; /* skip initial $ if present */
358 p = value;
359 validid = true;
360 if (!isidstart(*p))
361 validid = false;
362 while (*p && !nasm_isspace(*p)) {
363 if (!isidchar(*p))
364 validid = false;
365 p++;
367 if (!validid) {
368 nasm_error(ERR_NONFATAL, "identifier expected after COMMON");
369 break;
371 if (*p) {
372 p = nasm_zap_spaces_fwd(p);
373 q = p;
374 while (*q && *q != ':')
375 q++;
376 if (*q == ':') {
377 *q++ = '\0';
378 special = q;
379 } else {
380 special = NULL;
382 size = readnum(p, &rn_error);
383 if (rn_error) {
384 nasm_error(ERR_NONFATAL,
385 "invalid size specified"
386 " in COMMON declaration");
387 break;
389 } else {
390 nasm_error(ERR_NONFATAL,
391 "no size specified in"
392 " COMMON declaration");
393 break;
396 if (pass0 < 2) {
397 define_common(value, seg_alloc(), size, special);
398 } else if (pass0 == 2) {
399 if (special)
400 ofmt->symdef(value, 0L, 0L, 3, special);
402 break;
405 case D_ABSOLUTE: /* [ABSOLUTE address] */
407 expr *e;
409 stdscan_reset();
410 stdscan_set(value);
411 tokval.t_type = TOKEN_INVALID;
412 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
413 if (e) {
414 if (!is_reloc(e))
415 nasm_error(pass0 ==
416 1 ? ERR_NONFATAL : ERR_PANIC,
417 "cannot use non-relocatable expression as "
418 "ABSOLUTE address");
419 else {
420 absolute.segment = reloc_seg(e);
421 absolute.offset = reloc_value(e);
423 } else if (passn == 1)
424 absolute.offset = 0x100; /* don't go near zero in case of / */
425 else
426 nasm_panic(0, "invalid ABSOLUTE address "
427 "in pass two");
428 in_absolute = true;
429 location.segment = NO_SEG;
430 break;
433 case D_DEBUG: /* [DEBUG] */
435 bool badid, overlong;
436 char debugid[128];
438 p = value;
439 q = debugid;
440 badid = overlong = false;
441 if (!isidstart(*p)) {
442 badid = true;
443 } else {
444 while (*p && !nasm_isspace(*p)) {
445 if (q >= debugid + sizeof debugid - 1) {
446 overlong = true;
447 break;
449 if (!isidchar(*p))
450 badid = true;
451 *q++ = *p++;
453 *q = 0;
455 if (badid) {
456 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
457 "identifier expected after DEBUG");
458 break;
460 if (overlong) {
461 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
462 "DEBUG identifier too long");
463 break;
465 p = nasm_skip_spaces(p);
466 if (pass0 == 2)
467 dfmt->debug_directive(debugid, p);
468 break;
471 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
473 enum warn_action { WID_OFF, WID_ON, WID_RESET };
474 enum warn_action action;
475 int i;
477 value = nasm_skip_spaces(value);
478 switch(*value) {
479 case '-': action = WID_OFF; value++; break;
480 case '+': action = WID_ON; value++; break;
481 case '*': action = WID_RESET; value++; break;
482 default: action = WID_ON; break;
485 for (i = 1; i <= ERR_WARN_MAX; i++)
486 if (!nasm_stricmp(value, warnings[i].name))
487 break;
488 if (i <= ERR_WARN_MAX) {
489 switch (action) {
490 case WID_OFF:
491 warning_on[i] = false;
492 break;
493 case WID_ON:
494 warning_on[i] = true;
495 break;
496 case WID_RESET:
497 warning_on[i] = warning_on_global[i];
498 break;
501 break;
504 case D_CPU: /* [CPU] */
505 cpu = get_cpu(value);
506 break;
508 case D_LIST: /* [LIST {+|-}] */
509 value = nasm_skip_spaces(value);
510 if (*value == '+') {
511 user_nolist = false;
512 } else {
513 if (*value == '-') {
514 user_nolist = true;
515 } else {
516 bad_param = true;
519 break;
521 case D_DEFAULT: /* [DEFAULT] */
522 stdscan_reset();
523 stdscan_set(value);
524 tokval.t_type = TOKEN_INVALID;
525 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
526 switch (tokval.t_integer) {
527 case S_REL:
528 globalrel = 1;
529 break;
530 case S_ABS:
531 globalrel = 0;
532 break;
533 case P_BND:
534 globalbnd = 1;
535 break;
536 case P_NOBND:
537 globalbnd = 0;
538 break;
539 default:
540 bad_param = true;
541 break;
543 } else {
544 bad_param = true;
546 break;
548 case D_FLOAT:
549 if (float_option(value)) {
550 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_PANIC,
551 "unknown 'float' directive: %s", value);
553 break;
555 case D_PRAGMA:
556 process_pragma(value);
557 break;
561 /* A common error message */
562 if (bad_param) {
563 nasm_error(ERR_NONFATAL, "invalid parameter to [%s] directive",
564 directive);
567 return d;