Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Richards M.The BCPL Cintcode and Cintpos user guide.2005

.pdf
Скачиваний:
16
Добавлен:
23.08.2013
Размер:
798.85 Кб
Скачать

The BCPL Cintcode and Cintpos

User Guide

by

Martin Richards

mr@cl.cam.ac.uk

http://www.cl.cam.ac.uk/~mr/

Computer Laboratory

University of Cambridge

Revised version (under development): September 12, 2005

Abstract

BCPL is a simple systems programming language with a small fast compiler which is easily ported to new machines. The language was ¯rst implemented in 1967 and has been in continuous use since then. It is a typeless and provides machine independent pointer arithmetic allowing a simple way to represent vectors and structures. BCPL functions are recursive and variadic but, like C, do not allow dynamic free variables, and so can be represented by just their entry addresses. There is no built-in garbage collector and all input-output is done using library calls.

This document describes the new revised version of the BCPL Cintcode System giving a de¯nition of the language, its library and running environment. It also describes a native code version of the system and the Cintpos portable operating system. Installation instructions are included.

Keywords

Systems programming language, Typeless language, BCPL, Cintcode, Coroutines, Cintpos.

2

Contents

Preface

 

v

1

The System Overview

1

 

1.1

A Console Session . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

2

The BCPL Language

9

 

2.1

Language Overview . . . . . . . . . . . . . . . . . . . . . . . . . .

10

 

 

2.1.1 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

 

 

2.1.2 The GET Directive . . . . . . . . . . . . . . . . . . . . . . .

11

 

 

2.1.3 Conditional Compilation . . . . . . . . . . . . . . . . . . .

11

 

 

2.1.4 Section Brackets . . . . . . . . . . . . . . . . . . . . . . .

11

 

2.2

Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

2.2.1Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.2.2Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.2.3Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2.2.4Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.2.5Pre¯xed Expression Operators . . . . . . . . . . . . . . . . 14

2.2.6 In¯xed Expression Operators . . . . . . . . . . . . . . . . 15

2.2.7Boolean Evaluation . . . . . . . . . . . . . . . . . . . . . . 16

2.2.8 VALOF Expressions . . . . . . . . . . . . . . . . . . . . . . 16

2.2.9Expression Precedence . . . . . . . . . . . . . . . . . . . . 16

2.2.10 Manifest Constant Expressions . . . . . . . . . . . . . . .

17

2.3 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

2.3.1Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.3.2Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.3.3Conditional Commands . . . . . . . . . . . . . . . . . . . . 18

2.3.4 Repetitive Commands . . . . . . . . . . . . . . . . . . . . 19

2.3.5SWITCHON command . . . . . . . . . . . . . . . . . . . . . . 19

2.3.6Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . 20

2.3.7Compound Commands . . . . . . . . . . . . . . . . . . . . 20

2.3.8Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.4Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.4.1Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

i

ii

CONTENTS

2.4.2Manifest Declarations . . . . . . . . . . . . . . . . . . . . . 21

2.4.3Global Declarations . . . . . . . . . . . . . . . . . . . . . . 22

2.4.4Static Declarations . . . . . . . . . . . . . . . . . . . . . . 22

2.4.5

LET Declarations . . . . . . . . . . . . . . . . . . . . . . .

23

2.4.6

Local Variable Declarations . . . . . . . . . . . . . . . . .

23

2.4.7

Local Vector Declarations . . . . . . . . . . . . . . . . . .

23

2.4.8Procedure Declarations . . . . . . . . . . . . . . . . . . . . 23

2.4.9 Dynamic Free Variables . . . . . . . . . . . . . . . . . . . 25

2.5Separate Compilation . . . . . . . . . . . . . . . . . . . . . . . . . 25

3 The Library

27

3.1 Manifest constants . . . . . . . . . . . . . . . . . . . . . . . . . .

27

3.1.1Rootnode . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3.1.2Other manifest constants . . . . . . . . . . . . . . . . . . . 32

3.1.3Input/Output system manifests . . . . . . . . . . . . . . . 36

3.2Global vector variables . . . . . . . . . . . . . . . . . . . . . . . . 37

3.3Global vector entry points . . . . . . . . . . . . . . . . . . . . . . 38

3.3.1SYSLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

3.3.2sys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

3.3.3Interpreter Management . . . . . . . . . . . . . . . . . . . 38

3.3.4 Primitive I/O Operations . . . . . . . . . . . . . . . . . . 40

3.4BLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.4.1 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 43

3.4.2 Stream Input/Output . . . . . . . . . . . . . . . . . . . . 44

3.4.3Input Functions . . . . . . . . . . . . . . . . . . . . . . . . 46

3.4.4Output Functions . . . . . . . . . . . . . . . . . . . . . . . 46

3.4.5 The Filing System . . . . . . . . . . . . . . . . . . . . . . 48

3.4.6File Deletion and Renaming . . . . . . . . . . . . . . . . . 49

3.4.7Non Local Jumps . . . . . . . . . . . . . . . . . . . . . . . 49

3.4.8 Command Arguments . . . . . . . . . . . . . . . . . . . . 49

3.4.9Program Loading and Control . . . . . . . . . . . . . . . . 52

3.4.10Character Handling . . . . . . . . . . . . . . . . . . . . . . 54

3.4.11Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

3.4.12

Hamming's Problem . . . . . . . . . . . . . . . . . . . . .

57

3.4.13

Scaled Arithmetic . . . . . . . . . . . . . . . . . . . . . . .

59

4 The Command Language

61

4.1Bootstrapping single threaded BCPL . . . . . . . . . . . . . . . . 61

4.2Bootstrapping Cintpos . . . . . . . . . . . . . . . . . . . . . . . . 63

4.2.1The Cintpos BOOT module . . . . . . . . . . . . . . . . . 63

4.2.2klibstart . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

4.3

Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

4.4

CLI.b and CLI

 

INIT.b . . . . . . . . . . . . . . . . . . . . . . . .

76

CONTENTS

iii

5 The Debugger

81

6 The design of OCODE

85

6.1

Representation of OCODE . . . . . . . . . . . . . . . . . . . . . .

85

6.2

The OCODE Abstract Machine . . . . . . . . . . . . . . . . . . .

86

6.3

Loading and Storing values . . . . . . . . . . . . . . . . . . . . . .

87

6.4

Expression operators . . . . . . . . . . . . . . . . . . . . . . . . .

88

6.5

Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

89

6.6

Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

6.7

Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

92

6.8Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

7 The Design of Cintcode

95

7.1Designing for Compactness . . . . . . . . . . . . . . . . . . . . . . 96

7.1.1 Global Variables . . . . . . . . . . . . . . . . . . . . . . . 97

7.1.2Composite Instructions . . . . . . . . . . . . . . . . . . . . 98

7.1.3Relative Addressing . . . . . . . . . . . . . . . . . . . . . . 98

7.2 The Cintcode Instruction Set . . . . . . . . . . . . . . . . . . . . 99

7.2.1Byte Ordering and Alignment . . . . . . . . . . . . . . . . 99

7.2.2 Loading values . . . . . . . . . . . . . . . . . . . . . . . . 101

7.2.3Indirect Load . . . . . . . . . . . . . . . . . . . . . . . . . 102

7.2.4Expression Operators . . . . . . . . . . . . . . . . . . . . . 102

7.2.5Simple Assignment . . . . . . . . . . . . . . . . . . . . . . 103

7.2.6

Indirect Assignment . . . . . . . . . . . . . . . . . . . . .

104

7.2.7

Procedure calls . . . . . . . . . . . . . . . . . . . . . . . .

104

7.2.8Flow of Control and Relations . . . . . . . . . . . . . . . . 105

7.2.9Switch Instructions . . . . . . . . . . . . . . . . . . . . . . 106

7.2.10Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . 107

7.2.11Unde¯ned Instructions . . . . . . . . . . . . . . . . . . . . 108

7.2.12Corruption of B . . . . . . . . . . . . . . . . . . . . . . . . 108

7.2.13Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

8 Installation

111

8.1Linux Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

8.2Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . 112

8.3 Installation on Other Machines . . . . . . . . . . . . . . . . . . . 113

8.4Installation for Windows 95/98/NT . . . . . . . . . . . . . . . . . 113

8.5Installation for Windows CE2.0 . . . . . . . . . . . . . . . . . . . 114

8.6The Native Code Version . . . . . . . . . . . . . . . . . . . . . . . 114

9 Example Programs

115

9.1Coins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

9.2Primes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

iv

CONTENTS

9.3Queens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

9.4Fridays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

9.5 Lambda Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . 119

9.6Fast Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . 124

Bibliography

127

A BCPL Syntax Diagrams

129

Preface

The concept for BCPL originated in 1966 and was ¯rst outlined in my PhD thesis [4]. Its was ¯rst implemented early in 1967 when I was working at M.I.T. Its heyday was perhaps from the mid 70s to the mid 80s, but even now it is still continues to be used at some universities, in industry and by private individuals. It is a useful language for experimenting with algorithms and for research in optimizing compilers. Cintpos is the multi-tasking version of the system based on the Tripos [5]. It is simple and easy to maintain and can be used for real-time applications such as process control. BCPL was designed many years ago but is still useful in areas where small size, simplicity and portability are important.

This document is intended to provide a record of the main features of the BCPL in su±cient depth to allow a serious reader to obtain a proper understanding of philosophy behind the language. An e±cient interpretive implementation is presented, the source of which is freely available via my home page [3]. The implementation is machine independent and should be easy to transfer to almost any architecture both now and in the future.

The main topics covered by this report are:

²A speci¯cation of the BCPL language.

²A description of its runtime library and the extensions used in the Cintpos system.

²The design and implementation of command language interpreters for both the single and multi-threaded versions of the system.

²A description of OCODE, the intermediate code used in the compiler, and Cintcode, the compact byte stream target code used by the interpreter.

²A description of the single and multi-threaded interactive debugger and other debugging aids.

²The e±cient implementation of the Cintcode interpreter for several processors including both RISC and i386/Pentium based machines.

²The pro¯ling and statistics gathering facilities o®ered by the system.

v

vi

CONTENTS

Chapter 1

The System Overview

This document contains a full description of an interpretive implementation of BCPL that supports a command language and low level interactive debugger. As an introduction, an example console session is presented to exhibit some of the key features of the single threaded version of the system.

1.1A Console Session

When the system is started (on a machine called meopham) in the directory bcplprogs/demo, its opening message is as follows:

meopham$ cinterp

BCPL Cintcode System (11 June 2004) 0>

The characters 0> are followed by a space character and is the command language prompt string inviting the user to type a command. The integer gives the execution time of the preceeding command. A program to compute factorials can be displayed using the type command as follows:

> type fact.b GET "libhdr"

LET start() = VALOF

{FOR i = 1 TO 5 DO writef("fact(%n) = %i4*n", i, fact(i)) RESULTIS 0

}

AND fact(n) = n=0 -> 1, n*fact(n-1) 0>

The directive GET "libhdr" causes the standard library declarations to be inserted at that position. The text:

1

2

CHAPTER 1. THE SYSTEM OVERVIEW

LET start() = VALOF

is the heading for the declaration of the function start which, by convention, is the ¯rst function to be called when a program is run. The empty parentheses () indicate that the routine expects no arguments. The text

FOR i = 1 TO 5 DO

introduces a for-loop whose control variable i successively takes the values from 1 to 5. The body of the for-loop is calls the library routine writef whose e®ect is to output the format string after replacing the substitution items %n and %i4 by appropriately formatted representations of i and fact(i). Within the string *n represents the newline character. The statement RESULTIS 0 exits from the VALOF construct providing the result of start that indicates the program completed successfully. The text:

AND fact(n) =

introduces the de¯nition of the function fact which take one argument (n) and yields n factorial. The word AND causes fact to available to the previously de¯ned function. This program can be compiled by using the following command:

10> bcpl fact.b to fact

BCPL (10 June 2004) Code size = 104 bytes 10>

This command compiles the source ¯le fact.b creating an executable object module in the ¯le called fact. The program can then be run by simply typing the name of this ¯le.

10> fact

 

fact(1) =

1

fact(2) =

2

fact(3) =

6

fact(4) =

24

fact(5) =

120

0>

 

When the BCPL compiler is invoked, it can be given additional arguments that control the compiler options. One of these (d1) directs the compiler to output the compiled code in a readable form, as follows:

10> bcpl fact.b to fact d1

BCPL (10 June 2004)

0:DATAW 0x00000000

4: DATAW 0x0000DFDF