Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
50
Добавлен:
14.04.2015
Размер:
1.37 Mб
Скачать

Keil Software — Cx51 Compiler User’s Guide

129

 

 

Reentrant Functions

A reentrant function can be shared by several processes at the same time. When a reentrant function is executing, another process can interrupt the execution and then begin to execute that same reentrant function. Normally, functions in the Cx51 compiler cannot be called recursively or in a fashion which causes reentrancy. The reason for this limitation is that function arguments and local variables are stored in fixed memory locations. The reentrant function attribute allows you to declare functions that may be reentrant and, therefore, may be called recursively. For example:

 

 

 

 

int calc (char i, int b) reentrant

{

 

3

}

 

 

int x;

 

 

 

x = table [i];

 

 

 

return (x * b);

 

 

 

 

 

 

 

 

 

 

 

Reentrant functions can be called recursively and can be called simultaneously by two or more processes. Reentrant functions are often required in real-time applications or in situations where interrupt code and non-interrupt code must share a function.

As in the above example, you may selectively define (using the reentrant attribute) functions as being reentrant. For each reentrant function, a reentrant stack area is simulated in internal or external memory depending upon the memory model used, as follows:

Small model reentrant functions simulate the reentrant stack in idata memory.

Compact model reentrant functions simulate the reentrant stack in pdata memory.

Large model reentrant functions simulate the reentrant stack in xdata memory.

 

130

Chapter 3. Language Extensions

 

 

 

Reentrant functions use the default memory model to determine which memory

 

space to use for the reentrant stack. You may specify (with the small, compact,

 

and large function attributes) which memory model to use for a function. Refer

 

to “Specifying the Memory Model for a Function” on page 121 for more

 

information about memory models and function declarations.

 

The following rules apply to functions declared with the reentrant attribute.

 

bit type function arguments may not be used. Local bit scalars are also not

 

 

available. The reentrant capability does not support bit-addressable variables.

3

Reentrant functions must not be called from alien functions.

 

Reentrant function cannot use the alien attribute specifier to enable PL/M-51

 

argument passing conventions.

A reentrant function may simultaneously have other attributes like using and interrupt and may include an explicit memory model attribute (small, compact, large).

Return addresses are stored in the 8051 hardware stack. Any other required PUSH and POP operations also affect the 8051 hardware stack.

Reentrant functions using different memory models may be intermixed. However, each reentrant function must be properly prototyped and must include its memory model attribute in the prototype. This is necessary for calling routines to place the function arguments in the proper reentrant stack.

Each of the three possible reentrant models contains its own reentrant stack area and stack pointer. For example, if small and large reentrant functions are declared in a module, both small and large reentrant stacks are created along with two associated stack pointers (one for small and one for large).

The reentrant stack simulation architecture is inefficient, but necessary due to a lack of suitable addressing methods available on the 8051. For this reason, use reentrant functions sparingly.

The simulated stack used by reentrant functions has its own stack pointer which is independent of the 8051 stack and stack pointer. The stack and stack pointer are defined and initialized in the STARTUP.A51 file.

Keil Software — Cx51 Compiler User’s Guide

131

 

 

The following table details the stack pointer assembler variable name, data area, and size for each of the three memory models.

Model

Stack Pointer

Stack Area

SMALL

?C_IBP (1 Byte)

Indirectly accessible internal memory (idata).

 

 

256 bytes maximum stack area.

COMPACT

?C_PBP (1 Byte)

Page-addressable external memory (pdata).

 

 

256 bytes maximum stack area.

LARGE

?C_XBP (2 Bytes)

Externally accessible memory (xdata). 64 KBytes

 

 

maximum stack area.

The simulated stack area for reentrant functions is organized from top to bottom. The 8051 hardware stack is just the opposite and is organized bottom to top.

When using the SMALL memory model, both the simulated stack and the 8051 3 hardware stack share the same memory area but from opposite directions.

The simulated stack and stack pointers are declared and initialized in the Cx51 startup code in STARTUP.A51 which can be found in the LIB subdirectory. You must modify the startup code to specify which simulated stack(s) to initialize in order to use reentrant functions. You can also modify the starting address for the top of the simulated stack(s) in the startup code. Refer to “STARTUP.A51” on page 151 for more information on reentrant function stack areas.

132 Chapter 3. Language Extensions

Alien Function (PL/M-51 Interface)

You may call routines written in PL/M-51 from your C programs to access PL/M-51 routines from C, declare them external with the alien function type specifier. For example:

 

 

extern alien char plm_func (int, char);

 

 

char c_func (void) {

 

 

 

int i;

 

 

 

char c;

 

 

 

for (i = 0; i < 100; i++) {

 

3

 

c = plm_func (i, c);

/* call PL/M func */

 

}

 

 

 

 

 

 

return (c);

 

 

 

}

 

 

 

You may create functions in C that are called by PL/M-51 routines. To do this,

 

 

 

use the alien function type specifier in the C function declaration. For example:

 

 

 

 

 

 

alien char c_func (char a, int b)

{

 

 

return (a * b);

 

 

 

}

 

Parameters and return values of PL/M-51 functions may be any of the following types: bit, char, unsigned char, int, and unsigned int. Other types, including long, float, and all types of pointers, can be declared in C functions with the alien type specifier. However, use these types with care because PL/M-51 does not directly support 32-bit binary integers or floating-point numbers.

Public variables declared in the PL/M-51 module are available to your C programs by declaring them external like you would for any C variable.

Keil Software — Cx51 Compiler User’s Guide

133

 

 

Real-time Function Tasks

The Cx51 compiler provides support for the RTX51 Full and RTX51 Tiny real-time multitasking operating systems through use of the _task_ and _priority_ keywords. The _task_ keyword defines a function as a real-time task. The _priority_ keyword specifies the priority for the task.

For example:

void func (void) _task_ num _priority_ pri

where:

num

is a task ID number from 0 to 255 for RTX51 Full or 0 to 15

3

 

 

for RTX51 Tiny.

 

pri

is the priority for the task. Refer to the RTX51 User’s Guide

 

 

or the RTX51 Tiny User’s Guide for more information.

 

Task functions must be declared with a void return type and a void argument list.

134

Chapter 3. Language Extensions

 

 

3

Соседние файлы в папке HLP