fixup! riscv: Implement large addend for global address
[tinycc.git] / tccasm.c
blobba7ffe97588580e7b14bea1c4132701745437590
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 uint8_t *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 char filename[512];
777 filename[0] = '\0';
778 next();
779 if (tok == TOK_STR)
780 pstrcat(filename, sizeof(filename), tokc.str.data);
781 else
782 pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
783 tcc_warning_c(warn_unsupported)("ignoring .file %s", filename);
784 next();
786 break;
787 case TOK_ASMDIR_ident:
789 char ident[256];
791 ident[0] = '\0';
792 next();
793 if (tok == TOK_STR)
794 pstrcat(ident, sizeof(ident), tokc.str.data);
795 else
796 pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
797 tcc_warning_c(warn_unsupported)("ignoring .ident %s", ident);
798 next();
800 break;
801 case TOK_ASMDIR_size:
803 Sym *sym;
805 next();
806 sym = asm_label_find(tok);
807 if (!sym) {
808 tcc_error("label not found: %s", get_tok_str(tok, NULL));
810 /* XXX .size name,label2-label1 */
811 tcc_warning_c(warn_unsupported)("ignoring .size %s,*", get_tok_str(tok, NULL));
812 next();
813 skip(',');
814 while (tok != TOK_LINEFEED && tok != ';' && tok != CH_EOF) {
815 next();
818 break;
819 case TOK_ASMDIR_type:
821 Sym *sym;
822 const char *newtype;
824 next();
825 sym = get_asm_sym(tok, NULL);
826 next();
827 skip(',');
828 if (tok == TOK_STR) {
829 newtype = tokc.str.data;
830 } else {
831 if (tok == '@' || tok == '%')
832 next();
833 newtype = get_tok_str(tok, NULL);
836 if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
837 sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
838 if (sym->c) {
839 ElfSym *esym = elfsym(sym);
840 esym->st_info = ELFW(ST_INFO)(ELFW(ST_BIND)(esym->st_info), STT_FUNC);
842 } else
843 tcc_warning_c(warn_unsupported)("change type of '%s' from 0x%x to '%s' ignored",
844 get_tok_str(sym->v, NULL), sym->type.t, newtype);
846 next();
848 break;
849 case TOK_ASMDIR_pushsection:
850 case TOK_ASMDIR_section:
852 char sname[256];
853 int old_nb_section = s1->nb_sections;
855 tok1 = tok;
856 /* XXX: support more options */
857 next();
858 sname[0] = '\0';
859 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
860 if (tok == TOK_STR)
861 pstrcat(sname, sizeof(sname), tokc.str.data);
862 else
863 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
864 next();
866 if (tok == ',') {
867 /* skip section options */
868 next();
869 if (tok != TOK_STR)
870 expect("string constant");
871 next();
872 if (tok == ',') {
873 next();
874 if (tok == '@' || tok == '%')
875 next();
876 next();
879 last_text_section = cur_text_section;
880 if (tok1 == TOK_ASMDIR_section)
881 use_section(s1, sname);
882 else
883 push_section(s1, sname);
884 /* If we just allocated a new section reset its alignment to
885 1. new_section normally acts for GCC compatibility and
886 sets alignment to PTR_SIZE. The assembler behaves different. */
887 if (old_nb_section != s1->nb_sections)
888 cur_text_section->sh_addralign = 1;
890 break;
891 case TOK_ASMDIR_previous:
893 Section *sec;
894 next();
895 if (!last_text_section)
896 tcc_error("no previous section referenced");
897 sec = cur_text_section;
898 use_section1(s1, last_text_section);
899 last_text_section = sec;
901 break;
902 case TOK_ASMDIR_popsection:
903 next();
904 pop_section(s1);
905 break;
906 #ifdef TCC_TARGET_I386
907 case TOK_ASMDIR_code16:
909 next();
910 s1->seg_size = 16;
912 break;
913 case TOK_ASMDIR_code32:
915 next();
916 s1->seg_size = 32;
918 break;
919 #endif
920 #ifdef TCC_TARGET_X86_64
921 /* added for compatibility with GAS */
922 case TOK_ASMDIR_code64:
923 next();
924 break;
925 #endif
926 #ifdef TCC_TARGET_RISCV64
927 case TOK_ASMDIR_option:
928 next();
929 switch(tok){
930 case TOK_ASM_rvc: /* Will be deprecated soon in favor of arch */
931 case TOK_ASM_norvc: /* Will be deprecated soon in favor of arch */
932 case TOK_ASM_pic:
933 case TOK_ASM_nopic:
934 case TOK_ASM_relax:
935 case TOK_ASM_norelax:
936 case TOK_ASM_push:
937 case TOK_ASM_pop:
938 /* TODO: unimplemented */
939 next();
940 break;
941 case TOK_ASM_arch:
942 /* TODO: unimplemented, requires extra parsing */
943 tcc_error("unimp .option '.%s'", get_tok_str(tok, NULL));
944 break;
945 default:
946 tcc_error("unknown .option '.%s'", get_tok_str(tok, NULL));
947 break;
949 break;
950 #endif
951 default:
952 tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
953 break;
958 /* assemble a file */
959 static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global)
961 int opcode;
962 int saved_parse_flags = parse_flags;
964 parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR;
965 if (do_preprocess)
966 parse_flags |= PARSE_FLAG_PREPROCESS;
967 for(;;) {
968 next();
969 if (tok == TOK_EOF)
970 break;
971 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
972 redo:
973 if (tok == '#') {
974 /* horrible gas comment */
975 while (tok != TOK_LINEFEED)
976 next();
977 } else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
978 asm_parse_directive(s1, global);
979 } else if (tok == TOK_PPNUM) {
980 const char *p;
981 int n;
982 p = tokc.str.data;
983 n = strtoul(p, (char **)&p, 10);
984 if (*p != '\0')
985 expect("':'");
986 /* new local label */
987 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
988 next();
989 skip(':');
990 goto redo;
991 } else if (tok >= TOK_IDENT) {
992 /* instruction or label */
993 opcode = tok;
994 next();
995 if (tok == ':') {
996 /* new label */
997 asm_new_label(s1, opcode, 0);
998 next();
999 goto redo;
1000 } else if (tok == '=') {
1001 set_symbol(s1, opcode);
1002 goto redo;
1003 } else {
1004 asm_opcode(s1, opcode);
1007 /* end of line */
1008 if (tok != ';' && tok != TOK_LINEFEED)
1009 expect("end of line");
1010 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
1013 parse_flags = saved_parse_flags;
1014 return 0;
1017 /* Assemble the current file */
1018 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
1020 int ret;
1021 tcc_debug_start(s1);
1022 /* default section is text */
1023 cur_text_section = text_section;
1024 ind = cur_text_section->data_offset;
1025 nocode_wanted = 0;
1026 ret = tcc_assemble_internal(s1, do_preprocess, 1);
1027 cur_text_section->data_offset = ind;
1028 tcc_debug_end(s1);
1029 return ret;
1032 /********************************************************************/
1033 /* GCC inline asm support */
1035 /* assemble the string 'str' in the current C compilation unit without
1036 C preprocessing. NOTE: str is modified by modifying the '\0' at the
1037 end */
1038 static void tcc_assemble_inline(TCCState *s1, char *str, int len, int global)
1040 const int *saved_macro_ptr = macro_ptr;
1041 int dotid = set_idnum('.', IS_ID);
1042 #ifndef TCC_TARGET_RISCV64
1043 int dolid = set_idnum('$', 0);
1044 #endif
1046 tcc_open_bf(s1, ":asm:", len);
1047 memcpy(file->buffer, str, len);
1048 macro_ptr = NULL;
1049 tcc_assemble_internal(s1, 0, global);
1050 tcc_close();
1052 #ifndef TCC_TARGET_RISCV64
1053 set_idnum('$', dolid);
1054 #endif
1055 set_idnum('.', dotid);
1056 macro_ptr = saved_macro_ptr;
1059 /* find a constraint by its number or id (gcc 3 extended
1060 syntax). return -1 if not found. Return in *pp in char after the
1061 constraint */
1062 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
1063 const char *name, const char **pp)
1065 int index;
1066 TokenSym *ts;
1067 const char *p;
1069 if (isnum(*name)) {
1070 index = 0;
1071 while (isnum(*name)) {
1072 index = (index * 10) + (*name) - '0';
1073 name++;
1075 if ((unsigned)index >= nb_operands)
1076 index = -1;
1077 } else if (*name == '[') {
1078 name++;
1079 p = strchr(name, ']');
1080 if (p) {
1081 ts = tok_alloc(name, p - name);
1082 for(index = 0; index < nb_operands; index++) {
1083 if (operands[index].id == ts->tok)
1084 goto found;
1086 index = -1;
1087 found:
1088 name = p + 1;
1089 } else {
1090 index = -1;
1092 } else {
1093 index = -1;
1095 if (pp)
1096 *pp = name;
1097 return index;
1100 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
1101 CString *out_str, const char *str)
1103 int c, index, modifier;
1104 ASMOperand *op;
1105 SValue sv;
1107 for(;;) {
1108 c = *str++;
1109 if (c == '%') {
1110 if (*str == '%') {
1111 str++;
1112 goto add_char;
1114 modifier = 0;
1115 if (*str == 'c' || *str == 'n' ||
1116 *str == 'b' || *str == 'w' || *str == 'h' || *str == 'k' ||
1117 *str == 'q' || *str == 'l' ||
1118 #ifdef TCC_TARGET_RISCV64
1119 *str == 'z' ||
1120 #endif
1121 /* P in GCC would add "@PLT" to symbol refs in PIC mode,
1122 and make literal operands not be decorated with '$'. */
1123 *str == 'P')
1124 modifier = *str++;
1125 index = find_constraint(operands, nb_operands, str, &str);
1126 if (index < 0)
1127 tcc_error("invalid operand reference after %%");
1128 op = &operands[index];
1129 if (modifier == 'l') {
1130 cstr_cat(out_str, get_tok_str(op->is_label, NULL), -1);
1131 } else {
1132 sv = *op->vt;
1133 if (op->reg >= 0) {
1134 sv.r = op->reg;
1135 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
1136 sv.r |= VT_LVAL;
1138 subst_asm_operand(out_str, &sv, modifier);
1140 } else {
1141 add_char:
1142 cstr_ccat(out_str, c);
1143 if (c == '\0')
1144 break;
1150 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
1151 int is_output)
1153 ASMOperand *op;
1154 int nb_operands;
1155 char* astr;
1157 if (tok != ':') {
1158 nb_operands = *nb_operands_ptr;
1159 for(;;) {
1160 if (nb_operands >= MAX_ASM_OPERANDS)
1161 tcc_error("too many asm operands");
1162 op = &operands[nb_operands++];
1163 op->id = 0;
1164 if (tok == '[') {
1165 next();
1166 if (tok < TOK_IDENT)
1167 expect("identifier");
1168 op->id = tok;
1169 next();
1170 skip(']');
1172 astr = parse_mult_str("string constant")->data;
1173 pstrcpy(op->constraint, sizeof op->constraint, astr);
1174 skip('(');
1175 gexpr();
1176 if (is_output) {
1177 if (!(vtop->type.t & VT_ARRAY))
1178 test_lvalue();
1179 } else {
1180 /* we want to avoid LLOCAL case, except when the 'm'
1181 constraint is used. Note that it may come from
1182 register storage, so we need to convert (reg)
1183 case */
1184 if ((vtop->r & VT_LVAL) &&
1185 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
1186 (vtop->r & VT_VALMASK) < VT_CONST) &&
1187 !strchr(op->constraint, 'm')) {
1188 gv(RC_INT);
1191 op->vt = vtop;
1192 skip(')');
1193 if (tok == ',') {
1194 next();
1195 } else {
1196 break;
1199 *nb_operands_ptr = nb_operands;
1203 /* parse the GCC asm() instruction */
1204 ST_FUNC void asm_instr(void)
1206 CString astr, *astr1;
1208 ASMOperand operands[MAX_ASM_OPERANDS];
1209 int nb_outputs, nb_operands, i, must_subst, out_reg, nb_labels;
1210 uint8_t clobber_regs[NB_ASM_REGS];
1211 Section *sec;
1213 /* since we always generate the asm() instruction, we can ignore
1214 volatile */
1215 while (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3
1216 || tok == TOK_GOTO) {
1217 next();
1220 astr1 = parse_asm_str();
1221 cstr_new_s(&astr);
1222 cstr_cat(&astr, astr1->data, astr1->size);
1224 nb_operands = 0;
1225 nb_outputs = 0;
1226 nb_labels = 0;
1227 must_subst = 0;
1228 memset(clobber_regs, 0, sizeof(clobber_regs));
1229 if (tok == ':') {
1230 next();
1231 must_subst = 1;
1232 /* output args */
1233 parse_asm_operands(operands, &nb_operands, 1);
1234 nb_outputs = nb_operands;
1235 if (tok == ':') {
1236 next();
1237 if (tok != ')') {
1238 /* input args */
1239 parse_asm_operands(operands, &nb_operands, 0);
1240 if (tok == ':') {
1241 /* clobber list */
1242 /* XXX: handle registers */
1243 next();
1244 for(;;) {
1245 if (tok == ':')
1246 break;
1247 if (tok != TOK_STR)
1248 expect("string constant");
1249 asm_clobber(clobber_regs, tokc.str.data);
1250 next();
1251 if (tok == ',') {
1252 next();
1253 } else {
1254 break;
1258 if (tok == ':') {
1259 /* goto labels */
1260 next();
1261 for (;;) {
1262 Sym *csym;
1263 int asmname;
1264 if (nb_operands + nb_labels >= MAX_ASM_OPERANDS)
1265 tcc_error("too many asm operands");
1266 if (tok < TOK_UIDENT)
1267 expect("label identifier");
1268 operands[nb_operands + nb_labels++].id = tok;
1270 csym = label_find(tok);
1271 if (!csym) {
1272 csym = label_push(&global_label_stack, tok,
1273 LABEL_FORWARD);
1274 } else {
1275 if (csym->r == LABEL_DECLARED)
1276 csym->r = LABEL_FORWARD;
1278 next();
1279 asmname = asm_get_prefix_name(tcc_state, "LG.",
1280 ++asmgoto_n);
1281 if (!csym->c)
1282 put_extern_sym2(csym, SHN_UNDEF, 0, 0, 1);
1283 get_asm_sym(asmname, csym);
1284 operands[nb_operands + nb_labels - 1].is_label = asmname;
1286 if (tok != ',')
1287 break;
1288 next();
1294 skip(')');
1295 /* NOTE: we do not eat the ';' so that we can restore the current
1296 token after the assembler parsing */
1297 if (tok != ';')
1298 expect("';'");
1300 /* save all values in the memory */
1301 save_regs(0);
1303 /* compute constraints */
1304 asm_compute_constraints(operands, nb_operands, nb_outputs,
1305 clobber_regs, &out_reg);
1307 /* substitute the operands in the asm string. No substitution is
1308 done if no operands (GCC behaviour) */
1309 #ifdef ASM_DEBUG
1310 printf("asm: \"%s\"\n", (char *)astr.data);
1311 #endif
1312 if (must_subst) {
1313 cstr_reset(astr1);
1314 cstr_cat(astr1, astr.data, astr.size);
1315 cstr_reset(&astr);
1316 subst_asm_operands(operands, nb_operands + nb_labels, &astr, astr1->data);
1319 #ifdef ASM_DEBUG
1320 printf("subst_asm: \"%s\"\n", (char *)astr.data);
1321 #endif
1323 /* generate loads */
1324 asm_gen_code(operands, nb_operands, nb_outputs, 0,
1325 clobber_regs, out_reg);
1327 /* We don't allow switching section within inline asm to
1328 bleed out to surrounding code. */
1329 sec = cur_text_section;
1330 /* assemble the string with tcc internal assembler */
1331 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 0);
1332 cstr_free_s(&astr);
1333 if (sec != cur_text_section) {
1334 tcc_warning("inline asm tries to change current section");
1335 use_section1(tcc_state, sec);
1338 /* restore the current C token */
1339 next();
1341 /* store the output values if needed */
1342 asm_gen_code(operands, nb_operands, nb_outputs, 1,
1343 clobber_regs, out_reg);
1345 /* free everything */
1346 for(i=0;i<nb_operands;i++) {
1347 vpop();
1352 ST_FUNC void asm_global_instr(void)
1354 CString *astr;
1355 int saved_nocode_wanted = nocode_wanted;
1357 /* Global asm blocks are always emitted. */
1358 nocode_wanted = 0;
1359 next();
1360 astr = parse_asm_str();
1361 skip(')');
1362 /* NOTE: we do not eat the ';' so that we can restore the current
1363 token after the assembler parsing */
1364 if (tok != ';')
1365 expect("';'");
1367 #ifdef ASM_DEBUG
1368 printf("asm_global: \"%s\"\n", (char *)astr.data);
1369 #endif
1370 cur_text_section = text_section;
1371 ind = cur_text_section->data_offset;
1373 /* assemble the string with tcc internal assembler */
1374 tcc_assemble_inline(tcc_state, astr->data, astr->size - 1, 1);
1376 cur_text_section->data_offset = ind;
1378 /* restore the current C token */
1379 next();
1381 nocode_wanted = saved_nocode_wanted;
1384 /********************************************************/
1385 #else
1386 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
1388 tcc_error("asm not supported");
1391 ST_FUNC void asm_instr(void)
1393 tcc_error("inline asm() not supported");
1396 ST_FUNC void asm_global_instr(void)
1398 tcc_error("inline asm() not supported");
1400 #endif /* CONFIG_TCC_ASM */