-
Notifications
You must be signed in to change notification settings - Fork 7
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
Added support for pointer in LC by porting integration_tests/associate_02.f90
from LFortran
#83
Changes from all commits
2b42cbb
f1e3b4e
4b37bce
b3efc00
4447aa9
ee13ef9
8bee7cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
#include <complex> | ||
|
||
using namespace std::complex_literals; | ||
|
||
#define check(p, t, v) if( *p != v ) { \ | ||
exit(2); \ | ||
} \ | ||
if( t != v ) { \ | ||
exit(2); \ | ||
} \ | ||
|
||
int main() { | ||
|
||
int* p1; | ||
double* p2; | ||
int t1 = 2; | ||
double t2 = 2.0; | ||
std::complex<double>* p3; | ||
std::complex<double> t3 = 2.0 + 1i*3.0; | ||
|
||
p1 = &t1; | ||
p2 = &t2; | ||
p3 = &t3; | ||
*p1 = 1; | ||
*p2 = 4.0; | ||
|
||
std::cout << *p1 << std::endl; | ||
std::cout << t1 << std::endl; | ||
check(p1, t1, 1) | ||
|
||
t1 = *p2 + *p1; | ||
|
||
std::cout << *p1 << std::endl; | ||
std::cout << t1 << std::endl; | ||
check(p1, t1, 5); | ||
|
||
t1 = 8; | ||
|
||
std::cout << *p1 << std::endl; | ||
std::cout << t1 << std::endl; | ||
check(p1, t1, 8); | ||
|
||
*p3 = 2.0 * (*p3); | ||
std::cout << *p3 << std::endl; | ||
std::cout << t3 << std::endl; | ||
check(p3, t3, 4.0 + 1i*6.0); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,6 +311,8 @@ expr | |
| DictLen(expr arg, ttype type, expr? value) | ||
|
||
| Var(symbol v) | ||
| AddressOf(symbol v, ttype type) | ||
| DereferencePointer(symbol v, ttype type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we introduce a node like this one, it should be consistently used everywhere. I think it might simplify the LLVM backend with all the loads. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a compile time value is needed. Furthermore, isn't this the same as Var? Var currently automatically dereferences. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think so. Pointer is an address, available at runtime. So we can only know what's in the address when we run the code. Consider the following example, #include <iostream>
int main() {
int p = 10;
const int* ptr = &p;
p = 20;
std::cout << *ptr << std::endl;
return 0;
} (lc) 14:43:21:~/lc_project/lc % ./a.out
20 |
||
| FunctionParam(int param_number, ttype type, expr? value) --- used in types | ||
|
||
| ArrayConstant(expr* args, ttype type, arraystorage storage_format) | ||
|
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.
We have to be careful here.
This seems to be the same as CLoc (or GetPointer). These two nodes should be merged then.
Also we want to be able to add an optional debug time checking for dangling pointers. So we have to be careful how these nodes are designed, so that it is possible to add checking.
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.
AddressOf
can be called on anything including array items, any type of variable, functions. However,c_loc
can only be called on variables that too withtarget
orpointer
attribute. Also,AddressOf
as a separate node will be very helpful when implementing pointer arithmetic features. Merging nodes can be option when we implement pointer arithmetic features, until then I would suggest not to merge nodes.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 would not support pointer arithmetic, unless we absolutely have to. That's a feature that will be hard to check in Debug mode. We have to be extremely careful with introducing new features to ASR.
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.
See here: lfortran/lfortran#3354.
I am afraid we'll have to revert this change.
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.
Okay I will revert this PR and turn off the test for
llvm
.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.
Done in #88