fixed linker symbol generation - output format support
[tinycc.git] / tccasm.c
blob26117adb3eaddebf14cdaeef395fdbf442423926
1 /*
2 * GAS like assembler for TCC
3 *
4 * Copyright (c) 2001, 2002 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 static int asm_get_local_label_name(TCCState *s1, unsigned int n)
23 char buf[64];
24 TokenSym *ts;
26 snprintf(buf, sizeof(buf), "L..%u", n);
27 ts = tok_alloc(buf, strlen(buf));
28 return ts->tok;
31 static void asm_expr(TCCState *s1, ExprValue *pe);
33 /* We do not use the C expression parser to handle symbols. Maybe the
34 C expression parser could be tweaked to do so. */
36 static void asm_expr_unary(TCCState *s1, ExprValue *pe)
38 Sym *sym;
39 int op, n, label;
40 const char *p;
42 switch(tok) {
43 case TOK_PPNUM:
44 p = tokc.cstr->data;
45 n = strtoul(p, (char **)&p, 0);
46 if (*p == 'b' || *p == 'f') {
47 /* backward or forward label */
48 label = asm_get_local_label_name(s1, n);
49 sym = label_find(label);
50 if (*p == 'b') {
51 /* backward : find the last corresponding defined label */
52 if (sym && sym->r == 0)
53 sym = sym->prev_tok;
54 if (!sym)
55 error("local label '%d' not found backward", n);
56 } else {
57 /* forward */
58 if (!sym || sym->r) {
59 /* if the last label is defined, then define a new one */
60 sym = label_push(&s1->asm_labels, label, 0);
61 sym->type.t = VT_STATIC | VT_VOID;
64 pe->v = 0;
65 pe->sym = sym;
66 } else if (*p == '\0') {
67 pe->v = n;
68 pe->sym = NULL;
69 } else {
70 error("invalid number syntax");
72 next();
73 break;
74 case '+':
75 next();
76 asm_expr_unary(s1, pe);
77 break;
78 case '-':
79 case '~':
80 op = tok;
81 next();
82 asm_expr_unary(s1, pe);
83 if (pe->sym)
84 error("invalid operation with label");
85 if (op == '-')
86 pe->v = -pe->v;
87 else
88 pe->v = ~pe->v;
89 break;
90 case TOK_CCHAR:
91 case TOK_LCHAR:
92 pe->v = tokc.i;
93 pe->sym = NULL;
94 next();
95 break;
96 case '(':
97 next();
98 asm_expr(s1, pe);
99 skip(')');
100 break;
101 default:
102 if (tok >= TOK_IDENT) {
103 /* label case : if the label was not found, add one */
104 sym = label_find(tok);
105 if (!sym) {
106 sym = label_push(&s1->asm_labels, tok, 0);
107 /* NOTE: by default, the symbol is global */
108 sym->type.t = VT_VOID;
110 if (sym->r == SHN_ABS) {
111 /* if absolute symbol, no need to put a symbol value */
112 pe->v = (long)sym->next;
113 pe->sym = NULL;
114 } else {
115 pe->v = 0;
116 pe->sym = sym;
118 next();
119 } else {
120 error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
122 break;
126 static void asm_expr_prod(TCCState *s1, ExprValue *pe)
128 int op;
129 ExprValue e2;
131 asm_expr_unary(s1, pe);
132 for(;;) {
133 op = tok;
134 if (op != '*' && op != '/' && op != '%' &&
135 op != TOK_SHL && op != TOK_SAR)
136 break;
137 next();
138 asm_expr_unary(s1, &e2);
139 if (pe->sym || e2.sym)
140 error("invalid operation with label");
141 switch(op) {
142 case '*':
143 pe->v *= e2.v;
144 break;
145 case '/':
146 if (e2.v == 0) {
147 div_error:
148 error("division by zero");
150 pe->v /= e2.v;
151 break;
152 case '%':
153 if (e2.v == 0)
154 goto div_error;
155 pe->v %= e2.v;
156 break;
157 case TOK_SHL:
158 pe->v <<= e2.v;
159 break;
160 default:
161 case TOK_SAR:
162 pe->v >>= e2.v;
163 break;
168 static void asm_expr_logic(TCCState *s1, ExprValue *pe)
170 int op;
171 ExprValue e2;
173 asm_expr_prod(s1, pe);
174 for(;;) {
175 op = tok;
176 if (op != '&' && op != '|' && op != '^')
177 break;
178 next();
179 asm_expr_prod(s1, &e2);
180 if (pe->sym || e2.sym)
181 error("invalid operation with label");
182 switch(op) {
183 case '&':
184 pe->v &= e2.v;
185 break;
186 case '|':
187 pe->v |= e2.v;
188 break;
189 default:
190 case '^':
191 pe->v ^= e2.v;
192 break;
197 static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
199 int op;
200 ExprValue e2;
202 asm_expr_logic(s1, pe);
203 for(;;) {
204 op = tok;
205 if (op != '+' && op != '-')
206 break;
207 next();
208 asm_expr_logic(s1, &e2);
209 if (op == '+') {
210 if (pe->sym != NULL && e2.sym != NULL)
211 goto cannot_relocate;
212 pe->v += e2.v;
213 if (pe->sym == NULL && e2.sym != NULL)
214 pe->sym = e2.sym;
215 } else {
216 pe->v -= e2.v;
217 /* NOTE: we are less powerful than gas in that case
218 because we store only one symbol in the expression */
219 if (!pe->sym && !e2.sym) {
220 /* OK */
221 } else if (pe->sym && !e2.sym) {
222 /* OK */
223 } else if (pe->sym && e2.sym) {
224 if (pe->sym == e2.sym) {
225 /* OK */
226 } else if (pe->sym->r == e2.sym->r && pe->sym->r != 0) {
227 /* we also accept defined symbols in the same section */
228 pe->v += (long)pe->sym->next - (long)e2.sym->next;
229 } else {
230 goto cannot_relocate;
232 pe->sym = NULL; /* same symbols can be substracted to NULL */
233 } else {
234 cannot_relocate:
235 error("invalid operation with label");
241 static void asm_expr(TCCState *s1, ExprValue *pe)
243 asm_expr_sum(s1, pe);
246 static int asm_int_expr(TCCState *s1)
248 ExprValue e;
249 asm_expr(s1, &e);
250 if (e.sym)
251 expect("constant");
252 return e.v;
255 /* NOTE: the same name space as C labels is used to avoid using too
256 much memory when storing labels in TokenStrings */
257 static void asm_new_label1(TCCState *s1, int label, int is_local,
258 int sh_num, int value)
260 Sym *sym;
262 sym = label_find(label);
263 if (sym) {
264 if (sym->r) {
265 /* the label is already defined */
266 if (!is_local) {
267 error("assembler label '%s' already defined",
268 get_tok_str(label, NULL));
269 } else {
270 /* redefinition of local labels is possible */
271 goto new_label;
274 } else {
275 new_label:
276 sym = label_push(&s1->asm_labels, label, 0);
277 sym->type.t = VT_STATIC | VT_VOID;
279 sym->r = sh_num;
280 sym->next = (void *)value;
283 static void asm_new_label(TCCState *s1, int label, int is_local)
285 asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
288 static void asm_free_labels(TCCState *st)
290 Sym *s, *s1;
291 Section *sec;
293 for(s = st->asm_labels; s != NULL; s = s1) {
294 s1 = s->prev;
295 /* define symbol value in object file */
296 if (s->r) {
297 if (s->r == SHN_ABS)
298 sec = SECTION_ABS;
299 else
300 sec = st->sections[s->r];
301 put_extern_sym(s, sec, (long)s->next, 0);
303 /* remove label */
304 table_ident[s->v - TOK_IDENT]->sym_label = NULL;
305 tcc_free(s);
307 st->asm_labels = NULL;
310 static void use_section1(TCCState *s1, Section *sec)
312 cur_text_section->data_offset = ind;
313 cur_text_section = sec;
314 ind = cur_text_section->data_offset;
317 static void use_section(TCCState *s1, const char *name)
319 Section *sec;
320 sec = find_section(s1, name);
321 use_section1(s1, sec);
324 static void asm_parse_directive(TCCState *s1)
326 int n, offset, v, size, tok1;
327 Section *sec;
328 uint8_t *ptr;
330 /* assembler directive */
331 next();
332 sec = cur_text_section;
333 switch(tok) {
334 case TOK_ASM_align:
335 case TOK_ASM_skip:
336 case TOK_ASM_space:
337 tok1 = tok;
338 next();
339 n = asm_int_expr(s1);
340 if (tok1 == TOK_ASM_align) {
341 if (n < 0 || (n & (n-1)) != 0)
342 error("alignment must be a positive power of two");
343 offset = (ind + n - 1) & -n;
344 size = offset - ind;
345 /* the section must have a compatible alignment */
346 if (sec->sh_addralign < n)
347 sec->sh_addralign = n;
348 } else {
349 size = n;
351 v = 0;
352 if (tok == ',') {
353 next();
354 v = asm_int_expr(s1);
356 zero_pad:
357 if (sec->sh_type != SHT_NOBITS) {
358 sec->data_offset = ind;
359 ptr = section_ptr_add(sec, size);
360 memset(ptr, v, size);
362 ind += size;
363 break;
364 case TOK_ASM_byte:
365 size = 1;
366 goto asm_data;
367 case TOK_ASM_word:
368 case TOK_SHORT:
369 size = 2;
370 goto asm_data;
371 case TOK_LONG:
372 case TOK_INT:
373 size = 4;
374 asm_data:
375 next();
376 for(;;) {
377 ExprValue e;
378 asm_expr(s1, &e);
379 if (sec->sh_type != SHT_NOBITS) {
380 if (size == 4) {
381 gen_expr32(&e);
382 } else {
383 if (e.sym)
384 expect("constant");
385 if (size == 1)
386 g(e.v);
387 else
388 gen_le16(e.v);
390 } else {
391 ind += size;
393 if (tok != ',')
394 break;
395 next();
397 break;
398 case TOK_ASM_fill:
400 int repeat, size, val, i, j;
401 uint8_t repeat_buf[8];
402 next();
403 repeat = asm_int_expr(s1);
404 if (repeat < 0) {
405 error("repeat < 0; .fill ignored");
406 break;
408 size = 1;
409 val = 0;
410 if (tok == ',') {
411 next();
412 size = asm_int_expr(s1);
413 if (size < 0) {
414 error("size < 0; .fill ignored");
415 break;
417 if (size > 8)
418 size = 8;
419 if (tok == ',') {
420 next();
421 val = asm_int_expr(s1);
424 /* XXX: endianness */
425 repeat_buf[0] = val;
426 repeat_buf[1] = val >> 8;
427 repeat_buf[2] = val >> 16;
428 repeat_buf[3] = val >> 24;
429 repeat_buf[4] = 0;
430 repeat_buf[5] = 0;
431 repeat_buf[6] = 0;
432 repeat_buf[7] = 0;
433 for(i = 0; i < repeat; i++) {
434 for(j = 0; j < size; j++) {
435 g(repeat_buf[j]);
439 break;
440 case TOK_ASM_org:
442 unsigned long n;
443 next();
444 /* XXX: handle section symbols too */
445 n = asm_int_expr(s1);
446 if (n < ind)
447 error("attempt to .org backwards");
448 v = 0;
449 size = n - ind;
450 goto zero_pad;
452 break;
453 case TOK_ASM_globl:
454 case TOK_ASM_global:
456 Sym *sym;
458 next();
459 sym = label_find(tok);
460 if (!sym) {
461 sym = label_push(&s1->asm_labels, tok, 0);
462 sym->type.t = VT_VOID;
464 sym->type.t &= ~VT_STATIC;
465 next();
467 break;
468 case TOK_ASM_string:
469 case TOK_ASM_ascii:
470 case TOK_ASM_asciz:
472 const uint8_t *p;
473 int i, size, t;
475 t = tok;
476 next();
477 for(;;) {
478 if (tok != TOK_STR)
479 expect("string constant");
480 p = tokc.cstr->data;
481 size = tokc.cstr->size;
482 if (t == TOK_ASM_ascii && size > 0)
483 size--;
484 for(i = 0; i < size; i++)
485 g(p[i]);
486 next();
487 if (tok == ',') {
488 next();
489 } else if (tok != TOK_STR) {
490 break;
494 break;
495 case TOK_ASM_text:
496 case TOK_ASM_data:
497 case TOK_ASM_bss:
499 char sname[64];
500 tok1 = tok;
501 n = 0;
502 next();
503 if (tok != ';' && tok != TOK_LINEFEED) {
504 n = asm_int_expr(s1);
505 next();
507 sprintf(sname, (n?".%s%d":".%s"), get_tok_str(tok1, NULL), n);
508 use_section(s1, sname);
510 break;
511 case TOK_SECTION1:
513 char sname[256];
515 /* XXX: support more options */
516 next();
517 sname[0] = '\0';
518 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
519 if (tok == TOK_STR)
520 pstrcat(sname, sizeof(sname), tokc.cstr->data);
521 else
522 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
523 next();
525 if (tok == ',') {
526 /* skip section options */
527 next();
528 if (tok != TOK_STR)
529 expect("string constant");
530 next();
532 last_text_section = cur_text_section;
533 use_section(s1, sname);
535 break;
536 case TOK_ASM_previous:
538 Section *sec;
539 next();
540 if (!last_text_section)
541 error("no previous section referenced");
542 sec = cur_text_section;
543 use_section1(s1, last_text_section);
544 last_text_section = sec;
546 break;
547 default:
548 error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
549 break;
554 /* assemble a file */
555 static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
557 int opcode;
559 #if 0
560 /* print stats about opcodes */
562 const ASMInstr *pa;
563 int freq[4];
564 int op_vals[500];
565 int nb_op_vals, i, j;
567 nb_op_vals = 0;
568 memset(freq, 0, sizeof(freq));
569 for(pa = asm_instrs; pa->sym != 0; pa++) {
570 freq[pa->nb_ops]++;
571 for(i=0;i<pa->nb_ops;i++) {
572 for(j=0;j<nb_op_vals;j++) {
573 if (pa->op_type[i] == op_vals[j])
574 goto found;
576 op_vals[nb_op_vals++] = pa->op_type[i];
577 found: ;
580 for(i=0;i<nb_op_vals;i++) {
581 int v = op_vals[i];
582 if ((v & (v - 1)) != 0)
583 printf("%3d: %08x\n", i, v);
585 printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
586 sizeof(asm_instrs), sizeof(asm_instrs) / sizeof(ASMInstr),
587 freq[0], freq[1], freq[2], freq[3]);
589 #endif
591 /* XXX: undefine C labels */
593 ch = file->buf_ptr[0];
594 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
595 parse_flags = PARSE_FLAG_ASM_COMMENTS;
596 if (do_preprocess)
597 parse_flags |= PARSE_FLAG_PREPROCESS;
598 next();
599 for(;;) {
600 if (tok == TOK_EOF)
601 break;
602 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
603 redo:
604 if (tok == '#') {
605 /* horrible gas comment */
606 while (tok != TOK_LINEFEED)
607 next();
608 } else if (tok == '.') {
609 asm_parse_directive(s1);
610 } else if (tok == TOK_PPNUM) {
611 const char *p;
612 int n;
613 p = tokc.cstr->data;
614 n = strtoul(p, (char **)&p, 10);
615 if (*p != '\0')
616 expect("':'");
617 /* new local label */
618 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
619 next();
620 skip(':');
621 goto redo;
622 } else if (tok >= TOK_IDENT) {
623 /* instruction or label */
624 opcode = tok;
625 next();
626 if (tok == ':') {
627 /* new label */
628 asm_new_label(s1, opcode, 0);
629 next();
630 goto redo;
631 } else if (tok == '=') {
632 int n;
633 next();
634 n = asm_int_expr(s1);
635 asm_new_label1(s1, opcode, 0, SHN_ABS, n);
636 goto redo;
637 } else {
638 asm_opcode(s1, opcode);
641 /* end of line */
642 if (tok != ';' && tok != TOK_LINEFEED){
643 expect("end of line");
645 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
646 next();
649 asm_free_labels(s1);
651 return 0;
654 /* Assemble the current file */
655 static int tcc_assemble(TCCState *s1, int do_preprocess)
657 Sym *define_start;
658 int ret;
660 preprocess_init(s1);
662 /* default section is text */
663 cur_text_section = text_section;
664 ind = cur_text_section->data_offset;
666 define_start = define_stack;
668 ret = tcc_assemble_internal(s1, do_preprocess);
670 cur_text_section->data_offset = ind;
672 free_defines(define_start);
674 return ret;
677 /********************************************************************/
678 /* GCC inline asm support */
680 /* assemble the string 'str' in the current C compilation unit without
681 C preprocessing. NOTE: str is modified by modifying the '\0' at the
682 end */
683 static void tcc_assemble_inline(TCCState *s1, char *str, int len)
685 BufferedFile *bf, *saved_file;
686 int saved_parse_flags, *saved_macro_ptr;
688 bf = tcc_malloc(sizeof(BufferedFile));
689 memset(bf, 0, sizeof(BufferedFile));
690 bf->fd = -1;
691 bf->buf_ptr = str;
692 bf->buf_end = str + len;
693 str[len] = CH_EOB;
694 /* same name as current file so that errors are correctly
695 reported */
696 pstrcpy(bf->filename, sizeof(bf->filename), file->filename);
697 bf->line_num = file->line_num;
698 saved_file = file;
699 file = bf;
700 saved_parse_flags = parse_flags;
701 saved_macro_ptr = macro_ptr;
702 macro_ptr = NULL;
704 tcc_assemble_internal(s1, 0);
706 parse_flags = saved_parse_flags;
707 macro_ptr = saved_macro_ptr;
708 file = saved_file;
709 tcc_free(bf);
712 /* find a constraint by its number or id (gcc 3 extended
713 syntax). return -1 if not found. Return in *pp in char after the
714 constraint */
715 static int find_constraint(ASMOperand *operands, int nb_operands,
716 const char *name, const char **pp)
718 int index;
719 TokenSym *ts;
720 const char *p;
722 if (isnum(*name)) {
723 index = 0;
724 while (isnum(*name)) {
725 index = (index * 10) + (*name) - '0';
726 name++;
728 if ((unsigned)index >= nb_operands)
729 index = -1;
730 } else if (*name == '[') {
731 name++;
732 p = strchr(name, ']');
733 if (p) {
734 ts = tok_alloc(name, p - name);
735 for(index = 0; index < nb_operands; index++) {
736 if (operands[index].id == ts->tok)
737 goto found;
739 index = -1;
740 found:
741 name = p + 1;
742 } else {
743 index = -1;
745 } else {
746 index = -1;
748 if (pp)
749 *pp = name;
750 return index;
753 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
754 int nb_outputs,
755 CString *out_str, CString *in_str)
757 int c, index, modifier;
758 const char *str;
759 ASMOperand *op;
760 SValue sv;
762 cstr_new(out_str);
763 str = in_str->data;
764 for(;;) {
765 c = *str++;
766 if (c == '%') {
767 if (*str == '%') {
768 str++;
769 goto add_char;
771 modifier = 0;
772 if (*str == 'c' || *str == 'n' ||
773 *str == 'b' || *str == 'w' || *str == 'h')
774 modifier = *str++;
775 index = find_constraint(operands, nb_operands, str, &str);
776 if (index < 0)
777 error("invalid operand reference after %%");
778 op = &operands[index];
779 sv = *op->vt;
780 if (op->reg >= 0) {
781 sv.r = op->reg;
782 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL)
783 sv.r |= VT_LVAL;
785 subst_asm_operand(out_str, &sv, modifier);
786 } else {
787 add_char:
788 cstr_ccat(out_str, c);
789 if (c == '\0')
790 break;
796 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
797 int is_output)
799 ASMOperand *op;
800 int nb_operands;
802 if (tok != ':') {
803 nb_operands = *nb_operands_ptr;
804 for(;;) {
805 if (nb_operands >= MAX_ASM_OPERANDS)
806 error("too many asm operands");
807 op = &operands[nb_operands++];
808 op->id = 0;
809 if (tok == '[') {
810 next();
811 if (tok < TOK_IDENT)
812 expect("identifier");
813 op->id = tok;
814 next();
815 skip(']');
817 if (tok != TOK_STR)
818 expect("string constant");
819 op->constraint = tcc_malloc(tokc.cstr->size);
820 strcpy(op->constraint, tokc.cstr->data);
821 next();
822 skip('(');
823 gexpr();
824 if (is_output) {
825 test_lvalue();
826 } else {
827 /* we want to avoid LLOCAL case, except when the 'm'
828 constraint is used. Note that it may come from
829 register storage, so we need to convert (reg)
830 case */
831 if ((vtop->r & VT_LVAL) &&
832 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
833 (vtop->r & VT_VALMASK) < VT_CONST) &&
834 !strchr(op->constraint, 'm')) {
835 gv(RC_INT);
838 op->vt = vtop;
839 skip(')');
840 if (tok == ',') {
841 next();
842 } else {
843 break;
846 *nb_operands_ptr = nb_operands;
850 static void parse_asm_str(CString *astr)
852 skip('(');
853 /* read the string */
854 if (tok != TOK_STR)
855 expect("string constant");
856 cstr_new(astr);
857 while (tok == TOK_STR) {
858 /* XXX: add \0 handling too ? */
859 cstr_cat(astr, tokc.cstr->data);
860 next();
862 cstr_ccat(astr, '\0');
865 /* parse the GCC asm() instruction */
866 static void asm_instr(void)
868 CString astr, astr1;
869 ASMOperand operands[MAX_ASM_OPERANDS];
870 int nb_inputs, nb_outputs, nb_operands, i, must_subst, out_reg;
871 uint8_t clobber_regs[NB_ASM_REGS];
873 next();
874 /* since we always generate the asm() instruction, we can ignore
875 volatile */
876 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
877 next();
879 parse_asm_str(&astr);
880 nb_operands = 0;
881 nb_outputs = 0;
882 must_subst = 0;
883 memset(clobber_regs, 0, sizeof(clobber_regs));
884 if (tok == ':') {
885 next();
886 must_subst = 1;
887 /* output args */
888 parse_asm_operands(operands, &nb_operands, 1);
889 nb_outputs = nb_operands;
890 if (tok == ':') {
891 next();
892 /* input args */
893 parse_asm_operands(operands, &nb_operands, 0);
894 if (tok == ':') {
895 /* clobber list */
896 /* XXX: handle registers */
897 next();
898 for(;;) {
899 if (tok != TOK_STR)
900 expect("string constant");
901 asm_clobber(clobber_regs, tokc.cstr->data);
902 next();
903 if (tok == ',') {
904 next();
905 } else {
906 break;
912 skip(')');
913 /* NOTE: we do not eat the ';' so that we can restore the current
914 token after the assembler parsing */
915 if (tok != ';')
916 expect("';'");
917 nb_inputs = nb_operands - nb_outputs;
919 /* save all values in the memory */
920 save_regs(0);
922 /* compute constraints */
923 asm_compute_constraints(operands, nb_operands, nb_outputs,
924 clobber_regs, &out_reg);
926 /* substitute the operands in the asm string. No substitution is
927 done if no operands (GCC behaviour) */
928 #ifdef ASM_DEBUG
929 printf("asm: \"%s\"\n", (char *)astr.data);
930 #endif
931 if (must_subst) {
932 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
933 cstr_free(&astr);
934 } else {
935 astr1 = astr;
937 #ifdef ASM_DEBUG
938 printf("subst_asm: \"%s\"\n", (char *)astr1.data);
939 #endif
941 /* generate loads */
942 asm_gen_code(operands, nb_operands, nb_outputs, 0,
943 clobber_regs, out_reg);
945 /* assemble the string with tcc internal assembler */
946 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
948 /* restore the current C token */
949 next();
951 /* store the output values if needed */
952 asm_gen_code(operands, nb_operands, nb_outputs, 1,
953 clobber_regs, out_reg);
955 /* free everything */
956 for(i=0;i<nb_operands;i++) {
957 ASMOperand *op;
958 op = &operands[i];
959 tcc_free(op->constraint);
960 vpop();
962 cstr_free(&astr1);
965 static void asm_global_instr(void)
967 CString astr;
969 next();
970 parse_asm_str(&astr);
971 skip(')');
972 /* NOTE: we do not eat the ';' so that we can restore the current
973 token after the assembler parsing */
974 if (tok != ';')
975 expect("';'");
977 #ifdef ASM_DEBUG
978 printf("asm_global: \"%s\"\n", (char *)astr.data);
979 #endif
980 cur_text_section = text_section;
981 ind = cur_text_section->data_offset;
983 /* assemble the string with tcc internal assembler */
984 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
986 cur_text_section->data_offset = ind;
988 /* restore the current C token */
989 next();
991 cstr_free(&astr);