Compiler Directive in RPGLE AS400

Compiler directive is an instruction given to the compiler to do specific tasks during compilation of RPGLE programs
Compiler Directive in RPGLE AS400,compiler directive, compiler dicrective in rpg, compiler directive in rpgle, compiler directive in as400, compiler directive in ibmi, compiler directives, introdution to compiler directive in as400
Compiler Directive in RPGLE AS400

Compiler Directive

  • The compiler directive statements /TITLE, /EJECT, /SPACE, /COPY, and /INCLUDE allow us to include heading information, control the spacing of the compiler listing, and insert records from other sources file members during the source RPG member compilation.
  • The conditional compiler directive statements /DEFINE, /UNDEFINE, /IF, /ELSEIF, /ELSE, /ENDIF, and /EOF allows us to select or omit the source lines.These compiler directives can be specified for Control, File, Definition, and Procedure statements and none other directives can be defined within these compiler directive statements.
  • The /SET and /RESTORE compiler directive allows us to temporarily change some of the control statements keywords.
  • The /OVERLOAD compiler directive allows us to get additional information about how the compiler knows which overloaded prototype to use during the procedure call.
  • The /FREE and /END-FREE compiler directives are no longer used. If defined they will be ignored if used **FREE fully free RPG but the syntax will still be checked. However earlier /FREE compiler directive specifies the start of free-form calculation specs and /END-FREE compiler directive specifies the end of the block.
  • The compiler directive will be defined in 7th position or later in Fixed-form RPG and in 1st position or later in **FREE fully free RPG.
  • Within Calculation specs, we can not define the compiler directives in free-form RPG.
  • The special directive **FREE can only be specified in the 1st position of the 1st line of the source member. When **FREE has specified then the source member is in fully-free RPG format.
  • /TITLE

    Use this /TITLE compiler directive to specify heading information for the compiler listing that would be displayed at the top of every page. The /TITLE compiler directive must be followed by the blank and the text after the blank would be printed on the compiler listing page. An RPG program can have more than one /TITLE compiler directive defined.

           /TITLE Test title 1   
    

    /EJECT

    Use this /EJECT compiler directive to specify that print the next lines of specifications on the new page of the compiler listing. /EJECT compiler directive must be followed by at least 2 blanks. This compiler directive cannot be coded in a free-form RPG.

        C                   EVAL      VAR1 = 'TEST'                             
         /EJECT                                                                 
        C                   SETON                                        LR     
    

    /SPACE

    Use this /SPACE compiler directive to control the line spacing within the source section of the compiler listing. The /SPACE compiler directive must be followed by 1 space and then followed by a value between 1 to 112 that tells the number of lines to be spaces in the compiler listing. This compiler directive cannot be coded in Free-form RPG.

         C                   EVAL      VAR1 = 'TEST'                          
          /SPACE 5                                                            
         C                   SETON                                        LR  
    

    /SET

    Use this /SET compiler directive to temporarily change values for keywords in control specs. We can specify the following keywords with the /SET directive.

  • CCSID(*CHAR: ccsid)
  • Specify the default CCSID for character fields that are defined without the CCSID keyword. Here CCSID would be *JOBRUN or *JOBRUNMIX or *UTF8 or *HEX or number.

  • CCSID(*GRAPH: ccsid)
  • Specify the default CCSID for graphic fields that are defined without the CCSID keyword. Here CCSID would be *JOBRUN or *JOBRUN or *SRC or *HEX or *IGNORE or number.

  • CCSID(*UCS2 : ccsid)
  • Specify the default CCSID for UCS-2 fields that are defined without the CCSID keyword. Here CCSID would be *UTF16 or number.

  • DATFMT(format)
  • Specify the default format and separators for date fields that are specified without the date format.

  • TIMFMT(format)
  • Specify the default format and separators for time fields that are specified without the time format.

    Example of using /SET compiler directive

    In this example, we will see how /SET compiler directive will affect the CCSID for the FLD2 variable.

    CTL-OPT CCSID(*CHAR : 37)
    DCL-S FLD1 char(10);   
    /SET CCSID(*CHAR : 273)  
    DCL-S FLD2 char(10);     
    
  • Initially, the CCSID is set as default 37 for those character variables for which the CCSID keyword is not defined.
  • FLD1 has a CCSID of 37 as defined in the control statement above it.
  • After that /SET compiler directive is used which is then setting ccsid as 273 for character fields.
  • Therefore FLD2 has ccsid of 273 instead of 37.
  • /RESTORE

    Use this /RESTORE compiler directive to restore values previously set by /SET directive in the same source member or undoes the effect of the previous /SET compiler directive.

  • CCSID(*CHAR)
  • CCSID(*GRAPH)
  • CCSID(*UCS2)
  • DATFMT
  • TIMFMT
  • Example of using /RESTORE compiler directive

    Here we use the above example used with /SET compiler directive, we will see how /RESTORE compiler directive will affect the CCSID for the FLD3 variable.

    CTL-OPT CCSID(*CHAR : 37)
    DCL-S FLD1 char(10);   
    /SET CCSID(*CHAR : 273)  
    DCL-S FLD2 char(10); 
    /RESTORE CCSID(*CHAR)  
    DCL-S FLD3 char(10);  
    
  • Initially, the CCSID is set as default 37 for those character variables for which the CeCSID keyword is not defined.
  • FLD1 has a CCSID of 37 as defined in the control statement above it.
  • After that /SET compiler directive is used which is then setting ccsid as 273 for character fields.
  • Therefore FLD2 has ccsid of 273 instead of 37.
  • After that /RESTORE compiler directive is used which will restore the ccsid for the character fields defined without ccsid.
  • FLD3 will have CCSID of 37.
  • /OVERLOAD

    The /OVERLOAD compiler directive has parameters DETAIL or NODETAIL. Specify DETAIL if we want to see detailed information of the calls to the overloaded prototypes in statements following the compiler directive in the compiler listing.

    /COPY or /INCLUDE

    Both the compiler directives /COPY or /INCLUDE have the same purpose and the same syntax but are handled differently by the SQL precompiler and it depends upon the RPG preprocessor options parameter (RPGPPOPT) specified on the CRTSQLRPGI command.

    When *NONE is specified for the RPGPPOPT parameter on the CRTSQLRPGI command, then the compiler is not called for preprocessing.

    When *NONE is specified for the RPGPPOPT parameter on the CRTSQLRPGI command, then the compiler is not called for preprocessing.

    When *LVL1 is specified for the RPGPPOPT parameter on the CRTSQLRPGI command, then the compiler is called for preprocessing to expand /COPY and handle the conditional compilation directives.

    When *LVL2 is specified for RPGPPOPT parameter on CRTSQLRPGI command, then the compiler is called for preprocessing to expand /COPY and /INCLUDE and handle the conditional compilation directives except for the /INCLUDE compiler directive.

  • The /COPY and /INCLUDE compiler directives will insert the source lines in the current source member at the line where /COPY or /INCLUDE is used from the different source members during compilation.
  • Main source member cannot be included using compiler directive /COPY and /INCLUDE. This will give the error Unable to open /COPY or /INCLUDE file.
  • Specify the physical files or the IFS files with the /COPY and /INCLUDE statement.
  • Using physical files with /COPY and /INCLUDE

    /COPY and /INCLUDE followed by 1 space followed by file name or path. The format can be as follows:

  • The source member will be searched in the library/source file specified. If not found then will be searched on IFS.
  • Libraryname/Filename,Membername

    Or

  • The source member will be searched in the source file specified. The library list will be searched for the source file and its member, if not found then will be searched on IFS.
  • Filename,Membername

    Or

  • The source member will be searched in the QRPGLESRC source file assumed in the library list, if not found then will be searched on IFS.
  • Membername
    
  • A member's name must be specified.
  • If a file name is not specified then QRPGLESRC is assumed.
  • If a library is not specified, then the library list is searched for the file and its member.
  • Using IFS file with /COPY and /INCLUDE

  • The IFS file path can be enclosed within single or double-quotes. If the IFS path is blank then it must be enclosed within quotes.
  • If the path does not contain a suffix such as ".rpgle" or ".rpgleinc" then the file gets searched with its name and also for files suffixed with ".rpgle" or ".rpgleinc"
  • The syntax mentioned above for including physical files can be used for IFS files as well.
  • Copies from the IFS file PGM1.rpgle in directory EASYCLASS1.
  • /COPY /EASYCLASS1/PGM1.rpgle 
    
  • Copies from the IFS file PGM1, PGM1.rpgle, PGM1.rpgleinc in directory EASYCLASS1.
  • /COPY /EASYCLASS1/PGM1
  • Copies from the IFS file PGM1.rpgle in directory EASYCLASS1.Directory EASYCLASS1 will be searched using the IFS search path.
  • /COPY EASYCLASS1/PGM1.rpgle
  • Copies from the IFS file whose names contain blanks.
  • /COPY "IFS file having blanks" 
    /COPY 'IFS file having blanks'
    

    Nested /COPY or /INCLUDE

    Nesting of /COPY and /INCLUDE compiler directives is allowed.

  • These inserted source members can have further /COPY and /INCLUDE compiler directive statements up to the maximum nesting depth specified by the COPYNEST keyword. (Max. 32 when not specified)
  • Conditional Compiler Directive

    The conditional compiler directive allows us to include or exclude sections of source code from the program compilation.

    Define the condition name and remove the condition using the /DEFINE compiler directive and /UNDEFINE compiler directive

    /DEFINE

    The /DEFINE conditional directive defines conditions for conditional program compilation. /DEFINE must be followed by at least one space and then the condition name should also be specified on the same line. The /DEFINE compiler directive cannot be specified within the free-format RPG and it adds the condition name to the currently defined conditions list. Therefore, /IF DEFINED(condition_name) is TRUE and /IF NOT DEFINED(condition_name) is FALSE. There can be a maximum of up to 320 conditions that can be added using the DEFINE directive on the CRTBNDRPG and CRTRPGMOD commands.

    /UNDEFINE

    The /UNDEFINE compiler directive tells that the condition is no longer defined or exists. The /UNDEFINE compiler directive must be followed by one space and then the condition name should also be specified on the same line./UNDEFINE cannot be used with free-format RPG. The /UNDEFINE compiler directive removes the condition name from the currently defined conditions list.

    Predefined Conditions

    Conditions that are defined by the compiler for us are predefined conditions. These conditions cannot be used with /DEFINE and /UNDEFINE compiler directives. The predefined conditions can only be used with /IF and /ELSEIF.

  • *ILERPG: This condition is defined if the program is being compiled by the RPG IV compiler.
  •                      
          /IF DEFINED(*ILERPG)
         H BNDDIR('QC2LE')
          /ENDIF
    
  • *CRTBNDRPG: This condition is defined if the program is being compiled by the CRTBNDRPG command which creates a program object.
  •                      
          /IF DEFINED(*CRTBNDRPG)
         H DFTACTGRP(*NO)
          /ENDIF
    
  • *CRTRPGMOD: This condition is defined if the program is being compiled by the CRTRPGMOD command which creates a module object.
  •                      
          /IF DEFINED(*CRTRPGMOD)
         H 
          /ENDIF
    
  • *VxRxMx: This condition is defined if the program is being compiled for the specific version that is greater or equal to the release defined in this condition starting with *V4R4M0(Version 4 Release 4 Modification 0).
  •                      
          /IF DEFINED(*V6R1M0)
            // specify the code valid on V6R1M0
          /ENDIF
    

    Testing Conditions

    Conditions are tested using /IF directive groups. The /IF directive can be followed by zero or more /ELSEIF directive and optionally followed by /ELSE directive and finally followed by and /ENDIF directive. All the source lines except compile-time data are valid inside the /IF directive groups.

    /EOF

    /EOF compiler directive tells the compiler to ignore the rest of the source lines in the compiled source member.

    Post a Comment

    © AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain