Generic Bytecode & Generic Bytecode Interpreter

GByC & GByCI

GByC is a simple bytecode concept. This page was created to explain GByC and to offer a FLOSS reference implementation of GByC, called GByCI.

One GByC instruction consists of its identifier, a length identifier n and up to n instruction arguments. Look at this example for the PUSH instruction:

PUSH Instruction (Example)

As you can see, every position on the GByC Stack is 16 Bit (2 Byte) long. This means you are allowed to write onto 65,536 "cells". [The GByCI implementation uses int32_t for every cell. Consequently, the range for each cell is -2,147,483,647 to 2,147,483,647.]

All instructions are explained below.

Download GByCI

GByCI 0.8 (Written in C)

GByCI 0.8 (Win32 Binary, Compiler: tcc by Fabrice Bellard)

License: BSD-like license (see source code)

Instructions

8-BIT PUSH

Example:
PUSH Instruction (Example)

Identifier1
Length identifier n> 2
Byte 1 & Byte 2Rewind to this position before pushing
Bytes …nto be pushed
FeaturePush n chars onto Stack

16-BIT PUSH

Example:
16-BIT PUSH Instruction (Example)

Identifier2
Length identifier n> 2
Byte 1 & Byte 2Rewind to this position before pushing
Byte 2 & Byte 3to be pushed ((B2 << 8) | B3)
Byte 4 & Byte 6to be pushed ((B4 << 8) | B5)
FeaturePush (n-2)/2 16-bit ints onto Stack

PRINT STACK

Example:
PRINT STACK Instruction (Example)

Identifier4
Length identifier n= 0
FeaturePrint Stack on stdout, assuming every Stack byte is one char

REGISTER JUMP

Identifier8
Length identifier n= 2
Byte 1 & Byte 2Pseudo-Pointer to JUMP registry
FeatureWrite current file position (ftell()) to JUMP registry

REMOTE REGISTER JUMP

Identifier9
Length identifier n= 6
Byte 1 & Byte 2Pseudo-Pointer to JUMP registry
Byte 3, Byte 4, Byte 5, Byte 6Pointer to bytecode position (max. 4GiB)
FeatureWrite file position (given in B3-B6) to JUMP registry

DO JUMP

Identifier10
Length identifier n= 2
Byte 1 & Byte 2Pseudo-Pointer to JUMP registry
FeatureJump to index (B1-B2) in JUMP registry; register JUMP #65535 with current file position (ftell())

COND JUMP

Identifier12
Length identifier n= 6
Byte 1 & Byte 2If condition 1, must be on Stack
Byte 3 & Byte 4If condition 2, must be on Stack
Byte 5 & Byte 6Pseudo-Pointer to JUMP registry
FeatureCompare Stack{B1-B2} with Stack{B3-B4}; if equal, JUMP to B5-B6 in JUMP registry (register JUMP #65535 with current ftell()). If not, proceed.

NOT COND JUMP

Identifier13
Length identifier n= 6
Byte 1 & Byte 2If condition 1, must be on Stack
Byte 3 & Byte 4If condition 2, must be on Stack
Byte 5 & Byte 6Pseudo-Pointer to JUMP registry
FeatureCompare Stack{B1-B2} with Stack{B3-B4}; if not equal, JUMP to B5-B6 in JUMP registry (register JUMP #65535 with current ftell()). If, proceed.

COND JUMP WITH ELSE

Identifier14
Length identifier n= 6
Byte 1 & Byte 2If condition 1, must be on Stack
Byte 3 & Byte 4If condition 2, must be on Stack
Byte 5 & Byte 6Pseudo-Pointer to JUMP registry
Byte 7 & Byte 8Pseudo-Pointer to JUMP registry for ELSE
FeatureCompare {B1-B2} with {B3-B4}; if equal, JUMP to B5-B6 in JUMP registry (register JUMP #65535 with current ftell()). If not, JUMP to B7-B8 (register JUMP #65535 with current ftell()).

ADD

Identifier22
Length identifier n= 6
Byte 1 & Byte 2Stack position
Byte 3 & Byte 4Stack position
Byte 5 & Byte 6Stack position
Feature{B5-B6} will contain the result of the {B1-B2}+{B3-B4} addition.

SUB

Identifier24
Length identifier n= 6
Byte 1 & Byte 2Stack position
Byte 3 & Byte 4Stack position
Byte 5 & Byte 6Stack position
Feature{B5-B6} will contain the result of the {B1-B2}+{B3-B4} subtraction.

NEG

Identifier26
Length identifier n= 4
Byte 1 & Byte 2Stack position
Byte 3 & Byte 4Stack position
Feature{B3-B4} will be the result of the {B1-B2} negation (equal to {B1-B2}*-1).

EXIT

Identifier254
Length identifier n= 2
Byte 1 & Byte 216-bit return value
FeatureSet return value to B1-B2.

Bytecode examples

Example 1: gbyci_1.bc Print a string
mgod output:

1 6 0 0 77 82 80 71 1 3 0 4 33 4 0 254 2 0 0

Example 2: gbyci_2.bc Repeats a string
mgod output:

1 3 0 13 8 1 3 0 14 1 8 2 0 0 1 6 0 0 77 82 80 71 4 0 24 6 0 13 0 14 0 13
13 6 0 13 0 14 0 0 254 2 0 0

Example 3: gbyci_3.bc Prints all printable ASCII chars
mgod output:

1 3 0 0 33 1 3 0 14 1 1 3 0 15 127 8 2 0 0 4 0 22 6 0 0 0 14 0 0
13 6 0 0 0 15 0 0 254 2 0 0