[H8-ML(2019)] volatile 宣言(GCC で検証)
From: matsuzono_keiji@xxxxxxxxxxxxxxxxxxxx
Date: 2002年06月27日(木)12時13分14秒
まつぞの です

フォローありがとうございます
volatile を付けても最適化されてしまう場合がある、ということで
GCC で試してみました。結論を先に言うと、GCC だと最高レベルの
最適化を指定しても最適化(コードが実行されない)ことはありません
でした。コンパイラ依存なのでしょうか。

コンパイラ:GCC 3.0.3
x86用とH8用で試しました。

ソースコード:
-------------------------------------------------------------------
// io_repeat.c
// IOポートへの書き込みのための代入文が、最適化によって消えてしまう
// ことがあるかテストするプログラム

#ifdef VOLATILE_DEFINE
volatile int count;
#else
int count;
#endif

int main(){
	count = 0x01;
	count = 0x02;
	count = 0x04;
	return 0;
}
-----------------------------------------------------------------------

コンパイルオプションとコンパイル結果

(a-1) H8, volatile 指定なし、最適化レベル0(最適化しない)
(a-2) H8, volatile 指定なし、最適化レベル2
(a-3) H8, volatile 指定あり、最適化レベル2

(a-1) H8, volatile 指定なし、最適化レベル0(最適化しない)
 $ h8300-hms-gcc -o no_volatile_O0.S -S -O0 io_repeat.c

// 最適化しないようにオプション指定しているので、volatile 指定しなくても
// 最適化されていません
---------------- h8_no_volatile_O0.S ------------------------------------
;	GCC For the Hitachi H8/300
;	By Hitachi America Ltd and Cygnus Support


	.file	"io_repeat.c"
	.section .text
	.align 1
	.global _main
_main:
	push	r6
	mov.w	r7,r6
	mov.w	#1,r2
	mov.w	r2,@_count
	mov.w	#2,r2
	mov.w	r2,@_count
	mov.w	#4,r2
	mov.w	r2,@_count
	sub.w	r0,r0
	pop	r6
	rts
	.comm _count,2
	.end
	.ident
"GCC: (GNU) 3.0.3"
---------------------------------------------------------------------


(a-2) H8, volatile 指定なし、最適化レベル2
 $ h8300-hms-gcc -o h8_no_volatile_O2.S -S -O2 io_repeat.c
// volatile 指定をしないと、最適化レベル2ですでに
// 最適化されてしまっています
---------------- h8_no_volatile_O2.S ------------------------------------

;	GCC For the Hitachi H8/300
;	By Hitachi America Ltd and Cygnus Support
; -O2


	.file	"io_repeat.c"
	.section .text
	.align 1
	.global _main
_main:
	push	r6
	mov.w	r7,r6
	mov.w	#4,r2
	mov.w	r2,@_count
	sub.w	r0,r0
	pop	r6
	rts
	.comm _count,2
	.end
	.ident
"GCC: (GNU) 3.0.3"
-------------------------------------------------------------------

(a-3) H8, volatile 指定あり、最適化レベル2
 $ h8300-hms-gcc -o h8_no_volatile_O2.S -S -O2 io_repeat.c

// volatile 指定をすると、最適化されません
// 最高レベルの -O9 を指定 しても最適化されませんでした
// x86 用 GCC でも同様の結果でした
---------------- h8_no_volatile_O2.S ------------------------------------
;	GCC For the Hitachi H8/300
;	By Hitachi America Ltd and Cygnus Support
; -O2


	.file	"io_repeat.c"
	.section .text
	.align 1
	.global _main
_main:
	push	r6
	mov.w	r7,r6
	mov.w	#1,r2
	mov.w	r2,@_count
	mov.w	#2,r2
	mov.w	r2,@_count
	adds	#2,r2
	mov.w	r2,@_count
	sub.w	r0,r0
	pop	r6
	rts
	.comm _count,2
	.end
	.ident
"GCC: (GNU) 3.0.3"
-----------------------------------------------------------






スレッド概略
[1992(R)](起点)
 └[2018(U)]
   └[表示中]
     └[2020(1)]


投稿順に移動
[←前の記事へ(P)]
[→次の記事へ(N)]


リスト表示へ
[このスレッド(T)]
[本記事の前後(L)]