History

absolute symbols support - .org, .fill and .previous directives

bellard committed 22 years ago

Commit 0bd402d1 parent 96684991 file · +189 −36Browse files

Unified · Split

tccasm.c+189−36
@@ -28,6 +28,8 @@ static int asm_get_local_label_name(TCCState *s1, unsigned int n) · 27 unmodified lines
28 return ts->tok;28 return ts->tok;
29}29}
30 30
31static void asm_expr(TCCState *s1, ExprValue *pe);
32
31/* We do not use the C expression parser to handle symbols. Maybe the33/* We do not use the C expression parser to handle symbols. Maybe the
32 C expression parser could be tweaked to do so. */34 C expression parser could be tweaked to do so. */
33 35
@@ -91,6 +93,11 @@ static void asm_expr_unary(TCCState *s1, ExprValue *pe) · 57 unmodified lines
91 pe->sym = NULL;93 pe->sym = NULL;
92 next();94 next();
93 break;95 break;
96 case '(':
97 next();
98 asm_expr(s1, pe);
99 skip(')');
100 break;
94 default:101 default:
95 if (tok >= TOK_IDENT) {102 if (tok >= TOK_IDENT) {
96 /* label case : if the label was not found, add one */103 /* label case : if the label was not found, add one */
@@ -100,8 +107,14 @@ static void asm_expr_unary(TCCState *s1, ExprValue *pe) · 3 unmodified lines
100 /* NOTE: by default, the symbol is global */107 /* NOTE: by default, the symbol is global */
101 sym->type.t = VT_VOID;108 sym->type.t = VT_VOID;
102 }109 }
103 pe->v = 0;110 if (sym->r == SHN_ABS) {
104 pe->sym = sym;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;
117 }
105 next();118 next();
106 } else {119 } else {
107 error("bad expression syntax [%s]", get_tok_str(tok, &tokc));120 error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
@@ -241,7 +254,8 @@ static int asm_int_expr(TCCState *s1) · 133 unmodified lines
241 254
242/* NOTE: the same name space as C labels is used to avoid using too255/* NOTE: the same name space as C labels is used to avoid using too
243 much memory when storing labels in TokenStrings */256 much memory when storing labels in TokenStrings */
244static void asm_new_label(TCCState *s1, int label, int is_local)257static void asm_new_label1(TCCState *s1, int label, int is_local,
258 int sh_num, int value)
245{259{
246 Sym *sym;260 Sym *sym;
247 261
@@ -262,18 +276,29 @@ static void asm_new_label(TCCState *s1, int label, int is_local) · 14 unmodified lines
262 sym = label_push(&s1->asm_labels, label, 0);276 sym = label_push(&s1->asm_labels, label, 0);
263 sym->type.t = VT_STATIC | VT_VOID;277 sym->type.t = VT_STATIC | VT_VOID;
264 }278 }
265 sym->r = cur_text_section->sh_num;279 sym->r = sh_num;
266 sym->next = (void *)ind;280 sym->next = (void *)value;
281}
282
283static void asm_new_label(TCCState *s1, int label, int is_local)
284{
285 asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind);
267}286}
268 287
269static void asm_free_labels(TCCState *st)288static void asm_free_labels(TCCState *st)
270{289{
271 Sym *s, *s1;290 Sym *s, *s1;
291 Section *sec;
292
272 for(s = st->asm_labels; s != NULL; s = s1) {293 for(s = st->asm_labels; s != NULL; s = s1) {
273 s1 = s->prev;294 s1 = s->prev;
274 /* define symbol value in object file */295 /* define symbol value in object file */
275 if (s->r) {296 if (s->r) {
276 put_extern_sym(s, st->sections[s->r], (long)s->next, 0);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);
277 }302 }
278 /* remove label */303 /* remove label */
279 table_ident[s->v - TOK_IDENT]->sym_label = NULL;304 table_ident[s->v - TOK_IDENT]->sym_label = NULL;
@@ -282,15 +307,20 @@ static void asm_free_labels(TCCState *st) · 2 unmodified lines
282 st->asm_labels = NULL;307 st->asm_labels = NULL;
283}308}
284 309
285static void use_section(TCCState *s1, const char *name)310static void use_section1(TCCState *s1, Section *sec)
286{311{
287 Section *sec;
288 sec = find_section(s1, name);
289 cur_text_section->data_offset = ind;312 cur_text_section->data_offset = ind;
290 cur_text_section = sec;313 cur_text_section = sec;
291 ind = cur_text_section->data_offset;314 ind = cur_text_section->data_offset;
292}315}
293 316
317static void use_section(TCCState *s1, const char *name)
318{
319 Section *sec;
320 sec = find_section(s1, name);
321 use_section1(s1, sec);
322}
323
294static void asm_parse_directive(TCCState *s1)324static void asm_parse_directive(TCCState *s1)
295{325{
296 int n, offset, v, size, tok1;326 int n, offset, v, size, tok1;
@@ -312,6 +342,9 @@ static void asm_parse_directive(TCCState *s1) · 15 unmodified lines
312 error("alignment must be a positive power of two");342 error("alignment must be a positive power of two");
313 offset = (ind + n - 1) & -n;343 offset = (ind + n - 1) & -n;
314 size = offset - ind;344 size = offset - ind;
345 /* the section must have a compatible alignment */
346 if (sec->sh_addralign < n)
347 sec->sh_addralign = n;
315 } else {348 } else {
316 size = n;349 size = n;
317 }350 }
@@ -320,6 +353,7 @@ static void asm_parse_directive(TCCState *s1) · 2 unmodified lines
320 next();353 next();
321 v = asm_int_expr(s1);354 v = asm_int_expr(s1);
322 }355 }
356 zero_pad:
323 if (sec->sh_type != SHT_NOBITS) {357 if (sec->sh_type != SHT_NOBITS) {
324 sec->data_offset = ind;358 sec->data_offset = ind;
325 ptr = section_ptr_add(sec, size);359 ptr = section_ptr_add(sec, size);
@@ -361,6 +395,61 @@ static void asm_parse_directive(TCCState *s1) · 35 unmodified lines
361 next();395 next();
362 }396 }
363 break;397 break;
398 case TOK_ASM_fill:
399 {
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;
407 }
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;
416 }
417 if (size > 8)
418 size = 8;
419 if (tok == ',') {
420 next();
421 val = asm_int_expr(s1);
422 }
423 }
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]);
436 }
437 }
438 }
439 break;
440 case TOK_ASM_org:
441 {
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;
451 }
452 break;
364 case TOK_ASM_globl:453 case TOK_ASM_globl:
365 case TOK_ASM_global:454 case TOK_ASM_global:
366 { 455 {
@@ -420,9 +509,28 @@ static void asm_parse_directive(TCCState *s1) · 53 unmodified lines
420 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));509 pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL));
421 next();510 next();
422 }511 }
512 if (tok == ',') {
513 /* skip section options */
514 next();
515 if (tok != TOK_STR)
516 expect("string constant");
517 next();
518 }
519 last_text_section = cur_text_section;
423 use_section(s1, sname);520 use_section(s1, sname);
424 }521 }
425 break;522 break;
523 case TOK_ASM_previous:
524 {
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;
532 }
533 break;
426 default:534 default:
427 error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));535 error("unknown assembler directive '.%s'", get_tok_str(tok, NULL));
428 break;536 break;
@@ -471,7 +579,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 42 unmodified lines
471 579
472 ch = file->buf_ptr[0];580 ch = file->buf_ptr[0];
473 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;581 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
474 parse_flags = 0;582 parse_flags = PARSE_FLAG_ASM_COMMENTS;
475 if (do_preprocess)583 if (do_preprocess)
476 parse_flags |= PARSE_FLAG_PREPROCESS;584 parse_flags |= PARSE_FLAG_PREPROCESS;
477 next();585 next();
@@ -507,6 +615,12 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 29 unmodified lines
507 asm_new_label(s1, opcode, 0);615 asm_new_label(s1, opcode, 0);
508 next();616 next();
509 goto redo;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;
510 } else {624 } else {
511 asm_opcode(s1, opcode);625 asm_opcode(s1, opcode);
512 }626 }
@@ -527,6 +641,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 14 unmodified lines
527/* Assemble the current file */641/* Assemble the current file */
528static int tcc_assemble(TCCState *s1, int do_preprocess)642static int tcc_assemble(TCCState *s1, int do_preprocess)
529{643{
644 Sym *define_start;
530 int ret;645 int ret;
531 646
532 preprocess_init(s1);647 preprocess_init(s1);
@@ -535,9 +650,14 @@ static int tcc_assemble(TCCState *s1, int do_preprocess) · 2 unmodified lines
535 cur_text_section = text_section;650 cur_text_section = text_section;
536 ind = cur_text_section->data_offset;651 ind = cur_text_section->data_offset;
537 652
653 define_start = define_stack;
654
538 ret = tcc_assemble_internal(s1, do_preprocess);655 ret = tcc_assemble_internal(s1, do_preprocess);
539 656
540 cur_text_section->data_offset = ind;657 cur_text_section->data_offset = ind;
658
659 free_defines(define_start);
660
541 return ret;661 return ret;
542}662}
543 663
@@ -691,12 +811,14 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, · 147 unmodified lines
691 if (is_output) {811 if (is_output) {
692 test_lvalue();812 test_lvalue();
693 } else {813 } else {
694 /* we want to avoid LLOCAL case. note that it may come814 /* we want to avoid LLOCAL case, except when the 'm'
695 from register storage, so we need to convert (reg)815 constraint is used. Note that it may come from
816 register storage, so we need to convert (reg)
696 case */817 case */
697 if ((vtop->r & VT_LVAL) &&818 if ((vtop->r & VT_LVAL) &&
698 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||819 ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
699 (vtop->r & VT_VALMASK) < VT_CONST)) {820 (vtop->r & VT_VALMASK) < VT_CONST) &&
821 !strchr(op->constraint, 'm')) {
700 gv(RC_INT);822 gv(RC_INT);
701 }823 }
702 }824 }
@@ -712,14 +834,27 @@ static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, · 9 unmodified lines
712 }834 }
713}835}
714 836
837static void parse_asm_str(CString *astr)
838{
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();
848 }
849 cstr_ccat(astr, '\0');
850}
851
715/* parse the GCC asm() instruction */852/* parse the GCC asm() instruction */
716static void asm_instr(void)853static void asm_instr(void)
717{854{
718 CString astr, astr1;855 CString astr, astr1;
719 ASMOperand operands[MAX_ASM_OPERANDS];856 ASMOperand operands[MAX_ASM_OPERANDS];
720 int nb_inputs, nb_outputs, nb_operands, i;857 int nb_inputs, nb_outputs, nb_operands, i, must_subst, out_reg;
721 uint8_t input_regs_allocated[NB_ASM_REGS];
722 uint8_t output_regs_allocated[NB_ASM_REGS];
723 uint8_t clobber_regs[NB_ASM_REGS];858 uint8_t clobber_regs[NB_ASM_REGS];
724 859
725 next();860 next();
@@ -728,22 +863,14 @@ static void asm_instr(void) · 2 unmodified lines
728 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {863 if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
729 next();864 next();
730 }865 }
731 skip('(');866 parse_asm_str(&astr);
732 /* read the string */
733 if (tok != TOK_STR)
734 expect("string constant");
735 cstr_new(&astr);
736 while (tok == TOK_STR) {
737 /* XXX: add \0 handling too ? */
738 cstr_cat(&astr, tokc.cstr->data);
739 next();
740 }
741 cstr_ccat(&astr, '\0');
742 nb_operands = 0;867 nb_operands = 0;
743 nb_outputs = 0;868 nb_outputs = 0;
869 must_subst = 0;
744 memset(clobber_regs, 0, sizeof(clobber_regs));870 memset(clobber_regs, 0, sizeof(clobber_regs));
745 if (tok == ':') {871 if (tok == ':') {
746 next();872 next();
873 must_subst = 1;
747 /* output args */874 /* output args */
748 parse_asm_operands(operands, &nb_operands, 1);875 parse_asm_operands(operands, &nb_operands, 1);
749 nb_outputs = nb_operands;876 nb_outputs = nb_operands;
@@ -780,19 +907,15 @@ static void asm_instr(void) · 30 unmodified lines
780 save_regs(0);907 save_regs(0);
781 908
782 /* compute constraints */909 /* compute constraints */
783 asm_compute_constraints(input_regs_allocated, 910 asm_compute_constraints(operands, nb_operands, nb_outputs,
784 operands, nb_operands, nb_outputs, 0, 911 clobber_regs, &out_reg);
785 NULL);
786 asm_compute_constraints(output_regs_allocated,
787 operands, nb_operands, nb_outputs, 1,
788 input_regs_allocated);
789 912
790 /* substitute the operands in the asm string. No substitution is913 /* substitute the operands in the asm string. No substitution is
791 done if no operands (GCC behaviour) */914 done if no operands (GCC behaviour) */
792#ifdef ASM_DEBUG915#ifdef ASM_DEBUG
793 printf("asm: \"%s\"\n", (char *)astr.data);916 printf("asm: \"%s\"\n", (char *)astr.data);
794#endif917#endif
795 if (nb_operands > 0) {918 if (must_subst) {
796 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);919 subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
797 cstr_free(&astr);920 cstr_free(&astr);
798 } else {921 } else {
@@ -803,7 +926,8 @@ static void asm_instr(void) · 4 unmodified lines
803#endif926#endif
804 927
805 /* generate loads */928 /* generate loads */
806 asm_gen_code(operands, nb_operands, nb_outputs, 0, clobber_regs); 929 asm_gen_code(operands, nb_operands, nb_outputs, 0,
930 clobber_regs, out_reg);
807 931
808 /* assemble the string with tcc internal assembler */932 /* assemble the string with tcc internal assembler */
809 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);933 tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
@@ -812,7 +936,8 @@ static void asm_instr(void) · 2 unmodified lines
812 next();936 next();
813 937
814 /* store the output values if needed */938 /* store the output values if needed */
815 asm_gen_code(operands, nb_operands, nb_outputs, 1, clobber_regs); 939 asm_gen_code(operands, nb_operands, nb_outputs, 1,
940 clobber_regs, out_reg);
816 941
817 /* free everything */942 /* free everything */
818 for(i=0;i<nb_operands;i++) {943 for(i=0;i<nb_operands;i++) {
@@ -824,3 +949,31 @@ static void asm_instr(void) · 5 unmodified lines
824 cstr_free(&astr1);949 cstr_free(&astr1);
825}950}
826 951
952static void asm_global_instr(void)
953{
954 CString astr;
955
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("';'");
963
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;
969
970 /* assemble the string with tcc internal assembler */
971 tcc_assemble_inline(tcc_state, astr.data, astr.size - 1);
972
973 cur_text_section->data_offset = ind;
974
975 /* restore the current C token */
976 next();
977
978 cstr_free(&astr);
979}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t