-
Notifications
You must be signed in to change notification settings - Fork 0
/
kelp.h
111 lines (84 loc) · 3.19 KB
/
kelp.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef KELP_H
#define KELP_H
typedef enum {
NOP, PUSH, ADDI, ADDF, SUBI, SUBF, MULI, MULF, DIVI, DIVF, SHL, SHR, PRINTI, PRINTF, JMP, JG,
} Opcode;
typedef struct {
int as_int;
float as_float;
} Operand;
typedef struct {
Opcode opcode;
Operand operand;
} Instruction;
typedef struct {
Instruction *instructions;
size_t num_instructions;
} Instructions;
Instructions *instructions_init();
void instructions_free(Instructions *instructions);
void emit_instruction(Instructions *instructions, Instruction instruction);
void write_instructions_to_file(Instructions *instructions, const char *filename);
Instructions *read_instructions_from_file(const char *filename);
#include "kvm/kvm.h"
void kvm_execute_program(Kvm *kvm, Instructions *instructions);
Operand make_operand(float value);
#define RESIZE_INSTRUCTIONS instructions->instructions = realloc(instructions->instructions, sizeof(Instruction) * instructions->num_instructions)
#define UNUSED(x) (void)x
#define NO_OPERAND (Operand) {0}
#endif
#ifdef KELP_IMPLEMENTATION
#include "kvm/kvm.c"
Operand make_operand(float value) {
Operand operand;
operand.as_int = (int)value;
operand.as_float = (float)value;
return operand;
}
Instruction make_instruction(Opcode opcode, Operand operand) {
return (Instruction){.opcode = opcode, .operand = operand};
}
void write_instructions_to_file(Instructions *instructions, const char *filename) {
FILE * file= fopen(filename, "wb");
fwrite(&instructions->num_instructions, sizeof(size_t), 1, file);
for (size_t i = 0; i < instructions->num_instructions; i++) {
fwrite(&(instructions->instructions[i]), sizeof(Instruction), 1, file);
}
fclose(file);
}
Instructions *instructions_from_data(Instruction *instruction_list, size_t num_instructions) {
Instructions *instructions = (Instructions *)malloc(sizeof(Instructions));
instructions->num_instructions = num_instructions;
instructions->instructions = instruction_list;
return instructions;
}
Instructions *read_instructions_from_file(const char *filename) {
FILE * file= fopen(filename, "rb");
size_t num_instructions;
int res = fread(&num_instructions, sizeof(size_t), 1, file);
UNUSED(res);
Instruction *instructions = malloc(sizeof(Instruction) * num_instructions);
for (size_t i = 0; i < num_instructions; i++) {
Instruction instruction;
res = fread(&instruction, sizeof(Instruction), 1, file);
instructions[i] = instruction;
}
fclose(file);
return instructions_from_data(instructions, num_instructions);
}
void emit_instruction(Instructions *instructions, Instruction instruction) {
instructions->num_instructions += 1;
RESIZE_INSTRUCTIONS;
instructions->instructions[instructions->num_instructions - 1] = instruction;
}
Instructions *instructions_init() {
Instructions *instructions = (Instructions *)malloc(sizeof(Instructions));
instructions->num_instructions = 0;
instructions->instructions = malloc(sizeof(Instruction) * instructions->num_instructions);
return instructions;
}
void instructions_free(Instructions *instructions) {
free(instructions->instructions);
free(instructions);
}
#endif