blob: 2d949129ee62e5f2e9bdb12d738be38285f15c51 [file] [log] [blame]
Nick Kledzik0c610552009-09-18 00:07:52 +00001//===-- switch.S - Implement switch* --------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar19336a22009-10-27 17:49:50 +000010#include "../assembly.h"
11
Nick Kledzik0c610552009-09-18 00:07:52 +000012//
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//
Daniel Dunbarb4b1e8c2009-10-27 17:50:21 +000032DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switchu8)
Nick Kledzik0c610552009-09-18 00:07:52 +000033 ldrb ip, [lr, #-1] // get first byte in table
34 cmp r0, ip // compare with index
35 ldrbcc r0, [lr, r0] // get indexed byte out of table
36 ldrbhs r0, [lr, ip] // if out of range, use last entry in table
37 add ip, lr, r0, lsl #1 // compute label = lr + element*2
38 bx ip // jump to computed label
39
40
41
42//
43// The table contains signed byte sized elements which are 1/2 the distance
44// from lr to the target label.
45//
Daniel Dunbarb4b1e8c2009-10-27 17:50:21 +000046DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch8)
Nick Kledzik0c610552009-09-18 00:07:52 +000047 ldrb ip, [lr, #-1] // get first byte in table
48 cmp r0, ip // signed compare with index
49 ldrsbcc r0, [lr, r0] // get indexed byte out of table
50 ldrsbhs r0, [lr, ip] // if out of range, use last entry in table
51 add ip, lr, r0, lsl #1 // compute label = lr + element*2
52 bx ip // jump to computed label
53
54
55//
56// The table contains signed 2-byte sized elements which are 1/2 the distance
57// from lr to the target label.
58//
Daniel Dunbarb4b1e8c2009-10-27 17:50:21 +000059DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch16)
Nick Kledzik0c610552009-09-18 00:07:52 +000060 ldrh ip, [lr, #-1] // get first 16-bit word in table
61 cmp r0, ip // compare with index
62 add r0, lr, r0, lsl #1 // compute address of element in table
63 ldrshcc r0, [r0, #1] // load 16-bit element if r0 is in range
64 add ip, lr, ip, lsl #1 // compute address of last element in table
65 ldrshhs r0, [ip, #1] // load 16-bit element if r0 out of range
66 add ip, lr, r0, lsl #1 // compute label = lr + element*2
67 bx ip // jump to computed label
68
69
70//
71// The table contains signed 4-byte sized elements which are the distance
72// from lr to the target label.
73//
Daniel Dunbarb4b1e8c2009-10-27 17:50:21 +000074DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switch32)
Nick Kledzik0c610552009-09-18 00:07:52 +000075 ldr ip, [lr, #-1] // get first 32-bit word in table
76 cmp r0, ip // compare with index
77 add r0, lr, r0, lsl #2 // compute address of element in table
78 ldrcc r0, [r0, #3] // load 32-bit element if r0 is in range
79 add ip, lr, ip, lsl #2 // compute address of last element in table
80 ldrcs r0, [ip, #3] // load 32-bit element if r0 out of range
81 add ip, lr, r0 // compute label = lr + element
82 bx ip // jump to computed label
83
84
85 // tell linker it can break up file at label boundaries
86 .subsections_via_symbols
87