Skip to content

Commit e7f055b

Browse files
committed
WIP: Add support for variable attributes
1 parent a81a37f commit e7f055b

7 files changed

+91
-14
lines changed

gcc/jit/jit-playback.cc

+40-6
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,16 @@ const char* fn_attribute_to_string(gcc_jit_fn_attribute attr)
522522
return NULL;
523523
}
524524

525+
const char* variable_attribute_to_string(gcc_jit_variable_attribute attr)
526+
{
527+
switch (attr)
528+
{
529+
case GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY:
530+
return "visibility";
531+
}
532+
return NULL;
533+
}
534+
525535
/* Construct a playback::function instance. */
526536

527537
playback::function *
@@ -674,7 +684,8 @@ global_new_decl (location *loc,
674684
type *type,
675685
const char *name,
676686
enum global_var_flags flags,
677-
bool readonly)
687+
bool readonly,
688+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
678689
{
679690
gcc_assert (type);
680691
gcc_assert (name);
@@ -719,9 +730,27 @@ global_new_decl (location *loc,
719730
if (loc)
720731
set_tree_location (inner, loc);
721732

733+
set_variable_attribute (attributes, inner);
734+
722735
return inner;
723736
}
724737

738+
void
739+
playback::
740+
set_variable_attribute(const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes, tree decl)
741+
{
742+
for (auto attr: attributes)
743+
{
744+
gcc_jit_variable_attribute& name = std::get<0>(attr);
745+
std::string& value = std::get<1>(attr);
746+
tree attribute_value = build_tree_list (NULL_TREE, ::build_string (value.length () + 1, value.c_str ()));
747+
tree ident = get_identifier (variable_attribute_to_string (name));
748+
749+
DECL_ATTRIBUTES (decl) =
750+
tree_cons (ident, attribute_value, DECL_ATTRIBUTES (decl));
751+
}
752+
}
753+
725754
/* In use by new_global and new_global_initialized. */
726755

727756
playback::lvalue *
@@ -742,10 +771,11 @@ new_global (location *loc,
742771
type *type,
743772
const char *name,
744773
enum global_var_flags flags,
745-
bool readonly)
774+
bool readonly,
775+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
746776
{
747777
tree inner =
748-
global_new_decl (loc, kind, type, name, flags, readonly);
778+
global_new_decl (loc, kind, type, name, flags, readonly, attributes);
749779

750780
return global_finalize_lvalue (inner);
751781
}
@@ -891,9 +921,10 @@ new_global_initialized (location *loc,
891921
const void *initializer,
892922
const char *name,
893923
enum global_var_flags flags,
894-
bool readonly)
924+
bool readonly,
925+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
895926
{
896-
tree inner = global_new_decl (loc, kind, type, name, flags, readonly);
927+
tree inner = global_new_decl (loc, kind, type, name, flags, readonly, attributes);
897928

898929
vec<constructor_elt, va_gc> *constructor_elements = NULL;
899930

@@ -2072,7 +2103,8 @@ playback::lvalue *
20722103
playback::function::
20732104
new_local (location *loc,
20742105
type *type,
2075-
const char *name)
2106+
const char *name,
2107+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
20762108
{
20772109
gcc_assert (type);
20782110
gcc_assert (name);
@@ -2085,6 +2117,8 @@ new_local (location *loc,
20852117
DECL_CHAIN (inner) = BIND_EXPR_VARS (m_inner_bind_expr);
20862118
BIND_EXPR_VARS (m_inner_bind_expr) = inner;
20872119

2120+
set_variable_attribute (attributes, inner);
2121+
20882122
if (loc)
20892123
set_tree_location (inner, loc);
20902124
return new lvalue (m_ctxt, inner);

gcc/jit/jit-playback.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ namespace jit {
4343

4444
namespace playback {
4545

46+
void
47+
set_variable_attribute(const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes, tree decl);
48+
4649
/* playback::context is an abstract base class.
4750
4851
The two concrete subclasses are:
@@ -118,7 +121,8 @@ class context : public log_user
118121
type *type,
119122
const char *name,
120123
enum global_var_flags flags,
121-
bool readonly);
124+
bool readonly,
125+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
122126

123127
lvalue *
124128
new_global_initialized (location *loc,
@@ -129,7 +133,8 @@ class context : public log_user
129133
const void *initializer,
130134
const char *name,
131135
enum global_var_flags flags,
132-
bool readonly);
136+
bool readonly,
137+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
133138

134139
rvalue *
135140
new_ctor (location *log,
@@ -332,7 +337,8 @@ class context : public log_user
332337
type *type,
333338
const char *name,
334339
enum global_var_flags flags,
335-
bool readonly);
340+
bool readonly,
341+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
336342
lvalue *
337343
global_finalize_lvalue (tree inner);
338344

@@ -520,7 +526,8 @@ class function : public wrapper
520526
lvalue *
521527
new_local (location *loc,
522528
type *type,
523-
const char *name);
529+
const char *name,
530+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
524531

525532
block*
526533
new_block (const char *name);

gcc/jit/jit-recording.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,11 @@ void recording::lvalue::set_alignment (unsigned bytes)
40954095
m_alignment = bytes;
40964096
}
40974097

4098+
void recording::lvalue::add_attribute (gcc_jit_variable_attribute attribute, const char* value)
4099+
{
4100+
m_attributes.push_back (std::make_pair (attribute, std::string (value)));
4101+
}
4102+
40984103
/* The implementation of class gcc::jit::recording::param. */
40994104

41004105
/* Implementation of pure virtual hook recording::memento::replay_into
@@ -4969,13 +4974,15 @@ recording::global::replay_into (replayer *r)
49694974
m_initializer,
49704975
playback_string (m_name),
49714976
m_flags,
4972-
m_readonly)
4977+
m_readonly,
4978+
m_attributes)
49734979
: r->new_global (playback_location (r, m_loc),
49744980
m_kind,
49754981
m_type->playback_type (),
49764982
playback_string (m_name),
49774983
m_flags,
4978-
m_readonly);
4984+
m_readonly,
4985+
m_attributes);
49794986

49804987
if (m_tls_model != GCC_JIT_TLS_MODEL_NONE)
49814988
global->set_tls_model (recording::tls_models[m_tls_model]);
@@ -6916,7 +6923,8 @@ recording::local::replay_into (replayer *r)
69166923
playback::lvalue *obj = m_func->playback_function ()
69176924
->new_local (playback_location (r, m_loc),
69186925
m_type->playback_type (),
6919-
playback_string (m_name));
6926+
playback_string (m_name),
6927+
m_attributes);
69206928

69216929
if (m_reg_name != NULL)
69226930
obj->set_register_name (m_reg_name->c_str ());

gcc/jit/jit-recording.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,8 @@ class lvalue : public rvalue
13031303
m_link_section (NULL),
13041304
m_reg_name (NULL),
13051305
m_tls_model (GCC_JIT_TLS_MODEL_NONE),
1306-
m_alignment (0)
1306+
m_alignment (0),
1307+
m_attributes ()
13071308
{}
13081309

13091310
playback::lvalue *
@@ -1329,6 +1330,8 @@ class lvalue : public rvalue
13291330
m_readonly = true;
13301331
}
13311332

1333+
void add_attribute (gcc_jit_variable_attribute attribute, const char* value);
1334+
13321335
virtual const char *access_as_lvalue (reproducer &r);
13331336
virtual bool is_global () const { return false; }
13341337
void set_tls_model (enum gcc_jit_tls_model model);
@@ -1343,6 +1346,7 @@ class lvalue : public rvalue
13431346
enum gcc_jit_tls_model m_tls_model;
13441347
unsigned m_alignment;
13451348
bool m_readonly = false;
1349+
std::vector<std::pair<gcc_jit_variable_attribute, std::string>> m_attributes;
13461350
};
13471351

13481352
class param : public lvalue

gcc/jit/libgccjit.cc

+8
Original file line numberDiff line numberDiff line change
@@ -4111,6 +4111,14 @@ gcc_jit_function_add_string_attribute (gcc_jit_function *func, gcc_jit_fn_attrib
41114111
func->add_string_attribute (attribute, value);
41124112
}
41134113

4114+
void
4115+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable, gcc_jit_variable_attribute attribute, const char* value)
4116+
{
4117+
RETURN_IF_FAIL (variable, NULL, NULL, "NULL variable");
4118+
4119+
variable->add_attribute (attribute, value);
4120+
}
4121+
41144122
/* Public entrypoint. See description in libgccjit.h.
41154123
41164124
After error-checking, the real work is done by the

gcc/jit/libgccjit.h

+11
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,17 @@ gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute att
20612061
extern void
20622062
gcc_jit_function_add_string_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute, const char* value);
20632063

2064+
/* Variable attributes. */
2065+
enum gcc_jit_variable_attribute
2066+
{
2067+
GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY,
2068+
};
2069+
2070+
/* Add an attribute to a variable. */
2071+
// TODO: also support integer values.
2072+
extern void
2073+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable, gcc_jit_variable_attribute attribute, const char* value);
2074+
20642075
#ifdef __cplusplus
20652076
}
20662077
#endif /* __cplusplus */

gcc/jit/libgccjit.map

+5
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,8 @@ LIBGCCJIT_ABI_30 {
305305
gcc_jit_function_add_attribute;
306306
gcc_jit_function_add_string_attribute;
307307
} LIBGCCJIT_ABI_29;
308+
309+
LIBGCCJIT_ABI_31 {
310+
global:
311+
gcc_jit_lvalue_add_attribute;
312+
} LIBGCCJIT_ABI_30;

0 commit comments

Comments
 (0)