C and C++ and Assembly Language Reference
C and C++ and Assembly Language Reference
C and C++ and Assembly Language Reference
com
CodeWarrior
C, C++, and Assembly
Language Reference
www.EhsanAvr.com
Metrowerks Corporation
2201 Donley Drive, Suite 310
Austin, TX 78758
U.S.A.
Canada
Metrowerks Inc.
1500 du College, Suite 300
Ville St-Laurent, QC
Canada H4L 5G6
Mail order
http://www.metrowerks.com
Registration information
register@metrowerks.com
Technical support
support@metrowerks.com
sales@metrowerks.com
America Online
keyword: Metrowerks
CompuServe
goto Metrowerks
www.EhsanAvr.com
Table of Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Overview of the C/C++/ASM Reference . . . . . .
Conventions Used in This Manual . . . . . . . . .
The C/C++ Project Settings Panels . . . . . . . . .
Whats New . . . . . . . . . . . . . . . . . . .
The long long type . . . . . . . . . . . . . .
Turning off register coloring in the 68K compiler .
More information on enumerated types . . . . .
New pragmas . . . . . . . . . . . . . . . .
New intrinsic functions . . . . . . . . . . . .
Improved documentation . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
13
14
14
17
17
17
17
17
18
18
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
61
62
63
64
65
66
67
70
70
71
72
72
73
75
76
77
78
79
80
80
81
81
82
82
83
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
85
86
86
87
87
87
88
89
89
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 90
. 91
. 92
. 93
. 94
. 95
. 96
. 96
. 96
. 97
. 98
. 99
. 100
. 101
. 103
. 104
. 105
. 106
. 109
. 109
. 111
. 112
. 112
. 113
. 113
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 115
. 116
. 116
. 119
. 119
. 120
. 121
. 121
. 122
dc . . .
ds . . .
entry . .
fralloc .
frfree . .
machine
opword.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 122
. 122
. 123
. 123
. 123
. 124
. 124
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 161
. 161
. 163
. 163
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 164
. 164
. 164
. 164
. 165
. 165
. 165
. 166
. 166
. 169
. 169
. 170
. 170
. 171
. 171
. 172
. 173
. 174
. 175
. 176
. 176
. 177
. 177
. 178
. 178
. 179
. 180
. 180
. 182
. 182
. 204
. 204
. 204
. 204
. 205
. 205
. 206
. 207
. 207
. 208
. 209
. 210
. 211
. 211
. 211
. 212
. 212
. 213
. 213
. 214
. 214
. 215
. 216
. 217
. 217
. 218
. 218
. 219
. 219
. 219
. 220
. 220
. 221
. 221
. 222
. 222
warn_illpragma . . . . . . . .
warn_possunwant . . . . . . .
warn_unusedarg. . . . . . . .
warn_unusedvar. . . . . . . .
warning (Win32/x86 only) . . .
Predefined Symbols . . . . . . . . .
ANSI Predefined Symbols. . . .
Metrowerks Predefined Symbols .
Options Checking . . . . . . . . .
Options table . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 223
. 223
. 224
. 225
. 225
. 226
. 226
. 228
. 229
. 230
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
1
Introduction
This manual describes how the Metrowerks C and C++ compilers
implement the C and C++ standards and its in-line assembler.
This chapter
Documents
How Metrowerks C implements the C standard. It also describes the parts of C++ that it shares with C.
Overview of 68K Assem- How to use the 68K inline assembler, which is part of
bler Notes
Metrowerks C and C++, to include assembly code in your
program.
Overview of PowerPC
Assembler Notes
I n t ro d u c t i o n
Conventions Used in This Manual
This chapter
Documents
Overview of Win32/x86
Assembler Notes
Overview of Pragmas
and Predefined Symbols
The pragma statement, which lets you change your programs options from your source code. It also describes the
preprocessor function __option(), which lets you test the
setting of many pragmas and options, and the predefined
symbols that Metrowerks C and C++ use.
If the text
looks like
Then
literal
metasymbol
Replace the symbol with an appropriate value. The text after the
syntax example describes what the appropriate values are.
a | b | c
Use one and only one of the symbols in the statement: either a, b,
or c.
[a]
Include this symbol only if necessary. The text after the syntax example describes when to include it.
Int rodu ct io n
The C/C++ Project Settings Panels
Is described here
ARM Conformance
Enable RTTI
Inlining
Pool Strings
ANSI Strict
Expand Trigraphs
I n t ro d u c t i o n
The C/C++ Project Settings Panels
This option
Is described here
Multi-Byte Aware
Direct to SOM
Map Newlines to CR
Is described here
Illegal Pragmas
Empty Declarations
Possible Errors
Int rod u ct io n
Whats New
This option
Is described here
Unused Variables
Unused Arguments
Extra Commas
Whats New
This section describes the new documentation in this manual.
The long long type
Metrowerks C/C++ now has a 64-bit integer, the long long. See
64-bit integers on page 46.
Turning off register coloring in the 68K compiler
You can now turn off register coloring in the 68K Mac OS compiler.
This is useful when youre debugging code. See Register coloring
on page 24.
More information on enumerated types
This manual now explains how the compiler implements enumerated types and on how to use enumerators that are large enough to
be a long long. See Enumerated types on page 28.
New pragmas
There are three new pragmas:
longlong on page 198
longlong_enums on page 199
no_register_coloring (68K Macintosh and Magic Cap only)
on page 202
I n t ro d u c t i o n
Whats New
2
C and C++
Language Notes
This chapter describes how Metrowerks handles the C programming language. Since many of the features in C are also in C++, this
chapter is where youll find basic information on C++ also.
C a n d C + + L an g u ag e N o tes
The Metrowerks Implementation of C and C++
Identifiers
(K&R, A2.3) The C and C++ compilers let you create identifiers of
any size. However, only the first 255 characters are significant for internal and external linkage.
The C++ compiler creates mangled names in which all the characters in are significant. You do not need to keep your class and class
member names artificially short to prevent the compiler from creating mangled names that are too long.
Include files
(K&R, A12.4) The C and C++ compilers can nest #include files up
to 32 times. An include file is nested if another #include file uses it
in an #include statement. For example, if Main.c includes the file
MyFunctions.h, which includes the file MyUtilities.h, the file
MyUtilities.h is nested once.
You can use full path names in #include directives, as in this example:
#include "HD:Tools:my headers:macros.h"
TIP: To add folders to the Access Paths settings panel, see the
CodeWarrior IDE Users Guide.
The CodeWarrior IDE lets you specify where the compiler looks for
#include files with the Access Paths settings panel, shown in Figure
2.1. It contains two lists of folders: the User list and the System list.
By default, each list contains one folder. The User list contains
{Project }, which is the folder that the project file is in and all
the folders it contains. The System list contains {Compiler },
which is the folder that the compiler is in and all the folders it contains.
C a n d C + + L an g u ag e N o tes
The Metrowerks Implementation of C and C++
Figure 2.1
The compiler searches for an #include file in either the System list or
both the User and System lists, depending on which characters enclose the file. If you enclose the file in brackets (#include
<stdio.h>), the compiler looks for the file in the System lists folders section. If you enclose the file in quotes (#include
"myfuncs.h"), the compiler looks for the file in the User lists folders and then in the System lists folders. In general, use brackets for
include files that are for a large variety of projects and use quotes for
include files that are for a specific project.
TIP: If youre using the compilers under MPW, you can specify
where to find #include files with the -i compiler option and the
{CIncludes} variable, described in Command-Line Tools Manual
and MPW Command Reference.
Register variables
(K&R, A4.1, A8.1) The C and C++ compilers automatically allocate local variables and parameters to registers according to how
frequently theyre used and how many registers are available. If
youre optimizing for speed, the compilers give preference to variables used in loops.
The PowerPC and 68K Macintosh compilers give preference to variables declared to be register, but do not automatically assign
them to registers. For example, the compilers are more likely to
place a variable from an inner loop in a register than a variable declared register.
The Win32/x86 compiler ignores the register declaration and decides on its own which variables to place in registers.
The PowerPC Macintosh compiler can use these registers for local
variables:
GPR13 through GPR31 for integers and pointers
FPR14 through FPR31 for floating point variables.
The 68K Macintosh and Magic Cap compilers can use these registers
for local variables:
A2 through A4 for pointers
D3 through D7 for integers and pointers.
If you turn on the 68881 Codegen option, the 68K compilers also use
these registers:
FP4 through FP7 for 96-bit floating-point numbers
The Win32/x86 compiler can use these registers for local variables:
EAX
EBX
ECX
C a n d C + + L an g u ag e N o tes
The Metrowerks Implementation of C and C++
EDX
ESI
EDI
Register coloring
The Macintosh and Magic Cap compilers can also perform an additional register optimization, called register coloring. In this optimization, the compiler lets two or more variables share a register: it
assigns different variables or parameters to the same register if you
do not use the variables at the same time. In this example, the compilers could place i and j in the same register:
short i;
int j;
for (i=0; i<100; i++) {
for (j=0; j<1000; j++) {
MyFunc(i); }
OurFunc(j); }
However, if a line like the one below appears anywhere in the function, the compiler would realize that youre using i and j at the
same time and place them in different registers:
int k = i + j;
To let the PowerPC compiler perform register coloring, turn on the
Global Optimizer option in the PPC Processor settings panel and
set the Level to 1 or more. To let the 68K Macintosh and Magic Cap
compilers perform register coloring, turn on the Global Register
Allocation option in the 68K Processor settings panel. The Global
Optimizer option corresponds to the global_optimizer pragma,
described on global_optimizer, optimization_level (PowerPC Macintosh only) on page 192. The Global Register Allocation option
corresponds to the no_register_coloring pragma, described on
no_register_coloring (68K Macintosh and Magic Cap only) on
page 202.
If register coloring is on while you debug your project, it may appear as though theres something wrong with the variables sharing
a register. In the example above, i and j would always have the
same value. When i changes, j changes in the same way. When j
Volatile variables
(K&R, A4.4) When you declare a variable to be volatile, both the C
or C++ compilers take the following precautions:
It does not store the variable in a register.
It computes the variables address every time a piece of code
references the variable.
Listing 2.1 shows an example of volatile variables.
Listing 2.1
volatile variables
void main(void)
{
int i[100];
volatile int a, b;
a = 5;
b = 20;
i[a + b] = 15;
i[a + b] = 30;
}
C a n d C + + L an g u ag e N o tes
The Metrowerks Implementation of C and C++
//
//
//
//
//
USUALLY OK.
Wrong only when you use
68K compiler and turn off
the Far Data option in the
Processor settings panel
Declaration specifiers
CodeWarrior lets you choose how to implement a function or variable with the declaration specifier __declspec(arg), where arg
specifies how to implement it. The Macintosh and Win32/x86 have
different sets of arguments
For 68K and PowerPC Macintosh code, arg can be one of the following values:
__declspec(internal) lets you specify that this variable
or function is internal and not imported. It corresponds to the
pragma internal, described at internal (Macintosh only)
on page 197.
__declspec(import) lets you import this variable or function which is in another code fragment or shared library. It
corresponds to the pragma import, described at import
(Macintosh only) on page 195.
__declspec(export) lets you export this variable or function from this code fragment or shared library. It corresponds
to the pragma export, described at export (Macintosh
only) on page 186.
__declspec(lib_export) ignores the pragmas export,
import, and internal for this variable or function. It corresponds to the pragma lib_export, described at lib_export
(Macintosh only) on page 198.
For Win32/x86 code, arg can be one of the following values:
__declspec(dllexport) specifies that this function or
variable is exported from the executable or DLL that defines
it.
__declspec(dllimport) specifies that this function or
variable is imported from another DLL or executable.
__declspec(naked) specifies that this function is entirely
implemented with assembler code and the compiler does not
need to produce any prefix or suffix code. Its the same as
using the asm keyword.
__declspec(thread) specifies that a copy of this global
variable (i.e. static or extern) is created for each separate
thread in this program. Creating separate copies can simplify
C a n d C + + L an g u ag e N o tes
The Metrowerks Implementation of C and C++
Enumerated types
This section describes how the C/C++ selects the underlying integer type for an enumerated type. There are two different different
strategies, depending on the setting of the Enum Always Int option
in the C/C++ Language settings panel, which corresponds to the
enumsalwaysint pragma.
If Enums Always Int is on, the underlying type is always signed
int. All enumerators must be no larger than a signed int. However, if the ANSI Strict option is off, enumerators that can be represented as an unsigned int are implicitly converted to signed
int. (The ANSI Strict option is in the C/C++ Language settings
panel and corresponds to the ANSI_strict pragma.)
Listing 2.2
#pragma enumsalwaysint on
#pragma ANSI_strict on
enum foo { a=0xFFFFFFFF }; // ERROR. a is 4,294,967,295:
//
too big for a signed int
#pragma ANSI_strict off
enum bar { b=0xFFFFFFFF }; // OK: b can be represented as an
//
unsigned int, but is implicitly
//
converted to a signed int (-1).
If Enums Always Int is off, the compiler picks one of the following:
If all enumerators are positive, it picks the smallest unsigned
integral base type that is large enough to represent all enumerators
If at least one enumerator is negative, it picks the smallest
signed integral base type large enough to represent all enumerators.
Listing 2.3
The compiler will only use long long base types if is the
longlong_enums pragma is on. (There is no settings panel option
corresponding to the longlong_enums pragma)
Listing 2.4
When the longlong_enums pragma is off and ANSI strict is on, you
cannot mix huge unsigned 32-bit enumerators (greater than
0x7FFFFFFF) and negative enumerators. If both the
longlong_enums pragma and the ANSI strict option are off, huge
unsigned 32-bit enumerators are implicitly converted to signed 32bit types.
C a n d C + + L an g u ag e N o tes
Number Formats
Listing 2.5
// error
// base type: signed int (b==-1)
For more information on Enums Always Int, see Enumerated constants of any size on page 51. For more information on ANSI Strict,
see ANSI extensions you disable with ANSI Strict on page 47. For
more information on the longlong_enums pragma, see
longlong_enums on page 199.
Number Formats
(K&R, A4.2) This section describes how the C and C++ compilers
implement integer and floating-point types. You can also read limits.h for more information on integer types and float.h for more
information on floating-point types.
This section contains the following:
68K Macintosh integer formats on page 30
68K Macintosh floating-point formats on page 32
PowerPC Macintosh, Magic Cap, and Win32/x86 integer
formats on page 33
PowerPC Macintosh and Win32/x86 floating-point formats on page 34
Magic Cap Floating-Point Formats on page 34
If this is true
Size is
bool
Always true
8 bits
true or false
char
8 bits
-128 to 127
8 bits
0 to 255
signed char
Always true
8 bits
-128 to 127
unsigned char
Always true
8 bits
0 to 255
short
Always true
16 bits
-32,768 to 32,767
unsigned
short
Always true
16 bits
0 to 65,535
int
16 bits
-32,768 to 32,767
4-Byte Ints is on
32 bits
-2,147,483,648 to
2,147,483,647
16 bits
0 to 65,535
4-Byte Ints is on
32 bits
0 to 4,294,967,295
long
Always true
32 bits
-2,147,483,648 to
2,147,483,647
unsigned long
Always true
32 bits
0 to 4,294,967,295
long long
Always true
64 bits
-9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
unsigned
long long
Always true
64 bits
0 to
18,446,744,073,709,551,615
unsigned int
C a n d C + + L an g u ag e N o tes
Number Formats
If this is true
Its size is
float
Always true
32 bits
1.17549e-38 to
3.40282e+38
short double
Always true
64 bits
2.22507e-308 to
1.79769e+308
double
8-Byte Doubles is on
64 bits
2.22507e-308 to
1.79769e+308
80 bits
1.68105e-4932 to
1.18973e+4932
96 bits
1.68105e-4932 to
1.18973e+4932
80 bits
1.68105e-4932 to
1.18973e+4932
68881 Codegen is on
96 bits
1.68105e-4932 to
1.18973e+4932
long double
If this is true
Size is
bool
Always true
8 bits
true or false
char
8 bits
-128 to 127
8 bits
0 to 255
signed char
Always true
8 bits
-128 to 127
unsigned char
Always true
8 bits
0 to 255
short
Always true
16 bits
-32,768 to 32,767
unsigned
short
Always true
16 bits
0 to 65,535
int
Always true
32 bits
-2,147,483,648 to
2,147,483,647
unsigned int
Always true
32 bits
0 to 4,294,967,295
long
Always true
32 bits
-2,147,483,648 to
2,147,483,647
unsigned long
Always true
32 bits
0 to 4,294,967,295
long long
Always true
64 bits
-9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
unsigned
long long
Always true
64 bits
0 to
18,446,744,073,709,551,615
C a n d C + + L an g u ag e N o tes
Number Formats
WARNING! Do not turn off the 4-Byte Ints option in Magic Cap
code. Although the Magic Cap compiler lets you change the setting of this option, your code will not run correctly if its off. It is on
by default.
Type
Size
Range
float
32 bits
1.17549e-38 to 3.40282e+38
short double
64 bits
2.22507e-308 to 1.79769e+308
double
64 bits
2.22507e-308 to 1.79769e+308
long double
64 bits
2.22507e-308 to 1.79769e+308
Type
Size
Range
float
32 bits
1.17549e-38 to 3.40282e+38
short double
64 bits
2.22507e-308 to 1.79769e+308
double
96 bits
1.68105e-4932 to 1.18973e+4932
long double
96 bits
1.68105e-4932 to 1.18973e+4932
Calling Conventions
(K&R, A8.6.3) This section describes the C and C++ calling conventions for both the Macintosh, Magic Cap, and Win32/x86 compilers.
It contains the following:
68K Macintosh calling conventions on page 35
PowerPC calling conventions on page 36
Win32/x86 calling conventions on page 39
Magic Cap calling conventions on page 39
C a n d C + + L an g u ag e N o tes
Calling Conventions
Calling a C function
return address
pointer to return value (if needed)
first argument
.
.
.
last argument
In the parameter area, parameters are laid out in the order they appear, with the left-most parameter at the lowest offset. Each parameter starts at a word boundary regardless of size. For example,
characters take up a word and doubles may not be on a double
C a n d C + + L an g u ag e N o tes
Calling Conventions
Figure 2.3 shows how the compiler would store the parameters in
function foo(), shown below. Note that r4, r5, and r6 are empty
and that the floating-point members of the struct are not stored in
floating-point registers. Also, the compiler fills up the floating-point
registers one after the other, even though the floating-point parameters do not follow each other.
typedef struct rec {
int i;
float f;
double d;
} rec;
void foo( int i1, float f1, double d1, rec r,
int i3, float f3, double d3 );
Figure 2.3
Parameter Stack
Floating-point Registers
24
i1
r3
i1
fp1
f1
28
f1
r4
empty
fp2
d1 (first word)
32
d1 (first word)
r5
empty
fp3
d1 (second word)
36
d1 (second word)
r6
empty
fp4
f3
40
rec.i
r7
rec.i
fp5
d3 (first word)
44
rec.f
r8
rec.f
fp6
d3 (second word)
48
r9
fp7
empty
52
r10
fp8
empty
56
i3
fp9
empty
60
f3
fp10
empty
64
d3 (first word)
fp11
empty
68
d3 (second word)
fp12
empty
fp13
empty
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
If youre declaring a function for an API, specify the standard calling convention with __stdcall. Its the same as the default calling
convention, except that the callee removes parameters from stack.
If youre declaring a non-static member function, the compiler automatically uses the __thiscall calling convention unless you explicitly specify the standard calling convention with __stdcall.
The __thiscall calling convention is the same as the standard
calling convention, except that it passes the this pointer in the ECX
register.
Extensions to C or C++
This section describes some of Metrowerks C and C++s extensions
to the C and C++ standards. You can disable most of these extensions with options in the Language preference panel, as shown in
Figure 2.4.
Figure 2.4
Using Prototypes
Enumerated constants of any size
Chars always
unsigned
Using prototypes
Storing strings
Inlining functions
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
Equivalent hexadecimal
ABCD'
0x41424344
ABC'
0x00414243
AB'
0x00004142
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
more than one opcode, enclose them in brackets. Listing 2.6 shows
two opcode inline functions.
Listing 2.6
NOTE: Only the 68K Macintosh C and C++ compilers lets you
use opcode inline function declarations. However, all the C++ compilers let you use C++ inline functions, declared with the inline
keyword.
asm { 0x4e714e71,0x4e714e71 };
// four 68K NOP instructions
asm ((char)'C',(char)'o',(short)'de',"Warrior");
}
A6, #$0000
; First two NOPs
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
64-bit integers
The C or C++ compiler lets you define a 64-bit integer with the type
specifier long long. This is twice as large as a long int, which
is a 32-bit integer. A long long can hold values from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. An
unsigned long long can hold values from 0 to
18,446,744,073,709,551,615.
In an enumerated type, you can use an enumerator large enough for
a long long. For more information, see Enumerated types on
page 28. However, long long bitfields are not supported.
You can disable the long long type with the pragma longlong,
described at longlong on page 198. There is no settings panel option to disable it. If this pragma is off, using long long causes a
syntax error. To check whether this option is on, use
__option (longlong), described in Options Checking on page
229. By default, this pragma is on.
The optional ANSI extensions are the following. If you turn on the
ANSI Strict option, the compiler generates an error if it encounters
any of these extensions.
C++-style comments on page 48
Unnamed arguments in function definitions on page 48
A # not followed by argument in macro definition on page
48
An identifier after #endif on page 48
Using typecasted pointers as lvalues on page 49
For more information on how this option affects enumerated types,
see Enumerated types on page 28.
The ANSI Strict option corresponds to the pragma ANSI_strict,
described at ANSI_strict on page 173. To check whether this option is on, use __option (ANSI_strict), described at
ANSI_strict on page 230. By default, this option is off.
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
C++-style comments
(K&R, A2.2) In the C compiler, you can use C++-style comments.
Anything that follows // on a line is considered a comment. For example:
a = b;
If you turn on the ANSI Strict option, you can make the identifiers
into comments, like this:
#ifdef __MWERKS__
# ifndef __cplusplus
/*
* . . .
*/
# endif /* __cplusplus */
#endif /* __MWERKS__ */
Using typecasted pointers as lvalues
The C and C++ compilers let you use a pointer that youve typecasted to another pointer type as an lvalue. For example:
char *cp;
((long *) cp)++; /* OK if ANSI Strict is off. */
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
Additional keywords
(K&R, A2.4) If youre writing code that must follow the ANSI standard strictly, turn on the ANSI Keywords Only option in the Language preference panel. The compiler generates an error if it
encounters any of the Metrowerks C/C++ additional keywords.
This sections contains the following:
Macintosh and Magic Cap keywords on page 50
Win32/x86 keywords on page 51
The ANSI Keywords Only option corresponds to the pragma
only_std_keywords, described at only_std_keywords on page
204. To check whether this option is on, use __option
(only_std_keywords), described at only_std_keywords on
page 233. By default, this option is off.
Macintosh and Magic Cap keywords
The 68K Macintosh, PowerPC Macintosh, and Magic Cap C /C++
compilers recognize three additional reserved keywords.
asm lets you compile a functions body with built-in assembler. For more information on how to use the built-in assembler, consult Overview of 68K Assembler Notes on page
115 and Overview of PowerPC Assembler Notes on page
125. (K&R, A10.1)
far (68K only) lets you declare a variable or a function to use
the far mode addressing regardless of how you set the options Far Data, Far Virtual Function Tables, and Far String
Constants in the Processor settings. For more information on
the far mode, see the CodeWarrior IDE Users Guide. (K&R,
A8.1)
NOTE: The PowerPC compiler ignores the far qualifier but does
not generate an error.
pascal lets you declare a function that uses Pascal calling
conventions. For information, see Calling Macintosh Tool-
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
enum BigNumber
{ ThreeThousandMillion = 3000000000 };
/* If Enuns Always Int is off, this type will
be the same size as a long int.
If this option is on, the compiler may
generate an error. */
For more information on how enumerated types are implemented,
see Enumerated types on page 28.
The Enums Always Int option corresponds to the pragma
enumsalwaysint, described at enumsalwaysints on page 184. To
check whether this option is on, use __option (enumsalwaysint), described at enumsalwaysint on page 231. By default, this
option is off.
Inlining functions
Metrowerks C/C++ gives you several different ways to inline both
C and C++ functions. When you call an inline function, the caller inserts the functions code instead of a function call. Inlining functions
makes your programs faster (since the compiler executes the functions code immediately without a function call), but possibly larger
(since the functions code may be repeated in several different
places).
If you turn off the ANSI Keywords Only option, you can declare C
functions to be inline, just as you do in C++. And the Inlining
menu lets you choose to inline all small functions, only functions
declared inline, or no functions, as shown in the table below:
This option
Does this
Dont Inline
Normal
Inlines only C and C++ functions declared inline and member functions defined within a
class declaration. Note that Metrowerks may not
be able to inline all the functions you declare inline.
Auto-Inline
Lets the compiler choose which functions to inline. Also inlines C++ functions declared inline and member functions defined within a
class declaration.
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
Using prototypes
(K&R, A8.6.3, A10.1) The C and C++ compilers let you choose
how to enforce function prototypes:
Requiring prototypes on page 54 explains the Require Prototypes option which forces you to prototype every function
so you can find errors caused by forgotten prototypes.
Relaxing pointer checking on page 56 explains the Relaxed
Pointer Type Rules option which treats char*, unsigned
char*, and Ptr as the same type.
Requiring prototypes
When the Require Prototypes option is on, the compiler generates
an error if you use a function that does not have a prototype. This
option helps you prevent errors that happen when you use a function before you define it. If you do not use function prototypes, your
code may not work as you expect even though it compiles without
error.
In Listing 2.10, PrintNum() is called with an integer argument but
is later defined to take a floating-point argument.
Listing 2.10
Unnoticed type-mismatch
#include <stdio.h>
void main(void)
{
PrintNum(1);
}
void PrintNum(float x)
{
printf("%f\n", x);
}
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
C a n d C + + L an g u ag e N o tes
Extensions to C or C++
Regardless of how this option is set, the compiler stores string constants used in the global scope in the global data segment. For example:
#pragma pcrelstrings on
int f(char *);
int x = f("Hello"); // "Hello" is allocated in
// the global data segment
int bar()
{
return f("World"); // "World" is allocated in
}
// the code segment
// (pc-relative)
#pragma pcrelstrings reset
NOTE: If you turn the Pool Strings option on, the compiler ignores the setting of the PC-Relative Strings option.
Reusing strings
If the Dont Reuse Strings option in the C/C++ Languages settings
panel is on, the compiler stores each string literal separately. If this
option is off, the compiler stores only one copy of identical string lit-
erals. This option helps you save memory if your program contains
lots of identical string literals which you do not modify.
For example, take this code segment:
char *str1="Hello";
char *str2="Hello"
*str2 = 'Y';
If this option is on, str1 is "Hello" and str2 is "Yello". If this
option is off, both str1 and str2 are "Yello".
The Dont Reuse Strings option corresponds to the pragma
dont_reuse_strings, described at dont_reuse_strings on page
184. To check whether this option is on, use __option
(dont_reuse_strings), described at dont_reuse_strings on
page 231. By default, this option is on. (Strings are not reused.)
C a n d C + + L an g u ag e N o tes
Warnings for Common Mistakes
The one option that isnt a warning is the Treat All Warnings as Errors option. If these option is on, the compiler treats all the warnings
the compiler generates, including the ones described here, as errors,
and it wont compile your code until you resolve them.
Figure 2.5
Illegal pragmas
If the Illegal Pragmas option is on, the compiler displays a warning
when it encounters an illegal pragma. For example, these pragma
statements generate warnings:
#pragma near_data off
// WARNING: near_data is not a pragma.
Empty declarations
If the Empty Declarations option is on, the compiler displays a
warning when it encounters a declaration with no variables. For example:
int ;
int i;
// WARNING
// OK
// WARNING: a=b is an
//
assignment
C a n d C + + L an g u ag e N o tes
Warnings for Common Mistakes
if ((a=b)!=0) f();
// OK: (a=b)!=0 is a
//
comparison
if (a==b) f();
// OK: (a==b) is a
//
comparison
An equal comparison in a statement that contains a single expression. This check is useful if you frequently use == when
you meant to use =. For example:
a == 0;
a = 0;
// WARNING: Unintended
//
infinite loop
If you intended to create an infinite loop, put white space or a comment between the while statement and the a comment. For example, these statements do not generate errors:
while (i++) ; // OK: White space separation
while (i++)
/*: Comment separation */ ;
The Possible Errors option corresponds to the pragma
warn_possunwant, described at warn_possunwant on page 223.
To check whether this option is on, use __option
(warn_possunwant), described at warn_possunwant on page
235. By default, this option is off.
Unused variables
If the Unused Variables option is on, the compiler generates a
warning when it encounters a variable you declare but do not use.
This check helps you find misspelled variable names and variables
you have written out of your program. For example:
void foo(void)
{
int temp, errer;
// ERROR: errer is
// misspelled
error = do_something()
// WARNING: temp and error are unused.
If you need to declare a variable that you dont use, use the pragma
unused, as in this example:
void foo(void)
{
int i, temp, error;
#pragma unused (i, temp) /* Compiler wont warn
error=do_something();
* that i and temp are
}
* not used
*/
The Unused Variables option corresponds to the pragma
warn_unusedvar, described at warn_unusedvar on page 225. To
check whether this option is on, use __option
(warn_unusedvar), described at warn_unusedvar on page 236.
By default, this option is off.
Unused arguments
If the Unused Arguments option is on, the compiler generates a
warning when it encounters an argument you declare but do not
use. This check helps you find misspelled argument names and arguments you have written out of your program.
void foo(int temp,int errer); // ERROR: errer is
// misspelled
{
error = do_something();
}
// WARNING: temp and error are unused.
If you need to declare an argument that you dont use, there are two
ways to avoid this warning. You can use the pragma unused, as in
this example:
void foo(int temp, int error)
{
#pragma unused (temp) /* Compiler wont warn
error=do_something(); * that temp is not used
}
*/
C a n d C + + L an g u ag e N o tes
Warnings for Common Mistakes
You can also turn off the ANSI Strict option, and not give the unused argument a name, like this:
void foo(int /* temp */, int error)
{
/* Compiler wont warn
#pragma unused (temp)
* that temp is not used
error=do_something(); */
}
The Unused Arguments option corresponds to the pragma
warn_unusedarg, described at warn_unusedarg on page 224. To
check whether this option is on, use __option
(warn_unusedarg), described at warn_unusedarg on page 235.
By default, this option is off.
Extra commas
If the Extra Commas option is on, the compiler generates a warning
when it encounters an extra comma. For example, this statement is
legal in C, but it causes a warning when this option is on:
int a[] = { 1, 2, 3, 4, };
// ^ WARNING: Extra comma
//
after 4
The Extra Commas option corresponds to the pragma
warn_extracomma, described at warn_extracomma on page 222.
To check whether this option is on, use __option
(warn_extracomma), described at warn_extracomma on page
235. By default, this option is off.
/* WARNING */
/* OK */
/* OK */
C a n d C + + L an g u ag e N o tes
Warnings for Common Mistakes
Function hiding
If the Hidden virtual functions option is on, the compiler generates
a warning if you declare a non-virtual member function that hides a
virtual function in a superclass. One function hides another if it has
the same name but a different argument types. For example:
class A {
public:
virtual void f(int);
virtual void g(int);
};
class B: public A {
public:
void f(char);
// WARNING:
// Hides A::f(int)
virtual void g(int); // OK:
// Overrides A::g(int)
};
The Hidden virtual functions option corresponds to the pragma
warn_hidevirtual, described at warn_hidevirtual on page 222.
To check whether this option is on, use __option
(warn_hidevirtual), described at warn_hidevirtual on page
235. By default, this option is off.
C a n d C + + L an g u ag e N o tes
Generating Code for Specific 68K Processors (Macintosh Only)
Figure 2.6
Compiling
for a specific
68K chip
To check whether the computer on which your application is running has a specific processor use the gestalt() function. The following code sample displays an alert if the application is for an
MC68881 and the machine does not have an MC68881:
void main(void)
{
#if __option (code68881)
if (!HasFPU())
// Calls gestalt() to check
{
// if the computer has FPU
DisplayNoFPU(); // Displays an alert
return;
// saying there is no FPU
}
#endif
// . . .
}
TIP: For more information on gestalt(), see Chapter Gestalt
Manager in Inside Macintosh: Operating System Utilities.
Note that HasFPU() and DisplayNoFPU() are not Toolbox functions. If you use this code, you must define these functions.
C a n d C + + L an g u ag e N o tes
Generating Code for Specific 68K Processors (Macintosh Only)
The rest of this section describes what happens when you turn on
the MC68881 Codegen option.
Using the Extended data type
If you turn on the 68881 Codegen option, the compiler stores any
variable declared extended or long double in the Motorola 96bit format, instead of the SANE 80-bit format. Both formats meet the
IEEE standards for accuracy. The main difference between them is
that the 96-bit format contains 16 bits of padding so that an extended number fits evenly into three 32-bit memory accesses.
Types.h defines the extended type. SANE.h contains two other
type definitions: extended80 and extended96. It also contains
functions that convert between 80-bit and 96-bit formats:
x96tox80() and x80tox96().
NOTE: The PowerPC architecture does not support the
extended type. Use double instead.
C a n d C + + L an g u ag e N o tes
Calling MPW Functions
MPW C options
Note that even if you turn on the MPW C Calling Convention option, MPW and Metrowerks arent completely compatible in certain
situations. For more information, see Declaring MPW C functions
(Macintosh Only) on page 75.
This section contains the following:
Adding an MPW library to a CodeWarrior project on page
73
Declaring MPW C functions (Macintosh Only) on page 75
Using MPW C newlines on page 76
WARNING! Do not turn off the MPW C Calling Convention or
Map Newlines to CR options in Magic Cap code. Although the
Magic Cap compiler lets you change the settings of these options,
your code will not run correctly if theyre off. They are on by default.
Add the library to your project with the Add Files command in
the Project menu.
2.
If youre using a 68K library, turn on the MPW C Calling Convention option.
You can either turn on the MPW C Calling Convention option in
the Processor preference panel, or you can use the pragma mpwc. If
you use the MPW C Calling Convention option, all functions in
your project use MPW C calling conventions. If you use the pragma
C a n d C + + L an g u ag e N o tes
Calling MPW Functions
mpwc, only those functions declared with that pragma use MPW C
calling convention.
To use the pragma, turn on the pragma mpwc in the header file that
declares the MPW C functions, declare the functions, and turn off
the pragma mpwc. For example:
#pragma mpwc on
int func1(double a, int b);
int func2(int a, double b);
#pragma mpwc reset
For more information, see Declaring MPW C functions (Macintosh
Only) on page 75.
3.
4.
5.
To this:
long MPWfunc( long a, long b, long c,
long d, char *e );
Passes any floating-point arguments as a long double. For
example, the compiler converts this declaration:
void MPWfunc( float a, double b,
long double c );
To this:
void MPWfunc( long double a, long double b,
long double c );
Returns any pointer value in D0 (even if the pragma
pointers_in_D0 is off).
Returns any 1-byte, 2-byte, or 4-byte structure in D0.
If the 68881 Codegen option is on, returns any floating-point
value in FP0.
NOTE: The MPW C Calling Conventions option is available
only with the 68K compilers. The PowerPC compilers dont need it,
since all PowerPC compilers use the same calling conventions.
C a n d C + + L an g u ag e N o tes
Calling MPW Functions
Note that even if you turn on the MPW C Calling Convention option, MPW and Metrowerks arent completely compatible in these
situations:
Metrowerks C++ and MPW C++ classes are generally not
compatible. Unless you follow the directions in Declaring
MPW-Compatible Classes on page 104, you cannot use a
Metrowerks C++ library in MPW or an MPW C++ library in
a CodeWarrior project. If you need to use an MPW C library
with Metrowerks C++ code, dont turn on the MPW C Calling Conventions option. Instead use the pragma mpwc as
needed for non-member functions.
To use MPW C functions that return a floating-point value,
you must turn on the 68881 Codegen option. If that option is
off, Metrowerks C returns a long double value in a temporary
variable, while MPW C returns it in a register.
This option corresponds to the pragma mpwc, described at mpwc
(68k Macintosh only) on page 200. To check whether this pragma is
on, __option(mpwc), described at mpwc (68K only) on page
233. By default, this option is off.
C a n d C + + L an g u ag e N o tes
Calling Macintosh Toolbox Functions (Macintosh Only)
C a n d C + + L an g u ag e N o tes
Intrinsic PowerPC Functions (Macintosh Only)
*/
void __sync(void)
/* Synchronize
*/
void __isync(void)
/* Instruction Synchronize
*/
Floating-point functions
These functions generate inline instructions that take the absolute
value of a number.
int __abs(int);
/* Absolute value of an integer.
*/
float __fabs(float);
/* Absolute value of a float.
*/
float __fnabs(float);
/* Negative of the absolute value of a float.*/
long __labs(long);
/* Absolute value of a long int.
*/
Byte-reversing functions
These functions generate inline instructions than can dramatically
speed up certain code sequences, especially byte-reversal operations
int __cntlzw(int);
/* Count leading zeros in a integer.
*/
*/
*/
*/
*/
C a n d C + + L an g u ag e N o tes
Intrinsic PowerPC Functions (Macintosh Only)
*/
*/
double __fsqrte(double);
/* Floating Reciprocal Square Root Estimate */
double __fsel(double, double, double)
/* Floating Select
*/
C a n d C + + L an g u ag e N o tes
Intrinsic PowerPC Functions (Macintosh Only)
3
C++ Language Notes
This chapter describes how Metrowerks C++ handles the parts of
the C++ language that are unique to C++ and not shared by C.
C+ + L a n g u ag e N o tes
Unsupported Extensions
Unsupported Extensions
The C++ compiler does not currently support these common extension to The Annotated C++ Reference Manual (Addison-Wesley) by
Ellis and Stroustrap:
Overloading methods operator new[] and operator
delete[], which let you allocate and deallocate the memory
for a whole array of objects at once. Instead, overload operator new() and operator delete(), which are the functions that operator new[] and operator delete[]
call. (ARM, 5.3.3, 5.3.4)
Name spaces
The mutable keyword
C++ Languag e N o t es
Metrowerks Implementation of C++
// OK
// ERROR
// OK
// ERROR
Additional keywords
(ARM 2.4, ANSI 2.8) In addition to reserving the symbols in 2.3
of the ARM as keywords, Metrowerks C++ reserves these symbols
from 2.8 of the ANSI Draft C++ Standard as keywords:
bool
explicit
namespace
true
const_cast
false
reinterpret_char
typeid
dynamic_cast
mutable
static_cast
using
C+ + L a n g u ag e N o tes
Metrowerks Implementation of C++
other words, unless the second and third expressions are numeric
types, they must be the same type.
Listing 3.2
};
AA); // OK
BB); // ERROR: BB is not declared
};
//
yet
C++ Languag e N o t es
Metrowerks Implementation of C++
C+ + L a n g u ag e N o tes
Metrowerks Implementation of C++
Constructors
class Simple { int f; };
void simpleFunc (Simple s1)
{
Simple s2=Simple(s1);
// ERROR: An explicit copy constructor
//
call. The compiler generates
//
no default copy constructor.
Simple s3=s1;
// OK: The compiler performs a
}
//
bitwise copy
C++ Languag e N o t es
Metrowerks Implementation of C++
This stub checks for a floating-point unit: (Note that you must define the functions HasFPU() and DisplayNoFPU() yourself.)
Listing 3.6
C+ + L a n g u ag e N o tes
Setting C++ Options
This example creates a Q class that draws its objects by adding behavior to the O class:
Listing 3.7
// Perform behavior of
//
base class
// Perform added behavior
C++ Languag e N o t es
Setting C++ Options
Figure 3.1
C+ + L a n g u ag e N o tes
Setting C++ Options
C++ Languag e N o t es
Setting C++ Options
C+ + L a n g u ag e N o tes
Using Run-Time Type Information (RTTI)
C++ Languag e N o t es
Using Run-Time Type Information (RTTI)
C+ + L a n g u ag e N o tes
Using Run-Time Type Information (RTTI)
C++ Languag e N o t es
Using Templates
Using Templates
(ARM, 14) This section describes the best way to organize your
template declarations and definitions in files. It also documents how
to explicitly instantiate templates, using a syntax that is not in the
ARM but is part of the ANSI C++ draft standard.
This section contains the following:
Declaring and defining templates on page 100
Instantiating templates on page 101
C+ + L a n g u ag e N o tes
Using Templates
In a source file, include the header file, and define the function templates and the member functions of the class templates, as shown in
Listing 3.9. This is a template definition file. Youll include this file in
any file that uses your templates. You do not need to add the template definition file to your project.
Listing 3.9
C++ Languag e N o t es
Using Templates
Instantiating templates
The template definition file does not generate code. The compiler
cannot generate code for a template until you specify what values it
should substitute for the templates arguments. Specifying these values is called instantiating the template.
Metrowerks C++ gives you two ways to instantiate a template. You
can let the compiler instantiate it automatically when you first use
it, or you can explicitly create all the instantiations youll need in
one place:
If you use automatic instantiation, the compiler may take
longer to compile your program since it has to determine on
its own which instantiations youll need. Also, the object
code for the template instantiations will be scattered
throughout your program.
If you use explicit instantiation, the compiler compiles your
program quicker. Since the instantiations can be in one file,
with no other code, you can choose to put them all in one
segment or even in a separate library.
C+ + L a n g u ag e N o tes
Using Templates
To instantiate templates automatically, include the template definition file in all the source files that use the templates, and just use the
templates as you would any other type or function. The compiler
automatically generates code for a template instantiation whenever
it sees a new one. Listing 3.10 shows how to automatically instantiate the templates in Listing 3.8 and Listing 3.9.
Listing 3.10
C++ Languag e N o t es
Using Exceptions
Listing 3.11
Using Exceptions
If you turn on the Enable C++ Exceptions options in the C/C++
Languages preference panel, you can use the try and catch statements to perform exception handling. If your program doesnt use
exception handling, turn this option to make your program smaller.
You can throw exceptions across any code thats compiled by the
CodeWarrior 8 (or later) Metrowerks C/C++ compiler with the Enable C++ Exceptions option turned on. You cannot throw exceptions across the following:
Macintosh Toolbox function calls
Libraries compiled with the Enable C++ Exceptions option
turned off
Libraries compiled with versions of the Metrowerks C/C++
compiler earlier than CodeWarrior 8
Libraries compiled with Metrowerks Pascal or other
compilers.
C+ + L a n g u ag e N o tes
Declaring MPW-Compatible Classes
Since these classes do not let you use multiple-inheritance or runtime type information (RTTI), they can save you some overhead.
The compiler stores information about an objects virtual functions
in a data structure called a virtual table. The virtual table for a singleinheritance object can be much simpler and smaller than the one for
a multiple-inheritance object.
HandleObject has all the features as SingleObject, with one additional feature: Any object descended from it is automatically stored
on the applications heap, and you reference the object with a handle. You treat these handles as pointers, since the compiler automat-
C++ Languag e N o t es
Creating Direct-to-SOM Code
// OK
// OK
// ERROR
Avoid taking the address of a member of a HandleObject object (such as &myObj->a). Since the object is in the heap, it
may move unexpectedly and the address will point to garbage.
C+ + L a n g u ag e N o tes
Creating Direct-to-SOM Code
There are two ways to create SOM code. You can turn select On or
On with Environment Checks from the Direct to SOM menu in the
C/C++ Language preference panel, or use the direct_to_som
pragma before you import any SOM header files, like this:
#pragma direct_to_som on
If you select On with Environment Checks from the Direct to SOM
menu, the compiler performs some automatic error checking, as described in Automatic SOM error checking on page 109.
Note that when you turn on the Direct to SOM option, you should
turn on the Enums Always Int option in the C/C++ Language preference panel, described in Enumerated constants of any size on
page 51.
Also, when you define a SOM class, Metrowerks C/C++ uses
PowerPC alignment for that class. In other words, the compiler acts
as though you enclosed the class definition with #pragma options align=powerpc and #pragma options align=reset.
For more information on structure alignment, see Targeting Mac OS.
The rest of this section describes the restrictions SOM code must
abide by, some useful SOM header files, and pragmas for SOM
classes:
SOM class restrictions on page 106
Using SOM headers on page 109
Using SOM pragmas on page 111
C++ Languag e N o t es
Creating Direct-to-SOM Code
When you create a SOM class and define its members, keep these restrictions in mind:
The base class must be SOMObject or a descendant of SOMObect. If you use multiple inheritance, all parent classes must
be descendants of SOMObject. (You cannot mix SOM classes
with other classes in the base list for any class.)
You must declare the class with the class keyword. A class
declared as struct or union cannot be a SOM class.
All the class inheritance must be virtual.
All the classs data members must be private.
The only member functions you can overload are inline
member functions that are not virtual. They are not considered to be SOM methods.
The only operations you can overload are new and delete.
The class must contain at least one member function thats
not inline. MacSOM uses the first such class to determine
whether the class is implemented in a particular compilation
unit.
The class cannot contain the following:
nested class definitions
static data or function members.
constructors (ctors) with parameters.
copy constructors
In a member function, you cannot do the following:
use long double parameters or return type
use a variable length argument list
When you use a SOM class in your code, remember that you cannot
do the following:
Create global SOM objects.
Use sizeof() with SOM objects or classes.
Create class templates that expand to SOM objects.
Create arrays of SOM objects.
C+ + L a n g u ag e N o tes
Creating Direct-to-SOM Code
Use the placement and array forms of new (such as new(address) T or new T[n]) or the array form of delete (such
as delete [] p).
Declare SOM classes as members of other classes. (You can
declare pointers to SOM class objects as members.)
Take the address of a member of a SOM class. For example,
&foo::bar is not allowed if foo is a SOM class.
Pass aggregate parameters by value to a SOM member function.
Use SOM objects as function parameters. (You can use a
pointer to a SOM object as a parameter. )
Perform an assignment with SOM classes
Return a SOM object as a functions value
Also when you invoke a method with explicit scope (such as
obj->B::func()), the specified class (B) must be the same class as
the object (obj) or a direct parent of the objects class.
For example, if class A is the parent of class B which is the parent of
class C, then
C* obj = new C;
obj->C::func();
obj->B::func();
obj->A::func();
C++ Languag e N o t es
Creating Direct-to-SOM Code
SOM Headers
This header
Contains this
somobj.hh
SOMObject, a SOM base class. If your file subclasses from SOMObject, include this header. If
youre converting a file from IDL to Metrowerks
C++, you can use this header as a replacement for
somobj.idl and somobj.xh.
somcls.hh
som.xh
somobj.xh
somcls.xh
C+ + L a n g u ag e N o tes
Creating Direct-to-SOM Code
C++ Languag e N o t es
Creating Direct-to-SOM Code
C+ + L a n g u ag e N o tes
Creating Direct-to-SOM Code
C++ Languag e N o t es
Creating Direct-to-SOM Code
C+ + L a n g u ag e N o tes
Creating Direct-to-SOM Code
4
68K Assembler
Notes
This chapter describes the 68K assembler that is part of the
CodeWarrior package of compilers.
6 8 K A s s e m bler N o tes
Writing an Assembly Function for 68K
bfclr
bfexts
bfextu
bfffo
bfins
bfset
bftst
68K Assembler N o t es
Writing an Assembly Function for 68K
divsl
divs.l
divul
divu.l
muls.l
mulu.l
extb.l
rtd
0xABCDEF, d5
$ABCDEF, d5
// OK
// ERROR
Assembler directives, instructions, and registers are not casesensitive. For example these two statements are same:
move.l
MOVE.L
b, DO
b, d0
// OK
// ALSO OK
6 8 K A s s e m bler N o tes
Writing an Assembly Function for 68K
d5,d5
d5,d5
d5,d5
; ERROR
// OK
/* OK */
The rest of this section describes how to create local variables, access
function parameters, refer to fields within a structure, and use the
preprocessor with the assembler. A section at the end of the chapter
describes some special assembler directives that the built-in assembler allows.
68K Assembler N o t es
Writing an Assembly Function for 68K
// Moving x into d0
6 8 K A s s e m bler N o tes
Writing an Assembly Function for 68K
n,a
a,a
a,D0
frfree
rts
}
struct(WindowRecord.refCon) (A0), D0
68K Assembler N o t es
Writing an Assembly Function for 68K
moveq %0,D0
// No RTS statement
// OK
6 8 K A s s e m bler N o tes
Assembler directives
Assembler directives
This section describes some special assembler directives that the
Metrowerks built-in assembler accepts. The directives are listed alphabetically.
dc
dc[.(b|w|l)] constexpr (,constexpr)*
Defines a block of constant expressions, constexpr, as initialized
bytes, words, or long words. If there is no qualifier, .w is assumed.
For dc.b you can specify any string constant (C or Pascal). For dc.w
you can specify any 16-bit relative offset to a local label. For example:
asm
{
x1:
x2:
x3:
}
void foo(void)
dc.b
dc.w
dc.l
ds
ds[.(b|w|l)] size
Defines a block of size bytes, words, or longs. The block is initialized
with null characters. If there is no qualifier, .w is assumed. For example, this statement defines a block big enough for the structure
DRVRHeader.
ds.b
sizeof(DRVRHeader)
68K Assembler N o t es
Assembler directives
entry
entry [extern|static] name
Defines an entry point into the current function. Use the extern
qualifier to declare a global entry point and use the static qualifier
to declare a local entry point. If you leave out the qualifier, extern
is assumed.
Listing 4.3
static MyEntry
b,d0
fralloc
fralloc [+]
Lets you declare local variables in an assembly function. The fralloc directive makes space on the stack for your local stack variables
and reserves registers for your local register variables (with the
statement link #x,a6). For more information, see Using Local
Variables and Arguments in 68K Assembly on page 119.
There are two versions of fralloc. The fralloc directive (without
a +) , pushes modified registers onto the stack. The fralloc + directive also pushes all register arguments into their 68K registers.
frfree
frfree
Frees the stack storage area and restores the registers (with the statement unlk a6) that fralloc reserved. For more information, see
6 8 K A s s e m bler N o tes
Assembler directives
68010
68020
68030
68040
68349
68881
68882
68851
To use the following MC68020 assembler instructions, specify
68020, 68030, or 68040:
bfchg
bfclr
bfexts
bfextu
bfffo
bfins
bfset
bftst
divsl
divs.l
divul
divu.l
muls.l
mulu.l
extb.l
rtd
0xA860
// WaitNextEvent
5
PowerPC Assembler
Notes
This chapter describes the PowerPC assembler that is part of the
CodeWarrior package of compilers.
PowerPC Assembler N o t es
Writing an Assembly Function for PowerPC
The built-in assembler uses all the standard PowerPC assembler instructions. It accepts some additional directives described in PowerPC Assembler Directives on page 134. If you use the machine
directive, you can also use instructions that are available only in certain versions of the PowerPC. For more information, see machine
on page 136.
Keep these tips in mind as you write assembly functions:
All statements must follow this syntax:
[LocalLabel:] (instruction | directive) [operands]
Each instruction must end with a newline or a semicolon (;).
Hex constants must be in C-style , not Pascal-style. For example:
li
li
r3, 0xABCDEF
r3, $ABCDEF
// OK
// ERROR
Assembler directives, instructions, and registers are case-sensitive and must be in lowercase. For example these two statements are different:
add
ADD
r2,r3,r4
R2,R3,R4
// ok
// ERROR
// OK
The rest of this section describes how to create local variables, access
function parameters, refer to fields within a structure, and use the
preprocessor with the assembler. A section at the end of the chapter
describes some special assembler directives that the built-in assembler allows.
//
//
//
//
OK
OK
ERROR: Needs colon
OK
PowerPC Assembler N o t es
Writing an Assembly Function for PowerPC
r3,r4,r5
r3,r4,r5
r3,r4,r5
# ERROR
// OK
/* OK */
memory for your local variables and saves and restores the register
contents. This example shows where to put these directives:
asm void foo ()
{
fralloc
// Your code here
frfree
blr
}
The fralloc directive has an optional argument number which lets
you specify the size in bytes of the parameter area of the stack
frame. By default, the compiler creates a 32-byte parameter area. If
your assembly-language routine calls any function that takes more
than 32 bytes of parameters, you must specify a larger amount.
PowerPC Assembler N o t es
Writing an Assembly Function for PowerPC
For more information on PowerPC register conventions and argument-passing conventions, see the Apple Assembler for PowerPC on
the CodeWarrior CD.
bne+
bne-
@2
@1
Overflow, which sets the SO and OV bits in the XER if the result overflows. This form ends in the letter "o".
Overflow and Record, which sets both registers. This form
ends in "o.".
add
add.
addo
addo.
r3,r4,r5
r3,r4,r5
r3,r4,r5
r3,r4,r5
//
//
//
//
//
Normal add
Add with record: sets cr0
Add with overflow:sets XER
Add with overflow and
record: sets cr0 and XER
//
//
//
PowerPC Assembler N o t es
Writing an Assembly Function for PowerPC
names. For bla and la instructions, you use absolute addresses For
other branches, you must use the name of a label. For example:
b
b
bl
bl
bne
@3
foo
//
//
//
@3
//
foo //
foo //
//
r3,myVar(SP)
r3,myVar(SP)
lwz
lwz
lwz
lbz
r3,myRect.top
r3,myArray[2](SP)
r3,myRectArray[2].top
r3,myRectArray[2].top+1(SP)
r3,myGlobalRect(RTOC)
// load TOC pointer for myGlobalRect
r4,Rect.top(r3)
// fetch 'top' field
r3,myGlobalRect.top(RTOC)
// nonsensical
You use the same method for obtaining the address of a function:
lwz
r3,myFunction(RTOC)
// load TOC-pointer for TVector
// to myFunction
r4,Rect.top(r3)
r6,r6,Rect.left
PowerPC Assembler N o t es
PowerPC Assembler Directives
Listing 5.2
fralloc
fralloc [ number ]
Creates a stack frame for a function and reserves registers for your
local register variables. You need to create a stack frame, if the function
Calls other functions
Uses more than 224 bytes of local variables
Declares local register variables.
For more information, see Creating a Stack Frame for PowerPC Assembly on page 129.
The fralloc directive has an optional argument number which lets
you specify the size in bytes of the parameter area of the stack
frame. By default, the compiler creates a 32-byte parameter area. If
your assembly-language routine calls any function that takes more
than 32 bytes of parameters, you must specify a larger amount.
frfree
frfree
Frees the stack frame and restores the registers that fralloc reserved. For more information, see Creating a Stack Frame for PowerPC Assembly on page 129.
NOTE: The frfree directive does not generate a blr instruction. You must include one explicitly.
machine
machine number
Specifies which CPU the assembly code is for. The number must be
one of the following:
601
603
604
all
If you use all, you can use only those instructions that are available
on all PowerPC CPUs. If you dont use the machine directive, the
compiler assumes all.
PowerPC Assembler N o t es
PowerPC Assembler Directives
If you use 601, you can also use the following instructions:
abs
abs.
abso
abso.
clcs
div
div.
divo
divo.
doz
doz.
dozo
dozo.
dozi
lscbx
lscbx.
maskg
maskg.
markir
markir.
mul
mul.
mulo
mulo.
nabs
nabs.
nabso
nabso.
rlmi
rlmi.
rrib
rrib.
sle
sle.
sleq
sleq.
sliq
sliq.
slliq
slliq.
sllq
sllq.
slq
slq.
sraig
sraig.
sraq
sraq.
sre
sre.
srea
srea.
sreq
sreq.
sriq
sriq.
srliq
srliq.
srlq
srlq.
srq
srq.
tlbie
If you use 603 or 604, you can also use the following instructions:
fres
fres.
frsqrte
frsqrte.
fsel
fsel.
mftb
mftbl
stfiwx
tlbld
tlbli
tlbsync
smclass
smclass PR | GL
Lets you set the class for a function. By default, all functions have
class {PR} which means they are normal executable code. If youre
writing a glue routine, like the __ptr_glue routine that implements calls through function pointers, use smclass GL to set the
class to {GL}.
You shouldn't need this directive for your own code, but CodeWarrior PowerPC runtime library uses it frequently
Arguments
Description
abs
rD,rA
Absolute [601]
abs.
rD,rA
Absolute [601]
abso
rD,rA
Absolute [601]
abso.
rD,rA
Absolute [601]
add
rD,rA,rB
Add
add.
rD,rA,rB
Add
addo
rD,rA,rB
Add
addo.
rD,rA,rB
Add
addc
rD,rA,rB
Add Carrying
addc.
rD,rA,rB
Add Carrying
addco
rD,rA,rB
Add Carrying
addco.
rD,rA,rB
Add Carrying
adde
rD,rA,rB
Add Extended
adde.
rD,rA,rB
Add Extended
addeo
rD,rA,rB
Add Extended
addeo.
rD,rA,rB
Add Extended
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
addi
rD,rA,SIMM
Add Immediate
addic
rD,rA,SIMM
addic.
rD,rA,SIMM
addis
rD,rA,SIMM
addme
rD,rA
addme.
rD,rA
addmeo
rD,rA
addmeo.
rD,rA
addze
rD,rA
addze.
rD,rA
addzeo
rD,rA
addzeo.
rD,rA
and
rA,rS,rB
AND
and.
rA,rS,rB
AND
andc
rA,rS,rB
andc.
rA,rS,rB
andi.
rA,rS,UIMM
AND Immediate
andis.
rA,rS,UIMM
AND Immediate
target
Branch
ba
address
Branch Absolute
bc
BO,BI,target
Branch Conditional
bcctr
BO,BI
bcctrl
BO,BI
bcl
BO,BI,target
bclr
BO,BI
bclrl
BO,BI
bctr
Instruction
Arguments
Description
Branch to Count Register and Link
bctrl
bdnz
target
bdnzf
BI,target
bdnzfl
BI,target
bdnzflr
BI
bdnzflrl
BI
bdnzl
target
bdnzlr
bdnzlrl
bdnzt
BI,target
bdnztl
BI,target
bdnztlr
BI
bdnztlrl
BI
bdz
target
bdzf
BI,target
bdzfl
BI,target
Decrement CTR, branch if CTR zero and condition False and Link
bdzflr
BI
Decrement CTR, branch if CTR zero and condition False to Link Register
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
bdzflrl
BI
Decrement CTR, branch if CTR zero and condition False to Link Register and Link
bdzl
target
bdzlr
bdzlrl
bdzt
BI,target
bdztl
BI,target
Decrement CTR, branch if CTR zero and condition True and Link
bdztlr
BI
Decrement CTR, branch if CTR zero and condition True to Link Register
bdztlrl
BI
Decrement CTR, branch if CTR zero and condition True to Link Register and Link
beq
[crf,]target
Branch if Equal
beqctr
[crf]
beqctrl
[crf]
beql
[crf,]target
beqlr
[crf]
beqlrl
[crf]
bf
BI,target
bfctr
BI
bfctrl
BI
bfl
BI,target
bflr
BI
bflrl
BI
bge
[crf,]target
Instruction
Arguments
Description
bgectr
[crf]
bgectrl
[crf]
bgel
[crf,]target
bgelr
[crf]
bgelrl
[crf]
bgt
[crf,]target
Branch if Greater
bgtctr
[crf]
bgtctrl
[crf]
bgtl
[crf,]target
bgtlr
[crf]
bgtlrl
[crf]
bl
target
bla
address
ble
[crf,]target
blectr
[crf]
blectrl
[crf]
blel
[crf,]target
blelr
[crf]
blelrl
[crf]
blr
blrl
blt
[crf,]target
Branch if Less
bltctr
[crf]
bltctrl
[crf]
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
bltl
[crf,]target
bltlr
[crf]
bltlrl
[crf]
bne
[crf,]target
bnectr
[crf]
bnectrl
[crf]
bnel
[crf,]target
bnelr
[crf]
bnelrl
[crf]
bng
[crf,]target
bngctr
[crf]
bngctrl
[crf]
bngl
[crf,]target
bnglr
[crf]
bnglrl
[crf]
bnl
[crf,]target
bnlctr
[crf]
bnlctrl
[crf]
bnll
[crf,]target
bnllr
[crf]
bnllrl
[crf]
bns
[crf,]target
bnsctr
[crf]
Instruction
Arguments
Description
bnsctrl
[crf]
bnsl
[crf,]target
bnslr
[crf]
bnslrl
[crf]
bnu
[crf,]target
bnuctr
[crf]
bnuctrl
[crf]
bnul
[crf,]target
bnulr
[crf]
bnulrl
[crf]
bso
[crf,]target
bsoctr
[crf]
bsoctrl
[crf]
bsol
[crf,]target
bsolr
[crf]
bsolrl
[crf]
bt
BI,target
btctr
BI
btctrl
BI
btl
BI,target
btlr
BI
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
btlrl
BI
bun
[crf,]target
Branch if Unordered
bunctr
[crf]
bunctrl
[crf]
bunl
[crf,]target
bunlr
[crf]
bunlrl
[crf]
clcs
rD,rA
cmp
crfD,L,rA,rB
Compare
cmpi
crfD,L,rA,SIMM
Compare Immediate
cmpl
crfD,L,rA,rB
Compare Logical
cmpli
crfD,L,rA,UIMM
cmplw
[crfD,]rA,rB
cmplwi
[crfD,]rA,UIMM
cmpw
[crfD,]rA,rB
Compare Word
cmpwi
[crfD,]rA,SIMM
cntlzw
rA,rS
crand
crbD,crbA,crbB
crandc
crbD,crbA,crbB
creqv
crbD,crbA,crbB
crnand
crbD,crbA,crbB
crnor
crbD,crbA,crbB
cror
crbD,crbA,crbB
Condition Register OR
crorc
crbD,crbA,crbB
crxor
crbD,crbA,crbB
Instruction
Arguments
Description
dcbf
rA,rB
dcbi
rA,rB
dcbst
rA,rB
dcbt
rA,rB
dcbtst
rA,rB
dcbz
rA,rB
div
rD,rA,rB
Divide [601]
div.
rD,rA,rB
Divide [601]
divo
rD,rA,rB
Divide [601]
divo.
rD,rA,rB
Divide [601]
divs
rD,rA,rB
divs.
rD,rA,rB
divso
rD,rA,rB
divso.
rD,rA,rB
divw
rD,rA,rB
Divide Word
divw.
rD,rA,rB
Divide Word
divwo
rD,rA,rB
Divide Word
divwo.
rD,rA,rB
Divide Word
divwu
rD,rA,rB
divwu.
rD,rA,rB
divwuo
rD,rA,rB
divwuo.
rD,rA,rB
doz
rD,rA
doz.
rD,rA
dozo
rD,rA
dozo.
rD,rA
dozi
rD,rA,SIMM
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
eciwx
rD,rA,rB
ecowx
rD,rA,rB
eieio
eqv
rA,rS,rB
Equivalent
eqv.
rA,rS,rB
Equivalent
extsb
rA,rS
extsb.
rA,rS
extsh
rA,rS
extsh.
rA,rS
fabs
frD,frB
fabs.
frD,frB
fadd
frD,frA,frB
Floating-Point Add
fadd.
frD,frA,frB
Floating-Point Add
fadds
frD,frA,frB
fadds.
frD,frA,frB
fcmpo
[crfD,]frA,frB
fcmpu
[crfD,]frA,frB
fctiw
frD,frB
fctiw.
frD,frB
fctiwz
frD,frB
fctiwz.
frD,frB
fdiv
frD,frA,frB
Floating-Point Divide
fdiv.
frD,frA,frB
Floating-Point Divide
fdivs
frD,frA,frB
fdivs.
frD,frA,frB
Instruction
Arguments
Description
fmadd
frD,frA,frC,frB
Floating-Point Multiply-Add
fmadd.
frD,frA,frC,frB
Floating-Point Multiply-Add
fmadds
frD,frA,frC,frB
fmadds.
frD,frA,frC,frB
fmr
frD,frB
fmr.
frD,frB
fmsub
frD,frA,frC,frB
Floating-Point Multiply-Subtract
fmsub.
frD,frA,frC,frB
Floating-Point Multiply-Subtract
fmsubs
frD,frA,frC,frB
fmsubs.
frD,frA,frC,frB
fmul
frD,frA,frC
Floating-Point Multiply
fmul.
frD,frA,frC
Floating-Point Multiply
fmuls
frD,frA,frC
fmuls.
frD,frA,frC
fnabs
frD,frB
fnabs.
frD,frB
fneg
frD,frB
Floating-Point Negate
fneg.
frD,frB
Floating-Point Negate
fnmadd
frD,frA,frC,frB
fnmadd.
frD,frA,frC,frB
fnmadds
frD,frA,frC,frB
fnmadds.
frD,frA,frC,frB
fnmsub
frD,frA,frC,frB
fnmsub.
frD,frA,frC,frB
fnmsubs
frD,frA,frC,frB
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
fnmsubs.
frD,frA,frC,frB
fres
frD,frB
fres.
frD,frB
frsp
frD,frB
frsp.
frD,frB
frsqrte
frD,frB
frsqrte.
frD,frB
fsel
frD,frA,frC,frB
fsel.
frD,frA,frC,frB
fsub
frD,frA,frB
Floating-Point Subtract
fsub.
frD,frA,frB
Floating-Point Subtract
fsubs
frD,frA,frB
fsubs.
frD,frA,frB
icbi
rA,rB
isync
la
rD,d(rA)
Load Address
lbz
rD,d(rA)
lbzu
rD,d(rA)
lbzux
rD,rA,rB
lbzx
rD,rA,rB
lfd
frD,d(rA)
lfdu
frD,d(rA)
lfdux
frD,rA,rB
Instruction
Arguments
Description
lfdx
frD,rA,rB
lfs
frD,d(rA)
lfsu
frD,d(rA)
lfsux
frD,rA,rB
lfsx
frD,rA,rB
lha
rD,d(rA)
lhau
rD,d(rA)
lhaux
rD,rA,rB
lhax
rD,rA,rB
lhbrx
rD,rA,rB
lhz
rD,d(rA)
lhzu
rD,d(rA)
lhzux
rD,rA,rB
lhzx
rD,rA,rB
li
rD,SIMM
Load Immediate
lis
rD,SIMM
lmw
rD,d(rA)
lscbx
rD,rA,rB
lscbx.
rD,rA,rB
lswi
rD,rA,NB
lswx
rD,rA,rB
lwarx
rD,rA,rB
lwbrx
rD,rA,rB
lwz
rD,d(rA)
lwzu
rD,d(rA)
lwzux
rD,rA,rB
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
lwzx
rD,rA,rB
maskg
rA,rS,rB
maskg.
rA,rS,rB
maskir
rA,rS,rB
maskir.
rA,rS,rB
mcrf
crfD,crfS
mcrfs
crfD,crfS
mcrxr
crfD
mfcr
rD
mfctr
rD
mffs
frD
mffs.
frD
mflr
rD
mfmsr
rD
mfspr
rD,SPR
mfsr
rD,SR
mfsrin
rD,rB
mftb
rD
mftbu
rD
mfxer
rD
mr
rA,rS
Move Register
mr.
rA,rS
Move Register
mtcrf
CRM,rS
mtctr
rS
mtfsb0
crbD
mtfsb0.
crbD
mtfsb1
crbD
Instruction
Arguments
Description
mtfsb1.
crbD
mtfsf
FM,frB
mtfsf.
FM,frB
mtfsfi
crfD,IMM
mtfsfi.
crfD,IMM
mtlr
rS
mtmsr
rS
mtspr
SPR,rS
mtsr
SR,rS
mtsrin
rS,rB
mtxer
rS
Move to XER
mul
rD,rA,rB
Multiply [601]
mul.
rD,rA,rB
Multiply [601]
mulo
rD,rA,rB
Multiply [601]
mulo.
rD,rA,rB
Multiply [601]
mulhw
rD,rA,rB
mulhw.
rD,rA,rB
mulhwu
rD,rA,rB
mulhwu.
rD,rA,rB
mulli
rD,rA,SIMM
mullw
rD,rA,rB
mullw.
rD,rA,rB
mullwo
rD,rA,rB
mullwo.
rD,rA,rB
nabs
rD,rA
nabs.
rD,rA
nabso
rD,rA
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
nabso.
rD,rA
nand
rA,rS,rB
NAND
nand.
rA,rS,rB
NAND
neg
rD,rA
Negate
neg.
rD,rA
Negate
nego
rD,rA
Negate
nego.
rD,rA
Negate
No Operation
nop
nor
rA,rS,rB
NOR
nor.
rA,rS,rB
NOR
not
rA,rS
NOT
not.
rA,rS
NOT
or
rA,rS,rB
OR
or.
rA,rS,rB
OR
orc
rA,rS,rB
OR with Complement
orc.
rA,rS,rB
OR with Complement
ori
rA,rS,UIMM
OR Immediate
oris
rA,rS,UIMM
OR Immediate
Return from Interrupt
rfi
rlmi
rA,rS,rB,MB,ME
rlmi.
rA,rS,rB,MB,ME
rlwimi
rA,rS,SH,MB,ME
rlwimi.
rA,rS,SH,MB,ME
rlwinm
rA,rS,SH,MB,ME
rlwinm.
rA,rS,SH,MB,ME
Instruction
Arguments
Description
rlwnm
rA,rS,rB,MB,ME
rlwnm.
rA,rS,rB,MB,ME
rrib
rA,rS,rB
rrib.
rA,rS,rB
sc
sle
rA,rS,rB
sle.
rA,rS,rB
sleq
rA,rS,rB
sleq.
rA,rS,rB
sliq
rA,rS,SH
sliq.
rA,rS,SH
slliq
rA,rS,SH
slliq.
rA,rS,SH
sllq
rA,rS,rB
sllq.
rA,rS,rB
slq
rA,rS,rB
slq.
rA,rS,rB
slw
rA,rS,rB
slw.
rA,rS,rB
sraiq
rA,rS,SH
sraiq.
rA,rS,SH
sraq
rA,rS,rB
sraq.
rA,rS,rB
sraw
rA,rS,rB
sraw.
rA,rS,rB
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
srawi
rA,rS,SH
srawi.
rA,rS,SH
sre
rA,rS,rB
sre.
rA,rS,rB
srea
rA,rS,rB
srea.
rA,rS,rB
sreq
rA,rS,rB
sreq.
rA,rS,rB
sriq
rA,rS,SH
sriq.
rA,rS,SH
srliq
rA,rS,SH
srliq.
rA,rS,SH
srlq
rA,rS,rB
srlq.
rA,rS,rB
srq
rA,rS,rB
srq.
rA,rS,rB
srw
rA,rS,rB
srw.
rA,rS,rB
stb
rS,d(rA)
Store Byte
stbu
rS,d(rA)
stbux
rS,rA,rB
stbx
rS,rA,rB
stfd
frS,d(rA)
stfdu
frS,d(rA)
stfdux
frS,rA,rB
stfdx
frS,rA,rB
Instruction
Arguments
Description
stfiwx
frS,rA,rB
stfs
frS,d(rA)
stfsu
frS,d(rA)
stfsux
frS,rA,rB
stfsx
frS,rA,rB
sth
rS,d(rA)
Store Halfword
sthbrx
rS,rA,rB
sthu
rS,d(rA)
sthux
rS,rA,rB
sthx
rS,rA,rB
stmw
rS,d(rA)
stswi
rS,rA,NB
stswx
rS,rA,rB
stw
rS,d(rA)
Store Word
stwbrx
rS,rA,rB
stwcx.
rS,rA,rB
stwu
rS,d(rA)
stwux
rS,rA,rB
stwx
rS,rA,rB
sub
rD,rB,rA
Subtract
sub.
rD,rB,rA
Subtract
subo
rD,rB,rA
Subtract
subo.
rD,rB,rA
Subtract
subc
rD,rB,rA
Subtract Carrying
subc.
rD,rB,rA
Subtract Carrying
subco
rD,rB,rA
Subtract Carrying
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
subco.
rD,rB,rA
Subtract Carrying
subf
rD,rA,rB
Subtract From
subf.
rD,rA,rB
Subtract From
subfo
rD,rA,rB
Subtract From
subfo.
rD,rA,rB
Subtract From
subfc
rD,rA,rB
subfc.
rD,rA,rB
subfco
rD,rA,rB
subfco.
rD,rA,rB
subfe
rD,rA,rB
subfe.
rD,rA,rB
subfeo
rD,rA,rB
subfeo.
rD,rA,rB
subfic
rD,rA,SIMM
subfme
rD,rA
subfme.
rD,rA
subfmeo
rD,rA
subfmeo.
rD,rA
subfze
rD,rA
subfze.
rD,rA
subfzeo
rD,rA
subfzeo.
rD,rA
subi
rD,rA,SIMM
Subtract Immediate
subic
rD,rA,SIMM
subic.
rD,rA,SIMM
subis
rD,rA,SIMM
sync
Synchronize
Instruction
Arguments
Description
tlbie
rB
tlbld
rB
tlbli
rB
tlbsync
trap
Trap Unconditionally
tw
TO,rA,rB
Trap Word
tweq
rA,rB
tweqi
rA,SIMM
twge
rA,rB
twgei
rA,SIMM
twgt
rA,rB
twgti
rA,SIMM
twi
TO,rA,SIMM
twle
rA,rB
twlei
rA,SIMM
twlge
rA,rB
twlgei
rA,SIMM
twlgt
rA,rB
twlgti
rA,SIMM
twlle
rA,rB
twllei
rA,SIMM
twllt
rA,rB
twllti
rA,SIMM
twlng
rA,rB
twlngi
rA,SIMM
twlnl
rA,rB
PowerPC Assembler N o t es
PowerPC Assembler Instructions
Instruction
Arguments
Description
twlnli
rA,SIMM
twlt
rA,rB
twlti
rA,SIMM
twne
rA,rB
twnei
rA,SIMM
twng
rA,rB
twngi
rA,SIMM
twnl
rA,rB
twnli
rA,SIMM
xor
rA,rS,rB
XOR
xor.
rA,rS,rB
XOR
xori
rA,rS,UIMM
XOR Immediate
xoris
rA,rS,UIMM
XOR Immediate
6
MIPS Assembler
Notes
This chapter describes the MIPS assembler that is part of the
Metrowerks C/C++ compiler.
M I P S A s s e m ble r N o tes
Writing an Assembly Function
The built-in assembler supports all the standard MIPS assembler instructions. It accepts some additional directives described in Assembler Directive on page 166, as well as macros.
Keep these tips in mind as you write assembly functions:
All statements must either be a label, like this:
[LocalLabel:]
Or an instruction, like this:
( (instruction | directive) [operands] )
Each statement must end with a newline.
Hex constants must be in C-style , not Pascal-style. For example:
li
li
t0, 0xABCDEF
t0, $ABCDEF
// OK
// ERROR
Assembler directives, instructions, and registers are case-sensitive and must be in lowercase. For example these two statements are different:
add
ADD
t2,t3,t4
T2,T3,T4
// ok
// ERROR
// ERROR: No jr ra statement
M IPS Assembler N o t es
Writing an Assembly Function
// OK
t0,t1
// ERROR
t0,t0,t1 // OK
The rest of this section describes how to create local variables, access
function parameters, refer to fields within a structure, and use the
preprocessor with the assembler. A section at the end of the chapter
describes some special assembler directives that the built-in assembler allows.
Creating labels
A label can be any identifier that you havent already declared as a
local variable. A label must end with a colon. An instruction cannot
follow a label on the same line. For example:
x1:
x2:
add
t0,t1,t2
add
t0,t1,t2
// ERROR
// OK
// OK
Using comments
You cannot begin comments with a pound sign (#), since the preprocessor uses the pound sign. However, you can begin comments with
a semicolon (;) or use C and C++ comments. For example:
add
add
add
add
t3,t4,t5
t3,t4,t5
t3,t4,t5
t3,t4,t5
#
//
/*
;
ERROR
OK
OK */
OK
M I P S A s s e m ble r N o tes
Writing an Assembly Function
\
\
Specifying operands
This section describes how to specify the operands for assembly language instructions.
Using registers
For a register operand, you must use either:
The register number with a dollar sign ($) in front
$0, $1, $2, . . . $32
The software name
zero, v0, v1, a0a3, t0t9, k0, k1, gp, sp, fp, ra
$f0$f31
M IPS Assembler N o t es
Writing an Assembly Function
Using parameters
To refer to a parameter, you must use the hardware register its
passed in. For more information on parameter passing, see System V
Application Binary Interface, MIPS Processor Supplement .
For example:
asm int ADD (int x, int y)
{
// return x + y
.set
reorder
add
v0, a0, a1
jr
ra
}
Using global variables
You can refer to global variables by their names. For example:
int
POINT
Glob;
P;
M I P S A s s e m ble r N o tes
Assembler Directive
pressions. The in-line assembler carries out all arithmetic with 32-bit
signed integers.
An immediate operand can also be a reference to a member of a
struct or class type. You can use any struct or class name from a
typedef statement, followed by any number of member references.
This evaluates to the offset of the member from the start of the
struct. For example:
lw
t0, Rect.top(a0)
Assembler Directive
The MIPS assembler supports one directive.
.set
.set [ reorder | noreorder ]
If you use the reorder option, the assembler reorders machine language instructions to improve performance. By default, the assembler uses noreorder, and does not reorder instructions. For
example:
asm int ADD1 (void){
jr
ra
// return statement
addi v0,a0,1 // increment in the branch
//
delay slot
}
7
Win32/x86
Assembler Notes
This chapter describes the Win32/x86 assembler that is part of the
Metrowerks C/C++ compiler.
W i n 3 2 / x 8 6 A s s e mbler N o tes
Writing an Assembly Function
8
Pragmas and
Predefined Symbols
This chapter describes the pragmas and predefined symbols available with Metrowerks C/C++.
Pragmas
Metrowerks C and C++ let your source code change how the compiler compiles it with pragmas. Most of the pragmas correspond to
options in the Project Settings dialog. Usually, youll use the Preference dialog to set the options for most of your code and use pragmas to change the options for special cases. For example, with the
Project Settings dialog, you can turn off a time-consuming optimization and, with a pragma, turn it on only for the code it helps most.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Pragma Syntax
Most pragmas have this syntax:
#pragma option-name on | off | reset
Generally, use on or off to change the options setting, and then use
reset to restore the options original setting, as shown below:
#pragma profile off
/* If the option Generate Profiler Calls is ,
* on, turn it off for these functions.
*/
#include <smallfuncs.h>
#pragma profile reset
/* If the option Generate Profiler Calls was
* on, turn it back on.
* Otherwise, the option remains off
*/
Suppose that you use #pragma profile on instead of #pragma
profile reset. If you later turn off Generate Profiler Calls from
the Preference dialog, that pragma turns it on. Using reset ensures
that you dont inadvertently change the settings in the Project Settings dialog.
The Pragmas
The rest of this section is an alphabetical listing of all pragma options with descriptions.
The compiler
mac68k
mac68k4byte
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
If alignment is
The compiler
power
native
Aligns every field using the standard alignment. It is equivalent to using mac68k for
68K Macintosh computers and power for
Power Macintosh computers.
packed
reset
Listing 8.1
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
*/
*/
*/
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Before your program runs code optimized for the MC68020 , use the
gestalt() function to make sure the chip is available. For more information on gestalt(), see Chapter Gestalt Manager in Inside
Macintosh: Operating System Utilities.
In the Macintosh compiler, this option is off by default. In the Magic
Cap compiler, this option is on by default. If you change its setting,
be sure to change the setting of the pragma code68349 to the same
value.
This pragma corresponds to the 68020 Codegen option in the 68K
Processor settings panel. To check whether this option is on, use
__option (code68020), described in Options Checking on
page 229.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Before your program runs code optimized for the MC68881, use the
gestalt() function to make sure an FPU is available. For more in-
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
cpp_extensions
#pragma cpp_extensions on | off | reset
If this option is on, it enables these extensions to the ANSI C++ standard:
Anonymous structs. For example:
#pragma cpp_extensions on
void foo()
{
union {
long hilo;
struct { short hi, lo; };
// annonymous struct
};
hi=0x1234;
lo=0x5678; // hilo==0x12345678
}
Unqualified pointer to a member function. For example:
#pragma cpp_extensions on
struct Foo { void f(); }
void Foo::f()
{
void (Foo::*ptmf1)() = &Foo::f;
// ALWAYS OK
void (Foo::*ptmf2)() = f;
// OK, if cpp_exptensions is on.
}
This pragma does not correspond to any option in the settings
panel. To check whether this option is on, use the __option
(cpp_extensions), described in Options Checking on page
229. By default, this option is off.
d0_pointers (68K Macintosh only)
#pragma d0_pointers
This pragma lets you choose between two calling conventions: the
convention for MPW and Macintosh Toolbox routines and the con-
vention for Metrowerks C and C++ routines. In the MPW and Macintosh Toolbox calling convention, functions return pointers in the
register DO. In the Metrowerks C and C++ convention, functions return pointers in the register A0.
When you declare functions from the Macintosh Toolbox or a library compiled with MPW, turn on the d0_pointers pragma.
After you declare those functions, turn off the pragma to start declaring or defining Metrowerks C and C++ functions.
In Listing 8.2, the Toolbox functions in Sound.h return pointers in
D0 and the user-defined functions in Myheader.h use A0.
Listing 8.2
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
This pragma mimics a feature thats available in THINK C and Symantec C++. Use this pragma only if youre porting code that relies
on this feature, since it drastically increases your codes size and decreases its speed. In new code, declare a variable to be volatile if
you expect its value to persist across setjmp() calls.
This pragma does not correspond to any option in the settings
panel. To check whether this option is on, use the __option
(disable_registers), described in Options Checking on page
229. By default, this option is off.
dont_inline
#pragma dont_inline on | off | reset
If the pragma dont_inline is on, the compiler doesnt inline any
function calls, even functions declared with the inline keyword or
member functions defined within a class declaration. Also, it
doesnt automatically inline functions, regardless of the setting of
the auto_inline pragma, described in auto_inline on page 175.
If this option is off, the compiler expands all inline function calls.
This pragma corresponds to the Dont Inline option of the Inlining
menu the C/C++ Language settings panel. To check whether this
option is on, use __option (dont_inline), described in Options Checking on page 229.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
dont_reuse_strings
#pragma dont_reuse_strings on | off | reset
If the pragma dont_reuse_strings is on, the compiler stores
each string literal separately. If this pragma is off, the compiler
stores only one copy of identical string literals. This pragma helps
you save memory if your program contains lots of identical string
literals which you do not modify.
For example, take this code segment:
char *str1="Hello";
char *str2="Hello"
*str2 = 'Y';
If this option is on, str1 is "Hello" and str2 is "Yello". If this
option is off, both str1 and str2 are "Yello".
This pragma corresponds to the Dont Reuse Strings option in the
C/C++ Language settings panel. To check whether this option is on,
use __option (dont_reuse_strings), described in Options
Checking on page 229.
enumsalwaysints
#pragma enumsalwaysint on | off | reset
When pragma enumsalwaysint is on, the C or C++ compiler
makes an enumerated types the same size as an int. If an enumerated constant is larger than int, the compiler generates an error.
When the pragma is off, the compiler makes an enumerated type
the size of any integral type. It chooses the integral type with the
size that most closely matches the size of the largest enumerated
constant. The type could be as small as a char or as large as a long
int.
For example:
enum SmallNumber { One = 1, Two = 2 };
/* If enumsalwaysint is on, this type will
be the same size as a char.
If the pragma is off, this type will be
the same size as an int. */
enum BigNumber
{ ThreeThousandMillion = 3000000000 };
/* If enumsalwaysint is on, this type will
be the same size as a long int.
If this pragma is off, the compiler may
generate an error. */
For more information on how the compiler handles enumerated
types, see Enumerated types on page 28.
This pragma corresponds to the Enums Always Int option in the C/
C++ Language settings panel. To check whether this option is on,
use __option (enumsalwaysint), described in Options Checking on page 229.
exceptions (C++ only)
#pragma exceptions on | off | reset
If you turn on this pragma, you can use the try and catch statements to perform exception handling. If your program doesnt use
exception handling, turn this option to make your program smaller.
You can throw exceptions across any code thats compiled by the
CodeWarrior 8 (or later) Metrowerks C/C++ compiler with the Enable C++ Exceptions option turned on. You cannot throw exceptions across the following:
Macintosh Toolbox function calls
Libraries compiled with the Enable C++ Exceptions option
turned off
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
/* WARNING */
/* OK */
/* OK */
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Although the Magic Cap compiler does not raise an error if you use
this pragma, it ignores the pragmas value. In Magic Cap code, this
option is always on. In Macintosh code, this option is off by default.
This pragma does not correspond to any option in the settings
panel. To check whether this option is on, use the __option
(force_active), described in Options Checking on page 229.
fourbyteints (68K Macintosh only)
#pragma fourbyteints on | off | reset
When this option is on, the size of an int is 4 bytes. When this option is off, the size of an int is 2 byes.
WARNING! Do not turn this option off in Magic Cap code. Although the Magic Cap compiler lets you change the setting of this
option, your code will not run correctly if its off. It is on by default.
This pragma corresponds to the 4-Byte Ints option in the 68K Processor settings panel. To check whether this option is on, use
__option (fourbyteints), described in Options Checking on
page 229.
NOTE: Whenever possible, set this option from the settings
panel and not from a pragma. If you must set it from a pragma,
place the pragma at the beginning of your program, before you include any files or declare any functions or variables.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
If IEEEDoubles
is
and code68881
is
on
on or off
64 bits
off
off
80 bits
off
on
96 bits
WARNING! Do not turn this option on in Magic Cap code. Although the Magic Cap compiler lets you change the setting of this
option, your code will not run correctly if its on. It is off by default.
ignore_oldstyle
#pragma ignore_oldstyle on | off | reset
If pragma ignore_oldstyle is on, the compiler ignores old-style
function declarations and lets you prototype a function any way
you want. In old-style declarations, you dont specify the types of
the arguments in the argument list but on separate lines. Its the
style of declaration used in the first edition of The C Programming
Language (Prentice Hall) by Kernighan and Ritchie.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
#pragma macsbug on
#pragma macsbug on
#pragma oldstyle_symbols on
#pragma macsbug on
#pragma oldstyle_symbols off
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
This pragma corresponds to the MPW C Calling Convention option in the 68K Processor settings panel. To check whether this option is on, use __option (mpwc), described in Options Checking
on page 229.
mpwc_newline
#pragma mpwc_newline on | off | reset
If you turn on the pragma mpwc_newline, the compiler uses the
MPW conventions for the '\n' and '\r' characters. If this pragma
is off, the compiler uses the Metrowerks C and C++ conventions for
these characters.
In MPW, '\n' is a Carriage Return (0x0D) and '\r' is a Line Feed
(0x0A). In Metrowerks C and C++, theyre reversed: '\n' is a Line
Feed and '\r' is a Carriage Return.
If you want to turn this pragma on, be sure you use the ANSI C and
C++ libraries that were compiled with this option on. The 68K versions of these libraries are marked with an N; for example, ANSI
(N/2i) C.68K.Lib. The PowerPC versions of these libraries are
marked with NL; for example, ANSI (NL) C.PPC.Lib.
If you turn this pragma on and use the standard ANSI C and C++ libraries, you wont be able to read and write '\n' and '\r' properly. For example, printing '\n' brings you to the beginning of the
current line instead of inserting a new line.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
WARNING! Do not turn this option off in Magic Cap code. Although the Magic Cap compiler lets you change the setting of this
option, your code will not run correctly if its off. It is on by default.
MyFunc(i); }
OurFunc(j); }
However, if a line like the one below appears anywhere in the function, the compiler would realize that youre using i and j at the
same time and place them in different registers:
int k = i + j;
If register coloring is on while you debug your project, it may appear as though theres something wrong with the variables sharing
a register. In the example above, i and j would always have the
same value. When i changes, j changes in the same way. When j
changes, i changes in the same way. To avoid this confusion while
debugging, turn off register coloring or declare the variables you
want to watch as volatile.
The pragma corresponds to the Global Register Allocation option
in the 68K Processor settings panel. To check whether this option is
on, use __option (no_register_coloring), described in Options Checking on page 229. By default, this option is off.
NOTE: To turn off register coloring in code for a PowerPC Macintosh, use the statement #pragma global_optimizer off. For
more information, see global_optimizer, optimization_level (PowerPC Macintosh only) on page 192.
once
#pragma once [ on | off ]
Use this pragma to ensure that the compiler includes header files
only once in a source file. This pragma is especially useful in precompiled header files.
There are two versions of this pragma: #pragma once and
#pragma once on. Use #pragma once in a header file to ensure
that the header file is included only once in a source file. Use
#pragma once on in a header file or source file to insure that any
file is included only once in a source file.
This pragma does not correspond to an option in any settings panel.
By default this option is off.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Does this
#pragma pack(n)
#pragma pack(push, n)
#pragma pack(pop)
#pragma pack()
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
When you define the function, you need to specify the registers
right in the parameter list, as described in Specifying the registers
for arguments (68K Macintosh Only) on page 45.
This pragma does not correspond to an option in any settings panel.
pcrelstrings (68K Macintosh only)
#pragma pcrelstrings on | off | reset
If this option is on, the compiler stores the string constants used in a
local scope in the code segment and addresses these strings with
PC-relative instructions. If this option is off, the compiler stores all
string constants in the global data segment. Regardless of how this
option is set, the compiler stores string constants used in the global
scope in the global data segment. For example:
#pragma pcrelstrings on
int foo(char *);
int x = f("Hello");
// "Hello" is allocated in
// the global data segment
int bar()
{
return f("World"); //"World" is allocated in
}
// the code segment
// (pc-relative)
Strings in C++ initialization code are always allocated in the global
data segment.
NOTE: If you turn the pool_strings pragma on, the compiler
ignores the setting of the pcrelstrings pragma.
WARNING! Do not turn this option off in Magic Cap code. Although the Magic Cap compiler lets you change the setting of this
option, your code will not run correctly if its off. It is on by default.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
your programs size, since it uses a less efficient method to store the
strings address.
This pragma is especially useful if your program is large and has
many string constants or uses the Metrowerks Profiler.
NOTE: If you turn the pool_strings pragma on, the compiler
ignores the setting of the pcrelstrings pragma.
These pragmas are available so you can use MacApp with Metrowerks C and C++. If youre writing new code and need to set a
pragma option to its original value, use the reset argument, described in Pragma Syntax on page 170.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
precompile_target
#pragma precompile_target filename
This pragma specifies the filename for a precompiled header file. If
you dont specify the filename , the compiler gives the precompiled
header file the same name as its source file.
Filename can be a simple filename or an absolute pathname. If filename is a simple filename, the compiler saves the file in the same
folder as the source file. If filename is a path name, the compiler
saves the file in the specified folder.
Listing 8.5 shows sample source code from the MacHeaders precompiled header source file. By using the predefined symbols
__cplusplus and powerc and the pragma precompile_target,
the compiler can use the same source code to create different precompiled header files for C and C++, 680x0 and PowerPC.
Listing 8.5
"MacHeadersPPC++"
"MacHeaders68K++"
"MacHeadersPPC"
"MacHeaders68K"
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
This pragma corresponds to the Require Function Prototypes option in the C/C++ Language settings panel. To check whether this
option is on, use __option (require_prototypes), described
in Options Checking on page 229.
RTTI
#pragma RTTI on | off | reset
When the pragma RTTI is on, you can use Run-Time Type Information (or RTTI) features, such as dyanamic_cast and typeid. The
other RTTI expressions are available even if the Enable RTTI option
is off. Note that *type_info::before(const type_info&) is
not yet implemented.
This pragma corresponds to the Enable RTTI option in the C/C++
Language settings panel. To check whether this option is on, use
__option (RTTI), described in Options Checking on page 229.
scheduling (PowerPC Macintosh only)
#pragma scheduling 601 | 603 | 604 |
on | off | reset
This pragma lets you choose how the compiler rearranges instructions to increase speed. Some instructions, such as a memory load,
take more than one processor cycle. By moving an unrelated instruction between the load and the instruction that uses the loaded item,
the compiler saves a cycle when executing the program.
CodeWarrior lets you choose the type of scheduling that works best
for each PowerPC chip. You can use 601, 603, or 604. If you use on,
the compiler performs 601 scheduling.
However, if youre debugging your code, turn this pragma off. Since
it rearranges the instructions produced from your code, the debugger will not be able to match the statements in your source code to
the produced instructions.
This pragma corresponds to the Instruction Scheduling option in
the PPC Processor settings panel.
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
// If *p is an alias for a,
// this changes a.
ject classes use OIDL, so if you override a method from one of them,
you should not include the Environment pointer.
This pragma is ignored if the direct_to_SOM pragma, described in
Creating Direct-to-SOM Code on page 105, is off.
This pragma does not correspond to an option in any settings panel.
To check whether this option is on, use __option (SOMCheckEnvironment). See Options Checking on page 229. By default, this
pragma is set to IDL.
SOMCheckEnvironment (Macintosh and C++ only)
#pragma SOMCheckEnvironment on | off | reset
When the pragma SOMCheckEnvironment is on, the compiler performs automatic SOM environment checking. It transforms every
IDL method call and new allocation into an expression which also
calls an error-checking function. You must define separate errorchecking functions for method calls and allocations. For more information on how to write these functions, see Automatic SOM error
checking on page 109.
For example, the compiler transforms this IDL method call:
SOMobj->func(&env, arg1, arg2) ;
into something that is equivalent to this:
( temp=SOMobj->func(&env, arg1, arg2),
__som_check_ev(&env), temp ) ;
First, the compiler calls the method and stores the result in a temporary variable. Then it checks the environment pointer. Finally, it returns the methods result.
And, the compiler transforms this new allocation:
new SOMclass;
into something that is equivalent to this:
( temp=new SOMclass, __som_check_new(temp),
temp);
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
// WARNING
// OK
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
// WARNING:
// Hides A::f(int)
virtual void g(int); // OK:
// Overrides A::g(int)
};
// WARNING: a=b is an
//
assignment
Pr ag ma s a nd P re d efi n e d S ymbols
Pragmas
if (a==b) f();
// OK: (a==b) is a
//
comparison
An equal comparison in a statement that contains a single expression. This check is useful if you frequently use == when
you meant to use =. For example:
a == 0;
a = 0;
// ERROR: error is
//
undefined
// WARNING: temp and error are unused.
Pr ag ma s a nd P re d efi n e d S ymbols
Predefined Symbols
Predefined Symbols
Metrowerks C and C++ define several preprocessor symbols that
give you information about the compile-time environment. Note
that these symbols are evaluated at compile time and not at run
time.
ANSI Predefined Symbols
The table below lists the symbols that the ANSI C standard requires.
Table 8.1
is
__DATE__
The date at which the file is compiled; for example, "Jul 14, 1995".
__FILE__
__LINE__
__TIME__
The time at which the file is compiled in 24hour format; for example, "13:01:45".
__STDC__
Listing 8.6 shows a small program that uses the ANSI predefined
symbols.
Listing 8.6
Pr ag ma s a nd P re d efi n e d S ymbols
Predefined Symbols
is
__A5__
(68K only.)
__cplusplus
__fourbyteints__
(68K only.)
__IEEEdoubles__
(68K only.)
1, if you turn on the 8-Byte Doubles option in the Processor settings panel. 0, if
you turn off that option. Its undefined in
the PowerPC compiler.
__INTEL__
__MC68K__
__MC68020__
(68K only.)
1, if you turn on the 68020 Codegen option in the Processor settings panel. 0, if
you turn that option off. Its undefined in
the PowerPC compiler.
__MC68881__
(68K only.)
1, if you turn on the 68881 Codegen option in the Processor settings panel. 0, if
you turn that option off. Its undefined in
the PowerPC compiler.
__MWBROWSER__
This macro
is
__MWERKS__
__profile__
__powerc,
powerc,
__POWERPC__
macintosh
Options Checking
The preprocessor function __option() lets you test the setting of
many pragmas and options in the Project Settings dialog. Its syntax
is:
__option(option-name)
If the option is on , __option () returns 1; otherwise, it returns 0.
This function is useful when you want one source file to contains
code thats used for different option settings. The example below
shows how to compile one series of lines if youre compiling for machines with the MC68881 floating-point unit and another series if
youre compiling for machines without out:
#if __option (code68881)
// Code optimized for the floating point unit.
#else
// Code for any Macintosh
#endif
Pr ag ma s a nd P re d efi n e d S ymbols
Options Checking
Options table
The table below lists all the option names.
This argument
Corresponds to the
a6frames
(68K only)
ANSI Strict option in the C/C++ Language settings panel and pragma
ANSI_strict.
ARM_conform
auto_inline
bool
check_header_flags
Pragma check_header_flags.
code68020
(68K only)
68020 Codegen option in the 68K Processor settings panel and pragma
code68020.
code68881
(68K only)
68881 Codegen option in the 68K Processor settings panel and pragma
code68881.
code68349
(68K only)
Pragma code68349
cplusplus
This argument
Corresponds to the
cpp_extensions
Pragma cpp_extensions
d0_pointers
(68K only)
direct_destruction
direct_to_SOM
disable_registers
(PowerPC only)
Pragma disable_registers.
dont_inline
dont_reuse_strings
enumsalwaysint
exceptions
export
Pragma export.
far_strings
(68K only)
Pr ag ma s a nd P re d efi n e d S ymbols
Options Checking
This argument
Corresponds to the
far_vtables
(68K only)
Far Method Tables in the 68K Processor settings panel and pragma
far_vtables.
force_active
(68K only)
Pragma force_active.
fourbyteints
(68K only)
4-Byte Ints option in the 68K Processor settings panel and pragma fourbyteints.
fp_contract
(PowerPC only)
global_optimizer
(PowerPC only)
IEEEdoubles
(68K only)
8-Byte Doubles option in the 68K Processor settings panel and pragma
IEEEdoubles.
ignore_oldstyle
Pragma ignore_oldstyle.
import
Pragma import.
internal
Pragma internal.
lib_export
Pragma lib_export.
linksym
little_endian
longlong
Pragma longlong.
longlong_enums
Pragma longlong_enums.
This argument
Corresponds to the
macsbug
(68K only)
mpwc
(68K only)
mpwc_newline
mpwc_relax
only_std_keywords
optimize_for_size
pcrelstrings
(68K only)
peephole
Pr ag ma s a nd P re d efi n e d S ymbols
Options Checking
This argument
Corresponds to the
pool_strings
precompile
preprocess
profile
readonly_strings
(PowerPC only)
require_prototypes
RTTI
side_effects
Pragma side_effects.
Pragma static_inlines
sym
This argument
Corresponds to the
toc_data
traceback
(PowerPC only)
Pragma traceback.
trigraphs
unsigned_char
warn_emptydecl
warn_extracomma
warn_hidevirtual
warn_illpragma
warn_possunwant
warn_unusedarg
Pr ag ma s a nd P re d efi n e d S ymbols
Options Checking
This argument
Corresponds to the
warn_unusedvar
warning_errors
Index
Symbols
#, and macros 48
#else 48
#endif 48
#pragma statements 170
illegal 60
* 63
=
accidental 61
operator 89
?: conditional operator 87, 94
\n 76
\p 78
\r 76
__A5__ 228
__abs() 81
__cntlzw() 81
__cplusplus 228
__DATE__ 226
__declspec 27
__eieio() 80
__fabs() 81
__FILE__ 226
__fnabs() 81
__fourbyteints__ 228
__fres() 82
__fsel() 82
__fsqrte() 82
__ieeedoubles__ 228
__INTEL__ 228
__isync() 80
__labs() 81
__lhbrx() 81
__LINE__ 226
__lwbrx() 81
__MC68020__ 228
__MC68881__ 228
__MC68K__ 228
__MWBROWSER__ 228
__MWERKS__ 229
__option(), preprocessor function 229
__powerc 229
__POWERPC__ 229
__PreInit__() 90
__profile__ 229
__rlwimi() 83
__rlwinm() 83
__rlwnm() 83
__setflm() 82
__som_check_ev() 110
__som_check_new() 110
__STDC__ 226
__stdcall 51
__sthbrx() 81
__stwbrx() 81
__sync() 80
__TIME__ 226
Numerics
4-Byte Int option 30
__MC68020__ 228
68020 Codegen 70
__MC68881__ 228
68881 Codegen option 32, 71
68K assembly 115
8-Byte Doubles option 32
A
__A5__ 228
a6frames pragma 171
__abs() 81
Access Paths preference panel 22
Activate C++ Compiler option 93
address
specifying for variable 43
align pragma 171
align_array_members pragma 172
anonymous structs 95
ANSI Keywords Only option 50
ANSI Strict option 28, 47
ANSI_strict pragma 47, 173
arguments
default 88
passing in registers 45
Index
unnamed 48
unused 63
VAR 77
ARM Conformance option 94
ARM_conform 94, 95
ARM_conform pragma 174
arrays
size of 26
asm instruction 167
asm keyword 50, 115, 125, 161, 167
asm<Default Para Text> statement 44
Assembler 167
assembler, inline 115, 125, 161, 167
assembly instructions 138
assembly, 68K 115
assembly, PowerPC 125
assembly, 68K 115
assignment, accidental 61
Auto-Inline option 53
auto_inline pragma 53, 175
Cell 77
char 52
char size 31, 33
characters, multi-byte 43
check_header_flags pragma 176
CIncludes 22
class declaration, local 89
__cntlzw() 81
code_seg pragma 177
code68020 pragma 177
code68349 pragma 178
code68881 pragma 178
commas, extra 64
comments, C++-styles 48
conditional operator 87, 94
const_cast keyword 87
copy constructor 89
__cplusplus 228
cplusplus pragma 93, 179
cpp_extensions pragma 95, 180
C
c2pstr() 78
calling conventions 35
MPW 75
registers 45
carriage return 76
catch statement 86, 96, 103, 185
In d ex
dynamic_cast 97
dynamic_cast keyword 87
E
__eieio() 80
8-Byte Doubles option 32
#else 48
empty declarations 61
Empty Declarations option 61
Enable Exception Handling option 96
#endif 48
entry assembly statement 123, 134
Enum Always Int option 28
enumerated type 65
size of 51
enumerated types 28
Enums Always Int option 51
enumsalwaysint pragma 52, 184
=
accidental 61
operator 89
errors
and warnings 60
exception handling 96, 103
exceptions pragma 185
.exp file 195
Expand Trigraphs option 49
explicit keyword 87
export pragma 186
Export Symbols option 195
extb.l assembly statement 117
Extended Error Checking option 65
extended type 71
extended_errorchecking pragma 66, 187
extended80 71
Extra Commas option 64
F
__fabs() 81
false keyword 87
Far Data option 26
far keyword 26, 50
far_code pragma 189
far_data pragma 26, 189
G
Global Register Allocation option 24, 233
global_optimizer pragma 192
H
HandleObject 104
header files 21
header files, for templates 100
I
identifiers 21
IEEE floating-point standards 71
__ieeedoubles__ 228
IEEEdoubles pragma 193
if statement 62, 94
ignore_oldstyle pragma 194
Illegal Pragmas option 60
import pragma 195
include files, see header files
infinite loop 62
Index
K
keywords, additional 50, 87
L
__labs() 81
Language preference panel 40
__lhbrx() 81
lib_export pragma 198
__LINE__ 226
local class declaration 89
long double size 32, 34
long long 46
long long size 31, 33
long size 31, 33
longlong 198
longlong pragma 46
longlong_enums pragma 199
__lwbrx() 81
M
machine assembly statement 116, 136
Macintosh Toolbox functions 77
macros
and # 48
and inline assembler 121, 129, 164
macsbug pragma 199
Magic Cap
calling conventions 39
number formats 33, 34
mangled names 21
Map Newlines to CR option 76
__MC68020__ 228
MC68020 processor 70
__MC68881__ 228
MC68881 floating-point unit 70
__MC68K__ 228
member function pointer 95
MPW C Calling Convention option 75
MPW compatibility 72, 104
mpwc pragma 76, 200
mpwc_newline pragma 77, 201
mpwc_relax pragma 56, 202
muls.l assembly statement 117
multi-byte characters 43
mulu.l assembly statement 117
mutable keyword 87
__MWBROWSER__ 228
__MWERKS__ 229
N
\n 76
namespace keyword 87
near_code pragma 189
newline 76
number formats 30
O
oldstyle_symbols pragma 199
once pragma 203
only_std_keywords pragma 50, 204
Opcode inline functions 43
OpenDoc 105
operator delete 86
operator new 86
operator= 89
optimization_level pragma 192
optimize_for_size pragma 204
__option(), preprocessor function 229
options align= pragma 171
opword assembly statement 124
OSType 77
In d ex
P
\p 78
p2cstr() 78
pack pragma 205
parameter pragma 45, 205
parameters, see arguments
pascal keyword 50
and PowerPC 79
Pascal strings 78
pcrelstrings pragma 58, 206
peephole pragma 207
Point 77
pointer to member function 95
pointer types 56
pointers_in_A0 pragma 207
pointers_in_D0 pragma 207
Pool Strings option 56
pool_strings pragma 57, 208
pop pragma 209
Possible Errors option 61
__powerc 229
__POWERPC__ 229
PowerPC assembly 125
PowerPC intrinsic functions 80
#pragma statements 170
illegal 60
precompile_target pragma 210
__PreInit__() 90
preprocessor
and # 48
and inline assembler 121, 129, 164
__profile__ 229
profile pragma 211
protected base classes 94
prototypes 54
requiring 54
push pragma 209
R
\r 76
readonly_strings pragma 211
Rect 77
registers
coloring 24
floating-point 72
passing arugments in 45
variables 23
reinterpret_char keyword 87
Relaxed Pointer Type Rules option 54, 56
Require Prototypes option 54
require_prototypes pragma 56, 211
ResType 77
return statement
empty 66
missing 65
return, carriage 76
__rlwimi() 83
__rlwinm() 83
__rlwnm() 83
rtd assembly statement 117
RTTI 96, 212
RTTI option 96
RTTI pragma 212
Run-time type information 96, 212
S
SANE.h 71
scheduling pragma 212
segment pragma 213
__setflm() 82
short double size 32, 34
short size 31, 33
side_effects pragma 213
signed char size 31, 33
simple class 89
SingleObject 104
68020 Codegen 70
68881 Codegen option 32, 71
68K assembly 115
size
of data structures 26
of enumerated types 51
of numbers 30
size_t 22
sizeof() operator 22
smart_code pragma 189
smclass assembly statement 137
SOM 105, 113
Index
T
template class statement 102
templates 99
instantiating 101
__TIME__ 226
toc_data pragma 219
Toolbox functions 77
traceback pragma 219
Treat All Warnings as Errors option 60
trigraph characters 49
trigraphs pragma 49, 219
true keyword 87
try statement 86, 96, 103, 185
type-checking 56
type_info 99
typeid 98
typeid keyword 87
typeid keyword 212
Types.h 71
U
unnamed arguments 48
unsigned char 52
unsigned char size 31, 33
unsigned int size 31, 33
unsigned long long size 31, 33
unsigned long size 31, 33
unsigned short size 31, 33
unsigned_char pragma 220
Unused Arguments option 63
unused pragma 63, 220
Unused Variables option 62
Use Unsigned Chars option 41, 52
using keyword 87
V
VAR arguments 77
variables
declaring by address 43
register 23
unused 62
volatile 25
virtual keyword 87
volatile variables 25
W
warn_emptydecl pragma 61, 221
warn_extracomma pragma 64, 222
warn_hidevirtual pragma 222
warn_illpragma pragma 61, 223
warn_possunwant pragma 62, 223
warn_unusedarg pragma 64, 224
warn_unusedvar pragma 63, 225
warning pragma 225
warning_errors pragma 60, 221
warnings 59
as errors 60
wchar_t keyword 87
while statement 62, 94
Win32/x86
In d ex
keywords 51
number formats 33, 34, 39
registers 23
X
x80tox96() 71
x96tox80() 71
Index
CodeWarrior
C, C++, and Assembly Language
Reference
Credits
engineering: Andreas Hommel,
John McEnerney, Jason Eckhardt
writing: Jeff Mattson
frontline warriors: Fred Peterson
See this
QuickStart Guide
QuickStart Guide;
Tutorials (Apple Guide)
IDE Users Guide
Debugger Manual
Release Notes folder
Targeting Mac OS;
Mac OS folder
Targeting Win32;
Win32/x86 folder
Targeting Java
Sun Java Documentation folder
Targeting Magic Cap;
Magic Cap folder
IDE Users Guide
IDE Users Guide
Command Line Tools Manual
C, C++, and Assembly Reference;
MSL C Reference;
MSL C++ Reference
Pascal Language Manual;
Pascal Library Reference
Errors Reference
ZoneRanger Manual
Profiler Manual
The PowerPlant Book;
PowerPlant Advanced Topics;
PowerPlant reference documents
Constructor Manual
Constructor for Java Manual
Discover Programming for Macintosh
Discover Programming for Java
Quick Start Guide
email Report Forms in the Release Notes
folder
CodeWarrior Examples folder;
The PowerPlant Book;
PowerPlant Advanced Topics;
Tutorials (Apple Guide)
Internet newsgroup [docs] folder