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 {...@@ -1275,7 +1275,7 @@ fn airCall(o: *Object, inst: Air.Inst.Index) !CValue {
1275fn airDbgStmt(o: *Object, inst: Air.Inst.Index) !CValue {1275fn airDbgStmt(o: *Object, inst: Air.Inst.Index) !CValue {
1276 const dbg_stmt = o.air.instructions.items(.data)[inst].dbg_stmt;1276 const dbg_stmt = o.air.instructions.items(.data)[inst].dbg_stmt;
1277 const writer = o.writer();1277 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});
1279 return CValue.none;1279 return CValue.none;
1280}1280}
12811281
...@@ -1403,35 +1403,41 @@ fn airSwitchBr(o: *Object, inst: Air.Inst.Index) !CValue {...@@ -1403,35 +1403,41 @@ fn airSwitchBr(o: *Object, inst: Air.Inst.Index) !CValue {
1403 const pl_op = o.air.instructions.items(.data)[inst].pl_op;1403 const pl_op = o.air.instructions.items(.data)[inst].pl_op;
1404 const condition = try o.resolveInst(pl_op.operand);1404 const condition = try o.resolveInst(pl_op.operand);
1405 const condition_ty = o.air.typeOf(pl_op.operand);1405 const condition_ty = o.air.typeOf(pl_op.operand);
1406 const switch_br = o.air.extraData(Air.SwitchBr, pl_op.payload);
1406 const writer = o.writer();1407 const writer = o.writer();
14071408
1408 try writer.writeAll("switch (");1409 try writer.writeAll("switch (");
1409 try o.writeCValue(writer, condition);1410 try o.writeCValue(writer, condition);
1410 try writer.writeAll(") {\n");1411 try writer.writeAll(") {");
1411 o.indent_writer.pushIndent();1412 o.indent_writer.pushIndent();
14121413
1413 // Need to rework Sema so that multiple cases are represented rather than1414 var extra_index: usize = switch_br.end;
1414 // getting branching logic inside the else, this way we get multiple case1415 var case_i: u32 = 0;
1415 // labels here rather than logic in the default case.1416 while (case_i < switch_br.data.cases_len) : (case_i += 1) {
1416 _ = condition_ty;1417 const case = o.air.extraData(Air.SwitchBr.Case, extra_index);
1417 return o.dg.fail("TODO implement switch in C backend", .{});1418 const items = @bitCast([]const Air.Inst.Ref, o.air.extra[case.end..][0..case.data.items_len]);
14181419 const case_body = o.air.extra[case.end + items.len ..][0..case.data.body_len];
1419 //for (inst.cases) |case| {1420 extra_index = case.end + case.data.items_len + case_body.len;
1420 // try writer.writeAll("case ");1421
1421 // try o.dg.renderValue(writer, condition_ty, case.item);1422 for (items) |item| {
1422 // try writer.writeAll(": ");1423 try o.indent_writer.insertNewline();
1423 // // the case body must be noreturn so we don't need to insert a break1424 try writer.writeAll("case ");
1424 // try genBody(o, case.body);1425 try o.dg.renderValue(writer, condition_ty, o.air.value(item).?);
1425 // try o.indent_writer.insertNewline();1426 try writer.writeAll(": ");
1426 //}1427 }
14271428 // The case body must be noreturn so we don't need to insert a break.
1428 //try writer.writeAll("default: ");1429 try genBody(o, case_body);
1429 //try genBody(o, inst.else_body);1430 }
1430 //try o.indent_writer.insertNewline();1431
14311432 const else_body = o.air.extra[extra_index..][0..switch_br.data.else_body_len];
1432 //o.indent_writer.popIndent();1433 try o.indent_writer.insertNewline();
1433 //try writer.writeAll("}\n");1434 try writer.writeAll("default: ");
1434 //return CValue.none;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;
1435}1441}
14361442
1437fn airAsm(o: *Object, inst: Air.Inst.Index) !CValue {1443fn airAsm(o: *Object, inst: Air.Inst.Index) !CValue {