am 1b154930: Merge change 1867 into donut
Merge commit '1b154930b2c2634dce9c04d5d5cec7acb229ccb0'
* commit '1b154930b2c2634dce9c04d5d5cec7acb229ccb0':
Cleaning up whitespace in adb sources. Nothing more, nothing less.
diff --git a/adb/commandline.c b/adb/commandline.c
index 7410dce..41b340a 100644
--- a/adb/commandline.c
+++ b/adb/commandline.c
@@ -96,7 +96,8 @@
" -e - directs command to the only running emulator.\n"
" returns an error if more than one emulator is running.\n"
" -s <serial number> - directs command to the USB device or emulator with\n"
- " the given serial number\n"
+ " the given serial number. Overrides ANDROID_SERIAL\n"
+ " envivornment variable.\n"
" -p <product name or path> - simple product name like 'sooner', or\n"
" a relative/absolute path to a product\n"
" out directory like 'out/target/product/sooner'.\n"
@@ -766,6 +767,8 @@
}
// TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
+ serial = getenv("ANDROID_SERIAL");
+
/* modifiers and flags */
while(argc > 0) {
if(!strcmp(argv[0],"nodaemon")) {
diff --git a/include/arch/freebsd-x86/AndroidConfig.h b/include/arch/freebsd-x86/AndroidConfig.h
new file mode 100644
index 0000000..cc118f4
--- /dev/null
+++ b/include/arch/freebsd-x86/AndroidConfig.h
@@ -0,0 +1,317 @@
+/*
+ * Copyright (C) 2005 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Android config -- "FreeBSD". Used for desktop x86 FreeBSD.
+ */
+#ifndef _ANDROID_CONFIG_H
+#define _ANDROID_CONFIG_H
+
+/*
+ * make sure we are building for FreeBSD
+ */
+#ifndef OS_FREEBSD
+#define OS_FREEBSD
+#endif
+/*
+ * ===========================================================================
+ * !!! IMPORTANT !!!
+ * ===========================================================================
+ *
+ * This file is included by ALL C/C++ source files. Don't put anything in
+ * here unless you are absolutely certain it can't go anywhere else.
+ *
+ * Any C++ stuff must be wrapped with "#ifdef __cplusplus". Do not use "//"
+ * comments.
+ */
+
+/*
+ * Threading model. Choose one:
+ *
+ * HAVE_PTHREADS - use the pthreads library.
+ * HAVE_WIN32_THREADS - use Win32 thread primitives.
+ * -- combine HAVE_CREATETHREAD, HAVE_CREATEMUTEX, and HAVE__BEGINTHREADEX
+ */
+#define HAVE_PTHREADS
+
+/*
+ * Do we have the futex syscall?
+ */
+/* #define HAVE_FUTEX */
+
+/*
+ * Process creation model. Choose one:
+ *
+ * HAVE_FORKEXEC - use fork() and exec()
+ * HAVE_WIN32_PROC - use CreateProcess()
+ */
+#define HAVE_FORKEXEC
+
+/*
+ * Process out-of-memory adjustment. Set if running on Linux,
+ * where we can write to /proc/<pid>/oom_adj to modify the out-of-memory
+ * badness adjustment.
+ */
+/* #define HAVE_OOM_ADJ */
+
+/*
+ * IPC model. Choose one:
+ *
+ * HAVE_SYSV_IPC - use the classic SysV IPC mechanisms (semget, shmget).
+ * HAVE_MACOSX_IPC - use Macintosh IPC mechanisms (sem_open, mmap).
+ * HAVE_WIN32_IPC - use Win32 IPC (CreateSemaphore, CreateFileMapping).
+ * HAVE_ANDROID_IPC - use Android versions (?, mmap).
+ */
+#define HAVE_SYSV_IPC
+
+/*
+ * Memory-mapping model. Choose one:
+ *
+ * HAVE_POSIX_FILEMAP - use the Posix sys/mmap.h
+ * HAVE_WIN32_FILEMAP - use Win32 filemaps
+ */
+#define HAVE_POSIX_FILEMAP
+
+/*
+ * Define this if you have <termio.h>
+ */
+/* #define HAVE_TERMIO_H */
+
+/*
+ * Define this if you build against MSVCRT.DLL
+ */
+/* #define HAVE_MS_C_RUNTIME */
+
+/*
+ * Define this if you have sys/uio.h
+ */
+#define HAVE_SYS_UIO_H
+
+/*
+ * Define this if your platforms implements symbolic links
+ * in its filesystems
+ */
+#define HAVE_SYMLINKS
+
+/*
+ * Define this if we have localtime_r().
+ */
+#define HAVE_LOCALTIME_R
+
+/*
+ * Define this if we have gethostbyname_r().
+ */
+/* #define HAVE_GETHOSTBYNAME_R */
+
+/*
+ * Define this if we have ioctl().
+ */
+#define HAVE_IOCTL
+
+/*
+ * Define this if we want to use WinSock.
+ */
+/* #define HAVE_WINSOCK */
+
+/*
+ * Define this if have clock_gettime() and friends
+ *
+ * Desktop Linux has this in librt, but it's broken in goobuntu, yielding
+ * mildly or wildly inaccurate results.
+ */
+#define HAVE_POSIX_CLOCKS
+
+/*
+ * Define this if we have pthread_cond_timedwait_monotonic() and
+ * clock_gettime(CLOCK_MONOTONIC).
+ */
+/* #define HAVE_TIMEDWAIT_MONOTONIC */
+
+/*
+ * Define this if we have linux style epoll()
+ */
+/* #define HAVE_EPOLL */
+
+/*
+ * Endianness of the target machine. Choose one:
+ *
+ * HAVE_ENDIAN_H -- have endian.h header we can include.
+ * HAVE_LITTLE_ENDIAN -- we are little endian.
+ * HAVE_BIG_ENDIAN -- we are big endian.
+ */
+/* #define HAVE_ENDIAN_H */
+#define HAVE_LITTLE_ENDIAN
+
+/*
+ * Define this if you have sys/endian.h
+ * NOTE: mutually exclusive with HAVE_ENDIAN_H
+ */
+#define HAVE_SYS_ENDIAN_H
+
+/*
+ * We need to choose between 32-bit and 64-bit off_t. All of our code should
+ * agree on the same size. For desktop systems, use 64-bit values,
+ * because some of our libraries (e.g. wxWidgets) expect to be built that way.
+ */
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE_SOURCE 1
+
+/*
+ * Defined if we have the backtrace() call for retrieving a stack trace.
+ * Needed for CallStack to operate; if not defined, CallStack is
+ * non-functional.
+ */
+#define HAVE_BACKTRACE 0
+
+/*
+ * Defined if we have the dladdr() call for retrieving the symbol associated
+ * with a memory address. If not defined, stack crawls will not have symbolic
+ * information.
+ */
+#define HAVE_DLADDR 1
+
+/*
+ * Defined if we have the cxxabi.h header for demangling C++ symbols. If
+ * not defined, stack crawls will be displayed with raw mangled symbols
+ */
+#define HAVE_CXXABI 0
+
+/*
+ * Defined if we have the gettid() system call.
+ */
+/* #define HAVE_GETTID */
+
+/*
+ * Defined if we have the sched_setscheduler() call
+ */
+#define HAVE_SCHED_SETSCHEDULER
+
+/*
+ * Add any extra platform-specific defines here.
+ */
+
+/*
+ * Define if we have <malloc.h> header
+ */
+#define HAVE_MALLOC_H
+
+/*
+ * Define if we have Linux-style non-filesystem Unix Domain Sockets
+ */
+
+/*
+ * What CPU architecture does this platform use?
+ */
+#define ARCH_X86
+
+
+/*
+ * Define if we have Linux's inotify in <sys/inotify.h>.
+ */
+/*#define HAVE_INOTIFY 1*/
+
+/*
+ * Define if we have madvise() in <sys/mman.h>
+ */
+#define HAVE_MADVISE 1
+
+/*
+ * Define if tm struct has tm_gmtoff field
+ */
+#define HAVE_TM_GMTOFF 1
+
+/*
+ * Define if dirent struct has d_type field
+ */
+#define HAVE_DIRENT_D_TYPE 1
+
+/*
+ * Define if libc includes Android system properties implementation.
+ */
+/* #define HAVE_LIBC_SYSTEM_PROPERTIES */
+
+/*
+ * Define if system provides a system property server (should be
+ * mutually exclusive with HAVE_LIBC_SYSTEM_PROPERTIES).
+ */
+#define HAVE_SYSTEM_PROPERTY_SERVER
+
+/*
+ * sprintf() format string for shared library naming.
+ */
+#define OS_SHARED_LIB_FORMAT_STR "lib%s.so"
+
+/*
+ * type for the third argument to mincore().
+ */
+#define MINCORE_POINTER_TYPE char *
+
+/*
+ * Do we have the sigaction flag SA_NOCLDWAIT?
+ */
+#define HAVE_SA_NOCLDWAIT
+
+/*
+ * Define if we include <sys/mount.h> for statfs()
+ */
+#define INCLUDE_SYS_MOUNT_FOR_STATFS 1
+
+/*
+ * The default path separator for the platform
+ */
+#define OS_PATH_SEPARATOR '/'
+
+/*
+ * Is the filesystem case sensitive?
+ */
+#define OS_CASE_SENSITIVE
+
+/*
+ * Define if <sys/socket.h> exists.
+ */
+#define HAVE_SYS_SOCKET_H 1
+
+/*
+ * Define if the strlcpy() function exists on the system.
+ */
+#define HAVE_STRLCPY 1
+
+/*
+ * Define if prctl() exists
+ */
+/* #define HAVE_PRCTL 1 */
+
+/*
+ * Define if writev() exists
+ */
+#define HAVE_WRITEV 1
+
+/*
+ * Define if <alloca.h> does not exist
+ * NOTE: <alloca.h> defines alloca() which
+ * on FreeBSD is defined in <stdlib.h>
+ */
+#define HAVE_NO_ALLOCA_H
+
+/*
+ * Defines CLOCK_PROCESS_CPUTIME_ID for clock_gettime()
+ * XXX: CLOCK_PROF seems to be commonly used replacement
+ */
+#ifndef CLOCK_PROCESS_CPUTIME_ID
+#define CLOCK_PROCESS_CPUTIME_ID CLOCK_PROF
+#endif
+
+#endif /*_ANDROID_CONFIG_H*/
diff --git a/include/cutils/tztime.h b/include/cutils/tztime.h
index 69dbc88..cf103ca 100644
--- a/include/cutils/tztime.h
+++ b/include/cutils/tztime.h
@@ -17,6 +17,8 @@
#ifndef _CUTILS_TZTIME_H
#define _CUTILS_TZTIME_H
+#include <time.h>
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -24,6 +26,9 @@
time_t mktime_tz(struct tm * const tmp, char const * tz);
void localtime_tz(const time_t * const timep, struct tm * tmp, const char* tz);
+#ifndef HAVE_ANDROID_OS
+/* the following is defined in <time.h> in Bionic */
+
struct strftime_locale {
const char *mon[12]; /* short names */
const char *month[12]; /* long names */
@@ -40,6 +45,8 @@
size_t strftime_tz(char *s, size_t max, const char *format, const struct tm *tm, const struct strftime_locale *locale);
+#endif /* !HAVE_ANDROID_OS */
+
#ifdef __cplusplus
}
#endif
diff --git a/init/builtins.c b/init/builtins.c
index 95fb223..bcdfee1 100644
--- a/init/builtins.c
+++ b/init/builtins.c
@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/resource.h>
+#include <linux/loop.h>
#include "init.h"
#include "keywords.h"
@@ -257,7 +258,7 @@
int do_mount(int nargs, char **args)
{
char tmp[64];
- char *source;
+ char *source, *target, *system;
char *options = NULL;
unsigned flags = 0;
int n, i;
@@ -275,15 +276,70 @@
options = args[n];
}
+ system = args[1];
source = args[2];
+ target = args[3];
+
if (!strncmp(source, "mtd@", 4)) {
n = mtd_name_to_number(source + 4);
- if (n >= 0) {
- sprintf(tmp, "/dev/block/mtdblock%d", n);
- source = tmp;
+ if (n < 0) {
+ return -1;
}
+
+ sprintf(tmp, "/dev/block/mtdblock%d", n);
+
+ if (mount(tmp, target, system, flags, options) < 0) {
+ return -1;
+ }
+
+ return 0;
+ } else if (!strncmp(source, "loop@", 5)) {
+ int mode, loop, fd;
+ struct loop_info info;
+
+ mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
+ fd = open(source + 5, mode);
+ if (fd < 0) {
+ return -1;
+ }
+
+ for (n = 0; ; n++) {
+ sprintf(tmp, "/dev/block/loop%d", n);
+ loop = open(tmp, mode);
+ if (loop < 0) {
+ return -1;
+ }
+
+ /* if it is a blank loop device */
+ if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
+ /* if it becomes our loop device */
+ if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
+ close(fd);
+
+ if (mount(tmp, target, system, flags, options) < 0) {
+ ioctl(loop, LOOP_CLR_FD, 0);
+ close(loop);
+ return -1;
+ }
+
+ close(loop);
+ return 0;
+ }
+ }
+
+ close(loop);
+ }
+
+ close(fd);
+ ERROR("out of loopback devices");
+ return -1;
+ } else {
+ if (mount(source, target, system, flags, options) < 0) {
+ return -1;
+ }
+
+ return 0;
}
- return mount(source, args[3], args[1], flags, options);
}
int do_setkey(int nargs, char **args)
diff --git a/init/devices.c b/init/devices.c
index c278607..300faab 100644
--- a/init/devices.c
+++ b/init/devices.c
@@ -131,6 +131,8 @@
{ "/dev/qmi0", 0640, AID_RADIO, AID_RADIO, 0 },
{ "/dev/qmi1", 0640, AID_RADIO, AID_RADIO, 0 },
{ "/dev/qmi2", 0640, AID_RADIO, AID_RADIO, 0 },
+ /* CDMA radio interface MUX */
+ { "/dev/ts0710mux", 0640, AID_RADIO, AID_RADIO, 1 },
{ "/dev/tun", 0640, AID_VPN , AID_VPN, 0 },
{ NULL, 0, 0, 0, 0 },
};
@@ -388,6 +390,9 @@
} else if(!strncmp(uevent->subsystem, "mtd", 3)) {
base = "/dev/mtd/";
mkdir(base, 0755);
+ } else if(!strncmp(uevent->subsystem, "sound", 5)) {
+ base = "/dev/snd/";
+ mkdir(base, 0755);
} else if(!strncmp(uevent->subsystem, "misc", 4) &&
!strncmp(name, "log_", 4)) {
base = "/dev/log/";
diff --git a/init/init.c b/init/init.c
index a748ec3..283608c 100644
--- a/init/init.c
+++ b/init/init.c
@@ -248,7 +248,9 @@
setuid(svc->uid);
}
- execve(svc->args[0], (char**) svc->args, (char**) ENV);
+ if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
+ ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
+ }
_exit(127);
}
diff --git a/libacc/Android.mk b/libacc/Android.mk
new file mode 100644
index 0000000..c3207cc
--- /dev/null
+++ b/libacc/Android.mk
@@ -0,0 +1,12 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+#
+# Shared library
+#
+
+LOCAL_MODULE:= acc
+LOCAL_SRC_FILES := acc.cpp disassem.cpp
+LOCAL_MODULE_TAGS := tests
+
+include $(BUILD_EXECUTABLE)
diff --git a/libacc/FEATURES b/libacc/FEATURES
new file mode 100644
index 0000000..1a44415
--- /dev/null
+++ b/libacc/FEATURES
@@ -0,0 +1,65 @@
+
+Supported C language subset (read joint example 'otccex.c' to have
+ an introduction to OTCC dialect):
+
+ - Expressions:
+
+ * binary operators, by decreasing priority order: '*' '/' '%',
+ '+' '-', '>>' '<<', '<' '<=' '>' '>=', '==' '!=', '&',
+ '^', '|', '=', '&&', '||'.
+
+ * '&&' and '||' have the same semantics as C : left to right
+ evaluation and early exit.
+
+ * Parenthesis are supported.
+
+ * Unary operators: '&', '*' (pointer indirection), '-'
+ (negation), '+', '!', '~', post fixed '++' and '--'.
+
+ * Pointer indirection ('*') only works with explicit cast to
+ 'char *', 'int *' or 'int (*)()' (function pointer).
+
+ * '++', '--', and unary '&' can only be used with variable
+ lvalue (left value).
+
+ * '=' can only be used with variable or '*' (pointer
+ indirection) lvalue.
+
+ * Function calls are supported with standard i386 calling
+ convention. Function pointers are supported with explicit
+ cast. Functions can be used before being declared.
+
+ - Types: only signed integer ('int') variables and functions can
+ be declared. Variables cannot be initialized in
+ declarations. Only old K&R function declarations are parsed
+ (implicit integer return value and no types on arguments).
+
+ - Any function or variable from the libc can be used because OTCC
+ uses the libc dynamic linker to resolve undefined symbols.
+
+ - Instructions: blocks ('{' '}') are supported as in C. 'if' and
+ 'else' can be used for tests. The 'while' and 'for' C constructs
+ are supported for loops. 'break' can be used to exit
+ loops. 'return' is used for the return value of a function.
+
+ - Identifiers are parsed the same way as C. Local variables are
+ handled, but there is no local name space (not a problem if
+ different names are used for local and global variables).
+
+ - Numbers can be entered in decimal, hexadecimal ('0x' or '0X'
+ prefix), or octal ('0' prefix).
+
+ - '#define' is supported without function like arguments. No macro
+ recursion is tolerated. Other preprocessor directives are
+ ignored.
+
+ - C Strings and C character constants are supported. Only '\n',
+ '\"', '\'' and '\\' escapes are recognized.
+
+ - Both C comments ( /* */ ) and C++ comments ( // ... end-of-line ) can be used.
+
+ - No error is displayed if an incorrect program is given.
+
+ - Memory: the code, data, and symbol sizes are limited to 100KB
+ (it can be changed in the source code).
+
diff --git a/libacc/MODULE_LICENSE_BSD_LIKE b/libacc/MODULE_LICENSE_BSD_LIKE
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/libacc/MODULE_LICENSE_BSD_LIKE
diff --git a/libacc/acc.cpp b/libacc/acc.cpp
new file mode 100644
index 0000000..567e25e
--- /dev/null
+++ b/libacc/acc.cpp
@@ -0,0 +1,1629 @@
+/*
+ Obfuscated Tiny C Compiler
+
+ Copyright (C) 2001-2003 Fabrice Bellard
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product and its documentation
+ *is* required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+ */
+
+#include <ctype.h>
+#include <dlfcn.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#if defined(__arm__)
+#include <unistd.h>
+#endif
+
+#include "disassem.h"
+
+namespace acc {
+
+class compiler {
+ class CodeBuf {
+ char* ind;
+ char* pProgramBase;
+
+ void release() {
+ if (pProgramBase != 0) {
+ free(pProgramBase);
+ pProgramBase = 0;
+ }
+ }
+
+ public:
+ CodeBuf() {
+ pProgramBase = 0;
+ ind = 0;
+ }
+
+ ~CodeBuf() {
+ release();
+ }
+
+ void init(int size) {
+ release();
+ pProgramBase = (char*) calloc(1, size);
+ ind = pProgramBase;
+ }
+
+ void o(int n) {
+ /* cannot use unsigned, so we must do a hack */
+ while (n && n != -1) {
+ *ind++ = n;
+ n = n >> 8;
+ }
+ }
+
+ int o4(int n) {
+ int result = (int) ind;
+ * (int*) ind = n;
+ ind += 4;
+ return result;
+ }
+
+ /*
+ * Output a byte. Handles all values, 0..ff.
+ */
+ void ob(int n) {
+ *ind++ = n;
+ }
+
+ /* output a symbol and patch all calls to it */
+ void gsym(int t) {
+ int n;
+ while (t) {
+ n = *(int *) t; /* next value */
+ *(int *) t = ((int) ind) - t - 4;
+ t = n;
+ }
+ }
+
+ /* psym is used to put an instruction with a data field which is a
+ reference to a symbol. It is in fact the same as oad ! */
+ int psym(int n, int t) {
+ return oad(n, t);
+ }
+
+ /* instruction + address */
+ int oad(int n, int t) {
+ o(n);
+ *(int *) ind = t;
+ t = (int) ind;
+ ind = ind + 4;
+ return t;
+ }
+
+ inline void* getBase() {
+ return (void*) pProgramBase;
+ }
+
+ int getSize() {
+ return ind - pProgramBase;
+ }
+
+ int getPC() {
+ return (int) ind;
+ }
+ };
+
+ class CodeGenerator {
+ public:
+ CodeGenerator() {}
+ virtual ~CodeGenerator() {}
+
+ virtual void init(CodeBuf* pCodeBuf) {
+ this->pCodeBuf = pCodeBuf;
+ }
+
+ /* returns address to patch with local variable size
+ */
+ virtual int functionEntry(int argCount) = 0;
+
+ virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) = 0;
+
+ /* load immediate value */
+ virtual void li(int t) = 0;
+
+ virtual int gjmp(int t) = 0;
+
+ /* l = 0: je, l == 1: jne */
+ virtual int gtst(bool l, int t) = 0;
+
+ virtual void gcmp(int op) = 0;
+
+ virtual void genOp(int op) = 0;
+
+ virtual void clearECX() = 0;
+
+ virtual void pushEAX() = 0;
+
+ virtual void popECX() = 0;
+
+ virtual void storeEAXToAddressECX(bool isInt) = 0;
+
+ virtual void loadEAXIndirect(bool isInt) = 0;
+
+ virtual void leaEAX(int ea) = 0;
+
+ virtual void storeEAX(int ea) = 0;
+
+ virtual void loadEAX(int ea, bool isIncDec, int op) = 0;
+
+ virtual int beginFunctionCallArguments() = 0;
+
+ virtual void storeEAToArg(int l) = 0;
+
+ virtual void endFunctionCallArguments(int a, int l) = 0;
+
+
+ virtual int callForward(int symbol) = 0;
+
+ virtual void callRelative(int t) = 0;
+
+ virtual void callIndirect(int l) = 0;
+
+ virtual void adjustStackAfterCall(int l, bool isIndirect) = 0;
+
+ virtual int disassemble(FILE* out) = 0;
+
+ /* output a symbol and patch all calls to it */
+ virtual void gsym(int t) {
+ pCodeBuf->gsym(t);
+ }
+
+ virtual int finishCompile() {
+#if defined(__arm__)
+ const long base = long(pCodeBuf->getBase());
+ const long curr = base + long(pCodeBuf->getSize());
+ int err = cacheflush(base, curr, 0);
+ return err;
+#else
+ return 0;
+#endif
+ }
+
+ /**
+ * Adjust relative branches by this amount.
+ */
+ virtual int jumpOffset() = 0;
+
+ protected:
+ void o(int n) {
+ pCodeBuf->o(n);
+ }
+
+ /*
+ * Output a byte. Handles all values, 0..ff.
+ */
+ void ob(int n) {
+ pCodeBuf->ob(n);
+ }
+
+ /* psym is used to put an instruction with a data field which is a
+ reference to a symbol. It is in fact the same as oad ! */
+ int psym(int n, int t) {
+ return oad(n, t);
+ }
+
+ /* instruction + address */
+ int oad(int n, int t) {
+ return pCodeBuf->oad(n,t);
+ }
+
+ int getBase() {
+ return (int) pCodeBuf->getBase();
+ }
+
+ int getPC() {
+ return pCodeBuf->getPC();
+ }
+
+ int o4(int data) {
+ return pCodeBuf->o4(data);
+ }
+ private:
+ CodeBuf* pCodeBuf;
+ };
+
+ class ARMCodeGenerator : public CodeGenerator {
+ public:
+ ARMCodeGenerator() {}
+ virtual ~ARMCodeGenerator() {}
+
+ /* returns address to patch with local variable size
+ */
+ virtual int functionEntry(int argCount) {
+ fprintf(stderr, "functionEntry(%d);\n", argCount);
+ // sp -> arg4 arg5 ...
+ // Push our register-based arguments back on the stack
+ if (argCount > 0) {
+ int regArgCount = argCount <= 4 ? argCount : 4;
+ o4(0xE92D0000 | ((1 << argCount) - 1)); // stmfd sp!, {}
+ }
+ // sp -> arg0 arg1 ...
+ o4(0xE92D4800); // stmfd sp!, {fp, lr}
+ // sp, fp -> oldfp, retadr, arg0 arg1 ....
+ o4(0xE1A0B00D); // mov fp, sp
+ return o4(0xE24DD000); // sub sp, sp, # <local variables>
+ }
+
+ virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
+ fprintf(stderr, "functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize);
+ // Patch local variable allocation code:
+ if (localVariableSize < 0 || localVariableSize > 255) {
+ error("localVariables out of range: %d", localVariableSize);
+ }
+ *(char*) (localVariableAddress) = localVariableSize;
+
+ // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ...
+ o4(0xE1A0E00B); // mov lr, fp
+ o4(0xE59BB000); // ldr fp, [fp]
+ o4(0xE28ED004); // add sp, lr, #4
+ // sp -> retadr, arg0, ...
+ o4(0xE8BD4000); // ldmfd sp!, {lr}
+ // sp -> arg0 ....
+ if (argCount > 0) {
+ // We store the PC into the lr so we can adjust the sp before
+ // returning. We need to pull off the registers we pushed
+ // earlier. We don't need to actually store them anywhere,
+ // just adjust the stack.
+ int regArgCount = argCount <= 4 ? argCount : 4;
+ o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2
+ }
+ o4(0xE12FFF1E); // bx lr
+ }
+
+ /* load immediate value */
+ virtual void li(int t) {
+ fprintf(stderr, "li(%d);\n", t);
+ if (t >= 0 && t < 255) {
+ o4(0xE3A00000 + t); // mov r0, #0
+ } else if (t >= -256 && t < 0) {
+ // mvn means move constant ^ ~0
+ o4(0xE3E00001 - t); // mvn r0, #0
+ } else {
+ o4(0xE51F0000); // ldr r0, .L3
+ o4(0xEA000000); // b .L99
+ o4(t); // .L3: .word 0
+ // .L99:
+ }
+ }
+
+ virtual int gjmp(int t) {
+ fprintf(stderr, "gjmp(%d);\n", t);
+ return o4(0xEA000000 | encodeAddress(t)); // b .L33
+ }
+
+ /* l = 0: je, l == 1: jne */
+ virtual int gtst(bool l, int t) {
+ fprintf(stderr, "gtst(%d, %d);\n", l, t);
+ o4(0xE3500000); // cmp r0,#0
+ int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq
+ return o4(branch | encodeAddress(t));
+ }
+
+ virtual void gcmp(int op) {
+ fprintf(stderr, "gcmp(%d);\n", op);
+ o4(0xE1510000); // cmp r1, r1
+ switch(op) {
+ case OP_EQUALS:
+ o4(0x03A00001); // moveq r0,#1
+ o4(0x13A00000); // movne r0,#0
+ break;
+ case OP_NOT_EQUALS:
+ o4(0x03A00000); // moveq r0,#0
+ o4(0x13A00001); // movne r0,#1
+ break;
+ case OP_LESS_EQUAL:
+ o4(0xD3A00001); // movle r0,#1
+ o4(0xC3A00000); // movgt r0,#0
+ break;
+ case OP_GREATER:
+ o4(0xD3A00000); // movle r0,#0
+ o4(0xC3A00001); // movgt r0,#1
+ break;
+ case OP_GREATER_EQUAL:
+ o4(0xA3A00001); // movge r0,#1
+ o4(0xB3A00000); // movlt r0,#0
+ break;
+ case OP_LESS:
+ o4(0xA3A00000); // movge r0,#0
+ o4(0xB3A00001); // movlt r0,#1
+ break;
+ default:
+ error("Unknown comparison op %d", op);
+ break;
+ }
+ }
+
+ virtual void genOp(int op) {
+ fprintf(stderr, "genOp(%d);\n", op);
+ switch(op) {
+ case OP_MUL:
+ o4(0x0E0000091); // mul r0,r1,r0
+ break;
+ case OP_DIV:
+ callRuntime(runtime_DIV);
+ break;
+ case OP_MOD:
+ callRuntime(runtime_MOD);
+ break;
+ case OP_PLUS:
+ o4(0xE0810000); // add r0,r1,r0
+ break;
+ case OP_MINUS:
+ o4(0xE0410000); // sub r0,r1,r0
+ break;
+ case OP_SHIFT_LEFT:
+ o4(0xE1A00011); // lsl r0,r1,r0
+ break;
+ case OP_SHIFT_RIGHT:
+ o4(0xE1A00051); // asr r0,r1,r0
+ break;
+ case OP_BIT_AND:
+ o4(0xE0010000); // and r0,r1,r0
+ break;
+ case OP_BIT_XOR:
+ o4(0xE0210000); // eor r0,r1,r0
+ break;
+ case OP_BIT_OR:
+ o4(0xE1810000); // orr r0,r1,r0
+ break;
+ case OP_BIT_NOT:
+ o4(0xE1E00000); // mvn r0, r0
+ break;
+ default:
+ error("Unimplemented op %d\n", op);
+ break;
+ }
+#if 0
+ o(decodeOp(op));
+ if (op == OP_MOD)
+ o(0x92); /* xchg %edx, %eax */
+#endif
+ }
+
+ virtual void clearECX() {
+ fprintf(stderr, "clearECX();\n");
+ o4(0xE3A01000); // mov r1, #0
+ }
+
+ virtual void pushEAX() {
+ fprintf(stderr, "pushEAX();\n");
+ o4(0xE92D0001); // stmfd sp!,{r0}
+ }
+
+ virtual void popECX() {
+ fprintf(stderr, "popECX();\n");
+ o4(0xE8BD0002); // ldmfd sp!,{r1}
+ }
+
+ virtual void storeEAXToAddressECX(bool isInt) {
+ fprintf(stderr, "storeEAXToAddressECX(%d);\n", isInt);
+ if (isInt) {
+ o4(0xE5810000); // str r0, [r1]
+ } else {
+ o4(0xE5C10000); // strb r0, [r1]
+ }
+ }
+
+ virtual void loadEAXIndirect(bool isInt) {
+ fprintf(stderr, "loadEAXIndirect(%d);\n", isInt);
+ if (isInt)
+ o4(0xE5900000); // ldr r0, [r0]
+ else
+ o4(0xE5D00000); // ldrb r0, [r0]
+ }
+
+ virtual void leaEAX(int ea) {
+ fprintf(stderr, "leaEAX(%d);\n", ea);
+ if (ea < LOCAL) {
+ // Local, fp relative
+ if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) {
+ error("Offset out of range: %08x", ea);
+ }
+ if (ea < 0) {
+ o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea
+ } else {
+ o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea
+ }
+ } else {
+ // Global, absolute.
+ o4(0xE59F0000); // ldr r0, .L1
+ o4(0xEA000000); // b .L99
+ o4(ea); // .L1: .word 0
+ // .L99:
+ }
+ }
+
+ virtual void storeEAX(int ea) {
+ fprintf(stderr, "storeEAX(%d);\n", ea);
+ if (ea < LOCAL) {
+ // Local, fp relative
+ if (ea < -4095 || ea > 4095) {
+ error("Offset out of range: %08x", ea);
+ }
+ if (ea < 0) {
+ o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea]
+ } else {
+ o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea]
+ }
+ } else{
+ // Global, absolute
+ o4(0xE59F1000); // ldr r1, .L1
+ o4(0xEA000000); // b .L99
+ o4(ea); // .L1: .word 0
+ o4(0xE5810000); // .L99: str r0, [r1]
+ }
+ }
+
+ virtual void loadEAX(int ea, bool isIncDec, int op) {
+ fprintf(stderr, "loadEAX(%d, %d, %d);\n", ea, isIncDec, op);
+ if (ea < LOCAL) {
+ // Local, fp relative
+ if (ea < -4095 || ea > 4095) {
+ error("Offset out of range: %08x", ea);
+ }
+ if (ea < 0) {
+ o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea]
+ } else {
+ o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea]
+ }
+ } else {
+ // Global, absolute
+ o4(0xE59F2000); // ldr r2, .L1
+ o4(0xEA000000); // b .L99
+ o4(ea); // .L1: .word ea
+ o4(0xE5920000); // .L99: ldr r0, [r2]
+ }
+
+ if (isIncDec) {
+ switch (op) {
+ case OP_INCREMENT:
+ o4(0xE2801001); // add r1, r0, #1
+ break;
+ case OP_DECREMENT:
+ o4(0xE2401001); // sub r1, r0, #1
+ break;
+ default:
+ error("unknown opcode: %d", op);
+ }
+ if (ea < LOCAL) {
+ // Local, fp relative
+ // Don't need range check, was already checked above
+ if (ea < 0) {
+ o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea]
+ } else {
+ o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea]
+ }
+ } else{
+ // Global, absolute
+ // r2 is already set up from before.
+ o4(0xE5821000); // str r1, [r2]
+ }
+ }
+ }
+
+ virtual int beginFunctionCallArguments() {
+ fprintf(stderr, "beginFunctionCallArguments();\n");
+ return o4(0xE24DDF00); // Placeholder
+ }
+
+ virtual void storeEAToArg(int l) {
+ fprintf(stderr, "storeEAToArg(%d);\n", l);
+ if (l < 0 || l > 4096-4) {
+ error("l out of range for stack offset: 0x%08x", l);
+ }
+ o4(0xE58D0000 + l); // str r0, [sp, #4]
+ }
+
+ virtual void endFunctionCallArguments(int a, int l) {
+ fprintf(stderr, "endFunctionCallArguments(0x%08x, %d);\n", a, l);
+ if (l < 0 || l > 0x3FC) {
+ error("L out of range for stack adjustment: 0x%08x", l);
+ }
+ * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2
+ int argCount = l >> 2;
+ if (argCount > 0) {
+ int regArgCount = argCount > 4 ? 4 : argCount;
+ o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{}
+ }
+ }
+
+ virtual int callForward(int symbol) {
+ fprintf(stderr, "callForward(%d);\n", symbol);
+ // Forward calls are always short (local)
+ return o4(0xEB000000 | encodeAddress(symbol));
+ }
+
+ virtual void callRelative(int t) {
+ fprintf(stderr, "callRelative(%d);\n", t);
+ int abs = t + getPC() + jumpOffset();
+ fprintf(stderr, "abs=%d (0x%08x)\n", abs, abs);
+ if (t >= - (1 << 25) && t < (1 << 25)) {
+ o4(0xEB000000 | encodeAddress(t));
+ } else {
+ // Long call.
+ o4(0xE59FC000); // ldr r12, .L1
+ o4(0xEA000000); // b .L99
+ o4(t - 12); // .L1: .word 0
+ o4(0xE08CC00F); // .L99: add r12,pc
+ o4(0xE12FFF3C); // blx r12
+ }
+ }
+
+ virtual void callIndirect(int l) {
+ fprintf(stderr, "callIndirect(%d);\n", l);
+ int argCount = l >> 2;
+ int poppedArgs = argCount > 4 ? 4 : argCount;
+ int adjustedL = l - (poppedArgs << 2);
+ if (adjustedL < 0 || adjustedL > 4096-4) {
+ error("l out of range for stack offset: 0x%08x", l);
+ }
+ o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL]
+ o4(0xE12FFF3C); // blx r12
+ }
+
+ virtual void adjustStackAfterCall(int l, bool isIndirect) {
+ fprintf(stderr, "adjustStackAfterCall(%d, %d);\n", l, isIndirect);
+ int argCount = l >> 2;
+ int stackArgs = argCount > 4 ? argCount - 4 : 0;
+ int stackUse = stackArgs + (isIndirect ? 1 : 0);
+ if (stackUse) {
+ if (stackUse < 0 || stackUse > 255) {
+ error("L out of range for stack adjustment: 0x%08x", l);
+ }
+ o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2
+ }
+ }
+
+ virtual int jumpOffset() {
+ return 8;
+ }
+
+ /* output a symbol and patch all calls to it */
+ virtual void gsym(int t) {
+ fprintf(stderr, "gsym(0x%x)\n", t);
+ int n;
+ int base = getBase();
+ int pc = getPC();
+ fprintf(stderr, "pc = 0x%x\n", pc);
+ while (t) {
+ int data = * (int*) t;
+ int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2);
+ if (decodedOffset == 0) {
+ n = 0;
+ } else {
+ n = base + decodedOffset; /* next value */
+ }
+ *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK)
+ | encodeRelAddress(pc - t - 8);
+ t = n;
+ }
+ }
+
+ virtual int disassemble(FILE* out) {
+ disasmOut = out;
+ disasm_interface_t di;
+ di.di_readword = disassemble_readword;
+ di.di_printaddr = disassemble_printaddr;
+ di.di_printf = disassemble_printf;
+
+ int base = getBase();
+ int pc = getPC();
+ for(int i = base; i < pc; i += 4) {
+ fprintf(out, "%08x: %08x ", i, *(int*) i);
+ ::disasm(&di, i, 0);
+ }
+ return 0;
+ }
+
+ private:
+ static FILE* disasmOut;
+
+ static u_int
+ disassemble_readword(u_int address)
+ {
+ return(*((u_int *)address));
+ }
+
+ static void
+ disassemble_printaddr(u_int address)
+ {
+ fprintf(disasmOut, "0x%08x", address);
+ }
+
+ static void
+ disassemble_printf(const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ vfprintf(disasmOut, fmt, ap);
+ va_end(ap);
+ }
+
+ static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff;
+
+ /** Encode a relative address that might also be
+ * a label.
+ */
+ int encodeAddress(int value) {
+ int base = getBase();
+ if (value >= base && value <= getPC() ) {
+ // This is a label, encode it relative to the base.
+ value = value - base;
+ }
+ return encodeRelAddress(value);
+ }
+
+ int encodeRelAddress(int value) {
+ return BRANCH_REL_ADDRESS_MASK & (value >> 2);
+ }
+
+ typedef int (*int2FnPtr)(int a, int b);
+ void callRuntime(int2FnPtr fn) {
+ o4(0xE59F2000); // ldr r2, .L1
+ o4(0xEA000000); // b .L99
+ o4((int) fn); //.L1: .word fn
+ o4(0xE12FFF32); //.L99: blx r2
+ }
+
+ static int runtime_DIV(int a, int b) {
+ return b / a;
+ }
+
+ static int runtime_MOD(int a, int b) {
+ return b % a;
+ }
+
+ void error(const char* fmt,...) {
+ va_list ap;
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ exit(12);
+ }
+ };
+
+ class X86CodeGenerator : public CodeGenerator {
+ public:
+ X86CodeGenerator() {}
+ virtual ~X86CodeGenerator() {}
+
+ /* returns address to patch with local variable size
+ */
+ virtual int functionEntry(int argCount) {
+ o(0xe58955); /* push %ebp, mov %esp, %ebp */
+ return oad(0xec81, 0); /* sub $xxx, %esp */
+ }
+
+ virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
+ o(0xc3c9); /* leave, ret */
+ *(int *) localVariableAddress = localVariableSize; /* save local variables */
+ }
+
+ /* load immediate value */
+ virtual void li(int t) {
+ oad(0xb8, t); /* mov $xx, %eax */
+ }
+
+ virtual int gjmp(int t) {
+ return psym(0xe9, t);
+ }
+
+ /* l = 0: je, l == 1: jne */
+ virtual int gtst(bool l, int t) {
+ o(0x0fc085); /* test %eax, %eax, je/jne xxx */
+ return psym(0x84 + l, t);
+ }
+
+ virtual void gcmp(int op) {
+ int t = decodeOp(op);
+ o(0xc139); /* cmp %eax,%ecx */
+ li(0);
+ o(0x0f); /* setxx %al */
+ o(t + 0x90);
+ o(0xc0);
+ }
+
+ virtual void genOp(int op) {
+ o(decodeOp(op));
+ if (op == OP_MOD)
+ o(0x92); /* xchg %edx, %eax */
+ }
+
+ virtual void clearECX() {
+ oad(0xb9, 0); /* movl $0, %ecx */
+ }
+
+ virtual void pushEAX() {
+ o(0x50); /* push %eax */
+ }
+
+ virtual void popECX() {
+ o(0x59); /* pop %ecx */
+ }
+
+ virtual void storeEAXToAddressECX(bool isInt) {
+ o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
+ }
+
+ virtual void loadEAXIndirect(bool isInt) {
+ if (isInt)
+ o(0x8b); /* mov (%eax), %eax */
+ else
+ o(0xbe0f); /* movsbl (%eax), %eax */
+ ob(0); /* add zero in code */
+ }
+
+ virtual void leaEAX(int ea) {
+ gmov(10, ea); /* leal EA, %eax */
+ }
+
+ virtual void storeEAX(int ea) {
+ gmov(6, ea); /* mov %eax, EA */
+ }
+
+ virtual void loadEAX(int ea, bool isIncDec, int op) {
+ gmov(8, ea); /* mov EA, %eax */
+ if (isIncDec) {
+ /* Implement post-increment or post decrement.
+ */
+ gmov(0, ea); /* 83 ADD */
+ o(decodeOp(op));
+ }
+ }
+
+ virtual int beginFunctionCallArguments() {
+ return oad(0xec81, 0); /* sub $xxx, %esp */
+ }
+
+ virtual void storeEAToArg(int l) {
+ oad(0x248489, l); /* movl %eax, xxx(%esp) */
+ }
+
+ virtual void endFunctionCallArguments(int a, int l) {
+ * (int*) a = l;
+ }
+
+ virtual int callForward(int symbol) {
+ return psym(0xe8, symbol); /* call xxx */
+ }
+
+ virtual void callRelative(int t) {
+ psym(0xe8, t); /* call xxx */
+ }
+
+ virtual void callIndirect(int l) {
+ oad(0x2494ff, l); /* call *xxx(%esp) */
+ }
+
+ virtual void adjustStackAfterCall(int l, bool isIndirect) {
+ if (isIndirect) {
+ l += 4;
+ }
+ oad(0xc481, l); /* add $xxx, %esp */
+ }
+
+ virtual int jumpOffset() {
+ return 5;
+ }
+
+ virtual int disassemble(FILE* out) {
+ return 1;
+ }
+
+ private:
+ static const int operatorHelper[];
+
+ int decodeOp(int op) {
+ if (op < 0 || op > OP_COUNT) {
+ fprintf(stderr, "Out-of-range operator: %d\n", op);
+ exit(1);
+ }
+ return operatorHelper[op];
+ }
+
+ void gmov(int l, int t) {
+ o(l + 0x83);
+ oad((t < LOCAL) << 7 | 5, t);
+ }
+ };
+
+ /* vars: value of variables
+ loc : local variable index
+ glo : global variable index
+ ind : output code ptr
+ rsym: return symbol
+ prog: output code
+ dstk: define stack
+ dptr, dch: macro state
+ */
+ int tok, tokc, tokl, ch, vars, rsym, loc, glo, sym_stk, dstk,
+ dptr, dch, last_id;
+ void* pSymbolBase;
+ void* pGlobalBase;
+ void* pVarsBase;
+ FILE* file;
+
+ CodeBuf codeBuf;
+ CodeGenerator* pGen;
+
+ static const int ALLOC_SIZE = 99999;
+
+ /* depends on the init string */
+ static const int TOK_STR_SIZE = 48;
+ static const int TOK_IDENT = 0x100;
+ static const int TOK_INT = 0x100;
+ static const int TOK_IF = 0x120;
+ static const int TOK_ELSE = 0x138;
+ static const int TOK_WHILE = 0x160;
+ static const int TOK_BREAK = 0x190;
+ static const int TOK_RETURN = 0x1c0;
+ static const int TOK_FOR = 0x1f8;
+ static const int TOK_DEFINE = 0x218;
+ static const int TOK_MAIN = 0x250;
+
+ static const int TOK_DUMMY = 1;
+ static const int TOK_NUM = 2;
+
+ static const int LOCAL = 0x200;
+
+ static const int SYM_FORWARD = 0;
+ static const int SYM_DEFINE = 1;
+
+ /* tokens in string heap */
+ static const int TAG_TOK = ' ';
+ static const int TAG_MACRO = 2;
+
+ static const int OP_INCREMENT = 0;
+ static const int OP_DECREMENT = 1;
+ static const int OP_MUL = 2;
+ static const int OP_DIV = 3;
+ static const int OP_MOD = 4;
+ static const int OP_PLUS = 5;
+ static const int OP_MINUS = 6;
+ static const int OP_SHIFT_LEFT = 7;
+ static const int OP_SHIFT_RIGHT = 8;
+ static const int OP_LESS_EQUAL = 9;
+ static const int OP_GREATER_EQUAL = 10;
+ static const int OP_LESS = 11;
+ static const int OP_GREATER = 12;
+ static const int OP_EQUALS = 13;
+ static const int OP_NOT_EQUALS = 14;
+ static const int OP_LOGICAL_AND = 15;
+ static const int OP_LOGICAL_OR = 16;
+ static const int OP_BIT_AND = 17;
+ static const int OP_BIT_XOR = 18;
+ static const int OP_BIT_OR = 19;
+ static const int OP_BIT_NOT = 20;
+ static const int OP_LOGICAL_NOT = 21;
+ static const int OP_COUNT = 22;
+
+ /* Operators are searched from front, the two-character operators appear
+ * before the single-character operators with the same first character.
+ * @ is used to pad out single-character operators.
+ */
+ static const char* operatorChars;
+ static const char operatorLevel[];
+
+ void pdef(int t) {
+ *(char *) dstk++ = t;
+ }
+
+ void inp() {
+ if (dptr) {
+ ch = *(char *) dptr++;
+ if (ch == TAG_MACRO) {
+ dptr = 0;
+ ch = dch;
+ }
+ } else
+ ch = fgetc(file);
+ /* printf("ch=%c 0x%x\n", ch, ch); */
+ }
+
+ int isid() {
+ return isalnum(ch) | (ch == '_');
+ }
+
+ /* read a character constant */
+ void getq() {
+ if (ch == '\\') {
+ inp();
+ if (ch == 'n')
+ ch = '\n';
+ }
+ }
+
+ void next() {
+ int l, a;
+
+ while (isspace(ch) | (ch == '#')) {
+ if (ch == '#') {
+ inp();
+ next();
+ if (tok == TOK_DEFINE) {
+ next();
+ pdef(TAG_TOK); /* fill last ident tag */
+ *(int *) tok = SYM_DEFINE;
+ *(int *) (tok + 4) = dstk; /* define stack */
+ }
+ /* well we always save the values ! */
+ while (ch != '\n') {
+ pdef(ch);
+ inp();
+ }
+ pdef(ch);
+ pdef(TAG_MACRO);
+ }
+ inp();
+ }
+ tokl = 0;
+ tok = ch;
+ /* encode identifiers & numbers */
+ if (isid()) {
+ pdef(TAG_TOK);
+ last_id = dstk;
+ while (isid()) {
+ pdef(ch);
+ inp();
+ }
+ if (isdigit(tok)) {
+ tokc = strtol((char*) last_id, 0, 0);
+ tok = TOK_NUM;
+ } else {
+ *(char *) dstk = TAG_TOK; /* no need to mark end of string (we
+ suppose data is initialized to zero by calloc) */
+ tok = (int) (strstr((char*) sym_stk, (char*) (last_id - 1))
+ - sym_stk);
+ *(char *) dstk = 0; /* mark real end of ident for dlsym() */
+ tok = tok * 8 + TOK_IDENT;
+ if (tok > TOK_DEFINE) {
+ tok = vars + tok;
+ /* printf("tok=%s %x\n", last_id, tok); */
+ /* define handling */
+ if (*(int *) tok == SYM_DEFINE) {
+ dptr = *(int *) (tok + 4);
+ dch = ch;
+ inp();
+ next();
+ }
+ }
+ }
+ } else {
+ inp();
+ if (tok == '\'') {
+ tok = TOK_NUM;
+ getq();
+ tokc = ch;
+ inp();
+ inp();
+ } else if ((tok == '/') & (ch == '*')) {
+ inp();
+ while (ch) {
+ while (ch != '*')
+ inp();
+ inp();
+ if (ch == '/')
+ ch = 0;
+ }
+ inp();
+ next();
+ } else if ((tok == '/') & (ch == '/')) {
+ inp();
+ while (ch && (ch != '\n')) {
+ inp();
+ }
+ inp();
+ next();
+ } else {
+ const char* t = operatorChars;
+ int opIndex = 0;
+ while ((l = *t++) != 0) {
+ a = *t++;
+ tokl = operatorLevel[opIndex];
+ tokc = opIndex;
+ if ((l == tok) & ((a == ch) | (a == '@'))) {
+#if 0
+ printf("%c%c -> tokl=%d tokc=0x%x\n",
+ l, a, tokl, tokc);
+#endif
+ if (a == ch) {
+ inp();
+ tok = TOK_DUMMY; /* dummy token for double tokens */
+ }
+ break;
+ }
+ opIndex++;
+ }
+ if (l == 0) {
+ tokl = 0;
+ tokc = 0;
+ }
+ }
+ }
+#if 0
+ {
+ int p;
+
+ printf("tok=0x%x ", tok);
+ if (tok >= TOK_IDENT) {
+ printf("'");
+ if (tok> TOK_DEFINE)
+ p = sym_stk + 1 + (tok - vars - TOK_IDENT) / 8;
+ else
+ p = sym_stk + 1 + (tok - TOK_IDENT) / 8;
+ while (*(char *)p != TAG_TOK && *(char *)p)
+ printf("%c", *(char *)p++);
+ printf("'\n");
+ } else if (tok == TOK_NUM) {
+ printf("%d\n", tokc);
+ } else {
+ printf("'%c'\n", tok);
+ }
+ }
+#endif
+ }
+
+ void error(const char *fmt, ...) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ fprintf(stderr, "%ld: ", ftell((FILE *) file));
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
+ va_end(ap);
+ exit(1);
+ }
+
+ void skip(int c) {
+ if (tok != c) {
+ error("'%c' expected", c);
+ }
+ next();
+ }
+
+ /* l is one if '=' parsing wanted (quick hack) */
+ void unary(int l) {
+ int n, t, a, c;
+ t = 0;
+ n = 1; /* type of expression 0 = forward, 1 = value, other =
+ lvalue */
+ if (tok == '\"') {
+ pGen->li(glo);
+ while (ch != '\"') {
+ getq();
+ *(char *) glo++ = ch;
+ inp();
+ }
+ *(char *) glo = 0;
+ glo = (glo + 4) & -4; /* align heap */
+ inp();
+ next();
+ } else {
+ c = tokl;
+ a = tokc;
+ t = tok;
+ next();
+ if (t == TOK_NUM) {
+ pGen->li(a);
+ } else if (c == 2) {
+ /* -, +, !, ~ */
+ unary(0);
+ pGen->clearECX();
+ if (t == '!')
+ pGen->gcmp(a);
+ else
+ pGen->genOp(a);
+ } else if (t == '(') {
+ expr();
+ skip(')');
+ } else if (t == '*') {
+ /* parse cast */
+ skip('(');
+ t = tok; /* get type */
+ next(); /* skip int/char/void */
+ next(); /* skip '*' or '(' */
+ if (tok == '*') {
+ /* function type */
+ skip('*');
+ skip(')');
+ skip('(');
+ skip(')');
+ t = 0;
+ }
+ skip(')');
+ unary(0);
+ if (tok == '=') {
+ next();
+ pGen->pushEAX();
+ expr();
+ pGen->popECX();
+ pGen->storeEAXToAddressECX(t == TOK_INT);
+ } else if (t) {
+ pGen->loadEAXIndirect(t == TOK_INT);
+ }
+ } else if (t == '&') {
+ pGen->leaEAX(*(int *) tok);
+ next();
+ } else {
+ n = *(int *) t;
+ /* forward reference: try dlsym */
+ if (!n) {
+ n = (int) dlsym(RTLD_DEFAULT, (char*) last_id);
+ }
+ if ((tok == '=') & l) {
+ /* assignment */
+ next();
+ expr();
+ pGen->storeEAX(n);
+ } else if (tok != '(') {
+ /* variable */
+ pGen->loadEAX(n, tokl == 11, tokc);
+ if (tokl == 11) {
+ next();
+ }
+ }
+ }
+ }
+
+ /* function call */
+ if (tok == '(') {
+ if (n == 1)
+ pGen->pushEAX();
+
+ /* push args and invert order */
+ a = pGen->beginFunctionCallArguments();
+ next();
+ l = 0;
+ while (tok != ')') {
+ expr();
+ pGen->storeEAToArg(l);
+ if (tok == ',')
+ next();
+ l = l + 4;
+ }
+ pGen->endFunctionCallArguments(a, l);
+ next();
+ if (!n) {
+ /* forward reference */
+ t = t + 4;
+ *(int *) t = pGen->callForward(*(int *) t);
+ } else if (n == 1) {
+ pGen->callIndirect(l);
+ } else {
+ pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset());
+ }
+ if (l | (n == 1))
+ pGen->adjustStackAfterCall(l, n == 1);
+ }
+ }
+
+ void sum(int l) {
+ int t, n, a;
+ t = 0;
+ if (l-- == 1)
+ unary(1);
+ else {
+ sum(l);
+ a = 0;
+ while (l == tokl) {
+ n = tok;
+ t = tokc;
+ next();
+
+ if (l > 8) {
+ a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
+ sum(l);
+ } else {
+ pGen->pushEAX();
+ sum(l);
+ pGen->popECX();
+
+ if ((l == 4) | (l == 5)) {
+ pGen->gcmp(t);
+ } else {
+ pGen->genOp(t);
+ }
+ }
+ }
+ /* && and || output code generation */
+ if (a && l > 8) {
+ a = pGen->gtst(t == OP_LOGICAL_OR, a);
+ pGen->li(t != OP_LOGICAL_OR);
+ pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */
+ pGen->gsym(a);
+ pGen->li(t == OP_LOGICAL_OR);
+ }
+ }
+ }
+
+ void expr() {
+ sum(11);
+ }
+
+ int test_expr() {
+ expr();
+ return pGen->gtst(0, 0);
+ }
+
+ void block(int l) {
+ int a, n, t;
+
+ if (tok == TOK_IF) {
+ next();
+ skip('(');
+ a = test_expr();
+ skip(')');
+ block(l);
+ if (tok == TOK_ELSE) {
+ next();
+ n = pGen->gjmp(0); /* jmp */
+ pGen->gsym(a);
+ block(l);
+ pGen->gsym(n); /* patch else jmp */
+ } else {
+ pGen->gsym(a); /* patch if test */
+ }
+ } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) {
+ t = tok;
+ next();
+ skip('(');
+ if (t == TOK_WHILE) {
+ n = codeBuf.getPC(); // top of loop, target of "next" iteration
+ a = test_expr();
+ } else {
+ if (tok != ';')
+ expr();
+ skip(';');
+ n = codeBuf.getPC();
+ a = 0;
+ if (tok != ';')
+ a = test_expr();
+ skip(';');
+ if (tok != ')') {
+ t = pGen->gjmp(0);
+ expr();
+ pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset());
+ pGen->gsym(t);
+ n = t + 4;
+ }
+ }
+ skip(')');
+ block((int) &a);
+ pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */
+ pGen->gsym(a);
+ } else if (tok == '{') {
+ next();
+ /* declarations */
+ decl(1);
+ while (tok != '}')
+ block(l);
+ next();
+ } else {
+ if (tok == TOK_RETURN) {
+ next();
+ if (tok != ';')
+ expr();
+ rsym = pGen->gjmp(rsym); /* jmp */
+ } else if (tok == TOK_BREAK) {
+ next();
+ *(int *) l = pGen->gjmp(*(int *) l);
+ } else if (tok != ';')
+ expr();
+ skip(';');
+ }
+ }
+
+ /* 'l' is true if local declarations */
+ void decl(int l) {
+ int a;
+
+ while ((tok == TOK_INT) | ((tok != -1) & (!l))) {
+ if (tok == TOK_INT) {
+ next();
+ while (tok != ';') {
+ if (l) {
+ loc = loc + 4;
+ *(int *) tok = -loc;
+ } else {
+ *(int *) tok = glo;
+ glo = glo + 4;
+ }
+ next();
+ if (tok == ',')
+ next();
+ }
+ skip(';');
+ } else {
+ /* patch forward references (XXX: do not work for function
+ pointers) */
+ pGen->gsym(*(int *) (tok + 4));
+ /* put function address */
+ *(int *) tok = codeBuf.getPC();
+ next();
+ skip('(');
+ a = 8;
+ int argCount = 0;
+ while (tok != ')') {
+ /* read param name and compute offset */
+ *(int *) tok = a;
+ a = a + 4;
+ next();
+ if (tok == ',')
+ next();
+ argCount++;
+ }
+ next(); /* skip ')' */
+ rsym = loc = 0;
+ a = pGen->functionEntry(argCount);
+ block(0);
+ pGen->gsym(rsym);
+ pGen->functionExit(argCount, a, loc);
+ }
+ }
+ }
+
+ void cleanup() {
+ if (sym_stk != 0) {
+ free((void*) sym_stk);
+ sym_stk = 0;
+ }
+ if (pGlobalBase != 0) {
+ free((void*) pGlobalBase);
+ pGlobalBase = 0;
+ }
+ if (pVarsBase != 0) {
+ free(pVarsBase);
+ pVarsBase = 0;
+ }
+ if (pGen) {
+ delete pGen;
+ pGen = 0;
+ }
+ }
+
+ void clear() {
+ tok = 0;
+ tokc = 0;
+ tokl = 0;
+ ch = 0;
+ vars = 0;
+ rsym = 0;
+ loc = 0;
+ glo = 0;
+ sym_stk = 0;
+ dstk = 0;
+ dptr = 0;
+ dch = 0;
+ last_id = 0;
+ file = 0;
+ pGlobalBase = 0;
+ pVarsBase = 0;
+ pGen = 0;
+ }
+
+ void setArchitecture(const char* architecture) {
+ delete pGen;
+ pGen = 0;
+
+ if (architecture != NULL) {
+ if (strcmp(architecture, "arm") == 0) {
+ pGen = new ARMCodeGenerator();
+ } else if (strcmp(architecture, "x86") == 0) {
+ pGen = new X86CodeGenerator();
+ } else {
+ fprintf(stderr, "Unknown architecture %s", architecture);
+ }
+ }
+
+ if (pGen == NULL) {
+ pGen = new ARMCodeGenerator();
+ }
+ }
+
+public:
+ struct args {
+ args() {
+ architecture = 0;
+ }
+ const char* architecture;
+ };
+
+ compiler() {
+ clear();
+ }
+
+ ~compiler() {
+ cleanup();
+ }
+
+ int compile(FILE* in, args& args) {
+ cleanup();
+ clear();
+ codeBuf.init(ALLOC_SIZE);
+ setArchitecture(args.architecture);
+ pGen->init(&codeBuf);
+ file = in;
+ sym_stk = (int) calloc(1, ALLOC_SIZE);
+ dstk = (int) strcpy((char*) sym_stk,
+ " int if else while break return for define main ")
+ + TOK_STR_SIZE;
+ pGlobalBase = calloc(1, ALLOC_SIZE);
+ glo = (int) pGlobalBase;
+ pVarsBase = calloc(1, ALLOC_SIZE);
+ vars = (int) pVarsBase;
+ inp();
+ next();
+ decl(0);
+ pGen->finishCompile();
+ return 0;
+ }
+
+ int run(int argc, char** argv) {
+ typedef int (*mainPtr)(int argc, char** argv);
+ mainPtr aMain = (mainPtr) *(int*) (vars + TOK_MAIN);
+ if (!aMain) {
+ fprintf(stderr, "Could not find function \"main\".\n");
+ return -1;
+ }
+ return aMain(argc, argv);
+ }
+
+ int dump(FILE* out) {
+ fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out);
+ return 0;
+ }
+
+ int disassemble(FILE* out) {
+ return pGen->disassemble(out);
+ }
+
+};
+
+const char* compiler::operatorChars =
+ "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
+
+const char compiler::operatorLevel[] =
+ {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
+ 5, 5, /* ==, != */
+ 9, 10, /* &&, || */
+ 6, 7, 8, /* & ^ | */
+ 2, 2 /* ~ ! */
+ };
+
+FILE* compiler::ARMCodeGenerator::disasmOut;
+
+const int compiler::X86CodeGenerator::operatorHelper[] = {
+ 0x1, // ++
+ 0xff, // --
+ 0xc1af0f, // *
+ 0xf9f79991, // /
+ 0xf9f79991, // % (With manual assist to swap results)
+ 0xc801, // +
+ 0xd8f7c829, // -
+ 0xe0d391, // <<
+ 0xf8d391, // >>
+ 0xe, // <=
+ 0xd, // >=
+ 0xc, // <
+ 0xf, // >
+ 0x4, // ==
+ 0x5, // !=
+ 0x0, // &&
+ 0x1, // ||
+ 0xc821, // &
+ 0xc831, // ^
+ 0xc809, // |
+ 0xd0f7, // ~
+ 0x4 // !
+};
+
+} // namespace acc
+
+// This is a separate function so it can easily be set by breakpoint in gdb.
+int run(acc::compiler& c, int argc, char** argv) {
+ return c.run(argc, argv);
+}
+
+int main(int argc, char** argv) {
+ bool doDump = false;
+ bool doDisassemble = false;
+ const char* inFile = NULL;
+ const char* outFile = NULL;
+ const char* architecture = "arm";
+ int i;
+ for (i = 1; i < argc; i++) {
+ char* arg = argv[i];
+ if (arg[0] == '-') {
+ switch (arg[1]) {
+ case 'a':
+ if (i + 1 >= argc) {
+ fprintf(stderr, "Expected architecture after -a\n");
+ return 2;
+ }
+ architecture = argv[i+1];
+ i += 1;
+ break;
+ case 'd':
+ if (i + 1 >= argc) {
+ fprintf(stderr, "Expected filename after -d\n");
+ return 2;
+ }
+ doDump = true;
+ outFile = argv[i + 1];
+ i += 1;
+ break;
+ case 'S':
+ doDisassemble = true;
+ break;
+ default:
+ fprintf(stderr, "Unrecognized flag %s\n", arg);
+ return 3;
+ }
+ } else if (inFile == NULL) {
+ inFile = arg;
+ } else {
+ break;
+ }
+ }
+
+ FILE* in = stdin;
+ if (inFile) {
+ in = fopen(inFile, "r");
+ if (!in) {
+ fprintf(stderr, "Could not open input file %s\n", inFile);
+ return 1;
+ }
+ }
+ acc::compiler compiler;
+ acc::compiler::args args;
+ args.architecture = architecture;
+ int compileResult = compiler.compile(in, args);
+ if (in != stdin) {
+ fclose(in);
+ }
+ if (compileResult) {
+ fprintf(stderr, "Compile failed: %d\n", compileResult);
+ return 6;
+ }
+ if (doDisassemble) {
+ compiler.disassemble(stderr);
+ }
+ if (doDump) {
+ FILE* save = fopen(outFile, "w");
+ if (!save) {
+ fprintf(stderr, "Could not open output file %s\n", outFile);
+ return 5;
+ }
+ compiler.dump(save);
+ fclose(save);
+ } else {
+ fprintf(stderr, "Executing compiled code:\n");
+ int codeArgc = argc - i + 1;
+ char** codeArgv = argv + i - 1;
+ codeArgv[0] = (char*) (inFile ? inFile : "stdin");
+ int result = run(compiler, codeArgc, codeArgv);
+ fprintf(stderr, "result: %d\n", result);
+ return result;
+ }
+
+ return 0;
+}
diff --git a/libacc/armreg.h b/libacc/armreg.h
new file mode 100644
index 0000000..fde81ba
--- /dev/null
+++ b/libacc/armreg.h
@@ -0,0 +1,300 @@
+/* $NetBSD: armreg.h,v 1.28 2003/10/31 16:30:15 scw Exp $ */
+
+/*-
+ * Copyright (c) 1998, 2001 Ben Harris
+ * Copyright (c) 1994-1996 Mark Brinicombe.
+ * Copyright (c) 1994 Brini.
+ * All rights reserved.
+ *
+ * This code is derived from software written for Brini by Mark Brinicombe
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Brini.
+ * 4. The name of the company nor the name of the author may be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: /repoman/r/ncvs/src/sys/arm/include/armreg.h,v 1.3 2005/11/21 19:06:25 cognet Exp $
+ */
+
+#ifndef MACHINE_ARMREG_H
+#define MACHINE_ARMREG_H
+#define INSN_SIZE 4
+#define INSN_COND_MASK 0xf0000000 /* Condition mask */
+#define PSR_MODE 0x0000001f /* mode mask */
+#define PSR_USR26_MODE 0x00000000
+#define PSR_FIQ26_MODE 0x00000001
+#define PSR_IRQ26_MODE 0x00000002
+#define PSR_SVC26_MODE 0x00000003
+#define PSR_USR32_MODE 0x00000010
+#define PSR_FIQ32_MODE 0x00000011
+#define PSR_IRQ32_MODE 0x00000012
+#define PSR_SVC32_MODE 0x00000013
+#define PSR_ABT32_MODE 0x00000017
+#define PSR_UND32_MODE 0x0000001b
+#define PSR_SYS32_MODE 0x0000001f
+#define PSR_32_MODE 0x00000010
+#define PSR_FLAGS 0xf0000000 /* flags */
+
+#define PSR_C_bit (1 << 29) /* carry */
+
+/* The high-order byte is always the implementor */
+#define CPU_ID_IMPLEMENTOR_MASK 0xff000000
+#define CPU_ID_ARM_LTD 0x41000000 /* 'A' */
+#define CPU_ID_DEC 0x44000000 /* 'D' */
+#define CPU_ID_INTEL 0x69000000 /* 'i' */
+#define CPU_ID_TI 0x54000000 /* 'T' */
+
+/* How to decide what format the CPUID is in. */
+#define CPU_ID_ISOLD(x) (((x) & 0x0000f000) == 0x00000000)
+#define CPU_ID_IS7(x) (((x) & 0x0000f000) == 0x00007000)
+#define CPU_ID_ISNEW(x) (!CPU_ID_ISOLD(x) && !CPU_ID_IS7(x))
+
+/* On ARM3 and ARM6, this byte holds the foundry ID. */
+#define CPU_ID_FOUNDRY_MASK 0x00ff0000
+#define CPU_ID_FOUNDRY_VLSI 0x00560000
+
+/* On ARM7 it holds the architecture and variant (sub-model) */
+#define CPU_ID_7ARCH_MASK 0x00800000
+#define CPU_ID_7ARCH_V3 0x00000000
+#define CPU_ID_7ARCH_V4T 0x00800000
+#define CPU_ID_7VARIANT_MASK 0x007f0000
+
+/* On more recent ARMs, it does the same, but in a different format */
+#define CPU_ID_ARCH_MASK 0x000f0000
+#define CPU_ID_ARCH_V3 0x00000000
+#define CPU_ID_ARCH_V4 0x00010000
+#define CPU_ID_ARCH_V4T 0x00020000
+#define CPU_ID_ARCH_V5 0x00030000
+#define CPU_ID_ARCH_V5T 0x00040000
+#define CPU_ID_ARCH_V5TE 0x00050000
+#define CPU_ID_VARIANT_MASK 0x00f00000
+
+/* Next three nybbles are part number */
+#define CPU_ID_PARTNO_MASK 0x0000fff0
+
+/* Intel XScale has sub fields in part number */
+#define CPU_ID_XSCALE_COREGEN_MASK 0x0000e000 /* core generation */
+#define CPU_ID_XSCALE_COREREV_MASK 0x00001c00 /* core revision */
+#define CPU_ID_XSCALE_PRODUCT_MASK 0x000003f0 /* product number */
+
+/* And finally, the revision number. */
+#define CPU_ID_REVISION_MASK 0x0000000f
+
+/* Individual CPUs are probably best IDed by everything but the revision. */
+#define CPU_ID_CPU_MASK 0xfffffff0
+
+/* Fake CPU IDs for ARMs without CP15 */
+#define CPU_ID_ARM2 0x41560200
+#define CPU_ID_ARM250 0x41560250
+
+/* Pre-ARM7 CPUs -- [15:12] == 0 */
+#define CPU_ID_ARM3 0x41560300
+#define CPU_ID_ARM600 0x41560600
+#define CPU_ID_ARM610 0x41560610
+#define CPU_ID_ARM620 0x41560620
+
+/* ARM7 CPUs -- [15:12] == 7 */
+#define CPU_ID_ARM700 0x41007000 /* XXX This is a guess. */
+#define CPU_ID_ARM710 0x41007100
+#define CPU_ID_ARM7500 0x41027100 /* XXX This is a guess. */
+#define CPU_ID_ARM710A 0x41047100 /* inc ARM7100 */
+#define CPU_ID_ARM7500FE 0x41077100
+#define CPU_ID_ARM710T 0x41807100
+#define CPU_ID_ARM720T 0x41807200
+#define CPU_ID_ARM740T8K 0x41807400 /* XXX no MMU, 8KB cache */
+#define CPU_ID_ARM740T4K 0x41817400 /* XXX no MMU, 4KB cache */
+
+/* Post-ARM7 CPUs */
+#define CPU_ID_ARM810 0x41018100
+#define CPU_ID_ARM920T 0x41129200
+#define CPU_ID_ARM920T_ALT 0x41009200
+#define CPU_ID_ARM922T 0x41029220
+#define CPU_ID_ARM940T 0x41029400 /* XXX no MMU */
+#define CPU_ID_ARM946ES 0x41049460 /* XXX no MMU */
+#define CPU_ID_ARM966ES 0x41049660 /* XXX no MMU */
+#define CPU_ID_ARM966ESR1 0x41059660 /* XXX no MMU */
+#define CPU_ID_ARM1020E 0x4115a200 /* (AKA arm10 rev 1) */
+#define CPU_ID_ARM1022ES 0x4105a220
+#define CPU_ID_SA110 0x4401a100
+#define CPU_ID_SA1100 0x4401a110
+#define CPU_ID_TI925T 0x54029250
+#define CPU_ID_SA1110 0x6901b110
+#define CPU_ID_IXP1200 0x6901c120
+#define CPU_ID_80200 0x69052000
+#define CPU_ID_PXA250 0x69052100 /* sans core revision */
+#define CPU_ID_PXA210 0x69052120
+#define CPU_ID_PXA250A 0x69052100 /* 1st version Core */
+#define CPU_ID_PXA210A 0x69052120 /* 1st version Core */
+#define CPU_ID_PXA250B 0x69052900 /* 3rd version Core */
+#define CPU_ID_PXA210B 0x69052920 /* 3rd version Core */
+#define CPU_ID_PXA250C 0x69052d00 /* 4th version Core */
+#define CPU_ID_PXA210C 0x69052d20 /* 4th version Core */
+#define CPU_ID_80321_400 0x69052420
+#define CPU_ID_80321_600 0x69052430
+#define CPU_ID_80321_400_B0 0x69052c20
+#define CPU_ID_80321_600_B0 0x69052c30
+#define CPU_ID_IXP425_533 0x690541c0
+#define CPU_ID_IXP425_400 0x690541d0
+#define CPU_ID_IXP425_266 0x690541f0
+
+/* ARM3-specific coprocessor 15 registers */
+#define ARM3_CP15_FLUSH 1
+#define ARM3_CP15_CONTROL 2
+#define ARM3_CP15_CACHEABLE 3
+#define ARM3_CP15_UPDATEABLE 4
+#define ARM3_CP15_DISRUPTIVE 5
+
+/* ARM3 Control register bits */
+#define ARM3_CTL_CACHE_ON 0x00000001
+#define ARM3_CTL_SHARED 0x00000002
+#define ARM3_CTL_MONITOR 0x00000004
+
+/*
+ * Post-ARM3 CP15 registers:
+ *
+ * 1 Control register
+ *
+ * 2 Translation Table Base
+ *
+ * 3 Domain Access Control
+ *
+ * 4 Reserved
+ *
+ * 5 Fault Status
+ *
+ * 6 Fault Address
+ *
+ * 7 Cache/write-buffer Control
+ *
+ * 8 TLB Control
+ *
+ * 9 Cache Lockdown
+ *
+ * 10 TLB Lockdown
+ *
+ * 11 Reserved
+ *
+ * 12 Reserved
+ *
+ * 13 Process ID (for FCSE)
+ *
+ * 14 Reserved
+ *
+ * 15 Implementation Dependent
+ */
+
+/* Some of the definitions below need cleaning up for V3/V4 architectures */
+
+/* CPU control register (CP15 register 1) */
+#define CPU_CONTROL_MMU_ENABLE 0x00000001 /* M: MMU/Protection unit enable */
+#define CPU_CONTROL_AFLT_ENABLE 0x00000002 /* A: Alignment fault enable */
+#define CPU_CONTROL_DC_ENABLE 0x00000004 /* C: IDC/DC enable */
+#define CPU_CONTROL_WBUF_ENABLE 0x00000008 /* W: Write buffer enable */
+#define CPU_CONTROL_32BP_ENABLE 0x00000010 /* P: 32-bit exception handlers */
+#define CPU_CONTROL_32BD_ENABLE 0x00000020 /* D: 32-bit addressing */
+#define CPU_CONTROL_LABT_ENABLE 0x00000040 /* L: Late abort enable */
+#define CPU_CONTROL_BEND_ENABLE 0x00000080 /* B: Big-endian mode */
+#define CPU_CONTROL_SYST_ENABLE 0x00000100 /* S: System protection bit */
+#define CPU_CONTROL_ROM_ENABLE 0x00000200 /* R: ROM protection bit */
+#define CPU_CONTROL_CPCLK 0x00000400 /* F: Implementation defined */
+#define CPU_CONTROL_BPRD_ENABLE 0x00000800 /* Z: Branch prediction enable */
+#define CPU_CONTROL_IC_ENABLE 0x00001000 /* I: IC enable */
+#define CPU_CONTROL_VECRELOC 0x00002000 /* V: Vector relocation */
+#define CPU_CONTROL_ROUNDROBIN 0x00004000 /* RR: Predictable replacement */
+#define CPU_CONTROL_V4COMPAT 0x00008000 /* L4: ARMv4 compat LDR R15 etc */
+
+#define CPU_CONTROL_IDC_ENABLE CPU_CONTROL_DC_ENABLE
+
+/* XScale Auxillary Control Register (CP15 register 1, opcode2 1) */
+#define XSCALE_AUXCTL_K 0x00000001 /* dis. write buffer coalescing */
+#define XSCALE_AUXCTL_P 0x00000002 /* ECC protect page table access */
+#define XSCALE_AUXCTL_MD_WB_RA 0x00000000 /* mini-D$ wb, read-allocate */
+#define XSCALE_AUXCTL_MD_WB_RWA 0x00000010 /* mini-D$ wb, read/write-allocate */
+#define XSCALE_AUXCTL_MD_WT 0x00000020 /* mini-D$ wt, read-allocate */
+#define XSCALE_AUXCTL_MD_MASK 0x00000030
+
+/* Cache type register definitions */
+#define CPU_CT_ISIZE(x) ((x) & 0xfff) /* I$ info */
+#define CPU_CT_DSIZE(x) (((x) >> 12) & 0xfff) /* D$ info */
+#define CPU_CT_S (1U << 24) /* split cache */
+#define CPU_CT_CTYPE(x) (((x) >> 25) & 0xf) /* cache type */
+
+#define CPU_CT_CTYPE_WT 0 /* write-through */
+#define CPU_CT_CTYPE_WB1 1 /* write-back, clean w/ read */
+#define CPU_CT_CTYPE_WB2 2 /* w/b, clean w/ cp15,7 */
+#define CPU_CT_CTYPE_WB6 6 /* w/b, cp15,7, lockdown fmt A */
+#define CPU_CT_CTYPE_WB7 7 /* w/b, cp15,7, lockdown fmt B */
+
+#define CPU_CT_xSIZE_LEN(x) ((x) & 0x3) /* line size */
+#define CPU_CT_xSIZE_M (1U << 2) /* multiplier */
+#define CPU_CT_xSIZE_ASSOC(x) (((x) >> 3) & 0x7) /* associativity */
+#define CPU_CT_xSIZE_SIZE(x) (((x) >> 6) & 0x7) /* size */
+
+/* Fault status register definitions */
+
+#define FAULT_TYPE_MASK 0x0f
+#define FAULT_USER 0x10
+
+#define FAULT_WRTBUF_0 0x00 /* Vector Exception */
+#define FAULT_WRTBUF_1 0x02 /* Terminal Exception */
+#define FAULT_BUSERR_0 0x04 /* External Abort on Linefetch -- Section */
+#define FAULT_BUSERR_1 0x06 /* External Abort on Linefetch -- Page */
+#define FAULT_BUSERR_2 0x08 /* External Abort on Non-linefetch -- Section */
+#define FAULT_BUSERR_3 0x0a /* External Abort on Non-linefetch -- Page */
+#define FAULT_BUSTRNL1 0x0c /* External abort on Translation -- Level 1 */
+#define FAULT_BUSTRNL2 0x0e /* External abort on Translation -- Level 2 */
+#define FAULT_ALIGN_0 0x01 /* Alignment */
+#define FAULT_ALIGN_1 0x03 /* Alignment */
+#define FAULT_TRANS_S 0x05 /* Translation -- Section */
+#define FAULT_TRANS_P 0x07 /* Translation -- Page */
+#define FAULT_DOMAIN_S 0x09 /* Domain -- Section */
+#define FAULT_DOMAIN_P 0x0b /* Domain -- Page */
+#define FAULT_PERM_S 0x0d /* Permission -- Section */
+#define FAULT_PERM_P 0x0f /* Permission -- Page */
+
+#define FAULT_IMPRECISE 0x400 /* Imprecise exception (XSCALE) */
+
+/*
+ * Address of the vector page, low and high versions.
+ */
+#define ARM_VECTORS_LOW 0x00000000U
+#define ARM_VECTORS_HIGH 0xffff0000U
+
+/*
+ * ARM Instructions
+ *
+ * 3 3 2 2 2
+ * 1 0 9 8 7 0
+ * +-------+-------------------------------------------------------+
+ * | cond | instruction dependant |
+ * |c c c c| |
+ * +-------+-------------------------------------------------------+
+ */
+
+#define INSN_SIZE 4 /* Always 4 bytes */
+#define INSN_COND_MASK 0xf0000000 /* Condition mask */
+#define INSN_COND_AL 0xe0000000 /* Always condition */
+
+#endif /* !MACHINE_ARMREG_H */
diff --git a/libacc/disassem.cpp b/libacc/disassem.cpp
new file mode 100644
index 0000000..ac35342
--- /dev/null
+++ b/libacc/disassem.cpp
@@ -0,0 +1,711 @@
+/* $NetBSD: disassem.c,v 1.14 2003/03/27 16:58:36 mycroft Exp $ */
+
+/*-
+ * Copyright (c) 1996 Mark Brinicombe.
+ * Copyright (c) 1996 Brini.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Brini.
+ * 4. The name of the company nor the name of the author may be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * RiscBSD kernel project
+ *
+ * db_disasm.c
+ *
+ * Kernel disassembler
+ *
+ * Created : 10/02/96
+ *
+ * Structured after the sparc/sparc/db_disasm.c by David S. Miller &
+ * Paul Kranenburg
+ *
+ * This code is not complete. Not all instructions are disassembled.
+ */
+
+#include <sys/cdefs.h>
+//__FBSDID("$FreeBSD: /repoman/r/ncvs/src/sys/arm/arm/disassem.c,v 1.2 2005/01/05 21:58:47 imp Exp $");
+#include <sys/param.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "disassem.h"
+#include "armreg.h"
+//#include <ddb/ddb.h>
+
+/*
+ * General instruction format
+ *
+ * insn[cc][mod] [operands]
+ *
+ * Those fields with an uppercase format code indicate that the field
+ * follows directly after the instruction before the separator i.e.
+ * they modify the instruction rather than just being an operand to
+ * the instruction. The only exception is the writeback flag which
+ * follows a operand.
+ *
+ *
+ * 2 - print Operand 2 of a data processing instruction
+ * d - destination register (bits 12-15)
+ * n - n register (bits 16-19)
+ * s - s register (bits 8-11)
+ * o - indirect register rn (bits 16-19) (used by swap)
+ * m - m register (bits 0-3)
+ * a - address operand of ldr/str instruction
+ * e - address operand of ldrh/strh instruction
+ * l - register list for ldm/stm instruction
+ * f - 1st fp operand (register) (bits 12-14)
+ * g - 2nd fp operand (register) (bits 16-18)
+ * h - 3rd fp operand (register/immediate) (bits 0-4)
+ * b - branch address
+ * t - thumb branch address (bits 24, 0-23)
+ * k - breakpoint comment (bits 0-3, 8-19)
+ * X - block transfer type
+ * Y - block transfer type (r13 base)
+ * c - comment field bits(0-23)
+ * p - saved or current status register
+ * F - PSR transfer fields
+ * D - destination-is-r15 (P) flag on TST, TEQ, CMP, CMN
+ * L - co-processor transfer size
+ * S - set status flag
+ * P - fp precision
+ * Q - fp precision (for ldf/stf)
+ * R - fp rounding
+ * v - co-processor data transfer registers + addressing mode
+ * W - writeback flag
+ * x - instruction in hex
+ * # - co-processor number
+ * y - co-processor data processing registers
+ * z - co-processor register transfer registers
+ */
+
+struct arm32_insn {
+ u_int mask;
+ u_int pattern;
+ const char* name;
+ const char* format;
+};
+
+static const struct arm32_insn arm32_i[] = {
+ { 0x0fffffff, 0x0ff00000, "imb", "c" }, /* Before swi */
+ { 0x0fffffff, 0x0ff00001, "imbrange", "c" }, /* Before swi */
+ { 0x0f000000, 0x0f000000, "swi", "c" },
+ { 0xfe000000, 0xfa000000, "blx", "t" }, /* Before b and bl */
+ { 0x0f000000, 0x0a000000, "b", "b" },
+ { 0x0f000000, 0x0b000000, "bl", "b" },
+ { 0x0fe000f0, 0x00000090, "mul", "Snms" },
+ { 0x0fe000f0, 0x00200090, "mla", "Snmsd" },
+ { 0x0fe000f0, 0x00800090, "umull", "Sdnms" },
+ { 0x0fe000f0, 0x00c00090, "smull", "Sdnms" },
+ { 0x0fe000f0, 0x00a00090, "umlal", "Sdnms" },
+ { 0x0fe000f0, 0x00e00090, "smlal", "Sdnms" },
+ { 0x0d700000, 0x04200000, "strt", "daW" },
+ { 0x0d700000, 0x04300000, "ldrt", "daW" },
+ { 0x0d700000, 0x04600000, "strbt", "daW" },
+ { 0x0d700000, 0x04700000, "ldrbt", "daW" },
+ { 0x0c500000, 0x04000000, "str", "daW" },
+ { 0x0c500000, 0x04100000, "ldr", "daW" },
+ { 0x0c500000, 0x04400000, "strb", "daW" },
+ { 0x0c500000, 0x04500000, "ldrb", "daW" },
+ { 0x0e1f0000, 0x080d0000, "stm", "YnWl" },/* separate out r13 base */
+ { 0x0e1f0000, 0x081d0000, "ldm", "YnWl" },/* separate out r13 base */
+ { 0x0e100000, 0x08000000, "stm", "XnWl" },
+ { 0x0e100000, 0x08100000, "ldm", "XnWl" },
+ { 0x0e1000f0, 0x00100090, "ldrb", "deW" },
+ { 0x0e1000f0, 0x00000090, "strb", "deW" },
+ { 0x0e1000f0, 0x001000d0, "ldrsb", "deW" },
+ { 0x0e1000f0, 0x001000b0, "ldrh", "deW" },
+ { 0x0e1000f0, 0x000000b0, "strh", "deW" },
+ { 0x0e1000f0, 0x001000f0, "ldrsh", "deW" },
+ { 0x0f200090, 0x00200090, "und", "x" }, /* Before data processing */
+ { 0x0e1000d0, 0x000000d0, "und", "x" }, /* Before data processing */
+ { 0x0ff00ff0, 0x01000090, "swp", "dmo" },
+ { 0x0ff00ff0, 0x01400090, "swpb", "dmo" },
+ { 0x0fbf0fff, 0x010f0000, "mrs", "dp" }, /* Before data processing */
+ { 0x0fb0fff0, 0x0120f000, "msr", "pFm" },/* Before data processing */
+ { 0x0fb0f000, 0x0320f000, "msr", "pF2" },/* Before data processing */
+ { 0x0ffffff0, 0x012fff10, "bx", "m" },
+ { 0x0fff0ff0, 0x016f0f10, "clz", "dm" },
+ { 0x0ffffff0, 0x012fff30, "blx", "m" },
+ { 0xfff000f0, 0xe1200070, "bkpt", "k" },
+ { 0x0de00000, 0x00000000, "and", "Sdn2" },
+ { 0x0de00000, 0x00200000, "eor", "Sdn2" },
+ { 0x0de00000, 0x00400000, "sub", "Sdn2" },
+ { 0x0de00000, 0x00600000, "rsb", "Sdn2" },
+ { 0x0de00000, 0x00800000, "add", "Sdn2" },
+ { 0x0de00000, 0x00a00000, "adc", "Sdn2" },
+ { 0x0de00000, 0x00c00000, "sbc", "Sdn2" },
+ { 0x0de00000, 0x00e00000, "rsc", "Sdn2" },
+ { 0x0df00000, 0x01100000, "tst", "Dn2" },
+ { 0x0df00000, 0x01300000, "teq", "Dn2" },
+ { 0x0df00000, 0x01500000, "cmp", "Dn2" },
+ { 0x0df00000, 0x01700000, "cmn", "Dn2" },
+ { 0x0de00000, 0x01800000, "orr", "Sdn2" },
+ { 0x0de00000, 0x01a00000, "mov", "Sd2" },
+ { 0x0de00000, 0x01c00000, "bic", "Sdn2" },
+ { 0x0de00000, 0x01e00000, "mvn", "Sd2" },
+ { 0x0ff08f10, 0x0e000100, "adf", "PRfgh" },
+ { 0x0ff08f10, 0x0e100100, "muf", "PRfgh" },
+ { 0x0ff08f10, 0x0e200100, "suf", "PRfgh" },
+ { 0x0ff08f10, 0x0e300100, "rsf", "PRfgh" },
+ { 0x0ff08f10, 0x0e400100, "dvf", "PRfgh" },
+ { 0x0ff08f10, 0x0e500100, "rdf", "PRfgh" },
+ { 0x0ff08f10, 0x0e600100, "pow", "PRfgh" },
+ { 0x0ff08f10, 0x0e700100, "rpw", "PRfgh" },
+ { 0x0ff08f10, 0x0e800100, "rmf", "PRfgh" },
+ { 0x0ff08f10, 0x0e900100, "fml", "PRfgh" },
+ { 0x0ff08f10, 0x0ea00100, "fdv", "PRfgh" },
+ { 0x0ff08f10, 0x0eb00100, "frd", "PRfgh" },
+ { 0x0ff08f10, 0x0ec00100, "pol", "PRfgh" },
+ { 0x0f008f10, 0x0e000100, "fpbop", "PRfgh" },
+ { 0x0ff08f10, 0x0e008100, "mvf", "PRfh" },
+ { 0x0ff08f10, 0x0e108100, "mnf", "PRfh" },
+ { 0x0ff08f10, 0x0e208100, "abs", "PRfh" },
+ { 0x0ff08f10, 0x0e308100, "rnd", "PRfh" },
+ { 0x0ff08f10, 0x0e408100, "sqt", "PRfh" },
+ { 0x0ff08f10, 0x0e508100, "log", "PRfh" },
+ { 0x0ff08f10, 0x0e608100, "lgn", "PRfh" },
+ { 0x0ff08f10, 0x0e708100, "exp", "PRfh" },
+ { 0x0ff08f10, 0x0e808100, "sin", "PRfh" },
+ { 0x0ff08f10, 0x0e908100, "cos", "PRfh" },
+ { 0x0ff08f10, 0x0ea08100, "tan", "PRfh" },
+ { 0x0ff08f10, 0x0eb08100, "asn", "PRfh" },
+ { 0x0ff08f10, 0x0ec08100, "acs", "PRfh" },
+ { 0x0ff08f10, 0x0ed08100, "atn", "PRfh" },
+ { 0x0f008f10, 0x0e008100, "fpuop", "PRfh" },
+ { 0x0e100f00, 0x0c000100, "stf", "QLv" },
+ { 0x0e100f00, 0x0c100100, "ldf", "QLv" },
+ { 0x0ff00f10, 0x0e000110, "flt", "PRgd" },
+ { 0x0ff00f10, 0x0e100110, "fix", "PRdh" },
+ { 0x0ff00f10, 0x0e200110, "wfs", "d" },
+ { 0x0ff00f10, 0x0e300110, "rfs", "d" },
+ { 0x0ff00f10, 0x0e400110, "wfc", "d" },
+ { 0x0ff00f10, 0x0e500110, "rfc", "d" },
+ { 0x0ff0ff10, 0x0e90f110, "cmf", "PRgh" },
+ { 0x0ff0ff10, 0x0eb0f110, "cnf", "PRgh" },
+ { 0x0ff0ff10, 0x0ed0f110, "cmfe", "PRgh" },
+ { 0x0ff0ff10, 0x0ef0f110, "cnfe", "PRgh" },
+ { 0xff100010, 0xfe000010, "mcr2", "#z" },
+ { 0x0f100010, 0x0e000010, "mcr", "#z" },
+ { 0xff100010, 0xfe100010, "mrc2", "#z" },
+ { 0x0f100010, 0x0e100010, "mrc", "#z" },
+ { 0xff000010, 0xfe000000, "cdp2", "#y" },
+ { 0x0f000010, 0x0e000000, "cdp", "#y" },
+ { 0xfe100090, 0xfc100000, "ldc2", "L#v" },
+ { 0x0e100090, 0x0c100000, "ldc", "L#v" },
+ { 0xfe100090, 0xfc000000, "stc2", "L#v" },
+ { 0x0e100090, 0x0c000000, "stc", "L#v" },
+ { 0xf550f000, 0xf550f000, "pld", "ne" },
+ { 0x0ff00ff0, 0x01000050, "qaad", "dmn" },
+ { 0x0ff00ff0, 0x01400050, "qdaad", "dmn" },
+ { 0x0ff00ff0, 0x01600050, "qdsub", "dmn" },
+ { 0x0ff00ff0, 0x01200050, "dsub", "dmn" },
+ { 0x0ff000f0, 0x01000080, "smlabb", "nmsd" }, // d & n inverted!!
+ { 0x0ff000f0, 0x010000a0, "smlatb", "nmsd" }, // d & n inverted!!
+ { 0x0ff000f0, 0x010000c0, "smlabt", "nmsd" }, // d & n inverted!!
+ { 0x0ff000f0, 0x010000e0, "smlatt", "nmsd" }, // d & n inverted!!
+ { 0x0ff000f0, 0x01400080, "smlalbb","ndms" }, // d & n inverted!!
+ { 0x0ff000f0, 0x014000a0, "smlaltb","ndms" }, // d & n inverted!!
+ { 0x0ff000f0, 0x014000c0, "smlalbt","ndms" }, // d & n inverted!!
+ { 0x0ff000f0, 0x014000e0, "smlaltt","ndms" }, // d & n inverted!!
+ { 0x0ff000f0, 0x01200080, "smlawb", "nmsd" }, // d & n inverted!!
+ { 0x0ff0f0f0, 0x012000a0, "smulwb","nms" }, // d & n inverted!!
+ { 0x0ff000f0, 0x012000c0, "smlawt", "nmsd" }, // d & n inverted!!
+ { 0x0ff0f0f0, 0x012000e0, "smulwt","nms" }, // d & n inverted!!
+ { 0x0ff0f0f0, 0x01600080, "smulbb","nms" }, // d & n inverted!!
+ { 0x0ff0f0f0, 0x016000a0, "smultb","nms" }, // d & n inverted!!
+ { 0x0ff0f0f0, 0x016000c0, "smulbt","nms" }, // d & n inverted!!
+ { 0x0ff0f0f0, 0x016000e0, "smultt","nms" }, // d & n inverted!!
+ { 0x00000000, 0x00000000, NULL, NULL }
+};
+
+static char const arm32_insn_conditions[][4] = {
+ "eq", "ne", "cs", "cc",
+ "mi", "pl", "vs", "vc",
+ "hi", "ls", "ge", "lt",
+ "gt", "le", "", "nv"
+};
+
+static char const insn_block_transfers[][4] = {
+ "da", "ia", "db", "ib"
+};
+
+static char const insn_stack_block_transfers[][4] = {
+ "ed", "ea", "fd", "fa"
+};
+
+static char const op_shifts[][4] = {
+ "lsl", "lsr", "asr", "ror"
+};
+
+static char const insn_fpa_rounding[][2] = {
+ "", "p", "m", "z"
+};
+
+static char const insn_fpa_precision[][2] = {
+ "s", "d", "e", "p"
+};
+
+static char const insn_fpaconstants[][8] = {
+ "0.0", "1.0", "2.0", "3.0",
+ "4.0", "5.0", "0.5", "10.0"
+};
+
+#define insn_condition(x) arm32_insn_conditions[(x >> 28) & 0x0f]
+#define insn_blktrans(x) insn_block_transfers[(x >> 23) & 3]
+#define insn_stkblktrans(x) insn_stack_block_transfers[(x >> 23) & 3]
+#define op2_shift(x) op_shifts[(x >> 5) & 3]
+#define insn_fparnd(x) insn_fpa_rounding[(x >> 5) & 0x03]
+#define insn_fpaprec(x) insn_fpa_precision[(((x >> 18) & 2)|(x >> 7)) & 1]
+#define insn_fpaprect(x) insn_fpa_precision[(((x >> 21) & 2)|(x >> 15)) & 1]
+#define insn_fpaimm(x) insn_fpaconstants[x & 0x07]
+
+/* Local prototypes */
+static void disasm_register_shift(const disasm_interface_t *di, u_int insn);
+static void disasm_print_reglist(const disasm_interface_t *di, u_int insn);
+static void disasm_insn_ldrstr(const disasm_interface_t *di, u_int insn,
+ u_int loc);
+static void disasm_insn_ldrhstrh(const disasm_interface_t *di, u_int insn,
+ u_int loc);
+static void disasm_insn_ldcstc(const disasm_interface_t *di, u_int insn,
+ u_int loc);
+static u_int disassemble_readword(u_int address);
+static void disassemble_printaddr(u_int address);
+
+u_int
+disasm(const disasm_interface_t *di, u_int loc, int altfmt)
+{
+ const struct arm32_insn *i_ptr = &arm32_i[0];
+
+ u_int insn;
+ int matchp;
+ int branch;
+ const char* f_ptr;
+ int fmt;
+
+ fmt = 0;
+ matchp = 0;
+ insn = di->di_readword(loc);
+
+/* di->di_printf("loc=%08x insn=%08x : ", loc, insn);*/
+
+ while (i_ptr->name) {
+ if ((insn & i_ptr->mask) == i_ptr->pattern) {
+ matchp = 1;
+ break;
+ }
+ i_ptr++;
+ }
+
+ if (!matchp) {
+ di->di_printf("und%s\t%08x\n", insn_condition(insn), insn);
+ return(loc + INSN_SIZE);
+ }
+
+ /* If instruction forces condition code, don't print it. */
+ if ((i_ptr->mask & 0xf0000000) == 0xf0000000)
+ di->di_printf("%s", i_ptr->name);
+ else
+ di->di_printf("%s%s", i_ptr->name, insn_condition(insn));
+
+ f_ptr = i_ptr->format;
+
+ /* Insert tab if there are no instruction modifiers */
+
+ if (*(f_ptr) < 'A' || *(f_ptr) > 'Z') {
+ ++fmt;
+ di->di_printf("\t");
+ }
+
+ while (*f_ptr) {
+ switch (*f_ptr) {
+ /* 2 - print Operand 2 of a data processing instruction */
+ case '2':
+ if (insn & 0x02000000) {
+ int rotate= ((insn >> 7) & 0x1e);
+
+ di->di_printf("#0x%08x",
+ (insn & 0xff) << (32 - rotate) |
+ (insn & 0xff) >> rotate);
+ } else {
+ disasm_register_shift(di, insn);
+ }
+ break;
+ /* d - destination register (bits 12-15) */
+ case 'd':
+ di->di_printf("r%d", ((insn >> 12) & 0x0f));
+ break;
+ /* D - insert 'p' if Rd is R15 */
+ case 'D':
+ if (((insn >> 12) & 0x0f) == 15)
+ di->di_printf("p");
+ break;
+ /* n - n register (bits 16-19) */
+ case 'n':
+ di->di_printf("r%d", ((insn >> 16) & 0x0f));
+ break;
+ /* s - s register (bits 8-11) */
+ case 's':
+ di->di_printf("r%d", ((insn >> 8) & 0x0f));
+ break;
+ /* o - indirect register rn (bits 16-19) (used by swap) */
+ case 'o':
+ di->di_printf("[r%d]", ((insn >> 16) & 0x0f));
+ break;
+ /* m - m register (bits 0-4) */
+ case 'm':
+ di->di_printf("r%d", ((insn >> 0) & 0x0f));
+ break;
+ /* a - address operand of ldr/str instruction */
+ case 'a':
+ disasm_insn_ldrstr(di, insn, loc);
+ break;
+ /* e - address operand of ldrh/strh instruction */
+ case 'e':
+ disasm_insn_ldrhstrh(di, insn, loc);
+ break;
+ /* l - register list for ldm/stm instruction */
+ case 'l':
+ disasm_print_reglist(di, insn);
+ break;
+ /* f - 1st fp operand (register) (bits 12-14) */
+ case 'f':
+ di->di_printf("f%d", (insn >> 12) & 7);
+ break;
+ /* g - 2nd fp operand (register) (bits 16-18) */
+ case 'g':
+ di->di_printf("f%d", (insn >> 16) & 7);
+ break;
+ /* h - 3rd fp operand (register/immediate) (bits 0-4) */
+ case 'h':
+ if (insn & (1 << 3))
+ di->di_printf("#%s", insn_fpaimm(insn));
+ else
+ di->di_printf("f%d", insn & 7);
+ break;
+ /* b - branch address */
+ case 'b':
+ branch = ((insn << 2) & 0x03ffffff);
+ if (branch & 0x02000000)
+ branch |= 0xfc000000;
+ di->di_printaddr(loc + 8 + branch);
+ break;
+ /* t - blx address */
+ case 't':
+ branch = ((insn << 2) & 0x03ffffff) |
+ (insn >> 23 & 0x00000002);
+ if (branch & 0x02000000)
+ branch |= 0xfc000000;
+ di->di_printaddr(loc + 8 + branch);
+ break;
+ /* X - block transfer type */
+ case 'X':
+ di->di_printf("%s", insn_blktrans(insn));
+ break;
+ /* Y - block transfer type (r13 base) */
+ case 'Y':
+ di->di_printf("%s", insn_stkblktrans(insn));
+ break;
+ /* c - comment field bits(0-23) */
+ case 'c':
+ di->di_printf("0x%08x", (insn & 0x00ffffff));
+ break;
+ /* k - breakpoint comment (bits 0-3, 8-19) */
+ case 'k':
+ di->di_printf("0x%04x",
+ (insn & 0x000fff00) >> 4 | (insn & 0x0000000f));
+ break;
+ /* p - saved or current status register */
+ case 'p':
+ if (insn & 0x00400000)
+ di->di_printf("spsr");
+ else
+ di->di_printf("cpsr");
+ break;
+ /* F - PSR transfer fields */
+ case 'F':
+ di->di_printf("_");
+ if (insn & (1 << 16))
+ di->di_printf("c");
+ if (insn & (1 << 17))
+ di->di_printf("x");
+ if (insn & (1 << 18))
+ di->di_printf("s");
+ if (insn & (1 << 19))
+ di->di_printf("f");
+ break;
+ /* B - byte transfer flag */
+ case 'B':
+ if (insn & 0x00400000)
+ di->di_printf("b");
+ break;
+ /* L - co-processor transfer size */
+ case 'L':
+ if (insn & (1 << 22))
+ di->di_printf("l");
+ break;
+ /* S - set status flag */
+ case 'S':
+ if (insn & 0x00100000)
+ di->di_printf("s");
+ break;
+ /* P - fp precision */
+ case 'P':
+ di->di_printf("%s", insn_fpaprec(insn));
+ break;
+ /* Q - fp precision (for ldf/stf) */
+ case 'Q':
+ break;
+ /* R - fp rounding */
+ case 'R':
+ di->di_printf("%s", insn_fparnd(insn));
+ break;
+ /* W - writeback flag */
+ case 'W':
+ if (insn & (1 << 21))
+ di->di_printf("!");
+ break;
+ /* # - co-processor number */
+ case '#':
+ di->di_printf("p%d", (insn >> 8) & 0x0f);
+ break;
+ /* v - co-processor data transfer registers+addressing mode */
+ case 'v':
+ disasm_insn_ldcstc(di, insn, loc);
+ break;
+ /* x - instruction in hex */
+ case 'x':
+ di->di_printf("0x%08x", insn);
+ break;
+ /* y - co-processor data processing registers */
+ case 'y':
+ di->di_printf("%d, ", (insn >> 20) & 0x0f);
+
+ di->di_printf("c%d, c%d, c%d", (insn >> 12) & 0x0f,
+ (insn >> 16) & 0x0f, insn & 0x0f);
+
+ di->di_printf(", %d", (insn >> 5) & 0x07);
+ break;
+ /* z - co-processor register transfer registers */
+ case 'z':
+ di->di_printf("%d, ", (insn >> 21) & 0x07);
+ di->di_printf("r%d, c%d, c%d, %d",
+ (insn >> 12) & 0x0f, (insn >> 16) & 0x0f,
+ insn & 0x0f, (insn >> 5) & 0x07);
+
+/* if (((insn >> 5) & 0x07) != 0)
+ di->di_printf(", %d", (insn >> 5) & 0x07);*/
+ break;
+ default:
+ di->di_printf("[%c - unknown]", *f_ptr);
+ break;
+ }
+ if (*(f_ptr+1) >= 'A' && *(f_ptr+1) <= 'Z')
+ ++f_ptr;
+ else if (*(++f_ptr)) {
+ ++fmt;
+ if (fmt == 1)
+ di->di_printf("\t");
+ else
+ di->di_printf(", ");
+ }
+ };
+
+ di->di_printf("\n");
+
+ return(loc + INSN_SIZE);
+}
+
+
+static void
+disasm_register_shift(const disasm_interface_t *di, u_int insn)
+{
+ di->di_printf("r%d", (insn & 0x0f));
+ if ((insn & 0x00000ff0) == 0)
+ ;
+ else if ((insn & 0x00000ff0) == 0x00000060)
+ di->di_printf(", rrx");
+ else {
+ if (insn & 0x10)
+ di->di_printf(", %s r%d", op2_shift(insn),
+ (insn >> 8) & 0x0f);
+ else
+ di->di_printf(", %s #%d", op2_shift(insn),
+ (insn >> 7) & 0x1f);
+ }
+}
+
+
+static void
+disasm_print_reglist(const disasm_interface_t *di, u_int insn)
+{
+ int loop;
+ int start;
+ int comma;
+
+ di->di_printf("{");
+ start = -1;
+ comma = 0;
+
+ for (loop = 0; loop < 17; ++loop) {
+ if (start != -1) {
+ if (loop == 16 || !(insn & (1 << loop))) {
+ if (comma)
+ di->di_printf(", ");
+ else
+ comma = 1;
+ if (start == loop - 1)
+ di->di_printf("r%d", start);
+ else
+ di->di_printf("r%d-r%d", start, loop - 1);
+ start = -1;
+ }
+ } else {
+ if (insn & (1 << loop))
+ start = loop;
+ }
+ }
+ di->di_printf("}");
+
+ if (insn & (1 << 22))
+ di->di_printf("^");
+}
+
+static void
+disasm_insn_ldrstr(const disasm_interface_t *di, u_int insn, u_int loc)
+{
+ int offset;
+
+ offset = insn & 0xfff;
+ if ((insn & 0x032f0000) == 0x010f0000) {
+ /* rA = pc, immediate index */
+ if (insn & 0x00800000)
+ loc += offset;
+ else
+ loc -= offset;
+ di->di_printaddr(loc + 8);
+ } else {
+ di->di_printf("[r%d", (insn >> 16) & 0x0f);
+ if ((insn & 0x03000fff) != 0x01000000) {
+ di->di_printf("%s, ", (insn & (1 << 24)) ? "" : "]");
+ if (!(insn & 0x00800000))
+ di->di_printf("-");
+ if (insn & (1 << 25))
+ disasm_register_shift(di, insn);
+ else
+ di->di_printf("#0x%03x", offset);
+ }
+ if (insn & (1 << 24))
+ di->di_printf("]");
+ }
+}
+
+static void
+disasm_insn_ldrhstrh(const disasm_interface_t *di, u_int insn, u_int loc)
+{
+ int offset;
+
+ offset = ((insn & 0xf00) >> 4) | (insn & 0xf);
+ if ((insn & 0x004f0000) == 0x004f0000) {
+ /* rA = pc, immediate index */
+ if (insn & 0x00800000)
+ loc += offset;
+ else
+ loc -= offset;
+ di->di_printaddr(loc + 8);
+ } else {
+ di->di_printf("[r%d", (insn >> 16) & 0x0f);
+ if ((insn & 0x01400f0f) != 0x01400000) {
+ di->di_printf("%s, ", (insn & (1 << 24)) ? "" : "]");
+ if (!(insn & 0x00800000))
+ di->di_printf("-");
+ if (insn & (1 << 22))
+ di->di_printf("#0x%02x", offset);
+ else
+ di->di_printf("r%d", (insn & 0x0f));
+ }
+ if (insn & (1 << 24))
+ di->di_printf("]");
+ }
+}
+
+static void
+disasm_insn_ldcstc(const disasm_interface_t *di, u_int insn, u_int loc)
+{
+ if (((insn >> 8) & 0xf) == 1)
+ di->di_printf("f%d, ", (insn >> 12) & 0x07);
+ else
+ di->di_printf("c%d, ", (insn >> 12) & 0x0f);
+
+ di->di_printf("[r%d", (insn >> 16) & 0x0f);
+
+ di->di_printf("%s, ", (insn & (1 << 24)) ? "" : "]");
+
+ if (!(insn & (1 << 23)))
+ di->di_printf("-");
+
+ di->di_printf("#0x%03x", (insn & 0xff) << 2);
+
+ if (insn & (1 << 24))
+ di->di_printf("]");
+
+ if (insn & (1 << 21))
+ di->di_printf("!");
+}
+
+static u_int
+disassemble_readword(u_int address)
+{
+ return(*((u_int *)address));
+}
+
+static void
+disassemble_printaddr(u_int address)
+{
+ printf("0x%08x", address);
+}
+
+static void
+disassemble_printf(const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ vprintf(fmt, ap);
+ va_end(ap);
+}
+
+static const disasm_interface_t disassemble_di = {
+ disassemble_readword, disassemble_printaddr, disassemble_printf
+};
+
+void
+disassemble(u_int address)
+{
+
+ (void)disasm(&disassemble_di, address, 0);
+}
+
+/* End of disassem.c */
diff --git a/libacc/disassem.h b/libacc/disassem.h
new file mode 100644
index 0000000..02747cd
--- /dev/null
+++ b/libacc/disassem.h
@@ -0,0 +1,65 @@
+/* $NetBSD: disassem.h,v 1.4 2001/03/04 04:15:58 matt Exp $ */
+
+/*-
+ * Copyright (c) 1997 Mark Brinicombe.
+ * Copyright (c) 1997 Causality Limited.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Mark Brinicombe.
+ * 4. The name of the company nor the name of the author may be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Define the interface structure required by the disassembler.
+ *
+ * $FreeBSD: /repoman/r/ncvs/src/sys/arm/include/disassem.h,v 1.2 2005/01/05 21:58:48 imp Exp $
+ */
+
+#ifndef ANDROID_MACHINE_DISASSEM_H
+#define ANDROID_MACHINE_DISASSEM_H
+
+#include <sys/types.h>
+
+#if __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ u_int (*di_readword)(u_int);
+ void (*di_printaddr)(u_int);
+ void (*di_printf)(const char *, ...);
+} disasm_interface_t;
+
+/* Prototypes for callable functions */
+
+u_int disasm(const disasm_interface_t *, u_int, int);
+void disassemble(u_int);
+
+#if __cplusplus
+}
+#endif
+
+#endif /* !ANDROID_MACHINE_DISASSEM_H */
diff --git a/libacc/test b/libacc/test
new file mode 100755
index 0000000..0b767da
--- /dev/null
+++ b/libacc/test
@@ -0,0 +1,3 @@
+#!/bin/sh
+g++ acc.cpp disassem.cpp -g -ldl -o tests/acc && tests/acc tests/otcc.c -a x86 -d tests/otcc.out && diff tests/otcc.out tests/otcc.out-orig
+tests/acc -S tests/returnval.c
diff --git a/libacc/tests/.gitignore b/libacc/tests/.gitignore
new file mode 100644
index 0000000..9974532
--- /dev/null
+++ b/libacc/tests/.gitignore
@@ -0,0 +1,2 @@
+acc
+*.out
diff --git a/libacc/tests/bellard.otccex.c b/libacc/tests/bellard.otccex.c
new file mode 100644
index 0000000..e8f0989
--- /dev/null
+++ b/libacc/tests/bellard.otccex.c
@@ -0,0 +1,126 @@
+/* #!/usr/local/bin/otcc */
+/*
+ * Sample OTCC C example. You can uncomment the first line and install
+ * otcc in /usr/local/bin to make otcc scripts !
+ */
+
+/* Any preprocessor directive except #define are ignored. We put this
+ include so that a standard C compiler can compile this code too. */
+#include <stdio.h>
+
+/* defines are handled, but macro arguments cannot be given. No
+ recursive defines are tolerated */
+#define DEFAULT_BASE 10
+
+/*
+ * Only old style K&R prototypes are parsed. Only int arguments are
+ * allowed (implicit types).
+ *
+ * By benchmarking the execution time of this function (for example
+ * for fib(35)), you'll notice that OTCC is quite fast because it
+ * generates native i386 machine code.
+ */
+fib(n)
+{
+ if (n <= 2)
+ return 1;
+ else
+ return fib(n-1) + fib(n-2);
+}
+
+/* Identifiers are parsed the same way as C: begins with letter or
+ '_', and then letters, '_' or digits */
+fact(n)
+{
+ /* local variables can be declared. Only 'int' type is supported */
+ int i, r;
+ r = 1;
+ /* 'while' and 'for' loops are supported */
+ for(i=2;i<=n;i++)
+ r = r * i;
+ return r;
+}
+
+/* Well, we could use printf, but it would be too easy */
+print_num(n, b)
+{
+ int tab, p, c;
+ /* Numbers can be entered in decimal, hexadecimal ('0x' prefix) and
+ octal ('0' prefix) */
+ /* more complex programs use malloc */
+ tab = malloc(0x100);
+ p = tab;
+ while (1) {
+ c = n % b;
+ /* Character constants can be used */
+ if (c >= 10)
+ c = c + 'a' - 10;
+ else
+ c = c + '0';
+ *(char *)p = c;
+ p++;
+ n = n / b;
+ /* 'break' is supported */
+ if (n == 0)
+ break;
+ }
+ while (p != tab) {
+ p--;
+ printf("%c", *(char *)p);
+ }
+ free(tab);
+}
+
+/* 'main' takes standard 'argc' and 'argv' parameters */
+main(argc, argv)
+{
+ /* no local name space is supported, but local variables ARE
+ supported. As long as you do not use a globally defined
+ variable name as local variable (which is a bad habbit), you
+ won't have any problem */
+ int s, n, f, base;
+
+ /* && and || operator have the same semantics as C (left to right
+ evaluation and early exit) */
+ if (argc != 2 && argc != 3) {
+ /* '*' operator is supported with explicit casting to 'int *',
+ 'char *' or 'int (*)()' (function pointer). Of course, 'int'
+ are supposed to be used as pointers too. */
+ s = *(int *)argv;
+ help(s);
+ return 1;
+ }
+ /* Any libc function can be used because OTCC uses dynamic linking */
+ n = atoi(*(int *)(argv + 4));
+ base = DEFAULT_BASE;
+ if (argc >= 3) {
+ base = atoi(*(int *)(argv + 8));
+ if (base < 2 || base > 36) {
+ /* external variables can be used too (here: 'stderr') */
+ fprintf(stderr, "Invalid base\n");
+ return 1;
+ }
+ }
+ printf("fib(%d) = ", n);
+ print_num(fib(n), base);
+ printf("\n");
+
+ printf("fact(%d) = ", n);
+ if (n > 12) {
+ printf("Overflow");
+ } else {
+ /* why not using a function pointer ? */
+ f = &fact;
+ print_num((*(int (*)())f)(n), base);
+ }
+ printf("\n");
+ return 0;
+}
+
+/* functions can be used before being defined */
+help(name)
+{
+ printf("usage: %s n [base]\n", name);
+ printf("Compute fib(n) and fact(n) and output the result in base 'base'\n");
+}
+
diff --git a/libacc/tests/expr.c b/libacc/tests/expr.c
new file mode 100644
index 0000000..4f2d2e7
--- /dev/null
+++ b/libacc/tests/expr.c
@@ -0,0 +1,60 @@
+/* Test operators */
+
+testInc() { int a, b; a = 3; b = a++; printf("3++ = %d %d\n", b, a); }
+testDec() { int a, b; a = 3; b = a--; printf("3-- = %d %d\n", b, a); }
+testTimes(){ printf("%d * %d = %d\n", 10, 4, 10 * 4); }
+testDiv(){ printf("%d / %d = %d\n", 11, 4, 11 / 4); }
+testMod(){ printf("%d %% %d = %d\n", 11, 4, 11 % 4); }
+testPlus(){ printf("%d + %d = %d\n", 10, 4, 10 + 4); }
+testMinus(){ printf("%d - %d = %d\n", 10, 4, 10 - 4); }
+testShiftLeft(){ printf("%d << %d = %d\n", 10, 4, 10 << 4); }
+testShiftRight(){ printf("%d >> %d = %d\n", 100, 4, 100 >> 4); }
+testLess(){ printf("%d < %d = %d\n", 10, 4, 10 < 4); }
+testLesEqual(){ printf("%d <= %d = %d\n", 10, 4, 10 <= 4); }
+testGreater(){ printf("%d > %d = %d\n", 10, 4, 10 > 4); }
+testGreaterEqual(){ printf("%d >= %d = %d\n", 10, 4, 10 >= 4); }
+testEqualTo(){ printf("%d == %d = %d\n", 10, 4, 10 == 4); }
+testNotEqualTo(){ printf("%d != %d = %d\n", 10, 4, 10 != 4); }
+testBitAnd(){ printf("%d & %d = %d\n", 10, 7, 10 & 7); }
+testBitXor(){ printf("%d ^ %d = %d\n", 10, 7, 10 ^ 7); }
+testBitOr(){ printf("%d | %d = %d\n", 10, 4, 10 | 4); }
+testAssignment(){ int a, b; a = 3; b = a; printf("b == %d\n", b); }
+testLogicalAnd(){ printf("%d && %d = %d\n", 10, 4, 10 && 4); }
+testLogicalOr(){ printf("%d || %d = %d\n", 10, 4, 10 || 4); }
+testAddressOf(){ int a; printf("&a is %d\n", &a); }
+testPointerIndirection(){ int a, b; a = &b; b = 17; printf("*%d = %d =?= %d\n", a, * (int*) a, b); }
+testNegation(){ printf("-%d = %d\n", 10, -10); }
+testUnaryPlus(){ printf("+%d = %d\n", 10, +10); }
+testUnaryNot(){ printf("!%d = %d\n", 10, !10); }
+testBitNot(){ printf("~%d = %d\n", 10, ~10); }
+
+main(a,b) {
+ testInc();
+ testDec();
+ testTimes();
+ testDiv();
+ testMod();
+ testPlus();
+ testMinus();
+ testShiftLeft();
+ testShiftRight();
+ testLess();
+ testLesEqual();
+ testGreater();
+ testGreaterEqual();
+ testEqualTo();
+ testNotEqualTo();
+ testBitAnd();
+ testBinXor();
+ testBitOr();
+ testAssignment();
+ testLogicalAnd();
+ testLogicalOr();
+ testAddressOf();
+ testPointerIndirection();
+ testNegation();
+ testUnaryPlus();
+ testUnaryNot();
+ testBitNot();
+ return 0;
+}
\ No newline at end of file
diff --git a/libacc/tests/hello.c b/libacc/tests/hello.c
new file mode 100644
index 0000000..585ce6c
--- /dev/null
+++ b/libacc/tests/hello.c
@@ -0,0 +1,3 @@
+main(a,b) {
+ printf("Hello, world\n");
+}
diff --git a/libacc/tests/hello.out-orig b/libacc/tests/hello.out-orig
new file mode 100644
index 0000000..1fb7bf5
--- /dev/null
+++ b/libacc/tests/hello.out-orig
Binary files differ
diff --git a/libacc/tests/missing-main.c b/libacc/tests/missing-main.c
new file mode 100644
index 0000000..e73eec4
--- /dev/null
+++ b/libacc/tests/missing-main.c
@@ -0,0 +1,4 @@
+/* No main. */
+
+a() {
+}
\ No newline at end of file
diff --git a/libacc/tests/otcc.c b/libacc/tests/otcc.c
new file mode 100644
index 0000000..577fcf3
--- /dev/null
+++ b/libacc/tests/otcc.c
@@ -0,0 +1,446 @@
+#include <stdio.h>
+#define k *(int*)
+#define a if(
+#define c ad()
+#define i else
+#define p while(
+#define x *(char*)
+#define b ==
+#define V =calloc(1,99999)
+#define f ()
+#define J return
+#define l ae(
+#define n e)
+#define u d!=
+#define F int
+#define y (j)
+#define r m=
+#define t +4
+F d,z,C,h,P,K,ac,q,G,v,Q,R,D,L,W,M;
+E(n{
+x D++=e;
+}
+o f{
+a L){
+h=x L++;
+a h b 2){
+L=0;
+h=W;
+}
+}
+i h=fgetc(Q);
+}
+X f{
+J isalnum(h)|h b 95;
+}
+Y f{
+a h b 92){
+o f;
+a h b 110)h=10;
+}
+}
+c{
+F e,j,m;
+p isspace(h)|h b 35){
+a h b 35){
+o f;
+c;
+a d b 536){
+c;
+E(32);
+k d=1;
+k(d t)=D;
+}
+p h!=10){
+E(h);
+o f;
+}
+E(h);
+E(2);
+}
+o f;
+}
+C=0;
+d=h;
+a X f){
+E(32);
+M=D;
+p X f){
+E(h);
+o f;
+}
+a isdigit(d)){
+z=strtol(M,0,0);
+d=2;
+}
+i{
+x D=32;
+d=strstr(R,M-1)-R;
+x D=0;
+d=d*8+256;
+a d>536){
+d=P+d;
+a k d b 1){
+L=k(d t);
+W=h;
+o f;
+c;
+}
+}
+}
+}
+i{
+o f;
+a d b 39){
+d=2;
+Y f;
+z=h;
+o f;
+o f;
+}
+i a d b 47&h b 42){
+o f;
+p h){
+p h!=42)o f;
+o f;
+a h b 47)h=0;
+}
+o f;
+c;
+}
+i{
+e="++#m--%am*@R<^1c/@%[_[H3c%@%[_[H3c+@.B#d-@%:_^BKd<<Z/03e>>`/03e<=0f>=/f<@.f>@1f==&g!='g&&k||#l&@.BCh^@.BSi|@.B+j~@/%Yd!@&d*@b";
+p j=x e++){
+r x e++;
+z=0;
+p(C=x e++-98)<0)z=z*64+C+64;
+a j b d&(m b h|m b 64)){
+a m b h){
+o f;
+d=1;
+}
+break;
+}
+}
+}
+}
+}
+l g){
+p g&&g!=-1){
+x q++=g;
+g=g>>8;
+}
+}
+A(n{
+F g;
+p n{
+g=k e;
+k e=q-e-4;
+e=g;
+}
+}
+s(g,n{
+l g);
+k q=e;
+e=q;
+q=q t;
+J e;
+}
+H(n{
+s(184,n;
+}
+B(n{
+J s(233,n;
+}
+S(j,n{
+l 1032325);
+J s(132+j,n;
+}
+Z(n{
+l 49465);
+H(0);
+l 15);
+l e+144);
+l 192);
+}
+N(j,n{
+l j+131);
+s((e<512)<<7|5,n;
+}
+T y{
+F g,e,m,aa;
+g=1;
+a d b 34){
+H(v);
+p h!=34){
+Y f;
+x v++=h;
+o f;
+}
+x v=0;
+v=v t&-4;
+o f;
+c;
+}
+i{
+aa=C;
+r z;
+e=d;
+c;
+a e b 2){
+H(m);
+}
+i a aa b 2){
+T(0);
+s(185,0);
+a e b 33)Z(m);
+i l m);
+}
+i a e b 40){
+w f;
+c;
+}
+i a e b 42){
+c;
+e=d;
+c;
+c;
+a d b 42){
+c;
+c;
+c;
+c;
+e=0;
+}
+c;
+T(0);
+a d b 61){
+c;
+l 80);
+w f;
+l 89);
+l 392+(e b 256));
+}
+i a n{
+a e b 256)l 139);
+i l 48655);
+q++;
+}
+}
+i a e b 38){
+N(10,k d);
+c;
+}
+i{
+g=k e;
+a!g)g=dlsym(0,M);
+a d b 61&j){
+c;
+w f;
+N(6,g);
+}
+i a u 40){
+N(8,g);
+a C b 11){
+N(0,g);
+l z);
+c;
+}
+}
+}
+}
+a d b 40){
+a g b 1)l 80);
+r s(60545,0);
+c;
+j=0;
+p u 41){
+w f;
+s(2393225,j);
+a d b 44)c;
+j=j t;
+}
+k r j;
+c;
+a!g){
+e=e t;
+k e=s(232,k n;
+}
+i a g b 1){
+s(2397439,j);
+j=j t;
+}
+i{
+s(232,g-q-5);
+}
+a j)s(50305,j);
+}
+}
+O y{
+F e,g,m;
+a j--b 1)T(1);
+i{
+O y;
+r 0;
+p j b C){
+g=d;
+e=z;
+c;
+a j>8){
+r S(e,m);
+O y;
+}
+i{
+l 80);
+O y;
+l 89);
+a j b 4|j b 5){
+Z(n;
+}
+i{
+l n;
+a g b 37)l 146);
+}
+}
+}
+a m&&j>8){
+r S(e,m);
+H(e^1);
+B(5);
+A(m);
+H(n;
+}
+}
+}
+w f{
+O(11);
+}
+U f{
+w f;
+J S(0,0);
+}
+I y{
+F m,g,e;
+a d b 288){
+c;
+c;
+r U f;
+c;
+I y;
+a d b 312){
+c;
+g=B(0);
+A(m);
+I y;
+A(g);
+}
+i{
+A(m);
+}
+}
+i a d b 352|d b 504){
+e=d;
+c;
+c;
+a e b 352){
+g=q;
+r U f;
+}
+i{
+a u 59)w f;
+c;
+g=q;
+r 0;
+a u 59)r U f;
+c;
+a u 41){
+e=B(0);
+w f;
+B(g-q-5);
+A(n;
+g=e t;
+}
+}
+c;
+I(&m);
+B(g-q-5);
+A(m);
+}
+i a d b 123){
+c;
+ab(1);
+p u 125)I y;
+c;
+}
+i{
+a d b 448){
+c;
+a u 59)w f;
+K=B(K);
+}
+i a d b 400){
+c;
+k j=B(k j);
+}
+i a u 59)w f;
+c;
+}
+}
+ab y{
+F m;
+p d b 256|u-1&!j){
+a d b 256){
+c;
+p u 59){
+a j){
+G=G t;
+k d=-G;
+}
+i{
+k d=v;
+v=v t;
+}
+c;
+a d b 44)c;
+}
+c;
+}
+i{
+A(k(d t));
+k d=q;
+c;
+c;
+r 8;
+p u 41){
+k d=m;
+r m t;
+c;
+a d b 44)c;
+}
+c;
+K=G=0;
+l 15042901);
+r s(60545,0);
+I(0);
+A(K);
+l 50121);
+k r G;
+}
+}
+}
+main(g,n{
+Q=stdin;
+a g-->1){
+e=e t;
+Q=fopen(k e,"r");
+}
+D=strcpy(R V," int if else while break return for define main ")+48;
+v V;
+q=ac V;
+P V;
+o f;
+c;
+ab(0);
+J(*(int(*)f)k(P+592))(g,n;
+}
+
diff --git a/libacc/tests/otcc.out-orig b/libacc/tests/otcc.out-orig
new file mode 100644
index 0000000..fa14c72
--- /dev/null
+++ b/libacc/tests/otcc.out-orig
Binary files differ
diff --git a/libacc/tests/returnval.c b/libacc/tests/returnval.c
new file mode 100644
index 0000000..3142fe2
--- /dev/null
+++ b/libacc/tests/returnval.c
@@ -0,0 +1,3 @@
+main() {
+ return 42;
+}
\ No newline at end of file
diff --git a/libacc/tests/simplest.c b/libacc/tests/simplest.c
new file mode 100644
index 0000000..bae895a
--- /dev/null
+++ b/libacc/tests/simplest.c
@@ -0,0 +1 @@
+main() {}
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index f5e49f5..c9b0ebe 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -37,6 +37,9 @@
properties.c \
threads.c
+commonHostSources := \
+ ashmem-host.c
+
# some files must not be compiled when building against Mingw
# they correspond to features not used by our host development tools
# which are also hard or even impossible to port to native Win32
@@ -59,16 +62,18 @@
mspace.c \
selector.c \
tztime.c \
- tzstrftime.c \
adb_networking.c \
- zygote.c
+ zygote.c
+
+ commonHostSources += \
+ tzstrftime.c
endif
# Static library for host
# ========================================================
LOCAL_MODULE := libcutils
-LOCAL_SRC_FILES := $(commonSources) ashmem-host.c
+LOCAL_SRC_FILES := $(commonSources) $(commonHostSources)
LOCAL_LDLIBS := -lpthread
LOCAL_STATIC_LIBRARIES := liblog
include $(BUILD_HOST_STATIC_LIBRARY)
@@ -80,7 +85,7 @@
# ========================================================
include $(CLEAR_VARS)
LOCAL_MODULE := libcutils
-LOCAL_SRC_FILES := $(commonSources) memory.c dlmalloc_stubs.c ashmem-host.c
+LOCAL_SRC_FILES := $(commonSources) $(commonHostSources) memory.c dlmalloc_stubs.c
LOCAL_LDLIBS := -lpthread
LOCAL_SHARED_LIBRARIES := liblog
include $(BUILD_SHARED_LIBRARY)
diff --git a/libcutils/strdup16to8.c b/libcutils/strdup16to8.c
index fadaabe..1a8ba86 100644
--- a/libcutils/strdup16to8.c
+++ b/libcutils/strdup16to8.c
@@ -15,6 +15,8 @@
** limitations under the License.
*/
+#include <limits.h> /* for SIZE_MAX */
+
#include <cutils/jstring.h>
#include <assert.h>
#include <stdlib.h>
@@ -26,19 +28,67 @@
*/
extern size_t strnlen16to8(const char16_t* utf16Str, size_t len)
{
- size_t utf8Len = 0;
+ size_t utf8Len = 0;
- while (len--) {
- unsigned int uic = *utf16Str++;
+ /* A small note on integer overflow. The result can
+ * potentially be as big as 3*len, which will overflow
+ * for len > SIZE_MAX/3.
+ *
+ * Moreover, the result of a strnlen16to8 is typically used
+ * to allocate a destination buffer to strncpy16to8 which
+ * requires one more byte to terminate the UTF-8 copy, and
+ * this is generally done by careless users by incrementing
+ * the result without checking for integer overflows, e.g.:
+ *
+ * dst = malloc(strnlen16to8(utf16,len)+1)
+ *
+ * Due to this, the following code will try to detect
+ * overflows, and never return more than (SIZE_MAX-1)
+ * when it detects one. A careless user will try to malloc
+ * SIZE_MAX bytes, which will return NULL which can at least
+ * be detected appropriately.
+ *
+ * As far as I know, this function is only used by strndup16(),
+ * but better be safe than sorry.
+ */
- if (uic > 0x07ff)
- utf8Len += 3;
- else if (uic > 0x7f || uic == 0)
- utf8Len += 2;
- else
- utf8Len++;
- }
- return utf8Len;
+ /* Fast path for the usual case where 3*len is < SIZE_MAX-1.
+ */
+ if (len < (SIZE_MAX-1)/3) {
+ while (len--) {
+ unsigned int uic = *utf16Str++;
+
+ if (uic > 0x07ff)
+ utf8Len += 3;
+ else if (uic > 0x7f || uic == 0)
+ utf8Len += 2;
+ else
+ utf8Len++;
+ }
+ return utf8Len;
+ }
+
+ /* The slower but paranoid version */
+ while (len--) {
+ unsigned int uic = *utf16Str++;
+ size_t utf8Cur = utf8Len;
+
+ if (uic > 0x07ff)
+ utf8Len += 3;
+ else if (uic > 0x7f || uic == 0)
+ utf8Len += 2;
+ else
+ utf8Len++;
+
+ if (utf8Len < utf8Cur) /* overflow detected */
+ return SIZE_MAX-1;
+ }
+
+ /* don't return SIZE_MAX to avoid common user bug */
+ if (utf8Len == SIZE_MAX)
+ utf8Len = SIZE_MAX-1;
+
+ return utf8Len;
}
@@ -50,7 +100,7 @@
*
* Make sure you allocate "utf8Str" with the result of strlen16to8() + 1,
* not just "len".
- *
+ *
* Please note, a terminated \0 is always added, so your result will always
* be "strlen16to8() + 1" bytes long.
*/
@@ -58,6 +108,10 @@
{
char* utf8cur = utf8Str;
+ /* Note on overflows: We assume the user did check the result of
+ * strnlen16to8() properly or at a minimum checked the result of
+ * its malloc(SIZE_MAX) in case of overflow.
+ */
while (len--) {
unsigned int uic = *utf16Str++;
@@ -73,8 +127,8 @@
if (uic == 0) {
break;
- }
- }
+ }
+ }
}
*utf8cur = '\0';
@@ -85,20 +139,30 @@
/**
* Convert a UTF-16 string to UTF-8.
*
- * Make sure you allocate "dest" with the result of strblen16to8(),
- * not just "strlen16()".
*/
char * strndup16to8 (const char16_t* s, size_t n)
{
- char *ret;
+ char* ret;
+ size_t len;
if (s == NULL) {
return NULL;
}
- ret = malloc(strnlen16to8(s, n) + 1);
+ len = strnlen16to8(s, n);
+
+ /* We are paranoid, and we check for SIZE_MAX-1
+ * too since it is an overflow value for our
+ * strnlen16to8 implementation.
+ */
+ if (len >= SIZE_MAX-1)
+ return NULL;
+
+ ret = malloc(len + 1);
+ if (ret == NULL)
+ return NULL;
strncpy16to8 (ret, s, n);
-
- return ret;
+
+ return ret;
}
diff --git a/liblog/logprint.c b/liblog/logprint.c
index 2cf1254..080f9e3 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -23,7 +23,6 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
-#include <alloca.h>
#include <assert.h>
#include <arpa/inet.h>
diff --git a/libpixelflinger/Android.mk b/libpixelflinger/Android.mk
index 50eb5f5..0cc85d9 100644
--- a/libpixelflinger/Android.mk
+++ b/libpixelflinger/Android.mk
@@ -64,12 +64,14 @@
LOCAL_MODULE:= libpixelflinger
LOCAL_SRC_FILES := $(PIXELFLINGER_SRC_FILES)
LOCAL_CFLAGS := $(PIXELFLINGER_CFLAGS)
+
ifneq ($(BUILD_TINY_ANDROID),true)
# Really this should go away entirely or at least not depend on
# libhardware, but this at least gets us built.
LOCAL_SHARED_LIBRARIES += libhardware_legacy
LOCAL_CFLAGS += -DWITH_LIB_HARDWARE
endif
+
ifeq ($(TARGET_ARCH),arm)
LOCAL_WHOLE_STATIC_LIBRARIES := libpixelflinger_armv6
endif
diff --git a/toolbox/ls.c b/toolbox/ls.c
index f609df21..087e4d5 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -15,9 +15,10 @@
#include <linux/kdev_t.h>
// bits for flags argument
-#define LIST_LONG (1 << 0)
-#define LIST_ALL (1 << 1)
-#define LIST_RECURSIVE (1 << 2)
+#define LIST_LONG (1 << 0)
+#define LIST_ALL (1 << 1)
+#define LIST_RECURSIVE (1 << 2)
+#define LIST_DIRECTORIES (1 << 3)
// fwd
static int listpath(const char *name, int flags);
@@ -238,7 +239,7 @@
return -1;
}
- if (S_ISDIR(s.st_mode)) {
+ if ((flags & LIST_DIRECTORIES) == 0 && S_ISDIR(s.st_mode)) {
if (flags & LIST_RECURSIVE)
printf("\n%s:\n", name);
return listdir(name, flags);
@@ -269,6 +270,8 @@
flags |= LIST_ALL;
} else if (!strcmp(argv[i], "-R")) {
flags |= LIST_RECURSIVE;
+ } else if (!strcmp(argv[i], "-d")) {
+ flags |= LIST_DIRECTORIES;
} else {
listed++;
if(listpath(argv[i], flags) != 0) {
diff --git a/toolbox/mkdosfs.c b/toolbox/mkdosfs.c
index 744aad1..66e720b 100644
--- a/toolbox/mkdosfs.c
+++ b/toolbox/mkdosfs.c
@@ -393,7 +393,7 @@
bpb.bsec = length / bpb.bps;
bpb.spt = bpb.bsec;
// use FAT32 for 2 gig or greater
- if (length >= 2 *1024 *1024 *1024) {
+ if (length >= 2LL *1024 *1024 *1024) {
fat = 32;
} else {
fat = 16;
diff --git a/toolbox/mount.c b/toolbox/mount.c
index ef13e1f..395c943 100644
--- a/toolbox/mount.c
+++ b/toolbox/mount.c
@@ -138,14 +138,17 @@
if (loop) {
int file_fd, device_fd;
+ int flags;
+
+ flags = (rwflag & MS_RDONLY) ? O_RDONLY : O_RDWR;
// FIXME - only one loop mount supported at a time
- file_fd = open(dev, O_RDWR);
+ file_fd = open(dev, flags);
if (file_fd < -1) {
perror("open backing file failed");
return 1;
}
- device_fd = open(LOOP_DEVICE, O_RDWR);
+ device_fd = open(LOOP_DEVICE, flags);
if (device_fd < -1) {
perror("open loop device failed");
close(file_fd);
diff --git a/toolbox/smd.c b/toolbox/smd.c
index 65ff994e..91e495c 100644
--- a/toolbox/smd.c
+++ b/toolbox/smd.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
diff --git a/vold/mmc.c b/vold/mmc.c
index 0f08964..fb61b2f 100644
--- a/vold/mmc.c
+++ b/vold/mmc.c
@@ -250,7 +250,7 @@
char filename[255];
char *uevent_buffer;
ssize_t sz;
- char *uevent_params[4];
+ char *uevent_params[5];
char tmp[255];
FILE *fp;
char line[255];