Tagged Values and Representation Invariants

Use a tag bit to distinguish immediate integers from aligned references and calculate the integer range that remains.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. A representation reserves distinguishable bit patterns
  2. Every consumer must agree on the invariant

A representation reserves distinguishable bit patterns

If object addresses are aligned so their low bit is zero, a runtime can reserve low bit one for small integer values. For an unsigned \(w\)-bit word, encode \(n\) as \(2n+1\) for \(0\le n<2^{w-1}\). Recover \(n\) by shifting right. The lost bit narrows the immediate range, so larger numbers may use heap objects or another representation. A runtime can choose other tags for other value shapes.

Every consumer must agree on the invariant

A collector tracing references must distinguish actual pointers from immediates. Arithmetic must check or otherwise handle an output that fails the immediate range. Representational changes across calls and generated code require compatible contracts. Mark and sweep depends on locating reference fields. Bytecode stack effects provide useful abstract types before concrete representation decisions.[1][2]

References

  1. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩
  2. Robert Nystrom, Crafting Interpreters ↩