absolute symbols support - .org, .fill and .previous directives
[tinycc.git] / tccasm.c
blobf781f20d26343431fba6756d1b445e0fa875a764
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:
470 const uint8_t *p;
471 int i;
473 next();
474 if (tok != TOK_STR)
475 expect("string constant");
476 p = tokc.cstr->data;
477 for(i = 0; i < tokc.cstr->size; i++)
478 g(p[i]);
479 next();
481 break;
482 case TOK_ASM_text:
483 case TOK_ASM_data:
484 case TOK_ASM_bss:
486 char sname[64];
487 tok1 = tok;
488 n = 0;
489 next();
490 if (tok != ';' && tok != TOK_LINEFEED) {
491 n = asm_int_expr(s1);
492 next();
494 sprintf(sname, (n?".%s%d":".%s"), get_tok_str(tok1, NULL), n);
495 use_section(s1, sname);
497 break;
498 case TOK_SECTION1:
500 char sname[256];
502 /* XXX: support more options */
503 next();
504 sname[0] = '\0';
505 while (tok != ';' && tok != TOK_LINEFEED && tok != ',') {
506 if (tok == TOK_STR)
507 pstrcat(sname, sizeof(sname), tokc.cstr->data);
508 else
509 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
510 next();
512 if (tok == ',') {
513 /* skip section options */
514 next();
515 if (tok != TOK_STR)
516 expect("string constant");
517 next();
519 last_text_section = cur_text_section;
520 use_section(s1, sname);
522 break;
523 case TOK_ASM_previous:
525 Section *sec;
526 next();
527 if (!last_text_section)
528 error("no previous section referenced");
529 sec = cur_text_section;
530 use_section1(s1, last_text_section);
531 last_text_section = sec;
533 break;
534 default:
535 error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
536 break;
541 /* assemble a file */
542 static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
544 int opcode;
546 #if 0
547 /* print stats about opcodes */
549 const ASMInstr *pa;
550 int freq[4];
551 int op_vals[500];
552 int nb_op_vals, i, j;
554 nb_op_vals = 0;
555 memset(freq, 0, sizeof(freq));
556 for(pa = asm_instrs; pa->sym != 0; pa++) {
557 freq[pa->nb_ops]++;
558 for(i=0;i<pa->nb_ops;i++) {
559 for(j=0;j<nb_op_vals;j++) {
560 if (pa->op_type[i] == op_vals[j])
561 goto found;
563 op_vals[nb_op_vals++] = pa->op_type[i];
564 found: ;
567 for(i=0;i<nb_op_vals;i++) {
568 int v = op_vals[i];
569 if ((v & (v - 1)) != 0)
570 printf("%3d: %08x\n", i, v);
572 printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
573 sizeof(asm_instrs), sizeof(asm_instrs) / sizeof(ASMInstr),
574 freq[0], freq[1], freq[2], freq[3]);
576 #endif
578 /* XXX: undefine C labels */
580 ch = file->buf_ptr[0];
581 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
582 parse_flags = PARSE_FLAG_ASM_COMMENTS;
583 if (do_preprocess)
584 parse_flags |= PARSE_FLAG_PREPROCESS;
585 next();
586 for(;;) {
587 if (tok == TOK_EOF)
588 break;
589 parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
590 redo:
591 if (tok == '#') {
592 /* horrible gas comment */
593 while (tok != TOK_LINEFEED)
594 next();
595 } else if (tok == '.') {
596 asm_parse_directive(s1);
597 } else if (tok == TOK_PPNUM) {
598 const char *p;
599 int n;
600 p = tokc.cstr->data;
601 n = strtoul(p, (char **)&p, 10);
602 if (*p != '\0')
603 expect("':'");
604 /* new local label */
605 asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
606 next();
607 skip(':');
608 goto redo;
609 } else if (tok >= TOK_IDENT) {
610 /* instruction or label */
611 opcode = tok;
612 next();
613 if (tok == ':') {
614 /* new label */
615 asm_new_label(s1, opcode, 0);
616 next();
617 goto redo;
618 } else if (tok == '=') {
619 int n;
620 next();
621 n = asm_int_expr(s1);
622 asm_new_label1(s1, opcode, 0, SHN_ABS, n);
623 goto redo;
624 } else {
625 asm_opcode(s1, opcode);
628 /* end of line */
629 if (tok != ';' && tok != TOK_LINEFEED){
630 expect("end of line");
632 parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
633 next();
636 asm_free_labels(s1);
638 return 0;
641 /* Assemble the current file */
642 static int tcc_assemble(TCCState *s1, int do_preprocess)
644 Sym *define_start;
645 int ret;
647 preprocess_init(s1);
649 /* default section is text */
650 cur_text_section = text_section;
651 ind = cur_text_section->data_offset;
653 define_start = define_stack;
655 ret = tcc_assemble_internal(s1, do_preprocess);
657 cur_text_section->data_offset = ind;
659 free_defines(define_start);
661 return ret;
664 /********************************************************************/
665 /* GCC inline asm support */
667 /* assemble the string 'str' in the current C compilation unit without
668 C preprocessing. NOTE: str is modified by modifying the '\0' at the
669 end */
670 static void tcc_assemble_inline(TCCState *s1, char *str, int len)
672 BufferedFile *bf, *saved_file;
673 int saved_parse_flags, *saved_macro_ptr;
675 bf = tcc_malloc(sizeof(BufferedFile));
676 memset(bf, 0, sizeof(BufferedFile));
677 bf->fd = -1;
678 bf->buf_ptr = str;
679 bf->buf_end = str + len;
680 str[len] = CH_EOB;
681 /* same name as current file so that errors are correctly
682 reported */
683 pstrcpy(bf->filename, sizeof(bf->filename), file->filename);
684 bf->line_num = file->line_num;
685 saved_file = file;
686 file = bf;
687 saved_parse_flags = parse_flags;
688 saved_macro_ptr = macro_ptr;
689 macro_ptr = NULL;
691 tcc_assemble_internal(s1, 0);
693 parse_flags = saved_parse_flags;
694 macro_ptr = saved_macro_ptr;
695 file = saved_file;
696 tcc_free(bf);
699 /* find a constraint by its number or id (gcc 3 extended
700 syntax). return -1 if not found. Return in *pp in char after the
701 constraint */
702 static int find_constraint(ASMOperand *operands, int nb_operands,
703 const char *name, const char **pp)
705 int index;
706 TokenSym *ts;
707 const char *p;
709 if (isnum(*name)) {
710 index = 0;
711 while (isnum(*name)) {
712 index = (index * 10) + (*name) - '0';
713 name++;
715 if ((unsigned)index >= nb_operands)
716 index = -1;
717 } else if (*name == '[') {
718 name++;
719 p = strchr(name, ']');
720 if (p) {
721 ts = tok_alloc(name, p - name);
722 for(index = 0; index < nb_operands; index++) {
723 if (operands[index].id == ts->tok)
724 goto found;
726 index = -1;
727 found:
728 name = p + 1;
729 } else {
730 index = -1;
732 } else {
733 index = -1;
735 if (pp)
736 *pp = name;
737 return index;
740 static void subst_asm_operands(ASMOperand *operands, int nb_operands,
741 int nb_outputs,
742 CString *out_str, CString *in_str)
744 int c, index, modifier;
745 const char *str;
746 ASMOperand *op;
747 SValue sv;
749 cstr_new(out_str);
750 str = in_str->data;
751 for(;;) {
752 c = *str++;
753 if (c == '%') {
754 if (*str == '%') {
755 str++;
756 goto add_char;
758 modifier = 0;
759 if (*str == 'c' || *str == 'n' ||
760 *str == 'b' || *str == 'w' || *str == 'h')
761 modifier = *str++;
762 index = find_constraint(operands, nb_operands, str, &str);
763 if (index < 0)
764 error("invalid operand reference after %%");
765 op = &operands[index];
766 sv = *op->vt;
767 if (op->reg >= 0) {
768 sv.r = op->reg;
769 if ((op->vt->r & VT_VALMASK) == VT_LLOCAL)
770 sv.r |= VT_LVAL;
772 subst_asm_operand(out_str, &sv, modifier);
773 } else {
774 add_char:
775 cstr_ccat(out_str, c);
776 if (c == '\0')
777 break;
783 static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
784 int is_output)
786 ASMOperand *op;
787 int nb_operands;
789 if (tok != ':') {
790 nb_operands = *nb_operands_ptr;
791 for(;;) {
792 if (nb_operands >= MAX_ASM_OPERANDS)
793 error("too many asm operands");
794 op = &operands[nb_operands++];
795 op->id = 0;
796 if (tok == '[') {
797 next();
798 if (tok < TOK_IDENT)
799 expect("identifier");
800 op->id = tok;
801 next();
802 skip(']');
804 if (tok != TOK_STR)
805 expect("string constant");
806 op->constraint = tcc_malloc(tokc.cstr->size);
807 strcpy(op->constraint, tokc.cstr->data);
808 next();
809 skip('(');
810 gexpr();
811 if (is_output) {
812 test_lvalue();
813 } else {
814 /* we want to avoid LLOCAL case, except when the 'm'
815 constraint is used. Note that it may come from
816 register storage, so we need to convert (reg)
817 case */
818 if ((vtop->r & VT_LVAL) &&
819 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
820 (vtop->r & VT_VALMASK) < VT_CONST) &&
821 !strchr(op->constraint, 'm')) {
822 gv(RC_INT);
825 op->vt = vtop;
826 skip(')');
827 if (tok == ',') {
828 next();
829 } else {
830 break;
833 *nb_operands_ptr = nb_operands;
837 static void parse_asm_str(CString *astr)
839 skip('(');
840 /* read the string */
841 if (tok != TOK_STR)
842 expect("string constant");
843 cstr_new(astr);
844 while (tok == TOK_STR) {
845 /* XXX: add \0 handling too ? */
846 cstr_cat(astr, tokc.cstr->data);
847 next();
849 cstr_ccat(astr, '\0');
852 /* parse the GCC asm() instruction */
853 static void asm_instr(void)
855 CString astr, astr1;
856 ASMOperand operands[MAX_ASM_OPERANDS];
857 int nb_inputs, nb_outputs, nb_operands, i, must_subst, out_reg;
858 uint8_t clobber_regs[NB_ASM_REGS];
860 next();
861 /* since we always generate the asm() instruction, we can ignore
862 volatile */
863 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
864 next();
866 parse_asm_str(&astr);
867 nb_operands = 0;
868 nb_outputs = 0;
869 must_subst = 0;
870 memset(clobber_regs, 0, sizeof(clobber_regs));
871 if (tok == ':') {
872 next();
873 must_subst = 1;
874 /* output args */
875 parse_asm_operands(operands, &nb_operands, 1);
876 nb_outputs = nb_operands;
877 if (tok == ':') {
878 next();
879 /* input args */
880 parse_asm_operands(operands, &nb_operands, 0);
881 if (tok == ':') {
882 /* clobber list */
883 /* XXX: handle registers */
884 next();
885 for(;;) {
886 if (tok != TOK_STR)
887 expect("string constant");
888 asm_clobber(clobber_regs, tokc.cstr->data);
889 next();
890 if (tok == ',') {
891 next();
892 } else {
893 break;
899 skip(')');
900 /* NOTE: we do not eat the ';' so that we can restore the current
901 token after the assembler parsing */
902 if (tok != ';')
903 expect("';'");
904 nb_inputs = nb_operands - nb_outputs;
906 /* save all values in the memory */
907 save_regs(0);
909 /* compute constraints */
910 asm_compute_constraints(operands, nb_operands, nb_outputs,
911 clobber_regs, &out_reg);
913 /* substitute the operands in the asm string. No substitution is
914 done if no operands (GCC behaviour) */
915 #ifdef ASM_DEBUG
916 printf("asm: \"%s\"\n", (char *)astr.data);
917 #endif
918 if (must_subst) {
919 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
920 cstr_free(&astr);
921 } else {
922 astr1 = astr;
924 #ifdef ASM_DEBUG
925 printf("subst_asm: \"%s\"\n", (char *)astr1.data);
926 #endif
928 /* generate loads */
929 asm_gen_code(operands, nb_operands, nb_outputs, 0,
930 clobber_regs, out_reg);
932 /* assemble the string with tcc internal assembler */
933 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
935 /* restore the current C token */
936 next();
938 /* store the output values if needed */
939 asm_gen_code(operands, nb_operands, nb_outputs, 1,
940 clobber_regs, out_reg);
942 /* free everything */
943 for(i=0;i<nb_operands;i++) {
944 ASMOperand *op;
945 op = &operands[i];
946 tcc_free(op->constraint);
947 vpop();
949 cstr_free(&astr1);
952 static void asm_global_instr(void)
954 CString astr;
956 next();
957 parse_asm_str(&astr);
958 skip(')');
959 /* NOTE: we do not eat the ';' so that we can restore the current
960 token after the assembler parsing */
961 if (tok != ';')
962 expect("';'");
964 #ifdef ASM_DEBUG
965 printf("asm_global: \"%s\"\n", (char *)astr.data);
966 #endif
967 cur_text_section = text_section;
968 ind = cur_text_section->data_offset;
970 /* assemble the string with tcc internal assembler */
971 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
973 cur_text_section->data_offset = ind;
975 /* restore the current C token */
976 next();
978 cstr_free(&astr);