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

wrong var->offset calculation on emit_code() #13

Open
AoiMoe opened this issue Oct 9, 2018 · 0 comments
Open

wrong var->offset calculation on emit_code() #13

AoiMoe opened this issue Oct 9, 2018 · 0 comments

Comments

@AoiMoe
Copy link

AoiMoe commented Oct 9, 2018

void emit_code(Function *fn) {
  // Assign an offset from RBP to each local variable.
  int off = 0;
  for (int i = 0; i < fn->lvars->len; i++) {
    Var *var = fn->lvars->data[i];
    off += var->ty->size;
    off = roundup(off, var->ty->align);  // (*)
    var->offset = -off;
  }
...

At the point (*), var->ty->ailgn may be 0. In such case, roundup() returns 0 regardless of off value and it causes the accumulation of the offset to break.

An example of the fix:

diff --git a/gen_x86.c b/gen_x86.c
index 5453e01..5aa2989 100644
--- a/gen_x86.c
+++ b/gen_x86.c
@@ -184,7 +184,8 @@ void emit_code(Function *fn) {
   for (int i = 0; i < fn->lvars->len; i++) {
     Var *var = fn->lvars->data[i];
     off += var->ty->size;
-    off = roundup(off, var->ty->align);
+    if (var->ty->ty != FUNC)
+      off = roundup(off, var->ty->align);
     var->offset = -off;
   }
 
diff --git a/util.c b/util.c
index a4d30d0..7bf0c5b 100644
--- a/util.c
+++ b/util.c
@@ -129,6 +129,7 @@ char *sb_get(StringBuilder *sb) {
 }
 
 int roundup(int x, int align) {
+  assert(align != 0);
   return (x + align - 1) & ~(align - 1);
 }
 
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

No branches or pull requests

1 participant