-pic


< Compile Options / Microcontroller Options >

[Format]

-pic

 

-

[Default]

This option does not generate code with the program section as PIC (position independent code).

[Description]

-

This option generates code with the program section as PIC (position independent code).

-

In PIC, all function calls are performed with BSR or BRA instructions. When acquiring the address of a function, a relative address from the PC should be used. This allows PIC to be located at a desired address after linkage.

[Example]

-

Calling a function (only for branch=32)

void func()
{
        sub();
}
[Without -pic]
_func:
            MOV.L       #_sub,R14
            JMP         R14
[With -pic]
_func:
            MOV.L       #_sub-L11,R14
L11:
            BRA         R14

-

Acquiring a function address

void func1(void);
void (*f_ptr)(void);
void func2(void)
{
        f_ptr = func1;
}
[Without -pic]
_func2:
            MOV.L       #_f_ptr,R4
            MOV.L       #_func1,[R4]
            RTS
[With -pic]
_func2:
         MOV.L       #_f_ptr,R4
L11:
         MVFC        PC,R14
         ADD         #_func1-L11,R14
         MOV.L       R14,[R4]
         RTS

[Remarks]

-

In C++ or EC++ compilation, the pic option cannot be selected. If selected, message W0511171 is output as a warning and the selection of the pic option is disabled.

-

The address of a function which is PIC should not be used in the initialization expression used for static initialization. If used, error E0523026 will occur.

-

<Example of using a PIC address for static initialization>

void pic_func1(void), pic_func2(int), pic_func3(int); /* Becomes PIC */
void (*fptr1_for_pic) = pic_func1; /* Uses PIC address in static initialization: Error */
struct PIC_funcs{ int code; void (*fptr)(int); };
struct PIC_funcs pic_funcs[] = {
    { 2, pic_func2 },              /* Uses PIC address in static initialization: Error */
    { 3, pic_func3 },              /* Uses PIC address in static initialization: Error */
};

-

When creating a code for startup of the application program using the PIC function, refer to the Application Startup section of the STARTUP chapter.

-

For the PIC function, also refer to the Usage of PIC/PID Function of the STARTUP section.