#include "activation.h"

int getCurrentActivation(int skipCount) {
  int ret = 0;
  asm("mov %%ebp, %0" : "=r"(ret));
  ret = getCaller(ret);
  while (skipCount--)
    ret = getCaller(ret);
  return ret;
}

int getCaller(int frameHandle) { return *(int *)frameHandle; }

void *callerLocalsEnd(int frameHandle) {
  return (char *) frameHandle - 16;
}

// Warning: the returned PC really points to the next instruction to
// execute, not the current one.
void *getCallerPC(int frameHandle) {
  return ((void **) frameHandle)[1];
}

void setCallerPC(int frameHandle, void *absPC) {
  //fprintf(stderr, "setCallerPC : new pc = %x\n", absPC);
  ((void **) frameHandle)[1] = absPC;
}

