Algol 60 Compilation and Assessment
Total Page:16
File Type:pdf, Size:1020Kb
Algol 60 Compilation and Assessment B. A. Wichmann August 1973 ii Preface The ease and effectiveness of computer utilisation depend very heavily on the quality of the implementation of the programming language used. This book is dedicated to raising the quality of ALGOL 60 implementations. It gives an objective comparative assessment of over twenty existing compilers in accordance with numerical and non- numerical criteria, and describes in greater detail the techniques which contribute to high quality. The book is required reading for any programmer embarking on an implementa- tion of ALGOL 60; and is equally valuable for implementors of many subsequently designed languages. For users of ALGOL 60, it will not only assist in the selection of a high quality compiler, but it also shows how to use an existing compiler more effectively. Finally, for all designers and users of programming languages and other software, it gives an example of how the quality of software may be objectively de- fined and assessed. The first chapter considers the compilation of the more straightforward aspects of ALGOL, by using an illustrative one-address computer. It is clear that most inter- comparisons must depend upon statistical information of the use of ALGOL 60 and this is presented and analysed in chapter 2 to make it useful for performance assessment. These statistical data also give guide lines on the construction of efficient algorithms, which are detailed in chapter 3. The problem of syntax analysis of ALGOL 60 source text is not considered in de- tail, as this is handled very ably elsewhere. However, some difficulties are enumerated in chapter 4, since a few existing compilers are known to handle these matters incor- rectly. As far as the user is concerned, the diagnostic features are all-important. If he cannot understand these diagnostics, or inexplicably gets the wrong answers, it is of little consequence that the compiler is “efficient” in terms of machine resources. These aspects are discussed in chapter 5. A related topic is the documentation of computer software, or in this case, ALGOL 60 source text. Several automatic aids and compiling options are considered. In chapter 7, the compilation of the obscure points of ALGOL 60 is discussed. In many cases, these features are omitted from an implementation, not necessarily because of their difficulty, but because they are rarely used. A related problem is that of demonstrating the apparent correctness of a compiler. Certainly the known trouble- spots can be the subject of explicit tests, which, to be reasonably effective, must amount to about 100 programs. The design of such tests is described in chapter 8. A detailed comparison of six existing compilers is made in chapter 9: for Atlas, KDF9, 1900, CDC 6600, B5500 and the 1108. The range of architecture enables a judgement to be made of the desirable features of a machine for good ALGOL object code. Numerous examples are given from test programs to illustrate the points made. The improvement of the object code in a compiler by various optimisation strategies is discussed in chapter 10. Simple local optimisation is compared with more ambitious schemes. This analysis leads naturally to what must be regarded as minimal machine features to support ALGOL 60 and the reasoning behind this is presented in chapter 11. The implementation of ALGOL 60 implies various mechanisms within both the compiler and object-code. These mechanisms are capable of supporting features which are not present in ALGOL 60, and are hence candidates for extensions to the language. This leads to a discussion on language design in the final chapter which is arranged in iii order of increasing complexity from minor extensions to a brief analysis of ALGOL W and ALGOL 68. I should like to thank all the “ALGOL men” with whom I have had much useful discussion, enabling this book to be written. Professor C. A. R. Hoare, Mr R. Scowen and Mr M. Woodger have contributed substantially to my understanding of ALGOL 60. Dr P. T. Cameron and Mr J. Wielgosz have contributed to some of the work reported in chapter 9, and Dr C. Phelps to that in chapter 2; this collaboration has proved very useful and I am glad to acknowledge their support. I should also like to thank Mr D. S. Akka for permission to quote from his M.Sc. Thesis. This work would not have been possible without the permission of the Director of the National Physical Laboratory to use the published reports and papers upon which this book is largely based. Any opinions expressed however, are mine, and not neces- sarily those of the Laboratory. Finally, I should like to thank my wife for her support in the preparation of the book. August 1973 B.A.W. Logical order of the chapters 1 - 5 n n@ @ R@ 6 - 7 n n@ @ R@ 4 - 8 n n@ @ R@ 2 - 9 - 11 - 12 n@ n@ n n @ @ R@ R@ 3 10 n n iv Contents 1 The Generation of Machine-Code 3 1.1 Arithmetic Expressions . 3 1.1.1 SIMPLE ARITHMETIC EXPRESSIONS . 3 1.1.2 ANONYMOUS WORKING STORE . 5 1.1.3 TYPE CONVERSION . 6 1.1.4 OPERATORS NOT AVAILABLE ON THE HARDWARE OF THE MACHINE . 7 1.1.5 NESTED EXPRESSIONS . 10 1.2 Array Variables . 11 1.2.1 THE “ARRAY WORD” . 11 1.2.2 THE DOPE VECTOR TECHNIQUE . 11 1.2.3 THE CODE WORD METHOD . 12 1.2.4 THE STORAGE LAYOUT . 12 1.2.5 MACHINE CODE EXAMPLES . 13 1.2.6 SUBSCRIPT BOUND CHECKING . 17 1.3 Constants . 18 1.4 Boolean Expressions . 19 1.4.1 THE RELATIONAL OPERATORS . 19 1.5 Conditional Statements and Expressions . 21 1.6 Assignment Statements . 22 1.7 For Loops . 24 1.7.1 MULTIPLE FOR LIST ELEMENTS . 25 1.7.2 THE STEP-UNTIL ELEMENT . 26 1.7.3 THE WHILE ELEMENT . 29 1.7.4 THE GENERAL CASE . 29 1.8 Storage allocation . 30 1.8.1 BLOCK LEVEL ADDRESSING . 31 1.8.2 PROCEDURE LEVEL ADDRESSING . 32 1.8.3 FIRST AND SECOND ORDER WORKING STORE . 34 1.8.4 LINK DATA AND THE USE OF REGISTERS . 39 1.9 Procedure and Function Calls . 40 1.9.1 PARAMETERLESS PROCEDURE ENTRY AND EXIT . 40 1.9.2 THUNKS . 43 1.9.3 VALUE PARAMETERS . 44 1.9.4 STRING PARAMETERS . 46 1.9.5 ARRAY PARAMETERS . 47 1.9.6 LABEL PARAMETERS . 47 1.9.7 STANDARD FUNCTIONS . 48 v vi CONTENTS 1.9.8 SIDE-EFFECTS . 48 1.10 Name Call . 49 1.10.1 THE GENERAL CASE . 50 1.10.2 OPTIMISATION WITH SIMPLE VARIABLES . 51 1.10.3 REAL-INTEGER TYPE CONVERSION . 52 1.11 Switches and Gotos . 52 1.11.1 LABELS . 52 1.11.2 DESIGNATIONAL EXPRESSIONS . 53 1.11.3 SWITCHES . 54 2 Performance Measurement 55 2.1 General Techniques . 55 2.2 Statement Times . 56 2.2.1 TIMING THE SIMPLE STATEMENTS . 56 2.2.2 CHOICE OF THE STATEMENTS . 58 2.2.3 THE ACTUAL TIMES OBSERVED . 58 2.2.4 ANALYSIS OF THE TIMES—A MODEL . 58 2.2.5 THE LIMITATIONS AND USES OF THE MODEL . 60 2.2.6 SOME REMARKS ON THE OBSERVED TIMES . 61 2.3 Program Analysis—General Techniques . 68 2.3.1 WHETSTONE ALGOL DYNAMIC ANALYSIS . 69 2.3.2 WORK OF D. S. AKKA . 78 2.3.3 STATIC ANALYSIS OF SOURCE TEXT . 79 2.3.4 STATIC ANALYSIS OF COMPILED CODE . 84 2.4 An ALGOL Performance Measure . 85 2.4.1 EXPANSION OF THE SIMPLE STATEMENTS . 87 2.4.2 CALCULATION OF THE WEIGHTS . 87 2.4.3 COMPARISON OF THE MIX FIGURES . 91 2.5 Benchmarks . 93 3 Estimating the Speed of a Program 95 3.1 Arithmetic expressions . 95 3.2 Assignment statements . 97 3.3 Relational operators and Boolean expressions . 97 3.4 Declarations . 98 3.5 For loops . 99 3.6 Procedure and function calls . 100 3.6.1 EXAMPLES . 100 3.7 Access via Formal Parameters . 101 3.8 Labels, designational expressions and switches . 101 3.9 Use and accuracy of the estimation . 101 3.10 Tabular Summary of the Estimates . 102 4 Syntax Analysis 105 4.1 Common Syntactic Errors . 105 4.1.1 ERRORS IN SYNTAX ONLY . 105 4.1.2 ERRORS EFFECTING SEMANTICS . 106 4.2 Output from the Syntax Analysis . 107 4.2.1 INFORMATION REQUIRED FOR CODE GENERATION . 108 CONTENTS vii 5 Diagnostic and Debugging Aids 111 5.1 General Techniques . 111 5.2 Compile-time Diagnostics . 111 5.2.1 COMPILING OPTIONS . 114 5.2.2 COMPILE-TIME CHECKING . 115 5.3 Run-time Checking . 115 5.3.1 SUBSCRIPT CHECKING . 116 5.3.2 OVERFLOW AND UNDERFLOW . 116 5.3.3 STACK OVERFLOW . 117 5.3.4 USE BEFORE ASSIGNMENT . 118 5.3.5 ADDITIONAL CHECKS . 119 5.3.6 TRACING . 119 5.4 Failure Time Diagnostics . 120 5.4.1 FAILURE INFORMATION . 120 5.4.2 INFORMATION RELATED TO THE FAILURE . 121 5.4.3 PROGRAM STATUS INFORMATION . 122 5.4.4 THE KIDSGROVE ALGOL POST-MORTEM FACILITY . 123 5.4.5 FAILURE STATISTICS . 124 5.5 Operating System Features . 125 6 Program Documentation Aids 127 6.1 Source Text Methods . 127 6.2 Compiler-oriented Documentation Aids . 129 6.3 Execution Aids . 130 6.4 Machine-code Analysis . 132 7 Trouble-spots in the Compilation Process 135 7.1 The Dimensions of Formal Arrays . 135 7.2 Formal Procedure Parameters . 136 7.3 Arrays By Value . 137 7.4 Subscripted Variable as a Control Variable . 138 7.5 Function and Name Calls .