xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/arm/switch8.S (revision 30308f423af31fedfebb54e1e3833af8bd10f23b)
1156cd587Sjoerg//===-- switch.S - Implement switch* --------------------------------------===//
2156cd587Sjoerg//
3156cd587Sjoerg//                     The LLVM Compiler Infrastructure
4156cd587Sjoerg//
5156cd587Sjoerg// This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg// Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg//
8156cd587Sjoerg//===----------------------------------------------------------------------===//
9156cd587Sjoerg
10156cd587Sjoerg#include "../assembly.h"
11156cd587Sjoerg
12156cd587Sjoerg//
13156cd587Sjoerg// When compiling switch statements in thumb mode, the compiler
14156cd587Sjoerg// can use these __switch* helper functions  The compiler emits a blx to
15156cd587Sjoerg// the __switch* function followed by a table of displacements for each
16156cd587Sjoerg// case statement.  On entry, R0 is the index into the table. The __switch*
17156cd587Sjoerg// function uses the return address in lr to find the start of the table.
18156cd587Sjoerg// The first entry in the table is the count of the entries in the table.
19156cd587Sjoerg// It then uses R0 to index into the table and get the displacement of the
20156cd587Sjoerg// address to jump to.  If R0 is greater than the size of the table, it jumps
21156cd587Sjoerg// to the last entry in the table. Each displacement in the table is actually
22156cd587Sjoerg// the distance from lr to the label, thus making the tables PIC.
23156cd587Sjoerg
24156cd587Sjoerg
25156cd587Sjoerg	.text
26156cd587Sjoerg	.syntax unified
27156cd587Sjoerg
28156cd587Sjoerg//
29156cd587Sjoerg// The table contains signed byte sized elements which are 1/2 the distance
30156cd587Sjoerg// from lr to the target label.
31156cd587Sjoerg//
32*61f2f256Sjoerg	.p2align 2
33156cd587SjoergDEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch8)
34156cd587Sjoerg	ldrb    ip, [lr, #-1]           // get first byte in table
35156cd587Sjoerg	cmp     r0, ip                  // signed compare with index
36156cd587Sjoerg	ite lo
37156cd587Sjoerg	ldrsblo r0, [lr, r0]            // get indexed byte out of table
38156cd587Sjoerg	ldrsbhs r0, [lr, ip]            // if out of range, use last entry in table
39156cd587Sjoerg	add     ip, lr, r0, lsl #1      // compute label = lr + element*2
40156cd587Sjoerg	bx      ip                      // jump to computed label
41156cd587SjoergEND_COMPILERRT_FUNCTION(__switch8)
42156cd587Sjoerg
43