@@ -826,7 +826,7 @@ module array {
826826 assert(i >= 0);
827827 assert(i == stack_size - 2 || i == stack_size - 3);
828828
829- const elements: HeapObject = ReloadElements(sortState);
829+ let elements: HeapObject = ReloadElements(sortState);
830830 const Load: LoadFn = GetLoadFn(sortState);
831831
832832 const pending_runs: FixedArray =
@@ -859,6 +859,7 @@ module array {
859859 const k: Smi = CallGallopRight(
860860 context, sortState, Load, key_right, base_a, length_a, 0, False)
861861 otherwise Bailout;
862+ elements = ReloadElements(sortState);
862863 assert(k >= 0);
863864
864865 base_a = base_a + k;
@@ -874,6 +875,7 @@ module array {
874875 length_b = CallGallopLeft(
875876 context, sortState, Load, key_left, base_b, length_b, length_b - 1,
876877 False) otherwise Bailout;
878+ elements = ReloadElements(sortState);
877879 assert(length_b >= 0);
878880 if (length_b == 0) return kSuccess;
879881
@@ -893,6 +895,12 @@ module array {
893895 }
894896 }
895897
898+ macro LoadElementsOrTempArray(
899+ useTempArray: Boolean, sortState: FixedArray): HeapObject {
900+ return useTempArray == True ? GetTempArray(sortState) :
901+ ReloadElements(sortState);
902+ }
903+
896904 // Locates the proper position of key in a sorted array; if the array contains
897905 // an element equal to key, return the position immediately to the left of
898906 // the leftmost equal element. (GallopRight does the same except returns the
@@ -916,25 +924,17 @@ module array {
916924 assert(length > 0 && base >= 0);
917925 assert(0 <= hint && hint < length);
918926
919- // We cannot leave a pointer to elements on the stack (see comment at
920- // ReloadElements). For this reason we pass a flag whether to reload
921- // and which array to use.
922- let elements: HeapObject = useTempArray == True ? GetTempArray(sortState) :
923- ReloadElements(sortState);
924-
925927 let last_ofs: Smi = 0;
926928 let offset: Smi = 1;
927929
928930 try {
929- const base_hint_element: Object =
930- CallLoad(context, sortState, Load, elements, base + hint)
931+ const base_hint_element: Object = CallLoad(
932+ context, sortState, Load,
933+ LoadElementsOrTempArray(useTempArray, sortState), base + hint)
931934 otherwise Bailout;
932935 let order: Number =
933936 CallCompareFn(context, sortState, base_hint_element, key)
934937 otherwise Bailout;
935- if (useTempArray == False) {
936- elements = ReloadElements(sortState);
937- }
938938
939939 if (order < 0) {
940940 // a[base + hint] < key: gallop right, until
@@ -943,14 +943,13 @@ module array {
943943 // a[base + length - 1] is highest.
944944 let max_ofs: Smi = length - hint;
945945 while (offset < max_ofs) {
946- const offset_element: Object =
947- CallLoad(context, sortState, Load, elements, base + hint + offset)
946+ const offset_element: Object = CallLoad(
947+ context, sortState, Load,
948+ LoadElementsOrTempArray(useTempArray, sortState),
949+ base + hint + offset)
948950 otherwise Bailout;
949951 order = CallCompareFn(context, sortState, offset_element, key)
950952 otherwise Bailout;
951- if (useTempArray == False) {
952- elements = ReloadElements(sortState);
953- }
954953
955954 // a[base + hint + offset] >= key? Break.
956955 if (order >= 0) break;
@@ -975,14 +974,13 @@ module array {
975974 // a[base + hint] is lowest.
976975 let max_ofs: Smi = hint + 1;
977976 while (offset < max_ofs) {
978- const offset_element: Object =
979- CallLoad(context, sortState, Load, elements, base + hint - offset)
977+ const offset_element: Object = CallLoad(
978+ context, sortState, Load,
979+ LoadElementsOrTempArray(useTempArray, sortState),
980+ base + hint - offset)
980981 otherwise Bailout;
981982 order = CallCompareFn(context, sortState, offset_element, key)
982983 otherwise Bailout;
983- if (useTempArray == False) {
984- elements = ReloadElements(sortState);
985- }
986984
987985 if (order < 0) break;
988986
@@ -1011,14 +1009,12 @@ module array {
10111009 while (last_ofs < offset) {
10121010 const m: Smi = last_ofs + ((offset - last_ofs) >>> 1);
10131011
1014- const base_m_element: Object =
1015- CallLoad(context, sortState, Load, elements, base + m)
1012+ const base_m_element: Object = CallLoad(
1013+ context, sortState, Load,
1014+ LoadElementsOrTempArray(useTempArray, sortState), base + m)
10161015 otherwise Bailout;
10171016 order = CallCompareFn(context, sortState, base_m_element, key)
10181017 otherwise Bailout;
1019- if (useTempArray == False) {
1020- elements = ReloadElements(sortState);
1021- }
10221018
10231019 if (order < 0) {
10241020 last_ofs = m + 1; // a[base + m] < key.
@@ -1051,25 +1047,17 @@ module array {
10511047 assert(length > 0 && base >= 0);
10521048 assert(0 <= hint && hint < length);
10531049
1054- // We cannot leave a pointer to elements on the stack (see comment at
1055- // ReloadElements). For this reason we pass a flag whether to reload
1056- // and which array to use.
1057- let elements: HeapObject = useTempArray == True ? GetTempArray(sortState) :
1058- ReloadElements(sortState);
1059-
10601050 let last_ofs: Smi = 0;
10611051 let offset: Smi = 1;
10621052
10631053 try {
1064- const base_hint_element: Object =
1065- CallLoad(context, sortState, Load, elements, base + hint)
1054+ const base_hint_element: Object = CallLoad(
1055+ context, sortState, Load,
1056+ LoadElementsOrTempArray(useTempArray, sortState), base + hint)
10661057 otherwise Bailout;
10671058 let order: Number =
10681059 CallCompareFn(context, sortState, key, base_hint_element)
10691060 otherwise Bailout;
1070- if (useTempArray == False) {
1071- elements = ReloadElements(sortState);
1072- }
10731061
10741062 if (order < 0) {
10751063 // key < a[base + hint]: gallop left, until
@@ -1078,14 +1066,13 @@ module array {
10781066 // a[base + hint] is lowest.
10791067 let max_ofs: Smi = hint + 1;
10801068 while (offset < max_ofs) {
1081- const offset_element: Object =
1082- CallLoad(context, sortState, Load, elements, base + hint - offset)
1069+ const offset_element: Object = CallLoad(
1070+ context, sortState, Load,
1071+ LoadElementsOrTempArray(useTempArray, sortState),
1072+ base + hint - offset)
10831073 otherwise Bailout;
10841074 order = CallCompareFn(context, sortState, key, offset_element)
10851075 otherwise Bailout;
1086- if (useTempArray == False) {
1087- elements = ReloadElements(sortState);
1088- }
10891076
10901077 if (order >= 0) break;
10911078
@@ -1109,14 +1096,13 @@ module array {
11091096 // a[base + length - 1] is highest.
11101097 let max_ofs: Smi = length - hint;
11111098 while (offset < max_ofs) {
1112- const offset_element: Object =
1113- CallLoad(context, sortState, Load, elements, base + hint + offset)
1099+ const offset_element: Object = CallLoad(
1100+ context, sortState, Load,
1101+ LoadElementsOrTempArray(useTempArray, sortState),
1102+ base + hint + offset)
11141103 otherwise Bailout;
11151104 order = CallCompareFn(context, sortState, key, offset_element)
11161105 otherwise Bailout;
1117- if (useTempArray == False) {
1118- elements = ReloadElements(sortState);
1119- }
11201106
11211107 // a[base + hint + ofs] <= key.
11221108 if (order < 0) break;
@@ -1144,14 +1130,12 @@ module array {
11441130 while (last_ofs < offset) {
11451131 const m: Smi = last_ofs + ((offset - last_ofs) >>> 1);
11461132
1147- const base_m_element: Object =
1148- CallLoad(context, sortState, Load, elements, base + m)
1133+ const base_m_element: Object = CallLoad(
1134+ context, sortState, Load,
1135+ LoadElementsOrTempArray(useTempArray, sortState), base + m)
11491136 otherwise Bailout;
11501137 order = CallCompareFn(context, sortState, key, base_m_element)
11511138 otherwise Bailout;
1152- if (useTempArray == False) {
1153- elements = ReloadElements(sortState);
1154- }
11551139
11561140 if (order < 0) {
11571141 offset = m; // key < a[base + m].
@@ -1288,6 +1272,7 @@ module array {
12881272 nof_wins_a = CallGallopRight(
12891273 context, sortState, Load<TempArrayElements>, key_right,
12901274 cursor_temp, length_a, 0, True) otherwise Bailout;
1275+ elements = ReloadElements(sortState);
12911276 assert(nof_wins_a >= 0);
12921277
12931278 if (nof_wins_a > 0) {
@@ -1313,6 +1298,7 @@ module array {
13131298 context, sortState, LoadF, temp_array[cursor_temp], cursor_b,
13141299 length_b, 0, False)
13151300 otherwise Bailout;
1301+ elements = ReloadElements(sortState);
13161302 assert(nof_wins_b >= 0);
13171303 if (nof_wins_b > 0) {
13181304 CallCopyWithinSortArray(
@@ -1461,6 +1447,7 @@ module array {
14611447 context, sortState, LoadF, temp_array[cursor_temp], baseA,
14621448 length_a, length_a - 1, False)
14631449 otherwise Bailout;
1450+ elements = ReloadElements(sortState);
14641451 assert(k >= 0);
14651452 nof_wins_a = length_a - k;
14661453
@@ -1487,6 +1474,7 @@ module array {
14871474 k = CallGallopLeft(
14881475 context, sortState, Load<TempArrayElements>, key, 0, length_b,
14891476 length_b - 1, True) otherwise Bailout;
1477+ elements = ReloadElements(sortState);
14901478 assert(k >= 0);
14911479 nof_wins_b = length_b - k;
14921480
@@ -1743,8 +1731,7 @@ module array {
17431731 // 2. Let obj be ? ToObject(this value).
17441732 const obj: JSReceiver = ToObject(context, receiver);
17451733
1746- const sort_state: FixedArray =
1747- AllocateZeroedFixedArray(kSortStateSize);
1734+ const sort_state: FixedArray = AllocateZeroedFixedArray(kSortStateSize);
17481735 FillFixedArrayWithSmiZero(sort_state, SmiTag(kSortStateSize));
17491736
17501737 sort_state[kReceiverIdx] = obj;
0 commit comments