Skip to content

Commit 9022bee

Browse files
committed
Add support for packed struct
1 parent 2afbf55 commit 9022bee

7 files changed

+47
-7
lines changed

gcc/jit/jit-playback.cc

+10-2
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ playback::compound_type *
405405
playback::context::
406406
new_compound_type (location *loc,
407407
const char *name,
408-
bool is_struct) /* else is union */
408+
bool is_struct, /* else is union */
409+
bool is_packed)
409410
{
410411
gcc_assert (name);
411412

@@ -415,14 +416,17 @@ new_compound_type (location *loc,
415416
TYPE_NAME (t) = get_identifier (name);
416417
TYPE_SIZE (t) = 0;
417418

419+
if (is_packed)
420+
TYPE_PACKED (t) = 1;
421+
418422
if (loc)
419423
set_tree_location (t, loc);
420424

421425
return new compound_type (t);
422426
}
423427

424428
void
425-
playback::compound_type::set_fields (const auto_vec<playback::field *> *fields)
429+
playback::compound_type::set_fields (const auto_vec<playback::field *> *fields, bool is_packed)
426430
{
427431
/* Compare with c/c-decl.cc: finish_struct. */
428432
tree t = as_tree ();
@@ -439,6 +443,10 @@ playback::compound_type::set_fields (const auto_vec<playback::field *> *fields)
439443
DECL_SIZE (x) = bitsize_int (width);
440444
DECL_BIT_FIELD (x) = 1;
441445
}
446+
447+
if (is_packed && (DECL_BIT_FIELD (x)
448+
|| TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
449+
DECL_PACKED (x) = 1;
442450
fieldlist = chainon (x, fieldlist);
443451
}
444452
fieldlist = nreverse (fieldlist);

gcc/jit/jit-playback.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class context : public log_user
8585
compound_type *
8686
new_compound_type (location *loc,
8787
const char *name,
88-
bool is_struct); /* else is union */
88+
bool is_struct, /* else is union */
89+
bool is_packed);
8990

9091
type *
9192
new_function_type (type *return_type,
@@ -472,7 +473,7 @@ class compound_type : public type
472473
: type (inner)
473474
{}
474475

475-
void set_fields (const auto_vec<field *> *fields);
476+
void set_fields (const auto_vec<field *> *fields, bool is_packed);
476477
};
477478

478479
class field : public wrapper

gcc/jit/jit-recording.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,12 @@ recording::type::get_aligned (size_t alignment_in_bytes)
23852385
return result;
23862386
}
23872387

2388+
void
2389+
recording::type::set_packed ()
2390+
{
2391+
m_packed = true;
2392+
}
2393+
23882394
/* Given a type, get a vector version of the type.
23892395
23902396
Implements the post-error-checking part of
@@ -3578,7 +3584,8 @@ recording::struct_::replay_into (replayer *r)
35783584
set_playback_obj (
35793585
r->new_compound_type (playback_location (r, get_loc ()),
35803586
get_name ()->c_str (),
3581-
true /* is_struct */));
3587+
true, /* is_struct */
3588+
m_packed));
35823589
}
35833590

35843591
const char *
@@ -3632,7 +3639,8 @@ recording::union_::replay_into (replayer *r)
36323639
set_playback_obj (
36333640
r->new_compound_type (playback_location (r, get_loc ()),
36343641
get_name ()->c_str (),
3635-
false /* is_struct */));
3642+
false, /* is_struct */
3643+
m_packed));
36363644
}
36373645

36383646
/* Implementation of recording::memento::make_debug_string for
@@ -3703,7 +3711,7 @@ recording::fields::replay_into (replayer *)
37033711
playback_fields.create (m_fields.length ());
37043712
for (unsigned i = 0; i < m_fields.length (); i++)
37053713
playback_fields.safe_push (m_fields[i]->playback_field ());
3706-
m_struct_or_union->playback_compound_type ()->set_fields (&playback_fields);
3714+
m_struct_or_union->playback_compound_type ()->set_fields (&playback_fields, m_struct_or_union->m_packed);
37073715
}
37083716

37093717
/* Override the default implementation of

gcc/jit/jit-recording.h

+6
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ class type : public memento
545545
type *get_aligned (size_t alignment_in_bytes);
546546
type *get_vector (size_t num_units);
547547

548+
void set_packed ();
549+
548550
/* Get the type obtained when dereferencing this type.
549551
550552
This will return NULL if it's not valid to dereference this type.
@@ -613,9 +615,13 @@ class type : public memento
613615
protected:
614616
type (context *ctxt)
615617
: memento (ctxt),
618+
m_packed (false),
616619
m_pointer_to_this_type (NULL)
617620
{}
618621

622+
public:
623+
bool m_packed;
624+
619625
private:
620626
type *m_pointer_to_this_type;
621627
};

gcc/jit/libgccjit.cc

+8
Original file line numberDiff line numberDiff line change
@@ -4017,6 +4017,14 @@ gcc_jit_type_get_aligned (gcc_jit_type *type,
40174017
return (gcc_jit_type *)type->get_aligned (alignment_in_bytes);
40184018
}
40194019

4020+
void
4021+
gcc_jit_type_set_packed (gcc_jit_type *type)
4022+
{
4023+
RETURN_IF_FAIL (type, NULL, NULL, "NULL type");
4024+
4025+
type->set_packed ();
4026+
}
4027+
40204028
/* Public entrypoint. See description in libgccjit.h.
40214029
40224030
After error-checking, the real work is done by the

gcc/jit/libgccjit.h

+4
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,10 @@ gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type);
20192019
extern gcc_jit_type *
20202020
gcc_jit_type_unqualified (gcc_jit_type *type);
20212021

2022+
/* Given type "T", get type "T __attribute__ ((packed))". */
2023+
extern void
2024+
gcc_jit_type_set_packed (gcc_jit_type *type);
2025+
20222026
#ifdef __cplusplus
20232027
}
20242028
#endif /* __cplusplus */

gcc/jit/libgccjit.map

+5
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,8 @@ LIBGCCJIT_ABI_25 {
278278
gcc_jit_context_new_vector_constructor;
279279
gcc_jit_context_new_vector_access;
280280
} LIBGCCJIT_ABI_24;
281+
282+
LIBGCCJIT_ABI_26 {
283+
global:
284+
gcc_jit_type_set_packed;
285+
} LIBGCCJIT_ABI_25;

0 commit comments

Comments
 (0)