mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-07 04:00:17 +00:00
Compare commits
3 Commits
76af7c8de2
...
a1a7609ec0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1a7609ec0 | ||
|
|
89b6fd59f1 | ||
|
|
8999de5ad7 |
17
build_g_32.sh
Normal file
17
build_g_32.sh
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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
|
||||||
|
|
||||||
@ -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, self->elem_size * self->length);
|
memcpy(retval.data, self->data, (size_t)self->elem_size * (size_t)self->length);
|
||||||
retval.length = self->length;
|
retval.length = self->length;
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
@ -32,23 +32,22 @@ 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){
|
void c11_vector__clear(c11_vector* self) { self->length = 0; }
|
||||||
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 + self->elem_size * self->length;
|
void* p = (char*)self->data + (size_t)self->elem_size * (size_t)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 + self->elem_size * i;
|
void* p = (char*)self->data + (size_t)self->elem_size * (size_t)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;
|
||||||
|
|||||||
@ -1110,8 +1110,6 @@ 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) {
|
||||||
@ -1191,7 +1189,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(is_small_int(value)) {
|
if((int16_t)value == 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;
|
||||||
|
|||||||
@ -36,6 +36,7 @@ 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) {
|
||||||
@ -47,13 +48,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(val >= INT8_MIN && val <= INT8_MAX) {
|
if((int8_t)val == val) {
|
||||||
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(val >= INT16_MIN && val <= INT16_MAX) {
|
} else if((int16_t)val == val) {
|
||||||
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(val >= INT32_MIN && val <= INT32_MAX) {
|
} else if((int32_t)val == val) {
|
||||||
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 {
|
||||||
|
|||||||
@ -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 * NANOS_PER_SEC;
|
int64_t nanos = tms.tv_sec * (int64_t)NANOS_PER_SEC;
|
||||||
/* Add full nanoseconds */
|
/* Add full nanoseconds */
|
||||||
nanos += tms.tv_nsec;
|
nanos += tms.tv_nsec;
|
||||||
return nanos;
|
return nanos;
|
||||||
|
|||||||
@ -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) {
|
||||||
if(arg < INT16_MIN || arg > INT16_MAX) {
|
|
||||||
c11__abort("set_signed_arg: %d is out of range", arg);
|
|
||||||
}
|
|
||||||
self->arg = (int16_t)arg;
|
self->arg = (int16_t)arg;
|
||||||
|
if((int16_t)self->arg != arg) {
|
||||||
|
c11__abort("Bytecode__set_signed_arg(): %d is not representable in int16_t", arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Bytecode__is_forward_jump(const Bytecode* self) {
|
bool Bytecode__is_forward_jump(const Bytecode* self) {
|
||||||
|
|||||||
@ -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(), (py_i64)argv->_obj);
|
py_newint(py_retval(), (intptr_t)argv->_obj);
|
||||||
} else {
|
} else {
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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(), (py_i64)argv->_obj);
|
py_newint(py_retval(), (intptr_t)argv->_obj);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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 100MB of random data
|
# test 1GB random data
|
||||||
rnd = [random.randint(0, 255) for _ in range(1024*1024*100)]
|
rnd = [random.randint(0, 255) for _ in range(1024*1024*1024//16)]
|
||||||
test(bytes(rnd))
|
test(bytes(rnd))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user