Unified · Split
| @@ -28,6 +28,8 @@ static int asm_get_local_label_name(TCCState *s1, unsigned int n) · 27 unmodified lines | ||
| 28 | 28 | return ts->tok; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | +static void asm_expr(TCCState *s1, ExprValue *pe); | |
| 32 | + | |
| 31 | 33 | /* We do not use the C expression parser to handle symbols. Maybe the |
| 32 | 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 | 93 | pe->sym = NULL; |
| 92 | 94 | next(); |
| 93 | 95 | break; |
| 96 | + case '(': | |
| 97 | + next(); | |
| 98 | + asm_expr(s1, pe); | |
| 99 | + skip(')'); | |
| 100 | + break; | |
| 94 | 101 | default: |
| 95 | 102 | if (tok >= TOK_IDENT) { |
| 96 | 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 | 107 | /* NOTE: by default, the symbol is global */ |
| 101 | 108 | sym->type.t = VT_VOID; |
| 102 | 109 | } |
| 103 | − pe->v = 0; | |
| 104 | − pe->sym = sym; | |
| 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; | |
| 117 | + } | |
| 105 | 118 | next(); |
| 106 | 119 | } else { |
| 107 | 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 | 255 | /* NOTE: the same name space as C labels is used to avoid using too |
| 243 | 256 | much memory when storing labels in TokenStrings */ |
| 244 | −static void asm_new_label(TCCState *s1, int label, int is_local) | |
| 257 | +static void asm_new_label1(TCCState *s1, int label, int is_local, | |
| 258 | + int sh_num, int value) | |
| 245 | 259 | { |
| 246 | 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 | 276 | sym = label_push(&s1->asm_labels, label, 0); |
| 263 | 277 | sym->type.t = VT_STATIC | VT_VOID; |
| 264 | 278 | } |
| 265 | − sym->r = cur_text_section->sh_num; | |
| 266 | − sym->next = (void *)ind; | |
| 279 | + sym->r = sh_num; | |
| 280 | + sym->next = (void *)value; | |
| 281 | +} | |
| 282 | + | |
| 283 | +static 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 | |
| 269 | 288 | static void asm_free_labels(TCCState *st) |
| 270 | 289 | { |
| 271 | 290 | Sym *s, *s1; |
| 291 | + Section *sec; | |
| 292 | + | |
| 272 | 293 | for(s = st->asm_labels; s != NULL; s = s1) { |
| 273 | 294 | s1 = s->prev; |
| 274 | 295 | /* define symbol value in object file */ |
| 275 | 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 | 303 | /* remove label */ |
| 279 | 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 | 307 | st->asm_labels = NULL; |
| 283 | 308 | } |
| 284 | 309 | |
| 285 | −static void use_section(TCCState *s1, const char *name) | |
| 310 | +static void use_section1(TCCState *s1, Section *sec) | |
| 286 | 311 | { |
| 287 | − Section *sec; | |
| 288 | − sec = find_section(s1, name); | |
| 289 | 312 | cur_text_section->data_offset = ind; |
| 290 | 313 | cur_text_section = sec; |
| 291 | 314 | ind = cur_text_section->data_offset; |
| 292 | 315 | } |
| 293 | 316 | |
| 317 | +static 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 | + | |
| 294 | 324 | static void asm_parse_directive(TCCState *s1) |
| 295 | 325 | { |
| 296 | 326 | int n, offset, v, size, tok1; |
| @@ -312,6 +342,9 @@ static void asm_parse_directive(TCCState *s1) · 15 unmodified lines | ||
| 312 | 342 | error("alignment must be a positive power of two"); |
| 313 | 343 | offset = (ind + n - 1) & -n; |
| 314 | 344 | size = offset - ind; |
| 345 | + /* the section must have a compatible alignment */ | |
| 346 | + if (sec->sh_addralign < n) | |
| 347 | + sec->sh_addralign = n; | |
| 315 | 348 | } else { |
| 316 | 349 | size = n; |
| 317 | 350 | } |
| @@ -320,6 +353,7 @@ static void asm_parse_directive(TCCState *s1) · 2 unmodified lines | ||
| 320 | 353 | next(); |
| 321 | 354 | v = asm_int_expr(s1); |
| 322 | 355 | } |
| 356 | + zero_pad: | |
| 323 | 357 | if (sec->sh_type != SHT_NOBITS) { |
| 324 | 358 | sec->data_offset = ind; |
| 325 | 359 | ptr = section_ptr_add(sec, size); |
| @@ -361,6 +395,61 @@ static void asm_parse_directive(TCCState *s1) · 35 unmodified lines | ||
| 361 | 395 | next(); |
| 362 | 396 | } |
| 363 | 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 | 453 | case TOK_ASM_globl: |
| 365 | 454 | case TOK_ASM_global: |
| 366 | 455 | { |
| @@ -420,9 +509,28 @@ static void asm_parse_directive(TCCState *s1) · 53 unmodified lines | ||
| 420 | 509 | pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL)); |
| 421 | 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 | 520 | use_section(s1, sname); |
| 424 | 521 | } |
| 425 | 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 | 534 | default: |
| 427 | 535 | error("unknown assembler directive '.%s'", get_tok_str(tok, NULL)); |
| 428 | 536 | break; |
| @@ -471,7 +579,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 42 unmodified lines | ||
| 471 | 579 | |
| 472 | 580 | ch = file->buf_ptr[0]; |
| 473 | 581 | tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; |
| 474 | − parse_flags = 0; | |
| 582 | + parse_flags = PARSE_FLAG_ASM_COMMENTS; | |
| 475 | 583 | if (do_preprocess) |
| 476 | 584 | parse_flags |= PARSE_FLAG_PREPROCESS; |
| 477 | 585 | next(); |
| @@ -507,6 +615,12 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 29 unmodified lines | ||
| 507 | 615 | asm_new_label(s1, opcode, 0); |
| 508 | 616 | next(); |
| 509 | 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 | 624 | } else { |
| 511 | 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 | 641 | /* Assemble the current file */ |
| 528 | 642 | static int tcc_assemble(TCCState *s1, int do_preprocess) |
| 529 | 643 | { |
| 644 | + Sym *define_start; | |
| 530 | 645 | int ret; |
| 531 | 646 | |
| 532 | 647 | preprocess_init(s1); |
| @@ -535,9 +650,14 @@ static int tcc_assemble(TCCState *s1, int do_preprocess) · 2 unmodified lines | ||
| 535 | 650 | cur_text_section = text_section; |
| 536 | 651 | ind = cur_text_section->data_offset; |
| 537 | 652 | |
| 653 | + define_start = define_stack; | |
| 654 | + | |
| 538 | 655 | ret = tcc_assemble_internal(s1, do_preprocess); |
| 539 | 656 | |
| 540 | 657 | cur_text_section->data_offset = ind; |
| 658 | + | |
| 659 | + free_defines(define_start); | |
| 660 | + | |
| 541 | 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 | 811 | if (is_output) { |
| 692 | 812 | test_lvalue(); |
| 693 | 813 | } else { |
| 694 | − /* we want to avoid LLOCAL case. note that it may come | |
| 695 | − from register storage, so we need to convert (reg) | |
| 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) | |
| 696 | 817 | case */ |
| 697 | 818 | if ((vtop->r & VT_LVAL) && |
| 698 | 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 | 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 | |
| 837 | +static 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 | 852 | /* parse the GCC asm() instruction */ |
| 716 | 853 | static void asm_instr(void) |
| 717 | 854 | { |
| 718 | 855 | CString astr, astr1; |
| 719 | 856 | ASMOperand operands[MAX_ASM_OPERANDS]; |
| 720 | − int nb_inputs, nb_outputs, nb_operands, i; | |
| 721 | − uint8_t input_regs_allocated[NB_ASM_REGS]; | |
| 722 | − uint8_t output_regs_allocated[NB_ASM_REGS]; | |
| 857 | + int nb_inputs, nb_outputs, nb_operands, i, must_subst, out_reg; | |
| 723 | 858 | uint8_t clobber_regs[NB_ASM_REGS]; |
| 724 | 859 | |
| 725 | 860 | next(); |
| @@ -728,22 +863,14 @@ static void asm_instr(void) · 2 unmodified lines | ||
| 728 | 863 | if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) { |
| 729 | 864 | next(); |
| 730 | 865 | } |
| 731 | − skip('('); | |
| 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'); | |
| 866 | + parse_asm_str(&astr); | |
| 742 | 867 | nb_operands = 0; |
| 743 | 868 | nb_outputs = 0; |
| 869 | + must_subst = 0; | |
| 744 | 870 | memset(clobber_regs, 0, sizeof(clobber_regs)); |
| 745 | 871 | if (tok == ':') { |
| 746 | 872 | next(); |
| 873 | + must_subst = 1; | |
| 747 | 874 | /* output args */ |
| 748 | 875 | parse_asm_operands(operands, &nb_operands, 1); |
| 749 | 876 | nb_outputs = nb_operands; |
| @@ -780,19 +907,15 @@ static void asm_instr(void) · 30 unmodified lines | ||
| 780 | 907 | save_regs(0); |
| 781 | 908 | |
| 782 | 909 | /* compute constraints */ |
| 783 | − asm_compute_constraints(input_regs_allocated, | |
| 784 | − operands, nb_operands, nb_outputs, 0, | |
| 785 | − NULL); | |
| 786 | − asm_compute_constraints(output_regs_allocated, | |
| 787 | − operands, nb_operands, nb_outputs, 1, | |
| 788 | − input_regs_allocated); | |
| 910 | + asm_compute_constraints(operands, nb_operands, nb_outputs, | |
| 911 | + clobber_regs, &out_reg); | |
| 789 | 912 | |
| 790 | 913 | /* substitute the operands in the asm string. No substitution is |
| 791 | 914 | done if no operands (GCC behaviour) */ |
| 792 | 915 | #ifdef ASM_DEBUG |
| 793 | 916 | printf("asm: \"%s\"\n", (char *)astr.data); |
| 794 | 917 | #endif |
| 795 | − if (nb_operands > 0) { | |
| 918 | + if (must_subst) { | |
| 796 | 919 | subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr); |
| 797 | 920 | cstr_free(&astr); |
| 798 | 921 | } else { |
| @@ -803,7 +926,8 @@ static void asm_instr(void) · 4 unmodified lines | ||
| 803 | 926 | #endif |
| 804 | 927 | |
| 805 | 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 | 932 | /* assemble the string with tcc internal assembler */ |
| 809 | 933 | tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1); |
| @@ -812,7 +936,8 @@ static void asm_instr(void) · 2 unmodified lines | ||
| 812 | 936 | next(); |
| 813 | 937 | |
| 814 | 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 | 942 | /* free everything */ |
| 818 | 943 | for(i=0;i<nb_operands;i++) { |
| @@ -824,3 +949,31 @@ static void asm_instr(void) · 5 unmodified lines | ||
| 824 | 949 | cstr_free(&astr1); |
| 825 | 950 | } |
| 826 | 951 | |
| 952 | +static 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 | +} | |