1276789Sdim//===-- switch.S - Implement switch* --------------------------------------===//
2276789Sdim//
3276789Sdim//                     The LLVM Compiler Infrastructure
4276789Sdim//
5276789Sdim// This file is dual licensed under the MIT and the University of Illinois Open
6276789Sdim// Source Licenses. See LICENSE.TXT for details.
7276789Sdim//
8276789Sdim//===----------------------------------------------------------------------===//
9276789Sdim
10276789Sdim#include "../assembly.h"
11276789Sdim
12276789Sdim//
13276789Sdim// When compiling switch statements in thumb mode, the compiler
14276789Sdim// can use these __switch* helper functions  The compiler emits a blx to
15276789Sdim// the __switch* function followed by a table of displacements for each
16276789Sdim// case statement.  On entry, R0 is the index into the table. The __switch*
17276789Sdim// function uses the return address in lr to find the start of the table.
18276789Sdim// The first entry in the table is the count of the entries in the table.
19276789Sdim// It then uses R0 to index into the table and get the displacement of the
20276789Sdim// address to jump to.  If R0 is greater than the size of the table, it jumps
21276789Sdim// to the last entry in the table. Each displacement in the table is actually
22276789Sdim// the distance from lr to the label, thus making the tables PIC.
23276789Sdim
24276789Sdim
25276789Sdim	.text
26276789Sdim	.syntax unified
27276789Sdim
28276789Sdim//
29276789Sdim// The table contains signed byte sized elements which are 1/2 the distance
30276789Sdim// from lr to the target label.
31276789Sdim//
32276789Sdim	.p2align 2
33276789SdimDEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch8)
34276789Sdim	ldrb    ip, [lr, #-1]           // get first byte in table
35276789Sdim	cmp     r0, ip                  // signed compare with index
36276789Sdim	ite lo
37276789Sdim	ldrsblo r0, [lr, r0]            // get indexed byte out of table
38276789Sdim	ldrsbhs r0, [lr, ip]            // if out of range, use last entry in table
39276789Sdim	add     ip, lr, r0, lsl #1      // compute label = lr + element*2
40	bx      ip                      // jump to computed label
41END_COMPILERRT_FUNCTION(__switch8)
42
43