2009-07-10 12:58:34 -07:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version 1.1 (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.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
|
|
|
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
|
|
|
* language governing rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is [Open Source Virtual Machine.]
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Adobe System Incorporated. Portions created
|
|
|
|
* by the Initial Developer are Copyright (C)[ 2004-2006 ] Adobe Systems Incorporated. All Rights
|
|
|
|
* Reserved.
|
2008-06-18 19:55:26 -07:00
|
|
|
*
|
|
|
|
* Contributor(s): Adobe AS3 Team
|
2008-06-28 09:58:15 -07:00
|
|
|
* Andreas Gal <gal@mozilla.com>
|
2008-09-02 10:15:26 -07:00
|
|
|
* Asko Tontti <atontti@cc.hut.fi>
|
2009-07-10 12:58:34 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of either the GNU
|
|
|
|
* General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public
|
|
|
|
* License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the
|
|
|
|
* LGPL are applicable instead of those above. If you wish to allow use of your version of this
|
|
|
|
* file only under the terms of either the GPL or the LGPL, and not to allow others to use your
|
|
|
|
* version of this file under the terms of the MPL, indicate your decision by deleting provisions
|
|
|
|
* above and replace them with the notice and other provisions required by the GPL or the
|
|
|
|
* LGPL. If you do not delete the provisions above, a recipient may use your version of this file
|
|
|
|
* under the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
2008-06-18 19:55:26 -07:00
|
|
|
***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef avm_h___
|
|
|
|
#define avm_h___
|
|
|
|
|
2009-09-30 17:45:17 -07:00
|
|
|
#include "VMPI.h"
|
2010-02-16 18:36:29 -08:00
|
|
|
#include "njcpudetect.h"
|
2010-02-15 17:56:41 -08:00
|
|
|
#include "njconfig.h"
|
2008-06-18 19:55:26 -07:00
|
|
|
|
2009-02-15 18:10:03 -08:00
|
|
|
#if !defined(AVMPLUS_LITTLE_ENDIAN) && !defined(AVMPLUS_BIG_ENDIAN)
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
|
|
|
#define AVMPLUS_BIG_ENDIAN
|
|
|
|
#else
|
|
|
|
#define AVMPLUS_LITTLE_ENDIAN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-09-18 16:07:49 -07:00
|
|
|
#if defined(_MSC_VER) && defined(_M_IX86)
|
|
|
|
#define FASTCALL __fastcall
|
|
|
|
#elif defined(__GNUC__) && defined(__i386__) && \
|
|
|
|
((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
|
|
|
|
#define FASTCALL __attribute__((fastcall))
|
|
|
|
#else
|
|
|
|
#define FASTCALL
|
|
|
|
#define NO_FASTCALL
|
|
|
|
#endif
|
2008-08-10 00:39:18 -07:00
|
|
|
|
2009-09-18 16:07:49 -07:00
|
|
|
#if defined(NO_FASTCALL)
|
2008-09-02 11:43:55 -07:00
|
|
|
#if defined(AVMPLUS_IA32)
|
|
|
|
#define SIMULATE_FASTCALL(lr, state_ptr, frag_ptr, func_addr) \
|
|
|
|
asm volatile( \
|
|
|
|
"call *%%esi" \
|
|
|
|
: "=a" (lr) \
|
|
|
|
: "c" (state_ptr), "d" (frag_ptr), "S" (func_addr) \
|
|
|
|
: "memory", "cc" \
|
|
|
|
);
|
|
|
|
#endif /* defined(AVMPLUS_IA32) */
|
2009-09-18 16:07:49 -07:00
|
|
|
#endif /* defined(NO_FASTCALL) */
|
2008-09-02 11:43:55 -07:00
|
|
|
|
2008-09-02 10:28:14 -07:00
|
|
|
#ifdef WIN32
|
2008-07-10 15:39:51 -07:00
|
|
|
#include <windows.h>
|
2009-08-24 01:21:37 -07:00
|
|
|
#elif defined(AVMPLUS_OS2)
|
|
|
|
#define INCL_DOSMEMMGR
|
|
|
|
#include <os2.h>
|
2008-06-18 20:45:37 -07:00
|
|
|
#endif
|
|
|
|
|
2010-04-28 02:36:04 -07:00
|
|
|
#if defined(__SUNPRO_CC)
|
|
|
|
#define __asm__ asm
|
|
|
|
#define __volatile__ volatile
|
|
|
|
#define __inline__ inline
|
|
|
|
#endif
|
|
|
|
|
2009-10-15 16:04:55 -07:00
|
|
|
#if defined(DEBUG) || defined(NJ_NO_VARIADIC_MACROS)
|
2008-07-29 17:19:51 -07:00
|
|
|
#if !defined _DEBUG
|
2008-06-19 15:32:40 -07:00
|
|
|
#define _DEBUG
|
2008-07-29 17:19:51 -07:00
|
|
|
#endif
|
2008-09-24 10:21:49 -07:00
|
|
|
#define NJ_VERBOSE 1
|
2008-06-19 15:32:40 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
2008-06-19 10:47:58 -07:00
|
|
|
|
2008-08-28 19:07:49 -07:00
|
|
|
#ifdef _DEBUG
|
2010-04-13 12:41:15 -07:00
|
|
|
namespace avmplus {
|
|
|
|
void AvmAssertFail(const char* msg);
|
|
|
|
}
|
2008-08-28 19:07:49 -07:00
|
|
|
#endif
|
|
|
|
|
2009-10-27 02:00:15 -07:00
|
|
|
#if defined(AVMPLUS_IA32)
|
|
|
|
#if defined(_MSC_VER)
|
2010-03-28 15:31:53 -07:00
|
|
|
|
|
|
|
# define AVMPLUS_HAS_RDTSC 1
|
|
|
|
|
2009-10-27 02:00:15 -07:00
|
|
|
__declspec(naked) static inline __int64 rdtsc()
|
2008-07-29 17:19:51 -07:00
|
|
|
{
|
2009-10-27 02:00:15 -07:00
|
|
|
__asm
|
|
|
|
{
|
|
|
|
rdtsc;
|
|
|
|
ret;
|
|
|
|
}
|
2008-07-29 17:19:51 -07:00
|
|
|
}
|
2010-03-28 15:31:53 -07:00
|
|
|
|
2010-04-28 02:36:04 -07:00
|
|
|
#elif defined(__i386__) || defined(__i386)
|
2010-03-28 15:31:53 -07:00
|
|
|
|
|
|
|
# define AVMPLUS_HAS_RDTSC 1
|
|
|
|
|
2008-07-01 02:37:07 -07:00
|
|
|
static __inline__ unsigned long long rdtsc(void)
|
|
|
|
{
|
|
|
|
unsigned long long int x;
|
2010-04-28 02:36:04 -07:00
|
|
|
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
|
|
|
|
return x;
|
2008-07-01 02:37:07 -07:00
|
|
|
}
|
2010-03-28 15:31:53 -07:00
|
|
|
|
2008-09-05 16:56:03 -07:00
|
|
|
#endif /* compilers */
|
|
|
|
|
2008-07-01 02:37:07 -07:00
|
|
|
#elif defined(__x86_64__)
|
|
|
|
|
2010-03-28 15:31:53 -07:00
|
|
|
# define AVMPLUS_HAS_RDTSC 1
|
|
|
|
|
2008-08-18 12:41:55 -07:00
|
|
|
static __inline__ uint64_t rdtsc(void)
|
2008-07-01 02:37:07 -07:00
|
|
|
{
|
|
|
|
unsigned hi, lo;
|
|
|
|
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
|
2008-08-18 12:41:55 -07:00
|
|
|
return ( (uint64_t)lo)|( ((uint64_t)hi)<<32 );
|
2008-07-01 02:37:07 -07:00
|
|
|
}
|
|
|
|
|
2009-10-27 02:00:15 -07:00
|
|
|
#elif defined(_MSC_VER) && defined(_M_AMD64)
|
|
|
|
|
2010-03-28 15:31:53 -07:00
|
|
|
# define AVMPLUS_HAS_RDTSC 1
|
|
|
|
|
2009-10-27 02:00:15 -07:00
|
|
|
#include <intrin.h>
|
|
|
|
#pragma intrinsic(__rdtsc)
|
|
|
|
|
|
|
|
static inline unsigned __int64 rdtsc(void)
|
|
|
|
{
|
|
|
|
return __rdtsc();
|
|
|
|
}
|
|
|
|
|
2008-07-01 02:37:07 -07:00
|
|
|
#elif defined(__powerpc__)
|
|
|
|
|
2010-03-28 15:31:53 -07:00
|
|
|
# define AVMPLUS_HAS_RDTSC 1
|
|
|
|
|
2008-07-01 02:37:07 -07:00
|
|
|
typedef unsigned long long int unsigned long long;
|
|
|
|
|
|
|
|
static __inline__ unsigned long long rdtsc(void)
|
|
|
|
{
|
|
|
|
unsigned long long int result=0;
|
|
|
|
unsigned long int upper, lower,tmp;
|
|
|
|
__asm__ volatile(
|
|
|
|
"0: \n"
|
|
|
|
"\tmftbu %0 \n"
|
|
|
|
"\tmftb %1 \n"
|
|
|
|
"\tmftbu %2 \n"
|
|
|
|
"\tcmpw %2,%0 \n"
|
|
|
|
"\tbne 0b \n"
|
|
|
|
: "=r"(upper),"=r"(lower),"=r"(tmp)
|
|
|
|
);
|
|
|
|
result = upper;
|
|
|
|
result = result<<32;
|
|
|
|
result = result|lower;
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2008-09-05 16:56:03 -07:00
|
|
|
#endif /* architecture */
|
2008-07-01 02:37:07 -07:00
|
|
|
|
2010-03-28 15:31:53 -07:00
|
|
|
#ifndef AVMPLUS_HAS_RDTSC
|
|
|
|
# define AVMPLUS_HAS_RDTSC 0
|
|
|
|
#endif
|
|
|
|
|
2008-07-08 17:16:51 -07:00
|
|
|
struct JSContext;
|
|
|
|
|
2009-09-18 13:31:09 -07:00
|
|
|
#ifdef PERFM
|
|
|
|
# define PERFM_NVPROF(n,v) _nvprof(n,v)
|
|
|
|
# define PERFM_NTPROF(n) _ntprof(n)
|
|
|
|
# define PERFM_TPROF_END() _tprof_end()
|
|
|
|
#else
|
|
|
|
# define PERFM_NVPROF(n,v)
|
|
|
|
# define PERFM_NTPROF(n)
|
|
|
|
# define PERFM_TPROF_END()
|
|
|
|
#endif
|
|
|
|
|
2009-07-16 11:50:27 -07:00
|
|
|
namespace avmplus {
|
|
|
|
|
2008-11-07 15:52:51 -08:00
|
|
|
typedef int FunctionID;
|
2008-06-19 10:47:58 -07:00
|
|
|
|
2009-07-15 16:50:01 -07:00
|
|
|
extern void AvmLog(char const *msg, ...);
|
|
|
|
|
2008-06-19 15:32:40 -07:00
|
|
|
static const int kstrconst_emptyString = 0;
|
|
|
|
|
|
|
|
class AvmInterpreter
|
|
|
|
{
|
|
|
|
class Labels {
|
|
|
|
public:
|
2008-07-07 02:47:40 -07:00
|
|
|
const char* format(const void* ip)
|
2008-06-19 15:32:40 -07:00
|
|
|
{
|
|
|
|
static char buf[33];
|
|
|
|
sprintf(buf, "%p", ip);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Labels _labels;
|
|
|
|
public:
|
|
|
|
Labels* labels;
|
|
|
|
|
|
|
|
AvmInterpreter()
|
|
|
|
{
|
|
|
|
labels = &_labels;
|
|
|
|
}
|
|
|
|
|
2008-06-19 10:47:58 -07:00
|
|
|
};
|
2009-07-10 12:58:34 -07:00
|
|
|
|
|
|
|
class AvmConsole
|
2008-06-18 20:29:01 -07:00
|
|
|
{
|
2008-06-19 10:47:58 -07:00
|
|
|
public:
|
2008-06-19 15:32:40 -07:00
|
|
|
AvmConsole& operator<<(const char* s)
|
|
|
|
{
|
2008-07-18 14:13:31 -07:00
|
|
|
fprintf(stdout, "%s", s);
|
2008-06-19 15:32:40 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class AvmCore
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AvmInterpreter interp;
|
|
|
|
AvmConsole console;
|
2009-07-10 12:58:34 -07:00
|
|
|
|
2010-02-15 17:56:41 -08:00
|
|
|
static nanojit::Config config;
|
2008-06-19 15:32:40 -07:00
|
|
|
|
2009-06-10 15:21:10 -07:00
|
|
|
#ifdef AVMPLUS_IA32
|
2008-06-19 15:32:40 -07:00
|
|
|
static inline bool
|
|
|
|
use_sse2()
|
2008-06-19 10:47:58 -07:00
|
|
|
{
|
2010-02-15 17:56:41 -08:00
|
|
|
return config.i386_sse2;
|
2008-06-19 10:47:58 -07:00
|
|
|
}
|
2009-06-10 15:21:10 -07:00
|
|
|
#endif
|
2009-07-10 12:58:34 -07:00
|
|
|
|
2008-09-26 20:39:21 -07:00
|
|
|
static inline bool
|
|
|
|
use_cmov()
|
|
|
|
{
|
2009-06-10 15:21:10 -07:00
|
|
|
#ifdef AVMPLUS_IA32
|
2010-02-15 17:56:41 -08:00
|
|
|
return config.i386_use_cmov;
|
2009-06-10 15:21:10 -07:00
|
|
|
#else
|
2009-07-10 12:58:34 -07:00
|
|
|
return true;
|
2008-12-11 13:50:55 -08:00
|
|
|
#endif
|
2009-06-10 15:21:10 -07:00
|
|
|
}
|
2008-06-18 20:29:01 -07:00
|
|
|
};
|
2008-06-19 15:32:40 -07:00
|
|
|
|
2008-06-18 20:29:01 -07:00
|
|
|
/**
|
2009-07-10 12:58:34 -07:00
|
|
|
* Bit vectors are an efficent method of keeping True/False information
|
|
|
|
* on a set of items or conditions. Class BitSet provides functions
|
2008-06-18 20:29:01 -07:00
|
|
|
* to manipulate individual bits in the vector.
|
|
|
|
*
|
|
|
|
* This object is not optimized for a fixed sized bit vector
|
|
|
|
* it instead allows for dynamically growing the bit vector.
|
2009-07-10 12:58:34 -07:00
|
|
|
*/
|
2008-06-18 20:29:01 -07:00
|
|
|
class BitSet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum { kUnit = 8*sizeof(long),
|
|
|
|
kDefaultCapacity = 4 };
|
|
|
|
|
|
|
|
BitSet()
|
|
|
|
{
|
|
|
|
capacity = kDefaultCapacity;
|
2010-03-14 18:30:32 -07:00
|
|
|
ar = (long*)calloc(capacity, sizeof(long));
|
2008-06-18 20:29:01 -07:00
|
|
|
reset();
|
|
|
|
}
|
2009-07-10 12:58:34 -07:00
|
|
|
|
2008-06-18 20:29:01 -07:00
|
|
|
~BitSet()
|
|
|
|
{
|
2010-03-14 18:30:32 -07:00
|
|
|
free(ar);
|
2008-06-18 20:29:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
2010-03-14 18:30:32 -07:00
|
|
|
for (int i = 0; i < capacity; i++)
|
|
|
|
ar[i] = 0;
|
2008-06-18 20:29:01 -07:00
|
|
|
}
|
|
|
|
|
2009-07-08 13:21:55 -07:00
|
|
|
void set(int bitNbr)
|
2008-06-18 20:29:01 -07:00
|
|
|
{
|
|
|
|
int index = bitNbr / kUnit;
|
|
|
|
int bit = bitNbr % kUnit;
|
|
|
|
if (index >= capacity)
|
2009-07-08 13:21:55 -07:00
|
|
|
grow(index+1);
|
2008-06-18 20:29:01 -07:00
|
|
|
|
2010-03-14 18:30:32 -07:00
|
|
|
ar[index] |= (1<<bit);
|
2008-06-18 20:29:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear(int bitNbr)
|
|
|
|
{
|
|
|
|
int index = bitNbr / kUnit;
|
|
|
|
int bit = bitNbr % kUnit;
|
|
|
|
if (index < capacity)
|
2010-03-14 18:30:32 -07:00
|
|
|
ar[index] &= ~(1<<bit);
|
2008-06-18 20:29:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool get(int bitNbr) const
|
|
|
|
{
|
|
|
|
int index = bitNbr / kUnit;
|
|
|
|
int bit = bitNbr % kUnit;
|
|
|
|
bool value = false;
|
|
|
|
if (index < capacity)
|
2010-03-14 18:30:32 -07:00
|
|
|
value = ( ar[index] & (1<<bit) ) ? true : false;
|
2008-06-18 20:29:01 -07:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Grow the array until at least newCapacity big
|
2009-07-08 13:21:55 -07:00
|
|
|
void grow(int newCapacity)
|
2008-06-18 20:29:01 -07:00
|
|
|
{
|
2009-07-10 12:58:34 -07:00
|
|
|
// create vector that is 2x bigger than requested
|
2008-06-18 20:29:01 -07:00
|
|
|
newCapacity *= 2;
|
|
|
|
//MEMTAG("BitVector::Grow - long[]");
|
2010-03-14 18:30:32 -07:00
|
|
|
long* newAr = (long*)calloc(newCapacity, sizeof(long));
|
2008-06-18 20:29:01 -07:00
|
|
|
|
2009-07-10 12:58:34 -07:00
|
|
|
// copy the old one
|
2010-03-14 18:30:32 -07:00
|
|
|
for (int i = 0; i < capacity; i++)
|
|
|
|
newAr[i] = ar[i];
|
2008-06-18 20:29:01 -07:00
|
|
|
|
|
|
|
// in with the new out with the old
|
2010-03-14 18:30:32 -07:00
|
|
|
free(ar);
|
2008-06-18 20:29:01 -07:00
|
|
|
|
2010-03-14 18:30:32 -07:00
|
|
|
ar = newAr;
|
2008-06-18 20:29:01 -07:00
|
|
|
capacity = newCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
int capacity;
|
2010-03-14 18:30:32 -07:00
|
|
|
long* ar;
|
2008-06-18 20:29:01 -07:00
|
|
|
};
|
2008-06-18 19:55:26 -07:00
|
|
|
}
|
|
|
|
|
2008-06-18 20:29:01 -07:00
|
|
|
#endif
|