Margaret Jacobs Phd Thesis
Total Page:16
File Type:pdf, Size:1020Kb
TRANSLATION OF APL TO OTHER HIGH-LEVEL LANGUAGES Margaret M. Jacobs A Thesis Submitted for the Degree of PhD at the University of St Andrews 1975 Full metadata for this item is available in St Andrews Research Repository at: http://research-repository.st-andrews.ac.uk/ Please use this identifier to cite or link to this item: http://hdl.handle.net/10023/13417 This item is protected by original copyright The code generated corresponding to a particular APL routine will not at first be very efficient. However, methods of optimising the generated code are discussed at length in the thesis. A brief comparison is made with other possible methods of conversion. There are certain restrictions on the types of APL statements able to be handled by the translation method. These restrictions a/re listed in an accompanying appendix. Throughout the text, several examples are given of the code which will be generated from particular APL statements or expressions. Some more lengthy examples of conversion of APL routines to FORTRAN are provided as an appendix. TRANSLATION OP APL TO OTHER HIGH-LEVEL LANGUAGES MARGARET M. JACOBS ,\x ABSTRACT The research work required to produce this thesis was carried out in the Department of Computational Science, University of St. Andrews. Financial assistance was provided by the Science Research Council. The thesis describes a method of translating the computer language APL to other high-level languages. Particular reference is made to FORTRAN, a language widely available to computer users. Although gaining in popularity, APL is not at present so readily available, and the main aim of the translation process is to enable the more desirable features of APL to be at the disposal of a far greater number of users. The translation process should also speed up the running of routines, since compilation in general leads to greater efficiency than interpretive techniques. Some inefficiencies of the APL language have been removed by the translation process. The above reasons for translating APL to other high-level languages are discussed in the introduction to the thesis. A description of the method of translation forms the main part of the thesis. The APL input code is first lexically scanned, a process whereby the subsequent phases are greatly simplified. An intermediate code form is produced in which bracketing is used to group operators and operands together, and to assign priorities to operators such that sub-expressions will be handled in the correct order. By scanning the intermediate code form, information is stacked until required later. The information is used to make possible a process of macro expansion. Each of the above processes is discussed in the main text of the thesis. The format of all information which can or must be supplied at translation time is clearly > outlined in the text. To MY HUSBAND AND PARENTS A description of a method of translating APL into other high-level languages, with particular reference to FORTRAN. The work for this project was carried out in the Department of Computational Science, University of St. Andrews, for the degree of Ph.D. The project commenced on the 10th October, 1971* The research for the subject matter of this thesis has been carried out by myself, and the thesis has been composed by myself. The thesis has not been accepted in fulfilment of the requirements of any other degree or professional qualification. ACKNOWLEDGEMENTS I would like to thank Professor A.J. Cole for his excellent supervision of my work throughout the course of my studies in St. Andrews and for his advice on the preparation of my thesis. I am extremely grateful to all the members of staff of the John Honey Building at the University of St. Andrews for their willing assistance and encouragement, and to my fellow research students for their helpful comments. I also must thank Mrs. V. Butterworth for typing the text of my thesis, and the Science Research Council for their financial assistance. CONTENTS page INTRODUCTION 1 Chapter I INPUT PHASE AND METHOD OF STORAGE ALLOCATION ' 5 II LEXICAL SCANNING- PHASE 31 III RIGHT-TO-LEFT SCAN AND PRODUCTION OF INTERMEDIATE CODE 59 IV LEFT-TO-RIGHT SCAN, PRODUCTION OF STACKED INFORMATION 76 AND ORGANISATION OF MACRO EXPANSIONS V THE MACRO METHOD 109 VI LABELS AND JUMPS 154 VII PROCESSING OF INITIAL INFORMATION I 61 VIII CODE OPTIMISATION 1?1 Appendix 1 SYMBOLS USED FOR INPUT OF APL DURING TESTING 2 TABLE OF USEFUL INFORMATION FOR APL OPERATORS 3 LIST OF MACRO INSTRUCTIONS AND THEIR FUNCTIONS 4 LIST OF MACRO BODIES 5 COMPARISON OF BRACKETING METHOD AND REVERSE POLISH METHOD 6 RESTRICTIONS 7 EASE OF CONVERSION TO OTHER LANGUAGES 8 FUNCTION OF GLOBAL VARIABLES (RUN-TIME) 9 EXAMPLES OF CONVERTED ROUTINES 10 DESCRIPTION OF FINITE-STATE AUTOMATON FOR REMOVAL OF UNNECESSARY "FIND" CALLS INTRODUCTION The following text describes a method of translation of APL to other high-level languages. The version of APL able to be translated is that described in the IBM APL 360-OS and APL 360-DOS User's Manual, with a few restrictions. These restrictions are listed in Appendix 6. Throughout the text, the target language is assumed to be FORTRAN, but similar techniques can be applied to translate from APL to ALGOL or to Pl/1. In generating the target language code, only a subset of the permissible FORTRAN statements has been used. The subset was chosen such that its members (as far as possible) have counterparts in ALGOL and Pl/1. This facilitates conversion to either of these languages instead of FORTRAN. The ease of conversion to ALGOL or to Pl/1 is discussed in Appendix 7• The translation was intended in the first place to handle conversion of APL subroutines and functions, but main programs may also be translated. The APL routines are not intended to remain interactive after conversion, but to be run under a batch-processing system. There have previously been some attempts to produce a batch-processor for APL. One such attempt was made by H. Van Hedel, who implemented an APL batch-process or in Pl/1 for the IBbl/36st). The only restriction he imposed was that function names and local variable names should be distinct. (This restriction, among others, has been placed on the types of APL state- 7 ments able to be converted to FORTRAN.) In Van Hedel the following example is quoted: 2 V r <-fi x R <~@XV V&1 ; F1 H V V G2 hV Yh Z <- F1 -A V Such an example creates ambiguity in the source text, for H returns differ ent values in G-1 and G-2 . For an interactive interpreter it is important that each operation is executed as soon as sufficient information is gathered. For a batch- processor, as much as possible of the analysis has to be done before the execution takes place. The above example is ambiguous to a compiler, but not to an interpreter. It is intended that only working routines be converted to other languages. Thus, the amount of checking required during conversion is greatly reduced. It can be assumed, for example, that all dimensions are conformable in matrix operations. The reasons behind the translation (see Sayers^) are as follows; (i) It is intended to provide a more easily transportable system. There are at present more FORTRAN compilers than APL interpreters. Since APL is highly suited to the development of algorithms (Smillie^), it would be very convenient to be able to use these algorithms on a larger scale. This would be possible if the AFL routines wore translated to FORTRAN. (The 3 same argument can be applied to the translation of APL to ALG-OL or PL/1. ) To make transport of the converted routines as convenient as possible, the user is provided with an option to specify the output medium for the converted routines. (ii) A secondary aim was to improve run-time efficiency by using compilation rather than interpretation. The amount of code to be interpreted is reduced if the user supplies some information about non-scalar variables. The more information supplied, the greater the amount of compilation possible. In many cases, the types and dimensions of variables will not change, and such examples readily lend themselves to the improvement of run-time effic iency. The method of supplying extra information to improve run-time efficiency is described in Chapter I. (iii) It is hoped that code can be optimised during the course of translation by the removal of some of the inefficiencies of APL. An example of an inefficient APL expression is 4 4* A+B where A and B are non-scalar. This is obviously inefficient as all the elements of A and B are summed, whereas only four summations are essential. The method of removing the above inefficiency is outlined in the following text. In December, 1971, V.L.Moruzzi gave a set of simple rules for translating from APL to FORTRAN by hand. He estimated that mechanical APB/FORTRAN trans- ■ lations could achieve a 3,^-fold reduction in CPU time. This is discussed in . 3 Moruzzx . At a private meeting, Dr. J.L.Alty of Liverpool University remarked that, after visiting various APL installations in the U.S.A. and Canada, he found APL three to four times faster than-other languages for program development, but one hundred times slower for execution. This result, he stated, emphasized the need for interchangeability between APL and other languages. The translation from APL to FORTRAN is effected by a series of macro expansions. The order of expansion of macros is determined by the order of the operators in the APL source test.