OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/execution.h" | 7 #include "src/execution.h" |
8 #include "src/handles.h" | 8 #include "src/handles.h" |
9 #include "src/interpreter/bytecode-array-builder.h" | 9 #include "src/interpreter/bytecode-array-builder.h" |
10 #include "src/interpreter/interpreter.h" | 10 #include "src/interpreter/interpreter.h" |
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 " this.func = function(a, b, c, d, e, f, g, h, i, j) {" | 884 " this.func = function(a, b, c, d, e, f, g, h, i, j) {" |
885 " return this.prefix + a + b + c + d + e + f + g + h + i + j;" | 885 " return this.prefix + a + b + c + d + e + f + g + h + i + j;" |
886 " }" | 886 " }" |
887 "})()"); | 887 "})()"); |
888 Handle<Object> return_val = callable(object).ToHandleChecked(); | 888 Handle<Object> return_val = callable(object).ToHandleChecked(); |
889 Handle<i::String> expected = | 889 Handle<i::String> expected = |
890 factory->NewStringFromAsciiChecked("prefix_abcdefghij"); | 890 factory->NewStringFromAsciiChecked("prefix_abcdefghij"); |
891 CHECK(i::String::cast(*return_val)->Equals(*expected)); | 891 CHECK(i::String::cast(*return_val)->Equals(*expected)); |
892 } | 892 } |
893 } | 893 } |
| 894 |
| 895 |
| 896 static BytecodeArrayBuilder& SetRegister(BytecodeArrayBuilder& builder, |
| 897 Register reg, int value, |
| 898 Register scratch) { |
| 899 return builder.StoreAccumulatorInRegister(scratch) |
| 900 .LoadLiteral(Smi::FromInt(value)) |
| 901 .StoreAccumulatorInRegister(reg) |
| 902 .LoadAccumulatorWithRegister(scratch); |
| 903 } |
| 904 |
| 905 |
| 906 static BytecodeArrayBuilder& IncrementRegister(BytecodeArrayBuilder& builder, |
| 907 Register reg, int value, |
| 908 Register scratch) { |
| 909 return builder.StoreAccumulatorInRegister(scratch) |
| 910 .LoadLiteral(Smi::FromInt(value)) |
| 911 .BinaryOperation(Token::Value::ADD, reg) |
| 912 .StoreAccumulatorInRegister(reg) |
| 913 .LoadAccumulatorWithRegister(scratch); |
| 914 } |
| 915 |
| 916 |
| 917 TEST(InterpreterJumps) { |
| 918 HandleAndZoneScope handles; |
| 919 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); |
| 920 builder.set_locals_count(2); |
| 921 builder.set_parameter_count(0); |
| 922 Register reg(0), scratch(1); |
| 923 BytecodeLabel label[3]; |
| 924 |
| 925 builder.LoadLiteral(Smi::FromInt(0)) |
| 926 .StoreAccumulatorInRegister(reg) |
| 927 .Jump(&label[1]); |
| 928 SetRegister(builder, reg, 1024, scratch).Bind(&label[0]); |
| 929 IncrementRegister(builder, reg, 1, scratch).Jump(&label[2]); |
| 930 SetRegister(builder, reg, 2048, scratch).Bind(&label[1]); |
| 931 IncrementRegister(builder, reg, 2, scratch).Jump(&label[0]); |
| 932 SetRegister(builder, reg, 4096, scratch).Bind(&label[2]); |
| 933 IncrementRegister(builder, reg, 4, scratch) |
| 934 .LoadAccumulatorWithRegister(reg) |
| 935 .Return(); |
| 936 |
| 937 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 938 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 939 auto callable = tester.GetCallable<>(); |
| 940 Handle<Object> return_value = callable().ToHandleChecked(); |
| 941 CHECK_EQ(Smi::cast(*return_value)->value(), 7); |
| 942 } |
| 943 |
| 944 |
| 945 TEST(InterpreterConditionalJumps) { |
| 946 HandleAndZoneScope handles; |
| 947 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); |
| 948 builder.set_locals_count(2); |
| 949 builder.set_parameter_count(0); |
| 950 Register reg(0), scratch(1); |
| 951 BytecodeLabel label[2]; |
| 952 BytecodeLabel done, done1; |
| 953 |
| 954 builder.LoadLiteral(Smi::FromInt(0)) |
| 955 .StoreAccumulatorInRegister(reg) |
| 956 .LoadFalse() |
| 957 .JumpIfFalse(&label[0]); |
| 958 IncrementRegister(builder, reg, 1024, scratch) |
| 959 .Bind(&label[0]) |
| 960 .LoadTrue() |
| 961 .JumpIfFalse(&done); |
| 962 IncrementRegister(builder, reg, 1, scratch).LoadTrue().JumpIfTrue(&label[1]); |
| 963 IncrementRegister(builder, reg, 2048, scratch).Bind(&label[1]); |
| 964 IncrementRegister(builder, reg, 2, scratch).LoadFalse().JumpIfTrue(&done1); |
| 965 IncrementRegister(builder, reg, 4, scratch) |
| 966 .LoadAccumulatorWithRegister(reg) |
| 967 .Bind(&done) |
| 968 .Bind(&done1) |
| 969 .Return(); |
| 970 |
| 971 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 972 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 973 auto callable = tester.GetCallable<>(); |
| 974 Handle<Object> return_value = callable().ToHandleChecked(); |
| 975 CHECK_EQ(Smi::cast(*return_value)->value(), 7); |
| 976 } |
OLD | NEW |