12#include <ae2f/c90/StdInt.h>
13#include <ae2f/c90/StdBool.h>
15#include <clang-c/Index.h>
16#include <clang-c/CXString.h>
19#include <util/emitx.h>
20#include <util/iddef.h>
21#include <util/constant.h>
22#include <util/cursor.h>
24#include <util/scale.h>
26#include <spirv/unified1/spirv.h>
29static enum CXChildVisitResult emit_expr(
37 EMIT_EXPR_BIN_1_FAILURE,
44 EMIT_EXPR_BIN_1_NOT_THE_CASE
48
49
50
51
52
53
54
57 const h_util_ctx_t h_ctx,
58 const aclspv_id_t c_newid,
67#define TYPE_NEW_REQ ((*rdwr_type_req))
70 unsigned m_is_constant : 1;
71 unsigned m_is_int : 1;
72 unsigned m_need_cast : 1;
75 FLAGS.m_is_constant = 0;
79 return EMIT_EXPR_BIN_1_FAILURE;
81 switch((uintmax_t)c_cur.kind) {
84 return EMIT_EXPR_BIN_1_NOT_THE_CASE;
88 case CXCursor_IntegerLiteral:
89 case CXCursor_FloatingLiteral:
91 CXEvalResult EVAL = clang_Cursor_Evaluate(c_cur);
96 assert(0 &&
"EVAL is not running");
97 return EMIT_EXPR_BIN_1_FAILURE;
100 switch((uintmax_t)clang_EvalResult_getKind(EVAL)) {
102 EVRES.m_ull = clang_EvalResult_getAsUnsigned(EVAL);
104 FLAGS.m_is_constant = 1;
106 FLAGS.m_need_cast = 1;
115 if(EVRES.m_ull <= UINT32_MAX) {
117 &h_ctx->m_section.m_types
118 , h_ctx->m_count.m_types
123 )) { assert(0);
break; }
135 &h_ctx->m_section.m_types
136 , h_ctx->m_count.m_types
152 EVRES.m_dbl = clang_EvalResult_getAsDouble(EVAL);
154 FLAGS.m_is_constant = 1;
155 FLAGS.m_need_cast = 1;
164 RES = EMIT_EXPR_BIN_1_FAILURE;
169 &h_ctx->m_section.m_types
170 , h_ctx->m_count.m_types
183 assert(0 &&
"unknown evaluation");
187 clang_EvalResult_dispose(EVAL);
189 if(FLAGS.m_need_cast && RES != EMIT_EXPR_BIN_1_FAILURE) {
195 case CXCursor_DeclRefExpr:
197 const CXCursor DECL = clang_getCursorReferenced(c_cur);
200 , h_ctx->m_cursors.m_p
205 return EMIT_EXPR_BIN_1_FAILURE;
207#define DECL_INFO DECL_IDX[((util_cursor* ae2f_restrict)h_ctx->m_cursors.m_p)]
210 unless(util_default_is_number(
DECL_INFO.m_data.m_var_simple.m_type_id))
211 return EMIT_EXPR_BIN_1_FAILURE;
216 &h_ctx->m_section.m_fnimpl
217 , h_ctx->m_count.m_fnimpl
219 ,
DECL_INFO.m_data.m_var_simple.m_type_id
222 ))
return EMIT_EXPR_BIN_1_FAILURE;
225 &h_ctx->m_section.m_fnimpl
226 , h_ctx->m_count.m_fnimpl
228 ,
DECL_INFO.m_data.m_var_simple.m_type_id
231 ))
return EMIT_EXPR_BIN_1_FAILURE;
235 != util_default_bit_width(
DECL_INFO.m_data.m_var_simple.m_type_id))
237#define IS_FLT util_default_is_float(DECL_INFO.m_data.m_var_simple.m_type_id)
239#define OPCODE IS_FLT ? SpvOpFConvert : SpvOpUConvert
240#define NEWTYPEGEN (IS_FLT ? util_default_float : util_default_unsigned)(BWIDTH)
243 &h_ctx->m_section.m_fnimpl
244 , h_ctx->m_count.m_fnimpl
249 ))
return EMIT_EXPR_BIN_1_FAILURE;
258#define OPCODE IS_FLT ? SpvOpConvertSToF : SpvOpConvertFToS
260 &h_ctx->m_section.m_fnimpl
261 , h_ctx->m_count.m_fnimpl
266 ))
return EMIT_EXPR_BIN_1_FAILURE;
278 unless(FLAGS.m_is_constant) {
279 assert(0 &&
"not constant");
280 return EMIT_EXPR_BIN_1_FAILURE;
284 assert(0 &&
"requested type is not default maybe");
285 return EMIT_EXPR_BIN_1_FAILURE;
297 EVRES.m_ll = (intmax_t)EVRES.m_dbl;
299 EVRES.m_ll = (
char)EVRES.m_ll;
307 EVRES.m_ll = (intmax_t)EVRES.m_dbl;
309 EVRES.m_ll = (
short)EVRES.m_ll;
318 EVRES.m_ll = (intmax_t)EVRES.m_dbl;
320 EVRES.m_ll = (
int)EVRES.m_ll;
328 EVRES.m_flt = (
float)EVRES.m_ll;
330 EVRES.m_flt = (
float)EVRES.m_dbl;
334 &h_ctx->m_section.m_types
335 , h_ctx->m_count.m_types
340 )) {
return EMIT_EXPR_BIN_1_FAILURE; }
347 EVRES.m_dbl = (
double)EVRES.m_ull;
354 EVRES.m_ll = (intmax_t)EVRES.m_dbl;
358 &h_ctx->m_section.m_types
359 , h_ctx->m_count.m_types
365 ))
return EMIT_EXPR_BIN_1_FAILURE;
369 unless(util_get_default_id(ID_DEFAULT_F32, h_ctx)) {
371 return EMIT_EXPR_BIN_1_FAILURE;
375 EVRES.m_flt = (
float)EVRES.m_ull;
377 EVRES.m_flt = (
float)EVRES.m_dbl;
380 &h_ctx->m_section.m_types
381 , h_ctx->m_count.m_types
386 )) { assert(0);
return EMIT_EXPR_BIN_1_FAILURE; }
389 &h_ctx->m_section.m_types
390 , h_ctx->m_count.m_types
391 , SpvOpSpecConstantOp
395 )) { assert(0);
return EMIT_EXPR_BIN_1_FAILURE; }
401 case ID_DEFAULT_I32_PTR_FUNC:
402 case ID_DEFAULT_U8_PTR_FUNC:
403 case ID_DEFAULT_U16_PTR_FUNC:
404 case ID_DEFAULT_U32_PTR_FUNC:
405 case ID_DEFAULT_U64_PTR_FUNC:
406 case ID_DEFAULT_U32V4_PTR_INP:
407 case ID_DEFAULT_U32V4_PTR_OUT:
409 case ID_DEFAULT_FN_VOID:
410 case ID_DEFAULT_VOID:
411 case ID_DEFAULT_F16_PTR_FUNC:
412 case ID_DEFAULT_F32_PTR_FUNC:
413 case ID_DEFAULT_F64_PTR_FUNC:
415 assert(0 &&
"unsuppported");
416 return EMIT_EXPR_BIN_1_FAILURE;
423 const CXCursor c_cur,
424 const h_util_ctx_t h_ctx,
425 const aclspv_id_t c_newid,
426 const aclspv_id_t c_result_type_id,
428 aclspv_id_t wr_operands[2]
435 assert(0 &&
"emit_expr_bin_2::h_ctx is null");
436 return EMIT_EXPR_BIN_1_FAILURE;
439 switch((
umax)c_cur.kind) {
441 return EMIT_EXPR_BIN_1_NOT_THE_CASE;
443 case CXCursor_BinaryOperator:
444 case CXCursor_CompoundAssignOperator:
448 switch((uintmax_t)clang_getCursorBinaryOperatorKind(c_cur)) {
450 return EMIT_EXPR_BIN_1_NOT_THE_CASE;
452 case CXBinaryOperator_Add:
453 switch(c_result_type_id) {
471 assert(0 &&
"no support::add");
472 return EMIT_EXPR_BIN_1_FAILURE;
475 case CXBinaryOperator_Sub:
476 switch(c_result_type_id) {
493 assert(0 &&
"no support::sub");
494 return EMIT_EXPR_BIN_1_FAILURE;
497 case CXBinaryOperator_Mul:
498 switch(c_result_type_id) {
515 assert(0 &&
"no support::mul");
516 return EMIT_EXPR_BIN_1_FAILURE;
519 case CXBinaryOperator_Div:
520 switch(c_result_type_id) {
540 assert(0 &&
"no support::div");
541 return EMIT_EXPR_BIN_1_FAILURE;
544 case CXBinaryOperator_Rem:
545 switch(c_result_type_id) {
559 assert(0 &&
"no support::rem");
560 return EMIT_EXPR_BIN_1_FAILURE;
567 NEW_SCALE = mk_scale_from_vec(h_cmdscale,
count_to_sz(8));
569 return EMIT_EXPR_BIN_1_FAILURE;
574 return EMIT_EXPR_BIN_1_FAILURE;
576 if(c_newid == h_ctx
->m_id)
582 VEC[3] = c_result_type_id;
589 wr_operands[0] = VEC[6];
590 wr_operands[1] = VEC[7];
597ae2f_inline static aclspv_id_t emit_expr_type(
const CXType type,
const h_util_ctx_t CTX)
599 aclspv_id_t TYPE_ID = 0;
601 switch((uintmax_t)type.kind) {
604 TYPE_ID = util_get_default_id(ID_DEFAULT_U32, CTX);
609 TYPE_ID = util_get_default_id(ID_DEFAULT_U16, CTX);
614 TYPE_ID = util_get_default_id(ID_DEFAULT_U8, CTX);
618 TYPE_ID = util_get_default_id(ID_DEFAULT_F32, CTX);
622 TYPE_ID = util_get_default_id(ID_DEFAULT_F64, CTX);
627 case CXType_LongLong:
628 case CXType_ULongLong:
629 TYPE_ID = util_get_default_id(ID_DEFAULT_U64, CTX);
641static enum CXChildVisitResult emit_expr(
644 CXClientData rdwr_data
655#define STCK CTX->m_tmp.m_v0
656#define CMDSTCK_SCALE CTX->m_tmp.m_v1
659 aclspv_id_t ID_REQ_CURRENT =
ID_REQ;
663 CTX->m_err = ACLSPV_COMPILE_ALLOC_FAILED;
664 assert(0 &&
"stack is acting funny");
665 return CXChildVisit_Break;
672 CTX->m_err = ACLSPV_COMPILE_ALLOC_FAILED;
673 return CXChildVisit_Break;
677 const CXString SPELL = clang_getCursorSpelling(h_cur);
678 const CXString KINDSPELL = clang_getCursorKindSpelling(h_cur.kind);
679 printf(
"EMITEXPR(%u) STACK(%lu) LST_SCALE_BUF(%lu %u): ", ID_REQ_CURRENT,
STCK_COUNT, LST_SCALE->m_id, LST_SCALE_BUF[0]);
681 puts(KINDSPELL.data);
682 clang_disposeString(SPELL);
683 clang_disposeString(KINDSPELL);
686 return CXChildVisit_Recurse;
693 case EMIT_EXPR_BIN_1_FAILURE:
694 assert(0 &&
"bin_1 is fishy");
695 return CXChildVisit_Break;
699 LST_SCALE_BUF[1] = 1;
705 while(LST_SCALE->m_id &&
COUNTER_OPRND == LST_SCALE_BUF[0]) {
710 if(LST_SCALE_BUF[1]) {
711#define XORSWAP(a, b) (a) ^= (b); (b) ^= (a); (a) ^= (b);
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728 (
void)LST_SCALE_BUF[2];
731
732
733
734
735
736
737
738 XORSWAP(LST_SCALE_BUF[3], LST_SCALE_BUF[5]);
744
745
746
747
748
749
750
751
752
753 XORSWAP(LST_SCALE_BUF[4], LST_SCALE_BUF[5]);
757#define TMPL_SECTION (*(IS_NOT_CONSTANT ? &CTX->m_section.m_fnimpl : &CTX->m_section.m_types))
758#define TMPL_COUNT (*(IS_NOT_CONSTANT ? &CTX->m_count.m_fnimpl : &CTX->m_count.m_types))
760 _aclspv_grow_vec_with_copy(
770 CTX->m_err = ACLSPV_COMPILE_ALLOC_FAILED;
771 return CXChildVisit_Break;
776 , LST_SCALE_BUF + 2 + LST_SCALE_BUF[1]
790 }
return CXChildVisit_Recurse;
791 case EMIT_EXPR_BIN_1_NOT_THE_CASE:
799 switch(emit_expr_bin_2(
806 case EMIT_EXPR_BIN_1_FAILURE:
807 assert(0 &&
"FAILURE_expr_bin2");
808 return CXChildVisit_Break;
812 printf(
"OPERANDS %u %u\n", OPRNDS[0], OPRNDS[1]);
817 CTX->m_err = ACLSPV_COMPILE_ALLOC_FAILED;
818 return CXChildVisit_Break;
825 case EMIT_EXPR_BIN_1_NOT_THE_CASE:
832 CTX->m_err = ACLSPV_COMPILE_OK;
833 return CXChildVisit_Recurse;
842ae2f_inline static int emit_get_expr(
const aclspv_id_t c_id_req,
const aclspv_id_t c_type_req,
const CXCursor c_cur, h_util_ctx_t h_ctx) {
846 BUF[0] = (
uptr)h_ctx;
850 BUF[4] = c_type_req ? c_type_req : ID_DEFAULT_U32;
853 puts(
"HERE IS YOUR CALL");
855 _aclspv_grow_vec(_aclspv_malloc, _aclspv_free, h_ctx->m_tmp.m_v0, 100);
867 if(emit_expr(c_cur, c_cur, BUF) == CXChildVisit_Break)
876 clang_visitChildren(c_cur, emit_expr, BUF);
878 return (
int)h_ctx->m_err;
#define ae2f_WhenCXX(a)
Appears when the current language is C.
#define ae2f_WhenC(a)
Appears when the current language is C++.
#define unless(a)
Invokes when condition is false.
#define ae2f_extern
Suggests the existence of external variable or function, in naming of C. [non-mangling].
#define ACLSPV_ABI_DECL
Declaration as ABI.
#define ae2f_retnew
The returning pointer does not alias to existing object.
#define ae2f_assume(a)
tells the compiler that value if a is false, below this keyword is not expected to be reached.
#define ae2f_unexpected_but_if(a)
#define ae2f_ccpure
Keyword as [[pure]] on C23.
#define ae2f_expected_but_else(a)
#define ae2f_expected_if(a)
#define ae2f_ccconst
Keyword as [[const]] on C23..
#define ae2f_noexcept
marker that this function does not throw something.
#define ae2f_restrict
Keyword as restrict on C99.
#define ae2f_expected_not(a)
expectes a as false.
#define ae2f_unreachable()
tells the compiler that below this keyword is not expected to be reached.
#define ae2f_fallthrough
explicitly tells compiler that fallthrough on switch is expected.
#define ae2f_inline
inline
#define ae2f_expected(a)
expectes a as true.
#define util_emitx_spec_constant(h_wrds, c_wrdcount, c_ty, c_retid, c_val)
#define util_emitx_variable(h_wrds, c_wrdcount, c_type, c_retid, c_storage_class)
#define util_emitx_spec_constant_op2(h_wrds, c_wrdcount, c_retid, c_ty, c_operator, c_opr_0, c_opr_1)
#define util_emitx_type_array(h_wrds, c_wrdcount, c_retid, c_elm_type_id, c_arrcount_id)
#define util_emitx_type_pointer(h_wrds, c_wrdcount, c_retid, c_storage_class, c_elm_type_id)
#define ___mkname_on_dbg(c_ID)
@ EMIT_EXPR_BIN_1_SUCCESS_CONSTANT
@ EMIT_EXPR_BIN_1_SUCCESS
#define get_buf_from_scale(h_alloc, c_scale)
#define get_last_scale_from_vec(h_alloc)
#define get_prv_from_scale(h_alloc, c_scale)
#define SCALE_HEADER_SIZE
#define get_first_scale_from_vec(h_alloc)
#define get_nxt_from_scale(h_alloc, c_scale)
#define get_scale_header_from_vec(h_alloc)
#define get_buf_from_scale2(buf_T, h_alloc, c_scale)
#define aclspv_opcode_t
integer as operation code
aclspv_wrd_t aclspv_wrdcount_t
the integer type represents the number of word.
#define ACLSPV_MASK_OPCODE
mask for opcode
#define aclspv_wrd_t
integer as word
#define ACLSPV_MASK_NOPRNDS
mask for number of operands
x_aclspv_vec m_scale_vars
aclspv_wrdcount_t m_num_type_uniques
x_aclspv_vec m_cursors
cache for cursors for parsing one function for its use see util/cursor.h
x_aclspv_vec m_constant_cache
x_aclspv_vec m_ret
word count for m_ret
aclspv_wrd_t m_is_logical
when on, ignores m_is_buffer_64.
aclspv_wrdcount_t m_num_cursor
number of m_cursors. for its use see util/cursor.h
x_aclspv_vec m_type_uniques
cache for complex types which needs to be stored somewhere for its use see util/type_unique....
CXCursor m_cursor
CXCursorKind could be retrieved here
aclspv_id_t m_id
label id for goto
aclspv_wrd_t m_is_allocated
aclspv_wrd_t m_met_branch
aclspv_wrdcount_t m_arr_count_id
util_bind_unified m_unified
#define mk_noprnds(c_num_opprm)
#define emit_opcode(h_wrds, c_wrdcount, c_opcode, c_num_opprm_opt)
try emit opcode with num_opprm
#define sz_to_count(c_sz)
byte size to word count
#define get_wrd_of_vec(vec)
get word buffer from vector
aclspv_wrdcount_t spvsz_t
#define count_to_sz(c_count)
word count to byte size
#define set_oprnd_count_for_opcode(cr_wrd, c_num_opprm)
#define opcode_to_wrd(c_opcode, c_num_opprm)