Skip to content

Commit bcafd46

Browse files
committed
Add support for the weak variable attribute
1 parent fd3498b commit bcafd46

7 files changed

+74
-9
lines changed

gcc/jit/jit-playback.cc

+26-6
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ const char* variable_attribute_to_string (gcc_jit_variable_attribute attr)
590590
{
591591
case GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY:
592592
return "visibility";
593+
case GCC_JIT_VARIABLE_ATTRIBUTE_WEAK:
594+
return "weak";
593595
case GCC_JIT_VARIABLE_ATTRIBUTE_MAX:
594596
return NULL;
595597
}
@@ -775,7 +777,8 @@ global_new_decl (location *loc,
775777
const char *name,
776778
enum global_var_flags flags,
777779
const std::vector<std::pair<gcc_jit_variable_attribute,
778-
std::string>> &attributes,
780+
std::string>> &string_attributes,
781+
const std::vector<gcc_jit_variable_attribute> &attributes,
779782
bool readonly,
780783
bool removed)
781784
{
@@ -831,7 +834,19 @@ global_new_decl (location *loc,
831834
if (loc)
832835
set_tree_location (inner, loc);
833836

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

836851
return inner;
837852
}
@@ -879,12 +894,14 @@ new_global (location *loc,
879894
const char *name,
880895
enum global_var_flags flags,
881896
const std::vector<std::pair<gcc_jit_variable_attribute,
882-
std::string>> &attributes,
897+
std::string>> &string_attributes,
898+
const std::vector<gcc_jit_variable_attribute> &attributes,
883899
bool readonly,
884900
bool removed)
885901
{
886902
tree inner =
887-
global_new_decl (loc, kind, type, name, flags, attributes, readonly, removed);
903+
global_new_decl (loc, kind, type, name, flags, string_attributes,
904+
attributes, readonly, removed);
888905

889906
return global_finalize_lvalue (inner, removed);
890907
}
@@ -1031,11 +1048,14 @@ new_global_initialized (location *loc,
10311048
const char *name,
10321049
enum global_var_flags flags,
10331050
const std::vector<std::pair<gcc_jit_variable_attribute,
1034-
std::string>> &attributes,
1051+
std::string>> &string_attributes,
1052+
const std::vector<gcc_jit_variable_attribute>
1053+
&attributes,
10351054
bool readonly,
10361055
bool removed)
10371056
{
1038-
tree inner = global_new_decl (loc, kind, type, name, flags, attributes, readonly, removed);
1057+
tree inner = global_new_decl (loc, kind, type, name, flags,
1058+
string_attributes, attributes, readonly, removed);
10391059

10401060
vec<constructor_elt, va_gc> *constructor_elements = NULL;
10411061

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
@@ -4398,6 +4398,12 @@ void recording::lvalue::add_string_attribute (
43984398
m_string_attributes.push_back (std::make_pair (attribute, std::string (value)));
43994399
}
44004400

4401+
void recording::lvalue::add_attribute (
4402+
gcc_jit_variable_attribute attribute)
4403+
{
4404+
m_attributes.push_back (attribute);
4405+
}
4406+
44014407
/* The implementation of class gcc::jit::recording::param. */
44024408

44034409
/* Implementation of pure virtual hook recording::memento::replay_into
@@ -5453,6 +5459,7 @@ recording::global::replay_into (replayer *r)
54535459
playback_string (m_name),
54545460
m_flags,
54555461
m_string_attributes,
5462+
m_attributes,
54565463
m_readonly,
54575464
m_removed)
54585465
: r->new_global (playback_location (r, m_loc),
@@ -5461,6 +5468,7 @@ recording::global::replay_into (replayer *r)
54615468
playback_string (m_name),
54625469
m_flags,
54635470
m_string_attributes,
5471+
m_attributes,
54645472
m_readonly,
54655473
m_removed);
54665474

@@ -5530,6 +5538,7 @@ recording::global::write_to_dump (dump &d)
55305538
if (attribute)
55315539
d.write ("__attribute(%s(\"%s\"))__\n", attribute, value.c_str());
55325540
}
5541+
// TODO: handle m_attributes.
55335542
d.write ("%s %s",
55345543
m_type->get_debug_string (),
55355544
get_debug_string ());
@@ -5602,6 +5611,7 @@ static const char * const tls_model_enum_strings[] = {
56025611

56035612
static const char * const gcc_jit_variable_attribute_enum_strings[] = {
56045613
"GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY",
5614+
"GCC_JIT_VARIABLE_ATTRIBUTE_WEAK",
56055615
};
56065616

56075617
void
@@ -5638,6 +5648,7 @@ recording::global::write_reproducer (reproducer &r)
56385648
id,
56395649
gcc_jit_variable_attribute_enum_strings[std::get<0>(attribute)],
56405650
std::get<1>(attribute).c_str());
5651+
// TODO: handle m_attributes.
56415652

56425653

56435654
if (m_initializer)

gcc/jit/jit-recording.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,8 @@ class lvalue : public rvalue
14291429
m_reg_name (NULL),
14301430
m_tls_model (GCC_JIT_TLS_MODEL_NONE),
14311431
m_alignment (0),
1432-
m_string_attributes ()
1432+
m_string_attributes (),
1433+
m_attributes ()
14331434
{}
14341435

14351436
playback::lvalue *
@@ -1451,6 +1452,7 @@ class lvalue : public rvalue
14511452
const char *access_as_rvalue (reproducer &r) override;
14521453

14531454
void add_string_attribute (gcc_jit_variable_attribute attribute, const char* value);
1455+
void add_attribute (gcc_jit_variable_attribute attribute);
14541456

14551457
void set_readonly ()
14561458
{
@@ -1478,6 +1480,7 @@ class lvalue : public rvalue
14781480
unsigned m_alignment;
14791481
std::vector<std::pair<gcc_jit_variable_attribute,
14801482
std::string>> m_string_attributes;
1483+
std::vector<gcc_jit_variable_attribute> m_attributes;
14811484
bool m_readonly = false;
14821485
bool m_removed = false;
14831486
};

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
@@ -2176,6 +2176,7 @@ gcc_jit_function_add_integer_array_attribute (
21762176
enum gcc_jit_variable_attribute
21772177
{
21782178
GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY,
2179+
GCC_JIT_VARIABLE_ATTRIBUTE_WEAK,
21792180

21802181
/* Maximum value of this enum, should always be last. */
21812182
GCC_JIT_VARIABLE_ATTRIBUTE_MAX,
@@ -2187,6 +2188,11 @@ gcc_jit_lvalue_add_string_attribute (gcc_jit_lvalue *variable,
21872188
enum gcc_jit_variable_attribute attribute,
21882189
const char* value);
21892190

2191+
/* Add an attribute to a variable. */
2192+
extern void
2193+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable,
2194+
enum gcc_jit_variable_attribute attribute);
2195+
21902196
extern gcc_jit_target_info *
21912197
gcc_jit_context_get_target_info (gcc_jit_context *ctxt);
21922198

gcc/jit/libgccjit.map

+4
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,7 @@ LIBGCCJIT_ABI_41 {
368368
gcc_jit_lvalue_remove;
369369
gcc_jit_rvalue_set_type;
370370
} LIBGCCJIT_ABI_40;
371+
372+
LIBGCCJIT_ABI_42 {
373+
gcc_jit_lvalue_add_attribute;
374+
} LIBGCCJIT_ABI_41;

0 commit comments

Comments
 (0)