ebook img

A Retargetable C Compiler Design and Implementation PDF

578 Pages·1995·8.009 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview A Retargetable C Compiler Design and Implementation

A Retargetable C Compiler: Design and Implementation Christopher W. Fraser AT&T BELL LABORATORIES David R. Han son 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 lllustration: 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 If..T£X, T£'(, and Adobe Illus trator. Instructional Material Disclaimer 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, photocopying, 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. II. Title. QA76.73.C15F75 1995 005.4'53--dc20 94-24583 CIP 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 To Linda To Maylee Contents Preface xiii 1 Introduction 1 1.1 Llterate Programs 1 1.2 How to Read This Book 2 1.3 Overview 4 1.4 Design 11 1.5 Common Declarations 16 1.6 Syntax Specifications 19 1.7 Errors 20 Further Reading 21 2 Storage Management 23 2.1 Memory Management Interface 23 2.2 Arena Representation 24 2.3 Allocating Space 26 2.4 Deallocating Space 28 2.5 Strings 28 Further Reading 31 Exercises 3 2 3 Symbol Management 35 3.1 Representing Symbols 37 3.2 Representing Symbol Tables 39 3.3 Changing Scope 42 3.4 Finding and Installing Identifiers 44 3.5 Labels 45 3.6 Constants 47 3.7 Generated Variables 49 Further Reading 51 Exercises 51 vii vii CONTENTS 4 Types 53 4.1 Representing Types 53 4.2 Type Management 56 4.3 Type Predicates 60 4.4 Type Constructors 61 4.5 Function Types 63 4.6 Structure and Enumeration Types 65 4. 7 Type-Checking Functions 69 4.8 Type Mapping 73 Further Reading 74 Exercises 74 5 Code Generation Interface 78 5.1 Type Metrics 78 5.2 Interface Records 79 5. 3 Symbols 80 5.4 Types 81 5.5 Dag Operators 81 5.6 Interface Flags 87 5.7 Initialization 89 5.8 Definitions 89 5.9 Constants 91 5.10 Functions 92 5.11 Interface Binding 96 5.12 Upcalls 97 Further Reading 99 Exercises 100 6 Lexical Analysis 102 6.1 Input 103 6.2 Recognizing Tokens 107 6.3 Recognizing Keywords 113 6.4 Recognizing Identifiers 114 6. 5 Recognizing Numbers 115 6.6 Recognizing Character Constants and Strings 121 Further Reading 124 Exercises 12 5 CONTENTS ix 7 Parsing 127 7.1 Languages and Grammars 127 7.2 Ambiguity and Parse Trees 128 7.3 Top-Down Parsing 132 7.4 FIRST and FOLLOW Sets 134 7.5 Writing Parsing Functions 137 7.6 Handling Syntax Errors 140 Further Reading 145 Exercises 14 5 8 Expressions 147 8.1 Representing Expressions 147 8.2 Parsing Expressions 151 8.3 Parsing C Expressions 154 8.4 Assignment Expressions 156 8.5 Conditional Expressions 159 8.6 Binary Expressions 160 8.7 Unary and Postfix Expressions 163 8.8 Primary Expressions 166 Further Reading 170 Exercises 171 9 Expression Semantics 172 9.1 Conversions 172 9.2 Unary and Postfix Operators 178 9.3 Function Calls 183 9.4 Binary Operators 191 9.5 Assignments 195 9.6 Conditionals 200 9.7 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 If Statements 224 10.5 Labels and Gotos 226 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 2 54 11.3 Declarators 265 11.4 Function Declarators 2 70 11.5 Structure Specifiers 276 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 12.5 Function Calls 332 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 13.l Organization of the Code Generator 353 13.2 Interface Extensions 3 54 13.3 Upcalls 357 13.4 Node Extensions 358 13.5 Symbol Extensions 362 13.6 Frame Layout 364 CONTENTS xi 13.7 Generating Code to Copy Blocks 367 13.8 Initialization 370 Further Reading 3 71 Exercises 3 72 14 Selecting and Emitting Instructions 373 14.1 Specifications 375 14.2 Labelling the Tree 377 14.3 Reducing the Tree 379 14.4 Cost Functions 388 14.5 Debugging 389 14.6 The Emitter 391 14.7 Register Targeting 397 14.8 Coordinating Instruction Selection 402 14.9 Shared Rules 403 14.10 Writing Specifications 404 Further Reading 405 Exercises 406 15 Register Allocation 408 15.1 Organization 409 15.2 Tracking the Register State 410 15.3 Allocating Registers 413 15.4 Spilling 420 Further Reading 428 Exercises 429 16 Generating MIPS R3000 Code 430 16.1 Registers 432 16.2 Selecting Instructions 435 16.3 Implementing Functions 447 16.4 Defining Data 4 5 5 16.5 Copying Blocks 460 Further Reading 461 Exercises 461 17 Generating SPARC Code 463 17.1 Registers 465 17.2 Selecting Instructions 469 xii CONTENTS 17.3 Implementing Functions 483 17.4 Defining Data 490 17.5 CopyingBlocks 492 Further Reading 494 Exercises 495 18 Generating X86 Code 496 18.1 Registers 498 18.2 Selecting Instructions 503 18.3 Implementing Functions 518 18.4 Defining Data 520 Further Reading 524 Exercises 524 19 Retrospective 526 19.1 Data Structures 526 19.2 Interface 527 19.3 Syntactic and Semantic Analyses 529 19.4 Code Generation and Optimization 531 19.5 Testing and Validation 531 Further Reading 5 3 3 Bibliography 535 Index 541 How to Obtain Ice 563 Preface The compiler is the linchpin of the programmer's toolbox. Working pro grammers use compilers every day and count heavily on their correct ness and reliability. A compiler must accept the standard definition of the programming language so that source code will be portable across platforms. A compiler must generate efficient object code. Perhaps more important, a compiler must generate correct object code; an application is only as reliable as the compiler that compiled it. A compiler is itself a large and complex application that is worthy of study in its own right. This book tours most of the implementation of 1 cc, a compiler for the ANSI C programming language. It is to compil ing what Software Tools by B. W. Kernighan and P. J. Plauger (Addison Wesley, 1976) is to text processing like text editors and macro proces sors. Software design and implementation are best learned through ex perience with real tools. This book explains in detail and shows most of the code for a real compiler. The accompanying diskette holds the source code for the complete compiler. 1 cc is a production compiler. It's been used to compile production programs since 1988 and is now used by hundreds of C programmers daily. Detailing most of a production compiler in a book leaves little room for supporting material, so we present only the theory needed for the implementation at hand and leave the broad survey of compiling techniques to existing texts. The book omits a few language features - those with mundane or repetitive implementations and those deliberately treated only in the exercises - but the full compiler is available on the diskette, and the book makes it understandable. The obvious use for this book is to learn more about compiler con struction. But only few programmers need to know how to design and implement compilers. Most work on applications and other aspects of systems programming. There are four reasons why this majority of C programmers may benefit from this book. First, programmers who understand how a C compiler works are often better programmers in general and better C programmers in particular. The compiler writer must understand even the darkest comers of the C language; touring the implementation of those comers reveals much about the language itself and its efficient realization on modem comput ers. Second, most texts on programming must necessarily use small ex amples, which often demonstrate techniques simply and elegantly. Most xiii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.