| @@ -506,6 +506,123 @@ static void asm_long_multiplication_opcode(TCCState *s1, int token) · 505 unmodified lines |
| 506 | } | 506 | } |
| 507 | } | 507 | } |
| 508 | | 508 | |
| | 509 | static void asm_single_data_transfer_opcode(TCCState *s1, int token) |
| | 510 | { |
| | 511 | Operand ops[3]; |
| | 512 | int exclam = 0; |
| | 513 | int closed_bracket = 0; |
| | 514 | int op2_minus = 0; |
| | 515 | uint32_t opcode = 1 << 26; |
| | 516 | // Note: ldr r0, [r4, #4] ; simple offset: r0 = *(int*)(r4+4); r4 unchanged |
| | 517 | // Note: ldr r0, [r4, #4]! ; pre-indexed: r0 = *(int*)(r4+4); r4 = r4+4 |
| | 518 | // Note: ldr r0, [r4], #4 ; post-indexed: r0 = *(int*)(r4+0); r4 = r4+4 |
| | 519 | |
| | 520 | parse_operand(s1, &ops[0]); |
| | 521 | if (ops[0].type == OP_REG32) |
| | 522 | opcode |= ENCODE_RD(ops[0].reg); |
| | 523 | else { |
| | 524 | expect("(destination operand) register"); |
| | 525 | return; |
| | 526 | } |
| | 527 | if (tok != ',') |
| | 528 | expect("two arguments"); |
| | 529 | else |
| | 530 | next(); // skip ',' |
| | 531 | if (tok != '[') |
| | 532 | expect("'['"); |
| | 533 | else |
| | 534 | next(); // skip '[' |
| | 535 | |
| | 536 | parse_operand(s1, &ops[1]); |
| | 537 | if (ops[1].type == OP_REG32) |
| | 538 | opcode |= ENCODE_RN(ops[1].reg); |
| | 539 | else { |
| | 540 | expect("(first source operand) register"); |
| | 541 | return; |
| | 542 | } |
| | 543 | if (tok == ']') { |
| | 544 | next(); |
| | 545 | closed_bracket = 1; |
| | 546 | // exclam = 1; // implicit in hardware; don't do it in software |
| | 547 | } |
| | 548 | if (tok != ',') |
| | 549 | expect("','"); |
| | 550 | else |
| | 551 | next(); // skip ',' |
| | 552 | if (tok == '-') { |
| | 553 | op2_minus = 1; |
| | 554 | next(); |
| | 555 | } |
| | 556 | parse_operand(s1, &ops[2]); |
| | 557 | if (!closed_bracket) { |
| | 558 | if (tok != ']') |
| | 559 | expect("']'"); |
| | 560 | else |
| | 561 | next(); // skip ']' |
| | 562 | opcode |= 1 << 24; // add offset before transfer |
| | 563 | if (tok == '!') { |
| | 564 | exclam = 1; |
| | 565 | next(); // skip '!' |
| | 566 | } |
| | 567 | } |
| | 568 | |
| | 569 | // single data transfer: 0 1 I P U B W L << 20 (general case): |
| | 570 | // operands: |
| | 571 | // Rd: destination operand [ok] |
| | 572 | // Rn: first source operand [ok] |
| | 573 | // Operand2: bits 11...0 [ok] |
| | 574 | // I: immediate operand? [ok] |
| | 575 | // P: Pre/post indexing is PRE: Add offset before transfer [ok] |
| | 576 | // U: Up/down is up? (*adds* offset to base) [ok] |
| | 577 | // B: Byte/word is byte? TODO |
| | 578 | // W: Write address back into base? [ok] |
| | 579 | // L: Load/store is load? [ok] |
| | 580 | if (exclam) |
| | 581 | opcode |= 1 << 21; // write offset back into register |
| | 582 | |
| | 583 | if (ops[2].type == OP_IM32 || ops[2].type == OP_IM8 || ops[2].type == OP_IM8N) { |
| | 584 | int v = ops[2].e.v; |
| | 585 | if (op2_minus) |
| | 586 | tcc_error("minus before '#' not supported for immediate values"); |
| | 587 | if (v >= 0) { |
| | 588 | opcode |= 1 << 23; // up |
| | 589 | if (v >= 0x1000) |
| | 590 | tcc_error("offset out of range for '%s'", get_tok_str(token, NULL)); |
| | 591 | else |
| | 592 | opcode |= v; |
| | 593 | } else { // down |
| | 594 | if (v <= -0x1000) |
| | 595 | tcc_error("offset out of range for '%s'", get_tok_str(token, NULL)); |
| | 596 | else |
| | 597 | opcode |= -v; |
| | 598 | } |
| | 599 | } else if (ops[2].type == OP_REG32) { |
| | 600 | if (!op2_minus) |
| | 601 | opcode |= 1 << 23; // up |
| | 602 | opcode |= ENCODE_IMMEDIATE_FLAG; /* if set, it means it's NOT immediate */ |
| | 603 | opcode |= ops[2].reg; |
| | 604 | } else |
| | 605 | expect("register"); |
| | 606 | |
| | 607 | switch (ARM_INSTRUCTION_GROUP(token)) { |
| | 608 | case TOK_ASM_strbeq: |
| | 609 | opcode |= 1 << 22; // B |
| | 610 | /* fallthrough */ |
| | 611 | case TOK_ASM_streq: |
| | 612 | asm_emit_opcode(token, opcode); |
| | 613 | break; |
| | 614 | case TOK_ASM_ldrbeq: |
| | 615 | opcode |= 1 << 22; // B |
| | 616 | /* fallthrough */ |
| | 617 | case TOK_ASM_ldreq: |
| | 618 | opcode |= 1 << 20; // L |
| | 619 | asm_emit_opcode(token, opcode); |
| | 620 | break; |
| | 621 | default: |
| | 622 | expect("data transfer instruction"); |
| | 623 | } |
| | 624 | } |
| | 625 | |
| 509 | ST_FUNC void asm_opcode(TCCState *s1, int token) | 626 | ST_FUNC void asm_opcode(TCCState *s1, int token) |
| 510 | { | 627 | { |
| 511 | while (token == TOK_LINEFEED) { | 628 | while (token == TOK_LINEFEED) { |
| @@ -546,6 +663,12 @@ ST_FUNC void asm_opcode(TCCState *s1, int token) · 34 unmodified lines |
| 546 | case TOK_ASM_uxtheq: | 663 | case TOK_ASM_uxtheq: |
| 547 | return asm_binary_opcode(s1, token); | 664 | return asm_binary_opcode(s1, token); |
| 548 | | 665 | |
| | 666 | case TOK_ASM_ldreq: |
| | 667 | case TOK_ASM_ldrbeq: |
| | 668 | case TOK_ASM_streq: |
| | 669 | case TOK_ASM_strbeq: |
| | 670 | return asm_single_data_transfer_opcode(s1, token); |
| | 671 | |
| 549 | case TOK_ASM_muleq: | 672 | case TOK_ASM_muleq: |
| 550 | case TOK_ASM_mulseq: | 673 | case TOK_ASM_mulseq: |
| 551 | case TOK_ASM_mlaeq: | 674 | case TOK_ASM_mlaeq: |