assembler.hpp revision 605:98cb887364d3
1227825Stheraven/*
2227825Stheraven * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
3227825Stheraven * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4227825Stheraven *
5227825Stheraven * This code is free software; you can redistribute it and/or modify it
6227825Stheraven * under the terms of the GNU General Public License version 2 only, as
7227825Stheraven * published by the Free Software Foundation.
8227825Stheraven *
9227825Stheraven * This code is distributed in the hope that it will be useful, but WITHOUT
10227825Stheraven * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11227825Stheraven * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12227825Stheraven * version 2 for more details (a copy is included in the LICENSE file that
13227825Stheraven * accompanied this code).
14227825Stheraven *
15227825Stheraven * You should have received a copy of the GNU General Public License version
16227825Stheraven * 2 along with this work; if not, write to the Free Software Foundation,
17227825Stheraven * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18227825Stheraven *
19227825Stheraven * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20227825Stheraven * CA 95054 USA or visit www.sun.com if you need additional information or
21227825Stheraven * have any questions.
22227825Stheraven *
23227825Stheraven */
24227825Stheraven
25227825Stheraven// This file contains platform-independent assembler declarations.
26227825Stheraven
27227825Stheravenclass CodeBuffer;
28227825Stheravenclass MacroAssembler;
29227825Stheravenclass AbstractAssembler;
30227825Stheravenclass Label;
31227825Stheraven
32227825Stheraven/**
33227825Stheraven * Labels represent destinations for control transfer instructions.  Such
34227825Stheraven * instructions can accept a Label as their target argument.  A Label is
35227825Stheraven * bound to the current location in the code stream by calling the
36227825Stheraven * MacroAssembler's 'bind' method, which in turn calls the Label's 'bind'
37227825Stheraven * method.  A Label may be referenced by an instruction before it's bound
38227825Stheraven * (i.e., 'forward referenced').  'bind' stores the current code offset
39227825Stheraven * in the Label object.
40227825Stheraven *
41227825Stheraven * If an instruction references a bound Label, the offset field(s) within
42227825Stheraven * the instruction are immediately filled in based on the Label's code
43227825Stheraven * offset.  If an instruction references an unbound label, that
44227825Stheraven * instruction is put on a list of instructions that must be patched
45227825Stheraven * (i.e., 'resolved') when the Label is bound.
46227825Stheraven *
47227825Stheraven * 'bind' will call the platform-specific 'patch_instruction' method to
48227825Stheraven * fill in the offset field(s) for each unresolved instruction (if there
49227825Stheraven * are any).  'patch_instruction' lives in one of the
50227825Stheraven * cpu/<arch>/vm/assembler_<arch>* files.
51227825Stheraven *
52227825Stheraven * Instead of using a linked list of unresolved instructions, a Label has
53227825Stheraven * an array of unresolved instruction code offsets.  _patch_index
54227825Stheraven * contains the total number of forward references.  If the Label's array
55227825Stheraven * overflows (i.e., _patch_index grows larger than the array size), a
56227825Stheraven * GrowableArray is allocated to hold the remaining offsets.  (The cache
57227825Stheraven * size is 4 for now, which handles over 99.5% of the cases)
58227825Stheraven *
59227825Stheraven * Labels may only be used within a single CodeSection.  If you need
60227825Stheraven * to create references between code sections, use explicit relocations.
61227825Stheraven */
62227825Stheravenclass Label VALUE_OBJ_CLASS_SPEC {
63227825Stheraven private:
64227825Stheraven  enum { PatchCacheSize = 4 };
65227825Stheraven
66227825Stheraven  // _loc encodes both the binding state (via its sign)
67227825Stheraven  // and the binding locator (via its value) of a label.
68227825Stheraven  //
69227825Stheraven  // _loc >= 0   bound label, loc() encodes the target (jump) position
70227825Stheraven  // _loc == -1  unbound label
71227825Stheraven  int _loc;
72227825Stheraven
73227825Stheraven  // References to instructions that jump to this unresolved label.
74227825Stheraven  // These instructions need to be patched when the label is bound
75227825Stheraven  // using the platform-specific patchInstruction() method.
76227825Stheraven  //
77227825Stheraven  // To avoid having to allocate from the C-heap each time, we provide
78227825Stheraven  // a local cache and use the overflow only if we exceed the local cache
79227825Stheraven  int _patches[PatchCacheSize];
80227825Stheraven  int _patch_index;
81227825Stheraven  GrowableArray<int>* _patch_overflow;
82227825Stheraven
83227825Stheraven  Label(const Label&) { ShouldNotReachHere(); }
84227825Stheraven
85227825Stheraven public:
86227825Stheraven
87227825Stheraven  /**
88227825Stheraven   * After binding, be sure 'patch_instructions' is called later to link
89227825Stheraven   */
90227825Stheraven  void bind_loc(int loc) {
91227825Stheraven    assert(loc >= 0, "illegal locator");
92227825Stheraven    assert(_loc == -1, "already bound");
93227825Stheraven    _loc = loc;
94227825Stheraven  }
95227825Stheraven  void bind_loc(int pos, int sect);  // = bind_loc(locator(pos, sect))
96227825Stheraven
97227825Stheraven#ifndef PRODUCT
98227825Stheraven  // Iterates over all unresolved instructions for printing
99227825Stheraven  void print_instructions(MacroAssembler* masm) const;
100227825Stheraven#endif // PRODUCT
101227825Stheraven
102227825Stheraven  /**
103227825Stheraven   * Returns the position of the the Label in the code buffer
104227825Stheraven   * The position is a 'locator', which encodes both offset and section.
105227825Stheraven   */
106227825Stheraven  int loc() const {
107227825Stheraven    assert(_loc >= 0, "unbound label");
108227825Stheraven    return _loc;
109227825Stheraven  }
110227825Stheraven  int loc_pos() const;   // == locator_pos(loc())
111227825Stheraven  int loc_sect() const;  // == locator_sect(loc())
112227825Stheraven
113227825Stheraven  bool is_bound() const    { return _loc >=  0; }
114227825Stheraven  bool is_unbound() const  { return _loc == -1 && _patch_index > 0; }
115227825Stheraven  bool is_unused() const   { return _loc == -1 && _patch_index == 0; }
116227825Stheraven
117227825Stheraven  /**
118227825Stheraven   * Adds a reference to an unresolved displacement instruction to
119227825Stheraven   * this unbound label
120227825Stheraven   *
121227825Stheraven   * @param cb         the code buffer being patched
122227825Stheraven   * @param branch_loc the locator of the branch instruction in the code buffer
123227825Stheraven   */
124227825Stheraven  void add_patch_at(CodeBuffer* cb, int branch_loc);
125227825Stheraven
126227825Stheraven  /**
127227825Stheraven   * Iterate over the list of patches, resolving the instructions
128227825Stheraven   * Call patch_instruction on each 'branch_loc' value
129227825Stheraven   */
130227825Stheraven  void patch_instructions(MacroAssembler* masm);
131227825Stheraven
132227825Stheraven  void init() {
133227825Stheraven    _loc = -1;
134227825Stheraven    _patch_index = 0;
135227825Stheraven    _patch_overflow = NULL;
136227825Stheraven  }
137227825Stheraven
138227825Stheraven  Label() {
139227825Stheraven    init();
140227825Stheraven  }
141227825Stheraven};
142227825Stheraven
143227825Stheraven
144227825Stheraven// The Abstract Assembler: Pure assembler doing NO optimizations on the
145227825Stheraven// instruction level; i.e., what you write is what you get.
146227825Stheraven// The Assembler is generating code into a CodeBuffer.
147227825Stheravenclass AbstractAssembler : public ResourceObj  {
148227825Stheraven  friend class Label;
149227825Stheraven
150227825Stheraven protected:
151227825Stheraven  CodeSection* _code_section;          // section within the code buffer
152227825Stheraven  address      _code_begin;            // first byte of code buffer
153227825Stheraven  address      _code_limit;            // first byte after code buffer
154227825Stheraven  address      _code_pos;              // current code generation position
155227825Stheraven  OopRecorder* _oop_recorder;          // support for relocInfo::oop_type
156227825Stheraven
157227825Stheraven  // Code emission & accessing
158227825Stheraven  address addr_at(int pos) const       { return _code_begin + pos; }
159227825Stheraven
160227825Stheraven  // This routine is called with a label is used for an address.
161227825Stheraven  // Labels and displacements truck in offsets, but target must return a PC.
162227825Stheraven  address target(Label& L);            // return _code_section->target(L)
163227825Stheraven
164227825Stheraven  bool is8bit(int x) const             { return -0x80 <= x && x < 0x80; }
165227825Stheraven  bool isByte(int x) const             { return 0 <= x && x < 0x100; }
166227825Stheraven  bool isShiftCount(int x) const       { return 0 <= x && x < 32; }
167227825Stheraven
168227825Stheraven  void emit_byte(int x);  // emit a single byte
169227825Stheraven  void emit_word(int x);  // emit a 16-bit word (not a wordSize word!)
170227825Stheraven  void emit_long(jint x); // emit a 32-bit word (not a longSize word!)
171227825Stheraven  void emit_address(address x); // emit an address (not a longSize word!)
172227825Stheraven
173227825Stheraven  // Instruction boundaries (required when emitting relocatable values).
174227825Stheraven  class InstructionMark: public StackObj {
175227825Stheraven   private:
176227825Stheraven    AbstractAssembler* _assm;
177227825Stheraven
178227825Stheraven   public:
179227825Stheraven    InstructionMark(AbstractAssembler* assm) : _assm(assm) {
180227825Stheraven      assert(assm->inst_mark() == NULL, "overlapping instructions");
181227825Stheraven      _assm->set_inst_mark();
182227825Stheraven    }
183227825Stheraven    ~InstructionMark() {
184227825Stheraven      _assm->clear_inst_mark();
185227825Stheraven    }
186227825Stheraven  };
187227825Stheraven  friend class InstructionMark;
188227825Stheraven  #ifdef ASSERT
189227825Stheraven  // Make it return true on platforms which need to verify
190227825Stheraven  // instruction boundaries for some operations.
191227825Stheraven  inline static bool pd_check_instruction_mark();
192227825Stheraven  #endif
193227825Stheraven
194227825Stheraven  // Label functions
195227825Stheraven  void print(Label& L);
196227825Stheraven
197227825Stheraven public:
198227825Stheraven
199227825Stheraven  // Creation
200227825Stheraven  AbstractAssembler(CodeBuffer* code);
201227825Stheraven
202227825Stheraven  // save end pointer back to code buf.
203227825Stheraven  void sync();
204227825Stheraven
205227825Stheraven  // ensure buf contains all code (call this before using/copying the code)
206227825Stheraven  void flush();
207227825Stheraven
208227825Stheraven  // Accessors
209227825Stheraven  CodeBuffer*   code() const;          // _code_section->outer()
210227825Stheraven  CodeSection*  code_section() const   { return _code_section; }
211227825Stheraven  int           sect() const;          // return _code_section->index()
212227825Stheraven  address       pc() const             { return _code_pos; }
213227825Stheraven  int           offset() const         { return _code_pos - _code_begin; }
214227825Stheraven  int           locator() const;       // CodeBuffer::locator(offset(), sect())
215227825Stheraven  OopRecorder*  oop_recorder() const   { return _oop_recorder; }
216227825Stheraven  void      set_oop_recorder(OopRecorder* r) { _oop_recorder = r; }
217227825Stheraven
218227825Stheraven  address  inst_mark() const;
219227825Stheraven  void set_inst_mark();
220227825Stheraven  void clear_inst_mark();
221227825Stheraven
222227825Stheraven  // Constants in code
223227825Stheraven  void a_byte(int x);
224227825Stheraven  void a_long(jint x);
225227825Stheraven  void relocate(RelocationHolder const& rspec, int format = 0);
226227825Stheraven  void relocate(   relocInfo::relocType rtype, int format = 0) {
227227825Stheraven    if (rtype != relocInfo::none)
228227825Stheraven      relocate(Relocation::spec_simple(rtype), format);
229227825Stheraven  }
230227825Stheraven
231227825Stheraven  static int code_fill_byte();         // used to pad out odd-sized code buffers
232227825Stheraven
233227825Stheraven  // Associate a comment with the current offset.  It will be printed
234227825Stheraven  // along with the disassembly when printing nmethods.  Currently
235227825Stheraven  // only supported in the instruction section of the code buffer.
236227825Stheraven  void block_comment(const char* comment);
237227825Stheraven
238227825Stheraven  // Label functions
239227825Stheraven  void bind(Label& L); // binds an unbound label L to the current code position
240227825Stheraven
241227825Stheraven  // Move to a different section in the same code buffer.
242227825Stheraven  void set_code_section(CodeSection* cs);
243227825Stheraven
244227825Stheraven  // Inform assembler when generating stub code and relocation info
245227825Stheraven  address    start_a_stub(int required_space);
246227825Stheraven  void       end_a_stub();
247227825Stheraven  // Ditto for constants.
248227825Stheraven  address    start_a_const(int required_space, int required_align = sizeof(double));
249227825Stheraven  void       end_a_const();
250227825Stheraven
251227825Stheraven  // fp constants support
252227825Stheraven  address double_constant(jdouble c) {
253227825Stheraven    address ptr = start_a_const(sizeof(c), sizeof(c));
254227825Stheraven    if (ptr != NULL) {
255227825Stheraven      *(jdouble*)ptr = c;
256227825Stheraven      _code_pos = ptr + sizeof(c);
257227825Stheraven      end_a_const();
258227825Stheraven    }
259227825Stheraven    return ptr;
260227825Stheraven  }
261227825Stheraven  address float_constant(jfloat c) {
262227825Stheraven    address ptr = start_a_const(sizeof(c), sizeof(c));
263227825Stheraven    if (ptr != NULL) {
264227825Stheraven      *(jfloat*)ptr = c;
265227825Stheraven      _code_pos = ptr + sizeof(c);
266227825Stheraven      end_a_const();
267227825Stheraven    }
268227825Stheraven    return ptr;
269227825Stheraven  }
270227825Stheraven  address address_constant(address c, RelocationHolder const& rspec) {
271227825Stheraven    address ptr = start_a_const(sizeof(c), sizeof(c));
272227825Stheraven    if (ptr != NULL) {
273227825Stheraven      relocate(rspec);
274227825Stheraven      *(address*)ptr = c;
275227825Stheraven      _code_pos = ptr + sizeof(c);
276227825Stheraven      end_a_const();
277227825Stheraven    }
278227825Stheraven    return ptr;
279227825Stheraven  }
280227825Stheraven  inline address address_constant(Label& L);
281227825Stheraven  inline address address_table_constant(GrowableArray<Label*> label);
282227825Stheraven
283227825Stheraven  // Bang stack to trigger StackOverflowError at a safe location
284227825Stheraven  // implementation delegates to machine-specific bang_stack_with_offset
285227825Stheraven  void generate_stack_overflow_check( int frame_size_in_bytes );
286227825Stheraven  virtual void bang_stack_with_offset(int offset) = 0;
287227825Stheraven
288227825Stheraven
289227825Stheraven  /**
290227825Stheraven   * A platform-dependent method to patch a jump instruction that refers
291227825Stheraven   * to this label.
292227825Stheraven   *
293227825Stheraven   * @param branch the location of the instruction to patch
294227825Stheraven   * @param masm the assembler which generated the branch
295227825Stheraven   */
296227825Stheraven  void pd_patch_instruction(address branch, address target);
297227825Stheraven
298227825Stheraven#ifndef PRODUCT
299227825Stheraven  /**
300227825Stheraven   * Platform-dependent method of printing an instruction that needs to be
301227825Stheraven   * patched.
302227825Stheraven   *
303227825Stheraven   * @param branch the instruction to be patched in the buffer.
304227825Stheraven   */
305227825Stheraven  static void pd_print_patched_instruction(address branch);
306227825Stheraven#endif // PRODUCT
307227825Stheraven};
308227825Stheraven
309227825Stheraven#include "incls/_assembler_pd.hpp.incl"
310227825Stheraven