tccgen.c: Try to make sizeof(!x) work.
[tinycc.git] / tccasm.c
blob044db8345d3a330abb33192998e5696d5b74fcfd
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 #include "tcc.h"
22 #ifdef CONFIG_TCC_ASM
24 ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n)
26 char buf[64];
27 TokenSym *ts;
29 snprintf(buf, sizeof(buf), "L..%u", n);
30 ts = tok_alloc(buf, strlen(buf));
31 return ts->tok;
34 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
36 /* We do not use the C expression parser to handle symbols. Maybe the
37 C expression parser could be tweaked to do so. */
39 static void asm_expr_unary(TCCState *s1, ExprValue *pe)
41 Sym *sym;
42 int op, n, label;
43 const char *p;
45 switch(tok) {
46 case TOK_PPNUM:
47 p = tokc.cstr->data;
48 n = strtoul(p, (char **)&p, 0);
49 if (*p == 'b' || *p == 'f') {
50 /* backward or forward label */
51 label = asm_get_local_label_name(s1, n);
52 sym = label_find(label);
53 if (*p == 'b') {
54 /* backward : find the last corresponding defined label */
55 if (sym && sym->r == 0)
56 sym = sym->prev_tok;
57 if (!sym)
58 tcc_error("local label '%d' not found backward", n);
59 } else {
60 /* forward */
61 if (!sym || sym->r) {
62 /* if the last label is defined, then define a new one */
63 sym = label_push(&s1->asm_labels, label, 0);
64 sym->type.t = VT_STATIC | VT_VOID;
67 pe->v = 0;
68 pe->sym = sym;
69 } else if (*p == '\0') {
70 pe->v = n;
71 pe->sym = NULL;
72 } else {
73 tcc_error("invalid number syntax");
75 next();
76 break;
77 case '+':
78 next();
79 asm_expr_unary(s1, pe);
80 break;
81 case '-':
82 case '~':
83 op = tok;
84 next();
85 asm_expr_unary(s1, pe);
86 if (pe->sym)
87 tcc_error("invalid operation with label");
88 if (op == '-')
89 pe->v = -pe->v;
90 else
91 pe->v = ~pe->v;
92 break;
93 case TOK_CCHAR:
94 case TOK_LCHAR:
95 pe->v = tokc.i;
96 pe->sym = NULL;
97 next();
98 break;
99 case '(':
100 next();
101 asm_expr(s1, pe);
102 skip(')');
103 break;
104 default:
105 if (tok >= TOK_IDENT) {
106 /* label case : if the label was not found, add one */
107 sym = label_find(tok);
108 if (!sym) {
109 sym = label_push(&s1->asm_labels, tok, 0);
110 /* NOTE: by default, the symbol is global */
111 sym->type.t = VT_VOID;
113 if (sym->r == SHN_ABS) {
114 /* if absolute symbol, no need to put a symbol value */
115 pe->v = sym->jnext;
116 pe->sym = NULL;
117 } else {
118 pe->v = 0;
119 pe->sym = sym;
121 next();
122 } else {
123 tcc_error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
125 break;
129 static void asm_expr_prod(TCCState *s1, ExprValue *pe)
131 int op;
132 ExprValue e2;
134 asm_expr_unary(s1, pe);
135 for(;;) {
136 op = tok;
137 if (op != '*' && op != '/' && op != '%' &&
138 op != TOK_SHL && op != TOK_SAR)
139 break;
140 next();
141 asm_expr_unary(s1, &e2);
142 if (pe->sym || e2.sym)
143 tcc_error("invalid operation with label");
144 switch(op) {
145 case '*':
146 pe->v *= e2.v;
147 break;
148 case '/':
149 if (e2.v == 0) {
150 div_error:
151 tcc_error("division by zero");
153 pe->v /= e2.v;
154 break;
155 case '%':
156 if (e2.v == 0)
157 goto div_error;
158 pe->v %= e2.v;
159 break;
160 case TOK_SHL:
161 pe->v <<= e2.v;
162 break;
163 default:
164 case TOK_SAR:
165 pe->v >>= e2.v;
166 break;
171 static void asm_expr_logic(TCCState *s1, ExprValue *pe)
173 int op;
174 ExprValue e2;
176 asm_expr_prod(s1, pe);
177 for(;;) {
178 op = tok;
179 if (op != '&' && op != '|' && op != '^')
180 break;
181 next();
182 asm_expr_prod(s1, &e2);
183 if (pe->sym || e2.sym)
184 tcc_error("invalid operation with label");
185 switch(op) {
186 case '&':
187 pe->v &= e2.v;
188 break;
189 case '|':
190 pe->v |= e2.v;
191 break;
192 default:
193 case '^':
194 pe->v ^= e2.v;
195 break;
200 static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
202 int op;
203 ExprValue e2;
205 asm_expr_logic(s1, pe);
206 for(;;) {
207 op = tok;
208 if (op != '+' && op != '-')
209 break;
210 next();
211 asm_expr_logic(s1, &e2);
212 if (op == '+') {
213 if (pe->sym != NULL && e2.sym != NULL)
214 goto cannot_relocate;
215 pe->v += e2.v;
216 if (pe->sym == NULL && e2.sym != NULL)
217 pe->sym = e2.sym;
218 } else {
219 pe->v -= e2.v;
220 /* NOTE: we are less powerful than gas in that case
221 because we store only one symbol in the expression */
222 if (!pe->sym && !e2.sym) {
223 /* OK */
224 } else if (pe->sym && !e2.sym) {
225 /* OK */
226 } else if (pe->sym && e2.sym) {
227 if (pe->sym == e2.sym) {
228 /* OK */
229 } else if (pe->sym->r == e2.sym->r && pe->sym->r != 0) {
230 /* we also accept defined symbols in the same section */
231 pe->v += pe->sym->jnext - e2.sym->jnext;
232 } else {
233 goto cannot_relocate;
235 pe->sym = NULL; /* same symbols can be subtracted to NULL */
236 } else {
237 cannot_relocate:
238 tcc_error("invalid operation with label");
244 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
246 asm_expr_sum(s1, pe);
249 ST_FUNC int asm_int_expr(TCCState *s1)
251 ExprValue e;
252 asm_expr(s1, &e);
253 if (e.sym)
254 expect("constant");
255 return e.v;
258 /* NOTE: the same name space as C labels is used to avoid using too
259 much memory when storing labels in TokenStrings */
260 static void asm_new_label1(TCCState *s1, int label, int is_local,
261 int sh_num, int value)
263 Sym *sym;
265 sym = label_find(label);
266 if (sym) {
267 if (sym->r) {
268 /* the label is already defined */
269 if (!is_local) {
270 tcc_error("assembler label '%s' already defined",
271 get_tok_str(label, NULL));
272 } else {
273 /* redefinition of local labels is possible */
274 goto new_label;
277 } else {
278 new_label:
279 sym = label_push(&s1->asm_labels, label, 0);
280 sym->type.t = VT_STATIC | VT_VOID;
282 sym->r = sh_num;
283 sym->jnext = value;
286 static void asm_new_label(TCCState *s1, int label, int is_local)
288 asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
291 static void asm_free_labels(TCCState *st)
293 Sym *s, *s1;
294 Section *sec;
296 for(s = st->asm_labels; s != NULL; s = s1) {
297 s1 = s->prev;
298 /* define symbol value in object file */
299 if (s->r) {
300 if (s->r == SHN_ABS)
301 sec = SECTION_ABS;
302 else
303 sec = st->sections[s->r];
304 put_extern_sym2(s, sec, s->jnext, 0, 0);
306 /* remove label */
307 table_ident[s->v - TOK_IDENT]->sym_label = NULL;
308 sym_free(s);
310 st->asm_labels = NULL;
313 static void use_section1(TCCState *s1, Section *sec)
315 cur_text_section->data_offset = ind;
316 cur_text_section = sec;
317 ind = cur_text_section->data_offset;
320 static void use_section(TCCState *s1, const char *name)
322 Section *sec;
323 sec = find_section(s1, name);
324 use_section1(s1, sec);
327 static void asm_parse_directive(TCCState *s1)
329 int n, offset, v, size, tok1;
330 Section *sec;
331 uint8_t *ptr;
333 /* assembler directive */
334 next();
335 sec = cur_text_section;
336 switch(tok) {
337 case TOK_ASM_align:
338 case TOK_ASM_p2align:
339 case TOK_ASM_skip:
340 case TOK_ASM_space:
341 tok1 = tok;
342 next();
343 n = asm_int_expr(s1);
344 if (tok1 == TOK_ASM_p2align)
346 if (n < 0 || n > 30)
347 tcc_error("invalid p2align, must be between 0 and 30");
348 n = 1 << n;
349 tok1 = TOK_ASM_align;
351 if (tok1 == TOK_ASM_align) {
352 if (n < 0 || (n & (n-1)) != 0)
353 tcc_error("alignment must be a positive power of two");
354 offset = (ind + n - 1) & -n;
355 size = offset - ind;
356 /* the section must have a compatible alignment */
357 if (sec->sh_addralign < n)
358 sec->sh_addralign = n;
359 } else {
360 size = n;
362 v = 0;
363 if (tok == ',') {
364 next();
365 v = asm_int_expr(s1);
367 zero_pad:
368 if (sec->sh_type != SHT_NOBITS) {
369 sec->data_offset = ind;
370 ptr = section_ptr_add(sec, size);
371 memset(ptr, v, size);
373 ind += size;
374 break;
375 case TOK_ASM_quad:
376 next();
377 for(;;) {
378 uint64_t vl;
379 const char *p;
381 p = tokc.cstr->data;
382 if (tok != TOK_PPNUM) {
383 error_constant:
384 tcc_error("64 bit constant");
386 vl = strtoll(p, (char **)&p, 0);
387 if (*p != '\0')
388 goto error_constant;
389 next();
390 if (sec->sh_type != SHT_NOBITS) {
391 /* XXX: endianness */
392 gen_le32(vl);
393 gen_le32(vl >> 32);
394 } else {
395 ind += 8;
397 if (tok != ',')
398 break;
399 next();
401 break;
402 case TOK_ASM_byte:
403 size = 1;
404 goto asm_data;
405 case TOK_ASM_word:
406 case TOK_SHORT:
407 size = 2;
408 goto asm_data;
409 case TOK_LONG:
410 case TOK_INT:
411 size = 4;
412 asm_data:
413 next();
414 for(;;) {
415 ExprValue e;
416 asm_expr(s1, &e);
417 if (sec->sh_type != SHT_NOBITS) {
418 if (size == 4) {
419 gen_expr32(&e);
420 } else {
421 if (e.sym)
422 expect("constant");
423 if (size == 1)
424 g(e.v);
425 else
426 gen_le16(e.v);
428 } else {
429 ind += size;
431 if (tok != ',')
432 break;
433 next();
435 break;
436 case TOK_ASM_fill:
438 int repeat, size, val, i, j;
439 uint8_t repeat_buf[8];
440 next();
441 repeat = asm_int_expr(s1);
442 if (repeat < 0) {
443 tcc_error("repeat < 0; .fill ignored");
444 break;
446 size = 1;
447 val = 0;
448 if (tok == ',') {
449 next();
450 size = asm_int_expr(s1);
451 if (size < 0) {
452 tcc_error("size < 0; .fill ignored");
453 break;
455 if (size > 8)
456 size = 8;
457 if (tok == ',') {
458 next();
459 val = asm_int_expr(s1);
462 /* XXX: endianness */
463 repeat_buf[0] = val;
464 repeat_buf[1] = val >> 8;
465 repeat_buf[2] = val >> 16;
466 repeat_buf[3] = val >> 24;
467 repeat_buf[4] = 0;
468 repeat_buf[5] = 0;
469 repeat_buf[6] = 0;
470 repeat_buf[7] = 0;
471 for(i = 0; i < repeat; i++) {
472 for(j = 0; j < size; j++) {
473 g(repeat_buf[j]);
477 break;
478 case TOK_ASM_org:
480 unsigned long n;
481 next();
482 /* XXX: handle section symbols too */
483 n = asm_int_expr(s1);
484 if (n < ind)
485 tcc_error("attempt to .org backwards");
486 v = 0;
487 size = n - ind;
488 goto zero_pad;
490 break;
491 case TOK_ASM_globl:
492 case TOK_ASM_global:
493 case TOK_ASM_weak:
494 case TOK_ASM_hidden:
495 tok1 = tok;
496 do {
497 Sym *sym;
499 next();
500 sym = label_find(tok);
501 if (!sym) {
502 sym = label_push(&s1->asm_labels, tok, 0);
503 sym->type.t = VT_VOID;
505 if (tok1 != TOK_ASM_hidden)
506 sym->type.t &= ~VT_STATIC;
507 if (tok1 == TOK_ASM_weak)
508 sym->type.t |= VT_WEAK;
509 else if (tok1 == TOK_ASM_hidden)
510 sym->type.t |= STV_HIDDEN << VT_VIS_SHIFT;
511 next();
512 } while (tok == ',');
513 break;
514 case TOK_ASM_string:
515 case TOK_ASM_ascii:
516 case TOK_ASM_asciz:
518 const uint8_t *p;
519 int i, size, t;
521 t = tok;
522 next();
523 for(;;) {
524 if (tok != TOK_STR)
525 expect("string constant");
526 p = tokc.cstr->data;
527 size = tokc.cstr->size;
528 if (t == TOK_ASM_ascii && size > 0)
529 size--;
530 for(i = 0; i < size; i++)
531 g(p[i]);
532 next();
533 if (tok == ',') {
534 next();
535 } else if (tok != TOK_STR) {
536 break;
540 break;
541 case TOK_ASM_text:
542 case TOK_ASM_data:
543 case TOK_ASM_bss:
545 char sname[64];
546 tok1 = tok;
547 n = 0;
548 next();
549 if (tok != ';' && tok != TOK_LINEFEED) {
550 n = asm_int_expr(s1);
551 next();
553 if (n)
554 sprintf(sname, ".%s%d", get_tok_str(tok1, NULL), n);
555 else
556 sprintf(sname, ".%s", get_tok_str(tok1, NULL));
557 use_section(s1, sname);
559 break;
560 case TOK_ASM_file:
562 char filename[512];
564 filename[0] = '\0';
565 next();
567 if (tok == TOK_STR)
568 pstrcat(filename, sizeof(filename), tokc.cstr->data);
569 else
570 pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
572 if (s1->warn_unsupported)
573 tcc_warning("ignoring .file %s", filename);
575 next();
577 break;
578 case TOK_ASM_ident:
580 char ident[256];
582 ident[0] = '\0';
583 next();
585 if (tok == TOK_STR)
586 pstrcat(ident, sizeof(ident), tokc.cstr->data);
587 else
588 pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
590 if (s1->warn_unsupported)
591 tcc_warning("ignoring .ident %s", ident);
593 next();
595 break;
596 case TOK_ASM_size:
598 Sym *sym;
600 next();
601 sym = label_find(tok);
602 if (!sym) {
603 tcc_error("label not found: %s", get_tok_str(tok, NULL));
606 /* XXX .size name,label2-label1 */
607 if (s1->warn_unsupported)
608 tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL));
610 next();
611 skip(',');
612 while (tok != '\n' && tok != CH_EOF) {
613 next();
616 break;
617 case TOK_ASM_type:
619 Sym *sym;
620 const char *newtype;
622 next();
623 sym = label_find(tok);
624 if (!sym) {
625 sym = label_push(&s1->asm_labels, tok, 0);
626 sym->type.t = VT_VOID;
629 next();
630 skip(',');
631 if (tok == TOK_STR) {
632 newtype = tokc.cstr->data;
633 } else {
634 if (tok == '@' || tok == '%')
635 skip(tok);
636 newtype = get_tok_str(tok, NULL);
639 if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
640 sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
642 else if (s1->warn_unsupported)
643 tcc_warning("change type of '%s' from 0x%x to '%s' ignored",
644 get_tok_str(sym->v, NULL), sym->type.t, newtype);
646 next();
648 break;
649 case TOK_SECTION1:
651 char sname[256];
653 /* XXX: support more options */
654 next();
655 sname[0] = '\0';
656 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
657 if (tok == TOK_STR)
658 pstrcat(sname, sizeof(sname), tokc.cstr->data);
659 else
660 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
661 next();
663 if (tok == ',') {
664 /* skip section options */
665 next();
666 if (tok != TOK_STR)
667 expect("string constant");
668 next();
670 last_text_section = cur_text_section;
671 use_section(s1, sname);
673 break;
674 case TOK_ASM_previous:
676 Section *sec;
677 next();
678 if (!last_text_section)
679 tcc_error("no previous section referenced");
680 sec = cur_text_section;
681 use_section1(s1, last_text_section);
682 last_text_section = sec;
684 break;
685 #ifdef TCC_TARGET_I386
686 case TOK_ASM_code16:
688 next();
689 s1->seg_size = 16;
691 break;
692 case TOK_ASM_code32:
694 next();
695 s1->seg_size = 32;
697 break;
698 #endif
699 #ifdef TCC_TARGET_X86_64
700 /* added for compatibility with GAS */
701 case TOK_ASM_code64:
702 next();
703 break;
704 #endif
705 default:
706 tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
707 break;
712 /* assemble a file */
713 static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
715 int opcode;
717 #if 0
718 /* print stats about opcodes */
720 const ASMInstr *pa;
721 int freq[4];
722 int op_vals[500];
723 int nb_op_vals, i, j;
725 nb_op_vals = 0;
726 memset(freq, 0, sizeof(freq));
727 for(pa = asm_instrs; pa->sym != 0; pa++) {
728 freq[pa->nb_ops]++;
729 for(i=0;i<pa->nb_ops;i++) {
730 for(j=0;j<nb_op_vals;j++) {
731 if (pa->op_type[i] == op_vals[j])
732 goto found;
734 op_vals[nb_op_vals++] = pa->op_type[i];
735 found: ;
738 for(i=0;i<nb_op_vals;i++) {
739 int v = op_vals[i];
740 if ((v & (v - 1)) != 0)
741 printf("%3d: %08x\n", i, v);
743 printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
744 sizeof(asm_instrs), sizeof(asm_instrs) / sizeof(ASMInstr),
745 freq[0], freq[1], freq[2], freq[3]);
747 #endif
749 /* XXX: undefine C labels */
751 ch = file->buf_ptr[0];
752 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
753 parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR;
754 if (do_preprocess)
755 parse_flags |= PARSE_FLAG_PREPROCESS;
756 next();
757 for(;;) {
758 if (tok == TOK_EOF)
759 break;
760 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
761 redo:
762 if (tok == '#') {
763 /* horrible gas comment */
764 while (tok != TOK_LINEFEED)
765 next();
766 } else if (tok == '.') {
767 asm_parse_directive(s1);
768 } else if (tok == TOK_PPNUM) {
769 const char *p;
770 int n;
771 p = tokc.cstr->data;
772 n = strtoul(p, (char **)&p, 10);
773 if (*p != '\0')
774 expect("':'");
775 /* new local label */
776 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
777 next();
778 skip(':');
779 goto redo;
780 } else if (tok >= TOK_IDENT) {
781 /* instruction or label */
782 opcode = tok;
783 next();
784 if (tok == ':') {
785 /* new label */
786 asm_new_label(s1, opcode, 0);
787 next();
788 goto redo;
789 } else if (tok == '=') {
790 int n;
791 next();
792 n = asm_int_expr(s1);
793 asm_new_label1(s1, opcode, 0, SHN_ABS, n);
794 goto redo;
795 } else {
796 asm_opcode(s1, opcode);
799 /* end of line */
800 if (tok != ';' && tok != TOK_LINEFEED){
801 expect("end of line");
803 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
804 next();
807 asm_free_labels(s1);
809 return 0;
812 /* Assemble the current file */
813 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
815 Sym *define_start;
816 int ret;
818 preprocess_init(s1);
820 /* default section is text */
821 cur_text_section = text_section;
822 ind = cur_text_section->data_offset;
824 define_start = define_stack;
826 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
827 symbols can be safely used */
828 put_elf_sym(symtab_section, 0, 0,
829 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
830 SHN_ABS, file->filename);
832 ret = tcc_assemble_internal(s1, do_preprocess);
834 cur_text_section->data_offset = ind;
836 free_defines(define_start);
838 return ret;
841 /********************************************************************/
842 /* GCC inline asm support */
844 /* assemble the string 'str' in the current C compilation unit without
845 C preprocessing. NOTE: str is modified by modifying the '\0' at the
846 end */
847 static void tcc_assemble_inline(TCCState *s1, char *str, int len)
849 int saved_parse_flags;
850 const int *saved_macro_ptr;
852 saved_parse_flags = parse_flags;
853 saved_macro_ptr = macro_ptr;
855 tcc_open_bf(s1, ":asm:", len);
856 memcpy(file->buffer, str, len);
858 macro_ptr = NULL;
859 tcc_assemble_internal(s1, 0);
860 tcc_close();
862 parse_flags = saved_parse_flags;
863 macro_ptr = saved_macro_ptr;
866 /* find a constraint by its number or id (gcc 3 extended
867 syntax). return -1 if not found. Return in *pp in char after the
868 constraint */
869 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
870 const char *name, const char **pp)
872 int index;
873 TokenSym *ts;
874 const char *p;
876 if (isnum(*name)) {
877 index = 0;
878 while (isnum(*name)) {
879 index = (index * 10) + (*name) - '0';
880 name++;
882 if ((unsigned)index >= nb_operands)
883 index = -1;
884 } else if (*name == '[') {
885 name++;
886 p = strchr(name, ']');
887 if (p) {
888 ts = tok_alloc(name, p - name);
889 for(index = 0; index < nb_operands; index++) {
890 if (operands[index].id == ts->tok)
891 goto found;
893 index = -1;
894 found:
895 name = p + 1;
896 } else {
897 index = -1;
899 } else {
900 index = -1;
902 if (pp)
903 *pp = name;
904 return index;
907 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
908 int nb_outputs,
909 CString *out_str, CString *in_str)
911 int c, index, modifier;
912 const char *str;
913 ASMOperand *op;
914 SValue sv;
916 cstr_new(out_str);
917 str = in_str->data;
918 for(;;) {
919 c = *str++;
920 if (c == '%') {
921 if (*str == '%') {
922 str++;
923 goto add_char;
925 modifier = 0;
926 if (*str == 'c' || *str == 'n' ||
927 *str == 'b' || *str == 'w' || *str == 'h')
928 modifier = *str++;
929 index = find_constraint(operands, nb_operands, str, &str);
930 if (index < 0)
931 tcc_error("invalid operand reference after %%");
932 op = &operands[index];
933 sv = *op->vt;
934 if (op->reg >= 0) {
935 sv.r = op->reg;
936 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
937 sv.r |= VT_LVAL;
939 subst_asm_operand(out_str, &sv, modifier);
940 } else {
941 add_char:
942 cstr_ccat(out_str, c);
943 if (c == '\0')
944 break;
950 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
951 int is_output)
953 ASMOperand *op;
954 int nb_operands;
956 if (tok != ':') {
957 nb_operands = *nb_operands_ptr;
958 for(;;) {
959 if (nb_operands >= MAX_ASM_OPERANDS)
960 tcc_error("too many asm operands");
961 op = &operands[nb_operands++];
962 op->id = 0;
963 if (tok == '[') {
964 next();
965 if (tok < TOK_IDENT)
966 expect("identifier");
967 op->id = tok;
968 next();
969 skip(']');
971 if (tok != TOK_STR)
972 expect("string constant");
973 op->constraint = tcc_malloc(tokc.cstr->size);
974 strcpy(op->constraint, tokc.cstr->data);
975 next();
976 skip('(');
977 gexpr();
978 if (is_output) {
979 test_lvalue();
980 } else {
981 /* we want to avoid LLOCAL case, except when the 'm'
982 constraint is used. Note that it may come from
983 register storage, so we need to convert (reg)
984 case */
985 if ((vtop->r & VT_LVAL) &&
986 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
987 (vtop->r & VT_VALMASK) < VT_CONST) &&
988 !strchr(op->constraint, 'm')) {
989 gv(RC_INT);
992 op->vt = vtop;
993 skip(')');
994 if (tok == ',') {
995 next();
996 } else {
997 break;
1000 *nb_operands_ptr = nb_operands;
1004 /* parse the GCC asm() instruction */
1005 ST_FUNC void asm_instr(void)
1007 CString astr, astr1;
1008 ASMOperand operands[MAX_ASM_OPERANDS];
1009 int nb_outputs, nb_operands, i, must_subst, out_reg;
1010 uint8_t clobber_regs[NB_ASM_REGS];
1012 next();
1013 /* since we always generate the asm() instruction, we can ignore
1014 volatile */
1015 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
1016 next();
1018 parse_asm_str(&astr);
1019 nb_operands = 0;
1020 nb_outputs = 0;
1021 must_subst = 0;
1022 memset(clobber_regs, 0, sizeof(clobber_regs));
1023 if (tok == ':') {
1024 next();
1025 must_subst = 1;
1026 /* output args */
1027 parse_asm_operands(operands, &nb_operands, 1);
1028 nb_outputs = nb_operands;
1029 if (tok == ':') {
1030 next();
1031 if (tok != ')') {
1032 /* input args */
1033 parse_asm_operands(operands, &nb_operands, 0);
1034 if (tok == ':') {
1035 /* clobber list */
1036 /* XXX: handle registers */
1037 next();
1038 for(;;) {
1039 if (tok != TOK_STR)
1040 expect("string constant");
1041 asm_clobber(clobber_regs, tokc.cstr->data);
1042 next();
1043 if (tok == ',') {
1044 next();
1045 } else {
1046 break;
1053 skip(')');
1054 /* NOTE: we do not eat the ';' so that we can restore the current
1055 token after the assembler parsing */
1056 if (tok != ';')
1057 expect("';'");
1059 /* save all values in the memory */
1060 save_regs(0);
1062 /* compute constraints */
1063 asm_compute_constraints(operands, nb_operands, nb_outputs,
1064 clobber_regs, &out_reg);
1066 /* substitute the operands in the asm string. No substitution is
1067 done if no operands (GCC behaviour) */
1068 #ifdef ASM_DEBUG
1069 printf("asm: \"%s\"\n", (char *)astr.data);
1070 #endif
1071 if (must_subst) {
1072 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
1073 cstr_free(&astr);
1074 } else {
1075 astr1 = astr;
1077 #ifdef ASM_DEBUG
1078 printf("subst_asm: \"%s\"\n", (char *)astr1.data);
1079 #endif
1081 /* generate loads */
1082 asm_gen_code(operands, nb_operands, nb_outputs, 0,
1083 clobber_regs, out_reg);
1085 /* assemble the string with tcc internal assembler */
1086 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
1088 /* restore the current C token */
1089 next();
1091 /* store the output values if needed */
1092 asm_gen_code(operands, nb_operands, nb_outputs, 1,
1093 clobber_regs, out_reg);
1095 /* free everything */
1096 for(i=0;i<nb_operands;i++) {
1097 ASMOperand *op;
1098 op = &operands[i];
1099 tcc_free(op->constraint);
1100 vpop();
1102 cstr_free(&astr1);
1105 ST_FUNC void asm_global_instr(void)
1107 CString astr;
1109 next();
1110 parse_asm_str(&astr);
1111 skip(')');
1112 /* NOTE: we do not eat the ';' so that we can restore the current
1113 token after the assembler parsing */
1114 if (tok != ';')
1115 expect("';'");
1117 #ifdef ASM_DEBUG
1118 printf("asm_global: \"%s\"\n", (char *)astr.data);
1119 #endif
1120 cur_text_section = text_section;
1121 ind = cur_text_section->data_offset;
1123 /* assemble the string with tcc internal assembler */
1124 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
1126 cur_text_section->data_offset = ind;
1128 /* restore the current C token */
1129 next();
1131 cstr_free(&astr);
1133 #endif /* CONFIG_TCC_ASM */