authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-01-16 15:48:28+01:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-01-16 15:48:28+01:00
log3974b7d31d5b163989255c9a164a8db15bd0ddf1
tree31a6c47a7c17c21b27eece6ebfece3d6d70b3d04
parentf59dcc55463dcf27ce05a5b507dcc7bf26fc60af

translate_c can now translate if statements on integers and floats


2 files changed, 102 insertions(+), 7 deletions(-)

src/translate_c.cpp+81-7
......@@ -2217,12 +2217,6 @@ static AstNode *trans_if_statement(Context *c, TransScope *scope, const IfStmt *
22172217 // if (c) t else e
22182218 AstNode *if_node = trans_create_node(c, NodeTypeIfBoolExpr);
22192219
2220 // TODO: condition != 0
2221 AstNode *condition_node = trans_expr(c, ResultUsedYes, scope, stmt->getCond(), TransRValue);
2222 if (condition_node == nullptr)
2223 return nullptr;
2224 if_node->data.if_bool_expr.condition = condition_node;
2225
22262220 TransScope *then_scope = trans_stmt(c, scope, stmt->getThen(), &if_node->data.if_bool_expr.then_block);
22272221 if (then_scope == nullptr)
22282222 return nullptr;
......@@ -2233,7 +2227,87 @@ static AstNode *trans_if_statement(Context *c, TransScope *scope, const IfStmt *
22332227 return nullptr;
22342228 }
22352229
2236 return if_node;
2230 AstNode *condition_node = trans_expr(c, ResultUsedYes, scope, stmt->getCond(), TransRValue);
2231 if (condition_node == nullptr)
2232 return nullptr;
2233
2234 switch (condition_node->type) {
2235 case NodeTypeBinOpExpr:
2236 switch (condition_node->data.bin_op_expr.bin_op) {
2237 case BinOpTypeBoolOr:
2238 case BinOpTypeBoolAnd:
2239 case BinOpTypeCmpEq:
2240 case BinOpTypeCmpNotEq:
2241 case BinOpTypeCmpLessThan:
2242 case BinOpTypeCmpGreaterThan:
2243 case BinOpTypeCmpLessOrEq:
2244 case BinOpTypeCmpGreaterOrEq:
2245 if_node->data.if_bool_expr.condition = condition_node;
2246 return if_node;
2247 default:
2248 goto convert_to_bitcast;
2249 }
2250
2251 case NodeTypePrefixOpExpr:
2252 switch (condition_node->data.prefix_op_expr.prefix_op) {
2253 case PrefixOpBoolNot:
2254 if_node->data.if_bool_expr.condition = condition_node;
2255 return if_node;
2256 default:
2257 goto convert_to_bitcast;
2258 }
2259
2260 case NodeTypeBoolLiteral:
2261 if_node->data.if_bool_expr.condition = condition_node;
2262 return if_node;
2263
2264 default: {
2265 // In Zig, float, int and pointer does not work in if statements.
2266 // To make it work, we bitcast any value we get to an int of the right size
2267 // and comp it to 0
2268 // TODO: This doesn't work for pointers, as they become nullable on
2269 // translate
2270 // c: if (cond) { }
2271 // zig: {
2272 // zig: const _tmp = cond;
2273 // zig: if (@bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0) { }
2274 // zig: }
2275 convert_to_bitcast:
2276 TransScopeBlock *child_scope = trans_scope_block_create(c, scope);
2277
2278 // const _tmp = cond;
2279 // TODO: avoid name collisions with generated variable names
2280 Buf* tmp_var_name = buf_create_from_str("_tmp");
2281 AstNode *tmp_var_decl = trans_create_node_var_decl_local(c, true, tmp_var_name, nullptr, condition_node);
2282 child_scope->node->data.block.statements.append(tmp_var_decl);
2283
2284 // @sizeOf(@typeOf(_tmp)) * 8
2285 AstNode *typeof_tmp = trans_create_node_builtin_fn_call_str(c, "typeOf");
2286 typeof_tmp->data.fn_call_expr.params.append(trans_create_node_symbol(c, tmp_var_name));
2287 AstNode *sizeof_tmp = trans_create_node_builtin_fn_call_str(c, "sizeOf");
2288 sizeof_tmp->data.fn_call_expr.params.append(typeof_tmp);
2289 AstNode *sizeof_tmp_in_bits = trans_create_node_bin_op(
2290 c, sizeof_tmp, BinOpTypeMult,
2291 trans_create_node_unsigned_negative(c, 8, false));
2292
2293 // @IntType(false, @sizeOf(@typeOf(_tmp)) * 8)
2294 AstNode *int_type = trans_create_node_builtin_fn_call_str(c, "IntType");
2295 int_type->data.fn_call_expr.params.append(trans_create_node_bool(c, false));
2296 int_type->data.fn_call_expr.params.append(sizeof_tmp_in_bits);
2297
2298 // @bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp)
2299 AstNode *bit_cast = trans_create_node_builtin_fn_call_str(c, "bitCast");
2300 bit_cast->data.fn_call_expr.params.append(int_type);
2301 bit_cast->data.fn_call_expr.params.append(trans_create_node_symbol(c, tmp_var_name));
2302
2303 // if (@bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0) { }
2304 AstNode *not_eql_zero = trans_create_node_bin_op(c, bit_cast, BinOpTypeCmpNotEq, trans_create_node_unsigned_negative(c, 0, false));
2305 if_node->data.if_bool_expr.condition = not_eql_zero;
2306 child_scope->node->data.block.statements.append(if_node);
2307
2308 return child_scope->node;
2309 }
2310 }
22372311}
22382312
22392313static AstNode *trans_call_expr(Context *c, ResultUsed result_used, TransScope *scope, const CallExpr *stmt) {
test/translate_c.zig+21
......@@ -1114,4 +1114,25 @@ pub fn addCases(cases: &tests.TranslateCContext) {
11141114 ,
11151115 \\pub const NRF_GPIO = if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Pointer) @ptrCast(&NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeId(@typeOf(NRF_GPIO_BASE)) == @import("builtin").TypeId.Int) @intToPtr(&NRF_GPIO_Type, NRF_GPIO_BASE) else (&NRF_GPIO_Type)(NRF_GPIO_BASE);
11161116 );
1117
1118 cases.add("if on int",
1119 \\int if_int(int i) {
1120 \\ if (i) {
1121 \\ return 0;
1122 \\ } else {
1123 \\ return 1;
1124 \\ }
1125 \\}
1126 ,
1127 \\pub fn if_int(i: c_int) -> c_int {
1128 \\ {
1129 \\ const _tmp = i;
1130 \\ if (@bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0) {
1131 \\ return 0;
1132 \\ } else {
1133 \\ return 1;
1134 \\ };
1135 \\ };
1136 \\}
1137 );
11171138}