switchu8.S 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //===-- switch.S - Implement switch* --------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "../assembly.h"
  9. //
  10. // When compiling switch statements in thumb mode, the compiler
  11. // can use these __switch* helper functions The compiler emits a blx to
  12. // the __switch* function followed by a table of displacements for each
  13. // case statement. On entry, R0 is the index into the table. The __switch*
  14. // function uses the return address in lr to find the start of the table.
  15. // The first entry in the table is the count of the entries in the table.
  16. // It then uses R0 to index into the table and get the displacement of the
  17. // address to jump to. If R0 is greater than the size of the table, it jumps
  18. // to the last entry in the table. Each displacement in the table is actually
  19. // the distance from lr to the label, thus making the tables PIC.
  20. .text
  21. .syntax unified
  22. //
  23. // The table contains unsigned byte sized elements which are 1/2 the distance
  24. // from lr to the target label.
  25. //
  26. .p2align 2
  27. DEFINE_COMPILERRT_PRIVATE_FUNCTION(__switchu8)
  28. ldrb ip, [lr, #-1] // get first byte in table
  29. cmp r0, ip // compare with index
  30. ite lo
  31. ldrblo r0, [lr, r0] // get indexed byte out of table
  32. ldrbhs r0, [lr, ip] // if out of range, use last entry in table
  33. add ip, lr, r0, lsl #1 // compute label = lr + element*2
  34. bx ip // jump to computed label
  35. END_COMPILERRT_FUNCTION(__switchu8)
  36. NO_EXEC_STACK_DIRECTIVE