Changelog: cleanup
[tinycc.git] / tccasm.c
blobd1d2af19ab872715036d3471171f065cc1cd45ca
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 substracted 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_skip:
339 case TOK_ASM_space:
340 tok1 = tok;
341 next();
342 n = asm_int_expr(s1);
343 if (tok1 == TOK_ASM_align) {
344 if (n < 0 || (n & (n-1)) != 0)
345 tcc_error("alignment must be a positive power of two");
346 offset = (ind + n - 1) & -n;
347 size = offset - ind;
348 /* the section must have a compatible alignment */
349 if (sec->sh_addralign < n)
350 sec->sh_addralign = n;
351 } else {
352 size = n;
354 v = 0;
355 if (tok == ',') {
356 next();
357 v = asm_int_expr(s1);
359 zero_pad:
360 if (sec->sh_type != SHT_NOBITS) {
361 sec->data_offset = ind;
362 ptr = section_ptr_add(sec, size);
363 memset(ptr, v, size);
365 ind += size;
366 break;
367 case TOK_ASM_quad:
368 next();
369 for(;;) {
370 uint64_t vl;
371 const char *p;
373 p = tokc.cstr->data;
374 if (tok != TOK_PPNUM) {
375 error_constant:
376 tcc_error("64 bit constant");
378 vl = strtoll(p, (char **)&p, 0);
379 if (*p != '\0')
380 goto error_constant;
381 next();
382 if (sec->sh_type != SHT_NOBITS) {
383 /* XXX: endianness */
384 gen_le32(vl);
385 gen_le32(vl >> 32);
386 } else {
387 ind += 8;
389 if (tok != ',')
390 break;
391 next();
393 break;
394 case TOK_ASM_byte:
395 size = 1;
396 goto asm_data;
397 case TOK_ASM_word:
398 case TOK_SHORT:
399 size = 2;
400 goto asm_data;
401 case TOK_LONG:
402 case TOK_INT:
403 size = 4;
404 asm_data:
405 next();
406 for(;;) {
407 ExprValue e;
408 asm_expr(s1, &e);
409 if (sec->sh_type != SHT_NOBITS) {
410 if (size == 4) {
411 gen_expr32(&e);
412 } else {
413 if (e.sym)
414 expect("constant");
415 if (size == 1)
416 g(e.v);
417 else
418 gen_le16(e.v);
420 } else {
421 ind += size;
423 if (tok != ',')
424 break;
425 next();
427 break;
428 case TOK_ASM_fill:
430 int repeat, size, val, i, j;
431 uint8_t repeat_buf[8];
432 next();
433 repeat = asm_int_expr(s1);
434 if (repeat < 0) {
435 tcc_error("repeat < 0; .fill ignored");
436 break;
438 size = 1;
439 val = 0;
440 if (tok == ',') {
441 next();
442 size = asm_int_expr(s1);
443 if (size < 0) {
444 tcc_error("size < 0; .fill ignored");
445 break;
447 if (size > 8)
448 size = 8;
449 if (tok == ',') {
450 next();
451 val = asm_int_expr(s1);
454 /* XXX: endianness */
455 repeat_buf[0] = val;
456 repeat_buf[1] = val >> 8;
457 repeat_buf[2] = val >> 16;
458 repeat_buf[3] = val >> 24;
459 repeat_buf[4] = 0;
460 repeat_buf[5] = 0;
461 repeat_buf[6] = 0;
462 repeat_buf[7] = 0;
463 for(i = 0; i < repeat; i++) {
464 for(j = 0; j < size; j++) {
465 g(repeat_buf[j]);
469 break;
470 case TOK_ASM_org:
472 unsigned long n;
473 next();
474 /* XXX: handle section symbols too */
475 n = asm_int_expr(s1);
476 if (n < ind)
477 tcc_error("attempt to .org backwards");
478 v = 0;
479 size = n - ind;
480 goto zero_pad;
482 break;
483 case TOK_ASM_globl:
484 case TOK_ASM_global:
485 case TOK_ASM_weak:
486 tok1 = tok;
487 do {
488 Sym *sym;
490 next();
491 sym = label_find(tok);
492 if (!sym) {
493 sym = label_push(&s1->asm_labels, tok, 0);
494 sym->type.t = VT_VOID;
496 sym->type.t &= ~VT_STATIC;
497 if (tok1 == TOK_ASM_weak)
498 sym->type.t |= VT_WEAK;
499 next();
500 } while (tok == ',');
501 break;
502 case TOK_ASM_string:
503 case TOK_ASM_ascii:
504 case TOK_ASM_asciz:
506 const uint8_t *p;
507 int i, size, t;
509 t = tok;
510 next();
511 for(;;) {
512 if (tok != TOK_STR)
513 expect("string constant");
514 p = tokc.cstr->data;
515 size = tokc.cstr->size;
516 if (t == TOK_ASM_ascii && size > 0)
517 size--;
518 for(i = 0; i < size; i++)
519 g(p[i]);
520 next();
521 if (tok == ',') {
522 next();
523 } else if (tok != TOK_STR) {
524 break;
528 break;
529 case TOK_ASM_text:
530 case TOK_ASM_data:
531 case TOK_ASM_bss:
533 char sname[64];
534 tok1 = tok;
535 n = 0;
536 next();
537 if (tok != ';' && tok != TOK_LINEFEED) {
538 n = asm_int_expr(s1);
539 next();
541 sprintf(sname, (n?".%s%d":".%s"), get_tok_str(tok1, NULL), n);
542 use_section(s1, sname);
544 break;
545 case TOK_ASM_file:
547 char filename[512];
549 filename[0] = '\0';
550 next();
552 if (tok == TOK_STR)
553 pstrcat(filename, sizeof(filename), tokc.cstr->data);
554 else
555 pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
557 if (s1->warn_unsupported)
558 tcc_warning("ignoring .file %s", filename);
560 next();
562 break;
563 case TOK_ASM_ident:
565 char ident[256];
567 ident[0] = '\0';
568 next();
570 if (tok == TOK_STR)
571 pstrcat(ident, sizeof(ident), tokc.cstr->data);
572 else
573 pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
575 if (s1->warn_unsupported)
576 tcc_warning("ignoring .ident %s", ident);
578 next();
580 break;
581 case TOK_ASM_size:
583 Sym *sym;
585 next();
586 sym = label_find(tok);
587 if (!sym) {
588 tcc_error("label not found: %s", get_tok_str(tok, NULL));
591 next();
592 skip(',');
593 /* XXX .size name,label2-label1 */
594 if (s1->warn_unsupported)
595 tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL));
597 while (tok != '\n' && tok != CH_EOF) {
598 next();
601 break;
602 case TOK_ASM_type:
604 Sym *sym;
605 const char *newtype;
607 next();
608 sym = label_find(tok);
609 if (!sym) {
610 sym = label_push(&s1->asm_labels, tok, 0);
611 sym->type.t = VT_VOID;
614 next();
615 skip(',');
616 if (tok == TOK_STR) {
617 newtype = tokc.cstr->data;
618 } else {
619 if (tok == '@' || tok == '%')
620 skip(tok);
621 newtype = get_tok_str(tok, NULL);
624 if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
625 sym->type.t = VT_FUNC;
627 else if (s1->warn_unsupported)
628 tcc_warning("change type of '%s' from 0x%x to '%s' ignored",
629 get_tok_str(sym->v, NULL), sym->type.t, newtype);
631 next();
633 break;
634 case TOK_SECTION1:
636 char sname[256];
638 /* XXX: support more options */
639 next();
640 sname[0] = '\0';
641 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
642 if (tok == TOK_STR)
643 pstrcat(sname, sizeof(sname), tokc.cstr->data);
644 else
645 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
646 next();
648 if (tok == ',') {
649 /* skip section options */
650 next();
651 if (tok != TOK_STR)
652 expect("string constant");
653 next();
655 last_text_section = cur_text_section;
656 use_section(s1, sname);
658 break;
659 case TOK_ASM_previous:
661 Section *sec;
662 next();
663 if (!last_text_section)
664 tcc_error("no previous section referenced");
665 sec = cur_text_section;
666 use_section1(s1, last_text_section);
667 last_text_section = sec;
669 break;
670 #ifdef TCC_TARGET_I386
671 case TOK_ASM_code16:
673 next();
674 s1->seg_size = 16;
676 break;
677 case TOK_ASM_code32:
679 next();
680 s1->seg_size = 32;
682 break;
683 #endif
684 #ifdef TCC_TARGET_X86_64
685 /* added for compatibility with GAS */
686 case TOK_ASM_code64:
687 next();
688 break;
689 #endif
690 default:
691 tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
692 break;
697 /* assemble a file */
698 static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
700 int opcode;
702 #if 0
703 /* print stats about opcodes */
705 const ASMInstr *pa;
706 int freq[4];
707 int op_vals[500];
708 int nb_op_vals, i, j;
710 nb_op_vals = 0;
711 memset(freq, 0, sizeof(freq));
712 for(pa = asm_instrs; pa->sym != 0; pa++) {
713 freq[pa->nb_ops]++;
714 for(i=0;i<pa->nb_ops;i++) {
715 for(j=0;j<nb_op_vals;j++) {
716 if (pa->op_type[i] == op_vals[j])
717 goto found;
719 op_vals[nb_op_vals++] = pa->op_type[i];
720 found: ;
723 for(i=0;i<nb_op_vals;i++) {
724 int v = op_vals[i];
725 if ((v & (v - 1)) != 0)
726 printf("%3d: %08x\n", i, v);
728 printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
729 sizeof(asm_instrs), sizeof(asm_instrs) / sizeof(ASMInstr),
730 freq[0], freq[1], freq[2], freq[3]);
732 #endif
734 /* XXX: undefine C labels */
736 ch = file->buf_ptr[0];
737 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
738 parse_flags = PARSE_FLAG_ASM_COMMENTS;
739 if (do_preprocess)
740 parse_flags |= PARSE_FLAG_PREPROCESS;
741 next();
742 for(;;) {
743 if (tok == TOK_EOF)
744 break;
745 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
746 redo:
747 if (tok == '#') {
748 /* horrible gas comment */
749 while (tok != TOK_LINEFEED)
750 next();
751 } else if (tok == '.') {
752 asm_parse_directive(s1);
753 } else if (tok == TOK_PPNUM) {
754 const char *p;
755 int n;
756 p = tokc.cstr->data;
757 n = strtoul(p, (char **)&p, 10);
758 if (*p != '\0')
759 expect("':'");
760 /* new local label */
761 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
762 next();
763 skip(':');
764 goto redo;
765 } else if (tok >= TOK_IDENT) {
766 /* instruction or label */
767 opcode = tok;
768 next();
769 if (tok == ':') {
770 /* new label */
771 asm_new_label(s1, opcode, 0);
772 next();
773 goto redo;
774 } else if (tok == '=') {
775 int n;
776 next();
777 n = asm_int_expr(s1);
778 asm_new_label1(s1, opcode, 0, SHN_ABS, n);
779 goto redo;
780 } else {
781 asm_opcode(s1, opcode);
784 /* end of line */
785 if (tok != ';' && tok != TOK_LINEFEED){
786 expect("end of line");
788 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
789 next();
792 asm_free_labels(s1);
794 return 0;
797 /* Assemble the current file */
798 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
800 Sym *define_start;
801 int ret;
803 preprocess_init(s1);
805 /* default section is text */
806 cur_text_section = text_section;
807 ind = cur_text_section->data_offset;
809 define_start = define_stack;
811 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
812 symbols can be safely used */
813 put_elf_sym(symtab_section, 0, 0,
814 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
815 SHN_ABS, file->filename);
817 ret = tcc_assemble_internal(s1, do_preprocess);
819 cur_text_section->data_offset = ind;
821 free_defines(define_start);
823 return ret;
826 /********************************************************************/
827 /* GCC inline asm support */
829 /* assemble the string 'str' in the current C compilation unit without
830 C preprocessing. NOTE: str is modified by modifying the '\0' at the
831 end */
832 static void tcc_assemble_inline(TCCState *s1, char *str, int len)
834 int saved_parse_flags;
835 const int *saved_macro_ptr;
837 saved_parse_flags = parse_flags;
838 saved_macro_ptr = macro_ptr;
840 tcc_open_bf(s1, file->filename, len);
841 file->line_num = file->prev->line_num;
842 memcpy(file->buffer, str, len);
844 macro_ptr = NULL;
845 tcc_assemble_internal(s1, 0);
846 tcc_close();
848 parse_flags = saved_parse_flags;
849 macro_ptr = saved_macro_ptr;
852 /* find a constraint by its number or id (gcc 3 extended
853 syntax). return -1 if not found. Return in *pp in char after the
854 constraint */
855 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
856 const char *name, const char **pp)
858 int index;
859 TokenSym *ts;
860 const char *p;
862 if (isnum(*name)) {
863 index = 0;
864 while (isnum(*name)) {
865 index = (index * 10) + (*name) - '0';
866 name++;
868 if ((unsigned)index >= nb_operands)
869 index = -1;
870 } else if (*name == '[') {
871 name++;
872 p = strchr(name, ']');
873 if (p) {
874 ts = tok_alloc(name, p - name);
875 for(index = 0; index < nb_operands; index++) {
876 if (operands[index].id == ts->tok)
877 goto found;
879 index = -1;
880 found:
881 name = p + 1;
882 } else {
883 index = -1;
885 } else {
886 index = -1;
888 if (pp)
889 *pp = name;
890 return index;
893 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
894 int nb_outputs,
895 CString *out_str, CString *in_str)
897 int c, index, modifier;
898 const char *str;
899 ASMOperand *op;
900 SValue sv;
902 cstr_new(out_str);
903 str = in_str->data;
904 for(;;) {
905 c = *str++;
906 if (c == '%') {
907 if (*str == '%') {
908 str++;
909 goto add_char;
911 modifier = 0;
912 if (*str == 'c' || *str == 'n' ||
913 *str == 'b' || *str == 'w' || *str == 'h')
914 modifier = *str++;
915 index = find_constraint(operands, nb_operands, str, &str);
916 if (index < 0)
917 tcc_error("invalid operand reference after %%");
918 op = &operands[index];
919 sv = *op->vt;
920 if (op->reg >= 0) {
921 sv.r = op->reg;
922 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
923 sv.r |= VT_LVAL;
925 subst_asm_operand(out_str, &sv, modifier);
926 } else {
927 add_char:
928 cstr_ccat(out_str, c);
929 if (c == '\0')
930 break;
936 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
937 int is_output)
939 ASMOperand *op;
940 int nb_operands;
942 if (tok != ':') {
943 nb_operands = *nb_operands_ptr;
944 for(;;) {
945 if (nb_operands >= MAX_ASM_OPERANDS)
946 tcc_error("too many asm operands");
947 op = &operands[nb_operands++];
948 op->id = 0;
949 if (tok == '[') {
950 next();
951 if (tok < TOK_IDENT)
952 expect("identifier");
953 op->id = tok;
954 next();
955 skip(']');
957 if (tok != TOK_STR)
958 expect("string constant");
959 op->constraint = tcc_malloc(tokc.cstr->size);
960 strcpy(op->constraint, tokc.cstr->data);
961 next();
962 skip('(');
963 gexpr();
964 if (is_output) {
965 test_lvalue();
966 } else {
967 /* we want to avoid LLOCAL case, except when the 'm'
968 constraint is used. Note that it may come from
969 register storage, so we need to convert (reg)
970 case */
971 if ((vtop->r & VT_LVAL) &&
972 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
973 (vtop->r & VT_VALMASK) < VT_CONST) &&
974 !strchr(op->constraint, 'm')) {
975 gv(RC_INT);
978 op->vt = vtop;
979 skip(')');
980 if (tok == ',') {
981 next();
982 } else {
983 break;
986 *nb_operands_ptr = nb_operands;
990 /* parse the GCC asm() instruction */
991 ST_FUNC void asm_instr(void)
993 CString astr, astr1;
994 ASMOperand operands[MAX_ASM_OPERANDS];
995 int nb_outputs, nb_operands, i, must_subst, out_reg;
996 uint8_t clobber_regs[NB_ASM_REGS];
998 next();
999 /* since we always generate the asm() instruction, we can ignore
1000 volatile */
1001 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
1002 next();
1004 parse_asm_str(&astr);
1005 nb_operands = 0;
1006 nb_outputs = 0;
1007 must_subst = 0;
1008 memset(clobber_regs, 0, sizeof(clobber_regs));
1009 if (tok == ':') {
1010 next();
1011 must_subst = 1;
1012 /* output args */
1013 parse_asm_operands(operands, &nb_operands, 1);
1014 nb_outputs = nb_operands;
1015 if (tok == ':') {
1016 next();
1017 if (tok != ')') {
1018 /* input args */
1019 parse_asm_operands(operands, &nb_operands, 0);
1020 if (tok == ':') {
1021 /* clobber list */
1022 /* XXX: handle registers */
1023 next();
1024 for(;;) {
1025 if (tok != TOK_STR)
1026 expect("string constant");
1027 asm_clobber(clobber_regs, tokc.cstr->data);
1028 next();
1029 if (tok == ',') {
1030 next();
1031 } else {
1032 break;
1039 skip(')');
1040 /* NOTE: we do not eat the ';' so that we can restore the current
1041 token after the assembler parsing */
1042 if (tok != ';')
1043 expect("';'");
1045 /* save all values in the memory */
1046 save_regs(0);
1048 /* compute constraints */
1049 asm_compute_constraints(operands, nb_operands, nb_outputs,
1050 clobber_regs, &out_reg);
1052 /* substitute the operands in the asm string. No substitution is
1053 done if no operands (GCC behaviour) */
1054 #ifdef ASM_DEBUG
1055 printf("asm: \"%s\"\n", (char *)astr.data);
1056 #endif
1057 if (must_subst) {
1058 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
1059 cstr_free(&astr);
1060 } else {
1061 astr1 = astr;
1063 #ifdef ASM_DEBUG
1064 printf("subst_asm: \"%s\"\n", (char *)astr1.data);
1065 #endif
1067 /* generate loads */
1068 asm_gen_code(operands, nb_operands, nb_outputs, 0,
1069 clobber_regs, out_reg);
1071 /* assemble the string with tcc internal assembler */
1072 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
1074 /* restore the current C token */
1075 next();
1077 /* store the output values if needed */
1078 asm_gen_code(operands, nb_operands, nb_outputs, 1,
1079 clobber_regs, out_reg);
1081 /* free everything */
1082 for(i=0;i<nb_operands;i++) {
1083 ASMOperand *op;
1084 op = &operands[i];
1085 tcc_free(op->constraint);
1086 vpop();
1088 cstr_free(&astr1);
1091 ST_FUNC void asm_global_instr(void)
1093 CString astr;
1095 next();
1096 parse_asm_str(&astr);
1097 skip(')');
1098 /* NOTE: we do not eat the ';' so that we can restore the current
1099 token after the assembler parsing */
1100 if (tok != ';')
1101 expect("';'");
1103 #ifdef ASM_DEBUG
1104 printf("asm_global: \"%s\"\n", (char *)astr.data);
1105 #endif
1106 cur_text_section = text_section;
1107 ind = cur_text_section->data_offset;
1109 /* assemble the string with tcc internal assembler */
1110 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
1112 cur_text_section->data_offset = ind;
1114 /* restore the current C token */
1115 next();
1117 cstr_free(&astr);
1119 #endif /* CONFIG_TCC_ASM */