#include #include #include #include "vm/ByteCode.h" ByteCode* bcInit(void) { ByteCode* bc = (ByteCode*)malloc(sizeof(ByteCode)); bc->capacity = 16; bc->length = 0; bc->code = (unsigned char*)malloc((size_t)bc->capacity); return bc; } void bcDelete(ByteCode* bc) { free(bc->code); free(bc); } static void bcReAlloc(ByteCode* bc, int length) { while(bc->length + length > bc->capacity) { bc->capacity *= 2; bc->code = (unsigned char*)realloc(bc->code, (size_t)bc->capacity); } } int bcReserveBytes(ByteCode* bc, int length) { bcReAlloc(bc, length); int p = bc->length; bc->length += length; return p; } void bcSetBytes(ByteCode* bc, int p, const void* data, int length) { memcpy(bc->code + p, data, (size_t)length); } void bcAddBytes(ByteCode* bc, const void* data, int length) { bcSetBytes(bc, bcReserveBytes(bc, length), data, length); } int bcGetAddress(ByteCode* bc) { return bc->length; } void bcInsertBytes(ByteCode* bc, const void* data, int length, int address) { bcReAlloc(bc, length); memmove(bc->code + address + length, bc->code + address, (size_t)(bc->length - address)); memcpy(bc->code + address, data, (size_t)length); bc->length += length; }