tiny.chant.lower.convert
Defined in lower.
API (8)
Actions
Public operations.
Source
Source: lib/chant/src/lower/convert.zig
zig
const choir = @import("choir");const ast = @import("../ast/root.zig");const context = @import("context.zig");const emit = @import("emit.zig");const Error = @import("error.zig").Error;const ArithDialect = choir.dialects.ArithDialect;const MemrefDialect = choir.dialects.MemrefDialect;const Lowerer = context.Lowerer;pub fn scalarKind(c_type: *const ast.Type) Error!choir.dialects.arith.ScalarKind { return switch (c_type.kind) { .enum_type => try scalarKind(c_type.child orelse &ast.types.int_type), .char_type => if (c_type.is_unsigned) .u8 else .i8, .short_type => if (c_type.is_unsigned) .u16 else .i16, .int_type => if (c_type.is_unsigned) .u32 else .i32, .long_type => if (c_type.is_unsigned) .u64 else .i64, .bitint_type => try bitintScalarKind(c_type.bit_width, c_type.is_unsigned), .float_type => .f32, .double_type => .f64, .decimal32_type => .f32, .decimal64_type => .f64, .decimal128_type => error.UnsupportedType, else => error.UnsupportedType, };}fn bitintScalarKind(width: u16, is_unsigned: bool) Error!choir.dialects.arith.ScalarKind { if (width <= 8) return if (is_unsigned) .u8 else .i8; if (width <= 16) return if (is_unsigned) .u16 else .i16; if (width <= 32) return if (is_unsigned) .u32 else .i32; if (width <= 64) return if (is_unsigned) .u64 else .i64; return error.UnsupportedType;}pub fn scalarType(lowerer: *Lowerer, c_type: *const ast.Type) Error!choir.Type { const kind = try scalarKind(c_type); return ArithDialect.getScalarType(lowerer.ctx, kind) catch return error.OutOfMemory;}pub fn pointerElement(c_type: *const ast.Type) Error!*const ast.Type { var current = ast.types.element(c_type) orelse return error.UnsupportedType; while (current.kind == .array) { current = ast.types.element(current) orelse return error.UnsupportedType; } if (!ast.types.isArithmetic(current)) return error.UnsupportedType; return current;}pub fn valueType(lowerer: *Lowerer, c_type: *const ast.Type) Error!choir.Type { if (ast.types.isPointerLike(c_type)) { const element = try pointerElement(c_type); const element_type = try scalarType(lowerer, element); return MemrefDialect.getMemrefTypeDynamic(lowerer.ctx, element_type, .host) catch return error.OutOfMemory; } return scalarType(lowerer, c_type);}pub fn sameScalar(lhs: *const ast.Type, rhs: *const ast.Type) bool { if (lhs.kind == .enum_type and rhs.kind == .enum_type) { return sameScalar(lhs.child orelse &ast.types.int_type, rhs.child orelse &ast.types.int_type); } return lhs.kind == rhs.kind and lhs.is_unsigned == rhs.is_unsigned and lhs.bit_width == rhs.bit_width;}pub fn promote(c_type: *const ast.Type) *const ast.Type { if (c_type.kind == .bitint_type) return c_type; if (c_type.kind == .enum_type) return promote(c_type.child orelse &ast.types.int_type); if (ast.types.isInteger(c_type) and ast.types.integerRank(c_type) < ast.types.integerRank(&ast.types.int_type)) { return &ast.types.int_type; } return c_type;}pub fn convert( lowerer: *Lowerer, value: *choir.Value, from: *const ast.Type, to: *const ast.Type,) Error!*choir.Value { if (sameScalar(from, to)) return value; if (!ast.types.isArithmetic(from) or !ast.types.isArithmetic(to)) return error.UnsupportedType; const target = try scalarType(lowerer, to); const cast = ArithDialect.CastOp.create(lowerer.ctx, lowerer.loc, value, target) catch return error.OutOfMemory; try emit.append(lowerer, cast.op); var mutable = cast; return mutable.getResult();}pub fn toIndex(lowerer: *Lowerer, value: *choir.Value, from: *const ast.Type) Error!*choir.Value { if (!ast.types.isInteger(from)) return error.UnsupportedType; const index_type = ArithDialect.getIndexType(lowerer.ctx) catch return error.OutOfMemory; const cast = ArithDialect.CastOp.create(lowerer.ctx, lowerer.loc, value, index_type) catch return error.OutOfMemory; try emit.append(lowerer, cast.op); var mutable = cast; return mutable.getResult();}Source: lib/chant/src/lower/root.zig:5
zig
pub const convert = @import("convert.zig");Complete caller list for lower.convert.scalarType
7 direct callers.
tiny.chant.lower.convert.convert[function] atlib/chant/src/lower/convert.zig:75tiny.chant.lower.convert.valueType[function] atlib/chant/src/lower/convert.zig:50lib.chant.src.lower.local.intValue[function] — private source atlib/chant/src/lower/local.zig:157in nearest public ownertiny.chant.lower.locallib.chant.src.lower.local.zeroValue[function] — private source atlib/chant/src/lower/local.zig:147in nearest public ownertiny.chant.lower.localtiny.chant.lower.memory.allocaObject[function] atlib/chant/src/lower/memory.zig:27tiny.chant.lower.memory.allocaScalar[function] atlib/chant/src/lower/memory.zig:18tiny.chant.lower.memory.loadElement[function] atlib/chant/src/lower/memory.zig:51
Audit
| Definitions | 9 |
|---|---|
| Public names | 9 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |