blob: dcca3bcc331e1fdc22624243dfdb48cab91c8eda [file] [log] [blame]
Ian Rogersb033c752011-07-20 12:22:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Ian Rogersb033c752011-07-20 12:22:35 -07002
3#ifndef ART_SRC_CALLING_CONVENTION_H_
4#define ART_SRC_CALLING_CONVENTION_H_
5
Ian Rogers0d666d82011-08-14 16:03:46 -07006#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "managed_register.h"
8#include "object.h"
9#include "thread.h"
Ian Rogersb033c752011-07-20 12:22:35 -070010
11namespace art {
12
13// Top-level abstraction for different calling conventions
14class CallingConvention {
15 public:
Ian Rogersb033c752011-07-20 12:22:35 -070016 bool IsReturnAReference() const { return method_->IsReturnAReference(); }
17
Ian Rogersdf20fe02011-07-20 20:34:16 -070018 size_t SizeOfReturnValue() const { return method_->ReturnSize(); }
19
Ian Rogersb033c752011-07-20 12:22:35 -070020 // Register that holds result of this method
Ian Rogers2c8f6532011-09-02 17:16:34 -070021 virtual ManagedRegister ReturnRegister() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070022 // Register reserved for scratch usage during procedure calls
Ian Rogers2c8f6532011-09-02 17:16:34 -070023 virtual ManagedRegister InterproceduralScratchRegister() = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070024
Carl Shapiroe2d373e2011-07-25 15:20:06 -070025 // Offset of Method within the frame
26 FrameOffset MethodStackOffset();
27
Ian Rogersb033c752011-07-20 12:22:35 -070028 // Iterator interface
29
30 // Place iterator at start of arguments. The displacement is applied to
31 // frame offset methods to account for frames which may be on the stack
32 // below the one being iterated over.
33 void ResetIterator(FrameOffset displacement) {
34 displacement_ = displacement;
Shih-wei Liao5381cf92011-07-27 00:28:04 -070035 itr_slots_ = 0;
36 itr_args_ = 0;
Shih-wei Liao668512a2011-09-01 14:18:34 -070037 itr_refs_ = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070038 itr_longs_and_doubles_ = 0;
39 }
40
Ian Rogers2c8f6532011-09-02 17:16:34 -070041 virtual ~CallingConvention() {}
42
Ian Rogersb033c752011-07-20 12:22:35 -070043 protected:
44 explicit CallingConvention(Method* method) : displacement_(0),
45 method_(method) {}
Ian Rogers2c8f6532011-09-02 17:16:34 -070046
47 Method* GetMethod() const { return method_; }
Ian Rogersb033c752011-07-20 12:22:35 -070048
Shih-wei Liao5381cf92011-07-27 00:28:04 -070049 // The slot number for current calling_convention argument.
50 // Note that each slot is 32-bit. When the current argument is bigger
51 // than 32 bits, return the first slot number for this argument.
52 unsigned int itr_slots_;
Ian Rogers7a99c112011-09-07 12:48:27 -070053 // The number of references iterated past
54 unsigned int itr_refs_;
Shih-wei Liao5381cf92011-07-27 00:28:04 -070055 // The argument number along argument list for current argument
56 unsigned int itr_args_;
57 // Number of longs and doubles seen along argument list
Ian Rogersb033c752011-07-20 12:22:35 -070058 unsigned int itr_longs_and_doubles_;
59 // Space for frames below this on the stack
60 FrameOffset displacement_;
61
62 private:
Ian Rogers2c8f6532011-09-02 17:16:34 -070063 Method* method_;
Ian Rogersb033c752011-07-20 12:22:35 -070064};
65
66// Abstraction for managed code's calling conventions
67class ManagedRuntimeCallingConvention : public CallingConvention {
68 public:
Ian Rogers2c8f6532011-09-02 17:16:34 -070069 static ManagedRuntimeCallingConvention* Create(Method* native_method,
70 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -070071
72 size_t FrameSize();
73
Ian Rogers2c8f6532011-09-02 17:16:34 -070074 // Register that holds the incoming method argument
75 virtual ManagedRegister MethodRegister() = 0;
76
Ian Rogersb033c752011-07-20 12:22:35 -070077 // Iterator interface
78 bool HasNext();
79 void Next();
80 bool IsCurrentParamAReference();
Ian Rogers7a99c112011-09-07 12:48:27 -070081 bool IsCurrentArgExplicit(); // ie a non-implict argument such as this
82 bool IsCurrentArgPossiblyNull();
Ian Rogersdf20fe02011-07-20 20:34:16 -070083 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -070084 virtual bool IsCurrentParamInRegister() = 0;
85 virtual bool IsCurrentParamOnStack() = 0;
86 virtual ManagedRegister CurrentParamRegister() = 0;
87 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070088
Ian Rogers2c8f6532011-09-02 17:16:34 -070089 virtual ~ManagedRuntimeCallingConvention() {}
90
91 protected:
92 explicit ManagedRuntimeCallingConvention(Method* method) :
93 CallingConvention(method) {}
Ian Rogersb033c752011-07-20 12:22:35 -070094};
95
96// Abstraction for JNI calling conventions
97// | incoming stack args | <-- Prior SP
Ian Rogers0d666d82011-08-14 16:03:46 -070098// | { Return address } | (x86)
Ian Rogersdf20fe02011-07-20 20:34:16 -070099// | { Return value spill } | (live on return slow paths)
Ian Rogers408f79a2011-08-23 18:22:33 -0700100// | { Stack Indirect Ref. |
101// | Table... |
Ian Rogersdf20fe02011-07-20 20:34:16 -0700102// | num. refs./link } | (here to prior SP is frame size)
Ian Rogers0d666d82011-08-14 16:03:46 -0700103// | { Spill area } | (ARM)
Ian Rogersb033c752011-07-20 12:22:35 -0700104// | Method* | <-- Anchor SP written to thread
105// | { Outgoing stack args |
106// | ... } | <-- SP at point of call
107// | Native frame |
108class JniCallingConvention : public CallingConvention {
109 public:
Ian Rogers2c8f6532011-09-02 17:16:34 -0700110 static JniCallingConvention* Create(Method* native_method,
111 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700112
113 // Size of frame excluding space for outgoing args (its assumed Method* is
114 // always at the bottom of a frame, but this doesn't work for outgoing
115 // native args). Includes alignment.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700116 virtual size_t FrameSize() = 0;
Ian Rogers762400c2011-08-23 12:14:16 -0700117 // Offset within the frame of the return pc
Ian Rogers2c8f6532011-09-02 17:16:34 -0700118 virtual size_t ReturnPcOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700119 // Size of outgoing arguments, including alignment
Ian Rogers2c8f6532011-09-02 17:16:34 -0700120 virtual size_t OutArgSize() = 0;
121 // Size of area used to hold spilled registers
122 virtual size_t SpillAreaSize() = 0;
Ian Rogers408f79a2011-08-23 18:22:33 -0700123 // Number of references in stack indirect reference table
124 size_t ReferenceCount();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700125 // Location where the return value of a call can be squirreled if another
126 // call is made following the native call
127 FrameOffset ReturnValueSaveLocation();
Ian Rogersb033c752011-07-20 12:22:35 -0700128
Ian Rogers0d666d82011-08-14 16:03:46 -0700129 // Registers that must be spilled (due to clobbering) before the call into
130 // the native routine
131 const std::vector<ManagedRegister>& RegsToSpillPreCall() {
Elliott Hughes90a33692011-08-30 13:27:07 -0700132 return spill_regs_;
Ian Rogers0d666d82011-08-14 16:03:46 -0700133 }
134
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700135 // Returns true if the register will be clobbered by an outgoing
136 // argument value.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700137 virtual bool IsOutArgRegister(ManagedRegister reg) = 0;
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700138
Ian Rogersb033c752011-07-20 12:22:35 -0700139 // Iterator interface
140 bool HasNext();
141 void Next();
142 bool IsCurrentParamAReference();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700143 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700144 virtual bool IsCurrentParamInRegister() = 0;
145 virtual bool IsCurrentParamOnStack() = 0;
146 virtual ManagedRegister CurrentParamRegister() = 0;
147 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700148
149 // Iterator interface extension for JNI
Ian Rogers408f79a2011-08-23 18:22:33 -0700150 FrameOffset CurrentParamSirtEntryOffset();
Ian Rogersb033c752011-07-20 12:22:35 -0700151
Ian Rogers408f79a2011-08-23 18:22:33 -0700152 // Position of SIRT and interior fields
153 FrameOffset SirtOffset() {
Ian Rogersb033c752011-07-20 12:22:35 -0700154 return FrameOffset(displacement_.Int32Value() +
Ian Rogers0d666d82011-08-14 16:03:46 -0700155 SpillAreaSize() +
Ian Rogersb033c752011-07-20 12:22:35 -0700156 kPointerSize); // above Method*
157 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700158 FrameOffset SirtNumRefsOffset() {
159 return FrameOffset(SirtOffset().Int32Value() +
160 StackIndirectReferenceTable::NumberOfReferencesOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700161 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700162 FrameOffset SirtLinkOffset() {
163 return FrameOffset(SirtOffset().Int32Value() +
164 StackIndirectReferenceTable::LinkOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700165 }
166
Ian Rogers2c8f6532011-09-02 17:16:34 -0700167 virtual ~JniCallingConvention() {}
168
169 protected:
Ian Rogersb033c752011-07-20 12:22:35 -0700170 // Named iterator positions
171 enum IteratorPos {
172 kJniEnv = 0,
173 kObjectOrClass = 1
174 };
175
Ian Rogers2c8f6532011-09-02 17:16:34 -0700176 explicit JniCallingConvention(Method* native_method) :
177 CallingConvention(native_method) {}
178
Ian Rogers408f79a2011-08-23 18:22:33 -0700179 // Number of stack slots for outgoing arguments, above which the SIRT is
Ian Rogersb033c752011-07-20 12:22:35 -0700180 // located
Ian Rogers2c8f6532011-09-02 17:16:34 -0700181 virtual size_t NumberOfOutgoingStackArgs() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700182
Ian Rogers2c8f6532011-09-02 17:16:34 -0700183 protected:
184 static size_t NumberOfExtraArgumentsForJni(Method* method);
Ian Rogers0d666d82011-08-14 16:03:46 -0700185
186 // Extra registers to spill before the call into native
Elliott Hughes90a33692011-08-30 13:27:07 -0700187 std::vector<ManagedRegister> spill_regs_;
Ian Rogersb033c752011-07-20 12:22:35 -0700188};
189
190} // namespace art
191
192#endif // ART_SRC_CALLING_CONVENTION_H_