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
Support global variable initialization #13
Conversation
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.
Append "Close #12" in git commit message(s). Therefore, you can link a pull request to an issue to show that a fix is in progress and to automatically close the issue when the pull request is merged.
See https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue
You can remove or change the statement "2. Global variable initialization is not supported." in |
tests/global.c
Outdated
@@ -0,0 +1,13 @@ | |||
int a = 44; |
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.
How about dropping tests/global.c
and adding the original test in file tests/driver.sh
?
I am planning to delete some trivial files in directory tests
such as def.c
, fib.c
, loop.c
, and pointer.c
, which are already covered in test driver.
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.
However, it still makes sense to generate an executable from tests/hello.c
.
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! Move global initialization into tests/driver.sh
You can prepare another git commit which applies this change to simplify the handing in file |
1b17da3
to
9e04dcd
Compare
src/cfront.c
Outdated
char buffer[10]; | ||
/* global initialization must be constant */ | ||
/* TODO: support rvalue as expression e.g. int a = 3 + 2; */ | ||
if (lex_peek(T_numeric, buffer)) |
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.
Can you reuse the code in function read_body_assignment
? That is, split some logic/operations into a helper function and then invoke the helper for reading the expressions from assignments.
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.
Do I need to do that anyway? The reason I don't like to do that is:
- The only duplicate code in
read_body_assignment
isfind_global_var
- Future update about global initialization will make more different with
read_body_assignment
. If I merge now, I guessread_body_assignment
will become harder to read - Merge control flow into
read_body_assignment
for one line re-use makesread_body_assignment
more complicated. I don't think one function(find_global_var
) reuse worth it.
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 agree. Let's stick to the reasonably effective changes.
I have changed commit with key word Close #12 but nothing change. Did it work after PR merge? |
README.md
Outdated
However, it is valid to use `ptr[0]`, which behaves the same of `*ptr`. | ||
4. The support of varying number of function arguments is incomplete. No `<stdarg.h>` can be used. | ||
3. The support of varying number of function arguments is incomplete. No `<stdarg.h>` can be used. |
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.
Don't change the order. Minimize the required changes.
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.
Later, we can create the corresponding issues via GitHub rather than the bullet points.
src/cfront.c
Outdated
char buffer[10]; | ||
int isneg = 0; | ||
/* global initialization must be constant */ | ||
/* TODO: support rvalue as expression e.g. int a = 3 + 2; */ |
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.
This issue should be resolved before this pull request is about to be merged.
Latest commit support using expression to initialize global variable (also add test bench in
|
src/cfront.c
Outdated
error("Invalid value after assignment"); | ||
lex_expect(T_numeric); | ||
if (isneg) | ||
return -1 * res; |
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.
Rewrite as (-1) * res
.
src/cfront.c
Outdated
lex_expect(T_numeric); | ||
if (isneg) | ||
return -1 * res; | ||
else |
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.
Eliminate this else
.
src/cfront.c
Outdated
return res; | ||
} | ||
|
||
int get_expression_imm(opcode_t op, int op1, int op2) |
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.
The verb get
might not reflect what the function does. It can be evaluate
or eval
in short.
src/cfront.c
Outdated
break; | ||
default: | ||
error("Using operation not support"); | ||
break; |
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.
No break
is required. Remove it.
README.md
Outdated
@@ -165,8 +165,7 @@ int fib(int n) fib: Reserve stack frame for function | |||
|
|||
1. Any non-zero value is NOT treated as logical truth by all ops. | |||
That is, the expression `0 == strcmp(ptr, "hello")` is not equivalent to `!strcmp(ptr, "hello")`. | |||
2. Global variable initialization is not supported. | |||
Therefore, you can not initialize a global such as `int i = [expr]`. | |||
2. ELF lacks of .bss and .rodata section |
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.
Should be "The generated ELF file lacks..."
src/cfront.c
Outdated
res = op1 >> op2; | ||
break; | ||
default: | ||
error("Using operation not support"); |
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.
Should be "The requested operation is not supported."
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.
Improve the git commit message. You should address 1) how you made it in summary; 2) Known limitation; 3) New test item.
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.
Change the message "Add correspond test bench in line 250 of driver.sh" to "Add global initialization item in test driver."
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.
In git commit messages, always use quotes ("...") instead of backquote. Change "help us evaluate" to "evaluates."
Change "This commit doesn't" to "this patch ..."
Close sysprog21#12 1. To initialize global variable, we need to decide the value before execution time. "read_global_assignment" evaluates this immediate value and store it in .data section where store the initial value of variable in generated ELF. 2. This patch doesn't support pointer and array initialization. 3. Add global initialization item in test driver.
Thank @eecheng87 for contributing! |
In latest version, there already have .data section which is for storing initialized global variable. As a result, I don't add .bss section which is for uninitialized variable in this pull request.