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

stack alignment with VAR on aarch64 #100

Open
sgraham opened this issue Jan 18, 2025 · 0 comments
Open

stack alignment with VAR on aarch64 #100

sgraham opened this issue Jan 18, 2025 · 0 comments

Comments

@sgraham
Copy link

sgraham commented Jan 18, 2025

It seems that 16 byte stack alignment doesn't get maintained on aarch64 when using ir_VAR, ir_VSTORE, ir_VLOAD. I'm testing on Mac/aarch64 and the C below generates this code:

myfunc:
	sub sp, sp, #8
	mov w0, #0x4d2
	str w0, [sp]
	ldr w0, [sp]
	add sp, sp, #8
	ret

which makes sp unaligned so bus error when trying to store w0.

If I fiddle around and add a few more local VAR (and disable opts) then it seems to get and stay aligned with more local allocation.

Or am I using ir_VAR incorrectly?

#include <stdlib.h>
#include "ir.h"
#include "ir_builder.h"

/*
 * int32_t myfunc(void) {
 *	int x = 1234;
 *	return x;
 * }
 */
typedef int32_t (*myfunc_t)(void);

void gen_myfunc(ir_ctx* ctx) {
  ir_START();
  ir_ref x = ir_VAR(IR_I32, "x");
  ir_VSTORE(x, ir_CONST_I32(1234));
  ir_ref val = ir_VLOAD(IR_I32, x);
  ir_RETURN(val);
}

int main(int argc, char** argv) {
  ir_ctx ctx;

  ir_consistency_check();

  ir_init(&ctx, IR_FUNCTION, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN);
  ctx.ret_type = IR_I32;

  gen_myfunc(&ctx);

  size_t size;
  void* entry = ir_jit_compile(&ctx, 0, &size);

  ir_disasm_init();
  ir_disasm("myfunc", entry, size, false, &ctx, stderr);
  ir_disasm_free();

  if (entry) {
    printf("func: %d\n", ((myfunc_t)entry)());
  }

  ir_free(&ctx);

  return 0;
}
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