1 /*
  2  * Copyright (c) 1997, 2025, 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 #ifndef SHARE_ASM_ASSEMBLER_HPP
 26 #define SHARE_ASM_ASSEMBLER_HPP
 27 
 28 #include "asm/codeBuffer.hpp"
 29 #include "asm/register.hpp"
 30 #include "code/oopRecorder.hpp"
 31 #include "code/relocInfo.hpp"
 32 #include "memory/allocation.hpp"
 33 #include "utilities/checkedCast.hpp"
 34 #include "utilities/debug.hpp"
 35 #include "utilities/growableArray.hpp"
 36 #include "utilities/macros.hpp"
 37 
 38 // This file contains platform-independent assembler declarations.
 39 
 40 class MacroAssembler;
 41 class AbstractAssembler;
 42 class Label;
 43 
 44 /**
 45  * Labels represent destinations for control transfer instructions.  Such
 46  * instructions can accept a Label as their target argument.  A Label is
 47  * bound to the current location in the code stream by calling the
 48  * MacroAssembler's 'bind' method, which in turn calls the Label's 'bind'
 49  * method.  A Label may be referenced by an instruction before it's bound
 50  * (i.e., 'forward referenced').  'bind' stores the current code offset
 51  * in the Label object.
 52  *
 53  * If an instruction references a bound Label, the offset field(s) within
 54  * the instruction are immediately filled in based on the Label's code
 55  * offset.  If an instruction references an unbound label, that
 56  * instruction is put on a list of instructions that must be patched
 57  * (i.e., 'resolved') when the Label is bound.
 58  *
 59  * 'bind' will call the platform-specific 'patch_instruction' method to
 60  * fill in the offset field(s) for each unresolved instruction (if there
 61  * are any).  'patch_instruction' lives in one of the
 62  * cpu/<arch>/vm/assembler_<arch>* files.
 63  *
 64  * Instead of using a linked list of unresolved instructions, a Label has
 65  * an array of unresolved instruction code offsets.  _patch_index
 66  * contains the total number of forward references.  If the Label's array
 67  * overflows (i.e., _patch_index grows larger than the array size), a
 68  * GrowableArray is allocated to hold the remaining offsets.  (The cache
 69  * size is 4 for now, which handles over 99.5% of the cases)
 70  *
 71  * Labels may only be used within a single CodeSection.  If you need
 72  * to create references between code sections, use explicit relocations.
 73  */
 74 class Label {
 75  private:
 76   enum { PatchCacheSize = 4 DEBUG_ONLY( +4 ) };
 77 
 78   // _loc encodes both the binding state (via its sign)
 79   // and the binding locator (via its value) of a label.
 80   //
 81   // _loc >= 0   bound label, loc() encodes the target (jump) position
 82   // _loc == -1  unbound label
 83   int _loc;
 84 
 85   // References to instructions that jump to this unresolved label.
 86   // These instructions need to be patched when the label is bound
 87   // using the platform-specific patchInstruction() method.
 88   //
 89   // To avoid having to allocate from the C-heap each time, we provide
 90   // a local cache and use the overflow only if we exceed the local cache
 91   int _patches[PatchCacheSize];
 92   int _patch_index;
 93   GrowableArray<int>* _patch_overflow;
 94 
 95   NONCOPYABLE(Label);
 96  protected:
 97 
 98   // The label will be bound to a location near its users.
 99   bool _is_near;
100 
101 #ifdef ASSERT
102   // Sourcre file and line location of jump instruction
103   int _lines[PatchCacheSize];
104   const char* _files[PatchCacheSize];
105 #endif
106  public:
107 
108   /**
109    * After binding, be sure 'patch_instructions' is called later to link
110    */
111   void bind_loc(int loc) {
112     assert(loc >= 0, "illegal locator");
113     assert(_loc == -1, "already bound");
114     _loc = loc;
115   }
116   void bind_loc(int pos, int sect) { bind_loc(CodeBuffer::locator(pos, sect)); }
117 
118 #ifndef PRODUCT
119   // Iterates over all unresolved instructions for printing
120   void print_instructions(MacroAssembler* masm) const;
121 #endif // PRODUCT
122 
123   /**
124    * Returns the position of the Label in the code buffer
125    * The position is a 'locator', which encodes both offset and section.
126    */
127   int loc() const {
128     assert(_loc >= 0, "unbound label");
129     return _loc;
130   }
131   int loc_pos()  const { return CodeBuffer::locator_pos(loc()); }
132   int loc_sect() const { return CodeBuffer::locator_sect(loc()); }
133 
134   bool is_bound() const    { return _loc >=  0; }
135   bool is_unbound() const  { return _loc == -1 && _patch_index > 0; }
136   bool is_unused() const   { return _loc == -1 && _patch_index == 0; }
137 
138   // The label will be bound to a location near its users. Users can
139   // optimize on this information, e.g. generate short branches.
140   bool is_near()           { return _is_near; }
141 
142   /**
143    * Adds a reference to an unresolved displacement instruction to
144    * this unbound label
145    *
146    * @param cb         the code buffer being patched
147    * @param branch_loc the locator of the branch instruction in the code buffer
148    */
149   void add_patch_at(CodeBuffer* cb, int branch_loc, const char* file = nullptr, int line = 0);
150 
151   /**
152    * Iterate over the list of patches, resolving the instructions
153    * Call patch_instruction on each 'branch_loc' value
154    */
155   void patch_instructions(MacroAssembler* masm);
156 
157   void init() {
158     _loc = -1;
159     _patch_index = 0;
160     _patch_overflow = nullptr;
161     _is_near = false;
162   }
163 
164   Label() {
165     init();
166   }
167 
168   ~Label() {
169     assert(is_bound() || is_unused(), "Label was never bound to a location, but it was used as a jmp target");
170   }
171 
172   void reset() {
173     init(); //leave _patch_overflow because it points to CodeBuffer.
174   }
175 };
176 
177 // A NearLabel must be bound to a location near its users. Users can
178 // optimize on this information, e.g. generate short branches.
179 class NearLabel : public Label {
180  public:
181   NearLabel() : Label() { _is_near = true; }
182 };
183 
184 // A union type for code which has to assemble both constant and
185 // non-constant operands, when the distinction cannot be made
186 // statically.
187 class RegisterOrConstant {
188  private:
189   Register _r;
190   intptr_t _c;
191 
192  public:
193   RegisterOrConstant(): _r(noreg), _c(0) {}
194   RegisterOrConstant(Register r): _r(r), _c(0) {}
195   RegisterOrConstant(intptr_t c): _r(noreg), _c(c) {}
196 
197   Register as_register() const { assert(is_register(),""); return _r; }
198   intptr_t as_constant() const { assert(is_constant(),""); return _c; }
199 
200   Register register_or_noreg() const { return _r; }
201   intptr_t constant_or_zero() const  { return _c; }
202 
203   bool is_register() const { return _r != noreg; }
204   bool is_constant() const { return _r == noreg; }
205 };
206 
207 // The Abstract Assembler: Pure assembler doing NO optimizations on the
208 // instruction level; i.e., what you write is what you get.
209 // The Assembler is generating code into a CodeBuffer.
210 class AbstractAssembler : public ResourceObj  {
211   friend class Label;
212 
213  protected:
214   CodeSection* _code_section;          // section within the code buffer
215   OopRecorder* _oop_recorder;          // support for relocInfo::oop_type
216 
217  public:
218   // Code emission & accessing
219   address addr_at(int pos) const { return code_section()->start() + pos; }
220 
221  protected:
222   // This routine is called with a label is used for an address.
223   // Labels and displacements truck in offsets, but target must return a PC.
224   address target(Label& L)             { return code_section()->target(L, pc()); }
225 
226   bool is8bit(int x) const             { return -0x80 <= x && x < 0x80; }
227   bool isByte(int x) const             { return 0 <= x && x < 0x100; }
228   bool isShiftCount(int x) const       { return 0 <= x && x < 32; }
229 
230   // Mark instruction boundaries, this is required when emitting relocatable values.
231   // Basically, all instructions that directly or indirectly use Assembler::emit_data* methods.
232   class InstructionMark: public StackObj {
233    private:
234     AbstractAssembler* _assm;
235 
236    public:
237     InstructionMark(AbstractAssembler* assm) : _assm(assm) {
238       assert(assm->inst_mark() == nullptr, "overlapping instructions");
239       _assm->set_inst_mark();
240     }
241     ~InstructionMark() {
242       _assm->clear_inst_mark();
243     }
244   };
245   friend class InstructionMark;
246 
247  public:
248   // count size of instructions which are skipped from inline heuristics
249   class InlineSkippedInstructionsCounter: public StackObj {
250    private:
251     AbstractAssembler* _assm;
252     address _start;
253    public:
254     InlineSkippedInstructionsCounter(AbstractAssembler* assm) : _assm(assm), _start(assm->pc()) {
255     }
256     ~InlineSkippedInstructionsCounter() {
257       _assm->register_skipped(checked_cast<int>(_assm->pc() - _start));
258     }
259   };
260 
261  protected:
262 #ifdef ASSERT
263   // Make it return true on platforms which need to verify
264   // instruction boundaries for some operations.
265   static bool pd_check_instruction_mark();
266 
267   // Add delta to short branch distance to verify that it still fit into imm8.
268   int _short_branch_delta;
269 
270   int  short_branch_delta() const { return _short_branch_delta; }
271   void set_short_branch_delta()   { _short_branch_delta = 32; }
272   void clear_short_branch_delta() { _short_branch_delta = 0; }
273 
274   class ShortBranchVerifier: public StackObj {
275    private:
276     AbstractAssembler* _assm;
277 
278    public:
279     ShortBranchVerifier(AbstractAssembler* assm) : _assm(assm) {
280       assert(assm->short_branch_delta() == 0, "overlapping instructions");
281       _assm->set_short_branch_delta();
282     }
283     ~ShortBranchVerifier() {
284       _assm->clear_short_branch_delta();
285     }
286   };
287 #else
288   // Dummy in product.
289   class ShortBranchVerifier: public StackObj {
290    public:
291     ShortBranchVerifier(AbstractAssembler* assm) {}
292   };
293 #endif
294 
295   // sign-extended tolerant cast needed by callers of emit_int8 and emit_int16
296   // Some callers pass signed types that need to fit into the unsigned type so check
297   // that the range is correct.
298   template <typename T>
299   constexpr T narrow_cast(int x) const {
300     if (x < 0) {
301       using stype = std::make_signed_t<T>;
302       assert(x >= std::numeric_limits<stype>::min(), "too negative"); // >= -128 for 8 bits
303       return static_cast<T>(x);  // cut off sign bits
304     } else {
305       return checked_cast<T>(x);
306     }
307   }
308 
309  public:
310 
311   // Creation
312   AbstractAssembler(CodeBuffer* code);
313 
314   // ensure buf contains all code (call this before using/copying the code)
315   void flush();
316 
317   void emit_int8(       int x1)                                     { code_section()->emit_int8(narrow_cast<uint8_t>(x1)); }
318 
319   void emit_int16(       int x)                                     { code_section()->emit_int16(narrow_cast<uint16_t>(x)); }
320 
321   void emit_int16(      int x1,     int x2)                         { code_section()->emit_int16(narrow_cast<uint8_t>(x1),
322                                                                                                  narrow_cast<uint8_t>(x2)); }
323 
324   void emit_int24(      int x1,     int x2,     int x3)             { code_section()->emit_int24(narrow_cast<uint8_t>(x1),
325                                                                                                  narrow_cast<uint8_t>(x2),
326                                                                                                  narrow_cast<uint8_t>(x3)); }
327 
328   void emit_int32(  uint32_t x)                                     { code_section()->emit_int32(x); }
329   void emit_int32(      int x1,     int x2,     int x3,     int x4) { code_section()->emit_int32(narrow_cast<uint8_t>(x1),
330                                                                                                  narrow_cast<uint8_t>(x2),
331                                                                                                  narrow_cast<uint8_t>(x3),
332                                                                                                  narrow_cast<uint8_t>(x4)); }
333 
334   void emit_int64(  uint64_t x)                                     { code_section()->emit_int64(x); }
335 
336   void emit_float(  jfloat  x)                                      { code_section()->emit_float(x); }
337   void emit_double( jdouble x)                                      { code_section()->emit_double(x); }
338   void emit_address(address x)                                      { code_section()->emit_address(x); }
339 
340   enum { min_simm10 = -512 };
341 
342   // Test if x is within signed immediate range for width.
343   static bool is_simm(int64_t x, uint w) {
344     precond(1 < w && w < 64);
345     int64_t limes = INT64_C(1) << (w - 1);
346     return -limes <= x && x < limes;
347   }
348 
349   static bool is_simm8(int64_t x) { return is_simm(x, 8); }
350   static bool is_simm9(int64_t x) { return is_simm(x, 9); }
351   static bool is_simm10(int64_t x) { return is_simm(x, 10); }
352   static bool is_simm16(int64_t x) { return is_simm(x, 16); }
353   static bool is_simm32(int64_t x) { return is_simm(x, 32); }
354 
355   // Test if x is within unsigned immediate range for width.
356   static bool is_uimm(uint64_t x, uint w) {
357     precond(0 < w && w < 64);
358     uint64_t limes = UINT64_C(1) << w;
359     return x < limes;
360   }
361 
362   static bool is_uimm12(uint64_t x) { return is_uimm(x, 12); }
363   static bool is_uimm32(uint64_t x) { return is_uimm(x, 32); }
364 
365   // Accessors
366   CodeSection*  code_section() const   { return _code_section; }
367   CodeBuffer*   code()         const   { return code_section()->outer(); }
368   int           sect()         const   { return code_section()->index(); }
369   address       pc()           const   { return code_section()->end();   }
370   address       begin()        const   { return code_section()->start(); }
371   int           offset()       const   { return code_section()->size();  }
372   int           locator()      const   { return CodeBuffer::locator(offset(), sect()); }
373 
374   OopRecorder*  oop_recorder() const   { return _oop_recorder; }
375   void      set_oop_recorder(OopRecorder* r) { _oop_recorder = r; }
376 
377   void   register_skipped(int size) { code_section()->register_skipped(size); }
378 
379   address       inst_mark() const         { return code_section()->mark();          }
380   void      set_inst_mark()               {        code_section()->set_mark();      }
381   void      set_inst_mark(address addr)   {        code_section()->set_mark(addr);  }
382   void    clear_inst_mark()               {        code_section()->clear_mark();    }
383   void set_inst_end(address addr)         {        code_section()->set_end(addr);   }
384 
385   // Constants in code
386   void relocate(RelocationHolder const& rspec, int format = 0) {
387     assert(!pd_check_instruction_mark()
388         || inst_mark() == nullptr || inst_mark() == code_section()->end(),
389         "call relocate() between instructions");
390     code_section()->relocate(code_section()->end(), rspec, format);
391   }
392   void relocate(   relocInfo::relocType rtype, int format = 0) {
393     code_section()->relocate(code_section()->end(), rtype, format);
394   }
395   void relocate(address addr, relocInfo::relocType rtype, int format = 0) {
396     code_section()->relocate(addr, rtype, format);
397   }
398   void relocate(address addr, RelocationHolder const& rspec, int format = 0) {
399     code_section()->relocate(addr, rspec, format);
400   }
401 
402   static int code_fill_byte();         // used to pad out odd-sized code buffers
403 
404   // Associate a comment with the current offset.  It will be printed
405   // along with the disassembly when printing nmethods.  Currently
406   // only supported in the instruction section of the code buffer.
407   void block_comment(const char* comment);
408   // Copy str to a buffer that has the same lifetime as the CodeBuffer
409   const char* code_string(const char* str);
410 
411   // Label functions
412   void bind(Label& L); // binds an unbound label L to the current code position
413 
414   // Move to a different section in the same code buffer.
415   void set_code_section(CodeSection* cs);
416 
417   // Inform assembler when generating stub code and relocation info
418   address    start_a_stub(int required_space);
419   void       end_a_stub();
420   // Ditto for constants.
421   address    start_a_const(int required_space, int required_align = sizeof(double));
422   void       end_a_const(CodeSection* cs);  // Pass the codesection to continue in (insts or stubs?).
423 
424   // constants support
425   //
426   // We must remember the code section (insts or stubs) in c1
427   // so we can reset to the proper section in end_a_const().
428   address int_constant(jint c) {
429     CodeSection* c1 = _code_section;
430     address ptr = start_a_const(sizeof(c), sizeof(c));
431     if (ptr != nullptr) {
432       emit_int32(c);
433       end_a_const(c1);
434     }
435     return ptr;
436   }
437   address long_constant(jlong c) {
438     CodeSection* c1 = _code_section;
439     address ptr = start_a_const(sizeof(c), sizeof(c));
440     if (ptr != nullptr) {
441       emit_int64(c);
442       end_a_const(c1);
443     }
444     return ptr;
445   }
446   address double_constant(jdouble c) {
447     CodeSection* c1 = _code_section;
448     address ptr = start_a_const(sizeof(c), sizeof(c));
449     if (ptr != nullptr) {
450       emit_double(c);
451       end_a_const(c1);
452     }
453     return ptr;
454   }
455   address float_constant(jfloat c) {
456     CodeSection* c1 = _code_section;
457     address ptr = start_a_const(sizeof(c), sizeof(c));
458     if (ptr != nullptr) {
459       emit_float(c);
460       end_a_const(c1);
461     }
462     return ptr;
463   }
464   address address_constant(address c) {
465     CodeSection* c1 = _code_section;
466     address ptr = start_a_const(sizeof(c), sizeof(c));
467     if (ptr != nullptr) {
468       emit_address(c);
469       end_a_const(c1);
470     }
471     return ptr;
472   }
473   address address_constant(address c, RelocationHolder const& rspec) {
474     CodeSection* c1 = _code_section;
475     address ptr = start_a_const(sizeof(c), sizeof(c));
476     if (ptr != nullptr) {
477       relocate(rspec);
478       emit_address(c);
479       end_a_const(c1);
480     }
481     return ptr;
482   }
483   address array_constant(const GrowableArray<jbyte>* c, int alignment) {
484     CodeSection* c1 = _code_section;
485     address ptr = start_a_const(c->length(), alignment);
486     if (ptr != nullptr) {
487       for (int i = 0; i < c->length(); i++) {
488         emit_int8(c->at(i));
489       }
490       end_a_const(c1);
491     }
492     return ptr;
493   }
494 
495   // Bang stack to trigger StackOverflowError at a safe location
496   // implementation delegates to machine-specific bang_stack_with_offset
497   void generate_stack_overflow_check( int frame_size_in_bytes );
498   virtual void bang_stack_with_offset(int offset) = 0;
499 
500 
501   /**
502    * A platform-dependent method to patch a jump instruction that refers
503    * to this label.
504    *
505    * @param branch the location of the instruction to patch
506    * @param masm the assembler which generated the branch
507    */
508   void pd_patch_instruction(address branch, address target, const char* file, int line);
509 
510 };
511 
512 #include CPU_HEADER(assembler)
513 
514 #endif // SHARE_ASM_ASSEMBLER_HPP