Skip to content

Commit 55003dd

Browse files
committed
Fixed certain type issues.
1 parent bb3676b commit 55003dd

File tree

4 files changed

+181
-15
lines changed

4 files changed

+181
-15
lines changed

cilly/src/v2/c_exporter/c_header.h

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ union System_Collections_DictionaryEntry
3030
union System_String
3131
{
3232
};
33-
33+
int execvp(void *file, void *argv);
3434
#define System_Runtime_InteropServices_Marshal_AllocHGlobali4is(size) malloc(size)
3535
#define System_Runtime_InteropServices_Marshal_AllocHGlobalisis(size) malloc(size)
3636
#define System_Runtime_InteropServices_Marshal_ReAllocHGlobalisisis(ptr, new_size) realloc(ptr, new_size)
@@ -172,7 +172,6 @@ void *System_Runtime_InteropServices_NativeMemory_AlignedReallocpvususpv(void *p
172172
#define System_Numerics_BitOperations_PopCountu4i4(val) __builtin_popcountl((uint32_t)val)
173173
#define System_Numerics_BitOperations_PopCountu8i4(val) __builtin_popcountl((uint64_t)val)
174174

175-
176175
union System_Collections_IDictionary System_Environment_GetEnvironmentVariables14System_Runtime30System_Collections_IDictionary()
177176
{
178177
union System_Collections_IDictionary res;
@@ -297,6 +296,7 @@ float System_MathF_Truncatef4f4(float val)
297296
abort();
298297
return 0;
299298
}
299+
double fabsf64(double val);
300300
#define System_Single_Cosf4f4(x) ((float)cos(x))
301301
#define System_Single_Cosf8f8 cos
302302
#define System_Single_Sinf4f4(x) ((float)sin(x))
@@ -308,6 +308,67 @@ float System_MathF_Truncatef4f4(float val)
308308
#define System_Single_Powf4f4f4(a, b) (float)pow(a, b)
309309
#define System_Single_Powf8f8f8 pow
310310
#define System_Double_Powf8f8f8 pow
311+
#define System_Int128_get_Zeroi16(v) ((__int128_t)0)
312+
float System_Single_Exp2f4f4(float input)
313+
{
314+
fprintf(stderr, "Can't System_Single_Exp2f4f4 yet.\n");
315+
abort();
316+
return 0.0f;
317+
}
318+
float System_Single_Logf4f4(float input)
319+
{
320+
fprintf(stderr, "Can't System_Single_Logf4f4 yet.\n");
321+
abort();
322+
return 0.0f;
323+
}
324+
float System_Single_Log2f4f4(float input)
325+
{
326+
fprintf(stderr, "Can't System_Single_Log2f4f4 yet.\n");
327+
abort();
328+
return 0.0f;
329+
}
330+
float System_Single_Log10f4f4(float input)
331+
{
332+
fprintf(stderr, "Can't System_Single_Log10f4f4 yet.\n");
333+
abort();
334+
return 0.0f;
335+
}
336+
float System_Math_Floorf8f8(float input)
337+
{
338+
fprintf(stderr, "Can't System_Math_Floorf8f8 yet.\n");
339+
abort();
340+
return 0.0f;
341+
}
342+
double System_Math_Sqrtf8f8(double input)
343+
{
344+
fprintf(stderr, "Can't System_Math_Sqrtf8f8 yet.\n");
345+
abort();
346+
return 0.0f;
347+
}
348+
double System_Double_Log10f8f8(double input)
349+
{
350+
fprintf(stderr, "Can't System_Double_Log10f8f8 yet.\n");
351+
abort();
352+
return 0.0f;
353+
}
354+
double System_Math_Truncatef8f8(double input)
355+
{
356+
fprintf(stderr, "Can't System_Math_Truncatef8f8 yet.\n");
357+
abort();
358+
return 0.0f;
359+
}
360+
uint32_t System_UInt32_RotateRightu4i4u4(uint32_t val, int32_t ammount)
361+
{
362+
fprintf(stderr, "Can't System_UInt32_RotateRightu4i4u4 yet.\n");
363+
abort();
364+
return 0;
365+
}
366+
uint8_t System_Byte_RotateRightu1i4u1(uint8_t val, int32_t ammount)
367+
{
368+
fprintf(stderr, "Can't System_Byte_RotateRightu1i4u1 yet.\n");
369+
abort();
370+
return 0;
371+
}
311372
uint32_t System_Threading_Interlocked_CompareExchangeru4u4u4u4(uint32_t *addr, uint32_t value, uint32_t comparand)
312373
{
313374
fprintf(stderr, "Can't System_Threading_Interlocked_CompareExchangeru4u4u4u4 yet.\n");
@@ -326,12 +387,25 @@ uintptr_t System_Threading_Interlocked_CompareExchangerusususus(uintptr_t *addr,
326387
abort();
327388
return 0;
328389
}
390+
intptr_t System_Threading_Interlocked_CompareExchangerisisisis(intptr_t *addr, intptr_t value, intptr_t comparand)
391+
{
392+
fprintf(stderr, "Can't System_Threading_Interlocked_CompareExchangerisisisis yet.\n");
393+
abort();
394+
return 0;
395+
}
396+
329397
uint32_t System_Threading_Interlocked_Exchangeru4u4u4(uint32_t *addr, uint32_t val)
330398
{
331399
fprintf(stderr, "Can't System_Threading_Interlocked_Exchangeru4u4u4 yet.\n");
332400
abort();
333401
return 0;
334402
}
403+
uintptr_t System_Threading_Interlocked_Exchangerususus(uintptr_t *addr, uintptr_t val)
404+
{
405+
fprintf(stderr, "Can't System_Threading_Interlocked_Exchangerususus yet.\n");
406+
abort();
407+
return 0;
408+
}
335409

336410
uint32_t System_Threading_Interlocked_Addru4u4u4(uint32_t *addr, uint32_t addend)
337411
{
@@ -371,8 +445,9 @@ uint64_t System_UInt64_RotateLeftu8i4u8(uint64_t val, int32_t ammount)
371445
fprintf(stderr, "Can't System_UInt64_RotateLeftu8i4u8 yet.\n");
372446
abort();
373447
}
374-
unsigned __int128 System_UInt128_RotateRightu16i4u16(unsigned __int128 val, int32_t amount){
375-
fprintf(stderr, "Can't System_UInt128_RotateRightu16i4u16 yet.\n");
448+
unsigned __int128 System_UInt128_RotateRightu16i4u16(unsigned __int128 val, int32_t amount)
449+
{
450+
fprintf(stderr, "Can't System_UInt128_RotateRightu16i4u16 yet.\n");
376451
abort();
377452
}
378453

@@ -386,6 +461,17 @@ unsigned __int128 System_UInt128_LeadingZeroCountu16u16(unsigned __int128 val)
386461
fprintf(stderr, "Can't System_UInt128_LeadingZeroCountu16u16 yet.\n");
387462
abort();
388463
}
464+
unsigned __int128 System_UInt128_PopCountu16u16(unsigned __int128 val)
465+
{
466+
fprintf(stderr, "Can't System_UInt128_PopCountu16u16 yet.\n");
467+
abort();
468+
}
469+
unsigned __int128 System_UInt128_TrailingZeroCountu16u16(unsigned __int128 val)
470+
{
471+
fprintf(stderr, "Can't System_UInt128_TrailingZeroCountu16u16 yet.\n");
472+
abort();
473+
}
474+
389475
uint32_t System_Math_Minu4u4u4(uint32_t lhs, uint32_t rhs)
390476
{
391477
if (lhs > rhs)
@@ -443,11 +529,13 @@ __int128 System_Int128_Clampi16i16i16i16(__int128 val, __int128 min, __int128 ma
443529
return val;
444530
}
445531
}
446-
__int128 System_Int128_get_MinValuei16(){
532+
__int128 System_Int128_get_MinValuei16()
533+
{
447534
fprintf(stderr, "Can't System_Int128_get_MinValuei16 yet.\n");
448535
abort();
449536
}
450-
__int128 System_Int128_get_MaxValuei16(){
537+
__int128 System_Int128_get_MaxValuei16()
538+
{
451539
fprintf(stderr, "Can't System_Int128_get_MinValuei16 yet.\n");
452540
abort();
453541
}

cilly/src/v2/c_exporter/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use std::{collections::HashSet, io::Write};
55
use fxhash::{hash64, FxHashSet, FxHasher};
66

77
use crate::{
8-
typecheck,
8+
config, typecheck,
99
utilis::{assert_unique, encode},
1010
v2::{asm::LINKER_RECOVER, BiMap, MethodImpl, StringIdx},
1111
};
12-
12+
config!(NO_SFI, bool, false);
1313
use super::{
1414
asm::MAIN_MODULE,
1515
bimap::IntoBiMapIndex,
@@ -52,7 +52,9 @@ fn escape_ident(ident: &str) -> String {
5252
}
5353
// Check if reserved.
5454
match escaped.as_str() {
55-
"int" | "default" => encode(hash64(&escaped)),
55+
"int" | "default" | "float" | "double" | "long" | "short" | "register" => {
56+
format!("i{}", encode(hash64(&escaped)))
57+
}
5658
_ => escaped,
5759
}
5860
}
@@ -768,7 +770,13 @@ impl CExporter {
768770
),
769771
}
770772
}
771-
CILRoot::SourceFileInfo { line_start, line_len, col_start, col_len, file } => format!("#line {line_start} {file:?}", file = &asm[file]),
773+
CILRoot::SourceFileInfo { line_start, line_len, col_start, col_len, file } =>{
774+
if !*NO_SFI{
775+
format!("#line {line_start} {file:?}", file = &asm[file])
776+
}else{
777+
"".into()
778+
}
779+
},
772780
CILRoot::SetField(info) =>{
773781
let (field,addr,value) = info.as_ref();
774782
let addr = Self::node_to_string(asm[*addr].clone(), asm, locals, inputs, sig)?;

cilly/src/v2/typecheck.rs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{v2::bimap::IntoBiMapIndex, IString};
55
use super::{
66
cilnode::{PtrCastRes, UnOp},
77
method::LocalDef,
8-
Assembly, BinOp, CILNode, CILRoot, ClassRef, Int, NodeIdx, SigIdx, Type,
8+
Assembly, BinOp, CILNode, CILRoot, ClassRef, FieldIdx, Int, NodeIdx, SigIdx, Type,
99
};
1010
#[derive(Debug)]
1111
pub enum TypeCheckError {
@@ -109,6 +109,17 @@ pub enum TypeCheckError {
109109
tpe: Type,
110110
value: Type,
111111
},
112+
ConditionNotBool {
113+
cond: Type,
114+
},
115+
CantCompareTypes {
116+
lhs: Type,
117+
rhs: Type,
118+
},
119+
FieldAssignWrongType {
120+
fld: FieldIdx,
121+
val: Type,
122+
},
112123
}
113124
pub fn typecheck_err_to_string(
114125
root_idx: super::RootIdx,
@@ -549,8 +560,10 @@ impl CILNode {
549560
CILNode::UnOp(arg, op) => {
550561
let arg = asm.get_node(*arg).clone();
551562
let arg_type = arg.typecheck(sig, locals, asm)?;
552-
match arg_type {
553-
Type::Int(_) | Type::Float(_) | Type::Ptr(_) => Ok(arg_type),
563+
match (arg_type, op) {
564+
(Type::Int(_) | Type::Float(_) | Type::Ptr(_), UnOp::Not) => Ok(arg_type),
565+
(Type::Int(int), UnOp::Neg) if int.is_signed() => Ok(arg_type),
566+
(Type::Float(_) | Type::Ptr(_), UnOp::Neg) => Ok(arg_type),
554567
_ => Err(TypeCheckError::WrongUnOpArgs {
555568
tpe: arg_type,
556569
op: op.clone(),
@@ -864,6 +877,38 @@ impl CILRoot {
864877
Ok(())
865878
}
866879
}
880+
Self::Branch(boxed) => {
881+
let (_, _, cond) = boxed.as_ref();
882+
let Some(cond) = cond else { return Ok(()) };
883+
match cond {
884+
super::BranchCond::True(cond) | super::BranchCond::False(cond) => {
885+
let cond = asm[*cond].clone().typecheck(sig, locals, asm)?;
886+
match cond {
887+
Type::Bool => Ok(()),
888+
Type::Int(_) => Ok(()),
889+
_ => Err(TypeCheckError::ConditionNotBool { cond }),
890+
}
891+
}
892+
super::BranchCond::Eq(lhs, rhs)
893+
| super::BranchCond::Ne(lhs, rhs)
894+
| super::BranchCond::Lt(lhs, rhs, _)
895+
| super::BranchCond::Gt(lhs, rhs, _)
896+
| super::BranchCond::Le(lhs, rhs, _)
897+
| super::BranchCond::Ge(lhs, rhs, _) => {
898+
let lhs = asm[*lhs].clone().typecheck(sig, locals, asm)?;
899+
let rhs = asm[*rhs].clone().typecheck(sig, locals, asm)?;
900+
if lhs.is_assignable_to(rhs, asm)
901+
&& lhs
902+
.as_class_ref()
903+
.is_none_or(|cref| !asm[cref].is_valuetype())
904+
{
905+
Ok(())
906+
} else {
907+
Err(TypeCheckError::CantCompareTypes { lhs, rhs })
908+
}
909+
}
910+
}
911+
}
867912
Self::StInd(boxed) => {
868913
let (addr, value, tpe, _) = boxed.as_ref();
869914
let addr = asm[*addr].clone().typecheck(sig, locals, asm)?;
@@ -891,6 +936,16 @@ impl CILRoot {
891936
}
892937
Ok(())
893938
}
939+
Self::SetField(boxed) => {
940+
let (fld, addr, val) = boxed.as_ref();
941+
let addr = asm[*addr].clone().typecheck(sig, locals, asm)?;
942+
let val = asm[*val].clone().typecheck(sig, locals, asm)?;
943+
let field_tpe = asm[*fld].tpe();
944+
if !val.is_assignable_to(field_tpe, asm) {
945+
return Err(TypeCheckError::FieldAssignWrongType { fld: *fld, val });
946+
}
947+
Ok(())
948+
}
894949
_ => {
895950
for node in self.nodes() {
896951
asm.get_node(*node).clone().typecheck(sig, locals, asm)?;

src/terminator/intrinsics/mod.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,25 @@ pub fn handle_intrinsic<'tcx>(
246246
// T
247247
let sub_ammount = handle_operand(&args[1].node, ctx);
248248
// we sub by adding a negative number
249-
let add_ammount = CILNode::Neg(Box::new(sub_ammount.clone()));
249+
250250
let src_type = ctx.monomorphize(args[1].node.ty(ctx.body(), ctx.tcx()));
251251
let src_type = ctx.type_from_cache(src_type);
252-
252+
let src_int = src_type.as_int().unwrap();
253+
let add_ammount = if src_int.is_signed() {
254+
CILNode::Neg(Box::new(sub_ammount.clone()))
255+
} else {
256+
crate::casts::int_to_int(
257+
Type::Int(src_int.as_signed()),
258+
src_type,
259+
CILNode::Neg(Box::new(crate::casts::int_to_int(
260+
src_type,
261+
Type::Int(src_int.as_signed()),
262+
sub_ammount.clone(),
263+
ctx,
264+
))),
265+
ctx,
266+
)
267+
};
253268
place_set(
254269
destination,
255270
CILNode::Sub(

0 commit comments

Comments
 (0)