blob: 8e7f1891a2134d34a1c268ddf75c64b261882709 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Andrei Popescu402d9372010-02-26 13:31:12 +000028// Flags: --nofull-compiler --nofast-compiler
Steve Blocka7e24c12009-10-30 11:49:00 +000029
Andrei Popescu402d9372010-02-26 13:31:12 +000030// Test paths in the code generator where values in specific registers
31// get moved around.
Steve Blocka7e24c12009-10-30 11:49:00 +000032function identity(x) {
33 return x;
34}
35
Steve Block6ded16b2010-05-10 14:33:55 +010036function lookup(w, a) {
37 // This function tests a code path in the generation of a keyed load IC
38 // where the key and the value are both in the same register.
39 a = a;
40 w[a] = a;
41}
42
Steve Blocka7e24c12009-10-30 11:49:00 +000043function cover_codegen_paths() {
44 var x = 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000045
46 // This test depends on the fixed order of register allocation. We try to
47 // get values in specific registers (ia32, x64):
48 var a; // Register eax, rax.
49 var b; // Register ebx, rbx.
50 var c; // Register ecx, rcx.
51 var d; // Register edx, rdx.
52 var di; // Register edi, rdi.
Steve Blocka7e24c12009-10-30 11:49:00 +000053
54 while (x == 1) {
Andrei Popescu402d9372010-02-26 13:31:12 +000055 // The call will spill registers and leave x in {eax,rax}.
Steve Blocka7e24c12009-10-30 11:49:00 +000056 x = identity(1);
Andrei Popescu402d9372010-02-26 13:31:12 +000057 // The add will spill x and reuse {eax,rax} for the result.
Steve Blocka7e24c12009-10-30 11:49:00 +000058 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000059 // A fresh register {ebx,rbx} will be allocated for x, then reused for
60 // the result.
61 b = x + 1;
62 // Et cetera.
Steve Blocka7e24c12009-10-30 11:49:00 +000063 c = x + 1;
64 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000065 di = x + 1;
66 // Locals are in the corresponding registers here.
Andrei Popescu402d9372010-02-26 13:31:12 +000067 assertEquals(8, c << a);
Steve Blocka7e24c12009-10-30 11:49:00 +000068
69 x = identity(1);
70 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000071 b = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000072 c = x + 1;
73 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000074 di = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000075 assertEquals(8, a << c);
Steve Blocka7e24c12009-10-30 11:49:00 +000076
77 x = identity(1);
78 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000079 b = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000080 c = x + 1;
81 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000082 di = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000083 c = 0; // Free register ecx.
Andrei Popescu402d9372010-02-26 13:31:12 +000084 assertEquals(8, a << d);
Steve Blocka7e24c12009-10-30 11:49:00 +000085
86 x = identity(1);
87 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000088 b = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000089 c = x + 1;
90 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000091 di = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000092 b = 0; // Free register ebx.
Andrei Popescu402d9372010-02-26 13:31:12 +000093 assertEquals(8, a << d);
94
95 // Test the non-commutative subtraction operation with a smi on the
96 // left, all available registers on the right, and a non-smi result.
97 x = identity(-1073741824); // Least (31-bit) smi.
98 a = x + 1; // Still a smi, the greatest smi negated.
99 b = x + 1;
100 c = x + 1;
101 d = x + 1;
102 di = x + 1;
103 // Subtraction should overflow the 31-bit smi range. The result
104 // (1073741824) is outside the 31-bit smi range so it doesn't hit the
105 // "unsafe smi" code that spills a register.
106 assertEquals(1073741824, 1 - a);
107
108 x = identity(-1073741824);
109 a = x + 1;
110 b = x + 1;
111 c = x + 1;
112 d = x + 1;
113 di = x + 1;
114 assertEquals(1073741824, 1 - b);
115
116 x = identity(-1073741824);
117 a = x + 1;
118 b = x + 1;
119 c = x + 1;
120 d = x + 1;
121 di = x + 1;
122 assertEquals(1073741824, 1 - c);
123
124 x = identity(-1073741824);
125 a = x + 1;
126 b = x + 1;
127 c = x + 1;
128 d = x + 1;
129 di = x + 1;
130 assertEquals(1073741824, 1 - d);
131
132 x = identity(-1073741824);
133 a = x + 1;
134 b = x + 1;
135 c = x + 1;
136 d = x + 1;
137 di = x + 1;
138 assertEquals(1073741824, 1 - di);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
140 x = 3;
Steve Block6ded16b2010-05-10 14:33:55 +0100141 var w = { };
142 lookup(w, x);
143 lookup(w, x);
144 lookup(w, x);
145
146 x = 3; // Terminate while loop.
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 }
148}
149
150cover_codegen_paths();