Implements C transpiler for unpack function.

This commit is contained in:
2020-12-29 00:00:00 +00:00
parent b96fbb6eb2
commit 899aceb118
12 changed files with 622 additions and 97 deletions

36
c/unpack.h Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define unpack_Nbit_(N) \
static inline void biner_unpack_l##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \
*v = (*v & (0xff << (s*8))) | (c << (s*8)); \
} \
static inline void biner_unpack_b##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \
biner_unpack_l##N(v, c, N/8-s-1); \
}
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define unpack_Nbit_(N) \
static inline void biner_unpack_b##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \
*v = (*v & (0xff << (s*8))) | (c << (s*8)); \
} \
static inline void biner_unpack_l##N(int##N##_t* v, uint8_t c, size_t s) { \
assert(s < N/8); \
biner_unpack_b##N(v, c, N/8-s-1); \
}
#else
# error "byte order unknown"
#endif
unpack_Nbit_(8);
unpack_Nbit_(16);
unpack_Nbit_(32);
unpack_Nbit_(64);
#undef unpack_Nbit_

52
c/zone.h Normal file
View File

@@ -0,0 +1,52 @@
#pragma once
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define biner_zone_ptr(T) uintptr_t
typedef struct biner_zone_t {
uintptr_t tail;
size_t size;
uint8_t* ptr;
} biner_zone_t;
#define BINER_ZONE_RESERVE 1024
static inline uintptr_t biner_zone_alloc(biner_zone_t* z, size_t sz) {
assert(z != NULL);
assert(sz > 0);
if (z->ptr == NULL) {
z->ptr = calloc(1, sz);
if (z->ptr == NULL) {
fprintf(stderr, "malloc failure\n");
abort();
}
}
const uintptr_t oldtail = z->tail;
z->tail += sz;
if (z->tail > z->size) {
z->size = z->tail;
z->ptr = realloc(z->ptr, z->size);
}
memset(z->ptr+oldtail, 0, sz);
return oldtail;
}
static inline uintptr_t biner_zone_strnew(biner_zone_t* z, const char* str) {
assert(z != NULL);
const uintptr_t ret = biner_zone_alloc(z, strlen(str)+1);
strcpy((char*) (z->ptr+ret), str);
return ret;
}
static inline void biner_zone_deinitialize(biner_zone_t* z) {
assert(z != NULL);
if (z->ptr != NULL) free(z->ptr);
}