忍者ブログ

なんだか

知らないほうが幸せかもしれない

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

rx-elfのgccをちょっと賢くする

rxのマイコンにはメモリに対するbit操作命令があるのですが

char foo;
void
bit0_set(void)
{
  foo |=1;
}


こういうことをやろうとする、rx-elf-gccは

0000 FB E2 00 00 00 00 mov.L #_foo, r14
0006 CC E4             mov.B [r14], r4
0008 65 14             or #1, r4
000a C3 E4             mov.B r4, [r14]

なぜか、こういう上のコードを出力しています。

rx.mdにはbit操作命令を使うような記述はあるのですが、無視されています。

0000 FB E2 00 00 00 00 mov.L   #_foo, r14
0006 F0 E0             bset    #0, [r14].B

(set (mem/c:QI (reg/f:SI 26) [0 foo+0 S1 A8])
    (subreg:QI (ior:SI (subreg:SI (mem/c:QI (reg/f:SI 26) [0 foo+0 S1 A8]) 0)
            (const_int 1 [0x1])) 0))

これは、上の命令パターンを認識できていないために起こっています。

この命令パターン認識される記述は下のようになります。

(define_insn "*bitset_in_memory_little"
  [(set (match_operand:QI 0 "rx_restricted_mem_operand" "+Q")
       (subreg:QI (ior:SI (subreg:SI (match_dup 0) 0)
                          (match_operand:QI 1 "const_int_operand" "i")) 0))]
  "exact_log2 (INTVAL (operands[1]) & 0xff) >= 0
   && exact_log2 (INTVAL (operands[1]) & 0xff) <= 7"
  "*
{
  rtx xoperands[2];
  xoperands[0] = operands[0];
  xoperands[1] = GEN_INT (exact_log2 (INTVAL (operands[1]) & 0xff));
  output_asm_insn (\"bset\\t%1, %0.B\", xoperands);
  return \"\";
}
  "
  [(set_attr "length" "3")
   (set_attr "timings" "33")]
)

今度は、コンパイル時に-mbig-endian-dataをつけた場合を考えてみます。

(set (mem/c:QI (reg/f:SI 26) [0 foo+0 S1 A8])
    (subreg:QI (ior:SI (subreg:SI (mem/c:QI (reg/f:SI 26) [0 foo+0 S1 A8]) 0)
            (const_int 1 [0x1])) 3))

このパターンを認識される記述はこうなります。

(define_insn "*bitset_in_memory_big"
  [(set (match_operand:QI 0 "rx_restricted_mem_operand" "+Q")
       (subreg:QI (ior:SI (subreg:SI (match_dup 0) 0)
                          (match_operand:QI 1 "const_int_operand" "i")) 3))]
  "exact_log2 (INTVAL (operands[1]) & 0xff) >= 0
   && exact_log2 (INTVAL (operands[1]) & 0xff) <= 7"
  "*
{
  rtx xoperands[2];
  xoperands[0] = operands[0];
  xoperands[1] = GEN_INT (exact_log2 (INTVAL (operands[1]) & 0xff));
  output_asm_insn (\"bset\\t%1, %0.B\", xoperands);
  return \"\";
}
  "
  [(set_attr "length" "3")
   (set_attr "timings" "33")]
)


この記述方法は、v850.mdの記述を参考にしています。

次に、ビットフィールドの時の場合を考えてみます。

union
{
  unsigned char BYTE;
  struct
  {
#ifdef __RX_BIG_ENDIAN__
    unsigned char B7:1;
    unsigned char B6:1;
    unsigned char B5:1;
    unsigned char B4:1;
    unsigned char B3:1;
    unsigned char B2:1;
    unsigned char B1:1;
    unsigned char B0:1;
#else
    unsigned char B0:1;
    unsigned char B1:1;
    unsigned char B2:1;
    unsigned char B3:1;
    unsigned char B4:1;
    unsigned char B5:1;
    unsigned char B6:1;
    unsigned char B7:1;
#endif
  } BIT;

void
set_bit0(void)
{
  foo.BIT.B0 = 1;
}

0000 FB E2 00 00 00 00                     mov.L   #_foo, r14
0006 CC E4                                 mov.B   [r14], r4
0008 78 04                                 bset    #0, r4
000a C3 E4                                 mov.B   r4, [r14]

この時に、必要な記述は
(set (mem/j/c:QI (reg/f:SI 24) [0+0 S1 A8])
    (ior:QI (mem/j/c:QI (reg/f:SI 24) [0+0 S1 A8])
        (const_int 1 [0x1])))

こうなので

(define_insn "*bitset_in_memory_bitfiled"
  [(set (match_operand:QI 0 "rx_restricted_mem_operand" "+Q")
        (ior:QI (match_dup 0)
                (match_operand:QI 1 "const_int_operand" "i")))]
  "exact_log2 (INTVAL (operands[1]) & 0xff) >= 0
   && exact_log2 (INTVAL (operands[1]) & 0xff) <= 7"
  "*
{
  rtx xoperands[2];
  xoperands[0] = operands[0];
  xoperands[1] = GEN_INT (exact_log2 (INTVAL (operands[1]) & 0xff));
  output_asm_insn (\"bset\\t%1, %0.B\", xoperands);
  return \"\";
}
  "
  [(set_attr "length" "3")
   (set_attr "timings" "33")]
)

記述はこうなります。この場合、エンディアンは無視できます。

bitmem.patch

bclr,bnotを使えるようにしました。

拍手[0回]

PR

コメント

お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード Vodafone絵文字 i-mode絵文字 Ezweb絵文字

カレンダー

03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

フリーエリア

最新CM

最新記事

(07/27)
(03/27)
(03/26)
(03/23)
(03/22)
(03/19)
(03/18)
(03/18)
(03/15)
(03/14)
(03/13)
(03/12)
(03/11)
(03/11)
(03/08)
(03/08)
(03/06)
(03/05)
(03/02)
(03/01)
(02/28)
(02/27)
(02/24)
(02/23)
(02/22)

プロフィール

HN:
kenrou
性別:
非公開

バーコード

ブログ内検索

最古記事

(12/15)
(12/16)
(12/17)
(12/18)
(12/19)
(12/20)
(12/21)
(12/22)
(12/23)
(12/24)
(12/25)
(12/26)
(12/27)
(12/28)
(12/29)
(12/30)
(12/31)
(01/01)
(01/02)
(01/21)
(01/22)
(01/23)
(01/24)
(01/24)
(01/28)

P R

忍者アナライズ

コガネモチ

忍者カウンター

カレンダー

03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30