Skip to content

Towards support full range of small ints in bytecode #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions py/emitbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
}

STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
byte buf[10], *p = buf + sizeof(buf) - 1;
// We encode in little-ending order, but store in big-endian, to help decoding
while (num >= 0x80) {
*p-- = num & 0x7f;
num >>= 7;
}
// High bit == 0
*p = (byte)num;
//printf("Encoded %x using %d bytes\n", num, buf + sizeof(buf) - p);
byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
while (p != buf + sizeof(buf) - 1) {
*c++ = *p++ | 0x80;
}
*c = *p;

#if 0
if (num <= 127) { // fits in 0x7f
// fit argument in single byte
byte* c = emit_get_cur_to_write_byte_code(emit, 1);
Expand All @@ -121,6 +137,7 @@ STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
// larger numbers not implemented/supported
assert(0);
}
#endif
}

// integers (for small ints) are stored as 24 bits, in excess
Expand Down
25 changes: 23 additions & 2 deletions py/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,31 @@ typedef enum {
UNWIND_JUMP,
} mp_unwind_reason_t;

#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
#if 0
uint decode_uint() {
uint val = 0;
do {
val = (val << 7) + (*ip & 0x7f);
} while ((*ip++ & 0x80) != 0);
}
#endif

//#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
#define DECODE_UINT { \
unum = 0; \
do { \
unum = (unum << 7) + (*ip & 0x7f); \
} while ((*ip++ & 0x80) != 0); \
}
#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
#define DECODE_QSTR do { qst = *ip++; if (qst > 127) { qst = ((qst & 0x3f) << 8) | (*ip++); } } while (0)
//#define DECODE_QSTR do { qst = *ip++; if (qst > 127) { qst = ((qst & 0x3f) << 8) | (*ip++); } } while (0)
#define DECODE_QSTR { \
qst = 0; \
do { \
qst = (qst << 7) + (*ip & 0x7f); \
} while ((*ip++ & 0x80) != 0); \
}
#define PUSH(val) *++sp = (val)
#define POP() (*sp--)
#define TOP() (*sp)
Expand Down