blob: 42c371ba26dccdb3b24da896a6146bf836279d66 [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
36function cover_codegen_paths() {
37 var x = 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000038
39 // This test depends on the fixed order of register allocation. We try to
40 // get values in specific registers (ia32, x64):
41 var a; // Register eax, rax.
42 var b; // Register ebx, rbx.
43 var c; // Register ecx, rcx.
44 var d; // Register edx, rdx.
45 var di; // Register edi, rdi.
Steve Blocka7e24c12009-10-30 11:49:00 +000046
47 while (x == 1) {
Andrei Popescu402d9372010-02-26 13:31:12 +000048 // The call will spill registers and leave x in {eax,rax}.
Steve Blocka7e24c12009-10-30 11:49:00 +000049 x = identity(1);
Andrei Popescu402d9372010-02-26 13:31:12 +000050 // The add will spill x and reuse {eax,rax} for the result.
Steve Blocka7e24c12009-10-30 11:49:00 +000051 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000052 // A fresh register {ebx,rbx} will be allocated for x, then reused for
53 // the result.
54 b = x + 1;
55 // Et cetera.
Steve Blocka7e24c12009-10-30 11:49:00 +000056 c = x + 1;
57 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000058 di = x + 1;
59 // Locals are in the corresponding registers here.
Andrei Popescu402d9372010-02-26 13:31:12 +000060 assertEquals(8, c << a);
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62 x = identity(1);
63 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000064 b = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000065 c = x + 1;
66 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000067 di = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000068 assertEquals(8, a << c);
Steve Blocka7e24c12009-10-30 11:49:00 +000069
70 x = identity(1);
71 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000072 b = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000073 c = x + 1;
74 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000075 di = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000076 c = 0; // Free register ecx.
Andrei Popescu402d9372010-02-26 13:31:12 +000077 assertEquals(8, a << d);
Steve Blocka7e24c12009-10-30 11:49:00 +000078
79 x = identity(1);
80 a = x + 1;
Andrei Popescu402d9372010-02-26 13:31:12 +000081 b = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000082 c = x + 1;
83 d = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000084 di = x + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000085 b = 0; // Free register ebx.
Andrei Popescu402d9372010-02-26 13:31:12 +000086 assertEquals(8, a << d);
87
88 // Test the non-commutative subtraction operation with a smi on the
89 // left, all available registers on the right, and a non-smi result.
90 x = identity(-1073741824); // Least (31-bit) smi.
91 a = x + 1; // Still a smi, the greatest smi negated.
92 b = x + 1;
93 c = x + 1;
94 d = x + 1;
95 di = x + 1;
96 // Subtraction should overflow the 31-bit smi range. The result
97 // (1073741824) is outside the 31-bit smi range so it doesn't hit the
98 // "unsafe smi" code that spills a register.
99 assertEquals(1073741824, 1 - a);
100
101 x = identity(-1073741824);
102 a = x + 1;
103 b = x + 1;
104 c = x + 1;
105 d = x + 1;
106 di = x + 1;
107 assertEquals(1073741824, 1 - b);
108
109 x = identity(-1073741824);
110 a = x + 1;
111 b = x + 1;
112 c = x + 1;
113 d = x + 1;
114 di = x + 1;
115 assertEquals(1073741824, 1 - c);
116
117 x = identity(-1073741824);
118 a = x + 1;
119 b = x + 1;
120 c = x + 1;
121 d = x + 1;
122 di = x + 1;
123 assertEquals(1073741824, 1 - d);
124
125 x = identity(-1073741824);
126 a = x + 1;
127 b = x + 1;
128 c = x + 1;
129 d = x + 1;
130 di = x + 1;
131 assertEquals(1073741824, 1 - di);
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133 x = 3;
134 }
135}
136
137cover_codegen_paths();