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 4-byte sized elements which are the distance 30156cd587Sjoerg// from lr to the target label. 31156cd587Sjoerg// 32*61f2f256Sjoerg .p2align 2 33156cd587SjoergDEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch32) 34156cd587Sjoerg ldr ip, [lr, #-1] // get first 32-bit word in table 35156cd587Sjoerg cmp r0, ip // compare with index 36156cd587Sjoerg add r0, lr, r0, lsl #2 // compute address of element in table 37156cd587Sjoerg add ip, lr, ip, lsl #2 // compute address of last element in table 38156cd587Sjoerg ite lo 39156cd587Sjoerg ldrlo r0, [r0, #3] // load 32-bit element if r0 is in range 40156cd587Sjoerg ldrhs r0, [ip, #3] // load 32-bit element if r0 out of range 41156cd587Sjoerg add ip, lr, r0 // compute label = lr + element 42156cd587Sjoerg bx ip // jump to computed label 43156cd587SjoergEND_COMPILERRT_FUNCTION(__switch32) 44156cd587Sjoerg 45