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 {...@@ -351,6 +351,7 @@ pub const Object = struct {
351 liveness: Liveness,351 liveness: Liveness,
352 ) !void {352 ) !void {
353 var dg: DeclGen = .{353 var dg: DeclGen = .{
354 .context = self.context,
354 .object = self,355 .object = self,
355 .module = module,356 .module = module,
356 .decl = func.owner_decl,357 .decl = func.owner_decl,
...@@ -375,15 +376,16 @@ pub const Object = struct {...@@ -375,15 +376,16 @@ pub const Object = struct {
375 bb.deleteBasicBlock();376 bb.deleteBasicBlock();
376 }377 }
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");
381 builder.positionBuilderAtEnd(entry_block);382 builder.positionBuilderAtEnd(entry_block);
382383
383 var fg: FuncGen = .{384 var fg: FuncGen = .{
384 .gpa = dg.gpa,385 .gpa = dg.gpa,
385 .air = air,386 .air = air,
386 .liveness = liveness,387 .liveness = liveness,
388 .context = dg.context,
387 .dg = &dg,389 .dg = &dg,
388 .builder = builder,390 .builder = builder,
389 .args = args,391 .args = args,
...@@ -409,6 +411,7 @@ pub const Object = struct {...@@ -409,6 +411,7 @@ pub const Object = struct {
409411
410 pub fn updateDecl(self: *Object, module: *Module, decl: *Module.Decl) !void {412 pub fn updateDecl(self: *Object, module: *Module, decl: *Module.Decl) !void {
411 var dg: DeclGen = .{413 var dg: DeclGen = .{
414 .context = self.context,
412 .object = self,415 .object = self,
413 .module = module,416 .module = module,
414 .decl = decl,417 .decl = decl,
...@@ -428,6 +431,7 @@ pub const Object = struct {...@@ -428,6 +431,7 @@ pub const Object = struct {
428};431};
429432
430pub const DeclGen = struct {433pub const DeclGen = struct {
434 context: *const llvm.Context,
431 object: *Object,435 object: *Object,
432 module: *Module,436 module: *Module,
433 decl: *Module.Decl,437 decl: *Module.Decl,
...@@ -447,10 +451,6 @@ pub const DeclGen = struct {...@@ -447,10 +451,6 @@ pub const DeclGen = struct {
447 return self.object.llvm_module;451 return self.object.llvm_module;
448 }452 }
449453
450 fn context(self: *DeclGen) *const llvm.Context {
451 return self.object.context;
452 }
453
454 fn genDecl(self: *DeclGen) !void {454 fn genDecl(self: *DeclGen) !void {
455 const decl = self.decl;455 const decl = self.decl;
456 assert(decl.has_tv);456 assert(decl.has_tv);
...@@ -525,13 +525,13 @@ pub const DeclGen = struct {...@@ -525,13 +525,13 @@ pub const DeclGen = struct {
525 fn llvmType(self: *DeclGen, t: Type) error{ OutOfMemory, CodegenFail }!*const llvm.Type {525 fn llvmType(self: *DeclGen, t: Type) error{ OutOfMemory, CodegenFail }!*const llvm.Type {
526 log.debug("llvmType for {}", .{t});526 log.debug("llvmType for {}", .{t});
527 switch (t.zigTypeTag()) {527 switch (t.zigTypeTag()) {
528 .Void => return self.context().voidType(),528 .Void => return self.context.voidType(),
529 .NoReturn => return self.context().voidType(),529 .NoReturn => return self.context.voidType(),
530 .Int => {530 .Int => {
531 const info = t.intInfo(self.module.getTarget());531 const info = t.intInfo(self.module.getTarget());
532 return self.context().intType(info.bits);532 return self.context.intType(info.bits);
533 },533 },
534 .Bool => return self.context().intType(1),534 .Bool => return self.context.intType(1),
535 .Pointer => {535 .Pointer => {
536 if (t.isSlice()) {536 if (t.isSlice()) {
537 return self.todo("implement slices", .{});537 return self.todo("implement slices", .{});
...@@ -551,13 +551,16 @@ pub const DeclGen = struct {...@@ -551,13 +551,16 @@ pub const DeclGen = struct {
551551
552 var optional_types: [2]*const llvm.Type = .{552 var optional_types: [2]*const llvm.Type = .{
553 try self.llvmType(child_type),553 try self.llvmType(child_type),
554 self.context().intType(1),554 self.context.intType(1),
555 };555 };
556 return self.context().structType(&optional_types, 2, .False);556 return self.context.structType(&optional_types, 2, .False);
557 } else {557 } else {
558 return self.todo("implement optional pointers as actual pointers", .{});558 return self.todo("implement optional pointers as actual pointers", .{});
559 }559 }
560 },560 },
561 .ErrorUnion => {
562 return self.todo("implement llvmType for error unions", .{});
563 },
561 .ComptimeInt => unreachable,564 .ComptimeInt => unreachable,
562 .ComptimeFloat => unreachable,565 .ComptimeFloat => unreachable,
563 .Type => unreachable,566 .Type => unreachable,
...@@ -569,7 +572,6 @@ pub const DeclGen = struct {...@@ -569,7 +572,6 @@ pub const DeclGen = struct {
569572
570 .Float,573 .Float,
571 .Struct,574 .Struct,
572 .ErrorUnion,
573 .ErrorSet,575 .ErrorSet,
574 .Enum,576 .Enum,
575 .Union,577 .Union,
...@@ -638,7 +640,7 @@ pub const DeclGen = struct {...@@ -638,7 +640,7 @@ pub const DeclGen = struct {
638 return self.todo("handle other sentinel values", .{});640 return self.todo("handle other sentinel values", .{});
639 } else false;641 } 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));
642 } else {644 } else {
643 return self.todo("handle more array values", .{});645 return self.todo("handle more array values", .{});
644 }646 }
...@@ -652,15 +654,15 @@ pub const DeclGen = struct {...@@ -652,15 +654,15 @@ pub const DeclGen = struct {
652 if (tv.val.tag() == .null_value) {654 if (tv.val.tag() == .null_value) {
653 var optional_values: [2]*const llvm.Value = .{655 var optional_values: [2]*const llvm.Value = .{
654 llvm_child_type.constNull(),656 llvm_child_type.constNull(),
655 self.context().intType(1).constNull(),657 self.context.intType(1).constNull(),
656 };658 };
657 return self.context().constStruct(&optional_values, 2, .False);659 return self.context.constStruct(&optional_values, 2, .False);
658 } else {660 } else {
659 var optional_values: [2]*const llvm.Value = .{661 var optional_values: [2]*const llvm.Value = .{
660 try self.genTypedValue(.{ .ty = child_type, .val = tv.val }, fg),662 try self.genTypedValue(.{ .ty = child_type, .val = tv.val }, fg),
661 self.context().intType(1).constAllOnes(),663 self.context.intType(1).constAllOnes(),
662 };664 };
663 return self.context().constStruct(&optional_values, 2, .False);665 return self.context.constStruct(&optional_values, 2, .False);
664 }666 }
665 } else {667 } else {
666 return self.todo("implement const of optional pointer", .{});668 return self.todo("implement const of optional pointer", .{});
...@@ -674,7 +676,7 @@ pub const DeclGen = struct {...@@ -674,7 +676,7 @@ pub const DeclGen = struct {
674 fn addAttr(self: *DeclGen, val: *const llvm.Value, index: llvm.AttributeIndex, name: []const u8) void {676 fn addAttr(self: *DeclGen, val: *const llvm.Value, index: llvm.AttributeIndex, name: []const u8) void {
675 const kind_id = llvm.getEnumAttributeKindForName(name.ptr, name.len);677 const kind_id = llvm.getEnumAttributeKindForName(name.ptr, name.len);
676 assert(kind_id != 0);678 assert(kind_id != 0);
677 const llvm_attr = self.context().createEnumAttribute(kind_id, 0);679 const llvm_attr = self.context.createEnumAttribute(kind_id, 0);
678 val.addAttributeAtIndex(index, llvm_attr);680 val.addAttributeAtIndex(index, llvm_attr);
679 }681 }
680682
...@@ -689,6 +691,7 @@ pub const FuncGen = struct {...@@ -689,6 +691,7 @@ pub const FuncGen = struct {
689 dg: *DeclGen,691 dg: *DeclGen,
690 air: Air,692 air: Air,
691 liveness: Liveness,693 liveness: Liveness,
694 context: *const llvm.Context,
692695
693 builder: *const llvm.Builder,696 builder: *const llvm.Builder,
694697
...@@ -734,10 +737,6 @@ pub const FuncGen = struct {...@@ -734,10 +737,6 @@ pub const FuncGen = struct {
734 return self.dg.object.llvm_module;737 return self.dg.object.llvm_module;
735 }738 }
736739
737 fn context(self: *FuncGen) *const llvm.Context {
738 return self.dg.object.context;
739 }
740
741 fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) !*const llvm.Value {740 fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) !*const llvm.Value {
742 if (self.air.value(inst)) |val| {741 if (self.air.value(inst)) |val| {
743 return self.dg.genTypedValue(.{ .ty = self.air.typeOf(inst), .val = val }, self);742 return self.dg.genTypedValue(.{ .ty = self.air.typeOf(inst), .val = val }, self);
...@@ -884,7 +883,7 @@ pub const FuncGen = struct {...@@ -884,7 +883,7 @@ pub const FuncGen = struct {
884 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;883 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
885 const extra = self.air.extraData(Air.Block, ty_pl.payload);884 const extra = self.air.extraData(Air.Block, ty_pl.payload);
886 const body = self.air.extra[extra.end..][0..extra.data.body_len];885 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
889 // 5 breaks to a block seems like a reasonable default.888 // 5 breaks to a block seems like a reasonable default.
890 var break_bbs = try BreakBasicBlocks.initCapacity(self.gpa, 5);889 var break_bbs = try BreakBasicBlocks.initCapacity(self.gpa, 5);
...@@ -943,8 +942,8 @@ pub const FuncGen = struct {...@@ -943,8 +942,8 @@ pub const FuncGen = struct {
943 const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len];942 const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len];
944 const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];943 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");945 const then_block = self.context.appendBasicBlock(self.llvm_func, "Then");
947 const else_block = self.context().appendBasicBlock(self.llvm_func, "Else");946 const else_block = self.context.appendBasicBlock(self.llvm_func, "Else");
948 {947 {
949 const prev_block = self.builder.getInsertBlock();948 const prev_block = self.builder.getInsertBlock();
950 defer self.builder.positionBuilderAtEnd(prev_block);949 defer self.builder.positionBuilderAtEnd(prev_block);
...@@ -963,7 +962,7 @@ pub const FuncGen = struct {...@@ -963,7 +962,7 @@ pub const FuncGen = struct {
963 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;962 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
964 const loop = self.air.extraData(Air.Block, ty_pl.payload);963 const loop = self.air.extraData(Air.Block, ty_pl.payload);
965 const body = self.air.extra[loop.end..][0..loop.data.body_len];964 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");
967 _ = self.builder.buildBr(loop_block);966 _ = self.builder.buildBr(loop_block);
968967
969 self.builder.positionBuilderAtEnd(loop_block);968 self.builder.positionBuilderAtEnd(loop_block);
...@@ -1119,7 +1118,7 @@ pub const FuncGen = struct {...@@ -1119,7 +1118,7 @@ pub const FuncGen = struct {
1119 const operand = try self.resolveInst(un_op);1118 const operand = try self.resolveInst(un_op);
11201119
1121 if (operand_is_ptr) {1120 if (operand_is_ptr) {
1122 const index_type = self.context().intType(32);1121 const index_type = self.context.intType(32);
11231122
1124 var indices: [2]*const llvm.Value = .{1123 var indices: [2]*const llvm.Value = .{
1125 index_type.constNull(),1124 index_type.constNull(),
...@@ -1151,7 +1150,7 @@ pub const FuncGen = struct {...@@ -1151,7 +1150,7 @@ pub const FuncGen = struct {
1151 const operand = try self.resolveInst(ty_op.operand);1150 const operand = try self.resolveInst(ty_op.operand);
11521151
1153 if (operand_is_ptr) {1152 if (operand_is_ptr) {
1154 const index_type = self.context().intType(32);1153 const index_type = self.context.intType(32);
11551154
1156 var indices: [2]*const llvm.Value = .{1155 var indices: [2]*const llvm.Value = .{
1157 index_type.constNull(),1156 index_type.constNull(),