v3 Bytecode Format (BC2)

Lesson, slides, and applied problem sets.

View Slides

Lesson

v3 Bytecode Format (BC2)

Why this module exists

With nested functions, we need a bytecode format that preserves unique function IDs. BC2 is a simple, readable format for that.


1) BC2 overview

BC2
FUNC 0 makeAdder x
MAKE_CLOSURE 1
RETURN
END
FUNC 1 add y
LOAD x
LOAD y
ADD
RETURN
END
MAIN
PUSH_NUM 1
CALL 1
END

Rules:

  • First non-empty line is BC2
  • FUNC <id> <name> <param...> starts a function section
  • MAIN starts the main section
  • END ends the current section
  • Empty lines and # comments are ignored

2) Instructions

Same encoding style as BC1, but includes new opcodes:

  • MAKE_CLOSURE <id>
  • MAKE_ARRAY <n>
  • MAKE_MAP <n>
  • GET_INDEX
  • SET_INDEX

3) Why text?

It is readable and easy to debug. Once the pipeline is stable, you can switch to a binary format later.


Module Items