Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: src/code-stubs.cc

Issue 23618002: Hydrogenisation of binops (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: rebase Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/code-stubs.h ('k') | src/code-stubs-hydrogen.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 130
131 131
132 Handle<Code> CodeStub::GetCode(Isolate* isolate) { 132 Handle<Code> CodeStub::GetCode(Isolate* isolate) {
133 Factory* factory = isolate->factory(); 133 Factory* factory = isolate->factory();
134 Heap* heap = isolate->heap(); 134 Heap* heap = isolate->heap();
135 Code* code; 135 Code* code;
136 if (UseSpecialCache() 136 if (UseSpecialCache()
137 ? FindCodeInSpecialCache(&code, isolate) 137 ? FindCodeInSpecialCache(&code, isolate)
138 : FindCodeInCache(&code, isolate)) { 138 : FindCodeInCache(&code, isolate)) {
139 ASSERT(IsPregenerated(isolate) == code->is_pregenerated()); 139 ASSERT(IsPregenerated(isolate) == code->is_pregenerated());
140 ASSERT(GetCodeKind() == code->kind());
140 return Handle<Code>(code); 141 return Handle<Code>(code);
141 } 142 }
142 143
143 { 144 {
144 HandleScope scope(isolate); 145 HandleScope scope(isolate);
145 146
146 Handle<Code> new_object = GenerateCode(isolate); 147 Handle<Code> new_object = GenerateCode(isolate);
147 new_object->set_major_key(MajorKey()); 148 new_object->set_major_key(MajorKey());
148 FinishCode(new_object); 149 FinishCode(new_object);
149 RecordCodeGeneration(*new_object, isolate); 150 RecordCodeGeneration(*new_object, isolate);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 stream->Add("%s", MajorName(MajorKey(), false)); 197 stream->Add("%s", MajorName(MajorKey(), false));
197 } 198 }
198 199
199 200
200 void CodeStub::PrintName(StringStream* stream) { 201 void CodeStub::PrintName(StringStream* stream) {
201 PrintBaseName(stream); 202 PrintBaseName(stream);
202 PrintState(stream); 203 PrintState(stream);
203 } 204 }
204 205
205 206
206 void BinaryOpStub::Generate(MacroAssembler* masm) { 207 void BinaryOpStub::PrintBaseName(StringStream* stream) {
207 // Explicitly allow generation of nested stubs. It is safe here because 208 const char* op_name = Token::Name(op_);
208 // generation code does not use any raw pointers. 209 stream->Add("BinaryOpStub_%s",
209 AllowStubCallsScope allow_stub_calls(masm, true); 210 op_name);
210 211 }
211 BinaryOpIC::TypeInfo operands_type = Max(left_type_, right_type_); 212
212 if (left_type_ == BinaryOpIC::ODDBALL && right_type_ == BinaryOpIC::ODDBALL) { 213
213 // The OddballStub handles a number and an oddball, not two oddballs. 214 void BinaryOpStub::PrintState(StringStream* stream) {
214 operands_type = BinaryOpIC::GENERIC; 215 stream->Add("(");
215 } 216 stream->Add(StateToName(left_state_));
216 switch (operands_type) { 217 if (left_oddball_state_ != NO_ODDBALL) {
217 case BinaryOpIC::UNINITIALIZED: 218 stream->Add(",");
218 GenerateTypeTransition(masm); 219 stream->Add(OddballStateToName(left_oddball_state_));
219 break; 220 }
220 case BinaryOpIC::SMI: 221 stream->Add("*");
221 GenerateSmiStub(masm); 222 if (fixed_right_arg_.has_value) {
222 break; 223 stream->Add("%d", fixed_right_arg_.value);
223 case BinaryOpIC::INT32: 224 } else {
224 GenerateInt32Stub(masm); 225 stream->Add(StateToName(right_state_));
225 break; 226 if (right_oddball_state_ != NO_ODDBALL) {
226 case BinaryOpIC::NUMBER: 227 stream->Add(",");
227 GenerateNumberStub(masm); 228 stream->Add(OddballStateToName(right_oddball_state_));
228 break; 229 }
229 case BinaryOpIC::ODDBALL: 230 }
230 GenerateOddballStub(masm); 231 stream->Add("->");
231 break; 232 stream->Add(StateToName(result_state_));
232 case BinaryOpIC::STRING: 233 stream->Add(")");
233 GenerateStringStub(masm); 234 }
234 break; 235
235 case BinaryOpIC::GENERIC: 236
236 GenerateGeneric(masm); 237 Maybe<Handle<Object> > BinaryOpStub::Result(Handle<Object> left,
237 break; 238 Handle<Object> right,
239 Isolate* isolate) {
240 Handle<JSBuiltinsObject> builtins(isolate->js_builtins_object());
241 Builtins::JavaScript func = BinaryOpIC::TokenToJSBuiltin(op_);
242 Object* builtin = builtins->javascript_builtin(func);
243 Handle<JSFunction> builtin_function =
244 Handle<JSFunction>(JSFunction::cast(builtin), isolate);
245 bool caught_exception;
246 Handle<Object> result = Execution::Call(isolate, builtin_function, left,
247 1, &right, &caught_exception);
248 return Maybe<Handle<Object> >(!caught_exception, result);
249 }
250
251
252 void BinaryOpStub::init() {
253 fixed_right_arg_.has_value = false;
254 left_state_ = right_state_ = result_state_ = NONE;
255 left_oddball_state_ = right_oddball_state_ = NO_ODDBALL;
256 }
257
258
259 void BinaryOpStub::GenerateAheadOfTimeAllOps(Isolate* isolate,
260 BinaryOpStub stub) {
261 Token::Value binop[] = {Token::SUB, Token::MOD, Token::DIV, Token::MUL,
262 Token::ADD, Token::SAR, Token::BIT_OR, Token::BIT_AND,
263 Token::BIT_XOR, Token::SHL, Token::SHR};
264 for (int i = 0; i < 11; i++) {
265 // Bitwise operations cannot return heap numbers.
266 if (i > 5 && stub.result_state_ > INT32) continue;
267 // TODO(olivf) NumberTagU is not snapshot safe yet so we have to skip SHR
268 // since that produces a unsigned int32.
269 if (binop[i] == Token::SHR && stub.result_state_ > SMI) continue;
270
271 stub.op_ = binop[i];
272 stub.GetCode(isolate);
273 }
274 }
275
276
277 void BinaryOpStub::GenerateAheadOfTime(Isolate* isolate) {
278 BinaryOpStub unint_stub(UNINITIALIZED);
279 GenerateAheadOfTimeAllOps(isolate, unint_stub);
280
281 BinaryOpStub stub(INITIALIZED);
282 for (int left = SMI; left <= NUMBER; left++) {
283 stub.left_state_ = static_cast<State>(left);
284 for (int right = SMI; right <= NUMBER; right++) {
285 stub.right_state_ = static_cast<State>(right);
286 for (int res = SMI; res <= NUMBER; res++) {
287 stub.result_state_ = static_cast<State>(res);
288 GenerateAheadOfTimeAllOps(isolate, stub);
289 }
290 }
291 }
292 stub.left_state_ = STRING;
293 stub.right_state_ = STRING;
294 stub.result_state_ = STRING;
295 stub.op_ = Token::ADD;
296 stub.GetCode(isolate);
297 }
298
299
300 bool BinaryOpStub::can_encode_arg_value(int32_t value) const {
301 return op_ == Token::MOD && value > 0 && IsPowerOf2(value) &&
302 FixedRightArgValueBits::is_valid(WhichPowerOf2(value));
303 }
304
305
306 int BinaryOpStub::encode_arg_value(int32_t value) const {
307 ASSERT(can_encode_arg_value(value));
308 return WhichPowerOf2(value);
309 }
310
311
312 int32_t BinaryOpStub::decode_arg_value(int value) const {
313 return 1 << value;
314 }
315
316
317 int BinaryOpStub::encode_token(Token::Value op) const {
318 ASSERT(op >= FIRST_TOKEN && op <= LAST_TOKEN);
319 return op - FIRST_TOKEN;
320 }
321
322
323 Token::Value BinaryOpStub::decode_token(int op) const {
324 int res = op + FIRST_TOKEN;
325 ASSERT(res >= FIRST_TOKEN && res <= LAST_TOKEN);
326 return static_cast<Token::Value>(res);
327 }
328
329
330 const char* BinaryOpStub::StateToName(State state) {
331 switch (state) {
238 default: 332 default:
239 UNREACHABLE(); 333 UNREACHABLE();
240 } 334 case NONE:
241 } 335 return "None";
242 336 case SMI:
243 337 return "Smi";
244 #define __ ACCESS_MASM(masm) 338 case INT32:
245 339 return "Int32";
246 340 case NUMBER:
247 void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) { 341 return "Number";
248 switch (op_) { 342 case STRING:
249 case Token::ADD: 343 return "String";
250 __ InvokeBuiltin(Builtins::ADD, CALL_FUNCTION); 344 case GENERIC:
251 break; 345 return "Generic";
252 case Token::SUB: 346 }
253 __ InvokeBuiltin(Builtins::SUB, CALL_FUNCTION); 347 }
254 break; 348
255 case Token::MUL: 349
256 __ InvokeBuiltin(Builtins::MUL, CALL_FUNCTION); 350 const char* BinaryOpStub::OddballStateToName(OddballState state) {
257 break; 351 switch (state) {
258 case Token::DIV:
259 __ InvokeBuiltin(Builtins::DIV, CALL_FUNCTION);
260 break;
261 case Token::MOD:
262 __ InvokeBuiltin(Builtins::MOD, CALL_FUNCTION);
263 break;
264 case Token::BIT_OR:
265 __ InvokeBuiltin(Builtins::BIT_OR, CALL_FUNCTION);
266 break;
267 case Token::BIT_AND:
268 __ InvokeBuiltin(Builtins::BIT_AND, CALL_FUNCTION);
269 break;
270 case Token::BIT_XOR:
271 __ InvokeBuiltin(Builtins::BIT_XOR, CALL_FUNCTION);
272 break;
273 case Token::SAR:
274 __ InvokeBuiltin(Builtins::SAR, CALL_FUNCTION);
275 break;
276 case Token::SHR:
277 __ InvokeBuiltin(Builtins::SHR, CALL_FUNCTION);
278 break;
279 case Token::SHL:
280 __ InvokeBuiltin(Builtins::SHL, CALL_FUNCTION);
281 break;
282 default: 352 default:
283 UNREACHABLE(); 353 UNREACHABLE();
284 } 354 case NO_ODDBALL:
285 } 355 return "None";
286 356 case BOOLEAN:
287 357 return "Boolean";
288 #undef __ 358 case NULL_VALUE:
289 359 return "Undefined";
290 360 case BOOLEN_AND_NULL:
291 void BinaryOpStub::PrintName(StringStream* stream) { 361 return "Boolean,Null";
292 const char* op_name = Token::Name(op_); 362 }
293 const char* overwrite_name; 363 }
294 switch (mode_) { 364
295 case NO_OVERWRITE: overwrite_name = "Alloc"; break; 365
296 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break; 366 void BinaryOpStub::UpdateStatus(Handle<Object> left,
297 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break; 367 Handle<Object> right,
298 default: overwrite_name = "UnknownOverwrite"; break; 368 Maybe<Handle<Object> > result) {
299 } 369 int old_state = GetExtraICState();
300 stream->Add("BinaryOpStub_%s_%s_%s+%s", 370
301 op_name, 371 UpdateStatus(left, &left_state_, &left_oddball_state_);
302 overwrite_name, 372 UpdateStatus(right, &right_state_, &right_oddball_state_);
303 BinaryOpIC::GetName(left_type_), 373
304 BinaryOpIC::GetName(right_type_)); 374 int32_t value;
305 } 375 bool new_has_fixed_right_arg =
306 376 right->ToInt32(&value) && can_encode_arg_value(value) &&
307 377 (left_state_ == SMI || left_state_ == INT32) &&
308 void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) { 378 (result_state_ == NONE || !fixed_right_arg_.has_value);
309 ASSERT(left_type_ == BinaryOpIC::STRING || right_type_ == BinaryOpIC::STRING); 379
310 ASSERT(op_ == Token::ADD); 380 fixed_right_arg_ = Maybe<int32_t>(new_has_fixed_right_arg, value);
311 if (left_type_ == BinaryOpIC::STRING && right_type_ == BinaryOpIC::STRING) { 381
312 GenerateBothStringStub(masm); 382 if (result.has_value) UpdateStatus(result.value, &result_state_);
313 return; 383
314 } 384 State max_result = has_int_result() ? INT32 : NUMBER;
315 // Try to add arguments as strings, otherwise, transition to the generic 385 State max_input = Max(left_state_, right_state_);
316 // BinaryOpIC type. 386
317 GenerateAddStrings(masm); 387 // Avoid unnecessary Representation changes.
318 GenerateTypeTransition(masm); 388 if (left_state_ == STRING && right_state_ < STRING) {
319 } 389 right_state_ = GENERIC;
320 390 } else if (right_state_ == STRING && left_state_ < STRING) {
321 391 left_state_ = GENERIC;
392 } else if ((right_state_ == GENERIC && left_state_ != STRING) ||
393 (left_state_ == GENERIC && right_state_ != STRING)) {
394 left_state_ = right_state_ = GENERIC;
395 } else if (max_input <= NUMBER && max_input > result_state_) {
396 result_state_ = Min(max_result, max_input);
397 }
398
399 ASSERT(result_state_ <= max_result || op_ == Token::ADD);
400
401 if (old_state == GetExtraICState()) {
402 // Since the fpu is to precise, we might bail out on numbers which
403 // actually would truncate with 64 bit precision.
404 ASSERT(!CpuFeatures::IsSupported(SSE2) &&
405 result_state_ <= INT32);
406 result_state_ = NUMBER;
407 }
408 }
409
410
411 void BinaryOpStub::UpdateStatus(Handle<Object> object,
412 State* state,
413 OddballState* oddball_state) {
414 v8::internal::TypeInfo type = v8::internal::TypeInfo::FromValue(object);
415 if (object->IsUndefined()) {
416 // Undefined will be automatically truncated for us by HChange.
417 type = (op_ == Token::BIT_AND || op_ == Token::BIT_OR ||
418 op_ == Token::BIT_XOR || op_ == Token::SAR ||
419 op_ == Token::SHL || op_ == Token::SHR)
420 ? TypeInfo::Integer32()
421 : TypeInfo::Double();
422 }
423 State int_state = SmiValuesAre32Bits() ? NUMBER : INT32;
424 if (type.IsSmi() && *state <= SMI) {
425 *state = SMI;
426 } else if (type.IsInteger32() && *state <= int_state) {
427 *state = int_state;
428 } else if (type.IsNumber() && *state <= NUMBER) {
429 *state = NUMBER;
430 } else if (object->IsString() && operation() == Token::ADD &&
431 (*state == NONE || *state == STRING)) {
432 *state = STRING;
433 } else if (object->IsNull()) {
434 ASSERT(oddball_state != NULL);
435 *oddball_state = (*oddball_state == NO_ODDBALL ||
436 *oddball_state == NULL_VALUE)
437 ? NULL_VALUE : BOOLEN_AND_NULL;
438 } else if (object->IsBoolean()) {
439 ASSERT(oddball_state != NULL);
440 *oddball_state = (*oddball_state == NO_ODDBALL ||
441 *oddball_state == BOOLEAN)
442 ? BOOLEAN : BOOLEN_AND_NULL;
443 } else {
444 *state = GENERIC;
445 }
446 }
447
448
449 Handle<Type> BinaryOpStub::StateToType(State state,
450 OddballState oddball_state,
451 Isolate* isolate) {
452 Handle<Type> t = handle(Type::None(), isolate);
453 switch (state) {
454 case NUMBER:
455 t = handle(Type::Union(t, handle(Type::Number(), isolate)), isolate);
456 break;
457 case INT32:
458 t = handle(Type::Union(t, handle(Type::Signed32(), isolate)), isolate);
459 break;
460 case SMI:
461 t = handle(Type::Union(t, handle(Type::Smi(), isolate)), isolate);
462 break;
463
464 case STRING:
465 t = handle(Type::Union(t, handle(Type::String(), isolate)), isolate);
466 break;
467 case GENERIC:
468 return handle(Type::Any(), isolate);
469 break;
470 case NONE:
471 break;
472 }
473 switch (oddball_state) {
474 case NULL_VALUE:
475 t = handle(Type::Union(t, handle(Type::Null(), isolate)), isolate);
476 break;
477 case BOOLEN_AND_NULL:
478 t = handle(Type::Union(t, handle(Type::Null(), isolate)), isolate);
479 // Fall through.
480 case BOOLEAN:
481 t = handle(Type::Union(t, handle(Type::Boolean(), isolate)), isolate);
482 break;
483 case NO_ODDBALL:
484 break;
485 }
486 return t;
487 }
488
489
490 Handle<Type> BinaryOpStub::GetLeftType(Isolate* isolate) const {
491 return StateToType(left_state_, left_oddball_state_, isolate);
492 }
493
494
495 Handle<Type> BinaryOpStub::GetRightType(Isolate* isolate) const {
496 return StateToType(right_state_, right_oddball_state_, isolate);
497 }
498
499
500 Handle<Type> BinaryOpStub::GetResultType(Isolate* isolate) const {
501 if (HasSideEffects(isolate)) return StateToType(NONE, NO_ODDBALL, isolate);
502 if (result_state_ == GENERIC && op_ == Token::ADD) {
503 return handle(Type::Union(handle(Type::Number(), isolate),
504 handle(Type::String(), isolate)), isolate);
505 }
506 ASSERT(result_state_ != GENERIC);
507 if (result_state_ == NUMBER && op_ == Token::SHR) {
508 return handle(Type::Unsigned32(), isolate);
509 }
510 return StateToType(result_state_, NO_ODDBALL, isolate);
511 }
512
513
322 InlineCacheState ICCompareStub::GetICState() { 514 InlineCacheState ICCompareStub::GetICState() {
323 CompareIC::State state = Max(left_, right_); 515 CompareIC::State state = Max(left_, right_);
324 switch (state) { 516 switch (state) {
325 case CompareIC::UNINITIALIZED: 517 case CompareIC::UNINITIALIZED:
326 return ::v8::internal::UNINITIALIZED; 518 return ::v8::internal::UNINITIALIZED;
327 case CompareIC::SMI: 519 case CompareIC::SMI:
328 case CompareIC::NUMBER: 520 case CompareIC::NUMBER:
329 case CompareIC::INTERNALIZED_STRING: 521 case CompareIC::INTERNALIZED_STRING:
330 case CompareIC::STRING: 522 case CompareIC::STRING:
331 case CompareIC::UNIQUE_NAME: 523 case CompareIC::UNIQUE_NAME:
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 InstallDescriptor(isolate, &stub3); 987 InstallDescriptor(isolate, &stub3);
796 } 988 }
797 989
798 InternalArrayConstructorStub::InternalArrayConstructorStub( 990 InternalArrayConstructorStub::InternalArrayConstructorStub(
799 Isolate* isolate) { 991 Isolate* isolate) {
800 InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate); 992 InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
801 } 993 }
802 994
803 995
804 } } // namespace v8::internal 996 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/code-stubs.h ('k') | src/code-stubs-hydrogen.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698