Skip to content

Commit 4b9b178

Browse files
committed
Add support for the weak variable attribute
1 parent 9d618e7 commit 4b9b178

7 files changed

+75
-9
lines changed

gcc/jit/jit-playback.cc

+26-6
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ const char* variable_attribute_to_string (gcc_jit_variable_attribute attr)
595595
{
596596
case GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY:
597597
return "visibility";
598+
case GCC_JIT_VARIABLE_ATTRIBUTE_WEAK:
599+
return "weak";
598600
case GCC_JIT_VARIABLE_ATTRIBUTE_MAX:
599601
return NULL;
600602
}
@@ -780,7 +782,8 @@ global_new_decl (location *loc,
780782
const char *name,
781783
enum global_var_flags flags,
782784
const std::vector<std::pair<gcc_jit_variable_attribute,
783-
std::string>> &attributes,
785+
std::string>> &string_attributes,
786+
const std::vector<gcc_jit_variable_attribute> &attributes,
784787
bool readonly)
785788
{
786789
gcc_assert (type);
@@ -826,7 +829,19 @@ global_new_decl (location *loc,
826829
if (loc)
827830
set_tree_location (inner, loc);
828831

829-
set_variable_string_attribute (attributes, inner);
832+
set_variable_string_attribute (string_attributes, inner);
833+
834+
tree var_attributes = NULL_TREE;
835+
for (auto attr: attributes)
836+
{
837+
const char* attribute = variable_attribute_to_string (attr);
838+
if (attribute)
839+
{
840+
tree ident = get_identifier (attribute);
841+
var_attributes = tree_cons (ident, NULL_TREE, var_attributes);
842+
}
843+
}
844+
decl_attributes (&inner, var_attributes, 0);
830845

831846
return inner;
832847
}
@@ -873,11 +888,13 @@ new_global (location *loc,
873888
const char *name,
874889
enum global_var_flags flags,
875890
const std::vector<std::pair<gcc_jit_variable_attribute,
876-
std::string>> &attributes,
891+
std::string>> &string_attributes,
892+
const std::vector<gcc_jit_variable_attribute> &attributes,
877893
bool readonly)
878894
{
879895
tree inner =
880-
global_new_decl (loc, kind, type, name, flags, attributes, readonly);
896+
global_new_decl (loc, kind, type, name, flags, string_attributes,
897+
attributes, readonly);
881898

882899
return global_finalize_lvalue (inner);
883900
}
@@ -1024,10 +1041,13 @@ new_global_initialized (location *loc,
10241041
const char *name,
10251042
enum global_var_flags flags,
10261043
const std::vector<std::pair<gcc_jit_variable_attribute,
1027-
std::string>> &attributes,
1044+
std::string>> &string_attributes,
1045+
const std::vector<gcc_jit_variable_attribute>
1046+
&attributes,
10281047
bool readonly)
10291048
{
1030-
tree inner = global_new_decl (loc, kind, type, name, flags, attributes, readonly);
1049+
tree inner = global_new_decl (loc, kind, type, name, flags,
1050+
string_attributes, attributes, readonly);
10311051

10321052
vec<constructor_elt, va_gc> *constructor_elements = NULL;
10331053

gcc/jit/jit-playback.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ class context : public log_user
136136
const char *name,
137137
enum global_var_flags flags,
138138
const std::vector<std::pair<gcc_jit_variable_attribute,
139-
std::string>> &attributes,
139+
std::string>> &string_attributes,
140+
const std::vector<gcc_jit_variable_attribute> &attributes,
140141
bool readonly);
141142

142143
lvalue *
@@ -151,6 +152,8 @@ class context : public log_user
151152
const std::vector<std::pair<
152153
gcc_jit_variable_attribute,
153154
std::string>>
155+
&string_attributes,
156+
const std::vector<gcc_jit_variable_attribute>
154157
&attributes,
155158
bool readonly);
156159

@@ -360,7 +363,8 @@ class context : public log_user
360363
const char *name,
361364
enum global_var_flags flags,
362365
const std::vector<std::pair<gcc_jit_variable_attribute,
363-
std::string>> &attributes,
366+
std::string>> &string_attributes,
367+
const std::vector<gcc_jit_variable_attribute> &attributes,
364368
bool readonly);
365369
lvalue *
366370
global_finalize_lvalue (tree inner);

gcc/jit/jit-recording.cc

+11
Original file line numberDiff line numberDiff line change
@@ -4406,6 +4406,12 @@ void recording::lvalue::add_string_attribute (
44064406
m_string_attributes.push_back (std::make_pair (attribute, std::string (value)));
44074407
}
44084408

4409+
void recording::lvalue::add_attribute (
4410+
gcc_jit_variable_attribute attribute)
4411+
{
4412+
m_attributes.push_back (attribute);
4413+
}
4414+
44094415
/* The implementation of class gcc::jit::recording::param. */
44104416

44114417
/* Implementation of pure virtual hook recording::memento::replay_into
@@ -5461,13 +5467,15 @@ recording::global::replay_into (replayer *r)
54615467
playback_string (m_name),
54625468
m_flags,
54635469
m_string_attributes,
5470+
m_attributes,
54645471
m_readonly)
54655472
: r->new_global (playback_location (r, m_loc),
54665473
m_kind,
54675474
m_type->playback_type (),
54685475
playback_string (m_name),
54695476
m_flags,
54705477
m_string_attributes,
5478+
m_attributes,
54715479
m_readonly);
54725480

54735481
if (m_tls_model != GCC_JIT_TLS_MODEL_NONE)
@@ -5536,6 +5544,7 @@ recording::global::write_to_dump (dump &d)
55365544
if (attribute)
55375545
d.write ("__attribute(%s(\"%s\"))__\n", attribute, value.c_str());
55385546
}
5547+
// TODO: handle m_attributes.
55395548
d.write ("%s %s",
55405549
m_type->get_debug_string (),
55415550
get_debug_string ());
@@ -5608,6 +5617,7 @@ static const char * const tls_model_enum_strings[] = {
56085617

56095618
static const char * const gcc_jit_variable_attribute_enum_strings[] = {
56105619
"GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY",
5620+
"GCC_JIT_VARIABLE_ATTRIBUTE_WEAK",
56115621
};
56125622

56135623
void
@@ -5644,6 +5654,7 @@ recording::global::write_reproducer (reproducer &r)
56445654
id,
56455655
gcc_jit_variable_attribute_enum_strings[std::get<0>(attribute)],
56465656
std::get<1>(attribute).c_str());
5657+
// TODO: handle m_attributes.
56475658

56485659
if (m_readonly)
56495660
r.write (" gcc_jit_global_set_readonly (%s /* gcc_jit_lvalue *lvalue */);\n",

gcc/jit/jit-recording.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,8 @@ class lvalue : public rvalue
14461446
m_reg_name (NULL),
14471447
m_tls_model (GCC_JIT_TLS_MODEL_NONE),
14481448
m_alignment (0),
1449-
m_string_attributes ()
1449+
m_string_attributes (),
1450+
m_attributes ()
14501451
{}
14511452

14521453
playback::lvalue *
@@ -1468,6 +1469,7 @@ class lvalue : public rvalue
14681469
const char *access_as_rvalue (reproducer &r) override;
14691470

14701471
void add_string_attribute (gcc_jit_variable_attribute attribute, const char* value);
1472+
void add_attribute (gcc_jit_variable_attribute attribute);
14711473

14721474
bool get_readonly () const
14731475
{
@@ -1495,6 +1497,7 @@ class lvalue : public rvalue
14951497
unsigned m_alignment;
14961498
std::vector<std::pair<gcc_jit_variable_attribute,
14971499
std::string>> m_string_attributes;
1500+
std::vector<gcc_jit_variable_attribute> m_attributes;
14981501
bool m_readonly = false;
14991502
};
15001503

gcc/jit/libgccjit.cc

+17
Original file line numberDiff line numberDiff line change
@@ -4406,6 +4406,23 @@ gcc_jit_lvalue_add_string_attribute (gcc_jit_lvalue *variable,
44064406
variable->add_string_attribute (attribute, value);
44074407
}
44084408

4409+
void
4410+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable,
4411+
gcc_jit_variable_attribute attribute)
4412+
{
4413+
RETURN_IF_FAIL (variable, NULL, NULL, "NULL variable");
4414+
RETURN_IF_FAIL (variable->is_global () || variable->is_local (),
4415+
NULL,
4416+
NULL,
4417+
"variable should be a variable");
4418+
RETURN_IF_FAIL ((attribute >= 0 && attribute < GCC_JIT_VARIABLE_ATTRIBUTE_MAX),
4419+
NULL,
4420+
NULL,
4421+
"attribute should be a `gcc_jit_variable_attribute` enum value");
4422+
4423+
variable->add_attribute (attribute);
4424+
}
4425+
44094426
void
44104427
gcc_jit_type_set_packed (gcc_jit_type *type)
44114428
{

gcc/jit/libgccjit.h

+6
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,7 @@ gcc_jit_function_add_integer_array_attribute (
21942194
enum gcc_jit_variable_attribute
21952195
{
21962196
GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY,
2197+
GCC_JIT_VARIABLE_ATTRIBUTE_WEAK,
21972198

21982199
/* Maximum value of this enum, should always be last. */
21992200
GCC_JIT_VARIABLE_ATTRIBUTE_MAX,
@@ -2211,6 +2212,11 @@ gcc_jit_context_set_output_ident (gcc_jit_context *ctxt,
22112212

22122213
#define LIBGCCJIT_HAVE_gcc_jit_context_set_output_ident
22132214

2215+
/* Add an attribute to a variable. */
2216+
extern void
2217+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable,
2218+
enum gcc_jit_variable_attribute attribute);
2219+
22142220
extern gcc_jit_target_info *
22152221
gcc_jit_context_get_target_info (gcc_jit_context *ctxt);
22162222

gcc/jit/libgccjit.map

+5
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,8 @@ LIBGCCJIT_ABI_41 {
368368
global:
369369
gcc_jit_rvalue_set_type;
370370
} LIBGCCJIT_ABI_40;
371+
372+
LIBGCCJIT_ABI_42 {
373+
global:
374+
gcc_jit_lvalue_add_attribute;
375+
} LIBGCCJIT_ABI_41;

0 commit comments

Comments
 (0)