Skip to content

Commit 17ebc14

Browse files
committed
Implement bitcast
1 parent 5bb4c29 commit 17ebc14

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

gcc/jit/jit-playback.c

+14
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,20 @@ new_cast (playback::location *loc,
14111411
return new rvalue (this, t_cast);
14121412
}
14131413

1414+
playback::rvalue *
1415+
playback::context::
1416+
new_bitcast (location *loc,
1417+
rvalue *expr,
1418+
type *type_)
1419+
{
1420+
// TODO: use loc?
1421+
tree t_bitcast = build1 (VIEW_CONVERT_EXPR,
1422+
type_->as_tree (), expr->as_tree ());
1423+
if (loc)
1424+
set_tree_location (t_bitcast, loc);
1425+
return new rvalue (this, t_bitcast);
1426+
}
1427+
14141428
/* Construct a playback::lvalue instance (wrapping a tree) for an
14151429
array access. */
14161430

gcc/jit/jit-playback.h

+5
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ class context : public log_user
180180
rvalue *expr,
181181
type *type_);
182182

183+
rvalue *
184+
new_bitcast (location *loc,
185+
rvalue *expr,
186+
type *type_);
187+
183188
lvalue *
184189
new_array_access (location *loc,
185190
rvalue *ptr,

gcc/jit/jit-recording.c

+66
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,22 @@ recording::context::new_cast (recording::location *loc,
12461246
return result;
12471247
}
12481248

1249+
/* Create a recording::bitcast instance and add it to this context's list
1250+
of mementos.
1251+
1252+
Implements the post-error-checking part of
1253+
gcc_jit_context_new_bitcast. */
1254+
1255+
recording::rvalue *
1256+
recording::context::new_bitcast (location *loc,
1257+
rvalue *expr,
1258+
type *type_)
1259+
{
1260+
recording::rvalue *result = new bitcast (this, loc, expr, type_);
1261+
record (result);
1262+
return result;
1263+
}
1264+
12491265
/* Create a recording::call instance and add it to this context's list
12501266
of mementos.
12511267
@@ -5817,6 +5833,56 @@ recording::cast::write_reproducer (reproducer &r)
58175833
r.get_identifier_as_type (get_type ()));
58185834
}
58195835

5836+
/* Implementation of pure virtual hook recording::memento::replay_into
5837+
for recording::bitcast. */
5838+
5839+
void
5840+
recording::bitcast::replay_into (replayer *r)
5841+
{
5842+
set_playback_obj (r->new_bitcast (playback_location (r, m_loc),
5843+
m_rvalue->playback_rvalue (),
5844+
get_type ()->playback_type ()));
5845+
}
5846+
5847+
/* Implementation of pure virtual hook recording::rvalue::visit_children
5848+
for recording::bitcast. */
5849+
void
5850+
recording::bitcast::visit_children (rvalue_visitor *v)
5851+
{
5852+
v->visit (m_rvalue);
5853+
}
5854+
5855+
/* Implementation of recording::memento::make_debug_string for
5856+
casts. */
5857+
5858+
recording::string *
5859+
recording::bitcast::make_debug_string ()
5860+
{
5861+
enum precedence prec = get_precedence ();
5862+
return string::from_printf (m_ctxt,
5863+
"bitcast(%s, %s)",
5864+
m_rvalue->get_debug_string_parens (prec),
5865+
get_type ()->get_debug_string ());
5866+
}
5867+
5868+
/* Implementation of recording::memento::write_reproducer for casts. */
5869+
5870+
void
5871+
recording::bitcast::write_reproducer (reproducer &r)
5872+
{
5873+
const char *id = r.make_identifier (this, "rvalue");
5874+
r.write (" gcc_jit_rvalue *%s =\n"
5875+
" gcc_jit_context_new_bitcast (%s,\n"
5876+
" %s, /* gcc_jit_location *loc */\n"
5877+
" %s, /* gcc_jit_rvalue *rvalue */\n"
5878+
" %s); /* gcc_jit_type *type */\n",
5879+
id,
5880+
r.get_identifier (get_context ()),
5881+
r.get_identifier (m_loc),
5882+
r.get_identifier_as_rvalue (m_rvalue),
5883+
r.get_identifier_as_type (get_type ()));
5884+
}
5885+
58205886
/* The implementation of class gcc::jit::recording::base_call. */
58215887

58225888
/* The constructor for gcc::jit::recording::base_call. */

gcc/jit/jit-recording.h

+32
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ class context : public log_user
205205
rvalue *expr,
206206
type *type_);
207207

208+
rvalue *
209+
new_bitcast (location *loc,
210+
rvalue *expr,
211+
type *type_);
212+
208213
lvalue *
209214
new_array_access (location *loc,
210215
rvalue *ptr,
@@ -1691,6 +1696,33 @@ class cast : public rvalue
16911696
rvalue *m_rvalue;
16921697
};
16931698

1699+
class bitcast : public rvalue
1700+
{
1701+
public:
1702+
bitcast (context *ctxt,
1703+
location *loc,
1704+
rvalue *a,
1705+
type *type_)
1706+
: rvalue (ctxt, loc, type_),
1707+
m_rvalue (a)
1708+
{}
1709+
1710+
void replay_into (replayer *r) FINAL OVERRIDE;
1711+
1712+
void visit_children (rvalue_visitor *v) FINAL OVERRIDE;
1713+
1714+
private:
1715+
string * make_debug_string () FINAL OVERRIDE;
1716+
void write_reproducer (reproducer &r) FINAL OVERRIDE;
1717+
enum precedence get_precedence () const FINAL OVERRIDE
1718+
{
1719+
return PRECEDENCE_CAST;
1720+
}
1721+
1722+
private:
1723+
rvalue *m_rvalue;
1724+
};
1725+
16941726
class base_call : public rvalue
16951727
{
16961728
public:

gcc/jit/libgccjit.c

+28
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,34 @@ gcc_jit_context_new_cast (gcc_jit_context *ctxt,
24052405
return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
24062406
}
24072407

2408+
/* Public entrypoint. See description in libgccjit.h.
2409+
2410+
After error-checking, the real work is done by the
2411+
gcc::jit::recording::context::new_bitcast method in jit-recording.c. */
2412+
2413+
gcc_jit_rvalue *
2414+
gcc_jit_context_new_bitcast (gcc_jit_context *ctxt,
2415+
gcc_jit_location *loc,
2416+
gcc_jit_rvalue *rvalue,
2417+
gcc_jit_type *type)
2418+
{
2419+
RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
2420+
JIT_LOG_FUNC (ctxt->get_logger ());
2421+
/* LOC can be NULL. */
2422+
RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
2423+
RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
2424+
// TODO: check the sizes.
2425+
/*RETURN_NULL_IF_FAIL_PRINTF3 (
2426+
is_valid_cast (rvalue->get_type (), type),
2427+
ctxt, loc,
2428+
"cannot cast %s from type: %s to type: %s",
2429+
rvalue->get_debug_string (),
2430+
rvalue->get_type ()->get_debug_string (),
2431+
type->get_debug_string ());*/
2432+
2433+
return static_cast <gcc_jit_rvalue *> (ctxt->new_bitcast (loc, rvalue, type));
2434+
}
2435+
24082436
/* Public entrypoint. See description in libgccjit.h.
24092437
24102438
After error-checking, the real work is done by the

gcc/jit/libgccjit.h

+9
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,15 @@ gcc_jit_context_new_cast (gcc_jit_context *ctxt,
12171217
gcc_jit_rvalue *rvalue,
12181218
gcc_jit_type *type);
12191219

1220+
/* Reinterpret a value as another type.
1221+
1222+
The types must be of the same size. */
1223+
extern gcc_jit_rvalue *
1224+
gcc_jit_context_new_bitcast (gcc_jit_context *ctxt,
1225+
gcc_jit_location *loc,
1226+
gcc_jit_rvalue *rvalue,
1227+
gcc_jit_type *type);
1228+
12201229
extern gcc_jit_lvalue *
12211230
gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
12221231
gcc_jit_location *loc,

gcc/jit/libgccjit.map

+6
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,14 @@ LIBGCCJIT_ABI_18 {
238238
} LIBGCCJIT_ABI_17;
239239

240240
LIBGCCJIT_ABI_19 {
241+
global:
241242
gcc_jit_context_new_array_constructor;
242243
gcc_jit_context_new_struct_constructor;
243244
gcc_jit_context_new_union_constructor;
244245
gcc_jit_global_set_initializer_rvalue;
245246
} LIBGCCJIT_ABI_18;
247+
248+
LIBGCCJIT_ABI_20 {
249+
global:
250+
gcc_jit_context_new_bitcast;
251+
} LIBGCCJIT_ABI_19;

0 commit comments

Comments
 (0)