From 130be45de162c32617ea27fe2cf1219df6836027 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 26 Dec 2024 22:19:00 -0600 Subject: [PATCH 1/3] playing around with representations in rust --- Cargo.toml | 2 +- bootstrap/makefile | 16 ++++++++++++++-- bootstrap/src/add.ty | 3 +++ bootstrap/src/main.c | 9 +++++++++ bootstrap/src/test.c | 6 ++++++ bootstrap/src/test.o | Bin 0 -> 1240 bytes repr/Cargo.toml | 6 ++++++ repr/src/lib.rs | 16 ++++++++++++++++ 8 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 bootstrap/src/add.ty create mode 100644 bootstrap/src/main.c create mode 100644 bootstrap/src/test.c create mode 100644 bootstrap/src/test.o create mode 100644 repr/Cargo.toml create mode 100644 repr/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 1196dee..0abaee5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ members = [ "fir", "oir", "scir", - "datatable", + "datatable", "repr", ] resolver = "2" diff --git a/bootstrap/makefile b/bootstrap/makefile index 17e328d..0661bb5 100644 --- a/bootstrap/makefile +++ b/bootstrap/makefile @@ -1,5 +1,17 @@ -.ty/token.o: - ty obj src/token.ty +#.ty/token.o: +# ty obj src/token.ty +../target/release/librepr.rlib: ../repr/src/lib.rs + cd .. && cargo build -p repr --release + cd .ty && ar x ../../target/release/librepr.rlib + +.ty/test: .ty/main.o + cc .ty/main.o .ty/repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o -o .ty/test + +.ty/main.o: src/main.c + cc -c src/main.c -o .ty/main.o + +.PHONY: clean clean: rm -rf .ty + mkdir .ty diff --git a/bootstrap/src/add.ty b/bootstrap/src/add.ty new file mode 100644 index 0000000..51fa238 --- /dev/null +++ b/bootstrap/src/add.ty @@ -0,0 +1,3 @@ +pub const add = fn(x: u64, y: u64) u64 { + return x + y +} diff --git a/bootstrap/src/main.c b/bootstrap/src/main.c new file mode 100644 index 0000000..ad1002b --- /dev/null +++ b/bootstrap/src/main.c @@ -0,0 +1,9 @@ +#include +#include + +extern uint64_t add(uint64_t, uint64_t); + +int main() { + printf("Hello"); + printf("Hello %lu", add(2, 7)); +} diff --git a/bootstrap/src/test.c b/bootstrap/src/test.c new file mode 100644 index 0000000..f56d4aa --- /dev/null +++ b/bootstrap/src/test.c @@ -0,0 +1,6 @@ +#include + +uint64_t add(uint64_t left, uint64_t right) { + return left + right; +} + diff --git a/bootstrap/src/test.o b/bootstrap/src/test.o new file mode 100644 index 0000000000000000000000000000000000000000..15ea44f568947df516e103e70077b566eb784d5e GIT binary patch literal 1240 zcmbVLy-EW?5T1)M{zZ*qAw(rsD(I2~1+kDoICF(qhyfeXOL8Wl`QdI4qM&v{u*swN z2!bzQ=?e&!5u0G6Gnd`CEU|H5c4oerZ)R_I@6O!c3mFDTF<=*dBZ&gMDqtpnGZ2Ld z_?%w)%3G~Bt98@0T7JiR$ak#=vtt=AC(n?}W_J>+`GQ~Lej=4(n=HAJ@`EsSn@+Rj z=9+-zq%b-$)M3=zK^{T?PlOGjlFaoC7}tkjG-F0QIx^bEy)7V4O0r0cITgDPWhWy? zSU~X~LZ20ekMBY0p#djBy=E0Wm_=mnc$^i%E|r2-iC_iVf1n}+VBS@g+Xb}T?b(X- zxYOjoN;bEF6+91Ev0kk@HN8$_DUq>$WOrokH=Tlx4 zIbYf>OiEv^YpH?a>$N84Mo{VgRcVVLp@Qh^ZDK1sF$5cck;FuYPw#vBdl+1&Ro`kIbMqh~E%}<$h() G(fL1)FI9yA literal 0 HcmV?d00001 diff --git a/repr/Cargo.toml b/repr/Cargo.toml new file mode 100644 index 0000000..62df674 --- /dev/null +++ b/repr/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "repr" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/repr/src/lib.rs b/repr/src/lib.rs new file mode 100644 index 0000000..63fdae6 --- /dev/null +++ b/repr/src/lib.rs @@ -0,0 +1,16 @@ +#[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 }; +} + +#[repr(C)] +pub struct BinOp { + thing: u64, + small: char, + big: u64, +} From 9019bb54d99994adf17d4cbfa9c61fc5254a1446 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 27 Dec 2024 13:28:31 -0600 Subject: [PATCH 2/3] playing around with representations in rust --- bootstrap/makefile | 4 ++-- bootstrap/src/main.c | 1 + bootstrap/src/test.o | Bin 1240 -> 0 bytes 3 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 bootstrap/src/test.o diff --git a/bootstrap/makefile b/bootstrap/makefile index 0661bb5..4a83d5e 100644 --- a/bootstrap/makefile +++ b/bootstrap/makefile @@ -1,5 +1,5 @@ -#.ty/token.o: -# ty obj src/token.ty +.ty/token.o: + ty obj src/token.ty ../target/release/librepr.rlib: ../repr/src/lib.rs cd .. && cargo build -p repr --release diff --git a/bootstrap/src/main.c b/bootstrap/src/main.c index ad1002b..8736cd7 100644 --- a/bootstrap/src/main.c +++ b/bootstrap/src/main.c @@ -6,4 +6,5 @@ extern uint64_t add(uint64_t, uint64_t); int main() { printf("Hello"); printf("Hello %lu", add(2, 7)); + puts("Hello"); } diff --git a/bootstrap/src/test.o b/bootstrap/src/test.o deleted file mode 100644 index 15ea44f568947df516e103e70077b566eb784d5e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1240 zcmbVLy-EW?5T1)M{zZ*qAw(rsD(I2~1+kDoICF(qhyfeXOL8Wl`QdI4qM&v{u*swN z2!bzQ=?e&!5u0G6Gnd`CEU|H5c4oerZ)R_I@6O!c3mFDTF<=*dBZ&gMDqtpnGZ2Ld z_?%w)%3G~Bt98@0T7JiR$ak#=vtt=AC(n?}W_J>+`GQ~Lej=4(n=HAJ@`EsSn@+Rj z=9+-zq%b-$)M3=zK^{T?PlOGjlFaoC7}tkjG-F0QIx^bEy)7V4O0r0cITgDPWhWy? zSU~X~LZ20ekMBY0p#djBy=E0Wm_=mnc$^i%E|r2-iC_iVf1n}+VBS@g+Xb}T?b(X- zxYOjoN;bEF6+91Ev0kk@HN8$_DUq>$WOrokH=Tlx4 zIbYf>OiEv^YpH?a>$N84Mo{VgRcVVLp@Qh^ZDK1sF$5cck;FuYPw#vBdl+1&Ro`kIbMqh~E%}<$h() G(fL1)FI9yA From f14a099aee6db65c2e996cefa1abae1891fb1ea2 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 28 Dec 2024 10:55:06 -0600 Subject: [PATCH 3/3] added all the pieces for repr rust comparisons --- bootstrap/makefile | 10 ----- bootstrap/{src/test.c => repr/integration.c} | 0 bootstrap/{src/add.ty => repr/integration.ty} | 1 + bootstrap/{src => repr}/main.c | 0 bootstrap/repr/makefile | 25 ++++++++++++ fir/src/lib.rs | 38 ++++++++++++++++--- linter/src/lib.rs | 16 +++++++- repr/src/lib.rs | 1 - 8 files changed, 73 insertions(+), 18 deletions(-) rename bootstrap/{src/test.c => repr/integration.c} (100%) rename bootstrap/{src/add.ty => repr/integration.ty} (98%) rename bootstrap/{src => repr}/main.c (100%) create mode 100644 bootstrap/repr/makefile diff --git a/bootstrap/makefile b/bootstrap/makefile index 4a83d5e..0654872 100644 --- a/bootstrap/makefile +++ b/bootstrap/makefile @@ -1,16 +1,6 @@ .ty/token.o: ty obj src/token.ty -../target/release/librepr.rlib: ../repr/src/lib.rs - cd .. && cargo build -p repr --release - cd .ty && ar x ../../target/release/librepr.rlib - -.ty/test: .ty/main.o - cc .ty/main.o .ty/repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o -o .ty/test - -.ty/main.o: src/main.c - cc -c src/main.c -o .ty/main.o - .PHONY: clean clean: rm -rf .ty diff --git a/bootstrap/src/test.c b/bootstrap/repr/integration.c similarity index 100% rename from bootstrap/src/test.c rename to bootstrap/repr/integration.c diff --git a/bootstrap/src/add.ty b/bootstrap/repr/integration.ty similarity index 98% rename from bootstrap/src/add.ty rename to bootstrap/repr/integration.ty index 51fa238..2921c0c 100644 --- a/bootstrap/src/add.ty +++ b/bootstrap/repr/integration.ty @@ -1,3 +1,4 @@ pub const add = fn(x: u64, y: u64) u64 { return x + y } + diff --git a/bootstrap/src/main.c b/bootstrap/repr/main.c similarity index 100% rename from bootstrap/src/main.c rename to bootstrap/repr/main.c diff --git a/bootstrap/repr/makefile b/bootstrap/repr/makefile new file mode 100644 index 0000000..3ab941f --- /dev/null +++ b/bootstrap/repr/makefile @@ -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 diff --git a/fir/src/lib.rs b/fir/src/lib.rs index 36c7606..0ad10a5 100644 --- a/fir/src/lib.rs +++ b/fir/src/lib.rs @@ -45,17 +45,27 @@ impl Fir { type_tables: &Vec, 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( @@ -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, + type_tables: &Vec, + oir: &mut Oir, + ) -> ResultFir { + 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, @@ -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) } diff --git a/linter/src/lib.rs b/linter/src/lib.rs index c78a7b1..abfd644 100644 --- a/linter/src/lib.rs +++ b/linter/src/lib.rs @@ -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 } "; diff --git a/repr/src/lib.rs b/repr/src/lib.rs index 63fdae6..eeead7f 100644 --- a/repr/src/lib.rs +++ b/repr/src/lib.rs @@ -8,7 +8,6 @@ pub fn make_binop(thing: u64, small: char, big: u64) -> BinOp { return BinOp { thing, small, big }; } -#[repr(C)] pub struct BinOp { thing: u64, small: char,