zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 79fe7e6515555baacf0147fed8c8f27e5f051023 (tree)
parent 7c1dbfab724291b492be5e64fd93ec14e48b202c
Author: Merlyn Morgan-Graham <kavika@gmail.com>
Date:   Sun,  8 Dec 2019 17:02:58 -0800

Add mul and div binary operators to self-hosted translate-C

Diffstat:
Msrc-self-hosted/translate_c.zig | 38++++++++++++++++++++++++++++++++++++--
1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig @@ -393,8 +393,42 @@ fn transBinaryOperator( .node_scope = scope, }); }, - .Mul, - .Div, + .Mul => { + const node = if (cIsUnsignedInteger(qt)) + try transCreateNodeInfixOp(rp, scope, stmt, .MultWrap, .AsteriskPercent, "*%", true) + else + try transCreateNodeInfixOp(rp, scope, stmt, .Mult, .Asterisk, "*", true); + return maybeSuppressResult(rp, scope, result_used, TransResult{ + .node = node, + .child_scope = scope, + .node_scope = scope, + }); + }, + .Div => { + if (!cIsUnsignedInteger(qt)) { + // signed integer division uses @divTrunc + const div_trunc_node = try transCreateNodeBuiltinFnCall(rp.c, "@divTrunc"); + const lhs = try transExpr(rp, scope, ZigClangBinaryOperator_getLHS(stmt), .used, .l_value); + try div_trunc_node.params.push(lhs.node); + _ = try appendToken(rp.c, .Comma, ","); + const rhs = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value); + try div_trunc_node.params.push(rhs.node); + div_trunc_node.rparen_token = try appendToken(rp.c, .RParen, ")"); + return maybeSuppressResult(rp, scope, result_used, TransResult{ + .node = &div_trunc_node.base, + .child_scope = scope, + .node_scope = scope, + }); + } else { + // unsigned/float division uses the operator + const node = try transCreateNodeInfixOp(rp, scope, stmt, .Div, .Slash, "/", true); + return maybeSuppressResult(rp, scope, result_used, TransResult{ + .node = node, + .child_scope = scope, + .node_scope = scope, + }); + } + }, .Rem, .Shl, .Shr,