2008-06-04 22:21:27 +00:00
|
|
|
/* Copyright (c) 2008, Avian Contributors
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#ifndef POWERPC_H
|
|
|
|
#define POWERPC_H
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
# if __DARWIN_UNIX03 && defined(_STRUCT_X86_EXCEPTION_STATE32)
|
|
|
|
# define IP_REGISTER(context) (context->uc_mcontext->__ss.__srr0)
|
|
|
|
# define BASE_REGISTER(context) (context->uc_mcontext->__ss.__r13)
|
|
|
|
# define STACK_REGISTER(context) (context->uc_mcontext->__ss.__r1)
|
|
|
|
# define THREAD_REGISTER(context) (context->uc_mcontext->__ss.__r14)
|
|
|
|
# else
|
|
|
|
# define IP_REGISTER(context) (context->uc_mcontext->ss.srr0)
|
|
|
|
# define BASE_REGISTER(context) (context->uc_mcontext->ss.r13)
|
|
|
|
# define STACK_REGISTER(context) (context->uc_mcontext->ss.r1)
|
|
|
|
# define THREAD_REGISTER(context) (context->uc_mcontext->ss.r14)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define IP_REGISTER(context) (context->uc_mcontext.gregs[32])
|
|
|
|
# define BASE_REGISTER(context) (context->uc_mcontext.gregs[13])
|
|
|
|
# define STACK_REGISTER(context) (context->uc_mcontext.gregs[1])
|
|
|
|
# define THREAD_REGISTER(context) (context->uc_mcontext.gregs[14])
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" uint64_t
|
2008-06-08 19:47:37 +00:00
|
|
|
vmNativeCall(void* function, unsigned stackTotal, void* memoryTable,
|
|
|
|
unsigned memoryCount, void* gprTable, void* fprTable,
|
|
|
|
unsigned returnType);
|
2008-06-04 22:21:27 +00:00
|
|
|
|
|
|
|
namespace vm {
|
|
|
|
|
|
|
|
inline uint64_t
|
|
|
|
dynamicCall(void* function, uintptr_t* arguments, uint8_t* argumentTypes,
|
2008-06-15 20:17:52 +00:00
|
|
|
unsigned argumentCount, unsigned argumentsSize,
|
|
|
|
unsigned returnType)
|
2008-06-04 22:21:27 +00:00
|
|
|
{
|
2008-06-15 18:48:53 +00:00
|
|
|
const unsigned LinkageArea = 24;
|
|
|
|
|
2008-06-04 22:21:27 +00:00
|
|
|
const unsigned GprCount = 8;
|
|
|
|
uintptr_t gprTable[GprCount];
|
|
|
|
unsigned gprIndex = 0;
|
|
|
|
|
|
|
|
const unsigned FprCount = 13;
|
|
|
|
uint64_t fprTable[FprCount];
|
|
|
|
unsigned fprIndex = 0;
|
|
|
|
|
2008-06-15 20:17:52 +00:00
|
|
|
uintptr_t stack[argumentsSize / BytesPerWord];
|
2008-06-08 19:47:37 +00:00
|
|
|
unsigned stackSkip = 0;
|
2008-06-04 22:21:27 +00:00
|
|
|
unsigned stackIndex = 0;
|
|
|
|
|
2008-06-15 20:17:52 +00:00
|
|
|
unsigned ai = 0;
|
|
|
|
for (unsigned ati = 0; ati < argumentCount; ++ ati) {
|
|
|
|
switch (argumentTypes[ati]) {
|
2008-06-04 22:21:27 +00:00
|
|
|
case FLOAT_TYPE: {
|
|
|
|
if (fprIndex < FprCount) {
|
2008-06-15 20:17:52 +00:00
|
|
|
fprTable[fprIndex++] = arguments[ai];
|
2008-06-08 19:47:37 +00:00
|
|
|
++ gprIndex;
|
|
|
|
++ stackSkip;
|
2008-06-04 22:21:27 +00:00
|
|
|
} else {
|
2008-06-15 20:17:52 +00:00
|
|
|
stack[stackIndex++] = arguments[ai];
|
2008-06-04 22:21:27 +00:00
|
|
|
}
|
2008-06-15 20:17:52 +00:00
|
|
|
++ ai;
|
2008-06-04 22:21:27 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case DOUBLE_TYPE: {
|
|
|
|
if (fprIndex < FprCount) {
|
2008-06-15 20:17:52 +00:00
|
|
|
memcpy(fprTable + fprIndex, arguments + ai, 8);
|
2008-06-08 19:47:37 +00:00
|
|
|
++ fprIndex;
|
2008-06-04 22:21:27 +00:00
|
|
|
gprIndex += BytesPerWord / 4;
|
2008-06-08 19:47:37 +00:00
|
|
|
stackSkip += BytesPerWord / 4;
|
2008-06-04 22:21:27 +00:00
|
|
|
} else {
|
2008-06-15 20:17:52 +00:00
|
|
|
memcpy(stack + stackIndex, arguments + ai, 8);
|
2008-06-04 22:21:27 +00:00
|
|
|
stackIndex += BytesPerWord / 4;
|
|
|
|
}
|
2008-06-15 20:17:52 +00:00
|
|
|
ai += BytesPerWord / 4;
|
2008-06-04 22:21:27 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case INT64_TYPE: {
|
2008-06-15 20:17:52 +00:00
|
|
|
if (gprIndex + BytesPerWord / 4 <= GprCount) {
|
|
|
|
memcpy(gprTable + gprIndex, arguments + ai, 8);
|
2008-06-04 22:21:27 +00:00
|
|
|
gprIndex += BytesPerWord / 4;
|
2008-06-08 19:47:37 +00:00
|
|
|
stackSkip += BytesPerWord / 4;
|
2008-06-04 22:21:27 +00:00
|
|
|
} else {
|
2008-06-15 20:17:52 +00:00
|
|
|
memcpy(stack + stackIndex, arguments + ai, 8);
|
2008-06-04 22:21:27 +00:00
|
|
|
stackIndex += BytesPerWord / 4;
|
|
|
|
}
|
2008-06-15 20:17:52 +00:00
|
|
|
ai += BytesPerWord / 4;
|
2008-06-04 22:21:27 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
if (gprIndex < GprCount) {
|
2008-06-15 20:17:52 +00:00
|
|
|
gprTable[gprIndex++] = arguments[ai];
|
2008-06-08 19:47:37 +00:00
|
|
|
++ stackSkip;
|
2008-06-04 22:21:27 +00:00
|
|
|
} else {
|
2008-06-15 20:17:52 +00:00
|
|
|
stack[stackIndex++] = arguments[ai];
|
2008-06-04 22:21:27 +00:00
|
|
|
}
|
2008-06-15 20:17:52 +00:00
|
|
|
++ ai;
|
2008-06-04 22:21:27 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-15 18:28:48 +00:00
|
|
|
return vmNativeCall
|
|
|
|
(function,
|
2008-06-15 20:17:52 +00:00
|
|
|
- ((((1 + stackSkip + stackIndex) * BytesPerWord) + LinkageArea + 15)
|
|
|
|
& -16),
|
|
|
|
stack, stackIndex * BytesPerWord,
|
2008-06-15 18:28:48 +00:00
|
|
|
(gprIndex ? gprTable : 0),
|
|
|
|
(fprIndex ? fprTable : 0), returnType);
|
2008-06-04 22:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vm
|
|
|
|
|
|
|
|
#endif//POWERPC_H
|