This commit is contained in:
agra
2026-03-02 17:18:47 +02:00
parent ba9c4d69ce
commit 2f4f898d54
20 changed files with 418 additions and 49 deletions

View File

@@ -2103,8 +2103,9 @@ pub const LLVMEmitter = struct {
}
// int→int: SExt, ZExt, or Trunc
const from_bits = intBits(from);
const to_bits = intBits(to);
const ptr_bits: u32 = @as(u32, self.ir_mod.types.pointer_size) * 8;
const from_bits = if (intBits(from) == 0) ptr_bits else intBits(from);
const to_bits = if (intBits(to) == 0) ptr_bits else intBits(to);
if (to_bits > from_bits) {
return if (isSignedType(from))
c.LLVMBuildSExt(self.builder, operand, to_ty, "sext")
@@ -2539,6 +2540,7 @@ pub const LLVMEmitter = struct {
.string => self.getStringStructType(),
.any => self.getAnyStructType(),
.noreturn => self.cached_void,
.isize, .usize => if (self.target_config.isWasm()) self.cached_i32 else self.cached_i64,
else => self.toLLVMTypeInfo(ty),
};
}
@@ -2665,6 +2667,7 @@ pub const LLVMEmitter = struct {
// For now, use opaque ptr
return self.cached_ptr;
},
.usize, .isize => if (self.target_config.isWasm()) self.cached_i32 else self.cached_i64,
};
}
@@ -2684,12 +2687,11 @@ pub const LLVMEmitter = struct {
if (info == .slice) return self.cached_ptr;
}
// WASM32: i64 → i32 for C ABI (size_t/ssize_t are 32-bit on wasm32)
// WASM32: usize/isize are pointer-sized (i32 on wasm32).
// Other integer types (s64, u64) keep their declared size — they represent
// genuinely 64-bit values (SDL_WindowFlags, timestamps, etc.).
if (self.target_config.isWasm()) {
if (c.LLVMGetTypeKind(llvm_ty) == c.LLVMIntegerTypeKind and c.LLVMGetIntTypeWidth(llvm_ty) == 64) {
// s64/u64 in extern decls → i32 on wasm32 (matches C's size_t, ssize_t, etc.)
return self.cached_i32;
}
if (ir_ty == .usize or ir_ty == .isize) return self.cached_i32;
return llvm_ty;
}
@@ -3075,7 +3077,7 @@ fn isFloatOrVecFloat(ty: TypeId, types: *const TypeTable) bool {
fn isSignedType(ty: TypeId) bool {
return switch (ty) {
.s8, .s16, .s32, .s64 => true,
.s8, .s16, .s32, .s64, .isize => true,
else => false,
};
}
@@ -3095,6 +3097,7 @@ fn intBits(ty: TypeId) u32 {
.s32, .u32 => 32,
.s64, .u64 => 64,
.bool => 1,
.usize, .isize => 0, // target-dependent — caller must query pointer_size
else => 64,
};
}