can gccjit support ternary condition operator?
zhao xin
esrrhs@163.com
Wed Dec 27 02:36:10 GMT 2023
Hi,
I am writing a simple Lua JIT toy by using the GGC JIT. and Lua has the grammar "a and b", the usage just like below
--------
local a = nil
print(a and a.b)
a = { b = 1 }
print(a and a.b)
--------
it will print
--------
nil
1
--------
it is different with the C “&&” grammar, but more like the C ternary condition operator "a ? a : b".
and I didn't find the relevant API in GCC JIT, so it may only be used to use alternatives "({int _temp; if (cond) _temp=then; else _temp=otherwise; _temp;})"
but, it can not use in the global variable, here is the C++ code
--------
#define GET_SIZE(cond, then, otherwise) ({int _temp; if (cond) _temp=then; else _temp=otherwise; _temp;})
std::string *s = 0;
//auto size = GET_SIZE(s, s->size(), 0); // compile fail : statement-expressions are not allowed outside functions nor in template-argument lists
auto size = auto size = s ? s->size() : 0; // compile ok
int main() {
std::cout << GET_SIZE(s, s->size(), 0) << std::endl; // compile ok
return 0;
}
--------
so is there any other way to implement "a and b"? or maybe add ternary condition operator support in GCC JIT?
thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/jit/attachments/20231227/ad507308/attachment.htm>
More information about the Jit
mailing list