2012-09-23 08:28:13 +00:00
|
|
|
|
|
|
|
#include "qv4isel_masm_p.h"
|
|
|
|
#include "qmljs_runtime.h"
|
|
|
|
#include "qmljs_objects.h"
|
|
|
|
|
|
|
|
#include <assembler/LinkBuffer.h>
|
2012-10-09 06:17:47 +00:00
|
|
|
#include <WTFStubs.h>
|
2012-09-23 08:28:13 +00:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#ifndef NO_UDIS86
|
|
|
|
# include <udis86.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace QQmlJS;
|
|
|
|
using namespace QQmlJS::MASM;
|
|
|
|
using namespace QQmlJS::VM;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
QTextStream qout(stdout, QIODevice::WriteOnly);
|
|
|
|
}
|
|
|
|
|
2012-10-09 06:17:47 +00:00
|
|
|
static void printDisassmbleOutputWithCalls(const char* output, const QHash<void*, const char*>& functions)
|
|
|
|
{
|
|
|
|
QByteArray processedOutput(output);
|
|
|
|
for (QHash<void*, const char*>::ConstIterator it = functions.begin(), end = functions.end();
|
|
|
|
it != end; ++it) {
|
|
|
|
QByteArray ptrString = QByteArray::number(qlonglong(it.key()), 16);
|
|
|
|
ptrString.prepend("0x");
|
|
|
|
processedOutput = processedOutput.replace(ptrString, it.value());
|
|
|
|
}
|
|
|
|
fprintf(stdout, "%s\n", processedOutput.constData());
|
|
|
|
}
|
|
|
|
|
2012-09-23 08:28:13 +00:00
|
|
|
InstructionSelection::InstructionSelection(VM::ExecutionEngine *engine, IR::Module *module, uchar *buffer)
|
|
|
|
: _engine(engine)
|
|
|
|
, _module(module)
|
|
|
|
, _function(0)
|
|
|
|
, _block(0)
|
|
|
|
, _buffer(buffer)
|
|
|
|
, _code(buffer)
|
|
|
|
, _codePtr(buffer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InstructionSelection::~InstructionSelection()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::operator()(IR::Function *function)
|
|
|
|
{
|
2012-10-01 07:29:20 +00:00
|
|
|
qSwap(_function, function);
|
|
|
|
|
2012-10-10 19:15:43 +00:00
|
|
|
int locals = (_function->tempCount - _function->locals.size() + _function->maxNumberOfArguments);
|
|
|
|
locals = (locals + 1) & ~1;
|
2012-10-04 20:46:42 +00:00
|
|
|
enterStandardStackFrame(locals);
|
2012-10-01 07:29:20 +00:00
|
|
|
|
|
|
|
push(ContextRegister);
|
2012-10-10 11:10:07 +00:00
|
|
|
#if CPU(X86)
|
2012-10-01 07:29:20 +00:00
|
|
|
loadPtr(addressForArgument(0), ContextRegister);
|
2012-10-10 11:10:07 +00:00
|
|
|
#elif CPU(X86_64)
|
|
|
|
move(RegisterArgument1, ContextRegister);
|
|
|
|
#else
|
|
|
|
assert(!"TODO");
|
|
|
|
#endif
|
2012-10-01 07:29:20 +00:00
|
|
|
|
|
|
|
foreach (IR::BasicBlock *block, _function->basicBlocks) {
|
|
|
|
_block = block;
|
2012-10-02 04:49:49 +00:00
|
|
|
_addrs[block] = label();
|
2012-10-01 07:29:20 +00:00
|
|
|
foreach (IR::Stmt *s, block->statements) {
|
|
|
|
s->accept(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pop(ContextRegister);
|
|
|
|
|
2012-10-04 20:46:42 +00:00
|
|
|
leaveStandardStackFrame(locals);
|
2012-10-01 07:29:20 +00:00
|
|
|
ret();
|
|
|
|
|
2012-10-02 04:49:49 +00:00
|
|
|
QHashIterator<IR::BasicBlock *, QVector<Jump> > it(_patches);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
IR::BasicBlock *block = it.key();
|
|
|
|
Label target = _addrs.value(block);
|
|
|
|
assert(target.isSet());
|
|
|
|
foreach (Jump jump, it.value())
|
|
|
|
jump.linkTo(target, this);
|
|
|
|
}
|
|
|
|
|
2012-09-23 08:28:13 +00:00
|
|
|
JSC::JSGlobalData dummy;
|
2012-10-01 07:29:20 +00:00
|
|
|
JSC::LinkBuffer linkBuffer(dummy, this, 0);
|
2012-10-09 06:17:47 +00:00
|
|
|
QHash<void*, const char*> functions;
|
|
|
|
foreach (CallToLink ctl, _callsToLink) {
|
2012-10-01 07:29:20 +00:00
|
|
|
linkBuffer.link(ctl.call, ctl.externalFunction);
|
2012-10-09 06:17:47 +00:00
|
|
|
functions[ctl.externalFunction.value()] = ctl.functionName;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* disasmOutput = 0;
|
|
|
|
size_t disasmLength = 0;
|
|
|
|
FILE* disasmStream = open_memstream(&disasmOutput, &disasmLength);
|
|
|
|
WTF::setDataFile(disasmStream);
|
|
|
|
|
2012-10-01 07:29:20 +00:00
|
|
|
_function->codeRef = linkBuffer.finalizeCodeWithDisassembly("operator()(IR::Function*)");
|
2012-10-09 06:17:47 +00:00
|
|
|
|
|
|
|
WTF::setDataFile(stdout);
|
|
|
|
fclose(disasmStream);
|
|
|
|
printDisassmbleOutputWithCalls(disasmOutput, functions);
|
|
|
|
free(disasmOutput);
|
|
|
|
|
2012-10-01 07:29:20 +00:00
|
|
|
_function->code = (void (*)(VM::Context *, const uchar *)) _function->codeRef.code().executableAddress();
|
|
|
|
|
|
|
|
qSwap(_function, function);
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String *InstructionSelection::identifier(const QString &s)
|
|
|
|
{
|
|
|
|
return _engine->identifier(s);
|
|
|
|
}
|
|
|
|
|
2012-10-07 19:43:29 +00:00
|
|
|
InstructionSelection::Pointer InstructionSelection::loadTempAddress(RegisterID reg, IR::Temp *t)
|
2012-10-01 19:39:25 +00:00
|
|
|
{
|
|
|
|
int32_t offset = 0;
|
|
|
|
if (t->index < 0) {
|
|
|
|
const int arg = -t->index - 1;
|
|
|
|
loadPtr(Address(ContextRegister, offsetof(Context, arguments)), reg);
|
|
|
|
offset = arg * sizeof(Value);
|
|
|
|
} else if (t->index < _function->locals.size()) {
|
|
|
|
loadPtr(Address(ContextRegister, offsetof(Context, locals)), reg);
|
|
|
|
offset = t->index * sizeof(Value);
|
|
|
|
} else {
|
|
|
|
const int arg = _function->maxNumberOfArguments + t->index - _function->locals.size();
|
2012-10-10 19:15:43 +00:00
|
|
|
offset = - sizeof(Value) * arg - sizeof(void*); // size of ebp
|
2012-10-01 19:39:25 +00:00
|
|
|
reg = StackFrameRegister;
|
|
|
|
}
|
2012-10-07 19:43:29 +00:00
|
|
|
return Pointer(reg, offset);
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::callActivationProperty(IR::Call *call, IR::Temp *result)
|
|
|
|
{
|
2012-10-02 06:55:18 +00:00
|
|
|
IR::Name *baseName = call->base->asName();
|
|
|
|
assert(baseName != 0);
|
|
|
|
|
2012-10-04 21:23:50 +00:00
|
|
|
if (baseName->id)
|
2012-10-07 19:43:29 +00:00
|
|
|
callRuntimeMethod(__qmljs_call_activation_property, result, call->base, call->args);
|
2012-10-04 21:23:50 +00:00
|
|
|
else {
|
2012-10-02 06:55:18 +00:00
|
|
|
switch (baseName->builtin) {
|
|
|
|
case IR::Name::builtin_invalid:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
case IR::Name::builtin_typeof:
|
2012-10-07 19:43:29 +00:00
|
|
|
callRuntimeMethod(__qmljs_builtin_typeof, result, call->args);
|
2012-10-02 06:55:18 +00:00
|
|
|
break;
|
|
|
|
case IR::Name::builtin_delete:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
case IR::Name::builtin_throw:
|
2012-10-07 19:43:29 +00:00
|
|
|
callRuntimeMethod(__qmljs_builtin_throw, result, call->args);
|
2012-10-02 06:55:18 +00:00
|
|
|
break;
|
2012-10-10 08:27:04 +00:00
|
|
|
case IR::Name::builtin_rethrow: {
|
|
|
|
int argc = prepareVariableArguments(call->args);
|
|
|
|
generateFunctionCall(__qmljs_builtin_rethrow, ContextRegister, result, baseAddressForCallArguments(), TrustedImm32(argc));
|
2012-10-02 06:55:18 +00:00
|
|
|
return; // we need to return to avoid checking the exceptions
|
|
|
|
}
|
2012-10-10 08:27:04 +00:00
|
|
|
}
|
2012-10-02 06:55:18 +00:00
|
|
|
}
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InstructionSelection::callValue(IR::Call *call, IR::Temp *result)
|
|
|
|
{
|
2012-10-07 19:57:10 +00:00
|
|
|
IR::Temp *baseTemp = call->base->asTemp();
|
|
|
|
assert(baseTemp != 0);
|
|
|
|
|
|
|
|
int argc = prepareVariableArguments(call->args);
|
|
|
|
IR::Temp* thisObject = 0;
|
|
|
|
generateFunctionCall(__qmljs_call_value, ContextRegister, result, baseTemp, thisObject, baseAddressForCallArguments(), TrustedImm32(argc));
|
|
|
|
checkExceptions();
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::callProperty(IR::Call *call, IR::Temp *result)
|
|
|
|
{
|
2012-10-07 19:57:10 +00:00
|
|
|
IR::Member *member = call->base->asMember();
|
|
|
|
assert(member != 0);
|
|
|
|
assert(member->base->asTemp() != 0);
|
|
|
|
|
|
|
|
int argc = prepareVariableArguments(call->args);
|
|
|
|
IR::Temp* thisObject = 0;
|
|
|
|
generateFunctionCall(__qmljs_call_property, ContextRegister, result, member->base->asTemp(), identifier(*member->name), baseAddressForCallArguments(), TrustedImm32(argc));
|
|
|
|
checkExceptions();
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::constructActivationProperty(IR::New *call, IR::Temp *result)
|
|
|
|
{
|
2012-10-04 21:23:50 +00:00
|
|
|
IR::Name *baseName = call->base->asName();
|
|
|
|
assert(baseName != 0);
|
|
|
|
|
2012-10-07 19:43:29 +00:00
|
|
|
callRuntimeMethod(__qmljs_construct_activation_property, result, call->base, call->args);
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::constructProperty(IR::New *call, IR::Temp *result)
|
|
|
|
{
|
2012-10-07 20:02:06 +00:00
|
|
|
IR::Member *member = call->base->asMember();
|
|
|
|
assert(member != 0);
|
|
|
|
assert(member->base->asTemp() != 0);
|
|
|
|
|
|
|
|
int argc = prepareVariableArguments(call->args);
|
|
|
|
IR::Temp* thisObject = 0;
|
|
|
|
generateFunctionCall(__qmljs_construct_property, ContextRegister, result, member->base->asTemp(), identifier(*member->name), baseAddressForCallArguments(), TrustedImm32(argc));
|
|
|
|
checkExceptions();
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::constructValue(IR::New *call, IR::Temp *result)
|
|
|
|
{
|
2012-10-07 20:02:06 +00:00
|
|
|
IR::Temp *baseTemp = call->base->asTemp();
|
|
|
|
assert(baseTemp != 0);
|
|
|
|
|
|
|
|
int argc = prepareVariableArguments(call->args);
|
|
|
|
generateFunctionCall(__qmljs_construct_value, ContextRegister, result, baseTemp, baseAddressForCallArguments(), TrustedImm32(argc));
|
|
|
|
checkExceptions();
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::checkExceptions()
|
|
|
|
{
|
2012-10-02 06:55:18 +00:00
|
|
|
Address addr(ContextRegister, offsetof(Context, hasUncaughtException));
|
|
|
|
Jump jmp = branch8(Equal, addr, TrustedImm32(1));
|
|
|
|
_patches[_function->handlersBlock].append(jmp);
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::visitExp(IR::Exp *s)
|
|
|
|
{
|
2012-10-07 20:02:41 +00:00
|
|
|
if (IR::Call *c = s->expr->asCall()) {
|
|
|
|
if (c->base->asName()) {
|
|
|
|
callActivationProperty(c, 0);
|
|
|
|
return;
|
|
|
|
} else if (c->base->asTemp()) {
|
|
|
|
callValue(c, 0);
|
|
|
|
return;
|
|
|
|
} else if (c->base->asMember()) {
|
|
|
|
callProperty(c, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-09-23 08:28:13 +00:00
|
|
|
assert(!"TODO");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::visitEnter(IR::Enter *)
|
|
|
|
{
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
assert(!"TODO");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::visitLeave(IR::Leave *)
|
|
|
|
{
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
assert(!"TODO");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::visitMove(IR::Move *s)
|
|
|
|
{
|
2012-10-01 07:29:20 +00:00
|
|
|
if (s->op == IR::OpInvalid) {
|
2012-10-02 06:55:18 +00:00
|
|
|
if (IR::Name *n = s->target->asName()) {
|
|
|
|
String *propertyName = identifier(*n->id);
|
|
|
|
|
|
|
|
if (IR::Temp *t = s->source->asTemp()) {
|
2012-10-05 09:35:46 +00:00
|
|
|
generateFunctionCall(__qmljs_set_activation_property, ContextRegister, propertyName, t);
|
2012-10-02 06:55:18 +00:00
|
|
|
checkExceptions();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
} else if (IR::Temp *t = s->target->asTemp()) {
|
2012-10-01 20:52:05 +00:00
|
|
|
if (IR::Name *n = s->source->asName()) {
|
|
|
|
if (*n->id == QStringLiteral("this")) { // ### `this' should be a builtin.
|
2012-10-05 09:35:46 +00:00
|
|
|
generateFunctionCall(__qmljs_get_thisObject, ContextRegister, t);
|
2012-10-01 20:52:05 +00:00
|
|
|
} else {
|
|
|
|
String *propertyName = identifier(*n->id);
|
2012-10-05 09:35:46 +00:00
|
|
|
generateFunctionCall(__qmljs_get_activation_property, ContextRegister, t, propertyName);
|
2012-10-01 20:52:05 +00:00
|
|
|
checkExceptions();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (IR::Const *c = s->source->asConst()) {
|
2012-10-01 19:39:25 +00:00
|
|
|
Address dest = loadTempAddress(Gpr0, t);
|
2012-10-01 07:29:20 +00:00
|
|
|
switch (c->type) {
|
2012-10-01 20:35:53 +00:00
|
|
|
case IR::NullType:
|
|
|
|
storeValue<Value::Null_Type>(TrustedImm32(0), dest);
|
|
|
|
break;
|
|
|
|
case IR::UndefinedType:
|
|
|
|
storeValue<Value::Undefined_Type>(TrustedImm32(0), dest);
|
|
|
|
break;
|
|
|
|
case IR::BoolType:
|
|
|
|
storeValue<Value::Boolean_Type>(TrustedImm32(c->value != 0), dest);
|
|
|
|
break;
|
2012-10-01 07:29:20 +00:00
|
|
|
case IR::NumberType:
|
|
|
|
// ### Taking address of pointer inside IR.
|
|
|
|
loadDouble(&c->value, FPGpr0);
|
|
|
|
storeDouble(FPGpr0, dest);
|
|
|
|
break;
|
2012-10-01 20:35:53 +00:00
|
|
|
default:
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
assert(!"TODO");
|
2012-10-01 07:29:20 +00:00
|
|
|
}
|
2012-10-01 20:52:05 +00:00
|
|
|
return;
|
2012-10-04 19:40:17 +00:00
|
|
|
} else if (IR::Temp *t2 = s->source->asTemp()) {
|
2012-10-09 08:05:27 +00:00
|
|
|
copyValue(t, t2);
|
2012-10-04 19:48:38 +00:00
|
|
|
return;
|
2012-10-04 20:08:22 +00:00
|
|
|
} else if (IR::String *str = s->source->asString()) {
|
2012-10-05 09:48:05 +00:00
|
|
|
generateFunctionCall(__qmljs_init_string, t, _engine->newString(*str->value));
|
2012-10-07 20:14:01 +00:00
|
|
|
return;
|
2012-10-02 06:55:18 +00:00
|
|
|
} else if (IR::Closure *clos = s->source->asClosure()) {
|
2012-10-07 17:45:40 +00:00
|
|
|
generateFunctionCall(__qmljs_init_closure, ContextRegister, t, TrustedImmPtr(clos->value));
|
2012-10-02 06:55:18 +00:00
|
|
|
return;
|
2012-10-04 20:08:22 +00:00
|
|
|
} else if (IR::New *ctor = s->source->asNew()) {
|
2012-10-04 21:23:50 +00:00
|
|
|
if (ctor->base->asName()) {
|
|
|
|
constructActivationProperty(ctor, t);
|
|
|
|
return;
|
|
|
|
} else if (ctor->base->asMember()) {
|
|
|
|
constructProperty(ctor, t);
|
|
|
|
return;
|
|
|
|
} else if (ctor->base->asTemp()) {
|
|
|
|
constructValue(ctor, t);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-04 20:08:22 +00:00
|
|
|
} else if (IR::Member *m = s->source->asMember()) {
|
2012-10-07 20:10:27 +00:00
|
|
|
//__qmljs_get_property(ctx, result, object, name);
|
|
|
|
if (IR::Temp *base = m->base->asTemp()) {
|
|
|
|
generateFunctionCall(__qmljs_get_property, ContextRegister, t, base, identifier(*m->name));
|
|
|
|
checkExceptions();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(!"wip");
|
|
|
|
return;
|
2012-10-04 20:08:22 +00:00
|
|
|
} else if (IR::Subscript *ss = s->source->asSubscript()) {
|
2012-10-07 20:10:27 +00:00
|
|
|
generateFunctionCall(__qmljs_get_element, ContextRegister, t, ss->base->asTemp(), ss->index->asTemp());
|
|
|
|
checkExceptions();
|
|
|
|
return;
|
2012-10-04 20:08:22 +00:00
|
|
|
} else if (IR::Unop *u = s->source->asUnop()) {
|
2012-10-04 20:50:01 +00:00
|
|
|
if (IR::Temp *e = u->expr->asTemp()) {
|
|
|
|
switch (u->op) {
|
|
|
|
case IR::OpIfTrue: assert(!"unreachable"); break;
|
2012-10-10 11:22:47 +00:00
|
|
|
case IR::OpNot: generateFunctionCall(__qmljs_not, ContextRegister, t, e); break;
|
|
|
|
case IR::OpUMinus: generateFunctionCall(__qmljs_uminus, ContextRegister, t, e); break;
|
|
|
|
case IR::OpUPlus: generateFunctionCall(__qmljs_uplus, ContextRegister, t, e); break;
|
|
|
|
case IR::OpCompl: generateFunctionCall(__qmljs_compl, ContextRegister, t, e); break;
|
2012-10-04 20:50:01 +00:00
|
|
|
default: assert(!"unreachable"); break;
|
|
|
|
} // switch
|
|
|
|
return;
|
|
|
|
}
|
2012-10-04 20:08:22 +00:00
|
|
|
} else if (IR::Binop *b = s->source->asBinop()) {
|
2012-10-04 20:46:42 +00:00
|
|
|
IR::Temp *l = b->left->asTemp();
|
|
|
|
IR::Temp *r = b->right->asTemp();
|
|
|
|
if (l && r) {
|
|
|
|
switch ((IR::AluOp) b->op) {
|
|
|
|
case IR::OpInvalid:
|
|
|
|
case IR::OpIfTrue:
|
|
|
|
case IR::OpNot:
|
|
|
|
case IR::OpUMinus:
|
|
|
|
case IR::OpUPlus:
|
|
|
|
case IR::OpCompl:
|
|
|
|
assert(!"unreachable");
|
|
|
|
break;
|
|
|
|
|
2012-10-10 11:22:47 +00:00
|
|
|
case IR::OpBitAnd: generateFunctionCall(__qmljs_bit_and, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpBitOr: generateFunctionCall(__qmljs_bit_or, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpBitXor: generateFunctionCall(__qmljs_bit_xor, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpAdd: generateFunctionCall(__qmljs_add, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpSub: generateFunctionCall(__qmljs_sub, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpMul: generateFunctionCall(__qmljs_mul, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpDiv: generateFunctionCall(__qmljs_div, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpMod: generateFunctionCall(__qmljs_mod, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpLShift: generateFunctionCall(__qmljs_shl, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpRShift: generateFunctionCall(__qmljs_shr, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpURShift: generateFunctionCall(__qmljs_ushr, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpGt: generateFunctionCall(__qmljs_gt, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpLt: generateFunctionCall(__qmljs_lt, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpGe: generateFunctionCall(__qmljs_ge, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpLe: generateFunctionCall(__qmljs_le, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpEqual: generateFunctionCall(__qmljs_eq, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpNotEqual: generateFunctionCall(__qmljs_ne, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpStrictEqual: generateFunctionCall(__qmljs_se, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpStrictNotEqual: generateFunctionCall(__qmljs_sne, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpInstanceof: generateFunctionCall(__qmljs_instanceof, ContextRegister, t, l, r); break;
|
|
|
|
case IR::OpIn: generateFunctionCall(__qmljs_in, ContextRegister, t, l, r); break;
|
2012-10-04 20:46:42 +00:00
|
|
|
|
|
|
|
case IR::OpAnd:
|
|
|
|
case IR::OpOr:
|
|
|
|
assert(!"unreachable");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-10-02 06:55:18 +00:00
|
|
|
} else if (IR::Call *c = s->source->asCall()) {
|
|
|
|
if (c->base->asName()) {
|
|
|
|
callActivationProperty(c, t);
|
|
|
|
return;
|
|
|
|
} else if (c->base->asMember()) {
|
|
|
|
callProperty(c, t);
|
|
|
|
return;
|
|
|
|
} else if (c->base->asTemp()) {
|
|
|
|
callValue(c, t);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-01 07:29:20 +00:00
|
|
|
}
|
2012-10-04 19:48:38 +00:00
|
|
|
} else if (IR::Member *m = s->target->asMember()) {
|
2012-10-07 20:10:27 +00:00
|
|
|
if (IR::Temp *base = m->base->asTemp()) {
|
|
|
|
if (IR::Temp *t = s->source->asTemp()) {
|
|
|
|
generateFunctionCall(__qmljs_set_property, ContextRegister, base, identifier(*m->name), t);
|
|
|
|
checkExceptions();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
2012-10-04 19:48:38 +00:00
|
|
|
} else if (IR::Subscript *ss = s->target->asSubscript()) {
|
2012-10-07 20:10:27 +00:00
|
|
|
if (IR::Temp *t2 = s->source->asTemp()) {
|
2012-10-08 19:58:24 +00:00
|
|
|
generateFunctionCall(__qmljs_set_element, ContextRegister, ss->base->asTemp(), ss->index->asTemp(), t2);
|
2012-10-07 20:10:27 +00:00
|
|
|
checkExceptions();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
}
|
2012-10-01 07:29:20 +00:00
|
|
|
}
|
2012-10-04 19:48:38 +00:00
|
|
|
} else {
|
2012-10-05 06:09:12 +00:00
|
|
|
// inplace assignment, e.g. x += 1, ++x, ...
|
|
|
|
if (IR::Temp *t = s->target->asTemp()) {
|
|
|
|
if (IR::Temp *t2 = s->source->asTemp()) {
|
|
|
|
switch (s->op) {
|
2012-10-10 11:22:47 +00:00
|
|
|
case IR::OpBitAnd: generateFunctionCall(__qmljs_bit_and, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpBitOr: generateFunctionCall(__qmljs_bit_or, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpBitXor: generateFunctionCall(__qmljs_bit_xor, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpAdd: generateFunctionCall(__qmljs_add, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpSub: generateFunctionCall(__qmljs_sub, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpMul: generateFunctionCall(__qmljs_mul, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpDiv: generateFunctionCall(__qmljs_div, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpMod: generateFunctionCall(__qmljs_mod, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpLShift: generateFunctionCall(__qmljs_shl, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpRShift: generateFunctionCall(__qmljs_shr, ContextRegister, t, t, t2); break;
|
|
|
|
case IR::OpURShift: generateFunctionCall(__qmljs_ushr, ContextRegister, t, t, t2); break;
|
2012-10-05 06:09:12 +00:00
|
|
|
default:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (IR::Name *n = s->target->asName()) {
|
|
|
|
if (IR::Temp *t = s->source->asTemp()) {
|
|
|
|
switch (s->op) {
|
2012-10-10 11:22:47 +00:00
|
|
|
case IR::OpBitAnd: generateFunctionCall(__qmljs_inplace_bit_and_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpBitOr: generateFunctionCall(__qmljs_inplace_bit_or_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpBitXor: generateFunctionCall(__qmljs_inplace_bit_xor_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpAdd: generateFunctionCall(__qmljs_inplace_add_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpSub: generateFunctionCall(__qmljs_inplace_sub_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpMul: generateFunctionCall(__qmljs_inplace_mul_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpDiv: generateFunctionCall(__qmljs_inplace_div_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpMod: generateFunctionCall(__qmljs_inplace_mod_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpLShift: generateFunctionCall(__qmljs_inplace_shl_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpRShift: generateFunctionCall(__qmljs_inplace_shr_name, ContextRegister, identifier(*n->id), t); break;
|
|
|
|
case IR::OpURShift: generateFunctionCall(__qmljs_inplace_ushr_name, ContextRegister, identifier(*n->id), t); break;
|
2012-10-05 06:09:12 +00:00
|
|
|
default:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
checkExceptions();
|
|
|
|
return;
|
|
|
|
}
|
2012-10-07 20:10:27 +00:00
|
|
|
} else if (IR::Subscript *ss = s->target->asSubscript()) {
|
|
|
|
if (IR::Temp *t = s->source->asTemp()) {
|
2012-10-10 11:22:47 +00:00
|
|
|
IR::Temp* base = ss->base->asTemp();
|
|
|
|
IR::Temp* index = ss->index->asTemp();
|
2012-10-07 20:10:27 +00:00
|
|
|
switch (s->op) {
|
2012-10-10 11:22:47 +00:00
|
|
|
case IR::OpBitAnd: generateFunctionCall(__qmljs_inplace_bit_and_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpBitOr: generateFunctionCall(__qmljs_inplace_bit_or_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpBitXor: generateFunctionCall(__qmljs_inplace_bit_xor_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpAdd: generateFunctionCall(__qmljs_inplace_add_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpSub: generateFunctionCall(__qmljs_inplace_sub_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpMul: generateFunctionCall(__qmljs_inplace_mul_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpDiv: generateFunctionCall(__qmljs_inplace_div_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpMod: generateFunctionCall(__qmljs_inplace_mod_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpLShift: generateFunctionCall(__qmljs_inplace_shl_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpRShift: generateFunctionCall(__qmljs_inplace_shr_element, ContextRegister, base, index, t); break;
|
|
|
|
case IR::OpURShift: generateFunctionCall(__qmljs_inplace_ushr_element, ContextRegister, base, index, t); break;
|
2012-10-07 20:10:27 +00:00
|
|
|
default:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-10 11:22:47 +00:00
|
|
|
checkExceptions();
|
2012-10-07 20:10:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (IR::Member *m = s->target->asMember()) {
|
|
|
|
if (IR::Temp *t = s->source->asTemp()) {
|
2012-10-10 11:22:47 +00:00
|
|
|
IR::Temp* base = m->base->asTemp();
|
|
|
|
String* member = identifier(*m->name);
|
2012-10-07 20:10:27 +00:00
|
|
|
switch (s->op) {
|
2012-10-10 11:22:47 +00:00
|
|
|
case IR::OpBitAnd: generateFunctionCall(__qmljs_inplace_bit_and_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpBitOr: generateFunctionCall(__qmljs_inplace_bit_or_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpBitXor: generateFunctionCall(__qmljs_inplace_bit_xor_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpAdd: generateFunctionCall(__qmljs_inplace_add_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpSub: generateFunctionCall(__qmljs_inplace_sub_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpMul: generateFunctionCall(__qmljs_inplace_mul_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpDiv: generateFunctionCall(__qmljs_inplace_div_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpMod: generateFunctionCall(__qmljs_inplace_mod_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpLShift: generateFunctionCall(__qmljs_inplace_shl_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpRShift: generateFunctionCall(__qmljs_inplace_shr_member, ContextRegister, base, member, t); break;
|
|
|
|
case IR::OpURShift: generateFunctionCall(__qmljs_inplace_ushr_member, ContextRegister, base, member, t); break;
|
2012-10-07 20:10:27 +00:00
|
|
|
default:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkExceptions();
|
|
|
|
return;
|
|
|
|
}
|
2012-10-05 06:09:12 +00:00
|
|
|
}
|
2012-10-01 07:29:20 +00:00
|
|
|
}
|
2012-09-23 08:28:13 +00:00
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
s->dump(qout, IR::Stmt::MIR);
|
|
|
|
qout << endl;
|
|
|
|
assert(!"TODO");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::visitJump(IR::Jump *s)
|
|
|
|
{
|
2012-10-04 17:24:22 +00:00
|
|
|
jumpToBlock(s->target);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::jumpToBlock(IR::BasicBlock *target)
|
|
|
|
{
|
|
|
|
if (_block->index + 1 != target->index)
|
|
|
|
_patches[target].append(jump());
|
2012-09-23 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::visitCJump(IR::CJump *s)
|
|
|
|
{
|
2012-10-04 17:24:22 +00:00
|
|
|
if (IR::Temp *t = s->cond->asTemp()) {
|
|
|
|
Address temp = loadTempAddress(Gpr1, t);
|
|
|
|
Address tag = temp;
|
|
|
|
tag.offset += offsetof(VM::ValueData, tag);
|
|
|
|
Jump booleanConversion = branch32(NotEqual, tag, TrustedImm32(VM::Value::Boolean_Type));
|
|
|
|
|
|
|
|
Address data = temp;
|
|
|
|
data.offset += offsetof(VM::ValueData, b);
|
|
|
|
load32(data, Gpr1);
|
|
|
|
Jump testBoolean = jump();
|
|
|
|
|
|
|
|
booleanConversion.link(this);
|
|
|
|
{
|
2012-10-05 09:48:05 +00:00
|
|
|
generateFunctionCall(__qmljs_to_boolean, ContextRegister, t);
|
2012-10-04 17:24:22 +00:00
|
|
|
move(ReturnValueRegister, Gpr1);
|
|
|
|
}
|
|
|
|
|
|
|
|
testBoolean.link(this);
|
|
|
|
move(TrustedImm32(1), Gpr0);
|
|
|
|
Jump target = branch32(Equal, Gpr1, Gpr0);
|
|
|
|
_patches[s->iftrue].append(target);
|
|
|
|
|
|
|
|
jumpToBlock(s->iffalse);
|
|
|
|
return;
|
2012-10-04 20:02:43 +00:00
|
|
|
} else if (IR::Binop *b = s->cond->asBinop()) {
|
|
|
|
IR::Temp *l = b->left->asTemp();
|
|
|
|
IR::Temp *r = b->right->asTemp();
|
|
|
|
if (l && r) {
|
|
|
|
bool (*op)(Context *, const Value *, const Value *);
|
|
|
|
switch (b->op) {
|
|
|
|
default: Q_UNREACHABLE(); assert(!"todo"); break;
|
|
|
|
case IR::OpGt: op = __qmljs_cmp_gt; break;
|
|
|
|
case IR::OpLt: op = __qmljs_cmp_lt; break;
|
|
|
|
case IR::OpGe: op = __qmljs_cmp_ge; break;
|
|
|
|
case IR::OpLe: op = __qmljs_cmp_le; break;
|
|
|
|
case IR::OpEqual: op = __qmljs_cmp_eq; break;
|
|
|
|
case IR::OpNotEqual: op = __qmljs_cmp_ne; break;
|
|
|
|
case IR::OpStrictEqual: op = __qmljs_cmp_se; break;
|
|
|
|
case IR::OpStrictNotEqual: op = __qmljs_cmp_sne; break;
|
|
|
|
case IR::OpInstanceof: op = __qmljs_cmp_instanceof; break;
|
|
|
|
case IR::OpIn: op = __qmljs_cmp_in; break;
|
|
|
|
} // switch
|
|
|
|
|
2012-10-05 09:48:05 +00:00
|
|
|
generateFunctionCall(op, ContextRegister, l, r);
|
2012-10-04 20:02:43 +00:00
|
|
|
move(ReturnValueRegister, Gpr0);
|
|
|
|
|
|
|
|
move(TrustedImm32(1), Gpr1);
|
|
|
|
Jump target = branch32(Equal, Gpr0, Gpr1);
|
|
|
|
_patches[s->iftrue].append(target);
|
|
|
|
|
|
|
|
jumpToBlock(s->iffalse);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
assert(!"wip");
|
|
|
|
}
|
|
|
|
Q_UNIMPLEMENTED();
|
2012-10-04 17:24:22 +00:00
|
|
|
}
|
2012-09-23 08:28:13 +00:00
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
assert(!"TODO");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionSelection::visitRet(IR::Ret *s)
|
|
|
|
{
|
2012-10-01 07:29:20 +00:00
|
|
|
if (IR::Temp *t = s->expr->asTemp()) {
|
2012-10-09 08:05:27 +00:00
|
|
|
copyValue(Pointer(ContextRegister, offsetof(Context, result)), t);
|
2012-10-01 07:29:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-09-23 08:28:13 +00:00
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
Q_UNUSED(s);
|
|
|
|
}
|
|
|
|
|
2012-10-07 19:43:29 +00:00
|
|
|
int InstructionSelection::prepareVariableArguments(IR::ExprList* args)
|
2012-10-04 21:23:50 +00:00
|
|
|
{
|
|
|
|
int argc = 0;
|
|
|
|
for (IR::ExprList *it = args; it; it = it->next) {
|
|
|
|
++argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (IR::ExprList *it = args; it; it = it->next, ++i) {
|
|
|
|
IR::Temp *arg = it->expr->asTemp();
|
|
|
|
assert(arg != 0);
|
2012-10-09 08:05:27 +00:00
|
|
|
copyValue(argumentAddressForCall(i), arg);
|
2012-10-04 21:23:50 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 05:55:05 +00:00
|
|
|
return argc;
|
2012-10-04 21:23:50 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 07:06:12 +00:00
|
|
|
void InstructionSelection::callRuntimeMethodImp(const char* name, ActivationMethod method, IR::Temp *result, IR::Expr *base, IR::ExprList *args)
|
2012-10-04 21:23:50 +00:00
|
|
|
{
|
|
|
|
IR::Name *baseName = base->asName();
|
|
|
|
assert(baseName != 0);
|
|
|
|
|
2012-10-05 05:55:05 +00:00
|
|
|
int argc = prepareVariableArguments(args);
|
2012-10-09 07:06:12 +00:00
|
|
|
generateFunctionCallImp(name, method, ContextRegister, result, identifier(*baseName->id), baseAddressForCallArguments(), TrustedImm32(argc));
|
2012-10-07 19:43:29 +00:00
|
|
|
checkExceptions();
|
2012-10-04 21:23:50 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 07:06:12 +00:00
|
|
|
void InstructionSelection::callRuntimeMethodImp(const char* name, BuiltinMethod method, IR::Temp *result, IR::ExprList *args)
|
2012-10-04 21:23:50 +00:00
|
|
|
{
|
2012-10-05 05:55:05 +00:00
|
|
|
int argc = prepareVariableArguments(args);
|
2012-10-09 07:06:12 +00:00
|
|
|
generateFunctionCallImp(name, method, ContextRegister, result, baseAddressForCallArguments(), TrustedImm32(argc));
|
2012-10-07 19:43:29 +00:00
|
|
|
checkExceptions();
|
2012-10-04 21:23:50 +00:00
|
|
|
}
|
2012-10-07 19:43:29 +00:00
|
|
|
|
2012-10-09 08:05:27 +00:00
|
|
|
template <typename Result, typename Source>
|
|
|
|
void InstructionSelection::copyValue(Result result, Source source)
|
|
|
|
{
|
2012-10-10 11:46:12 +00:00
|
|
|
// ### could avoid using Gpr0 in many cases
|
|
|
|
loadArgument(source, Gpr0);
|
|
|
|
loadDouble(Gpr0, FPGpr0);
|
|
|
|
loadArgument(result, Gpr0);
|
|
|
|
storeDouble(FPGpr0, Gpr0);
|
2012-10-09 08:05:27 +00:00
|
|
|
}
|