authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-25 18:49:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-25 18:49:25-07:00
log5b4885fe89939679ec2aa78f6bf16fe42faa781a
tree01aac39ae9129a32d176b8de7ec7706054467bdf
parent66986dd248a6f8b551509131b7268d49beb772ab

stage2 llvm backend: DeclGen and DeclFn have context field

instead of a context() accessor method.

1 files changed, 28 insertions(+), 29 deletions(-)

src/codegen/llvm.zig+28-29
......@@ -351,6 +351,7 @@ pub const Object = struct {
351351 liveness: Liveness,
352352 ) !void {
353353 var dg: DeclGen = .{
354 .context = self.context,
354355 .object = self,
355356 .module = module,
356357 .decl = func.owner_decl,
......@@ -375,15 +376,16 @@ pub const Object = struct {
375376 bb.deleteBasicBlock();
376377 }
377378
378 const builder = dg.context().createBuilder();
379 const builder = dg.context.createBuilder();
379380
380 const entry_block = dg.context().appendBasicBlock(llvm_func, "Entry");
381 const entry_block = dg.context.appendBasicBlock(llvm_func, "Entry");
381382 builder.positionBuilderAtEnd(entry_block);
382383
383384 var fg: FuncGen = .{
384385 .gpa = dg.gpa,
385386 .air = air,
386387 .liveness = liveness,
388 .context = dg.context,
387389 .dg = &dg,
388390 .builder = builder,
389391 .args = args,
......@@ -409,6 +411,7 @@ pub const Object = struct {
409411
410412 pub fn updateDecl(self: *Object, module: *Module, decl: *Module.Decl) !void {
411413 var dg: DeclGen = .{
414 .context = self.context,
412415 .object = self,
413416 .module = module,
414417 .decl = decl,
......@@ -428,6 +431,7 @@ pub const Object = struct {
428431};
429432
430433pub const DeclGen = struct {
434 context: *const llvm.Context,
431435 object: *Object,
432436 module: *Module,
433437 decl: *Module.Decl,
......@@ -447,10 +451,6 @@ pub const DeclGen = struct {
447451 return self.object.llvm_module;
448452 }
449453
450 fn context(self: *DeclGen) *const llvm.Context {
451 return self.object.context;
452 }
453
454454 fn genDecl(self: *DeclGen) !void {
455455 const decl = self.decl;
456456 assert(decl.has_tv);
......@@ -525,13 +525,13 @@ pub const DeclGen = struct {
525525 fn llvmType(self: *DeclGen, t: Type) error{ OutOfMemory, CodegenFail }!*const llvm.Type {
526526 log.debug("llvmType for {}", .{t});
527527 switch (t.zigTypeTag()) {
528 .Void => return self.context().voidType(),
529 .NoReturn => return self.context().voidType(),
528 .Void => return self.context.voidType(),
529 .NoReturn => return self.context.voidType(),
530530 .Int => {
531531 const info = t.intInfo(self.module.getTarget());
532 return self.context().intType(info.bits);
532 return self.context.intType(info.bits);
533533 },
534 .Bool => return self.context().intType(1),
534 .Bool => return self.context.intType(1),
535535 .Pointer => {
536536 if (t.isSlice()) {
537537 return self.todo("implement slices", .{});
......@@ -551,13 +551,16 @@ pub const DeclGen = struct {
551551
552552 var optional_types: [2]*const llvm.Type = .{
553553 try self.llvmType(child_type),
554 self.context().intType(1),
554 self.context.intType(1),
555555 };
556 return self.context().structType(&optional_types, 2, .False);
556 return self.context.structType(&optional_types, 2, .False);
557557 } else {
558558 return self.todo("implement optional pointers as actual pointers", .{});
559559 }
560560 },
561 .ErrorUnion => {
562 return self.todo("implement llvmType for error unions", .{});
563 },
561564 .ComptimeInt => unreachable,
562565 .ComptimeFloat => unreachable,
563566 .Type => unreachable,
......@@ -569,7 +572,6 @@ pub const DeclGen = struct {
569572
570573 .Float,
571574 .Struct,
572 .ErrorUnion,
573575 .ErrorSet,
574576 .Enum,
575577 .Union,
......@@ -638,7 +640,7 @@ pub const DeclGen = struct {
638640 return self.todo("handle other sentinel values", .{});
639641 } else false;
640642
641 return self.context().constString(payload.data.ptr, @intCast(c_uint, payload.data.len), llvm.Bool.fromBool(!zero_sentinel));
643 return self.context.constString(payload.data.ptr, @intCast(c_uint, payload.data.len), llvm.Bool.fromBool(!zero_sentinel));
642644 } else {
643645 return self.todo("handle more array values", .{});
644646 }
......@@ -652,15 +654,15 @@ pub const DeclGen = struct {
652654 if (tv.val.tag() == .null_value) {
653655 var optional_values: [2]*const llvm.Value = .{
654656 llvm_child_type.constNull(),
655 self.context().intType(1).constNull(),
657 self.context.intType(1).constNull(),
656658 };
657 return self.context().constStruct(&optional_values, 2, .False);
659 return self.context.constStruct(&optional_values, 2, .False);
658660 } else {
659661 var optional_values: [2]*const llvm.Value = .{
660662 try self.genTypedValue(.{ .ty = child_type, .val = tv.val }, fg),
661 self.context().intType(1).constAllOnes(),
663 self.context.intType(1).constAllOnes(),
662664 };
663 return self.context().constStruct(&optional_values, 2, .False);
665 return self.context.constStruct(&optional_values, 2, .False);
664666 }
665667 } else {
666668 return self.todo("implement const of optional pointer", .{});
......@@ -674,7 +676,7 @@ pub const DeclGen = struct {
674676 fn addAttr(self: *DeclGen, val: *const llvm.Value, index: llvm.AttributeIndex, name: []const u8) void {
675677 const kind_id = llvm.getEnumAttributeKindForName(name.ptr, name.len);
676678 assert(kind_id != 0);
677 const llvm_attr = self.context().createEnumAttribute(kind_id, 0);
679 const llvm_attr = self.context.createEnumAttribute(kind_id, 0);
678680 val.addAttributeAtIndex(index, llvm_attr);
679681 }
680682
......@@ -689,6 +691,7 @@ pub const FuncGen = struct {
689691 dg: *DeclGen,
690692 air: Air,
691693 liveness: Liveness,
694 context: *const llvm.Context,
692695
693696 builder: *const llvm.Builder,
694697
......@@ -734,10 +737,6 @@ pub const FuncGen = struct {
734737 return self.dg.object.llvm_module;
735738 }
736739
737 fn context(self: *FuncGen) *const llvm.Context {
738 return self.dg.object.context;
739 }
740
741740 fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) !*const llvm.Value {
742741 if (self.air.value(inst)) |val| {
743742 return self.dg.genTypedValue(.{ .ty = self.air.typeOf(inst), .val = val }, self);
......@@ -884,7 +883,7 @@ pub const FuncGen = struct {
884883 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
885884 const extra = self.air.extraData(Air.Block, ty_pl.payload);
886885 const body = self.air.extra[extra.end..][0..extra.data.body_len];
887 const parent_bb = self.context().createBasicBlock("Block");
886 const parent_bb = self.context.createBasicBlock("Block");
888887
889888 // 5 breaks to a block seems like a reasonable default.
890889 var break_bbs = try BreakBasicBlocks.initCapacity(self.gpa, 5);
......@@ -943,8 +942,8 @@ pub const FuncGen = struct {
943942 const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len];
944943 const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
945944
946 const then_block = self.context().appendBasicBlock(self.llvm_func, "Then");
947 const else_block = self.context().appendBasicBlock(self.llvm_func, "Else");
945 const then_block = self.context.appendBasicBlock(self.llvm_func, "Then");
946 const else_block = self.context.appendBasicBlock(self.llvm_func, "Else");
948947 {
949948 const prev_block = self.builder.getInsertBlock();
950949 defer self.builder.positionBuilderAtEnd(prev_block);
......@@ -963,7 +962,7 @@ pub const FuncGen = struct {
963962 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
964963 const loop = self.air.extraData(Air.Block, ty_pl.payload);
965964 const body = self.air.extra[loop.end..][0..loop.data.body_len];
966 const loop_block = self.context().appendBasicBlock(self.llvm_func, "Loop");
965 const loop_block = self.context.appendBasicBlock(self.llvm_func, "Loop");
967966 _ = self.builder.buildBr(loop_block);
968967
969968 self.builder.positionBuilderAtEnd(loop_block);
......@@ -1119,7 +1118,7 @@ pub const FuncGen = struct {
11191118 const operand = try self.resolveInst(un_op);
11201119
11211120 if (operand_is_ptr) {
1122 const index_type = self.context().intType(32);
1121 const index_type = self.context.intType(32);
11231122
11241123 var indices: [2]*const llvm.Value = .{
11251124 index_type.constNull(),
......@@ -1151,7 +1150,7 @@ pub const FuncGen = struct {
11511150 const operand = try self.resolveInst(ty_op.operand);
11521151
11531152 if (operand_is_ptr) {
1154 const index_type = self.context().intType(32);
1153 const index_type = self.context.intType(32);
11551154
11561155 var indices: [2]*const llvm.Value = .{
11571156 index_type.constNull(),