xref: /aosp_15_r20/external/compiler-rt/lib/builtins/arm/switch8.S (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot//===-- switch.S - Implement switch* --------------------------------------===//
2*7c3d14c8STreehugger Robot//
3*7c3d14c8STreehugger Robot//                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot//
5*7c3d14c8STreehugger Robot// This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot// Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot//
8*7c3d14c8STreehugger Robot//===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot
10*7c3d14c8STreehugger Robot#include "../assembly.h"
11*7c3d14c8STreehugger Robot
12*7c3d14c8STreehugger Robot//
13*7c3d14c8STreehugger Robot// When compiling switch statements in thumb mode, the compiler
14*7c3d14c8STreehugger Robot// can use these __switch* helper functions  The compiler emits a blx to
15*7c3d14c8STreehugger Robot// the __switch* function followed by a table of displacements for each
16*7c3d14c8STreehugger Robot// case statement.  On entry, R0 is the index into the table. The __switch*
17*7c3d14c8STreehugger Robot// function uses the return address in lr to find the start of the table.
18*7c3d14c8STreehugger Robot// The first entry in the table is the count of the entries in the table.
19*7c3d14c8STreehugger Robot// It then uses R0 to index into the table and get the displacement of the
20*7c3d14c8STreehugger Robot// address to jump to.  If R0 is greater than the size of the table, it jumps
21*7c3d14c8STreehugger Robot// to the last entry in the table. Each displacement in the table is actually
22*7c3d14c8STreehugger Robot// the distance from lr to the label, thus making the tables PIC.
23*7c3d14c8STreehugger Robot
24*7c3d14c8STreehugger Robot
25*7c3d14c8STreehugger Robot	.text
26*7c3d14c8STreehugger Robot	.syntax unified
27*7c3d14c8STreehugger Robot
28*7c3d14c8STreehugger Robot//
29*7c3d14c8STreehugger Robot// The table contains signed byte sized elements which are 1/2 the distance
30*7c3d14c8STreehugger Robot// from lr to the target label.
31*7c3d14c8STreehugger Robot//
32*7c3d14c8STreehugger Robot	.p2align 2
33*7c3d14c8STreehugger RobotDEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch8)
34*7c3d14c8STreehugger Robot	ldrb    ip, [lr, #-1]           // get first byte in table
35*7c3d14c8STreehugger Robot	cmp     r0, ip                  // signed compare with index
36*7c3d14c8STreehugger Robot	ite lo
37*7c3d14c8STreehugger Robot	ldrsblo r0, [lr, r0]            // get indexed byte out of table
38*7c3d14c8STreehugger Robot	ldrsbhs r0, [lr, ip]            // if out of range, use last entry in table
39*7c3d14c8STreehugger Robot	add     ip, lr, r0, lsl #1      // compute label = lr + element*2
40*7c3d14c8STreehugger Robot	bx      ip                      // jump to computed label
41*7c3d14c8STreehugger RobotEND_COMPILERRT_FUNCTION(__switch8)
42*7c3d14c8STreehugger Robot
43*7c3d14c8STreehugger RobotNO_EXEC_STACK_DIRECTIVE
44*7c3d14c8STreehugger Robot
45