100% found this document useful (5 votes)
35 views

Computer Science and Compiler Design An Introduction with C 1st Edition by Compiler Generators in C ISBN - The ebook is available for online reading or easy download

The document provides information about various computer science ebooks available for instant download at ebookball.com, including titles on compiler design, data structures, and algorithms. It also details a specific book on compilers and compiler generators by P.D. Terry, which is aimed at senior undergraduates and mixes theory with practical applications. The text includes a comprehensive overview of the compiler construction process, case studies, and supporting software for educational purposes.

Uploaded by

birchrampak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
35 views

Computer Science and Compiler Design An Introduction with C 1st Edition by Compiler Generators in C ISBN - The ebook is available for online reading or easy download

The document provides information about various computer science ebooks available for instant download at ebookball.com, including titles on compiler design, data structures, and algorithms. It also details a specific book on compilers and compiler generators by P.D. Terry, which is aimed at senior undergraduates and mixes theory with practical applications. The text includes a comprehensive overview of the compiler construction process, case studies, and supporting software for educational purposes.

Uploaded by

birchrampak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Instant Ebook Access, One Click Away – Begin at ebookball.

com

Computer Science and Compiler Design An


Introduction with C 1st Edition by Compiler
Generators in C ISBN

https://ebookball.com/product/computer-science-and-compiler-
design-an-introduction-with-c-1st-edition-by-compiler-
generators-in-c-isbn-11720/

OR CLICK BUTTON

DOWLOAD EBOOK

Get Instant Ebook Downloads – Browse at https://ebookball.com


Your digital treasures (PDF, ePub, MOBI) await
Download instantly and pick your perfect format...

Read anywhere, anytime, on any device!

Data Structures Algorithms and Applications in C With


Microsoft Compiler 2nd Edition by Sartaj Sahni ISBN
0929306325 9780929306322

https://ebookball.com/product/data-structures-algorithms-and-
applications-in-c-with-microsoft-compiler-2nd-edition-by-sartaj-sahni-
isbn-0929306325-9780929306322-15764/

ebookball.com

Data Structures Algorithms and Applications in C++ With


Microsoft Compiler 1st edition by Sartaj Sahni ISBN
007236226X 978-0072362268

https://ebookball.com/product/data-structures-algorithms-and-
applications-in-c-with-microsoft-compiler-1st-edition-by-sartaj-sahni-
isbn-007236226x-978-0072362268-16394/

ebookball.com

Data Structures Algorithms and Applications in C++ With


Microsoft Compiler 2nd edition by Michael Goodrich,
Roberto Tamassia, David Mount ISBN B005FHM6X2
978-0470383278

https://ebookball.com/product/data-structures-algorithms-and-
applications-in-c-with-microsoft-compiler-2nd-edition-by-michael-
goodrich-roberto-tamassia-david-mount-
isbn-b005fhm6x2-978-0470383278-16544/
ebookball.com

Modern Compiler Design Worldwide Series in Computer


Science 1st Edition by Dick Grune, Henri Bal, Ceriel
Jacobs, Koen Langendoen ISBN 0471976970 9780471976974

https://ebookball.com/product/modern-compiler-design-worldwide-series-
in-computer-science-1st-edition-by-dick-grune-henri-bal-ceriel-jacobs-
koen-langendoen-isbn-0471976970-9780471976974-19834/

ebookball.com
An Introduction to Design Patterns in C with Qt 4 1st
Edition by Alan Ezust, Paul Ezust ISBN 0131879057
9780131879058

https://ebookball.com/product/an-introduction-to-design-patterns-in-c-
with-qt-4-1st-edition-by-alan-ezust-paul-ezust-
isbn-0131879057-9780131879058-12664/

ebookball.com

Introduction to Design Patterns in C with Qt 2nd Edition


by Alan Ezust, Paul Ezust ISBN 0132851636 9780132851633

https://ebookball.com/product/introduction-to-design-patterns-in-c-
with-qt-2nd-edition-by-alan-ezust-paul-ezust-
isbn-0132851636-9780132851633-14144/

ebookball.com

Compiler Construction: An Advanced Course 1st Edition by


Bauer FL ISBN 3540069585 9783540069584

https://ebookball.com/product/compiler-construction-an-advanced-
course-1st-edition-by-bauer-fl-isbn-3540069585-9783540069584-13264/

ebookball.com

Developing Software for Symbian OS An Introduction to c in


C++ 1st edition by Steve Babin ISBN 0470018453
978-0470018453

https://ebookball.com/product/developing-software-for-symbian-os-an-
introduction-to-c-in-c-1st-edition-by-steve-babin-
isbn-0470018453-978-0470018453-13366/

ebookball.com

Algorithms for Compiler Design 1st edition by Kakde ISBN


‎ 1584501006 978-1584501008

https://ebookball.com/product/algorithms-for-compiler-design-1st-
edition-by-kakde-isbn-aeurz-1584501006-978-1584501008-17748/

ebookball.com
Compilers and Compiler Generators
an introduction with C++
© P.D. Terry, Rhodes University, 1996

[email protected]

This is a set of Adobe PDF® files of the text of my book "Compilers and Compiler Generators - an
introduction with C++", published in 1997 by International Thomson Computer Press. The original
edition is now out of print, and the copyright has reverted to me.

The book is also available in other formats. The latest versions of the distribution and details of
how to download up-to-date compressed versions of the text and its supporting software and
courseware can be found at http://www.scifac.ru.ac.za/compilers/

The text of the book is Copyright © PD Terry. Although you are free to make use of the material
for academic purposes, the material may not be redistributed without my knowledge or permission.

File List
The 18 chapters of the book are filed as chap01.pdf through chap18.pdf
The 4 appendices to the book are filed as appa.pdf through appd.pdf
The original appendix A of the book is filed as appa0.pdf
The contents of the book is filed as contents.pdf
The preface of the book is filed as preface.pdf
An index for the book is filed as index.pdf. Currently (January 2000) the page numbers refer
to an A4 version in PCL® format available at
http://www.scifac.ru.ac.za/compilers/longpcl.zip. However, software tools like GhostView
may be used to search the files for specific text.
The bibliography for the book is filed as biblio.pdf

Change List
18-October-1999 - Pre-release
12-November-1999 - First official on-line release
16-January-2000 - First release of Postscript version (incorporates minor corrections to
chapter 12)
17-January-2000 - First release of PDF version
Compilers and Compiler Generators © P.D. Terry, 2000

PREFACE
This book has been written to support a practically oriented course in programming language
translation for senior undergraduates in Computer Science. More specifically, it is aimed at students
who are probably quite competent in the art of imperative programming (for example, in C++,
Pascal, or Modula-2), but whose mathematics may be a little weak; students who require only a
solid introduction to the subject, so as to provide them with insight into areas of language design
and implementation, rather than a deluge of theory which they will probably never use again;
students who will enjoy fairly extensive case studies of translators for the sorts of languages with
which they are most familiar; students who need to be made aware of compiler writing tools, and to
come to appreciate and know how to use them. It will hopefully also appeal to a certain class of
hobbyist who wishes to know more about how translators work.

The reader is expected to have a good knowledge of programming in an imperative language and,
preferably, a knowledge of data structures. The book is practically oriented, and the reader who
cannot read and write code will have difficulty following quite a lot of the discussion. However, it
is difficult to imagine that students taking courses in compiler construction will not have that sort of
background!

There are several excellent books already extant in this field. What is intended to distinguish this
one from the others is that it attempts to mix theory and practice in a disciplined way, introducing
the use of attribute grammars and compiler writing tools, at the same time giving a highly practical
and pragmatic development of translators of only moderate size, yet large enough to provide
considerable challenge in the many exercises that are suggested.

Overview

The book starts with a fairly simple overview of the translation process, of the constituent parts of a
compiler, and of the concepts of porting and bootstrapping compilers. This is followed by a chapter
on machine architecture and machine emulation, as later case studies make extensive use of code
generation for emulated machines, a very common strategy in introductory courses. The next
chapter introduces the student to the notions of regular expressions, grammars, BNF and EBNF,
and the value of being able to specify languages concisely and accurately.

Two chapters follow that discuss simple features of assembler language, accompanied by the
development of an assembler/interpreter system which allows not only for very simple assembly,
but also for conditional assembly, macro-assembly, error detection, and so on. Complete code for
such an assembler is presented in a highly modularized form, but with deliberate scope left for
extensions, ranging from the trivial to the extensive.

Three chapters follow on formal syntax theory, parsing, and the manual construction of scanners
and parsers. The usual classifications of grammars and restrictions on practical grammars are
discussed in some detail. The material on parsing is kept to a fairly simple level, but with a
thorough discussion of the necessary conditions for LL(1) parsing. The parsing method treated in
most detail is the method of recursive descent, as is found in many Pascal compilers; LR parsing is
only briefly discussed.
The next chapter is on syntax directed translation, and stresses to the reader the importance and
usefulness of being able to start from a context-free grammar, adding attributes and actions that
allow for the manual or mechanical construction of a program that will handle the system that it
defines. Obvious applications come from the field of translators, but applications in other areas
such as simple database design are also used and suggested.

The next two chapters give a thorough introduction to the use of Coco/R, a compiler generator
based on L- attributed grammars. Besides a discussion of Cocol, the specification language for this
tool, several in-depth case studies are presented, and the reader is given some indication of how
parser generators are themselves constructed.

The next two chapters discuss the construction of a recursive descent compiler for a simple
Pascal-like source language, using both hand-crafted and machine-generated techniques. The
compiler produces pseudo-code for a hypothetical stack-based computer (for which an interpreter
was developed in an earlier chapter). "On the fly" code generation is discussed, as well as the use of
intermediate tree construction.

The last chapters extend the simple language (and its compiler) to allow for procedures and
functions, demonstrate the usual stack-frame approach to storage management, and go on to discuss
the implementation of simple concurrent programming. At all times the student can see how these
are handled by the compiler/interpreter system, which slowly grows in complexity and usefulness
until the final product enables the development of quite sophisticated programs.

The text abounds with suggestions for further exploration, and includes references to more
advanced texts where these can be followed up. Wherever it seems appropriate the opportunity is
taken to make the reader more aware of the strong and weak points in topical imperative languages.
Examples are drawn from several languages, such as Pascal, Modula-2, Oberon, C, C++, Edison
and Ada.

Support software

An earlier version of this text, published by Addison-Wesley in 1986, used Pascal throughout as a
development tool. By that stage Modula-2 had emerged as a language far better suited to serious
programming. A number of discerning teachers and programmers adopted it enthusiastically, and
the material in the present book was originally and successfully developed in Modula-2. More
recently, and especially in the USA, one has witnessed the spectacular rise in popularity of C++,
and so as to reflect this trend, this has been adopted as the main language used in the present text.
Although offering much of value to skilled practitioners, C++ is a complex language. As the aim of
the text is not to focus on intricate C++programming, but compiler construction, the supporting
software has been written to be as clear and as simple as possible. Besides the C++ code, complete
source for all the case studies has also been provided on an accompanying IBM-PC compatible
diskette in Turbo Pascal and Modula-2, so that readers who are proficient programmers in those
languages but only have a reading knowledge of C++ should be able to use the material very
successfully.

Appendix A gives instructions for unpacking the software provided on the diskette and installing it
on a reader’s computer. In the same appendix will be found the addresses of various sites on the
Internet where this software (and other freely available compiler construction software) can be
found in various formats. The software provided on the diskette includes
Emulators for the two virtual machines described in Chapter 4 (one of these is a simple
accumulator based machine, the other is a simple stack based machine).

The one- and two-pass assemblers for the accumulator based machine, discussed in Chapter 6.

A macro assembler for the accumulator-based machine, discussed in Chapter 7.

Three executable versions of the Coco/R compiler generator used in the text and described in
detail in Chapter 12, along with the frame files that it needs. (The three versions produce
Turbo Pascal, Modula-2 or C/C++ compilers)

Complete source code for hand-crafted versions of each of the versions of the Clang compiler
that is developed in a layered way in Chapters 14 through 18. This highly modularized code
comes with an "on the fly" code generator, and also with an alternative code generator that
builds and then walks a tree representation of the intermediate code.

Cocol grammars and support modules for the numerous case studies throughout the book that
use Coco/R. These include grammars for each of the versions of the Clang compiler.

A program for investigating the construction of minimal perfect hash functions (as discussed
in Chapter 14).

A simple demonstration of an LR parser (as discussed in Chapter 10).

Use as a course text

The book can be used for courses of various lengths. By choosing a selection of topics it could be
used on courses as short as 5-6 weeks (say 15-20 hours of lectures and 6 lab sessions). It could also
be used to support longer and more intensive courses. In our university, selected parts of the
material have been successfully used for several years in a course of about 35 - 40 hours of lectures
with strictly controlled and structured, related laboratory work, given to students in a pre-Honours
year. During that time the course has evolved significantly, from one in which theory and formal
specification played a very low key, to the present stage where students have come to appreciate the
use of specification and syntax-directed compiler-writing systems as very powerful and useful tools
in their armoury.

It is hoped that instructors can select material from the text so as to suit courses tailored to their
own interests, and to their students’ capabilities. The core of the theoretical material is to be found
in Chapters 1, 2, 5, 8, 9, 10 and 11, and it is suggested that this material should form part of any
course based on the book. Restricting the selection of material to those chapters would deny the
student the very important opportunity to see the material in practice, and at least a partial selection
of the material in the practically oriented chapters should be studied. However, that part of the
material in Chapter 4 on the accumulator-based machine, and Chapters 6 and 7 on writing
assemblers for this machine could be omitted without any loss of continuity. The development of
the small Clang compiler in Chapters 14 through 18 is handled in a way that allows for the later
sections of Chapter 15, and for Chapters 16 through 18 to be omitted if time is short. A very wide
variety of laboratory exercises can be selected from those suggested as exercises, providing the
students with both a challenge, and a feeling of satisfaction when they rise to meet that challenge.
Several of these exercises are based on the idea of developing a small compiler for a language
similar to the one discussed in detail in the text. Development of such a compiler could rely entirely
on traditional hand-crafted techniques, or could rely entirely on a tool-based approach (both
approaches have been successfully used at our university). If a hand-crafted approach were used,
Chapters 12 and 13 could be omitted; Chapter 12 is largely a reference manual in any event, and
could be left to the students to study for themselves as the need arose. Similarly, Chapter 3 falls into
the category of background reading.

At our university we have also used an extended version of the Clang compiler as developed in the
text (one incorporating several of the extensions suggested as exercises) as a system for students to
study concurrent programming per se, and although it is a little limited, it is more than adequate for
the purpose. We have also used a slightly extended version of the assembler program very
successfully as our primary tool for introducing students to the craft of programming at the
assembler level.

Limitations

It is, perhaps, worth a slight digression to point out some things which the book does not claim to
be, and to justify some of the decisions made in the selection of material.

In the first place, while it is hoped that it will serve as a useful foundation for students who are
already considerably more advanced, a primary aim has been to make the material as accessible as
possible to students with a fairly limited background, to enhance the background, and to make them
somewhat more critical of it. In many cases this background is still Pascal based; increasingly it is
tending to become C++ based. Both of these languages have become rather large and complex, and
I have found that many students have a very superficial idea of how they really fit together. After a
course such as this one, many of the pieces of the language jigsaw fit together rather better.

When introducing the use of compiler writing tools, one might follow the many authors who
espouse the classic lex/yacc approach. However, there are now a number of excellent LL(1) based
tools, and these have the advantage that the code which is produced is close to that which might be
hand-crafted; at the same time, recursive descent parsing, besides being fairly intuitive, is powerful
enough to handle very usable languages.

That the languages used in case studies and their translators are relative toys cannot be denied. The
Clang language of later chapters, for example, supports only integer variables and simple
one-dimensional arrays of these, and has concurrent features allowing little beyond the simulation
of some simple textbook examples. The text is not intended to be a comprehensive treatise on
systems programming in general, just on certain selected topics in that area, and so very little is said
about native machine code generation and optimization, linkers and loaders, the interaction and
relationship with an operating system, and so on. These decisions were all taken deliberately, to
keep the material readily understandable and as machine-independent as possible. The systems may
be toys, but they are very usable toys! Of course the book is then open to the criticism that many of
the more difficult topics in translation (such as code generation and optimization) are effectively
not covered at all, and that the student may be deluded into thinking that these areas do not exist.
This is not entirely true; the careful reader will find most of these topics mentioned somewhere.

Good teachers will always want to put something of their own into a course, regardless of the
quality of the prescribed textbook. I have found that a useful (though at times highly dangerous)
technique is deliberately not to give the best solutions to a problem in a class discussion, with the
optimistic aim that students can be persuaded to "discover" them for themselves, and even gain a
sense of achievement in so doing. When applied to a book the technique is particularly dangerous,
but I have tried to exploit it on several occasions, even though it may give the impression that the
author is ignorant.

Another dangerous strategy is to give too much away, especially in a book like this aimed at
courses where, so far as I am aware, the traditional approach requires that students make far more
of the design decisions for themselves than my approach seems to allow them. Many of the books
in the field do not show enough of how something is actually done: the bridge between what they
give and what the student is required to produce is in excess of what is reasonable for a course
which is only part of a general curriculum. I have tried to compensate by suggesting what I hope is
a very wide range of searching exercises. The solutions to some of these are well known, and
available in the literature. Again, the decision to omit explicit references was deliberate (perhaps
dangerously so). Teachers often have to find some way of persuading the students to search the
literature for themselves, and this is not done by simply opening the journal at the right page for
them.

Acknowledgements
I am conscious of my gratitude to many people for their help and inspiration while this book has
been developed.

Like many others, I am grateful to Niklaus Wirth, whose programming languages and whose
writings on the subject of compiler construction and language design refute the modern trend
towards ever-increasing complexity in these areas, and serve as outstanding models of the way in
which progress should be made.

This project could not have been completed without the help of Hanspeter Mössenböck (author of
the original Coco/R compiler generator) and Francisco Arzu (who ported it to C++), who not only
commented on parts of the text, but also willingly gave permission for their software to be
distributed with the book. My thanks are similarly due to Richard Cichelli for granting permission
to distribute (with the software for Chapter 14) a program based on one he wrote for computing
minimal perfect hash functions, and to Christopher Cockburn for permission to include his
description of tonic sol-fa (used in Chapter 13).

I am grateful to Volker Pohlers for help with the port of Coco/R to Turbo Pascal, and to Dave
Gillespie for developing p2c, a most useful program for converting Modula-2 and Pascal code to
C/C++.

I am deeply indebted to my colleagues Peter Clayton, George Wells and Peter Wentworth for many
hours of discussion and fruitful suggestions. John Washbrook carefully reviewed the manuscript,
and made many useful suggestions for its improvement. Shaun Bangay patiently provided
incomparable technical support in the installation and maintenance of my hardware and software,
and rescued me from more than one disaster when things went wrong. To Rhodes University I am
indebted for the use of computer facilities, and for granting me leave to complete the writing of the
book. And, of course, several generations of students have contributed in intangible ways by their
reaction to my courses.

The development of the software in this book relied heavily on the use of electronic mail, and I am
grateful to Randy Bush, compiler writer and network guru extraordinaire, for his friendship, and for
his help in making the Internet a reality in developing countries in Africa and elsewhere.
But, as always, the greatest debt is owed to my wife Sally and my children David and Helen, for
their love and support through the many hours when they must have wondered where my priorities
lay.

Pat Terry
Rhodes University
Grahamstown

Trademarks
Ada is a trademark of the US Department of Defense.
Apple II is a trademark of Apple Corporation.
Borland C++, Turbo C++, TurboPascal and Delphi are trademarks of Borland
International Corporation.
GNU C Compiler is a trademark of the Free Software Foundation.
IBM and IBM PC are trademarks of International Business Machines Corporation.
Intel is a registered trademark of Intel Corporation.
MC68000 and MC68020 are trademarks of Motorola Corporation.
MIPS is a trademark of MIPS computer systems.
Microsoft, MS and MS-DOS are registered trademarks and Windows is a trademark of
Microsoft Corporation.
SPARC is a trademark of Sun Microsystems.
Stony Brook Software and QuickMod are trademarks of Gogesch Micro Systems, Inc.
occam and Transputer are trademarks of Inmos.
UCSD Pascal and UCSD p-System are trademarks of the Regents of the University of
California.
UNIX is a registered trademark of AT&T Bell Laboratories.
Z80 is a trademark of Zilog Corporation.
COMPILERS AND COMPILER
GENERATORS
an introduction with C++
© P.D. Terry, Rhodes University, 1996

e-mail [email protected]

The Postscript ® edition of this book was derived from the on-line versions available at
http://www.scifac.ru.ac.za/compilers/, a WWW site that is occasionally updated, and which
contains the latest versions of the various editions of the book, with details of how to download
compressed versions of the text and its supporting software and courseware.

The original edition of this book, published originally by International Thomson, is now out of
print, but has a home page at http://cs.ru.ac.za/homes/cspt/compbook.htm. In preparing the on-line
edition, the opportunity was taken to correct the few typographical mistakes that crept into the first
printing, and to create a few hyperlinks to where the source files can be found.

Feel free to read and use this book for study or teaching, but please respect my copyright and do not
distribute it further without my consent. If you do make use of it I would appreciate hearing from
you.

CONTENTS
Preface

Acknowledgements

1 Introduction
1.1 Objectives
1.2 Systems programs and translators
1.3 The relationship between high-level languages and translators

2 Translator classification and structure


2.1 T-diagrams
2.2 Classes of translator
2.3 Phases in translation
2.4 Multi-stage translators
2.5 Interpreters, interpretive compilers, and emulators

3 Compiler construction and bootstrapping


3.1 Using a high-level host language
3.2 Porting a high-level translator
3.3 Bootstrapping
3.4 Self-compiling compilers
3.5 The half bootstrap
3.6 Bootstrapping from a portable interpretive compiler
3.7 A P-code assembler

4 Machine emulation
4.1 Simple machine architecture
4.2 Addressing modes
4.3 Case study 1 - a single-accumulator machine
4.4 Case study 2 - a stack-oriented computer

5 Language specification
5.1 Syntax, semantics, and pragmatics
5.2 Languages, symbols, alphabets and strings
5.3 Regular expressions
5.4 Grammars and productions
5.5 Classic BNF notation for productions
5.6 Simple examples
5.7 Phrase structure and lexical structure
5.8 -productions
5.9 Extensions to BNF
5.10 Syntax diagrams
5.11 Formal treatment of semantics

6 Simple assemblers
6.1 A simple ASSEMBLER language
6.2 One- and two-pass assemblers, and symbol tables
6.3 Towards the construction of an assembler
6.4 Two-pass assembly
6.5 One-pass assembly

7 Advanced assembler features


7.1 Error detection
7.2 Simple expressions as addresses
7.3 Improved symbol table handling - hash tables
7.4 Macro-processing facilities
7.5 Conditional assembly
7.6 Relocatable code
7.7 Further projects

8 Grammars and their classification


8.1 Equivalent grammars
8.2 Case study - equivalent grammars for describing expressions
8.3 Some simple restrictions on grammars
8.4 Ambiguous grammars
8.5 Context sensitivity
8.6 The Chomsky hierarchy
8.7 Case study - Clang

9 Deterministic top-down parsing


9.1 Deterministic top-down parsing
9.2 Restrictions on grammars so as to allow LL(1) parsing
9.3 The effect of the LL(1) conditions on language design

10 Parser and scanner construction


10.1 Construction of simple recursive descent parsers
10.2 Case studies
10.3 Syntax error detection and recovery
10.4 Construction of simple scanners
10.5 Case studies
10.6 LR parsing
10.7 Automated construction of scanners and parsers

11 Syntax-directed translation
11.1 Embedding semantic actions into syntax rules
11.2 Attribute grammars
11.3 Synthesized and inherited attributes
11.4 Classes of attribute grammars
11.5 Case study - a small student database

12 Using Coco/R - overview


12.1 Installing and running Coco/R
12.2 Case study - a simple adding machine
12.3 Scanner specification
12.4 Parser specification
12.5 The driver program

13 Using Coco/R - Case studies


13.1 Case study - Understanding C declarations
13.2 Case study - Generating one-address code from expressions
13.3 Case study - Generating one-address code from an AST
13.4 Case study - How do parser generators work?
13.5 Project suggestions

14 A simple compiler - the front end


14.1 Overall compiler structure
14.2 Source handling
14.3 Error reporting
14.4 Lexical analysis
14.5 Syntax analysis
14.6 Error handling and constraint analysis
14.7 The symbol table handler
14.8 Other aspects of symbol table management - further types

15 A simple compiler - the back end


15.1 The code generation interface
15.2 Code generation for a simple stack machine
15.3 Other aspects of code generation

16 Simple block structure


16.1 Parameterless procedures
16.2 Storage management

17 Parameters and functions


17.1 Syntax and semantics
17.2 Symbol table support for context sensitive features
17.3 Actual parameters and stack frames
17.4 Hypothetical stack machine support for parameter passing
17.5 Context sensitivity and LL(1) conflict resolution
17.6 Semantic analysis and code generation
17.7 Language design issues

18 Concurrent programming
18.1 Fundamental concepts
18.2 Parallel processes, exclusion and synchronization
18.3 A semaphore-based system - syntax, semantics, and code generation
18.4 Run-time implementation

Appendix A: Software resources for this book

Appendix B: Source code for the Clang compiler/interpreter

Appendix C: Cocol grammar for the Clang compiler/interpreter

Appendix D: Source code for a macro assembler

Bibliography

Index
Compilers and Compiler Generators © P.D. Terry, 2000

1 INTRODUCTION

1.1 Objectives
The use of computer languages is an essential link in the chain between human and computer. In
this text we hope to make the reader more aware of some aspects of

Imperative programming languages - their syntactic and semantic features; the ways of
specifying syntax and semantics; problem areas and ambiguities; the power and usefulness of
various features of a language.

Translators for programming languages - the various classes of translator (assemblers,


compilers, interpreters); implementation of translators.

Compiler generators - tools that are available to help automate the construction of translators
for programming languages.

This book is a complete revision of an earlier one published by Addison-Wesley (Terry, 1986). It
has been written so as not to be too theoretical, but to relate easily to languages which the reader
already knows or can readily understand, like Pascal, Modula-2, C or C++. The reader is expected
to have a good background in one of those languages, access to a good implementation of it, and,
preferably, some background in assembly language programming and simple machine architecture.
We shall rely quite heavily on this background, especially on the understanding the reader should
have of the meaning of various programming constructs.

Significant parts of the text concern themselves with case studies of actual translators for simple
languages. Other important parts of the text are to be found in the many exercises and suggestions
for further study and experimentation on the part of the reader. In short, the emphasis is on "doing"
rather than just "reading", and the reader who does not attempt the exercises will miss many, if not
most, of the finer points.

The primary language used in the implementation of our case studies is C++ (Stroustrup, 1990).
Machine readable source code for all these case studies is to be found on the IBM-PC compatible
diskette that is included with the book. As well as C++ versions of this code, we have provided
equivalent source in Modula-2 and Turbo Pascal, two other languages that are eminently suitable
for use in a course of this nature. Indeed, for clarity, some of the discussion is presented in a
pseudo-code that often resembles Modula-2 rather more than it does C++. It is only fair to warn the
reader that the code extracts in the book are often just that - extracts - and that there are many
instances where identifiers are used whose meaning may not be immediately apparent from their
local context. The conscientious reader will have to expend some effort in browsing the code.
Complete source for an assembler and interpreter appears in the appendices, but the discussion
often revolves around simplified versions of these programs that are found in their entirety only on
the diskette.
1.2 Systems programs and translators
Users of modern computing systems can be divided into two broad categories. There are those who
never develop their own programs, but simply use ones developed by others. Then there are those
who are concerned as much with the development of programs as with their subsequent use. This
latter group - of whom we as computer scientists form a part - is fortunate in that program
development is usually aided by the use of high-level languages for expressing algorithms, the use
of interactive editors for program entry and modification, and the use of sophisticated job control
languages or graphical user interfaces for control of execution. Programmers armed with such tools
have a very different picture of computer systems from those who are presented with the hardware
alone, since the use of compilers, editors and operating systems - a class of tools known generally
as systems programs - removes from humans the burden of developing their systems at the
machine level. That is not to claim that the use of such tools removes all burdens, or all possibilities
for error, as the reader will be well aware.

Well within living memory, much program development was done in machine language - indeed,
some of it, of necessity, still is - and perhaps some readers have even tried this for themselves when
experimenting with microprocessors. Just a brief exposure to programs written as almost
meaningless collections of binary or hexadecimal digits is usually enough to make one grateful for
the presence of high-level languages, clumsy and irritating though some of their features may be.

However, in order for high-level languages to be usable, one must be able to convert programs
written in them into the binary or hexadecimal digits and bitstrings that a machine will understand.
At an early stage it was realized that if constraints were put on the syntax of a high-level language
the translation process became one that could be automated. This led to the development of
translators or compilers - programs which accept (as data) a textual representation of an algorithm
expressed in a source language, and which produce (as primary output) a representation of the
same algorithm expressed in another language, the object or target language.

Beginners often fail to distinguish between the compilation (compile-time) and execution (run-time)
phases in developing and using programs written in high-level languages. This is an easy trap to fall
into, since the translation (compilation) is often hidden from sight, or invoked with a special
function key from within an integrated development environment that may possess many other
magic function keys. Furthermore, beginners are often taught programming with this distinction
deliberately blurred, their teachers offering explanations such as "when a computer executes a read
statement it reads a number from the input data into a variable". This hides several low-level
operations from the beginner. The underlying implications of file handling, character conversion,
and storage allocation are glibly ignored - as indeed is the necessity for the computer to be
programmed to understand the word read in the first place. Anyone who has attempted to program
input/output (I/O) operations directly in assembler languages will know that many of them are
non-trivial to implement.

A translator, being a program in its own right, must itself be written in a computer language, known
as its host or implementation language. Today it is rare to find translators that have been
developed from scratch in machine language. Clearly the first translators had to be written in this
way, and at the outset of translator development for any new system one has to come to terms with
the machine language and machine architecture for that system. Even so, translators for new
machines are now invariably developed in high-level languages, often using the techniques of
cross-compilation and bootstrapping that will be discussed in more detail later.

The first major translators written may well have been the Fortran compilers developed by Backus
and his colleagues at IBM in the 1950’s, although machine code development aids were in
existence by then. The first Fortran compiler is estimated to have taken about 18 person-years of
effort. It is interesting to note that one of the primary concerns of the team was to develop a system
that could produce object code whose efficiency of execution would compare favourably with that
which expert human machine coders could achieve. An automatic translation process can rarely
produce code as optimal as can be written by a really skilled user of machine language, and to this
day important components of systems are often developed at (or very near to) machine level, in the
interests of saving time or space.

Translator programs themselves are never completely portable (although parts of them may be), and
they usually depend to some extent on other systems programs that the user has at his or her
disposal. In particular, input/output and file management on modern computer systems are usually
controlled by the operating system. This is a program or suite of programs and routines whose job
it is to control the execution of other programs so as best to share resources such as printers,
plotters, disk files and tapes, often making use of sophisticated techniques such as parallel
processing, multiprogramming and so on. For many years the development of operating systems
required the use of programming languages that remained closer to the machine code level than did
languages suitable for scientific or commercial programming. More recently a number of successful
higher level languages have been developed with the express purpose of catering for the design of
operating systems and real-time control. The most obvious example of such a language is C,
developed originally for the implementation of the UNIX operating system, and now widely used in
all areas of computing.

1.3 The relationship between high-level languages and translators


The reader will rapidly become aware that the design and implementation of translators is a subject
that may be developed from many possible angles and approaches. The same is true for the design
of programming languages.

Computer languages are generally classed as being "high-level" (like Pascal, Fortran, Ada,
Modula-2, Oberon, C or C++) or "low-level" (like ASSEMBLER). High-level languages may
further be classified as "imperative" (like all of those just mentioned), or "functional" (like Lisp,
Scheme, ML, or Haskell), or "logic" (like Prolog).

High-level languages are claimed to possess several advantages over low-level ones:

Readability: A good high-level language will allow programs to be written that in some ways
resemble a quasi-English description of the underlying algorithms. If care is taken, the coding
may be done in a way that is essentially self-documenting, a highly desirable property when
one considers that many programs are written once, but possibly studied by humans many
times thereafter.

Portability: High-level languages, being essentially machine independent, hold out the
promise of being used to develop portable software. This is software that can, in principle
(and even occasionally in practice), run unchanged on a variety of different machines -
provided only that the source code is recompiled as it moves from machine to machine.

To achieve machine independence, high-level languages may deny access to low-level


features, and are sometimes spurned by programmers who have to develop low-level machine
dependent systems. However, some languages, like C and Modula-2, were specifically
designed to allow access to these features from within the context of high-level constructs.
Structure and object orientation: There is general agreement that the structured programming
movement of the 1960’s and the object-oriented movement of the 1990’s have resulted in a
great improvement in the quality and reliability of code. High-level languages can be
designed so as to encourage or even subtly enforce these programming paradigms.

Generality: Most high-level languages allow the writing of a wide variety of programs, thus
relieving the programmer of the need to become expert in many diverse languages.

Brevity: Programs expressed in high-level languages are often considerably shorter (in terms
of their number of source lines) than their low-level equivalents.

Error checking: Being human, a programmer is likely to make many mistakes in the
development of a computer program. Many high-level languages - or at least their
implementations - can, and often do, enforce a great deal of error checking both at
compile-time and at run-time. For this they are, of course, often criticized by programmers
who have to develop time-critical code, or who want their programs to abort as quickly as
possible.

These advantages sometimes appear to be over-rated, or at any rate, hard to reconcile with reality.
For example, readability is usually within the confines of a rather stilted style, and some beginners
are disillusioned when they find just how unnatural a high-level language is. Similarly, the
generality of many languages is confined to relatively narrow areas, and programmers are often
dismayed when they find areas (like string handling in standard Pascal) which seem to be very
poorly handled. The explanation is often to be found in the close coupling between the development
of high-level languages and of their translators. When one examines successful languages, one finds
numerous examples of compromise, dictated largely by the need to accommodate language ideas to
rather uncompromising, if not unsuitable, machine architectures. To a lesser extent, compromise is
also dictated by the quirks of the interface to established operating systems on machines. Finally,
some appealing language features turn out to be either impossibly difficult to implement, or too
expensive to justify in terms of the machine resources needed. It may not immediately be apparent
that the design of Pascal (and of several of its successors such as Modula-2 and Oberon) was
governed partly by a desire to make it easy to compile. It is a tribute to its designer that, in spite of
the limitations which this desire naturally introduced, Pascal became so popular, the model for so
many other languages and extensions, and encouraged the development of superfast compilers such
as are found in Borland’s Turbo Pascal and Delphi systems.

The design of a programming language requires a high degree of skill and judgement. There is
evidence to show that one’s language is not only useful for expressing one’s ideas. Because
language is also used to formulate and develop ideas, one’s knowledge of language largely
determines how and, indeed, what one can think. In the case of programming languages, there has
been much controversy over this. For example, in languages like Fortran - for long the lingua
franca of the scientific computing community - recursive algorithms were "difficult" to use (not
impossible, just difficult!), with the result that many programmers brought up on Fortran found
recursion strange and difficult, even something to be avoided at all costs. It is true that recursive
algorithms are sometimes "inefficient", and that compilers for languages which allow recursion
may exacerbate this; on the other hand it is also true that some algorithms are more simply
explained in a recursive way than in one which depends on explicit repetition (the best examples
probably being those associated with tree manipulation).

There are two divergent schools of thought as to how programming languages should be designed.
The one, typified by the Wirth school, stresses that languages should be small and understandable,
and that much time should be spent in consideration of what tempting features might be omitted
without crippling the language as a vehicle for system development. The other, beloved of
languages designed by committees with the desire to please everyone, packs a language full of
every conceivable potentially useful feature. Both schools claim success. The Wirth school has
given us Pascal, Modula-2 and Oberon, all of which have had an enormous effect on the thinking of
computer scientists. The other approach has given us Ada, C and C++, which are far more difficult
to master well and extremely complicated to implement correctly, but which claim spectacular
successes in the marketplace.

Other aspects of language design that contribute to success include the following:

Orthogonality: Good languages tend to have a small number of well thought out features that
can be combined in a logical way to supply more powerful building blocks. Ideally these
features should not interfere with one another, and should not be hedged about by a host of
inconsistencies, exceptional cases and arbitrary restrictions. Most languages have blemishes -
for example, in Wirth’s original Pascal a function could only return a scalar value, not one of
any structured type. Many potentially attractive extensions to well-established languages
prove to be extremely vulnerable to unfortunate oversights in this regard.

Familiar notation: Most computers are "binary" in nature. Blessed with ten toes on which to
check out their number-crunching programs, humans may be somewhat relieved that
high-level languages usually make decimal arithmetic the rule, rather than the exception, and
provide for mathematical operations in a notation consistent with standard mathematics.
When new languages are proposed, these often take the form of derivatives or dialects of
well-established ones, so that programmers can be tempted to migrate to the new language
and still feel largely at home - this was the route taken in developing C++ from C, Java from
C++, and Oberon from Modula-2, for example.

Besides meeting the ones mentioned above, a successful modern high-level language will have
been designed to meet the following additional criteria:

Clearly defined: It must be clearly described, for the benefit of both the user and the compiler
writer.

Quickly translated: It should admit quick translation, so that program development time when
using the language is not excessive.

Modularity: It is desirable that programs can be developed in the language as a collection of


separately compiled modules, with appropriate mechanisms for ensuring self-consistency
between these modules.

Efficient: It should permit the generation of efficient object code.

Widely available: It should be possible to provide translators for all the major machines and
for all the major operating systems.

The importance of a clear language description or specification cannot be over-emphasized. This


must apply, firstly, to the so-called syntax of the language - that is, it must specify accurately what
form a source program may assume. It must apply, secondly, to the so-called static semantics of
the language - for example, it must be clear what constraints must be placed on the use of entities of
differing types, or the scope that various identifiers have across the program text. Finally, the
specification must also apply to the dynamic semantics of programs that satisfy the syntactic and
static semantic rules - that is, it must be capable of predicting the effect any program expressed in
that language will have when it is executed.

Programming language description is extremely difficult to do accurately, especially if it is


attempted through the medium of potentially confusing languages like English. There is an
increasing trend towards the use of formalism for this purpose, some of which will be illustrated in
later chapters. Formal methods have the advantage of precision, since they make use of the clearly
defined notations of mathematics. To offset this, they may be somewhat daunting to programmers
weak in mathematics, and do not necessarily have the advantage of being very concise - for
example, the informal description of Modula-2 (albeit slightly ambiguous in places) took only some
35 pages (Wirth, 1985), while a formal description prepared by an ISO committee runs to over 700
pages.

Formal specifications have the added advantage that, in principle, and to a growing degree in
practice, they may be used to help automate the implementation of translators for the language.
Indeed, it is increasingly rare to find modern compilers that have been implemented without the
help of so-called compiler generators. These are programs that take a formal description of the
syntax and semantics of a programming language as input, and produce major parts of a compiler
for that language as output. We shall illustrate the use of compiler generators at appropriate points
in our discussion, although we shall also show how compilers may be crafted by hand.

Exercises

1.1 Make a list of as many translators as you can think of that can be found on your computer
system.

1.2 Make a list of as many other systems programs (and their functions) as you can think of that can
be found on your computer system.

1.3 Make a list of existing features in your favourite (or least favourite) programming language that
you find irksome. Make a similar list of features that you would like to have seen added. Then
examine your lists and consider which of the features are probably related to the difficulty of
implementation.

Further reading

As we proceed, we hope to make the reader more aware of some of the points raised in this section.
Language design is a difficult area, and much has been, and continues to be, written on the topic.
The reader might like to refer to the books by Tremblay and Sorenson (1985), Watson (1989), and
Watt (1991) for readable summaries of the subject, and to the papers by Wirth (1974, 1976a,
1988a), Kernighan (1981), Welsh, Sneeringer and Hoare (1977), and Cailliau (1982). Interesting
background on several well-known languages can be found in ACM SIGPLAN Notices for August
1978 and March 1993 (Lee and Sammet, 1978, 1993), two special issues of that journal devoted to
the history of programming language development. Stroustrup (1993) gives a fascinating exposition
of the development of C++, arguably the most widely used language at the present time. The terms
"static semantics" and "dynamic semantics" are not used by all authors; for a discussion on this
point see the paper by Meek (1990).
Compilers and Compiler Generators © P.D. Terry, 2000

2 TRANSLATOR CLASSIFICATION AND STRUCTURE


In this chapter we provide the reader with an overview of the inner structure of translators, and
some idea of how they are classified.

A translator may formally be defined as a function, whose domain is a source language, and whose
range is contained in an object or target language.

A little experience with translators will reveal that it is rarely considered part of the translator’s
function to execute the algorithm expressed by the source, merely to change its representation from
one form to another. In fact, at least three languages are involved in the development of translators:
the source language to be translated, the object or target language to be generated, and the host
language to be used for implementing the translator. If the translation takes place in several stages,
there may even be other, intermediate, languages. Most of these - and, indeed, the host language
and object languages themselves - usually remain hidden from a user of the source language.

2.1 T-diagrams
A useful notation for describing a computer program, particularly a translator, uses so-called
T-diagrams, examples of which are shown in Figure 2.1.

We shall use the notation "M-code" to stand for "machine code" in these diagrams. Translation
itself is represented by standing the T on a machine, and placing the source program and object
program on the left and right arms, as depicted in Figure 2.2.
We can also regard this particular combination as depicting an abstract machine (sometimes called
a virtual machine), whose aim in life is to convert Turbo Pascal source programs into their 8086
machine code equivalents.

T-diagrams were first introduced by Bratman (1961). They were further refined by Earley and
Sturgis (1970), and are also used in the books by Bennett (1990), Watt (1993), and Aho, Sethi and
Ullman (1986).

2.2 Classes of translator


It is common to distinguish between several well-established classes of translator:

The term assembler is usually associated with those translators that map low-level language
instructions into machine code which can then be executed directly. Individual source
language statements usually map one-for-one to machine-level instructions.

The term macro-assembler is also associated with those translators that map low-level
language instructions into machine code, and is a variation on the above. Most source
language statements map one- for-one into their target language equivalents, but some macro
statements map into a sequence of machine- level instructions - effectively providing a text
replacement facility, and thereby extending the assembly language to suit the user. (This is
not to be confused with the use of procedures or other subprograms to "extend" high-level
languages, because the method of implementation is usually very different.)

The term compiler is usually associated with those translators that map high-level language
instructions into machine code which can then be executed directly. Individual source
language statements usually map into many machine-level instructions.

The term pre-processor is usually associated with those translators that map a superset of a
high-level language into the original high-level language, or that perform simple text
substitutions before translation takes place. The best-known pre-processor is probably that
which forms an integral part of implementations of the language C, and which provides many
of the features that contribute to the widely- held perception that C is the only really portable
language.

The term high-level translator is often associated with those translators that map one
high-level language into another high-level language - usually one for which sophisticated
compilers already exist on a range of machines. Such translators are particularly useful as
components of a two-stage compiling system, or in assisting with the bootstrapping
techniques to be discussed shortly.
The terms decompiler and disassembler refer to translators which attempt to take object
code at a low level and regenerate source code at a higher level. While this can be done quite
successfully for the production of assembler level code, it is much more difficult when one
tries to recreate source code originally written in, say, Pascal.

Many translators generate code for their host machines. These are called self-resident translators.
Others, known as cross-translators, generate code for machines other than the host machine.
Cross-translators are often used in connection with microcomputers, especially in embedded
systems, which may themselves be too small to allow self-resident translators to operate
satisfactorily. Of course, cross-translation introduces additional problems in connection with
transferring the object code from the donor machine to the machine that is to execute the translated
program, and can lead to delays and frustration in program development.

The output of some translators is absolute machine code, left loaded at fixed locations in a machine
ready for immediate execution. Other translators, known as load-and-go translators, may even
initiate execution of this code. However, a great many translators do not produce fixed-address
machine code. Rather, they produce something closely akin to it, known as semicompiled or
binary symbolic or relocatable form. A frequent use for this is in the development of composite
libraries of special purpose routines, possibly originating from a mixture of source languages.
Routines compiled in this way are linked together by programs called linkage editors or linkers,
which may be regarded almost as providing the final stage for a multi-stage translator. Languages
that encourage the separate compilation of parts of a program - like Modula-2 and C++ - depend
critically on the existence of such linkers, as the reader is doubtless aware. For developing really
large software projects such systems are invaluable, although for the sort of "throw away" programs
on which most students cut their teeth, they can initially appear to be a nuisance, because of the
overheads of managing several files, and of the time taken to link their contents together.

T-diagrams can be combined to show the interdependence of translators, loaders and so on. For
example, the FST Modula-2 system makes use of a compiler and linker as shown in Figure 2.3.

Exercises

2.1 Make a list of as many translators as you can think of that can be found on your system.

2.2 Which of the translators known to you are of the load-and-go type?

2.3 Do you know whether any of the translators you use produce relocatable code? Is this of a
standard form? Do you know the names of the linkage editors or loaders used on your system?
2.4 Are there any pre-processors on your system? What are they used for?

2.3 Phases in translation


Translators are highly complex programs, and it is unreasonable to consider the translation process
as occurring in a single step. It is usual to regard it as divided into a series of phases. The simplest
breakdown recognizes that there is an analytic phase, in which the source program is analysed to
determine whether it meets the syntactic and static semantic constraints imposed by the language.
This is followed by a synthetic phase in which the corresponding object code is generated in the
target language. The components of the translator that handle these two major phases are said to
comprise the front end and the back end of the compiler. The front end is largely independent of
the target machine, the back end depends very heavily on the target machine. Within this structure
we can recognize smaller components or phases, as shown in Figure 2.4.

The character handler is the section that communicates with the outside world, through the
operating system, to read in the characters that make up the source text. As character sets and file
handling vary from system to system, this phase is often machine or operating system dependent.

The lexical analyser or scanner is the section that fuses characters of the source text into groups
that logically make up the tokens of the language - symbols like identifiers, strings, numeric
constants, keywords like while and if, operators like <=, and so on. Some of these symbols are
very simply represented on the output from the scanner, some need to be associated with various
properties such as their names or values.

Lexical analysis is sometimes easy, and at other times not. For example, the Modula-2 statement
WHILE A > 3 * B DO A := A - 1 END

easily decodes into tokens


WHILE keyword
A identifier name A
> operator comparison
3 constant literal value 3
* operator multiplication
B identifier name B
DO keyword
A identifier name A
:= operator assignment
A identifier name A
- operator subtraction
1 constant literal value 1
END keyword

as we read it from left to right, but the Fortran statement


10 DO 20 I = 1 . 30

is more deceptive. Readers familiar with Fortran might see it as decoding into
10 label
DO keyword
20 statement label
I INTEGER identifier
= assignment operator
1 INTEGER constant literal
, separator
30 INTEGER constant literal

while those who enjoy perversity might like to see it as it really is:
10 label
DO20I REAL identifier
= assignment operator
1.30 REAL constant literal

One has to look quite hard to distinguish the period from the "expected" comma. (Spaces are
irrelevant in Fortran; one would, of course be perverse to use identifiers with unnecessary and
highly suggestive spaces in them.) While languages like Pascal, Modula-2 and C++ have been
cleverly designed so that lexical analysis can be clearly separated from the rest of the analysis, the
same is obviously not true of Fortran and other languages that do not have reserved keywords.

The syntax analyser or parser groups the tokens produced by the scanner into syntactic structures
- which it does by parsing expressions and statements. (This is analogous to a human analysing a
sentence to find components like "subject", "object" and "dependent clauses"). Often the parser is
combined with the contextual constraint analyser, whose job it is to determine that the
components of the syntactic structures satisfy such things as scope rules and type rules within the
context of the structure being analysed. For example, in Modula-2 the syntax of a while statement is
sometimes described as
WHILE Expression DO StatementSequence END

It is reasonable to think of a statement in the above form with any type of Expression as being
syntactically correct, but as being devoid of real meaning unless the value of the Expression is
constrained (in this context) to be of the Boolean type. No program really has any meaning until it
is executed dynamically. However, it is possible with strongly typed languages to predict at
compile-time that some source programs can have no sensible meaning (that is, statically, before an
attempt is made to execute the program dynamically). Semantics is a term used to describe
"meaning", and so the constraint analyser is often called the static semantic analyser, or simply
the semantic analyser.

The output of the syntax analyser and semantic analyser phases is sometimes expressed in the form
of a decorated abstract syntax tree (AST). This is a very useful representation, as it can be used in
clever ways to optimize code generation at a later stage.
Whereas the concrete syntax of many programming languages incorporates many keywords and
tokens, the abstract syntax is rather simpler, retaining only those components of the language
needed to capture the real content and (ultimately) meaning of the program. For example, whereas
the concrete syntax of a while statement requires the presence of WHILE, DO and END as shown
above, the essential components of the while statement are simply the (Boolean) Expression and the
statements comprising the StatementSequence.

Thus the Modula-2 statement


WHILE (1 < P) AND (P < 9) DO P := P + Q END

or its C++ equivalent


while (1 < P && P < 9) P = P + Q;

are both depicted by the common AST shown in Figure 2.5.

An abstract syntax tree on its own is devoid of some semantic detail; the semantic analyser has the
task of adding "type" and other contextual information to the various nodes (hence the term
"decorated" tree).

Sometimes, as for example in the case of most Pascal compilers, the construction of such a tree is
not explicit, but remains implicit in the recursive calls to procedures that perform the syntax and
semantic analysis.

Of course, it is also possible to construct concrete syntax trees. The Modula-2 form of the statement
WHILE (1 < P) AND (P < 9) DO P := P + Q END

could be depicted in full and tedious detail by the tree shown in Figure 2.6. The reader may have to
make reference to Modula-2 syntax diagrams and the knowledge of Modula-2 precedence rules to
understand why the tree looks so complicated.
The phases just discussed are all analytic in nature. The ones that follow are more synthetic. The
first of these might be an intermediate code generator, which, in practice, may also be integrated
with earlier phases, or omitted altogether in the case of some very simple translators. It uses the
data structures produced by the earlier phases to generate a form of code, perhaps in the form of
simple code skeletons or macros, or ASSEMBLER or even high-level code for processing by an
external assembler or separate compiler. The major difference between intermediate code and
actual machine code is that intermediate code need not specify in detail such things as the exact
machine registers to be used, the exact addresses to be referred to, and so on.

Our example statement


WHILE (1 < P) AND (P < 9) DO P := P + Q END

might produce intermediate code equivalent to


L0 if 1 < P goto L1
goto L3
L1 if P < 9 goto L2
goto L3
L2 P := P + Q
goto L0
L3 continue

Then again, it might produce something like


L0 T1 := 1 < P
T2 := P < 9
if T1 and T2 goto L1
goto L2
L1 P := P + Q
goto L0
L2 continue

depending on whether the implementors of the translator use the so-called sequential conjunction or
short-circuit approach to handling compound Boolean expressions (as in the first case) or the
so-called Boolean operator approach. The reader will recall that Modula-2 and C++ require the
short-circuit approach. However, the very similar language Pascal did not specify that one approach
be preferred above the other.

A code optimizer may optionally be provided, in an attempt to improve the intermediate code in
the interests of speed or space or both. To use the same example as before, obvious optimization
would lead to code equivalent to
L0 if 1 >= P goto L1
if P >= 9 goto L1
P := P + Q
goto L0
L1 continue

The most important phase in the back end is the responsibility of the code generator. In a real
compiler this phase takes the output from the previous phase and produces the object code, by
deciding on the memory locations for data, generating code to access such locations, selecting
registers for intermediate calculations and indexing, and so on. Clearly this is a phase which calls
for much skill and attention to detail, if the finished product is to be at all efficient. Some translators
go on to a further phase by incorporating a so-called peephole optimizer in which attempts are
made to reduce unnecessary operations still further by examining short sequences of generated code
in closer detail.

Below we list the actual code generated by various MS-DOS compilers for this statement. It is
readily apparent that the code generation phases in these compilers are markedly different. Such
differences can have a profound effect on program size and execution speed.
Borland C++ 3.1 (47 bytes) Turbo Pascal (46 bytes)
(with no short circuit evaluation)

CS:A0 BBB702 MOV BX,02B7 CS:09 833E3E0009 CMP WORD PTR[003E],9


CS:A3 C746FE5100 MOV WORD PTR[BP-2],0051 CS:0E 7C04 JL 14
CS:A8 EB07 JMP B1 CS:10 B000 MOV AL,0
CS:AA 8BC3 MOV AX,BX CS:12 EB02 JMP 16
CS:AC 0346FE ADD AX,[BP-2] CS:14 B001 MOV AL,1
CS:AF 8BD8 MOV BX,AX CS:16 8AD0 MOV DL,AL
CS:B1 83FB01 CMP BX,1 CS:18 833E3E0001 CMP WORD PTR[003E],1
CS:B4 7E05 JLE BB CS:1D 7F04 JG 23
CS:B6 B80100 MOV AX,1 CS:1F B000 MOV AL,0
CS:B9 EB02 JMP BD CS:21 EB02 JMP 25
CS:BB 33C0 XOR AX,AX CS:23 B001 MOV AL,01
CS:BD 50 PUSH AX CS:25 22C2 AND AL,DL
CS:BE 83FB09 CMP BX,9 CS:27 08C0 OR AL,AL
CS:C1 7D05 JGE C8 CS:29 740C JZ 37
CS:C3 B80100 MOV AX,1 CS:2B A13E00 MOV AX,[003E]
CS:C6 EB02 JMP CA CS:2E 03064000 ADD AX,[0040]
CS:C8 33C0 XOR AX,AX CS:32 A33E00 MOV [003E],AX
CS:CA 5A POP DX CS:35 EBD2 JMP 9
CS:CB 85D0 TEST DX,AX
CS:CD 75DB JNZ AA

JPI TopSpeed Modula-2 (29 bytes) Stony Brook QuickMod (24 bytes)

CS:19 2E CS: CS:69 BB2D00 MOV BX,2D


CS:1A 8E1E2700 MOV DS,[0027] CS:6C B90200 MOV CX,2
CS:1E 833E000001 CMP WORD PTR[0000],1 CS:6F E90200 JMP 74
CS:23 7E11 JLE 36 CS:72 01D9 ADD CX,BX
CS:25 833E000009 CMP WORD PTR[0000],9 CS:74 83F901 CMP CX,1
CS:2A 7D0A JGE 36 CS:77 7F03 JG 7C
CS:2C 8B0E0200 MOV CX,[0002] CS:79 E90500 JMP 81
CS:30 010E0000 ADD [0000],CX CS:7C 83F909 CMP CX,9
CS:34 EBE3 JMP 19 CS:7F 7CF1 JL 72

A translator inevitably makes use of a complex data structure, known as the symbol table, in which
it keeps track of the names used by the program, and associated properties for these, such as their
type, and their storage requirements (in the case of variables), or their values (in the case of
constants).
As is well known, users of high-level languages are apt to make many errors in the development of
even quite simple programs. Thus the various phases of a compiler, especially the earlier ones, also
communicate with an error handler and error reporter which are invoked when errors are
detected. It is desirable that compilation of erroneous programs be continued, if possible, so that the
user can clean several errors out of the source before recompiling. This raises very interesting
issues regarding the design of error recovery and error correction techniques. (We speak of error
recovery when the translation process attempts to carry on after detecting an error, and of error
correction or error repair when it attempts to correct the error from context - usually a contentious
subject, as the correction may be nothing like what the programmer originally had in mind.)

Error detection at compile-time in the source code must not be confused with error detection at
run-time when executing the object code. Many code generators are responsible for adding
error-checking code to the object program (to check that subscripts for arrays stay in bounds, for
example). This may be quite rudimentary, or it may involve adding considerable code and data
structures for use with sophisticated debugging systems. Such ancillary code can drastically reduce
the efficiency of a program, and some compilers allow it to be suppressed.

Sometimes mistakes in a program that are detected at compile-time are known as errors, and errors
that show up at run-time are known as exceptions, but there is no universally agreed terminology
for this.

Figure 2.4 seems to imply that compilers work serially, and that each phase communicates with the
next by means of a suitable intermediate language, but in practice the distinction between the
various phases often becomes a little blurred. Moreover, many compilers are actually constructed
around a central parser as the dominant component, with a structure rather more like the one in
Figure 2.7.

Exercises

2.5 What sort of problems can you foresee a Fortran compiler having in analysing statements
beginning
IF ( I(J) - I(K) ) ........
CALL IF (4 , ...........
IF (3 .EQ. MAX) GOTO ......
100 FORMAT(X3H)=(I5)

2.6 What sort of code would you have produced had you been coding a statement like "WHILE (1 <
P) AND (P < 9) DO P := P + Q END" into your favourite ASSEMBLER language?

2.7 Draw the concrete syntax tree for the C++ version of the while statement used for illustration in
this section.

2.8 Are there any reasons why short-circuit evaluation should be preferred over the Boolean
operator approach? Can you think of any algorithms that would depend critically on which
approach was adopted?

2.9 Write down a few other high-level constructs and try to imagine what sort of
ASSEMBLER-like machine code a compiler would produce for them.

2.10 What do you suppose makes it relatively easy to compile Pascal? Can you think of any aspects
of Pascal which could prove really difficult?

2.11 We have used two undefined terms which at first seem interchangeable, namely "separate" and
"independent" compilation. See if you can discover what the differences are.

2.12 Many development systems - in particular debuggers - allow a user to examine the object code
produced by a compiler. If you have access to one of these, try writing a few very simple (single
statement) programs, and look at the sort of object code that is generated for them.

2.4 Multi-stage translators


Besides being conceptually divided into phases, translators are often divided into passes, in each of
which several phases may be combined or interleaved. Traditionally, a pass reads the source
program, or output from a previous pass, makes some transformations, and then writes output to an
intermediate file, whence it may be rescanned on a subsequent pass.

These passes may be handled by different integrated parts of a single compiler, or they may be
handled by running two or more separate programs. They may communicate by using their own
specialized forms of intermediate language, they may communicate by making use of internal data
structures (rather than files), or they may make several passes over the same original source code.

The number of passes used depends on a variety of factors. Certain languages require at least two
passes to be made if code is to be generated easily - for example, those where declaration of
identifiers may occur after the first reference to the identifier, or where properties associated with
an identifier cannot be readily deduced from the context in which it first appears. A multi-pass
compiler can often save space. Although modern computers are usually blessed with far more
memory than their predecessors of only a few years back, multiple passes may be an important
consideration if one wishes to translate complicated languages within the confines of small systems.
Multi-pass compilers may also allow for better provision of code optimization, error reporting and
error handling. Lastly, they lend themselves to team development, with different members of the
team assuming responsibility for different passes. However, multi-pass compilers are usually
slower than single-pass ones, and their probable need to keep track of several files makes them
slightly awkward to write and to use. Compromises at the design stage often result in languages that
are well suited to single-pass compilation.

In practice, considerable use is made of two-stage translators in which the first stage is a high-level
translator that converts the source program into ASSEMBLER, or even into some other relatively
high-level language for which an efficient translator already exists. The compilation process would
then be depicted as in Figure 2.8 - our example shows a Modula-3 program being prepared for
execution on a machine that has a Modula-3 to C converter:

It is increasingly common to find compilers for high-level languages that have been implemented
using C, and which themselves produce C code as output. The success of these is based on the
premises that "all modern computers come equipped with a C compiler" and "source code written in
C is truly portable". Neither premise is, unfortunately, completely true. However, compilers written
in this way are as close to achieving the dream of themselves being portable as any that exist at the
present time. The way in which such compilers may be used is discussed further in Chapter 3.

Exercises

2.13 Try to find out which of the compilers you have used are single-pass, and which are
multi-pass, and for the latter, find out how many passes are involved. Which produce relocatable
code needing further processing by linkers or linkage editors?

2.14 Do any of the compilers in use on your system produce ASSEMBLER, C or other such code
during the compilation process? Can you foresee any particular problems that users might
experience in using such compilers?

2.15 One of several compilers that translates from Modula-2 to C is called mtc, and is freely
available from several ftp sites. If you are a Modula-2 programmer, obtain a copy, and experiment
with it.

2.16 An excellent compiler that translates Pascal to C is called p2c, and is widely available for Unix
systems from several ftp sites. If you are a Pascal programmer, obtain a copy, and experiment with
it.

2.17 Can you foresee any practical difficulties in using C as an intermediate language?

2.5 Interpreters, interpretive compilers, and emulators


Compilers of the sort that we have been discussing have a few properties that may not immediately
be apparent. Firstly, they usually aim to produce object code that can run at the full speed of the
target machine. Secondly, they are usually arranged to compile an entire section of code before any
of it can be executed.
In some interactive environments the need arises for systems that can execute part of an application
without preparing all of it, or ones that allow the user to vary his or her course of action on the fly.
Typical scenarios involve the use of spreadsheets, on-line databases, or batch files or shell scripts
for operating systems. With such systems it may be feasible (or even desirable) to exchange some
of the advantages of speed of execution for the advantage of procuring results on demand.

Systems like these are often constructed so as to make use of an interpreter. An interpreter is a
translator that effectively accepts a source program and executes it directly, without, seemingly,
producing any object code first. It does this by fetching the source program instructions one by one,
analysing them one by one, and then "executing" them one by one. Clearly, a scheme like this, if it
is to be successful, places some quite severe constraints on the nature of the source program.
Complex program structures such as nested procedures or compound statements do not lend
themselves easily to such treatment. On the other hand, one-line queries made of a data base, or
simple manipulations of a row or column of a spreadsheet, can be handled very effectively.

This idea is taken quite a lot further in the development of some translators for high-level
languages, known as interpretive compilers. Such translators produce (as output) intermediate
code which is intrinsically simple enough to satisfy the constraints imposed by a practical
interpreter, even though it may still be quite a long way from the machine code of the system on
which it is desired to execute the original program. Rather than continue translation to the level of
machine code, an alternative approach that may perform acceptably well is to use the intermediate
code as part of the input to a specially written interpreter. This in turn "executes" the original
algorithm, by simulating a virtual machine for which the intermediate code effectively is the
machine code. The distinction between the machine code and pseudo-code approaches to execution
is summarized in Figure 2.9.

We may depict the process used in an interpretive compiler running under MS-DOS for a toy
language like Clang, the one illustrated in later chapters, in T-diagram form (see Figure 2.10).

It is not necessary to confine interpreters merely to work with intermediate output from a translator.
More generally, of course, even a real machine can be viewed as a highly specialized interpreter -
one that executes the machine level instructions by fetching, analysing, and then interpreting them
one by one. In a real machine this all happens "in hardware", and hence very quickly. By carrying
on this train of thought, the reader should be able to see that a program could be written to allow
one real machine to emulate any other real machine, albeit perhaps slowly, simply by writing an
interpreter - or, as it is more usually called, an emulator - for the second machine.
For example, we might develop an emulator that runs on a Sun SPARC machine and makes it
appear to be an IBM PC (or the other way around). Once we have done this, we are (in principle) in
a position to execute any software developed for an IBM PC on the Sun SPARC machine -
effectively the PC software becomes portable!

The T-diagram notation is easily extended to handle the concept of such virtual machines. For
example, running Turbo Pascal on our Sun SPARC machine could be depicted by Figure 2.11.

The interpreter/emulator approach is widely used in the design and development both of new
machines themselves, and the software that is to run on those machines.

An interpretive approach may have several points in its favour:

It is far easier to generate hypothetical machine code (which can be tailored towards the
quirks of the original source language) than real machine code (which has to deal with the
uncompromising quirks of real machines).

A compiler written to produce (as output) well-defined pseudo-machine code capable of easy
interpretation on a range of machines can be made highly portable, especially if it is written in
a host language that is widely available (such as ANSI C), or even if it is made available
already implemented in its own pseudo- code.

It can more easily be made "user friendly" than can the native code approach. Since the
interpreter works closer to the source code than does a fully translated program, error
messages and other debugging aids may readily be related to this source.

A whole range of languages may quickly be implemented in a useful form on a wide range of
different machines relatively easily. This is done by producing intermediate code to a
well-defined standard, for which a relatively efficient interpreter should be easy to implement
on any particular real machine.

It proves to be useful in connection with cross-translators such as were mentioned earlier. The
code produced by such translators can sometimes be tested more effectively by simulated
execution on the donor machine, rather than after transfer to the target machine - the delays
inherent in the transfer from one machine to the other may be balanced by the degradation of
execution time in an interpretive simulation.

Lastly, intermediate languages are often very compact, allowing large programs to be
handled, even on relatively small machines. The success of the once very widely used UCSD
Pascal and UCSD p-System stands as an example of what can be done in this respect.
For all these advantages, interpretive systems carry fairly obvious overheads in execution speed,
because execution of intermediate code effectively carries with it the cost of virtual translation into
machine code each time a hypothetical machine instruction is obeyed.

One of the best known of the early portable interpretive compilers was the one developed at
Zürich and known as the "Pascal-P" compiler (Nori et al., 1981). This was supplied in a kit of three
components:

The first component was the source form of a Pascal compiler, written in a very complete
subset of the language, known as Pascal-P. The aim of this compiler was to translate Pascal-P
source programs into a well-defined and well-documented intermediate language, known as
P-code, which was the "machine code" for a hypothetical stack-based computer, known as the
P-machine.

The second component was a compiled version of the first - the P-codes that would be
produced by the Pascal-P compiler, were it to compile itself.

Lastly, the kit contained an interpreter for the P-code language, supplied as a Pascal
algorithm.

The interpreter served primarily as a model for writing a similar program for the target machine, to
allow it to emulate the hypothetical P-machine. As we shall see in a later chapter, emulators are
relatively easy to develop - even, if necessary, in ASSEMBLER - so that this stage was usually
fairly painlessly achieved. Once one had loaded the interpreter - that is to say, the version of it
tailored to a local real machine - into a real machine, one was in a position to "execute" P-code, and
in particular the P-code of the P-compiler. The compilation and execution of a user program could
then be achieved in a manner depicted in Figure 2.12.

Exercises

2.18 Try to find out which of the translators you have used are interpreters, rather than full
compilers.

2.19 If you have access to both a native-code compiler and an interpreter for a programming
language known to you, attempt to measure the loss in efficiency when the interpreter is used to run
a large program (perhaps one that does substantial number-crunching).
Compilers and Compiler Generators © P.D. Terry, 2000

3 COMPILER CONSTRUCTION AND BOOTSTRAPPING


By now the reader may have realized that developing translators is a decidedly non-trivial exercise.
If one is faced with the task of writing a full-blown translator for a fairly complex source language,
or an emulator for a new virtual machine, or an interpreter for a low-level intermediate language,
one would probably prefer not to implement it all in machine code.

Fortunately one rarely has to contemplate such a radical step. Translator systems are now widely
available and well understood. A fairly obvious strategy when a translator is required for an old
language on a new machine, or a new language on an old machine (or even a new language on a
new machine), is to make use of existing compilers on either machine, and to do the development in
a high level language. This chapter provides a few examples that should make this clearer.

3.1 Using a high-level host language


If, as is increasingly common, one’s dream machine M is supplied with the machine coded version
of a compiler for a well-established language like C, then the production of a compiler for one’s
dream language X is achievable by writing the new compiler, say XtoM, in C and compiling the
source (XtoM.C) with the C compiler (CtoM.M) running directly on M (see Figure 3.1). This
produces the object version (XtoM.M) which can then be executed on M.

Even though development in C is much easier than development in machine code, the process is
still complex. As was mentioned earlier, it may be possible to develop a large part of the compiler
source using compiler generator tools - assuming, of course, that these are already available either
in executable form, or as C source that can itself be compiled easily. The hardest part of the
development is probably that associated with the back end, since this is intensely machine
dependent. If one has access to the source code of a compiler like CtoM one may be able to use this
to good avail. Although commercial compilers are rarely released in source form, source code is
available for many compilers produced at academic institutions or as components of the GNU
project carried out under the auspices of the Free Software Foundation.

3.2 Porting a high level translator


The process of modifying an existing compiler to work on a new machine is often known as
porting the compiler. In some cases this process may be almost trivially easy. Consider, for
example, the fairly common scenario where a compiler XtoC for a popular language X has been
implemented in C on machine A by writing a high-level translator to convert programs written in X
to C, and where it is desired to use language X on a machine M that, like A, has already been
blessed with a C compiler of its own. To construct a two-stage compiler for use on either machine,
all one needs to do, in principle, is to install the source code for XtoC on machine M and recompile
it.

Such an operation is conveniently represented in terms of T-diagrams chained together. Figure


3.2(a) shows the compilation of the X to C compiler, and Figure 3.2(b) shows the two-stage
compilation process needed to compile programs written in X to M-code.

The portability of a compiler like XtoC.C is almost guaranteed, provided that it is itself written in
"portable" C. Unfortunately, or as Mr. Murphy would put it, "interchangeable parts don’t" (more
explicitly, "portable C isn’t"). Some time may have to be spent in modifying the source code of
XtoC.C before it is acceptable as input to CtoM.M, although it is to be hoped that the developers of
XtoC.C will have used only standard C in their work, and used pre-processor directives that allow
for easy adaptation to other systems.

If there is an initial strong motivation for making a compiler portable to other systems it is, indeed,
often written so as to produce high-level code as output. More often, of course, the original
implementation of a language is written as a self-resident translator with the aim of directly
producing machine code for the current host system.

3.3 Bootstrapping
All this may seem to be skirting around a really nasty issue - how might the first high-level
language have been implemented? In ASSEMBLER? But then how was the assembler for
ASSEMBLER produced?

A full assembler is itself a major piece of software, albeit rather simple when compared with a
compiler for a really high level language, as we shall see. It is, however, quite common to define
one language as a subset of another, so that subset 1 is contained in subset 2 which in turn is
contained in subset 3 and so on, that is:
One might first write an assembler for subset 1 of ASSEMBLER in machine code, perhaps on a
load-and-go basis (more likely one writes in ASSEMBLER, and then hand translates it into
machine code). This subset assembler program might, perhaps, do very little other than convert
mnemonic opcodes into binary form. One might then write an assembler for subset 2 of
ASSEMBLER in subset 1 of ASSEMBLER, and so on.

This process, by which a simple language is used to translate a more complicated program, which
in turn may handle an even more complicated program and so on, is known as bootstrapping, by
analogy with the idea that it might be possible to lift oneself off the ground by tugging at one’s
boot-straps.

3.4 Self-compiling compilers


Once one has a working system, one can start using it to improve itself. Many compilers for popular
languages were first written in another implementation language, as implied in section 3.1, and then
rewritten in their own source language. The rewrite gives source for a compiler that can then be
compiled with the compiler written in the original implementation language. This is illustrated in
Figure 3.3.

Clearly, writing a compiler by hand not once, but twice, is a non-trivial operation, unless the
original implementation language is close to the source language. This is not uncommon: Oberon
compilers could be implemented in Modula-2; Modula-2 compilers, in turn, were first implemented
in Pascal (all three are fairly similar), and C++ compilers were first implemented in C.

Developing a self-compiling compiler has four distinct points to recommend it. Firstly, it
constitutes a non-trivial test of the viability of the language being compiled. Secondly, once it has
been done, further development can be done without recourse to other translator systems. Thirdly,
any improvements that can be made to its back end manifest themselves both as improvements to
the object code it produces for general programs and as improvements to the compiler itself. Lastly,
it provides a fairly exhaustive self-consistency check, for if the compiler is used to compile its own
source code, it should, of course, be able to reproduce its own object code (see Figure 3.4).

Furthermore, given a working compiler for a high-level language it is then very easy to produce
compilers for specialized dialects of that language.
3.5 The half bootstrap
Compilers written to produce object code for a particular machine are not intrinsically portable.
However, they are often used to assist in a porting operation. For example, by the time that the first
Pascal compiler was required for ICL machines, the Pascal compiler available in Zürich (where
Pascal had first been implemented on CDC mainframes) existed in two forms (Figure 3.5).

The first stage of the transportation process involved changing PasToCDC.Pas to generate ICL
machine code - thus producing a cross compiler. Since PasToCDC.Pas had been written in a
high-level language, this was not too difficult to do, and resulted in the compiler PasToICL.Pas.

Of course this compiler could not yet run on any machine at all. It was first compiled using
PasToCDC.CDC, on the CDC machine (see Figure 3.6(a)). This gave a cross-compiler that could
run on CDC machines, but still not, of course, on ICL machines. One further compilation of
PasToICL.Pas, using the cross-compiler PasToICL.CDC on the CDC machine, produced the final
result, PasToICL.ICL (Figure 3.6(b)).
The final product (PasToICL.ICL) was then transported on magnetic tape to the ICL machine, and
loaded quite easily. Having obtained a working system, the ICL team could (and did) continue
development of the system in Pascal itself.

This porting operation was an example of what is known as a half bootstrap system. The work of
transportation is essentially done entirely on the donor machine, without the need for any translator
in the target machine, but a crucial part of the original compiler (the back end, or code generator)
has to be rewritten in the process. Clearly the method is hazardous - any flaws or oversights in
writing PasToICL.Pas could have spelled disaster. Such problems can be reduced by minimizing
changes made to the original compiler. Another technique is to write an emulator for the target
machine that runs on the donor machine, so that the final compiler can be tested on the donor
machine before being transferred to the target machine.

3.6 Bootstrapping from a portable interpretive compiler


Because of the inherent difficulty of the half bootstrap for porting compilers, a variation on the full
bootstrap method described above for assemblers has often been successfully used in the case of
Pascal and other similar high-level languages. Here most of the development takes place on the
target machine, after a lot of preliminary work has been done on the donor machine to produce an
interpretive compiler that is almost portable. It will be helpful to illustrate with the well-known
example of the Pascal-P implementation kit mentioned in section 2.5.

Users of this kit typically commenced operations by implementing an interpreter for the P-machine.
The bootstrap process was then initiated by developing a compiler (PasPtoM.PasP) to translate
Pascal-P source programs to the local machine code. This compiler could be written in Pascal-P
source, development being guided by the source of the Pascal-P to P-code compiler supplied as part
of the kit. This new compiler was then compiled with the interpretive compiler (PasPtoP.P) from
the kit (Figure 3.7(a)) and the source of the Pascal to M-code compiler was then compiled by this
new compiler, interpreted once again by the P-machine, to give the final product, PasPtoM.M
(Figure 3.7(b)).

The Zürich P-code interpretive compiler could be, and indeed was, used as a highly portable
development system. It was employed to remarkable effect in developing the UCSD Pascal system,
which was the first serious attempt to implement Pascal on microcomputers. The UCSD Pascal
team went on to provide the framework for an entire operating system, editors and other utilities -
all written in Pascal, and all compiled into a well-defined P-code object code. Simply by providing
an alternative interpreter one could move the whole system to a new microcomputer system
virtually unchanged.

3.7 A P-code assembler


There is, of course, yet another way in which a portable interpretive compiler kit might be used.
One might commence by writing a P-code to M-code assembler, probably a relatively simple task.
Once this has been produced one would have the assembler depicted in Figure 3.8.

The P-codes for the P-code compiler would then be assembled by this system to give another cross
compiler (Figure 3.9(a)), and the same P-code/M-code assembler could then be used as a back-end
to the cross compiler (Figure 3.9(b)).

Exercises

3.1 Draw the T-diagram representations for the development of a P-code to M-code assembler,
assuming that you have a C++ compiler available on the target system.

3.2 Later in this text we shall develop an interpretive compiler for a small language called Clang,
using C++ as the host language. Draw T-diagram representations of the various components of the
system as you foresee them.

Further reading

A very clear exposition of bootstrapping is to be found in the book by Watt (1993). The ICL
bootstrap is further described by Welsh and Quinn (1972). Other early insights into bootstrapping
are to be found in papers by Lecarme and Peyrolle-Thomas (1973), by Nori et al. (1981), and
Cornelius, Lowman and Robson (1984).
Compilers and Compiler Generators © P.D. Terry, 2000

4 MACHINE EMULATION
In Chapter 2 we discussed the use of emulation or interpretation as a tool for programming
language translation. In this chapter we aim to discuss hypothetical machine languages and the
emulation of hypothetical machines for these languages in more detail. Modern computers are
among the most complex machines ever designed by the human mind. However, this is a text on
programming language translation and not on electronic engineering, and our restricted discussion
will focus only on rather primitive object languages suited to the simple translators to be discussed
in later chapters.

4.1 Simple machine architecture


Many CPU (central processor unit) chips used in modern computers have one or more internal
registers or accumulators, which may be regarded as highly local memory where simple
arithmetic and logical operations may be performed, and between which local data transfers may
take place. These registers may be restricted to the capacity of a single byte (8 bits), or, as is typical
of most modern processors, they may come in a variety of small multiples of bytes or machine
words.

One fundamental internal register is the instruction register (IR), through which moves the
bitstrings (bytes) representing the fundamental machine-level instructions that the processor can
obey. These instructions tend to be extremely simple - operations such as "clear a register" or
"move a byte from one register to another" being the typical order of complexity. Some of these
instructions may be completely defined by a single byte value. Others may need two or more bytes
for a complete definition. Of these multi-byte instructions, the first usually denotes an operation,
and the rest relate either to a value to be operated upon, or to the address of a location in memory at
which can be found the value to be operated upon.

The simplest processors have only a few data registers, and are very limited in what they can
actually do with their contents, and so processors invariably make provision for interfacing to the
memory of the computer, and allow transfers to take place along so-called bus lines between the
internal registers and the far greater number of external memory locations. When information is to
be transferred to or from memory, the CPU places the appropriate address information on the
address bus, and then transmits or receives the data itself on the data bus. This is illustrated in
Figure 4.1.
The memory may simplistically be viewed as a one-dimensional array of byte values, analogous to
what might be described in high-level language terms by declarations like the following
TYPE
ADDRESS = CARDINAL [0 .. MemSize - 1];
BYTES = CARDINAL [0 .. 255];
VAR
Mem : ARRAY ADDRESS OF BYTES;

in Modula-2, or, in C++ (which does not provide for the subrange types so useful in this regard)
typedef unsigned char BYTES;
BYTES Mem[MemSize];

Since the memory is used to store not only "data" but also "instructions", another important internal
register in a processor, the so-called program counter or instruction pointer (denoted by PC or
IP), is used to keep track of the address in memory of the next instruction to be fed to the
processor’s instruction register (IR).

Perhaps it will be helpful to think of the processor itself in high-level terms:


TYPE
PROCESSOR = struct processor {
RECORD BYTES IR;
IR, BYTES R1, R2, R3;
R1, R2, R3 : BYTES; unsigned PC;
PC : ADDRESS; };
END;
VAR processor cpu;
CPU : PROCESSOR;

The operation of the machine is repeatedly to fetch a byte at a time from memory (along the data
bus), place it in the IR, and then execute the operation which this byte represents. Multi-byte
instructions may require the fetching of further bytes before the instruction itself can be decoded
fully by the CPU, of course. After the instruction denoted by the contents of IR has been executed,
the value of PC will have been changed to point to the next instruction to be fetched. This
fetch-execute cycle may be described by the following algorithm:
BEGIN
CPU.PC := initialValue; (* address of first code instruction *)
LOOP
CPU.IR := Mem[CPU.PC]; (* fetch *)
Increment(CPU.PC); (* bump PC in anticipation *)
Execute(CPU.IR); (* affecting other registers, memory, PC *)
(* handle machine interrupts if necessary *)
END
END.

Normally the value of PC alters by small steps (since instructions are usually stored in memory in
sequence); execution of branch instructions may, however, have a rather more dramatic effect. So
might the occurrence of hardware interrupts, although we shall not discuss interrupt handling
further.

A program for such a machine consists, in the last resort, of a long string of byte values. Were these
to be written on paper (as binary, decimal, or hexadecimal values), they would appear pretty
meaningless to the human reader. We might, for example, find a section of program reading
25 45 21 34 34 30 45

Although it may not be obvious, this might be equivalent to a high-level statement like
Price := 2 * Price + MarkUp;

Machine-level programming is usually performed by associating mnemonics with the recognizable


operations, like HLT for "halt" or ADD for "add to register". The above code is far more
comprehensible when written (with commentary) as
LDA 45 ; load accumulator with value stored in memory location 45
SHL ; shift accumulator one bit left (multiply by 2)
ADI 34 ; add 34 to the accumulator
STA 45 ; store the value in the accumulator at memory location 45

Programs written in an assembly language - which have first to be assembled before they can be
executed - usually make use of other named entities, for example
MarkUp EQU 34 ; CONST MarkUp = 34;
LDA Price ; CPU.A := Price;
SHL ; CPU.A := 2 * CPU.A;
ADI MarkUp ; CPU.A := CPU.A + 34;
STA Price ; Price := CPU.A;

When we use code fragments such as these for illustration we shall make frequent use of
commentary showing an equivalent fragment written in a high-level language. Commentary follows
the semicolon on each line, a common convention in assembler languages.

4.2 Addressing modes


As the examples given earlier suggest, programs prepared at or near the machine level frequently
consist of a sequence of simple instructions, each involving a machine-level operation and one or
more parameters.

An example of a simple operation expressed in a high-level language might be


AmountDue := Price + Tax;

Some machines and assembler languages provide for such operations in terms of so-called
three-address code, in which an operation - denoted by a mnemonic usually called the opcode - is
followed by two operands and a destination. In general this takes the form
operation destination, operand 1, operand 2

for example
ADD AmountDue, Price, Tax

We may also express this in a general sense as a function call


destination := operation(operand 1, operand 2 )

which helps to stress the important idea that the operands really denote "values", while the
destination denotes a processor register, or an address in memory where the result is to be stored.

In many cases this generality is restricted (that is, the machine suffers from non-orthogonality in
design). Typically the value of one operand is required to be the value originally stored at the
destination. This corresponds to high-level statements like
Price := Price * InflationFactor;

and is mirrored at the low-level by so-called two-address code of the general form
operation destination, operand
for example
MUL Price, InflationFactor

In passing, we should point out an obvious connection between some of the assignment operations
in C++ and two-address code. In C++ the above assignment would probably have been written
Price *= InflationFactor;

which, while less transparent to a Modula-2 programmer, is surely a hint to a C++ compiler to
generate code of this form. (Perhaps this example may help you understand why C++ is regarded by
some as the world’s finest assembly language!)

In many real machines even general two-address code is not found at the machine level. One of
destination and operand might be restricted to denoting a machine register (the other one might
denote a machine register, or a constant, or a machine address). This is often called one and a half
address code, and is exemplified by
MOV R1, Value ; CPU.R1 := Value
ADD Answer, R1 ; Answer := Answer + CPU.R1
MOV Result, R2 ; Result := CPU.R2

Finally, in so-called accumulator machines we may be restricted to one-address code, where the
destination is always a machine register (except for those operations that copy (store) the contents
of a machine register into memory). In some assembler languages such instructions may still appear
to be of the two-address form, as above. Alternatively they might be written in terms of opcodes
that have the register implicit in the mnemonic, for example
LDA Value ; CPU.A := Value
ADA Answer ; CPU.A := CPU.A + Answer
STB Result ; Result := CPU.B

Although many of these examples might give the impression that the corresponding machine level
operations require multiple bytes for their representation, this is not necessarily true. For example,
operations that only involve machine registers, exemplified by
MOV R1, R2 ; CPU.R1 := CPU.R2
LDA B ; CPU.A := CPU.B
TAX ; CPU.X := CPU.A

might require only a single byte - as would be most obvious in an assembler language that used the
third representation. The assembly of such programs is be eased considerably by a simple and
self-consistent notation for the source code, a subject that we shall consider further in a later
chapter.

In those instructions that do involve the manipulation of values other than those in the machine
registers alone, multi-byte instructions are usually required. The first byte typically specifies the
operation itself (and possibly the register or registers that are involved), while the remaining bytes
specify the other values (or the memory addresses of the other values) involved. In such
instructions there are several ways in which the ancillary bytes might be used. This variety gives
rise to what are known as different addressing modes for the processor, and whose purpose it is to
provide an effective address to be used in an instruction. Exactly which modes are available varies
tremendously from processor to processor, and we can mention only a few representative examples
here. The various possibilities may be distinguished in some assembler languages by the use of
different mnemonics for what at first sight appear to be closely related operations. In other
assembler languages the distinction may be drawn by different syntactic forms used to specify the
registers, addresses or values. One may even find different assembler languages for a common
processor.

In inherent addressing the operand is implicit in the opcode itself, and often the instruction is
contained in a single byte. For example, to clear a machine register named A we might have
CLA or CLR A ; CPU.A := 0

Again we stress that, though the second form seems to have two components, it does not always
imply the use of two bytes of code at the machine level.

In immediate addressing the ancillary bytes for an instruction typically give the actual value that
is to be combined with a value in a register. Examples might be
ADI 34 or ADD A, #34 ; CPU.A := CPU.A + 34

In these two addressing modes the use of the word "address" is almost misleading, as the value of
the ancillary bytes may often have nothing to do with a memory address at all. In the modes now to
be discussed the connection with memory addresses is far more obvious.

In direct or absolute addressing the ancillary bytes typically specify the memory address of the
value that is to be retrieved or combined with the value in a register, or specify where a register
value is to be stored. Examples are
LDA 34 or MOV A, 34 ; CPU.A := Mem[34]
STA 45 MOV 45, A ; Mem[45] := CPU.A
ADD 38 ADD A, 38 ; CPU.A := CPU.A + Mem[38]

Beginners frequently confuse immediate and direct addressing, a situation not improved by the fact
that there is no consistency in notation between different assembler languages, and there may even
be a variety of ways of expressing a particular addressing mode. For example, for the Intel 80x86
processors as used in the IBM-PC and compatibles, low-level code is written in a two-address form
similar to that shown above - but the immediate mode is denoted without needing a special symbol
like #, while the direct mode may have the address in brackets:
ADD AX, 34 ; CPU.AX := CPU.AX + 34 Immediate
MOV AX, [34] ; CPU.AX := Mem[34] Direct

In register-indexed addressing one of the operands in an instruction specifies both an address and
also an index register, whose value at the time of execution may be thought of as specifying the
subscript to an array stored from that address
LDX 34 or MOV A, 34[X] ; CPU.A := Mem[34 + CPU.X]
STX 45 MOV 45[X], A ; Mem[45+CPU.X] := CPU.A
ADX 38 ADD A, 38[X] ; CPU.A := CPU.A + Mem[38+CPU.X]

In register-indirect addressing one of the operands in an instruction specifies a register whose


value at the time of execution gives the effective address where the value of the operand is to be
found. This relates to the concept of pointers as used in Modula-2, Pascal and C++.
MOV R1, @R2 ; CPU.R1 := Mem[CPU.R2]
MOV AX, [BX] ; CPU.AX := Mem[CPU.BX]

Not all the registers in a machine can necessarily be used in these ways. Indeed, some machines
have rather awkward restrictions in this regard.

Some processors allow for very powerful variations on indexed and indirect addressing modes. For
example, in memory-indexed addressing, a single operand may specify two memory addresses -
the first of which gives the address of the first element of an array, and the second of which gives
the address of a variable whose value will be used as a subscript to the array.
MOV R1, 400[100] ; CPU.R1 := Mem[400 + Mem[100]]

Similarly, in memory-indirect addressing one of the operands in an instruction specifies a


memory address at which will be found a value that forms the effective address where another
operand is to be found.
MOV R1, @100 ; CPU.R1 := Mem[Mem[100]]

This mode is not as commonly found as the others; where it does occur it directly corresponds to
the use of pointer variables in languages that support them. Code like
TYPE
ARROW = POINTER TO CARDINAL; typedef int *ARROW;
VAR
Arrow : ARROW; ARROW Arrow;
Target : CARDINAL; int Target;
BEGIN
Target := Arrow^; Target = *Arrow;

might translate to equivalent code in assembler like


MOV AX, @Arrow
MOV Target, AX

or even
MOV Target, @Arrow

where, once again, we can see an immediate correspondence between the syntax in C++ and the
corresponding assembler.

Finally, in relative addressing an operand specifies an amount by which the current program count
register PC must be incremented or decremented to find the actual address of interest. This is
chiefly found in "branching" instructions, rather than in those that move data between various
registers and/or locations in memory.

Further reading

Most books on assembler level programming have far deeper discussions of the subject of
addressing modes than we have presented. Two very readable accounts are to be found in the books
by Wakerly (1981) and MacCabe (1993). A deeper discussion of machine architectures is to be
found in the book by Hennessy and Patterson (1990).

4.3 Case study 1 - A single-accumulator machine


Although sophisticated processors may have several registers, their basic principles - especially as
they apply to emulation - may be illustrated by the following model of a single-accumulator
processor and computer, very similar to one suggested by Wakerly (1981). Here we shall take
things to extremes and presume the existence of a system with all registers only 1 byte (8 bits)
wide.
4.3.1 Machine architecture

Diagrammatically we might represent this machine as in Figure 4.2.

The symbols in this diagram refer to the following components of the machine

ALU is the arithmetic logic unit, where arithmetic and logical operations are actually
performed.

A is the 8-bit accumulator, a register for doing arithmetic or logical operations.

SP is an 8-bit stack pointer, a register that points to an area in memory that may be
utilized as a stack.

X is an 8-bit index register, which is used in indexing areas of memory which


conceptually form data arrays.

Z, P, C are single bit condition flags or status registers, which are set "true" when an
operation causes a register to change to a zero value, or to a positive value, or to
propagate a carry, respectively.

IR is the 8-bit instruction register, in which is held the byte value of the instruction
currently being executed.

PC is the 8-bit program counter, which contains the address in memory of the
instruction that is next to be executed.

EAR is the effective address register, which contains the address of the byte of data
which is being manipulated by the current instruction.

The programmer’s model of this sort of machine is somewhat simpler - it consists of a number of
"variables" (in the C++ or Modula-2 sense), each of which is one byte in capacity. Some of these
correspond to processor registers, while the others form the random access read/write (RAM)
memory, of which we have assumed there to be 256 bytes, addressed by the values 0 through 255.
In this memory, as usual, will be stored both the data and the instructions for the program under
execution. The processor, its registers, and the associated RAM memory can be thought of as
though they were described by declarations like
TYPE
BYTES = CARDINAL [0 .. 255]; typedef unsigned char bytes;
PROCESSOR = RECORD struct processor {
A, SP, X, IR, PC : BYTES; bytes a, sp, x, ir, pc;
Z, P, C : BOOLEAN; bool z, p, c;
END; };
TYPE STATUS = (running, finished, typedef enum { running, finished,
nodata, baddata, nodata, baddata, badop
badop); } status;
VAR
CPU : PROCESSOR; processor cpu;
Mem : ARRAY BYTES OF BYTES; bytes mem[256];
PS : STATUS; status ps;

where the concept of the processor status PS has been introduced in terms of an enumeration that
defines the states in which an emulator might find itself.

4.3.2 Instruction set

Some machine operations are described by a single byte. Others require two bytes, and have the
format
Byte 1 Opcode
Byte 2 Address field

The set of machine code functions available is quite small. Those marked * affect the P and Z flags,
and those marked + affect the C flag. An informal description of their semantics follows:

Mnemonic Hex Decimal Function


opcode

NOP 00h 0 No operation (this might be used to set a break point in an emulator)
CLA 01h 1 Clear accumulator A
CLC + 02h 2 Clear carry bit C
CLX 03h 3 Clear index register X
CMC + 04h 4 Complement carry bit C
INC * 05h 5 Increment accumulator A by 1
DEC * 06h 6 Decrement accumulator A by 1
INX * 07h 7 Increment index register X by 1
DEX * 08h 8 Decrement index register X by 1
TAX 09h 9 Transfer accumulator A to index register X
INI * 0Ah 10 Load accumulator A with integer read from input in decimal
INH * 0Bh 11 Load accumulator A with integer read from input in hexadecimal
INB * 0Ch 12 Load accumulator A with integer read from input in binary
INA * 0Dh 13 Load accumulator A with ASCII value read from input (a single character)
OTI 0Eh 14 Write value of accumulator A to output as a signed decimal number
OTC 0Fh 15 Write value of accumulator A to output as an unsigned decimal number
OTH 10h 16 Write value of accumulator A to output as an unsigned hexadecimal number
OTB 11h 17 Write value of accumulator A to output as an unsigned binary number
OTA 12h 18 Write value of accumulator A to output as a single character
PSH 13h 19 Decrement SP and push value of accumulator A onto stack
POP * 14h 20 Pop stack into accumulator A and increment SP
SHL + * 15h 21 Shift accumulator A one bit left
SHR + * 16h 22 Shift accumulator A one bit right
RET 17h 23 Return from subroutine (return address popped from stack)
HLT 18h 24 Halt program execution

The above are all single-byte instructions. The following are all double-byte instructions.

LDA B * 19h 25 Load accumulator A directly with contents of location whose address is
given as B
LDX B * 1Ah 26 Load accumulator A with contents of location whose address is given as B,
indexed by the value of X (that is, an address computed as the value of B + X)
LDI B * 1Bh 27 Load accumulator A with the immediate value B
LSP B 1Ch 28 Load stack pointer SP with contents of location whose address is given as B
LSI B 1Dh 29 Load stack pointer SP immediately with the value B
STA B 1Eh 30 Store accumulator A on the location whose address is given as B
STX B 1Fh 31 Store accumulator A on the location whose address is given as B, indexed
by the value of X
ADD B + * 20h 32 Add to accumulator A the contents of the location whose address is given as B
ADX B + * 21h 33 Add to accumulator A the contents of the location whose address is given as
B,indexed by the value of X
ADI B + * 22h 34 Add the immediate value B to accumulator A
ADC B + * 23h 35 Add to accumulator A the value of the carry bit C plus the contents of the
location whose address is given as B
ACX B + * 24h 36 Add to accumulator A the value of the carry bit C plus the contents of the
location whose address is given as B, indexed by the value of X
ACI B + * 25h 37 Add the immediate value B plus the value of the carry bit C to accumulator A
SUB B + * 26h 38 Subtract from accumulator A the contents of the location whose address is
given as B
SBX B + * 27h 39 Subtract from accumulator A the contents of the location whose address is
given as B, indexed by the value of X
SBI B + * 28h 40 Subtract the immediate value B from accumulator A
SBC B + * 29h 41 Subtract from accumulator A the value of the carry bit C plus the contents
of the location whose address is given as B
SCX B + * 2Ah 42 Subtract from accumulator A the value of the carry bit C plus the contents
of the location whose address is given as B, indexed by the value of X
SCI B + * 2Bh 43 Subtract the immediate value B plus the value of the carry bit C from
accumulator A
CMP B + * 2Ch 44 Compare accumulator A with the contents of the location whose address is
given as B
CPX B + * 2Dh 45 Compare accumulator A with the contents of the location whose address is
given as B, indexed by the value of X
CPI B + * 2Eh 46 Compare accumulator A directly with the value B

These comparisons are done by virtual subtraction of the operand from A, and setting the flags P
and Z as appropriate

ANA B + * 2Fh 47 Bitwise AND accumulator A with the contents of the location whose address
is given as B
ANX B + * 30h 48 Bitwise AND accumulator A with the contents of the location whose address
is given as B, indexed by the value of X
ANI B + * 31h 49 Bitwise AND accumulator A with the immediate value B
ORA B + * 32h 50 Bitwise OR accumulator A with the contents of the location whose address
is given as B
ORX B + * 33h 51 Bitwise OR accumulator A with the contents of the location whose address
is given as B, indexed by the value of X
ORI B + * 34h 52 Bitwise OR accumulator A with the immediate value B

BRN B 35h 53 Branch to the address given as B


BZE B 36h 54 Branch to the address given as B if the Z condition flag is set
BNZ B 37h 55 Branch to the address given as B if the Z condition flag is unset
BPZ B 38h 56 Branch to the address given as B if the P condition flag is set
BNG B 39h 57 Branch to the address given as B if the P condition flag is unset
BCC B 3Ah 58 Branch to the address given as B if the C condition flag is unset
BCS B 3Bh 59 Branch to the address given as B if the C condition flag is set

JSR B 3Ch 60 Call subroutine whose address is B, pushing return address onto the stack

Most of the operations listed above are typical of those found in real machines. Notable exceptions
are provided by the I/O (input/output) operations. Most real machines have extremely primitive
facilities for doing anything like this directly, but for the purposes of this discussion we shall cheat
somewhat and assume that our machine has several very powerful single-byte opcodes for handling
I/O. (Actually this is not cheating too much, for some macro-assemblers allow instructions like this
which are converted into procedure calls into part of an underlying operating system, stored perhaps
in a ROM BIOS).
A careful examination of the machine and its instruction set will show some features that are
typical of real machines. Although there are three data registers, A, X and SP, two of them (X and
SP) can only be used in very specialized ways. For example, it is possible to transfer a value from A
to X, but not vice versa, and while it is possible to load a value into SP it is not possible to examine
the value of SP at a later stage. The logical operations affect the carry bit (they all unset it), but,
surprisingly, the INC and DEC operations do not.

It is this model upon which we shall build an emulator in section 4.3.4. In a sense the formal
semantics of these opcodes are then embodied directly in the operational semantics of the machine
(or pseudo-machine) responsible for executing them.

Exercises

4.1 Which addressing mode is used in each of the operations defined above? Which addressing
modes are not represented?

4.2 Many 8-bit microprocessors have 2-byte (16-bit) index registers, and one, two, and three-byte
instructions (and even longer). What peculiar or restrictive features does our machine possess,
compared to such processors?

4.3 As we have already commented, informal descriptions in English, as we have above, are not as
precise as semantics that are formulated mathematically. Compare the informal description of the
INC operation with the following:

INC * 05h 5 A := (A + 1) mod 256; Z := A = 0; P := A IN {0 ... 127}

Try to express the semantics of each of the other machine instructions in a similar way.

4.3.3 A specimen program

Some examples of code for this machine may help the reader’s understanding. Consider the
problem of reading a number and then counting the number of non-zero bits in its binary
representation.

Example 4.1

The listing below shows a program to solve this problem coded in an ASSEMBLER language
based on the mnemonics given previously, as it might be listed by an assembler program, showing
the hexadecimal representation of each byte and where it is located in memory.
00 BEG ; Count the bits in a number
00 0A INI ; Read(A)
01 LOOP ; REPEAT
01 16 SHR ; A := A DIV 2
02 3A 0D BCC EVEN ; IF A MOD 2 # 0 THEN
04 1E 13 STA TEMP ; TEMP := A
06 19 14 LDA BITS
08 05 INC
09 1E 14 STA BITS ; BITS := BITS + 1
0B 19 13 LDA TEMP ; A := TEMP
0D 37 01 EVEN BNZ LOOP ; UNTIL A = 0
0F 19 14 LDA BITS ;
11 0E OTI ; Write(BITS)
12 18 HLT ; terminate execution
13 TEMP DS 1 ; VAR TEMP : BYTE
14 00 BITS DC 0 ; BITS : BYTE
15 END

Example 4.2 (absolute byte values)

In a later chapter we shall discuss how this same program can be translated into the following
corresponding absolute format (expressed this time as decimal numbers):
10 22 58 13 30 19 25 20 5 30 20 25 19 55 1 25 20 14 24 0 0

Example 4.3 (mnemonics with absolute address fields)

For the moment, we shall allow ourselves to consider the absolute form as equivalent to a form in
which the mnemonics still appear for the sake of clarity, but where the operands have all been
converted into absolute (decimal) addresses and values:
INI
SHR
BCC 13
STA 19
LDA 20
INC
STA 20
LDA 19
BNZ 1
LDA 20
OTI
HLT
0
0

Exercises

4.4 The machine does not possess an instruction for negating the value in the accumulator. What
code would one have to write to be able to achieve this?

4.5 Similarly, it does not possess instructions for multiplication and division. Is it possible to use
the existing instructions to develop code for doing these operations? If so, how efficiently can they
be done?

4.6 Try to write programs for this machine that will

(a) Find the largest of three numbers.

(b) Find the largest and the smallest of a list of numbers terminated by a zero (which is
not regarded as a member of the list).

(c) Find the average of a list of non-zero numbers, the list being terminated by a zero.

(d) Compute N! for small N. Try using an iterative as well as a recursive approach.

(e) Read a word and then write it backwards. The word is terminated with a period. Try
using an "array", or alternatively, the "stack".

(f) Determine the prime numbers between 0 and 255.

(g) Determine the longest repeated sequence in a sequence of digits terminated with
zero. For example, for data reading 1 2 3 3 3 3 4 5 4 4 4 4 4 4 4 6 5 5 report that "4
appeared 7 times".

(h) Read an input sequence of numbers terminated with zero, and then extract the
embedded monotonically increasing sequence. For example, from 1 2 12 7 4 14 6 23
extract the sequence 1 2 12 14 23.

(i) Read a small array of integers or characters and sort them into order.

(j) Search for and report on the largest byte in the program code itself.

(k) Search for and report on the largest byte currently in memory.

(l) Read a piece of text terminated with a period, and then report on how many times
each letter appeared. To make things interesting, ignore the difference between upper
and lower case.

(m) Repeat some of the above problems using 16-bit arithmetic (storing values as pairs
of bytes, and using the "carry" operations to perform extended arithmetic).

4.7 Based on your experiences with Exercise 4.6, comment on the usefulness, redundancy and any
other features of the code set for the machine.

4.3.4 An emulator for the single-accumulator machine

Although a processor for our machine almost certainly does not exist "in silicon", its action may
easily be simulated "in software". Essentially we need only to write an emulator that models the
fetch-execute cycle of the machine, and we can do this in any suitable language for which we
already have a compiler on a real machine.

Languages like Modula-2 or C++ are highly suited to this purpose. Not only do they have
"bit-twiddling" capabilities for performing operations like "bitwise and", they have the advantage
that one can implement the various phases of translators and emulators as coherent, clearly
separated modules (in Modula-2) or classes (in C++). Extended versions of Pascal, such as Turbo
Pascal, also provide support for such modules in the form of units. C is also very suitable on the
first score, but is less well equipped to deal with clearly separated modules, as the header file
mechanism used in C is less watertight than the mechanisms in the other languages.

In modelling our hypothetical machine in Modula-2 or C++ it will thus be convenient to define an
interface in the usual way by means of a definition module, or by the public interface to a class. (In
this text we shall illustrate code in C++; equivalent code in Modula-2 and Turbo Pascal will be
found on the diskette that accompanies the book.)

The main responsibility of the interface is to declare an emulator routine for interpreting the code
stored in the memory of the machine. For expediency we choose to extend the interface to expose
the values of the operations, and the memory itself, and to provide various other useful facilities
that will help us develop an assembler or compiler for the machine in due course. (In this, and in
other interfaces, "private" members are not shown.)
// machine instructions - order is significant
enum MC_opcodes {
MC_nop, MC_cla, MC_clc, MC_clx, MC_cmc, MC_inc, MC_dec, MC_inx, MC_dex,
MC_tax, MC_ini, MC_inh, MC_inb, MC_ina, MC_oti, MC_otc, MC_oth, MC_otb,
Another Random Document on
Scribd Without Any Related Topics
Irrevocably agreed on with another.
Ces. By this ‘too late,’ I think you only mean
To tantalize my too late declaration.
If that be your intent, I am well punisht
Already; be content with my contrition.
You say you love me; and would well desire
To see me wed your sister; seal at once
My happiness, nor chill the opening day,
Nor my love’s blossom, by a lingering ‘Yea.’
Fel. Indeed, indeed, my Cesar, not to revenge
Delay of speech, or insufficient token,
But with repeated sorrow I repeat,
My sister’s hand is pledged beyond recall,
And to another; whom, for certain reasons,
I dare not name, not even to herself,
As yet—
Ces. If I survive, ’tis that fate knows
How much more terrible is life than death!
Don Felix, you have well revenged yourself
Upon my vain ambition, speech delay’d,
And signs that you would not articulate;
But let my fate be as it will, may hers,
Hers, yea, and his whose life you link to hers,
Be so indissolubly prosperous,
That only death forget to envy them!
Farewell.
Fel. Farewell then: and remember, Cesar,
Let not this luckless business interrupt
Our long and loving intimacy.
Ces. Nay.
It shall not, cannot, Felix, come what may.
[Exeunt severally.
Enter Prince.
Prince. When in my love’s confusion and excess
I fancy many a fond unlikely chance,
Desire grows stronger, resolution less,
I linger more the more I would advance.
False to my nobler self, I madly seize
Upon a medicine alien to my ill;
And feeding still with that should cure disease,
At once my peace and reputation kill
By turns; as the conflicting passions fire,
And chase each other madly through my breast,
I worship and despise, blame and admire,
Weep and rejoice, and covet and detest.
Alas! a bitter bargain he must choose,
Who love with life, or life with love, must lose!

Enter Lazaro.
Laz. Where can my master be? I shall go crazy, I think, running
from room to room, and house to house, after him and his distracted
wits.
Prince. Lazaro! Well, what news abroad?
Laz. Ah, my lord, there has been little of that under the sun this
long while, they say. For instance, the slasht doublets just come into
fashion, and which they call new; why ’twas I invented them years
ago.
Prince. You! how?
Laz. Why, look you; once on a time when I was not so well off as
now, and my coat was out at elbows, the shirt came through: many
saw and admired— and so it has grown into a fashion.
Prince. Who listens to you but carries away food for reflection!
[Exit.
Laz. Aha! you are somewhat surfeited with that already, I take it.
So while the world her wonted journey keeps,
Lazarus chuckles while poor Dives weeps.

Enter Cesar.

Ces. Lazaro, I waited till the Prince was gone.


Listen to me. Don Felix has betroth’d
His sister to another, not to me;
He will not tell me whom, nor does it matter:
All ill alike. But out of this despair
I’ll pluck the crown that hope could never reach.
There is no time to lose; this very night
I’ll carry her away.
Laz. Only beware
Telling Don Arias what you mean to do.
Is ’t possible you see not all along
Your secret playing on his faithless lips?
Here’s one last chance.
Ces. True, true.
Laz. You cannot lose
By secrecy—what gain by telling him?
Ces. You may be right: and to clear up the cause
Of past mischance, and make the future safe,
I’ll take your counsel.
Laz. Then hey for victory!
Meanwhile, sir, talk with all and trust in none,
And least of all in him is coming hither.
And then in ocean when the weary sun
Washes his swollen face, ‘there shall be done
A deed of dreadful note.’

Enter Arias.
Ar. How now, Don Cesar?
Laz. (aside). Here are you, be sure,
When aught is stirring.
Ar. How speeds Love with you?
Laz. (aside). The lighter, sir, now you are left behind.
Ces. Arias, my friend! All’s lost!
The love I grew deep in my heart of hearts
Is wither’d at the moment of its blossom.
I went to Felix, ask’d his sister’s hand:
It was betroth’d, he told me, to another:
I was too late. All’s lost! It were in vain
Weeping for that I never can attain:
I will forget what I must needs forgo,
And turn to other—
Laz. (to Arias). Pray, sir, pardon me;
But pri’thee say no more to him just now;
It brings on such a giddiness.
Ar. Alas!
But can I be of service?
Laz. Only, sir,
By saying nothing more.
Ar. I am truly sorry.

[Exit.
Laz. That you can lie no longer in the matter.
Oh, the Lord speed you!
Ces. O Love, if mortal anguish ever move thee,
At this last hour requite me with one smile
For all thy sorrows! let what I have suffer’d
Appease thy jealous godhead! I complain not
That you condemn my merits as too poor
For the great glory they aspire unto;
Yet who could brook to see a rival bear
The wreath that neither can deserve to wear!

Enter Prince and Arias.


Prince (to Arias). Even so?
Good. That he may not think ’twas out of malice,
I made my business trench upon his love,
Now that his love’s but Love-in-idleness,
I’ll occupy him still. Cesar!
Ces. My lord!
Prince. I had like to have forgot. ’Tis Monday, is ’t not?
I have despatches both for Rome and Naples
We must see to them to-night.
Ces. My lord!
Prince. Bring hither
Your writing.
Ces. (apart). Oh! the cup-full at my lips,
And dasht down, and for ever!
(To Lazaro.) Villain, the victory you told me of!
Laz. What fault of mine, sir?
Ces. What fault? said you not
All now was well?
Laz. Is ’t I who make it wrong?
Ces. You meddled.
Prince. Are you ready?
Ces. Immediately.
Alas, alas! how shall my pen run clear
Of the thick fountain that is welling here!
Prince (aside). And I shall learn from you how that dark pair
Contrive to smile, Jealousy and Despair.

[Desk and papers brought in: exeunt Arias and Lazaro.


Now, are you ready? (Cesar sits at the desk.)
Ces. Ay, my lord.
Prince. Begin then.
‘I am secretly’—
Ces. ‘Secretly’—driven to madness!
Prince. ‘About the marriage’—
Ces. ‘Marriage’—that never shall take place!
Prince. ‘All is fair for you’—
Ces. ‘For you’—though perdition to me!
Prince. ‘Believe me’—
Ces. I shall not survive it!
Prince. ‘That Donna Anna of Castelvi’—
Ces. ‘That Donna Anna’—I can write no more!
Prince. ‘Is such in birth, beauty, and wit’—
Ces. Oh, my lord, pardon me; but may I know
This letter’s destination?
Prince. Eh? to Flanders.
Why do you ask?
Ces. To Flanders! But, my lord,
Surely no Flemish courier leaves to-day,
Might not to-morrow—
Prince (aside). At the name of Anna
His colour changed. (Aloud.) No matter. ’Tis begun,
And we’ll ev’n finish it. Where left I off?
Ces. (reading). ‘Can write no more’—
Prince. Eh? ‘Write no more?’ Did I
Say that?
Ces My lord?
Ces. My lord?
Prince. The letter. Give me it.
Ces. (aside). Come what come may then, what is writ is writ!
Prince (reading). ‘I am secretly driven to madness about the
marriage that never shall take place. All is fair for you, though
perdition to me. Believe me I shall not survive it, that Donna Anna—
I can write no more.’
Was this what I dictated?
Ces. (throwing himself at the Prince’s feet). O my lord,
O noble Alexander! if the service
You have so often praised beyond desert
Deserve of you at all, snatch not from me
The only crown I ever ask’d for it,
To gild a less familiar brow withal.
This lady, Donna Anna,
Whom you are now devoting to another,
Is mine, my lord; mine, if a two years’ suit
Of unremitted love not unreturn’d
Should make her mine; which mine beyond dispute
Would long ere this have made her, had not I
How many a golden opportunity
Lost from my love to spend it on my Prince!
And this is my reward! Oh, knew I not
How the ill star that rules my destiny
Might of itself dispose the gracious Prince,
Who call’d me for his friend from infancy,
To act my bitterest enemy unawares,
I might believe some babbler—
Prince. Nay, Don Cesar,
If in all these cross purposes of love
You recognise the secret hand of fate,
Accuse no mortal tongue, which could not reach
The stars that rule us all, wag as it would.
Enough. I am aggrieved, and not, I think,
Unjustly, that without my pleasure, nay,
Without my knowledge even, you, my subject,
And servant, (leaving the dear name of friend,)
Disposed so of yourself, and of a lady
Whose grace my court considers as its own.
Give me the pen: and, as you write so laxly,
I must myself report—
C M l d!
Ces. My lord!
Prince. The pen. (He writes.)
Ces. If in misfortune’s quiver there be left
One arrow, let it come!
Prince. You could not write,
Don Cesar; but perhaps can seal this letter:
Tis for Don Felix; send it to him straight.
Or stay—I’d have it go by a sure hand:
Take it yourself directly.
Ces. At one blow
My love and friendship laid for ever low!
[Exit.
Enter Felix and Arias.

Ar. The letter must be written.


Prince. Oh, Don Felix,
I have this moment sent to you. No matter:
’Twas but to say I have this instant heard
Your sister’s bridegroom is in Parma; nay,
Perhaps already at your house.
Fel. Oh, my lord,
How shall I thank you for this gracious news?
Prince. Nay, we will hear them from your sister’s lips.
To her at once.

[Exit Felix.
And now, Don Arias,
You have to swear upon the holy cross
That hilts this sword, that neither Donna Anna
Know that I ever loved her, nor Don Cesar
I ever cross’d his love.
Ar. Upon this cross
I swear it; and beseech you in return
Never, my lord, to tell Don Cesar who
Reveal’d his secret.
Prince. Be it so. I promise.
And now to see whether indeed I dare
Compete with him whose lofty name I wear.

[Exeunt.

Scene II.—A Room in Felix’s House.

Anna and Elvira.

Anna. Beside the charge of my own love, Elvira,


Whose crosses, I believe, will slay me soon,
My brother has confided to me at last
His passion for the Princess Nisida;
And, for he knows that I am near her heart,
Would have me whisper it into her ears;
Which, were it such a passion as I feel,
His eyes would have reveal’d her long ago.
However, I have told her, and have got
An answer such—But look! he comes.

Enter Felix.
Fel. Oh, sister,
Might but your news be half as good as mine!
A largess for it, come. You are betroth’d,
By me, and by the Prince himself, to one
In all ways worthy of you, and who long
Has silently adored.
Anna (aside). Is it possible?
Cesar! (Aloud.) Well, ask the largess that you will.
Fel. The Princess—
Anna. Well?
Fel. What says she?
Anna. All she could
At the first blush—nothing—and that means all:
Go to her, and press out the lingering Yes
That lives, they say, in silence.
Fel. Oh, my sister!
But who comes here?

Enter Cesar and Lazaro.

Ces. (giving the letter). I, Felix. This must be


My warrant—from the Prince. Oh misery!

Fel. I thank you, Cesar. (Reads.) ‘Because happiness is the less


welcome when anticipated, I have hitherto withheld from you, that
he to whom I have engaged your sister’s hand, is—Don Cesar! in
whom unites all that man or woman can desire. If the man lives who
can deserve such glory, it is he. Farewell.’

Ces. Great Heav’n!


Fel. Nay, read the letter.

Enter Prince, Nisida, Arias, and Train.


Prince. He shall not need,
Myself am here to speak it.
Ces. (kneeling). Oh, my lord!
Prince. Rise, Cesar. If your service, as it did,
Ask’d for reward, I think you have it now;
Such as not my dominion alone,
But all the world beside, could not supply.
Madam, your hand; Don Cesar, yours. I come
To give away the bride:
And after must immediately away
To Flanders, where by Philip’s trumpet led,
I will wear Maestricht’s laurel round my brows;
Leaving meanwhile Don Felix Governor
Till my return—by this sign manual.

[Puts Nisida’s hand in Felix’s.

Fel. My lord, my lord!


Laz. Elvira!
Elv. Lazaro!
Laz. I must be off. Our betters if we ape,
And they ape marriage, how shall we escape?
Ar. And learn this moral. None commend
A secret ev’n to trustiest friend:
Which secret still in peril lies
Even in the breast of the most wise;
And at his blabbing who should groan
Who could not even keep his own?

There are three other plays by Calderon, on this subject of


keeping one’s love secret; a policy, whose neglect is punisht by a
policy characteristically Spanish. 1. Amigo, Amante, y Leal: which
has the same Prince and Arias, only the Prince confides his love to
his rival. 2. El Secreto a Voces: where it is the ladies who shuffle the
secret about the men. And 3. Basta Callar, a more complicated
intrigue than any.
GIL PEREZ, THE GALLICIAN

DRAMATIS PERSONÆ

Gil Perez.
Isabel his Sister.
Don Alonso
} his two Friends.
Manuel Mendez
Pedro
} Servants in his house.
Casilda
Donna Juana a Portuguese Lady.
Juan Baptista a Lover of Isabel.
The Lord High Admiral of Portugal.
Donna Leonor his Cousin.
A Sheriff.
A Judge.
Leonardo a Traveller.
Alguazils, Officers, Attendants, Farmers, etc.

ACT I
Scene I.—Outside Gil Perez’s House.

Enter Pedro running; Gil Perez after him with a drawn dagger; and
Isabel and Casilda interceding.
Isab. Fly, Pedro, fly!
Gil. And what the use his flying
If I be after him?
Ped. Hold him! hold him back,
Both of you!
Gil. By the Lord, I’ll do for him.
Isab. But why so savage with him?
Gil. He must pay
The long arrear of mischief you’ve run up.
Isab. I understand you not.
Gil. I’ll kill him first,
And then explain.
Isab. I, who dread not bodily violence,
Dread your injurious words. What have I done
That you should use me thus?—my enemy,
And not my brother.
Gil. You say well your enemy,
Who, if you do as you have done so long,
Will one day bathe his sword in your heart’s blood,
And after in his own, and so wipe out
One scandal from the world.
Ped. As the good soul
Who meddles to make peace between two brawlers
Oft gets the bloody nose, I’ll take the hint.
Farewell, fair Spain! for evermore farewell!
Gil. Here! hark you, sir;
Before you go; you have escaped this time
By luck, not by desert. I give you warning,
Keep from my sight: for if I see your face
Fifty years hence, among the antipodes,
I’ll pay you off.
Ped. Pray don’t disturb yourself;
I’ll take you at your word, and straight be off
To some old friends of mine—indeed relations—
In central Africa—the Ourang Outangs:
A colony so distant as I trust
Will satisfy us both. And so, good bye.
[Exit; Casilda after him.
Isab. He’s gone, poor fellow.
And now perhaps, sir, as we are alone,
You’ll tell me why you do affront me thus.
Gil. Sister—oh, would to God that I had none
To call by such a name at such expense!
And can you think that I have been so blind,
As well as dumb, not to be ware the tricks
Of the sly gentleman who follows you
So constantly, and who, if this goes on,
Will one day filch away, not your own only,
But the long garner’d honour of our house?
Why, I have seen it all from first to last,
But would not show my teeth till I could bite;
Because, in points like this, a man of honour
Speaks once, and once for all.
This once is now. I’ll speak my mind to you;
Which, if you cannot understand, to-morrow
I must repeat in quite another language.
I know your man—Juan Baptista—one
Not man enough for me, and so, I tell you,
Not for my sister. This should be enough,
Without his being, as he is, a Jew.
To get you from his reach I brought you here
To Salvatierra, deep amid the mountains,
And safe enough I thought; but even here
His cursèd letters reach you through the hands
Of that fine rascal I have just pack’d off.
There; I have told my story; take ’t to heart;
Dismiss your man at once, or, by the Lord,
If you and he persist, I’ll fire his house,
And save the Inquisition that much trouble.
Isab. Your anger makes you blind—accusing me
Of things I never did.
Gil. You never did!
Isab. But so it is, poor women must submit
To such insinuations.
Gil. Pray, was ’t I
Insinuated that letter then?
Isab. Peace, peace!
I can explain it all, and shall, when fit.
What would you have of me? You are my brother,
And not my husband, sir; consider that:
And therefore, in fraternal kindness bound,
Should even take my word without ado.
You talk of honour: is not honour then
Slow to suspect—would rather be deceived
Itself than prematurely to accuse?
I am your sister, Perez, and I know
My duty towards you and myself. Enough—
Which, if you cannot understand, to-morrow
I must repeat in quite another language.
[Exit.

Gil. She says not ill; it better were indeed


Had I kept on the mask a little longer,
Till they had dropt theirs beyond all denial.
She’s right, and I was wrong; but from this time
I’ll steer another course.

Enter Casilda.

Cas. A gentleman
(Of Portugal, he says,) is at the door,
And asks for you.
Gil. Bid him come in. Away,
My troubles, for a while!

[Exit Casilda.
Enter Manuel Mendez.
Man. ’Twas well, Gil Perez,
You sent so quickly, or my impetuosity
Had overrun your leave.
Gil. What, Manuel Mendez!
Come to my arms. What! you in Salvatierra?
Man. And, I assure you, at no small expense
Of risk and heart-ache.
Gil. That’s unwelcome news.
Man. Not when ’tis all forgotten in the joy
Of seeing you again.
Gil. I shall not rest
Till I have heard; ill-manner’d though it be
To tax a man scarce winded from a journey
With such expense of breath.
Man. Then listen, Gil.
You, I am sure, remember (time and absence
Cannot have washt so much from memory)
The pleasant time when you were last at Lisbon,
And graced my house by making it your home.
I need not tell of all we did and talk’d,
Save what concerns me now; of the fair lady
You knew me then enamour’d of, (how deeply
I need not say—being a Portuguese,
Which saying, all is said)—Donna Juana,
At whose mere name I tremble, as some seer
Smit with the sudden presence of his God.
Two years we lived in the security
Of mutual love, with so much jealousy
(Without which love is scarcely love at all)
As served to freshen up its sleeping surface,
But not to stir its depths. Ah, dangerous
To warm the viper, or, for idle sport,
Trust to the treacherous sea—sooner or later
They turn upon us; so these jealousies
I liked to toy with first turn’d upon me;
When suddenly a rich young cavalier,
Well graced with all that does and ought to please,
(For I would not revenge me with my tongue
Upon his name, but with my sword in’s blood,)
Demanded her in marriage of her father;
Who being poor, and bargains quickly made
’Twixt avarice and wealth, quickly agreed.
The wedding day drew nigh that was to be
The day of funeral too—mixt dance and dirge,
And grave and bridal chamber both in one.
The guests were met; already night began
Loose the full tide of noisy merriment,
When I strode in; straight through the wedding throng
Up to the bride and bridegroom where they were,
And, seizing her with one hand, with the other
Struck him a corpse; and daring all, to die
Fighting, or fighting carry off my prize,
Carried her off; lifted her on a horse
I had outside; struck spur; and lightning-like
Away, until we reach’d the boundary
Of Portugal, and, safe on Spanish ground,
At last drew breath and bridle. Then on hither,
Where I was sure of refuge in the arms
Of my old friend Gil Perez; whom I pray
Not so much on the score of an old friendship,
So long and warm, but as a fugitive
Asking protection at his generous hands—
A plea the noble never hear in vain.
Nor for myself alone, but for my lady
Who comes with me, and whom I just have left
Under the poplars by the river-side,
Till I had told my news, and heard your answer.
A servant whom we met with on the way,
Pointed your house out—whither, travel-tired,
Press’d for my life, and deep in love with her
I bring, as curst by those I left behind,
And trusting him I come to—
Gil. Tut, tut, tut!
Go on so, I’ll not answer you at all;
All this fine talk to me! from Manuel Mendez!
As if ’twere not enough to say ‘Friend Gil,
I’ve left a gentleman I slew behind,
And got a living lady with me, so
Am come to visit you.’ Why go about
With phrases and fine speeches? I shall answer
Quite unpolitely thus, ‘Friend Manuel,
This house of mine is yours—for months, for years,
For all your life, with all the service in ’t
That I or mine can do for you.’ So back,
And bring your lady, telling her from me
I stay behind because I am unapt
At such fine speeches as her lover makes.
Man. Oh, let me thank you,—
Gil. Nay, ’twere better far
Go to your lady; who may be ill at ease
Alone in a strange place.
[Exit Manuel.
What, Isabel! (She enters.)
Isabel, if my former love and care
Deserve of you at all, forget awhile
All difference, (for there’s a time for all,)
And help me now to honour an old friend
To whom I owe great hospitalities;
Manuel Mendez, who with his bride is come
To be my guest.
Isab. I’ll do my best for you.
But hark! what noise?

(Shouts and fighting within.)

Gil. A quarrel’s up somewhere.


Voice within. Take him alive or dead.
Another voice. He’ll slip us yet!
Isab. Some one on horseback flying at full speed
From his pursuers.
Voices within. Fire upon him! fire!

(Shots within.)

Isab. Mercy, he’s dead!


Gil. Not he; only his horse;
And see he’s up again, and gallantly
Flashing his sword around on his pursuers
Keeps them at bay, and fighting, fighting, still
Retreats—
Isab. And to our house too—

Enter Don Alonso.


Alon. Shelter! shelter!
In pity to a wretched man at last
Fordone!
Gil. What, Don Alonso!
Alon. But a moment,
To ask you cover my retreat, Gil Perez;
My life depends on reaching Portugal.
Gil. Away then to the bridge you see below there.
God speed you.
Alon. And keep you!

[Exit.

Voices without. This way! this way!


Gil. But just in time!

Enter Sheriff with Officers.


Officer. I’m sure he pass’d by here.
Gil. Well, gentlemen, your business?
Sher. Don Alonso—
Came he this way?
Gil. He did, and he went that,
And must almost, unless I much mistake,
Be got to Portugal. For, by the Lord, sir,
His feet seem’d feather’d with the wind?
Sher. Away then!
After him!
Gil. Stop a moment!
Sher. Stop! what mean you?
Gil. Just what I say. Come, Mr. Sheriff, come,
You’ve done your duty; be content with that;
And don’t hunt gentlemen like wolves to death;
Justice is one thing, and fair play’s another,
All the world over.
Sher. When I’ve got my man
I’ll answer you.
Gil. Perhaps before.
Sher. Why, sir,
Would you detain me?
Gil. Why, if logic fails,
I must try other argument.
Sher. As what, sir?
Gil. Why, mathematical. As how? Look here.
You see me draw this line. Well then, ’fore God,
The man who passes it—dies. Q. E. D.
Sher. Down with him!
Gil B k I d i
Gil. Back, I advise you.
Voices. Down with him!
Gil. Chicken-hearts! Curs! Oh, you will down with me,
Will you indeed? and this the way you do it?
(He fights with them.)

One. Oh, I am slain.


Sher. I’m wounded.
Gil. Back with you!

[Exit, driving them in.

Scene II.—The River-side.

Enter Juana and Manuel.


Jua. Oh never did I owe more to your love,
Than for this quick return.
Man. O my Juana,
The love such beauty as your own inspires,
Surmounts impossibilities. However,
I needed not go on to Salvatierra,
Lighting on what I look’d for by the way,
Among the mountains; where my friend Gil Perez
(Whose honour I insult if I declare it)
Has pitcht his tent, with hospitality
Prophetic of our coming;
So peaceably our love may fold its wings
Under the shadow of my friend’s.
Jua. Oh, Manuel,
She who has left home, country, friends, and fame,
And would contentedly leave life, for you,
Desires no other temple of her love
Than a bleak rock, whose unchanged stedfastness
Shall not out-wear her own.
Alon. (within). I can no more!
Jua. Listen! What noise is that?
Man. A cavalier
Still with his sword in his exhausted hand.
He falls!

Enter Alonzo, who falls at the side.


Alon. They e’en must have me.
Man. Courage, sir.
Wounded? (Voices within.)
Alon. Hark! the bloodhounds are close by;
And worse, they must have slain Gil Perez first.
Who else—

Enter Gil.
Gil. Confound the rogues, they’ve got the bridge
And the way to ’t, and heav’n itself, I think,
To fight upon their side.
Man. Gil, what is this?
Gil. Trying to help a friend out of a ditch,
I’ve tumbled in myself.
Man. Come, we are two
In hand, and one in heart; at least can fight
And die together.
Alon. Nay, add me;
The cause—
Gil. There’s but a moment. Manuel,
I charge you by your friendship,
Draw not your sword to-day.
Man. Not I my sword
When theirs are on you?
Jua. (clinging to Manuel). Heav’ns!
Voices, within. This way! This way!
Man. They’re coming.
Gil (to Alonso). Listen! you can swim?
Alon. Alas—
Gil. I mean upon my shoulders. Manuel,
We two shall cross to Portugal,
Where follow us they may, but cannot seize us.
Meanwhile I leave you master of my house
And honour, centred (no time to say more)
In Isabel, my sister. Swear to me
That you will see to this.
Man. I swear it, Gil.
Gil. Enough, your hand! Adieu! Now courage, sir!
Gil. Enough, your hand! Adieu! Now courage, sir!
(Takes Alonso on his shoulders and plunges into the river.)

Jua. The man swims like a dolphin.


Gil (within). Manuel,
Remember!
Man. How he wrestles with the flood!
And now is half-way over.
Gil (within). Manuel,
Remember! I have trusted all to you.
Man. Waste not your breath. I’ll do ’t.
Gil (within). Adieu!
Man. Adieu!

[Exit Manuel with Juana.

Scene III.—The Portuguese bank of the River.

Enter the Admiral of Portugal and Donna Leonor as from hunting.


Adm. Since summer’s fiery Sirius, fair cousin,
Neither from place nor power in heaven declines,
Will you not rest?
Leonor. Ah, what a noble sport
Is hunting! who so abject-spirited
As not to love its generous cruelty!
Adm. It is indeed a noble imitation
Of noblest war. As when a white-tuskt boar
Holds out alone against the yelling pack,
Gores one, o’erthrows another, all the while
Bristling his back like to some ridge of spears:
While many a gallant hound, foil’d in his onset,
Tears his own flesh in disappointed rage,
Then to the charge again—he and his foe,
Each with redoubled fury firing up:
A chivalry that nature has implanted
Ev’n in the heart of beasts.
Leonor. So in falconry,
That I love even better; when the heron
Mounts to the wandering spheres of air and fire,
Poised between which alternately she burns
And freezes, while two falcons, wheeling round,
Strive to out-mount her, tilting all along
The fair blue field of heaven for their lists;
Until out-ris’n and stricken, drencht in blood,
Plumb down she falls like to some crimson star;
A rivalry that nature has implanted
Ev’n in the breast of birds.

Enter Pedro.
Ped. Which is the way, I wonder? What with fright and weariness,
I must rest awhile. Well, this is Portugal, where to be sure a poor
Spanish pimp may hope to escape ferocious honour. That I should
lose a post where others make their thousands at my first function!
But who are these? Fine folks too! Pray Heaven they be in want of
an officer.
Adm. A horse will soon carry you to the villa. Hark you, sir! (To
Pedro.)
Ped. My lord!
Adm. Who are you?
Ped. Nay, how should I know?
Adm. But are you one of my people?
Ped. Yes, if you like it. As said Lord Somebody, who neither served
king, man, or God, but who entering the palace one day at supper-
time, and seeing all the chamberlains at work without their coats,
whips off his, and begins carrying up dishes. Suddenly in comes the
major-domo, who perceiving a stranger, asks if he be sworn of the
service. ‘Not yet,’ says he, ‘but if swearing is all that’s wanted, I’ll
swear to what you please.’ So ’tis with me. Make me your servant,
and I’ll swear and forswear anything.
Adm. You are liberal of your humour.
Ped. ’Tis all I have to be liberal of; and it would not be right to
spare that.
Gil (within). Hold on, hold on!
Leonor. Who’s that?
Adm. Look, some one with erect head and vigorous arms,
buffeting the wave before him.
Leonor. With another on his shoulders too.
Adm. (to Pedro). Now, would you win an earnest of future favour,
plunge in to his assistance.
Ped. I would, sir, but I’m a wretched swimmer.
Leonor. They have reacht the shore at last.
Enter Gil Perez and Alonso, drencht.
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebookball.com

You might also like