Swift on z (available on z/OS and LinuxOne) is a fast, safe, and modern programming language.

Swift ships with a wide variety of modules and packages such as SwiftCore, Foundation, Lib-dispatch, IBM Kitura and more. These libraries provide methods for File Handling, Networking, JSON parsing, and much more. Although these libraries are very useful, the key design goal for Swift on z is to enable interoperability with existing non-Swift libraries on the Z platform.

What this means is that often times we want to make interlanguage calls to existing mainframe libraries which are written in typical mainframe languages. These libraries are typically written in ASM, PL/I, COBOL and C.

Fortunately, Swift on Z provides a mechanism for interlanguage calls between Swift and the desired language. This is performed via a bridge, known as a module map, which provides the location to a C header interface that exposes the routine or set of routines. Since Swift is a natively compiled language, it can directly link to existing libraries. On z/OS, these libraries must be compiled with 64-bit and XPLINK and must be in the GOFF object format.

Note that a C header interface can be provided for routines in C++, ASM and PL/I.

C Interoperability

The following code demonstrates an example of exposing a C header myheader.h to Swift

Here’s an example of a module map that bridges the C header to Swift:

module MyModule [extern_c] 
	header “myheader.h”
	link “mylib"
	export *

In this case, the header myheader.h provides the interface (function declarations) to Swift via the module named MyModule.

Assume myheader.h contains the following function declaration:

int sum(int a, int b);

The corresponding myheader.c implementation is provided below:

int sum(int a, int b) { return a + b; }

In Swift, in order to make use of the module MyModule, the following import statement must be specified at the top of the source file.

import MyModule

let x = sum(5, 5)

The C function can then be called directly in the Swift source file. How does Swift know to map C int to Swift’s Int64? Well, Swift has mappings of C data types to Swift data types. Read more here

In order to build this example, you must first compile the C source file (using xlc and option -q64) into a object, static library (.a) or dynamic library (DLL). Then you must compile and link the Swift source file with the corresponding library, making sure that you are indicating the path to the module map using the -I option

xlc -q64 -o myheader.o myheader.c
swiftc -I/location/to/ -o main main.swift myheader.o

Swift to PLI Interlanguage Calls

In order to expose routines from non-C languages, a C header file must be used as the bridge. In the case of PL/I, a function declaration must be created for every given PL/I function that you wish to expose to Swift.

For example, given the following PL/I routine:

*process display(std);
 write: proc(k,v) ext("writepair")
   returns(fixed bin(63) byvalue);
   dcl k pointer byvalue;
   dcl v pointer byvalue;
   dcl akey char(16) varz based(k);
   dcl avalue char(32) varz based(v);
   dcl key char(16);
   dcl value char(32) varz;
   display("In PLI program");
   call pliebcdic(addr(key),addr(akey),length(akey));
   call pliebcdic(addr(value),addr(avalue),length(avalue));

This routine would be exposed via the C header myheader.h:

int writepair(const char *,const char *);

You can then call the writepair PLI routine in Swift as follows:

let rc = writepair("stuff","of value")

The same mechanism can be applied to ASM and C++. Interlanguage calls with COBOL is currently not directly possible as the COBOL compiler can only produce 31-bit objects. Swift on z produces 64-bit modules at this time.

Swift is a unicode language. Keep this in mind when making interlanguage calls and when passing in strings. Strings are almost always passed in as ASCII strings and must be converted or handled appriopriately.


3 comments on"Swift on z/OS and Linux on Z – Interoperability with C, C++, PL/I, and ASM"

  1. Glenn Everitt September 28, 2017

    Is COBOL interoperability on the road-map?

    • Glenn Everitt September 28, 2017

      I guess the question should be is 64 bit COBOL on the road-map and then would Swift support 64 bit COBOL?

  2. Igor Todorovski September 18, 2018

    Hi Glenn, we are considering COBOL and Swift interoperability. The main limitation as you mentioned is that COBOL generates 31-bit program objects. In the meantime, we may be able to take advantage of the JSON features in the new COBOL compiler, JSON PARSE and JSON Generate and communicate via JSON between Swift and COBOL via IPC.

Join The Discussion

Your email address will not be published. Required fields are marked *