• Keine Ergebnisse gefunden

Define Instruction (#define)

Chapter 8 ANSI Library

9.5 Preprocessor Pseudo-Instructions

9.5.2 Define Instruction (#define)

Any substitute character string can be left defined as a Define name by the define instruction (#define), and the details of that definition can be referred to from various parts of the program using the Define name.

Instruction format

#define <Define name> [<Substitute character string>]

<Define name>:

• The first character is limited to a–z, A–Z and _.

• The second and the subsequent characters can use a–z, A–Z, 0–9 and _.

• Uppercase and lowercase characters are discriminated.

• One or more spaces or tab settings are necessary between the instruction and the Define name.

<Substitute character string>:

• The usable characters are limited to a–z, A–Z, 0–9, _, % and . (period). They must not contain any space or comma (,).

Values, operators, mnemonics, labels, and register names also can be specified.

• Uppercase and lowercase characters are discriminated.

• One or more spaces or tab settings are necessary between the Define name and the substitute character string.

• The substitute character string can be omitted. In that case, NULL is defined in lieu of the substitute character string. It can be used for the conditional assembly instruction.

Sample definitions:

#define TYPE1

#define L1 LABEL_01

#define li ld.w

#define r1 %r1

#define Mr1 [%r1] ...Error [ ] and [ ]+ cannot be used.

Expansion rule

If a Define name defined appears in the source, the pp33 substitutes a defined character string for that Define name.

Sample expansion:

#define gp %r8

:

ld.w [gp], %r1 ...Expanded to "ld.w [%r8], %r1".

When a number is specified for the substitute character string, the following rule is applied:

• The pp33 converts the defined number into a signed 32-bit data and expands it as a hexadecimal number.

• #define allows the substitute character strings to describe in an expression using operators. The Define names that have been defined can also be used as terms of the expression.

Sample expansion:

Before expansion

#define A 0x12

#define B A*2

:

ld.w %r2, A+B ...Expanded to "ld.w %r2, 0x36".

CHAPTER 9: PREPROCESSOR

E0C33 FAMILY EPSON 113

Precautions

• The pp33 only permits back reference of a Define name. The definition needs to have been completed before making the reference.

• Once a Define name is defined, it cannot be canceled. However, redefinition can be made using a Define name.

Example: #define XH %ahr

#define XHigh XH

ld.w XHigh, %r1 ...Expanded to "ld.w %ahr, %r1".

• When the same Define name is defined twice or more, a warning message will appear and the redefined character string is validated.

• No other characters than delimiters (space, tab, line feed, and comma) can be added before and after a Define name in the source, unless they are enclosed in [ ] or [ ]+. However, a symbol mask (@..) described behind is valid.

Examples: #define H %ah

ld.w Hr, %r1 ;Hr = %ahr? ...Specification like this is invalid.

#define L LABEL

ext L@h ...Replaced with "ext LABEL@h".

• The pp33 does not check the validity of a statement following the replacement of the character string.

• The pp33 handles the defined numbers as 32-bit data. If the specified number or the calculation result is a negative value, it is delivered as a decimal number with a minus sign. If the value is positive, it is delivered as a hexadecimal number. Pay attention to the immediate data size, especially when it has a minus value.

Example: #define NUM -1 ...-1 is handled as 0xffffffff.

ld.w %r1, NUM ...It will be expanded as "ld.w %r1, -1".

ld.w %r1, NUM^L ...It will be expanded as "ld.w %r1, 0x3f".

CHAPTER 9: PREPROCESSOR

9.5.3 Macro Instructions (#macro ... #endm)

Any statement string can be left defined as a macro using the macro instruction (#macro), and the content of that definition can be invoked from different parts of the program with the macro name. Unlike a subroutine, the part that is invoking a macro is replaced with the content of the definition by the pp33.

Instruction format

#macro <Macro name> [$1] [,$2] . . . [,$32]

<Statement string>

#endm <Macro name>:

• The first character is limited to a–z, A–Z and _.

• The second and the subsequent characters can use a–z, A–Z, 0–9, and _.

• Uppercase and lowercase characters are discriminated.

• One or more spaces or tab settings are necessary between the instruction and the macro name.

$1–$32:

• Dummy parameter symbols for macro definition. They are described when a macro to be defined needs parameters. Not more than 32 symbols can be specified.

• No other symbols than $1 to $32 can be used. You need to begin always with $1 and to arrange them in an ascending order ($1 → $32).

• One or more spaces or tab settings are necessary between the macro name and $1. When describing multiple parameters, a comma (,) is necessary between one parameter and another.

<Statement string>:

• The following statements can be described:

- Basic instruction (mnemonic and operand) - Extended instruction (see Instruction Extender) - Conditional assembly instruction

- Internal branch label*

- Comments

• The following statements cannot be described:

- Preprocessor pseudo-instructions (excluding conditional assembly instruction) - Assembler pseudo-instructions

- Other labels than internal branch labels - Macro invocation

* Internal branch label

A macro is spread over to several locations in the source. Therefore, if you describe a label in a macro, a double definition will result, with an error issued. So, use internal branch labels which are only valid within a macro.

• A maximum of 64 internal branch labels can be described per macro.

• The labels should be arranged like this: $$1–$$64 in order of description. (Each macro should begin with $$1.)

Sample definition:

#define Areg %r1

#macro ADD $1, $2

ld.w Areg, $1

add Areg, $2

#ifdef DEBUG

cmp Areg, 0x1

#else

cmp Areg, 0x2

#endif

xjrne $$1

CHAPTER 9: PREPROCESSOR

E0C33 FAMILY EPSON 115

ld.w [%r2], 0b11

$$1:

ld.w %r3, [%r2]+

jr LABEL1

#endm Expansion rules

When a defined macro name appears in the source, the pp33 inserts a statement string defined in that location.

If there are actual parameters described in that process, the dummy parameters ($1–$32) will be replaced with the actual parameters in the same order as the latter are arranged.

The internal branch labels are replaced, respectively, with __L0001–__L9999 from top of the source in the same order as they appear.

Sample expansion:

When the macro ADD shown above is defined:

Macro invocation

#define DEBUG

:

ADD 1, 2

:

After expansion

:

ld.w Areg, $1 ; ADD 1, 2

add Areg, $2

;#ifdef DEBUG

cmp Areg, 0x1

;#else

; cmp Areg, 0x2

;#endif

xjrne __L0001

ld.w [%r2], 0b11

_ _ L0001:

ld.w %r3, [%r2]+

jr LABEL1

("_ _ L0001" denotes the case where an internal branch label is expanded for the first time in the source.) Precautions

• The pp33 only permits back reference of a macro invocation. The definition needs to have been completed before making the reference.

• Once a defined macro name is defined, it cannot be canceled. If the same macro name is defined twice or more, a warning message will appear and the redefined macro is validated.

• No other characters than delimiters (space, tab, line feed, and commas) can be added before and after a dummy parameter in a statement, unless they are enclosed in [ ] or [ ]+. However, a symbol mask (@..) described behind is valid.

• The same character string as that of the #define and #define instruction cannot be used as a macro name.

• When the number of dummy parameters differs from that of actual parameters, an error will result.

• A maximum of 32 parameters and a maximum of 64 internal branch labels can be specified per macro. If these limits are surpassed, an error will result.

• "_ _ L####" used for the internal branch labels should not be employed as other label or symbol.

• Maximum 9999 internal branch labels can be expanded within one source file. If this limit is exceeded, an error will result.

CHAPTER 9: PREPROCESSOR