rename i386-tok.h i386-asm.c, add PRINTF_ASM_CODE
[tinycc.git] / tccasm.c
blobeec42082db1584cc697924966d6b06ba49511993
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
23 ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n)
25 char buf[64];
26 TokenSym *ts;
28 snprintf(buf, sizeof(buf), "L..%u", n);
29 ts = tok_alloc(buf, strlen(buf));
30 return ts->tok;
33 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
35 /* We do not use the C expression parser to handle symbols. Maybe the
36 C expression parser could be tweaked to do so. */
38 static void asm_expr_unary(TCCState *s1, ExprValue *pe)
40 Sym *sym;
41 int op, n, label;
42 const char *p;
44 switch(tok) {
45 case TOK_PPNUM:
46 p = tokc.cstr->data;
47 n = strtoul(p, (char **)&p, 0);
48 if (*p == 'b' || *p == 'f') {
49 /* backward or forward label */
50 label = asm_get_local_label_name(s1, n);
51 sym = label_find(label);
52 if (*p == 'b') {
53 /* backward : find the last corresponding defined label */
54 if (sym && sym->r == 0)
55 sym = sym->prev_tok;
56 if (!sym)
57 tcc_error("local label '%d' not found backward", n);
58 } else {
59 /* forward */
60 if (!sym || sym->r) {
61 /* if the last label is defined, then define a new one */
62 sym = label_push(&s1->asm_labels, label, 0);
63 sym->type.t = VT_STATIC | VT_VOID;
66 pe->v = 0;
67 pe->sym = sym;
68 } else if (*p == '\0') {
69 pe->v = n;
70 pe->sym = NULL;
71 } else {
72 tcc_error("invalid number syntax");
74 next();
75 break;
76 case '+':
77 next();
78 asm_expr_unary(s1, pe);
79 break;
80 case '-':
81 case '~':
82 op = tok;
83 next();
84 asm_expr_unary(s1, pe);
85 if (pe->sym)
86 tcc_error("invalid operation with label");
87 if (op == '-')
88 pe->v = -pe->v;
89 else
90 pe->v = ~pe->v;
91 break;
92 case TOK_CCHAR:
93 case TOK_LCHAR:
94 pe->v = tokc.i;
95 pe->sym = NULL;
96 next();
97 break;
98 case '(':
99 next();
100 asm_expr(s1, pe);
101 skip(')');
102 break;
103 default:
104 if (tok >= TOK_IDENT) {
105 /* label case : if the label was not found, add one */
106 sym = label_find(tok);
107 if (!sym) {
108 sym = label_push(&s1->asm_labels, tok, 0);
109 /* NOTE: by default, the symbol is global */
110 sym->type.t = VT_VOID;
112 if (sym->r == SHN_ABS) {
113 /* if absolute symbol, no need to put a symbol value */
114 pe->v = sym->jnext;
115 pe->sym = NULL;
116 } else {
117 pe->v = 0;
118 pe->sym = sym;
120 next();
121 } else {
122 tcc_error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
124 break;
128 static void asm_expr_prod(TCCState *s1, ExprValue *pe)
130 int op;
131 ExprValue e2;
133 asm_expr_unary(s1, pe);
134 for(;;) {
135 op = tok;
136 if (op != '*' && op != '/' && op != '%' &&
137 op != TOK_SHL && op != TOK_SAR)
138 break;
139 next();
140 asm_expr_unary(s1, &e2);
141 if (pe->sym || e2.sym)
142 tcc_error("invalid operation with label");
143 switch(op) {
144 case '*':
145 pe->v *= e2.v;
146 break;
147 case '/':
148 if (e2.v == 0) {
149 div_error:
150 tcc_error("division by zero");
152 pe->v /= e2.v;
153 break;
154 case '%':
155 if (e2.v == 0)
156 goto div_error;
157 pe->v %= e2.v;
158 break;
159 case TOK_SHL:
160 pe->v <<= e2.v;
161 break;
162 default:
163 case TOK_SAR:
164 pe->v >>= e2.v;
165 break;
170 static void asm_expr_logic(TCCState *s1, ExprValue *pe)
172 int op;
173 ExprValue e2;
175 asm_expr_prod(s1, pe);
176 for(;;) {
177 op = tok;
178 if (op != '&' && op != '|' && op != '^')
179 break;
180 next();
181 asm_expr_prod(s1, &e2);
182 if (pe->sym || e2.sym)
183 tcc_error("invalid operation with label");
184 switch(op) {
185 case '&':
186 pe->v &= e2.v;
187 break;
188 case '|':
189 pe->v |= e2.v;
190 break;
191 default:
192 case '^':
193 pe->v ^= e2.v;
194 break;
199 static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
201 int op;
202 ExprValue e2;
204 asm_expr_logic(s1, pe);
205 for(;;) {
206 op = tok;
207 if (op != '+' && op != '-')
208 break;
209 next();
210 asm_expr_logic(s1, &e2);
211 if (op == '+') {
212 if (pe->sym != NULL && e2.sym != NULL)
213 goto cannot_relocate;
214 pe->v += e2.v;
215 if (pe->sym == NULL && e2.sym != NULL)
216 pe->sym = e2.sym;
217 } else {
218 pe->v -= e2.v;
219 /* NOTE: we are less powerful than gas in that case
220 because we store only one symbol in the expression */
221 if (!pe->sym && !e2.sym) {
222 /* OK */
223 } else if (pe->sym && !e2.sym) {
224 /* OK */
225 } else if (pe->sym && e2.sym) {
226 if (pe->sym == e2.sym) {
227 /* OK */
228 } else if (pe->sym->r == e2.sym->r && pe->sym->r != 0) {
229 /* we also accept defined symbols in the same section */
230 pe->v += pe->sym->jnext - e2.sym->jnext;
231 } else {
232 goto cannot_relocate;
234 pe->sym = NULL; /* same symbols can be subtracted to NULL */
235 } else {
236 cannot_relocate:
237 tcc_error("invalid operation with label");
243 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe)
245 asm_expr_sum(s1, pe);
248 ST_FUNC int asm_int_expr(TCCState *s1)
250 ExprValue e;
251 asm_expr(s1, &e);
252 if (e.sym)
253 expect("constant");
254 return e.v;
257 /* NOTE: the same name space as C labels is used to avoid using too
258 much memory when storing labels in TokenStrings */
259 static void asm_new_label1(TCCState *s1, int label, int is_local,
260 int sh_num, int value)
262 Sym *sym;
264 sym = label_find(label);
265 if (sym) {
266 if (sym->r) {
267 /* the label is already defined */
268 if (!is_local) {
269 tcc_error("assembler label '%s' already defined",
270 get_tok_str(label, NULL));
271 } else {
272 /* redefinition of local labels is possible */
273 goto new_label;
276 } else {
277 new_label:
278 sym = label_push(&s1->asm_labels, label, 0);
279 sym->type.t = VT_STATIC | VT_VOID;
281 sym->r = sh_num;
282 sym->jnext = value;
285 static void asm_new_label(TCCState *s1, int label, int is_local)
287 asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
290 static void asm_free_labels(TCCState *st)
292 Sym *s, *s1;
293 Section *sec;
295 for(s = st->asm_labels; s != NULL; s = s1) {
296 s1 = s->prev;
297 /* define symbol value in object file */
298 if (s->r) {
299 if (s->r == SHN_ABS)
300 sec = SECTION_ABS;
301 else
302 sec = st->sections[s->r];
303 put_extern_sym2(s, sec, s->jnext, 0, 0);
305 /* remove label */
306 table_ident[s->v - TOK_IDENT]->sym_label = NULL;
307 sym_free(s);
309 st->asm_labels = NULL;
312 static void use_section1(TCCState *s1, Section *sec)
314 cur_text_section->data_offset = ind;
315 cur_text_section = sec;
316 ind = cur_text_section->data_offset;
319 static void use_section(TCCState *s1, const char *name)
321 Section *sec;
322 sec = find_section(s1, name);
323 use_section1(s1, sec);
326 static void asm_parse_directive(TCCState *s1)
328 int n, offset, v, size, tok1;
329 Section *sec;
330 uint8_t *ptr;
332 /* assembler directive */
333 next();
334 sec = cur_text_section;
335 switch(tok) {
336 case TOK_ASM_align:
337 case TOK_ASM_skip:
338 case TOK_ASM_space:
339 tok1 = tok;
340 next();
341 n = asm_int_expr(s1);
342 if (tok1 == TOK_ASM_align) {
343 if (n < 0 || (n & (n-1)) != 0)
344 tcc_error("alignment must be a positive power of two");
345 offset = (ind + n - 1) & -n;
346 size = offset - ind;
347 /* the section must have a compatible alignment */
348 if (sec->sh_addralign < n)
349 sec->sh_addralign = n;
350 } else {
351 size = n;
353 v = 0;
354 if (tok == ',') {
355 next();
356 v = asm_int_expr(s1);
358 zero_pad:
359 if (sec->sh_type != SHT_NOBITS) {
360 sec->data_offset = ind;
361 ptr = section_ptr_add(sec, size);
362 memset(ptr, v, size);
364 ind += size;
365 break;
366 case TOK_ASM_quad:
367 next();
368 for(;;) {
369 uint64_t vl;
370 const char *p;
372 p = tokc.cstr->data;
373 if (tok != TOK_PPNUM) {
374 error_constant:
375 tcc_error("64 bit constant");
377 vl = strtoll(p, (char **)&p, 0);
378 if (*p != '\0')
379 goto error_constant;
380 next();
381 if (sec->sh_type != SHT_NOBITS) {
382 /* XXX: endianness */
383 gen_le32(vl);
384 gen_le32(vl >> 32);
385 } else {
386 ind += 8;
388 if (tok != ',')
389 break;
390 next();
392 break;
393 case TOK_ASM_byte:
394 size = 1;
395 goto asm_data;
396 case TOK_ASM_word:
397 case TOK_SHORT:
398 size = 2;
399 goto asm_data;
400 case TOK_LONG:
401 case TOK_INT:
402 size = 4;
403 asm_data:
404 next();
405 for(;;) {
406 ExprValue e;
407 asm_expr(s1, &e);
408 if (sec->sh_type != SHT_NOBITS) {
409 if (size == 4) {
410 gen_expr32(&e);
411 } else {
412 if (e.sym)
413 expect("constant");
414 if (size == 1)
415 g(e.v);
416 else
417 gen_le16(e.v);
419 } else {
420 ind += size;
422 if (tok != ',')
423 break;
424 next();
426 break;
427 case TOK_ASM_fill:
429 int repeat, size, val, i, j;
430 uint8_t repeat_buf[8];
431 next();
432 repeat = asm_int_expr(s1);
433 if (repeat < 0) {
434 tcc_error("repeat < 0; .fill ignored");
435 break;
437 size = 1;
438 val = 0;
439 if (tok == ',') {
440 next();
441 size = asm_int_expr(s1);
442 if (size < 0) {
443 tcc_error("size < 0; .fill ignored");
444 break;
446 if (size > 8)
447 size = 8;
448 if (tok == ',') {
449 next();
450 val = asm_int_expr(s1);
453 /* XXX: endianness */
454 repeat_buf[0] = val;
455 repeat_buf[1] = val >> 8;
456 repeat_buf[2] = val >> 16;
457 repeat_buf[3] = val >> 24;
458 repeat_buf[4] = 0;
459 repeat_buf[5] = 0;
460 repeat_buf[6] = 0;
461 repeat_buf[7] = 0;
462 for(i = 0; i < repeat; i++) {
463 for(j = 0; j < size; j++) {
464 g(repeat_buf[j]);
468 break;
469 case TOK_ASM_org:
471 unsigned long n;
472 next();
473 /* XXX: handle section symbols too */
474 n = asm_int_expr(s1);
475 if (n < ind)
476 tcc_error("attempt to .org backwards");
477 v = 0;
478 size = n - ind;
479 goto zero_pad;
481 break;
482 case TOK_ASM_globl:
483 case TOK_ASM_global:
484 case TOK_ASM_weak:
485 case TOK_ASM_hidden:
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 if (tok1 != TOK_ASM_hidden)
497 sym->type.t &= ~VT_STATIC;
498 if (tok1 == TOK_ASM_weak)
499 sym->type.t |= VT_WEAK;
500 else if (tok1 == TOK_ASM_hidden)
501 sym->type.t |= STV_HIDDEN << VT_VIS_SHIFT;
502 next();
503 } while (tok == ',');
504 break;
505 case TOK_ASM_string:
506 case TOK_ASM_ascii:
507 case TOK_ASM_asciz:
509 const uint8_t *p;
510 int i, size, t;
512 t = tok;
513 next();
514 for(;;) {
515 if (tok != TOK_STR)
516 expect("string constant");
517 p = tokc.cstr->data;
518 size = tokc.cstr->size;
519 if (t == TOK_ASM_ascii && size > 0)
520 size--;
521 for(i = 0; i < size; i++)
522 g(p[i]);
523 next();
524 if (tok == ',') {
525 next();
526 } else if (tok != TOK_STR) {
527 break;
531 break;
532 case TOK_ASM_text:
533 case TOK_ASM_data:
534 case TOK_ASM_bss:
536 char sname[64];
537 tok1 = tok;
538 n = 0;
539 next();
540 if (tok != ';' && tok != TOK_LINEFEED) {
541 n = asm_int_expr(s1);
542 next();
544 sprintf(sname, (n?".%s%d":".%s"), get_tok_str(tok1, NULL), n);
545 use_section(s1, sname);
547 break;
548 case TOK_ASM_file:
550 char filename[512];
552 filename[0] = '\0';
553 next();
555 if (tok == TOK_STR)
556 pstrcat(filename, sizeof(filename), tokc.cstr->data);
557 else
558 pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL));
560 if (s1->warn_unsupported)
561 tcc_warning("ignoring .file %s", filename);
563 next();
565 break;
566 case TOK_ASM_ident:
568 char ident[256];
570 ident[0] = '\0';
571 next();
573 if (tok == TOK_STR)
574 pstrcat(ident, sizeof(ident), tokc.cstr->data);
575 else
576 pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL));
578 if (s1->warn_unsupported)
579 tcc_warning("ignoring .ident %s", ident);
581 next();
583 break;
584 case TOK_ASM_size:
586 Sym *sym;
588 next();
589 sym = label_find(tok);
590 if (!sym) {
591 tcc_error("label not found: %s", get_tok_str(tok, NULL));
594 /* XXX .size name,label2-label1 */
595 if (s1->warn_unsupported)
596 tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL));
598 next();
599 skip(',');
600 while (tok != '\n' && tok != CH_EOF) {
601 next();
604 break;
605 case TOK_ASM_type:
607 Sym *sym;
608 const char *newtype;
610 next();
611 sym = label_find(tok);
612 if (!sym) {
613 sym = label_push(&s1->asm_labels, tok, 0);
614 sym->type.t = VT_VOID;
617 next();
618 skip(',');
619 if (tok == TOK_STR) {
620 newtype = tokc.cstr->data;
621 } else {
622 if (tok == '@' || tok == '%')
623 skip(tok);
624 newtype = get_tok_str(tok, NULL);
627 if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
628 sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
630 else if (s1->warn_unsupported)
631 tcc_warning("change type of '%s' from 0x%x to '%s' ignored",
632 get_tok_str(sym->v, NULL), sym->type.t, newtype);
634 next();
636 break;
637 case TOK_SECTION1:
639 char sname[256];
641 /* XXX: support more options */
642 next();
643 sname[0] = '\0';
644 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
645 if (tok == TOK_STR)
646 pstrcat(sname, sizeof(sname), tokc.cstr->data);
647 else
648 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
649 next();
651 if (tok == ',') {
652 /* skip section options */
653 next();
654 if (tok != TOK_STR)
655 expect("string constant");
656 next();
658 last_text_section = cur_text_section;
659 use_section(s1, sname);
661 break;
662 case TOK_ASM_previous:
664 Section *sec;
665 next();
666 if (!last_text_section)
667 tcc_error("no previous section referenced");
668 sec = cur_text_section;
669 use_section1(s1, last_text_section);
670 last_text_section = sec;
672 break;
673 #ifdef TCC_TARGET_I386
674 case TOK_ASM_code16:
676 next();
677 s1->seg_size = 16;
679 break;
680 case TOK_ASM_code32:
682 next();
683 s1->seg_size = 32;
685 break;
686 #endif
687 #ifdef TCC_TARGET_X86_64
688 /* added for compatibility with GAS */
689 case TOK_ASM_code64:
690 next();
691 break;
692 #endif
693 default:
694 tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
695 break;
699 /* assemble a file */
700 static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
702 int opcode;
704 #ifdef PRINTF_ASM_CODE
705 ST_FUNC void printf_asm_opcode();
706 /* print stats about opcodes */
707 printf_asm_opcode();
708 #endif
710 /* XXX: undefine C labels */
712 ch = file->buf_ptr[0];
713 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
714 parse_flags = PARSE_FLAG_ASM_COMMENTS;
715 if (do_preprocess)
716 parse_flags |= PARSE_FLAG_PREPROCESS;
717 next();
718 for(;;) {
719 if (tok == TOK_EOF)
720 break;
721 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
722 redo:
723 if (tok == '#') {
724 /* horrible gas comment */
725 while (tok != TOK_LINEFEED)
726 next();
727 } else if (tok == '.') {
728 asm_parse_directive(s1);
729 } else if (tok == TOK_PPNUM) {
730 const char *p;
731 int n;
732 p = tokc.cstr->data;
733 n = strtoul(p, (char **)&p, 10);
734 if (*p != '\0')
735 expect("':'");
736 /* new local label */
737 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
738 next();
739 skip(':');
740 goto redo;
741 } else if (tok >= TOK_IDENT) {
742 /* instruction or label */
743 opcode = tok;
744 next();
745 if (tok == ':') {
746 /* new label */
747 asm_new_label(s1, opcode, 0);
748 next();
749 goto redo;
750 } else if (tok == '=') {
751 int n;
752 next();
753 n = asm_int_expr(s1);
754 asm_new_label1(s1, opcode, 0, SHN_ABS, n);
755 goto redo;
756 } else {
757 asm_opcode(s1, opcode);
760 /* end of line */
761 if (tok != ';' && tok != TOK_LINEFEED){
762 expect("end of line");
764 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
765 next();
768 asm_free_labels(s1);
770 return 0;
773 /* Assemble the current file */
774 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
776 Sym *define_start;
777 int ret;
779 preprocess_init(s1);
781 /* default section is text */
782 cur_text_section = text_section;
783 ind = cur_text_section->data_offset;
785 define_start = define_stack;
787 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
788 symbols can be safely used */
789 put_elf_sym(symtab_section, 0, 0, ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
790 SHN_ABS, file->filename);
792 ret = tcc_assemble_internal(s1, do_preprocess);
794 cur_text_section->data_offset = ind;
796 free_defines(define_start);
798 return ret;
801 /********************************************************************/
802 /* GCC inline asm support */
804 /* assemble the string 'str' in the current C compilation unit without
805 C preprocessing. NOTE: str is modified by modifying the '\0' at the
806 end */
807 static void tcc_assemble_inline(TCCState *s1, char *str, int len)
809 int saved_parse_flags;
810 const int *saved_macro_ptr;
812 saved_parse_flags = parse_flags;
813 saved_macro_ptr = macro_ptr;
815 tcc_open_bf(s1, ":asm:", len);
816 memcpy(file->buffer, str, len);
818 macro_ptr = NULL;
819 tcc_assemble_internal(s1, 0);
820 tcc_close();
822 parse_flags = saved_parse_flags;
823 macro_ptr = saved_macro_ptr;
826 /* find a constraint by its number or id (gcc 3 extended
827 syntax). return -1 if not found. Return in *pp in char after the
828 constraint */
829 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
830 const char *name, const char **pp)
832 int index;
833 TokenSym *ts;
834 const char *p;
836 if (isnum(*name)) {
837 index = 0;
838 while (isnum(*name)) {
839 index = (index * 10) + (*name) - '0';
840 name++;
842 if ((unsigned)index >= nb_operands)
843 index = -1;
844 } else if (*name == '[') {
845 name++;
846 p = strchr(name, ']');
847 if (p) {
848 ts = tok_alloc(name, p - name);
849 for(index = 0; index < nb_operands; index++) {
850 if (operands[index].id == ts->tok)
851 goto found;
853 index = -1;
854 found:
855 name = p + 1;
856 } else {
857 index = -1;
859 } else {
860 index = -1;
862 if (pp)
863 *pp = name;
864 return index;
867 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
868 int nb_outputs,
869 CString *out_str, CString *in_str)
871 int c, index, modifier;
872 const char *str;
873 ASMOperand *op;
874 SValue sv;
876 cstr_new(out_str);
877 str = in_str->data;
878 for(;;) {
879 c = *str++;
880 if (c == '%') {
881 if (*str == '%') {
882 str++;
883 goto add_char;
885 modifier = 0;
886 if (*str == 'c' || *str == 'n' ||
887 *str == 'b' || *str == 'w' || *str == 'h')
888 modifier = *str++;
889 index = find_constraint(operands, nb_operands, str, &str);
890 if (index < 0)
891 tcc_error("invalid operand reference after %%");
892 op = &operands[index];
893 sv = *op->vt;
894 if (op->reg >= 0) {
895 sv.r = op->reg;
896 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
897 sv.r |= VT_LVAL;
899 subst_asm_operand(out_str, &sv, modifier);
900 } else {
901 add_char:
902 cstr_ccat(out_str, c);
903 if (c == '\0')
904 break;
910 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
911 int is_output)
913 ASMOperand *op;
914 int nb_operands;
916 if (tok != ':') {
917 nb_operands = *nb_operands_ptr;
918 for(;;) {
919 if (nb_operands >= MAX_ASM_OPERANDS)
920 tcc_error("too many asm operands");
921 op = &operands[nb_operands++];
922 op->id = 0;
923 if (tok == '[') {
924 next();
925 if (tok < TOK_IDENT)
926 expect("identifier");
927 op->id = tok;
928 next();
929 skip(']');
931 if (tok != TOK_STR)
932 expect("string constant");
933 op->constraint = tcc_malloc(tokc.cstr->size);
934 strcpy(op->constraint, tokc.cstr->data);
935 next();
936 skip('(');
937 gexpr();
938 if (is_output) {
939 test_lvalue();
940 } else {
941 /* we want to avoid LLOCAL case, except when the 'm'
942 constraint is used. Note that it may come from
943 register storage, so we need to convert (reg)
944 case */
945 if ((vtop->r & VT_LVAL) &&
946 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
947 (vtop->r & VT_VALMASK) < VT_CONST) &&
948 !strchr(op->constraint, 'm')) {
949 gv(RC_INT);
952 op->vt = vtop;
953 skip(')');
954 if (tok == ',') {
955 next();
956 } else {
957 break;
960 *nb_operands_ptr = nb_operands;
964 /* parse the GCC asm() instruction */
965 ST_FUNC void asm_instr(void)
967 CString astr, astr1;
968 ASMOperand operands[MAX_ASM_OPERANDS];
969 int nb_outputs, nb_operands, i, must_subst, out_reg;
970 uint8_t clobber_regs[NB_ASM_REGS];
972 next();
973 /* since we always generate the asm() instruction, we can ignore
974 volatile */
975 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
976 next();
978 parse_asm_str(&astr);
979 nb_operands = 0;
980 nb_outputs = 0;
981 must_subst = 0;
982 memset(clobber_regs, 0, sizeof(clobber_regs));
983 if (tok == ':') {
984 next();
985 must_subst = 1;
986 /* output args */
987 parse_asm_operands(operands, &nb_operands, 1);
988 nb_outputs = nb_operands;
989 if (tok == ':') {
990 next();
991 if (tok != ')') {
992 /* input args */
993 parse_asm_operands(operands, &nb_operands, 0);
994 if (tok == ':') {
995 /* clobber list */
996 /* XXX: handle registers */
997 next();
998 for(;;) {
999 if (tok != TOK_STR)
1000 expect("string constant");
1001 asm_clobber(clobber_regs, tokc.cstr->data);
1002 next();
1003 if (tok == ',') {
1004 next();
1005 } else {
1006 break;
1013 skip(')');
1014 /* NOTE: we do not eat the ';' so that we can restore the current
1015 token after the assembler parsing */
1016 if (tok != ';')
1017 expect("';'");
1019 /* save all values in the memory */
1020 save_regs(0);
1022 /* compute constraints */
1023 asm_compute_constraints(operands, nb_operands, nb_outputs,
1024 clobber_regs, &out_reg);
1026 /* substitute the operands in the asm string. No substitution is
1027 done if no operands (GCC behaviour) */
1028 #ifdef ASM_DEBUG
1029 printf("asm: \"%s\"\n", (char *)astr.data);
1030 #endif
1031 if (must_subst) {
1032 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
1033 cstr_free(&astr);
1034 } else {
1035 astr1 = astr;
1037 #ifdef ASM_DEBUG
1038 printf("subst_asm: \"%s\"\n", (char *)astr1.data);
1039 #endif
1041 /* generate loads */
1042 asm_gen_code(operands, nb_operands, nb_outputs, 0,
1043 clobber_regs, out_reg);
1045 /* assemble the string with tcc internal assembler */
1046 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
1048 /* restore the current C token */
1049 next();
1051 /* store the output values if needed */
1052 asm_gen_code(operands, nb_operands, nb_outputs, 1,
1053 clobber_regs, out_reg);
1055 /* free everything */
1056 for(i=0;i<nb_operands;i++) {
1057 ASMOperand *op;
1058 op = &operands[i];
1059 tcc_free(op->constraint);
1060 vpop();
1062 cstr_free(&astr1);
1065 ST_FUNC void asm_global_instr(void)
1067 CString astr;
1069 next();
1070 parse_asm_str(&astr);
1071 skip(')');
1072 /* NOTE: we do not eat the ';' so that we can restore the current
1073 token after the assembler parsing */
1074 if (tok != ';')
1075 expect("';'");
1077 #ifdef ASM_DEBUG
1078 printf("asm_global: \"%s\"\n", (char *)astr.data);
1079 #endif
1080 cur_text_section = text_section;
1081 ind = cur_text_section->data_offset;
1083 /* assemble the string with tcc internal assembler */
1084 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
1086 cur_text_section->data_offset = ind;
1088 /* restore the current C token */
1089 next();
1091 cstr_free(&astr);
1093 #endif /* CONFIG_TCC_ASM */