blob: 8248694e94af6d7e1b48b68ebd5f628d00ac0628 [file] [log] [blame]
James Zernb6941842014-02-28 11:56:14 -08001#!/usr/bin/env perl
Martin Storsjo5f760802013-05-15 16:08:19 +03002##
Yaowu Xu9c01aa12016-09-01 14:32:49 -07003## Copyright (c) 2016, Alliance for Open Media. All rights reserved
Martin Storsjo5f760802013-05-15 16:08:19 +03004##
Yaowu Xu9c01aa12016-09-01 14:32:49 -07005## 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 Storsjo5f760802013-05-15 16:08:19 +030011##
12
13package thumb;
14
Martin Storsjo6b9a7b32013-05-15 17:09:22 +030015sub FixThumbInstructions($$)
Martin Storsjo5f760802013-05-15 16:08:19 +030016{
Martin Storsjo6b9a7b32013-05-15 17:09:22 +030017 my $short_branches = $_[1];
18 my $branch_shift_offset = $short_branches ? 1 : 0;
19
Martin Storsjo5f760802013-05-15 16:08:19 +030020 # 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 Storsjof0339cc2013-11-26 10:56:43 +020028 # registers aren't used, but doing the shift for consistency.
Martin Storsjo5f760802013-05-15 16:08:19 +030029 # 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 Storsjoe81a3ed2013-09-29 21:22:37 +030051 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 Storsjo5f760802013-05-15 16:08:19 +030052
53 # Convert register post indexing to a separate add instruction.
54 # This converts "ldrneb r9, [r0], r2" into "ldrneb r9, [r0]",
Martin Storsjo317e07e2014-03-17 12:21:31 +020055 # "addne r0, r0, r2".
Martin Storsjo5f760802013-05-15 16:08:19 +030056 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 Storsjo5d2f6ba2013-05-15 15:42:18 +030060 # "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 Storsjo6b9a7b32013-05-15 17:09:22 +030063 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 Storsjo5f760802013-05-15 16:08:19 +030064
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
711;