1 /* 2 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "asm/assembler.hpp" 27 #include "asm/assembler.inline.hpp" 28 #include "ci/ciEnv.hpp" 29 #include "gc/shared/cardTableBarrierSet.hpp" 30 #include "gc/shared/collectedHeap.inline.hpp" 31 #include "interpreter/interpreter.hpp" 32 #include "interpreter/interpreterRuntime.hpp" 33 #include "interpreter/templateInterpreterGenerator.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "prims/jvm_misc.hpp" 36 #include "prims/methodHandles.hpp" 37 #include "runtime/biasedLocking.hpp" 38 #include "runtime/interfaceSupport.inline.hpp" 39 #include "runtime/objectMonitor.hpp" 40 #include "runtime/os.hpp" 41 #include "runtime/sharedRuntime.hpp" 42 #include "runtime/stubRoutines.hpp" 43 #include "utilities/hashtable.hpp" 44 #include "utilities/macros.hpp" 45 46 // Returns whether given imm has equal bit fields <0:size-1> and <size:2*size-1>. 47 inline bool Assembler::LogicalImmediate::has_equal_subpatterns(uintx imm, int size) { 48 uintx mask = right_n_bits(size); 49 uintx subpattern1 = mask_bits(imm, mask); 50 uintx subpattern2 = mask_bits(imm >> size, mask); 51 return subpattern1 == subpattern2; 52 } 53 54 // Returns least size that is a power of two from 2 to 64 with the proviso that given 55 // imm is composed of repeating patterns of this size. 56 inline int Assembler::LogicalImmediate::least_pattern_size(uintx imm) { 57 int size = BitsPerWord; 58 while (size > 2 && has_equal_subpatterns(imm, size >> 1)) { 59 size >>= 1; 60 } 61 return size; 62 } 63 64 // Returns count of set bits in given imm. Based on variable-precision SWAR algorithm. 65 inline int Assembler::LogicalImmediate::population_count(uintx x) { 66 x -= ((x >> 1) & 0x5555555555555555L); 67 x = (((x >> 2) & 0x3333333333333333L) + (x & 0x3333333333333333L)); 68 x = (((x >> 4) + x) & 0x0f0f0f0f0f0f0f0fL); 69 x += (x >> 8); 70 x += (x >> 16); 71 x += (x >> 32); 72 return(x & 0x7f); 73 } 74 75 // Let given x be <A:B> where B = 0 and least bit of A = 1. Returns <A:C>, where C is B-size set bits. 76 inline uintx Assembler::LogicalImmediate::set_least_zeroes(uintx x) { 77 return x | (x - 1); 78 } 79 80 81 #ifdef ASSERT 82 83 // Restores immediate by encoded bit masks. 84 uintx Assembler::LogicalImmediate::decode() { 85 assert (_encoded, "should be"); 86 87 int len_code = (_immN << 6) | ((~_imms) & 0x3f); 88 assert (len_code != 0, "should be"); 89 90 int len = 6; 91 while (!is_set_nth_bit(len_code, len)) len--; 92 int esize = 1 << len; 93 assert (len > 0, "should be"); 94 assert ((_is32bit ? 32 : 64) >= esize, "should be"); 95 96 int levels = right_n_bits(len); 97 int S = _imms & levels; 98 int R = _immr & levels; 99 100 assert (S != levels, "should be"); 101 102 uintx welem = right_n_bits(S + 1); 103 uintx wmask = (R == 0) ? welem : ((welem >> R) | (welem << (esize - R))); 104 105 for (int size = esize; size < 64; size <<= 1) { 106 wmask |= (wmask << size); 107 } 108 109 return wmask; 110 } 111 112 #endif 113 114 115 // Constructs LogicalImmediate by given imm. Figures out if given imm can be used in AArch64 logical 116 // instructions (AND, ANDS, EOR, ORR) and saves its encoding. 117 void Assembler::LogicalImmediate::construct(uintx imm, bool is32) { 118 _is32bit = is32; 119 120 if (is32) { 121 assert(((imm >> 32) == 0) || (((intx)imm >> 31) == -1), "32-bit immediate is out of range"); 122 123 // Replicate low 32 bits. 124 imm &= 0xffffffff; 125 imm |= imm << 32; 126 } 127 128 // All-zeroes and all-ones can not be encoded. 129 if (imm != 0 && (~imm != 0)) { 130 131 // Let LPS (least pattern size) be the least size (power of two from 2 to 64) of repeating 132 // patterns in the immediate. If immediate value can be encoded, it is encoded by pattern 133 // of exactly LPS size (due to structure of valid patterns). In order to verify 134 // that immediate value can be encoded, LPS is calculated and <LPS-1:0> bits of immediate 135 // are verified to be valid pattern. 136 int lps = least_pattern_size(imm); 137 uintx lps_mask = right_n_bits(lps); 138 139 // A valid pattern has one of the following forms: 140 // | 0 x A | 1 x B | 0 x C |, where B > 0 and C > 0, or 141 // | 1 x A | 0 x B | 1 x C |, where B > 0 and C > 0. 142 // For simplicity, the second form of the pattern is inverted into the first form. 143 bool inverted = imm & 0x1; 144 uintx pattern = (inverted ? ~imm : imm) & lps_mask; 145 146 // | 0 x A | 1 x (B + C) | 147 uintx without_least_zeroes = set_least_zeroes(pattern); 148 149 // Pattern is valid iff without least zeroes it is a power of two - 1. 150 if ((without_least_zeroes & (without_least_zeroes + 1)) == 0) { 151 152 // Count B as population count of pattern. 153 int bits_count = population_count(pattern); 154 155 // Count B+C as population count of pattern without least zeroes 156 int left_range = population_count(without_least_zeroes); 157 158 // S-prefix is a part of imms field which encodes LPS. 159 // LPS | S prefix 160 // 64 | not defined 161 // 32 | 0b0 162 // 16 | 0b10 163 // 8 | 0b110 164 // 4 | 0b1110 165 // 2 | 0b11110 166 int s_prefix = (lps == 64) ? 0 : ~set_least_zeroes(lps) & 0x3f; 167 168 // immN bit is set iff LPS == 64. 169 _immN = (lps == 64) ? 1 : 0; 170 assert (!is32 || (_immN == 0), "32-bit immediate should be encoded with zero N-bit"); 171 172 // immr is the rotation size. 173 _immr = lps + (inverted ? 0 : bits_count) - left_range; 174 175 // imms is the field that encodes bits count and S-prefix. 176 _imms = ((inverted ? (lps - bits_count) : bits_count) - 1) | s_prefix; 177 178 _encoded = true; 179 assert (decode() == imm, "illegal encoding"); 180 181 return; 182 } 183 } 184 185 _encoded = false; 186 }