Skip to content
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

Merged
merged 1 commit into from Oct 16, 2020
Merged

Conversation

eecheng87
Copy link
Collaborator

@eecheng87 eecheng87 commented Oct 14, 2020

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.

Copy link
Collaborator

@jserv jserv left a 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

@jserv
Copy link
Collaborator

jserv commented Oct 14, 2020

You can remove or change the statement "2. Global variable initialization is not supported." in README.md. You added new TODO as well.

tests/global.c Outdated
@@ -0,0 +1,13 @@
int a = 44;
Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator Author

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

@jserv
Copy link
Collaborator

jserv commented Oct 14, 2020

You can prepare another git commit which applies this change to simplify the handing in file src/globals.c. The reason why function global_init exists is because the global variable initializations were not supported.

@eecheng87 eecheng87 changed the title Support global variable initialization Support global variable initialization Closes #12 Oct 14, 2020
@eecheng87 eecheng87 changed the title Support global variable initialization Closes #12 Support global variable initialization Oct 14, 2020
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))
Copy link
Collaborator

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.

Copy link
Collaborator Author

@eecheng87 eecheng87 Oct 15, 2020

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 is find_global_var
  • Future update about global initialization will make more different with read_body_assignment. If I merge now, I guess read_body_assignment will become harder to read
  • Merge control flow into read_body_assignment for one line re-use makes read_body_assignment more complicated. I don't think one function(find_global_var) reuse worth it.

Copy link
Collaborator

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.

@eecheng87
Copy link
Collaborator Author

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

I have changed commit with key word Close #12 but nothing change. Did it work after PR merge?

@jserv
Copy link
Collaborator

jserv commented Oct 14, 2020

I have changed commit with key word Close #12 but nothing change. Did it work after PR merge?

If you check the end of #12, the issue is already associated with this pull request. Then, when this pull request is merged, #12 will be closed automatically.

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.
Copy link
Collaborator

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.

Copy link
Collaborator

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; */
Copy link
Collaborator

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.

@eecheng87
Copy link
Collaborator Author

eecheng87 commented Oct 15, 2020

Latest commit support using expression to initialize global variable (also add test bench in driver.sh).

read_global_assignment using the similar concept with read_expr to process expression.
The reason I don't want merge them in one function is that they actually for totally different purpose.

read_global_assignment need to know the result of expression before execution time and write into .data section before it.
read_expr don't need to get exact result of expression, it only need to know series of instruction and write into .text section. It gets the result during execution time.

@eecheng87 eecheng87 requested a review from jserv October 15, 2020 14:26
src/cfront.c Outdated
error("Invalid value after assignment");
lex_expect(T_numeric);
if (isneg)
return -1 * res;
Copy link
Collaborator

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
Copy link
Collaborator

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)
Copy link
Collaborator

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;
Copy link
Collaborator

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
Copy link
Collaborator

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");
Copy link
Collaborator

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."

Copy link
Collaborator

@jserv jserv left a 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.

Copy link
Collaborator

@jserv jserv left a 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."

Copy link
Collaborator

@jserv jserv left a 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.
@jserv
Copy link
Collaborator

jserv commented Oct 16, 2020

Thank @eecheng87 for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants