Jump to content

IBM RPG

From Alfawiki
RPG
Company typeProgramming language
IndustryComputer programming
Founded1959
FounderWilf Hey
ProductsRPG programming language versions (RPG II, RPG III, RPG IV, ILE RPG)
ParentIBM

RPG is a high-level programming language developed for business use, first introduced in 1959 for the IBM 1401 system. It is primarily recognized as the core programming language for IBM's midrange computer series, including those running the IBM i platform. RPG is characterized by unique features such as the program cycle mechanism and a syntax organized by columns. The most recent iteration, RPG IV, incorporates modernization enhancements like free-form coding style.

Platforms

Initially designed by IBM for the 1401 model, RPG was subsequently adapted to run on the 7070/72/74 and System/360 lines. The RPG II variant became the dominant language across IBM’s midrange computers, covering systems such as the System/3, System/32, System/34, System/38, System/36, and the AS/400. Versions have also been developed for other platforms including DEC VAX, Sperry Univac BC/7, Siemens BS2000, Burroughs B700 and B1700, Hewlett Packard HP 3000, ICL 2900 series, Honeywell 6220 and 2020, Four-Phase IV/70 and IV/90, Singer System 10, Wang VS, plus Unix-based environments and personal computers through compilers like Infinite36 and Baby/400.

Applications written in RPG II continue to be supported on systems such as VSE, z/OS, Unisys MCP, Microsoft Windows, and OpenVMS.

Background

Created by IBM in 1959, Report Program Generator (RPG) was intended to simplify report creation from data files. Wilf Hey at IBM is credited with its development according to historical accounts. Its precursor was the FARGO (Fourteen-o-one Automatic Report Generation Operation), which operated on the IBM 1401.

Both RPG and FARGO helped IBM’s tabulating machine operators transition smoothly to computer programming by mimicking the control panel wiring of tab machines through program cycles. Over time, RPG became more widely adopted than FARGO as a report generation tool.

IBM introduced RPG(7070-RG-902) for the IBM 7070/72/74 systems but did not extend FARGO to these platforms. During this period, other programming options included Assembler, COBOL, and FORTRAN. While Assembler and COBOL were prevalent in mainframe business applications (System/360 models 30 and above), RPG was preferred by users migrating from tabulating equipment on systems like the System/360 model 20.

RPG II

Launched around 1969 alongside the introduction of the System/3 series, RPG II was later implemented on systems such as the System/32, System/34, System/36, and even larger platforms like the IBM System/370 mainframe running DOS/VSE variants. Additionally, ICL provided an RPG II version for its VME/K operating system.

A major advancement in RPG II was its program cycle concept: this automated processing of individual records throughout an entire file while controlling program flow via logical indicators numbered 01 through 99. This approach allowed efficient management of hierarchical data relationships such as headers and details or level breaks, enabling complex reports to be created with far less code compared to languages like COBOL.

The typical RPG II program was structured into several specification types: File Specifications enumerated files accessed or modified; Data Definition Specifications described elements including Data Structures and arrays; Calculation Specifications contained executable logic; and Output Specifications defined file or report layouts when needed. Use of external definitions further minimized manual coding for input/output operations.

RPG III

RPG III was created for the System/38 and its successor, the AS/400, incorporating advanced structured programming elements such as IF-ENDIF constructs, DO loops, and subroutine capabilities. This version was also accessible on larger platforms like the IBM System/370 mainframe running OS/VS1, as well as on Unisys machines operating the VS/9 OS for UNIVAC Series 90 mainframes.

Following the System/38 debut in 1979, the majority of RPG developers transitioned from relying on the cycle to employing conventional loop structures, although IBM ensured continued support for legacy code.

DE/RPG

DE/RPG (Data Entry RPG) was designed exclusively for IBM's IBM 5280 data-entry terminals during the early 1980s. It resembled RPG III but differed by lacking external Data Descriptions (DDS); instead, DDS definitions had to be embedded directly within the RPG source.

RPG/400

Essentially representing RPG III adapted for AS/400 environments, RPG/400 was IBM's rebranded RPG compiler for that platform. Initially identical to the System/38 RPG III compiler, IBM later introduced new operation codes such as SCAN, CAT, and XLATE specifically for RPG/400, thereby extending its capabilities beyond those of the original System/38 version.

RPG IV and ILE RPG

Launched in 1994 alongside OS/400 V3R2 (now known as IBM i), RPG IV—also called RPG ILE—marked a departure from treating RPG as merely an acronym. It enhanced expression handling with Extended Factor-2 Calculation Specifications and subsequently incorporated free-format Calculation Specifications and Procedure syntax.

Within the Integrated Language Environment (ILE), this iteration is referred to as ILE RPG, with IBM offering thorough documentation for users. Both RPG IV and ILE RPG continue to be supported on contemporary IBM i systems.

The OS/400 V5R1 release in 2001 introduced free-format syntax for calculation specifications, enabling more flexible coding styles akin to those found in popular programming languages. Initially limited to calculation specs, this free format was fully embraced by IBM i V7R1 TR7, removing the traditional column-based coding restrictions.

For RPG development, IBM advocates using Rational Developer for i (RDi), an Integrated Development Environment built on Eclipse. The older Source Entry Utility (SEU) editor has been deprecated since IBM i 6.1. Other legacy tools include CODE/400 and VisualAge for RPG.

Ongoing language development

IBM persistently enhances the RPG language through regular software updates and intra-release “technology refreshes” (TRs). Numerous built-in functions (BIFs) have been incorporated, and RPG now integrates with Java objects and IBM i APIs. It supports creating CGI applications using IBM's Cgidev2 Web toolkit, the RPG Toolbox, and various other commercial web-enablement solutions. Despite these advancements, RPG retains extensive backward compatibility, enabling programs written decades ago to operate with minimal changes.

The SQL precompiler empowers RPG programmers to utilize IBM’s cost-based SQE (SQL Query Engine). Rather than defining access paths via traditional F-Specs, developers can embed standard SQL statements directly into their code. The precompiler then translates these SQL commands into RPG calls that communicate with the database manager to execute queries.

While based on the EBCDIC character set, RPG IV also supports UTF-8, UTF-16, among others. Its thread-safe architecture assigns each thread a dedicated static storage area instead of making the runtime environment re-entrant, creating a hybrid threading model blending characteristics of threads and processes.

In 2010, IBM released RPG Open Access (also known as Rational Open Access: RPG Edition), which allows developers to create custom I/O handlers for data sources not natively supported by RPG. This feature significantly expands RPG’s integration potential within modern IT infrastructures.

Data types

RPG accommodates the following categories of data types.

Note: In the data type column, the character is specified in the Definition Specification at column 40 of a fixed-format source line, identified by D in column 6. Unlike free-form languages such as C that utilize keywords (e.g., int), RPG assigns variables through designated data type characters. When no character is provided, the default is A if decimals are not indicated, P for standalone fields including decimals, and S (ZONED) within data structures.

Data type Designation Size Explanation
A Alphanumeric character Fixed length: 1 to 16,773,104 bytes
Variable length: 1 to 16,773,100 bytes
Alphanumeric character data
B Binary numeric 1, 2, 4, or 8 bytes Signed binary integer value
C UCS-2 character Fixed length up to 8,386,552 characters
Variable length up to 8,386,550 characters
16-bit UCS-2 encoded character (DBCS or EGCS)
D Date 10 bytes Date format including year, month, and day
F Floating point numeric 4 or 8 bytes Signed floating-point real number in binary
G Graphic character Fixed length up to 8,386,552 characters
Variable length up to 8,386,550 characters
16-bit graphic character (DBCS or EGCS)
I Integer numeric 1, 2, 4, or 8 bytes Signed binary integer type
N Character indicator 1 byte '1' signifies TRUE
'0' signifies FALSE
O Object reference Size unspecified Reference to an object instance
P Packed decimal numeric From 1 to 63 digits,
two digits per byte plus sign nibble
Signed fixed-point decimal number with integer and fractional parts
S Zoned decimal numeric From 1 to 63 digits,
one digit per byte
Signed fixed-point decimal number including integer and fraction digits
T Time 8 bytes Time representation with hour, minute, and second components
U Integer numeric (unsigned) 1, 2, 4, or 8 bytes Unsigned binary integer value
Z Timestamp 26 bytes Date and time combined: year, month, day, hour, minute, second, microseconds included
* Based-Pointer
Procedure-Pointer
System-Pointer
16 bytes Memory address pointing to data
Address of an active procedure
Reference to an object entity

Example code

Presented below is a program that accepts a customer number as input and outputs the corresponding name and address through parameters. This sample demonstrates the original RPG IV syntax; subsequent versions introduced more contemporary syntax and relaxed constraints.

      * RPG traditionally used column-based formatting with some allowance for free-format.
      * The letter in column 6 specifies the line's role.
      * Lines starting with an asterisk (*) in column 7 are comments.

      * File specifications ("F") define files and I/O devices.
     F ARMstF1   IF   E       K     Disk    Rename(ARMST:RARMST)

      * Data specifications ("D") declare variables.
     D pCusNo          S              6p
     D pName           S             30a
     D pAddr1          S             30a
     D pAddr2          S             30a
     D pCity           S             25a
     D pState          S              2a
     D pZip            S             10a

      * Calculation specifications ("C") contain executable code.
      * Parameters are defined using plist and parm operations.
     C     *entry        plist
     C                   parm                    pCusNo
     C                   parm                    pName
     C                   parm                    pAddr1
     C                   parm                    pAddr2
     C                   parm                    pCity
     C                   parm                    pState
     C                   parm                    pZip

      * The "chain" operation performs random access on a keyed file.
     C     pCusNo        chain     ARMstF1

      * If a record is found, transfer fields into parameters.
     C                   if        %found
     C                   eval      pName  = ARNm01
     C                   eval      pAddr1 = ARAd01
     C                   eval      pAddr2 = ARAd02
     C                   eval      pCity  = ARCy01
     C                   eval      pState = ARSt01
     C                   eval      pZip   = ARZp15
     C                   endif

      * Setting "LR" indicates program and dataspace can be unloaded from memory.
     C                   eval      *InLR = *On  

The identical program utilizing free-form calculations introduced in V5R1:

      * File specifications ("F") declare files and I/O devices.
     FARMstF1   IF   E        K     Disk    Rename(ARMST:RARMST)

      * Data specs ("D") define variables and parameters; prototypes are maintained separately for external calls.
      /copy cust_pr

      * Procedure interface defines parameters for *ENTRY point.
     D getCustInf      PI
     D  pCusNo                        6p 0   const
     D  pName                        30a
     D  pAddr1                       30a
     D  pAddr2                       30a
     D  pCity                        25a 
     D  pState                        2a 
     D  pZip                         10a 

      /free 
        // Randomly access keyed file via chain command 
        chain pCusNo ARMstF1;

        // When record exists, assign database fields to output parameters 
        if %found;
           pName = ARNm01;
           pAddr1 = ARAd01;
           pAddr2 = ARAd02;
           pCity = ARCy01;
           pState = ARSt01;
           pZip = ARZp15;
        endif;

        // Activate LR flag indicating program can be removed from memory 
        *InLR = *On; 
      /end-free 

Supposing the ARMSTF1 table was established as follows:

<syntaxhighlight lang="sql"> create table armstf1 (

arcnum decimal(7,0),
arname char(30),
aradd1 char(30),
aradd2 char(30),
arcity char(25),
arstte char(2),
arzip char(10)

) </syntaxhighlight>

The same routine employing free calculations combined with embedded SQL:

      * RPG IV eliminates the requirement for the *INLR indicator to end a program.
      * Declaring MAIN on header spec identifies the entry procedure enabling more "C-style" logic.
     H MAIN(getCustInf)
      /copy cust_pr

     P getCustInf      B  
     D getCustInf      PI  
     D   pCusNo                       6p 0   const  
     D   pName                       30a  
     D   pAddr1                      30a  
     D   pAddr2                      30a  
     D   pCity                       25a  
     D   pState                      2a  
     D   pZip                        10a  

      /free  
        exec sql select arName, arAddr1, arAdd2, arCity, arStte, arZip  
                 into :pName, :pAddr1, :pAddr2, :pCity, :pState, :pZip  
                 from ARMstF1 where arCNum = :pCusNo for fetch only fetch first row only with CS;  
      /end-free  

     P GetCustInf      E  

Starting with OS version V7R1, prototypes may be included within the same source member:

<syntaxhighlight lang="sql"> H main(GetCustInf) D ARMSTF1 E DS

P GetCustInf B D GetCustInf PI extpgm('CUS001') D inCusNo like(arCNum) const D outName like(arName) D outAddr1 like(arAdd1) D outAddr2 like(arAdd2) D outCity like(arCity) D outState like(arStte) D outZip like(arZip)

/free  
  exec sql select arName, arAdd1, arAdd2, arCity, arStte, arZip   
           into :outName, :outAddr1, :outAddr2, :outCity, :outState, :outZip   
           from ARMSTF1   
           where arCNum = :inCusNo   
           fetch first row only   
           with CS use currently committed;  
/end-free  

P GetCustInf E </syntaxhighlight>

With Technology Refresh 7 (TR7) applied on OS V7.1 systems, programs can be written entirely in free-form:

<syntaxhighlight lang="sql"> ctl-opt main(GetCustInf);

dcl-ds ARMSTF1 ext end-ds;

dcl-proc GetCustInf;

dcl-pi *n extpgm('CUS001');
  inCusNo like(arCNum) const;
  outName like(arName);
  outAddr1 like(arAdd1);
  outAddr2 like(arAdd2);
  outCity like(arCity);
  outState like(arStte);
  outZip like(arZip);
end-pi;
exec sql select arName, arAdd1, arAdd2, arCity, arStte, arZip 
         into :outName, :outAddr1, :outAddr2,:outCity,:outState,:outZip 
         from ARMSTF1 
         where arCNum = :inCusNo 
         fetch first row only 
         with CS use currently committed;
return;

end-proc; </syntaxhighlight>

Further reading

  • 9PAC, Report Generator, History of Programming Languages (HOPL), Murdoch University, Australia, 2006
  • RPG, Report Program Generator, History of Programming Languages (HOPL), Murdoch University, Australia, 2006
  • DuCharme, Bob. "Extracting Data from Computers in the Mid-20th and Early 21st Centuries," February 26, 2006
  • Cozzi, Robert. The Contemporary RPG IV Language, 1996
  • Shelly, Gary B., and Thomas J. Cashman. Introduction to Computer Programming RPG, Anaheim Publishing Company, 1977

External links

  • Smith, Brian R., et al. "Discovering Advanced Techniques with RPG IV: A Sorcerer's Guide to System Access and Beyond," IBM International Technical Support Organization, April 25, 2000 — Highlights RPG IV as a modern development language suited for 21st-century applications.
  • Midrange.com — Comprehensive collection of RPG programming examples
  • RPGPGM.COM — Repository of articles featuring RPG code snippets and programming tools
  • RPG Open — Open-source materials supporting RPG IV and IBM i software development
  • IBM 1401 RPG manual (1964)
  • ILE RPG Programmer's Guide (2016)
  • RPG II for MVS, OS/390 and z/OS — Overview of the IBM RPG II product status on z/OS platforms