sharkCacheDecache.hpp 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/*
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2008, 2009 Red Hat, Inc.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

// Class hierarchy:
// - SharkStateScanner
//   - SharkCacherDecacher
//     - SharkDecacher
//       - SharkJavaCallDecacher
//       - SharkVMCallDecacher
//       - SharkTrapDecacher
//     - SharkCacher
//       - SharkJavaCallCacher
//       - SharkVMCallCacher
//       - SharkFunctionEntryCacher
//         - SharkNormalEntryCacher
//         - SharkOSREntryCacher

class SharkCacherDecacher : public SharkStateScanner {
 protected:
  SharkCacherDecacher(SharkFunction* function)
    : SharkStateScanner(function) {}

  // Helper
 protected:
  static int adjusted_offset(SharkValue* value, int offset) {
    if (value->is_two_word())
      offset--;
    return offset;
  }
};

class SharkDecacher : public SharkCacherDecacher {
 protected:
  SharkDecacher(SharkFunction* function, int bci)
    : SharkCacherDecacher(function), _bci(bci) {}

 private:
  int _bci;

 protected:
  int bci() const {
    return _bci;
  }

 private:
  int                           _pc_offset;
  OopMap*                       _oopmap;
  GrowableArray<ScopeValue*>*   _exparray;
  GrowableArray<MonitorValue*>* _monarray;
  GrowableArray<ScopeValue*>*   _locarray;

 private:
  int pc_offset() const {
    return _pc_offset;
  }
  OopMap* oopmap() const {
    return _oopmap;
  }
  GrowableArray<ScopeValue*>* exparray() const {
    return _exparray;
  }
  GrowableArray<MonitorValue*>* monarray() const {
    return _monarray;
  }
  GrowableArray<ScopeValue*>* locarray() const {
    return _locarray;
  }

  // Callbacks
 protected:
  void start_frame();

  void start_stack(int stack_depth);
  void process_stack_slot(int index, SharkValue** value, int offset);

  void start_monitors(int num_monitors);
  void process_monitor(int index, int box_offset, int obj_offset);

  void process_oop_tmp_slot(llvm::Value** value, int offset);
  void process_method_slot(llvm::Value** value, int offset);
  void process_pc_slot(int offset);

  void start_locals();
  void process_local_slot(int index, SharkValue** value, int offset);

  void end_frame();

  // oopmap and debuginfo helpers
 private:
  static int oopmap_slot_munge(int offset) {
    return SharkStack::oopmap_slot_munge(offset);
  }
  static VMReg slot2reg(int offset) {
    return SharkStack::slot2reg(offset);
  }
  static Location slot2loc(int offset, Location::Type type) {
    return Location::new_stk_loc(type, offset * wordSize);
  }
  static LocationValue* slot2lv(int offset, Location::Type type) {
    return new LocationValue(slot2loc(offset, type));
  }
  static Location::Type location_type(SharkValue** addr, bool maybe_two_word) {
    // low addresses this end
    //                           Type       32-bit    64-bit
    //   ----------------------------------------------------
    //   stack[0]    local[3]    jobject    oop       oop
    //   stack[1]    local[2]    NULL       normal    lng
    //   stack[2]    local[1]    jlong      normal    invalid
    //   stack[3]    local[0]    jint       normal    normal
    //
    // high addresses this end

    SharkValue *value = *addr;
    if (value) {
      if (value->is_jobject())
        return Location::oop;
#ifdef _LP64
      if (value->is_two_word())
        return Location::invalid;
#endif // _LP64
      return Location::normal;
    }
    else {
      if (maybe_two_word) {
        value = *(addr - 1);
        if (value && value->is_two_word()) {
#ifdef _LP64
          if (value->is_jlong())
            return Location::lng;
          if (value->is_jdouble())
            return Location::dbl;
          ShouldNotReachHere();
#else
          return Location::normal;
#endif // _LP64
        }
      }
      return Location::invalid;
    }
  }

  // Stack slot helpers
 protected:
  virtual bool stack_slot_needs_write(int index, SharkValue* value) = 0;
  virtual bool stack_slot_needs_oopmap(int index, SharkValue* value) = 0;
  virtual bool stack_slot_needs_debuginfo(int index, SharkValue* value) = 0;

  static Location::Type stack_location_type(int index, SharkValue** addr) {
    return location_type(addr, *addr == NULL);
  }

  // Local slot helpers
 protected:
  virtual bool local_slot_needs_write(int index, SharkValue* value) = 0;
  virtual bool local_slot_needs_oopmap(int index, SharkValue* value) = 0;
  virtual bool local_slot_needs_debuginfo(int index, SharkValue* value) = 0;

  static Location::Type local_location_type(int index, SharkValue** addr) {
    return location_type(addr, index > 0);
  }

  // Writer helper
 protected:
  void write_value_to_frame(const llvm::Type* type,
                            llvm::Value*      value,
                            int               offset);
};

class SharkJavaCallDecacher : public SharkDecacher {
 public:
  SharkJavaCallDecacher(SharkFunction* function, int bci, ciMethod* callee)
    : SharkDecacher(function, bci), _callee(callee) {}

 private:
  ciMethod* _callee;

 protected:
  ciMethod* callee() const {
    return _callee;
  }

  // Stack slot helpers
 protected:
  bool stack_slot_needs_write(int index, SharkValue* value) {
    return value && (index < callee()->arg_size() || value->is_jobject());
  }
  bool stack_slot_needs_oopmap(int index, SharkValue* value) {
    return value && value->is_jobject() && index >= callee()->arg_size();
  }
  bool stack_slot_needs_debuginfo(int index, SharkValue* value) {
    return index >= callee()->arg_size();
  }

  // Local slot helpers
 protected:
  bool local_slot_needs_write(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool local_slot_needs_oopmap(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool local_slot_needs_debuginfo(int index, SharkValue* value) {
    return true;
  }
};

class SharkVMCallDecacher : public SharkDecacher {
 public:
  SharkVMCallDecacher(SharkFunction* function, int bci)
    : SharkDecacher(function, bci) {}

  // Stack slot helpers
 protected:
  bool stack_slot_needs_write(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool stack_slot_needs_oopmap(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool stack_slot_needs_debuginfo(int index, SharkValue* value) {
    return true;
  }

  // Local slot helpers
 protected:
  bool local_slot_needs_write(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool local_slot_needs_oopmap(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool local_slot_needs_debuginfo(int index, SharkValue* value) {
    return true;
  }
};

class SharkTrapDecacher : public SharkDecacher {
 public:
  SharkTrapDecacher(SharkFunction* function, int bci)
    : SharkDecacher(function, bci) {}

  // Stack slot helpers
 protected:
  bool stack_slot_needs_write(int index, SharkValue* value) {
    return value != NULL;
  }
  bool stack_slot_needs_oopmap(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool stack_slot_needs_debuginfo(int index, SharkValue* value) {
    return true;
  }

  // Local slot helpers
 protected:
  bool local_slot_needs_write(int index, SharkValue* value) {
    return value != NULL;
  }
  bool local_slot_needs_oopmap(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
  bool local_slot_needs_debuginfo(int index, SharkValue* value) {
    return true;
  }
};

class SharkCacher : public SharkCacherDecacher {
 protected:
  SharkCacher(SharkFunction* function)
    : SharkCacherDecacher(function) {}

  // Callbacks
 protected:
  void process_stack_slot(int index, SharkValue** value, int offset);

  void process_oop_tmp_slot(llvm::Value** value, int offset);
  virtual void process_method_slot(llvm::Value** value, int offset);

  virtual void process_local_slot(int index, SharkValue** value, int offset);

  // Stack slot helper
 protected:
  virtual bool stack_slot_needs_read(int index, SharkValue* value) = 0;

  // Local slot helper
 protected:
  virtual bool local_slot_needs_read(int index, SharkValue* value) {
    return value && value->is_jobject();
  }

  // Writer helper
 protected:
  llvm::Value* read_value_from_frame(const llvm::Type* type, int offset);
};

class SharkJavaCallCacher : public SharkCacher {
 public:
  SharkJavaCallCacher(SharkFunction* function, ciMethod* callee)
    : SharkCacher(function), _callee(callee) {}

 private:
  ciMethod* _callee;

 protected:
  ciMethod* callee() const {
    return _callee;
  }

  // Stack slot helper
 protected:
  bool stack_slot_needs_read(int index, SharkValue* value) {
    return value && (index < callee()->return_type()->size() ||
                     value->is_jobject());
  }
};

class SharkVMCallCacher : public SharkCacher {
 public:
  SharkVMCallCacher(SharkFunction* function)
    : SharkCacher(function) {}

  // Stack slot helper
 protected:
  bool stack_slot_needs_read(int index, SharkValue* value) {
    return value && value->is_jobject();
  }
};

class SharkFunctionEntryCacher : public SharkCacher {
 public:
  SharkFunctionEntryCacher(SharkFunction* function, llvm::Value* method)
    : SharkCacher(function), _method(method) {}

 private:
  llvm::Value* _method;

 private:
  llvm::Value* method() const {
    return _method;
  }

  // Method slot callback
 protected:
  void process_method_slot(llvm::Value** value, int offset);

  // Stack slot helper
 protected:
  bool stack_slot_needs_read(int index, SharkValue* value) {
    ShouldNotReachHere(); // entry block shouldn't have stack
  }

  // Local slot helper
 protected:
  bool local_slot_needs_read(int index, SharkValue* value) {
    return value != NULL;
  }
};

class SharkNormalEntryCacher : public SharkFunctionEntryCacher {
 public:
  SharkNormalEntryCacher(SharkFunction* function, llvm::Value* method)
    : SharkFunctionEntryCacher(function, method) {}
};

class SharkOSREntryCacher : public SharkFunctionEntryCacher {
 public:
  SharkOSREntryCacher(SharkFunction* function,
                      llvm::Value*   method,
                      llvm::Value*   osr_buf)
    : SharkFunctionEntryCacher(function, method),
      _osr_buf(
        builder()->CreateBitCast(
          osr_buf,
          llvm::PointerType::getUnqual(
            llvm::ArrayType::get(
              SharkType::intptr_type(),
              max_locals() + max_monitors() * 2)))) {}

 private:
  llvm::Value* _osr_buf;

 private:
  llvm::Value* osr_buf() const {
    return _osr_buf;
  }

  // Callbacks
 protected:
  void process_monitor(int index, int box_offset, int obj_offset);
  void process_local_slot(int index, SharkValue** value, int offset);

  // Helper
 private:
  llvm::Value* CreateAddressOfOSRBufEntry(int offset, const llvm::Type* type);
};