...
This commit is contained in:
@@ -2286,6 +2286,9 @@ pub const CodeGen = struct {
|
||||
.index_expr => |ie| {
|
||||
return self.genIndexExpr(ie);
|
||||
},
|
||||
.slice_expr => |se| {
|
||||
return self.genSliceExpr(se);
|
||||
},
|
||||
.call => |call_node| {
|
||||
return self.genCall(call_node);
|
||||
},
|
||||
@@ -3381,6 +3384,12 @@ pub const CodeGen = struct {
|
||||
}
|
||||
return self.emitErrorFmt("no field '{s}' on slice (available: .len, .ptr)", .{fa.field});
|
||||
}
|
||||
if (entry.ty.isArray()) {
|
||||
if (std.mem.eql(u8, fa.field, "len")) {
|
||||
return c.LLVMConstInt(c.LLVMInt32TypeInContext(self.context), entry.ty.array_type.length, 0);
|
||||
}
|
||||
return self.emitErrorFmt("no field '{s}' on array (available: .len)", .{fa.field});
|
||||
}
|
||||
if (entry.ty.isAny()) {
|
||||
const any_val = c.LLVMBuildLoad2(self.builder, self.getAnyStructType(), entry.ptr, "any_load");
|
||||
if (std.mem.eql(u8, fa.field, "tag")) {
|
||||
@@ -3488,6 +3497,73 @@ pub const CodeGen = struct {
|
||||
return self.emitError("index expression requires an array, vector, string, or slice");
|
||||
}
|
||||
|
||||
fn genSliceExpr(self: *CodeGen, se: ast.SliceExpr) !c.LLVMValueRef {
|
||||
const obj_ty = self.inferType(se.object);
|
||||
const i32_ty = c.LLVMInt32TypeInContext(self.context);
|
||||
const zero = c.LLVMConstInt(i32_ty, 0, 0);
|
||||
const slice_struct_ty = self.getStringStructType();
|
||||
|
||||
// Resolve start (default: 0)
|
||||
const start_val = if (se.start) |s| try self.genExpr(s) else zero;
|
||||
|
||||
if (obj_ty.isArray()) {
|
||||
const arr_info = obj_ty.array_type;
|
||||
// Resolve end (default: array length)
|
||||
const end_val = if (se.end) |e| try self.genExpr(e) else c.LLVMConstInt(i32_ty, arr_info.length, 0);
|
||||
// Get array alloca
|
||||
const arr_ptr = blk: {
|
||||
if (se.object.data == .identifier) {
|
||||
if (self.named_values.get(se.object.data.identifier.name)) |entry| {
|
||||
break :blk entry.ptr;
|
||||
}
|
||||
}
|
||||
break :blk try self.genExpr(se.object);
|
||||
};
|
||||
// GEP to arr[start]
|
||||
var indices = [_]c.LLVMValueRef{ zero, start_val };
|
||||
const elem_ptr = c.LLVMBuildGEP2(self.builder, self.typeToLLVM(obj_ty), arr_ptr, &indices, 2, "slice_start");
|
||||
// len = end - start
|
||||
const len_val = c.LLVMBuildSub(self.builder, end_val, start_val, "slice_len");
|
||||
// Build {ptr, len}
|
||||
var result = c.LLVMGetUndef(slice_struct_ty);
|
||||
result = c.LLVMBuildInsertValue(self.builder, result, elem_ptr, 0, "slice_ptr");
|
||||
result = c.LLVMBuildInsertValue(self.builder, result, len_val, 1, "slice_len");
|
||||
return result;
|
||||
}
|
||||
|
||||
if (obj_ty == .string_type or obj_ty.isSlice()) {
|
||||
// Load {ptr, len} from variable or expression
|
||||
const obj_val = blk: {
|
||||
if (se.object.data == .identifier) {
|
||||
if (self.named_values.get(se.object.data.identifier.name)) |entry| {
|
||||
break :blk c.LLVMBuildLoad2(self.builder, slice_struct_ty, entry.ptr, "sslice_load");
|
||||
}
|
||||
}
|
||||
break :blk try self.genExpr(se.object);
|
||||
};
|
||||
const base_ptr = c.LLVMBuildExtractValue(self.builder, obj_val, 0, "sslice_ptr");
|
||||
const base_len = c.LLVMBuildExtractValue(self.builder, obj_val, 1, "sslice_len");
|
||||
// Resolve end (default: original length)
|
||||
const end_val = if (se.end) |e| try self.genExpr(e) else base_len;
|
||||
// GEP base_ptr + start
|
||||
const elem_llvm_ty = if (obj_ty == .string_type)
|
||||
c.LLVMInt8TypeInContext(self.context)
|
||||
else
|
||||
self.typeToLLVM(Type.fromName(obj_ty.slice_type.element_name) orelse return self.emitError("unknown slice element type"));
|
||||
var gep_indices = [_]c.LLVMValueRef{start_val};
|
||||
const new_ptr = c.LLVMBuildGEP2(self.builder, elem_llvm_ty, base_ptr, &gep_indices, 1, "sslice_off");
|
||||
// len = end - start
|
||||
const len_val = c.LLVMBuildSub(self.builder, end_val, start_val, "sslice_len");
|
||||
// Build {ptr, len}
|
||||
var result = c.LLVMGetUndef(slice_struct_ty);
|
||||
result = c.LLVMBuildInsertValue(self.builder, result, new_ptr, 0, "sslice_ptr");
|
||||
result = c.LLVMBuildInsertValue(self.builder, result, len_val, 1, "sslice_len");
|
||||
return result;
|
||||
}
|
||||
|
||||
return self.emitError("slice expression requires an array, string, or slice");
|
||||
}
|
||||
|
||||
fn genBinaryOp(self: *CodeGen, op: ast.BinaryOp.Op, lhs: c.LLVMValueRef, rhs: c.LLVMValueRef, result_type: Type) c.LLVMValueRef {
|
||||
// Vector types: dispatch based on element type (LLVM does element-wise automatically)
|
||||
if (result_type.isVector()) {
|
||||
@@ -5018,6 +5094,17 @@ pub const CodeGen = struct {
|
||||
return ty;
|
||||
}
|
||||
}
|
||||
if (std.mem.eql(u8, base, "Array")) {
|
||||
if (args.len >= 2) {
|
||||
const n: u32 = @intCast(self.resolveValueArg(args[0]));
|
||||
const elem = self.resolveType(args[1]);
|
||||
const elem_name = elem.displayName(self.allocator) catch return null;
|
||||
const ty: Type = .{ .array_type = .{ .element_name = elem_name, .length = n } };
|
||||
const any_name = std.fmt.allocPrint(self.allocator, "[{d}]{s}", .{ n, elem_name }) catch return null;
|
||||
_ = self.getAnyTypeId(any_name, ty) catch return null;
|
||||
return ty;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -5300,6 +5387,9 @@ pub const CodeGen = struct {
|
||||
if (obj_ty.isSlice()) {
|
||||
if (std.mem.eql(u8, fa.field, "len")) return Type.s(32);
|
||||
}
|
||||
if (obj_ty.isArray()) {
|
||||
if (std.mem.eql(u8, fa.field, "len")) return Type.s(32);
|
||||
}
|
||||
if (obj_ty.isAny()) {
|
||||
if (std.mem.eql(u8, fa.field, "tag")) return Type.s(32);
|
||||
if (std.mem.eql(u8, fa.field, "value")) return Type.s(64);
|
||||
@@ -5338,6 +5428,13 @@ pub const CodeGen = struct {
|
||||
}
|
||||
return Type.s(32);
|
||||
},
|
||||
.slice_expr => |se| {
|
||||
const obj_ty = self.inferType(se.object);
|
||||
if (obj_ty == .string_type) return .string_type;
|
||||
if (obj_ty.isArray()) return .{ .slice_type = .{ .element_name = obj_ty.array_type.element_name } };
|
||||
if (obj_ty.isSlice()) return obj_ty;
|
||||
return .void_type;
|
||||
},
|
||||
.array_literal => |al| {
|
||||
if (al.elements.len == 0) return .void_type;
|
||||
const elem_ty = self.inferType(al.elements[0]);
|
||||
|
||||
Reference in New Issue
Block a user