-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathOMRVirtualMachineOperandArray.cpp
183 lines (161 loc) · 7.55 KB
/
OMRVirtualMachineOperandArray.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
/*******************************************************************************
* Copyright (c) 2017, 2018 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 "ilgen/VirtualMachineOperandArray.hpp"
#include "compile/Compilation.hpp"
#include "il/SymbolReference.hpp"
#include "il/symbol/AutomaticSymbol.hpp"
#include "ilgen/BytecodeBuilder.hpp"
#include "ilgen/MethodBuilder.hpp"
#include "ilgen/TypeDictionary.hpp"
#include "ilgen/VirtualMachineRegister.hpp"
#include "ilgen/VirtualMachineState.hpp"
#define TraceEnabled (TR::comp()->getOption(TR_TraceILGen))
#define TraceIL(m, ...) {if (TraceEnabled) {traceMsg(TR::comp(), m, ##__VA_ARGS__);}}
OMR::VirtualMachineOperandArray::VirtualMachineOperandArray(TR::MethodBuilder *mb, int32_t numOfElements, TR::IlType *elementType, TR::VirtualMachineRegister *arrayBaseRegister)
: TR::VirtualMachineState(),
_mb(mb),
_numberOfElements(numOfElements),
_elementType(elementType),
_arrayBaseRegister(arrayBaseRegister)
{
int32_t numBytes = _numberOfElements * sizeof(TR::IlValue *);
_values = (TR::IlValue **) TR::comp()->trMemory()->allocateHeapMemory(numBytes);
memset(_values, 0, numBytes);
// store current operand stack pointer base address so we can use it whenever we need
// to recreate the stack as the interpreter would have
mb->Store("OperandArray_base", arrayBaseRegister->Load(mb));
}
OMR::VirtualMachineOperandArray::VirtualMachineOperandArray(TR::VirtualMachineOperandArray *other)
: TR::VirtualMachineState(),
_mb(other->_mb),
_numberOfElements(other->_numberOfElements),
_elementType(other->_elementType),
_arrayBaseRegister(other->_arrayBaseRegister)
{
int32_t numBytes = _numberOfElements * sizeof(TR::IlValue *);
_values = (TR::IlValue **) TR::comp()->trMemory()->allocateHeapMemory(numBytes);
memcpy(_values, other->_values, numBytes);
}
// commits the simulated operand array of values to the virtual machine state
// the given builder object is where the operations to commit the state will be inserted
// into the array which is assumed to be managed independently, most likely
void
OMR::VirtualMachineOperandArray::Commit(TR::IlBuilder *b)
{
TR::IlType *Element = _elementType;
TR::IlType *pElement = _mb->typeDictionary()->PointerTo(Element);
TR::IlValue *arrayBase = b->Load("OperandArray_base");
for (int32_t i = 0;i < _numberOfElements;i++)
{
TR::IlValue *element = _values[i];
if (element != NULL)
{
b->StoreAt(
b-> IndexAt(pElement,
arrayBase,
b-> ConstInt32(i)),
element);
}
}
}
void
OMR::VirtualMachineOperandArray::Reload(TR::IlBuilder* b)
{
TR::IlType* Element = _elementType;
TR::IlType* pElement = _mb->typeDictionary()->PointerTo(Element);
// reload the elements back into the simulated operand array
TR::IlValue* array = b->Load("OperandArray_base");
for (int32_t i = 0; i < _numberOfElements; i++)
{
_values[i] = b->LoadAt(pElement,
b-> IndexAt(pElement,
array,
b-> ConstInt32(i)));
}
}
void
OMR::VirtualMachineOperandArray::MergeInto(TR::VirtualMachineState *o, TR::IlBuilder *b)
{
TR::VirtualMachineOperandArray *other = (TR::VirtualMachineOperandArray *)o;
TR_ASSERT(_numberOfElements == other->_numberOfElements, "array are not same size");
for (int32_t i=0;i < _numberOfElements;i++)
{
if (NULL == other->_values[i])
{
TR_ASSERT(_values[i] == NULL, "if an element in the dest array at index is NULL it has to be NULL in the src array");
}
else if (other->_values[i]->getID() != _values[i]->getID())
{
// Types have to match!!!
TR_ASSERT(_values[i]->getDataType() == other->_values[i]->getDataType(), "invalid array merge: primitive type mismatch at same index");
TraceIL("VirtualMachineOperandArray[ %p ]::MergeInto builder %p index %d storeOver %p(%d) with %p(%d)\n", this, b, i, other->_values[i], other->_values[i]->getID(), _values[i], _values[i]->getID());
b->StoreOver(other->_values[i], _values[i]);
}
}
}
// Update the OperandArray_base after the Virtual Machine moves the array.
// This call will normally be followed by a call to Reload if any of the array values changed in the move
void
OMR::VirtualMachineOperandArray::UpdateArray(TR::IlBuilder *b, TR::IlValue *array)
{
b->Store("OperandArray_base", array);
}
// Allocate a new operand array and copy everything in this state
// If VirtualMachineOperandArray is subclassed, this function *must* also be implemented in the subclass!
TR::VirtualMachineState *
OMR::VirtualMachineOperandArray::MakeCopy()
{
TR::VirtualMachineOperandArray *copy = (TR::VirtualMachineOperandArray *) TR::comp()->trMemory()->allocateHeapMemory(sizeof(TR::VirtualMachineOperandArray));
new (copy) TR::VirtualMachineOperandArray(static_cast<TR::VirtualMachineOperandArray *>(this));
return copy;
}
TR::IlValue *
OMR::VirtualMachineOperandArray::Get(int32_t index)
{
TR_ASSERT(index < _numberOfElements, "index has to be less than the number of elements");
TR_ASSERT(index >= 0, "index can not be negative");
return _values[index];
}
void
OMR::VirtualMachineOperandArray::Set(int32_t index, TR::IlValue *value)
{
TR_ASSERT(index < _numberOfElements, "index has to be less than the number of elements");
TR_ASSERT(index >= 0, "index can not be negative");
if (NULL != _values[index])
{
TraceIL("VirtualMachineOperandArray[ %p ]::Set index %d to %p(%d) old value was %p(%d)\n", this, index, value, value->getID(), _values[index], _values[index]->getID());
}
else
{
TraceIL("VirtualMachineOperandArray[ %p ]::Set index %d to %p(%d)\n", this, index, value, value->getID());
}
_values[index] = value;
}
void
OMR::VirtualMachineOperandArray::Move(TR::IlBuilder *b, int32_t dstIndex, int32_t srcIndex)
{
TR_ASSERT(dstIndex < _numberOfElements, "dstIndex has to be less than the number of elements");
TR_ASSERT(dstIndex >= 0, "dstIndex can not be negative");
TR_ASSERT(srcIndex < _numberOfElements, "srcIndex has to be less than the number of elements");
TR_ASSERT(srcIndex >= 0, "srcIndex can not be negative");
_values[dstIndex] = b->Copy(_values[srcIndex]);
TraceIL("VirtualMachineOperandArray[ %p ]::Move builder %p move srcIndex %d %p(%d) to dstIndex %d %p(%d)\n", this, b, srcIndex, _values[srcIndex], _values[srcIndex]->getID(), dstIndex, _values[dstIndex], _values[dstIndex]->getID());
}