TEMPLATE_STRING_INDEXOF.S revision a8b91c52fd8a90b784835dfe1f8898035266c4dd
1    /*
2     * String's indexOf.
3     *
4     * Requires a0 to have been previously checked for null.  Will
5     * return index of match of a1 in v0.
6     *
7     * IMPORTANT NOTE:
8     *
9     * This code relies on hard-coded offsets for string objects, and must be
10     * kept in sync wth definitions in UtfString.h  See asm-constants.h
11     *
12     * On entry:
13     *    a0:   string object pointer
14     *    a1:   char to match
15     *    a2:   Starting offset in string data
16     */
17
18     lw    t0, STRING_FIELDOFF_OFFSET(a0)
19     lw    t1, STRING_FIELDOFF_COUNT(a0)
20     lw    v0, STRING_FIELDOFF_VALUE(a0)
21
22    /*
23     * At this point, we have:
24     *    v0: object pointer
25     *    a1: char to match
26     *    a2: starting offset
27     *    t0: offset
28     *    t1: string length
29     */
30
31    /* Point to first element */
32     addu  v0, 16                    # point to contents[0]
33
34    /* Build pointer to start of string data */
35     sll   t7, t0, 1                 # multiply offset by 2
36     addu  v0, v0, t7
37
38    /* Save a copy of starting data in v1 */
39     move  v1, v0
40
41    /* Clamp start to [0..count] */
42     slt   t7, a2, zero
43     movn  a2, zero, t7
44     sgt   t7, a2, t1
45     movn  a2, t1, t7
46
47    /* Build pointer to start of data to compare */
48     sll   t7, a2, 1                # multiply offset by 2
49     addu  v0, v0, t7
50
51    /* Compute iteration count */
52     subu  a3, t1, a2
53
54    /*
55     * At this point we have:
56     *   v0: start of data to test
57     *   a1: char to compare
58     *   a3: iteration count
59     *   v1: original start of string
60     *   t0-t7 available for loading string data
61     */
62     subu  a3, 4
63     bltz  a3, indexof_remainder
64
65indexof_loop4:
66     lhu   t0, 0(v0)
67     beq   t0, a1, match_0
68     lhu   t0, 2(v0)
69     beq   t0, a1, match_1
70     lhu   t0, 4(v0)
71     beq   t0, a1, match_2
72     lhu   t0, 6(v0)
73     beq   t0, a1, match_3
74     addu  v0, 8                     # offset to contents[i+4]
75     subu  a3, 4
76     bgez  a3, indexof_loop4
77
78indexof_remainder:
79     addu  a3, 4
80     beqz  a3, indexof_nomatch
81
82indexof_loop1:
83     lhu   t0, 0(v0)
84     beq   t0, a1, match_0
85     addu  v0, 2                     # offset to contents[i+1]
86     subu  a3, 1
87     bnez  a3, indexof_loop1
88
89indexof_nomatch:
90     li    v0, -1
91     RETURN
92
93match_0:
94     subu  v0, v1
95     sra   v0, v0, 1                 # divide by 2
96     RETURN
97match_1:
98     addu  v0, 2
99     subu  v0, v1
100     sra   v0, v0, 1                 # divide by 2
101     RETURN
102match_2:
103     addu  v0, 4
104     subu  v0, v1
105     sra   v0, v0, 1                 # divide by 2
106     RETURN
107match_3:
108     addu  v0, 6
109     subu  v0, v1
110     sra   v0, v0, 1                 # divide by 2
111     RETURN
112