blob: e40f956543173727abde82f643825060b2816d8f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 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 */
Damien Georgeebde3c62015-01-01 18:07:43 +000026#ifndef __MICROPY_INCLUDED_PY_ASMTHUMB_H__
27#define __MICROPY_INCLUDED_PY_ASMTHUMB_H__
28
29#include "py/misc.h"
Damien George04b91472014-05-03 23:27:38 +010030
Damien George36db6bc2014-05-07 17:24:22 +010031#define ASM_THUMB_PASS_COMPUTE (1)
32#define ASM_THUMB_PASS_EMIT (2)
Damien429d7192013-10-04 19:53:11 +010033
Damien George0b610de2014-09-29 16:25:04 +010034#define ASM_THUMB_REG_R0 (0)
35#define ASM_THUMB_REG_R1 (1)
36#define ASM_THUMB_REG_R2 (2)
37#define ASM_THUMB_REG_R3 (3)
38#define ASM_THUMB_REG_R4 (4)
39#define ASM_THUMB_REG_R5 (5)
40#define ASM_THUMB_REG_R6 (6)
41#define ASM_THUMB_REG_R7 (7)
42#define ASM_THUMB_REG_R8 (8)
43#define ASM_THUMB_REG_R9 (9)
44#define ASM_THUMB_REG_R10 (10)
45#define ASM_THUMB_REG_R11 (11)
46#define ASM_THUMB_REG_R12 (12)
47#define ASM_THUMB_REG_R13 (13)
48#define ASM_THUMB_REG_R14 (14)
49#define ASM_THUMB_REG_R15 (15)
50#define ASM_THUMB_REG_LR (REG_R14)
Damien429d7192013-10-04 19:53:11 +010051
Damien George0b610de2014-09-29 16:25:04 +010052#define ASM_THUMB_CC_EQ (0x0)
53#define ASM_THUMB_CC_NE (0x1)
54#define ASM_THUMB_CC_CS (0x2)
55#define ASM_THUMB_CC_CC (0x3)
56#define ASM_THUMB_CC_MI (0x4)
57#define ASM_THUMB_CC_PL (0x5)
58#define ASM_THUMB_CC_VS (0x6)
59#define ASM_THUMB_CC_VC (0x7)
60#define ASM_THUMB_CC_HI (0x8)
61#define ASM_THUMB_CC_LS (0x9)
62#define ASM_THUMB_CC_GE (0xa)
63#define ASM_THUMB_CC_LT (0xb)
64#define ASM_THUMB_CC_GT (0xc)
65#define ASM_THUMB_CC_LE (0xd)
Damien1a6633a2013-11-03 13:58:19 +000066
Damien429d7192013-10-04 19:53:11 +010067typedef struct _asm_thumb_t asm_thumb_t;
68
Damien5bfb7592013-10-05 18:41:24 +010069asm_thumb_t *asm_thumb_new(uint max_num_labels);
Damien429d7192013-10-04 19:53:11 +010070void asm_thumb_free(asm_thumb_t *as, bool free_code);
Damien George36db6bc2014-05-07 17:24:22 +010071void asm_thumb_start_pass(asm_thumb_t *as, uint pass);
Damien429d7192013-10-04 19:53:11 +010072void asm_thumb_end_pass(asm_thumb_t *as);
Damien George99886182015-04-06 22:38:53 +010073uint asm_thumb_get_code_pos(asm_thumb_t *as);
Damien429d7192013-10-04 19:53:11 +010074uint asm_thumb_get_code_size(asm_thumb_t *as);
75void *asm_thumb_get_code(asm_thumb_t *as);
76
77void asm_thumb_entry(asm_thumb_t *as, int num_locals);
78void asm_thumb_exit(asm_thumb_t *as);
79
Damien George6f355fd2014-04-10 14:11:31 +010080void asm_thumb_label_assign(asm_thumb_t *as, uint label);
Damien429d7192013-10-04 19:53:11 +010081
Damien Georgee5f8a772014-04-21 13:33:15 +010082void asm_thumb_align(asm_thumb_t* as, uint align);
83void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val);
84
Damien429d7192013-10-04 19:53:11 +010085// argument order follows ARM, in general dest is first
Damien826005c2013-10-05 23:17:28 +010086// note there is a difference between movw and mov.w, and many others!
Damien429d7192013-10-04 19:53:11 +010087
Damien George42495392015-02-16 17:46:49 +000088#define ASM_THUMB_OP_IT (0xbf00)
Damien George851f15f2014-09-29 10:05:32 +010089#define ASM_THUMB_OP_ITE_EQ (0xbf0c)
90#define ASM_THUMB_OP_ITE_CS (0xbf2c)
91#define ASM_THUMB_OP_ITE_MI (0xbf4c)
92#define ASM_THUMB_OP_ITE_VS (0xbf6c)
93#define ASM_THUMB_OP_ITE_HI (0xbf8c)
94#define ASM_THUMB_OP_ITE_GE (0xbfac)
95#define ASM_THUMB_OP_ITE_GT (0xbfcc)
96
Damien George90edf9e2014-04-18 16:56:54 +010097#define ASM_THUMB_OP_NOP (0xbf00)
98#define ASM_THUMB_OP_WFI (0xbf30)
99#define ASM_THUMB_OP_CPSID_I (0xb672) // cpsid i, disable irq
100#define ASM_THUMB_OP_CPSIE_I (0xb662) // cpsie i, enable irq
101
102void asm_thumb_op16(asm_thumb_t *as, uint op);
103void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2);
104
Damien George42495392015-02-16 17:46:49 +0000105static inline void asm_thumb_it_cc(asm_thumb_t *as, uint cc, uint mask)
106 { asm_thumb_op16(as, ASM_THUMB_OP_IT | (cc << 4) | mask); }
107
Damien George87210872014-04-13 00:30:32 +0100108// FORMAT 2: add/subtract
109
110#define ASM_THUMB_FORMAT_2_ADD (0x1800)
111#define ASM_THUMB_FORMAT_2_SUB (0x1a00)
112#define ASM_THUMB_FORMAT_2_REG_OPERAND (0x0000)
113#define ASM_THUMB_FORMAT_2_IMM_OPERAND (0x0400)
114
Damien Georgee41b21c2015-02-24 16:32:52 +0000115#define ASM_THUMB_FORMAT_2_ENCODE(op, rlo_dest, rlo_src, src_b) \
116 ((op) | ((src_b) << 6) | ((rlo_src) << 3) | (rlo_dest))
117
118static inline void asm_thumb_format_2(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src, int src_b) {
119 assert(rlo_dest < ASM_THUMB_REG_R8);
120 assert(rlo_src < ASM_THUMB_REG_R8);
121 asm_thumb_op16(as, ASM_THUMB_FORMAT_2_ENCODE(op, rlo_dest, rlo_src, src_b));
122}
Damien George87210872014-04-13 00:30:32 +0100123
124static inline void asm_thumb_add_rlo_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, uint rlo_src_b)
125 { asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_ADD | ASM_THUMB_FORMAT_2_REG_OPERAND, rlo_dest, rlo_src_a, rlo_src_b); }
126static inline void asm_thumb_add_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, int i3_src)
127 { asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_ADD | ASM_THUMB_FORMAT_2_IMM_OPERAND, rlo_dest, rlo_src_a, i3_src); }
128static inline void asm_thumb_sub_rlo_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, uint rlo_src_b)
129 { asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_SUB | ASM_THUMB_FORMAT_2_REG_OPERAND, rlo_dest, rlo_src_a, rlo_src_b); }
130static inline void asm_thumb_sub_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, int i3_src)
131 { asm_thumb_format_2(as, ASM_THUMB_FORMAT_2_SUB | ASM_THUMB_FORMAT_2_IMM_OPERAND, rlo_dest, rlo_src_a, i3_src); }
132
133// FORMAT 3: move/compare/add/subtract immediate
134// These instructions all do zero extension of the i8 value
135
136#define ASM_THUMB_FORMAT_3_MOV (0x2000)
137#define ASM_THUMB_FORMAT_3_CMP (0x2800)
138#define ASM_THUMB_FORMAT_3_ADD (0x3000)
139#define ASM_THUMB_FORMAT_3_SUB (0x3800)
140
Damien Georgee41b21c2015-02-24 16:32:52 +0000141#define ASM_THUMB_FORMAT_3_ENCODE(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
142
143static inline void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
144 assert(rlo < ASM_THUMB_REG_R8);
145 asm_thumb_op16(as, ASM_THUMB_FORMAT_3_ENCODE(op, rlo, i8));
146}
Damien George87210872014-04-13 00:30:32 +0100147
148static inline void asm_thumb_mov_rlo_i8(asm_thumb_t *as, uint rlo, int i8) { asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_MOV, rlo, i8); }
149static inline void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8) { asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_CMP, rlo, i8); }
150static inline void asm_thumb_add_rlo_i8(asm_thumb_t *as, uint rlo, int i8) { asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_ADD, rlo, i8); }
151static inline void asm_thumb_sub_rlo_i8(asm_thumb_t *as, uint rlo, int i8) { asm_thumb_format_3(as, ASM_THUMB_FORMAT_3_SUB, rlo, i8); }
152
153// FORMAT 4: ALU operations
154
155#define ASM_THUMB_FORMAT_4_AND (0x4000)
156#define ASM_THUMB_FORMAT_4_EOR (0x4040)
157#define ASM_THUMB_FORMAT_4_LSL (0x4080)
158#define ASM_THUMB_FORMAT_4_LSR (0x40c0)
159#define ASM_THUMB_FORMAT_4_ASR (0x4100)
160#define ASM_THUMB_FORMAT_4_ADC (0x4140)
161#define ASM_THUMB_FORMAT_4_SBC (0x4180)
162#define ASM_THUMB_FORMAT_4_ROR (0x41c0)
163#define ASM_THUMB_FORMAT_4_TST (0x4200)
164#define ASM_THUMB_FORMAT_4_NEG (0x4240)
165#define ASM_THUMB_FORMAT_4_CMP (0x4280)
166#define ASM_THUMB_FORMAT_4_CMN (0x42c0)
167#define ASM_THUMB_FORMAT_4_ORR (0x4300)
168#define ASM_THUMB_FORMAT_4_MUL (0x4340)
169#define ASM_THUMB_FORMAT_4_BIC (0x4380)
170#define ASM_THUMB_FORMAT_4_MVN (0x43c0)
171
172void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src);
173
174static inline void asm_thumb_cmp_rlo_rlo(asm_thumb_t *as, uint rlo_dest, uint rlo_src) { asm_thumb_format_4(as, ASM_THUMB_FORMAT_4_CMP, rlo_dest, rlo_src); }
175
176// FORMAT 9: load/store with immediate offset
177// For word transfers the offset must be aligned, and >>2
178
179// FORMAT 10: load/store halfword
180// The offset must be aligned, and >>1
181// The load is zero extended into the register
182
183#define ASM_THUMB_FORMAT_9_STR (0x6000)
184#define ASM_THUMB_FORMAT_9_LDR (0x6800)
185#define ASM_THUMB_FORMAT_9_WORD_TRANSFER (0x0000)
186#define ASM_THUMB_FORMAT_9_BYTE_TRANSFER (0x1000)
187
188#define ASM_THUMB_FORMAT_10_STRH (0x8000)
189#define ASM_THUMB_FORMAT_10_LDRH (0x8800)
190
Damien Georgee41b21c2015-02-24 16:32:52 +0000191#define ASM_THUMB_FORMAT_9_10_ENCODE(op, rlo_dest, rlo_base, offset) \
192 ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
193
194static inline void asm_thumb_format_9_10(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_base, uint offset)
195 { asm_thumb_op16(as, ASM_THUMB_FORMAT_9_10_ENCODE(op, rlo_dest, rlo_base, offset)); }
Damien George87210872014-04-13 00:30:32 +0100196
197static inline void asm_thumb_str_rlo_rlo_i5(asm_thumb_t *as, uint rlo_src, uint rlo_base, uint word_offset)
198 { asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_WORD_TRANSFER, rlo_src, rlo_base, word_offset); }
199static inline void asm_thumb_strb_rlo_rlo_i5(asm_thumb_t *as, uint rlo_src, uint rlo_base, uint byte_offset)
200 { asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_STR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER, rlo_src, rlo_base, byte_offset); }
Damien George851f15f2014-09-29 10:05:32 +0100201static inline void asm_thumb_strh_rlo_rlo_i5(asm_thumb_t *as, uint rlo_src, uint rlo_base, uint byte_offset)
202 { asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_10_STRH, rlo_src, rlo_base, byte_offset); }
Damien George87210872014-04-13 00:30:32 +0100203static inline void asm_thumb_ldr_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_base, uint word_offset)
204 { asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_WORD_TRANSFER, rlo_dest, rlo_base, word_offset); }
205static inline void asm_thumb_ldrb_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_base, uint byte_offset)
206 { asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_BYTE_TRANSFER , rlo_dest, rlo_base, byte_offset); }
Damien George851f15f2014-09-29 10:05:32 +0100207static inline void asm_thumb_ldrh_rlo_rlo_i5(asm_thumb_t *as, uint rlo_dest, uint rlo_base, uint byte_offset)
208 { asm_thumb_format_9_10(as, ASM_THUMB_FORMAT_10_LDRH, rlo_dest, rlo_base, byte_offset); }
Damien George87210872014-04-13 00:30:32 +0100209
210// TODO convert these to above format style
211
Damien Georgee41b21c2015-02-24 16:32:52 +0000212#define ASM_THUMB_OP_MOVW (0xf240)
213#define ASM_THUMB_OP_MOVT (0xf2c0)
214
Damien George87210872014-04-13 00:30:32 +0100215void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src);
Damien Georgee41b21c2015-02-24 16:32:52 +0000216void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src);
217
Damien George53457432015-02-25 15:45:55 +0000218// these return true if the destination is in range, false otherwise
219bool asm_thumb_b_n_label(asm_thumb_t *as, uint label);
Damien George9f142f02015-03-02 14:29:52 +0000220bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide);
Damien George53457432015-02-25 15:45:55 +0000221bool asm_thumb_bl_label(asm_thumb_t *as, uint label);
Damien429d7192013-10-04 19:53:11 +0100222
Damien George40f3c022014-07-03 13:25:24 +0100223void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32_src); // convenience
Damien826005c2013-10-05 23:17:28 +0100224void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32_src); // convenience
Damien George36db6bc2014-05-07 17:24:22 +0100225void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32); // convenience
Damien826005c2013-10-05 23:17:28 +0100226void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num_dest, uint rlo_src); // convenience
227void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
Damien9b9e9962013-11-03 14:25:43 +0000228void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
Damien429d7192013-10-04 19:53:11 +0100229
Damien George53457432015-02-25 15:45:55 +0000230void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience: picks narrow or wide branch
Damien George6f355fd2014-04-10 14:11:31 +0100231void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label); // convenience: picks narrow or wide branch
Damien George53457432015-02-25 15:45:55 +0000232void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp); // convenience
Damien Georgeebde3c62015-01-01 18:07:43 +0000233
234#endif // __MICROPY_INCLUDED_PY_ASMTHUMB_H__