IBM Z and LinuxONE - Languages

Languages

Languages

Broad range of supported development languages and tools allows to transform software delivery practices.

 View Only

METAL C Compiler Option in z/OS

By FANG LU posted Tue March 24, 2020 07:37 PM

  

Assembly language is powerful. It is close to the underlying hardware architecture; it allows you to access low level system features, interacting closely with the operating system; and it has no runtime dependencies. In short, it allows you to do almost anything you want.


But assembly language forces you to spell out all the minor details in the code. You need to focus on the leaves and trees, instead of the forest and the landscape. You need to spend time on the low level details and not the high level logic. You need to hand hold the underlying machine every step of the way. It is tedious. An assembly program can take time to write and debug. The resulting code can be difficult to maintain.


Wouldn’t it be nice if you could use a high level language to do low level programming ?


The METAL option of XL C compiler on z/OS is designed with this in mind. The notion of “metal” is such that the program can operate at the operating system level which is close to the hardware -- i.e. the metal. With the METAL option you can now use C language features to express low level programming logic, for example writing user exits. You are freed from the tedious tasks such as managing the registers and developing the correct instruction sequences while you program can enjoy highly optimized code tuned for the intended hardware.


The XL C compiler generated code requires the Language Environment to establish an overall execution context. C library functions also require the Language Environment in order to provide their services; most noticeably the functions to manage the heap storage and dynamic storage area, to do file input-output, and to handle exception conditions. If your C program needs to get close to the metal, the compiler will have to provide alternate means so that you get these services directly from the operating system, without going through LE. The operating system provides these through assembler macros. But you cannot invoke assembler macros from within C code unless you can embed short sequences of assembly instructions into a C program. This is what the METAL option provides.


The METAL option generates code in assembler source program format. You can then feed the assembly source through the High Level Assembler, like any other assembly programs. The resulting code is independent of the Language Environment. It can be used like other assembly code without tying to a runtime. You can write a statement like if (x + y * z > 0) x = -x, and the XL C compiler will turn it into assembly instructions. You can quickly try this out with a simple program:


<hello.c>

int abc;

int xyz;


int main() {

xyz = abc + abc;

return xyz;

}

</hello.c>


Under z/OS UNIX, you can use the xlc command to compiler hello.c as follows:


xlc -S -qMETAL hello.c


The -S flag ask the compiler to generate assembler code; the output file is hello.s. -qMETAL specifies the METAL compiler option. You can then assemble and linkedit hello.s like any other assembler source file.


With the METAL option, you can use certain assembler macros directly from within a C program. I will write more about this in the future.


Note: For further information on the METAL compiler option and related features, see z/OS Metal C Programming Guide and Reference and z/OS XL C/C++ User's Guide.

0 comments
1 view

Permalink