+/*
+ * emit.
+ */
+static int
+instr_hdr_emit_translate(struct rte_swx_pipeline *p,
+ struct action *action __rte_unused,
+ char **tokens,
+ int n_tokens,
+ struct instruction *instr,
+ struct instruction_data *data __rte_unused)
+{
+ struct header *h;
+
+ CHECK(n_tokens == 2, EINVAL);
+
+ h = header_parse(p, tokens[1]);
+ CHECK(h, EINVAL);
+
+ instr->type = INSTR_HDR_EMIT;
+ instr->io.hdr.header_id[0] = h->id;
+ instr->io.hdr.struct_id[0] = h->struct_id;
+ instr->io.hdr.n_bytes[0] = h->st->n_bits / 8;
+ return 0;
+}
+
+static inline void
+__instr_hdr_emit_exec(struct rte_swx_pipeline *p, uint32_t n_emit);
+
+static inline void
+__instr_hdr_emit_exec(struct rte_swx_pipeline *p, uint32_t n_emit)
+{
+ struct thread *t = &p->threads[p->thread_id];
+ struct instruction *ip = t->ip;
+ uint32_t n_headers_out = t->n_headers_out;
+ struct header_out_runtime *ho = &t->headers_out[n_headers_out - 1];
+ uint8_t *ho_ptr = NULL;
+ uint32_t ho_nbytes = 0, i;
+
+ for (i = 0; i < n_emit; i++) {
+ uint32_t header_id = ip->io.hdr.header_id[i];
+ uint32_t struct_id = ip->io.hdr.struct_id[i];
+ uint32_t n_bytes = ip->io.hdr.n_bytes[i];
+
+ struct header_runtime *hi = &t->headers[header_id];
+ uint8_t *hi_ptr = t->structs[struct_id];
+
+ TRACE("[Thread %2u]: emit header %u\n",
+ p->thread_id,
+ header_id);
+
+ /* Headers. */
+ if (!i) {
+ if (!t->n_headers_out) {
+ ho = &t->headers_out[0];
+
+ ho->ptr0 = hi->ptr0;
+ ho->ptr = hi_ptr;
+
+ ho_ptr = hi_ptr;
+ ho_nbytes = n_bytes;
+
+ n_headers_out = 1;
+
+ continue;
+ } else {
+ ho_ptr = ho->ptr;
+ ho_nbytes = ho->n_bytes;
+ }
+ }
+
+ if (ho_ptr + ho_nbytes == hi_ptr) {
+ ho_nbytes += n_bytes;
+ } else {
+ ho->n_bytes = ho_nbytes;
+
+ ho++;
+ ho->ptr0 = hi->ptr0;
+ ho->ptr = hi_ptr;
+
+ ho_ptr = hi_ptr;
+ ho_nbytes = n_bytes;
+
+ n_headers_out++;
+ }
+ }
+
+ ho->n_bytes = ho_nbytes;
+ t->n_headers_out = n_headers_out;
+}
+
+static inline void
+instr_hdr_emit_exec(struct rte_swx_pipeline *p)
+{
+ __instr_hdr_emit_exec(p, 1);
+
+ /* Thread. */
+ thread_ip_inc(p);
+}
+
+static inline void
+instr_hdr_emit_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 2 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 1);
+ instr_tx_exec(p);
+}
+
+static inline void
+instr_hdr_emit2_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 3 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 2);
+ instr_tx_exec(p);
+}
+
+static inline void
+instr_hdr_emit3_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 4 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 3);
+ instr_tx_exec(p);
+}
+
+static inline void
+instr_hdr_emit4_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 5 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 4);
+ instr_tx_exec(p);
+}
+
+static inline void
+instr_hdr_emit5_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 6 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 5);
+ instr_tx_exec(p);
+}
+
+static inline void
+instr_hdr_emit6_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 7 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 6);
+ instr_tx_exec(p);
+}
+
+static inline void
+instr_hdr_emit7_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 8 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 7);
+ instr_tx_exec(p);
+}
+
+static inline void
+instr_hdr_emit8_tx_exec(struct rte_swx_pipeline *p)
+{
+ TRACE("[Thread %2u] *** The next 9 instructions are fused. ***\n",
+ p->thread_id);
+
+ __instr_hdr_emit_exec(p, 8);
+ instr_tx_exec(p);
+}
+