Lecture 022

Compiler: translate code to machine code

Interpreter: simulate on hardware

Two Stage Execution

  1. compile the code into intermediate representation
  2. interpret intermediate representation (bytecode) using virtual machine

Diagram

Diagram
Advantage of Using Virtual Machine

Bytecode

To run program, you need:

Common Use of Two-stage Approach

Python: Python bytecode -> Python VM PHP, Js: common bytecode -> LLVM GCC: Clang Pascal: first mainstream language use two-stage approachh Java: popularized this approach. Java bytecode -> JVM

How cc0 work

How cc0 work

How coin work

How coin work

cc0 -s foo.c0 (can produce C code)
cc0 -b foo.c0 (can produce C0VM bytecode, this step is hard)
(this can be executed by c0vm, c0vm is easy)
c0vm foo.bc0

Writing the Interpreter

Understanding Arithmetic Expressions

opcode and operands

opcode and operands

Functions

Local Variable Array

Pools and Dealing with Large Constants

  1. having more bytes
  2. operating with more arguments
  3. replace with expression that evaluates to it
  4. write larger constants in the integer pool and provides an instruction to access them (C0VM)

Int Pool

Second segment of C0VM bytecode

Function Pool

Where function stores

Other Segments

C0 C0FFEE

C0 C0FFEE

Other Pools

Other Pools

Writing the Code

Given Data Structure

Data Structure

Data Structure

Table of Content