lib/chant/src/lower/emit.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const choir = @import("choir");
 2 const context = @import("context.zig");
 3 const Error = @import("error.zig").Error;
 4 
 5 const ArithDialect = choir.dialects.ArithDialect;
 6 const ScfDialect = choir.dialects.ScfDialect;
 7 const Lowerer = context.Lowerer;
 8 
 9 pub fn append(lowerer: *Lowerer, op: *choir.Operation) Error!void {
10     lowerer.block.addOperation(op) catch return error.OutOfMemory;
11 }
12 
13 pub fn indexConstant(lowerer: *Lowerer, value: i64) Error!*choir.Value {
14     const index_type = ArithDialect.getIndexType(lowerer.ctx) catch return error.OutOfMemory;
15     const constant = ArithDialect.ConstantOp.createInt(lowerer.ctx, lowerer.loc, index_type, value) catch return error.OutOfMemory;
16     try append(lowerer, constant.op);
17     var mutable = constant;
18     return mutable.getResult();
19 }
20 
21 pub fn emitYield(lowerer: *Lowerer, values: []const *choir.Value) Error!void {
22     if (lowerer.terminated) {
23         lowerer.terminated = false;
24         return;
25     }
26     const yield = ScfDialect.YieldOp.create(lowerer.ctx, lowerer.loc, values) catch return error.OutOfMemory;
27     try append(lowerer, yield.op);
28 }