Skip to content

Commit 9aa0b0b

Browse files
Repr rust (#35)
* added all the pieces for repr rust comparisons
1 parent d082b7a commit 9aa0b0b

File tree

10 files changed

+116
-8
lines changed

10 files changed

+116
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ members = [
2121
"fir",
2222
"oir",
2323
"scir",
24-
"datatable",
24+
"datatable", "repr",
2525
]
2626
resolver = "2"
2727

bootstrap/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.ty/token.o:
22
ty obj src/token.ty
33

4+
.PHONY: clean
45
clean:
56
rm -rf .ty
7+
mkdir .ty

bootstrap/repr/integration.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdint.h>
2+
3+
uint64_t add(uint64_t left, uint64_t right) {
4+
return left + right;
5+
}
6+

bootstrap/repr/integration.ty

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub const add = fn(x: u64, y: u64) u64 {
2+
return x + y
3+
}
4+

bootstrap/repr/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
4+
extern uint64_t add(uint64_t, uint64_t);
5+
6+
int main() {
7+
printf("Hello");
8+
printf("Hello %lu", add(2, 7));
9+
puts("Hello");
10+
}

bootstrap/repr/makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
all: .ty/main.o .ty/integrationc.o .ty/integration.o .ty/repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o
2+
cd .ty && objdump -D integrationc.o > integrationc.o.txt
3+
cd .ty && objdump -D integration.o > integrationty.o.txt
4+
cd .ty && objdump -D repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o > integrationrs.o.txt
5+
touch .ty/all
6+
7+
.ty/main.o: main.c
8+
cc -c main.c -o .ty/main.o -O3
9+
10+
.ty/integrationc.o: integration.c
11+
cc -c integration.c -o .ty/integrationc.o -O3
12+
13+
.ty/integration.o: integration.ty
14+
ty obj integration.ty
15+
16+
.ty/repr-37b621cda8f778ea.repr.c0ded12167d2f276-cgu.0.rcgu.o: ../../target/release/librepr.rlib
17+
cd .ty && ar x ../../../target/release/librepr.rlib
18+
19+
../../target/release/librepr.rlib: ../../repr/src/lib.rs
20+
cd ../.. && cargo build -p repr --release
21+
22+
.PHONY: clean
23+
clean:
24+
rm -rf .ty
25+
mkdir .ty

fir/src/lib.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,27 @@ impl Fir {
4545
type_tables: &Vec<TypeTable>,
4646
oir: &mut Oir,
4747
) -> Function {
48-
let mut sig = Signature::new(CallConv::SystemV);
48+
let sig = Signature::new(CallConv::Cold);
4949
let name = UserFuncName::user(namespace, index);
5050
// todo:: types need to be worked out, params and returns defined
51-
func_def
52-
.args
53-
.iter()
54-
.for_each(|_x| sig.params.push(AbiParam::new(I64)));
55-
sig.returns.push(AbiParam::new(I64));
5651
let mut func = Function::with_name_signature(name, sig);
5752
let mut builder = FunctionBuilder::new(&mut func, ctx);
5853
let root_block = builder.create_block();
54+
func_def.args.iter().for_each(|x| {
55+
let z = self
56+
.recurse(
57+
x.as_ref().as_ref(),
58+
&mut builder,
59+
dtbl,
60+
scopes,
61+
type_tables,
62+
oir,
63+
)
64+
.unwrap();
65+
builder.func.signature.params.push(AbiParam::new(I64));
66+
//let res = builder.block_params(root_block)[z.as_u32() as usize];
67+
});
68+
builder.func.signature.returns.push(AbiParam::new(I64));
5969
builder.append_block_params_for_function_params(root_block);
6070
builder.switch_to_block(root_block);
6171
let _result = self.recurse(
@@ -70,6 +80,19 @@ impl Fir {
7080
builder.finalize();
7181
func
7282
}
83+
pub fn handle_arg_init(
84+
&mut self,
85+
op: &SymbolInit,
86+
builder: &mut FunctionBuilder,
87+
dtbl: &DataTable,
88+
scopes: &Vec<ScopeTable>,
89+
type_tables: &Vec<TypeTable>,
90+
oir: &mut Oir,
91+
) -> ResultFir<Variable> {
92+
let result = self.add_var();
93+
self.sym.table.insert(op.ident.clone(), result.as_u32());
94+
Ok(result)
95+
}
7396
pub fn handle_const_init(
7497
&mut self,
7598
op: &Initialization,
@@ -265,6 +288,9 @@ impl Fir {
265288
TypeTree::ConstInit(op) => {
266289
self.handle_const_init(&op, builder, dtbl, scopes, type_tables, oir)
267290
}
291+
TypeTree::ArgInit(op) => {
292+
self.handle_arg_init(&op, builder, dtbl, scopes, type_tables, oir)
293+
}
268294
TypeTree::SymbolAccess(op) => {
269295
self.handle_sym_access(&op, dtbl, scopes, type_tables, oir, builder)
270296
}

linter/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,21 @@ mod tests {
12051205
);
12061206
}
12071207
#[test]
1208-
fn it_should_handle_basic_types() {
1208+
fn it_should_handle_func_args() {
1209+
const TEST_STR: &'static str = "const add = fn(x: u64, y: u64) u64 { return x + y }
1210+
";
1211+
let lexer = TLexer::new(TEST_STR);
1212+
let mut parser = Parser::new(lexer);
1213+
let result = parser.all();
1214+
let mut tts = vec![];
1215+
let mut scps = vec![];
1216+
let mut linter = LintSource::new(TEST_STR, &mut scps, &mut tts);
1217+
let _ = linter.lint_check(&result.unwrap());
1218+
1219+
assert!(linter.issues.len() == 0);
1220+
}
1221+
#[test]
1222+
fn it_should_handle_global_data() {
12091223
const TEST_STR: &'static str = "const val: usize = 2
12101224
const main = fn() void { return 7 + val }
12111225
";

repr/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "repr"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]

repr/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[no_mangle]
2+
pub extern "C" fn add(left: u64, right: u64) -> u64 {
3+
left + right
4+
}
5+
6+
#[no_mangle]
7+
pub fn make_binop(thing: u64, small: char, big: u64) -> BinOp {
8+
return BinOp { thing, small, big };
9+
}
10+
11+
pub struct BinOp {
12+
thing: u64,
13+
small: char,
14+
big: u64,
15+
}

0 commit comments

Comments
 (0)