v5 Bytecode: compiling modules

Lesson, slides, and applied problem sets.

View Slides

Lesson

v5 Bytecode: compiling modules

Module initialization

Each module compiles to init code:

  1. Enter a module scope.
  2. Define all locals and functions.
  3. Build a map of exports.
  4. Exit scope.
  5. Define a global const named after the module.

Pseudo-emission:

ENTER_SCOPE
... module statements ...
PUSH_STR "add"
LOAD add
MAKE_MAP 1
EXIT_SCOPE
DEFINE_CONST math

Imports

An import becomes a const alias in the module scope:

LOAD math
DEFINE_CONST m

Member access

m.add compiles as map indexing:

LOAD m
PUSH_STR "add"
GET_INDEX

No new opcodes needed.


Module Items