Wiki

Clone wiki

Alchemy / Bytecode

Bytecode

The following describes the format of the bytecode produced by the compile function in compiler.c. This is purely for aiding in understanding what goes on in compiler.c and program.c — the bytecode is such that it cannot be stored to disk and re-read at a later time. It is represented by an array of uint16_t. For the lines which add to the bytecode (described below), their outputs are prefixed by a single element (16 bits), where the lowest 1 bit is the op-code, and the high 15 bits are the line number.

LineNumber = (FirstChunk >> 1), OpCode = (FirstChunk & 1)

There four possible types of lines that can be encountered:

  1. Blank or comment (This is a comment)
  2. Step declaration Step 42:
  3. Jump if non-zero Saturnium? 42
  4. React Fuse 968 dr Mercurius, 959 oz Saturnium (2895 oz Alkahest): Aquariatus

Comments

If a blank line or comment is encountered, that line is simply skipped and has no effects.

Step Declarations

Step declarations are not actually inserted anywhere into the bytecode, but are instead saved into the alc_program's step table, where the key is the string representation of the step label, and its value is the offset into the bytecode at which the step begins.

Jump If Not Zero

Jump are written into the bytecode as ElementIndex[2] StepNumber[2].

Reaction

Reactions produce the largest code, structured:

(Process, Solvent, ReagentCount)[2] ReagentList[2n] SolventAmount[2] ProductIndex[2]

where ReagentList is Amount[2] ElementIndex[2], ReagentCount number of times, and the first chunk is split such that

Process = (FirstChunk >> 9), Solvent = ((FirstChunk >> 8) & 1), ReagentCount = (FirstChunk & 0xFF)

Updated