Skip to content

Commit e17fec1

Browse files
committed
WIP: Add support for variable attributes
1 parent fb37284 commit e17fec1

File tree

7 files changed

+91
-14
lines changed

7 files changed

+91
-14
lines changed

gcc/jit/jit-playback.cc

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,16 @@ const char* fn_attribute_to_string(gcc_jit_fn_attribute attr)
516516
return NULL;
517517
}
518518

519+
const char* variable_attribute_to_string(gcc_jit_variable_attribute attr)
520+
{
521+
switch (attr)
522+
{
523+
case GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY:
524+
return "visibility";
525+
}
526+
return NULL;
527+
}
528+
519529
/* Construct a playback::function instance. */
520530

521531
playback::function *
@@ -652,7 +662,8 @@ global_new_decl (location *loc,
652662
type *type,
653663
const char *name,
654664
enum global_var_flags flags,
655-
bool readonly)
665+
bool readonly,
666+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
656667
{
657668
gcc_assert (type);
658669
gcc_assert (name);
@@ -697,9 +708,27 @@ global_new_decl (location *loc,
697708
if (loc)
698709
set_tree_location (inner, loc);
699710

711+
set_variable_attribute (attributes, inner);
712+
700713
return inner;
701714
}
702715

716+
void
717+
playback::
718+
set_variable_attribute(const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes, tree decl)
719+
{
720+
for (auto attr: attributes)
721+
{
722+
gcc_jit_variable_attribute& name = std::get<0>(attr);
723+
std::string& value = std::get<1>(attr);
724+
tree attribute_value = build_tree_list (NULL_TREE, ::build_string (value.length () + 1, value.c_str ()));
725+
tree ident = get_identifier (variable_attribute_to_string (name));
726+
727+
DECL_ATTRIBUTES (decl) =
728+
tree_cons (ident, attribute_value, DECL_ATTRIBUTES (decl));
729+
}
730+
}
731+
703732
/* In use by new_global and new_global_initialized. */
704733

705734
playback::lvalue *
@@ -720,10 +749,11 @@ new_global (location *loc,
720749
type *type,
721750
const char *name,
722751
enum global_var_flags flags,
723-
bool readonly)
752+
bool readonly,
753+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
724754
{
725755
tree inner =
726-
global_new_decl (loc, kind, type, name, flags, readonly);
756+
global_new_decl (loc, kind, type, name, flags, readonly, attributes);
727757

728758
return global_finalize_lvalue (inner);
729759
}
@@ -869,9 +899,10 @@ new_global_initialized (location *loc,
869899
const void *initializer,
870900
const char *name,
871901
enum global_var_flags flags,
872-
bool readonly)
902+
bool readonly,
903+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
873904
{
874-
tree inner = global_new_decl (loc, kind, type, name, flags, readonly);
905+
tree inner = global_new_decl (loc, kind, type, name, flags, readonly, attributes);
875906

876907
vec<constructor_elt, va_gc> *constructor_elements = NULL;
877908

@@ -2033,7 +2064,8 @@ playback::lvalue *
20332064
playback::function::
20342065
new_local (location *loc,
20352066
type *type,
2036-
const char *name)
2067+
const char *name,
2068+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
20372069
{
20382070
gcc_assert (type);
20392071
gcc_assert (name);
@@ -2046,6 +2078,8 @@ new_local (location *loc,
20462078
DECL_CHAIN (inner) = BIND_EXPR_VARS (m_inner_bind_expr);
20472079
BIND_EXPR_VARS (m_inner_bind_expr) = inner;
20482080

2081+
set_variable_attribute (attributes, inner);
2082+
20492083
if (loc)
20502084
set_tree_location (inner, loc);
20512085
return new lvalue (m_ctxt, inner);

gcc/jit/jit-playback.h

Lines changed: 11 additions & 4 deletions
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:
@@ -117,7 +120,8 @@ class context : public log_user
117120
type *type,
118121
const char *name,
119122
enum global_var_flags flags,
120-
bool readonly);
123+
bool readonly,
124+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
121125

122126
lvalue *
123127
new_global_initialized (location *loc,
@@ -128,7 +132,8 @@ class context : public log_user
128132
const void *initializer,
129133
const char *name,
130134
enum global_var_flags flags,
131-
bool readonly);
135+
bool readonly,
136+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
132137

133138
rvalue *
134139
new_ctor (location *log,
@@ -331,7 +336,8 @@ class context : public log_user
331336
type *type,
332337
const char *name,
333338
enum global_var_flags flags,
334-
bool readonly);
339+
bool readonly,
340+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
335341
lvalue *
336342
global_finalize_lvalue (tree inner);
337343

@@ -519,7 +525,8 @@ class function : public wrapper
519525
lvalue *
520526
new_local (location *loc,
521527
type *type,
522-
const char *name);
528+
const char *name,
529+
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
523530

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

gcc/jit/jit-recording.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4098,6 +4098,11 @@ void recording::lvalue::set_alignment (unsigned bytes)
40984098
m_alignment = bytes;
40994099
}
41004100

4101+
void recording::lvalue::add_attribute (gcc_jit_variable_attribute attribute, const char* value)
4102+
{
4103+
m_attributes.push_back (std::make_pair (attribute, std::string (value)));
4104+
}
4105+
41014106
/* The implementation of class gcc::jit::recording::param. */
41024107

41034108
/* Implementation of pure virtual hook recording::memento::replay_into
@@ -4964,13 +4969,15 @@ recording::global::replay_into (replayer *r)
49644969
m_initializer,
49654970
playback_string (m_name),
49664971
m_flags,
4967-
m_readonly)
4972+
m_readonly,
4973+
m_attributes)
49684974
: r->new_global (playback_location (r, m_loc),
49694975
m_kind,
49704976
m_type->playback_type (),
49714977
playback_string (m_name),
49724978
m_flags,
4973-
m_readonly);
4979+
m_readonly,
4980+
m_attributes);
49744981

49754982
if (m_tls_model != GCC_JIT_TLS_MODEL_NONE)
49764983
global->set_tls_model (recording::tls_models[m_tls_model]);
@@ -6912,7 +6919,8 @@ recording::local::replay_into (replayer *r)
69126919
m_func->playback_function ()
69136920
->new_local (playback_location (r, m_loc),
69146921
m_type->playback_type (),
6915-
playback_string (m_name));
6922+
playback_string (m_name),
6923+
m_attributes);
69166924

69176925
if (m_reg_name != NULL)
69186926
local->set_register_name (m_reg_name->c_str ());

gcc/jit/jit-recording.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,8 @@ class lvalue : public rvalue
12951295
m_link_section (NULL),
12961296
m_reg_name (NULL),
12971297
m_tls_model (GCC_JIT_TLS_MODEL_NONE),
1298-
m_alignment (0)
1298+
m_alignment (0),
1299+
m_attributes ()
12991300
{}
13001301

13011302
playback::lvalue *
@@ -1319,6 +1320,8 @@ class lvalue : public rvalue
13191320
m_readonly = true;
13201321
}
13211322

1323+
void add_attribute (gcc_jit_variable_attribute attribute, const char* value);
1324+
13221325
const char *access_as_rvalue (reproducer &r) OVERRIDE;
13231326
virtual const char *access_as_lvalue (reproducer &r);
13241327
virtual bool is_global () const { return false; }
@@ -1334,6 +1337,7 @@ class lvalue : public rvalue
13341337
enum gcc_jit_tls_model m_tls_model;
13351338
unsigned m_alignment;
13361339
bool m_readonly = false;
1340+
std::vector<std::pair<gcc_jit_variable_attribute, std::string>> m_attributes;
13371341
};
13381342

13391343
class param : public lvalue

gcc/jit/libgccjit.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,6 +4091,14 @@ gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute att
40914091
func->add_attribute (attribute, value);
40924092
}
40934093

4094+
void
4095+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable, gcc_jit_variable_attribute attribute, const char* value)
4096+
{
4097+
RETURN_IF_FAIL (variable, NULL, NULL, "NULL variable");
4098+
4099+
variable->add_attribute (attribute, value);
4100+
}
4101+
40944102
/* Public entrypoint. See description in libgccjit.h.
40954103
40964104
After error-checking, the real work is done by the

gcc/jit/libgccjit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,17 @@ enum gcc_jit_fn_attribute
20482048
extern void
20492049
gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute, const char* value);
20502050

2051+
/* Variable attributes. */
2052+
enum gcc_jit_variable_attribute
2053+
{
2054+
GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY,
2055+
};
2056+
2057+
/* Add an attribute to a variable. */
2058+
// TODO: also support integer values.
2059+
extern void
2060+
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable, gcc_jit_variable_attribute attribute, const char* value);
2061+
20512062
#ifdef __cplusplus
20522063
}
20532064
#endif /* __cplusplus */

gcc/jit/libgccjit.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,8 @@ LIBGCCJIT_ABI_30 {
303303
global:
304304
gcc_jit_function_add_attribute;
305305
} LIBGCCJIT_ABI_29;
306+
307+
LIBGCCJIT_ABI_31 {
308+
global:
309+
gcc_jit_lvalue_add_attribute;
310+
} LIBGCCJIT_ABI_30;

0 commit comments

Comments
 (0)