So as if to atone from my awful disassembler hack I wrote a really neat stack frame class. Say you’re writing some function that needs some non-volatile registers and maybe a local variable. You define the frame like this:

StackFrame frame = StackFrame();

const Register start_addr     = frame.get_register();
const Register end_addr       = frame.get_register();
const FloatRegister mr_floaty = frame.get_float_register();

const Address variable = frame.get_local_variable();

Once you defined the frame you just wrap your code with my funky new prolog and epilog macros and all the storing and restoring and stack pointer setting and alignment stuff gets done for you:

__ prolog (frame);

__ addi (end_addr, start_addr, 8);
__ lfd (mr_floaty, variable);
__ call (some_func);

__ epilog (frame);

As far as I can see the other OpenJDK architectures just do this kind of thing manually which for someone as slapdash as me is a whole world of pain.

Fun function of the week:

void disassemble(address start, address end)
{
  const char *fmt = "/tmp/aztec-%d.%c";
  char c_file[BUFSIZ], o_file[BUFSIZ];
  sprintf(c_file, fmt, getpid(), 'c');
  sprintf(o_file, fmt, getpid(), 'o');

  FILE *fp = fopen(c_file, "w");
  if (fp == NULL)
    fatal("%s:%d: can't write file", __FILE__, __LINE__);

  fputs("unsigned char start[] = {", fp);
  for (address a = start; a < end; a++) {
    if (a != start)
      fputc(',', fp);
    fprintf(fp, "0x%02x", *a);
  }
  fputs("};\n", fp);
  fclose(fp);

  char cmd[BUFSIZ];
  sprintf(cmd, "gcc -c %s -o %s", c_file, o_file);
  if (system(cmd) != 0)
    fatal("%s:%d: can't compile file", __FILE__, __LINE__);

  putchar('\n');
  sprintf(cmd, "objdump -D -j .data %s | grep '^....:'", o_file);
  if (system(cmd) != 0)
    fatal("%s:%d: can't disassemble file", __FILE__, __LINE__);
  putchar('\n');

  unlink(c_file);
  unlink(o_file);
}

It actually did work too!

#0  report_unimplemented (
    file_name=0xfb3d624 "aztec/hotspot/src/cpu/ppc/vm/icache_ppc.cpp",
    line_no=65)
    at aztec/hotspot/src/share/vm/utilities/debug.cpp:241
#1  0xf5b8405c in ?? ()
#2  0x0f760d84 in AbstractICache::invalidate_range (start=0xf5b86400 "",
    nbytes=15)
    at aztec/hotspot/src/share/vm/runtime/icache.cpp:102

Obviously this is like “so what?” for all you JIT guys but I’m kind of stunned here.

Well, my assembler worked, I think:

3c 60 0f b4     lis     r3, file@hi
60 63 68 e4     ori     r3, r3, file@lo
38 80 00 3a     li      r4, line
3d 40 0f 6e     lis     r10, report_unimplemented@hi
61 4a 82 60     ori     r10, r10, report_unimplemented@lo
7d 48 03 a6     mtlr    r10
4e 80 00 21     blrl
3d 40 0f e5     lis     r10, abort@hi
61 4a 7b a0     ori     r10, r10, abort@lo
7d 48 03 a6     mtlr    r10
4e 80 00 21     blrl

I finally bit the bullet and threw myself into PPC assembly. And what better way to ease you into your first assembly since 1998 than writing an assembler! It’s been a long couple of days but finally — finally! — it compiled and ran:

3c600fb4606368e43880003a3d400f6e614a82607d48
03a64e8000213d400fe5614a7ba07d4803a64e800021

Check back Monday to see if that’s garbage…

I’ve been busy filling in stubs recently, but something didn’t feel right. The stubs all say Unimplemented() — and that was calling itself. I’d been working towards the short term goal of getting Unimplemented() to work when I discovered that what lay beneath Unimplemented() and assert() was the most complicated error handling and debugging system in the world.

First it scans the command line to see if you told it to ignore the particular error it’s trying to report. Then it prints a message to tell you the command line options to make it skip that particular error. Then it checks to see if this is the first error it’s seen, if any other threads are trying to report errors, if any other threads are trying to report this particular error, whether this particular error is being reported as the first error by another thread, and whether this error is being reported as a result of an error in the error reporting system. Then it prepares messages of varying detail to print to a file if possible and the screen if not. It creates and executes shell scripts (!) to do who knows what. Then it thinks about quitting.

This is great, except that all this wacky stack and thread stuff is calling unimplemented code, and filling in stubs that the error reporting system is calling was getting the error reporting system working — not the VM!

The error reporting system now looks like this:

fprintf(stderr, "error: %s: %s:%d\n", _message, _filename, _lineno);
exit(EXIT_FAILURE);

Om Shanti.