xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/arm/switch32.S (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1//===-- switch.S - Implement switch* --------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "../assembly.h"
10
11//
12// When compiling switch statements in thumb mode, the compiler
13// can use these __switch* helper functions  The compiler emits a blx to
14// the __switch* function followed by a table of displacements for each
15// case statement.  On entry, R0 is the index into the table. The __switch*
16// function uses the return address in lr to find the start of the table.
17// The first entry in the table is the count of the entries in the table.
18// It then uses R0 to index into the table and get the displacement of the
19// address to jump to.  If R0 is greater than the size of the table, it jumps
20// to the last entry in the table. Each displacement in the table is actually
21// the distance from lr to the label, thus making the tables PIC.
22
23
24	.text
25	.syntax unified
26
27//
28// The table contains signed 4-byte sized elements which are the distance
29// from lr to the target label.
30//
31	.p2align 2
32DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch32)
33	ldr     ip, [lr, #-1]            // get first 32-bit word in table
34	cmp     r0, ip                   // compare with index
35	add     r0, lr, r0, lsl #2       // compute address of element in table
36	add     ip, lr, ip, lsl #2       // compute address of last element in table
37	ite lo
38	ldrlo   r0, [r0, #3]             // load 32-bit element if r0 is in range
39	ldrhs   r0, [ip, #3]             // load 32-bit element if r0 out of range
40	add     ip, lr, r0               // compute label = lr + element
41	bx      ip                       // jump to computed label
42END_COMPILERRT_FUNCTION(__switch32)
43
44NO_EXEC_STACK_DIRECTIVE
45
46