The BCPL Cintsys and CintposUser GuidebyMartin [email protected]://www.cl.cam.ac.uk/users/mr10/Computer LaboratoryUniversity of CambridgeRev
2 CHAPTER 1. THE SYSTEM OVERVIEWLET start() = VALOFis the heading for the declaration of the function start which, by convention, isthe first function
92 CHAPTER 3. THE LIBRARYAND messcofn(node) = VALOF{ qitem(node) // Put the message on the work queue for this node{ // Start processing the first mes
3.7. COROUTINE EXAMPLES 93writef("*nCosim entered*n*n")writef("Network nodes: %n*n", nodes)writef("Stop time: %n*n", sto
94 CHAPTER 3. THE LIBRARY3.8 The Gr a phi cs Libra r yThe graphics library provides facilities for drawing pictures and outputi ng them to file.This li
3.8. THE GRAPHICS LIBRARY 953.8.3 The Graphics Functionsopengraphics(xsize, ysize) CIN:y, POS:y, NAT:yThis function opens the graphic s library. It al
96 CHAPTER 3. THE LIBRARYfillrect(x0, y0, x1, y1) CIN:y, POS:y, NAT:yThis function dr aws the rectangle (x0, y0), (x 1, y0), ((x1, y1), (x1, y0) filled
3.10. THE GL GRAPHICS LIBRARY 97GET "libhdr"MANIFEST { g_sdlbase=nnn } // Only used if the default setting of 450 in// libhdr is not suitabl
98 CHAPTER 3. THE LIBRARY3.11 The Sound Lib ra r yThis library is under developmentThe sound library uses the sys(Syssound,...) fun ct i ons to provid
Chapter 4The Command LanguageThe Command Language Interpreter (CLI) is a simple interactive interface between theuser an d the system. It loads and ex
100 CHAPTER 4. THE COMMAND LANGUAGEno return link has been stored i nto the stack, this call of start must not attemptto return in the normal way; how
4.2. BOOTSTRAPPING CINTPOS 101hold the dumped Cintcode registers. A result of zero, signifying successful completion,causes execution of Cintsys to te
1.1. A CINTSYS CONSOLE SESSION 38: DATAW 0x6174730B12: DATAW 0x2020747216: DATAW 0x20202020// Entry to: start20: L1:20: L121: SP322: L4:22: LP323: LF
102 CHAPTER 4. THE COMMAND LANGUAGEAll console input and output within BOOT and the standalone debugger is doneusing the standalone version of rdch an
4.2. BOOTSTRAPPING CINTPOS 103startroot is entered by the recursive call of interpret from BOOT with a newstack and a different global vector from that
104 CHAPTER 4. THE COMMAND LANGUAGEIt always starts execution at the start of the function irqrtn wit h Cintcode registerST set to 3 to indicate that
4.3. COMMANDS 105or global vector. There are thus 16 posible states a task can have of which only sixindicate that it is runnable, they are as follows
106 CHAPTER 4. THE COMMAND LANGUAGEadjclock OFFSET CIN:y, POS:y, NAT:yThe syntax of the OFFSET argument is [-][h][:m], that is: an optional minus sign
4.3. COMMANDS 107causes a line to be output by the com pi l e r for each non local identifier occurring in t heprogram. A typical such line is as follo
108 CHAPTER 4. THE COMMAND LANGUAGEThe HARD options causes both syntax and translation phase errors to callabort(100). This is useful in commands such
4.3. COMMANDS 109bgpm FROM,TO/K,UPB/K CIN:y, POS:y, NAT:yThis is an implementation of Chr i s toph er Strachey’s GPM macrogenerator. It takesinput fro
110 CHAPTER 4. THE COMMAND LANGUAGEbin-hex FROM/A,TO/K CIN:y, POS:y, NAT:yThis outputs the bytes of the FROM in hex. For instance, if the file xxx wasA
4.3. COMMANDS 111% Comment - skip all characters unti l a non white space ch ar-acter on a later input line.[Start of a new macro call.!Argument separ
4 CHAPTER 1. THE SYSTEM OVERVIEWthe debugger. The data word at location 4 holds a special bit pattern indicatingthe presence of a func ti o n name pla
112 CHAPTER 4. THE COMMAND LANGUAGEbounce CIN:n, POS:y, NAT:nThis command is part of the bounce demonstration that is only available underCintpos. It
4.3. COMMANDS 113casech FROM/A,TO/A,DICT/K,U/S,L/S,A/S CIN:y, POS:y, NAT:yThis command systematically converts all reserved words of a BCPL program to
114 CHAPTER 4. THE COMMAND LANGUAGEwise it is sent to the standard output stream. The MSECS options causes the time tohave higher precision. Typical o
4.3. COMMANDS 115endcli CIN:n, POS:y, NAT:nThis Cintpos command causes a CLI task to commit suicide.enlarge /A,TO/K CIN:y, POS:y, NAT:yThis command ou
116 CHAPTER 4. THE COMMAND LANGUAGEhexdump FROM/A,N,P,RL/K,RLB/K,TO/K CIN:y, POS:y, NAT:yThis program dumps a file specified by FROM in a combination of
4.3. COMMANDS 117input TO/A,TERM/K CIN:y, POS:y, NAT:yThis command will copy text from the current input sending it the the file specifiedby the AS argu
118 CHAPTER 4. THE COMMAND LANGUAGEkeyword. BLOCKS outputs a list of all blocks whether allocated or free in the blockchain used by getvec. CODE outpu
4.3. COMMANDS 119mkjunk NAME,SIZE/N CIN:y, POS:y, NAT:yThis creates a file as described in the mkdata command and then tests randomaccess to this file b
120 CHAPTER 4. THE COMMAND LANGUAGEprmcode CIN:y, POS:y, NAT:yThis command converts an MCODE (intermediate code for MCPL) file specified byFROM to a mor
4.3. COMMANDS 121This is command differs from fail since it terminates the execution of a completecommand-command while fail allows a command-command t
1.1. A CINTSYS CONSOLE SESSION 5This first loads the entry point of clihook (held in global va r ia b l e 4) and sets(b1) a breakpoint numbered 1 at th
122 CHAPTER 4. THE COMMAND LANGUAGEThe COUNT argument allows the user to specify how many Cintcode instr uct i ons toobey for each raster line. The de
4.3. COMMANDS 123SYNTRNCGGET StreamDeclaration VectorCompiled Code BufferCode OutputStackInput streamSYN Parse Tree TRN Parse Tree CG Parse TreeOCODE
124 CHAPTER 4. THE COMMAND LANGUAGECintsys and Cintpos logical variables are held in a linked list held in the rootnodeelement rtn envlist. If both NA
4.3. COMMANDS 125stats TO/K,PROFILE/S,ANALYSIS/S CIN:y, POS:y, NAT:yThis command controls the tallying facili ty which counts the ex e cut i on of i n
126 CHAPTER 4. THE COMMAND LANGUAGEtcpdump CIN:n, POS:y, NAT:nThis outputs the list of Cintpos TCP/IP devices that c ur r e ntly exist. The listinclud
4.3. COMMANDS 127vecstats CIN:y, POS:y, NAT:yThis command output information about blocks of Cintcode memory that are cur-rently allocated. Typical ou
128 CHAPTER 4. THE COMMAND LANGUAGEresulting file. Either FILE or LIST or both must be supplied. If given FILE is the firstfilename to be encoded followe
4.4. CLI.B AND CLI INIT.B 129clifaillevel CIN:y, POS:y, NAT:yclidata CIN:y, POS:y, NAT:yThis holds CLI data dependant on the context in which the CLI
130 CHAPTER 4. THE COMMAND LANGUAGEclibit noprompt Do not output prompts even when not in a command-command.clibiteofdel Delete this task when EOF is
Chapter 5Console Input and OutputWhen cintsys or cintpos is started a stream i s opened to recei ve input from standardinput which is normally the key
6 CHAPTER 1. THE SYSTEM OVERVIEW* \ A= 3 B= 1 46279: LF 46332* \ A= fact B= 3 46281: K9* \ A= 3 B= 3 46332: JNE0 46336* \ A= 3 B= 3 46336: LM1* \ A= -
132 CHAPTER 5. CONSOLE INPUT AND OUTPUT5.2 Cintpos console strea m sUnder Cintpos interaction with the console is somewhat more compli c at ed since C
5.2. CINTPOS CONSOLE STREAMS 133Sequence Purpose@A Set flag 1 in the currently selected task@BSet flag 2 in the currently selected task@CSet flag 3 in th
134 CHAPTER 5. CONSOLE INPUT AND OUTPUT5.2.2 Exclusive InputThe console handler can be set to exclusive input mode by the call:sendpkt(notinuse, conso
Chapter 6Cintpos De vi cesCintpos allows asynchronous communication with peripheral devices using the qpktand taskwait functi ons. If the pkt id field
136 CHAPTER 6. CINTPOS DEVICES6.0.5 The Keyboard DeviceThis device has i d entifier -2 and is currently not treated specially, and so it has a DCB,and
137Tcprcvbufsz arg1: sock arg2: szThis sets the receive buffer size of the given socket to size bytes. A zero resu l tindicates success.Tcpbind arg1: s
138 CHAPTER 6. CINTPOS DEVICESa specified timeout p er i od. If timeout is greater than zero it i s the timeout period inmilli-seconds, if it is zero t
Chapter 7The DebuggerBoth Cintsys and Cintpos have interactive debuggers but these are slightly differentand so wi l l be described separately.7.1 The
140 CHAPTER 7. THE DEBUGGER* ?? Print list of debug commandsGn Pn Rn Vn VariablesG P R V Pointersn #b101 #o377 #x7FF ’c Constants*e /e %e +e -e |e &am
7.1. THE CINTSYS DEBUGGER 141* 10sv1 11sv2 Put 10 and 11 in variables 1 and 2* vt5 Display the first 5 variablesV 0: 0 10 11 0 0** v1*50+v2= 511 A calc
1.2. A CINTPOS CONSOLE SESSION 7This shows that the total size of th e compiler is 34,576 bytes and t h at it can becompiled (on a 1.6GHz Pentium mach
142 CHAPTER 7. THE DEBUGGERAt this point the fi rs t instruction of rdargs is about to be executed. Its return addressis in P1, so a breakpoint can be
7.2. THE CINTPOS DEBUGGER 143Command Effect. Select current coroutine, Display next stack frame; Select parent coroutine[ Select first coroutine] Select
144 CHAPTER 7. THE DEBUGGERclock device) interfere with single ste ppi ng of Cintcode instructions, the K command isprovided to turn clock interrupts
Chapter 8The Design of OCODEBCPL was designed to be a portab l e language wit h a compiler that is easily transfe r r edfrom machine to machine. To he
146 CHAPTER 8. THE DESIGN OF OCODEexample, if the file test.b is the following:GET "libhdr"LET start() BE { LET a, b, c = 1, 0, -1writef(&quo
8.3. LOADING AND STORING VALUES 147to S in the description of OCODE s tat em ents should be regarded as a specification ofS for the subsequent statemen
148 CHAPTER 8. THE DESIGN OF OCODEStatement MeaningLF Ln P!S := entry point Ln; S := S+1LN nP!S := n; S := S+1FNUM m eP!S := <make float>(m, e);
8.5. EXPRESSION OPERATORS 149but if op is non zero it repr es ents and assignment operator (assop) and the statementis equivalent to:SLCT len:sh:0 OF
150 CHAPTER 8. THE DESIGN OF OCODEStatement MeaningGETBYTE S := S-1; P!(S-1) := P!(S-1) % P!SMULTS := S-1; P!(S-1) := P!(S-1) * P!SFMULTS := S-1; P!(S
8.6. FUNCTIONS AND ROUTINES 151ENTRY Li n C1. . . CnSAVE sbody of function or routineENDPROCLi is the label alloc ate d for the entry point. As a debu
8 CHAPTER 1. THE SYSTEM OVERVIEWThere is a benchmark program cal l ed bench100.b which can be compiled andrun as follows.0.000 1> c bc bench100bcpl
152 CHAPTER 8. THE DESIGN OF OCODEE1 E2 En ESPOld stack frame New stack framekFigure 8.4: The moment of calling E(E1,E2,...En)8.7 ControlThe statement
8.9. DISCUSSION 153Static variables and tables are allocated space in the program area using statementsof the form ITEMN n, where n is the ini ti al v
154 CHAPTER 8. THE DESIGN OF OCODE
Chapter 9The Design of CintcodeThe original version of Cintcode was a byte stream interpretive code designed to beboth compact and capable of efficient
156 CHAPTER 9. THE DESIGN OF CINTCODEThe registers A and B are used for expression evaluation, and C is used in in bytesubscription. P and G are point
9.1. DESIGNING FOR COMPACTNESS 157OperationExecutions Static countLoading a local var ia b l e 3777408 1479Updating a local variable1965885 1098Loadin
158 CHAPTER 9. THE DESIGN OF CINTCODELGLG1LGHbhB := A; A := G!(b+256)B := A; A := G!bB := A; A := G!hbHere, b and h are unsigned 8 and 16 bit values,
9.2. THE CINTCODE INSTRUCTION SET 159command. The use of a 16 bit resol v i ng word places a slight restriction on the maximumsize of relative referen
160 CHAPTER 9. THE DESIGN OF CINTCODE0 32 64 96 128 160 192 2240 - K LLP L LP SP AP A1 FLTOP KH LLPH LH LPH SPH APH AH2 BRK KW LLPW LW LPW SPW APW AW3
9.2. THE CINTCODE INSTRUCTION SET 161compile code with the opp osi t e endianess to that of the machine on which the compileris running, see the descr
1.2. A CINTPOS CONSOLE SESSION 9Notice that the root CLI (task 1) completes the execution of the run com m a n dand issues a prompt (0.000 1>) befo
162 CHAPTER 9. THE DESIGN OF CINTCODELLP b B := A; A := @P!bLLPH h B := A; A := @P!hLLPW w B := A; A := @P!wLLG b B := A; A := @G!bLLG1 b B := A; A :=
9.2. THE CINTCODE INSTRUCTION SET 163These in st ru ct i ons provide for all the normal arithmetic and bit pattern dyadic opera-tors. The instructions
164 CHAPTER 9. THE DESIGN OF CINTCODE9.2.6 Indirect AssignmentPBYT B%A := CXPBYT A%B := CST A!0 := BSTn 1 ≤ n ≤ 3 A!n := BST0Pn 3 ≤ n ≤ 4 P!n!0 := AST
9.2. THE CINTCODE INSTRUCTION SET 165Kn 3 ≤ n ≤ 11K bKH hKW wThese instructi ons call the function or routine whose e ntry point is in A and whose firs
166 CHAPTER 9. THE DESIGN OF CINTCODE9.2.8 Flow of Co ntrol and RelationsThe following instruction s are used in the compilation of conditional and un
9.2. THE CINTCODE INSTRUCTION SET 167dlab to jump to the default label. See Section 9.1.3 for details on how resolving halfwords are interpreted.SWB fi
168 CHAPTER 9. THE DESIGN OF CINTCODEThese instructions are used move val ue s between register A, B and C.NOPThis instruction has no effect.SYSThis in
9.2. THE CINTCODE INSTRUCTION SET 1699.2.11 Floating-point InstructionsFloating-point operations other than those performed by SELST are provided by t
170 CHAPTER 9. THE DESIGN OF CINTCODE9.2.14 Corruption of BTo i mpr ove the efficiency of some hand writte n machine code interpreters, the followingins
Chapter 10The Design of SialSial is an internal intermediate assembly language designed for BCPL. The first versionwas called Cial ( Compac t Internal
10 CHAPTER 1. THE SYSTEM OVERVIEW}task, count := 7, 1_000_000IF argv!0 DO task := !argv!0IF argv!1 DO count := !argv!1pkt!0, pkt!1, pkt!2 := notinuse,
172 CHAPTER 10. THE DESIGN OF SIALThis can be converted into something slightly more readable using the command:sial-sasm hello.sial to * giving: This
10.1. THE SIAL SPECIFICATION 173call *%eax# L K0xorl %ebx,%ebx# RTNmovl 4(%ebp),%eaxmovl 0(%ebp),%ebpjmp *%eax# STRING M9001 K6 C72 C101 C108 C108 C11
174 CHAPTER 10. THE DESIGN OF SIALF An opcode or dir e c ti veP A stack offset, 0 to #xFFFFFFG A global variable number, 0 to 65535K A 24-bit unsi gned
10.1. THE SIAL SPECIFICATION 175lkp Kk Pn a := P!n!klkg Kk Gn a := G!n!krv a := ! arvp Pn a := P!n!arvk Kn a := a!kst !a := bstp Pn P!n!a := bstk Kn a
176 CHAPTER 10. THE DESIGN OF SIALlsh a := b << arsh a := b >> aand a := b & aor a := b | axor a := b XOR aeqv a := b EQV agbyt a := b
10.1. THE SIAL SPECIFICATION 177brk Breakpoint instructionnop No operationchgco Change coroutinemdiv a := muldiv(P!3, P!4, P!5)sys System functionsect
178 CHAPTER 10. THE DESIGN OF SIALjfeq Ln Jump to Ln if b #= a; b := ?jfne Ln Jump to Ln if b #~= a; b := ?jfls Ln Jump to Ln if b #< a; b := ?jfgr
10.1. THE SIAL SPECIFICATION 179C41 C32 C61 C32 C37 C105 C52 C10F113 K4 C102 C97 C99 C116F111 L2F92 L5F11 K1F77F111 L5F12 K1F16 P3F69F9 L2F31 P4F73 P3
180 CHAPTER 10. THE DESIGN OF SIALLF L2K P4ATBLP P3MULRTNGLOBAL K1G1 L1G94MODEND10.2 The sial-386 TranslatorThe source of an unoptimi se d Sial transl
10.2. THE SIAL-386 TRANSLATOR 181# SP P9movl %ebx,36(%ebp)# LP P3movl 12(%ebp),%ebx# SP P8movl %ebx,32(%ebp)# LSTR M1leal MA1,%ebxshrl $2,%ebx# KPG P4
Chapter 2The BCPL LanguageThe design of BCPL owes much to th e work done on CPL (originally CambridgeProgramming Language) which was conceived at Camb
182 CHAPTER 10. THE DESIGN OF SIALLA2:movl %ebp,0(%edx)movl %edx,%ebppopl %edxmovl %edx,4(%ebp)movl %eax,8(%ebp)movl %ebx,12(%ebp)# JNE0 L5orl %ebx,%e
10.2. THE SIAL-386 TRANSLATOR 183When implementing sial-386 it was necessary to decide how t he Intel regis te r swere to be used and what the BCPL ca
184 CHAPTER 10. THE DESIGN OF SIALThe call cvfpg("KPG") reads the Sial stateme nt knowing it is of the form: KPGPk Gn. This outputs the stat
10.3. COMPACTION OF SIAL 18510.3 Compaction of SialIn order to transmit program to a device such as a mobile phone or s pac e probe over aslow connect
186 CHAPTER 10. THE DESIGN OF SIAL
Chapter 11The MC PackageThis chapt er describes the MC package which provides a machine independent way togenerate and execut e native machine code at
188 CHAPTER 11. THE MC PACKAGEMANIFEST {A=mc_a; B=mc_b; C=mc_c; D=mc_d; E=mc_e; F=mc_fa1=1; a2; a3}LET start() = VALOF{ // Load the dynamic code gener
11.1. MC EXAMPLE 189// MV A,A1movl 20(%ebp), %eax573: 8B 45 14// ADD A,A2addl 24(%ebp), %eax576: 03 45 18// LAB L1lab L1579: L1:// ADD A,A3addl 28(%eb
190 CHAPTER 11. THE MC PACKAGEFunction OperandsmcF No operandmcK One integer operandmcR One MC register operandmcA One operand specifying an argument
11.2. MC LIBRARY FUNCTIONS 191mcRK Two operands, R and KmcAK Two operands, A and KmcVK Two operands, V and KmcGK Two operands, G and KmcMK Two operand
2
12 CHAPTER 2. THE BCPL LANGUAGEvariant of GPM, called BGPM, is included in the standard BCPL distribution.BCPL was initially similar to this subset of
192 CHAPTER 11. THE MC PACKAGEmcRK(mc_mv, A, #x10000000)mcPRF("With D=%8x ", D)mcPRF("A=%8x ", A)mcPRF("B=%8x*n", B)mcR(
11.3. THE MC LANGUAGE 193When an MC function is decl ar ed it has a specified number of arguments and localvariables (see the ENTRY statement below). W
194 CHAPTER 11. THE MC PACKAGEALIGND KAlign the next item of data to an address which is a multiple of k which must be2, 4 or 8.AND RA RV RG RM RL RD
11.3. THE MC LANGUAGE 195DEC R A V G M L D DX DXs DXsBDecrement the specified register or memory word by 1, leaving the condition codeundefined.DIV K R
196 CHAPTER 11. THE MC PACKAGEJLT JS JL JRJump to the spec i fie d location if the first operand of a previous CMP instruction wasless than its second o
11.3. THE MC LANGUAGE 197RK AK VK GK MK LK DK DXK DXsK DXsBKMove the zero extended 16-bit val ue specified by the second operand into the first.LEA RA R
198 CHAPTER 11. THE MC PACKAGESEQ RSet the specified register to one if the first operand of a previous CMP instructionwas equal to its second operand,
11.3. THE MC LANGUAGE 199UJGE JS JL JRJump to the speci fie d location if the first operand of a previous CMP instruction wasgreater than or equal to it
200 CHAPTER 11. THE MC PACKAGE11.4 MC Debugging AidsThe primary debugging aid is to inspect the generated code and the is controlled bythe DEBUG direc
11.5. THE N-QUEENS DEMONSTRATION 201UNLESS rdargs("mc,lo/n,hi/n,-c/s,-m/s,-a/s,-b/s", argv, 50) DO{ writef("Bad arguments for mcqueens*
2.1. LANGUAGE OVERVIEW 132.1.2 The GET DirectiveA directives of the form GET "filename" is replaced by the contents of the namedfile. Ear l y
202 CHAPTER 11. THE MC PACKAGE}AND gencode(n) BE{ LET all = (1<<n) - 1mcKKK(mc_entry, n, 3, 0)mcRK(mc_mv, ld, 0)mcRK(mc_mv, col, 0)mcRK(mc_mv, r
11.5. THE N-QUEENS DEMONSTRATION 203mcRR(mc_add, col, p) // col := col+pmcRR(mc_add, rd, p)mcRK(mc_rsh, rd, 1) // rd := (rd+p)>>1cmpltry(i+1, n,
204 CHAPTER 11. THE MC PACKAGE
Chapter 12InstallationThe implementation of BCPL described in this report is freely available via my HomePage [3] to individuals for private use and t
206 CHAPTER 12. INSTALLATIONSome web browsers will have already decompressed the .tgz file, so you may haveuse the following command instead:cd distrib
12.1. LINUX INSTALLATION 207to the end of the file .bashrc in your home directory so the the BCPL environmentvariables are automatically setup whenever
208 CHAPTER 12. INSTALLATIONc compall6) The BCPL programs that are part of the system are: boot.b, blib.b, dlib andcli.b. These reside i n BCPL/cintco
12.3. INSTALLATION ON OTHER MACHINES 209-m n Set the Cintcode memory size to n words.-t n Set the tally vector size to n words.-s Enter the Cintcode s
210 CHAPTER 12. INSTALLATIONI installed the fre e l y available .NET Framework 3.5 and the corresponding SDK 3.5.This provided amongst many other thi
12.6. INSTALLATION FOR WINDOWS CE2.0 21112.6 Installation for Windows CE2.0A version of the BCPL Cintcode System is available for handheld machines ru
14 CHAPTER 2. THE BCPL LANGUAGE2.1.4 Section BracketsHistorically BCPL used the symbols $( and $) to bracket commands and decla-rations. These symbols
212 CHAPTER 12. INSTALLATION
Chapter 13Example Programs13.1 CoinsThe following program prints out how many different ways a sum of money can becomposed from coins of various denomi
214 CHAPTER 13. EXAMPLE PROGRAMS13.2 PrimesThe following program prints out a table of all primes less than 1000, using the sie vemethod.GET "lib
13.4. FRIDAYS 215LET start() = VALOF{ all := 1FOR i = 1 TO 16 DO{ count := 0try(0, 0, 0)writef("Number of solutions to %i2-queens is %i9*n",
216 CHAPTER 13. EXAMPLE PROGRAMS13.5 Lambda EvaluatorThe following program is a simple parser and evaluator for lambda expressions.GET "libhdr&qu
13.5. LAMBDA EVALUATOR 217// *************** Syntax analyser ***********************// Construct Corresponding Tree// a ,.., z --> [Id, ’a’] ,.., [
218 CHAPTER 13. EXAMPLE PROGRAMSAND lex() BE SWITCHON ch INTO{ DEFAULT: writef("Bad ch in lex: %c*n", ch)CASE Eof: token := EofRETURNCASE ’
13.5. LAMBDA EVALUATOR 219AND prim() = VALOF{ LET a = TABLE Num, 0SWITCHON token INTO{ DEFAULT: writef("Bad expression*n"); ENDCASECASE Id:
220 CHAPTER 13. EXAMPLE PROGRAMSAND try(expr) BE{ LET v = VEC 2000space := v+2000writef("Trying %s*n", expr)writef("Answer: %n*n",
13.6. FAST FOURIER TRANSFORM 221LET start() = VALOF{ writef("fft with N = %n and omega = %n modulus = %n*n*n",N, omega, modulus)data := getv
2.2. EXPRESSIONS 15The following are exam p l es of valid nu mbers:12341_234_456#B_1011_1100_0110#o377#X3fff#x_DEADC0DESince August 2014, floating poin
222 CHAPTER 13. EXAMPLE PROGRAMSAND reorder(v, n) BE{ LET j = 0FOR i = 0 TO n-2 DO{ LET k = n>>1// j is i with its bits is reverse orderIF i<
Bibliography[1] D . T. Ross et al. AED-0 programmer’s guide and user k it. Technical report, Elec-tronic Systems Laboratory M.I.T, 1964.[2] C. Jobson
224 BIBLIOGRAPHY
Appendix ABCPL Syntax DiagramsThe syntax of standard B CPL is specified using the transition diagrams given in fig-ures A.1, A.2, A.3 and A.4. In extend
226 APPENDIX A. BCPL SYNTAX DIAGRAMSand not{ IF i>10 DO i := i/2 } REPEATUNTIL i<5A useful property of these diagrams is that, once a test box h
227nameSTATICMANIFEST = E0 ; }D{};E0:nameGLOBALLET nameAND=nameVEC E0,,= E0,( name,)BE C= E0{Figure A.2: Declarations
228 APPENDIX A. BCPL SYNTAX DIAGRAMSGOTOTESTFOR name THENUNLESSIFE0 INTO,E0:=:=E0E0is callis name : CCASEDEFAULTE0E0 TO E0 BY E0DOCE0 THENDOC ELSE CE0
229TRUEEn?FALSEnamenumbercharacterstring!@+−ABS(NOTTABLE,E0E8E5E4E3E2E1E0 , E0CE0,E3E5E7))E0VALOFE4( n<9! n<8* n<6/ n<6+ n&
230 APPENDIX A. BCPL SYNTAX DIAGRAMS
16 CHAPTER 2. THE BCPL LANGUAGESLCT shift:offset means SLCT 0:shift:offsetSLCT offset means SLCT 0:0:offsetCharacter constants consist of a si n gl e char
2.2. EXPRESSIONS 17Escape Replacement*n A newline (end-of-line) character.*c A carriage return character.*p A newpage (form-feed) character.*s A space
18 CHAPTER 2. THE BCPL LANGUAGElength and bytes of the string are packed. If s is a str i n g t h en s%0 is its lengthand s%1 is its first character, s
2.2. EXPRESSIONS 19Here, E1points to the fields of an object, wit h the convention th at its ze-roth field (E1!0) i s a pointer to the methods vector. E
20 CHAPTER 2. THE BCPL LANGUAGEwhen a field is updated. Suppose p!3 holds the value #x12345678, then after theassignment:(SLCT 12:8:3) OF p := 1 + (SLC
2.2. EXPRESSIONS 212.2.8 VALOF ExpressionsAn expression of the form VALOF C, wh er e C is a command, is evaluated byexecuting the command C. On encoun
ContentsPreface v1 The Sy s tem Overview 11.1 A Cintsys Console Sessio n . . . . . . . . . . . . . . . . . . . . . . 11.2 A Cintpos Console Session .
22 CHAPTER 2. THE BCPL LANGUAGENotice that these precedence values imply that! f x means ! (f x)! @ x means ! (@ x)! v ! i ! j means ! ((v!i)!j)@ v !
2.3. COMMANDS 23used, the appropriate 8 bit location is updated by the least significant 8 bits ofE.If the left hand side is of the form S OFE then the
24 CHAPTER 2. THE BCPL LANGUAGEwhere E denotes an expression and C1and C2denote commands. The symbolsDO and THEN may be omitted whenever they are foll
2.3. COMMANDS 252.3.6 Flow of Co ntrolThe following commands affect the flow of control.RESULTIS ERETURNENDCASELOOPBREAKGOTO EFINISHRESULTIS causes eval
26 CHAPTER 2. THE BCPL LANGUAGEIn extended BCPL compilerd by xbcpl a more binding version of the sequenc-ing operator is a allowed. It is the symbol &
2.4. DECLARATIONS 272.4.2 Manifest DeclarationsA MANIFEST declaration has the following form:MANIFEST { N1= K1;...; Nn= Kn}where N1,...,Nnare names (s
28 CHAPTER 2. THE BCPL LANGUAGEwhere N1,...,Nnare names (see Section 2.2.1) and K1,...,Knare manifestconstant express io n s (see Section 2.2.10). Eac
2.4. DECLARATIONS 29where N is a name and K is a manifest constant expression. A location isallocated for N and initialized to a vector whose lower bo
30 CHAPTER 2. THE BCPL LANGUAGE(@x)!1, (@x)!2,. . . where x is t he first argument. This feature is useful in thedefinition of functions, such as writef
2.5. SEPARATE COMPILATION 31restriction is exemplified below.GLOBAL { var:200 }LET f1(...) BE{ LET oldvar = var // Save the current value of varvar :=
ii CONTENTS2.4.1 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.4.2 Manifest Declarati o n s . . . . . . . . . . . . . . . . . .
32 CHAPTER 2. THE BCPL LANGUAGEWhen these sections are l oa d ed , global 200 is initialized to the entry point offunction f defined in demolib.b and s
Chapter 3The LibraryThis manual describes three var i ants of the BCPL system. The simplest is in-voked by the shell command cintsys and provides a si
34 CHAPTER 3. THE LIBRARYMost implementations use pack 4 bytes into 32-bit words requiring B2Wsh=2, but on64-bit imple me ntations, such as native cod
3.1. MANIFEST CONSTANTS 35function name is too long its first and last five character are packed into the stringseparated by a single quote ’. Typically
36 CHAPTER 3. THE LIBRARYrtn. . . CIN:y, POS:y, NAT:yThe ro ot node is a vector accessible to all running programs to provide access toall global info
3.1. MANIFEST CONSTANTS 37rtnclwkq CIN:n, POS:y, NAT:nUnder Cintpos, this field is used to holds the ordered list of packets waiting to bereleased by t
38 CHAPTER 3. THE LIBRARYrtnhdrsvar CIN:y, POS:y, NAT:nThis field holds the name of the environment variable givin g the di r ec t or i es holdingBCPL
3.1. MANIFEST CONSTANTS 39rtnmembase, rtn memsize CIN:y, POS:y, NAT:nThese rootnode fie l ds hold, respectively, the star t of the memory block chain a
40 CHAPTER 3. THE LIBRARYrtnvecstatsv CIN:y, POS:y, NAT:nThis points to a vector holding counts of how many blocks of each requested sizehave been all
3.1. MANIFEST CONSTANTS 41scbid CIN:y, POS:y, NAT:nThis SCB field holds one of the values idinscb, id outscb or id inoutscb, indi-cating whether the st
CONTENTS iii4.4 cli.b and cliinit.b . . . . . . . . . . . . . . . . . . . . . . . . 1285 Console Input and Output 1315.1 Cintsys console streams . . .
42 CHAPTER 3. THE LIBRARYscbtnet, scbt file, scbt ram, scbt console or scbt mbx, scbt tcp. The last threehave strictly positive values causing output
3.2. GLOBAL VARIABLES 43modules hold the relocatable byte stream interpretive code used by all BCPL interpre-tive systems. Constants with names ending
44 CHAPTER 3. THE LIBRARYmaincobusy CIN:n, POS:y, NAT:nThis is a variable used i n the implementation of gomultievent under Cintpos.multicount CIN:n,
3.3. GLOBAL FUNCTIONS 45defined i n the standard re si de nt library. Most of these are defined in BCPL in the files:sysb/klib.b, sysb/blib.b and sysb/dl
46 CHAPTER 3. THE LIBRARYch := capitalch(ch) CIN:y, POS:y, NAT:yThis function converts l owercase letters to uppercase, leaving other characters un-ch
3.3. GLOBAL FUNCTIONS 47Decimal range GB2312 bytes0 < dd < 127 <dd>128 < xxyy < 9494 <xx+160> <yy+160>res := compch(ch1,
48 CHAPTER 3. THE LIBRARYnext resumes execution. The parent link points to the coroutine that called this one,or is zero if the coroutine not active.
3.3. GLOBAL FUNCTIONS 49The definition of createco is in blib.b and is as follows.LET createco(fn, size) = VALOF{ LET c = getvec(size+6)UNLESS c RESULT
50 CHAPTER 3. THE LIBRARYP2 L2 fn sz c P1 L1 0 cK9G 24 cowait(c)LP3J -7 } REPEATK6 fn( ... )LP5 {PCPThe new coroutinecoroutine ch
3.3. GLOBAL FUNCTIONS 51This call causes the time stamp in datv t o be converted to three strings v, v+5 andv+10. The string at v is set to the date i
iv CONTENTS9.2.8 Flow of Control and Relations . . . . . . . . . . . . . . . . 1669.2.9 Switch Instructions . . . . . . . . . . . . . . . . . . . . .
52 CHAPTER 3. THE LIBRARYblock (TCB) is unlinked from the priority chain and removed from tasktab. Finallyits segment list and the TCB itself return e
3.3. GLOBAL FUNCTIONS 53name is the string "*" then it opens the standard output stream which is normally tothe screen. If the file name is r
54 CHAPTER 3. THE LIBRARYcptr := initco(fn, size,a,b,c,d,e,f,g,h,i,j,k) CIN:y, POS:y, NAT:yThis function provides a convenient method of creating and
3.3. GLOBAL FUNCTIONS 55within the cur re nt function or routine can be performed using the GOTO command, solevel and longjump are only needed for non
56 CHAPTER 3. THE LIBRARYnewline() CIN:y, POS:y, NAT:yThis simply outputs the newline character (’*n’) to the currently selected outputstream.newpage(
3.3. GLOBAL FUNCTIONS 57destination task or device (specified by pktid!pkt). If this fiel d is positive it refer s toa task, if it is -1 it r e fe r s t
58 CHAPTER 3. THE LIBRARYwhere the decoded integer is stored. If a /N was not specified, the setti n g will be aBCPL string with its characters packed
3.3. GLOBAL FUNCTIONS 59ch := rdch() CIN:y, POS:y, NAT:yThis call reads the next character from the currently selected input stream. If thestream is e
60 CHAPTER 3. THE LIBRARYres := release(taskid) CIN:n, POS:y, NAT:nThis Cintpos function wi l l clear the HOLD bit in the spe ci fi ed task thus making
3.3. GLOBAL FUNCTIONS 61selectoutput(scb) CIN:y, POS:y, NAT:yThis r ou ti ne selects scb as the currently selected output stream. It aborts (withcode
PrefaceThe concept for BCPL originated in 1966 and was first outlined in my PhDthesis [4]. Its was first implemented early in 1967 when I was working at
62 CHAPTER 3. THE LIBRARYclireturncode and cli result2 where they can be inspected by commands such asif and why.n := str2numb(str) CIN:y, POS:y, NAT:
3.3. GLOBAL FUNCTIONS 63res := sys(Syscallc, c name2port, a1) CIN:y, POS:y, NAT:yThe name or decimals of a port is given in a1 and the resul t is its
64 CHAPTER 3. THE LIBRARYres := sys(Syscallc, c fd select, a1, a2, a3, a4, a5) CIN:y, POS:y, NAT:yThis inspects bit a1 in the bit vector a2. The resul
3.3. GLOBAL FUNCTIONS 65time of the last modification of the file given by name returning TRUE if successful.The first element datv!0 holds the number of
66 CHAPTER 3. THE LIBRARYthe negated value of a where a is a floating point number. The last four calls performfloating point multi p l i cat i on, divi
3.3. GLOBAL FUNCTIONS 67res := sys(Sysflt, fl modf, a)res := sys(Sysflt, fl fmod, x, y) CIN:y, POS:y, NAT:yThe first call returns the fractional part (
68 CHAPTER 3. THE LIBRARYstr := sys(Sysgetprefix) CIN:y, POS:y, NAT:yThis returns a pointer to prefix st r i ng which is in space allocated when Ci nts
3.3. GLOBAL FUNCTIONS 69directory. If a val i d module is not found ther e and name is a relative file name, itsearches through the directories specifie
70 CHAPTER 3. THE LIBRARYthat needs to be modified by the addition of the base address of the hunk. The codet bhunk is similar to t hunk only the data
3.3. GLOBAL FUNCTIONS 71otherwise it returns pollingch (=-3). If the input stream is exhausted it returnsendstreamch (= -1). Unlike sardch, the charac
vi CONTENTS• The SIAL intermediate code that allows easy translation o f BCPL in nativecode for most architectures, inclu ding, for instance, the Rasp
72 CHAPTER 3. THE LIBRARYoldcount := sys(Syssetcount, newcount) CIN:y, POS:y, NAT:nOne of the Cintcode registers is called count which is inspected ju
3.3. GLOBAL FUNCTIONS 73-1 Re-enter the interpreter with a new value in the the countregister0 Normal successful completion (by convention)1 Non exist
74 CHAPTER 3. THE LIBRARYthe specified value ret ur ni n g it previous setting. Setting it to a negative value disablesthe tracing mechani s m. See Sys
3.3. GLOBAL FUNCTIONS 75res := sys(Syssound, snd waveOutClose, a1) CIN:y, POS:y, NAT:yThis closes sound wave output device a1.res := sys(Syssound, snd
76 CHAPTER 3. THE LIBRARYsys(Systally, val) CIN:y, POS:y, NAT:nThis call provides a profiling facility that uses a globally accessible tall y vector to
3.3. GLOBAL FUNCTIONS 77res := sys(Sysusleep, usecs) CIN:y, PO S: y, NAT:yUnder ci ntsys, thi s call causes the system to sleep for usecs micro-second
78 CHAPTER 3. THE LIBRARYreturns TRUE if successful, and FALSE otherwise. A call of unrdch will alway s succeedsthe first time after a call of rdch. It
3.3. GLOBAL FUNCTIONS 79Item Substitution%s Write the next argument as a string using writes.%tn Write t h e next argument as a left justified string i
80 CHAPTER 3. THE LIBRARYFOR count = 0 TO 2 DOwritef("There %p\ is\are\ %-%n thing%-%ps.*n", count)outputs:There are 0 things.There is 1 thi
3.3. GLOBAL FUNCTIONS 81AND rdch() = VALOF{ LET pos = cis!scb_pos // Position of next byte, if anyUNLESS cis DO abort(186)IF pos<cis!scb_end DO { L
Chapter 1The System OverviewThis document contains a full description of an interpretive implementation ofBCPL that supports a command language and lo
82 CHAPTER 3. THE LIBRARYAND wrch(ch) = VALOF{ LET pos = cos!scb_posIF pos >= cos!scb_bufend DO{ // The buffer is fullUNLESS deplete(cos) RESULTIS
3.4. RANDOM ACCESS 833.4 Random AccessDisk files can be regarded as potentially huge vectors of bytes with the first byte beingat position zero of the fi
84 CHAPTER 3. THE LIBRARYble to commands such as bcpl, c and setroot via the rootnode fiel ds rtnrootvar,rtnpathvar, rtn hdrsvar and rtn scriptsvar.Whe
3.7. COROUTINE EXAMPLES 853.7.1 A square wave generatorThe following function is the main function of a coroutine that generates square wavesamples.LE
86 CHAPTER 3. THE LIBRARYMAINBUF1 TEE1X2 X3 X5MER1 MER2BUF2 TEE2 BUF3w w w w wwwwwwww c c c cwcccc cccc cFigure 3.10: Coroutine data flowThis problem i
3.7. COROUTINE EXAMPLES 87LET tee(args) BE // Body of TEE1 and TEE2{ LET in, out = args!0, args!1cowait() // End of initialisation.{ LET val = callco(
88 CHAPTER 3. THE LIBRARY3.7.3 A Discrete Event SimulatorThis is a benchmark test for a di sc r e te event simulator using coroutines. It simulatesa n
3.7. COROUTINE EXAMPLES 89// ################### Random number generator #######################// The following random number generator is based on o
90 CHAPTER 3. THE LIBRARYAND downheap(event, i) BE{ LET j, min = 2*i, ? // j is left child, if presentIF j > priqn DO{ upheap(event, i)RETURN}min :
3.7. COROUTINE EXAMPLES 91AND qitem(node) BE// The message has reached this node// It currently not busy, mark it as busy and return to process// the
Comments to this Manuals