Skip to content

Commit c14e9b6

Browse files
committed
feat: added hash in IRFunction & changed to pass IRFunction instead of raw IRLocalContext
1 parent e1c4c0e commit c14e9b6

5 files changed

Lines changed: 39 additions & 38 deletions

File tree

ir/src/conv/control.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn parse_if_statement_ir(func: &mut IRFunction, ctx: &IRContext, node: Box<A
3030

3131
ir_branches.push(ctx.inkwell_ctx.append_basic_block(func.inkwell_func, "out"));
3232

33-
let first_cond = parse_ir_value(Some(&func.lctx), ctx, cond, None, false)?;
33+
let first_cond = parse_ir_value(Some(&func), ctx, cond, None, false)?;
3434

3535
let bool_type = ctx.type_storage.get(BOOL_TYPE_HASH).unwrap();
3636

@@ -55,7 +55,7 @@ pub fn parse_if_statement_ir(func: &mut IRFunction, ctx: &IRContext, node: Box<A
5555
ASTTreeNode::IfElseStatement { cond, body } => {
5656
ctx.builder.position_at_end(ir_branches[ind]);
5757

58-
let cond_val = parse_ir_value(Some(&func.lctx), ctx, cond.unwrap(), None, false)?;
58+
let cond_val = parse_ir_value(Some(&func), ctx, cond.unwrap(), None, false)?;
5959

6060
let int_cond_val = match cond_val.obtain(ctx)?.obtain_as_bool() {
6161
Some(v) => *v,
@@ -121,7 +121,7 @@ pub fn parse_for_statement_ir(func: &mut IRFunction, ctx: &IRContext, node: Box<
121121

122122
let bool_type = ctx.type_storage.get(BOOL_TYPE_HASH).expect("Boolean type wasn't found!");
123123

124-
let cond_val = parse_ir_value(Some(&func.lctx), ctx, cond, None, false)?;
124+
let cond_val = parse_ir_value(Some(&func), ctx, cond, None, false)?;
125125
let cond_int = cond_val.obtain(ctx)?.obtain_as_bool().expect("Cannot cast condition result as int");
126126

127127
ctx.builder.build_conditional_branch(*cond_int, for_body_block, post_block);

ir/src/conv/func.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ pub fn parse_ir_shadow_function_decl(ctx: &mut IRContext, node: Box<ASTTreeNode>
2323
arguments.push((t, k.name.hash));
2424
}
2525

26-
let func = IRFunction::create_shadow(ctx, func_name.val.clone(), &ctx.module, return_type, arguments)?;
26+
let func = IRFunction::create_shadow(ctx, func_name.val.clone(), func_name.hash, &ctx.module, return_type, arguments)?;
2727

2828
ctx.add_function(func_name.hash, func)?;
2929

30-
return Ok(ctx.get_funtion(func_name.hash)?);
30+
return Ok(ctx.get_function(func_name.hash)?);
3131
}
3232

3333
return Err(PositionlessError::new("Cannot parse ir shadow funtion decl as the node is incompatible!"));
@@ -51,7 +51,7 @@ pub fn parse_ir_function_decl(ctx: &mut IRContext, node: Box<ASTTreeNode>) -> Po
5151
arguments.push((t, k.name.hash));
5252
}
5353

54-
let mut func = IRFunction::create(ctx, func_name.val, &ctx.module, return_type, arguments)?;
54+
let mut func = IRFunction::create(ctx, func_name.val,func_name.hash, &ctx.module, return_type, arguments)?;
5555

5656
let mut ind = 0;
5757
for argument in &func.args {
@@ -73,7 +73,7 @@ pub fn parse_ir_function_decl(ctx: &mut IRContext, node: Box<ASTTreeNode>) -> Po
7373

7474
ctx.add_function(func_name.hash, func)?;
7575

76-
return ctx.get_funtion(func_name.hash);
76+
return ctx.get_function(func_name.hash);
7777
}
7878

7979
return Err(PositionlessError::new("Given node in parse_ir_function_decl wasn't a function decl!"));
@@ -91,7 +91,7 @@ pub fn parse_ir_body(ctx: &IRContext, func: &mut IRFunction, nodes: Vec<Box<ASTT
9191
return Ok(true);
9292
}
9393

94-
pub fn parse_ir_function_call(ctx: &IRContext, lctx: &IRLocalContext, node: Box<ASTTreeNode>, owner: Option<IRPointer>, grab_result: bool) -> PositionlessResult<Option<IRValueRef>> {
94+
pub fn parse_ir_function_call(ctx: &IRContext, f: &IRFunction, node: Box<ASTTreeNode>, owner: Option<IRPointer>, grab_result: bool) -> PositionlessResult<Option<IRValueRef>> {
9595
if let ASTTreeNode::FunctionCall { func, args } = *node {
9696
let mut arguments = vec![];
9797

@@ -100,10 +100,10 @@ pub fn parse_ir_function_call(ctx: &IRContext, lctx: &IRLocalContext, node: Box<
100100
}
101101

102102
for v in args {
103-
arguments.push(parse_ir_value(Some(lctx), ctx, v, None, false)?);
103+
arguments.push(parse_ir_value(Some(&f), ctx, v, None, false)?);
104104
}
105105

106-
let func = ctx.get_funtion(func.hash)?;
106+
let func = ctx.get_function(func.hash)?;
107107

108108
let ret = func.call(ctx, arguments, grab_result)?;
109109

@@ -128,7 +128,7 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
128128
println!("Var name: {}", var_name.val.clone());
129129

130130
let initial = if let Some(v) = value {
131-
Some(parse_ir_value(Some(&func.lctx), ctx, v, None, true)?)
131+
Some(parse_ir_value(Some(&func), ctx, v, None, true)?)
132132
} else {
133133
None
134134
};
@@ -143,19 +143,19 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
143143
},
144144

145145
ASTTreeNode::StructLRFunction { .. } => {
146-
parse_ir_value(Some(&func.lctx), ctx, node, None, false)?;
146+
parse_ir_value(Some(&func), ctx, node, None, false)?;
147147

148148
return Ok(true)
149149
},
150150

151151
ASTTreeNode::StructLRVariable { .. } => {
152-
parse_ir_value(Some(&func.lctx), ctx, node, None, false)?;
152+
parse_ir_value(Some(&func), ctx, node, None, false)?;
153153

154154
return Ok(true)
155155
},
156156

157157
ASTTreeNode::FunctionCall { .. } => {
158-
parse_ir_function_call(ctx, &func.lctx, node, None, false)?;
158+
parse_ir_function_call(ctx, &func, node, None, false)?;
159159

160160
return Ok(true)
161161
},
@@ -167,7 +167,7 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
167167
return Ok(true);
168168
}
169169

170-
let val = parse_ir_value(Some(&func.lctx), ctx, val.unwrap(), None, true)?;
170+
let val = parse_ir_value(Some(&func), ctx, val.unwrap(), None, true)?;
171171

172172
ctx.builder.build_return(Some(&val.obtain(ctx)?.obtain().inner));
173173

@@ -187,7 +187,7 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
187187
return Err(PositionlessError::new("Cannot use a math expression in IR body if it is not assignments!"))
188188
}
189189

190-
parse_ir_value(Some(&func.lctx), ctx, node, None, false)?;
190+
parse_ir_value(Some(&func), ctx, node, None, false)?;
191191
return Ok(true);
192192
}
193193

ir/src/conv/val.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn get_variable_ref(lctx: &IRLocalContext, ctx: &IRContext, hash: u64) -> Po
2525
}
2626
}
2727

28-
pub fn parse_ir_value<'a>(lctx: Option<&IRLocalContext>, ctx: &IRContext, node: Box<ASTTreeNode>, left: Option<IRPointer>, in_var: bool) -> PositionlessResult<IRValueRef> {
28+
pub fn parse_ir_value<'a>(f: Option<&IRFunction>, ctx: &IRContext, node: Box<ASTTreeNode>, left: Option<IRPointer>, in_var: bool) -> PositionlessResult<IRValueRef> {
2929
match node.as_ref() {
3030
ASTTreeNode::IntegerLit { val: v, hash} => {
3131
let t = ctx.type_storage.get(*hash);
@@ -67,18 +67,18 @@ pub fn parse_ir_value<'a>(lctx: Option<&IRLocalContext>, ctx: &IRContext, node:
6767
return Ok(IRValueRef::from_pointer(ptr));
6868
}
6969

70-
let var = get_variable_ref(&lctx.unwrap(), ctx, e.hash)?;
70+
let var = get_variable_ref(&f.unwrap().lctx, ctx, e.hash)?;
7171

7272
return Ok(var);
7373
},
7474

7575
ASTTreeNode::FunctionCall { func, args } => {
7676

77-
if lctx.is_none() {
77+
if f.is_none() {
7878
return Err(PositionlessError::new("Cannot use function calls outside of a function!"))
7979
}
8080

81-
let k = parse_ir_function_call(ctx, lctx.unwrap(), node, left, in_var)?;
81+
let k = parse_ir_function_call(ctx, f.unwrap(), node, left, in_var)?;
8282

8383
if k.is_none() {
8484
return Err(PositionlessError::new("Function call returns void! cannot use as a value!"));
@@ -88,8 +88,8 @@ pub fn parse_ir_value<'a>(lctx: Option<&IRLocalContext>, ctx: &IRContext, node:
8888
},
8989

9090
ASTTreeNode::MathResult { lval, rval, operator, assigns } => {
91-
let left = parse_ir_value(lctx, ctx, lval.clone(), None, in_var)?;
92-
let right = parse_ir_value(lctx, ctx, rval.clone(), None, in_var)?;
91+
let left = parse_ir_value(f, ctx, lval.clone(), None, in_var)?;
92+
let right = parse_ir_value(f, ctx, rval.clone(), None, in_var)?;
9393

9494
let t = left.get_type();
9595

@@ -118,16 +118,16 @@ pub fn parse_ir_value<'a>(lctx: Option<&IRLocalContext>, ctx: &IRContext, node:
118118
},
119119

120120
ASTTreeNode::OperatorBasedConditionMember { lval, rval, operator } => {
121-
let l_val = parse_ir_value(lctx, ctx, lval.clone(), None, in_var)?;
122-
let r_val = parse_ir_value(lctx, ctx, rval.clone(), None, in_var)?;
121+
let l_val = parse_ir_value(f, ctx, lval.clone(), None, in_var)?;
122+
let r_val = parse_ir_value(f, ctx, rval.clone(), None, in_var)?;
123123

124124
let cmp = make_bool_cmp_int(ctx, l_val, r_val, operator.clone())?;
125125

126126
return Ok(IRValueRef::from_val(cmp));
127127
},
128128

129129
ASTTreeNode::BooleanBasedConditionMember { val, negate } => {
130-
let v = parse_ir_value(lctx, ctx, val.clone(), None, in_var)?;
130+
let v = parse_ir_value(f, ctx, val.clone(), None, in_var)?;
131131

132132
if *negate {
133133
return Ok(IRValueRef::from_val(make_bool_xor(ctx, v)?))
@@ -137,17 +137,17 @@ pub fn parse_ir_value<'a>(lctx: Option<&IRLocalContext>, ctx: &IRContext, node:
137137
}
138138

139139
ASTTreeNode::StructLRFunction { l, r } => {
140-
let l_val = parse_ir_value(lctx, ctx, l.clone(), None, in_var)?;
140+
let l_val = parse_ir_value(f, ctx, l.clone(), None, in_var)?;
141141
let l_ptr = l_val.as_pointer()?;
142142

143-
return parse_ir_value(lctx, ctx, r.clone(), Some(l_ptr), in_var);
143+
return parse_ir_value(f, ctx, r.clone(), Some(l_ptr), in_var);
144144
},
145145

146146
ASTTreeNode::StructLRVariable { l, r } => {
147-
let l_val = parse_ir_value(lctx, ctx, l.clone(), None, in_var)?;
147+
let l_val = parse_ir_value(f, ctx, l.clone(), None, in_var)?;
148148
let l_ptr = l_val.as_pointer()?;
149149

150-
return parse_ir_value(lctx, ctx, r.clone(), Some(l_ptr), in_var);
150+
return parse_ir_value(f, ctx, r.clone(), Some(l_ptr), in_var);
151151
}
152152

153153
_ => return Err(PositionlessError::new("The given node cannot be parsed as a value!"))

ir/src/ctx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl IRContext {
6666
return self.functions.get(&SelfHash { hash }).is_some() || self.static_vars.get(&SelfHash {hash}).is_some() || self.type_storage.get(hash).is_some();
6767
}
6868

69-
pub fn get_funtion(&self, hash: u64) -> PositionlessResult<Rc<IRFunction>> {
69+
pub fn get_function(&self, hash: u64) -> PositionlessResult<Rc<IRFunction>> {
7070
return match self.functions.get(&SelfHash { hash }) {
7171
Some(v) => Ok(v.clone()),
7272
None => Err(PositionlessError::new(&format!("Invalid function name! Got hash {}", hash)))

ir/src/irstruct/funcs.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@ pub struct IRFunction {
1313
pub ret_type: Option<Rc<IRType>>,
1414
pub args: Vec<(Rc<IRType>, u64)>,
1515
name: String,
16+
pub hash: u64,
1617

1718
pub lctx: IRLocalContext,
1819

1920
entry: Option<BasicBlock<'static>>
2021
}
2122

2223
impl IRFunction {
23-
pub fn new(ctx: &IRContext, name: String, func: FunctionValue, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> Self {
24+
pub fn new(ctx: &IRContext, name: String, hash: u64, func: FunctionValue, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> Self {
2425

2526
let block = ctx.inkwell_ctx.append_basic_block(func, "entry");
2627

27-
return IRFunction { owned: ctx.inkwell_ctx.clone(), inkwell_func: unsafe { transmute(func)}, ret_type, args, name, entry: Some(unsafe { transmute(block) }), lctx: IRLocalContext::new().into() }
28+
return IRFunction { owned: ctx.inkwell_ctx.clone(), inkwell_func: unsafe { transmute(func)}, ret_type, args, name, hash, entry: Some(unsafe { transmute(block) }), lctx: IRLocalContext::new().into() }
2829
}
2930

30-
pub fn new_shadow(ctx: &IRContext, name: String, func: FunctionValue, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> Self {
31-
return IRFunction { owned: ctx.inkwell_ctx.clone(), inkwell_func: unsafe { transmute(func)}, ret_type, args, name, entry: None, lctx: IRLocalContext::new().into() }
31+
pub fn new_shadow(ctx: &IRContext, name: String, hash: u64, func: FunctionValue, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> Self {
32+
return IRFunction { owned: ctx.inkwell_ctx.clone(), inkwell_func: unsafe { transmute(func)}, ret_type, args, name, hash, entry: None, lctx: IRLocalContext::new().into() }
3233
}
3334

34-
pub fn create_shadow(ctx: &IRContext, name: String, module: &Module, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> PositionlessResult<Self> {
35+
pub fn create_shadow(ctx: &IRContext, name: String, hash: u64, module: &Module, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> PositionlessResult<Self> {
3536
let mut kargs = vec![];
3637

3738
for k in &args {
@@ -45,10 +46,10 @@ impl IRFunction {
4546

4647
let func = module.add_function(&name, t, None);
4748

48-
return Ok(IRFunction::new_shadow(ctx, name, func, ret_type, args));
49+
return Ok(IRFunction::new_shadow(ctx, name, hash, func, ret_type, args));
4950
}
5051

51-
pub fn create(ctx: &IRContext, name: String, module: &Module, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> PositionlessResult<Self> {
52+
pub fn create(ctx: &IRContext, name: String, hash: u64, module: &Module, ret_type: Option<Rc<IRType>>, args: Vec<(Rc<IRType>, u64)>) -> PositionlessResult<Self> {
5253
let mut kargs = vec![];
5354

5455
for k in &args {
@@ -62,7 +63,7 @@ impl IRFunction {
6263

6364
let func = module.add_function(&name, t, None);
6465

65-
return Ok(IRFunction::new(ctx, name, func, ret_type, args));
66+
return Ok(IRFunction::new(ctx, name, hash, func, ret_type, args));
6667
}
6768

6869
pub fn call(&self, ctx: &IRContext, args: Vec<IRValueRef>, grab_return: bool) -> PositionlessResult<Option<IRPointer>> {

0 commit comments

Comments
 (0)