authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-30 17:09:40+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-30 17:13:56+02:00
logf8b779c114a5fcb82f08168912f2300d7027a2fd
tree703dcdf6d2e1dd8c40f2f044264142f48f362065
parent11ec7109c3d882615c2d220a813100c7f193cc0a

cbe: write more instructions inline


1 files changed, 56 insertions(+), 77 deletions(-)

src/codegen/c.zig+56-77
...@@ -395,6 +395,27 @@ pub const Function = struct {...@@ -395,6 +395,27 @@ pub const Function = struct {
395 .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits),395 .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits),
396 .shl_exact => try airBinOp(f, inst, "<<", "shl", .None),396 .shl_exact => try airBinOp(f, inst, "<<", "shl", .None),
397 .not => try airNot (f, inst),397 .not => try airNot (f, inst),
398
399 .is_err => try airIsErr(f, inst, false, "!="),
400 .is_non_err => try airIsErr(f, inst, false, "=="),
401 .is_err_ptr => try airIsErr(f, inst, true, "!="),
402 .is_non_err_ptr => try airIsErr(f, inst, true, "=="),
403
404 .is_null => try airIsNull(f, inst, "==", false),
405 .is_non_null => try airIsNull(f, inst, "!=", false),
406 .is_null_ptr => try airIsNull(f, inst, "==", true),
407 .is_non_null_ptr => try airIsNull(f, inst, "!=", true),
408
409 .get_union_tag => try airGetUnionTag(f, inst),
410 .clz => try airUnBuiltinCall(f, inst, "clz", .Bits),
411 .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits),
412 .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits),
413 .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits),
414 .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits),
415 .tag_name => try airTagName(f, inst),
416 .error_name => try airErrorName(f, inst),
417
418 .ptrtoint => try airPtrToInt(f, inst),
398 else => unreachable,419 else => unreachable,
399 // zig fmt: on420 // zig fmt: on
400 }421 }
...@@ -422,6 +443,7 @@ pub const Function = struct {...@@ -422,6 +443,7 @@ pub const Function = struct {
422 try w.writeByte('.');443 try w.writeByte('.');
423 return f.writeCValue(w, member, .Other);444 return f.writeCValue(w, member, .Other);
424 },445 },
446 .inline_index => unreachable, // Use resolveInstNoInline
425 else => return f.object.dg.writeCValueMember(w, c_value, member),447 else => return f.object.dg.writeCValueMember(w, c_value, member),
426 }448 }
427 }449 }
...@@ -2591,15 +2613,15 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO...@@ -2591,15 +2613,15 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
2591 .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst),2613 .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst),
2592 .wrap_optional => try airWrapOptional(f, inst),2614 .wrap_optional => try airWrapOptional(f, inst),
25932615
2594 .is_err => try airIsErr(f, inst, false, "!="),2616 .is_err => CValue{ .inline_index = inst },
2595 .is_non_err => try airIsErr(f, inst, false, "=="),2617 .is_non_err => CValue{ .inline_index = inst },
2596 .is_err_ptr => try airIsErr(f, inst, true, "!="),2618 .is_err_ptr => CValue{ .inline_index = inst },
2597 .is_non_err_ptr => try airIsErr(f, inst, true, "=="),2619 .is_non_err_ptr => CValue{ .inline_index = inst },
25982620
2599 .is_null => try airIsNull(f, inst, "==", false),2621 .is_null => CValue{ .inline_index = inst },
2600 .is_non_null => try airIsNull(f, inst, "!=", false),2622 .is_non_null => CValue{ .inline_index = inst },
2601 .is_null_ptr => try airIsNull(f, inst, "==", true),2623 .is_null_ptr => CValue{ .inline_index = inst },
2602 .is_non_null_ptr => try airIsNull(f, inst, "!=", true),2624 .is_non_null_ptr => CValue{ .inline_index = inst },
26032625
2604 .alloc => try airAlloc(f, inst),2626 .alloc => try airAlloc(f, inst),
2605 .ret_ptr => try airRetPtr(f, inst),2627 .ret_ptr => try airRetPtr(f, inst),
...@@ -2627,14 +2649,14 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO...@@ -2627,14 +2649,14 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
2627 .memset => try airMemset(f, inst),2649 .memset => try airMemset(f, inst),
2628 .memcpy => try airMemcpy(f, inst),2650 .memcpy => try airMemcpy(f, inst),
2629 .set_union_tag => try airSetUnionTag(f, inst),2651 .set_union_tag => try airSetUnionTag(f, inst),
2630 .get_union_tag => try airGetUnionTag(f, inst),2652 .get_union_tag => CValue{ .inline_index = inst },
2631 .clz => try airUnBuiltinCall(f, inst, "clz", .Bits),2653 .clz => CValue{ .inline_index = inst },
2632 .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits),2654 .ctz => CValue{ .inline_index = inst },
2633 .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits),2655 .popcount => CValue{ .inline_index = inst },
2634 .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits),2656 .byte_swap => CValue{ .inline_index = inst },
2635 .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits),2657 .bit_reverse => CValue{ .inline_index = inst },
2636 .tag_name => try airTagName(f, inst),2658 .tag_name => CValue{ .inline_index = inst },
2637 .error_name => try airErrorName(f, inst),2659 .error_name => CValue{ .inline_index = inst },
2638 .splat => try airSplat(f, inst),2660 .splat => try airSplat(f, inst),
2639 .select => try airSelect(f, inst),2661 .select => try airSelect(f, inst),
2640 .shuffle => try airShuffle(f, inst),2662 .shuffle => try airShuffle(f, inst),
...@@ -2670,7 +2692,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO...@@ -2670,7 +2692,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
2670 .fpext,2692 .fpext,
2671 => try airFloatCast(f, inst),2693 => try airFloatCast(f, inst),
26722694
2673 .ptrtoint => try airPtrToInt(f, inst),2695 .ptrtoint => CValue{ .inline_index = inst },
26742696
2675 .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)),2697 .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)),
2676 .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)),2698 .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)),
...@@ -2754,7 +2776,7 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [...@@ -2754,7 +2776,7 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [
27542776
2755 const inst_ty = f.air.typeOfIndex(inst);2777 const inst_ty = f.air.typeOfIndex(inst);
2756 const ty_op = f.air.instructions.items(.data)[inst].ty_op;2778 const ty_op = f.air.instructions.items(.data)[inst].ty_op;
2757 const operand = try f.resolveInst(ty_op.operand);2779 const operand = try f.resolveInstNoInline(ty_op.operand);
2758 const writer = f.object.writer();2780 const writer = f.object.writer();
2759 const local = try f.allocLocal(inst_ty, .Const);2781 const local = try f.allocLocal(inst_ty, .Const);
2760 try writer.writeAll(" = ");2782 try writer.writeAll(" = ");
...@@ -4234,16 +4256,11 @@ fn airIsNull(...@@ -4234,16 +4256,11 @@ fn airIsNull(
4234 inst: Air.Inst.Index,4256 inst: Air.Inst.Index,
4235 operator: []const u8,4257 operator: []const u8,
4236 is_ptr: bool,4258 is_ptr: bool,
4237) !CValue {4259) !void {
4238 if (f.liveness.isUnused(inst))
4239 return CValue.none;
4240
4241 const un_op = f.air.instructions.items(.data)[inst].un_op;4260 const un_op = f.air.instructions.items(.data)[inst].un_op;
4242 const writer = f.object.writer();4261 const writer = f.object.writer();
4243 const operand = try f.resolveInst(un_op);4262 const operand = try f.resolveInst(un_op);
42444263
4245 const local = try f.allocLocal(Type.initTag(.bool), .Const);
4246 try writer.writeAll(" = ");
4247 try if (is_ptr) f.writeCValueDeref(writer, operand) else f.writeCValue(writer, operand, .Other);4264 try if (is_ptr) f.writeCValueDeref(writer, operand) else f.writeCValue(writer, operand, .Other);
42484265
4249 const operand_ty = f.air.typeOf(un_op);4266 const operand_ty = f.air.typeOf(un_op);
...@@ -4271,8 +4288,6 @@ fn airIsNull(...@@ -4271,8 +4288,6 @@ fn airIsNull(
4271 try writer.writeAll(operator);4288 try writer.writeAll(operator);
4272 try writer.writeByte(' ');4289 try writer.writeByte(' ');
4273 try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other);4290 try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other);
4274 try writer.writeAll(";\n");
4275 return local;
4276}4291}
42774292
4278fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue {4293fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue {
...@@ -4842,21 +4857,15 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4842,21 +4857,15 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
4842 return local;4857 return local;
4843}4858}
48444859
4845fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue {4860fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !void {
4846 if (f.liveness.isUnused(inst))
4847 return CValue.none;
4848
4849 const un_op = f.air.instructions.items(.data)[inst].un_op;4861 const un_op = f.air.instructions.items(.data)[inst].un_op;
4850 const writer = f.object.writer();4862 const writer = f.object.writer();
4851 const operand = try f.resolveInst(un_op);4863 const operand = try f.resolveInst(un_op);
4852 const operand_ty = f.air.typeOf(un_op);4864 const operand_ty = f.air.typeOf(un_op);
4853 const local = try f.allocLocal(Type.initTag(.bool), .Const);
4854 const err_union_ty = if (is_ptr) operand_ty.childType() else operand_ty;4865 const err_union_ty = if (is_ptr) operand_ty.childType() else operand_ty;
4855 const payload_ty = err_union_ty.errorUnionPayload();4866 const payload_ty = err_union_ty.errorUnionPayload();
4856 const error_ty = err_union_ty.errorUnionSet();4867 const error_ty = err_union_ty.errorUnionSet();
48574868
4858 try writer.writeAll(" = ");
4859
4860 if (!error_ty.errorSetIsEmpty())4869 if (!error_ty.errorSetIsEmpty())
4861 if (payload_ty.hasRuntimeBits())4870 if (payload_ty.hasRuntimeBits())
4862 if (is_ptr)4871 if (is_ptr)
...@@ -4871,8 +4880,6 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const...@@ -4871,8 +4880,6 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
4871 try writer.writeAll(operator);4880 try writer.writeAll(operator);
4872 try writer.writeByte(' ');4881 try writer.writeByte(' ');
4873 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);4882 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
4874 try writer.writeAll(";\n");
4875 return local;
4876}4883}
48774884
4878fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {4885fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
...@@ -4946,21 +4953,16 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4946,21 +4953,16 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {
4946 return local;4953 return local;
4947}4954}
49484955
4949fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {4956fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !void {
4950 if (f.liveness.isUnused(inst)) return CValue.none;
4951
4952 const inst_ty = f.air.typeOfIndex(inst);4957 const inst_ty = f.air.typeOfIndex(inst);
4953 const local = try f.allocLocal(inst_ty, .Const);
4954 const un_op = f.air.instructions.items(.data)[inst].un_op;4958 const un_op = f.air.instructions.items(.data)[inst].un_op;
4955 const writer = f.object.writer();4959 const writer = f.object.writer();
4956 const operand = try f.resolveInst(un_op);4960 const operand = try f.resolveInst(un_op);
49574961
4958 try writer.writeAll(" = (");4962 try writer.writeAll("(");
4959 try f.renderTypecast(writer, inst_ty);4963 try f.renderTypecast(writer, inst_ty);
4960 try writer.writeByte(')');4964 try writer.writeByte(')');
4961 try f.writeCValue(writer, operand, .Other);4965 try f.writeCValue(writer, operand, .Other);
4962 try writer.writeAll(";\n");
4963 return local;
4964}4966}
49654967
4966fn airUnBuiltinCall(4968fn airUnBuiltinCall(
...@@ -4968,24 +4970,19 @@ fn airUnBuiltinCall(...@@ -4968,24 +4970,19 @@ fn airUnBuiltinCall(
4968 inst: Air.Inst.Index,4970 inst: Air.Inst.Index,
4969 operation: []const u8,4971 operation: []const u8,
4970 info: BuiltinInfo,4972 info: BuiltinInfo,
4971) !CValue {4973) !void {
4972 if (f.liveness.isUnused(inst)) return CValue.none;
4973
4974 const inst_ty = f.air.typeOfIndex(inst);
4975 const operand = f.air.instructions.items(.data)[inst].ty_op.operand;4974 const operand = f.air.instructions.items(.data)[inst].ty_op.operand;
4976 const operand_ty = f.air.typeOf(operand);4975 const operand_ty = f.air.typeOf(operand);
49774976
4978 const local = try f.allocLocal(inst_ty, .Const);
4979 const writer = f.object.writer();4977 const writer = f.object.writer();
4980 try writer.writeAll(" = zig_");4978 try writer.writeAll("zig_");
4981 try writer.writeAll(operation);4979 try writer.writeAll(operation);
4982 try writer.writeByte('_');4980 try writer.writeByte('_');
4983 try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty);4981 try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty);
4984 try writer.writeByte('(');4982 try writer.writeByte('(');
4985 try f.writeCValue(writer, try f.resolveInst(operand), .FunctionArgument);4983 try f.writeCValue(writer, try f.resolveInst(operand), .FunctionArgument);
4986 try f.object.dg.renderBuiltinInfo(writer, operand_ty, info);4984 try f.object.dg.renderBuiltinInfo(writer, operand_ty, info);
4987 try writer.writeAll(");\n");4985 try writer.writeAll(")");
4988 return local;
4989}4986}
49904987
4991fn airBinBuiltinCall(4988fn airBinBuiltinCall(
...@@ -5258,12 +5255,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5258,12 +5255,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
5258 return CValue.none;5255 return CValue.none;
5259}5256}
52605257
5261fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {5258fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !void {
5262 if (f.liveness.isUnused(inst))
5263 return CValue.none;
5264
5265 const inst_ty = f.air.typeOfIndex(inst);
5266 const local = try f.allocLocal(inst_ty, .Const);
5267 const ty_op = f.air.instructions.items(.data)[inst].ty_op;5259 const ty_op = f.air.instructions.items(.data)[inst].ty_op;
5268 const un_ty = f.air.typeOf(ty_op.operand);5260 const un_ty = f.air.typeOf(ty_op.operand);
5269 const writer = f.object.writer();5261 const writer = f.object.writer();
...@@ -5271,44 +5263,31 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5271,44 +5263,31 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
52715263
5272 const target = f.object.dg.module.getTarget();5264 const target = f.object.dg.module.getTarget();
5273 const layout = un_ty.unionGetLayout(target);5265 const layout = un_ty.unionGetLayout(target);
5274 if (layout.tag_size == 0) return CValue.none;5266 assert(layout.tag_size != 0);
52755267
5276 try writer.writeAll(" = ");
5277 try f.writeCValue(writer, operand, .Other);5268 try f.writeCValue(writer, operand, .Other);
5278 try writer.writeAll(".tag;\n");5269 try writer.writeAll(".tag");
5279 return local;
5280}5270}
52815271
5282fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {5272fn airTagName(f: *Function, inst: Air.Inst.Index) !void {
5283 if (f.liveness.isUnused(inst)) return CValue.none;
5284
5285 const un_op = f.air.instructions.items(.data)[inst].un_op;5273 const un_op = f.air.instructions.items(.data)[inst].un_op;
5286 const inst_ty = f.air.typeOfIndex(inst);
5287 const enum_ty = f.air.typeOf(un_op);5274 const enum_ty = f.air.typeOf(un_op);
5288 const operand = try f.resolveInst(un_op);5275 const operand = try f.resolveInst(un_op);
52895276
5290 const writer = f.object.writer();5277 const writer = f.object.writer();
5291 const local = try f.allocLocal(inst_ty, .Const);5278 try writer.print("{s}(", .{try f.object.dg.getTagNameFn(enum_ty)});
5292 try writer.print(" = {s}(", .{try f.object.dg.getTagNameFn(enum_ty)});
5293 try f.writeCValue(writer, operand, .Other);5279 try f.writeCValue(writer, operand, .Other);
5294 try writer.writeAll(");\n");5280 try writer.writeAll(")");
5295
5296 return local;
5297}5281}
52985282
5299fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {5283fn airErrorName(f: *Function, inst: Air.Inst.Index) !void {
5300 if (f.liveness.isUnused(inst)) return CValue.none;
5301
5302 const un_op = f.air.instructions.items(.data)[inst].un_op;5284 const un_op = f.air.instructions.items(.data)[inst].un_op;
5303 const writer = f.object.writer();5285 const writer = f.object.writer();
5304 const inst_ty = f.air.typeOfIndex(inst);
5305 const operand = try f.resolveInst(un_op);5286 const operand = try f.resolveInst(un_op);
5306 const local = try f.allocLocal(inst_ty, .Const);
53075287
5308 try writer.writeAll(" = zig_errorName[");5288 try writer.writeAll("zig_errorName[");
5309 try f.writeCValue(writer, operand, .Other);5289 try f.writeCValue(writer, operand, .Other);
5310 try writer.writeAll("];\n");5290 try writer.writeAll("]");
5311 return local;
5312}5291}
53135292
5314fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {5293fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {