| Nick Kledzik | 0c61055 | 2009-09-18 00:07:52 +0000 | [diff] [blame] | 1 | //===-- 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 Dunbar | 19336a2 | 2009-10-27 17:49:50 +0000 | [diff] [blame^] | 10 | #include "../assembly.h" |
| 11 | |
| Nick Kledzik | 0c61055 | 2009-09-18 00:07:52 +0000 | [diff] [blame] | 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 | // |
| 32 | .globl ___switchu8 |
| 33 | .private_extern ___switchu8 |
| 34 | ___switchu8: |
| 35 | ldrb ip, [lr, #-1] // get first byte in table |
| 36 | cmp r0, ip // compare with index |
| 37 | ldrbcc r0, [lr, r0] // get indexed byte out of table |
| 38 | 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 |
| 41 | |
| 42 | |
| 43 | |
| 44 | // |
| 45 | // The table contains signed byte sized elements which are 1/2 the distance |
| 46 | // from lr to the target label. |
| 47 | // |
| 48 | .globl ___switch8 |
| 49 | .private_extern ___switch8 |
| 50 | ___switch8: |
| 51 | ldrb ip, [lr, #-1] // get first byte in table |
| 52 | cmp r0, ip // signed compare with index |
| 53 | ldrsbcc r0, [lr, r0] // get indexed byte out of table |
| 54 | ldrsbhs r0, [lr, ip] // if out of range, use last entry in table |
| 55 | add ip, lr, r0, lsl #1 // compute label = lr + element*2 |
| 56 | bx ip // jump to computed label |
| 57 | |
| 58 | |
| 59 | // |
| 60 | // The table contains signed 2-byte sized elements which are 1/2 the distance |
| 61 | // from lr to the target label. |
| 62 | // |
| 63 | .globl ___switch16 |
| 64 | .private_extern ___switch16 |
| 65 | ___switch16: |
| 66 | ldrh ip, [lr, #-1] // get first 16-bit word in table |
| 67 | cmp r0, ip // compare with index |
| 68 | add r0, lr, r0, lsl #1 // compute address of element in table |
| 69 | ldrshcc r0, [r0, #1] // load 16-bit element if r0 is in range |
| 70 | add ip, lr, ip, lsl #1 // compute address of last element in table |
| 71 | ldrshhs r0, [ip, #1] // load 16-bit element if r0 out of range |
| 72 | add ip, lr, r0, lsl #1 // compute label = lr + element*2 |
| 73 | bx ip // jump to computed label |
| 74 | |
| 75 | |
| 76 | // |
| 77 | // The table contains signed 4-byte sized elements which are the distance |
| 78 | // from lr to the target label. |
| 79 | // |
| 80 | .globl ___switch32 |
| 81 | .private_extern ___switch32 |
| 82 | ___switch32: |
| 83 | ldr ip, [lr, #-1] // get first 32-bit word in table |
| 84 | cmp r0, ip // compare with index |
| 85 | add r0, lr, r0, lsl #2 // compute address of element in table |
| 86 | ldrcc r0, [r0, #3] // load 32-bit element if r0 is in range |
| 87 | add ip, lr, ip, lsl #2 // compute address of last element in table |
| 88 | ldrcs r0, [ip, #3] // load 32-bit element if r0 out of range |
| 89 | add ip, lr, r0 // compute label = lr + element |
| 90 | bx ip // jump to computed label |
| 91 | |
| 92 | |
| 93 | // tell linker it can break up file at label boundaries |
| 94 | .subsections_via_symbols |
| 95 | |