Compile an MI program

To compile an MI program, use the Create Program (QPRCRTPG) API.

Note: Read the Code license and disclaimer information for important legal information.

If you enter the source into a source physical file, you can now compile the source and create an MI program.

Note: The QPRCRTPG API assumes that the source statements presented to it are in code page 37. See iSeries™ Machine Interface instructions for the specific code points required to build MI programs.

Use CLCRTPG to create an MI program

Assume that the source is in a member named MI01 in the source file MISRC, which is created with a default record length (RCDLEN) of 92. The following CLCRTPG CL program can be used to create an MI program called MI01. (An MI program to call the Create Program (QPRCRTPG) API is developed in Creating an MI version of CLCRTPG.)

Note: All non-MI source examples are provided in CL, because CL is the one language (other than REXX) that is standard on all systems. Other high-level languages (HLLs) could be used in place of the CL programs (and in many cases would have been easier).

The following program reads a source file member into a program variable (&MIPGMSRC) and then does a CALL to the QPRCRTPG API. This program has many limitations (the major limitation is a program variable-size limit of 2000 bytes for the source), but provides for a reasonably simple MI program creation scenario.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/********************************************************************/
/********************************************************************/
/*                                                                  */
/*     Program Name: CLCRTPG                                        */
/*                                                                  */
/*     Programming Language: CL                                     */
/*                                                                  */
/*     Description: Create an MI program using the QPRCRTPG API.    */
/*                                                                  */
/*                                                                  */
/*     Header Files Included: None                                  */
/*                                                                  */
/*                                                                  */
/********************************************************************/
             PGM        PARM(&SRCMBR)
             DCLF       FILE(MISRC)
             DCL        VAR(&SRCMBR) TYPE(*CHAR) LEN(10)
             DCL        VAR(&MIPGMSRC) TYPE(*CHAR) LEN(2000)
             DCL        VAR(&MIPGMSRCSZ) TYPE(*CHAR) LEN(4)
             DCL        VAR(&OFFSET) TYPE(*DEC) LEN(5 0) VALUE(1)
             DCL        VAR(&PGMNAM) TYPE(*CHAR) LEN(20) +
                          VALUE('          *CURLIB   ')
             DCL        VAR(&PGMTXT) TYPE(*CHAR) LEN(50) +
                          VALUE('Compare two packed arguments and +
                          return larger')
             DCL        VAR(&PGMSRCF) TYPE(*CHAR) LEN(20) +
                          VALUE('*NONE')
             DCL        VAR(&PGMSRCM) TYPE(*CHAR) LEN(10) VALUE(' ')
             DCL        VAR(&PGMSRCCHG) TYPE(*CHAR) LEN(13) VALUE(' ')
             DCL        VAR(&PRTFNAM) TYPE(*CHAR) LEN(20) +
                          VALUE('QSYSPRT   *LIBL     ')
             DCL        VAR(&PRTSTRPAG) TYPE(*CHAR) LEN(4) +
                          VALUE(X'00000001')
             DCL        VAR(&PGMPUBAUT) TYPE(*CHAR) LEN(10) +
                          VALUE('*ALL      ')
             DCL        VAR(&PGMOPTS) TYPE(*CHAR) LEN(22) +
                          VALUE('*LIST      *REPLACE   ')
             DCL        VAR(&NUMOPTS) TYPE(*CHAR) LEN(4) +
                          VALUE(X'00000002')
 LOOP:       RCVF
             MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(CRTPGM))
             CHGVAR     VAR(%SST(&MIPGMSRC &OFFSET 80)) VALUE(&SRCDTA)
             CHGVAR     VAR(&OFFSET) VALUE(&OFFSET + 80)
             GOTO       CMDLBL(LOOP)
 CRTPGM:     CHGVAR     VAR(%SST(&PGMNAM 1 10)) VALUE(&SRCMBR)
             CHGVAR     VAR(%BIN(&MIPGMSRCSZ)) VALUE(&OFFSET)
             CALL       PGM(QSYS/QPRCRTPG) PARM(&MIPGMSRC +
                          &MIPGMSRCSZ &PGMNAM &PGMTXT &PGMSRCF +
                          &PGMSRCM &PGMSRCCHG &PRTFNAM &PRTSTRPAG +
                          &PGMPUBAUT &PGMOPTS &NUMOPTS)
             ENDPGM

Create the MI example program

After creating the CL program (assumed to be called CLCRTPG), the following statements create the previous MI program MI01:

DLTOVR MISRC
OVRDBF MISRC MBR(MI01)
CALL CLCRTPG MI01
Note: If the creation of MI01 fails, you should closely compare your source to that shown in this chapter. In general, consider the QPRCRTPG error messages that refer to "probable compiler error" as referring to your input source and not that the QPRCRTPG API itself is in error. (QPRCRTPG assumes its input is probably from a high-level language (HLL) compiler.)

Further, if the error message is CPF6399 (Identifier not declared), you can get an object definition table (ODT) listing by adding *XREF to the option template parameter (variable &PGMOPTS in the CLCRTPG program) when calling the QPRCRTPG API. Add *XREF to the existing *LIST and *REPLACE options, and change the number of option template entries parameter (variable &NUMOPTS) to 3.

Test MI01

In this topic, assume that MI01 was successfully created.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/********************************************************************/
/********************************************************************/
/*                                                                  */
/*     Program Name: CL01                                           */
/*                                                                  */
/*     Programming Language: CL                                     */
/*                                                                  */
/*     Description: Test the MI program MI01.                       */
/*                                                                  */
/*                                                                  */
/*     Header Files Included: None                                  */
/*                                                                  */
/*                                                                  */
/********************************************************************/
             PGM        PARM(&ARG1 &ARG2)
             DCL        VAR(&ARG1) TYPE(*DEC) LEN(15 5)
             DCL        VAR(&ARG2) TYPE(*DEC) LEN(15 5)
             DCL        VAR(&RESULT) TYPE(*DEC) LEN(15 5)
             DCL        VAR(&MSG) TYPE(*CHAR) LEN(20)
             DCL        VAR(&USR) TYPE(*CHAR) LEN(10)
             RTVJOBA    USER(&USR)
             CALL       PGM(MI01) PARM(&ARG1 &ARG2 &RESULT)
             CHGVAR     VAR(&MSG) VALUE(&RESULT)
             SNDMSG     MSG(&MSG) TOUSR(&USR)
             ENDPGM

The following statement calls the CL01 program:

CALL CL01 (-5    6)

This test should cause a message to be sent to your user message queue with the following value:

00000000000006.00000

Debug the MI program

The MI program (MI01) that you created is a standard *PGM object on the iSeries system. As you would expect, you can call MI01 from other high-level languages. You can delete MI01 with the Delete Program (DLTPGM) command, save and restore MI01 using the standard save (SAV) and restore (RST) commands, and so on.

You can also debug it using the standard debugger on the system. To debug it, you need to look at the listing produced by the QPRCRTPG API to determine the MI instruction number. Then use that number with the Add Breakpoint (ADDBKP) CL command. For example, when creating MI01 in the previous exercise, the following listing was generated by QPRCRTPG:

5763SS1 V3R1M0 940909                                Generated Output                                 08/08/94 09:46:36  Page     1
  SEQ (1)INST Offset    Generated Code     *... ... 1 ... ... 2 ... ... 3 ... ... 4 ... ... 5 ... ... 6 ... ... 7 ... ... 8
 00001                                             ENTRY * (PARM_LIST) EXT                                               ;
 00002                                             DCL SPCPTR ARG1@ PARM                                                 ;
 00003                                             DCL SPCPTR ARG2@ PARM                                                 ;
 00004                                             DCL SPCPTR RESULT@ PARM                                               ;
 00005                                             DCL OL PARM_LIST (ARG1@, ARG2@, RESULT@) PARM EXT                     ;
 00006                                             DCL DD ARG1 PKD(15,5) BAS(ARG1@)                                      ;
 00007                                             DCL DD ARG2 PKD(15,5) BAS(ARG2@)                                      ;
 00008                                             DCL DD RESULT PKD(15,5) BAS(RESULT@)                                  ;
 00009  0001 000004  3C46 2000 0006 0007           CMPNV(B) ARG1,ARG2 / LO(ITS2)                                         ;
                     0009
 00010  0002 00000E  1042 0008 0006                CPYNV RESULT,ARG1                                                     ;
 00011  0003 000014  1011 000A                     B RETURN                                                              ;
 00012  0004 000018  3042 0008 0007       ITS2:    CPYNV RESULT,ARG2                                                     ;
 00013  0005 00001E  22A1 0000  (2)        RETURN:  RTX *                                                                ;
 00014  0006 000022  0260                          PEND                                                                  ;
 5763SS1 V3R1M0 940909                                Generated Output                                 08/08/94 09:46:36  Page     2
  MSGID    ODT   ODT Name                                          Semantics and ODT Syntax Diagnostics
 5763SS1 V3R1M0 940909                                Generated Output                                 08/08/94 09:46:36  Page     3
   MSGID   MI Instruction Stream Semantic Diagnostics

Set breakpoints in the MI program

To view the value of RESULT at label RETURN, you first determine that RETURN corresponds to MI instruction ((1)) 0005 ((2)) and enter the following CL commands:

STRDBG PGM(MI01)
ADDBKP STMT('/0005') PGMVAR((RESULT ()))
CALL CL01 (-5    6)

The following display is shown:

+--------------------------------------------------------------------------------+
|                                                                                |
|                               Display Breakpoint                               |
|                                                                                |
|  Statement/Instruction . . . . . . . . . : /0005                               |
|  Program . . . . . . . . . . . . . . . . : MI01                                |
|  Recursion level . . . . . . . . . . . . : 1                                   |
|  Start position  . . . . . . . . . . . . : 1                                   |
|  Format  . . . . . . . . . . . . . . . . : *CHAR                               |
|  Length  . . . . . . . . . . . . . . . . : *DCL                                |
|                                                                                |
|  Variable  . . . . . . . . . . . . . . . : RESULT                              |
|    Type  . . . . . . . . . . . . . . . . :   PACKED                            |
|    Length  . . . . . . . . . . . . . . . :   15 5                              |
|   '          6.00000'                                                          |
|                                                                                |
+--------------------------------------------------------------------------------+

Breakpoints also can be set with a directive statement. Given that the MI01 program is able to be debugged and a break directive was not used, the purpose for which you use the directive may not be obvious. As mentioned in Create the MI example program, many expected users of the QPRCRTPG API are compilers of HLLs. The break (BRK) directive allows users of the QPRCRTPG API to associate an HLL statement identifier with a generated MI instruction. For example, assume that MI01 was developed to be an implementation of a fictional HLL language statement such as:

RESULT = MAX(ARG1, ARG2)

This assigns the MAX (defined as the largest argument) of ARG1 or ARG2 to RESULT. Also assume that an HLL programmer had written a program called HLLEXAMPLE with the following statements:

00001  RESULT = MAX(ARG1, ARG2)
00002  EXIT

By using break (BRK) directives, the QPRCRTPG user or compiler could associate the HLL statements with the generated MI instructions in the following way.

/********************************************************************/
/********************************************************************/
/*                                                                  */
/*     Program Name: MI01                                           */
/*                                                                  */
/*     Programming Language: MI                                     */
/*                                                                  */
/*     Description: Demonstrate how to associate HLL statement      */
/*                  identifiers with MI instructions using BRK      */
/*                  directives.                                     */
/*                                                                  */
/*     Header Files Included: None                                  */
/*                                                                  */
/*                                                                  */
/********************************************************************/
ENTRY * (PARM_LIST) EXT;
DCL     SPCPTR      ARG1@      PARM;
DCL     SPCPTR      ARG2@      PARM;
DCL     SPCPTR      RESULT@    PARM;
DCL     OL          PARM_LIST
                   (ARG1@,
                    ARG2@,
                    RESULT@)
                    PARM       EXT;
DCL     DD          ARG1       PKD(15,5)     BAS(ARG1@);
DCL     DD          ARG2       PKD(15,5)     BAS(ARG2@);
DCL     DD          RESULT     PKD(15,5)     BAS(RESULT@);
BRK "00001";
        CMPNV(B)    ARG1,ARG2  / LO(ITS2);
        CPYNV       RESULT,ARG1;
        B           RETURN;
ITS2:   CPYNV       RESULT,ARG2;
BRK "00002";
RETURN: RTX         *;
        PEND;

This allows the HLL programmer to use the following to debug the HLL program by using the statement identifiers of the HLL:

STRDBG PGM(HLLEXAMPLE)
ADDBKP STMT(00002) PGMVAR((RESULT ()))

The following display shows that the HLL statement 00002 has been equated with MI instruction 0005 due to the use of BRK directives:

+--------------------------------------------------------------------------------+
|                                                                                |
|                                                                                |
|                                                                                |
|                               Display Breakpoint                               |
|                                                                                |
|  Statement/Instruction . . . . . . . . . : 00002 /0005                         |
|  Program . . . . . . . . . . . . . . . . : HLLEXAMPLE                          |
|  Recursion level . . . . . . . . . . . . : 1                                   |
|  Start position  . . . . . . . . . . . . : 1                                   |
|  Format  . . . . . . . . . . . . . . . . : *CHAR                               |
|  Length  . . . . . . . . . . . . . . . . : *DCL                                |
|                                                                                |
|  Variable  . . . . . . . . . . . . . . . : RESULT                              |
|    Type  . . . . . . . . . . . . . . . . :   PACKED                            |
|    Length  . . . . . . . . . . . . . . . :   15 5                              |
|   '          6.00000'                                                          |
|                                                                                |
+--------------------------------------------------------------------------------+

Handle exceptions in the MI program

As coded, the MI01 program works fine when it is passed packed decimal parameters. But when the MI01 program is passed other data types, such as in CALL CL01 (abc 6), exceptions occur. To handle these exceptions, additional statements could be added to MI01 so that:

Add the following statements to MI01:

  1. Declare a fourth space parameter to receive the return code parameter:
    DCL     SPCPTR      RC@        PARM;
  2. Update the operand list directive for PARM_LIST:
    DCL     OL          PARM_LIST
                       (ARG1@,
                        ARG2@,
                        RESULT@,
                        RC@)              /* the new parameter  */
                        PARM       EXT;
  3. Declare the storage addressed by RC@ as a 1-byte character data element:
    DCL     DD          RC         CHAR(1)       BAS(RC@);
  4. Declare an exception handler for MCH1202. With this exception description, all occurrences of MCH1202 will cause an immediate (IMD) branch to label M1202.
    DCL     EXCM        DATAERROR  EXCID(H'0C02') BP (M1202) IMD;
    Note: The EXCID is the hexadecimal representation of the message identifier string 1202 where 12 = X'0C' and 02 = X'02'. While most MCH errors follow this relationship of message ID string to hexadecimal EXCID, you should always refer to the iSeries Machine Interface instructions to determine what specific exception IDs may be signaled by a given MI statement.
  5. Because label M1202 is being used to indicate an error, set the return code to 1 by using copy bytes left-justified and then end:
    M1202:  CPYBLA      RC,'1';
            RTX         *;
            PEND;

    A more complete example of how to handle exceptions is provided in Handling exceptions in the MICRTPG2 program.

  6. Because the non-M1202 path indicates that no error was detected, update the normal return path:
    RETURN: CPYBLA      RC,'0';
  7. Because M1202 was appended to the end of the MI01 source, remove the original MI01 PEND directive.

The an updated view of the MI01 program is as follows:

/********************************************************************/
/********************************************************************/
/*                                                                  */
/*     Program Name: MI01                                           */
/*                                                                  */
/*     Programming Language: MI                                     */
/*                                                                  */
/*     Description: Enhanced version of MI program MI01 that        */
/*                  demonstrates enabling an exception monitor.     */
/*                                                                  */
/*     Header Files Included: None                                  */
/*                                                                  */
/*                                                                  */
/********************************************************************/
ENTRY * (PARM_LIST) EXT;
DCL     SPCPTR      ARG1@      PARM;
DCL     SPCPTR      ARG2@      PARM;
DCL     SPCPTR      RESULT@    PARM;
DCL     SPCPTR      RC@        PARM;
DCL     OL          PARM_LIST
                   (ARG1@,
                    ARG2@,
                    RESULT@,
                    RC@)
                    PARM       EXT;
DCL     DD          ARG1       PKD(15,5)     BAS(ARG1@);
DCL     DD          ARG2       PKD(15,5)     BAS(ARG2@);
DCL     DD          RESULT     PKD(15,5)     BAS(RESULT@);
DCL     DD          RC         CHAR(1)       BAS(RC@);
DCL     EXCM        DATAERROR  EXCID(H'0C02') BP (M1202) IMD;
        CMPNV(B)    ARG1,ARG2  / LO(ITS2);
        CPYNV       RESULT,ARG1;
        B           RETURN;
ITS2:   CPYNV       RESULT,ARG2;
RETURN: CPYBLA      RC,'0';
        RTX         *;
M1202:  CPYBLA      RC,'1';
        RTX         *;
        PEND;

The following example updates CL01 to support the new return code parameter:

/********************************************************************/
/********************************************************************/
/*                                                                  */
/*     Program Name: CL01                                           */
/*                                                                  */
/*     Programming Language: CL                                     */
/*                                                                  */
/*     Description: Enhanced version of CL program CL01 that        */
/*                  demonstrates the use of enhanced MI01.          */
/*                                                                  */
/*     Header Files Included: None                                  */
/*                                                                  */
/*                                                                  */
/********************************************************************/
             PGM        PARM(&ARG1 &ARG2)
             DCL        VAR(&ARG1) TYPE(*DEC) LEN(15 5)
             DCL        VAR(&ARG2) TYPE(*DEC) LEN(15 5)
             DCL        VAR(&RESULT) TYPE(*DEC) LEN(15 5)
             DCL        VAR(&RC) TYPE(*CHAR) LEN(1)
             DCL        VAR(&MSG) TYPE(*CHAR) LEN(20)
             DCL        VAR(&USR) TYPE(*CHAR) LEN(10)
             RTVJOBA    USER(&USR)
             CALL       PGM(MI01) PARM(&ARG1 &ARG2 &RESULT &RC)
             IF         COND(&RC = '0') +
               THEN(CHGVAR     VAR(&MSG) VALUE(&RESULT))
             ELSE +
               CHGVAR     VAR(&MSG) VALUE('ERROR FOUND')
             SNDMSG     MSG(&MSG) TOUSR(&USR)
             ENDPGM

After recompiling the MI01 program and the CL01 program, CALL CL01 (abc 6) now results in the following message (not the previous MCH1202):

ERROR FOUND
Related reference
Create Program (QPRCRTPG)
iSeries Machine Interface instructions
Creating an MI version of CLCRTPG
Create the MICRTPG2 program