_CCodeGenerator.generate_bitfield_header(): use a Jinja 2 template
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 27 Aug 2020 15:35:47 +0000 (11:35 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 27 Aug 2020 15:44:15 +0000 (11:44 -0400)
Benefits:

* `\` instead of `\\` in the template.

* Jinja 2 variables instead of `$PREFIX`, `$prefix$`, etc. which makes
  it possible to have template syntax highlighting for example.

* `LITTLE_ENDIAN` or `BIG_ENDIAN` generated by the template using the
  barectf configuration.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/gen.py
barectf/templates.py
barectf/templates/bitfield.h.j2 [new file with mode: 0644]

index 0f02f616bcbda0e75f4c04ffade8ceb8a57b2302..d2e1474896e54f72e184821d8c732462eabb6018 100644 (file)
@@ -294,20 +294,7 @@ class _CCodeGenerator:
         self._cg.add_lines(tmpl)
 
     def generate_bitfield_header(self):
-        self._cg.reset()
-        tmpl = barectf_templates._BITFIELD
-        tmpl = tmpl.replace('$prefix$', self._iden_prefix)
-        tmpl = tmpl.replace('$PREFIX$', self._iden_prefix.upper())
-
-        if self._trace_type.default_byte_order == barectf_config.ByteOrder.BIG_ENDIAN:
-            endian_def = 'BIG_ENDIAN'
-        else:
-            endian_def = 'LITTLE_ENDIAN'
-
-        tmpl = tmpl.replace('$ENDIAN_DEF$', endian_def)
-        self._cg.add_lines(tmpl)
-
-        return self._cg.code
+        return self._create_file_template('bitfield.h.j2').render()
 
     def _generate_func_init_proto(self):
         tmpl = barectf_templates._FUNC_INIT_PROTO
index 9e677d1e7eecf795d0e512991ad92d1590d4655f..d85dc1d98556f2eb1cfa6bd4c9ac0fc065cb81ca 100644 (file)
@@ -595,224 +595,3 @@ void _commit_event(void *vctx)
                ctx->cbs.close_packet(ctx->data);
        }}
 }}'''
-
-
-_BITFIELD = '''#ifndef _$PREFIX$BITFIELD_H
-#define _$PREFIX$BITFIELD_H
-
-/*
- * BabelTrace
- *
- * Bitfields read/write functions.
- *
- * Copyright (c) 2010-2020 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-#include <limits.h>
-
-#ifdef __cplusplus
-# define CAST_PTR(_type, _value) \\
-       static_cast<_type>(static_cast<void *>(_value))
-#else
-# define CAST_PTR(_type, _value)       ((void *) (_value))
-#endif
-
-#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(_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;                                                   \\
-} while (0)
-
-/*
- * $prefix$bt_bitfield_write - write integer to a bitfield in native endianness
- *
- * Save integer to the bitfield, which starts at the "start" bit, has "len"
- * bits.
- * The inside of a bitfield is from high bits to low bits.
- * Uses native endianness.
- * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
- * For signed "v", sign-extend v if bitfield is larger than v.
- *
- * On little endian, bytes are placed from the less significant to the most
- * significant. Also, consecutive bitfields are placed from lower bits to higher
- * bits.
- *
- * On big endian, bytes are places from most significant to less significant.
- * Also, consecutive bitfields are placed from higher to lower bits.
- */
-
-#define _$prefix$bt_bitfield_write_le(_ptr, type, _start, _length, _vtype, _v) \\
-do {                                                                   \\
-       _vtype __v = (_v);                                      \\
-       type *__ptr = CAST_PTR(type *, _ptr);                           \\
-       unsigned long __start = (_start), __length = (_length);         \\
-       type mask, cmask;                                               \\
-       unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */     \\
-       unsigned long start_unit, end_unit, this_unit;                  \\
-       unsigned long end, cshift; /* cshift is "complement shift" */   \\
-                                                                       \\
-       if (!__length)                                                  \\
-               break;                                                  \\
-                                                                       \\
-       end = __start + __length;                                       \\
-       start_unit = __start / ts;                                      \\
-       end_unit = (end + (ts - 1)) / ts;                               \\
-                                                                       \\
-       /* Trim v high bits */                                          \\
-       if (__length < sizeof(__v) * CHAR_BIT)                          \\
-               __v &= ~((~(_vtype) 0) << __length);            \\
-                                                                       \\
-       /* We can now append v with a simple "or", shift it piece-wise */ \\
-       this_unit = start_unit;                                         \\
-       if (start_unit == end_unit - 1) {                               \\
-               mask = ~((~(type) 0) << (__start % ts));                \\
-               if (end % ts)                                           \\
-                       mask |= (~(type) 0) << (end % ts);              \\
-               cmask = (type) __v << (__start % ts);                   \\
-               cmask &= ~mask;                                         \\
-               __ptr[this_unit] &= mask;                               \\
-               __ptr[this_unit] |= cmask;                              \\
-               break;                                                  \\
-       }                                                               \\
-       if (__start % ts) {                                             \\
-               cshift = __start % ts;                                  \\
-               mask = ~((~(type) 0) << cshift);                        \\
-               cmask = (type) __v << cshift;                           \\
-               cmask &= ~mask;                                         \\
-               __ptr[this_unit] &= mask;                               \\
-               __ptr[this_unit] |= cmask;                              \\
-               _$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;                          \\
-               _$prefix$bt_piecewise_rshift(_vtype, __v, ts);          \\
-               __start += ts;                                          \\
-       }                                                               \\
-       if (end % ts) {                                                 \\
-               mask = (~(type) 0) << (end % ts);                       \\
-               cmask = (type) __v;                                     \\
-               cmask &= ~mask;                                         \\
-               __ptr[this_unit] &= mask;                               \\
-               __ptr[this_unit] |= cmask;                              \\
-       } else                                                          \\
-               __ptr[this_unit] = (type) __v;                          \\
-} while (0)
-
-#define _$prefix$bt_bitfield_write_be(_ptr, type, _start, _length, _vtype, _v) \\
-do {                                                                   \\
-       _vtype __v = (_v);                                      \\
-       type *__ptr = CAST_PTR(type *, _ptr);                           \\
-       unsigned long __start = (_start), __length = (_length);         \\
-       type mask, cmask;                                               \\
-       unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */     \\
-       unsigned long start_unit, end_unit, this_unit;                  \\
-       unsigned long end, cshift; /* cshift is "complement shift" */   \\
-                                                                       \\
-       if (!__length)                                                  \\
-               break;                                                  \\
-                                                                       \\
-       end = __start + __length;                                       \\
-       start_unit = __start / ts;                                      \\
-       end_unit = (end + (ts - 1)) / ts;                               \\
-                                                                       \\
-       /* Trim v high bits */                                          \\
-       if (__length < sizeof(__v) * CHAR_BIT)                          \\
-               __v &= ~((~(_vtype) 0) << __length);                    \\
-                                                                       \\
-       /* We can now append v with a simple "or", shift it piece-wise */ \\
-       this_unit = end_unit - 1;                                       \\
-       if (start_unit == end_unit - 1) {                               \\
-               mask = ~((~(type) 0) << ((ts - (end % ts)) % ts));      \\
-               if (__start % ts)                                       \\
-                       mask |= (~((type) 0)) << (ts - (__start % ts)); \\
-               cmask = (type) __v << ((ts - (end % ts)) % ts);         \\
-               cmask &= ~mask;                                         \\
-               __ptr[this_unit] &= mask;                               \\
-               __ptr[this_unit] |= cmask;                              \\
-               break;                                                  \\
-       }                                                               \\
-       if (end % ts) {                                                 \\
-               cshift = end % ts;                                      \\
-               mask = ~((~(type) 0) << (ts - cshift));                 \\
-               cmask = (type) __v << (ts - cshift);                    \\
-               cmask &= ~mask;                                         \\
-               __ptr[this_unit] &= mask;                               \\
-               __ptr[this_unit] |= cmask;                              \\
-               _$prefix$bt_piecewise_rshift(_vtype, __v, cshift); \\
-               end -= cshift;                                          \\
-               this_unit--;                                            \\
-       }                                                               \\
-       for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \\
-               __ptr[this_unit] = (type) __v;                          \\
-               _$prefix$bt_piecewise_rshift(_vtype, __v, ts); \\
-               end -= ts;                                              \\
-       }                                                               \\
-       if (__start % ts) {                                             \\
-               mask = (~(type) 0) << (ts - (__start % ts));            \\
-               cmask = (type) __v;                                     \\
-               cmask &= ~mask;                                         \\
-               __ptr[this_unit] &= mask;                               \\
-               __ptr[this_unit] |= cmask;                              \\
-       } else                                                          \\
-               __ptr[this_unit] = (type) __v;                          \\
-} while (0)
-
-/*
- * $prefix$bt_bitfield_write_le - write integer to a bitfield in little endian
- * $prefix$bt_bitfield_write_be - write integer to a bitfield in big endian
- */
-
-#if ($PREFIX$BYTE_ORDER == LITTLE_ENDIAN)
-
-#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, _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, _vtype, _v) \\
-       _$prefix$bt_bitfield_write_le(ptr, unsigned char, _start, _length, _vtype, _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) */
-
-#error "Byte order not supported"
-
-#endif
-
-#endif /* _$PREFIX$BITFIELD_H */
-'''
diff --git a/barectf/templates/bitfield.h.j2 b/barectf/templates/bitfield.h.j2
new file mode 100644 (file)
index 0000000..d19951d
--- /dev/null
@@ -0,0 +1,223 @@
+{% set prefix = cfg.options.code_generation_options.identifier_prefix %}
+{% set ucprefix = prefix | upper %}
+#ifndef _{{ ucprefix }}BITFIELD_H
+#define _{{ ucprefix }}BITFIELD_H
+
+/*
+ * BabelTrace
+ *
+ * Bitfields read/write functions.
+ *
+ * Copyright (c) 2010-2020 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <limits.h>
+
+#ifdef __cplusplus
+# define CAST_PTR(_type, _value) \
+       static_cast<_type>(static_cast<void *>(_value))
+#else
+# define CAST_PTR(_type, _value)       ((void *) (_value))
+#endif
+
+{% set def_bo = cfg.trace.type.default_byte_order %}
+{% set def_bo_str = 'LITTLE_ENDIAN' if def_bo == barectf_config.ByteOrder.LITTLE_ENDIAN else 'BIG_ENDIAN' %}
+#define {{ ucprefix }}BYTE_ORDER {{ def_bo_str }}
+
+
+/* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
+#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;                                                   \
+} while (0)
+
+/*
+ * {{ prefix }}bt_bitfield_write - write integer to a bitfield in native endianness
+ *
+ * Save integer to the bitfield, which starts at the "start" bit, has "len"
+ * bits.
+ * The inside of a bitfield is from high bits to low bits.
+ * Uses native endianness.
+ * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
+ * For signed "v", sign-extend v if bitfield is larger than v.
+ *
+ * On little endian, bytes are placed from the less significant to the most
+ * significant. Also, consecutive bitfields are placed from lower bits to higher
+ * bits.
+ *
+ * On big endian, bytes are places from most significant to less significant.
+ * Also, consecutive bitfields are placed from higher to lower bits.
+ */
+
+#define _{{ prefix }}bt_bitfield_write_le(_ptr, type, _start, _length, _vtype, _v) \
+do {                                                                   \
+       _vtype __v = (_v);                                              \
+       type *__ptr = CAST_PTR(type *, _ptr);                           \
+       unsigned long __start = (_start), __length = (_length);         \
+       type mask, cmask;                                               \
+       unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */     \
+       unsigned long start_unit, end_unit, this_unit;                  \
+       unsigned long end, cshift; /* cshift is "complement shift" */   \
+                                                                       \
+       if (!__length)                                                  \
+               break;                                                  \
+                                                                       \
+       end = __start + __length;                                       \
+       start_unit = __start / ts;                                      \
+       end_unit = (end + (ts - 1)) / ts;                               \
+                                                                       \
+       /* Trim v high bits */                                          \
+       if (__length < sizeof(__v) * CHAR_BIT)                          \
+               __v &= ~((~(_vtype) 0) << __length);                    \
+                                                                       \
+       /* We can now append v with a simple "or", shift it piece-wise */ \
+       this_unit = start_unit;                                         \
+       if (start_unit == end_unit - 1) {                               \
+               mask = ~((~(type) 0) << (__start % ts));                \
+               if (end % ts)                                           \
+                       mask |= (~(type) 0) << (end % ts);              \
+               cmask = (type) __v << (__start % ts);                   \
+               cmask &= ~mask;                                         \
+               __ptr[this_unit] &= mask;                               \
+               __ptr[this_unit] |= cmask;                              \
+               break;                                                  \
+       }                                                               \
+       if (__start % ts) {                                             \
+               cshift = __start % ts;                                  \
+               mask = ~((~(type) 0) << cshift);                        \
+               cmask = (type) __v << cshift;                           \
+               cmask &= ~mask;                                         \
+               __ptr[this_unit] &= mask;                               \
+               __ptr[this_unit] |= cmask;                              \
+               _{{ 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;                          \
+               _{{ prefix }}bt_piecewise_rshift(_vtype, __v, ts);              \
+               __start += ts;                                          \
+       }                                                               \
+       if (end % ts) {                                                 \
+               mask = (~(type) 0) << (end % ts);                       \
+               cmask = (type) __v;                                     \
+               cmask &= ~mask;                                         \
+               __ptr[this_unit] &= mask;                               \
+               __ptr[this_unit] |= cmask;                              \
+       } else                                                          \
+               __ptr[this_unit] = (type) __v;                          \
+} while (0)
+
+#define _{{ prefix }}bt_bitfield_write_be(_ptr, type, _start, _length, _vtype, _v) \
+do {                                                                   \
+       _vtype __v = (_v);                                              \
+       type *__ptr = CAST_PTR(type *, _ptr);                           \
+       unsigned long __start = (_start), __length = (_length);         \
+       type mask, cmask;                                               \
+       unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */     \
+       unsigned long start_unit, end_unit, this_unit;                  \
+       unsigned long end, cshift; /* cshift is "complement shift" */   \
+                                                                       \
+       if (!__length)                                                  \
+               break;                                                  \
+                                                                       \
+       end = __start + __length;                                       \
+       start_unit = __start / ts;                                      \
+       end_unit = (end + (ts - 1)) / ts;                               \
+                                                                       \
+       /* Trim v high bits */                                          \
+       if (__length < sizeof(__v) * CHAR_BIT)                          \
+               __v &= ~((~(_vtype) 0) << __length);                    \
+                                                                       \
+       /* We can now append v with a simple "or", shift it piece-wise */ \
+       this_unit = end_unit - 1;                                       \
+       if (start_unit == end_unit - 1) {                               \
+               mask = ~((~(type) 0) << ((ts - (end % ts)) % ts));      \
+               if (__start % ts)                                       \
+                       mask |= (~((type) 0)) << (ts - (__start % ts)); \
+               cmask = (type) __v << ((ts - (end % ts)) % ts);         \
+               cmask &= ~mask;                                         \
+               __ptr[this_unit] &= mask;                               \
+               __ptr[this_unit] |= cmask;                              \
+               break;                                                  \
+       }                                                               \
+       if (end % ts) {                                                 \
+               cshift = end % ts;                                      \
+               mask = ~((~(type) 0) << (ts - cshift));                 \
+               cmask = (type) __v << (ts - cshift);                    \
+               cmask &= ~mask;                                         \
+               __ptr[this_unit] &= mask;                               \
+               __ptr[this_unit] |= cmask;                              \
+               _{{ prefix }}bt_piecewise_rshift(_vtype, __v, cshift);  \
+               end -= cshift;                                          \
+               this_unit--;                                            \
+       }                                                               \
+       for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
+               __ptr[this_unit] = (type) __v;                          \
+               _{{ prefix }}bt_piecewise_rshift(_vtype, __v, ts);              \
+               end -= ts;                                              \
+       }                                                               \
+       if (__start % ts) {                                             \
+               mask = (~(type) 0) << (ts - (__start % ts));            \
+               cmask = (type) __v;                                     \
+               cmask &= ~mask;                                         \
+               __ptr[this_unit] &= mask;                               \
+               __ptr[this_unit] |= cmask;                              \
+       } else                                                          \
+               __ptr[this_unit] = (type) __v;                          \
+} while (0)
+
+/*
+ * {{ prefix }}bt_bitfield_write_le - write integer to a bitfield in little endian
+ * {{ prefix }}bt_bitfield_write_be - write integer to a bitfield in big endian
+ */
+
+#if ({{ ucprefix }}BYTE_ORDER == LITTLE_ENDIAN)
+
+#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, _vtype, _v) \
+       _{{ prefix }}bt_bitfield_write_be(ptr, unsigned char, _start, _length, _vtype, _v)
+
+#elif ({{ ucprefix }}BYTE_ORDER == BIG_ENDIAN)
+
+#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, _vtype, _v) \
+       _{{ prefix }}bt_bitfield_write_be(ptr, type, _start, _length, _vtype, _v)
+
+#else /* ({{ ucprefix }}BYTE_ORDER == PDP_ENDIAN) */
+
+#error "Byte order not supported"
+
+#endif
+
+#endif /* _{{ ucprefix }}BITFIELD_H */
This page took 0.029147 seconds and 4 git commands to generate.