1*7c3d14c8STreehugger Robot//===-- switch.S - Implement switch* --------------------------------------===// 2*7c3d14c8STreehugger Robot// 3*7c3d14c8STreehugger Robot// The LLVM Compiler Infrastructure 4*7c3d14c8STreehugger Robot// 5*7c3d14c8STreehugger Robot// This file is dual licensed under the MIT and the University of Illinois Open 6*7c3d14c8STreehugger Robot// Source Licenses. See LICENSE.TXT for details. 7*7c3d14c8STreehugger Robot// 8*7c3d14c8STreehugger Robot//===----------------------------------------------------------------------===// 9*7c3d14c8STreehugger Robot 10*7c3d14c8STreehugger Robot#include "../assembly.h" 11*7c3d14c8STreehugger Robot 12*7c3d14c8STreehugger Robot// 13*7c3d14c8STreehugger Robot// When compiling switch statements in thumb mode, the compiler 14*7c3d14c8STreehugger Robot// can use these __switch* helper functions The compiler emits a blx to 15*7c3d14c8STreehugger Robot// the __switch* function followed by a table of displacements for each 16*7c3d14c8STreehugger Robot// case statement. On entry, R0 is the index into the table. The __switch* 17*7c3d14c8STreehugger Robot// function uses the return address in lr to find the start of the table. 18*7c3d14c8STreehugger Robot// The first entry in the table is the count of the entries in the table. 19*7c3d14c8STreehugger Robot// It then uses R0 to index into the table and get the displacement of the 20*7c3d14c8STreehugger Robot// address to jump to. If R0 is greater than the size of the table, it jumps 21*7c3d14c8STreehugger Robot// to the last entry in the table. Each displacement in the table is actually 22*7c3d14c8STreehugger Robot// the distance from lr to the label, thus making the tables PIC. 23*7c3d14c8STreehugger Robot 24*7c3d14c8STreehugger Robot 25*7c3d14c8STreehugger Robot .text 26*7c3d14c8STreehugger Robot .syntax unified 27*7c3d14c8STreehugger Robot 28*7c3d14c8STreehugger Robot// 29*7c3d14c8STreehugger Robot// The table contains signed 4-byte sized elements which are the distance 30*7c3d14c8STreehugger Robot// from lr to the target label. 31*7c3d14c8STreehugger Robot// 32*7c3d14c8STreehugger Robot .p2align 2 33*7c3d14c8STreehugger RobotDEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch32) 34*7c3d14c8STreehugger Robot ldr ip, [lr, #-1] // get first 32-bit word in table 35*7c3d14c8STreehugger Robot cmp r0, ip // compare with index 36*7c3d14c8STreehugger Robot add r0, lr, r0, lsl #2 // compute address of element in table 37*7c3d14c8STreehugger Robot add ip, lr, ip, lsl #2 // compute address of last element in table 38*7c3d14c8STreehugger Robot ite lo 39*7c3d14c8STreehugger Robot ldrlo r0, [r0, #3] // load 32-bit element if r0 is in range 40*7c3d14c8STreehugger Robot ldrhs r0, [ip, #3] // load 32-bit element if r0 out of range 41*7c3d14c8STreehugger Robot add ip, lr, r0 // compute label = lr + element 42*7c3d14c8STreehugger Robot bx ip // jump to computed label 43*7c3d14c8STreehugger RobotEND_COMPILERRT_FUNCTION(__switch32) 44*7c3d14c8STreehugger Robot 45*7c3d14c8STreehugger RobotNO_EXEC_STACK_DIRECTIVE 46*7c3d14c8STreehugger Robot 47