aboutsummaryrefslogtreecommitdiff
path: root/src/bytecode.c
blob: a0b54067d705cf3e1f2d51c54c970063827abb09 (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
#include "../include/bytecode.h"
#include <assert.h>

void tsl_bytecode_writer_init(TslBytecodeWriter *self) {
    tsl_buffer_init(&self->buffer);
    self->register_counter = 0;
}

void tsl_bytecode_writer_deinit(TslBytecodeWriter *self) {
    tsl_buffer_deinit(&self->buffer);
}

void tsl_bytecode_writer_reset_register_counter(TslBytecodeWriter *self) {
    self->register_counter = 0;
}

TslRegister tsl_bytecode_writer_get_unique_register(TslBytecodeWriter *self) {
    if(self->register_counter < INT16_MAX)
        return self->register_counter++;
    return -1;
}

int tsl_bytecode_writer_load_number(TslBytecodeWriter *self, TslRegister dst, double number) {
    TslInstructionType1 instruction;
    instruction.opcode = TSL_OPCODE_LOAD_NUMBER;
    instruction.dst_reg = dst;
    instruction.number = number;
    return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction));
}

tsl_bytecode_writer_mov_reg(TslBytecodeWriter *self, TslRegister dst, TslRegister src) {
    TslInstructionType2 instruction;
    instruction.opcode = TSL_OPCODE_MOV_REG;
    instruction.dst_reg = dst;
    instruction.src_reg = src;
    return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction));
}