Commit 5bfb7599 authored by Damien's avatar Damien
Browse files

Incorporate emit_thumb into new emit framework.

parent 6cdd3af6
CC = gcc CC = gcc
CFLAGS = -Wall -ansi -std=gnu99 -Os -DEMIT_ENABLE_CPY -DEMIT_ENABLE_X64 #-DNDEBUG CFLAGS = -Wall -ansi -std=gnu99 -Os -DEMIT_ENABLE_CPY -DEMIT_ENABLE_X64 -DEMIT_ENABLE_THUMB #-DNDEBUG
LDFLAGS = LDFLAGS =
SRC = \ SRC = \
......
...@@ -28,7 +28,7 @@ struct _asm_thumb_t { ...@@ -28,7 +28,7 @@ struct _asm_thumb_t {
uint stack_adjust; uint stack_adjust;
}; };
asm_thumb_t *asm_thumb_new() { asm_thumb_t *asm_thumb_new(uint max_num_labels) {
asm_thumb_t *as; asm_thumb_t *as;
as = m_new(asm_thumb_t, 1); as = m_new(asm_thumb_t, 1);
...@@ -36,7 +36,8 @@ asm_thumb_t *asm_thumb_new() { ...@@ -36,7 +36,8 @@ asm_thumb_t *asm_thumb_new() {
as->code_offset = 0; as->code_offset = 0;
as->code_size = 0; as->code_size = 0;
as->code_base = NULL; as->code_base = NULL;
as->label_offsets = NULL; as->max_num_labels = max_num_labels;
as->label_offsets = m_new(int, max_num_labels);
as->num_locals = 0; as->num_locals = 0;
return as; return as;
...@@ -65,23 +66,13 @@ void asm_thumb_start_pass(asm_thumb_t *as, int pass) { ...@@ -65,23 +66,13 @@ void asm_thumb_start_pass(asm_thumb_t *as, int pass) {
as->pass = pass; as->pass = pass;
as->code_offset = 0; as->code_offset = 0;
as->next_label = 1; as->next_label = 1;
if (pass == ASM_THUMB_PASS_1) { if (pass == ASM_THUMB_PASS_2) {
as->max_num_labels = 0; memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
} else {
if (pass == ASM_THUMB_PASS_2) {
memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
}
} }
} }
void asm_thumb_end_pass(asm_thumb_t *as) { void asm_thumb_end_pass(asm_thumb_t *as) {
if (as->pass == ASM_THUMB_PASS_1) { if (as->pass == ASM_THUMB_PASS_2) {
// calculate number of labels need
if (as->next_label > as->max_num_labels) {
as->max_num_labels = as->next_label;
}
as->label_offsets = m_new(int, as->max_num_labels);
} else if (as->pass == ASM_THUMB_PASS_2) {
// calculate size of code in bytes // calculate size of code in bytes
as->code_size = as->code_offset; as->code_size = as->code_offset;
as->code_base = m_new(byte, as->code_size); as->code_base = m_new(byte, as->code_size);
...@@ -226,20 +217,23 @@ int asm_thumb_label_new(asm_thumb_t *as) { ...@@ -226,20 +217,23 @@ int asm_thumb_label_new(asm_thumb_t *as) {
} }
void asm_thumb_label_assign(asm_thumb_t *as, int label) { void asm_thumb_label_assign(asm_thumb_t *as, int label) {
if (as->pass > ASM_THUMB_PASS_1) { assert(label < as->max_num_labels);
assert(label < as->max_num_labels); if (as->pass == ASM_THUMB_PASS_2) {
if (as->pass == ASM_THUMB_PASS_2) { // assign label offset
// assign label offset assert(as->label_offsets[label] == -1);
assert(as->label_offsets[label] == -1); as->label_offsets[label] = as->code_offset;
as->label_offsets[label] = as->code_offset; } else if (as->pass == ASM_THUMB_PASS_3) {
} else if (as->pass == ASM_THUMB_PASS_3) { // ensure label offset has not changed from PASS_2 to PASS_3
// ensure label offset has not changed from PASS_2 to PASS_3 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
//printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset); assert(as->label_offsets[label] == as->code_offset);
assert(as->label_offsets[label] == as->code_offset);
}
} }
} }
static int get_label_dest(asm_thumb_t *as, int label) {
assert(label < as->max_num_labels);
return as->label_offsets[label];
}
// the i8 value will be zero extended into the r32 register! // the i8 value will be zero extended into the r32 register!
void asm_thumb_mov_reg_i8(asm_thumb_t *as, uint rlo_dest, int i8) { void asm_thumb_mov_reg_i8(asm_thumb_t *as, uint rlo_dest, int i8) {
assert(rlo_dest < REG_R8); assert(rlo_dest < REG_R8);
...@@ -339,23 +333,21 @@ void asm_thumb_ite_ge(asm_thumb_t *as) { ...@@ -339,23 +333,21 @@ void asm_thumb_ite_ge(asm_thumb_t *as) {
#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff)) #define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
void asm_thumb_b_label(asm_thumb_t *as, int label) { void asm_thumb_b_label(asm_thumb_t *as, int label) {
if (as->pass > ASM_THUMB_PASS_1) { int dest = get_label_dest(as, label);
int dest = as->label_offsets[label]; int rel = dest - as->code_offset;
int rel = dest - as->code_offset; rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction if (dest >= 0 && rel <= -4) {
if (dest >= 0 && rel <= -4) { // is a backwards jump, so we know the size of the jump on the first pass
// is a backwards jump, so we know the size of the jump on the first pass // calculate rel assuming 12 bit relative jump
// calculate rel assuming 12 bit relative jump if (SIGNED_FIT12(rel)) {
if (SIGNED_FIT12(rel)) { asm_thumb_write_op16(as, OP_B(rel));
asm_thumb_write_op16(as, OP_B(rel));
} else {
goto large_jump;
}
} else { } else {
// is a forwards jump, so need to assume it's large goto large_jump;
large_jump:
asm_thumb_write_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
} }
} else {
// is a forwards jump, so need to assume it's large
large_jump:
asm_thumb_write_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
} }
} }
...@@ -372,23 +364,21 @@ void asm_thumb_cmp_reg_bz_label(asm_thumb_t *as, uint rlo, int label) { ...@@ -372,23 +364,21 @@ void asm_thumb_cmp_reg_bz_label(asm_thumb_t *as, uint rlo, int label) {
asm_thumb_write_op16(as, OP_CMP_REG_IMM(rlo, 0)); asm_thumb_write_op16(as, OP_CMP_REG_IMM(rlo, 0));
// branch if equal // branch if equal
if (as->pass > ASM_THUMB_PASS_1) { int dest = get_label_dest(as, label);
int dest = as->label_offsets[label]; int rel = dest - as->code_offset;
int rel = dest - as->code_offset; rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction if (dest >= 0 && rel <= -4) {
if (dest >= 0 && rel <= -4) { // is a backwards jump, so we know the size of the jump on the first pass
// is a backwards jump, so we know the size of the jump on the first pass // calculate rel assuming 12 bit relative jump
// calculate rel assuming 12 bit relative jump if (SIGNED_FIT9(rel)) {
if (SIGNED_FIT9(rel)) { asm_thumb_write_op16(as, OP_BEQ(rel));
asm_thumb_write_op16(as, OP_BEQ(rel));
} else {
goto large_jump;
}
} else { } else {
// is a forwards jump, so need to assume it's large goto large_jump;
large_jump:
asm_thumb_write_op32(as, OP_BEQW_HI(rel), OP_BEQW_LO(rel));
} }
} else {
// is a forwards jump, so need to assume it's large
large_jump:
asm_thumb_write_op32(as, OP_BEQW_HI(rel), OP_BEQW_LO(rel));
} }
} }
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
typedef struct _asm_thumb_t asm_thumb_t; typedef struct _asm_thumb_t asm_thumb_t;
asm_thumb_t *asm_thumb_new(); asm_thumb_t *asm_thumb_new(uint max_num_labels);
void asm_thumb_free(asm_thumb_t *as, bool free_code); void asm_thumb_free(asm_thumb_t *as, bool free_code);
void asm_thumb_start_pass(asm_thumb_t *as, int pass); void asm_thumb_start_pass(asm_thumb_t *as, int pass);
void asm_thumb_end_pass(asm_thumb_t *as); void asm_thumb_end_pass(asm_thumb_t *as);
......
...@@ -30,6 +30,7 @@ typedef enum { ...@@ -30,6 +30,7 @@ typedef enum {
#define EMIT_OPT_NONE (0) #define EMIT_OPT_NONE (0)
#define EMIT_OPT_BYTE_CODE (1) #define EMIT_OPT_BYTE_CODE (1)
#define EMIT_OPT_NATIVE_PYTHON (2) #define EMIT_OPT_NATIVE_PYTHON (2)
#define EMIT_OPT_ASM_THUMB (3)
typedef struct _compiler_t { typedef struct _compiler_t {
qstr qstr___class__; qstr qstr___class__;
...@@ -41,6 +42,7 @@ typedef struct _compiler_t { ...@@ -41,6 +42,7 @@ typedef struct _compiler_t {
qstr qstr_assertion_error; qstr qstr_assertion_error;
qstr qstr_micropython; qstr qstr_micropython;
qstr qstr_native; qstr qstr_native;
qstr qstr_asm_thumb;
pass_kind_t pass; pass_kind_t pass;
...@@ -759,6 +761,8 @@ static bool compile_built_in_decorator(compiler_t *comp, int name_len, py_parse_ ...@@ -759,6 +761,8 @@ static bool compile_built_in_decorator(compiler_t *comp, int name_len, py_parse_
qstr attr = PY_PARSE_NODE_LEAF_ARG(name_nodes[1]); qstr attr = PY_PARSE_NODE_LEAF_ARG(name_nodes[1]);
if (attr == comp->qstr_native) { if (attr == comp->qstr_native) {
*emit_options = EMIT_OPT_NATIVE_PYTHON; *emit_options = EMIT_OPT_NATIVE_PYTHON;
} else if (attr == comp->qstr_asm_thumb) {
*emit_options = EMIT_OPT_ASM_THUMB;
} else { } else {
printf("SyntaxError: invalid micropython decorator\n"); printf("SyntaxError: invalid micropython decorator\n");
} }
...@@ -2551,6 +2555,7 @@ void py_compile(py_parse_node_t pn) { ...@@ -2551,6 +2555,7 @@ void py_compile(py_parse_node_t pn) {
comp->qstr_assertion_error = qstr_from_str_static("AssertionError"); comp->qstr_assertion_error = qstr_from_str_static("AssertionError");
comp->qstr_micropython = qstr_from_str_static("micropython"); comp->qstr_micropython = qstr_from_str_static("micropython");
comp->qstr_native = qstr_from_str_static("native"); comp->qstr_native = qstr_from_str_static("native");
comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb");
comp->max_num_labels = 0; comp->max_num_labels = 0;
comp->break_label = 0; comp->break_label = 0;
...@@ -2588,6 +2593,9 @@ void py_compile(py_parse_node_t pn) { ...@@ -2588,6 +2593,9 @@ void py_compile(py_parse_node_t pn) {
comp->emit_method_table = &emit_x64_method_table; comp->emit_method_table = &emit_x64_method_table;
break; break;
//case EMIT_OPT_ASM_THUMB:
//if (em
default: default:
if (emit_bc == NULL) { if (emit_bc == NULL) {
emit_bc = emit_bc_new(comp->max_num_labels); emit_bc = emit_bc_new(comp->max_num_labels);
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment