py, emitters: Fix dummy_data size for bytecode and thumb.

Thumb uses a bit less RAM, bytecode uses a tiny bit more, to avoid
overflow of the dummy buffer in certain cases.

Addresses issue #599.
This commit is contained in:
Damien George 2014-05-10 18:07:08 +01:00
parent 7db57bf6b2
commit 9597771fe4
2 changed files with 15 additions and 8 deletions

View File

@ -46,7 +46,7 @@ struct _asm_thumb_t {
uint code_offset;
uint code_size;
byte *code_base;
byte dummy_data[8];
byte dummy_data[4];
uint max_num_labels;
int *label_offsets;
@ -113,6 +113,7 @@ void asm_thumb_end_pass(asm_thumb_t *as) {
}
// all functions must go through this one to emit bytes
// if as->pass < ASM_THUMB_PASS_EMIT, then this function only returns a buffer of 4 bytes length
STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
//printf("emit %d\n", num_bytes_to_write);
if (as->pass < ASM_THUMB_PASS_EMIT) {
@ -251,10 +252,13 @@ void asm_thumb_align(asm_thumb_t* as, uint align) {
void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
// little endian
for (uint i = 0; i < bytesize; i++) {
*c++ = val;
val >>= 8;
// only write to the buffer in the emit pass (otherwise we overflow dummy_data)
if (as->pass == ASM_THUMB_PASS_EMIT) {
// little endian
for (uint i = 0; i < bytesize; i++) {
*c++ = val;
val >>= 8;
}
}
}

View File

@ -44,6 +44,9 @@
#if !MICROPY_EMIT_CPYTHON
#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
struct _emit_t {
pass_kind_t pass;
int stack_size;
@ -62,7 +65,7 @@ struct _emit_t {
uint bytecode_offset;
uint bytecode_size;
byte *code_base; // stores both byte code and code info
byte dummy_data[8];
byte dummy_data[DUMMY_DATA_SIZE];
};
STATIC void emit_bc_rot_two(emit_t *emit);
@ -152,7 +155,7 @@ STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, uint b2) {
STATIC void emit_write_bytecode_uint(emit_t* emit, uint num) {
// We store each 7 bits in a separate byte, and that's how many bytes needed
byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
byte buf[BYTES_FOR_INT];
byte *p = buf + sizeof(buf);
// We encode in little-ending order, but store in big-endian, to help decoding
do {
@ -171,7 +174,7 @@ STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, machine_int_t nu
emit_write_bytecode_byte(emit, b1);
// We store each 7 bits in a separate byte, and that's how many bytes needed
byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
byte buf[BYTES_FOR_INT];
byte *p = buf + sizeof(buf);
// We encode in little-ending order, but store in big-endian, to help decoding
do {