Skip to content

Commit 6125995

Browse files
committed
py: Factor out common code from assemblers into asmbase.[ch].
All assemblers should "derive" from mp_asm_base_t.
1 parent 21e1703 commit 6125995

13 files changed

Lines changed: 262 additions & 565 deletions

File tree

py/asmarm.c

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,6 @@
3838

3939
#define SIGNED_FIT24(x) (((x) & 0xff800000) == 0) || (((x) & 0xff000000) == 0xff000000)
4040

41-
struct _asm_arm_t {
42-
uint pass;
43-
mp_uint_t code_offset;
44-
mp_uint_t code_size;
45-
byte *code_base;
46-
byte dummy_data[4];
47-
48-
mp_uint_t max_num_labels;
49-
mp_uint_t *label_offsets;
50-
uint push_reglist;
51-
uint stack_adjust;
52-
};
53-
54-
asm_arm_t *asm_arm_new(uint max_num_labels) {
55-
asm_arm_t *as;
56-
57-
as = m_new0(asm_arm_t, 1);
58-
as->max_num_labels = max_num_labels;
59-
as->label_offsets = m_new(mp_uint_t, max_num_labels);
60-
61-
return as;
62-
}
63-
64-
void asm_arm_free(asm_arm_t *as, bool free_code) {
65-
if (free_code) {
66-
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
67-
}
68-
m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
69-
m_del_obj(asm_arm_t, as);
70-
}
71-
72-
void asm_arm_start_pass(asm_arm_t *as, uint pass) {
73-
if (pass == ASM_ARM_PASS_COMPUTE) {
74-
memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
75-
} else if (pass == ASM_ARM_PASS_EMIT) {
76-
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
77-
if (as->code_base == NULL) {
78-
assert(0);
79-
}
80-
}
81-
as->pass = pass;
82-
as->code_offset = 0;
83-
}
84-
8541
void asm_arm_end_pass(asm_arm_t *as) {
8642
if (as->pass == ASM_ARM_PASS_EMIT) {
8743
#ifdef __arm__
@@ -97,32 +53,6 @@ void asm_arm_end_pass(asm_arm_t *as) {
9753
}
9854
}
9955

100-
// all functions must go through this one to emit bytes
101-
// if as->pass < ASM_ARM_PASS_EMIT, then this function only returns a buffer of 4 bytes length
102-
STATIC byte *asm_arm_get_cur_to_write_bytes(asm_arm_t *as, int num_bytes_to_write) {
103-
if (as->pass < ASM_ARM_PASS_EMIT) {
104-
as->code_offset += num_bytes_to_write;
105-
return as->dummy_data;
106-
} else {
107-
assert(as->code_offset + num_bytes_to_write <= as->code_size);
108-
byte *c = as->code_base + as->code_offset;
109-
as->code_offset += num_bytes_to_write;
110-
return c;
111-
}
112-
}
113-
114-
uint asm_arm_get_code_pos(asm_arm_t *as) {
115-
return as->code_offset;
116-
}
117-
118-
uint asm_arm_get_code_size(asm_arm_t *as) {
119-
return as->code_size;
120-
}
121-
122-
void *asm_arm_get_code(asm_arm_t *as) {
123-
return as->code_base;
124-
}
125-
12656
// Insert word into instruction flow
12757
STATIC void emit(asm_arm_t *as, uint op) {
12858
*(uint*)asm_arm_get_cur_to_write_bytes(as, 4) = op;
@@ -263,35 +193,6 @@ void asm_arm_pop(asm_arm_t *as, uint reglist) {
263193
emit_al(as, asm_arm_op_pop(reglist));
264194
}
265195

266-
void asm_arm_label_assign(asm_arm_t *as, uint label) {
267-
assert(label < as->max_num_labels);
268-
if (as->pass < ASM_ARM_PASS_EMIT) {
269-
// assign label offset
270-
assert(as->label_offsets[label] == -1);
271-
as->label_offsets[label] = as->code_offset;
272-
} else {
273-
// ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
274-
assert(as->label_offsets[label] == as->code_offset);
275-
}
276-
}
277-
278-
void asm_arm_align(asm_arm_t* as, uint align) {
279-
// TODO fill unused data with NOPs?
280-
as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
281-
}
282-
283-
void asm_arm_data(asm_arm_t* as, uint bytesize, uint val) {
284-
byte *c = asm_arm_get_cur_to_write_bytes(as, bytesize);
285-
// only write to the buffer in the emit pass (otherwise we overflow dummy_data)
286-
if (as->pass == ASM_ARM_PASS_EMIT) {
287-
// little endian
288-
for (uint i = 0; i < bytesize; i++) {
289-
*c++ = val;
290-
val >>= 8;
291-
}
292-
}
293-
}
294-
295196
void asm_arm_mov_reg_reg(asm_arm_t *as, uint reg_dest, uint reg_src) {
296197
emit_al(as, asm_arm_op_mov_reg(reg_dest, reg_src));
297198
}

py/asmarm.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
#define __MICROPY_INCLUDED_PY_ASMARM_H__
2929

3030
#include "py/misc.h"
31-
32-
#define ASM_ARM_PASS_COMPUTE (1)
33-
#define ASM_ARM_PASS_EMIT (2)
31+
#include "py/asmbase.h"
3432

3533
#define ASM_ARM_REG_R0 (0)
3634
#define ASM_ARM_REG_R1 (1)
@@ -68,22 +66,16 @@
6866
#define ASM_ARM_CC_LE (0xd << 28)
6967
#define ASM_ARM_CC_AL (0xe << 28)
7068

71-
typedef struct _asm_arm_t asm_arm_t;
69+
typedef struct _asm_arm_t {
70+
mp_asm_base_t base;
71+
uint push_reglist;
72+
uint stack_adjust;
73+
} asm_arm_t;
7274

73-
asm_arm_t *asm_arm_new(uint max_num_labels);
74-
void asm_arm_free(asm_arm_t *as, bool free_code);
75-
void asm_arm_start_pass(asm_arm_t *as, uint pass);
7675
void asm_arm_end_pass(asm_arm_t *as);
77-
uint asm_arm_get_code_pos(asm_arm_t *as);
78-
uint asm_arm_get_code_size(asm_arm_t *as);
79-
void *asm_arm_get_code(asm_arm_t *as);
8076

8177
void asm_arm_entry(asm_arm_t *as, int num_locals);
8278
void asm_arm_exit(asm_arm_t *as);
83-
void asm_arm_label_assign(asm_arm_t *as, uint label);
84-
85-
void asm_arm_align(asm_arm_t* as, uint align);
86-
void asm_arm_data(asm_arm_t* as, uint bytesize, uint val);
8779

8880
void asm_arm_bkpt(asm_arm_t *as);
8981

py/asmbase.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <assert.h>
28+
#include <string.h>
29+
30+
#include "py/obj.h"
31+
#include "py/misc.h"
32+
#include "py/asmbase.h"
33+
34+
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
35+
36+
void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels) {
37+
as->max_num_labels = max_num_labels;
38+
as->label_offsets = m_new(size_t, max_num_labels);
39+
}
40+
41+
void mp_asm_base_deinit(mp_asm_base_t *as, bool free_code) {
42+
if (free_code) {
43+
MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
44+
}
45+
m_del(size_t, as->label_offsets, as->max_num_labels);
46+
}
47+
48+
void mp_asm_base_start_pass(mp_asm_base_t *as, int pass) {
49+
if (pass == MP_ASM_PASS_COMPUTE) {
50+
// reset all labels
51+
memset(as->label_offsets, -1, as->max_num_labels * sizeof(size_t));
52+
} else if (pass == MP_ASM_PASS_EMIT) {
53+
// allocating executable RAM is platform specific
54+
MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
55+
assert(as->code_base != NULL);
56+
}
57+
as->pass = pass;
58+
as->code_offset = 0;
59+
}
60+
61+
// all functions must go through this one to emit bytes
62+
// if as->pass < MP_ASM_PASS_EMIT, then this function returns dummy_data
63+
uint8_t *mp_asm_base_get_cur_to_write_bytes(mp_asm_base_t *as, size_t num_bytes_to_write) {
64+
if (as->pass < MP_ASM_PASS_EMIT) {
65+
as->code_offset += num_bytes_to_write;
66+
return as->dummy_data;
67+
} else {
68+
assert(as->code_offset + num_bytes_to_write <= as->code_size);
69+
uint8_t *c = as->code_base + as->code_offset;
70+
as->code_offset += num_bytes_to_write;
71+
return c;
72+
}
73+
}
74+
75+
void mp_asm_base_label_assign(mp_asm_base_t *as, size_t label) {
76+
assert(label < as->max_num_labels);
77+
if (as->pass < MP_ASM_PASS_EMIT) {
78+
// assign label offset
79+
assert(as->label_offsets[label] == (size_t)-1);
80+
as->label_offsets[label] = as->code_offset;
81+
} else {
82+
// ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
83+
assert(as->label_offsets[label] == as->code_offset);
84+
}
85+
}
86+
87+
// align must be a multiple of 2
88+
void mp_asm_base_align(mp_asm_base_t* as, unsigned int align) {
89+
as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
90+
}
91+
92+
// this function assumes a little endian machine
93+
void mp_asm_base_data(mp_asm_base_t* as, unsigned int bytesize, uintptr_t val) {
94+
uint8_t *c = mp_asm_base_get_cur_to_write_bytes(as, bytesize);
95+
// only write to the buffer in the emit pass (otherwise we may overflow dummy_data)
96+
if (as->pass == MP_ASM_PASS_EMIT) {
97+
for (unsigned int i = 0; i < bytesize; i++) {
98+
*c++ = val;
99+
val >>= 8;
100+
}
101+
}
102+
}
103+
104+
#endif // MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB

py/asmbase.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_PY_ASMBASE_H
27+
#define MICROPY_INCLUDED_PY_ASMBASE_H
28+
29+
#include <stdint.h>
30+
#include <stdbool.h>
31+
32+
#define MP_ASM_PASS_COMPUTE (1)
33+
#define MP_ASM_PASS_EMIT (2)
34+
35+
typedef struct _mp_asm_base_t {
36+
int pass;
37+
size_t code_offset;
38+
size_t code_size;
39+
uint8_t *code_base;
40+
41+
size_t max_num_labels;
42+
size_t *label_offsets;
43+
44+
// must be last in struct
45+
uint8_t dummy_data[4];
46+
} mp_asm_base_t;
47+
48+
void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels);
49+
void mp_asm_base_deinit(mp_asm_base_t *as, bool free_code);
50+
void mp_asm_base_start_pass(mp_asm_base_t *as, int pass);
51+
uint8_t *mp_asm_base_get_cur_to_write_bytes(mp_asm_base_t *as, size_t num_bytes_to_write);
52+
void mp_asm_base_label_assign(mp_asm_base_t *as, size_t label);
53+
void mp_asm_base_align(mp_asm_base_t* as, unsigned int align);
54+
void mp_asm_base_data(mp_asm_base_t* as, unsigned int bytesize, uintptr_t val);
55+
56+
static inline size_t mp_asm_base_get_code_pos(mp_asm_base_t *as) {
57+
return as->code_offset;
58+
}
59+
60+
static inline size_t mp_asm_base_get_code_size(mp_asm_base_t *as) {
61+
return as->code_size;
62+
}
63+
64+
static inline void *mp_asm_base_get_code(mp_asm_base_t *as) {
65+
return as->code_base;
66+
}
67+
68+
#endif // MICROPY_INCLUDED_PY_ASMBASE_H

0 commit comments

Comments
 (0)