aboutsummaryrefslogtreecommitdiff
path: root/include/bytecode.h
blob: 4a9d49cd9288811233685edbd49ec118f5ad2d27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef TSL_BYTECODE_H
#define TSL_BYTECODE_H

#include "std/buffer.h"
#include <stdint.h>

typedef uint8_t TslOpcodeType;
/* Registers are positive if they refer to local variables and negative when they refer to parameters */
typedef int16_t TslRegister;
typedef uint16_t TslValueIndex;

typedef enum {
    TSL_OPCODE_LOAD_NUMBER,
    TSL_OPCODE_MOV_REG
} TslOpcode;

typedef struct {
    TslBuffer buffer;
    TslRegister register_counter;
} TslBytecodeWriter;

typedef struct {
    TslRegister dst_reg;
    double number;
    TslOpcodeType opcode;
} TslInstructionType1;

typedef struct {
    TslRegister dst_reg;
    TslRegister src_reg;
    TslOpcodeType opcode;
} TslInstructionType2;

void tsl_bytecode_writer_init(TslBytecodeWriter *self);
void tsl_bytecode_writer_deinit(TslBytecodeWriter *self);
void tsl_bytecode_writer_reset_register_counter(TslBytecodeWriter *self);
/* Returns -1 on error (too many registers used (more than 2^15)) */
TslRegister tsl_bytecode_writer_get_unique_register(TslBytecodeWriter *self);

int tsl_bytecode_writer_load_number(TslBytecodeWriter *self, TslRegister dst, double number);
int tsl_bytecode_writer_mov_reg(TslBytecodeWriter *self, TslRegister dst, TslRegister src);

#endif /* TSL_BYTECODE_H */