From 79fe7e6515555baacf0147fed8c8f27e5f051023 Mon Sep 17 00:00:00 2001 From: Merlyn Morgan-Graham Date: Sun, 8 Dec 2019 17:02:58 -0800 Subject: [PATCH] Add mul and div binary operators to self-hosted translate-C --- src-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 index c0ac3fbd5..ace523100 100644 --- 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,