authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-20 15:52:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-20 15:52:58-07:00
log9c652cc6507e6cba9bba5403bd819676f23c93a1
treef5d3b59bfec23542590ea18f09b0afbc041dca1b
parentfe14e339458a578657f3890f00d654a15c84422c

stage2: C backend: implement support for switch_br AIR


1 files changed, 30 insertions(+), 24 deletions(-)

src/codegen/c.zig+30-24
......@@ -1275,7 +1275,7 @@ fn airCall(o: *Object, inst: Air.Inst.Index) !CValue {
12751275fn airDbgStmt(o: *Object, inst: Air.Inst.Index) !CValue {
12761276 const dbg_stmt = o.air.instructions.items(.data)[inst].dbg_stmt;
12771277 const writer = o.writer();
1278 try writer.print("#line {d}\n", .{dbg_stmt.line});
1278 try writer.print("#line {d}\n", .{dbg_stmt.line + 1});
12791279 return CValue.none;
12801280}
12811281
......@@ -1403,35 +1403,41 @@ fn airSwitchBr(o: *Object, inst: Air.Inst.Index) !CValue {
14031403 const pl_op = o.air.instructions.items(.data)[inst].pl_op;
14041404 const condition = try o.resolveInst(pl_op.operand);
14051405 const condition_ty = o.air.typeOf(pl_op.operand);
1406 const switch_br = o.air.extraData(Air.SwitchBr, pl_op.payload);
14061407 const writer = o.writer();
14071408
14081409 try writer.writeAll("switch (");
14091410 try o.writeCValue(writer, condition);
1410 try writer.writeAll(") {\n");
1411 try writer.writeAll(") {");
14111412 o.indent_writer.pushIndent();
14121413
1413 // Need to rework Sema so that multiple cases are represented rather than
1414 // getting branching logic inside the else, this way we get multiple case
1415 // labels here rather than logic in the default case.
1416 _ = condition_ty;
1417 return o.dg.fail("TODO implement switch in C backend", .{});
1418
1419 //for (inst.cases) |case| {
1420 // try writer.writeAll("case ");
1421 // try o.dg.renderValue(writer, condition_ty, case.item);
1422 // try writer.writeAll(": ");
1423 // // the case body must be noreturn so we don't need to insert a break
1424 // try genBody(o, case.body);
1425 // try o.indent_writer.insertNewline();
1426 //}
1427
1428 //try writer.writeAll("default: ");
1429 //try genBody(o, inst.else_body);
1430 //try o.indent_writer.insertNewline();
1431
1432 //o.indent_writer.popIndent();
1433 //try writer.writeAll("}\n");
1434 //return CValue.none;
1414 var extra_index: usize = switch_br.end;
1415 var case_i: u32 = 0;
1416 while (case_i < switch_br.data.cases_len) : (case_i += 1) {
1417 const case = o.air.extraData(Air.SwitchBr.Case, extra_index);
1418 const items = @bitCast([]const Air.Inst.Ref, o.air.extra[case.end..][0..case.data.items_len]);
1419 const case_body = o.air.extra[case.end + items.len ..][0..case.data.body_len];
1420 extra_index = case.end + case.data.items_len + case_body.len;
1421
1422 for (items) |item| {
1423 try o.indent_writer.insertNewline();
1424 try writer.writeAll("case ");
1425 try o.dg.renderValue(writer, condition_ty, o.air.value(item).?);
1426 try writer.writeAll(": ");
1427 }
1428 // The case body must be noreturn so we don't need to insert a break.
1429 try genBody(o, case_body);
1430 }
1431
1432 const else_body = o.air.extra[extra_index..][0..switch_br.data.else_body_len];
1433 try o.indent_writer.insertNewline();
1434 try writer.writeAll("default: ");
1435 try genBody(o, else_body);
1436 try o.indent_writer.insertNewline();
1437
1438 o.indent_writer.popIndent();
1439 try writer.writeAll("}\n");
1440 return CValue.none;
14351441}
14361442
14371443fn airAsm(o: *Object, inst: Air.Inst.Index) !CValue {