11
$\begingroup$

How do I implement strings, lists and dicts in bytecode?

For example, disassembling the bytecode might look like this:

LOAD str "hello, world"

But how does it work in real bytecode?

0x01 0x5 ???

PS: 0x01 is LOAD command, and 0x05 is str type.

$\endgroup$

3 Answers 3

7
$\begingroup$

Python's bytecode handles this by placing all constants in a container separate from the bytecode (co_consts), and then having instructions utilize indices into it. For lists and dicts an alternative mechanism is also used, where their literals are expanded into pushing the contained items onto the stack and then invoking a special instuction for building a literal with the specified number of items from the stack (BUILD_LIST, BUILD_MAP & some more).

$\endgroup$
2
  • 1
    $\begingroup$ Worth adding that the reason Python caches string constants in co_consts but not lists or dicts, is because the latter are mutable. Tuples do get cached if their elements are also constants, and lists and sets can be compiled to constant tuples or frozensets if they are used in expressions which can't leak references to them (e.g. x in {1, 2, 3} gets compiled to use a frozenset). $\endgroup$
    – kaya3
    May 30, 2023 at 10:19
  • $\begingroup$ Java does this as well, although too not for lists, arrays, or maps. $\endgroup$
    – Seggan
    May 30, 2023 at 12:42
4
$\begingroup$

Value "literals" in source code can be divided into four groups:

  1. Simple literals. This includes immutable "simple" values like numeric literals, and strings (if strings are immutable).
  2. Constant value expressions. This includes immutable "simple" values like numeric literals, and strings (if strings are immutable), but also immutable containers like in Python's tuple as long as all their elements are constant value expressions. (1, 2, 3) would be a constant value expression but (1, 2, three()) would not be. Something like (1, 2, 7 - 4) might be a constant value expression depending on if your parser implements constant folding.
  3. Mutable constant value expressions. There may be a better word for this, as it looks like a contradiction, but these are CVEs except the constraint of immutability is relaxed. Examples are string literals (if strings are mutable) and expressions of mutable containers only containing MCVEs. Using Python lists as examples, [1, 2, 3] would be an MCVE, but [1, 2, three()] would not. And [1, 2, 7 - 4] counting as MCVE would depend on constant folding.
  4. Arbitrary value displays. This includes (1, 2, three()) and [1, 2, three()].

To implement AVDs, you really need to consider these as expressions, like how one() + two() is implemented. In a stack-based bytecode language, that may compile to something like:

PUSH_FUNCTION one
CALL_FUNCTION
PUSH_FUNCTION two
CALL_FUNCTION
ADD

Whereas [one(), two()] may be:

PUSH_MARKER
PUSH_FUNCTION one
CALL_FUNCTION
PUSH_FUNCTION two
CALL_FUNCTION
GATHER_UP_TO_MARKER_AS_LIST

or

PUSH_FUNCTION one
CALL_FUNCTION
PUSH_FUNCTION two
CALL_FUNCTION
MAKE_LIST 2

For simple literals, there are two basic ways of dealing with them (as shown in the answers by abel1502 and RubenVerg): inline and indexing a specialised section of the bytecode.

I consider the indexing approach superior, because it allows you to collect all instances of a particular literal into a single occurrence in the bytecode. This can save memory and also CPU time, because you only have to construct the values at runtime once, even if the literal is evaluated many times. It can also help decoding instructions to be faster if there isn't an arbitrary offset between one instruction and the next (which is a consequence of inlining literals).

You can also combine both approaches, for example by inlining all fixed-size integers that fit into the argument part of the instruction and indexing for all other simple literals.

CVEs and MCVEs can be implemented the same as AVDs, but you can take advantage of their contents being known at compile time by treating them as simple literals, at the cost of making the literals section a bit more complex. And for MCVEs you need to take care that a copy is made when their expression is evaluated, either by emitting a DEEPCOPY instruction or by having the copying behaviour built into the instruction that retrieves the stored value.

$\endgroup$
2
$\begingroup$

Stuq (no public code available yet, sorry) simply inlines the string bytes.

"HELLO" compiles to

0x17 ; PUSH_STR_SHORT (length between 5 and 261 bytes)
0x00 ; 5 bytes long
0x67 ; 'H'
0x64 ; 'E'
0x6b ; 'L'
0x6b ; 'L'
0x71 ; 'O'

List literals don't exist in the traditional sense, but for completeness they look like this:

⟨ 1 2 3 ⟩ compiles to

0x3a ; PUSH_LIST_START_MARKER
0x20 ; PUSH_NUMBER_SHORT (1-byte)
0x01
0x20 0x02
0x20 0x03
0x3f ; CALL_LIST_END (collects all elements up to marker and pushes a list containing them)
$\endgroup$

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .