diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 27a637d849..81fcd6c1c2 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -164,17 +164,17 @@ Java_java_lang_Runtime_exec(JNIEnv* e, jclass, SetHandleInformation(in[0], HANDLE_FLAG_INHERIT, 0); jlong inDescriptor = static_cast(descriptor(e, in[0])); if(e->ExceptionCheck()) return; - e->SetLongArrayRegion(process, 1, 1, &inDescriptor); + e->SetLongArrayRegion(process, 2, 1, &inDescriptor); makePipe(e, out); SetHandleInformation(out[1], HANDLE_FLAG_INHERIT, 0); jlong outDescriptor = static_cast(descriptor(e, out[1])); if(e->ExceptionCheck()) return; - e->SetLongArrayRegion(process, 2, 1, &outDescriptor); + e->SetLongArrayRegion(process, 3, 1, &outDescriptor); makePipe(e, err); SetHandleInformation(err[0], HANDLE_FLAG_INHERIT, 0); jlong errDescriptor = static_cast(descriptor(e, err[0])); if(e->ExceptionCheck()) return; - e->SetLongArrayRegion(process, 3, 1, &errDescriptor); + e->SetLongArrayRegion(process, 4, 1, &errDescriptor); PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); @@ -190,6 +190,10 @@ Java_java_lang_Runtime_exec(JNIEnv* e, jclass, BOOL success = CreateProcess(0, (LPSTR) RUNTIME_ARRAY_BODY(line), 0, 0, 1, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT, 0, 0, &si, &pi); + + CloseHandle(in[1]); + CloseHandle(out[0]); + CloseHandle(err[1]); if (not success) { throwNew(e, "java/io/IOException", getErrorStr(GetLastError())); @@ -198,24 +202,12 @@ Java_java_lang_Runtime_exec(JNIEnv* e, jclass, jlong pid = reinterpret_cast(pi.hProcess); e->SetLongArrayRegion(process, 0, 1, &pid); - + jlong tid = reinterpret_cast(pi.hThread); + e->SetLongArrayRegion(process, 1, 1, &tid); } extern "C" JNIEXPORT jint JNICALL -Java_java_lang_Runtime_exitValue(JNIEnv* e, jclass, jlong pid) -{ - DWORD exitCode; - BOOL success = GetExitCodeProcess(reinterpret_cast(pid), &exitCode); - if(not success){ - throwNew(e, "java/lang/Exception", getErrorStr(GetLastError())); - } else if(exitCode == STILL_ACTIVE){ - throwNew(e, "java/lang/IllegalThreadStateException", "Process is still active"); - } - return exitCode; -} - -extern "C" JNIEXPORT jint JNICALL -Java_java_lang_Runtime_waitFor(JNIEnv* e, jclass, jlong pid) +Java_java_lang_Runtime_waitFor(JNIEnv* e, jclass, jlong pid, jlong tid) { DWORD exitCode; WaitForSingleObject(reinterpret_cast(pid), INFINITE); @@ -223,6 +215,10 @@ Java_java_lang_Runtime_waitFor(JNIEnv* e, jclass, jlong pid) if(not success){ throwNew(e, "java/lang/Exception", getErrorStr(GetLastError())); } + + CloseHandle(reinterpret_cast(pid)); + CloseHandle(reinterpret_cast(tid)); + return exitCode; } #else @@ -248,15 +244,15 @@ Java_java_lang_Runtime_exec(JNIEnv* e, jclass, makePipe(e, in); if(e->ExceptionCheck()) return; jlong inDescriptor = static_cast(in[0]); - e->SetLongArrayRegion(process, 1, 1, &inDescriptor); + e->SetLongArrayRegion(process, 2, 1, &inDescriptor); makePipe(e, out); if(e->ExceptionCheck()) return; jlong outDescriptor = static_cast(out[1]); - e->SetLongArrayRegion(process, 2, 1, &outDescriptor); + e->SetLongArrayRegion(process, 3, 1, &outDescriptor); makePipe(e, err); if(e->ExceptionCheck()) return; jlong errDescriptor = static_cast(err[0]); - e->SetLongArrayRegion(process, 3, 1, &errDescriptor); + e->SetLongArrayRegion(process, 4, 1, &errDescriptor); makePipe(e, msg); if(e->ExceptionCheck()) return; if(fcntl(msg[1], F_SETFD, FD_CLOEXEC) != 0) { @@ -317,21 +313,7 @@ Java_java_lang_Runtime_exec(JNIEnv* e, jclass, } extern "C" JNIEXPORT jint JNICALL -Java_java_lang_Runtime_exitValue(JNIEnv* e, jclass, jlong pid) -{ - int status; - pid_t returned = waitpid(pid, &status, WNOHANG); - if(returned == 0){ - throwNewErrno(e, "java/lang/IllegalThreadStateException"); - } else if(returned == -1){ - throwNewErrno(e, "java/lang/Exception"); - } - - return WEXITSTATUS(status); -} - -extern "C" JNIEXPORT jint JNICALL -Java_java_lang_Runtime_waitFor(JNIEnv*, jclass, jlong pid) +Java_java_lang_Runtime_waitFor(JNIEnv*, jclass, jlong pid, jlong) { bool finished = false; int status; diff --git a/classpath/java/lang/Runtime.java b/classpath/java/lang/Runtime.java index d1accb33fd..55f16cd456 100644 --- a/classpath/java/lang/Runtime.java +++ b/classpath/java/lang/Runtime.java @@ -44,28 +44,80 @@ public class Runtime { } public Process exec(String command) throws IOException { - long[] process = new long[4]; StringTokenizer t = new StringTokenizer(command); String[] cmd = new String[t.countTokens()]; for (int i = 0; i < cmd.length; i++) cmd[i] = t.nextToken(); - exec(cmd, process); - return new MyProcess(process[0], (int) process[1], (int) process[2], (int) process[3]); + + return exec(cmd); } - public Process exec(String[] command) { - long[] process = new long[4]; - exec(command, process); - return new MyProcess(process[0], (int) process[1], (int) process[2], (int) process[3]); + public Process exec(final String[] command) throws IOException { + final MyProcess[] process = new MyProcess[1]; + final Throwable[] exception = new Throwable[1]; + + synchronized (process) { + Thread t = new Thread() { + public void run() { + synchronized (process) { + try { + long[] info = new long[5]; + exec(command, info); + process[0] = new MyProcess + (info[0], info[1], (int) info[2], (int) info[3], + (int) info[4]); + } catch (Throwable e) { + exception[0] = e; + } finally { + process.notifyAll(); + } + } + + MyProcess p = process[0]; + if (p != null) { + synchronized (p) { + try { + if (p.pid != 0) { + p.exitCode = Runtime.waitFor(p.pid, p.tid); + p.pid = 0; + p.tid = 0; + } + } finally { + p.notifyAll(); + } + } + } + } + }; + t.setDaemon(true); + t.start(); + + while (process[0] == null && exception[0] == null) { + try { + process.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + + if (exception[0] != null) { + if (exception[0] instanceof IOException) { + throw new IOException(exception[0]); + } else { + throw new RuntimeException(exception[0]); + } + } + + return process[0]; } public native void addShutdownHook(Thread t); - private static native void exec(String[] command, long[] process); + private static native void exec(String[] command, long[] process) + throws IOException; - private static native int exitValue(long pid); - - private static native int waitFor(long pid); + private static native int waitFor(long pid, long tid); private static native void load(String name, boolean mapName); @@ -79,13 +131,15 @@ public class Runtime { private static class MyProcess extends Process { private long pid; + private long tid; private final int in; private final int out; private final int err; private int exitCode; - public MyProcess(long pid, int in, int out, int err) { + public MyProcess(long pid, long tid, int in, int out, int err) { this.pid = pid; + this.tid = tid; this.in = in; this.out = out; this.err = err; @@ -95,13 +149,6 @@ public class Runtime { throw new RuntimeException("not implemented"); } - public int exitValue() { - if (pid != 0) { - exitCode = Runtime.exitValue(pid); - } - return exitCode; - } - public InputStream getInputStream() { return new FileInputStream(new FileDescriptor(in)); } @@ -114,11 +161,19 @@ public class Runtime { return new FileInputStream(new FileDescriptor(err)); } - public int waitFor() throws InterruptedException { + public synchronized int exitValue() { if (pid != 0) { - exitCode = Runtime.waitFor(pid); - pid = 0; + throw new IllegalThreadStateException(); } + + return exitCode; + } + + public synchronized int waitFor() throws InterruptedException { + while (pid != 0) { + wait(); + } + return exitCode; } } diff --git a/makefile b/makefile index ba76e7afd4..9d1c1c63cc 100644 --- a/makefile +++ b/makefile @@ -189,15 +189,25 @@ ifeq ($(arch),powerpc) pointer-size = 4 endif ifeq ($(arch),arm) - asm = arm - pointer-size = 4 + asm = arm + pointer-size = 4 + cflags += -Wno-psabi + + ifneq ($(arch),$(build-arch)) + cxx = arm-linux-gnueabi-g++ + cc = arm-linux-gnueabi-gcc + ar = arm-linux-gnueabi-ar + ranlib = arm-linux-gnueabi-ranlib + strip = arm-linux-gnueabi-strip + endif endif ifeq ($(platform),darwin) version-script-flag = build-cflags = $(common-cflags) -fPIC -fvisibility=hidden -I$(src) build-lflags += -framework CoreFoundation - lflags = $(common-lflags) -ldl -framework CoreFoundation -framework CoreServices + lflags = $(common-lflags) -ldl -framework CoreFoundation \ + -framework CoreServices ifeq ($(bootimage),true) bootimage-lflags = -Wl,-segprot,__RWX,rwx,rwx endif @@ -207,6 +217,9 @@ ifeq ($(platform),darwin) shared = -dynamiclib ifeq ($(arch),powerpc) + ifneq (,$(filter i386 x86_64,$(build-arch))) + converter-cflags = -DOPPOSITE_ENDIAN + endif openjdk-extra-cflags += -arch ppc cflags += -arch ppc asmflags += -arch ppc @@ -214,6 +227,9 @@ ifeq ($(platform),darwin) endif ifeq ($(arch),i386) + ifeq ($(build-arch),powerpc) + converter-cflags = -DOPPOSITE_ENDIAN + endif openjdk-extra-cflags += -arch i386 cflags += -arch i386 asmflags += -arch i386 @@ -221,6 +237,9 @@ ifeq ($(platform),darwin) endif ifeq ($(arch),x86_64) + ifeq ($(build-arch),powerpc) + converter-cflags = -DOPPOSITE_ENDIAN + endif openjdk-extra-cflags += -arch x86_64 cflags += -arch x86_64 asmflags += -arch x86_64 @@ -364,7 +383,8 @@ generated-code = \ $(build)/type-declarations.cpp \ $(build)/type-constructors.cpp \ $(build)/type-initializations.cpp \ - $(build)/type-java-initializations.cpp + $(build)/type-java-initializations.cpp \ + $(build)/type-name-initializations.cpp vm-depends := $(generated-code) $(wildcard $(src)/*.h) @@ -650,19 +670,19 @@ $(build)/binaryToObject-main.o: $(src)/binaryToObject/main.cpp $(build-cxx) -c $(^) -o $(@) $(build)/binaryToObject-elf64.o: $(src)/binaryToObject/elf.cpp - $(build-cxx) -DBITS_PER_WORD=64 -c $(^) -o $(@) + $(build-cxx) $(converter-cflags) -DBITS_PER_WORD=64 -c $(^) -o $(@) $(build)/binaryToObject-elf32.o: $(src)/binaryToObject/elf.cpp - $(build-cxx) -DBITS_PER_WORD=32 -c $(^) -o $(@) + $(build-cxx) $(converter-cflags) -DBITS_PER_WORD=32 -c $(^) -o $(@) $(build)/binaryToObject-mach-o64.o: $(src)/binaryToObject/mach-o.cpp - $(build-cxx) -DBITS_PER_WORD=64 -c $(^) -o $(@) + $(build-cxx) $(converter-cflags) -DBITS_PER_WORD=64 -c $(^) -o $(@) $(build)/binaryToObject-mach-o32.o: $(src)/binaryToObject/mach-o.cpp - $(build-cxx) -DBITS_PER_WORD=32 -c $(^) -o $(@) + $(build-cxx) $(converter-cflags) -DBITS_PER_WORD=32 -c $(^) -o $(@) $(build)/binaryToObject-pe.o: $(src)/binaryToObject/pe.cpp - $(build-cxx) -c $(^) -o $(@) + $(build-cxx) $(converter-cflags) -c $(^) -o $(@) $(converter): $(converter-objects) $(build-cxx) $(^) -o $(@) diff --git a/src/arm.S b/src/arm.S index f3dd2b146f..fe068824b0 100644 --- a/src/arm.S +++ b/src/arm.S @@ -50,7 +50,9 @@ vmNativeCall: .globl vmJump vmJump: + mov lr, r0 + ldr r0, [sp] + ldr r1, [sp, #4] mov sp, r2 - mov r4, r3 - ldmia sp, {r0,r1} - mov pc, lr + mov r8, r3 + bx lr diff --git a/src/arm.cpp b/src/arm.cpp index 66af8a37d3..46e87a3b23 100644 --- a/src/arm.cpp +++ b/src/arm.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, Avian Contributors +/* Copyright (c) 2010, Avian Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided @@ -14,6 +14,7 @@ #define CAST1(x) reinterpret_cast(x) #define CAST2(x) reinterpret_cast(x) #define CAST3(x) reinterpret_cast(x) +#define CAST_BRANCH(x) reinterpret_cast(x) using namespace vm; @@ -29,9 +30,9 @@ inline int DATA(int cond, int opcode, int S, int Rn, int Rd, int shift, int Sh, inline int DATAS(int cond, int opcode, int S, int Rn, int Rd, int Rs, int Sh, int Rm) { return cond<<28 | opcode<<21 | S<<20 | Rn<<16 | Rd<<12 | Rs<<8 | Sh<<5 | 1<<4 | Rm; } inline int DATAI(int cond, int opcode, int S, int Rn, int Rd, int rot, int imm) -{ return cond<<28 | 1<<25 | opcode<<21 | S<<20 | Rn<<16 | Rd<<12 | rot<<8 | imm; } +{ return cond<<28 | 1<<25 | opcode<<21 | S<<20 | Rn<<16 | Rd<<12 | rot<<8 | (imm&0xff); } inline int BRANCH(int cond, int L, int offset) -{ return cond<<28 | 5<<25 | L<<24 | offset; } +{ return cond<<28 | 5<<25 | L<<24 | (offset&0xffffff); } inline int BRANCHX(int cond, int L, int Rm) { return cond<<28 | 0x4bffc<<6 | L<<5 | 1<<4 | Rm; } inline int MULTIPLY(int cond, int mul, int S, int Rd, int Rn, int Rs, int Rm) @@ -39,17 +40,19 @@ inline int MULTIPLY(int cond, int mul, int S, int Rd, int Rn, int Rs, int Rm) inline int XFER(int cond, int P, int U, int B, int W, int L, int Rn, int Rd, int shift, int Sh, int Rm) { return cond<<28 | 3<<25 | P<<24 | U<<23 | B<<22 | W<<21 | L<<20 | Rn<<16 | Rd<<12 | shift<<7 | Sh<<5 | Rm; } inline int XFERI(int cond, int P, int U, int B, int W, int L, int Rn, int Rd, int offset) -{ return cond<<28 | 2<<25 | P<<24 | U<<23 | B<<22 | W<<21 | L<<20 | Rn<<16 | Rd<<12 | offset; } +{ return cond<<28 | 2<<25 | P<<24 | U<<23 | B<<22 | W<<21 | L<<20 | Rn<<16 | Rd<<12 | (offset&0xfff); } inline int XFER2(int cond, int P, int U, int W, int L, int Rn, int Rd, int S, int H, int Rm) { return cond<<28 | P<<24 | U<<23 | W<<21 | L<<20 | Rn<<16 | Rd<<12 | 1<<7 | S<<6 | H<<5 | 1<<4 | Rm; } inline int XFER2I(int cond, int P, int U, int W, int L, int Rn, int Rd, int offsetH, int S, int H, int offsetL) -{ return cond<<28 | P<<24 | U<<23 | 1<<22 | W<<21 | L<<20 | Rn<<16 | Rd<<12 | offsetH<<8 | 1<<7 | S<<6 | H<<5 | 1<<4 | offsetL; } +{ return cond<<28 | P<<24 | U<<23 | 1<<22 | W<<21 | L<<20 | Rn<<16 | Rd<<12 | offsetH<<8 | 1<<7 | S<<6 | H<<5 | 1<<4 | (offsetL&0xf); } inline int BLOCKXFER(int cond, int P, int U, int S, int W, int L, int Rn, int rlist) { return cond<<28 | 4<<25 | P<<24 | U<<23 | S<<22 | W<<21 | L<<20 | Rn<<16 | rlist; } inline int SWI(int cond, int imm) -{ return cond<<28 | 0x0f<<24 | imm; } +{ return cond<<28 | 0x0f<<24 | (imm&0xffffff); } inline int SWAP(int cond, int B, int Rn, int Rd, int Rm) { return cond<<28 | 1<<24 | B<<22 | Rn<<16 | Rd<<12 | 9<<4 | Rm; } +// FIELD CALCULATORS +inline int calcU(int imm) { return imm >= 0 ? 1 : 0; } // INSTRUCTIONS // The "cond" and "S" fields are set using the SETCOND() and SETS() functions inline int b(int offset) { return BRANCH(AL, 0, offset); } @@ -65,10 +68,10 @@ inline int add(int Rd, int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, inline int adc(int Rd, int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0x5, 0, Rn, Rd, shift, Sh, Rm); } inline int sbc(int Rd, int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0x6, 0, Rn, Rd, shift, Sh, Rm); } inline int rsc(int Rd, int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0x7, 0, Rn, Rd, shift, Sh, Rm); } -inline int tst(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0x8, 0, Rn, 0, shift, Sh, Rm); } -inline int teq(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0x9, 0, Rn, 0, shift, Sh, Rm); } -inline int cmp(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0xa, 0, Rn, 0, shift, Sh, Rm); } -inline int cmn(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0xb, 0, Rn, 0, shift, Sh, Rm); } +inline int tst(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0x8, 1, Rn, 0, shift, Sh, Rm); } +inline int teq(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0x9, 1, Rn, 0, shift, Sh, Rm); } +inline int cmp(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0xa, 1, Rn, 0, shift, Sh, Rm); } +inline int cmn(int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0xb, 1, Rn, 0, shift, Sh, Rm); } inline int orr(int Rd, int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0xc, 0, Rn, Rd, shift, Sh, Rm); } inline int mov(int Rd, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0xd, 0, 0, Rd, shift, Sh, Rm); } inline int bic(int Rd, int Rn, int Rm, int Sh=0, int shift=0) { return DATA(AL, 0xe, 0, Rn, Rd, shift, Sh, Rm); } @@ -79,39 +82,40 @@ inline int subi(int Rd, int Rn, int imm, int rot=0) { return DATAI(AL, 0x2, 0, R inline int rsbi(int Rd, int Rn, int imm, int rot=0) { return DATAI(AL, 0x3, 0, Rn, Rd, rot, imm); } inline int addi(int Rd, int Rn, int imm, int rot=0) { return DATAI(AL, 0x4, 0, Rn, Rd, rot, imm); } inline int adci(int Rd, int Rn, int imm, int rot=0) { return DATAI(AL, 0x5, 0, Rn, Rd, rot, imm); } -inline int cmpi(int Rn, int imm, int rot=0) { return DATAI(AL, 0x0, 0, Rn, 0, rot, imm); } +inline int bici(int Rd, int Rn, int imm, int rot=0) { return DATAI(AL, 0xe, 0, Rn, Rd, rot, imm); } +inline int cmpi(int Rn, int imm, int rot=0) { return DATAI(AL, 0xa, 1, Rn, 0, rot, imm); } inline int orri(int Rd, int Rn, int imm, int rot=0) { return DATAI(AL, 0xc, 0, Rn, Rd, rot, imm); } inline int movi(int Rd, int imm, int rot=0) { return DATAI(AL, 0xd, 0, 0, Rd, rot, imm); } +inline int orrsh(int Rd, int Rn, int Rm, int Rs, int Sh) { return DATAS(AL, 0xc, 0, Rn, Rd, Rs, Sh, Rm); } inline int movsh(int Rd, int Rm, int Rs, int Sh) { return DATAS(AL, 0xd, 0, 0, Rd, Rs, Sh, Rm); } inline int mul(int Rd, int Rm, int Rs) { return MULTIPLY(AL, 0, 0, Rd, 0, Rs, Rm); } inline int mla(int Rd, int Rm, int Rs, int Rn) { return MULTIPLY(AL, 1, 0, Rd, Rn, Rs, Rm); } -inline int umull(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 4, 0, RdLo, RdHi, Rs, Rm); } -inline int umlal(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 5, 0, RdLo, RdHi, Rs, Rm); } -inline int smull(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 6, 0, RdLo, RdHi, Rs, Rm); } -inline int smlal(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 7, 0, RdLo, RdHi, Rs, Rm); } -inline int ldr(int Rd, int Rn, int Rm) { return XFER(AL, 1, 1, 0, 0, 1, Rn, Rd, 0, 0, Rm); } -inline int ldri(int Rd, int Rn, int imm) { return XFERI(AL, 1, 1, 0, 0, 1, Rn, Rd, imm); } +inline int umull(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 4, 0, RdHi, RdLo, Rs, Rm); } +inline int umlal(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 5, 0, RdHi, RdLo, Rs, Rm); } +inline int smull(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 6, 0, RdHi, RdLo, Rs, Rm); } +inline int smlal(int RdLo, int RdHi, int Rm, int Rs) { return MULTIPLY(AL, 7, 0, RdHi, RdLo, Rs, Rm); } +inline int ldr(int Rd, int Rn, int Rm, int W=0) { return XFER(AL, 1, 1, 0, W, 1, Rn, Rd, 0, 0, Rm); } +inline int ldri(int Rd, int Rn, int imm, int W=0) { return XFERI(AL, 1, calcU(imm), 0, W, 1, Rn, Rd, abs(imm)); } inline int ldrb(int Rd, int Rn, int Rm) { return XFER(AL, 1, 1, 1, 0, 1, Rn, Rd, 0, 0, Rm); } -inline int ldrbi(int Rd, int Rn, int imm) { return XFERI(AL, 1, 1, 1, 0, 1, Rn, Rd, imm); } +inline int ldrbi(int Rd, int Rn, int imm) { return XFERI(AL, 1, calcU(imm), 1, 0, 1, Rn, Rd, abs(imm)); } inline int str(int Rd, int Rn, int Rm, int W=0) { return XFER(AL, 1, 1, 0, W, 0, Rn, Rd, 0, 0, Rm); } -inline int stri(int Rd, int Rn, int imm, int W=0) { return XFERI(AL, 1, 1, 0, W, 0, Rn, Rd, imm); } +inline int stri(int Rd, int Rn, int imm, int W=0) { return XFERI(AL, 1, calcU(imm), 0, W, 0, Rn, Rd, abs(imm)); } inline int strb(int Rd, int Rn, int Rm) { return XFER(AL, 1, 1, 1, 0, 0, Rn, Rd, 0, 0, Rm); } -inline int strbi(int Rd, int Rn, int imm) { return XFERI(AL, 1, 1, 1, 0, 0, Rn, Rd, imm); } +inline int strbi(int Rd, int Rn, int imm) { return XFERI(AL, 1, calcU(imm), 1, 0, 0, Rn, Rd, abs(imm)); } inline int ldrh(int Rd, int Rn, int Rm) { return XFER2(AL, 1, 1, 0, 1, Rn, Rd, 0, 1, Rm); } -inline int ldrhi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, 1, 0, 1, Rn, Rd, imm>>4 & 0xf, 0, 1, imm&0xf); } +inline int ldrhi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, calcU(imm), 0, 1, Rn, Rd, abs(imm)>>4 & 0xf, 0, 1, abs(imm)&0xf); } inline int strh(int Rd, int Rn, int Rm) { return XFER2(AL, 1, 1, 0, 0, Rn, Rd, 0, 1, Rm); } -inline int strhi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, 1, 0, 0, Rn, Rd, imm>>4 & 0xf, 0, 1, imm&0xf); } +inline int strhi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, calcU(imm), 0, 0, Rn, Rd, abs(imm)>>4 & 0xf, 0, 1, abs(imm)&0xf); } inline int ldrsh(int Rd, int Rn, int Rm) { return XFER2(AL, 1, 1, 0, 1, Rn, Rd, 1, 1, Rm); } -inline int ldrshi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, 1, 0, 1, Rn, Rd, imm>>4 & 0xf, 1, 1, imm&0xf); } +inline int ldrshi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, calcU(imm), 0, 1, Rn, Rd, abs(imm)>>4 & 0xf, 1, 1, abs(imm)&0xf); } inline int ldrsb(int Rd, int Rn, int Rm) { return XFER2(AL, 1, 1, 0, 1, Rn, Rd, 1, 0, Rm); } -inline int ldrsbi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, 1, 0, 1, Rn, Rd, imm>>4 & 0xf, 1, 0, imm&0xf); } -inline int ldmib(int Rn, int rlist) { return BLOCKXFER(AL, 1, 1, 0, 0, 1, Rn, rlist); } -inline int ldmia(int Rn, int rlist) { return BLOCKXFER(AL, 0, 1, 0, 0, 1, Rn, rlist); } -inline int stmib(int Rn, int rlist) { return BLOCKXFER(AL, 1, 1, 0, 0, 0, Rn, rlist); } -inline int stmdb(int Rn, int rlist) { return BLOCKXFER(AL, 1, 0, 0, 0, 0, Rn, rlist); } +inline int ldrsbi(int Rd, int Rn, int imm) { return XFER2I(AL, 1, calcU(imm), 0, 1, Rn, Rd, abs(imm)>>4 & 0xf, 1, 0, abs(imm)&0xf); } +inline int pop(int Rd) { return XFERI(AL, 0, 1, 0, 0, 1, 13, Rd, 4); } +inline int ldmfd(int Rn, int rlist) { return BLOCKXFER(AL, 0, 1, 0, 1, 1, Rn, rlist); } +inline int stmfd(int Rn, int rlist) { return BLOCKXFER(AL, 1, 0, 0, 1, 0, Rn, rlist); } inline int swp(int Rd, int Rm, int Rn) { return SWAP(AL, 0, Rn, Rd, Rm); } inline int swpb(int Rd, int Rm, int Rn) { return SWAP(AL, 1, Rn, Rd, Rm); } -inline int SETCOND(int ins, int cond) { return ins&0x0fffffff | cond<<28; } +inline int SETCOND(int ins, int cond) { return ((ins&0x0fffffff) | (cond<<28)); } inline int SETS(int ins) { return ins | 1<<20; } // PSEUDO-INSTRUCTIONS inline int nop() { return mov(0, 0); } @@ -122,6 +126,16 @@ inline int lsri(int Rd, int Rm, int imm) { return mov(Rd, Rm, LSR, imm); } inline int asr(int Rd, int Rm, int Rs) { return movsh(Rd, Rm, Rs, ASR); } inline int asri(int Rd, int Rm, int imm) { return mov(Rd, Rm, ASR, imm); } inline int ror(int Rd, int Rm, int Rs) { return movsh(Rd, Rm, Rs, ROR); } +inline int beq(int offset) { return SETCOND(b(offset), EQ); } +inline int bne(int offset) { return SETCOND(b(offset), NE); } +inline int bls(int offset) { return SETCOND(b(offset), LS); } +inline int bhi(int offset) { return SETCOND(b(offset), HI); } +inline int blt(int offset) { return SETCOND(b(offset), LT); } +inline int bgt(int offset) { return SETCOND(b(offset), GT); } +inline int ble(int offset) { return SETCOND(b(offset), LE); } +inline int bge(int offset) { return SETCOND(b(offset), GE); } +inline int blo(int offset) { return SETCOND(b(offset), CC); } +inline int bhs(int offset) { return SETCOND(b(offset), CS); } } const uint64_t MASK_LO32 = 0xffffffff; @@ -134,46 +148,88 @@ inline unsigned hi16(int64_t i) { return lo16(i>>16); } inline unsigned lo8(int64_t i) { return (unsigned)(i&MASK_LO8); } inline unsigned hi8(int64_t i) { return lo8(i>>8); } +inline int ha16(int32_t i) { + return ((i >> 16) + ((i & 0x8000) ? 1 : 0)) & 0xffff; +} +inline int unha16(int32_t high, int32_t low) { + return ((high - ((low & 0x8000) ? 1 : 0)) << 16) | low; +} + inline bool isInt8(intptr_t v) { return v == static_cast(v); } inline bool isInt16(intptr_t v) { return v == static_cast(v); } -inline bool isInt24(intptr_t v) { return v == v & 0xffffff; } +inline bool isInt24(intptr_t v) { return v == (v & 0xffffff); } inline bool isInt32(intptr_t v) { return v == static_cast(v); } inline int carry16(intptr_t v) { return static_cast(v) < 0 ? 1 : 0; } -const unsigned FrameFooterSize = 0; +inline bool isOfWidth(long long i, int size) { return static_cast(i) >> size == 0; } +inline bool isOfWidth(int i, int size) { return static_cast(i) >> size == 0; } + +const unsigned FrameFooterSize = 2; +const unsigned FrameHeaderSize = 0; + const unsigned StackAlignmentInBytes = 8; const unsigned StackAlignmentInWords = StackAlignmentInBytes / BytesPerWord; +const int ThreadRegister = 8; const int StackRegister = 13; -const int ThreadRegister = 12; +const int LinkRegister = 14; +const int ProgramCounter = 15; + +const int32_t PoolOffsetMask = 0xFFF; + +const bool DebugPool = false; + +class Context; +class MyBlock; +class PoolOffset; +class PoolEvent; + +void +resolve(MyBlock*); + +unsigned +padding(MyBlock*, unsigned); class MyBlock: public Assembler::Block { public: - MyBlock(unsigned offset): - next(0), offset(offset), start(~0), size(0) + MyBlock(Context* context, unsigned offset): + context(context), next(0), poolOffsetHead(0), poolOffsetTail(0), + lastPoolOffsetTail(0), poolEventHead(0), poolEventTail(0), + lastEventOffset(0), offset(offset), start(~0), size(0) { } virtual unsigned resolve(unsigned start, Assembler::Block* next) { this->start = start; this->next = static_cast(next); - return start + size; + ::resolve(this); + + return start + size + padding(this, size); } + Context* context; MyBlock* next; + PoolOffset* poolOffsetHead; + PoolOffset* poolOffsetTail; + PoolOffset* lastPoolOffsetTail; + PoolEvent* poolEventHead; + PoolEvent* poolEventTail; + unsigned lastEventOffset; unsigned offset; unsigned start; unsigned size; }; class Task; +class ConstantPoolEntry; class Context { public: Context(System* s, Allocator* a, Zone* zone): s(s), zone(zone), client(0), code(s, a, 1024), tasks(0), result(0), - firstBlock(new (zone->allocate(sizeof(MyBlock))) MyBlock(0)), - lastBlock(firstBlock) + firstBlock(new (zone->allocate(sizeof(MyBlock))) MyBlock(this, 0)), + lastBlock(firstBlock), poolOffsetHead(0), poolOffsetTail(0), + constantPool(0), constantPoolCount(0) { } System* s; @@ -184,6 +240,10 @@ class Context { uint8_t* result; MyBlock* firstBlock; MyBlock* lastBlock; + PoolOffset* poolOffsetHead; + PoolOffset* poolOffsetTail; + ConstantPoolEntry* constantPool; + unsigned constantPoolCount; }; class Task { @@ -206,6 +266,10 @@ typedef void (*TernaryOperationType) (Context*, unsigned, Assembler::Operand*, Assembler::Operand*, Assembler::Operand*); +typedef void (*BranchOperationType) +(Context*, TernaryOperation, unsigned, Assembler::Operand*, + Assembler::Operand*, Assembler::Operand*); + class ArchitectureContext { public: ArchitectureContext(System* s): s(s) { } @@ -217,7 +281,9 @@ class ArchitectureContext { BinaryOperationType binaryOperations [BinaryOperationCount * OperandTypeCount * OperandTypeCount]; TernaryOperationType ternaryOperations - [TernaryOperationCount * OperandTypeCount]; + [NonBranchTernaryOperationCount * OperandTypeCount]; + BranchOperationType branchOperations + [BranchOperationCount * OperandTypeCount * OperandTypeCount]; }; inline void NO_RETURN @@ -265,7 +331,8 @@ class Offset: public Promise { virtual int64_t value() { assert(c, resolved()); - return block->start + (offset - block->offset); + unsigned o = offset - block->offset; + return block->start + padding(block, o) + o; } Context* c; @@ -287,10 +354,11 @@ bounded(int right, int left, int32_t v) } void* -updateOffset(System* s, uint8_t* instruction, bool conditional UNUSED, int64_t value) +updateOffset(System* s, uint8_t* instruction, int64_t value) { - int32_t v = reinterpret_cast(value) - instruction; - + // ARM's PC is two words ahead, and branches drop the bottom 2 bits. + int32_t v = (reinterpret_cast(value) - (instruction + 8)) >> 2; + int32_t mask; expect(s, bounded(0, 8, v)); mask = 0xFFFFFF; @@ -303,66 +371,59 @@ updateOffset(System* s, uint8_t* instruction, bool conditional UNUSED, int64_t v class OffsetListener: public Promise::Listener { public: - OffsetListener(System* s, uint8_t* instruction, bool conditional): + OffsetListener(System* s, uint8_t* instruction): s(s), - instruction(instruction), - conditional(conditional) + instruction(instruction) { } virtual bool resolve(int64_t value, void** location) { - void* p = updateOffset(s, instruction, conditional, value); + void* p = updateOffset(s, instruction, value); if (location) *location = p; return false; } System* s; uint8_t* instruction; - bool conditional; }; class OffsetTask: public Task { public: - OffsetTask(Task* next, Promise* promise, Promise* instructionOffset, - bool conditional): + OffsetTask(Task* next, Promise* promise, Promise* instructionOffset): Task(next), promise(promise), - instructionOffset(instructionOffset), - conditional(conditional) + instructionOffset(instructionOffset) { } virtual void run(Context* c) { if (promise->resolved()) { updateOffset - (c->s, c->result + instructionOffset->value(), conditional, - promise->value()); + (c->s, c->result + instructionOffset->value(), promise->value()); } else { new (promise->listen(sizeof(OffsetListener))) - OffsetListener(c->s, c->result + instructionOffset->value(), - conditional); + OffsetListener(c->s, c->result + instructionOffset->value()); } } Promise* promise; Promise* instructionOffset; - bool conditional; }; void -appendOffsetTask(Context* c, Promise* promise, Promise* instructionOffset, - bool conditional) +appendOffsetTask(Context* c, Promise* promise, Promise* instructionOffset) { c->tasks = new (c->zone->allocate(sizeof(OffsetTask))) OffsetTask - (c->tasks, promise, instructionOffset, conditional); + (c->tasks, promise, instructionOffset); } inline unsigned -index(UnaryOperation operation, OperandType operand) +index(ArchitectureContext*, UnaryOperation operation, OperandType operand) { return operation + (UnaryOperationCount * operand); } inline unsigned -index(BinaryOperation operation, +index(ArchitectureContext*, + BinaryOperation operation, OperandType operand1, OperandType operand2) { @@ -371,13 +432,34 @@ index(BinaryOperation operation, + (BinaryOperationCount * OperandTypeCount * operand2); } -inline unsigned -index(TernaryOperation operation, - OperandType operand1) +bool +isBranch(TernaryOperation op) { - return operation + (TernaryOperationCount * operand1); + return op > FloatMin; } +bool +isFloatBranch(TernaryOperation op) +{ + return op > JumpIfNotEqual; +} + +inline unsigned +index(ArchitectureContext* c UNUSED, + TernaryOperation operation, + OperandType operand1) +{ + assert(c, not isBranch(operation)); + + return operation + (NonBranchTernaryOperationCount * operand1); +} + +unsigned +branchIndex(ArchitectureContext* c UNUSED, OperandType operand1, + OperandType operand2) +{ + return operand1 + (OperandTypeCount * operand2); +} // BEGIN OPERATION COMPILERS @@ -387,57 +469,61 @@ using namespace isa; inline void emit(Context* con, int code) { con->code.append4(code); } inline int newTemp(Context* con) { return con->client->acquireTemporary(); } inline void freeTemp(Context* con, int r) { con->client->releaseTemporary(r); } -inline int64_t getValue(Assembler::Constant c) { return c->value->value(); } +inline int64_t getValue(Assembler::Constant* c) { return c->value->value(); } - -void shiftLeftR(Context* con, unsigned size, Assembler::Register a, Assembler::Register b, Assembler::Register t) +inline void +write4(uint8_t* dst, uint32_t v) { - if (size == 8) { - int tmpHi = newTemp(con), tmpLo = newTemp(con); - emit(con, SETS(rsbi(tmpHi, a->low, 32))); - emit(con, lsl(t->high, b->high, a->low)); - emit(con, lsr(tmpLo, b->low, tmpHi)); - emit(con, orr(t->high, t->high, tmpLo)); - emit(con, addi(tmpHi, a->low, -32)); - emit(con, lsl(tmpLo, b->low, tmpHi)); - emit(con, orr(t->high, t->high, tmpLo)); - freeTemp(con, tmpHi); freeTemp(con, tmpLo); - } - emit(con, lsl(t->low, b->low, a->low)); + memcpy(dst, &v, 4); } -void shiftLeftC(Context* con, unsigned size, Assembler::Constant a, Assembler::Register b, Assembler::Register t) +void shiftLeftR(Context* con, unsigned size, Assembler::Register* a, Assembler::Register* b, Assembler::Register* t) +{ + if (size == 8) { + int tmp1 = newTemp(con), tmp2 = newTemp(con); + emit(con, lsl(tmp1, b->high, a->low)); + emit(con, rsbi(tmp2, a->low, 32)); + emit(con, orrsh(tmp1, tmp1, b->low, tmp2, LSR)); + emit(con, SETS(subi(t->high, a->low, 32))); + emit(con, SETCOND(mov(t->high, tmp1), MI)); + emit(con, SETCOND(lsl(t->high, b->low, t->high), PL)); + emit(con, lsl(t->low, b->low, a->low)); + freeTemp(con, tmp1); freeTemp(con, tmp2); + } else { + emit(con, lsl(t->low, b->low, a->low)); + } +} + +void shiftLeftC(Context* con, unsigned size UNUSED, Assembler::Constant* a, Assembler::Register* b, Assembler::Register* t) { assert(con, size == BytesPerWord); emit(con, lsli(t->low, b->low, getValue(a))); } -void shiftRightR(Context* con, unsigned size, Assembler::Register a, Assembler::Register b, Assembler::Register t) +void shiftRightR(Context* con, unsigned size, Assembler::Register* a, Assembler::Register* b, Assembler::Register* t) { if (size == 8) { - int tmpHi = newTemp(con), tmpLo = newTemp(con); - emit(con, SETS(rsbi(tmpHi, a->low, 32))); - emit(con, lsr(t->low, b->low, a->low)); - emit(con, lsl(tmpLo, b->high, tmpHi)); - emit(con, orr(t->low, t->low, tmpLo)); - emit(con, SETS(addi(tmpHi, a->low, -32))); - emit(con, asr(tmpLo, b->high, tmpHi)); - emit(con, SETCOND(b(8), LE)); - emit(con, orri(t->low, tmpLo, 0)); + int tmp1 = newTemp(con), tmp2 = newTemp(con); + emit(con, lsr(tmp1, b->low, a->low)); + emit(con, rsbi(tmp2, a->low, 32)); + emit(con, orrsh(tmp1, tmp1, b->high, tmp2, LSL)); + emit(con, SETS(subi(t->low, a->low, 32))); + emit(con, SETCOND(mov(t->low, tmp1), MI)); + emit(con, SETCOND(asr(t->low, b->high, t->low), PL)); emit(con, asr(t->high, b->high, a->low)); - freeTemp(con, tmpHi); freeTemp(con, tmpLo); + freeTemp(con, tmp1); freeTemp(con, tmp2); } else { emit(con, asr(t->low, b->low, a->low)); } } -void shiftRightC(Context* con, unsigned size, Assembler::Constant a, Assembler::Register b, Assembler::Register t) +void shiftRightC(Context* con, unsigned size UNUSED, Assembler::Constant* a, Assembler::Register* b, Assembler::Register* t) { assert(con, size == BytesPerWord); emit(con, asri(t->low, b->low, getValue(a))); } -void unsignedShiftRightR(Context* con, unsigned size, Assembler::Register a, Assembler::Register b, Assembler::Register t) +void unsignedShiftRightR(Context* con, unsigned size, Assembler::Register* a, Assembler::Register* b, Assembler::Register* t) { emit(con, lsr(t->low, b->low, a->low)); if (size == 8) { @@ -453,81 +539,194 @@ void unsignedShiftRightR(Context* con, unsigned size, Assembler::Register a, Ass } } -void unsignedShiftRightC(Context* con, unsigned size, Assembler::Constant a, Assembler::Register b, Assembler::Register t) +void unsignedShiftRightC(Context* con, unsigned size UNUSED, Assembler::Constant* a, Assembler::Register* b, Assembler::Register* t) { assert(con, size == BytesPerWord); emit(con, lsri(t->low, b->low, getValue(a))); } -void -updateImmediate(System* s, void* dst, int64_t src, unsigned size) -{ - switch (size) { - case 4: { - int32_t* p = static_cast(dst); - int r = (p[0] >> 12) & 15; - - p[0] = movi(r, lo8(src)); - p[1] = orri(r, r, hi8(src), 12); - p[2] = orri(r, r, lo8(hi16(src)), 8); - p[3] = orri(r, r, hi8(hi16(src)), 4); - } break; - - default: abort(s); - } -} - -class ImmediateListener: public Promise::Listener { +class ConstantPoolEntry: public Promise { public: - ImmediateListener(System* s, void* dst, unsigned size, unsigned offset): - s(s), dst(dst), size(size), offset(offset) + ConstantPoolEntry(Context* c, Promise* constant, ConstantPoolEntry* next, + Promise* callOffset): + c(c), constant(constant), next(next), callOffset(callOffset), + address(0) + { } + + virtual int64_t value() { + assert(c, resolved()); + + return reinterpret_cast(address); + } + + virtual bool resolved() { + return address != 0; + } + + Context* c; + Promise* constant; + ConstantPoolEntry* next; + Promise* callOffset; + void* address; + unsigned constantPoolCount; +}; + +class ConstantPoolListener: public Promise::Listener { + public: + ConstantPoolListener(System* s, uintptr_t* address, uint8_t* returnAddress): + s(s), + address(address), + returnAddress(returnAddress) { } virtual bool resolve(int64_t value, void** location) { - updateImmediate(s, dst, value, size); - if (location) *location = static_cast(dst) + offset; - return false; + *address = value; + if (location) { + *location = returnAddress ? static_cast(returnAddress) : address; + } + return true; } System* s; - void* dst; - unsigned size; + uintptr_t* address; + uint8_t* returnAddress; +}; + +class PoolOffset { + public: + PoolOffset(MyBlock* block, ConstantPoolEntry* entry, unsigned offset): + block(block), entry(entry), next(0), offset(offset) + { } + + MyBlock* block; + ConstantPoolEntry* entry; + PoolOffset* next; unsigned offset; }; -class ImmediateTask: public Task { +class PoolEvent { public: - ImmediateTask(Task* next, Promise* promise, Promise* offset, unsigned size, - unsigned promiseOffset): - Task(next), - promise(promise), - offset(offset), - size(size), - promiseOffset(promiseOffset) + PoolEvent(PoolOffset* poolOffsetHead, PoolOffset* poolOffsetTail, + unsigned offset): + poolOffsetHead(poolOffsetHead), poolOffsetTail(poolOffsetTail), next(0), + offset(offset) { } - virtual void run(Context* c) { - if (promise->resolved()) { - updateImmediate - (c->s, c->result + offset->value(), promise->value(), size); - } else { - new (promise->listen(sizeof(ImmediateListener))) ImmediateListener - (c->s, c->result + offset->value(), size, promiseOffset); - } - } - - Promise* promise; - Promise* offset; - unsigned size; - unsigned promiseOffset; + PoolOffset* poolOffsetHead; + PoolOffset* poolOffsetTail; + PoolEvent* next; + unsigned offset; }; void -appendImmediateTask(Context* c, Promise* promise, Promise* offset, - unsigned size, unsigned promiseOffset = 0) +appendConstantPoolEntry(Context* c, Promise* constant, Promise* callOffset) { - c->tasks = new (c->zone->allocate(sizeof(ImmediateTask))) ImmediateTask - (c->tasks, promise, offset, size, promiseOffset); + if (constant->resolved()) { + // make a copy, since the original might be allocated on the + // stack, and we need our copy to live until assembly is complete + constant = new (c->zone->allocate(sizeof(ResolvedPromise))) + ResolvedPromise(constant->value()); + } + + c->constantPool = new (c->zone->allocate(sizeof(ConstantPoolEntry))) + ConstantPoolEntry(c, constant, c->constantPool, callOffset); + + ++ c->constantPoolCount; + + PoolOffset* o = new (c->zone->allocate(sizeof(PoolOffset))) PoolOffset + (c->lastBlock, c->constantPool, c->code.length() - c->lastBlock->offset); + + if (DebugPool) { + fprintf(stderr, "add pool offset %p %d to block %p\n", + o, o->offset, c->lastBlock); + } + + if (c->lastBlock->poolOffsetTail) { + c->lastBlock->poolOffsetTail->next = o; + } else { + c->lastBlock->poolOffsetHead = o; + } + c->lastBlock->poolOffsetTail = o; +} + +void +appendPoolEvent(Context* c, MyBlock* b, unsigned offset, PoolOffset* head, + PoolOffset* tail) +{ + PoolEvent* e = new (c->zone->allocate(sizeof(PoolEvent))) PoolEvent + (head, tail, offset); + + if (b->poolEventTail) { + b->poolEventTail->next = e; + } else { + b->poolEventHead = e; + } + b->poolEventTail = e; +} + +unsigned +padding(MyBlock* b, unsigned offset) +{ + unsigned total = 0; + for (PoolEvent* e = b->poolEventHead; e; e = e->next) { + if (e->offset <= offset) { + total += BytesPerWord; + for (PoolOffset* o = e->poolOffsetHead; o; o = o->next) { + total += BytesPerWord; + } + } else { + break; + } + } + return total; +} + +void +resolve(MyBlock* b) +{ + Context* c = b->context; + + if (b->poolOffsetHead) { + if (c->poolOffsetTail) { + c->poolOffsetTail->next = b->poolOffsetHead; + } else { + c->poolOffsetHead = b->poolOffsetHead; + } + c->poolOffsetTail = b->poolOffsetTail; + } + + if (c->poolOffsetHead) { + bool append; + if (b->next == 0 or b->next->poolEventHead) { + append = true; + } else { + int32_t v = (b->offset + b->size + b->next->size + BytesPerWord - 8) + - (c->poolOffsetHead->offset + c->poolOffsetHead->block->offset); + + append = (v != (v & PoolOffsetMask)); + + if (DebugPool) { + fprintf(stderr, + "offset %p %d is of distance %d to next block; append? %d\n", + c->poolOffsetHead, c->poolOffsetHead->offset, v, append); + } + } + + if (append) { + appendPoolEvent(c, b, b->size, c->poolOffsetHead, c->poolOffsetTail); + + if (DebugPool) { + for (PoolOffset* o = c->poolOffsetHead; o; o = o->next) { + fprintf(stderr, + "include %p %d in pool event %p at offset %d in block %p\n", + o, o->offset, b->poolEventTail, b->size, b); + } + } + + c->poolOffsetHead = 0; + c->poolOffsetTail = 0; + } + } } void @@ -606,7 +805,7 @@ moveZRR(Context* c, unsigned srcSize, Assembler::Register* src, switch (srcSize) { case 2: emit(c, lsli(dst->low, src->low, 16)); - emit(c, lsri(dst->low, src->low, 16)); + emit(c, lsri(dst->low, dst->low, 16)); break; default: abort(c); @@ -615,28 +814,14 @@ moveZRR(Context* c, unsigned srcSize, Assembler::Register* src, void moveCR2(Context* c, unsigned, Assembler::Constant* src, - unsigned dstSize, Assembler::Register* dst, unsigned promiseOffset) + unsigned dstSize, Assembler::Register* dst, Promise* callOffset) { if (dstSize <= 4) { - if (src->value->resolved()) { - int32_t i = getValue(c); - emit(c, movi(dst->low, lo8(i))); - if (!isInt8(i)) { - emit(c, orri(dst->low, dst->low, hi8(i), 12)); - if (!isInt16(i)) { - emit(c, orri(dst->low, dst->low, lo8(hi16(i)), 8)); - if (!isInt24(i)) { - emit(c, orri(dst->low, dst->low, hi8(hi16(i)), 4)); - } - } - } + if (src->value->resolved() and isOfWidth(getValue(src), 8)) { + emit(c, movi(dst->low, lo8(getValue(src)))); } else { - appendImmediateTask - (c, src->value, offset(c), BytesPerWord, promiseOffset); - emit(c, movi(dst->low, 0)); - emit(c, orri(dst->low, dst->low, 0, 12)); - emit(c, orri(dst->low, dst->low, 0, 8)); - emit(c, orri(dst->low, dst->low, 0, 4)); + appendConstantPoolEntry(c, src->value, callOffset); + emit(c, ldri(dst->low, ProgramCounter, 0)); } } else { abort(c); // todo @@ -650,36 +835,16 @@ moveCR(Context* c, unsigned srcSize, Assembler::Constant* src, moveCR2(c, srcSize, src, dstSize, dst, 0); } -void addR(Context* con, unsigned size, Assembler::Register a, Assembler::Register b, Assembler::Register t) { +void addR(Context* con, unsigned size, Assembler::Register* a, Assembler::Register* b, Assembler::Register* t) { if (size == 8) { - emit(con, SETS(addc(t->low, a->low, b->low))); + emit(con, SETS(add(t->low, a->low, b->low))); emit(con, adc(t->high, a->high, b->high)); } else { emit(con, add(t->low, a->low, b->low)); } } -void addC(Context* con, unsigned size, Assembler::Constant a, Assembler::Register b, Assembler::Register t) { - assert(con, size == BytesPerWord); - - int32_t i = getValue(a); - if (i) { - emit(con, addi(t->low, b->low, lo8(i))); - if (!isInt8(i)) { - emit(con, addi(t->low, b->low, hi8(i), 12)); - if (!isInt16(i)) { - emit(con, addi(t->low, b->low, lo8(hi16(i)), 8)); - if (!isInt24(i)) { - emit(con, addi(t->low, b->low, hi8(hi16(i)), 4)); - } - } - } - } else { - moveRR(con, size, b, size, t); - } -} - -void subR(Context* con, unsigned size, Assembler::Register a, Assembler::Register b, Assembler::Register t) { +void subR(Context* con, unsigned size, Assembler::Register* a, Assembler::Register* b, Assembler::Register* t) { if (size == 8) { emit(con, SETS(rsb(t->low, a->low, b->low))); emit(con, rsc(t->high, a->high, b->high)); @@ -688,19 +853,22 @@ void subR(Context* con, unsigned size, Assembler::Register a, Assembler::Registe } } -void subC(Context* c, unsigned size, Assembler::Constant a, Assembler::Register b, Assembler::Register t) { - assert(c, size == BytesPerWord); - - ResolvedPromise promise(- a->value->value()); - Assembler::Constant constant(&promise); - addC(c, size, &constant, b, t); -} - -void multiplyR(Context* con, unsigned size, Assembler::Register a, Assembler::Register b, Assembler::Register t) { +void multiplyR(Context* con, unsigned size, Assembler::Register* a, Assembler::Register* b, Assembler::Register* t) { if (size == 8) { - emit(con, mul(t->high, a->low, b->high)); - emit(con, mla(t->high, a->high, b->low, t->high)); - emit(con, smlal(t->low, t->high, a->low, b->low)); + bool useTemporaries = b->low == t->low; + int tmpLow = useTemporaries ? con->client->acquireTemporary() : t->low; + int tmpHigh = useTemporaries ? con->client->acquireTemporary() : t->high; + + emit(con, umull(tmpLow, tmpHigh, a->low, b->low)); + emit(con, mla(tmpHigh, a->low, b->high, tmpHigh)); + emit(con, mla(tmpHigh, a->high, b->low, tmpHigh)); + + if (useTemporaries) { + emit(con, mov(t->low, tmpLow)); + emit(con, mov(t->high, tmpHigh)); + con->client->releaseTemporary(tmpLow); + con->client->releaseTemporary(tmpHigh); + } } else { emit(con, mul(t->low, a->low, b->low)); } @@ -743,8 +911,10 @@ normalize(Context* c, int offset, int index, unsigned scale, ResolvedPromise offsetPromise(offset); Assembler::Constant offsetConstant(&offsetPromise); - addC(c, BytesPerWord, &offsetConstant, - &untranslatedIndex, &normalizedIndex); + Assembler::Register tmp(c->client->acquireTemporary()); + moveCR(c, BytesPerWord, &offsetConstant, BytesPerWord, &tmp); + addR(c, BytesPerWord, &tmp, &untranslatedIndex, &normalizedIndex); + c->client->releaseTemporary(tmp.low); } return normalizedIndex.low; @@ -786,7 +956,10 @@ store(Context* c, unsigned size, Assembler::Register* src, } if (release) c->client->releaseTemporary(normalized); - } else { + } else if (size == 8 + or abs(offset) == (abs(offset) & 0xFF) + or (size != 2 and abs(offset) == (abs(offset) & 0xFFF))) + { switch (size) { case 1: emit(c, strbi(src->low, base, offset)); @@ -808,6 +981,15 @@ store(Context* c, unsigned size, Assembler::Register* src, default: abort(c); } + } else { + Assembler::Register tmp(c->client->acquireTemporary()); + ResolvedPromise offsetPromise(offset); + Assembler::Constant offsetConstant(&offsetPromise); + moveCR(c, BytesPerWord, &offsetConstant, BytesPerWord, &tmp); + + store(c, size, src, base, 0, tmp.low, 1, false); + + c->client->releaseTemporary(tmp.low); } } @@ -828,7 +1010,7 @@ moveAndUpdateRM(Context* c, unsigned srcSize UNUSED, Assembler::Register* src, assert(c, dstSize == BytesPerWord); if (dst->index == NoRegister) { - emit(c, stri(src->low, dst->base, dst->offset, 1)); + emit(c, stri(src->low, dst->base, dst->offset, dst->offset ? 1 : 0)); } else { assert(c, dst->offset == 0); assert(c, dst->scale == 1); @@ -882,7 +1064,12 @@ load(Context* c, unsigned srcSize, int base, int offset, int index, } if (release) c->client->releaseTemporary(normalized); - } else { + } else if ((srcSize == 8 and dstSize == 8) + or abs(offset) == (abs(offset) & 0xFF) + or (srcSize != 2 + and (srcSize != 1 or not signExtend) + and abs(offset) == (abs(offset) & 0xFFF))) + { switch (srcSize) { case 1: if (signExtend) { @@ -916,6 +1103,15 @@ load(Context* c, unsigned srcSize, int base, int offset, int index, default: abort(c); } + } else { + Assembler::Register tmp(c->client->acquireTemporary()); + ResolvedPromise offsetPromise(offset); + Assembler::Constant offsetConstant(&offsetPromise); + moveCR(c, BytesPerWord, &offsetConstant, BytesPerWord, &tmp); + + load(c, srcSize, base, 0, tmp.low, 1, dstSize, dst, false, signExtend); + + c->client->releaseTemporary(tmp.low); } } @@ -947,16 +1143,47 @@ void andC(Context* c, unsigned size, Assembler::Constant* a, Assembler::Register* b, Assembler::Register* dst) { - assert(con, size == BytesPerWord); + int64_t v = a->value->value(); - int32_t i = getValue(a); - if (i) { - emit(con, andi(t->low, b->low, lo8(i))); - emit(con, andi(t->low, b->low, hi8(i), 12)); - emit(con, andi(t->low, b->low, lo8(hi16(i)), 8)); - emit(con, andi(t->low, b->low, hi8(hi16(i)), 4)); + if (size == 8) { + ResolvedPromise high((v >> 32) & 0xFFFFFFFF); + Assembler::Constant ah(&high); + + ResolvedPromise low(v & 0xFFFFFFFF); + Assembler::Constant al(&low); + + Assembler::Register bh(b->high); + Assembler::Register dh(dst->high); + + andC(c, 4, &al, b, dst); + andC(c, 4, &ah, &bh, &dh); } else { - moveRR(con, size, b, size, t); + uint32_t v32 = static_cast(v); + if (v32 != 0xFFFFFFFF) { + if ((v32 & 0xFFFFFF00) == 0xFFFFFF00) { + emit(c, bici(dst->low, b->low, (~(v32 & 0xFF)) & 0xFF)); + } else if ((v32 & 0xFFFFFF00) == 0) { + emit(c, andi(dst->low, b->low, v32 & 0xFF)); + } else { + // todo: there are other cases we can handle in one + // instruction + + bool useTemporary = b->low == dst->low; + Assembler::Register tmp(dst->low); + if (useTemporary) { + tmp.low = c->client->acquireTemporary(); + } + + moveCR(c, 4, a, 4, &tmp); + andR(c, 4, b, &tmp, dst); + + if (useTemporary) { + c->client->releaseTemporary(tmp.low); + } + } + } else { + moveRR(c, size, b, size, dst); + } } } @@ -964,75 +1191,36 @@ void orR(Context* c, unsigned size, Assembler::Register* a, Assembler::Register* b, Assembler::Register* dst) { - if (size == 8) orr(dst->high, a->high, b->high); + if (size == 8) emit(c, orr(dst->high, a->high, b->high)); emit(c, orr(dst->low, a->low, b->low)); } void -orC(Context* c, unsigned size, Assembler::Constant* a, - Assembler::Register* b, Assembler::Register* dst) +xorR(Context* con, unsigned size, Assembler::Register* a, + Assembler::Register* b, Assembler::Register* dst) { - assert(con, size == BytesPerWord); - - int32_t i = getValue(a); - if (i) { - emit(con, orri(t->low, b->low, lo8(i))); - if (!isInt8(i)) { - emit(con, orri(t->low, b->low, hi8(i), 12)); - if (!isInt16(i)) { - emit(con, orri(t->low, b->low, lo8(hi16(i)), 8)); - if (!isInt24(i)) { - emit(con, orri(t->low, b->low, hi8(hi16(i)), 4)); - } - } - } - } else { - moveRR(con, size, b, size, t); - } + if (size == 8) emit(con, eor(dst->high, a->high, b->high)); + emit(con, eor(dst->low, a->low, b->low)); } void -xorR(Context* com, unsigned size, Assembler::Register* a, - Assembler::Register* b, Assembler::Register* dst) +moveAR2(Context* c, unsigned srcSize, Assembler::Address* src, + unsigned dstSize, Assembler::Register* dst) { - if (size == 8) emit(com, eor(dst->high, a->high, b->high)); - emit(com, eor(dst->low, a->low, b->low)); -} + assert(c, srcSize == 4 and dstSize == 4); -void -xorC(Context* com, unsigned size, Assembler::Constant* a, - Assembler::Register* b, Assembler::Register* dst) -{ - assert(con, size == BytesPerWord); + Assembler::Constant constant(src->address); + moveCR(c, srcSize, &constant, dstSize, dst); - int32_t i = getValue(a); - if (i) { - emit(con, eori(t->low, b->low, lo8(i))); - if (!isInt8(i)) { - emit(con, eori(t->low, b->low, hi8(i), 12)); - if (!isInt16(i)) { - emit(con, eori(t->low, b->low, lo8(hi16(i)), 8)); - if (!isInt24(i)) { - emit(con, eori(t->low, b->low, hi8(hi16(i)), 4)); - } - } - } - } else { - moveRR(con, size, b, size, t); - } + Assembler::Memory memory(dst->low, 0, -1, 0); + moveMR(c, dstSize, &memory, dstSize, dst); } void moveAR(Context* c, unsigned srcSize, Assembler::Address* src, unsigned dstSize, Assembler::Register* dst) { - assert(c, srcSize == 4 and dstSize == 4); - - Assembler::Constant constant(src->address); - Assembler::Memory memory(dst->low, 0, -1, 0); - - moveCR(c, srcSize, &constant, dstSize, dst); - moveMR(c, dstSize, &memory, dstSize, dst); + moveAR2(c, srcSize, src, dstSize, dst); } void @@ -1040,6 +1228,8 @@ compareRR(Context* c, unsigned aSize UNUSED, Assembler::Register* a, unsigned bSize UNUSED, Assembler::Register* b) { assert(c, aSize == 4 and bSize == 4); + assert(c, b->low != a->low); + emit(c, cmp(b->low, a->low)); } @@ -1049,7 +1239,7 @@ compareCR(Context* c, unsigned aSize, Assembler::Constant* a, { assert(c, aSize == 4 and bSize == 4); - if (a->value->resolved() and isInt16(a->value->value())) { + if (a->value->resolved() and isOfWidth(a->value->value(), 8)) { emit(c, cmpi(b->low, a->value->value())); } else { Assembler::Register tmp(c->client->acquireTemporary()); @@ -1083,105 +1273,185 @@ compareRM(Context* c, unsigned aSize, Assembler::Register* a, c->client->releaseTemporary(tmp.low); } -void -longCompare(Context* c, Assembler::Operand* al, Assembler::Operand* ah, - Assembler::Operand* bl, Assembler::Operand* bh, - Assembler::Register* dst, BinaryOperationType compareSigned, - BinaryOperationType compareUnsigned) +int32_t +branch(Context* c, TernaryOperation op) { - ResolvedPromise negativePromise(-1); - Assembler::Constant negative(&negativePromise); + switch (op) { + case JumpIfEqual: + return beq(0); + + case JumpIfNotEqual: + return bne(0); + + case JumpIfLess: + return blt(0); + + case JumpIfGreater: + return bgt(0); + + case JumpIfLessOrEqual: + return ble(0); + + case JumpIfGreaterOrEqual: + return bge(0); + + default: + abort(c); + } +} - ResolvedPromise zeroPromise(0); - Assembler::Constant zero(&zeroPromise); +void +conditional(Context* c, int32_t branch, Assembler::Constant* target) +{ + appendOffsetTask(c, target->value, offset(c)); + emit(c, branch); +} - ResolvedPromise positivePromise(1); - Assembler::Constant positive(&positivePromise); +void +branch(Context* c, TernaryOperation op, Assembler::Constant* target) +{ + conditional(c, branch(c, op), target); +} +void +branchLong(Context* c, TernaryOperation op, Assembler::Operand* al, + Assembler::Operand* ah, Assembler::Operand* bl, + Assembler::Operand* bh, Assembler::Constant* target, + BinaryOperationType compareSigned, + BinaryOperationType compareUnsigned) +{ compareSigned(c, 4, ah, 4, bh); - unsigned less = c->code.length(); - emit(c, blt(0)); + unsigned next = 0; + + switch (op) { + case JumpIfEqual: + next = c->code.length(); + emit(c, bne(0)); - unsigned greater = c->code.length(); - emit(c, bgt(0)); + compareSigned(c, 4, al, 4, bl); + conditional(c, beq(0), target); + break; - compareUnsigned(c, 4, al, 4, bl); + case JumpIfNotEqual: + conditional(c, bne(0), target); - unsigned above = c->code.length(); - emit(c, bgt(0)); + compareSigned(c, 4, al, 4, bl); + conditional(c, bne(0), target); + break; - unsigned below = c->code.length(); - emit(c, blt(0)); + case JumpIfLess: + conditional(c, blt(0), target); - moveCR(c, 4, &zero, 4, dst); + next = c->code.length(); + emit(c, bgt(0)); - unsigned nextFirst = c->code.length(); - emit(c, b(0)); + compareUnsigned(c, 4, al, 4, bl); + conditional(c, blo(0), target); + break; - updateOffset - (c->s, c->code.data + less, true, reinterpret_cast - (c->code.data + c->code.length())); + case JumpIfGreater: + conditional(c, bgt(0), target); - updateOffset - (c->s, c->code.data + below, true, reinterpret_cast - (c->code.data + c->code.length())); + next = c->code.length(); + emit(c, blt(0)); - moveCR(c, 4, &negative, 4, dst); + compareUnsigned(c, 4, al, 4, bl); + conditional(c, bhi(0), target); + break; - unsigned nextSecond = c->code.length(); - emit(c, b(0)); + case JumpIfLessOrEqual: + conditional(c, blt(0), target); - updateOffset - (c->s, c->code.data + greater, true, reinterpret_cast - (c->code.data + c->code.length())); + next = c->code.length(); + emit(c, bgt(0)); - updateOffset - (c->s, c->code.data + above, true, reinterpret_cast - (c->code.data + c->code.length())); + compareUnsigned(c, 4, al, 4, bl); + conditional(c, bls(0), target); + break; - moveCR(c, 4, &positive, 4, dst); + case JumpIfGreaterOrEqual: + conditional(c, bgt(0), target); - updateOffset - (c->s, c->code.data + nextFirst, false, reinterpret_cast - (c->code.data + c->code.length())); + next = c->code.length(); + emit(c, blt(0)); - updateOffset - (c->s, c->code.data + nextSecond, false, reinterpret_cast - (c->code.data + c->code.length())); + compareUnsigned(c, 4, al, 4, bl); + conditional(c, bhs(0), target); + break; + + default: + abort(c); + } + + if (next) { + updateOffset + (c->s, c->code.data + next, reinterpret_cast + (c->code.data + c->code.length())); + } } void -longCompareR(Context* c, unsigned size UNUSED, Assembler::Register* a, - Assembler::Register* b, Assembler::Register* dst) +branchRR(Context* c, TernaryOperation op, unsigned size, + Assembler::Register* a, Assembler::Register* b, + Assembler::Constant* target) { - assert(c, size == 8); - - Assembler::Register ah(a->high); - Assembler::Register bh(b->high); - - longCompare(c, a, &ah, b, &bh, dst, CAST2(compareRR), - CAST2(compareUnsignedRR)); + if (size > BytesPerWord) { + Assembler::Register ah(a->high); + Assembler::Register bh(b->high); + + branchLong(c, op, a, &ah, b, &bh, target, CAST2(compareRR), + CAST2(compareRR)); + } else { + compareRR(c, size, a, size, b); + branch(c, op, target); + } } void -longCompareC(Context* c, unsigned size UNUSED, Assembler::Constant* a, - Assembler::Register* b, Assembler::Register* dst) +branchCR(Context* c, TernaryOperation op, unsigned size, + Assembler::Constant* a, Assembler::Register* b, + Assembler::Constant* target) { - assert(c, size == 8); + if (size > BytesPerWord) { + int64_t v = a->value->value(); - int64_t v = a->value->value(); + ResolvedPromise low(v & ~static_cast(0)); + Assembler::Constant al(&low); - ResolvedPromise low(v & ~static_cast(0)); - Assembler::Constant al(&low); - - ResolvedPromise high((v >> 32) & ~static_cast(0)); - Assembler::Constant ah(&high); - - Assembler::Register bh(b->high); - - longCompare(c, &al, &ah, b, &bh, dst, CAST2(compareCR), - CAST2(compareUnsignedCR)); + ResolvedPromise high((v >> 32) & ~static_cast(0)); + Assembler::Constant ah(&high); + + Assembler::Register bh(b->high); + + branchLong(c, op, &al, &ah, b, &bh, target, CAST2(compareCR), + CAST2(compareCR)); + } else { + compareCR(c, size, a, size, b); + branch(c, op, target); + } +} + +void +branchRM(Context* c, TernaryOperation op, unsigned size, + Assembler::Register* a, Assembler::Memory* b, + Assembler::Constant* target) +{ + assert(c, size <= BytesPerWord); + + compareRM(c, size, a, size, b); + branch(c, op, target); +} + +void +branchCM(Context* c, TernaryOperation op, unsigned size, + Assembler::Constant* a, Assembler::Memory* b, + Assembler::Constant* target) +{ + assert(c, size <= BytesPerWord); + + compareCM(c, size, a, size, b); + branch(c, op, target); } ShiftMaskPromise* @@ -1243,7 +1513,7 @@ callC(Context* c, unsigned size UNUSED, Assembler::Constant* target) { assert(c, size == BytesPerWord); - appendOffsetTask(c, target->value, offset(c), false); + appendOffsetTask(c, target->value, offset(c)); emit(c, bl(0)); } @@ -1252,8 +1522,8 @@ longCallC(Context* c, unsigned size UNUSED, Assembler::Constant* target) { assert(c, size == BytesPerWord); - Assembler::Register tmp(0); - moveCR2(c, BytesPerWord, target, BytesPerWord, &tmp, 12); + Assembler::Register tmp(4); + moveCR2(c, BytesPerWord, target, BytesPerWord, &tmp, offset(c)); callR(c, BytesPerWord, &tmp); } @@ -1262,8 +1532,8 @@ longJumpC(Context* c, unsigned size UNUSED, Assembler::Constant* target) { assert(c, size == BytesPerWord); - Assembler::Register tmp(0); - moveCR2(c, BytesPerWord, target, BytesPerWord, &tmp, 12); + Assembler::Register tmp(4); // a non-arg reg that we don't mind clobbering + moveCR2(c, BytesPerWord, target, BytesPerWord, &tmp, offset(c)); jumpR(c, BytesPerWord, &tmp); } @@ -1272,74 +1542,18 @@ jumpC(Context* c, unsigned size UNUSED, Assembler::Constant* target) { assert(c, size == BytesPerWord); - appendOffsetTask(c, target->value, offset(c), false); + appendOffsetTask(c, target->value, offset(c)); emit(c, b(0)); } -void -jumpIfEqualC(Context* c, unsigned size UNUSED, Assembler::Constant* target) -{ - assert(c, size == BytesPerWord); - - appendOffsetTask(c, target->value, offset(c), true); - emit(c, SETCOND(b(0), EQ)); -} - -void -jumpIfNotEqualC(Context* c, unsigned size UNUSED, Assembler::Constant* target) -{ - assert(c, size == BytesPerWord); - - appendOffsetTask(c, target->value, offset(c), true); - emit(c, SETCOND(b(0), NE)); -} - -void -jumpIfGreaterC(Context* c, unsigned size UNUSED, Assembler::Constant* target) -{ - assert(c, size == BytesPerWord); - - appendOffsetTask(c, target->value, offset(c), true); - emit(c, SETCOND(b(0), GT)); -} - -void -jumpIfGreaterOrEqualC(Context* c, unsigned size UNUSED, - Assembler::Constant* target) -{ - assert(c, size == BytesPerWord); - - appendOffsetTask(c, target->value, offset(c), true); - emit(c, SETCOND(b(0), GE)); -} - -void -jumpIfLessC(Context* c, unsigned size UNUSED, Assembler::Constant* target) -{ - assert(c, size == BytesPerWord); - - appendOffsetTask(c, target->value, offset(c), true); - emit(c, SETCOND(b(0), LS)); -} - -void -jumpIfLessOrEqualC(Context* c, unsigned size UNUSED, - Assembler::Constant* target) -{ - assert(c, size == BytesPerWord); - - appendOffsetTask(c, target->value, offset(c), true); - emit(c, SETCOND(b(0), LE)); -} - void return_(Context* c) { - emit(c, mov(15, 14)); + emit(c, bx(LinkRegister)); } void -memoryBarrier(Context* c) {} +memoryBarrier(Context*) {} // END OPERATION COMPILERS @@ -1355,96 +1569,90 @@ populateTables(ArchitectureContext* c) UnaryOperationType* uo = c->unaryOperations; BinaryOperationType* bo = c->binaryOperations; TernaryOperationType* to = c->ternaryOperations; + BranchOperationType* bro = c->branchOperations; zo[Return] = return_; zo[LoadBarrier] = memoryBarrier; zo[StoreStoreBarrier] = memoryBarrier; zo[StoreLoadBarrier] = memoryBarrier; - uo[index(LongCall, C)] = CAST1(longCallC); + uo[index(c, LongCall, C)] = CAST1(longCallC); - uo[index(LongJump, C)] = CAST1(longJumpC); + uo[index(c, AlignedLongCall, C)] = CAST1(longCallC); - uo[index(Jump, R)] = CAST1(jumpR); - uo[index(Jump, C)] = CAST1(jumpC); + uo[index(c, LongJump, C)] = CAST1(longJumpC); - uo[index(AlignedJump, R)] = CAST1(jumpR); - uo[index(AlignedJump, C)] = CAST1(jumpC); + uo[index(c, AlignedLongJump, C)] = CAST1(longJumpC); - uo[index(JumpIfEqual, C)] = CAST1(jumpIfEqualC); - uo[index(JumpIfNotEqual, C)] = CAST1(jumpIfNotEqualC); - uo[index(JumpIfGreater, C)] = CAST1(jumpIfGreaterC); - uo[index(JumpIfGreaterOrEqual, C)] = CAST1(jumpIfGreaterOrEqualC); - uo[index(JumpIfLess, C)] = CAST1(jumpIfLessC); - uo[index(JumpIfLessOrEqual, C)] = CAST1(jumpIfLessOrEqualC); + uo[index(c, Jump, R)] = CAST1(jumpR); + uo[index(c, Jump, C)] = CAST1(jumpC); - uo[index(Call, C)] = CAST1(callC); - uo[index(Call, R)] = CAST1(callR); + uo[index(c, AlignedJump, R)] = CAST1(jumpR); + uo[index(c, AlignedJump, C)] = CAST1(jumpC); - uo[index(AlignedCall, C)] = CAST1(callC); - uo[index(AlignedCall, R)] = CAST1(callR); + uo[index(c, Call, C)] = CAST1(callC); + uo[index(c, Call, R)] = CAST1(callR); - bo[index(Move, R, R)] = CAST2(moveRR); - bo[index(Move, C, R)] = CAST2(moveCR); - bo[index(Move, C, M)] = CAST2(moveCM); - bo[index(Move, M, R)] = CAST2(moveMR); - bo[index(Move, R, M)] = CAST2(moveRM); - bo[index(Move, A, R)] = CAST2(moveAR); + uo[index(c, AlignedCall, C)] = CAST1(callC); + uo[index(c, AlignedCall, R)] = CAST1(callR); - bo[index(MoveZ, R, R)] = CAST2(moveZRR); - bo[index(MoveZ, M, R)] = CAST2(moveZMR); - bo[index(MoveZ, C, R)] = CAST2(moveCR); + bo[index(c, Move, R, R)] = CAST2(moveRR); + bo[index(c, Move, C, R)] = CAST2(moveCR); + bo[index(c, Move, C, M)] = CAST2(moveCM); + bo[index(c, Move, M, R)] = CAST2(moveMR); + bo[index(c, Move, R, M)] = CAST2(moveRM); + bo[index(c, Move, A, R)] = CAST2(moveAR); - bo[index(Compare, R, R)] = CAST2(compareRR); - bo[index(Compare, C, R)] = CAST2(compareCR); - bo[index(Compare, R, M)] = CAST2(compareRM); - bo[index(Compare, C, M)] = CAST2(compareCM); + bo[index(c, MoveZ, R, R)] = CAST2(moveZRR); + bo[index(c, MoveZ, M, R)] = CAST2(moveZMR); + bo[index(c, MoveZ, C, R)] = CAST2(moveCR); - bo[index(Negate, R, R)] = CAST2(negateRR); + bo[index(c, Negate, R, R)] = CAST2(negateRR); - to[index(Add, R)] = CAST3(addR); - to[index(Add, C)] = CAST3(addC); + to[index(c, Add, R)] = CAST3(addR); - to[index(Subtract, R)] = CAST3(subR); - to[index(Subtract, C)] = CAST3(subC); + to[index(c, Subtract, R)] = CAST3(subR); - to[index(Multiply, R)] = CAST3(multiplyR); + to[index(c, Multiply, R)] = CAST3(multiplyR); - to[index(Divide, R)] = CAST3(divideR); + to[index(c, ShiftLeft, R)] = CAST3(shiftLeftR); + to[index(c, ShiftLeft, C)] = CAST3(shiftLeftC); - to[index(Remainder, R)] = CAST3(remainderR); + to[index(c, ShiftRight, R)] = CAST3(shiftRightR); + to[index(c, ShiftRight, C)] = CAST3(shiftRightC); - to[index(ShiftLeft, R)] = CAST3(shiftLeftR); - to[index(ShiftLeft, C)] = CAST3(shiftLeftC); + to[index(c, UnsignedShiftRight, R)] = CAST3(unsignedShiftRightR); + to[index(c, UnsignedShiftRight, C)] = CAST3(unsignedShiftRightC); - to[index(ShiftRight, R)] = CAST3(shiftRightR); - to[index(ShiftRight, C)] = CAST3(shiftRightC); + to[index(c, And, R)] = CAST3(andR); + to[index(c, And, C)] = CAST3(andC); - to[index(UnsignedShiftRight, R)] = CAST3(unsignedShiftRightR); - to[index(UnsignedShiftRight, C)] = CAST3(unsignedShiftRightC); + to[index(c, Or, R)] = CAST3(orR); - to[index(And, C)] = CAST3(andC); - to[index(And, R)] = CAST3(andR); + to[index(c, Xor, R)] = CAST3(xorR); - to[index(Or, C)] = CAST3(orC); - to[index(Or, R)] = CAST3(orR); - - to[index(Xor, C)] = CAST3(xorC); - to[index(Xor, R)] = CAST3(xorR); - - to[index(LongCompare, R)] = CAST3(longCompareR); - to[index(LongCompare, C)] = CAST3(longCompareC); + bro[branchIndex(c, R, R)] = CAST_BRANCH(branchRR); + bro[branchIndex(c, C, R)] = CAST_BRANCH(branchCR); + bro[branchIndex(c, C, M)] = CAST_BRANCH(branchCM); + bro[branchIndex(c, R, M)] = CAST_BRANCH(branchRM); } -// TODO class MyArchitecture: public Assembler::Architecture { public: MyArchitecture(System* system): c(system), referenceCount(0) { populateTables(&c); } - virtual unsigned registerCount() { - return 16; + virtual unsigned floatRegisterSize() { + return 0; + } + + virtual uint32_t generalRegisterMask() { + return 0xFFFF; + } + + virtual uint32_t floatRegisterMask() { + return 0; } virtual int stack() { @@ -1456,11 +1664,11 @@ class MyArchitecture: public Assembler::Architecture { } virtual int returnLow() { - return 4; + return 0; } virtual int returnHigh() { - return (BytesPerWord == 4 ? 3 : NoRegister); + return 1; } virtual int virtualCallTarget() { @@ -1471,19 +1679,20 @@ class MyArchitecture: public Assembler::Architecture { return 3; } - virtual bool condensedAddressing() { - return false; - } - virtual bool bigEndian() { return false; } + virtual uintptr_t maximumImmediateJump() { + return 0x1FFFFFF; + } + virtual bool reserved(int register_) { switch (register_) { + case LinkRegister: case StackRegister: case ThreadRegister: - case 15: + case ProgramCounter: return true; default: @@ -1506,9 +1715,9 @@ class MyArchitecture: public Assembler::Architecture { virtual int argumentRegister(unsigned index) { assert(&c, index < argumentRegisterCount()); - return index + 0; + return index; } - + virtual unsigned stackAlignmentInWords() { return StackAlignmentInWords; } @@ -1522,20 +1731,25 @@ class MyArchitecture: public Assembler::Architecture { } virtual void updateCall(UnaryOperation op UNUSED, - bool assertAlignment UNUSED, void* returnAddress, + void* returnAddress, void* newTarget) { switch (op) { case Call: - case Jump: { - updateOffset(c.s, static_cast(returnAddress) - 4, false, + case Jump: + case AlignedCall: + case AlignedJump: { + updateOffset(c.s, static_cast(returnAddress) - 4, reinterpret_cast(newTarget)); } break; case LongCall: - case LongJump: { - updateImmediate(c.s, static_cast(returnAddress) - 12, - reinterpret_cast(newTarget), BytesPerWord); + case LongJump: + case AlignedLongCall: + case AlignedLongJump: { + uint32_t* p = static_cast(returnAddress) - 2; + *reinterpret_cast(p + (((*p & PoolOffsetMask) + 8) / 4)) + = newTarget; } break; default: abort(&c); @@ -1546,13 +1760,8 @@ class MyArchitecture: public Assembler::Architecture { return 4; } - virtual uintptr_t getConstant(const void* src) { - const int32_t* p = static_cast(src); - return (p[0] << 16) | (p[1] & 0xFFFF); - } - virtual void setConstant(void* dst, uintptr_t constant) { - updateImmediate(c.s, dst, constant, BytesPerWord); + *static_cast(dst) = constant; } virtual unsigned alignFrameSize(unsigned sizeInWords) { @@ -1561,11 +1770,11 @@ class MyArchitecture: public Assembler::Architecture { } virtual void* frameIp(void* stack) { - return stack ? static_cast(stack)[2] : 0; + return stack ? static_cast(stack)[returnAddressOffset()] : 0; } virtual unsigned frameHeaderSize() { - return 0; + return FrameHeaderSize; } virtual unsigned frameReturnAddressSize() { @@ -1577,7 +1786,7 @@ class MyArchitecture: public Assembler::Architecture { } virtual int returnAddressOffset() { - return 8 / BytesPerWord; + return 1; } virtual int framePointerOffset() { @@ -1590,6 +1799,22 @@ class MyArchitecture: public Assembler::Architecture { *stack = *static_cast(*stack); } + virtual BinaryOperation hasBinaryIntrinsic(Thread*, object) { + return NoBinaryOperation; + } + + virtual TernaryOperation hasTernaryIntrinsic(Thread*, object) { + return NoTernaryOperation; + } + + virtual bool alwaysCondensed(BinaryOperation) { + return false; + } + + virtual bool alwaysCondensed(TernaryOperation) { + return false; + } + virtual void plan (UnaryOperation, unsigned, uint8_t* aTypeMask, uint64_t* aRegisterMask, @@ -1600,28 +1825,46 @@ class MyArchitecture: public Assembler::Architecture { *thunk = false; } - virtual void plan + virtual void planSource (BinaryOperation op, unsigned, uint8_t* aTypeMask, uint64_t* aRegisterMask, - unsigned, uint8_t* bTypeMask, uint64_t* bRegisterMask, - bool* thunk) + unsigned, bool* thunk) { *aTypeMask = ~0; *aRegisterMask = ~static_cast(0); - *bTypeMask = (1 << RegisterOperand) | (1 << MemoryOperand); - *bRegisterMask = ~static_cast(0); - *thunk = false; switch (op) { - case Compare: - *aTypeMask = (1 << RegisterOperand) | (1 << ConstantOperand); - *bTypeMask = (1 << RegisterOperand); - break; - case Negate: *aTypeMask = (1 << RegisterOperand); + break; + + case Absolute: + case FloatAbsolute: + case FloatSquareRoot: + case FloatNegate: + case Float2Float: + case Float2Int: + case Int2Float: + *thunk = true; + break; + + default: + break; + } + } + + virtual void planDestination + (BinaryOperation op, + unsigned, uint8_t, uint64_t, + unsigned, uint8_t* bTypeMask, uint64_t* bRegisterMask) + { + *bTypeMask = (1 << RegisterOperand) | (1 << MemoryOperand); + *bRegisterMask = ~static_cast(0); + + switch (op) { + case Negate: *bTypeMask = (1 << RegisterOperand); break; @@ -1630,12 +1873,30 @@ class MyArchitecture: public Assembler::Architecture { } } - virtual void plan + virtual void planMove + (unsigned, uint8_t* srcTypeMask, uint64_t* srcRegisterMask, + uint8_t* tmpTypeMask, uint64_t* tmpRegisterMask, + uint8_t dstTypeMask, uint64_t) + { + *srcTypeMask = ~0; + *srcRegisterMask = ~static_cast(0); + + *tmpTypeMask = 0; + *tmpRegisterMask = 0; + + if (dstTypeMask & (1 << MemoryOperand)) { + // can't move directly from memory or constant to memory + *srcTypeMask = 1 << RegisterOperand; + *tmpTypeMask = 1 << RegisterOperand; + *tmpRegisterMask = ~static_cast(0); + } + } + + virtual void planSource (TernaryOperation op, - unsigned aSize, uint8_t* aTypeMask, uint64_t* aRegisterMask, - unsigned, uint8_t* bTypeMask, uint64_t* bRegisterMask, - unsigned, uint8_t* cTypeMask, uint64_t* cRegisterMask, - bool* thunk) + unsigned, uint8_t* aTypeMask, uint64_t* aRegisterMask, + unsigned bSize, uint8_t* bTypeMask, uint64_t* bRegisterMask, + unsigned, bool* thunk) { *aTypeMask = (1 << RegisterOperand) | (1 << ConstantOperand); *aRegisterMask = ~static_cast(0); @@ -1646,33 +1907,58 @@ class MyArchitecture: public Assembler::Architecture { *thunk = false; switch (op) { - case Add: - case Subtract: - if (aSize == 8) { - *aTypeMask = *bTypeMask = (1 << RegisterOperand); - } + case ShiftLeft: + case ShiftRight: + case UnsignedShiftRight: + if (bSize == 8) *aTypeMask = *bTypeMask = (1 << RegisterOperand); break; + case Add: + case Subtract: + case Or: + case Xor: case Multiply: *aTypeMask = *bTypeMask = (1 << RegisterOperand); break; - case LongCompare: - *bTypeMask = (1 << RegisterOperand); - break; - case Divide: case Remainder: - *bTypeMask = ~0; + case FloatAdd: + case FloatSubtract: + case FloatMultiply: + case FloatDivide: + case FloatRemainder: + case JumpIfFloatEqual: + case JumpIfFloatNotEqual: + case JumpIfFloatLess: + case JumpIfFloatGreater: + case JumpIfFloatLessOrEqual: + case JumpIfFloatGreaterOrEqual: + case JumpIfFloatLessOrUnordered: + case JumpIfFloatGreaterOrUnordered: + case JumpIfFloatLessOrEqualOrUnordered: + case JumpIfFloatGreaterOrEqualOrUnordered: *thunk = true; break; default: break; } + } - *cTypeMask = *bTypeMask; - *cRegisterMask = *bRegisterMask; + virtual void planDestination + (TernaryOperation op, + unsigned, uint8_t, uint64_t, + unsigned, uint8_t, const uint64_t, + unsigned, uint8_t* cTypeMask, uint64_t* cRegisterMask) + { + if (isBranch(op)) { + *cTypeMask = (1 << ConstantOperand); + *cRegisterMask = 0; + } else { + *cTypeMask = (1 << RegisterOperand); + *cRegisterMask = ~static_cast(0); + } } virtual void acquire() { @@ -1705,6 +1991,11 @@ class MyAssembler: public Assembler { } virtual void saveFrame(unsigned stackOffset, unsigned) { + Register returnAddress(LinkRegister); + Memory returnAddressDst + (StackRegister, arch_->returnAddressOffset() * BytesPerWord); + moveRM(&c, BytesPerWord, &returnAddress, BytesPerWord, &returnAddressDst); + Register stack(StackRegister); Memory stackDst(ThreadRegister, stackOffset); moveRM(&c, BytesPerWord, &stack, BytesPerWord, &stackDst); @@ -1752,10 +2043,9 @@ class MyAssembler: public Assembler { } virtual void allocateFrame(unsigned footprint) { - Register returnAddress(0); - emit(&c, mov(returnAddress.low, 14)); + Register returnAddress(LinkRegister); - Memory returnAddressDst(StackRegister, 8); + Memory returnAddressDst(StackRegister, arch_->returnAddressOffset() * BytesPerWord); moveRM(&c, BytesPerWord, &returnAddress, BytesPerWord, &returnAddressDst); Register stack(StackRegister); @@ -1764,7 +2054,7 @@ class MyAssembler: public Assembler { } virtual void adjustFrame(unsigned footprint) { - Register nextStack(0); + Register nextStack(5); Memory stackSrc(StackRegister, 0); moveMR(&c, BytesPerWord, &stackSrc, BytesPerWord, &nextStack); @@ -1774,14 +2064,12 @@ class MyAssembler: public Assembler { virtual void popFrame() { Register stack(StackRegister); - Memory stackSrc(StackRegister, 0); + Memory stackSrc(StackRegister, arch_->framePointerOffset() * BytesPerWord); moveMR(&c, BytesPerWord, &stackSrc, BytesPerWord, &stack); - Register returnAddress(0); - Memory returnAddressSrc(StackRegister, 8); + Register returnAddress(LinkRegister); + Memory returnAddressSrc(StackRegister, arch_->returnAddressOffset() * BytesPerWord); moveMR(&c, BytesPerWord, &returnAddressSrc, BytesPerWord, &returnAddress); - - emit(&c, mov(14, returnAddress.low)); } virtual void popFrameForTailCall(unsigned footprint, @@ -1791,23 +2079,25 @@ class MyAssembler: public Assembler { { if (TailCalls) { if (offset) { - Register tmp(0); - Memory returnAddressSrc(StackRegister, 8 + (footprint * BytesPerWord)); - moveMR(&c, BytesPerWord, &returnAddressSrc, BytesPerWord, &tmp); + Register link(LinkRegister); + Memory returnAddressSrc + (StackRegister, BytesPerWord + (footprint * BytesPerWord)); + moveMR(&c, BytesPerWord, &returnAddressSrc, BytesPerWord, &link); - emit(&c, mov(14, tmp.low)); - + Register tmp(c.client->acquireTemporary()); Memory stackSrc(StackRegister, footprint * BytesPerWord); moveMR(&c, BytesPerWord, &stackSrc, BytesPerWord, &tmp); Memory stackDst(StackRegister, (footprint - offset) * BytesPerWord); moveAndUpdateRM(&c, BytesPerWord, &tmp, BytesPerWord, &stackDst); + c.client->releaseTemporary(tmp.low); + if (returnAddressSurrogate != NoRegister) { assert(&c, offset > 0); Register ras(returnAddressSurrogate); - Memory dst(StackRegister, 8 + (offset * BytesPerWord)); + Memory dst(StackRegister, BytesPerWord + (offset * BytesPerWord)); moveRM(&c, BytesPerWord, &ras, BytesPerWord, &dst); } @@ -1833,7 +2123,7 @@ class MyAssembler: public Assembler { assert(&c, (argumentFootprint % StackAlignmentInWords) == 0); if (TailCalls and argumentFootprint > StackAlignmentInWords) { - Register tmp(0); + Register tmp(5); Memory stackSrc(StackRegister, 0); moveMR(&c, BytesPerWord, &stackSrc, BytesPerWord, &tmp); @@ -1850,7 +2140,7 @@ class MyAssembler: public Assembler { { popFrame(); - Register tmp1(0); + Register tmp1(6); Memory stackSrc(StackRegister, 0); moveMR(&c, BytesPerWord, &stackSrc, BytesPerWord, &tmp1); @@ -1874,42 +2164,111 @@ class MyAssembler: public Assembler { virtual void apply(UnaryOperation op, unsigned aSize, OperandType aType, Operand* aOperand) { - arch_->c.unaryOperations[index(op, aType)](&c, aSize, aOperand); + arch_->c.unaryOperations[index(&(arch_->c), op, aType)] + (&c, aSize, aOperand); } virtual void apply(BinaryOperation op, unsigned aSize, OperandType aType, Operand* aOperand, unsigned bSize, OperandType bType, Operand* bOperand) { - arch_->c.binaryOperations[index(op, aType, bType)] + arch_->c.binaryOperations[index(&(arch_->c), op, aType, bType)] (&c, aSize, aOperand, bSize, bOperand); } virtual void apply(TernaryOperation op, - unsigned, OperandType aType, Operand* aOperand, + unsigned aSize, OperandType aType, Operand* aOperand, unsigned bSize, OperandType bType UNUSED, Operand* bOperand, unsigned cSize UNUSED, OperandType cType UNUSED, Operand* cOperand) { - assert(&c, bSize == cSize); - assert(&c, bType == RegisterOperand); - assert(&c, cType == RegisterOperand); + if (isBranch(op)) { + assert(&c, aSize == bSize); + assert(&c, cSize == BytesPerWord); + assert(&c, cType == ConstantOperand); - arch_->c.ternaryOperations[index(op, aType)] - (&c, bSize, aOperand, bOperand, cOperand); + arch_->c.branchOperations[branchIndex(&(arch_->c), aType, bType)] + (&c, op, aSize, aOperand, bOperand, cOperand); + } else { + assert(&c, bSize == cSize); + assert(&c, bType == RegisterOperand); + assert(&c, cType == RegisterOperand); + + arch_->c.ternaryOperations[index(&(arch_->c), op, aType)] + (&c, bSize, aOperand, bOperand, cOperand); + } } virtual void writeTo(uint8_t* dst) { c.result = dst; + unsigned dstOffset = 0; for (MyBlock* b = c.firstBlock; b; b = b->next) { - memcpy(dst + b->start, c.code.data + b->offset, b->size); + if (DebugPool) { + fprintf(stderr, "write block %p\n", b); + } + + unsigned blockOffset = 0; + for (PoolEvent* e = b->poolEventHead; e; e = e->next) { + unsigned size = e->offset - blockOffset; + memcpy(dst + dstOffset, c.code.data + b->offset + blockOffset, size); + blockOffset = e->offset; + dstOffset += size; + + unsigned poolSize = 0; + for (PoolOffset* o = e->poolOffsetHead; o; o = o->next) { + if (DebugPool) { + fprintf(stderr, "visit pool offset %p %d in block %p\n", + o, o->offset, b); + } + + poolSize += BytesPerWord; + + unsigned entry = dstOffset + poolSize; + + o->entry->address = dst + entry; + + unsigned instruction = o->block->start + + padding(o->block, o->offset) + o->offset; + + int32_t v = (entry - 8) - instruction; + expect(&c, v == (v & PoolOffsetMask)); + + int32_t* p = reinterpret_cast(dst + instruction); + *p = (v & PoolOffsetMask) | ((~PoolOffsetMask) & *p); + } + + write4(dst + dstOffset, ::b((poolSize + BytesPerWord - 8) >> 2)); + + dstOffset += poolSize + BytesPerWord; + } + + unsigned size = b->size - blockOffset; + + memcpy(dst + dstOffset, + c.code.data + b->offset + blockOffset, + size); + + dstOffset += size; } - + for (Task* t = c.tasks; t; t = t->next) { t->run(&c); } + + for (ConstantPoolEntry* e = c.constantPool; e; e = e->next) { + if (e->constant->resolved()) { + *static_cast(e->address) = e->constant->value(); + } else { + new (e->constant->listen(sizeof(ConstantPoolListener))) + ConstantPoolListener(c.s, static_cast(e->address), + e->callOffset + ? dst + e->callOffset->value() + 8 + : 0); + } +// fprintf(stderr, "constant %p at %p\n", reinterpret_cast(e->constant->value()), e->address); + } } virtual Promise* offset() { @@ -1921,13 +2280,47 @@ class MyAssembler: public Assembler { b->size = c.code.length() - b->offset; if (startNew) { c.lastBlock = new (c.zone->allocate(sizeof(MyBlock))) - MyBlock(c.code.length()); + MyBlock(&c, c.code.length()); } else { c.lastBlock = 0; } return b; } + virtual void endEvent() { + MyBlock* b = c.lastBlock; + unsigned thisEventOffset = c.code.length() - b->offset; + if (b->poolOffsetHead) { + int32_t v = (thisEventOffset + BytesPerWord - 8) + - b->poolOffsetHead->offset; + + if (v > 0 and v != (v & PoolOffsetMask)) { + appendPoolEvent + (&c, b, b->lastEventOffset, b->poolOffsetHead, + b->lastPoolOffsetTail); + + if (DebugPool) { + for (PoolOffset* o = b->poolOffsetHead; + o != b->lastPoolOffsetTail->next; o = o->next) + { + fprintf(stderr, + "in endEvent, include %p %d in pool event %p at offset %d " + "in block %p\n", + o, o->offset, b->poolEventTail, b->lastEventOffset, b); + } + } + + b->poolOffsetHead = b->lastPoolOffsetTail->next; + b->lastPoolOffsetTail->next = 0; + if (b->poolOffsetHead == 0) { + b->poolOffsetTail = 0; + } + } + } + b->lastEventOffset = thisEventOffset; + b->lastPoolOffsetTail = b->poolOffsetTail; + } + virtual unsigned length() { return c.code.length(); } @@ -1945,7 +2338,7 @@ class MyAssembler: public Assembler { namespace vm { Assembler::Architecture* -makeArchitecture(System* system) +makeArchitecture(System* system, bool) { return new (allocate(system, sizeof(MyArchitecture))) MyArchitecture(system); } diff --git a/src/arm.h b/src/arm.h index b7b844685d..f510d3e960 100644 --- a/src/arm.h +++ b/src/arm.h @@ -27,7 +27,7 @@ namespace vm { inline void trap() { - asm("nop"); + asm("bkpt"); } inline void diff --git a/src/assembler.h b/src/assembler.h index 918c548acb..70002768ca 100644 --- a/src/assembler.h +++ b/src/assembler.h @@ -418,9 +418,9 @@ class Assembler { virtual Block* endBlock(bool startNew) = 0; - virtual unsigned length() = 0; + virtual void endEvent() = 0; - virtual unsigned scratchSize() = 0; + virtual unsigned length() = 0; virtual void dispose() = 0; }; diff --git a/src/binaryToObject/elf.cpp b/src/binaryToObject/elf.cpp index 8a6c2417d1..2e58f08e27 100644 --- a/src/binaryToObject/elf.cpp +++ b/src/binaryToObject/elf.cpp @@ -232,7 +232,7 @@ writeObject(const uint8_t* data, unsigned size, FILE* out, fileHeader.e_entry = 0; fileHeader.e_phoff = 0; fileHeader.e_shoff = sizeof(FileHeader); - fileHeader.e_flags = 0; + fileHeader.e_flags = (machine == EM_ARM ? 0x04000000 : 0); fileHeader.e_ehsize = sizeof(FileHeader); fileHeader.e_phentsize = 0; fileHeader.e_phnum = 0; diff --git a/src/binaryToObject/mach-o.cpp b/src/binaryToObject/mach-o.cpp index d153694b5c..527130a8db 100644 --- a/src/binaryToObject/mach-o.cpp +++ b/src/binaryToObject/mach-o.cpp @@ -12,6 +12,32 @@ #include "stdio.h" #include "string.h" +#define V1(v) v + +#ifdef OPPOSITE_ENDIAN +# define V2(v) \ + ((((v) >> 8) & 0xFF) | \ + (((v) << 8))) +# define V4(v) \ + ((((v) >> 24) & 0x000000FF) | \ + (((v) >> 8) & 0x0000FF00) | \ + (((v) << 8) & 0x00FF0000) | \ + (((v) << 24))) +# define V8(v) \ + (((static_cast(v) >> 56) & UINT64_C(0x00000000000000FF)) | \ + ((static_cast(v) >> 40) & UINT64_C(0x000000000000FF00)) | \ + ((static_cast(v) >> 24) & UINT64_C(0x0000000000FF0000)) | \ + ((static_cast(v) >> 8) & UINT64_C(0x00000000FF000000)) | \ + ((static_cast(v) << 8) & UINT64_C(0x000000FF00000000)) | \ + ((static_cast(v) << 24) & UINT64_C(0x0000FF0000000000)) | \ + ((static_cast(v) << 40) & UINT64_C(0x00FF000000000000)) | \ + ((static_cast(v) << 56))) +#else +# define V2(v) v +# define V4(v) v +# define V8(v) v +#endif + #define MH_MAGIC_64 0xfeedfacf #define MH_MAGIC 0xfeedface @@ -37,6 +63,7 @@ #define CPU_SUBTYPE_POWERPC_ALL 0 #if (BITS_PER_WORD == 64) +# define VW(v) V8(v) # define Magic MH_MAGIC_64 # define Segment LC_SEGMENT_64 # define FileHeader mach_header_64 @@ -44,6 +71,7 @@ # define Section section_64 # define NList struct nlist_64 #elif (BITS_PER_WORD == 32) +# define VW(v) V4(v) # define Magic MH_MAGIC # define Segment LC_SEGMENT # define FileHeader mach_header @@ -191,32 +219,32 @@ writeObject(const uint8_t* data, unsigned size, FILE* out, unsigned endNameLength = strlen(endName) + 1; FileHeader header = { - Magic, // magic - cpuType, - cpuSubType, - MH_OBJECT, // filetype, - 2, // ncmds - sizeof(SegmentCommand) - + sizeof(Section) - + sizeof(symtab_command), // sizeofcmds - 0 // flags + V4(Magic), // magic + V4(cpuType), + V4(cpuSubType), + V4(MH_OBJECT), // filetype, + V4(2), // ncmds + V4(sizeof(SegmentCommand) + + sizeof(Section) + + sizeof(symtab_command)), // sizeofcmds + V4(0) // flags }; SegmentCommand segment = { - Segment, // cmd - sizeof(SegmentCommand) + sizeof(Section), // cmdsize + V4(Segment), // cmd + V4(sizeof(SegmentCommand) + sizeof(Section)), // cmdsize "", // segname - 0, // vmaddr - pad(size), // vmsize - sizeof(FileHeader) - + sizeof(SegmentCommand) - + sizeof(Section) - + sizeof(symtab_command), // fileoff - pad(size), // filesize - 7, // maxprot - 7, // initprot - 1, // nsects - 0 // flags + VW(0), // vmaddr + VW(pad(size)), // vmsize + VW(sizeof(FileHeader) + + sizeof(SegmentCommand) + + sizeof(Section) + + sizeof(symtab_command)), // fileoff + VW(pad(size)), // filesize + V4(7), // maxprot + V4(7), // initprot + V4(1), // nsects + V4(0) // flags }; strncpy(segment.segname, segmentName, sizeof(segment.segname)); @@ -224,55 +252,55 @@ writeObject(const uint8_t* data, unsigned size, FILE* out, Section sect = { "", // sectname "", // segname - 0, // addr - pad(size), // size - sizeof(FileHeader) - + sizeof(SegmentCommand) - + sizeof(Section) - + sizeof(symtab_command), // offset - log(alignment), // align - 0, // reloff - 0, // nreloc - S_REGULAR, // flags - 0, // reserved1 - 0, // reserved2 + VW(0), // addr + VW(pad(size)), // size + V4(sizeof(FileHeader) + + sizeof(SegmentCommand) + + sizeof(Section) + + sizeof(symtab_command)), // offset + V4(log(alignment)), // align + V4(0), // reloff + V4(0), // nreloc + V4(S_REGULAR), // flags + V4(0), // reserved1 + V4(0), // reserved2 }; strncpy(sect.segname, segmentName, sizeof(sect.segname)); strncpy(sect.sectname, sectionName, sizeof(sect.sectname)); symtab_command symbolTable = { - LC_SYMTAB, // cmd - sizeof(symtab_command), // cmdsize - sizeof(FileHeader) - + sizeof(SegmentCommand) - + sizeof(Section) - + sizeof(symtab_command) - + pad(size), // symoff - 2, // nsyms - sizeof(FileHeader) - + sizeof(SegmentCommand) - + sizeof(Section) - + sizeof(symtab_command) - + pad(size) - + (sizeof(NList) * 2), // stroff - 1 + startNameLength + endNameLength, // strsize + V4(LC_SYMTAB), // cmd + V4(sizeof(symtab_command)), // cmdsize + V4(sizeof(FileHeader) + + sizeof(SegmentCommand) + + sizeof(Section) + + sizeof(symtab_command) + + pad(size)), // symoff + V4(2), // nsyms + V4(sizeof(FileHeader) + + sizeof(SegmentCommand) + + sizeof(Section) + + sizeof(symtab_command) + + pad(size) + + (sizeof(NList) * 2)), // stroff + V4(1 + startNameLength + endNameLength), // strsize }; NList symbolList[] = { { - 1, // n_un - N_SECT | N_EXT, // n_type - 1, // n_sect - 0, // n_desc - 0 // n_value + V4(1), // n_un + V1(N_SECT | N_EXT), // n_type + V1(1), // n_sect + V2(0), // n_desc + VW(0) // n_value }, { - 1 + startNameLength, // n_un - N_SECT | N_EXT, // n_type - 1, // n_sect - 0, // n_desc - size // n_value + V4(1 + startNameLength), // n_un + V1(N_SECT | N_EXT), // n_type + V1(1), // n_sect + V2(0), // n_desc + VW(size) // n_value } }; diff --git a/src/compile-arm.S b/src/compile-arm.S new file mode 100644 index 0000000000..555c61476b --- /dev/null +++ b/src/compile-arm.S @@ -0,0 +1,264 @@ +/* Copyright (c) 2010, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +#include "types.h" + +.text + +#define BYTES_PER_WORD 4 + +#define LOCAL(x) .L##x + +#ifdef __APPLE__ +# define GLOBAL(x) _##x +#else +# define GLOBAL(x) x +#endif + +#define THREAD_STACK 2144 +#define THREAD_CONTINUATION 2148 +#define THREAD_EXCEPTION 44 +#define THREAD_EXCEPTION_STACK_ADJUSTMENT 2152 +#define THREAD_EXCEPTION_OFFSET 2156 +#define THREAD_EXCEPTION_HANDLER 2160 + +#define CONTINUATION_NEXT 4 +#define CONTINUATION_ADDRESS 16 +#define CONTINUATION_RETURN_ADDRESS_OFFSET 20 +#define CONTINUATION_FRAME_POINTER_OFFSET 24 +#define CONTINUATION_LENGTH 28 +#define CONTINUATION_BODY 32 + +#define ARGUMENT_BASE (BYTES_PER_WORD * 2) + +.globl GLOBAL(vmInvoke) +GLOBAL(vmInvoke): + /* + arguments + r0 : thread + r1 : function + r2 : arguments + r3 : argumentFootprint + [sp, #0] : frameSize (not used) + [sp, #4] : returnType + */ + + // save stack frame + mov ip, sp + + // save all non-volatile registers + stmfd sp!, {r4-r11, lr} + + // save return type + ldr r4, [ip, #4] + str r4, [sp, #-4]! + + // we're at the bottom of our local stack frame; save it + mov ip, sp + + // align stack, if necessary + eor r4, sp, r3 + tst r4, #4 + subne sp, sp, #4 + + // copy arguments into place + sub sp, r3 + mov r4, #0 + b LOCAL(vmInvoke_argumentTest) + +LOCAL(vmInvoke_argumentLoop): + ldr r5, [r2, r4] + str r5, [sp, r4] + add r4, r4, #BYTES_PER_WORD + +LOCAL(vmInvoke_argumentTest): + cmp r4, r3 + blt LOCAL(vmInvoke_argumentLoop) + + // save frame + str ip, [sp, #-8]! + + // we use r8 to hold the thread pointer, by convention + mov r8, r0 + + // load and call function address + blx r1 + +.globl GLOBAL(vmInvoke_returnAddress) +GLOBAL(vmInvoke_returnAddress): + + // restore frame + ldr sp, [sp] + +.globl GLOBAL(vmInvoke_safeStack) +GLOBAL(vmInvoke_safeStack): + +#ifdef AVIAN_CONTINUATIONS + // call the next continuation, if any + ldr r5,[r8,#THREAD_CONTINUATION] + cmp r5,#0 + beq LOCAL(vmInvoke_exit) + + ldr r6,[r5,#CONTINUATION_LENGTH] + lsl r6,r6,#2 + neg r7,r6 + add r7,r7,#-80 + mov r4,sp + str r4,[sp,r7]! + + add r7,r5,#CONTINUATION_BODY + + mov r11,#0 + add r10,sp,#ARGUMENT_BASE + b LOCAL(vmInvoke_continuationTest) + +LOCAL(vmInvoke_continuationLoop): + ldr r9,[r7,r11] + str r9,[r10,r11] + add r11,r11,#4 + +LOCAL(vmInvoke_continuationTest): + cmp r11,r6 + ble LOCAL(vmInvoke_continuationLoop) + + ldr r7,[r5,#CONTINUATION_RETURN_ADDRESS_OFFSET] + ldr r10,LOCAL(vmInvoke_returnAddress_word) + ldr r11,LOCAL(vmInvoke_getAddress_word) +LOCAL(vmInvoke_getAddress): + add r11,pc,r11 + ldr r11,[r11,r10] + str r11,[sp,r7] + + ldr r7,[r5,#CONTINUATION_FRAME_POINTER_OFFSET] + ldr r11,[sp] + add r7,r7,sp + str r11,[r7] + str r7,[sp] + + ldr r7,[r5,#CONTINUATION_NEXT] + str r7,[r8,#THREAD_CONTINUATION] + + // call the continuation unless we're handling an exception + ldr r7,[r8,#THREAD_EXCEPTION] + cmp r7,#0 + bne LOCAL(vmInvoke_handleException) + ldr r7,[r5,#CONTINUATION_ADDRESS] + bx r7 + +LOCAL(vmInvoke_handleException): + // we're handling an exception - call the exception handler instead + mov r11,#0 + str r11,[r8,#THREAD_EXCEPTION] + ldr r11,[r8,#THREAD_EXCEPTION_STACK_ADJUSTMENT] + ldr r9,[sp] + neg r11,r11 + str r9,[sp,r11]! + ldr r11,[r8,#THREAD_EXCEPTION_OFFSET] + str r7,[sp,r11] + + ldr r7,[r8,#THREAD_EXCEPTION_HANDLER] + bx r7 + +LOCAL(vmInvoke_exit): +#endif // AVIAN_CONTINUATIONS + + mov ip, #0 + str ip, [r8, #THREAD_STACK] + + // restore return type + ldr ip, [sp], #4 + + // restore callee-saved registers + ldmfd sp!, {r4-r11, lr} + +LOCAL(vmInvoke_void): + cmp ip, #VOID_TYPE + beq LOCAL(vmInvoke_return) + +LOCAL(vmInvoke_int64): + cmp ip, #INT64_TYPE + beq LOCAL(vmInvoke_return) + +LOCAL(vmInvoke_int32): + mov r1, #0 + +LOCAL(vmInvoke_return): + bx lr + +.globl GLOBAL(vmJumpAndInvoke) +GLOBAL(vmJumpAndInvoke): +#ifdef AVIAN_CONTINUATIONS + // r0: thread + // r1: address + // r2: (unused) + // r3: stack + // [sp,#0]: argumentFootprint + // [sp,#4]: arguments + // [sp,#8]: frameSize + + ldr r4,[sp] + ldr r5,[sp,#4] + ldr r6,[sp,#8] + + // restore (pseudo)-stack pointer (we don't want to touch the real + // stack pointer, since we haven't copied the arguments yet) + ldr r3,[r3] + + // make everything between sp and r3 one big stack frame while we + // shuffle things around + str r3,[sp] + + // allocate new frame, adding room for callee-saved registers + neg r10,r6 + add r10,r10,#-80 + mov r2,r3 + str r2,[r3,r10]! + + mov r8,r0 + + // copy arguments into place + mov r6,#0 + add r9,r3,#ARGUMENT_BASE + b LOCAL(vmJumpAndInvoke_argumentTest) + +LOCAL(vmJumpAndInvoke_argumentLoop): + ldr r12,[r5,r6] + str r12,[r9,r6] + add r6,r6,#4 + +LOCAL(vmJumpAndInvoke_argumentTest): + cmp r6,r4 + ble LOCAL(vmJumpAndInvoke_argumentLoop) + + // the arguments have been copied, so we can set the real stack + // pointer now + mov sp,r3 + + // set return address to vmInvoke_returnAddress + ldr r10,LOCAL(vmInvoke_returnAddress_word) + ldr r11,LOCAL(vmJumpAndInvoke_getAddress_word) +LOCAL(vmJumpAndInvoke_getAddress): + add r11,pc,r11 + ldr lr,[r11,r10] + + bx r1 + +LOCAL(vmInvoke_returnAddress_word): + .word GLOBAL(vmInvoke_returnAddress)(GOT) +LOCAL(vmInvoke_getAddress_word): + .word _GLOBAL_OFFSET_TABLE_-(LOCAL(vmInvoke_getAddress)+8) +LOCAL(vmJumpAndInvoke_getAddress_word): + .word _GLOBAL_OFFSET_TABLE_-(LOCAL(vmJumpAndInvoke_getAddress)+8) + +#else // not AVIAN_CONTINUATIONS + // vmJumpAndInvoke should only be called when continuations are + // enabled + bkpt +#endif // not AVIAN_CONTINUATIONS diff --git a/src/compile.cpp b/src/compile.cpp index 5c80c8de8f..6a46b5b39a 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -5416,14 +5416,14 @@ codeSingletonSizeInBytes(MyThread*, unsigned codeSizeInBytes) } uint8_t* -finish(MyThread* t, Allocator* allocator, Assembler* a, const char* name) +finish(MyThread* t, Allocator* allocator, Assembler* a, const char* name, + unsigned length) { - uint8_t* start = static_cast - (allocator->allocate(pad(a->length()))); + uint8_t* start = static_cast(allocator->allocate(pad(length))); a->writeTo(start); - logCompile(t, start, a->length(), 0, name, 0); + logCompile(t, start, length, 0, name, 0); return start; } @@ -8275,9 +8275,7 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) Assembler::Register result(t->arch->returnLow()); a->apply(Jump, BytesPerWord, RegisterOperand, &result); - a->endBlock(false)->resolve(0, 0); - - p->thunks.default_.length = a->length(); + p->thunks.default_.length = a->endBlock(false)->resolve(0, 0); } ThunkContext defaultVirtualContext(t, &zone); @@ -8321,9 +8319,7 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) Assembler::Register result(t->arch->returnLow()); a->apply(Jump, BytesPerWord, RegisterOperand, &result); - a->endBlock(false)->resolve(0, 0); - - p->thunks.defaultVirtual.length = a->length(); + p->thunks.defaultVirtual.length = a->endBlock(false)->resolve(0, 0); } ThunkContext nativeContext(t, &zone); @@ -8342,9 +8338,7 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) a->popFrameAndUpdateStackAndReturn(difference(&(t->stack), t)); - a->endBlock(false)->resolve(0, 0); - - p->thunks.native.length = a->length(); + p->thunks.native.length = a->endBlock(false)->resolve(0, 0); } ThunkContext aioobContext(t, &zone); @@ -8361,9 +8355,7 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) Assembler::Constant proc(&(aioobContext.promise)); a->apply(LongCall, BytesPerWord, ConstantOperand, &proc); - a->endBlock(false)->resolve(0, 0); - - p->thunks.aioob.length = a->length(); + p->thunks.aioob.length = a->endBlock(false)->resolve(0, 0); } ThunkContext tableContext(t, &zone); @@ -8377,13 +8369,12 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) Assembler::Constant proc(&(tableContext.promise)); a->apply(LongJump, BytesPerWord, ConstantOperand, &proc); - a->endBlock(false)->resolve(0, 0); - - p->thunks.table.length = a->length(); + p->thunks.table.length = a->endBlock(false)->resolve(0, 0); } p->thunks.default_.start = finish - (t, allocator, defaultContext.context.assembler, "default"); + (t, allocator, defaultContext.context.assembler, "default", + p->thunks.default_.length); BootImage* image = p->bootImage; uint8_t* imageBase = p->codeAllocator.base; @@ -8398,7 +8389,8 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) } p->thunks.defaultVirtual.start = finish - (t, allocator, defaultVirtualContext.context.assembler, "defaultVirtual"); + (t, allocator, defaultVirtualContext.context.assembler, "defaultVirtual", + p->thunks.defaultVirtual.length); { void* call; defaultVirtualContext.promise.listener->resolve @@ -8411,7 +8403,8 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) } p->thunks.native.start = finish - (t, allocator, nativeContext.context.assembler, "native"); + (t, allocator, nativeContext.context.assembler, "native", + p->thunks.native.length); { void* call; nativeContext.promise.listener->resolve @@ -8423,7 +8416,8 @@ compileThunks(MyThread* t, Allocator* allocator, MyProcessor* p) } p->thunks.aioob.start = finish - (t, allocator, aioobContext.context.assembler, "aioob"); + (t, allocator, aioobContext.context.assembler, "aioob", + p->thunks.aioob.length); { void* call; aioobContext.promise.listener->resolve @@ -8535,9 +8529,7 @@ compileVirtualThunk(MyThread* t, unsigned index, unsigned* size) Assembler::Constant thunk(&defaultVirtualThunkPromise); a->apply(Jump, BytesPerWord, ConstantOperand, &thunk); - a->endBlock(false)->resolve(0, 0); - - *size = a->length(); + *size = a->endBlock(false)->resolve(0, 0); uint8_t* start = static_cast(codeAllocator(t)->allocate(*size)); diff --git a/src/compiler.cpp b/src/compiler.cpp index 5e8c447b7e..f6a2458aaa 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -4890,10 +4890,14 @@ class BoundsCheckEvent: public Event { CodePromise* nextPromise = codePromise (c, static_cast(0)); + index->source->freeze(c, index); + ConstantSite next(nextPromise); apply(c, JumpIfGreater, 4, index->source, index->source, 4, &length, &length, BytesPerWord, &next, &next); + index->source->thaw(c, index); + if (constant == 0) { outOfBoundsPromise->offset = a->offset(); } @@ -5700,6 +5704,8 @@ compile(Context* c) p->offset = a->offset(); } + a->endEvent(); + LogicalInstruction* nextInstruction = next(c, e->logicalInstruction); if (e->next == 0 or (e->next->logicalInstruction != e->logicalInstruction @@ -5737,7 +5743,7 @@ compile(Context* c) block = next; } - return block->assemblerBlock->resolve(block->start, 0) + a->scratchSize(); + return block->assemblerBlock->resolve(block->start, 0); } unsigned diff --git a/src/machine.cpp b/src/machine.cpp index 9d3ab655fb..a3d9402941 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -45,7 +45,11 @@ void join(Thread* t, Thread* o) { if (t != o) { - o->systemThread->join(); + if (o->state != Thread::ZombieState + and o->state != Thread::JoinedState) + { + o->systemThread->join(); + } o->state = Thread::JoinedState; } } @@ -1979,6 +1983,13 @@ bootJavaClass(Thread* t, Machine::Type type, int superType, const char* name, (t, root(t, Machine::BootstrapClassMap), n, class_, byteArrayHash); } +void +nameClass(Thread* t, Machine::Type type, const char* name) +{ + object n = makeByteArray(t, name); + set(t, arrayBody(t, t->m->types, type), ClassName, n); +} + void boot(Thread* t) { @@ -2098,6 +2109,10 @@ boot(Thread* t) PROTECT(t, bootMethod); #include "type-java-initializations.cpp" + +#ifdef AVIAN_HEAPDUMP +# include "type-name-initializations.cpp" +#endif } } @@ -2393,6 +2408,12 @@ Thread::exit() } else { threadPeer(this, javaThread) = 0; enter(this, Thread::ZombieState); + + lock->dispose(); + lock = 0; + + systemThread->dispose(); + systemThread = 0; } } } @@ -2400,7 +2421,9 @@ Thread::exit() void Thread::dispose() { - lock->dispose(); + if (lock) { + lock->dispose(); + } if (systemThread) { systemThread->dispose(); diff --git a/src/machine.h b/src/machine.h index 15782d5240..21849fc04d 100644 --- a/src/machine.h +++ b/src/machine.h @@ -2761,7 +2761,11 @@ notifyAll(Thread* t, object o) inline void interrupt(Thread*, Thread* target) { - target->systemThread->interrupt(); + if (target->state != Thread::ZombieState + and target->state != Thread::JoinedState) + { + target->systemThread->interrupt(); + } } inline void diff --git a/src/posix.cpp b/src/posix.cpp index e72587eea6..66582fb5b4 100644 --- a/src/posix.cpp +++ b/src/posix.cpp @@ -784,6 +784,7 @@ class MySystem: public System { } virtual void abort() { + *static_cast(0) = 0; ::abort(); } diff --git a/src/powerpc.cpp b/src/powerpc.cpp index eb1d5ef33b..ce8a6bbb4b 100644 --- a/src/powerpc.cpp +++ b/src/powerpc.cpp @@ -2393,12 +2393,12 @@ class MyAssembler: public Assembler { return b; } - virtual unsigned length() { - return c.code.length(); + virtual void endEvent() { + // ignore } - virtual unsigned scratchSize() { - return c.constantPoolCount * BytesPerWord; + virtual unsigned length() { + return c.code.length(); } virtual void dispose() { diff --git a/src/powerpc.h b/src/powerpc.h index 9a55c13266..d252039b89 100644 --- a/src/powerpc.h +++ b/src/powerpc.h @@ -15,7 +15,7 @@ #include "common.h" #ifdef __APPLE__ -# if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32) +# if __DARWIN_UNIX03 && defined(_STRUCT_PPC_EXCEPTION_STATE) # define IP_REGISTER(context) (context->uc_mcontext->__ss.__srr0) # define STACK_REGISTER(context) (context->uc_mcontext->__ss.__r1) # define THREAD_REGISTER(context) (context->uc_mcontext->__ss.__r13) diff --git a/src/type-generator.cpp b/src/type-generator.cpp index c2eeb76684..a0ae158816 100644 --- a/src/type-generator.cpp +++ b/src/type-generator.cpp @@ -2172,13 +2172,34 @@ writeJavaInitializations(Output* out, Object* declarations) } } +void +writeNameInitialization(Output* out, Object* type) +{ + out->write("nameClass(t, Machine::"); + out->write(capitalize(typeName(type))); + out->write("Type, \"vm::"); + out->write(typeName(type)); + out->write("\");\n"); +} + +void +writeNameInitializations(Output* out, Object* declarations) +{ + for (Object* p = declarations; p; p = cdr(p)) { + Object* o = car(p); + if (o->type == Object::Type and typeJavaName(o) == 0) { + writeNameInitialization(out, o); + } + } +} + void usageAndExit(const char* command) { fprintf(stderr, "usage: %s " "{enums,declarations,constructors,initializations," - "java-initializations}\n", + "java-initializations,name-initializations}\n", command); exit(-1); } @@ -2207,7 +2228,8 @@ main(int ac, char** av) or local::equal(av[4], "declarations") or local::equal(av[4], "constructors") or local::equal(av[4], "initializations") - or local::equal(av[4], "java-initializations"))) + or local::equal(av[4], "java-initializations") + or local::equal(av[4], "name-initializations"))) { local::usageAndExit(av[0]); } @@ -2277,6 +2299,8 @@ main(int ac, char** av) local::writeInitializations(&out, declarations); } else if (local::equal(av[4], "java-initializations")) { local::writeJavaInitializations(&out, declarations); + } else if (local::equal(av[4], "name-initializations")) { + local::writeNameInitializations(&out, declarations); } return 0; diff --git a/src/x86.cpp b/src/x86.cpp index 2d4d3e55f7..3ff4f716bb 100644 --- a/src/x86.cpp +++ b/src/x86.cpp @@ -3532,12 +3532,12 @@ class MyAssembler: public Assembler { return b; } - virtual unsigned length() { - return c.code.length(); + virtual void endEvent() { + // ignore } - virtual unsigned scratchSize() { - return 0; + virtual unsigned length() { + return c.code.length(); } virtual void dispose() { diff --git a/test/Arrays.java b/test/Arrays.java index 52c519ba69..acc6e8a9c6 100644 --- a/test/Arrays.java +++ b/test/Arrays.java @@ -65,5 +65,11 @@ public class Arrays { p = false; expect(array[1] == array[p ? 0 : 1]); } + + { int[] array = new int[1024]; + array[1023] = -1; + expect(array[1023] == -1); + expect(array[1022] == 0); + } } } diff --git a/test/extra/DumpStats.java b/test/extra/DumpStats.java new file mode 100644 index 0000000000..c9e7774484 --- /dev/null +++ b/test/extra/DumpStats.java @@ -0,0 +1,147 @@ +package extra; + +import java.io.PrintStream; +import java.io.InputStream; +import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.EOFException; +import java.util.Map; +import java.util.HashMap; +import java.util.Comparator; +import java.util.Arrays; + +/** + * This is a simple utility to generate and print statistics from a + * heap dump generated by Avian's heapdump.cpp. The output is a list + * of classes (identified by number in the case of anonymous, + * VM-internal classes), each followed by (1) the total memory + * footprint of all instances of the class, and (2) the number of + * instances. The output is ordered by instance memory footprint. + */ +public class DumpStats { + private static final int Root = 0; + private static final int Size = 1; + private static final int ClassName = 2; + private static final int Push = 3; + private static final int Pop = 4; + + private static int readInt(InputStream in) throws IOException { + int b1 = in.read(); + int b2 = in.read(); + int b3 = in.read(); + int b4 = in.read(); + if (b4 == -1) throw new EOFException(); + return (int) ((b1 << 24) | (b2 << 16) | (b3 << 8) | (b4)); + } + + private static String readString(InputStream in) throws IOException { + int count = readInt(in); + byte[] b = new byte[count]; + int offset = 0; + int c; + while ((c = in.read(b, offset, b.length - offset)) != -1 + && offset < b.length) + { + offset += c; + } + if (offset != b.length) throw new EOFException(); + return new String(b); + } + + private static Record record(Map map, int key) { + Record r = map.get(key); + if (r == null) { + map.put(key, r = new Record(key)); + } + return r; + } + + private static Map read(InputStream in) + throws IOException + { + boolean done = false; + boolean popped = false; + int size = 0; + int last = 0; + Map map = new HashMap(); + + while (! done) { + int flag = in.read(); + switch (flag) { + case Root: { + last = readInt(in); + popped = false; + } break; + + case ClassName: { + record(map, last).name = readString(in); + } break; + + case Push: { + last = readInt(in); + if (! popped) { + Record r = record(map, last); + r.footprint += size; + ++ r.count; + } + popped = false; + } break; + + case Pop: { + popped = true; + } break; + + case Size: { + size = readInt(in); + } break; + + case -1: + done = true; + break; + + default: + throw new RuntimeException("bad flag: " + flag); + } + } + + return map; + } + + public static void main(String[] args) throws Exception { + Map map = read + (new BufferedInputStream(new FileInputStream(args[0]))); + + Record[] array = map.values().toArray(new Record[map.size()]); + Arrays.sort(array, new Comparator() { + public int compare(Record a, Record b) { + return b.footprint - a.footprint; + } + }); + + int footprint = 0; + int count = 0; + for (Record r: array) { + if (r.name == null) { + r.name = String.valueOf(r.key); + } + System.out.println(r.name + ": " + r.footprint + " " + r.count); + footprint += r.footprint; + count += r.count; + } + + System.out.println(); + System.out.println("total: " + footprint + " " + count); + } + + private static class Record { + public final int key; + public String name; + public int footprint; + public int count; + + public Record(int key) { + this.key = key; + } + } +} diff --git a/test/extra/PrintDump.java b/test/extra/PrintDump.java new file mode 100644 index 0000000000..8f43f419c9 --- /dev/null +++ b/test/extra/PrintDump.java @@ -0,0 +1,101 @@ +package extra; + +import java.io.PrintStream; +import java.io.InputStream; +import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.EOFException; + +/** + * This is a simple utility to print the contents of a heap dump + * generated by Avian's heapdump.cpp in a human-readable format. + */ +public class PrintDump { + private static final int Root = 0; + private static final int Size = 1; + private static final int ClassName = 2; + private static final int Push = 3; + private static final int Pop = 4; + + private static void indent(PrintStream out, int level) { + for (; level > 0; --level) out.print(" "); + } + + private static int readInt(InputStream in) throws IOException { + int b1 = in.read(); + int b2 = in.read(); + int b3 = in.read(); + int b4 = in.read(); + if (b4 == -1) throw new EOFException(); + return (int) ((b1 << 24) | (b2 << 16) | (b3 << 8) | (b4)); + } + + private static String readString(InputStream in) throws IOException { + int count = readInt(in); + byte[] b = new byte[count]; + int offset = 0; + int c; + while ((c = in.read(b, offset, b.length - offset)) != -1 + && offset < b.length) + { + offset += c; + } + if (offset != b.length) throw new EOFException(); + return new String(b); + } + + private static void pipe(InputStream in, PrintStream out) + throws IOException + { + boolean done = false; + boolean popped = false; + int level = 0; + while (! done) { + int flag = in.read(); + switch (flag) { + case Root: { + out.print("\nroot " + readInt(in)); + popped = false; + } break; + + case ClassName: { + out.print(" class " + readString(in)); + } break; + + case Push: { + ++ level; + out.println(); + indent(out, level); + if (! popped) { + out.print("first "); + } + out.print("child " + readInt(in)); + popped = false; + } break; + + case Pop: { + -- level; + popped = true; + } break; + + case Size: { + out.print(" size " + readInt(in)); + } break; + + case -1: + out.println(); + out.flush(); + done = true; + break; + + default: + throw new RuntimeException("bad flag: " + flag); + } + } + } + + public static void main(String[] args) throws Exception { + pipe(new BufferedInputStream(new FileInputStream(args[0])), System.out); + } +}