x86-64: use r8/r9 as generic integer registers
[tinycc.git] / tccasm.c
blob28161bed33db5002d53408cd14db86e17e148c1f
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"
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 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 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 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 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 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 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 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 substracted to NULL */
235 } else {
236 cannot_relocate:
237 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 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 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 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 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 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 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:
485 Sym *sym;
487 next();
488 sym = label_find(tok);
489 if (!sym) {
490 sym = label_push(&s1->asm_labels, tok, 0);
491 sym->type.t = VT_VOID;
493 sym->type.t &= ~VT_STATIC;
494 next();
496 break;
497 case TOK_ASM_string:
498 case TOK_ASM_ascii:
499 case TOK_ASM_asciz:
501 const uint8_t *p;
502 int i, size, t;
504 t = tok;
505 next();
506 for(;;) {
507 if (tok != TOK_STR)
508 expect("string constant");
509 p = tokc.cstr->data;
510 size = tokc.cstr->size;
511 if (t == TOK_ASM_ascii && size > 0)
512 size--;
513 for(i = 0; i < size; i++)
514 g(p[i]);
515 next();
516 if (tok == ',') {
517 next();
518 } else if (tok != TOK_STR) {
519 break;
523 break;
524 case TOK_ASM_text:
525 case TOK_ASM_data:
526 case TOK_ASM_bss:
528 char sname[64];
529 tok1 = tok;
530 n = 0;
531 next();
532 if (tok != ';' && tok != TOK_LINEFEED) {
533 n = asm_int_expr(s1);
534 next();
536 sprintf(sname, (n?".%s%d":".%s"), get_tok_str(tok1, NULL), n);
537 use_section(s1, sname);
539 break;
540 case TOK_SECTION1:
542 char sname[256];
544 /* XXX: support more options */
545 next();
546 sname[0] = '\0';
547 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
548 if (tok == TOK_STR)
549 pstrcat(sname, sizeof(sname), tokc.cstr->data);
550 else
551 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
552 next();
554 if (tok == ',') {
555 /* skip section options */
556 next();
557 if (tok != TOK_STR)
558 expect("string constant");
559 next();
561 last_text_section = cur_text_section;
562 use_section(s1, sname);
564 break;
565 case TOK_ASM_previous:
567 Section *sec;
568 next();
569 if (!last_text_section)
570 error("no previous section referenced");
571 sec = cur_text_section;
572 use_section1(s1, last_text_section);
573 last_text_section = sec;
575 break;
576 #ifdef TCC_TARGET_I386
577 case TOK_ASM_code16:
579 next();
580 s1->seg_size = 16;
582 break;
583 case TOK_ASM_code32:
585 next();
586 s1->seg_size = 32;
588 break;
589 #endif
590 #ifdef TCC_TARGET_X86_64
591 /* added for compatibility with GAS */
592 case TOK_ASM_code64:
593 next();
594 break;
595 #endif
596 default:
597 error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
598 break;
603 /* assemble a file */
604 static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
606 int opcode;
608 #if 0
609 /* print stats about opcodes */
611 const ASMInstr *pa;
612 int freq[4];
613 int op_vals[500];
614 int nb_op_vals, i, j;
616 nb_op_vals = 0;
617 memset(freq, 0, sizeof(freq));
618 for(pa = asm_instrs; pa->sym != 0; pa++) {
619 freq[pa->nb_ops]++;
620 for(i=0;i<pa->nb_ops;i++) {
621 for(j=0;j<nb_op_vals;j++) {
622 if (pa->op_type[i] == op_vals[j])
623 goto found;
625 op_vals[nb_op_vals++] = pa->op_type[i];
626 found: ;
629 for(i=0;i<nb_op_vals;i++) {
630 int v = op_vals[i];
631 if ((v & (v - 1)) != 0)
632 printf("%3d: %08x\n", i, v);
634 printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
635 sizeof(asm_instrs), sizeof(asm_instrs) / sizeof(ASMInstr),
636 freq[0], freq[1], freq[2], freq[3]);
638 #endif
640 /* XXX: undefine C labels */
642 ch = file->buf_ptr[0];
643 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
644 parse_flags = PARSE_FLAG_ASM_COMMENTS;
645 if (do_preprocess)
646 parse_flags |= PARSE_FLAG_PREPROCESS;
647 next();
648 for(;;) {
649 if (tok == TOK_EOF)
650 break;
651 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
652 redo:
653 if (tok == '#') {
654 /* horrible gas comment */
655 while (tok != TOK_LINEFEED)
656 next();
657 } else if (tok == '.') {
658 asm_parse_directive(s1);
659 } else if (tok == TOK_PPNUM) {
660 const char *p;
661 int n;
662 p = tokc.cstr->data;
663 n = strtoul(p, (char **)&p, 10);
664 if (*p != '\0')
665 expect("':'");
666 /* new local label */
667 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
668 next();
669 skip(':');
670 goto redo;
671 } else if (tok >= TOK_IDENT) {
672 /* instruction or label */
673 opcode = tok;
674 next();
675 if (tok == ':') {
676 /* new label */
677 asm_new_label(s1, opcode, 0);
678 next();
679 goto redo;
680 } else if (tok == '=') {
681 int n;
682 next();
683 n = asm_int_expr(s1);
684 asm_new_label1(s1, opcode, 0, SHN_ABS, n);
685 goto redo;
686 } else {
687 asm_opcode(s1, opcode);
690 /* end of line */
691 if (tok != ';' && tok != TOK_LINEFEED){
692 expect("end of line");
694 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
695 next();
698 asm_free_labels(s1);
700 return 0;
703 /* Assemble the current file */
704 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
706 Sym *define_start;
707 int ret;
709 preprocess_init(s1);
711 /* default section is text */
712 cur_text_section = text_section;
713 ind = cur_text_section->data_offset;
715 define_start = define_stack;
717 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
718 symbols can be safely used */
719 put_elf_sym(symtab_section, 0, 0,
720 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
721 SHN_ABS, file->filename);
723 ret = tcc_assemble_internal(s1, do_preprocess);
725 cur_text_section->data_offset = ind;
727 free_defines(define_start);
729 return ret;
732 /********************************************************************/
733 /* GCC inline asm support */
735 /* assemble the string 'str' in the current C compilation unit without
736 C preprocessing. NOTE: str is modified by modifying the '\0' at the
737 end */
738 static void tcc_assemble_inline(TCCState *s1, char *str, int len)
740 BufferedFile *bf, *saved_file;
741 int saved_parse_flags, *saved_macro_ptr;
743 bf = tcc_malloc(sizeof(BufferedFile));
744 memset(bf, 0, sizeof(BufferedFile));
745 bf->fd = -1;
746 bf->buf_ptr = str;
747 bf->buf_end = str + len;
748 str[len] = CH_EOB;
749 /* same name as current file so that errors are correctly
750 reported */
751 pstrcpy(bf->filename, sizeof(bf->filename), file->filename);
752 bf->line_num = file->line_num;
753 saved_file = file;
754 file = bf;
755 saved_parse_flags = parse_flags;
756 saved_macro_ptr = macro_ptr;
757 macro_ptr = NULL;
759 tcc_assemble_internal(s1, 0);
761 parse_flags = saved_parse_flags;
762 macro_ptr = saved_macro_ptr;
763 file = saved_file;
764 tcc_free(bf);
767 /* find a constraint by its number or id (gcc 3 extended
768 syntax). return -1 if not found. Return in *pp in char after the
769 constraint */
770 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands,
771 const char *name, const char **pp)
773 int index;
774 TokenSym *ts;
775 const char *p;
777 if (isnum(*name)) {
778 index = 0;
779 while (isnum(*name)) {
780 index = (index * 10) + (*name) - '0';
781 name++;
783 if ((unsigned)index >= nb_operands)
784 index = -1;
785 } else if (*name == '[') {
786 name++;
787 p = strchr(name, ']');
788 if (p) {
789 ts = tok_alloc(name, p - name);
790 for(index = 0; index < nb_operands; index++) {
791 if (operands[index].id == ts->tok)
792 goto found;
794 index = -1;
795 found:
796 name = p + 1;
797 } else {
798 index = -1;
800 } else {
801 index = -1;
803 if (pp)
804 *pp = name;
805 return index;
808 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
809 int nb_outputs,
810 CString *out_str, CString *in_str)
812 int c, index, modifier;
813 const char *str;
814 ASMOperand *op;
815 SValue sv;
817 cstr_new(out_str);
818 str = in_str->data;
819 for(;;) {
820 c = *str++;
821 if (c == '%') {
822 if (*str == '%') {
823 str++;
824 goto add_char;
826 modifier = 0;
827 if (*str == 'c' || *str == 'n' ||
828 *str == 'b' || *str == 'w' || *str == 'h')
829 modifier = *str++;
830 index = find_constraint(operands, nb_operands, str, &str);
831 if (index < 0)
832 error("invalid operand reference after %%");
833 op = &operands[index];
834 sv = *op->vt;
835 if (op->reg >= 0) {
836 sv.r = op->reg;
837 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory)
838 sv.r |= VT_LVAL;
840 subst_asm_operand(out_str, &sv, modifier);
841 } else {
842 add_char:
843 cstr_ccat(out_str, c);
844 if (c == '\0')
845 break;
851 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
852 int is_output)
854 ASMOperand *op;
855 int nb_operands;
857 if (tok != ':') {
858 nb_operands = *nb_operands_ptr;
859 for(;;) {
860 if (nb_operands >= MAX_ASM_OPERANDS)
861 error("too many asm operands");
862 op = &operands[nb_operands++];
863 op->id = 0;
864 if (tok == '[') {
865 next();
866 if (tok < TOK_IDENT)
867 expect("identifier");
868 op->id = tok;
869 next();
870 skip(']');
872 if (tok != TOK_STR)
873 expect("string constant");
874 op->constraint = tcc_malloc(tokc.cstr->size);
875 strcpy(op->constraint, tokc.cstr->data);
876 next();
877 skip('(');
878 gexpr();
879 if (is_output) {
880 test_lvalue();
881 } else {
882 /* we want to avoid LLOCAL case, except when the 'm'
883 constraint is used. Note that it may come from
884 register storage, so we need to convert (reg)
885 case */
886 if ((vtop->r & VT_LVAL) &&
887 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
888 (vtop->r & VT_VALMASK) < VT_CONST) &&
889 !strchr(op->constraint, 'm')) {
890 gv(RC_INT);
893 op->vt = vtop;
894 skip(')');
895 if (tok == ',') {
896 next();
897 } else {
898 break;
901 *nb_operands_ptr = nb_operands;
905 static void parse_asm_str(CString *astr)
907 skip('(');
908 /* read the string */
909 if (tok != TOK_STR)
910 expect("string constant");
911 cstr_new(astr);
912 while (tok == TOK_STR) {
913 /* XXX: add \0 handling too ? */
914 cstr_cat(astr, tokc.cstr->data);
915 next();
917 cstr_ccat(astr, '\0');
920 /* parse the GCC asm() instruction */
921 ST_FUNC void asm_instr(void)
923 CString astr, astr1;
924 ASMOperand operands[MAX_ASM_OPERANDS];
925 int nb_inputs, nb_outputs, nb_operands, i, must_subst, out_reg;
926 uint8_t clobber_regs[NB_ASM_REGS];
928 next();
929 /* since we always generate the asm() instruction, we can ignore
930 volatile */
931 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
932 next();
934 parse_asm_str(&astr);
935 nb_operands = 0;
936 nb_outputs = 0;
937 must_subst = 0;
938 memset(clobber_regs, 0, sizeof(clobber_regs));
939 if (tok == ':') {
940 next();
941 must_subst = 1;
942 /* output args */
943 parse_asm_operands(operands, &nb_operands, 1);
944 nb_outputs = nb_operands;
945 if (tok == ':') {
946 next();
947 if (tok != ')') {
948 /* input args */
949 parse_asm_operands(operands, &nb_operands, 0);
950 if (tok == ':') {
951 /* clobber list */
952 /* XXX: handle registers */
953 next();
954 for(;;) {
955 if (tok != TOK_STR)
956 expect("string constant");
957 asm_clobber(clobber_regs, tokc.cstr->data);
958 next();
959 if (tok == ',') {
960 next();
961 } else {
962 break;
969 skip(')');
970 /* NOTE: we do not eat the ';' so that we can restore the current
971 token after the assembler parsing */
972 if (tok != ';')
973 expect("';'");
974 nb_inputs = nb_operands - nb_outputs;
976 /* save all values in the memory */
977 save_regs(0);
979 /* compute constraints */
980 asm_compute_constraints(operands, nb_operands, nb_outputs,
981 clobber_regs, &out_reg);
983 /* substitute the operands in the asm string. No substitution is
984 done if no operands (GCC behaviour) */
985 #ifdef ASM_DEBUG
986 printf("asm: \"%s\"\n", (char *)astr.data);
987 #endif
988 if (must_subst) {
989 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
990 cstr_free(&astr);
991 } else {
992 astr1 = astr;
994 #ifdef ASM_DEBUG
995 printf("subst_asm: \"%s\"\n", (char *)astr1.data);
996 #endif
998 /* generate loads */
999 asm_gen_code(operands, nb_operands, nb_outputs, 0,
1000 clobber_regs, out_reg);
1002 /* assemble the string with tcc internal assembler */
1003 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
1005 /* restore the current C token */
1006 next();
1008 /* store the output values if needed */
1009 asm_gen_code(operands, nb_operands, nb_outputs, 1,
1010 clobber_regs, out_reg);
1012 /* free everything */
1013 for(i=0;i<nb_operands;i++) {
1014 ASMOperand *op;
1015 op = &operands[i];
1016 tcc_free(op->constraint);
1017 vpop();
1019 cstr_free(&astr1);
1022 ST_FUNC void asm_global_instr(void)
1024 CString astr;
1026 next();
1027 parse_asm_str(&astr);
1028 skip(')');
1029 /* NOTE: we do not eat the ';' so that we can restore the current
1030 token after the assembler parsing */
1031 if (tok != ';')
1032 expect("';'");
1034 #ifdef ASM_DEBUG
1035 printf("asm_global: \"%s\"\n", (char *)astr.data);
1036 #endif
1037 cur_text_section = text_section;
1038 ind = cur_text_section->data_offset;
1040 /* assemble the string with tcc internal assembler */
1041 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
1043 cur_text_section->data_offset = ind;
1045 /* restore the current C token */
1046 next();
1048 cstr_free(&astr);