Skip to content

Commit

Permalink
working on builtin funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam committed Mar 2, 2025
1 parent a6c079f commit 06b7b17
Show file tree
Hide file tree
Showing 92 changed files with 91 additions and 112 deletions.
Empty file modified .github/workflows/build.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified Cargo.lock
100644 → 100755
Empty file.
Empty file modified Cargo.toml
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified Makefile
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified ast/Cargo.toml
100644 → 100755
Empty file.
Empty file modified ast/src/ast.rs
100644 → 100755
Empty file.
Empty file modified ast/src/format.rs
100644 → 100755
Empty file.
Empty file modified ast/src/lib.rs
100644 → 100755
Empty file.
Empty file modified ast/src/tests.rs
100644 → 100755
Empty file.
Empty file modified ast/src/token.rs
100644 → 100755
Empty file.
Empty file modified cli.rs
100644 → 100755
Empty file.
Empty file modified compiler/Cargo.toml
100644 → 100755
Empty file.
Empty file modified compiler/src/blocks.rs
100644 → 100755
Empty file.
106 changes: 54 additions & 52 deletions compiler/src/builtins/funcs.rs
100644 → 100755
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;
}
64 changes: 23 additions & 41 deletions compiler/src/builtins/loader.rs
100644 → 100755
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 modified compiler/src/builtins/mod.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/context.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/control_flow.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/expressions.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/funcs.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/import.rs
100644 → 100755
Empty file.
6 changes: 1 addition & 5 deletions compiler/src/lib.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ast::{
ast::*,
token::{Location, Span, TokenKind},
};
use builtins::{funcs::builtin_func__sizeof, loader::builtins_loader};
use control_flow::LoopBlockPair;
use funcs::{FuncMetadata, FuncParamsRecords};
use gccjit_sys::*;
Expand Down Expand Up @@ -88,13 +87,10 @@ impl Compiler {

unsafe { gcc_jit_context_set_bool_allow_unreachable_blocks(context, 1) };

let func_table = Rc::new(RefCell::new(HashMap::new()));
builtins_loader(Rc::clone(&func_table), context);

Self {
program,
context,
func_table: func_table.deref().clone(),
func_table: RefCell::new(HashMap::new()),
global_struct_table: RefCell::new(HashMap::new()),
global_vars_table: RefCell::new(HashMap::new()),
param_table: RefCell::new(HashMap::new()),
Expand Down
Empty file modified compiler/src/location.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/options.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/output.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/scope.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/stdlib.rs
100644 → 100755
Empty file.
17 changes: 9 additions & 8 deletions compiler/src/structs.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ffi::CString;
use std::ptr::null_mut;
use std::rc::Rc;

use crate::builtins::loader::BUILTIN_FUNCS;
use crate::Compiler;
use crate::scope::ScopeRef;
use ast::ast::*;
Expand Down Expand Up @@ -106,14 +107,14 @@ impl Compiler {
scope: ScopeRef,
chains: Vec<FieldAccessOrMethodCall>,
) -> *mut gcc_jit_rvalue {
// Check for builtin funcs
let first_method_call = chains.first().unwrap().clone().method_call.unwrap().clone();
if first_method_call.func_name.identifier.name == "sizeof" {
let expr = first_method_call.arguments[0].clone();
let rvalue = self.compile_expression(Rc::clone(&scope), expr);
let rvalue_type = unsafe { gcc_jit_rvalue_get_type(rvalue) };
let size = unsafe { gcc_jit_context_new_sizeof(self.context, rvalue_type ) };
return size;
// If first chain item is method call we need to check that's a builtin func or not
if let Some(method_call) = chains.first().unwrap().method_call.clone() {
let func_name = method_call.func_name.identifier.name.clone();
if let Some(func_def) = BUILTIN_FUNCS.get(&func_name) {
let args =self.compile_func_arguments(Rc::clone(&scope), None, method_call.arguments);
let rvalue = func_def(self.file_path.clone(), self.context, args);
return rvalue;
}
}

let rvalue: *mut gcc_jit_rvalue = {
Expand Down
Empty file modified compiler/src/types.rs
100644 → 100755
Empty file.
Empty file modified compiler/src/variables.rs
100644 → 100755
Empty file.
Empty file modified devel.Dockerfile
100644 → 100755
Empty file.
Empty file modified docker-compose.yml
100644 → 100755
Empty file.
Empty file modified examples/arrays.cyr
100644 → 100755
Empty file.
Empty file modified examples/arrays2.cyr
100644 → 100755
Empty file.
Empty file modified examples/branches.cyr
100644 → 100755
Empty file.
Empty file modified examples/cyrus_ascii_logo.cyr
100644 → 100755
Empty file.
Empty file modified examples/decimal_to_hex.cyr
100644 → 100755
Empty file.
Empty file modified examples/display_square_of_numbers.cyr
100644 → 100755
Empty file.
Empty file modified examples/exporting_structs.cyr
100644 → 100755
Empty file.
Empty file modified examples/final_expectation.cyr
100644 → 100755
Empty file.
Empty file modified examples/for_loop1.cyr
100644 → 100755
Empty file.
Empty file modified examples/format_string.cyr
100644 → 100755
Empty file.
8 changes: 2 additions & 6 deletions examples/main.cyr
100644 → 100755
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 modified examples/nested_branches.cyr
100644 → 100755
Empty file.
Empty file modified examples/package_importing/dir1/module1.cyr
100644 → 100755
Empty file.
Empty file modified examples/package_importing/main.cyr
100644 → 100755
Empty file.
Empty file modified examples/pointers.cyr
100644 → 100755
Empty file.
Empty file modified examples/sample.cyr
100644 → 100755
Empty file.
Empty file modified examples/structs.cyr
100644 → 100755
Empty file.
Empty file modified examples/structs2.cyr
100644 → 100755
Empty file.
Empty file modified examples/sum_int32.cyr
100644 → 100755
Empty file.
Empty file modified examples/unconditional_for_loop1.cyr
100644 → 100755
Empty file.
Empty file modified examples/unconditional_for_loop2.cyr
100644 → 100755
Empty file.
Empty file modified examples/variable_scope.cyr
100644 → 100755
Empty file.
Empty file modified flake.lock
100644 → 100755
Empty file.
Empty file modified flake.nix
100644 → 100755
Empty file.
Empty file modified lexer/Cargo.toml
100644 → 100755
Empty file.
Empty file modified lexer/src/format.rs
100644 → 100755
Empty file.
Empty file modified lexer/src/lexer_cli.rs
100644 → 100755
Empty file.
Empty file modified lexer/src/lexer_test.rs
100644 → 100755
Empty file.
Empty file modified lexer/src/lib.rs
100644 → 100755
Empty file.
Empty file modified lexer/src/mod.rs
100644 → 100755
Empty file.
Empty file modified parser/Cargo.toml
100644 → 100755
Empty file.
Empty file modified parser/src/cli.rs
100644 → 100755
Empty file.
Empty file modified parser/src/common.rs
100644 → 100755
Empty file.
Empty file modified parser/src/debug_tools.rs
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions parser/src/expressions.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ impl<'a> Parser<'a> {
pub fn parse_array_index(&mut self, from_package: FromPackage) -> Result<ArrayIndex, ParseError> {
let start = self.current_token.span.start;

dbg!(self.current_token.kind.clone());

let mut dimensions: Vec<Expression> = Vec::new();

while self.current_token_is(TokenKind::LeftBracket) {
Expand Down
Empty file modified parser/src/lib.rs
100644 → 100755
Empty file.
Empty file modified parser/src/mod.rs
100644 → 100755
Empty file.
Empty file modified parser/src/precedences.rs
100644 → 100755
Empty file.
Empty file modified parser/src/statements.rs
100644 → 100755
Empty file.
Empty file modified parser/src/tests.rs
100644 → 100755
Empty file.
Empty file modified rustfmt.toml
100644 → 100755
Empty file.
Empty file modified scripts/install.ps1
100644 → 100755
Empty file.
Empty file modified scripts/install.sh
100644 → 100755
Empty file.
Empty file modified stdlib/io.cyr
100644 → 100755
Empty file.
Empty file modified stdlib/math.cyr
100644 → 100755
Empty file.
Empty file modified stdlib/mem.cyr
100644 → 100755
Empty file.
Empty file modified utils/Cargo.toml
100644 → 100755
Empty file.
Empty file modified utils/src/compile_time_errors/errors.rs
100644 → 100755
Empty file.
Empty file modified utils/src/compile_time_errors/lexer_errors.rs
100644 → 100755
Empty file.
Empty file modified utils/src/compile_time_errors/mod.rs
100644 → 100755
Empty file.
Empty file modified utils/src/compile_time_errors/parser_errors.rs
100644 → 100755
Empty file.
Empty file modified utils/src/compile_time_errors/tests.rs
100644 → 100755
Empty file.
Empty file modified utils/src/debugging_logs.rs
100644 → 100755
Empty file.
Empty file modified utils/src/fs.rs
100644 → 100755
Empty file.
Empty file modified utils/src/generate_random_hex.rs
100644 → 100755
Empty file.
Empty file modified utils/src/lib.rs
100644 → 100755
Empty file.
Empty file modified utils/src/purify_string.rs
100644 → 100755
Empty file.

0 comments on commit 06b7b17

Please sign in to comment.