1214152Sed//===-- switch.S - Implement switch* --------------------------------------===//
2214152Sed//
3214152Sed//                     The LLVM Compiler Infrastructure
4214152Sed//
5222656Sed// This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed// Source Licenses. See LICENSE.TXT for details.
7214152Sed//
8214152Sed//===----------------------------------------------------------------------===//
9214152Sed
10214152Sed#include "../assembly.h"
11214152Sed
12214152Sed//
13214152Sed// When compiling switch statements in thumb mode, the compiler
14214152Sed// can use these __switch* helper functions  The compiler emits a blx to
15214152Sed// the __switch* function followed by a table of displacements for each
16214152Sed// case statement.  On entry, R0 is the index into the table. The __switch*
17214152Sed// function uses the return address in lr to find the start of the table.
18214152Sed// The first entry in the table is the count of the entries in the table.
19214152Sed// It then uses R0 to index into the table and get the displacement of the
20214152Sed// address to jump to.  If R0 is greater than the size of the table, it jumps
21214152Sed// to the last entry in the table. Each displacement in the table is actually
22214152Sed// the distance from lr to the label, thus making the tables PIC.
23214152Sed
24214152Sed
25214152Sed	.text
26214152Sed	.syntax unified
27214152Sed
28214152Sed//
29214152Sed// The table contains signed 2-byte sized elements which are 1/2 the distance
30214152Sed// from lr to the target label.
31214152Sed//
32214152Sed	.align 2
33214152SedDEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch16)
34214152Sed	ldrh    ip, [lr, #-1]           // get first 16-bit word in table
35214152Sed	cmp     r0, ip                  // compare with index
36214152Sed	add     r0, lr, r0, lsl #1      // compute address of element in table
37214152Sed	ldrshcc r0, [r0, #1]            // load 16-bit element if r0 is in range
38214152Sed	add     ip, lr, ip, lsl #1      // compute address of last element in table
39214152Sed	ldrshhs r0, [ip, #1]            // load 16-bit element if r0 out of range
40214152Sed	add     ip, lr, r0, lsl #1      // compute label = lr + element*2
41214152Sed	bx      ip                      // jump to computed label
42214152Sed
43214152Sed	// tell linker it can break up file at label boundaries
44214152Sed	.subsections_via_symbols
45