-
Notifications
You must be signed in to change notification settings - Fork 392
/
SystemLinkage.cpp
367 lines (323 loc) · 11.7 KB
/
SystemLinkage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <algorithm>
#include <stddef.h>
#include <stdint.h>
#include "codegen/CodeGenerator.hpp"
#include "codegen/ConstantDataSnippet.hpp"
#include "env/FrontEnd.hpp"
#include "codegen/InstOpCode.hpp"
#include "codegen/Instruction.hpp"
#include "codegen/Linkage.hpp"
#include "codegen/Linkage_inlines.hpp"
#include "codegen/Machine.hpp"
#include "codegen/MemoryReference.hpp"
#include "codegen/RealRegister.hpp"
#include "codegen/Register.hpp"
#include "codegen/RegisterDependency.hpp"
#include "codegen/RegisterPair.hpp"
#include "codegen/Snippet.hpp"
#include "codegen/SystemLinkage.hpp"
#include "codegen/TreeEvaluator.hpp"
#include "compile/Compilation.hpp"
#include "compile/ResolvedMethod.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/CompilerEnv.hpp"
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "il/AutomaticSymbol.hpp"
#include "il/DataTypes.hpp"
#include "il/ILOpCodes.hpp"
#include "il/ILOps.hpp"
#include "il/LabelSymbol.hpp"
#include "il/MethodSymbol.hpp"
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"
#include "il/ParameterSymbol.hpp"
#include "il/ResolvedMethodSymbol.hpp"
#include "il/Symbol.hpp"
#include "il/SymbolReference.hpp"
#include "il/TreeTop.hpp"
#include "il/TreeTop_inlines.hpp"
#include "infra/Array.hpp"
#include "infra/Assert.hpp"
#include "infra/List.hpp"
#include "ras/Debug.hpp"
#include "ras/Delimiter.hpp"
#include "z/codegen/S390Evaluator.hpp"
#include "z/codegen/S390GenerateInstructions.hpp"
#include "z/codegen/S390Instruction.hpp"
#include "z/codegen/SystemLinkage.hpp"
TR::SystemLinkage::SystemLinkage(TR::CodeGenerator* cg, TR_LinkageConventions elc)
:
TR::Linkage(cg, elc),
_GPRSaveMask(0),
_FPRSaveMask(0)
{
}
TR::SystemLinkage *
TR::SystemLinkage::self()
{
return static_cast<TR::SystemLinkage *>(this);
}
void
TR::SystemLinkage::initS390RealRegisterLinkage()
{
int32_t icount = 0, ret_count=0;
TR::RealRegister * spReal = getStackPointerRealRegister();
spReal->setState(TR::RealRegister::Locked);
spReal->setAssignedRegister(spReal);
spReal->setHasBeenAssignedInMethod(true);
// set register weight
for (icount = TR::RealRegister::FirstGPR; icount >= TR::RealRegister::LastAssignableGPR; icount++)
{
int32_t weight;
if (getIntegerReturn((TR::RealRegister::RegNum) icount))
{
weight = ++ret_count;
}
else if (getPreserved((TR::RealRegister::RegNum) icount))
{
weight = 0xf000 + icount;
}
else
{
weight = icount;
}
cg()->machine()->getRealRegister((TR::RealRegister::RegNum) icount)->setWeight(weight);
}
}
// Non-Java use
void
TR::SystemLinkage::initParamOffset(TR::ResolvedMethodSymbol * method, int32_t stackIndex, List<TR::ParameterSymbol> *parameterList)
{
ListIterator<TR::ParameterSymbol> parameterIterator((parameterList != NULL)? parameterList : &method->getParameterList());
parameterIterator.reset();
TR::ParameterSymbol * parmCursor = parameterIterator.getFirst();
int8_t gprSize = cg()->machine()->getGPRSize();
int32_t paramNum = -1;
setIncomingParmAreaBeginOffset(stackIndex);
while (parmCursor != NULL)
{
paramNum++;
int8_t lri = parmCursor->getLinkageRegisterIndex();
int32_t parmSize = parmCursor->getSize();
if (isXPLinkLinkageType())
{
parmCursor->setParameterOffset(stackIndex);
// parameters aligned on GPR sized boundaries - set for next
int32_t alignedSize = parmCursor->getSize();
alignedSize = ((alignedSize + gprSize - 1)/gprSize)*gprSize;
stackIndex += alignedSize;
}
else if (lri == -1)
{
parmCursor->setParameterOffset(stackIndex);
stackIndex += std::max<int32_t>(parmCursor->getSize(), cg()->machine()->getGPRSize());
}
else
{ // linkage parm registers reserved in caller save area
TR::RealRegister::RegNum argRegNum;
if (parmCursor->getType().isFloatingPoint())
argRegNum = getFloatArgumentRegister(lri);
else
argRegNum = getIntegerArgumentRegister(lri);
parmCursor->setParameterOffset(getStackFrameSize() + getRegisterSaveOffset(REGNUM(argRegNum)));
}
if (isSmallIntParmsAlignedRight() && parmCursor->getType().isIntegral() && (gprSize > parmCursor->getSize())) // WCode motivated
{
// linkage related
parmCursor->setParameterOffset(parmCursor->getParameterOffset() + gprSize - parmCursor->getSize());
}
parmCursor = parameterIterator.getNext();
}
setIncomingParmAreaEndOffset(stackIndex);
}
/**
* General utility
* Reverse the bit ordering of the save mask
*/
uint16_t
TR::SystemLinkage::flipBitsRegisterSaveMask(uint16_t mask)
{
TR_ASSERT(NUM_S390_GPR == NUM_S390_FPR, "need special flip bits for FPR");
uint16_t i, outputMask;
outputMask = 0;
for (i = 0; i < NUM_S390_GPR; i++)
{
if (mask & (1 << i))
{
outputMask |= 1 << (NUM_S390_GPR-1-i);
}
}
return outputMask;
}
/**
* Front-end customization of callNativeFunction
*/
void
TR::SystemLinkage::generateInstructionsForCall(TR::Node * callNode, TR::RegisterDependencyConditions * deps, intptr_t targetAddress,
TR::Register * methodAddressReg, TR::Register * javaLitOffsetReg, TR::LabelSymbol * returnFromJNICallLabel,
TR::Snippet * callDataSnippet, bool isJNIGCPoint)
{
TR_ASSERT(0,"Different system types should have their own implementation.");
}
/**
* Call System routine
* @return return value will be return value from system routine copied to private linkage return reg
*/
TR::Register *
TR::SystemLinkage::callNativeFunction(TR::Node * callNode, TR::RegisterDependencyConditions * deps, intptr_t targetAddress,
TR::Register * methodAddressReg, TR::Register * javaLitOffsetReg, TR::LabelSymbol * returnFromJNICallLabel,
TR::Snippet * callDataSnippet, bool isJNIGCPoint)
{
return 0;
}
void
TR::SystemLinkage::mapStack(TR::ResolvedMethodSymbol * method)
{
{
mapStack(method,0);
}
}
/**
* This function will intitialize all the offsets from frame pointer
* i.e. stackPointer value before buying stack for method being jitted
* The offsets will be fixed up (at the end /in createPrologue) when the stackFrameSize is known
*/
void
TR::SystemLinkage::mapStack(TR::ResolvedMethodSymbol * method, uint32_t stackIndex)
{
int32_t i;
// ====== COMPLEXITY ALERT: (TODO: fix this nonsense: map stack positively)
// This method is called twice - once with a 0 value of stackIndex and secondly with the stack size.
// This adds complexities:
// - alignment: ensuring things are aligned properly
// - item layout is done in a reverse order : those things mapped first here are at higher addresses
// end offsets are really begin offsets and vice versa.
// ====== COMPLEXITY ALERT
setStackSizeCheckNeeded(true);
// process incomming parameters
if (!isZLinuxLinkageType())
{
initParamOffset(method,getOutgoingParmAreaBeginOffset()+ stackIndex);
}
// Now map the locals
//
setLocalsAreaBeginOffset(stackIndex);
ListIterator<TR::AutomaticSymbol> automaticIterator(&method->getAutomaticList());
TR::AutomaticSymbol * localCursor = automaticIterator.getFirst();
automaticIterator.reset();
localCursor = automaticIterator.getFirst();
while (localCursor != NULL)
{
if (!TR::Linkage::needsAlignment(localCursor->getDataType(), cg()))
{
mapSingleAutomatic(localCursor, stackIndex);
}
localCursor = automaticIterator.getNext();
}
automaticIterator.reset();
localCursor = automaticIterator.getFirst();
// align double - but there is more to do to align the stack in general as double.
while (localCursor != NULL)
{
if (TR::Linkage::needsAlignment(localCursor->getDataType(), cg()))
{
mapSingleAutomatic(localCursor, stackIndex);
}
localCursor = automaticIterator.getNext();
}
// Force the stack size to be increased by...
stackIndex -= (comp()->getOptions()->get390StackBufferSize()/4)*4;
stackIndex -= (stackIndex & 0x4) ? 4 : 0;
if (true)
{
//Save used Preserved FPRs
setFPRSaveAreaBeginOffset(stackIndex);
int16_t FPRSaveMask = 0;
for (i = TR::RealRegister::FirstFPR; i <= TR::RealRegister::LastFPR; ++i)
{
if (getPreserved(REGNUM(i)) && ((getRealRegister(REGNUM(i)))->getHasBeenAssignedInMethod()))
{
FPRSaveMask |= 1 << (i - TR::RealRegister::FirstFPR);
stackIndex -= cg()->machine()->getFPRSize();
}
}
setFPRSaveMask(FPRSaveMask);
if (FPRSaveMask != 0 && isOSLinkageType())
{
#define DELTA_ALIGN(x, align) ((x & (align-1)) ? (align -((x)&(align-1))) : 0)
stackIndex -= DELTA_ALIGN(stackIndex, 16);
}
setFPRSaveAreaEndOffset(stackIndex);
}
// Map slot for long displacement
if (comp()->target().isLinux())
{
// Linux on Z has a special reserved slot in the linkage convention
setOffsetToLongDispSlot(comp()->target().is64Bit() ? 8 : 4);
}
else
{
setOffsetToLongDispSlot(stackIndex -= 16);
}
if (comp()->getOption(TR_TraceCG))
traceMsg(comp(), "\n\nOffsetToLongDispSlot = %d\n", getOffsetToLongDispSlot());
if (isZLinuxLinkageType())
{
initParamOffset(method,getOutgoingParmAreaBeginOffset()+ stackIndex);
}
method->setLocalMappingCursor(stackIndex);
stackIndex -= (stackIndex & 0x4) ? 4 : 0;
setLocalsAreaEndOffset(stackIndex);
}
void TR::SystemLinkage::mapSingleAutomatic(TR::AutomaticSymbol * p, uint32_t & stackIndex)
{
size_t align = 1;
size_t size = p->getSize();
if ((size & (size - 1)) == 0 && size <= 8) // if size is power of 2 and small
{
align = size;
}
else if (size > 8)
{
align = 8;
}
align = (align == 0) ? 1 : align;
// we map in a backwards fashion
int32_t roundup = align - 1;
stackIndex = (stackIndex-size) & (~roundup);
p->setOffset(stackIndex);
}
bool TR::SystemLinkage::hasToBeOnStack(TR::ParameterSymbol * parm)
{
return parm->getAssignedGlobalRegisterIndex() >= 0 && parm->isParmHasToBeOnStack();
}
intptr_t TR::SystemLinkage::entryPointFromCompiledMethod()
{
return reinterpret_cast<intptr_t>(cg()->getCodeStart());
}
intptr_t TR::SystemLinkage::entryPointFromInterpretedMethod()
{
return reinterpret_cast<intptr_t>(cg()->getCodeStart());
}