AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page FAQ Library Reference Additional Documentation Example Projects

Using the GNU tools

これはGNUツールのAVR独自の部分の簡単なサマリーです。通常、これらのツールのドキュメントは大変大きく、texinfo形式で管理されています。コマンドラインオプションについてはマニュアルページで詳しく解説されています。

C compiler avr-gccのオプション

AVR独自のオプション

以下の機種依存オプションがCコンパイラフロントエンドに認識されます。

architectureに対応するようコードをコンパイルします。現在の所知られているアーキテクチャは以下のようなものがあります。

avr1Simple CPU core, only assembler support
avr2"Classic" CPU core, up to 8 KB of ROM
avr3"Classic" CPU core, more than 8 KB of ROM
avr4"Enhanced" CPU core, up to 8 KB of ROM
avr5"Enhanced" CPU core, more than 8 KB of ROM

デフォルトでは、avr2アーキテクチャ用のコードが生成されます。

-mmcu=architecture オプションだけを指定し、-mmcu=MCU type オプションを指定しなかった場合、どのデバイス定義を選ぶべきは判断できないため、ファイル<avr/io.h> のインクルード作業は正常に行われません。

MCU types 部には、avr-gccが把握しているデバイス名が入ります。この表は対応するavr-gcc アーキテクチャ名と-mmcuオプションで宣言されるプリプロセッサシンボルの一覧です。

ArchitectureMCU nameMacro
avr1at90s1200__AVR_AT90S1200__
avr1attiny11__AVR_ATtiny11__
avr1attiny12__AVR_ATtiny12__
avr1attiny15__AVR_ATtiny15__
avr1attiny28__AVR_ATtiny28__
avr2at90s2313__AVR_AT90S2313__
avr2at90s2323__AVR_AT90S2323__
avr2at90s2333__AVR_AT90S2333__
avr2at90s2343__AVR_AT90S2343__
avr2attiny22__AVR_ATtiny22__
avr2attiny24__AVR_ATtiny24__
avr2attiny25__AVR_ATtiny25__
avr2attiny26__AVR_ATtiny26__
avr2attiny261__AVR_ATtiny261__
avr2attiny44__AVR_ATtiny44__
avr2attiny45__AVR_ATtiny45__
avr2attiny461__AVR_ATtiny461__
avr2attiny84__AVR_ATtiny84__
avr2attiny85__AVR_ATtiny85__
avr2attiny861__AVR_ATtiny861__
avr2at90s4414__AVR_AT90S4414__
avr2at90s4433__AVR_AT90S4433__
avr2at90s4434__AVR_AT90S4434__
avr2at90s8515__AVR_AT90S8515__
avr2at90c8534__AVR_AT90C8534__
avr2at90s8535__AVR_AT90S8535__
avr2at86rf401__AVR_AT86RF401__
avr2attiny13__AVR_ATtiny13__
avr2attiny2313__AVR_ATtiny2313__
avr3atmega103__AVR_ATmega103__
avr3atmega603__AVR_ATmega603__
avr3at43usb320__AVR_AT43USB320__
avr3at43usb355__AVR_AT43USB355__
avr3at76c711__AVR_AT76C711__
avr4atmega48__AVR_ATmega48__
avr4atmega8__AVR_ATmega8__
avr4atmega8515__AVR_ATmega8515__
avr4atmega8535__AVR_ATmega8535__
avr4atmega88__AVR_ATmega88__
avr4at90pwm2__AVR_AT90PWM2__
avr4at90pwm3__AVR_AT90PWM3__
avr5at90can32__AVR_AT90CAN32__
avr5at90can64__AVR_AT90CAN64__
avr5at90can128__AVR_AT90CAN128__
avr5atmega128__AVR_ATmega128__
avr5atmega1280__AVR_ATmega1280__
avr5atmega1281__AVR_ATmega1281__
avr5atmega16__AVR_ATmega16__
avr5atmega161__AVR_ATmega161__
avr5atmega162__AVR_ATmega162__
avr5atmega163__AVR_ATmega163__
avr5atmega164__AVR_ATmega164__
avr5atmega165__AVR_ATmega165__
avr5atmega168__AVR_ATmega168__
avr5atmega169__AVR_ATmega169__
avr5atmega32__AVR_ATmega32__
avr5atmega323__AVR_ATmega323__
avr5atmega324__AVR_ATmega324__
avr5atmega325__AVR_ATmega325__
avr5atmega3250__AVR_ATmega3250__
avr5atmega329__AVR_ATmega329__
avr5atmega3290__AVR_ATmega3290__
avr5atmega64__AVR_ATmega64__
avr5atmega640__AVR_ATmega640__
avr5atmega644__AVR_ATmega644__
avr5atmega645__AVR_ATmega645__
avr5atmega6450__AVR_ATmega6450__
avr5atmega649__AVR_ATmega649__
avr5atmega6490__AVR_ATmega6490__
avr5at94k__AVR_AT94K__

レジスタ割り当ての順番を指定します。デフォルトは、
r24, r25,
r18, r19, r20, r21, r22, r23,
r30, r31,
r26, r27, r28, r29,
r17, r16,
r15, r14, r13, r12, r11, r10, r9, r8, r7, r6, r5, r4, r3, r2, r0, r1
Order 1 は以下の割り当てを使います。
r18, r19, r20, r21, r22, r23, r24, r25 ,
r30, r31,
r26, r27, r28, r29,
r17, r16,
r15, r14, r13, r12, r11, r10, r9, r8, r7, r6, r5, r4, r3, r2, r0, r1
Order 2 は以下の割り当てを使います。
r25, r24, r23, r22, r21, r20, r19, r18,
r30, r31,
r26, r27, r28, r29,
r17, r16,
r15, r14, r13, r12, r11, r10, r9, r8, r7, r6, r5, r4, r3, r2, r1, r0

intを8bit整数にします。これは実際にはavr-libcでサポートされていないので、通常は使用できません。デフォルトでは16-bit整数値を使います。

割り込みを禁止しないでスタックポインタを書き換えるコードを生成します。通常は、ステータスレジスタ SREG がテンポラリレジスタに保存され、スタックポインタ変更中割り込みは禁止され、最後にSREGが復帰されます(割り込み許可・禁止状態も元に戻されます)。
関数のprologue/epilogue(入口・出口処理)にサブルーチンを使います。たくさんのレジスタ(関数の入口・出口で退避復帰を要する)を使う複雑な関数では、このオプションによって実行速度と引き替えにいくらかのコード量の節約ができます。
初期のスタックポインタを nnnn に設定します。デフォルトでは、スタックポインタはランタイム初期化コードでRAMENDに設定される symbol __stackに設定されます。
スタックポインタの下位8bitだけを変更するようにします。※スタックが0x100以降に配置されないなら利用可能
テーブルジャンプ命令を生成しない。デフォルトでは、switch文を最適化するためにジャンプテーブルが使われます。このオプションによりテーブルジャンプを禁止すると、比較命令の羅列が生成されます。通常はジャンプ命令の方がより高速ですが、ほとんどのジャンプがデフォルトラベルに飛ぶような状況では、フラッシュメモリを無駄にしてしまいます。
ROMが8Kを越えるデバイスでもrjmp/rcall (ジャンプ範囲が限られている)を使う。avr2/avr4アーキテクチャ(ROMが8KB未満)では常にこの状況です。avr3/avr5アーキテクチャでは、現在の関数の外へのジャンプには全アドレス空間をカバーできるjmp/call命令を使いますが、これはコードサイズを大きくして実行速度も低下させます。
"RTL"と呼ばれる内部コンパイル結果をダンプして、generated assemblerコードのコメントに書き込みます。これはavr-gccをデバッグする時に使われます。
各行の アドレス、サイズ、相対的コストをgenerated assemblerコードのコメントに書き込みます。これはavr-gccをデバッグする時に使われます。
たくさんのデバッグ情報をstderrに書き出すようにします。

一般的なコンパイラオプションの抜粋

以下の一般的なgccオプションはAVRユーザーにはいくらか興味深いものでしょう。

最適化レベル nn の値が大きいとより最適化されることを意味し、最適化レベル0は全く最適化を行わない(-O オプションがない場合のデフォルト)ことを意味します。特別なオプション -Os は、コードサイズを増加させない最適化だけをONにすることを意味します。
-O3指定では、gccはすべての単純な関数をインラインに展開することに注意してください。AVRターゲットでは、これはコードサイズ増大により大変な悲惨化(※)を招きます。-O3と併用するとよい他の最適化としては、唯一 -frename-registers が上げられますが、これはむしろ(ソース内で)手動で設定されるものでしょう。
※原文ではoptimizeとoptimistを掛けているのか、optimizeの反対語としてpessimizationと書かれています:-)
単なる -O オプションは -O1 と等価です。
すべての最適化を切ると、最適化の時だけに行われるコード解析ステップに関連するウォーニングが出るのを防ぎます。 (実行されないコードや、使用されない変数などへの警告)

FAQ entry 最適化されたコードに関する項目も参照ください。

アセンブラやリンカにオプションリストを渡します。
avr-gdbで使用されるデバッグ情報を生成します。

"freestanding" 実行環境を前提とする。これは自動的に組み込まれる関数をOFFにする。(though they can still be reached by prepending __builtin_ to the actual function name) これは、コンパイラはmain() がvoidを返値として宣言されても警告を発しないというマイクロコントローラにはいくらかしっくり来る性質をもたらします。(アプリケーションは意味のあるリターン値を環境に返せないし、ほとんどの場合、main() はどこにも帰りようがない)
しかしながら、これは確かな名前で知られていて、当然スタンダードとして解説されているとおりの動作を行うとされている関数数類に関する最適化も全部オフにしてしまいます。たとえば、関数 strlen() をリテラル文字列("ABC"のような形)に対して使うとコンパイラはすぐにその文字列の実際の長さを調べて、その値を返すコードに差し替えますが、-ffreestandingをつけると、常に strlen() をランタイムで呼ぶようになります。

符号有無が定義づけられていない char型を unsigned charと見なす。このオプションがない場合は、デフォルトはsigned charとなります。
符号有無が定義づけられていない ビットフィールド型を unsigned と見なす。デフォルトはsigned.
enum型に対して、とりうる値の範囲を表現するのに最低限のバイトしか割り当てない。つまり、enum型はそれを収容するのに充分な部屋(bit数)を持つ最小の整数型となります。
構造体のメンバを隙間を空けずに詰め込む 
※uint8_t型とuint16_t型があれば、アドレスを偶数になんて考えずにくっつけて配置するってことかな?

Options for the assembler avr-as

Machine-specific assembler options

avr-as understands the same -mmcu= options as avr-gcc. By default, avr2 is assumed, but this can be altered by using the appropriate .arch pseudo-instruction inside the assembler source file.

Turns off opcode checking for the actual MCU type, and allows any possible AVR opcode to be assembled.

Don't emit a warning when trying to skip a 2-word instruction with a CPSE/SBIC/SBIS/SBRC/SBRS instruction. Early AVR devices suffered from a hardware bug where these instructions could not be properly skipped.

For RJMP/RCALL instructions, don't allow the target address to wrap around for devices that have more than 8 KB of memory.

Generate .stabs debugging symbols for assembler source lines. This enables avr-gdb to trace through assembler source files. This option must not be used when assembling sources that have been generated by the C compiler; these files already contain the appropriate line number information from the C source files.

Turn on the assembler listing. The sub-options are:

The various sub-options can be combined into a single -a option list; =file must be the last one in that case.

Examples for assembler options passed through the C compiler

Remember that assembler options can be passed from the C compiler frontend using -Wa (see above), so in order to include the C source code into the assembler listing in file foo.lst, when compiling foo.c, the following compiler command-line can be used:

	$ avr-gcc -c -O foo.c -o foo.o -Wa,-ahls=foo.lst

In order to pass an assembler file through the C preprocessor first, and have the assembler generate line number debugging information for it, the following command can be used:

	$ avr-gcc -c -x assembler-with-cpp -o foo.o foo.S -Wa,--gstabs

Note that on Unix systems that have case-distinguishing file systems, specifying a file name with the suffix .S (upper-case letter S) will make the compiler automatically assume -x assembler-with-cpp, while using .s would pass the file directly to the assembler (no preprocessing done).

Controlling the linker avr-ld

Selected linker options

While there are no machine-specific options for avr-ld, a number of the standard options might be of interest to AVR users.

Locate the archive library named libname.a, and use it to resolve currently unresolved symbols from it. The library is searched along a path that consists of builtin pathname entries that have been specified at compile time (e. g. /usr/local/avr/lib on Unix systems), possibly extended by pathname entries as specified by -L options (that must precede the -l options on the command-line).

Additional location to look for archive libraries requested by -l options.

Define a global symbol symbol using expr as the value.

Print a linker map to stdout.

Print a linker map to mapfile.

Output a cross reference table to the map file (in case -Map is also present), or to stdout.

Start section sectionname at absolute address org.

Start the bss, data, or text section at org, respectively.

Use scriptfile as the linker script, replacing the default linker script. Default linker scripts are stored in a system-specific location (e. g. under /usr/local/avr/lib/ldscripts on Unix systems), and consist of the AVR architecture name (avr2 through avr5) with the suffix .x appended. They describe how the various memory sections will be linked together.

Passing linker options from the C compiler

By default, all unknown non-option arguments on the avr-gcc command-line (i. e., all filename arguments that don't have a suffix that is handled by avr-gcc) are passed straight to the linker. Thus, all files ending in .o (object files) and .a (object libraries) are provided to the linker.

System libraries are usually not passed by their explicit filename but rather using the -l option which uses an abbreviated form of the archive filename (see above). avr-libc ships two system libraries, libc.a, and libm.a. While the standard library libc.a will always be searched for unresolved references when the linker is started using the C compiler frontend (i. e., there's always at least one implied -lc option), the mathematics library libm.a needs to be explicitly requested using -lm. See also the entry in the FAQ explaining this.

Conventionally, Makefiles use the make macro LDLIBS to keep track of -l (and possibly -L) options that should only be appended to the C compiler command-line when linking the final binary. In contrast, the macro LDFLAGS is used to store other command-line options to the C compiler that should be passed as options during the linking stage. The difference is that options are placed early on the command-line, while libraries are put at the end since they are to be used to resolve global symbols that are still unresolved at this point.

Specific linker flags can be passed from the C compiler command-line using the -Wl compiler option, see above. This option requires that there be no spaces in the appended linker option, while some of the linker options above (like -Map or --defsym) would require a space. In these situations, the space can be replaced by an equal sign as well. For example, the following command-line can be used to compile foo.c into an executable, and also produce a link map that contains a cross-reference list in the file foo.map:

	$ avr-gcc -O -o foo.out -Wl,-Map=foo.map -Wl,--cref foo.c

Alternatively, a comma as a placeholder will be replaced by a space before passing the option to the linker. So for a device with external SRAM, the following command-line would cause the linker to place the data segment at address 0x2000 in the SRAM:

	$ avr-gcc -mmcu=atmega128 -o foo.out -Wl,-Tdata,0x802000

See the explanation of the data section for why 0x800000 needs to be added to the actual value. Note that unless a -minit-stack option has been given when compiling the C source file that contains the function main(), the stack will still remain in internal RAM, through the symbol __stack that is provided by the run-time startup code. This is probably a good idea anyway (since internal RAM access is faster), and even required for some early devices that had hardware bugs preventing them from using a stack in external RAM. Note also that the heap for malloc() will still be placed after all the variables in the data section, so in this situation, no stack/heap collision can occur.


Automatically generated by Doxygen 1.4.1 on 23 Jan 2006.