Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/asm/assembler.hpp
32285 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_ASM_ASSEMBLER_HPP25#define SHARE_VM_ASM_ASSEMBLER_HPP2627#include "asm/codeBuffer.hpp"28#include "code/oopRecorder.hpp"29#include "code/relocInfo.hpp"30#include "memory/allocation.hpp"31#include "utilities/debug.hpp"32#include "utilities/growableArray.hpp"33#include "utilities/top.hpp"3435#ifdef TARGET_ARCH_x8636# include "register_x86.hpp"37# include "vm_version_x86.hpp"38#endif39#ifdef TARGET_ARCH_sparc40# include "register_sparc.hpp"41# include "vm_version_sparc.hpp"42#endif43#ifdef TARGET_ARCH_zero44# include "register_zero.hpp"45# include "vm_version_zero.hpp"46#endif47#ifdef TARGET_ARCH_arm48# include "register_arm.hpp"49# include "vm_version_arm.hpp"50#endif51#ifdef TARGET_ARCH_ppc52# include "register_ppc.hpp"53# include "vm_version_ppc.hpp"54#endif55#ifdef TARGET_ARCH_aarch3256# include "register_aarch32.hpp"57# include "vm_version_aarch32.hpp"58#endif59#ifdef TARGET_ARCH_aarch6460# include "register_aarch64.hpp"61# include "vm_version_aarch64.hpp"62#endif6364// This file contains platform-independent assembler declarations.6566class MacroAssembler;67class AbstractAssembler;68class Label;6970/**71* Labels represent destinations for control transfer instructions. Such72* instructions can accept a Label as their target argument. A Label is73* bound to the current location in the code stream by calling the74* MacroAssembler's 'bind' method, which in turn calls the Label's 'bind'75* method. A Label may be referenced by an instruction before it's bound76* (i.e., 'forward referenced'). 'bind' stores the current code offset77* in the Label object.78*79* If an instruction references a bound Label, the offset field(s) within80* the instruction are immediately filled in based on the Label's code81* offset. If an instruction references an unbound label, that82* instruction is put on a list of instructions that must be patched83* (i.e., 'resolved') when the Label is bound.84*85* 'bind' will call the platform-specific 'patch_instruction' method to86* fill in the offset field(s) for each unresolved instruction (if there87* are any). 'patch_instruction' lives in one of the88* cpu/<arch>/vm/assembler_<arch>* files.89*90* Instead of using a linked list of unresolved instructions, a Label has91* an array of unresolved instruction code offsets. _patch_index92* contains the total number of forward references. If the Label's array93* overflows (i.e., _patch_index grows larger than the array size), a94* GrowableArray is allocated to hold the remaining offsets. (The cache95* size is 4 for now, which handles over 99.5% of the cases)96*97* Labels may only be used within a single CodeSection. If you need98* to create references between code sections, use explicit relocations.99*/100class Label VALUE_OBJ_CLASS_SPEC {101private:102enum { PatchCacheSize = 4 };103104// _loc encodes both the binding state (via its sign)105// and the binding locator (via its value) of a label.106//107// _loc >= 0 bound label, loc() encodes the target (jump) position108// _loc == -1 unbound label109int _loc;110111// References to instructions that jump to this unresolved label.112// These instructions need to be patched when the label is bound113// using the platform-specific patchInstruction() method.114//115// To avoid having to allocate from the C-heap each time, we provide116// a local cache and use the overflow only if we exceed the local cache117int _patches[PatchCacheSize];118int _patch_index;119GrowableArray<int>* _patch_overflow;120121Label(const Label&) { ShouldNotReachHere(); }122123public:124125/**126* After binding, be sure 'patch_instructions' is called later to link127*/128void bind_loc(int loc) {129assert(loc >= 0, "illegal locator");130assert(_loc == -1, "already bound");131_loc = loc;132}133void bind_loc(int pos, int sect) { bind_loc(CodeBuffer::locator(pos, sect)); }134135#ifndef PRODUCT136// Iterates over all unresolved instructions for printing137void print_instructions(MacroAssembler* masm) const;138#endif // PRODUCT139140/**141* Returns the position of the the Label in the code buffer142* The position is a 'locator', which encodes both offset and section.143*/144int loc() const {145assert(_loc >= 0, "unbound label");146return _loc;147}148int loc_pos() const { return CodeBuffer::locator_pos(loc()); }149int loc_sect() const { return CodeBuffer::locator_sect(loc()); }150151bool is_bound() const { return _loc >= 0; }152bool is_unbound() const { return _loc == -1 && _patch_index > 0; }153bool is_unused() const { return _loc == -1 && _patch_index == 0; }154155/**156* Adds a reference to an unresolved displacement instruction to157* this unbound label158*159* @param cb the code buffer being patched160* @param branch_loc the locator of the branch instruction in the code buffer161*/162void add_patch_at(CodeBuffer* cb, int branch_loc);163164/**165* Iterate over the list of patches, resolving the instructions166* Call patch_instruction on each 'branch_loc' value167*/168void patch_instructions(MacroAssembler* masm);169170void init() {171_loc = -1;172_patch_index = 0;173_patch_overflow = NULL;174}175176Label() {177init();178}179180~Label() {181assert(is_bound() || is_unused(), "Label was never bound to a location, but it was used as a jmp target");182}183184void reset() {185init(); //leave _patch_overflow because it points to CodeBuffer.186}187};188189// A union type for code which has to assemble both constant and190// non-constant operands, when the distinction cannot be made191// statically.192class RegisterOrConstant VALUE_OBJ_CLASS_SPEC {193private:194Register _r;195intptr_t _c;196197public:198RegisterOrConstant(): _r(noreg), _c(0) {}199RegisterOrConstant(Register r): _r(r), _c(0) {}200RegisterOrConstant(intptr_t c): _r(noreg), _c(c) {}201202Register as_register() const { assert(is_register(),""); return _r; }203intptr_t as_constant() const { assert(is_constant(),""); return _c; }204205Register register_or_noreg() const { return _r; }206intptr_t constant_or_zero() const { return _c; }207208bool is_register() const { return _r != noreg; }209bool is_constant() const { return _r == noreg; }210};211212// The Abstract Assembler: Pure assembler doing NO optimizations on the213// instruction level; i.e., what you write is what you get.214// The Assembler is generating code into a CodeBuffer.215class AbstractAssembler : public ResourceObj {216friend class Label;217218protected:219CodeSection* _code_section; // section within the code buffer220OopRecorder* _oop_recorder; // support for relocInfo::oop_type221222public:223// Code emission & accessing224address addr_at(int pos) const { return code_section()->start() + pos; }225226protected:227// This routine is called with a label is used for an address.228// Labels and displacements truck in offsets, but target must return a PC.229address target(Label& L) { return code_section()->target(L, pc()); }230231bool is8bit(int x) const { return -0x80 <= x && x < 0x80; }232bool isByte(int x) const { return 0 <= x && x < 0x100; }233bool isShiftCount(int x) const { return 0 <= x && x < 32; }234235// Instruction boundaries (required when emitting relocatable values).236class InstructionMark: public StackObj {237private:238AbstractAssembler* _assm;239240public:241InstructionMark(AbstractAssembler* assm) : _assm(assm) {242assert(assm->inst_mark() == NULL, "overlapping instructions");243_assm->set_inst_mark();244}245~InstructionMark() {246_assm->clear_inst_mark();247}248};249friend class InstructionMark;250#ifdef ASSERT251// Make it return true on platforms which need to verify252// instruction boundaries for some operations.253static bool pd_check_instruction_mark();254255// Add delta to short branch distance to verify that it still fit into imm8.256int _short_branch_delta;257258int short_branch_delta() const { return _short_branch_delta; }259void set_short_branch_delta() { _short_branch_delta = 32; }260void clear_short_branch_delta() { _short_branch_delta = 0; }261262class ShortBranchVerifier: public StackObj {263private:264AbstractAssembler* _assm;265266public:267ShortBranchVerifier(AbstractAssembler* assm) : _assm(assm) {268assert(assm->short_branch_delta() == 0, "overlapping instructions");269_assm->set_short_branch_delta();270}271~ShortBranchVerifier() {272_assm->clear_short_branch_delta();273}274};275#else276// Dummy in product.277class ShortBranchVerifier: public StackObj {278public:279ShortBranchVerifier(AbstractAssembler* assm) {}280};281#endif282283public:284285// Creation286AbstractAssembler(CodeBuffer* code);287288// ensure buf contains all code (call this before using/copying the code)289void flush();290291void emit_int8( int8_t x) { code_section()->emit_int8( x); }292void emit_int16( int16_t x) { code_section()->emit_int16( x); }293void emit_int32( int32_t x) { code_section()->emit_int32( x); }294void emit_int64( int64_t x) { code_section()->emit_int64( x); }295296void emit_float( jfloat x) { code_section()->emit_float( x); }297void emit_double( jdouble x) { code_section()->emit_double( x); }298void emit_address(address x) { code_section()->emit_address(x); }299300// min and max values for signed immediate ranges301static int min_simm(int nbits) { return -(intptr_t(1) << (nbits - 1)) ; }302static int max_simm(int nbits) { return (intptr_t(1) << (nbits - 1)) - 1; }303304// Define some:305static int min_simm10() { return min_simm(10); }306static int min_simm13() { return min_simm(13); }307static int min_simm16() { return min_simm(16); }308309// Test if x is within signed immediate range for nbits310static bool is_simm(intptr_t x, int nbits) { return min_simm(nbits) <= x && x <= max_simm(nbits); }311312// Define some:313static bool is_simm5( intptr_t x) { return is_simm(x, 5 ); }314static bool is_simm8( intptr_t x) { return is_simm(x, 8 ); }315static bool is_simm10(intptr_t x) { return is_simm(x, 10); }316static bool is_simm11(intptr_t x) { return is_simm(x, 11); }317static bool is_simm12(intptr_t x) { return is_simm(x, 12); }318static bool is_simm13(intptr_t x) { return is_simm(x, 13); }319static bool is_simm16(intptr_t x) { return is_simm(x, 16); }320static bool is_simm26(intptr_t x) { return is_simm(x, 26); }321static bool is_simm32(intptr_t x) { return is_simm(x, 32); }322323// Accessors324CodeSection* code_section() const { return _code_section; }325CodeBuffer* code() const { return code_section()->outer(); }326int sect() const { return code_section()->index(); }327address pc() const { return code_section()->end(); }328int offset() const { return code_section()->size(); }329int locator() const { return CodeBuffer::locator(offset(), sect()); }330331OopRecorder* oop_recorder() const { return _oop_recorder; }332void set_oop_recorder(OopRecorder* r) { _oop_recorder = r; }333334address inst_mark() const { return code_section()->mark(); }335void set_inst_mark() { code_section()->set_mark(); }336void clear_inst_mark() { code_section()->clear_mark(); }337338// Constants in code339void relocate(RelocationHolder const& rspec, int format = 0) {340assert(!pd_check_instruction_mark()341|| inst_mark() == NULL || inst_mark() == code_section()->end(),342"call relocate() between instructions");343code_section()->relocate(code_section()->end(), rspec, format);344}345void relocate( relocInfo::relocType rtype, int format = 0) {346code_section()->relocate(code_section()->end(), rtype, format);347}348349static int code_fill_byte(); // used to pad out odd-sized code buffers350351// Associate a comment with the current offset. It will be printed352// along with the disassembly when printing nmethods. Currently353// only supported in the instruction section of the code buffer.354void block_comment(const char* comment);355// Copy str to a buffer that has the same lifetime as the CodeBuffer356const char* code_string(const char* str);357358// Label functions359void bind(Label& L); // binds an unbound label L to the current code position360361// Move to a different section in the same code buffer.362void set_code_section(CodeSection* cs);363364// Inform assembler when generating stub code and relocation info365address start_a_stub(int required_space);366void end_a_stub();367// Ditto for constants.368address start_a_const(int required_space, int required_align = sizeof(double));369void end_a_const(CodeSection* cs); // Pass the codesection to continue in (insts or stubs?).370371// constants support372//373// We must remember the code section (insts or stubs) in c1374// so we can reset to the proper section in end_a_const().375address long_constant(jlong c) {376CodeSection* c1 = _code_section;377address ptr = start_a_const(sizeof(c), sizeof(c));378if (ptr != NULL) {379emit_int64(c);380end_a_const(c1);381}382return ptr;383}384address double_constant(jdouble c) {385CodeSection* c1 = _code_section;386address ptr = start_a_const(sizeof(c), sizeof(c));387if (ptr != NULL) {388emit_double(c);389end_a_const(c1);390}391return ptr;392}393address float_constant(jfloat c) {394CodeSection* c1 = _code_section;395address ptr = start_a_const(sizeof(c), sizeof(c));396if (ptr != NULL) {397emit_float(c);398end_a_const(c1);399}400return ptr;401}402address address_constant(address c) {403CodeSection* c1 = _code_section;404address ptr = start_a_const(sizeof(c), sizeof(c));405if (ptr != NULL) {406emit_address(c);407end_a_const(c1);408}409return ptr;410}411address address_constant(address c, RelocationHolder const& rspec) {412CodeSection* c1 = _code_section;413address ptr = start_a_const(sizeof(c), sizeof(c));414if (ptr != NULL) {415relocate(rspec);416emit_address(c);417end_a_const(c1);418}419return ptr;420}421422// Bootstrapping aid to cope with delayed determination of constants.423// Returns a static address which will eventually contain the constant.424// The value zero (NULL) stands instead of a constant which is still uncomputed.425// Thus, the eventual value of the constant must not be zero.426// This is fine, since this is designed for embedding object field427// offsets in code which must be generated before the object class is loaded.428// Field offsets are never zero, since an object's header (mark word)429// is located at offset zero.430RegisterOrConstant delayed_value(int(*value_fn)(), Register tmp, int offset = 0);431RegisterOrConstant delayed_value(address(*value_fn)(), Register tmp, int offset = 0);432virtual RegisterOrConstant delayed_value_impl(intptr_t* delayed_value_addr, Register tmp, int offset) = 0;433// Last overloading is platform-dependent; look in assembler_<arch>.cpp.434static intptr_t* delayed_value_addr(int(*constant_fn)());435static intptr_t* delayed_value_addr(address(*constant_fn)());436static void update_delayed_values();437438// Bang stack to trigger StackOverflowError at a safe location439// implementation delegates to machine-specific bang_stack_with_offset440void generate_stack_overflow_check( int frame_size_in_bytes );441virtual void bang_stack_with_offset(int offset) = 0;442443444/**445* A platform-dependent method to patch a jump instruction that refers446* to this label.447*448* @param branch the location of the instruction to patch449* @param masm the assembler which generated the branch450*/451void pd_patch_instruction(address branch, address target);452453};454455#ifdef TARGET_ARCH_x86456# include "assembler_x86.hpp"457#endif458#ifdef TARGET_ARCH_aarch32459# include "assembler_aarch32.hpp"460#endif461#ifdef TARGET_ARCH_aarch64462# include "assembler_aarch64.hpp"463#endif464#ifdef TARGET_ARCH_sparc465# include "assembler_sparc.hpp"466#endif467#ifdef TARGET_ARCH_zero468# include "assembler_zero.hpp"469#endif470#ifdef TARGET_ARCH_arm471# include "assembler_arm.hpp"472#endif473#ifdef TARGET_ARCH_ppc474# include "assembler_ppc.hpp"475#endif476477478#endif // SHARE_VM_ASM_ASSEMBLER_HPP479480481