Compare commits

..

No commits in common. "a1a7609ec09aaab3e083867ae776e06295996a84" and "76af7c8de252d7e89f7a7b9f5853206515713556" have entirely different histories.

9 changed files with 30 additions and 45 deletions

View File

@ -1,17 +0,0 @@
set -e
python prebuild.py
SRC=$(find src/ -name "*.c")
FLAGS="-std=c11 -lm -ldl -I3rd/lz4 -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1 -DPK_BUILD_MODULE_LZ4=1"
SANITIZE_FLAGS="-fsanitize=address,leak,undefined"
if [ "$(uname)" == "Darwin" ]; then
SANITIZE_FLAGS="-fsanitize=address,undefined"
fi
echo "Compiling C files..."
gcc -m32 $FLAGS $SANITIZE_FLAGS $SRC src2/main.c 3rd/lz4/lz4libs/lz4.c -o main

View File

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "pocketpy/common/utils.h"
void c11_vector__ctor(c11_vector* self, int elem_size){ void c11_vector__ctor(c11_vector* self, int elem_size){
self->data = NULL; self->data = NULL;
@ -22,7 +22,7 @@ c11_vector c11_vector__copy(const c11_vector* self) {
c11_vector retval; c11_vector retval;
c11_vector__ctor(&retval, self->elem_size); c11_vector__ctor(&retval, self->elem_size);
c11_vector__reserve(&retval, self->capacity); c11_vector__reserve(&retval, self->capacity);
memcpy(retval.data, self->data, (size_t)self->elem_size * (size_t)self->length); memcpy(retval.data, self->data, self->elem_size * self->length);
retval.length = self->length; retval.length = self->length;
return retval; return retval;
} }
@ -32,22 +32,23 @@ void c11_vector__reserve(c11_vector* self, int capacity) {
if(capacity <= self->capacity) return; if(capacity <= self->capacity) return;
// self->elem_size * capacity may overflow // self->elem_size * capacity may overflow
self->data = realloc(self->data, (size_t)self->elem_size * (size_t)capacity); self->data = realloc(self->data, (size_t)self->elem_size * (size_t)capacity);
if(self->data == NULL) c11__abort("c11_vector__reserve(): out of memory");
self->capacity = capacity; self->capacity = capacity;
} }
void c11_vector__clear(c11_vector* self) { self->length = 0; } void c11_vector__clear(c11_vector* self){
self->length = 0;
}
void* c11_vector__emplace(c11_vector* self){ void* c11_vector__emplace(c11_vector* self){
if(self->length == self->capacity) c11_vector__reserve(self, self->capacity*2); if(self->length == self->capacity) c11_vector__reserve(self, self->capacity*2);
void* p = (char*)self->data + (size_t)self->elem_size * (size_t)self->length; void* p = (char*)self->data + self->elem_size * self->length;
self->length++; self->length++;
return p; return p;
} }
bool c11_vector__contains(const c11_vector *self, void *elem){ bool c11_vector__contains(const c11_vector *self, void *elem){
for(int i = 0; i < self->length; i++){ for(int i = 0; i < self->length; i++){
void* p = (char*)self->data + (size_t)self->elem_size * (size_t)i; void* p = (char*)self->data + self->elem_size * i;
if(memcmp(p, elem, self->elem_size) == 0) return true; if(memcmp(p, elem, self->elem_size) == 0) return true;
} }
return false; return false;

View File

@ -1110,6 +1110,8 @@ static void Ctx__dtor(Ctx* self) {
c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map); c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map);
} }
static bool is_small_int(int64_t value) { return value >= INT16_MIN && value <= INT16_MAX; }
static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break) { static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break) {
int index = self->curr_iblock; int index = self->curr_iblock;
while(index >= 0) { while(index >= 0) {
@ -1189,7 +1191,7 @@ static void Ctx__revert_last_emit_(Ctx* self) {
} }
static int Ctx__emit_int(Ctx* self, int64_t value, int line) { static int Ctx__emit_int(Ctx* self, int64_t value, int line) {
if((int16_t)value == value) { if(is_small_int(value)) {
return Ctx__emit_(self, OP_LOAD_SMALL_INT, (uint16_t)value, line); return Ctx__emit_(self, OP_LOAD_SMALL_INT, (uint16_t)value, line);
} else { } else {
py_TValue tmp; py_TValue tmp;

View File

@ -36,7 +36,6 @@ static void PickleObject__py_submit(PickleObject* self, py_OutRef out) {
unsigned char* data = c11_vector__submit(&self->codes, &size); unsigned char* data = c11_vector__submit(&self->codes, &size);
unsigned char* out_data = py_newbytes(out, size); unsigned char* out_data = py_newbytes(out, size);
memcpy(out_data, data, size); memcpy(out_data, data, size);
free(data);
} }
static void PickleObject__write_bytes(PickleObject* buf, const void* data, int size) { static void PickleObject__write_bytes(PickleObject* buf, const void* data, int size) {
@ -48,13 +47,13 @@ static void pkl__emit_op(PickleObject* buf, PickleOp op) {
} }
static void pkl__emit_int(PickleObject* buf, py_i64 val) { static void pkl__emit_int(PickleObject* buf, py_i64 val) {
if((int8_t)val == val) { if(val >= INT8_MIN && val <= INT8_MAX) {
pkl__emit_op(buf, PKL_INT8); pkl__emit_op(buf, PKL_INT8);
PickleObject__write_bytes(buf, &val, 1); PickleObject__write_bytes(buf, &val, 1);
} else if((int16_t)val == val) { } else if(val >= INT16_MIN && val <= INT16_MAX) {
pkl__emit_op(buf, PKL_INT16); pkl__emit_op(buf, PKL_INT16);
PickleObject__write_bytes(buf, &val, 2); PickleObject__write_bytes(buf, &val, 2);
} else if((int32_t)val == val) { } else if(val >= INT32_MIN && val <= INT32_MAX) {
pkl__emit_op(buf, PKL_INT32); pkl__emit_op(buf, PKL_INT32);
PickleObject__write_bytes(buf, &val, 4); PickleObject__write_bytes(buf, &val, 4);
} else { } else {

View File

@ -14,7 +14,7 @@
timespec_get(&tms, TIME_UTC); timespec_get(&tms, TIME_UTC);
#endif #endif
/* seconds, multiplied with 1 billion */ /* seconds, multiplied with 1 billion */
int64_t nanos = tms.tv_sec * (int64_t)NANOS_PER_SEC; int64_t nanos = tms.tv_sec * NANOS_PER_SEC;
/* Add full nanoseconds */ /* Add full nanoseconds */
nanos += tms.tv_nsec; nanos += tms.tv_nsec;
return nanos; return nanos;

View File

@ -4,10 +4,10 @@
#include <stdint.h> #include <stdint.h>
void Bytecode__set_signed_arg(Bytecode* self, int arg) { void Bytecode__set_signed_arg(Bytecode* self, int arg) {
self->arg = (int16_t)arg; if(arg < INT16_MIN || arg > INT16_MAX) {
if((int16_t)self->arg != arg) { c11__abort("set_signed_arg: %d is out of range", arg);
c11__abort("Bytecode__set_signed_arg(): %d is not representable in int16_t", arg);
} }
self->arg = (int16_t)arg;
} }
bool Bytecode__is_forward_jump(const Bytecode* self) { bool Bytecode__is_forward_jump(const Bytecode* self) {

View File

@ -455,7 +455,7 @@ static bool builtins_ord(int argc, py_Ref argv) {
static bool builtins_id(int argc, py_Ref argv) { static bool builtins_id(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
if(argv->is_ptr) { if(argv->is_ptr) {
py_newint(py_retval(), (intptr_t)argv->_obj); py_newint(py_retval(), (py_i64)argv->_obj);
} else { } else {
py_newnone(py_retval()); py_newnone(py_retval());
} }

View File

@ -16,7 +16,7 @@ bool pk__object_new(int argc, py_Ref argv) {
static bool object__hash__(int argc, py_Ref argv) { static bool object__hash__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
assert(argv->is_ptr); assert(argv->is_ptr);
py_newint(py_retval(), (intptr_t)argv->_obj); py_newint(py_retval(), (py_i64)argv->_obj);
return true; return true;
} }

View File

@ -26,6 +26,6 @@ for i in range(100):
ratio = test(gen_data()) ratio = test(gen_data())
# print(f'compression ratio: {ratio:.2f}') # print(f'compression ratio: {ratio:.2f}')
# test 1GB random data # test 100MB of random data
rnd = [random.randint(0, 255) for _ in range(1024*1024*1024//16)] rnd = [random.randint(0, 255) for _ in range(1024*1024*100)]
test(bytes(rnd)) test(bytes(rnd))