-
Notifications
You must be signed in to change notification settings - Fork 5
/
buffer_util.h
58 lines (48 loc) · 1.12 KB
/
buffer_util.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
#ifndef BUFFER_UTIL_H
#define BUFFER_UTIL_H
#include "compile.h"
static int instruction_position(compiler_t *ctx)
{
return heap_string_size(&ctx->function->bytecode);
}
static void dd(compiler_t *ctx, u32 i)
{
union
{
uint32_t i;
uint8_t b[4];
} u = { .i = i };
for(size_t i = 0; i < 4; ++i)
heap_string_push(&ctx->function->bytecode, u.b[i]);
}
static void dw(compiler_t *ctx, u16 i)
{
union
{
uint16_t s;
uint8_t b[2];
} u = { .s = i };
heap_string_push(&ctx->function->bytecode, u.b[0]);
heap_string_push(&ctx->function->bytecode, u.b[1]);
}
static void db(compiler_t *ctx, u8 op)
{
heap_string_push(&ctx->function->bytecode, op);
}
static void set8(compiler_t *ctx, int offset, u8 op)
{
ctx->function->bytecode[offset] = op;
}
static void set32(compiler_t *ctx, int offset, u32 value)
{
u32 *ptr = (u32*)&ctx->function->bytecode[offset];
*ptr = value;
}
static void buf(compiler_t *ctx, const char *buf, size_t len)
{
for(size_t i = 0; i < len; ++i)
{
heap_string_push(&ctx->function->bytecode, buf[i] & 0xff);
}
}
#endif