-
Notifications
You must be signed in to change notification settings - Fork 17
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
388 if should make local variable scope #474
388 if should make local variable scope #474
Conversation
The
This assertion is not correct now because
So I thought this should fix it but I got another error. I'll investigate it further. diff --git a/lib/skc_ast2hir/src/ctx_stack.rs b/lib/skc_ast2hir/src/ctx_stack.rs
index 7dcaab2..da1ed2f 100644
--- a/lib/skc_ast2hir/src/ctx_stack.rs
+++ b/lib/skc_ast2hir/src/ctx_stack.rs
@@ -402,8 +402,10 @@ impl<'hir_maker> Iterator for LVarIter<'hir_maker> {
self.cur -= 1;
Some(scope)
}
- // ::new() never sets `While` to .cur
- HirMakerContext::While(_) => panic!("must not happen"),
+ HirMakerContext::While(_) => {
+ self.cur -= 1;
+ self.next()
+ }
}
}
} |
Maybe the WhileCtx should also contains a field pub lvars: HashMap<String, CtxLVar>, I try in local with those change and the following example seems to work
|
Also I don't really understand what is the use of the lvar ? Sorry if it's a dummy question |
Oh that's right - I made a ticket for that. I got an error for this example. Maybe we need to push a
|
The error that you get seems to append in the codegen stage And it seems that it's because in gen_exprs.rs with the example above, Only the toplevel lvars are in ctx, so in the exemple above (x). And it looks like the lvars in ctx are set once in skc_codegen/src/lib.rs But I am not sure sure whether or not what I said is true. It's just my impression |
Yes. And local variables in a method are allocated here.
Let me consider which would be better. (sorry this issue is not as easy as I thought!) |
No worries no worries, and at least it makes me explore the code deeper than just the ast2hir module I think I will also try to implement one of your solution |
At first a) looked easier to implement:
So I'd recommend b).
|
Thanks for your indication, I will try implement that |
I think the |
Good job! I've confirmed all tests in As for CI, |
_set_default is used when a method argument has a default value. For example,
In this case
No new lvars will be created in this |
lib/skc_codegen/src/lambda.rs
Outdated
@@ -325,6 +326,7 @@ impl<'hir: 'ictx, 'run, 'ictx: 'run> CodeGen<'hir, 'run, 'ictx> { | |||
cond_expr, | |||
then_exprs, | |||
else_exprs, | |||
lvars: _, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[FYI] you can use ..
when you don't need the value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I would have needed to use the lvars in this match but I forget to use ..
I will fix that, Thank you
Indeed, I shouldn't have pushed and poped I will fix that too, Thank you |
… .. instead of <variable>: _ in match
examples/ray.ll generates much smaller output than expected. I'll investigate why. |
It seems that examples/ray.out crashes on allocating memory at certain point (that's why the output is small.) |
I reviewed myself, the pending review, do you think it maybe what caused the issues ? |
Ok I've found what is changed. Before this PR, nine variables are alloca'ed here: define %Void* @Plane_intersect(%Plane* %self, %Ray* %ray, %Isect* %isect) {
br label %alloca
alloca: ; preds = %0
%d = alloca %Float*, align 8
%m = alloca %Float*, align 8
%abs = alloca %Float*, align 8
%v = alloca %Float*, align 8
%t = alloca %Float*, align 8
%d2 = alloca %Float*, align 8
%f = alloca %Float*, align 8
%n = alloca %Float*, align 8
%d3 = alloca %Float*, align 8
br label %alloca_End With this PR, only three of them are alloca'ed define %Void* @Plane_intersect(%Plane* %self, %Ray* %ray, %Isect* %isect) {
br label %alloca
alloca: ; preds = %0
%v = alloca %Float*, align 8
%t = alloca %Float*, align 8
%d = alloca %Float*, align 8
br label %alloca_End and the rest are alloca'ed just before ...
"Invoke_Float#/_end": ; preds = %"Invoke_Float#/"
store %Float* %result69, %Float** %t, align 8
%m = alloca %Float*, align 8
%n = alloca %Float*, align 8
%abs = alloca %Float*, align 8
%d2 = alloca %Float*, align 8
%f = alloca %Float*, align 8
%d3 = alloca %Float*, align 8
br label %IfBegin Since |
Sorry for the late reply, I'm pretty busy right now but as soon as I have the time I will try to fix the allocation |
I think that the allocation should work but I not quite sure about my implementation choice Since now . if x == 1
puts "10"
else
let y = 1
p x + y
end if we add let mut lvars = extract_lvars(&mut toplevel_ctx.lvars);
let mut lvars_collected = extract_lvars(&mut toplevel_ctx.lvars_collected);
lvars.append(&mut lvars_collected);
// Use lvars |
Hmm, another possible approach is to collect lvars in
|
I traversed the body and it seems to work but I have 1 question |
Also if what I have done for the |
Congrats for CI pass! 🎉
That's right, you can skip traversing HirLambdaExpr.
Yes, please! |
I think there is a new issue now. In this example # e.sk
while true
let y = 10
p y
end
let y = "Hello world"
puts y the compilation will fail since the llvm will see 2 variables with the same name but 2 differents types |
the command
|
Oh, there are two options to fix this:
In either case, let's create a new github issue for this. |
Ok, sound good to be So excepted that, I think the lvars in But I wonder in rust, if a function return function();
// or
let () = function(); |
I've merged this PR. Thank you for your contribution!
Usually you don't need to create a variable for function(); // warning In this case you could define a variable to intentionally discard the error: let _ = function(); but in most cases you'll just use function()?; |
Thanks to you too for answering my questions It was nice to contribute to shiika and if I'm able to, I will try contribute to other issues on shiika |
#388
If(IfCtx)
in lib/skc_ast2hir/src/hir_maker_context.rs