| 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 | |
| 10 | // |
| 11 | // When compiling switch statements in thumb mode, the compiler |
| 12 | // can use these __switch* helper functions The compiler emits a blx to |
| 13 | // the __switch* function followed by a table of displacements for each |
| 14 | // case statement. On entry, R0 is the index into the table. The __switch* |
| 15 | // function uses the return address in lr to find the start of the table. |
| 16 | // The first entry in the table is the count of the entries in the table. |
| 17 | // It then uses R0 to index into the table and get the displacement of the |
| 18 | // address to jump to. If R0 is greater than the size of the table, it jumps |
| 19 | // to the last entry in the table. Each displacement in the table is actually |
| 20 | // the distance from lr to the label, thus making the tables PIC. |
| 21 | |
| 22 | |
| 23 | .text |
| 24 | .syntax unified |
| 25 | |
| 26 | // |
| 27 | // The table contains unsigned byte sized elements which are 1/2 the distance |
| 28 | // from lr to the target label. |
| 29 | // |
| 30 | .globl ___switchu8 |
| 31 | .private_extern ___switchu8 |
| 32 | ___switchu8: |
| 33 | 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 | // |
| 46 | .globl ___switch8 |
| 47 | .private_extern ___switch8 |
| 48 | ___switch8: |
| 49 | ldrb ip, [lr, #-1] // get first byte in table |
| 50 | cmp r0, ip // signed compare with index |
| 51 | ldrsbcc r0, [lr, r0] // get indexed byte out of table |
| 52 | ldrsbhs r0, [lr, ip] // if out of range, use last entry in table |
| 53 | add ip, lr, r0, lsl #1 // compute label = lr + element*2 |
| 54 | bx ip // jump to computed label |
| 55 | |
| 56 | |
| 57 | // |
| 58 | // The table contains signed 2-byte sized elements which are 1/2 the distance |
| 59 | // from lr to the target label. |
| 60 | // |
| 61 | .globl ___switch16 |
| 62 | .private_extern ___switch16 |
| 63 | ___switch16: |
| 64 | ldrh ip, [lr, #-1] // get first 16-bit word in table |
| 65 | cmp r0, ip // compare with index |
| 66 | add r0, lr, r0, lsl #1 // compute address of element in table |
| 67 | ldrshcc r0, [r0, #1] // load 16-bit element if r0 is in range |
| 68 | add ip, lr, ip, lsl #1 // compute address of last element in table |
| 69 | ldrshhs r0, [ip, #1] // load 16-bit element if r0 out of range |
| 70 | add ip, lr, r0, lsl #1 // compute label = lr + element*2 |
| 71 | bx ip // jump to computed label |
| 72 | |
| 73 | |
| 74 | // |
| 75 | // The table contains signed 4-byte sized elements which are the distance |
| 76 | // from lr to the target label. |
| 77 | // |
| 78 | .globl ___switch32 |
| 79 | .private_extern ___switch32 |
| 80 | ___switch32: |
| 81 | ldr ip, [lr, #-1] // get first 32-bit word in table |
| 82 | cmp r0, ip // compare with index |
| 83 | add r0, lr, r0, lsl #2 // compute address of element in table |
| 84 | ldrcc r0, [r0, #3] // load 32-bit element if r0 is in range |
| 85 | add ip, lr, ip, lsl #2 // compute address of last element in table |
| 86 | ldrcs r0, [ip, #3] // load 32-bit element if r0 out of range |
| 87 | add ip, lr, r0 // compute label = lr + element |
| 88 | bx ip // jump to computed label |
| 89 | |
| 90 | |
| 91 | // tell linker it can break up file at label boundaries |
| 92 | .subsections_via_symbols |
| 93 | |