[VM] The JavaScript Virtual Machine

发布时间 2023-10-24 15:09:04作者: Zhentiw

Table of Content

  • Introudction to VMs
  • CPU - Understanding the Pysical Machine
  • VMs - Arrays, Objects, functions, prototype chains
  • DepotExplorer: collecting data from the VM
  • Deopt: Calling Coventions & Inlining
  • Megamohpism & Inline Cache: Object properties
  • Holey Arrays: Prototype chains resolutions
  • == vs ===: `valueOf()`

 

Introudce to VMs

VM is the thing in the middle of picture, which translate the code we write (left) into the numbers (right) which CPU can understand.

The thing in the middle is called assembly language, which is basically numbers ocverted to a humum readable text.

 

There are many VMs, the most used is call V8 for Javascript.

Pretty much everything store in CPU memory are just numbers. And it is VMs job to translate those numbers into what we use in javascript.

 

How CPU works

  • Registers: wroks like a local variable
  • ALU (Arithmetic logic unit): doing math operation
  • Program Counter (ofter called: PC): keep track location of memory

How They Work Together:

In a nutshell, as the CPU operates, it constantly fetches and processes instructions. The PC helps it keep track of where it is in the program, the registers provide a place to quickly access and store data, and the ALU does the actual computation. The harmonious interaction of these components allows the computer to function efficiently.

 


More detail

CPU (Central Processing Unit): Often referred to as the "brain" of the computer, it performs most of the processing and is responsible for executing instructions.

1. Registers:

  • Small, fast storage locations directly within the CPU.
  • Used to temporarily hold data that the CPU is currently processing.
  • Types:
    • Data registers: Store data that is being processed.
    • Address registers: Store memory addresses where data or instructions are located.
    • Special-purpose registers: Have specific functions (e.g., status register, which might indicate if the last operation resulted in a zero value).

2. ALU (Arithmetic Logic Unit):

  • Performs arithmetic and logic operations.
  • Takes inputs from registers, processes them based on the instruction (like addition, subtraction, etc.), and then sends the result back to a register or memory.

3. Program Counter (often just called the "PC"):

  • Special-purpose register that holds the address of the next instruction to be executed.
  • Each time an instruction is executed, the PC is updated to point to the next instruction.
  • Some instructions, like jumps or branches, directly modify the PC.

How They Work Together:

  1. Fetch: The CPU fetches the instruction from the memory address located in the PC.
  2. Increment: After fetching the instruction, the PC is incremented to point to the next instruction.
  3. Decode: The fetched instruction is decoded to determine which operation should be performed by the ALU.
  4. Execute: The operation is performed. Data might be fetched from registers for the ALU to process.
  5. Store: After execution, the result is stored in a register or in memory.