ae2f_docs
Loading...
Searching...
No Matches
wrdemit.h
Go to the documentation of this file.
1/** @file wrdemit.h @brief word emission utilities */
2
3#ifndef build_wrdemit_h
4#define build_wrdemit_h
5
6#include <stddef.h>
7#include <aclspv/spvty.h>
8
9#include <ae2f/c90/StdInt.h>
10#include <ae2f/Keys.h>
11
12#include <assert.h>
13#include <string.h>
14
15#include "./ctx.h"
16
17/** size_t for spir-v format */
19
20/**
21 * @def sz_to_count
22 * @brief byte size to word count
23 * */
24#define sz_to_count(c_sz)
25 (((spvsz_t)(c_sz) + (spvsz_t)(sizeof(aclspv_wrd_t)) - 1) / (spvsz_t)(sizeof(aclspv_wrd_t)))
26
27
28/**
29 * @def count_to_sz
30 * @brief word count to byte size
31 * */
32#define count_to_sz(c_count) ((spvsz_t)(((spvsz_t)(c_count)) * (spvsz_t)(sizeof(aclspv_wrd_t))))
33
34/**
35 * @def get_wrd_of_vec
36 * @brief get word buffer from vector
37 * */
38#define get_wrd_of_vec(vec) ((aclspv_wrd_t* ae2f_restrict)((vec)->m_p))
39
40/**
41 *
42 * @fn emit_wrd
43 * @brief try emitting `c_wrd` on `h_vec`
44 * @return new c_wrdcount available. 0 when failed allocating.
45 * */
46static ae2f_inline spvsz_t util_emit_wrd(
47 x_aclspv_vec* ae2f_restrict const h_wrds,
48 const spvsz_t c_wrdcount,
49 const aclspv_wrd_t c_wrd
50 )
51{
52 if(count_to_sz(c_wrdcount + 1) > (spvsz_t)h_wrds->m_sz)
53 _aclspv_grow_vec_with_copy(
54 _aclspv_malloc, _aclspv_free, _aclspv_memcpy
55 , L_new, *h_wrds
56 , ((size_t)count_to_sz((c_wrdcount + 1) << 1))
57 );
58
59 ae2f_expected_but_else(h_wrds->m_p) return 0;
60 ae2f_unexpected_but_if(sz_to_count(h_wrds->m_sz) > UINT32_MAX) return 0;
61
62 get_wrd_of_vec(h_wrds)[c_wrdcount] = c_wrd;
63 return c_wrdcount + 1;
64}
65
66#define mk_noprnds(c_num_opprm)
67 ((((aclspv_wrd_t)((c_num_opprm) + 1)) << 16) & (aclspv_wrd_t)ACLSPV_MASK_NOPRNDS)
68
69#define opcode_to_wrd(c_opcode, c_num_opprm)
70 ((aclspv_wrd_t)(((aclspv_wrd_t)(c_opcode) & ACLSPV_MASK_OPCODE) | mk_noprnds(c_num_opprm)))
71/**
72 * @def emit_opcode
73 * @brief try emit opcode with num_opprm
74 * */
75#define emit_opcode(h_wrds, c_wrdcount, c_opcode, c_num_opprm_opt)
76 (util_emit_wrd(h_wrds, c_wrdcount, opcode_to_wrd(c_opcode, c_num_opprm_opt)))
77
78#define set_oprnd_count_for_opcode(cr_wrd, c_num_opprm)
79 (cr_wrd) = opcode_to_wrd((cr_wrd & ACLSPV_MASK_OPCODE), ((aclspv_num_opprm_t)(c_num_opprm)))
80
81/**
82 * @fn emit_str
83 * @brief emit string with word size padded.
84 * @returns 0 when failed.
85 * */
86ae2f_inline static spvsz_t util_emit_str(
87 x_aclspv_vec* ae2f_restrict const h_wrds,
88 const spvsz_t c_wrdcount,
89 const char* ae2f_restrict const rd_str
90 )
91{
92 /** string len: null included. */
93 const size_t _len = strlen(rd_str) + 1;
94 const spvsz_t len = (_len > (size_t)UINT32_MAX) ? 0 : (spvsz_t)_len;
95 const spvsz_t pad_wrds = (!!((len) & 3)) + ((len) >> 2);
96
97 ae2f_expected_but_else(len) return 0;
98
99 _aclspv_grow_vec_with_copy(_aclspv_malloc, _aclspv_free, _aclspv_memcpy, L_new
100 , *h_wrds, (size_t)count_to_sz(pad_wrds + c_wrdcount));
101
102 ae2f_expected_but_else(h_wrds->m_p) return 0;
103 assert(count_to_sz(pad_wrds + c_wrdcount) <= (spvsz_t)h_wrds->m_sz);
104
105 memset(&get_wrd_of_vec(h_wrds)[c_wrdcount], 0, (size_t)count_to_sz(pad_wrds));
106 _aclspv_memcpy(&get_wrd_of_vec(h_wrds)[c_wrdcount], rd_str, len);
107
108 return c_wrdcount + pad_wrds;
109}
110
111#endif
#define ae2f_IS_SHARED
Definition Call.auto.h:12
#define ae2f_OFF
Definition Call.auto.h:3
#define ae2f_WhenCXX(a)
Appears when the current language is C.
Definition Cxx.h:44
#define ae2f_WhenC(a)
Appears when the current language is C++.
Definition Cxx.h:38
#define unless(a)
Invokes when condition is false.
Definition Keys.h:34
#define ae2f_extern
Suggests the existence of external variable or function, in naming of C. [non-mangling].
Definition Keys.h:25
#define ae2f_NIL
Definition Nil.h:13
#define ACLSPV_ABI_DECL
Declaration as ABI.
Definition abi.h:23
#define STATE_VAL
#define FNINFO
#define cpysz
#define cpypad
#define ae2f_assume(a)
tells the compiler that value if a is false, below this keyword is not expected to be reached.
Definition cc.h:228
#define ae2f_unexpected_but_if(a)
Definition cc.h:192
#define ae2f_expected_but_else(a)
Definition cc.h:201
#define ae2f_ccconst
Keyword as [[const]] on C23..
Definition cc.h:66
#define ae2f_noexcept
marker that this function does not throw something.
Definition cc.h:133
#define ae2f_restrict
Keyword as restrict on C99.
Definition cc.h:81
#define ae2f_expected_not(a)
expectes a as false.
Definition cc.h:185
#define ae2f_unreachable()
tells the compiler that below this keyword is not expected to be reached.
Definition cc.h:213
#define ae2f_inline
inline
Definition cc.h:149
#define ae2f_expected(a)
expectes a as true.
Definition cc.h:184
#define CTX
#define EMIT_POS
#define util_emitx_type_pointer(h_wrds, c_wrdcount, c_retid, c_storage_class, c_elm_type_id)
Definition emitx.h:134
#define ret_count
#define get_buf_from_scale(h_alloc, c_scale)
Definition scale.h:34
#define aclspv_opcode_t
integer as operation code
Definition spvty.h:27
aclspv_wrd_t aclspv_wrdcount_t
the integer type represents the number of word.
Definition spvty.h:61
#define ACLSPV_MASK_OPCODE
mask for opcode
Definition spvty.h:37
#define aclspv_wrd_t
integer as word
Definition spvty.h:16
#define ACLSPV_MASK_NOPRNDS
mask for number of operands
Definition spvty.h:42
x_aclspv_vec m_scale_vars
Definition ctx.h:62
aclspv_wrdcount_t m_num_type_uniques
Definition ctx.h:44
x_aclspv_vec m_cursors
cache for cursors for parsing one function for its use see util/cursor.h
Definition ctx.h:77
x_aclspv_vec m_constant_cache
Definition ctx.h:65
aclspv_id_t m_id
id
Definition ctx.h:50
x_aclspv_vec m_ret
word count for m_ret
Definition ctx.h:56
aclspv_wrd_t m_is_logical
when on, ignores m_is_buffer_64.
Definition ctx.h:23
aclspv_wrdcount_t m_num_cursor
number of m_cursors. for its use see util/cursor.h
Definition ctx.h:41
x_aclspv_vec m_type_uniques
cache for complex types which needs to be stored somewhere for its use see util/type_unique....
Definition ctx.h:86
util_bind_unified m_unified
Definition bind.h:20
#define mk_noprnds(c_num_opprm)
Definition wrdemit.h:66
#define emit_opcode(h_wrds, c_wrdcount, c_opcode, c_num_opprm_opt)
try emit opcode with num_opprm
Definition wrdemit.h:75
#define sz_to_count(c_sz)
byte size to word count
Definition wrdemit.h:24
#define get_wrd_of_vec(vec)
get word buffer from vector
Definition wrdemit.h:38
aclspv_wrdcount_t spvsz_t
Definition wrdemit.h:18
#define count_to_sz(c_count)
word count to byte size
Definition wrdemit.h:32
#define set_oprnd_count_for_opcode(cr_wrd, c_num_opprm)
Definition wrdemit.h:78
#define opcode_to_wrd(c_opcode, c_num_opprm)
Definition wrdemit.h:69