Compilers. freescale ColdFire v6.0
Publicitate
Publicitate
CF.book Page 61 Friday, July 8, 2005 8:59 AM
5
Compilers
This chapter explains issues of C and C++ code generation for the ColdFire target.
NOTE
Special-Edition software compiles assembly and C code, but the code size must not exceed 128 kilobytes.
Standard-Edition software compiles assembly and C code, without any size restriction.
Professional-Edition software compiles assembly, C, and C++ code, without any size restriction.
This chapter consists of these sections:
Table 5.1 lists additional documents or chapters that provide information about code
generation.
Table 5.1 Additional CodeWarrior Compiler and Linker Documentation
Topic
CodeWarrior implementation of C/C++
C/C++ Language and C/C++ Warnings settings panels
Controlling C++ code size
Using compiler pragmas
Reference
C Compilers Reference
C Compilers Reference : chapter Setting C/
C++ Compiler Options
C Compilers Reference : chapter C++ and
Embedded Systems
C Compilers Reference : chapter Pragmas and Symbols
ColdFire Architectures v6.0 - Targeting Manual 61
CF.book Page 62 Friday, July 8, 2005 8:59 AM
Compilers
Language Extensions
Table 5.1 Additional CodeWarrior Compiler and Linker Documentation ( continued)
Topic
Meeting EC++ specifications
Initiating a build, controlling which files are compiled, handling error reports
Explanation of error messages
Inline assembly
Standalone assembler
Reference
C Compilers Reference
IDE User Guide
: chapter Compiling and
Linking
Error Reference
Inline Assembly chapter of this Targeting
Manual
Assembler Reference
Language Extensions
This section explains the ColdFire-specific extensions to the standards of the CodeWarrior
C/C++ compiler.
You can disable some of these extensions by using options of the C/C++ Language panel, which the C Compilers Reference manual explains.
PC-Relative Strings
The default compiler configuration is to store all string constants in the global data segment. This configuration corresponds to a clear PC-Relative Strings checkbox of the
ColdFire Processor panel.
But to keep the global data segment smaller, you can check the PC-Relative Strings checkbox. Configuring PC-relative strings means that the compiler:
• Stores local-scope string constants in the code segment
• Uses PC-relative instructions to address these strings
The PC-Relative Strings checkbox corresponds to the pragma pcrelstrings. To
check whether this pragma is in effect, use __option (pcrelstrings). Listing 5.1
shows an example of the pragma use.
Listing 5.1 Using PC-relative strings pragma
#pragma pcrelstrings on int f(char *); int bar()
{ return f("World"); // "World" allocated in code segment
62 ColdFire Architectures v6.0 - Targeting Manual
CF.book Page 63 Friday, July 8, 2005 8:59 AM
Compilers
Language Extensions
// (pc-relative)
}
#pragma pcrelstrings reset
Declaration Specifiers
A declaration specifier tells the compiler to override a default storage location, regardless of the object’s size or initialization. The syntax is:
__declspec (sectionName) dataType objectName where: sectionName
Identifies the storage section for the object dataType
Specifies the data type of the object objectName
Is the name of the variable object to be stored
Listing 5.2 Using the __declspec() declaration specifier
// create a user-defined section ".mycode"
#pragma define_section mycode ".mycode" far_absolute RX
__declspec(bss) int Large_Array_in_Small_Data_Section[1000];
__declspec(mycode) void Function_in_MyCode_Section()
{ printf("Hello from MyCode section!\n");
}
A declaration specifier may use any section, whether pre-defined or user-defined. (For
more information about pre-defined and user-defined sections, see define_section.)
A declaration specifier also can use an application binary interface (ABI). An ABI corresponds directly to the parameter-passing option (Standard, Compact or Register) that the ColdFire Processor Settings panel specifies. (For information about these ABIs, see
ColdFire Architectures v6.0 - Targeting Manual 63
CF.book Page 64 Friday, July 8, 2005 8:59 AM
Compilers
Integer Formats
Integer Formats
The ColdFire compiler lets you specify the number of bytes that the compiler allocates for an int. Bring up the ColdFire Processor settings panel, then use the 4-Byte Integers option.
Table 5.2 shows the size and range of the integer types available for ColdFire targets.
Table 5.2 ColdFire Integer Types
Type
bool char signed char unsigned char short unsigned short int
Option Setting
n/a
Use Unsigned
Chars is off in the
C/C++ Language panel
Use Unsigned
Chars is on in the
C/C++ Language panel n/a n/a n/a n/a unsigned int
Size
8 bits
8 bits
8 bits
8 bits
8 bits
16 bits
16 bits
4-Byte Integers is off
in the ColdFire
Processor panel
4-Byte Integers is on
in the ColdFire
Processor panel
4-Byte Integers is off in the ColdFire
Processor panel
4-Byte Integers is on
in the ColdFire
Processor panel
16 bits
32 bits
16 bits
32 bits
Range
true or false
-128 to 127
0 to 255
-128 to 127
0 to 255
-32,768 to 32,767
0 to 65,535
-32,768 to 32,767
-2,147,483,648 to 2,147,483,647
0 to 65,535
0 to 4,294,967,295
64 ColdFire Architectures v6.0 - Targeting Manual
CF.book Page 65 Friday, July 8, 2005 8:59 AM
Compilers
Calling Conventions
Table 5.2 ColdFire Integer Types ( continued)
Type
long unsigned long long long
Option Setting
n/a n/a n/a
Size
32 bits
32 bits
64 bits unsigned long long n/a
Range
-2,147,483,648 to 2,147,483,647
0 to 4,294,967,295
64 bits
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
0 to 18,446,744,073,709,551,615
Calling Conventions
For ColdFire development, the calling conventions are:
• Standard — the compiler uses the default amount of memory, expanding everything to int size.
• Compact — the compiler tries to minimize memory consumption.
• Register — the compiler tries to use memory registers, instead of the stack.
NOTE
The corresponding levels for the supported calling conventions are standard_abi
(the default), compact_abi, and register_abi.
For more ABI information, see ColdFire Processor Panel Elements.
The compiler passes parameters on the stack in reverse order. It passes the return value in different locations, depending on the nature of the value and compiler settings:
• Integer return value: register D0.
• Pointer return value: register A0.
• Any other return value: temporary storage area. (For any non-integer, non-pointer return type, the calling routine reserves this area in its stack. The calling routine passes a pointer to this area as its last argument. The called function returns its value in this temporary storage area.)
To have the compiler return pointer values in register D0, use the pragma pointers_in_D0
, which the C Compiler reference guide explains.
To reset pointer returns, use the pragma pointers_in_A0.
ColdFire Architectures v6.0 - Targeting Manual 65
CF.book Page 66 Friday, July 8, 2005 8:59 AM
Compilers
Variable Allocation
NOTE
If you use the pragma pointers_in_A0, be sure to use correct prototypes.
Otherwise, the pragma may not perform reliably.
Figure 5.1 depicts the stack when you use the ColdFire compiler to call a C function.
Figure 5.1 Calling a C Function: Stack Depiction
Last Argument
Stack Pointer
...
First Argument
Pointer to Return Value (if needed)
Return Address
Variable Allocation
For a ColdFire target, the compiler lets you declare structs and arrays to be any size, but imposes a few limits on how you allocate their space:
• Maximum bitfield size is 32 bits.
• There is no limit to local-variable space for a function. However, access is twice as fast for frames that do not exceed 32 kilobytes. To keep within this limit,
– Dynamically allocate large variables, or
– Declare large variables to be static (provided that this does not exceed the 32kilobyte limit on global variables).
• Maximum declaration size for a global variable is 32 kilobytes, unless you use far data. You must do one of the following:
– Dynamically allocate the variable.
– Use the far qualifier when declaring the variable.
– Select the Far (32 bit) option from the Code and Data model in the ColdFire
Processor settings panel.
Listing 5.3 shows how to declare a large struct or array. Keep in mind that declaring large
static arrays works only if the device has enough physical memory.
Listing 5.3 Declaring a large structure
int i[50000]; // Wrong with ColdFire compiler and the Far Data
// option in the Processor settings panel is off far int j[50000]; // ALWAYS OK.
66 ColdFire Architectures v6.0 - Targeting Manual
CF.book Page 67 Friday, July 8, 2005 8:59 AM
Compilers
Register Variables int *k; k = malloc(50000 * sizeof(int)); // ALWAYS OK.
Register Variables
The ColdFire back-end compiler automatically allocates local variables and parameters to registers, according to frequently of use and how many registers are available.
The ColdFire compiler can use these registers for local variables:
• A2 through A5 — for pointers
• D3 through D7 — for integers and pointers.
• FP3 through FP7 — for 64-bit floating-point numbers (provided that you select
Hardware in the Floating Point list box of the ColdFire Processor panel).
If you optimize for speed, the compiler gives preference to variables in loops.
The ColdFire back-end compiler gives preference to variables declared register, but does not automatically assign them to registers. For example, if the compiler must choose between an inner-loop variable and a variable declared register, the compiler places the inner-loop variable in the register.
Pragmas
For ColdFire development, the compiler supports the standard pragmas that Table 5.3
lists. The C Compilers Reference explains these pragmas, including their syntax.
Table 5.3 Standard Pragmas for ColdFire Development
a6frames align_array_members
ARM_conform bool cplusplus direct_destruction enumsalwaysints extended_errorcheck auto_inline align cpp_extensions dont_reuse_strings exceptions far_data force_active ignore_oldstyle
ColdFire Architectures v6.0 - Targeting Manual 67
CF.book Page 68 Friday, July 8, 2005 8:59 AM
Compilers
Pragmas
Table 5.3 Standard Pragmas for ColdFire Development ( continued)
far_vtables
IEEEdoubles inline_depth longlong mark mpwc_relax only_std_keywords parameter pointers_in_A0 pool_strings profile
RTTI unused warn_hidevirtual warn_unusedarg lib_export macsbug, oldstyle_symbols mpwc_newline once optimize_for_size pcrelstrings pointers_in_D0 precompile_target require_prototypes segment static_inlines unsigned_char warn_emptydecl warn_illpragma
ANSI_strict
The compiler also supports pragmas that are new — or have new definitions — for
ColdFire development. Table 5.4 lists these new pragmas; following text explains them.
Table 5.4 New Pragmas for ColdFire Development
68 ColdFire Architectures v6.0 - Targeting Manual
CF.book Page 69 Friday, July 8, 2005 8:59 AM
Compilers
Pragmas
codeColdFire
Controls organization and generation of ColdFire object code.
#pragma codeColdFire processor
Parameter
processor
Any of these specifier values: MCF521x, MCF5206e, MCF5249, MCF5272,
MCF5282
, MCF5307, MCF5407, MCF547x, MCF548x — or reset, which specifies the default processor.
const_multiply
Enables support for constant multiplies, using shifts and add/subtracts.
#pragma const_multiply [ on | off | reset ]
Remarks
The default value is on.
define_section
Specifies a predefined section or defines a new section for compiled object code.
#pragma define_section sname ".istr" [.ustr] [addrmode]
[accmode]
Parameters
sname
Identifier for source references to this user-defined section.
istr
Section-name string for initialized data assigned to this section. Double quotes must surround this parameter value, which must begin with a period. (Also applies to uninitialized data if there is no ustr value.)
ColdFire Architectures v6.0 - Targeting Manual 69
CF.book Page 70 Friday, July 8, 2005 8:59 AM
Compilers
Pragmas ustr
Optional: ELF section name for uninitialized data assigned to this section. Must begin with a period. Default value is the istr value.
addrmode
Optional: any of these address-mode values:
• standard — 32-bit absolute address (default)
• near_absolute — 16-bit absolute address
• far_absolute — 32-bit absolute address
• near_code — 16-bit offset from the PC address
• far_code — 32-bit offset from the PC address
• near_data — 16-bit offset from the A5 register address
• far_data — 32-bit offset from the A5 register address accmode
Optional: any of these letter combinations:
• R — readable
• RW — readable and writable
• RX — readable and executable
• RWX — readable, writable, and executable (default)
(No other letter orders are valid: WR, XR, or XRW would be an error.)
Remarks
The compiler predefines the common ColdFire sections that Table 5.5 lists.
Table 5.5 ColdFire Predefined Sections
Applicability
Absolute
Addressing
Mode
C++,
Regardless of
Addressing
Mode
Definition Pragmas
#pragma define_section text ".text" far_absolute RX
#pragma define_section data ".data" ".bss" far_absolute RW
#pragma define_section sdata ".sdata" ".sbss" near_data RW
#pragma define_section const ".rodata" far_absolute R
#pragma define_section exception ".exception" far_absolute R
#pragma define_section exceptlist ".exceptlist" far_absolute R
70 ColdFire Architectures v6.0 - Targeting Manual
CF.book Page 71 Friday, July 8, 2005 8:59 AM
Compilers
Pragmas
Table 5.5 ColdFire Predefined Sections ( continued)
PID Addressing
Mode
PIC Addressing
Mode
#pragma define_section text ".text" far_absolute RX
#pragma define_section data ".data" ".bss" far_data RW
#pragma define_section sdata ".sdata" ".sbss" near_data RW
#pragma define_section const ".rodata" far_absolute R
#pragma define_section text ".text" far_code RX
#pragma define_section data ".data" ".bss" far_absolute RW
#pragma define_section sdata ".sdata" ".sbss" near_data RW
#pragma define_section const ".rodata" far_code R
Another use for #pragma define_section is redefining the attributes of predefined sections:
• To force 16-bit absolute addressing for all data, use
#pragma define_section data ".data" near_absolute
• To force 32-bit TP-relative addressing for exception tables, use:
#pragma define_section exceptlist ".exceptlist" far_code
#pragma define_section exception ".exception" far_code
You should put any such attribute-redefinition pragmas a prefix file or other header that all your program’s source files will include.
NOTE
The ELF linker’s Section
Mappings settings panel must map any userdefined compiler section to an appropriate segment.
emac
Enables EMAC assembly instructions in inline assembly.
#pragma emac [ on | off | reset ]
Remarks
Enables inline-assembly instructions mac, msac, macl, msacl, move, and movclr
for the ColdFire EMAC unit.
The default value is OFF.
ColdFire Architectures v6.0 - Targeting Manual 71
CF.book Page 72 Friday, July 8, 2005 8:59 AM
Compilers
Pragmas
explicit_zero_data
Specifies storage area for zero-initialized data.
#pragma explicit_zero_data [ on | off | reset ]
Remarks
The default value OFF specifies storage in the .sbss or .bss section. The value
ON
specifies storage in the .data section. The value reset specifies storage in the most-recent previously specified section.
Example
#pragma explicit_zero_data on int in_data_section = 0;
#pragma explicit_zero_data off int in_bss_section = 0;
inline_intrinsics
Controls support for inline intrinsic optimizations strcopy and strlen.
#pragma inline_intrinsics [ on | off | reset ]
Remarks
In the strcopy optimization, the system copies the string via a set of moveimmediate commands to the source address. The system applies this optimization if the source is a string constant of fewer than 64 characters, and optimizing is set for speed.
In the strlen optimization, a move immediate of the length of the string to the result replaces the function call. The system applies this optimization if the source is a string constant.
The default value is ON.
72 ColdFire Architectures v6.0 - Targeting Manual
CF.book Page 73 Friday, July 8, 2005 8:59 AM
Compilers
Pragmas
interrupt
Controls compilation for interrupt-routine object code.
#pragma interrupt [ on | off | reset ]
Remarks
For the value ON, the compiler generates special prologues and epilogues for the functions this pragma encapsulates The compiler saves or restores all modified registers (both nonvolatile and scratch). Functions return via RTE instead of RTS.
You also can also use __declspec(interrupt) to mark functions as interrupt routines, for example:
__declspec(interrupt) void alpha()
{
//enter code here
}
opt_unroll_count
Limits the number of times a loop can be unrolled; fine-tunes the loop-unrolling optimization.
#pragma opt_unroll_count [ 0..127 | reset ]
Remarks
The default value is 8.
opt_unroll_instr_count
Limits the number of pseudo-instructions; fine-tunes the loop-unrolling optimization.
#pragma opt_unroll_instr_count [ 0..127 | reset ]
Remarks
There is not always a one-to-one mapping between pseudo-instructions and actual
ColdFire instructions.
The default value is 100.
ColdFire Architectures v6.0 - Targeting Manual 73
CF.book Page 74 Friday, July 8, 2005 8:59 AM
Compilers
Pragmas
profile
Organizes object code for the profiler library and enables simple profiling.
#pragma profile [on| off| reset]
Remarks
Corresponds to the Generate code for profiling checkbox of the ColdFire
Processor settings panel.
readonly_strings
Enables the compiler to place strings in the .rodata section.
#pragma readonly_strings [ on | off | reset ]
Remarks
The default value is ON.
For the OFF value, the compiler puts strings in initialized data sections .data or
.sdata
, according to the string size.
SDS_debug_support
Tries to make the DWARF output file compatible with the Software Development System
(SDS) debugger. The default value is OFF.
#pragma SDS_debug_support [ on | off | reset ]
section
74
Activates or deactivates a user-defined or predefined section.
#pragma section
sname begin | end
Parameters
sname
Identifier for a user-defined or predefined section.
ColdFire Architectures v6.0 - Targeting Manual
CF.book Page 75 Friday, July 8, 2005 8:59 AM
Compilers
Predefined Symbols begin
Activates the specified section from this point in program execution.
end
Deactivates the specified section from this point in program execution; the section returns to its default state.
Remarks
Each call to this pragma must include a begin parameter or an end parameter, but not both.
You may use this pragma with #pragma push and #pragma pop to ease complex or frequent changes to section settings.
NOTE
A simpler alternative to #pragma section is the __declspec()
declaration specifier. For more details, see Declaration Specifiers
Predefined Symbols
Metrowerks C and C++ include several preprocessor symbols that give information about the compile-time environment. For information on these symbols, see the C Compilers
Reference.
Table 5.6 lists additional predefined symbols for ColdFire development.
Table 5.6 Predefined Symbols
Macro
__embedded__
__BACKENDVERSION__
__COLDFIRE__
__profile__
__STDABI__
__REGABI__
Definition
1, if you compile code for an embedded target.
"3" hex id for chip
1, if you enable the Generate Profiler Calls setting of the
ColdFire Processor panel; 0 if you disable this setting.
1, if you check the standard passing parameter checkbox of the ColdFire Processor panel; 0 if you clear this checkbox.
1 if you select the passing parameter setting as register in the ColdFire Processor panel; 0 if you deselect it.
ColdFire Architectures v6.0 - Targeting Manual 75
CF.book Page 76 Friday, July 8, 2005 8:59 AM
Compilers
Position-Independent Code
Position-Independent Code
If you specify position-independent code, the compiler generates code that is the same regardless of its load address. Different processes of your application can share such code.
Listing 5.4 Position Independent Code
int relocatableAlpha();
int (*alpha)()=relocatableAlpha;
Follow these steps to enable the PIC compiler and runtime support:
1. Add a .picdynrel section to the linker command file. See Position-Independent
2. Enable PIC generation in the processor settings panel. See ColdFire Target.
3. Customize and recompile the runtime to support your loading routine. See Position-
76 ColdFire Architectures v6.0 - Targeting Manual
Descarcă
Publicitate