From 193ac49bc63dce7e72d1f520f0eb3b66a4d825b4 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 10 Nov 2016 13:16:59 -0500 Subject: [PATCH] Remove dependency on GNU C extensions __typeof__() dependency is removed by passing the type to the macro explicitly. Statement expression dependency is removed by using an in/out macro parameter instead. Signed-off-by: Philippe Proulx --- barectf/gen.py | 16 +++++++---- barectf/templates.py | 65 ++++++++++++++++++-------------------------- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/barectf/gen.py b/barectf/gen.py index 45d558c..de2511a 100644 --- a/barectf/gen.py +++ b/barectf/gen.py @@ -567,17 +567,19 @@ class CCodeGenerator: tmpl = templates._FUNC_SERIALIZE_EVENT_PROTO_END self._cg.add_lines(tmpl) - def _generate_bitfield_write(self, var, ctx, t): + def _generate_bitfield_write(self, ctype, var, ctx, t): ptr = '&{ctx}->buf[_BITS_TO_BYTES({ctx}->at)]'.format(ctx=ctx) start = self._sasa.byte_offset suffix = 'le' if t.byte_order is metadata.ByteOrder.LE else 'be' func = '{}bt_bitfield_write_{}'.format(self._cfg.prefix, suffix) - call = '{}({}, uint8_t, {}, {}, {});'.format(func, ptr, start, t.size, - var) + call_fmt = '{func}({ptr}, uint8_t, {start}, {size}, {ctype}, ({ctype}) {var});' + call = call_fmt.format(func=func, ptr=ptr, start=start, size=t.size, + ctype=ctype, var=var) self._cg.add_line(call) def _generate_serialize_int(self, var, ctx, t): - self._generate_bitfield_write(var, ctx, t) + ctype = self._get_int_ctype(t) + self._generate_bitfield_write(ctype, var, ctx, t) self._generate_incr_pos('{}->at'.format(ctx), t.size) def _generate_serialize_float(self, var, ctx, t): @@ -592,16 +594,17 @@ class CCodeGenerator: gen_union_var = True union_name = 'f2u' + int_ctype = 'uint32_t' elif ctype == 'double': if not self._ud_written: self._ud_written = True gen_union_var = True union_name = 'd2u' + int_ctype = 'uint64_t' if gen_union_var: # union for reading the bytes of the floating point number - self._cg.add_line('union {name} {name};'.format(name=union_name)) self._cg.add_empty_line() @@ -609,8 +612,9 @@ class CCodeGenerator: bf_var = '{}.u'.format(union_name) else: bf_var = '({}) {}'.format(ctype, var) + int_ctype = ctype - self._generate_bitfield_write(bf_var, ctx, t) + self._generate_bitfield_write(int_ctype, bf_var, ctx, t) self._generate_incr_pos('{}->at'.format(ctx), t.size) def _generate_serialize_enum(self, var, ctx, t): diff --git a/barectf/templates.py b/barectf/templates.py index 3f4cc32..993f819 100644 --- a/barectf/templates.py +++ b/barectf/templates.py @@ -519,29 +519,16 @@ _BITFIELD = '''#ifndef _$PREFIX$BITFIELD_H #define $PREFIX$BYTE_ORDER $ENDIAN_DEF$ /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */ -#define _$prefix$bt_piecewise_rshift(_v, _shift) \\ -__extension__ ({ \\ - __typeof__(_v) ___v = (_v); \\ - __typeof__(_shift) ___shift = (_shift); \\ - unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \\ - unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \\ - \\ - for (; sb; sb--) \\ - ___v >>= sizeof(___v) * CHAR_BIT - 1; \\ - ___v >>= final; \\ -}) - -#define _$prefix$bt_piecewise_lshift(_v, _shift) \\ -__extension__ ({ \\ - __typeof__(_v) ___v = (_v); \\ - __typeof__(_shift) ___shift = (_shift); \\ - unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \\ - unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \\ +#define _$prefix$bt_piecewise_rshift(_vtype, _v, _shift) \\ +do { \\ + unsigned long ___shift = (_shift); \\ + unsigned long sb = (___shift) / (sizeof(_v) * CHAR_BIT - 1); \\ + unsigned long final = (___shift) % (sizeof(_v) * CHAR_BIT - 1); \\ \\ for (; sb; sb--) \\ - ___v <<= sizeof(___v) * CHAR_BIT - 1; \\ - ___v <<= final; \\ -}) + _v >>= sizeof(_v) * CHAR_BIT - 1; \\ + _v >>= final; \\ +} while (0) #define _$prefix$bt_is_signed_type(type) ((type) -1 < (type) 0) @@ -570,9 +557,9 @@ __extension__ ({ \\ * Also, consecutive bitfields are placed from higher to lower bits. */ -#define _$prefix$bt_bitfield_write_le(_ptr, type, _start, _length, _v) \\ +#define _$prefix$bt_bitfield_write_le(_ptr, type, _start, _length, _vtype, _v) \\ do { \\ - __typeof__(_v) __v = (_v); \\ + _vtype __v = (_v); \\ type *__ptr = CAST_PTR(type *, _ptr); \\ unsigned long __start = (_start), __length = (_length); \\ type mask, cmask; \\ @@ -589,7 +576,7 @@ do { \\ \\ /* Trim v high bits */ \\ if (__length < sizeof(__v) * CHAR_BIT) \\ - __v &= ~((~(__typeof__(__v)) 0) << __length); \\ + __v &= ~((~(_vtype) 0) << __length); \\ \\ /* We can now append v with a simple "or", shift it piece-wise */ \\ this_unit = start_unit; \\ @@ -610,13 +597,13 @@ do { \\ cmask &= ~mask; \\ __ptr[this_unit] &= mask; \\ __ptr[this_unit] |= cmask; \\ - __v = _$prefix$bt_piecewise_rshift(__v, ts - cshift); \\ + _$prefix$bt_piecewise_rshift(_vtype, __v, ts - cshift); \\ __start += ts - cshift; \\ this_unit++; \\ } \\ for (; this_unit < end_unit - 1; this_unit++) { \\ __ptr[this_unit] = (type) __v; \\ - __v = _$prefix$bt_piecewise_rshift(__v, ts); \\ + _$prefix$bt_piecewise_rshift(_vtype, __v, ts); \\ __start += ts; \\ } \\ if (end % ts) { \\ @@ -629,9 +616,9 @@ do { \\ __ptr[this_unit] = (type) __v; \\ } while (0) -#define _$prefix$bt_bitfield_write_be(_ptr, type, _start, _length, _v) \\ +#define _$prefix$bt_bitfield_write_be(_ptr, type, _start, _length, _vtype, _v) \\ do { \\ - __typeof__(_v) __v = (_v); \\ + _vtype __v = (_v); \\ type *__ptr = CAST_PTR(type *, _ptr); \\ unsigned long __start = (_start), __length = (_length); \\ type mask, cmask; \\ @@ -648,7 +635,7 @@ do { \\ \\ /* Trim v high bits */ \\ if (__length < sizeof(__v) * CHAR_BIT) \\ - __v &= ~((~(__typeof__(__v)) 0) << __length); \\ + __v &= ~((~(_vtype) 0) << __length); \\ \\ /* We can now append v with a simple "or", shift it piece-wise */ \\ this_unit = end_unit - 1; \\ @@ -669,13 +656,13 @@ do { \\ cmask &= ~mask; \\ __ptr[this_unit] &= mask; \\ __ptr[this_unit] |= cmask; \\ - __v = _$prefix$bt_piecewise_rshift(__v, cshift); \\ + _$prefix$bt_piecewise_rshift(__v, cshift); \\ end -= cshift; \\ this_unit--; \\ } \\ for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \\ __ptr[this_unit] = (type) __v; \\ - __v = _$prefix$bt_piecewise_rshift(__v, ts); \\ + _$prefix$bt_piecewise_rshift(__v, ts); \\ end -= ts; \\ } \\ if (__start % ts) { \\ @@ -695,19 +682,19 @@ do { \\ #if ($PREFIX$BYTE_ORDER == LITTLE_ENDIAN) -#define $prefix$bt_bitfield_write_le(ptr, type, _start, _length, _v) \\ - _$prefix$bt_bitfield_write_le(ptr, type, _start, _length, _v) +#define $prefix$bt_bitfield_write_le(ptr, type, _start, _length, _vtype, _v) \\ + _$prefix$bt_bitfield_write_le(ptr, type, _start, _length, _vtype, _v) -#define $prefix$bt_bitfield_write_be(ptr, type, _start, _length, _v) \\ - _$prefix$bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v) +#define $prefix$bt_bitfield_write_be(ptr, type, _start, _length, _vtype, _v) \\ + _$prefix$bt_bitfield_write_be(ptr, unsigned char, _start, _length, _vtype, _v) #elif ($PREFIX$BYTE_ORDER == BIG_ENDIAN) -#define $prefix$bt_bitfield_write_le(ptr, type, _start, _length, _v) \\ - _$prefix$bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v) +#define $prefix$bt_bitfield_write_le(ptr, type, _start, _length, _vtype, _v) \\ + _$prefix$bt_bitfield_write_le(ptr, unsigned char, _start, _length, _vtype, _v) -#define $prefix$bt_bitfield_write_be(ptr, type, _start, _length, _v) \\ - _$prefix$bt_bitfield_write_be(ptr, type, _start, _length, _v) +#define $prefix$bt_bitfield_write_be(ptr, type, _start, _length, _vtype, _v) \\ + _$prefix$bt_bitfield_write_be(ptr, type, _start, _length, _vtype, _v) #else /* ($PREFIX$BYTE_ORDER == PDP_ENDIAN) */ -- 2.34.1