🎨 type convert support same type

This commit is contained in:
ValKmjolnir 2024-10-25 19:52:43 +08:00
parent c082f63afd
commit 0e572d134a
7 changed files with 63 additions and 9 deletions

View File

@ -6,7 +6,7 @@
- [bootstrap](./bootstrap/README.md) : available now, features not stable these days, but welcome to try.
- [src](./src/README.md) : bootstrapped compiler, not available now (lexer and parser is ready).
- [test](./test): test cases
- [test](./test): test cases.
## Bootstrap Compiler

View File

@ -293,12 +293,6 @@ type semantic::resolve_type_convert(type_convert* node) {
if (res.is_error() || type_res.is_error()) {
return type::error_type();
}
if (res == type_res) {
rp.report(node->get_target(),
"unnecessary type cast between \"" +
res.to_string() + "\" and \"" + type_res.to_string() + "\"."
);
}
// convert floating point number to pointer is unsafe at all
if ((res.is_float() && type_res.is_pointer()) ||

View File

@ -343,8 +343,17 @@ void sir_type_convert::dump(std::ostream& out) const {
// because like i64 & u64 share the same llvm type `i64`
if (real_src_type==real_dst_type &&
(real_src_type[0]=='i' || real_src_type[0]=='u')) {
out << destination << " = add " << real_src_type << " ";
out << source << ", 0";
out << destination << " = bitcast " << real_src_type << " ";
out << source << " to " << real_dst_type;
out << " ; " << src_type << " -> " << dst_type << "\n";
return;
}
// f32 f64 also
if (real_src_type==real_dst_type && real_src_type[0]=='f') {
out << destination << " = bitcast ";
out << (real_src_type == "f32"? "float":"double") << " ";
out << source << " to ";
out << (real_dst_type == "f32"? "float":"double");
out << " ; " << src_type << " -> " << dst_type << "\n";
return;
}

View File

@ -10,6 +10,8 @@ pub extern func write(fd: i32, buf: i8*, count: i64) -> i64;
pub extern func close(fd: i32) -> i32;
pub extern func exit(code: i32);
pub extern func puts(str: i8*) -> i32;
pub extern func strlen(src: i8*) -> i64;
pub extern func strcmp(left: i8*, right: i8*) -> i32;

26
src/std/ptr.colgm Normal file
View File

@ -0,0 +1,26 @@
use std::libc::{ malloc, free };
pub struct safe_ptr<T> {
ptr: T*,
freed: bool
}
impl safe_ptr<T> {
func create(size: u64) -> safe_ptr<T> {
return safe_ptr {
ptr: malloc(T::__size__() * size) => T*,
freed: false
};
}
func destroy(self) {
if (self->ptr => i8* == nil) {
self->freed = true;
return;
}
if (!self.freed) {
free(self.ptr => i8*);
self.freed = true;
}
}
}

View File

@ -1,4 +1,5 @@
use std::libc::{ malloc, realloc, free, memcpy, strlen, streq, exit, write };
use std::ptr::{ safe_ptr };
pub struct str {
c_str: i8*,

View File

@ -1,3 +1,5 @@
use libc::{ puts };
func main() -> i32 {
var a: i8 = 0 => i8;
var b: i16 = 0 => i16;
@ -14,23 +16,27 @@ func main() -> i32 {
a => u16;
a => u32;
a => u64;
a => i8;
a => i16;
a => i32;
a => i64;
a => f32;
a => f64;
a => i8*;
puts("[type_convert] a convert passed");
b => u8;
b => u16;
b => u32;
b => u64;
b => i8;
b => i16;
b => i32;
b => i64;
b => f32;
b => f64;
b => i8*;
puts("[type_convert] b convert passed");
c => u8;
c => u16;
@ -38,10 +44,12 @@ func main() -> i32 {
c => u64;
c => i8;
c => i16;
c => i32;
c => i64;
c => f32;
c => f64;
c => i8*;
puts("[type_convert] c convert passed");
d => u8;
d => u16;
@ -50,10 +58,13 @@ func main() -> i32 {
d => i8;
d => i16;
d => i32;
d => i64;
d => f32;
d => f64;
d => i8*;
puts("[type_convert] d convert passed");
e => u8;
e => u16;
e => u32;
e => u64;
@ -64,8 +75,10 @@ func main() -> i32 {
e => f32;
e => f64;
e => i8*;
puts("[type_convert] e convert passed");
f => u8;
f => u16;
f => u32;
f => u64;
f => i8;
@ -75,9 +88,11 @@ func main() -> i32 {
f => f32;
f => f64;
f => i8*;
puts("[type_convert] f convert passed");
g => u8;
g => u16;
g => u32;
g => u64;
g => i8;
g => i16;
@ -86,10 +101,12 @@ func main() -> i32 {
g => f32;
g => f64;
g => i8*;
puts("[type_convert] g convert passed");
h => u8;
h => u16;
h => u32;
h => u64;
h => i8;
h => i16;
h => i32;
@ -97,6 +114,7 @@ func main() -> i32 {
h => f32;
h => f64;
h => i8*;
puts("[type_convert] h convert passed");
i => u8;
i => u16;
@ -106,7 +124,9 @@ func main() -> i32 {
i => i16;
i => i32;
i => i64;
i => f32;
i => f64;
puts("[type_convert] i convert passed");
j => u8;
j => u16;
@ -117,6 +137,8 @@ func main() -> i32 {
j => i32;
j => i64;
j => f32;
j => f64;
puts("[type_convert] j convert passed");
return 0 => i32;
}