I'm reading the code of lcc, and also the book. And I'm stucking at the function isnullptr(Tree e), in file enode.c. The function is below:
static int isnullptr(e) Tree e; {
return (isint(e->type) && generic(e->op) == CNST
&& cast(e, unsignedtype)->u.v.u == 0)
|| (isvoidptr(e->type) && e->op == CNST+P
&& e->u.v.p == NULL);
}
I've checked the ANSI C standard about the null pointer and the null pointer constant, and still can't see how the return of isnullptr connect about the standard about null pointer or null pointer constant.is processed into a CNST+I tree? Is it caused by constant folding?
I also read the code of cast, does (isvoidptr(e->type) && e->op == CNST+P && e->u.v.p == NULL) implies that "a integer constant expression which value is 0 cast to type void *"? If so, why the op->e should be CNSP+P, as the code of lex.c suggested 0
Really hope to get help! Thanks!
Le 14/04/2020 à 15:58, Thimble Liu a écrit :
I'm reading the code of lcc, and also the book. And I'm stucking at the function isnullptr(Tree e), in file enode.c. The function is below:
static int isnullptr(e) Tree e; {
return (isint(e->type) && generic(e->op) == CNST
&& cast(e, unsignedtype)->u.v.u == 0)
|| (isvoidptr(e->type) && e->op == CNST+P
&& e->u.v.p == NULL);
}
is processed into a CNST+I tree? Is it caused by constant folding?I've checked the ANSI C standard about the null pointer and the null pointer constant, and still can't see how the return of isnullptr connect about the standard about null pointer or null pointer constant.
I also read the code of cast, does (isvoidptr(e->type) && e->op == CNST+P && e->u.v.p == NULL) implies that "a integer constant expression which value is 0 cast to type void *"? If so, why the op->e should be CNSP+P, as the code of lex.c suggested 0
Really hope to get help! Thanks!
There are two arms in the condition above:
1:
isint(e->type) &&
generic(e->op) == CNST &&
cast(e, unsignedtype)->u.v.u == 0)
An integer constant of value zero can be used as a null pointer
2:
isvoidptr(e->type) &&
e->op == CNST+P &&
e->u.v.p == NULL
A pointer with type constant and value zero
The zero is read as an integer, yes, but it can be later cast into a
void pointer or used to return a NULL value.
for instance
char *fn(void)
{
return 0;
}
P.S. There is no constant folding in the original lcc. I added that
feature much later.
I don't understand how a integer constant 0 cast to void *, can result a tree which op is CNST+P, and u.v.p field equal to NULL.
Le 15/04/2020 à 05:05, Thimble Liu a écrit :
I don't understand how a integer constant 0 cast to void *, can
result a tree which op is CNST+P, and u.v.p field equal to NULL.
I think that if you write:
char *p = 0x40407865;
You are making a pointer out of a constant. That wouldn't be the NULL
pointer since its value would be different from zero, hence the second condition.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 69:20:53 |
Calls: | 6,655 |
Calls today: | 1 |
Files: | 12,200 |
Messages: | 5,332,099 |
Posted today: | 1 |