springerbriefs in computer science3a978-1-4471...foreword we are all familiar with the time-honored...

10
SpringerBriefs in Computer Science For further volumes: http://www.springer.com/series/10028 Series Editors Stan Zdonik Peng Ning Shashi Shekhar Jonathan Katz Xindong Wu Lakhmi C. Jain David Padua Xuemin Shen Borko Furht

Upload: others

Post on 24-Feb-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

SpringerBriefs in Computer Science

For further volumes:http://www.springer.com/series/10028

Series EditorsStan ZdonikPeng NingShashi ShekharJonathan KatzXindong WuLakhmi C. JainDavid PaduaXuemin ShenBorko Furht

Page 2: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

Simone Campanoni

Guide to ILDJIT

123

Page 3: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

Dr. Simone CampanoniDepartment of Engineering and Applied SciencesHarvard UniversityOxford Street 33CambridgeMA 02138USAe-mail: [email protected]

ISSN 2191-5768 e-ISSN 2191-5776ISBN 978-1-4471-2193-0 e-ISBN 978-1-4471-2194-7DOI 10.1007/978-1-4471-2194-7Springer London Dordrecht Heidelberg New York

British Library Cataloguing in Publication DataA catalogue record for this book is available from the British Library

Library of Congress Control Number: 2011936140

� Simone Campanoni 2011Apart from any fair dealing for the purposes of research or private study, or criticism or review, aspermitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced,stored or transmitted, in any form or by any means, with the prior permission in writing of thepublishers, or in the case of reprographic reproduction in accordance with the terms of licenses issuedby the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should besent to the publishers.The use of registered names, trademarks, etc., in this publication does not imply, even in the absence ofa specific statement, that such names are exempt from the relevant laws and regulations and thereforefree for general use.The publisher makes no representation, express or implied, with regard to the accuracy of theinformation contained in this book and cannot accept any legal responsibility or liability for any errorsor omissions that may be made.

Cover design: eStudio Calamar, Berlin/Figueres

Printed on acid-free paper

Springer is part of Springer Science+Business Media (www.springer.com)

Page 4: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

Il bruco, mediante l’esercitato studio ditessere con mirabile artificio e sottile lavorointorno a sè la nova abitazione, esce poi foridi quella colle dipinte e belle ali, con quellelanciandosi verso il cielo

Leonardo da Vinci

Page 5: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

Foreword

We are all familiar with the time-honored idea of a Virtual Machine that is neutralwith respect to computer architecture and can be used to execute a high-levellanguage, after its translation to the VM byte-code. This approach can be found inseveral successful mono-language systems, that support popular languages such asJava or SmallTalk. Such systems combine interpretation with dynamic translation,including code optimization, to achieve higher performance.

Other more ambitious VMs support many languages, and the best knownexample of a source-language neutral VM is the Common Language Infrastructure(standard ECMA 335 or ISO/IEC 23271), available from the .NET proprietarysystem and from free open-source projects including the most established Mono and,more recently, ILDJIT. Let me disclaim any responsibility for this unpronounceableacronym that I had urged Simone to change, before the fast growing popularity of thesystem made renaming inconvenient—would you imagine repainting a boat namewhile she sails on a successful regatta? Yet the D for distributed reveals that this VMbelongs to a new generation that from the very start was designed for parallelexecution.

Since the early developments at the Formal Languages and Compiler Group,Simone shaped the software structure to be modular and extensible, so that otherteam members, including graduate students, could easily contribute new plug-ins.Two distinguishing objectives of the early ILDJIT were the emphasis on embeddedapplications running on small microprocessors, motivated by the support of chipmanufacturer STMicroelectronics, and the use of C both as application and com-piler implementation language.

The long way of the project status from curiosity to recognition and to adoptionby other research labs and developers has been punctuated by visible progresses interms of language and test suites coverage, reduction of residual errors, and, morethan anything else, by performance improvements that rank ILDJIT at the top ofCLI-based systems in many relevant cases.

In the last two years the project direction has moved to Harvard Universitywhere new and exciting developments have happened, the most original ones notyet documented in this book because under scientific investigation; I mention

vii

Page 6: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

friendlier user interfaces, a range of inter-procedural optimizations, the option touse the compiler ahead-of-time (i.e. statically), compatibility with byte-codesgenerated by Microsoft .NET front-end compilers, and porting of the system onMS Windows operating system. All that makes ILDJIT an attractive choice for acompilation platform, whenever, in addition to language and machine indepen-dence, run-time adaptation and program transformations are important. Futureusers and developers will find this timely handbook a practical companion. PleaseSimone, now take some rest before you commit yourself to some ambitious newdevelopment!

Politecnico di Milano, June 2011 Stefano Crespi Reghizzi

viii Foreword

Page 7: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

Acknowledgments

This book is the product of six years of both engineering works and researchesduring which I had the privilege to share these efforts with the enlightenedProf. Stefano Crespi Reghizzi, for whom I will be always thankful.

After the Ph.D. at Politecnico di Milano, I moved to Harvard University to workwith both Prof. David Brooks and Prof. Gu-Yeon Wei; we have worked to improveILDJIT to be useful for the extremely exciting researches we are doing together.I would like to thank Prof. David Brooks and Prof. Gu-Yeon Wei both to havegiven me the opportunity of working with them and for advising me in such a greatand remarkable way.

Any work of such breadth necessarily builds on efforts of more than one author;at the head of this list of people, which gave me a priceless help, there are AndreaDi Biagio, Martino Sykora, Michele Tartara, Ettore Speziale, Stefano Anelli andLuca Rocchini.

I would like to thank Andrea Cazzaniga, which was the first reader of this book.Last, but not least, I would like to thank all my family, especially my mom

Bruna and my brother Luca, who both always believe in me, no matter what, andhelped me to continue my university studies at Politecnico di Milano.

ix

Page 8: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 A Brief History of ILDJIT . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Major Features of ILDJIT. . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.3 Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . 4References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Generating, Compiling and Executing Bytecode Programs . . . . . . 52.1 Generating the Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2 Static Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.2.1 Static Compilation in ILDJIT . . . . . . . . . . . . . . . . . . . . 102.2.2 Ahead-of-Time Compilation in ILDJIT . . . . . . . . . . . . . 112.2.3 Partial Compilations in ILDJIT. . . . . . . . . . . . . . . . . . . 122.2.4 Cached Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.3 Dynamic Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142.3.1 Just-in-Time Compilation. . . . . . . . . . . . . . . . . . . . . . . 152.3.2 Dynamic Look-Ahead Compilation . . . . . . . . . . . . . . . . 16

2.4 Different Configurations with a Single Installation . . . . . . . . . . 18References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3 Platform Independent Extensions of the Framework . . . . . . . . . . . 213.1 Platform Independent Data Types . . . . . . . . . . . . . . . . . . . . . . 213.2 Platform Independent API . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

4 Compiling with Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.1 Code Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.2 Optimization Levels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4.2.1 Default Optimization Levels. . . . . . . . . . . . . . . . . . . . . 304.3 Enable and Disable Optimizations . . . . . . . . . . . . . . . . . . . . . . 31

4.3.1 Disabling Optimizations. . . . . . . . . . . . . . . . . . . . . . . . 314.3.2 Enabling Optimizations . . . . . . . . . . . . . . . . . . . . . . . . 32

xi

Page 9: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

4.4 Customizing Optimization Levels . . . . . . . . . . . . . . . . . . . . . . 324.4.1 Optimization Levels API . . . . . . . . . . . . . . . . . . . . . . . 334.4.2 Available Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . 354.4.3 Example of Optimization Levels Plugin. . . . . . . . . . . . . 364.4.4 Installing Optimization Levels Plugins. . . . . . . . . . . . . . 37

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

5 Intermediate Representation (IR) . . . . . . . . . . . . . . . . . . . . . . . . . 415.1 The Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425.2 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

5.2.1 Mathematic Operations . . . . . . . . . . . . . . . . . . . . . . . . 435.2.2 Compare Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 445.2.3 Bitwise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 445.2.4 Jump Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445.2.5 Memory Allocation Operations . . . . . . . . . . . . . . . . . . . 465.2.6 Memory Access Operations . . . . . . . . . . . . . . . . . . . . . 475.2.7 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 475.2.8 Miscellaneous Instructions . . . . . . . . . . . . . . . . . . . . . . 48

5.3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495.3.1 IR Variables and Constants . . . . . . . . . . . . . . . . . . . . . 505.3.2 IR Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515.3.3 IR Type Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . 53

5.4 IR API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535.4.1 Iterating Across IR Code . . . . . . . . . . . . . . . . . . . . . . . 55

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

6 Analyzing and Transforming Programs . . . . . . . . . . . . . . . . . . . . 636.1 Adding a New Code Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . 646.2 Code Tool API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666.3 Examples of Code Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 686.4 Examples of Code Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . 706.5 Examples of Code Optimizations. . . . . . . . . . . . . . . . . . . . . . . 71

7 Internal Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

7.1.1 Translation Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767.1.2 Software Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 76

7.2 Parallel Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787.2.1 Dynamic Lookahead Compilation . . . . . . . . . . . . . . . . . 787.2.2 Compilation Load Balancing . . . . . . . . . . . . . . . . . . . . 807.2.3 Static Memory Initialization . . . . . . . . . . . . . . . . . . . . . 817.2.4 Threads Communication . . . . . . . . . . . . . . . . . . . . . . . 837.2.5 Pipeline Entry. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

xii Contents

Page 10: SpringerBriefs in Computer Science3A978-1-4471...Foreword We are all familiar with the time-honored idea of a Virtual Machine that is neutral with respect to computer architecture

7.3 IR and Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847.3.1 IR Virtual Machine Components. . . . . . . . . . . . . . . . . . 85

7.4 Extensible Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867.4.1 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

7.5 Input Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887.5.1 Load and Decode Tasks. . . . . . . . . . . . . . . . . . . . . . . . 897.5.2 Layout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907.5.3 Exception Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . 917.5.4 Internal Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

7.6 Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Contents xiii