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