PeaBox Lab › PeaLang › Learn › Chapter 12 › Lesson 4

Part III — Advanced Systems · Chapter 12 · Lesson 4 of 7 · 5 min read

Lesson 4: PEAC Format, Versioning, Validation, and Portability

Lesson level: Advanced

Purpose: Understand how compiled code objects are serialized into PEAC files, how versions are recorded, why a loader must validate every file, and what makes compiled artifacts portable.

Release note: section names, header fields, and commands in this lesson are illustrative. Verify them against your installed v0.63 release. PEAC is a preview surface; do not assume permanent compatibility.

Learning objectives

By the end of this lesson, you should be able to:

4.1 Why a file format

Compiling on every run costs time and requires the full front end at runtime. A serialized format lets a build step compile once and lets hosts load ready-to-run code. It also gives release engineering a single artifact to hash and sign.

4.2 Logical layout

PartContents
HeaderMagic marker, format version, bytecode version, flags
MetadataSource file name, compiler version, optional build information
Constant poolNumbers, strings, names, and nested code objects
Code objectsInstructions, arity, local names, line table
Integrity dataOptional checksum of the payload

Treat this as a logical model. The exact byte encoding is defined by your release, not by this book.

4.3 Magic marker and versions

The magic marker lets a loader reject files that are not PEAC at all. The format version describes the container layout. The bytecode version describes the instruction set inside. A loader may support several versions of each, and must refuse anything outside that range.

peac header
  magic            PEAC
  format_version   1
  bytecode_version 3
  compiler         pealang 0.63.0rc2

4.4 Compatibility rules

SituationExpected loader behaviour
Same versionsLoad
Older bytecode, still supportedLoad, possibly with a warning
Newer bytecode than the VM knowsRefuse with a clear version message
Unknown format versionRefuse
Preview flag set, preview disabled in hostRefuse

Never "try anyway". Executing instructions the VM does not understand can corrupt state or bypass checks.

4.5 Validation before execution

A PEAC file is input. It may be truncated, corrupted, produced by an older compiler, or deliberately crafted. Before running anything, a loader should check:

4.6 Why stack validation matters

Bytecode that pops from an empty stack, or jumps into the middle of nowhere, could read memory the VM did not intend to expose. Verifying stack depth along every control path before execution turns such files into load errors instead of runtime faults.

4.7 Validation is not trust

A structurally valid file can still contain hostile logic. Validation ensures the VM can run the code safely within its own rules; signatures (Chapter 11) and capability limits (Chapter 14) decide whether you want to run it at all.

4.8 Building and loading PEAC

pea compile src/rules.pea -o build/rules.peac
pea inspect build/rules.peac
pea run build/rules.peac

These illustrative commands compile a module, print its header and sections, and run it on the VM. Confirm exact command names with pea --help in your release.

4.9 Portability

A PEAC file is portable when it depends only on the instruction set and portable values. It stops being portable if it relies on host-specific objects, absolute file paths in metadata that the host uses, or a bytecode version newer than the target VM.

FactorPortable choice
ValuesOnly null, boolean, integer, float, string, list, string-keyed map
PathsRelative module names, not absolute paths
VersionsCompile for the oldest VM you must support
CapabilitiesRequested at runtime, never embedded as host objects

4.10 Source or PEAC?

Ship source whenShip PEAC when
Hosts run the same release as the buildStartup time matters
Readers must review the exact codeThe host should not need the compiler
Bytecode format may change soonArtifacts are signed and versioned together

Many teams ship both: PEAC for execution and source for review and rebuilds. The release manifest (Chapter 24) records the hash of each.

4.11 Reproducible compilation

The same source and compiler version should produce byte-identical PEAC. Avoid embedding timestamps or machine names in metadata, or make them optional, so release hashes stay stable across build machines.

4.12 Worked example

A host running VM bytecode version 2 receives a PEAC file built with bytecode version 3. The loader reports an unsupported version and refuses to run it. The team rebuilds with a compiler setting that targets version 2, re-signs the artifact, and records both the target version and the hash in the release notes.

4.13 Common load failures

MessageLikely causeResponse
Not a PEAC fileWrong file or corruptionCheck the path and checksum
Unsupported bytecode versionCompiler newer than VMRebuild for the target or upgrade the VM
Invalid jump targetCorruption or tamperingRebuild from source; verify signature
Stack underflow on pathMalformed code objectTreat as untrusted; do not bypass

Common mistakes

4.14 Guided practice

4.15 Hands-on lab

Build PEAC artifacts for every order_rules module. Run the Chapter 10 differential tests against both the interpreter and the compiled artifacts. Then corrupt one artifact and confirm that loading fails before any rule executes.

Deliverable: the artifact list with hashes, the differential test result, and the loader output for the corrupted file.

4.16 Review questions

4.17 Applied challenge

Write a compatibility policy for a product that supports three VM releases at once. State which bytecode version you compile for, how you test each VM, and when you drop support for the oldest.

Lesson summary

Transition to Lesson 5

With a validated code object loaded, the VM can run it. Lesson 5 follows execution: the stack, call frames, closures, modules, and exceptions.

Lesson status: Ready for review.

← Previous Next →