Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repr rust #35

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ members = [
"fir",
"oir",
"scir",
"datatable",
"datatable", "repr",
]
resolver = "2"

2 changes: 2 additions & 0 deletions bootstrap/makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.ty/token.o:
ty obj src/token.ty

.PHONY: clean
clean:
rm -rf .ty
mkdir .ty
6 changes: 6 additions & 0 deletions bootstrap/repr/integration.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdint.h>

uint64_t add(uint64_t left, uint64_t right) {
return left + right;
}

4 changes: 4 additions & 0 deletions bootstrap/repr/integration.ty
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const add = fn(x: u64, y: u64) u64 {
return x + y
}

10 changes: 10 additions & 0 deletions bootstrap/repr/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdint.h>

extern uint64_t add(uint64_t, uint64_t);

int main() {
printf("Hello");
printf("Hello %lu", add(2, 7));
puts("Hello");
}
25 changes: 25 additions & 0 deletions bootstrap/repr/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
all: .ty/main.o .ty/integrationc.o .ty/integration.o .ty/repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o
cd .ty && objdump -D integrationc.o > integrationc.o.txt
cd .ty && objdump -D integration.o > integrationty.o.txt
cd .ty && objdump -D repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o > integrationrs.o.txt
touch .ty/all

.ty/main.o: main.c
cc -c main.c -o .ty/main.o -O3

.ty/integrationc.o: integration.c
cc -c integration.c -o .ty/integrationc.o -O3

.ty/integration.o: integration.ty
ty obj integration.ty

.ty/repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o: ../../target/release/librepr.rlib
cd .ty && ar x ../../../target/release/librepr.rlib

../../target/release/librepr.rlib: ../../repr/src/lib.rs
cd ../.. && cargo build -p repr --release

.PHONY: clean
clean:
rm -rf .ty
mkdir .ty
38 changes: 32 additions & 6 deletions fir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,27 @@ impl Fir {
type_tables: &Vec<TypeTable>,
oir: &mut Oir,
) -> Function {
let mut sig = Signature::new(CallConv::SystemV);
let sig = Signature::new(CallConv::Cold);
let name = UserFuncName::user(namespace, index);
// todo:: types need to be worked out, params and returns defined
func_def
.args
.iter()
.for_each(|_x| sig.params.push(AbiParam::new(I64)));
sig.returns.push(AbiParam::new(I64));
let mut func = Function::with_name_signature(name, sig);
let mut builder = FunctionBuilder::new(&mut func, ctx);
let root_block = builder.create_block();
func_def.args.iter().for_each(|x| {
let z = self
.recurse(
x.as_ref().as_ref(),
&mut builder,
dtbl,
scopes,
type_tables,
oir,
)
.unwrap();
builder.func.signature.params.push(AbiParam::new(I64));
//let res = builder.block_params(root_block)[z.as_u32() as usize];
});
builder.func.signature.returns.push(AbiParam::new(I64));
builder.append_block_params_for_function_params(root_block);
builder.switch_to_block(root_block);
let _result = self.recurse(
Expand All @@ -70,6 +80,19 @@ impl Fir {
builder.finalize();
func
}
pub fn handle_arg_init(
&mut self,
op: &SymbolInit,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
type_tables: &Vec<TypeTable>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
self.sym.table.insert(op.ident.clone(), result.as_u32());
Ok(result)
}
pub fn handle_const_init(
&mut self,
op: &Initialization,
Expand Down Expand Up @@ -265,6 +288,9 @@ impl Fir {
TypeTree::ConstInit(op) => {
self.handle_const_init(&op, builder, dtbl, scopes, type_tables, oir)
}
TypeTree::ArgInit(op) => {
self.handle_arg_init(&op, builder, dtbl, scopes, type_tables, oir)
}
TypeTree::SymbolAccess(op) => {
self.handle_sym_access(&op, dtbl, scopes, type_tables, oir, builder)
}
Expand Down
16 changes: 15 additions & 1 deletion linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,21 @@ mod tests {
);
}
#[test]
fn it_should_handle_basic_types() {
fn it_should_handle_func_args() {
const TEST_STR: &'static str = "const add = fn(x: u64, y: u64) u64 { return x + y }
";
let lexer = TLexer::new(TEST_STR);
let mut parser = Parser::new(lexer);
let result = parser.all();
let mut tts = vec![];
let mut scps = vec![];
let mut linter = LintSource::new(TEST_STR, &mut scps, &mut tts);
let _ = linter.lint_check(&result.unwrap());

assert!(linter.issues.len() == 0);
}
#[test]
fn it_should_handle_global_data() {
const TEST_STR: &'static str = "const val: usize = 2
const main = fn() void { return 7 + val }
";
Expand Down
6 changes: 6 additions & 0 deletions repr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "repr"
version = "0.1.0"
edition = "2021"

[dependencies]
15 changes: 15 additions & 0 deletions repr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[no_mangle]
pub extern "C" fn add(left: u64, right: u64) -> u64 {
left + right
}

#[no_mangle]
pub fn make_binop(thing: u64, small: char, big: u64) -> BinOp {
return BinOp { thing, small, big };
}

pub struct BinOp {
thing: u64,
small: char,
big: u64,
}
Loading