blob: 1844f11c604e71021fc8ad855d5775e408c39df9 [file] [log] [blame]
Daniel Dunbar4c43c2b2010-01-19 00:01:10 +00001//===-- switch.S - Implement switch* --------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant5b791f62010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Daniel Dunbar4c43c2b2010-01-19 00:01:10 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "../assembly.h"
11
12//
13// When compiling switch statements in thumb mode, the compiler
14// can use these __switch* helper functions The compiler emits a blx to
15// the __switch* function followed by a table of displacements for each
16// case statement. On entry, R0 is the index into the table. The __switch*
17// function uses the return address in lr to find the start of the table.
18// The first entry in the table is the count of the entries in the table.
19// It then uses R0 to index into the table and get the displacement of the
20// address to jump to. If R0 is greater than the size of the table, it jumps
21// to the last entry in the table. Each displacement in the table is actually
22// the distance from lr to the label, thus making the tables PIC.
23
24
25 .text
26 .syntax unified
27
28//
29// The table contains unsigned byte sized elements which are 1/2 the distance
30// from lr to the target label.
31//
Saleem Abdulrasool310874a2014-05-12 15:23:37 +000032 .p2align 2
Daniel Dunbar4c43c2b2010-01-19 00:01:10 +000033DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switchu8)
34 ldrb ip, [lr, #-1] // get first byte in table
35 cmp r0, ip // compare with index
Tim Northover38a0cb52013-11-11 22:50:13 +000036 ite lo
37 ldrblo r0, [lr, r0] // get indexed byte out of table
Daniel Dunbar4c43c2b2010-01-19 00:01:10 +000038 ldrbhs r0, [lr, ip] // if out of range, use last entry in table
39 add ip, lr, r0, lsl #1 // compute label = lr + element*2
40 bx ip // jump to computed label
Joerg Sonnenberger171e9cf2014-01-24 14:33:42 +000041END_COMPILERRT_FUNCTION(__switchu8)
Daniel Dunbar4c43c2b2010-01-19 00:01:10 +000042