Skip to content

Commit

Permalink
WIP: Add support for variable attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Jul 24, 2022
1 parent fb37284 commit e17fec1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 14 deletions.
46 changes: 40 additions & 6 deletions gcc/jit/jit-playback.cc
Expand Up @@ -516,6 +516,16 @@ const char* fn_attribute_to_string(gcc_jit_fn_attribute attr)
return NULL;
}

const char* variable_attribute_to_string(gcc_jit_variable_attribute attr)
{
switch (attr)
{
case GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY:
return "visibility";
}
return NULL;
}

/* Construct a playback::function instance. */

playback::function *
Expand Down Expand Up @@ -652,7 +662,8 @@ global_new_decl (location *loc,
type *type,
const char *name,
enum global_var_flags flags,
bool readonly)
bool readonly,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
{
gcc_assert (type);
gcc_assert (name);
Expand Down Expand Up @@ -697,9 +708,27 @@ global_new_decl (location *loc,
if (loc)
set_tree_location (inner, loc);

set_variable_attribute (attributes, inner);

return inner;
}

void
playback::
set_variable_attribute(const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes, tree decl)
{
for (auto attr: attributes)
{
gcc_jit_variable_attribute& name = std::get<0>(attr);
std::string& value = std::get<1>(attr);
tree attribute_value = build_tree_list (NULL_TREE, ::build_string (value.length () + 1, value.c_str ()));
tree ident = get_identifier (variable_attribute_to_string (name));

DECL_ATTRIBUTES (decl) =
tree_cons (ident, attribute_value, DECL_ATTRIBUTES (decl));
}
}

/* In use by new_global and new_global_initialized. */

playback::lvalue *
Expand All @@ -720,10 +749,11 @@ new_global (location *loc,
type *type,
const char *name,
enum global_var_flags flags,
bool readonly)
bool readonly,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
{
tree inner =
global_new_decl (loc, kind, type, name, flags, readonly);
global_new_decl (loc, kind, type, name, flags, readonly, attributes);

return global_finalize_lvalue (inner);
}
Expand Down Expand Up @@ -869,9 +899,10 @@ new_global_initialized (location *loc,
const void *initializer,
const char *name,
enum global_var_flags flags,
bool readonly)
bool readonly,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
{
tree inner = global_new_decl (loc, kind, type, name, flags, readonly);
tree inner = global_new_decl (loc, kind, type, name, flags, readonly, attributes);

vec<constructor_elt, va_gc> *constructor_elements = NULL;

Expand Down Expand Up @@ -2033,7 +2064,8 @@ playback::lvalue *
playback::function::
new_local (location *loc,
type *type,
const char *name)
const char *name,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
{
gcc_assert (type);
gcc_assert (name);
Expand All @@ -2046,6 +2078,8 @@ new_local (location *loc,
DECL_CHAIN (inner) = BIND_EXPR_VARS (m_inner_bind_expr);
BIND_EXPR_VARS (m_inner_bind_expr) = inner;

set_variable_attribute (attributes, inner);

if (loc)
set_tree_location (inner, loc);
return new lvalue (m_ctxt, inner);
Expand Down
15 changes: 11 additions & 4 deletions gcc/jit/jit-playback.h
Expand Up @@ -43,6 +43,9 @@ namespace jit {

namespace playback {

void
set_variable_attribute(const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes, tree decl);

/* playback::context is an abstract base class.
The two concrete subclasses are:
Expand Down Expand Up @@ -117,7 +120,8 @@ class context : public log_user
type *type,
const char *name,
enum global_var_flags flags,
bool readonly);
bool readonly,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);

lvalue *
new_global_initialized (location *loc,
Expand All @@ -128,7 +132,8 @@ class context : public log_user
const void *initializer,
const char *name,
enum global_var_flags flags,
bool readonly);
bool readonly,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);

rvalue *
new_ctor (location *log,
Expand Down Expand Up @@ -331,7 +336,8 @@ class context : public log_user
type *type,
const char *name,
enum global_var_flags flags,
bool readonly);
bool readonly,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
lvalue *
global_finalize_lvalue (tree inner);

Expand Down Expand Up @@ -519,7 +525,8 @@ class function : public wrapper
lvalue *
new_local (location *loc,
type *type,
const char *name);
const char *name,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);

block*
new_block (const char *name);
Expand Down
14 changes: 11 additions & 3 deletions gcc/jit/jit-recording.cc
Expand Up @@ -4098,6 +4098,11 @@ void recording::lvalue::set_alignment (unsigned bytes)
m_alignment = bytes;
}

void recording::lvalue::add_attribute (gcc_jit_variable_attribute attribute, const char* value)
{
m_attributes.push_back (std::make_pair (attribute, std::string (value)));
}

/* The implementation of class gcc::jit::recording::param. */

/* Implementation of pure virtual hook recording::memento::replay_into
Expand Down Expand Up @@ -4964,13 +4969,15 @@ recording::global::replay_into (replayer *r)
m_initializer,
playback_string (m_name),
m_flags,
m_readonly)
m_readonly,
m_attributes)
: r->new_global (playback_location (r, m_loc),
m_kind,
m_type->playback_type (),
playback_string (m_name),
m_flags,
m_readonly);
m_readonly,
m_attributes);

if (m_tls_model != GCC_JIT_TLS_MODEL_NONE)
global->set_tls_model (recording::tls_models[m_tls_model]);
Expand Down Expand Up @@ -6912,7 +6919,8 @@ recording::local::replay_into (replayer *r)
m_func->playback_function ()
->new_local (playback_location (r, m_loc),
m_type->playback_type (),
playback_string (m_name));
playback_string (m_name),
m_attributes);

if (m_reg_name != NULL)
local->set_register_name (m_reg_name->c_str ());
Expand Down
6 changes: 5 additions & 1 deletion gcc/jit/jit-recording.h
Expand Up @@ -1295,7 +1295,8 @@ class lvalue : public rvalue
m_link_section (NULL),
m_reg_name (NULL),
m_tls_model (GCC_JIT_TLS_MODEL_NONE),
m_alignment (0)
m_alignment (0),
m_attributes ()
{}

playback::lvalue *
Expand All @@ -1319,6 +1320,8 @@ class lvalue : public rvalue
m_readonly = true;
}

void add_attribute (gcc_jit_variable_attribute attribute, const char* value);

const char *access_as_rvalue (reproducer &r) OVERRIDE;
virtual const char *access_as_lvalue (reproducer &r);
virtual bool is_global () const { return false; }
Expand All @@ -1334,6 +1337,7 @@ class lvalue : public rvalue
enum gcc_jit_tls_model m_tls_model;
unsigned m_alignment;
bool m_readonly = false;
std::vector<std::pair<gcc_jit_variable_attribute, std::string>> m_attributes;
};

class param : public lvalue
Expand Down
8 changes: 8 additions & 0 deletions gcc/jit/libgccjit.cc
Expand Up @@ -4091,6 +4091,14 @@ gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute att
func->add_attribute (attribute, value);
}

void
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable, gcc_jit_variable_attribute attribute, const char* value)
{
RETURN_IF_FAIL (variable, NULL, NULL, "NULL variable");

variable->add_attribute (attribute, value);
}

/* Public entrypoint. See description in libgccjit.h.
After error-checking, the real work is done by the
Expand Down
11 changes: 11 additions & 0 deletions gcc/jit/libgccjit.h
Expand Up @@ -2048,6 +2048,17 @@ enum gcc_jit_fn_attribute
extern void
gcc_jit_function_add_attribute (gcc_jit_function *func, gcc_jit_fn_attribute attribute, const char* value);

/* Variable attributes. */
enum gcc_jit_variable_attribute
{
GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY,
};

/* Add an attribute to a variable. */
// TODO: also support integer values.
extern void
gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable, gcc_jit_variable_attribute attribute, const char* value);

#ifdef __cplusplus
}
#endif /* __cplusplus */
Expand Down
5 changes: 5 additions & 0 deletions gcc/jit/libgccjit.map
Expand Up @@ -303,3 +303,8 @@ LIBGCCJIT_ABI_30 {
global:
gcc_jit_function_add_attribute;
} LIBGCCJIT_ABI_29;

LIBGCCJIT_ABI_31 {
global:
gcc_jit_lvalue_add_attribute;
} LIBGCCJIT_ABI_30;

0 comments on commit e17fec1

Please sign in to comment.