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