Path: blob/master/src/hotspot/share/opto/constantTable.hpp
40930 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_OPTO_CONSTANTTABLE_HPP25#define SHARE_OPTO_CONSTANTTABLE_HPP2627#include "utilities/globalDefinitions.hpp"2829class CodeBuffer;30class Metadata;31class MachConstantNode;32class MachOper;3334class ConstantTable {35public:36// Constant entry of the constant table.37class Constant {38private:39BasicType _type;40union {41jvalue _value;42Metadata* _metadata;43} _v;44int _offset; // offset of this constant (in bytes) relative to the constant table base.45float _freq;46bool _can_be_reused; // true (default) if the value can be shared with other users.4748public:49Constant() : _type(T_ILLEGAL), _offset(-1), _freq(0.0f), _can_be_reused(true) { _v._value.l = 0; }50Constant(BasicType type, jvalue value, float freq = 0.0f, bool can_be_reused = true) :51_type(type),52_offset(-1),53_freq(freq),54_can_be_reused(can_be_reused)55{56assert(type != T_METADATA, "wrong constructor");57_v._value = value;58}59Constant(Metadata* metadata, bool can_be_reused = true) :60_type(T_METADATA),61_offset(-1),62_freq(0.0f),63_can_be_reused(can_be_reused)64{65_v._metadata = metadata;66}6768bool operator==(const Constant& other);6970BasicType type() const { return _type; }7172jint get_jint() const { return _v._value.i; }73jlong get_jlong() const { return _v._value.j; }74jfloat get_jfloat() const { return _v._value.f; }75jdouble get_jdouble() const { return _v._value.d; }76jobject get_jobject() const { return _v._value.l; }7778Metadata* get_metadata() const { return _v._metadata; }7980int offset() const { return _offset; }81void set_offset(int offset) { _offset = offset; }8283float freq() const { return _freq; }84void inc_freq(float freq) { _freq += freq; }8586bool can_be_reused() const { return _can_be_reused; }87};8889private:90GrowableArray<Constant> _constants; // Constants of this table.91int _size; // Size in bytes the emitted constant table takes (including padding).92int _table_base_offset; // Offset of the table base that gets added to the constant offsets.93int _nof_jump_tables; // Number of jump-tables in this constant table.9495static int qsort_comparator(Constant* a, Constant* b);9697// We use negative frequencies to keep the order of the98// jump-tables in which they were added. Otherwise we get into99// trouble with relocation.100float next_jump_table_freq() { return -1.0f * (++_nof_jump_tables); }101102public:103ConstantTable() :104_size(-1),105_table_base_offset(-1), // We can use -1 here since the constant table is always bigger than 2 bytes (-(size / 2), see MachConstantBaseNode::emit).106_nof_jump_tables(0)107{}108109int size() const { assert(_size != -1, "not calculated yet"); return _size; }110111int calculate_table_base_offset() const; // AD specific112void set_table_base_offset(int x) { assert(_table_base_offset == -1 || x == _table_base_offset, "can't change"); _table_base_offset = x; }113int table_base_offset() const { assert(_table_base_offset != -1, "not set yet"); return _table_base_offset; }114115bool emit(CodeBuffer& cb) const;116117// Returns the offset of the last entry (the top) of the constant table.118int top_offset() const { assert(_constants.top().offset() != -1, "not bound yet"); return _constants.top().offset(); }119120void calculate_offsets_and_size();121int find_offset(Constant& con) const;122123void add(Constant& con);124Constant add(MachConstantNode* n, BasicType type, jvalue value);125Constant add(Metadata* metadata);126Constant add(MachConstantNode* n, MachOper* oper);127Constant add(MachConstantNode* n, jint i) {128jvalue value; value.i = i;129return add(n, T_INT, value);130}131Constant add(MachConstantNode* n, jlong j) {132jvalue value; value.j = j;133return add(n, T_LONG, value);134}135Constant add(MachConstantNode* n, jfloat f) {136jvalue value; value.f = f;137return add(n, T_FLOAT, value);138}139Constant add(MachConstantNode* n, jdouble d) {140jvalue value; value.d = d;141return add(n, T_DOUBLE, value);142}143144// Jump-table145Constant add_jump_table(MachConstantNode* n);146void fill_jump_table(CodeBuffer& cb, MachConstantNode* n, GrowableArray<Label*> labels) const;147};148149150#endif // SHARE_OPTO_CONSTANTTABLE_HPP151152153