2018-05-30 00:55:58 +02:00
|
|
|
#include "pxtbase.h"
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
using namespace std;
|
2016-11-30 00:06:34 +01:00
|
|
|
|
|
|
|
namespace pxt {
|
2018-05-30 00:55:58 +02:00
|
|
|
|
|
|
|
TValue incr(TValue e) {
|
|
|
|
if (isRefCounted(e)) {
|
|
|
|
getVTable((RefObject *)e);
|
|
|
|
#if MEMDBG_ENABLED
|
|
|
|
if (((RefObject *)e)->refcnt != 0xffff)
|
|
|
|
MEMDBG("INCR: %p refs=%d", e, ((RefObject *)e)->refcnt);
|
|
|
|
#endif
|
|
|
|
((RefObject *)e)->ref();
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
return e;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void decr(TValue e) {
|
|
|
|
if (isRefCounted(e)) {
|
|
|
|
#if MEMDBG_ENABLED
|
|
|
|
if (((RefObject *)e)->refcnt != 0xffff)
|
|
|
|
MEMDBG("DECR: %p refs=%d", e, ((RefObject *)e)->refcnt);
|
|
|
|
#endif
|
|
|
|
((RefObject *)e)->unref();
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
// TODO
|
|
|
|
Action mkAction(int reflen, int totallen, int startptr) {
|
|
|
|
check(0 <= reflen && reflen <= totallen, ERR_SIZE, 1);
|
|
|
|
check(reflen <= totallen && totallen <= 255, ERR_SIZE, 2);
|
|
|
|
check(bytecode[startptr] == 0xffff, ERR_INVALID_BINARY_HEADER, 3);
|
|
|
|
check(bytecode[startptr + 1] == PXT_REF_TAG_ACTION, ERR_INVALID_BINARY_HEADER, 4);
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
uintptr_t tmp = (uintptr_t)&bytecode[startptr];
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
if (totallen == 0) {
|
|
|
|
return (TValue)tmp; // no closure needed
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void *ptr = ::operator new(sizeof(RefAction) + totallen * sizeof(unsigned));
|
|
|
|
RefAction *r = new (ptr) RefAction();
|
|
|
|
r->len = totallen;
|
|
|
|
r->reflen = reflen;
|
|
|
|
r->func = (ActionCB)((tmp + 4) | 1);
|
|
|
|
memset(r->fields, 0, r->len * sizeof(unsigned));
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
MEMDBG("mkAction: start=%p => %p", startptr, r);
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
return (Action)r;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
// TODO
|
|
|
|
TValue runAction3(Action a, TValue arg0, TValue arg1, TValue arg2) {
|
|
|
|
auto aa = (RefAction *)a;
|
|
|
|
if (aa->vtable == PXT_REF_TAG_ACTION) {
|
|
|
|
check(aa->refcnt == 0xffff, ERR_INVALID_BINARY_HEADER, 4);
|
|
|
|
return ((ActionCB)(((uintptr_t)a + 4) | 1))(NULL, arg0, arg1, arg2);
|
|
|
|
} else {
|
|
|
|
check(aa->refcnt != 0xffff, ERR_INVALID_BINARY_HEADER, 4);
|
|
|
|
return aa->runCore(arg0, arg1, arg2);
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue runAction2(Action a, TValue arg0, TValue arg1) {
|
|
|
|
return runAction3(a, arg0, arg1, 0);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue runAction1(Action a, TValue arg0) {
|
|
|
|
return runAction3(a, arg0, 0, 0);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue runAction0(Action a) {
|
|
|
|
return runAction3(a, 0, 0, 0);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
RefRecord *mkClassInstance(int vtableOffset) {
|
|
|
|
VTable *vtable = (VTable *)&bytecode[vtableOffset];
|
2016-12-08 00:44:35 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
intcheck(vtable->methods[0] == &RefRecord_destroy, ERR_SIZE, 3);
|
|
|
|
intcheck(vtable->methods[1] == &RefRecord_print, ERR_SIZE, 4);
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void *ptr = ::operator new(vtable->numbytes);
|
|
|
|
RefRecord *r = new (ptr) RefRecord(PXT_VTABLE_TO_INT(vtable));
|
|
|
|
memset(r->fields, 0, vtable->numbytes - sizeof(RefRecord));
|
|
|
|
MEMDBG("mkClass: vt=%p => %p", vtable, r);
|
|
|
|
return r;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue RefRecord::ld(int idx) {
|
|
|
|
// intcheck((reflen == 255 ? 0 : reflen) <= idx && idx < len, ERR_OUT_OF_BOUNDS, 1);
|
|
|
|
return fields[idx];
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue RefRecord::ldref(int idx) {
|
|
|
|
// DMESG("LD %p len=%d reflen=%d idx=%d", this, len, reflen, idx);
|
|
|
|
// intcheck(0 <= idx && idx < reflen, ERR_OUT_OF_BOUNDS, 2);
|
|
|
|
TValue tmp = fields[idx];
|
|
|
|
incr(tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefRecord::st(int idx, TValue v) {
|
|
|
|
// intcheck((reflen == 255 ? 0 : reflen) <= idx && idx < len, ERR_OUT_OF_BOUNDS, 3);
|
|
|
|
fields[idx] = v;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefRecord::stref(int idx, TValue v) {
|
|
|
|
// DMESG("ST %p len=%d reflen=%d idx=%d", this, len, reflen, idx);
|
|
|
|
// intcheck(0 <= idx && idx < reflen, ERR_OUT_OF_BOUNDS, 4);
|
|
|
|
decr(fields[idx]);
|
|
|
|
fields[idx] = v;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefObject::destroyVT() {
|
|
|
|
((RefObjectMethod)getVTable(this)->methods[0])(this);
|
|
|
|
::operator delete(this);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefObject::printVT() {
|
|
|
|
((RefObjectMethod)getVTable(this)->methods[1])(this);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefRecord_destroy(RefRecord *r) {
|
|
|
|
VTable *tbl = getVTable(r);
|
|
|
|
uint8_t *refmask = (uint8_t *)&tbl->methods[tbl->userdata & 0xff];
|
|
|
|
int len = (tbl->numbytes >> 2) - 1;
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
if (refmask[i])
|
|
|
|
decr(r->fields[i]);
|
|
|
|
r->fields[i] = 0;
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefRecord_print(RefRecord *r) {
|
|
|
|
DMESG("RefRecord %p r=%d size=%d bytes", r, r->refcnt, getVTable(r)->numbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
TValue Segment::get(unsigned i) {
|
2016-12-24 04:49:15 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("In Segment::get index:%d", i);
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
if (i < length) {
|
|
|
|
return data[i];
|
2016-12-24 04:49:15 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
return Segment::DefaultValue;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void Segment::setRef(unsigned i, TValue value) {
|
|
|
|
decr(get(i));
|
|
|
|
set(i, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Segment::set(unsigned i, TValue value) {
|
|
|
|
if (i < size) {
|
|
|
|
data[i] = value;
|
|
|
|
} else if (i < Segment::MaxSize) {
|
|
|
|
growByMin(i + 1);
|
|
|
|
data[i] = value;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (length <= i) {
|
|
|
|
length = i + 1;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("In Segment::set");
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
ramint_t Segment::growthFactor(ramint_t size) {
|
|
|
|
if (size == 0) {
|
2016-12-24 04:49:15 +01:00
|
|
|
return 4;
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
|
|
|
if (size < 64) {
|
2016-12-24 04:49:15 +01:00
|
|
|
return size * 2; // Double
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
if (size < 512) {
|
|
|
|
return size * 5 / 3; // Grow by 1.66 rate
|
2016-12-24 04:49:15 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
// Grow by constant rate
|
|
|
|
if ((unsigned)size + 256 < MaxSize)
|
|
|
|
return size + 256;
|
|
|
|
else
|
|
|
|
return MaxSize;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void Segment::growByMin(ramint_t minSize) {
|
|
|
|
growBy(max(minSize, growthFactor(size)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Segment::growBy(ramint_t newSize) {
|
2016-12-24 04:49:15 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("growBy: %d", newSize);
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
2018-05-30 00:55:58 +02:00
|
|
|
if (size < newSize) {
|
|
|
|
// this will throw if unable to allocate
|
|
|
|
TValue *tmp = (TValue *)(::operator new(newSize * sizeof(TValue)));
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
// Copy existing data
|
|
|
|
if (size) {
|
|
|
|
memcpy(tmp, data, size * sizeof(TValue));
|
|
|
|
}
|
|
|
|
// fill the rest with default value
|
|
|
|
memset(tmp + size, 0, (newSize - size) * sizeof(TValue));
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
// free older segment;
|
|
|
|
::operator delete(data);
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
data = tmp;
|
|
|
|
size = newSize;
|
2016-12-24 04:49:15 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("growBy - after reallocation");
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
2016-12-24 04:49:15 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
// else { no shrinking yet; }
|
|
|
|
return;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void Segment::ensure(ramint_t newSize) {
|
|
|
|
if (newSize < size) {
|
2017-01-04 22:07:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
growByMin(newSize);
|
|
|
|
}
|
2017-01-04 22:07:58 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void Segment::setLength(unsigned newLength) {
|
|
|
|
if (newLength > size) {
|
2017-07-21 20:42:48 +02:00
|
|
|
ensure(length);
|
2017-01-04 22:07:58 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
length = newLength;
|
|
|
|
return;
|
|
|
|
}
|
2017-01-04 22:07:58 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void Segment::push(TValue value) {
|
|
|
|
this->set(length, value);
|
|
|
|
}
|
2017-07-21 20:42:48 +02:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue Segment::pop() {
|
2016-12-24 04:49:15 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("In Segment::pop");
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
if (length > 0) {
|
2017-07-21 20:42:48 +02:00
|
|
|
--length;
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue value = data[length];
|
2017-01-09 03:43:14 +01:00
|
|
|
data[length] = Segment::DefaultValue;
|
2016-12-24 04:49:15 +01:00
|
|
|
return value;
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
return Segment::DefaultValue;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
// this function removes an element at index i and shifts the rest of the elements to
|
|
|
|
// left to fill the gap
|
|
|
|
TValue Segment::remove(unsigned i) {
|
2016-12-24 04:49:15 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("In Segment::remove index:%d", i);
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
2018-05-30 00:55:58 +02:00
|
|
|
if (i < length) {
|
|
|
|
// value to return
|
|
|
|
TValue ret = data[i];
|
|
|
|
if (i + 1 < length) {
|
|
|
|
// Move the rest of the elements to fill in the gap.
|
|
|
|
memmove(data + i, data + i + 1, (length - i - 1) * sizeof(unsigned));
|
2017-01-04 22:07:58 +01:00
|
|
|
}
|
2017-07-21 20:42:48 +02:00
|
|
|
length--;
|
|
|
|
data[length] = Segment::DefaultValue;
|
2017-01-04 22:07:58 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("After Segment::remove index:%d", i);
|
2017-01-04 22:07:58 +01:00
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
2017-01-04 22:07:58 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
return Segment::DefaultValue;
|
|
|
|
}
|
2017-01-04 22:07:58 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
// this function inserts element value at index i by shifting the rest of the elements right.
|
|
|
|
void Segment::insert(unsigned i, TValue value) {
|
2017-01-04 22:07:58 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("In Segment::insert index:%d value:%d", i, value);
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
2017-01-04 22:07:58 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
if (i < length) {
|
2017-01-04 22:07:58 +01:00
|
|
|
ensure(length + 1);
|
2018-05-30 00:55:58 +02:00
|
|
|
|
|
|
|
// Move the rest of the elements to fill in the gap.
|
|
|
|
memmove(data + i + 1, data + i, (length - i) * sizeof(unsigned));
|
2017-01-04 22:07:58 +01:00
|
|
|
|
2017-07-21 20:42:48 +02:00
|
|
|
data[i] = value;
|
2017-01-04 22:07:58 +01:00
|
|
|
length++;
|
2018-05-30 00:55:58 +02:00
|
|
|
} else {
|
|
|
|
// This is insert beyond the length, just call set which will adjust the length
|
2017-01-04 22:07:58 +01:00
|
|
|
set(i, value);
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2017-01-04 22:07:58 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("After Segment::insert index:%d", i);
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void Segment::print() {
|
|
|
|
DMESG("Segment: %p, length: %d, size: %d", data, (unsigned)length, (unsigned)size);
|
|
|
|
for (unsigned i = 0; i < size; i++) {
|
|
|
|
DMESG("-> %d", (unsigned)(uintptr_t)data[i]);
|
2016-12-24 04:49:15 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
bool Segment::isValidIndex(unsigned i) {
|
|
|
|
if (i > length) {
|
2016-12-24 04:49:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void Segment::destroy() {
|
2016-12-24 04:49:15 +01:00
|
|
|
#ifdef DEBUG_BUILD
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("In Segment::destroy");
|
|
|
|
this->print();
|
2017-07-21 20:42:48 +02:00
|
|
|
#endif
|
2018-05-30 00:55:58 +02:00
|
|
|
length = size = 0;
|
|
|
|
::operator delete(data);
|
|
|
|
data = nullptr;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefCollection::push(TValue x) {
|
|
|
|
incr(x);
|
|
|
|
head.push(x);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue RefCollection::pop() {
|
|
|
|
TValue ret = head.pop();
|
|
|
|
incr(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-12-24 04:49:15 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue RefCollection::getAt(int i) {
|
|
|
|
TValue tmp = head.get(i);
|
|
|
|
incr(tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
TValue RefCollection::removeAt(int i) {
|
|
|
|
return head.remove(i);
|
|
|
|
}
|
2017-01-04 22:07:58 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefCollection::insertAt(int i, TValue value) {
|
|
|
|
head.insert(i, value);
|
|
|
|
incr(value);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefCollection::setAt(int i, TValue value) {
|
|
|
|
incr(value);
|
|
|
|
head.setRef(i, value);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
int RefCollection::indexOf(TValue x, int start) {
|
|
|
|
#ifndef X86_64
|
|
|
|
unsigned i = start;
|
|
|
|
while (head.isValidIndex(i)) {
|
|
|
|
if (pxt::eq_bool(head.get(i), x)) {
|
2016-11-30 00:06:34 +01:00
|
|
|
return (int)i;
|
2016-12-24 04:49:15 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
i++;
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
bool RefCollection::removeElement(TValue x) {
|
|
|
|
int idx = indexOf(x, 0);
|
|
|
|
if (idx >= 0) {
|
|
|
|
decr(removeAt(idx));
|
2016-11-30 00:06:34 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
namespace Coll0 {
|
|
|
|
PXT_VTABLE_BEGIN(RefCollection, 0, 0)
|
|
|
|
PXT_VTABLE_END
|
|
|
|
} // namespace Coll0
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
RefCollection::RefCollection() : RefObject(0) {
|
|
|
|
vtable = PXT_VTABLE_TO_INT(&Coll0::RefCollection_vtable);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefCollection::destroy(RefCollection *t) {
|
|
|
|
for (unsigned i = 0; i < t->head.getLength(); i++) {
|
|
|
|
decr(t->head.get(i));
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
t->head.destroy();
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefCollection::print(RefCollection *t) {
|
|
|
|
DMESG("RefCollection %p r=%d size=%d", t, t->refcnt, t->head.getLength());
|
|
|
|
t->head.print();
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
PXT_VTABLE_CTOR(RefAction) {}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
// fields[] contain captured locals
|
|
|
|
void RefAction::destroy(RefAction *t) {
|
|
|
|
for (int i = 0; i < t->reflen; ++i) {
|
|
|
|
decr(t->fields[i]);
|
|
|
|
t->fields[i] = 0;
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefAction::print(RefAction *t) {
|
|
|
|
DMESG("RefAction %p r=%d pc=%X size=%d (%d refs)", t, t->refcnt,
|
|
|
|
(const uint8_t *)t->func - (const uint8_t *)bytecode, t->len, t->reflen);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefLocal::print(RefLocal *t) {
|
|
|
|
DMESG("RefLocal %p r=%d v=%d", t, t->refcnt, t->v);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefLocal::destroy(RefLocal *) {}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
PXT_VTABLE_CTOR(RefLocal) {
|
|
|
|
v = 0;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
PXT_VTABLE_CTOR(RefRefLocal) {
|
|
|
|
v = 0;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefRefLocal::print(RefRefLocal *t) {
|
|
|
|
DMESG("RefRefLocal %p r=%d v=%p", t, t->refcnt, (void *)t->v);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefRefLocal::destroy(RefRefLocal *t) {
|
|
|
|
decr(t->v);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
PXT_VTABLE_BEGIN(RefMap, 0, RefMapMarker)
|
|
|
|
PXT_VTABLE_END
|
|
|
|
RefMap::RefMap() : PXT_VTABLE_INIT(RefMap) {}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefMap::destroy(RefMap *t) {
|
|
|
|
for (unsigned i = 0; i < t->values.getLength(); ++i) {
|
|
|
|
decr(t->values.get(i));
|
|
|
|
t->values.set(i, 0);
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
t->keys.destroy();
|
|
|
|
t->values.destroy();
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
int RefMap::findIdx(unsigned key) {
|
|
|
|
for (unsigned i = 0; i < keys.getLength(); ++i) {
|
|
|
|
if ((uintptr_t)keys.get(i) == key)
|
|
|
|
return i;
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void RefMap::print(RefMap *t) {
|
|
|
|
DMESG("RefMap %p r=%d size=%d", t, t->refcnt, t->keys.getLength());
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
#ifdef PXT_MEMLEAK_DEBUG
|
|
|
|
std::set<TValue> allptrs;
|
|
|
|
void debugMemLeaks() {
|
|
|
|
DMESG("LIVE POINTERS:");
|
|
|
|
for (std::set<TValue>::iterator itr = allptrs.begin(); itr != allptrs.end(); itr++) {
|
|
|
|
anyPrint(*itr);
|
2016-12-08 00:44:35 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
DMESG("LIVE POINTERS END.");
|
|
|
|
dumpDmesg();
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
#else
|
2018-05-30 00:55:58 +02:00
|
|
|
void debugMemLeaks() {}
|
2016-11-30 00:06:34 +01:00
|
|
|
#endif
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void error(PXT_ERROR code, int subcode) {
|
|
|
|
DMESG("Error: %d [%d]", code, subcode);
|
|
|
|
target_panic(42);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
uint16_t *bytecode;
|
|
|
|
TValue *globals;
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
unsigned *allocate(ramint_t sz) {
|
|
|
|
unsigned *arr = new unsigned[sz];
|
|
|
|
memset(arr, 0, sz * sizeof(unsigned));
|
2016-11-30 00:06:34 +01:00
|
|
|
return arr;
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void checkStr(bool cond, const char *msg) {
|
2016-11-30 00:06:34 +01:00
|
|
|
if (!cond) {
|
2018-05-30 00:55:58 +02:00
|
|
|
while (true) {
|
|
|
|
// uBit.display.scroll(msg, 100);
|
|
|
|
// uBit.sleep(100);
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
int templateHash() {
|
|
|
|
return ((int *)bytecode)[4];
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
int programHash() {
|
|
|
|
return ((int *)bytecode)[6];
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
int getNumGlobals() {
|
2016-11-30 00:06:34 +01:00
|
|
|
return bytecode[16];
|
2018-05-30 00:55:58 +02:00
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
#ifndef X86_64
|
|
|
|
void exec_binary(unsigned *pc) {
|
2016-11-30 00:06:34 +01:00
|
|
|
// XXX re-enable once the calibration code is fixed and [editor/embedded.ts]
|
|
|
|
// properly prepends a call to [internal_main].
|
|
|
|
// ::touch_develop::internal_main();
|
|
|
|
|
|
|
|
// unique group for radio based on source hash
|
|
|
|
// ::touch_develop::micro_bit::radioDefaultGroup = programHash();
|
2016-12-08 00:44:35 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
unsigned ver = *pc++;
|
2016-11-30 00:06:34 +01:00
|
|
|
checkStr(ver == 0x4209, ":( Bad runtime version");
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
bytecode = *((uint16_t **)pc++); // the actual bytecode is here
|
|
|
|
globals = (TValue *)allocate(getNumGlobals());
|
|
|
|
|
|
|
|
// can be any valid address, best in RAM for speed
|
|
|
|
globals[0] = (TValue)&globals;
|
2016-11-30 00:06:34 +01:00
|
|
|
|
|
|
|
// just compare the first word
|
2018-05-30 00:55:58 +02:00
|
|
|
// TODO
|
|
|
|
checkStr(((uint32_t *)bytecode)[0] == 0x923B8E70 && (unsigned)templateHash() == *pc,
|
2016-11-30 00:06:34 +01:00
|
|
|
":( Failed partial flash");
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
uintptr_t startptr = (uintptr_t)bytecode;
|
|
|
|
|
2016-11-30 00:06:34 +01:00
|
|
|
startptr += 48; // header
|
2018-05-30 00:55:58 +02:00
|
|
|
startptr |= 1; // Thumb state
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
initRuntime();
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
((unsigned (*)())startptr)();
|
|
|
|
|
|
|
|
#ifdef PXT_MEMLEAK_DEBUG
|
2016-11-30 00:06:34 +01:00
|
|
|
pxt::debugMemLeaks();
|
|
|
|
#endif
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
pxt::releaseFiber();
|
|
|
|
}
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
void start() {
|
|
|
|
exec_binary((unsigned *)functionsAndBytecode);
|
2016-12-08 00:44:35 +01:00
|
|
|
}
|
2018-05-30 00:55:58 +02:00
|
|
|
#endif
|
2016-11-30 00:06:34 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
} // namespace pxt
|