53
53
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
54
54
import com .oracle .graal .python .builtins .PythonBuiltins ;
55
55
import com .oracle .graal .python .builtins .objects .PNone ;
56
+ import com .oracle .graal .python .builtins .objects .list .PList ;
56
57
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
57
58
import com .oracle .graal .python .lib .PyObjectGetItem ;
58
- import com .oracle .graal .python .lib .PyObjectSizeNode ;
59
59
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
60
60
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
61
61
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
@@ -92,40 +92,37 @@ static Object iter(PProduct self) {
92
92
public abstract static class NextNode extends PythonUnaryBuiltinNode {
93
93
94
94
@ Specialization (guards = {"!self.isStopped()" , "!hasLst(self)" })
95
- Object next (VirtualFrame frame , PProduct self ,
96
- @ Cached PyObjectGetItem getItemNode ,
95
+ Object next (PProduct self ,
97
96
@ Cached LoopConditionProfile loopProfile ) {
98
97
Object [] lst = new Object [self .getGears ().length ];
99
98
loopProfile .profileCounted (lst .length );
100
99
for (int i = 0 ; loopProfile .inject (i < lst .length ); i ++) {
101
- lst [i ] = getItemNode . execute ( frame , self .getGears ()[i ], 0 ) ;
100
+ lst [i ] = self .getGears ()[i ][ 0 ] ;
102
101
}
103
102
self .setLst (lst );
104
103
return factory ().createTuple (lst );
105
104
}
106
105
107
106
@ Specialization (guards = {"!self.isStopped()" , "hasLst(self)" })
108
- Object next (VirtualFrame frame , PProduct self ,
109
- @ Cached PyObjectGetItem getItemNode ,
110
- @ Cached PyObjectSizeNode sizeNode ,
107
+ Object next (PProduct self ,
111
108
@ Cached ConditionProfile gearsProfile ,
112
109
@ Cached ConditionProfile indexProfile ,
113
110
@ Cached BranchProfile wasStoppedProfile ,
114
111
@ Cached LoopConditionProfile loopProfile ,
115
112
@ Cached BranchProfile doneProfile ) {
116
113
117
- Object [] gears = self .getGears ();
114
+ Object [][] gears = self .getGears ();
118
115
int x = gears .length - 1 ;
119
116
if (gearsProfile .profile (x >= 0 )) {
120
- Object gear = gears [x ];
117
+ Object [] gear = gears [x ];
121
118
int [] indices = self .getIndices ();
122
119
int index = indices [x ] + 1 ;
123
- if (indexProfile .profile (index < sizeNode . execute ( frame , gear ) )) {
120
+ if (indexProfile .profile (index < gear . length )) {
124
121
// no carry: done
125
- self .getLst ()[x ] = getItemNode . execute ( frame , gear , index ) ;
122
+ self .getLst ()[x ] = gear [ index ] ;
126
123
indices [x ] = index ;
127
124
} else {
128
- rotatePreviousGear (frame , self , getItemNode , sizeNode , loopProfile , doneProfile );
125
+ rotatePreviousGear (self , loopProfile , doneProfile );
129
126
}
130
127
} else {
131
128
self .setStopped (true );
@@ -148,26 +145,26 @@ Object nextStopped(PProduct self) {
148
145
throw raise (StopIteration );
149
146
}
150
147
151
- private static void rotatePreviousGear (VirtualFrame frame , PProduct self , PyObjectGetItem getItemNode , PyObjectSizeNode sizeNode , LoopConditionProfile loopProfile , BranchProfile doneProfile ) {
148
+ private static void rotatePreviousGear (PProduct self , LoopConditionProfile loopProfile , BranchProfile doneProfile ) {
152
149
Object [] lst = self .getLst ();
153
- Object [] gears = self .getGears ();
150
+ Object [][] gears = self .getGears ();
154
151
int x = gears .length - 1 ;
155
- lst [x ] = getItemNode . execute ( frame , gears [x ], 0 ) ;
152
+ lst [x ] = gears [x ][ 0 ] ;
156
153
int [] indices = self .getIndices ();
157
154
indices [x ] = 0 ;
158
155
x = x - 1 ;
159
156
// the outer loop runs as long as a we have a carry
160
157
while (loopProfile .profile (x >= 0 )) {
161
- Object gear = gears [x ];
158
+ Object [] gear = gears [x ];
162
159
int index = indices [x ] + 1 ;
163
- if (index < sizeNode . execute ( frame , gear ) ) {
160
+ if (index < gear . length ) {
164
161
// no carry: done
165
162
doneProfile .enter ();
166
- lst [x ] = getItemNode . execute ( frame , gear , index ) ;
163
+ lst [x ] = gear [ index ] ;
167
164
indices [x ] = index ;
168
165
return ;
169
166
}
170
- lst [x ] = getItemNode . execute ( frame , gear , 0 ) ;
167
+ lst [x ] = gear [ 0 ] ;
171
168
indices [x ] = 0 ;
172
169
x = x - 1 ;
173
170
}
@@ -187,19 +184,27 @@ public abstract static class ReduceNode extends PythonUnaryBuiltinNode {
187
184
Object reduce (PProduct self ,
188
185
@ Cached GetClassNode getClassNode ) {
189
186
Object type = getClassNode .execute (self );
190
- PTuple gearTuples = factory (). createTuple ( self . getGears () );
187
+ PTuple gearTuples = createGearTuple ( self );
191
188
return factory ().createTuple (new Object []{type , gearTuples });
192
189
}
193
190
194
191
@ Specialization (guards = {"!self.isStopped()" , "hasLst(self)" })
195
192
Object reduceLst (PProduct self ,
196
193
@ Cached GetClassNode getClassNode ) {
197
194
Object type = getClassNode .execute (self );
198
- PTuple gearTuples = factory (). createTuple ( self . getGears () );
195
+ PTuple gearTuples = createGearTuple ( self );
199
196
PTuple indicesTuple = factory ().createTuple (PythonUtils .arrayCopyOf (self .getIndices (), self .getIndices ().length ));
200
197
return factory ().createTuple (new Object []{type , gearTuples , indicesTuple });
201
198
}
202
199
200
+ private PTuple createGearTuple (PProduct self ) {
201
+ PList [] lists = new PList [self .getGears ().length ];
202
+ for (int i = 0 ; i < lists .length ; i ++) {
203
+ lists [i ] = factory ().createList (self .getGears ()[i ]);
204
+ }
205
+ return factory ().createTuple (lists );
206
+ }
207
+
203
208
@ Specialization (guards = "self.isStopped()" )
204
209
Object reduceStopped (PProduct self ,
205
210
@ Cached GetClassNode getClassNode ) {
@@ -218,18 +223,17 @@ protected static boolean hasLst(PProduct self) {
218
223
public abstract static class SetStateNode extends PythonBinaryBuiltinNode {
219
224
@ Specialization
220
225
static Object setState (VirtualFrame frame , PProduct self , Object state ,
221
- @ Cached PyObjectSizeNode sizeNode ,
222
226
@ Cached PyObjectGetItem getItemNode ,
223
227
@ Cached LoopConditionProfile loopProfile ,
224
228
@ Cached BranchProfile stoppedProfile ,
225
229
@ Cached ConditionProfile indexProfile ) {
226
- Object [] gears = self .getGears ();
230
+ Object [][] gears = self .getGears ();
227
231
Object [] lst = new Object [gears .length ];
228
232
int [] indices = self .getIndices ();
229
233
loopProfile .profileCounted (gears .length );
230
234
for (int i = 0 ; loopProfile .inject (i < gears .length ); i ++) {
231
235
int index = (int ) getItemNode .execute (frame , state , i );
232
- int gearSize = sizeNode . execute ( frame , gears [i ]) ;
236
+ int gearSize = gears [i ]. length ;
233
237
if (indices == null || gearSize == 0 ) {
234
238
stoppedProfile .enter ();
235
239
self .setStopped (true );
@@ -241,7 +245,7 @@ static Object setState(VirtualFrame frame, PProduct self, Object state,
241
245
index = gearSize - 1 ;
242
246
}
243
247
indices [i ] = index ;
244
- lst [i ] = getItemNode . execute ( frame , gears [i ], index ) ;
248
+ lst [i ] = gears [i ][ index ] ;
245
249
}
246
250
self .setLst (lst );
247
251
return PNone .NONE ;
0 commit comments