Listoflectures IntroductionandFunctionalProgramming ImperativeProgrammingandDataStructures 3 Environment 4 Evaluation 5 ObjectOrientedProgramming 6 Macrosanddecorators 7 VirtualMachinesandBytecode 8 GarbageCollectionandNativeCode 9 DistributedComputing 10LogicProgramming 11Summary 1 2 TDDA69DataandProgramStructure VirtualMachinesandBytecode CyrilleBerger 2/40 Howisaprograminterpreted? Sourcecode Learnwhatisavirtual machine,bytecode Learnhowtogenerate thebytecodeandexecute it Parser Parser AbstractSyntaxTree Treevisitor Generator Sourcecode Bytecode VirtualMachine Assembler Assembly ... OperatingSystem Lecturegoal CPU 3/40 4/40 Lecturecontent VirtualMachines Typesofvirtual/hardwaremachines Bytecode VirtualMachines Simpleinstructionset FromASTtoBytecode Bytecodeinterpreter Meta-circular 5/40 LanguageVirtualMachine WhatisaVirtualMachine? AVirtualMachineisahardwareor softwareemulationofarealor hypotheticalcomputersystem Asystemvirtualmachineemulatesa completesystemandisintentedto executeacompleteoperatingsystem SourceCode Compiler ByteCode Examples:VirtualBox,VMWare,Parallels... Aprocess/languagevirtualmachineruns asingleprograminasingleprocess VirtualMachine VirtualMachine VirtualMachine Windows Linux Mac Examples:JVM,CPython,V8,Dalvik... 7 8 BenefitsofVirtualMachines NativeEnvironmentvsEmulatedEnvironment OperatingSystem Portability:VirtualMachinesare compatiblewithvarioushardware platformsandOperatingSystems Isolation:VirtualMachinesare isolatedfromeachother Input/Output NativeProgram Hardware Libraries VirtualMachine OperatingSystem Emulated HardwareInterface Hardware Bindings Libraries Emulated Input/Output EmulatedProgram Forrunningincompatibleapplicationsconcurrently Input/Output Encapsulation:computationis seperatedfromtheoperatingsystem Beneficialforsecurity 9 10 Typesofvirtual/hardwaremachines Typesofvirtual/hardwaremachines Register Stack 12 StackMachine:Formaldefinition RegisterMachine:Formaldefinition A(in)finitesetofregisters,whichholdsa singlenon-negativeinteger Aninstructionset,whichdefinesthe operationonregisters An(almost)infinitestack,whichholds integers Aninstructionset,whichdefinesthe operationonthestack Arithmetic,control,input/output... Arithmeticoperationsarealwaysappliedonthetoptwo registersandtheresultsisstoredinthetop Astateregister,whichholdsthecurrent instructionanditsindex Sequentiallistoflabeledinstructions whichdefinestheprogramtobeexecuted Astateregister,whichholdsthecurrent instructionanditsindex Sequentiallistoflabeledinstructions whichdefinestheprogramtobeexecuted 13 14 VirtualMachinesforDynamicTyping StackMachinesvsRegisterMachines Remember: StackMachinesneedmorecompactcode Arithmeticinstructionaresmaller Withstatictyping,typesarecheckedduring compilation Withdyamictyping,typesarecheckedduring execution StackMachineshavesimplercompiler, interpretersandaminimalprocessorstate StackMachineshaveaperformance disadvantages Implication Morememoryreferences,lesscachingoftemporaries Highercostoffactoringoutcommonsubexpressions Stack/Registerscontainspointertoobjects Functioncallconvention Ithastobestoredasatemporaryvariable Mostcommonhardwareareregistermachines 15 16 Bytecode Bytecode Foravirtualstack Instructionset Generatethe Interpretingthebytecode 18 Stackmanagment PUSH Simpleinstructionset Pushtheconstantonthestack POP[number] Popacertainnumbersofvariablesfromthe stack 20 Arithmeticoperators Variables ADD, LOAD Poptwoargumentsonthestack Pushtheresult Pushthevalueofvariable DCL Declarethevariable STORE[varname] Getthevalue,storetheresultandpushthe value 21 22 Jumps Functions JMP CALL[arguments] Jumptoexecuteinstructionatthegivenindex Popthefunctionobjectandcallitwiththe givennumberofarguments IFJMP RET Popthevalueandiftruejumpto[idx] Return Buthowtoknowwheretoreturn? 23 24 Objects LOAD_MEMBER Poptheobjectandpushthevalueofvariable STORE_MEMBER FromASTtoBytecode Poptheobjectandvalueandstoreitandpush thevalue 25 FromASTtoBytecode Arithmeticoperation Withatree Itcantakeseveral 1+2 a-2 var a=1 vara= a a=b*c a.b=c Findthevariables Computethejumps Generatethecode Therealchallengeistomap highlevellanguageto instructions 27 28 Functions Controls func(1) func(1,2) console.log('Helloworld!') functionfunc(a,b){returna+b; } if(a){console.log('test')} if(a){console.log('hello')} else{console.log('world')} vara=10; while(a){a-=1;} for(vara=0;a<10;a+=1) {console.log(a);} for(vara=0;a<10;a+=1) {console.log(a);if(a+b){break;}} 29 30 Componentsofbytecodeinterpreter Verifier Bytecodeinterpreter Bytecodemaycomefrombuggycompileror malicioussource Dynamiccheckingoftypes,arraybounds, functionarguments... Instructionexecuter 32 Verification Bytecodeinterpreter Verifiercheckscorrectnessof bytecode Standardvirtualmachine interpretsinstructions Everyinstructionmusthaveavalidoperation code Everyinstructionmusthavevalidparameters Everybranchinstructionmustbranchtothe startofsomeotherinstruction,notmiddleof instruction Performrun-timecheckssuchasarraybounds andtypesandfunctionarguments Possibletocompilebytecodetonativecode (JIT:Just-In-Time) Callnative TypicallyfunctionswritteninC 33 34 Meta-circularevaluator Meta-circularevaluator Anevaluatorthatiswritteninthe samelanguagethatitevaluatesis saidtobemetacircular(SICP4.1). Easytoextendthelanguage Makeiteasiertobuildsophisticated debuggers Moresuitedforbuildingnewlanguages 36 RPythonandPyPy PyPyVirtualMachine Usuallyinterpretersarewritten inatargetplatformlanguage suchasC RPythonisasubsetof PyPyuses SourceCode Compiler PyPyVM ByteCode toprovideasetoftoolsforimplementing interpretersforanylanguage apythonimplementationusingthosetools PyPyVM PyPyVM PyPyVM Windows Linux Mac 37 BenefitsofRPythonoverhostlanguage Easiertodevelop 38 Conclusion VirtualMachinesforinterpreting programs Stackmachinesareeasier toimplementbutslower thanregistermachines VirtualMachinesintroduce compilationoverhead,butare fastertoexecute IfyouknowPython,youknowRPython Easiertodebug SinceyoucanrunRPythonprogramwitha regularPythoninterpreter Factortheporttodifferent platform 39 40/40