fix: respect CFLAGS from environment, or set defaults
[tinycc.git] / tccasm.c
blobe52872b567e5d7d9a10e06e8b2668307d5b02fea
1 /*
2 * GAS like assembler for TCC
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define USING_GLOBALS
22 #include "tcc.h"
23 #ifdef CONFIG_TCC_ASM
25 static Section *last_text_section; /* to handle .previous asm directive */
26 static int asmgoto_n;
28 static int asm_get_prefix_name(TCCState *s1, const char *prefix, unsigned int n)
30 char buf[64];
31 snprintf(buf, sizeof(buf), "%s%u", prefix, n);
32 return tok_alloc_const(buf);
35 ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n)
37 return asm_get_prefix_name(s1, "L..", n);
40 static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global);
41 static Sym* asm_new_label(TCCState *s1, int label, int is_local);
42 static Sym* asm_new_label1(TCCState *s1, int label, int is_local, int sh_num, int value);
44 /* If a C name has an _ prepended then only asm labels that start
45 with _ are representable in C, by removing the first _. ASM names
46 without _ at the beginning don't correspond to C names, but we use
47 the global C symbol table to track ASM names as well, so we need to
48 transform those into ones that don't conflict with a C name,
49 so prepend a '.' for them, but force the ELF asm name to be set. */
50 static int asm2cname(int v, int *addeddot)
52 const char *name;
53 *addeddot = 0;
54 if (!tcc_state->leading_underscore)
55 return v;
56 name = get_tok_str(v, NULL);
57 if (!name)
58 return v;
59 if (name[0] == '_') {
60 v = tok_alloc_const(name + 1);
61 } else if (!strchr(name, '.')) {
62 char newname[256];
63 snprintf(newname, sizeof newname, ".%s", name);
64 v = tok_alloc_const(newname);
65 *addeddot = 1;
67 return v;
70 static Sym *asm_label_find(int v)
72 Sym *sym;
73 int addeddot;
74 v = asm2cname(v, &addeddot);
75 sym = sym_find(v);
76 while (sym && sym->sym_scope && !(sym->type.t & VT_STATIC))
77 sym = sym->prev_tok;
78 return sym;
81 static Sym *asm_label_push(int v)
83 int addeddot, v2 = asm2cname(v, &addeddot);
84 /* We always add VT_EXTERN, for sym definition that's tentative
85 (for .set, removed for real defs), for mere references it's correct
86 as is. */
87 Sym *sym = global_identifier_push(v2, VT_ASM | VT_EXTERN | VT_STATIC, 0);
88 if (addeddot)
89 sym->asm_label = v;
90 return sym;
93 /* Return a symbol we can use inside the assembler, having name NAME.
94 Symbols from asm and C source share a namespace. If we generate
95 an asm symbol it's also a (file-global) C symbol, but it's
96 either not accessible by name (like "L.123"), or its type information
97 is such that it's not usable without a proper C declaration.
99 Sometimes we need symbols accessible by name from asm, which
100 are anonymous in C, in this case CSYM can be used to transfer
101 all information from that symbol to the (possibly newly created)
102 asm symbol. */
103 ST_FUNC Sym* get_asm_sym(int name, Sym *csym)
105 Sym *sym = asm_label_find(name);
106 if (!sym) {
107 sym = asm_label_push(name);
108 if (csym)
109 sym->c = csym->c;
111 return sym;
114 static Sym* asm_section_sym(TCCState *s1, Section *sec)
116 char buf[100]; int label; Sym *sym;
117 snprintf(buf, sizeof buf, "L.%s", sec->name);
118 label = tok_alloc_const(buf);
119 sym = asm_label_find(label);
120 return sym ? sym : asm_new_label1(s1, label, 1, sec->sh_num, 0);
123 /* We do not use the C expression parser to handle symbols. Maybe the
124 C expression parser could be tweaked to do so. */
126 static void asm_expr_unary(TCCState *s1, ExprValue *pe)
128 Sym *sym;
129 int op, label;
130 uint64_t n;
131 const char *p;
133 switch(tok) {
134 case TOK_PPNUM:
135 p = tokc.str.data;
136 n = strtoull(p, (char **)&p, 0);
137 if (*p == 'b' || *p == 'f') {
138 /* backward or forward label */
139 label = asm_get_local_label_name(s1, n);
140 sym = asm_label_find(label);
141 if (*p == 'b') {
142 /* backward : find the last corresponding defined label */
143 if (sym && (!sym->c || elfsym(sym)->st_shndx == SHN_UNDEF))
144 sym = sym->prev_tok;
145 if (!sym)
146 tcc_error("local label '%d' not found backward", (int)n);
147 } else {
148 /* forward */
149 if (!sym || (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF)) {
150 /* if the last label is defined, then define a new one */
151 sym = asm_label_push(label);
154 pe->v = 0;
155 pe->sym = sym;
156 pe->pcrel = 0;
157 } else if (*p == '\0') {
158 pe->v = n;
159 pe->sym = NULL;
160 pe->pcrel = 0;
161 } else {
162 tcc_error("invalid number syntax");
164 next();
165 break;
166 case '+':
167 next();
168 asm_expr_unary(s1, pe);
169 break;
170 case '-':
171 case '~':
172 op = tok;
173 next();
174 asm_expr_unary(s1, pe);
175 if (pe->sym)
176 tcc_error("invalid operation with label");
177 if (op == '-')
178 pe->v = -pe->v;
179 else
180 pe->v = ~pe->v;
181 break;
182 case TOK_CCHAR:
183 case TOK_LCHAR:
184 pe->v = tokc.i;
185 pe->sym = NULL;
186 pe->pcrel = 0;
187 next();
188 break;
189 case '(':
190 next();
191 asm_expr(s1, pe);
192 skip(')');
193 break;
194 case '.':
195 pe->v = ind;
196 pe->sym = asm_section_sym(s1, cur_text_section);
197 pe->pcrel = 0;
198 next();
199 break;
200 default:
201 if (tok >= TOK_IDENT) {
202 ElfSym *esym;
203 /* label case : if the label was not found, add one */
204 sym = get_asm_sym(tok, NULL);
205 esym = elfsym(sym);
206 if (esym && esym->st_shndx == SHN_ABS) {
207 /* if absolute symbol, no need to put a symbol value */
208 pe->v = esym->st_value;
209 pe->sym = NULL;
210 pe->pcrel = 0;
211 } else {
212 pe->v = 0;
213 pe->sym = sym;
214 pe->pcrel = 0;
216 next();
217 } else {
218 tcc_error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
220 break;
224 static void asm_expr_prod(TCCState *s1, ExprValue *pe)
226 int op;
227 ExprValue e2;
229 asm_expr_unary(s1, pe);
230 for(;;) {
231 op = tok;
232 if (op != '*' && op != '/' && op != '%' &&
233 op != TOK_SHL && op != TOK_SAR)
234 break;
235 next();
236 asm_expr_unary(s1, &e2);
237 if (pe->sym || e2.sym)
238 tcc_error("invalid operation with label");
239 switch(op) {
240 case '*':
241 pe->v *= e2.v;
242 break;
243 case '/':
244 if (e2.v == 0) {
245 div_error:
246 tcc_error("division by zero");
248 pe->v /= e2.v;
249 break;
250 case '%':
251 if (e2.v == 0)
252 goto div_error;
253 pe->v %= e2.v;
254 break;
255 case TOK_SHL:
256 pe->v <<= e2.v;
257 break;
258 default:
259 case TOK_SAR:
260 pe->v >>= e2.v;
261 break;
266 static void asm_expr_logic(TCCState *s1, ExprValue *pe)
268 int op;
269 ExprValue e2;
271 asm_expr_prod(s1, pe);
272 for(;;) {
273 op = tok;
274 if (op != '&' && op != '|' && op != '^')
275 break;
276 next();
277 asm_expr_prod(s1, &e2);
278 if (pe->sym || e2.sym)
279 tcc_error("invalid operation with label");
280 switch(op) {
281 case '&':
282 pe->v &= e2.v;
283 break;
284 case '|':
285 pe->v |= e2.v;
286 break;
287 default:
288 case '^':
289 pe->v ^= e2.v;
290 break;
295 static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
297 int op;
298 ExprValue e2;
300 asm_expr_logic(s1, pe);
301 for(;;) {
302 op = tok;
303 if (op != '+' && op != '-')
304 break;
305 next();
306 asm_expr_logic(s1, &e2);
307 if (op == '+') {
308 if (pe->sym != NULL && e2.sym != NULL)
309 goto cannot_relocate;
310 pe->v += e2.v;
311 if (pe->sym == NULL && e2.sym != NULL)
312 pe->sym = e2.sym;
313 } else {
314 pe->v -= e2.v;
315 /* NOTE: we are less powerful than gas in that case
316 because we store only one symbol in the expression */
317 if (!e2.sym) {
318 /* OK */
319 } else if (pe->sym == e2.sym) {
320 /* OK */
321 pe->sym = NULL; /* same symbols can be subtracted to NULL */
322 } else {
323 ElfSym *esym1, *esym2;
324 esym1 = elfsym(pe->sym);
325 esym2 = elfsym(e2.sym);
326 if (!esym2)
327 goto cannot_relocate;
328 if (esym1 && esym1->st_shndx == esym2->st_shndx
329 && esym1->st_shndx != SHN_UNDEF) {
330 /* we also accept defined symbols in the same section */
331 pe->v += esym1->st_value - esym2->st_value;
332 pe->sym = NULL;
333 } else if (esym2->st_shndx == cur_text_section->sh_num) {
334 /* When subtracting a defined symbol in current section
335 this actually makes the value PC-relative. */
336 pe->v += 0 - esym2->st_value;
337 pe->pcrel = 1;
338 e2.sym = NULL;
339 } else {
340 cannot_relocate:
341 tcc_error("invalid operation with label");
348 static inline void asm_expr_cmp(TCCState *s1, ExprValue *pe)
350 int op;
351 ExprValue e2;
353 asm_expr_sum(s1, pe);
354 for(;;) {
355 op = tok;
356 if (op != TOK_EQ && op != TOK_NE
357 && (op > TOK_GT || op < TOK_ULE))
358 break;
359 next();
360 asm_expr_sum(s1, &e2);
361 if (pe->sym || e2.sym)
362 tcc_error("invalid operation with label");
363 switch(op) {
364 case TOK_EQ:
365 pe->v = pe->v == e2.v;
366 break;
367 case TOK_NE:
368 pe->v = pe->v != e2.v;
369 break;
370 case TOK_LT:
371 pe->v = (int64_t)pe->v < (int64_t)e2.v;
372 break;
373 case TOK_GE:
374 pe->v = (int64_t)pe->v >= (int64_t)e2.v;
375 break;
376 case TOK_LE:
377 pe->v = (int64_t)pe->v <= (int64_t)e2.v;
378 break;
379 case TOK_GT:
380 pe->v = (int64_t)pe->v > (int64_t)e2.v;
381 break;
382 default:
383 break;
385 /* GAS compare results are -1/0 not 1/0. */
386 pe->v = -(int64_t)pe->v;
390 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
392 asm_expr_cmp(s1, pe);
395 ST_FUNC int asm_int_expr(TCCState *s1)
397 ExprValue e;
398 asm_expr(s1, &e);
399 if (e.sym)
400 expect("constant");
401 return e.v;
404 static Sym* asm_new_label1(TCCState *s1, int label, int is_local,
405 int sh_num, int value)
407 Sym *sym;
408 ElfSym *esym;
410 sym = asm_label_find(label);
411 if (sym) {
412 esym = elfsym(sym);
413 /* A VT_EXTERN symbol, even if it has a section is considered
414 overridable. This is how we "define" .set targets. Real
415 definitions won't have VT_EXTERN set. */
416 if (esym && esym->st_shndx != SHN_UNDEF) {
417 /* the label is already defined */
418 if (IS_ASM_SYM(sym)
419 && (is_local == 1 || (sym->type.t & VT_EXTERN)))
420 goto new_label;
421 if (!(sym->type.t & VT_EXTERN))
422 tcc_error("assembler label '%s' already defined",
423 get_tok_str(label, NULL));
425 } else {
426 new_label:
427 sym = asm_label_push(label);
429 if (!sym->c)
430 put_extern_sym2(sym, SHN_UNDEF, 0, 0, 1);
431 esym = elfsym(sym);
432 esym->st_shndx = sh_num;
433 esym->st_value = value;
434 if (is_local != 2)
435 sym->type.t &= ~VT_EXTERN;
436 return sym;
439 static Sym* asm_new_label(TCCState *s1, int label, int is_local)
441 return asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
444 /* Set the value of LABEL to that of some expression (possibly
445 involving other symbols). LABEL can be overwritten later still. */
446 static Sym* set_symbol(TCCState *s1, int label)
448 long n;
449 ExprValue e;
450 Sym *sym;
451 ElfSym *esym;
452 next();
453 asm_expr(s1, &e);
454 n = e.v;
455 esym = elfsym(e.sym);
456 if (esym)
457 n += esym->st_value;
458 sym = asm_new_label1(s1, label, 2, esym ? esym->st_shndx : SHN_ABS, n);
459 elfsym(sym)->st_other |= ST_ASM_SET;
460 return sym;
463 static void use_section1(TCCState *s1, Section *sec)
465 cur_text_section->data_offset = ind;
466 cur_text_section = sec;
467 ind = cur_text_section->data_offset;
470 static void use_section(TCCState *s1, const char *name)
472 Section *sec;
473 sec = find_section(s1, name);
474 use_section1(s1, sec);
477 static void push_section(TCCState *s1, const char *name)
479 Section *sec = find_section(s1, name);
480 sec->prev = cur_text_section;
481 use_section1(s1, sec);
484 static void pop_section(TCCState *s1)
486 Section *prev = cur_text_section->prev;
487 if (!prev)
488 tcc_error(".popsection without .pushsection");
489 cur_text_section->prev = NULL;
490 use_section1(s1, prev);
493 static void asm_parse_directive(TCCState *s1, int global)
495 int n, offset, v, size, tok1;
496 Section *sec;
497 uint8_t *ptr;
499 /* assembler directive */
500 sec = cur_text_section;
501 switch(tok) {
502 case TOK_ASMDIR_align:
503 case TOK_ASMDIR_balign:
504 case TOK_ASMDIR_p2align:
505 case TOK_ASMDIR_skip:
506 case TOK_ASMDIR_space:
507 tok1 = tok;
508 next();
509 n = asm_int_expr(s1);
510 if (tok1 == TOK_ASMDIR_p2align)
512 if (n < 0 || n > 30)
513 tcc_error("invalid p2align, must be between 0 and 30");
514 n = 1 << n;
515 tok1 = TOK_ASMDIR_align;
517 if (tok1 == TOK_ASMDIR_align || tok1 == TOK_ASMDIR_balign) {
518 if (n < 0 || (n & (n-1)) != 0)
519 tcc_error("alignment must be a positive power of two");
520 offset = (ind + n - 1) & -n;
521 size = offset - ind;
522 /* the section must have a compatible alignment */
523 if (sec->sh_addralign < n)
524 sec->sh_addralign = n;
525 } else {
526 if (n < 0)
527 n = 0;
528 size = n;
530 v = 0;
531 if (tok == ',') {
532 next();
533 v = asm_int_expr(s1);
535 zero_pad:
536 if (sec->sh_type != SHT_NOBITS) {
537 sec->data_offset = ind;
538 ptr = section_ptr_add(sec, size);
539 memset(ptr, v, size);
541 ind += size;
542 break;
543 case TOK_ASMDIR_quad:
544 #ifdef TCC_TARGET_X86_64
545 size = 8;
546 goto asm_data;
547 #else
548 next();
549 for(;;) {
550 uint64_t vl;
551 const char *p;
553 p = tokc.str.data;
554 if (tok != TOK_PPNUM) {
555 error_constant:
556 tcc_error("64 bit constant");
558 vl = strtoll(p, (char **)&p, 0);
559 if (*p != '\0')
560 goto error_constant;
561 next();
562 if (sec->sh_type != SHT_NOBITS) {
563 /* XXX: endianness */
564 gen_le32(vl);
565 gen_le32(vl >> 32);
566 } else {
567 ind += 8;
569 if (tok != ',')
570 break;
571 next();
573 break;
574 #endif
575 case TOK_ASMDIR_byte:
576 size = 1;
577 goto asm_data;
578 case TOK_ASMDIR_word:
579 case TOK_ASMDIR_short:
580 size = 2;
581 goto asm_data;
582 case TOK_ASMDIR_long:
583 case TOK_ASMDIR_int:
584 size = 4;
585 asm_data:
586 next();
587 for(;;) {
588 ExprValue e;
589 asm_expr(s1, &e);
590 if (sec->sh_type != SHT_NOBITS) {
591 if (size == 4) {
592 gen_expr32(&e);
593 #ifdef TCC_TARGET_X86_64
594 } else if (size == 8) {
595 gen_expr64(&e);
596 #endif
597 } else {
598 if (e.sym)
599 expect("constant");
600 if (size == 1)
601 g(e.v);
602 else
603 gen_le16(e.v);
605 } else {
606 ind += size;
608 if (tok != ',')
609 break;
610 next();
612 break;
613 case TOK_ASMDIR_fill:
615 int repeat, size, val, i, j;
616 uint8_t repeat_buf[8];
617 next();
618 repeat = asm_int_expr(s1);
619 if (repeat < 0) {
620 tcc_error("repeat < 0; .fill ignored");
621 break;
623 size = 1;
624 val = 0;
625 if (tok == ',') {
626 next();
627 size = asm_int_expr(s1);
628 if (size < 0) {
629 tcc_error("size < 0; .fill ignored");
630 break;
632 if (size > 8)
633 size = 8;
634 if (tok == ',') {
635 next();
636 val = asm_int_expr(s1);
639 /* XXX: endianness */
640 repeat_buf[0] = val;
641 repeat_buf[1] = val >> 8;
642 repeat_buf[2] = val >> 16;
643 repeat_buf[3] = val >> 24;
644 repeat_buf[4] = 0;
645 repeat_buf[5] = 0;
646 repeat_buf[6] = 0;
647 repeat_buf[7] = 0;
648 for(i = 0; i < repeat; i++) {
649 for(j = 0; j < size; j++) {
650 g(repeat_buf[j]);
654 break;
655 case TOK_ASMDIR_rept:
657 int repeat;
658 TokenString *init_str;
659 next();
660 repeat = asm_int_expr(s1);
661 init_str = tok_str_alloc();
662 while (next(), tok != TOK_ASMDIR_endr) {
663 if (tok == CH_EOF)
664 tcc_error("we at end of file, .endr not found");
665 tok_str_add_tok(init_str);
667 tok_str_add(init_str, TOK_EOF);
668 begin_macro(init_str, 1);
669 while (repeat-- > 0) {
670 tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS),
671 global);
672 macro_ptr = init_str->str;
674 end_macro();
675 next();
676 break;
678 case TOK_ASMDIR_org:
680 unsigned long n;
681 ExprValue e;
682 ElfSym *esym;
683 next();
684 asm_expr(s1, &e);
685 n = e.v;
686 esym = elfsym(e.sym);
687 if (esym) {
688 if (esym->st_shndx != cur_text_section->sh_num)
689 expect("constant or same-section symbol");
690 n += esym->st_value;
692 if (n < ind)
693 tcc_error("attempt to .org backwards");
694 v = 0;
695 size = n - ind;
696 goto zero_pad;
698 break;
699 case TOK_ASMDIR_set:
700 next();
701 tok1 = tok;
702 next();
703 /* Also accept '.set stuff', but don't do anything with this.
704 It's used in GAS to set various features like '.set mips16'. */
705 if (tok == ',')
706 set_symbol(s1, tok1);
707 break;
708 case TOK_ASMDIR_globl:
709 case TOK_ASMDIR_global:
710 case TOK_ASMDIR_weak:
711 case TOK_ASMDIR_hidden:
712 tok1 = tok;
713 do {
714 Sym *sym;
715 next();
716 sym = get_asm_sym(tok, NULL);
717 if (tok1 != TOK_ASMDIR_hidden)
718 sym->type.t &= ~VT_STATIC;
719 if (tok1 == TOK_ASMDIR_weak)
720 sym->a.weak = 1;
721 else if (tok1 == TOK_ASMDIR_hidden)
722 sym->a.visibility = STV_HIDDEN;
723 update_storage(sym);
724 next();
725 } while (tok == ',');
726 break;
727 case TOK_ASMDIR_string:
728 case TOK_ASMDIR_ascii:
729 case TOK_ASMDIR_asciz:
731 const char *p;
732 int i, size, t;
734 t = tok;
735 next();
736 for(;;) {
737 if (tok != TOK_STR)
738 expect("string constant");
739 p = tokc.str.data;
740 size = tokc.str.size;
741 if (t == TOK_ASMDIR_ascii && size > 0)
742 size--;
743 for(i = 0; i < size; i++)
744 g(p[i]);
745 next();
746 if (tok == ',') {
747 next();
748 } else if (tok != TOK_STR) {
749 break;
753 break;
754 case TOK_ASMDIR_text:
755 case TOK_ASMDIR_data:
756 case TOK_ASMDIR_bss:
758 char sname[64];
759 tok1 = tok;
760 n = 0;
761 next();
762 if (tok != ';' && tok != TOK_LINEFEED) {
763 n = asm_int_expr(s1);
764 next();
766 if (n)
767 sprintf(sname, "%s%d", get_tok_str(tok1, NULL), n);
768 else
769 sprintf(sname, "%s", get_tok_str(tok1, NULL));
770 use_section(s1, sname);
772 break;
773 case TOK_ASMDIR_file:
775 const char *p;
776 parse_flags &= ~PARSE_FLAG_TOK_STR;
777 next();
778 if (tok == TOK_PPNUM)
779 next();
780 if (tok == TOK_PPSTR && tokc.str.data[0] == '"') {
781 tokc.str.data[tokc.str.size - 2] = 0;
782 p = tokc.str.data + 1;
783 } else if (tok >= TOK_IDENT) {
784 p = get_tok_str(tok, &tokc);
785 } else {
786 skip_to_eol(0);
787 break;
789 tccpp_putfile(p);
790 next();
792 break;
793 case TOK_ASMDIR_ident:
795 char ident[256];
797 ident[0] = '\0';
798 next();
799 if (tok == TOK_STR)
800 pstrcat(ident, sizeof(ident), tokc.str.data);
801 else
802 pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
803 tcc_warning_c(warn_unsupported)("ignoring .ident %s", ident);
804 next();
806 break;
807 case TOK_ASMDIR_size:
809 Sym *sym;
811 next();
812 sym = asm_label_find(tok);
813 if (!sym) {
814 tcc_error("label not found: %s", get_tok_str(tok, NULL));
816 /* XXX .size name,label2-label1 */
817 tcc_warning_c(warn_unsupported)("ignoring .size %s,*", get_tok_str(tok, NULL));
818 next();
819 skip(',');
820 while (tok != TOK_LINEFEED && tok != ';' && tok != CH_EOF) {
821 next();
824 break;
825 case TOK_ASMDIR_type:
827 Sym *sym;
828 const char *newtype;
830 next();
831 sym = get_asm_sym(tok, NULL);
832 next();
833 skip(',');
834 if (tok == TOK_STR) {
835 newtype = tokc.str.data;
836 } else {
837 if (tok == '@' || tok == '%')
838 next();
839 newtype = get_tok_str(tok, NULL);
842 if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
843 sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
844 if (sym->c) {
845 ElfSym *esym = elfsym(sym);
846 esym->st_info = ELFW(ST_INFO)(ELFW(ST_BIND)(esym->st_info), STT_FUNC);
848 } else
849 tcc_warning_c(warn_unsupported)("change type of '%s' from 0x%x to '%s' ignored",
850 get_tok_str(sym->v, NULL), sym->type.t, newtype);
852 next();
854 break;
855 case TOK_ASMDIR_pushsection:
856 case TOK_ASMDIR_section:
858 char sname[256];
859 int old_nb_section = s1->nb_sections;
861 tok1 = tok;
862 /* XXX: support more options */
863 next();
864 sname[0] = '\0';
865 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
866 if (tok == TOK_STR)
867 pstrcat(sname, sizeof(sname), tokc.str.data);
868 else
869 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
870 next();
872 if (tok == ',') {
873 /* skip section options */
874 next();
875 if (tok != TOK_STR)
876 expect("string constant");
877 next();
878 if (tok == ',') {
879 next();
880 if (tok == '@' || tok == '%')
881 next();
882 next();
885 last_text_section = cur_text_section;
886 if (tok1 == TOK_ASMDIR_section) {
887 use_section(s1, sname);
888 /* The section directive supports flags, but they are unsupported.
889 For now, just assume any section contains code. */
890 cur_text_section->sh_flags |= SHF_EXECINSTR;
892 else
893 push_section(s1, sname);
894 /* If we just allocated a new section reset its alignment to
895 1. new_section normally acts for GCC compatibility and
896 sets alignment to PTR_SIZE. The assembler behaves different. */
897 if (old_nb_section != s1->nb_sections)
898 cur_text_section->sh_addralign = 1;
900 break;
901 case TOK_ASMDIR_previous:
903 Section *sec;
904 next();
905 if (!last_text_section)
906 tcc_error("no previous section referenced");
907 sec = cur_text_section;
908 use_section1(s1, last_text_section);
909 last_text_section = sec;
911 break;
912 case TOK_ASMDIR_popsection:
913 next();
914 pop_section(s1);
915 break;
916 #ifdef TCC_TARGET_I386
917 case TOK_ASMDIR_code16:
919 next();
920 s1->seg_size = 16;
922 break;
923 case TOK_ASMDIR_code32:
925 next();
926 s1->seg_size = 32;
928 break;
929 #endif
930 #ifdef TCC_TARGET_X86_64
931 /* added for compatibility with GAS */
932 case TOK_ASMDIR_code64:
933 next();
934 break;
935 #endif
936 #ifdef TCC_TARGET_RISCV64
937 case TOK_ASMDIR_option:
938 next();
939 switch(tok){
940 case TOK_ASM_rvc: /* Will be deprecated soon in favor of arch */
941 case TOK_ASM_norvc: /* Will be deprecated soon in favor of arch */
942 case TOK_ASM_pic:
943 case TOK_ASM_nopic:
944 case TOK_ASM_relax:
945 case TOK_ASM_norelax:
946 case TOK_ASM_push:
947 case TOK_ASM_pop:
948 /* TODO: unimplemented */
949 next();
950 break;
951 case TOK_ASM_arch:
952 /* TODO: unimplemented, requires extra parsing */
953 tcc_error("unimp .option '.%s'", get_tok_str(tok, NULL));
954 break;
955 default:
956 tcc_error("unknown .option '.%s'", get_tok_str(tok, NULL));
957 break;
959 break;
960 #endif
961 default:
962 tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
963 break;
968 /* assemble a file */
969 static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global)
971 int opcode;
972 int saved_parse_flags = parse_flags;
974 parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR;
975 if (do_preprocess)
976 parse_flags |= PARSE_FLAG_PREPROCESS;
977 for(;;) {
978 next();
979 if (tok == TOK_EOF)
980 break;
981 tcc_debug_line(s1);
982 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
983 redo:
984 if (tok == '#') {
985 /* horrible gas comment */
986 while (tok != TOK_LINEFEED)
987 next();
988 } else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
989 asm_parse_directive(s1, global);
990 } else if (tok == TOK_PPNUM) {
991 const char *p;
992 int n;
993 p = tokc.str.data;
994 n = strtoul(p, (char **)&p, 10);
995 if (*p != '\0')
996 expect("':'");
997 /* new local label */
998 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
999 next();
1000 skip(':');
1001 goto redo;
1002 } else if (tok >= TOK_IDENT) {
1003 /* instruction or label */
1004 opcode = tok;
1005 next();
1006 if (tok == ':') {
1007 /* new label */
1008 asm_new_label(s1, opcode, 0);
1009 next();
1010 goto redo;
1011 } else if (tok == '=') {
1012 set_symbol(s1, opcode);
1013 goto redo;
1014 } else {
1015 asm_opcode(s1, opcode);
1018 /* end of line */
1019 if (tok != ';' && tok != TOK_LINEFEED)
1020 expect("end of line");
1021 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
1024 parse_flags = saved_parse_flags;
1025 return 0;
1028 /* Assemble the current file */
1029 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
1031 int ret;
1032 tcc_debug_start(s1);
1033 /* default section is text */
1034 cur_text_section = text_section;
1035 ind = cur_text_section->data_offset;
1036 nocode_wanted = 0;
1037 ret = tcc_assemble_internal(s1, do_preprocess, 1);
1038 cur_text_section->data_offset = ind;
1039 tcc_debug_end(s1);
1040 return ret;
1043 /********************************************************************/
1044 /* GCC inline asm support */
1046 /* assemble the string 'str' in the current C compilation unit without
1047 C preprocessing. */
1048 static void tcc_assemble_inline(TCCState *s1, const char *str, int len, int global)
1050 const int *saved_macro_ptr = macro_ptr;
1051 int dotid = set_idnum('.', IS_ID);
1052 #ifndef TCC_TARGET_RISCV64
1053 int dolid = set_idnum('$', 0);
1054 #endif
1056 tcc_open_bf(s1, ":asm:", len);
1057 memcpy(file->buffer, str, len);
1058 macro_ptr = NULL;
1059 tcc_assemble_internal(s1, 0, global);
1060 tcc_close();
1062 #ifndef TCC_TARGET_RISCV64
1063 set_idnum('$', dolid);
1064 #endif
1065 set_idnum('.', dotid);
1066 macro_ptr = saved_macro_ptr;
1069 /* find a constraint by its number or id (gcc 3 extended
1070 syntax). return -1 if not found. Return in *pp in char after the
1071 constraint */
1072 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
1073 const char *name, const char **pp)
1075 int index;
1076 TokenSym *ts;
1077 const char *p;
1079 if (isnum(*name)) {
1080 index = 0;
1081 while (isnum(*name)) {
1082 index = (index * 10) + (*name) - '0';
1083 name++;
1085 if ((unsigned)index >= nb_operands)
1086 index = -1;
1087 } else if (*name == '[') {
1088 name++;
1089 p = strchr(name, ']');
1090 if (p) {
1091 ts = tok_alloc(name, p - name);
1092 for(index = 0; index < nb_operands; index++) {
1093 if (operands[index].id == ts->tok)
1094 goto found;
1096 index = -1;
1097 found:
1098 name = p + 1;
1099 } else {
1100 index = -1;
1102 } else {
1103 index = -1;
1105 if (pp)
1106 *pp = name;
1107 return index;
1110 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
1111 CString *out_str, const char *str)
1113 int c, index, modifier;
1114 ASMOperand *op;
1115 SValue sv;
1117 for(;;) {
1118 c = *str++;
1119 if (c == '%') {
1120 if (*str == '%') {
1121 str++;
1122 goto add_char;
1124 modifier = 0;
1125 if (*str == 'c' || *str == 'n' ||
1126 *str == 'b' || *str == 'w' || *str == 'h' || *str == 'k' ||
1127 *str == 'q' || *str == 'l' ||
1128 #ifdef TCC_TARGET_RISCV64
1129 *str == 'z' ||
1130 #endif
1131 /* P in GCC would add "@PLT" to symbol refs in PIC mode,
1132 and make literal operands not be decorated with '$'. */
1133 *str == 'P')
1134 modifier = *str++;
1135 index = find_constraint(operands, nb_operands, str, &str);
1136 if (index < 0)
1137 tcc_error("invalid operand reference after %%");
1138 op = &operands[index];
1139 if (modifier == 'l') {
1140 cstr_cat(out_str, get_tok_str(op->is_label, NULL), -1);
1141 } else {
1142 sv = *op->vt;
1143 if (op->reg >= 0) {
1144 sv.r = op->reg;
1145 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
1146 sv.r |= VT_LVAL;
1148 subst_asm_operand(out_str, &sv, modifier);
1150 } else {
1151 add_char:
1152 cstr_ccat(out_str, c);
1153 if (c == '\0')
1154 break;
1160 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
1161 int is_output)
1163 ASMOperand *op;
1164 int nb_operands;
1165 char* astr;
1167 if (tok != ':') {
1168 nb_operands = *nb_operands_ptr;
1169 for(;;) {
1170 if (nb_operands >= MAX_ASM_OPERANDS)
1171 tcc_error("too many asm operands");
1172 op = &operands[nb_operands++];
1173 op->id = 0;
1174 if (tok == '[') {
1175 next();
1176 if (tok < TOK_IDENT)
1177 expect("identifier");
1178 op->id = tok;
1179 next();
1180 skip(']');
1182 astr = parse_mult_str("string constant")->data;
1183 pstrcpy(op->constraint, sizeof op->constraint, astr);
1184 skip('(');
1185 gexpr();
1186 if (is_output) {
1187 if (!(vtop->type.t & VT_ARRAY))
1188 test_lvalue();
1189 } else {
1190 /* we want to avoid LLOCAL case, except when the 'm'
1191 constraint is used. Note that it may come from
1192 register storage, so we need to convert (reg)
1193 case */
1194 if ((vtop->r & VT_LVAL) &&
1195 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
1196 (vtop->r & VT_VALMASK) < VT_CONST) &&
1197 !strchr(op->constraint, 'm')) {
1198 gv(RC_INT);
1201 op->vt = vtop;
1202 skip(')');
1203 if (tok == ',') {
1204 next();
1205 } else {
1206 break;
1209 *nb_operands_ptr = nb_operands;
1213 /* parse the GCC asm() instruction */
1214 ST_FUNC void asm_instr(void)
1216 CString astr, *astr1;
1218 ASMOperand operands[MAX_ASM_OPERANDS];
1219 int nb_outputs, nb_operands, i, must_subst, out_reg, nb_labels;
1220 uint8_t clobber_regs[NB_ASM_REGS];
1221 Section *sec;
1223 /* since we always generate the asm() instruction, we can ignore
1224 volatile */
1225 while (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3
1226 || tok == TOK_GOTO) {
1227 next();
1230 astr1 = parse_asm_str();
1231 cstr_new_s(&astr);
1232 cstr_cat(&astr, astr1->data, astr1->size);
1234 nb_operands = 0;
1235 nb_outputs = 0;
1236 nb_labels = 0;
1237 must_subst = 0;
1238 memset(clobber_regs, 0, sizeof(clobber_regs));
1239 if (tok == ':') {
1240 next();
1241 must_subst = 1;
1242 /* output args */
1243 parse_asm_operands(operands, &nb_operands, 1);
1244 nb_outputs = nb_operands;
1245 if (tok == ':') {
1246 next();
1247 if (tok != ')') {
1248 /* input args */
1249 parse_asm_operands(operands, &nb_operands, 0);
1250 if (tok == ':') {
1251 /* clobber list */
1252 /* XXX: handle registers */
1253 next();
1254 for(;;) {
1255 if (tok == ':')
1256 break;
1257 if (tok != TOK_STR)
1258 expect("string constant");
1259 asm_clobber(clobber_regs, tokc.str.data);
1260 next();
1261 if (tok == ',') {
1262 next();
1263 } else {
1264 break;
1268 if (tok == ':') {
1269 /* goto labels */
1270 next();
1271 for (;;) {
1272 Sym *csym;
1273 int asmname;
1274 if (nb_operands + nb_labels >= MAX_ASM_OPERANDS)
1275 tcc_error("too many asm operands");
1276 if (tok < TOK_UIDENT)
1277 expect("label identifier");
1278 operands[nb_operands + nb_labels++].id = tok;
1280 csym = label_find(tok);
1281 if (!csym) {
1282 csym = label_push(&global_label_stack, tok,
1283 LABEL_FORWARD);
1284 } else {
1285 if (csym->r == LABEL_DECLARED)
1286 csym->r = LABEL_FORWARD;
1288 next();
1289 asmname = asm_get_prefix_name(tcc_state, "LG.",
1290 ++asmgoto_n);
1291 if (!csym->c)
1292 put_extern_sym2(csym, SHN_UNDEF, 0, 0, 1);
1293 get_asm_sym(asmname, csym);
1294 operands[nb_operands + nb_labels - 1].is_label = asmname;
1296 if (tok != ',')
1297 break;
1298 next();
1304 skip(')');
1305 /* NOTE: we do not eat the ';' so that we can restore the current
1306 token after the assembler parsing */
1307 if (tok != ';')
1308 expect("';'");
1310 /* save all values in the memory */
1311 save_regs(0);
1313 /* compute constraints */
1314 asm_compute_constraints(operands, nb_operands, nb_outputs,
1315 clobber_regs, &out_reg);
1317 /* substitute the operands in the asm string. No substitution is
1318 done if no operands (GCC behaviour) */
1319 #ifdef ASM_DEBUG
1320 printf("asm: \"%s\"\n", (char *)astr.data);
1321 #endif
1322 if (must_subst) {
1323 cstr_reset(astr1);
1324 cstr_cat(astr1, astr.data, astr.size);
1325 cstr_reset(&astr);
1326 subst_asm_operands(operands, nb_operands + nb_labels, &astr, astr1->data);
1329 #ifdef ASM_DEBUG
1330 printf("subst_asm: \"%s\"\n", (char *)astr.data);
1331 #endif
1333 /* generate loads */
1334 asm_gen_code(operands, nb_operands, nb_outputs, 0,
1335 clobber_regs, out_reg);
1337 /* We don't allow switching section within inline asm to
1338 bleed out to surrounding code. */
1339 sec = cur_text_section;
1340 /* assemble the string with tcc internal assembler */
1341 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 0);
1342 cstr_free_s(&astr);
1343 if (sec != cur_text_section) {
1344 tcc_warning("inline asm tries to change current section");
1345 use_section1(tcc_state, sec);
1348 /* restore the current C token */
1349 next();
1351 /* store the output values if needed */
1352 asm_gen_code(operands, nb_operands, nb_outputs, 1,
1353 clobber_regs, out_reg);
1355 /* free everything */
1356 for(i=0;i<nb_operands;i++) {
1357 vpop();
1362 ST_FUNC void asm_global_instr(void)
1364 CString *astr;
1365 int saved_nocode_wanted = nocode_wanted;
1367 /* Global asm blocks are always emitted. */
1368 nocode_wanted = 0;
1369 next();
1370 astr = parse_asm_str();
1371 skip(')');
1372 /* NOTE: we do not eat the ';' so that we can restore the current
1373 token after the assembler parsing */
1374 if (tok != ';')
1375 expect("';'");
1377 #ifdef ASM_DEBUG
1378 printf("asm_global: \"%s\"\n", (char *)astr.data);
1379 #endif
1380 cur_text_section = text_section;
1381 ind = cur_text_section->data_offset;
1383 /* assemble the string with tcc internal assembler */
1384 tcc_assemble_inline(tcc_state, astr->data, astr->size - 1, 1);
1386 cur_text_section->data_offset = ind;
1388 /* restore the current C token */
1389 next();
1391 nocode_wanted = saved_nocode_wanted;
1394 /********************************************************/
1395 #else
1396 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
1398 tcc_error("asm not supported");
1401 ST_FUNC void asm_instr(void)
1403 tcc_error("inline asm() not supported");
1406 ST_FUNC void asm_global_instr(void)
1408 tcc_error("inline asm() not supported");
1410 #endif /* CONFIG_TCC_ASM */