12
Virtual Machines

Virtual Machines - Springer978-1-84628-246-1/1.pdf · Preface I love virtual machines (VMs) and I have done for a long time. Ifthat makes me "sad" or an "anorak", so be it. I love

Embed Size (px)

Citation preview

Virtual Machines

lain D. Craig

Virtual Machines

With 43 Figures

~ Springer

lain D. Craig, MA, PhD, MBCS, [email protected]

British Library Cataloguing in Publication DataCraig, I.

Virtual machines1. Virtual computer systems 2. Parallel processingI. Title006.8ISBN-10: 1852339691

Library of Congress Control Number: 2005923254

ISBN-10: 1-85233-969-1ISBN-13: 978-1-85233-969-2

Printed on acid-free paper

© Springer -Verlag London Limited 2006

Apart 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 publi cation may only be repro­duced, stored or transmitted, in any form or by any means, with the prior permission in writing ofthe publishers, or in the case of reprographic reproduction in accordance with the terms of licencesissued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those termsshould be sent to the publishers.

The use of registered names, trademarks, etc., in this publication does not imply, even in the absenceof a specific statement, that such names are exempt from the relevant laws and regulations andtherefore free for general use.

The publisher makes no representation, express or implied, with regard to the accuracy of the infor­mation contained in th is book and cannot accept any legal responsibility or liability for any errorsor omissions that may be made.

Printed in the United States of America (EB)

9 8 7 6 5 4 321

Springer Science+Business Mediaspringeronline.com

To Dr P. W. Dale

(Uncle Paul)

Preface

I love virt ual machines (VMs) and I have done for a long time. If that makesme "sad" or an "anorak", so be it . I love them because they are so much fun, aswell as being so useful. They have an element of original sin (writ ing assemblyprograms and being in control of an entire machine), while st ill being ableto claim that one is being a respectable member of the community (beingstructured, modular , high-level, object-oriented, and so on) . They also allowone to design machines of one's own, unencumbered by the rest rict ions of apart icular processor (at least , until one starts opt imising it for some physicalprocessor or other) .

I have been building virt ual machines, on and off, since 1980 or there­abouts. It has always been something of a hobby for me; it has also turnedout to be a technique of great power and applicability. I hope to cont inueworking on them, perhaps on some of the ideas out lined in the last chapter(I certainly want to do some more work with register-based VMs and concur­rency).

I originally wanted to write the book from a purely semantic viewpoint .I wanted to start with a formal semantics of some language, then show howa virt ual machine sat isfied the semantics; finally, I would have liked to haveshown how to derive an implementation. Unfort unately, there was insufficienttime to do all of this (although some parts- the semant ics of ALEX and apart proof of correctness- were done but omitted) . There wasn't enough timeto do all the necessary work and, in addit ion, SHirk et al. had published theirbook on Java [47] which does everything I had want ed to do (they do it withJava; I had wanted to define ad hoc languages).

I hope to have made it clear that I believe there to be a considerableamount of work left to be done with virtual machines. The entire last chapteris about this. As I have tried to make clear, some of the ideas included in thatchapte r are intended to make readers think, even if they consider the ideasstupid!

A word or two is in order concerning the instruction sets of the variousvirt ual machines that appear from Chapter Four onwards . The instructions

viii Preface

for the stack machines in Chapter Four seem relatively uncontroversial. Theinstructions in the chapter on register machines (Chapter Seven) might seemto be open to a little more questioning.

First, why not restrict the instruction set to those instructions required toimplement ALEX? This is because I wanted to show (if such a demonstrationwere really required) that it is possible to define a larger instruction set sothat more than one language can be supported.

Next , most of the jump and arithmetic instructions seem sensible enoughbut there are some strange cases, the jump branching to the address on the topof the stack is one case in point ; all these stack indexing operations constituteanother case. I decided to add these "exotic" instructions partly because,strange as they might appear to some, they are useful. Somewhere or other,I encountered a virtual machine that employed a jump instruction similar tothe one just mentioned (I also tried one out in one of the Harrison Machine'simplementations-it was quite useful), so I included it. Similarly, a lot of timeis spent in accessing variables on the stack, so I added instructions that wouldmake such accesses quite easy to compile; I was also aware that things likeprocess control blocks and closures might be on stacks. I decided to add theseinstructions to build up a good repertoire, a repertoire that is not restrictedto the instructions required to implement ALEX or one of the extensionsdescribed in Chapter Five.

I do admit, though, that the mnemonics for many of the operations couldhave been chosen with more care. (I was actually thinking that an assemblercould macro these names out .) One reason for this is that I defined the registermachine in about a day (the first ALEX machine was designed in about forty­five minutes!). Another (clearly) is that I am not terribly good at creatingmnemonics . I thought I'd better point these matters out before someone elsedoes.

I have made every effort to ensure that this text is free of errors . Undoubt­edly, they still lurk waiting to be revealed in their full horror and to show thatmy proof-reading is not perfect . Should errors be found, I apologise for themin advance.

Preface ix

Acknowledgements

Beverley Ford first thought of this book when looking through some notes 1had made on abstract machines. 1 would like to thank her and her staff atSpringer, especially Catherine Drury, for making the process of writing thisbook as smooth as possible.

My brother Adam should be thanked for creating the line drawings thatappear as some of the figures (I actually managed to do the rest myself). 1would also like to thank all those other people who helped in various wayswhile 1 was writing th is book (they know who they are).

l ain CraigMarket Square

Atherstone14 Ju ne, 2005

Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Introduction . .. ... .. . . . . . .. . . ... . .. . .. . .. . . . . . .. . . . . . . . . 11.2 Interpreters. . . .. . . .. . . . . . .. .. . . .. . . . . . . . . . . . . . . . .. . ... . . 31.3 Landin's SECD Machine 31.4 The Organisation of this Book 51.5 Omissions . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . ... . .. . . . .. . . . . 7

2 VMs for Portability: BCPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.1 Int roduction . .. . . . . . . . . . . . . ... ... . .. . . . . . . . . . . ... .... . . . 112.2 BCPL the Language 122.3 VM Operations 152.4 The OCODE Machine 172.5 OCODE Instructions and their Implementation 18

2.5.1 Expression Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.5.2 Load and Store Instructions 202.5.3 Instructions Relatin g to Routines . . . . . . . . . . . . . . . . . . .. 202.5.4 Control Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.5.5 Directives . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . 23

2.6 The Intcode/ Cintcode Machine 24

3 The Java Virtual Machine 273.1 Introduction . . . . . . . . . . . . . .. . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 273.2 JVM Organisation: An Overview 28

3.2.1 The stack 293.2.2 Method areas 303.2.3 The PC register 313.2.4 Other st ructures 32

3.3 Class Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.4 Object Representat ion at Runtime . . . . . . . . . . . . . . . . . . . . . . . .. 403.5 Initialisation. . ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... . . . . 423.6 Object Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

xii Contents

3.7 JVM Termination 453.8 Exception Handling 453.9 Instructions . . .. . . .. ...... ... ... ..... ... ..... . . . . . . . ..... 46

3.9.1 Data-manipulation instructions 483.9.2 Control instructions 513.9.3 Stack-manipulat ing inst ructions . . . . . . . . . . . . . . . . . . . . . 543.9.4 Support for object orientation . . . . . . . . . . . . . . . . . . . . . . . 563.9.5 Synchronisat ion .. . . . . . .... .. . .. . .. .. . . ... ... . . .. . . 59

3.10 Concluding Remarks 59

4 DIY VMs 614.1 Introduction . . . . . . . . .. . . ... ... .. . . . .. . . .... ...... . . ... .. 614.2 ALEX 62

4.2.1 Language Overview 624.2.2 What the Virtual Machine Must Support 654.2.3 Virtual Machine- Storage Structures 664.2.4 Virtu al Machine-Registers 684.2.5 Virt ual Machine-Instruction Set . . . . . . . . . . . . . . . . . . . . 704.2.6 An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 794.2.7 Implementat ion .. . . .. . . . .. . . . . . . . . . . . . . . . .. .. . .... 814.2.8 Extensions . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... ... . 854.2.9 Alternatives . . . . . .. . .. .... .. . .. . . . . . . .. . .. . .... . .. 884.2.10 Specification .. . . . . . .. ... . . . . . ... . . . . . .. .. . ...... . . 93

4.3 Issues ... .. ... .. .. . . ... . . . . . . . . . . . . . . .. . . . . . . . . . .. . . . . . . 964.3.1 Indirect and Relative Jumps . . . . . . . . . . . . . . . . . . . . . . . . 974.3.2 More Data Types 984.3.3 Higher-Order Routines 1064.3.4 Pri mitive Rout ines 106

4.4 Concluding Remarks 107

5 More Stack-lBased VMs 1095.1 Introduction 1095.2 A Simple Object-Oriented Language 110

5.2.1 Language Overview 1105.2.2 Virtual Machine-Storage Structures 1115.2.3 Virtual Machine-Registers 1135.2.4 Virtual Machine-Instruction Set 1135.2.5 Extensions 1165.2.6 Alternatives 116

5.3 A Parallel Language 1175.3.1 Language Overview 1175.3.2 Virtual Machine-Storage Structures 1195.3.3 Virt ual Machine-Registers 1215.3.4 Virtual Machine-Instruction Set 1225.3.5 Implementation 124

Contents xiii

5.3.6 Extensions 1265.3.7 Alternatives 1285.3.8 Issues 129

5.4 Concluding Remarks 1295.4.1 Some Optimisations 1295.4.2 Combining the Languages 130

6 Case Study: An Event-Driven Language 1316.1 Introduction 1316.2 The Structure of Rules 1336.3 Events 1366.4 Execution Cycle 1366.5 Interpretation Rules 1386.6 VM Specification 141

6.6.1 States and Notational Conventions 1426.6.2 Infra-Rule Transitions 1456.6.3 Extra-Rule Transitions 1486.6.4 VM-Only Trans itions 1506.6.5 Introspective Operations 151

6.7 Rule Equivalences 1536.8 Concluding Remarks 154

7 Flegister-13ased 11achines 1577.1 Introduct ion 1577.2 The Register-Transfer Model 1587.3 Register Machine Organisation 1617.4 Parrot-General Organisation 1657.5 Parrot Instruction Set 168

7.5.1 Control instructions 1697.5.2 Data management instructions 1697.5.3 Register and stack operations 170

7.6 DIY Register-Based Virtual Machine 1717.6.1 Informal Design 1727.6.2 Extensions 1767.6.3 Transition Rules 177

7.7 Translating ALEXVM into RTM 1837.8 Example Code 1867.9 Correctness of the Trans lation 1867.10 More Natural Compilation 1967.11 Extensions 200

xiv Contents

8 Implementation Techniques " 2018.1 Stack-Based 11achines 202

8.1.1 Direct Implement ation 2028.1.2 Translation 2038.1.3 Threaded Code 207

8.2 Register Machines 2098.2.1 Register sets 2108.2.2 Addressing 2108.2.3 Translation to Anot her V11 212

8.3 Using Transitions 2128.4 Concluding Remarks 213

9 Open Issues 2159.1 Security · 2159.2 New Languages 2169.3 Typed Inst ruct ion Sets and Intermediate Codes 2169.4 High-Level Inst ruct ions 2189.5 Additivity and Replacement 2189.6 Compiler Correctness 2189.7 Dynamic Code Insertion 2199.8 Instrumentation 2209.9 Including more Information about Source Code 2219.10 Integration with Databases 2229.11 Increased Inter-Op erability 2229.12 Code Mobili ty 2239.13 Small Platforms 2249.14 Real-Time V11s 2269.15 Code Morphing 2279.16 Great er Optimisation 2279.17 Operating System Const ructs 2289.18 Virtual Machines for more General Portabili ty 2299.19 Distribut ed V11s 2299.20 Obj ects and V11s 2299.21 Virtual V11s 2309.22 By Way of a Conclusion 231

A Compiling ALEX 233A.1 Introduction 233A.2 Notational Conventions 233A.3 Compilation Rules 235

B Harrison Machine Compilation Rules 241B.1 Introduction 241B.2 Compilatio n Rules 241

Contents xv

C Harrison Machine Instruction Set 257

References 261

Index 265