aboutsummaryrefslogtreecommitdiff
path: root/src/bytecode.c
blob: 9406852da734ea62d97a11c3a165ef3dbb8c310e (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
#include "../include/bytecode.h"
#include <assert.h>
#include <stdio.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++;
    fprintf(stderr, "Error: Too many variables in the same scope\n");
    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));
}

int 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));
}