16059Samurai//===-- switch.S - Implement switch* --------------------------------------===//
26059Samurai//
36059Samurai//                     The LLVM Compiler Infrastructure
46059Samurai//
56059Samurai// This file is dual licensed under the MIT and the University of Illinois Open
66059Samurai// Source Licenses. See LICENSE.TXT for details.
76059Samurai//
86059Samurai//===----------------------------------------------------------------------===//
96059Samurai
106059Samurai#include "../assembly.h"
116059Samurai
126059Samurai//
136059Samurai// When compiling switch statements in thumb mode, the compiler
146059Samurai// can use these __switch* helper functions  The compiler emits a blx to
156059Samurai// the __switch* function followed by a table of displacements for each
166059Samurai// case statement.  On entry, R0 is the index into the table. The __switch*
176059Samurai// function uses the return address in lr to find the start of the table.
186059Samurai// The first entry in the table is the count of the entries in the table.
198857Srgrimes// It then uses R0 to index into the table and get the displacement of the
2010858Samurai// address to jump to.  If R0 is greater than the size of the table, it jumps
218857Srgrimes// to the last entry in the table. Each displacement in the table is actually
226059Samurai// the distance from lr to the label, thus making the tables PIC.
236059Samurai
246059Samurai
256059Samurai	.text
266059Samurai	.syntax unified
276059Samurai
286059Samurai//
296059Samurai// The table contains unsigned byte sized elements which are 1/2 the distance
306059Samurai// from lr to the target label.
316059Samurai//
326059Samurai	.align 2
336059SamuraiDEFINE_COMPILERRT_PRIVATE_FUNCTION(__switchu8)
346059Samurai	ldrb    ip, [lr, #-1]           // get first byte in table
356059Samurai	cmp     r0, ip                  // compare with index
366059Samurai	ldrbcc  r0, [lr, r0]            // get indexed byte out of table
376735Samurai	ldrbhs  r0, [lr, ip]            // if out of range, use last entry in table
386059Samurai	add     ip, lr, r0, lsl #1      // compute label = lr + element*2
396059Samurai	bx      ip                      // jump to computed label
406059Samurai
416059Samurai	// tell linker it can break up file at label boundaries
426059Samurai	.subsections_via_symbols
436059Samurai
446059Samurai