Skip to content

Commit 8b43115

Browse files
committed
Add support for the weak variable attribute
1 parent a50c620 commit 8b43115

7 files changed

+74
-9
lines changed

gcc/jit/jit-playback.cc

+26-6
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ const char* variable_attribute_to_string (gcc_jit_variable_attribute attr)
594594
{
595595
case GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY:
596596
return "visibility";
597+
case GCC_JIT_VARIABLE_ATTRIBUTE_WEAK:
598+
return "weak";
597599
case GCC_JIT_VARIABLE_ATTRIBUTE_MAX:
598600
return NULL;
599601
}
@@ -779,7 +781,8 @@ global_new_decl (location *loc,
779781
const char *name,
780782
enum global_var_flags flags,
781783
const std::vector<std::pair<gcc_jit_variable_attribute,
782-
std::string>> &attributes,
784+
std::string>> &string_attributes,
785+
const std::vector<gcc_jit_variable_attribute> &attributes,
783786
bool readonly,
784787
bool removed)
785788
{
@@ -835,7 +838,19 @@ global_new_decl (location *loc,
835838
if (loc)
836839
set_tree_location (inner, loc);
837840

838-
set_variable_string_attribute (attributes, inner);
841+
set_variable_string_attribute (string_attributes, inner);
842+
843+
tree var_attributes = NULL_TREE;
844+
for (auto attr: attributes)
845+
{
846+
const char* attribute = variable_attribute_to_string (attr);
847+
if (attribute)
848+
{
849+
tree ident = get_identifier (attribute);
850+
var_attributes = tree_cons (ident, NULL_TREE, var_attributes);
851+
}
852+
}
853+
decl_attributes (&inner, var_attributes, 0);
839854

840855
return inner;
841856
}
@@ -883,12 +898,14 @@ new_global (location *loc,
883898
const char *name,
884899
enum global_var_flags flags,
885900
const std::vector<std::pair<gcc_jit_variable_attribute,
886-
std::string>> &attributes,
901+
std::string>> &string_attributes,
902+
const std::vector<gcc_jit_variable_attribute> &attributes,
887903
bool readonly,
888904
bool removed)
889905
{
890906
tree inner =
891-
global_new_decl (loc, kind, type, name, flags, attributes, readonly, removed);
907+
global_new_decl (loc, kind, type, name, flags, string_attributes,
908+
attributes, readonly, removed);
892909

893910
return global_finalize_lvalue (inner, removed);
894911
}
@@ -1035,11 +1052,14 @@ new_global_initialized (location *loc,
10351052
const char *name,
10361053
enum global_var_flags flags,
10371054
const std::vector<std::pair<gcc_jit_variable_attribute,
1038-
std::string>> &attributes,
1055+
std::string>> &string_attributes,
1056+
const std::vector<gcc_jit_variable_attribute>
1057+
&attributes,
10391058
bool readonly,
10401059
bool removed)
10411060
{
1042-
tree inner = global_new_decl (loc, kind, type, name, flags, attributes, readonly, removed);
1061+
tree inner = global_new_decl (loc, kind, type, name, flags,
1062+
string_attributes, attributes, readonly, removed);
10431063

10441064
vec<constructor_elt, va_gc> *constructor_elements = NULL;
10451065

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
bool removed);
142143

@@ -152,6 +153,8 @@ class context : public log_user
152153
const std::vector<std::pair<
153154
gcc_jit_variable_attribute,
154155
std::string>>
156+
&string_attributes,
157+
const std::vector<gcc_jit_variable_attribute>
155158
&attributes,
156159
bool readonly,
157160
bool removed);
@@ -363,7 +366,8 @@ class context : public log_user
363366
const char *name,
364367
enum global_var_flags flags,
365368
const std::vector<std::pair<gcc_jit_variable_attribute,
366-
std::string>> &attributes,
369+
std::string>> &string_attributes,
370+
const std::vector<gcc_jit_variable_attribute> &attributes,
367371
bool readonly,
368372
bool removed);
369373
lvalue *

gcc/jit/jit-recording.cc

+11
Original file line numberDiff line numberDiff line change
@@ -4403,6 +4403,12 @@ void recording::lvalue::add_string_attribute (
44034403
m_string_attributes.push_back (std::make_pair (attribute, std::string (value)));
44044404
}
44054405

4406+
void recording::lvalue::add_attribute (
4407+
gcc_jit_variable_attribute attribute)
4408+
{
4409+
m_attributes.push_back (attribute);
4410+
}
4411+
44064412
/* The implementation of class gcc::jit::recording::param. */
44074413

44084414
/* Implementation of pure virtual hook recording::memento::replay_into
@@ -5458,6 +5464,7 @@ recording::global::replay_into (replayer *r)
54585464
playback_string (m_name),
54595465
m_flags,
54605466
m_string_attributes,
5467+
m_attributes,
54615468
m_readonly,
54625469
m_removed)
54635470
: r->new_global (playback_location (r, m_loc),
@@ -5466,6 +5473,7 @@ recording::global::replay_into (replayer *r)
54665473
playback_string (m_name),
54675474
m_flags,
54685475
m_string_attributes,
5476+
m_attributes,
54695477
m_readonly,
54705478
m_removed);
54715479

@@ -5535,6 +5543,7 @@ recording::global::write_to_dump (dump &d)
55355543
if (attribute)
55365544
d.write ("__attribute(%s(\"%s\"))__\n", attribute, value.c_str());
55375545
}
5546+
// TODO: handle m_attributes.
55385547
d.write ("%s %s",
55395548
m_type->get_debug_string (),
55405549
get_debug_string ());
@@ -5607,6 +5616,7 @@ static const char * const tls_model_enum_strings[] = {
56075616

56085617
static const char * const gcc_jit_variable_attribute_enum_strings[] = {
56095618
"GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY",
5619+
"GCC_JIT_VARIABLE_ATTRIBUTE_WEAK",
56105620
};
56115621

56125622
void
@@ -5643,6 +5653,7 @@ recording::global::write_reproducer (reproducer &r)
56435653
id,
56445654
gcc_jit_variable_attribute_enum_strings[std::get<0>(attribute)],
56455655
std::get<1>(attribute).c_str());
5656+
// TODO: handle m_attributes.
56465657

56475658

56485659
if (m_initializer)

gcc/jit/jit-recording.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,8 @@ class lvalue : public rvalue
14341434
m_reg_name (NULL),
14351435
m_tls_model (GCC_JIT_TLS_MODEL_NONE),
14361436
m_alignment (0),
1437-
m_string_attributes ()
1437+
m_string_attributes (),
1438+
m_attributes ()
14381439
{}
14391440

14401441
playback::lvalue *
@@ -1456,6 +1457,7 @@ class lvalue : public rvalue
14561457
const char *access_as_rvalue (reproducer &r) override;
14571458

14581459
void add_string_attribute (gcc_jit_variable_attribute attribute, const char* value);
1460+
void add_attribute (gcc_jit_variable_attribute attribute);
14591461

14601462
void set_readonly ()
14611463
{
@@ -1483,6 +1485,7 @@ class lvalue : public rvalue
14831485
unsigned m_alignment;
14841486
std::vector<std::pair<gcc_jit_variable_attribute,
14851487
std::string>> m_string_attributes;
1488+
std::vector<gcc_jit_variable_attribute> m_attributes;
14861489
bool m_readonly = false;
14871490
bool m_removed = false;
14881491
};

gcc/jit/libgccjit.cc

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

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

gcc/jit/libgccjit.h

+6
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,7 @@ gcc_jit_function_add_integer_array_attribute (
21812181
enum gcc_jit_variable_attribute
21822182
{
21832183
GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY,
2184+
GCC_JIT_VARIABLE_ATTRIBUTE_WEAK,
21842185

21852186
/* Maximum value of this enum, should always be last. */
21862187
GCC_JIT_VARIABLE_ATTRIBUTE_MAX,
@@ -2192,6 +2193,11 @@ gcc_jit_lvalue_add_string_attribute (gcc_jit_lvalue *variable,
21922193
enum gcc_jit_variable_attribute attribute,
21932194
const char* value);
21942195

2196+
/* Add an attribute to a variable. */
2197+
extern void
2198+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable,
2199+
enum gcc_jit_variable_attribute attribute);
2200+
21952201
extern gcc_jit_target_info *
21962202
gcc_jit_context_get_target_info (gcc_jit_context *ctxt);
21972203

gcc/jit/libgccjit.map

+4
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,7 @@ LIBGCCJIT_ABI_42 {
379379
global:
380380
gcc_jit_rvalue_set_type;
381381
} LIBGCCJIT_ABI_40;
382+
383+
LIBGCCJIT_ABI_42 {
384+
gcc_jit_lvalue_add_attribute;
385+
} LIBGCCJIT_ABI_41;

0 commit comments

Comments
 (0)