SlideShare une entreprise Scribd logo
1  sur  11
How to Build a Virtual Machine
Terence Parr
University of San Francisco
May 13, 2014
Preliminaries
• What is a VM?
– A simulated computer that runs a simple instruction set
(bytecodes).
• And why do we want one?
– We can’t execute high-level languages like Java directly on
the computer
– Rather than compile to machine code, we generate
bytecodes for a VM; much easier
– We get code portability to anywhere with same VM
– But bytecodes are slower than raw machine code
– Ultimately VMs today compile by codes to machine code
on the fly
Goal: Simulate a simple computer
0600 A4 44 MEMCPY LDY CNT+0 ;Set Y = CNT.L
0602 D0 05 BNE LOOP ;If CNT.L > 0, then loop
0604 A5 45 LDA CNT+1 ;If CNT.H > 0,
0606 D0 01 BNE LOOP ; then loop
0608 60 RTS ;Return
0609 B1 40 LOOP LDA (SRC),Y ;Load A from ((SRC)+Y)
060B 91 42 STA (DST),Y ;Store A to ((DST)+Y)
060D 88 DEY ;Decr CNT.L
060E D0 F9 BNE LOOP ;if CNT.L > 0, then loop
0610 E6 41 INC SRC+1 ;Incr SRC += $0100
0612 E6 43 INC DST+1 ;Incr DST += $0100
0614 88 DEY ;Decr CNT.L
0615 C6 45 DEC CNT+1 ;Decr CNT.H
0617 D0 F0 BNE LOOP ;If CNT.H > 0, then loop
0619 60 RTS ;Return
061A END
Machine code Assembly code
address
Here is a real CPU block diagram and code
Programming our VM
• Our bytecodes will be very regular and higher
level than machine instructions
• Each bytecode does a tiny bit of work
• Print 1+2:
ICONST 1
ICONST 2
IADD
PRINT
0000: iconst 1 stack=[ 1 ]
0002: iconst 2 stack=[ 1 2 ]
0004: iadd stack=[ 3 ]
0005: print stack=[ ]
0006: halt stack=[ ]
Stack code Execution trace
Our instruction set
iadd integer add (pop 2 operands, add, push result)
isub
imul
ilt integer less than
ieq
br addr branch to address
brt addr branch if true
brf addr
iconst value push integer constant
load addr load local
gload addr load global variable
store addr
gstore addr
print
pop toss out the top of stack
call addr, numArgscall addr, expect numArgs
ret return from function, expected result on top of stack
halt
Instruction format
• Code memory is 32-bit word addressable
• Bytecodes stored as ints but they are bytes
• Data memory is 32-bit word addressable
• Addresses are just integer numbers
• Operands are 32-bit integers
bytecode
bytecode operand
bytecode operand1 operand2
+0
+1
+2
Sample bytecodes
0000 9 1 ICONST 1
0002 9 2 ICONST 2
0004 1 IADD
0005 14 PRINT
Address Bytecodes Assembly bytecode
Our little stack machine
data
memory
code
memory
...
stack
sp
fp
ip
registers
fetch
decode
execute
Fetch: opcode = code[ip]
Decode: switch (opcode) {…}
Execute: stack[++sp] = stack[sp--] + stack[sp--] (iadd instruction)
CPU: Fetch, decode, execute cycle
Sample implementations
case BR :
ip = code[ip++];
break;
case IADD:
b = stack[sp--]; // 2nd opnd at top of stack
a = stack[sp--]; // 1st opnd 1 below top
stack[++sp] = a + b; // push result
break;
Implementing functions
• f();
• g(int x,y) {
int z;
z = f(x,y);
...
• return 9;
call f, 0
pop
load x (fp-4)
load y (fp-3)
call f, 2
store z (fp+1)
...
iconst 9
ret

Contenu connexe

Tendances

Linux Performance Profiling and Monitoring
Linux Performance Profiling and MonitoringLinux Performance Profiling and Monitoring
Linux Performance Profiling and MonitoringGeorg Schönberger
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsoViller Hsiao
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介Akira Maruoka
 
OVS and DPDK - T.F. Herbert, K. Traynor, M. Gray
OVS and DPDK - T.F. Herbert, K. Traynor, M. GrayOVS and DPDK - T.F. Herbert, K. Traynor, M. Gray
OVS and DPDK - T.F. Herbert, K. Traynor, M. Grayharryvanhaaren
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)shimosawa
 
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Databricks
 
How Functions Work
How Functions WorkHow Functions Work
How Functions WorkSaumil Shah
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3Angel Boy
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to DebuggersSaumil Shah
 
Advanced heap exploitaion
Advanced heap exploitaionAdvanced heap exploitaion
Advanced heap exploitaionAngel Boy
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDBLinaro
 
USENIX ATC 2017: Visualizing Performance with Flame Graphs
USENIX ATC 2017: Visualizing Performance with Flame GraphsUSENIX ATC 2017: Visualizing Performance with Flame Graphs
USENIX ATC 2017: Visualizing Performance with Flame GraphsBrendan Gregg
 
DeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows KernelDeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows KernelPeter Hlavaty
 
Linux kernel tracing
Linux kernel tracingLinux kernel tracing
Linux kernel tracingViller Hsiao
 
Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...
Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...
Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...Databricks
 
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a ServiceZeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a ServiceDatabricks
 

Tendances (20)

Linux Performance Profiling and Monitoring
Linux Performance Profiling and MonitoringLinux Performance Profiling and Monitoring
Linux Performance Profiling and Monitoring
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdso
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介
 
Userspace networking
Userspace networkingUserspace networking
Userspace networking
 
OVS and DPDK - T.F. Herbert, K. Traynor, M. Gray
OVS and DPDK - T.F. Herbert, K. Traynor, M. GrayOVS and DPDK - T.F. Herbert, K. Traynor, M. Gray
OVS and DPDK - T.F. Herbert, K. Traynor, M. Gray
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
 
How Functions Work
How Functions WorkHow Functions Work
How Functions Work
 
Rust
RustRust
Rust
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3
 
MongoDB Oplog入門
MongoDB Oplog入門MongoDB Oplog入門
MongoDB Oplog入門
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to Debuggers
 
Advanced heap exploitaion
Advanced heap exploitaionAdvanced heap exploitaion
Advanced heap exploitaion
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
USENIX ATC 2017: Visualizing Performance with Flame Graphs
USENIX ATC 2017: Visualizing Performance with Flame GraphsUSENIX ATC 2017: Visualizing Performance with Flame Graphs
USENIX ATC 2017: Visualizing Performance with Flame Graphs
 
DeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows KernelDeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows Kernel
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to Makefile
 
Linux kernel tracing
Linux kernel tracingLinux kernel tracing
Linux kernel tracing
 
Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...
Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...
Redis + Structured Streaming—A Perfect Combination to Scale-Out Your Continuo...
 
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a ServiceZeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
 

En vedette

Static vs dynamic types
Static vs dynamic typesStatic vs dynamic types
Static vs dynamic typesTerence Parr
 
Antlr Conference Drools & Hibernate
Antlr Conference   Drools & HibernateAntlr Conference   Drools & Hibernate
Antlr Conference Drools & HibernateAlexandre Porcelli
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineChun-Yu Wang
 
Dc machines electrical machines – i
Dc machines   electrical machines – iDc machines   electrical machines – i
Dc machines electrical machines – iSoumyadeep Nag
 
Dynamic analysis of dc machine
Dynamic analysis of dc machineDynamic analysis of dc machine
Dynamic analysis of dc machineRamesh Babu
 
Gas turbine-power-plant[1]
Gas turbine-power-plant[1]Gas turbine-power-plant[1]
Gas turbine-power-plant[1]Manish Sadhu
 
Basics of Electrical Machines
Basics of Electrical MachinesBasics of Electrical Machines
Basics of Electrical Machinesmaneesh001
 
Electric machine
Electric machineElectric machine
Electric machineashok261084
 
power plant engineering
power plant engineeringpower plant engineering
power plant engineeringGulfaraz alam
 
Electric power transmission system engineering 2nd edition by turan gonen
Electric power transmission system engineering  2nd edition by turan gonenElectric power transmission system engineering  2nd edition by turan gonen
Electric power transmission system engineering 2nd edition by turan gonenUmmi Robbania Mushthofa
 
Power plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf downloadPower plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf downloadkiran555555
 
12.1 - Lenz's law
12.1  - Lenz's law12.1  - Lenz's law
12.1 - Lenz's lawsimonandisa
 
Powerpoint presentation about lenz's law
Powerpoint presentation about lenz's lawPowerpoint presentation about lenz's law
Powerpoint presentation about lenz's lawrdelizoneyou
 
Data acquisition system (DAS)
Data acquisition system (DAS)Data acquisition system (DAS)
Data acquisition system (DAS)Sumeet Patel
 

En vedette (19)

Static vs dynamic types
Static vs dynamic typesStatic vs dynamic types
Static vs dynamic types
 
Antlr Conference Drools & Hibernate
Antlr Conference   Drools & HibernateAntlr Conference   Drools & Hibernate
Antlr Conference Drools & Hibernate
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machine
 
From SIP to WebRTC and vice versa
From SIP to WebRTC and vice versaFrom SIP to WebRTC and vice versa
From SIP to WebRTC and vice versa
 
Dc machines electrical machines – i
Dc machines   electrical machines – iDc machines   electrical machines – i
Dc machines electrical machines – i
 
Understanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual MachineUnderstanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual Machine
 
Dynamic analysis of dc machine
Dynamic analysis of dc machineDynamic analysis of dc machine
Dynamic analysis of dc machine
 
Gas turbine-power-plant[1]
Gas turbine-power-plant[1]Gas turbine-power-plant[1]
Gas turbine-power-plant[1]
 
Basics of Electrical Machines
Basics of Electrical MachinesBasics of Electrical Machines
Basics of Electrical Machines
 
Distributed Generation
Distributed Generation Distributed Generation
Distributed Generation
 
Electrical Machines Notes
Electrical Machines NotesElectrical Machines Notes
Electrical Machines Notes
 
Electric machine
Electric machineElectric machine
Electric machine
 
power plant engineering
power plant engineeringpower plant engineering
power plant engineering
 
Electric power transmission system engineering 2nd edition by turan gonen
Electric power transmission system engineering  2nd edition by turan gonenElectric power transmission system engineering  2nd edition by turan gonen
Electric power transmission system engineering 2nd edition by turan gonen
 
Lecture 25 induction. faradays law. lenz law
Lecture 25   induction. faradays law. lenz lawLecture 25   induction. faradays law. lenz law
Lecture 25 induction. faradays law. lenz law
 
Power plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf downloadPower plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf download
 
12.1 - Lenz's law
12.1  - Lenz's law12.1  - Lenz's law
12.1 - Lenz's law
 
Powerpoint presentation about lenz's law
Powerpoint presentation about lenz's lawPowerpoint presentation about lenz's law
Powerpoint presentation about lenz's law
 
Data acquisition system (DAS)
Data acquisition system (DAS)Data acquisition system (DAS)
Data acquisition system (DAS)
 

Similaire à How to build a virtual machine

Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015curryon
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'tsyogesh46
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to knowRoberto Agostino Vitillo
 
Java Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoJava Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoValeriia Maliarenko
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIDavid Beazley (Dabeaz LLC)
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...Positive Hack Days
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationKernel TLV
 
Chapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structuChapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structuEstelaJeffery653
 
FPGA design with CλaSH
FPGA design with CλaSHFPGA design with CλaSH
FPGA design with CλaSHConrad Parker
 
LECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphesLECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphesAhmedMahjoub15
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set ArchitectureDilum Bandara
 
Java on arm theory, applications, and workloads [dev5048]
Java on arm  theory, applications, and workloads [dev5048]Java on arm  theory, applications, and workloads [dev5048]
Java on arm theory, applications, and workloads [dev5048]Aleksei Voitylov
 

Similaire à How to build a virtual machine (20)

Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to know
 
Rig nitc [autosaved] (copy)
Rig nitc [autosaved] (copy)Rig nitc [autosaved] (copy)
Rig nitc [autosaved] (copy)
 
Java Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoJava Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey Kovalenko
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard II
 
Module_01.ppt
Module_01.pptModule_01.ppt
Module_01.ppt
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
 
arduinoedit.pptx
arduinoedit.pptxarduinoedit.pptx
arduinoedit.pptx
 
Programming arduino makeymakey
Programming arduino makeymakeyProgramming arduino makeymakey
Programming arduino makeymakey
 
Introduction to Microcontrollers
Introduction to MicrocontrollersIntroduction to Microcontrollers
Introduction to Microcontrollers
 
Chapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structuChapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structu
 
FPGA design with CλaSH
FPGA design with CλaSHFPGA design with CλaSH
FPGA design with CλaSH
 
LECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphesLECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphes
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
ISA.pptx
ISA.pptxISA.pptx
ISA.pptx
 
Java on arm theory, applications, and workloads [dev5048]
Java on arm  theory, applications, and workloads [dev5048]Java on arm  theory, applications, and workloads [dev5048]
Java on arm theory, applications, and workloads [dev5048]
 

Dernier

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Dernier (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

How to build a virtual machine

  • 1. How to Build a Virtual Machine Terence Parr University of San Francisco May 13, 2014
  • 2. Preliminaries • What is a VM? – A simulated computer that runs a simple instruction set (bytecodes). • And why do we want one? – We can’t execute high-level languages like Java directly on the computer – Rather than compile to machine code, we generate bytecodes for a VM; much easier – We get code portability to anywhere with same VM – But bytecodes are slower than raw machine code – Ultimately VMs today compile by codes to machine code on the fly
  • 3. Goal: Simulate a simple computer 0600 A4 44 MEMCPY LDY CNT+0 ;Set Y = CNT.L 0602 D0 05 BNE LOOP ;If CNT.L > 0, then loop 0604 A5 45 LDA CNT+1 ;If CNT.H > 0, 0606 D0 01 BNE LOOP ; then loop 0608 60 RTS ;Return 0609 B1 40 LOOP LDA (SRC),Y ;Load A from ((SRC)+Y) 060B 91 42 STA (DST),Y ;Store A to ((DST)+Y) 060D 88 DEY ;Decr CNT.L 060E D0 F9 BNE LOOP ;if CNT.L > 0, then loop 0610 E6 41 INC SRC+1 ;Incr SRC += $0100 0612 E6 43 INC DST+1 ;Incr DST += $0100 0614 88 DEY ;Decr CNT.L 0615 C6 45 DEC CNT+1 ;Decr CNT.H 0617 D0 F0 BNE LOOP ;If CNT.H > 0, then loop 0619 60 RTS ;Return 061A END Machine code Assembly code address Here is a real CPU block diagram and code
  • 4. Programming our VM • Our bytecodes will be very regular and higher level than machine instructions • Each bytecode does a tiny bit of work • Print 1+2: ICONST 1 ICONST 2 IADD PRINT 0000: iconst 1 stack=[ 1 ] 0002: iconst 2 stack=[ 1 2 ] 0004: iadd stack=[ 3 ] 0005: print stack=[ ] 0006: halt stack=[ ] Stack code Execution trace
  • 5. Our instruction set iadd integer add (pop 2 operands, add, push result) isub imul ilt integer less than ieq br addr branch to address brt addr branch if true brf addr iconst value push integer constant load addr load local gload addr load global variable store addr gstore addr print pop toss out the top of stack call addr, numArgscall addr, expect numArgs ret return from function, expected result on top of stack halt
  • 6. Instruction format • Code memory is 32-bit word addressable • Bytecodes stored as ints but they are bytes • Data memory is 32-bit word addressable • Addresses are just integer numbers • Operands are 32-bit integers bytecode bytecode operand bytecode operand1 operand2 +0 +1 +2
  • 7. Sample bytecodes 0000 9 1 ICONST 1 0002 9 2 ICONST 2 0004 1 IADD 0005 14 PRINT Address Bytecodes Assembly bytecode
  • 8. Our little stack machine data memory code memory ... stack sp fp ip registers fetch decode execute Fetch: opcode = code[ip] Decode: switch (opcode) {…} Execute: stack[++sp] = stack[sp--] + stack[sp--] (iadd instruction)
  • 9. CPU: Fetch, decode, execute cycle
  • 10. Sample implementations case BR : ip = code[ip++]; break; case IADD: b = stack[sp--]; // 2nd opnd at top of stack a = stack[sp--]; // 1st opnd 1 below top stack[++sp] = a + b; // push result break;
  • 11. Implementing functions • f(); • g(int x,y) { int z; z = f(x,y); ... • return 9; call f, 0 pop load x (fp-4) load y (fp-3) call f, 2 store z (fp+1) ... iconst 9 ret