29.4.1 Global Variable Initialization

In a native compile the value of sizeof (UINTN) is computed by the compiler at compile time. This can be done because the compiler already knows the instruction set architecture. The EBC compiler cannot do that in the same way. Instead, it generates code to calculate this value at execution time if the result is different on different CPU architectures. This limitation means that EBC code cannot use sizeof (UINTN), sizeof (INTN), and sizeof (VOID *) (or other pointer types) in C language statements that require constant expressions.

Note: The type EFI_STATUS is required to by type UINTN by the UEFI Specification. This means that a variable of type EFI_STATUS cannot be used in C language statements that require constant expressions. The code in the following example fails when compiled for EBC.

Example 251-Global Variable Initialization that fails for EBC

#include <Uefi.h>
#include <UefiBootServicesTableLib.h>
//
// Global variable definitions
//
UINTN IntegerSize = sizeof (UINTN); // EBC compiler error
UINTN PointerSize = sizeof (VOID *); // EBC compiler error
EFI_STATUS Status = EFI_INVALID_PARAMETER; // EBC compiler error

The following example shows one method to address the EBC compiler errors in the previous example. The general technique is to move the initialization of global variables that are impacted by the EBC specific issue into the driver entry point or other function that executes before the global variables are used.

Example 252-Global Variable Initialization that works for EBC

#include <Uefi.h>
#include <UefiBootServicesTableLib.h>
//
// Global variable definition
//
UINTN IntegerSize;
UINTN PointerSize;
EFI_STATUS Status;
VOID
InitializeGlobals (
VOID
)
{
IntegerSize = sizeof (UINTN);
PointerSize = sizeof (VOID *);
Status = EFI_INVALID_PARAMETER;
}