A Retargetable C Compiler:
Design and
Implementation
Christopher W. Fraser
AT&T BELL LABORATORIES
David R. Hanson
PRINCETON UNIVERSITY
The Benjamin/Cummings Publishing Company, Inc.
Redwood City, CA • Menlo Park, CA • Reading, MA • New York
Don Mills, Ontario •Wokingham, UK •Amsterdam • Bonn
Sydney • Singapore • Tokyo • Madrid • San Juan
Acquisitions Editor: John Carter Shanklin
Executive Editor: Dan Joraanstad
Editorial Assistant: Melissa Standen
Production Supervisor: Ray Kanarr
Cover Design and Illustration: Cloyce Wall
Text Designer: Peter Vacek
Copyeditor: Elizabeth Gehrman
Proofreader: Christine Sabooni
Many of the designations used by manufacturers and sellers to distinguish their
products are claimed as trademarks. Where those designations appear in this
book, and Benjamin/Cummings was aware of a trademark claim, the designations
have been printed in initial caps or in all caps.
Camera-ready copy for this book was prepared using !<\TEX, Ti;){, and Adobe Illus
trator.
Instructional Material Disclaim.er
The program presented in this book has been included for its instructional value.
It has been tested with care but is not guaranteed for any particular purpose. Nei
ther the publisher, AT&T, or the authors offer any warranties or representations,
nor do they accept any liabilities with respect to the program.
Copyright© 1995 by AT&T and David R. Hanson.
All rights reserved. No part of this publication may be reproduced, or stored
in a database or retrieval system, or transmitted, in any form or by any means,
electronic, mechanical, photocop)'ing, recording, or otherwise, without the prior
written permission of the publisher. Printed in the United States of America.
Printed simultaneously in Canada.
Library of Congress Cataloging-in-Publication Data
Fraser, Chistopher W.
A retargetable C compiler: design and implementation I
Christopher W. Fraser, David R. Hanson
p.
cm.
Includes index.
ISBN 0-8053-1670-1
1. C (Computer program language) 2. Compilers (Computer programs)
I. Hanson, David R.
QA76.73.C15F75 1995
OOS.4'53--dc20
II. Title.
ISBN 0-8053-1670-1
1 2 3 4 5 6 7 8 9 10 DOC 98 97 96 95
The Benjamin/Cummings Publishing Company, Inc.
390 Bridge Parkway
Redwood City, CA 94065
94-24583
CIP
To Linda
To Maylee
Contents
Preface xiii
1
2
3
Introduction 1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
literate Programs 1
How to Read This Book 2
Overview 4
Design 11
Common Declarations 16
Syntax Specifications 19
Errors 20
Further Reading 21
Storage Management 23
2.1 Memory Management Interlace 23
2.2
2.3
2.4
2.5
Arena Representation 24
Allocating Space 26
Deallocating Space 28
Strings 28
Further Reading 31
Exercises 32
Symbol Management 35
3.1
3.2
3.3
3.4
3.5
3.6
3.7
Representing Symbols 37
Representing Symbol Tables 39
Changing Scope 42
Finding and Installing Identifiers 44
Labels 45
Constants 47
Generated Variables 49
Further Reading 51
Exercises 51
vii
viii
CONTENTS
4
5
6
Types 53
4.1
4.2
4.3
4.4
4.5
4.6
4. 7
4.8
Representing Types 53
Type Management 56
Type Predicates 60
Type Constructors 61
Function Types 63
Structure and Enumeration Types 65
Type-Checking Functions 69
Type Mapping 73
Further Reading 7 4
Exercises 7 4
Code Generation Interface 78
Type Metrics 78
5.1
Interface Records 79
5.2
Symbols 80
5.3
Types 81
5.4
Dag Operators 81
5.5
Interface Flags 87
5.6
Initialization 89
5.7
Definitions 89
5.8
Constants 91
5.9
5.10 Functions 92
5.11
5.12 Upcalls 97
Interface Binding 96
Further Reading 99
Exercises 100
Lexical Analysis 102
Input 103
6.1
Recognizing Tokens 107
6.2
6.3
Recognizing Keywords 113
Recognizing Identifiers 114
6.4
Recognizing Numbers 115
6.5
Recognizing Character Constants and Strings 121
6.6
Further Reading 124
Exercises 12 5
CONTENTS
7
8
9
Languages and Grammars 12 7
Ambiguity and Parse Trees 128
Top-Down Parsing 132
FIRST and FOLLOW Sets 134
Parsing 127
7 .1
7.2
7.3
7.4
7.5 Writing Parsing Functions 137
7.6
Handling Syntax Errors 140
Further Reading 14 5
Exercises 14 5
Expressions 147
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
Representing Expressions 147
Parsing Expressions 151
Parsing C Expressions 154
Assignment Expressions 156
Conditional Expressions 159
Binary Expressions 160
Unary and Postfix Expressions 163
Primary Expressions 166
Further Reading 170
Exercises 171
Expression Semantics 172
9.1
9.2
9.3
9.4
9.5
9.6
9.7
Conversions 172
Unary and Postfix Operators 178
Function Calls 183
Binary Operators 191
Assignments 195
Conditionals 200
Constant Folding 202
Further Reading 214
Exercises 214
10 Statements 216
10.1 Representing Code 216
10.2 Execution Points 220
10.3 Recognizing Statements 221
10.4
10.5 Labels and Gotos 226
If Statements 224
x
CONTENTS
10.6 Loops 227
10.7 Switch Statements 230
10.8 Return Statements 243
10.9 Managing Labels and Jumps 246
Further Reading 249
Exercises 250
11 Declarations 252
11.1 Translation Units 253
11.2 Declarations 254
11.3 Declarators 265
ll.4 Function Declarators 270
Structure Specifiers 276
11.5
11.6 Function Definitions 285
11. 7 Compound Statements 293
11.8 Finalization 303
11.9 The Main Program 305
Further Reading 308
Exercises 308
12 Generating lntennediate Code 311
12.1 Eliminating Common Subexpressions 313
12.2 Building Nodes 317
12.3 Flow of Control 321
12.4 Assignments 328
Function Calls 332
12.5
12.6 Enforcing Evaluation Order 335
12.7 Driving Code Generation 337
12.8 Eliminating Multiply Referenced Nodes 342
Further Reading 349
Exercises 349
13 Structuring the Code Generator 352
Interface Extensions 354
13.1 Organization of the Code Generator 353
13.2
13.3 Upcalls 357
13.4 Node Extensions 358
13.5
13.6 Frame Layout 364
Symbol Extensions 362