blob: 8f2a79c4f5d24fc5e1fcf7a7864edd093b1b8fa7 [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
Ian Rogersbdb03912011-09-14 00:55:44 -070067// | { Incoming stack args } |
68// | { Prior Method* } | <-- Prior SP
69// | { Return address } |
70// | { Callee saves } |
71// | { Spills ... } |
72// | { Outgoing stack args } |
73// | { Method* } | <-- SP
Ian Rogersb033c752011-07-20 12:22:35 -070074class ManagedRuntimeCallingConvention : public CallingConvention {
75 public:
Ian Rogers2c8f6532011-09-02 17:16:34 -070076 static ManagedRuntimeCallingConvention* Create(Method* native_method,
77 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -070078
79 size_t FrameSize();
80
Ian Rogers2c8f6532011-09-02 17:16:34 -070081 // Register that holds the incoming method argument
82 virtual ManagedRegister MethodRegister() = 0;
83
Ian Rogersb033c752011-07-20 12:22:35 -070084 // Iterator interface
85 bool HasNext();
86 void Next();
87 bool IsCurrentParamAReference();
Ian Rogers7a99c112011-09-07 12:48:27 -070088 bool IsCurrentArgExplicit(); // ie a non-implict argument such as this
89 bool IsCurrentArgPossiblyNull();
Ian Rogersdf20fe02011-07-20 20:34:16 -070090 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -070091 virtual bool IsCurrentParamInRegister() = 0;
92 virtual bool IsCurrentParamOnStack() = 0;
93 virtual ManagedRegister CurrentParamRegister() = 0;
94 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -070095
Ian Rogers2c8f6532011-09-02 17:16:34 -070096 virtual ~ManagedRuntimeCallingConvention() {}
97
98 protected:
99 explicit ManagedRuntimeCallingConvention(Method* method) :
Ian Rogersbdb03912011-09-14 00:55:44 -0700100 CallingConvention(method) {}
Ian Rogersb033c752011-07-20 12:22:35 -0700101};
102
103// Abstraction for JNI calling conventions
Ian Rogersbdb03912011-09-14 00:55:44 -0700104// | { Incoming stack args } | <-- Prior SP
105// | { Return address } |
106// | { Callee saves } | ([1])
107// | { Return value spill } | (live on return slow paths)
108// | { Stack Indirect Ref. Table |
109// | num. refs./link } | (here to prior SP is frame size)
110// | { Method* } | <-- Anchor SP written to thread
111// | { Outgoing stack args } | <-- SP at point of call
112// | Native frame |
113//
114// [1] We must save all callee saves here to enable any exception throws to restore
115// callee saves for frames above this one.
Ian Rogersb033c752011-07-20 12:22:35 -0700116class JniCallingConvention : public CallingConvention {
117 public:
Ian Rogers2c8f6532011-09-02 17:16:34 -0700118 static JniCallingConvention* Create(Method* native_method,
119 InstructionSet instruction_set);
Ian Rogersb033c752011-07-20 12:22:35 -0700120
121 // Size of frame excluding space for outgoing args (its assumed Method* is
122 // always at the bottom of a frame, but this doesn't work for outgoing
123 // native args). Includes alignment.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700124 virtual size_t FrameSize() = 0;
Ian Rogers762400c2011-08-23 12:14:16 -0700125 // Offset within the frame of the return pc
Ian Rogers2c8f6532011-09-02 17:16:34 -0700126 virtual size_t ReturnPcOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700127 // Size of outgoing arguments, including alignment
Ian Rogers2c8f6532011-09-02 17:16:34 -0700128 virtual size_t OutArgSize() = 0;
Ian Rogers408f79a2011-08-23 18:22:33 -0700129 // Number of references in stack indirect reference table
130 size_t ReferenceCount();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700131 // Location where the return value of a call can be squirreled if another
132 // call is made following the native call
133 FrameOffset ReturnValueSaveLocation();
Ian Rogersb033c752011-07-20 12:22:35 -0700134
Ian Rogersbdb03912011-09-14 00:55:44 -0700135 // Callee save registers to spill prior to native code (which may clobber)
136 virtual const std::vector<ManagedRegister>& CalleeSaveRegisters() const = 0;
137
138 // Spill mask values
139 virtual uint32_t CoreSpillMask() const = 0;
140 virtual uint32_t FpSpillMask() const = 0;
Ian Rogers0d666d82011-08-14 16:03:46 -0700141
Ian Rogers67375ac2011-09-14 00:55:44 -0700142 // Returns true if the method register will have been clobbered during argument
143 // set up
Ian Rogersad42e132011-09-17 20:23:33 -0700144 virtual bool IsMethodRegisterClobberedPreCall() = 0;
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700145
Ian Rogersb033c752011-07-20 12:22:35 -0700146 // Iterator interface
147 bool HasNext();
Ian Rogers67375ac2011-09-14 00:55:44 -0700148 virtual void Next();
Ian Rogersb033c752011-07-20 12:22:35 -0700149 bool IsCurrentParamAReference();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700150 size_t CurrentParamSize();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700151 virtual bool IsCurrentParamInRegister() = 0;
152 virtual bool IsCurrentParamOnStack() = 0;
153 virtual ManagedRegister CurrentParamRegister() = 0;
154 virtual FrameOffset CurrentParamStackOffset() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700155
156 // Iterator interface extension for JNI
Ian Rogers408f79a2011-08-23 18:22:33 -0700157 FrameOffset CurrentParamSirtEntryOffset();
Ian Rogersb033c752011-07-20 12:22:35 -0700158
Ian Rogers408f79a2011-08-23 18:22:33 -0700159 // Position of SIRT and interior fields
160 FrameOffset SirtOffset() {
Ian Rogersb033c752011-07-20 12:22:35 -0700161 return FrameOffset(displacement_.Int32Value() +
162 kPointerSize); // above Method*
163 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700164 FrameOffset SirtNumRefsOffset() {
165 return FrameOffset(SirtOffset().Int32Value() +
166 StackIndirectReferenceTable::NumberOfReferencesOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700167 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700168 FrameOffset SirtLinkOffset() {
169 return FrameOffset(SirtOffset().Int32Value() +
170 StackIndirectReferenceTable::LinkOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700171 }
172
Ian Rogers2c8f6532011-09-02 17:16:34 -0700173 virtual ~JniCallingConvention() {}
174
175 protected:
Ian Rogersb033c752011-07-20 12:22:35 -0700176 // Named iterator positions
177 enum IteratorPos {
178 kJniEnv = 0,
179 kObjectOrClass = 1
180 };
181
Ian Rogers2c8f6532011-09-02 17:16:34 -0700182 explicit JniCallingConvention(Method* native_method) :
183 CallingConvention(native_method) {}
184
Ian Rogers408f79a2011-08-23 18:22:33 -0700185 // Number of stack slots for outgoing arguments, above which the SIRT is
Ian Rogersb033c752011-07-20 12:22:35 -0700186 // located
Ian Rogers2c8f6532011-09-02 17:16:34 -0700187 virtual size_t NumberOfOutgoingStackArgs() = 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700188
Ian Rogers2c8f6532011-09-02 17:16:34 -0700189 protected:
190 static size_t NumberOfExtraArgumentsForJni(Method* method);
Ian Rogersb033c752011-07-20 12:22:35 -0700191};
192
193} // namespace art
194
195#endif // ART_SRC_CALLING_CONVENTION_H_