James Zern | b694184 | 2014-02-28 11:56:14 -0800 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 2 | ## |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 3 | ## Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 4 | ## |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 5 | ## This source code is subject to the terms of the BSD 2 Clause License and |
| 6 | ## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 7 | ## was not distributed with this source code in the LICENSE file, you can |
| 8 | ## obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 9 | ## Media Patent License 1.0 was not distributed with this source code in the |
| 10 | ## PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 11 | ## |
| 12 | |
| 13 | package thumb; |
| 14 | |
Martin Storsjo | 6b9a7b3 | 2013-05-15 17:09:22 +0300 | [diff] [blame] | 15 | sub FixThumbInstructions($$) |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 16 | { |
Martin Storsjo | 6b9a7b3 | 2013-05-15 17:09:22 +0300 | [diff] [blame] | 17 | my $short_branches = $_[1]; |
| 18 | my $branch_shift_offset = $short_branches ? 1 : 0; |
| 19 | |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 20 | # Write additions with shifts, such as "add r10, r11, lsl #8", |
| 21 | # in three operand form, "add r10, r10, r11, lsl #8". |
| 22 | s/(add\s+)(r\d+),\s*(r\d+),\s*(lsl #\d+)/$1$2, $2, $3, $4/g; |
| 23 | |
| 24 | # Convert additions with a non-constant shift into a sequence |
| 25 | # with left shift, addition and a right shift (to restore the |
| 26 | # register to the original value). Currently the right shift |
| 27 | # isn't necessary in the code base since the values in these |
Martin Storsjo | f0339cc | 2013-11-26 10:56:43 +0200 | [diff] [blame] | 28 | # registers aren't used, but doing the shift for consistency. |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 29 | # This converts instructions such as "add r12, r12, r5, lsl r4" |
| 30 | # into the sequence "lsl r5, r4", "add r12, r12, r5", "lsr r5, r4". |
| 31 | s/^(\s*)(add)(\s+)(r\d+),\s*(r\d+),\s*(r\d+),\s*lsl (r\d+)/$1lsl$3$6, $7\n$1$2$3$4, $5, $6\n$1lsr$3$6, $7/g; |
| 32 | |
| 33 | # Convert loads with right shifts in the indexing into a |
| 34 | # sequence of an add, load and sub. This converts |
| 35 | # "ldrb r4, [r9, lr, asr #1]" into "add r9, r9, lr, asr #1", |
| 36 | # "ldrb r9, [r9]", "sub r9, r9, lr, asr #1". |
| 37 | s/^(\s*)(ldrb)(\s+)(r\d+),\s*\[(\w+),\s*(\w+),\s*(asr #\d+)\]/$1add $3$5, $5, $6, $7\n$1$2$3$4, [$5]\n$1sub $3$5, $5, $6, $7/g; |
| 38 | |
| 39 | # Convert register indexing with writeback into a separate add |
| 40 | # instruction. This converts "ldrb r12, [r1, r2]!" into |
| 41 | # "ldrb r12, [r1, r2]", "add r1, r1, r2". |
| 42 | s/^(\s*)(ldrb)(\s+)(r\d+),\s*\[(\w+),\s*(\w+)\]!/$1$2$3$4, [$5, $6]\n$1add $3$5, $6/g; |
| 43 | |
| 44 | # Convert negative register indexing into separate sub/add instructions. |
| 45 | # This converts "ldrne r4, [src, -pstep, lsl #1]" into |
| 46 | # "subne src, src, pstep, lsl #1", "ldrne r4, [src]", |
| 47 | # "addne src, src, pstep, lsl #1". In a couple of cases where |
| 48 | # this is used, it's used for two subsequent load instructions, |
| 49 | # where a hand-written version of it could merge two subsequent |
| 50 | # add and sub instructions. |
Martin Storsjo | e81a3ed | 2013-09-29 21:22:37 +0300 | [diff] [blame] | 51 | s/^(\s*)((ldr|str|pld)(ne)?)(\s+)(r\d+,\s*)?\[(\w+), -([^\]]+)\]/$1sub$4$5$7, $7, $8\n$1$2$5$6\[$7\]\n$1add$4$5$7, $7, $8/g; |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 52 | |
| 53 | # Convert register post indexing to a separate add instruction. |
| 54 | # This converts "ldrneb r9, [r0], r2" into "ldrneb r9, [r0]", |
Martin Storsjo | 317e07e | 2014-03-17 12:21:31 +0200 | [diff] [blame] | 55 | # "addne r0, r0, r2". |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 56 | s/^(\s*)((ldr|str)(ne)?[bhd]?)(\s+)(\w+),(\s*\w+,)?\s*\[(\w+)\],\s*(\w+)/$1$2$5$6,$7 [$8]\n$1add$4$5$8, $8, $9/g; |
| 57 | |
| 58 | # Convert a conditional addition to the pc register into a series of |
| 59 | # instructions. This converts "addlt pc, pc, r3, lsl #2" into |
Martin Storsjo | 5d2f6ba | 2013-05-15 15:42:18 +0300 | [diff] [blame] | 60 | # "itttt lt", "movlt.n r12, pc", "addlt.w r12, #12", |
| 61 | # "addlt.w r12, r12, r3, lsl #2", "movlt.n pc, r12". |
| 62 | # This assumes that r12 is free at this point. |
Martin Storsjo | 6b9a7b3 | 2013-05-15 17:09:22 +0300 | [diff] [blame] | 63 | s/^(\s*)addlt(\s+)pc,\s*pc,\s*(\w+),\s*lsl\s*#(\d+)/$1itttt$2lt\n$1movlt.n$2r12, pc\n$1addlt.w$2r12, #12\n$1addlt.w$2r12, r12, $3, lsl #($4-$branch_shift_offset)\n$1movlt.n$2pc, r12/g; |
Martin Storsjo | 5f76080 | 2013-05-15 16:08:19 +0300 | [diff] [blame] | 64 | |
| 65 | # Convert "mov pc, lr" into "bx lr", since the former only works |
| 66 | # for switching from arm to thumb (and only in armv7), but not |
| 67 | # from thumb to arm. |
| 68 | s/mov(\s*)pc\s*,\s*lr/bx$1lr/g; |
| 69 | } |
| 70 | |
| 71 | 1; |