-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6c079f
commit 06b7b17
Showing
92 changed files
with
91 additions
and
112 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,77 @@ | ||
use crate::Compiler; | ||
use gccjit_sys::*; | ||
use utils::compiler_error; | ||
use utils::compile_time_errors::errors::*; | ||
use std::{ffi::CString, ptr::null_mut}; | ||
|
||
pub fn builtin_func__sizeof(context: *mut gcc_jit_context) -> *mut gcc_jit_function { | ||
let func_name = CString::new("__cyrus_builtin__sizeof").unwrap(); | ||
let rvalue_param_name = CString::new("rvalue").unwrap(); | ||
let size_t_type = Compiler::size_t_type(context); | ||
|
||
// Change the parameter type to the specific type you want to get the size of | ||
let int_type = unsafe { gcc_jit_context_get_int_type(context, 0, 1) }; // Example: int type | ||
let int_ptr_type = unsafe { gcc_jit_type_get_pointer(int_type) }; | ||
pub fn builtin_func__len( | ||
file_path: String, | ||
context: *mut gcc_jit_context, | ||
args: Vec<*mut gcc_jit_rvalue>, | ||
) -> *mut gcc_jit_rvalue { | ||
if args.len() != 1 { | ||
compiler_error!(format!("Expected 1 argument but got {}", args.len()), file_path); | ||
} | ||
let rvalue = args[0].clone(); | ||
let rvalue_type = unsafe { gcc_jit_rvalue_get_type(rvalue) }; | ||
|
||
let size_t_type = Compiler::size_t_type(context); | ||
let void_ptr_type = Compiler::void_ptr_type(context); | ||
let wrapper_func_name = CString::new("__cyrus_builtin_wrapped_func__len").unwrap(); | ||
// TODO Check param type to be array or string | ||
let rvalue_param_name = CString::new("rvalue").unwrap(); | ||
let rvalue_param = | ||
unsafe { gcc_jit_context_new_param(context, null_mut(), int_ptr_type, rvalue_param_name.as_ptr()) }; | ||
|
||
let func_ptr = unsafe { | ||
unsafe { gcc_jit_context_new_param(context, null_mut(), void_ptr_type, rvalue_param_name.as_ptr()) }; | ||
let wrapper_func = unsafe { | ||
gcc_jit_context_new_function( | ||
context, | ||
null_mut(), | ||
gcc_jit_function_kind::GCC_JIT_FUNCTION_INTERNAL, | ||
size_t_type, | ||
func_name.as_ptr(), | ||
wrapper_func_name.as_ptr(), | ||
1, | ||
[rvalue_param].as_mut_ptr(), | ||
0, | ||
) | ||
}; | ||
let block_name = CString::new("entry").unwrap(); | ||
let block = unsafe { gcc_jit_function_new_block(func_ptr, block_name.as_ptr()) }; | ||
|
||
// Get the type of the parameter directly | ||
let rvalue_type = unsafe { gcc_jit_rvalue_get_type(gcc_jit_param_as_rvalue(rvalue_param)) }; | ||
let rvalue_size = unsafe { gcc_jit_context_new_sizeof(context, rvalue_type) }; | ||
|
||
unsafe { gcc_jit_block_end_with_return(block, null_mut(), rvalue_size) }; | ||
|
||
return func_ptr; | ||
} | ||
|
||
// pub fn builtin_func__sizeof(context: *mut gcc_jit_context) -> *mut gcc_jit_function { | ||
// let func_name = CString::new("__cyrus_builtin__sizeof").unwrap(); | ||
// let rvalue_param_name = CString::new("rvalue").unwrap(); | ||
// let size_t_type = Compiler::size_t_type(context); | ||
// let void_ptr_type = Compiler::void_ptr_type(context); | ||
let block_name = CString::new("entry").unwrap(); | ||
let block = unsafe { gcc_jit_function_new_block(wrapper_func, block_name.as_ptr()) }; | ||
|
||
// let rvalue_param = | ||
// unsafe { gcc_jit_context_new_param(context, null_mut(), void_ptr_type, rvalue_param_name.as_ptr()) }; | ||
let sizeof_array = unsafe { gcc_jit_context_new_sizeof(context, rvalue_type) }; | ||
let first_item_of_array = unsafe { | ||
gcc_jit_context_new_array_access( | ||
context, | ||
null_mut(), | ||
rvalue, | ||
gcc_jit_context_new_rvalue_from_int(context, Compiler::i32_type(context), 0), | ||
) | ||
}; | ||
|
||
// let func_ptr = unsafe { | ||
// gcc_jit_context_new_function( | ||
// context, | ||
// null_mut(), | ||
// gcc_jit_function_kind::GCC_JIT_FUNCTION_INTERNAL, | ||
// size_t_type, | ||
// func_name.as_ptr(), | ||
// 1, | ||
// [rvalue_param].as_mut_ptr(), | ||
// 0, | ||
// ) | ||
// }; | ||
// let block_name = CString::new("entry").unwrap(); | ||
// let block = unsafe { gcc_jit_function_new_block(func_ptr, block_name.as_ptr()) }; | ||
let len_result = unsafe { | ||
gcc_jit_context_new_binary_op( | ||
context, | ||
null_mut(), | ||
gcc_jit_binary_op::GCC_JIT_BINARY_OP_DIVIDE, | ||
size_t_type, | ||
sizeof_array, | ||
unsafe { | ||
gcc_jit_lvalue_as_rvalue(first_item_of_array) | ||
}, | ||
) | ||
}; | ||
|
||
// // rvalue requires to be dereferenced before getting size | ||
// let mut rvalue = unsafe { gcc_jit_param_as_rvalue(rvalue_param) }; | ||
// rvalue = unsafe { gcc_jit_lvalue_as_rvalue(gcc_jit_rvalue_dereference(rvalue, null_mut())) }; | ||
unsafe { gcc_jit_block_end_with_return(block, null_mut(), len_result) }; | ||
|
||
// let rvalue_type = unsafe { gcc_jit_rvalue_get_type(rvalue) }; | ||
// let rvalue_size = unsafe { gcc_jit_context_new_sizeof(context, rvalue_type) }; | ||
return len_result; | ||
} | ||
|
||
// unsafe { gcc_jit_block_end_with_return(block, null_mut(), rvalue_size) }; | ||
pub fn builtin_func__sizeof(file_path: String, | ||
context: *mut gcc_jit_context, | ||
args: Vec<*mut gcc_jit_rvalue>,) -> *mut gcc_jit_rvalue { | ||
let rvalue = args.first().unwrap(); | ||
let rvalue_type = unsafe { gcc_jit_rvalue_get_type(*rvalue) }; | ||
let rvalue_size = unsafe { gcc_jit_context_new_sizeof(context, rvalue_type) }; | ||
|
||
// return func_ptr; | ||
// } | ||
return rvalue_size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,28 @@ | ||
use std::{cell::RefCell, collections::HashMap, rc::Rc}; | ||
use gccjit_sys::{gcc_jit_context, gcc_jit_rvalue}; | ||
use std::{collections::HashMap, sync::LazyLock}; | ||
|
||
use ast::{ | ||
ast::{FunctionParam, FunctionParams, Identifier, VisType}, | ||
token::{Location, Span, TokenKind}, | ||
}; | ||
use gccjit_sys::gcc_jit_context; | ||
use super::funcs::builtin_func__len; | ||
use crate::builtins::funcs::builtin_func__sizeof; | ||
|
||
use super::funcs::builtin_func__sizeof; | ||
use crate::{Compiler, funcs::FuncMetadata}; | ||
pub type BuiltinFuncDef = | ||
fn(file_path: String, context: *mut gcc_jit_context, args: Vec<*mut gcc_jit_rvalue>) -> *mut gcc_jit_rvalue; | ||
|
||
pub fn builtins_loader(func_table: Rc<RefCell<HashMap<String, FuncMetadata>>>, context: *mut gcc_jit_context) { | ||
let mut func_table = func_table.borrow_mut(); | ||
pub type BuiltinFuncsTable = HashMap<String, BuiltinFuncDef>; | ||
|
||
// if func_table | ||
// .keys() | ||
// .find(|&key| key.clone() == String::from("sizeof")) | ||
// .is_none() | ||
// { | ||
// dbg!("no exist"); | ||
// } | ||
|
||
// func_table.insert( | ||
// String::from("sizeof"), | ||
// FuncMetadata { | ||
// func_type: VisType::Internal, | ||
// ptr: builtin_func__sizeof(context), | ||
// return_type: TokenKind::SizeT, | ||
// params: FunctionParams { | ||
// list: vec![FunctionParam { | ||
// identifier: Identifier { | ||
// name: String::from("rvalue"), | ||
// span: Span::default(), | ||
// loc: Location::default(), | ||
// }, | ||
// ty: Some(TokenKind::Dereference(Box::new(TokenKind::Void))), | ||
// default_value: None, | ||
// span: Span::default(), | ||
// loc: Location::default(), | ||
// }], | ||
// is_variadic: false, | ||
// }, | ||
// imported_from: None, | ||
// }, | ||
// ); | ||
#[macro_export] | ||
macro_rules! build_builtin_funcs { | ||
($( $key:expr => $val:expr ),*) => { | ||
{ | ||
let mut map: BuiltinFuncsTable = HashMap::new(); | ||
$(map.insert($key.to_string(), $val);)* | ||
map | ||
} | ||
}; | ||
} | ||
|
||
pub static BUILTIN_FUNCS: LazyLock<BuiltinFuncsTable> = LazyLock::new(|| { | ||
build_builtin_funcs! { | ||
"len" => builtin_func__len, | ||
"sizeof" => builtin_func__sizeof | ||
} | ||
}); |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
import std::io; | ||
|
||
pub fn main() { | ||
#a = sizeof(10.0 as double); | ||
std::io::printf("result: %d\n", a); | ||
} | ||
#arr: i32[3] = [1, 2, 3]; | ||
#item = arr[2]; |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.