summaryrefslogtreecommitdiff
path: root/src/qml/compiler/qv4isel_p.cpp
blob: 4ed321b00627daea9b9fcfcd186e894b376123aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore/QDebug>
#include <QtCore/QBuffer>
#include "qv4jsir_p.h"
#include "qv4isel_p.h"
#include "qv4isel_util_p.h"
#include <private/qv4value_p.h>
#ifndef V4_BOOTSTRAP
#include <private/qqmlpropertycache_p.h>
#endif

#include <QString>

using namespace QV4;
using namespace QV4::IR;

EvalInstructionSelection::EvalInstructionSelection(QV4::ExecutableAllocator *execAllocator, Module *module, QV4::Compiler::JSUnitGenerator *jsGenerator)
    : useFastLookups(true)
    , useTypeInference(true)
    , executableAllocator(execAllocator)
    , irModule(module)
{
    if (!jsGenerator) {
        jsGenerator = new QV4::Compiler::JSUnitGenerator(module);
        ownJSGenerator.reset(jsGenerator);
    }
    this->jsGenerator = jsGenerator;
#ifndef V4_BOOTSTRAP
    Q_ASSERT(execAllocator);
#endif
    Q_ASSERT(module);
}

EvalInstructionSelection::~EvalInstructionSelection()
{}

EvalISelFactory::~EvalISelFactory()
{}

QQmlRefPointer<CompiledData::CompilationUnit> EvalInstructionSelection::compile(bool generateUnitData)
{
    for (int i = 0; i < irModule->functions.size(); ++i)
        run(i);

    QQmlRefPointer<QV4::CompiledData::CompilationUnit> unit = backendCompileStep();
    if (generateUnitData)
        unit->data = jsGenerator->generateUnit();
    return unit;
}

void IRDecoder::visitMove(IR::Move *s)
{
    if (IR::Name *n = s->target->asName()) {
        if (s->source->asTemp() || s->source->asConst() || s->source->asArgLocal()) {
            setActivationProperty(s->source, *n->id);
            return;
        }
    } else if (s->target->asTemp() || s->target->asArgLocal()) {
        if (IR::Name *n = s->source->asName()) {
            if (n->id && *n->id == QStringLiteral("this")) // TODO: `this' should be a builtin.
                loadThisObject(s->target);
            else if (n->builtin == IR::Name::builtin_qml_context)
                loadQmlContext(s->target);
            else if (n->builtin == IR::Name::builtin_qml_id_array)
                loadQmlIdArray(s->target);
            else if (n->builtin == IR::Name::builtin_qml_imported_scripts_object)
                loadQmlImportedScripts(s->target);
            else if (n->qmlSingleton)
                loadQmlSingleton(*n->id, s->target);
            else
                getActivationProperty(n, s->target);
            return;
        } else if (IR::Const *c = s->source->asConst()) {
            loadConst(c, s->target);
            return;
        } else if (s->source->asTemp() || s->source->asArgLocal()) {
            if (s->swap)
                swapValues(s->source, s->target);
            else
                copyValue(s->source, s->target);
            return;
        } else if (IR::String *str = s->source->asString()) {
            loadString(*str->value, s->target);
            return;
        } else if (IR::RegExp *re = s->source->asRegExp()) {
            loadRegexp(re, s->target);
            return;
        } else if (IR::Closure *clos = s->source->asClosure()) {
            initClosure(clos, s->target);
            return;
        } else if (IR::New *ctor = s->source->asNew()) {
            if (Name *func = ctor->base->asName()) {
                constructActivationProperty(func, ctor->args, s->target);
                return;
            } else if (IR::Member *member = ctor->base->asMember()) {
                constructProperty(member->base, *member->name, ctor->args, s->target);
                return;
            } else if (ctor->base->asTemp() || ctor->base->asArgLocal()) {
                constructValue(ctor->base, ctor->args, s->target);
                return;
            }
        } else if (IR::Member *m = s->source->asMember()) {
            if (m->property) {
#ifdef V4_BOOTSTRAP
                Q_UNIMPLEMENTED();
#else
                bool captureRequired = true;

                Q_ASSERT(m->kind != IR::Member::MemberOfEnum);
                const int attachedPropertiesId = m->attachedPropertiesIdOrEnumValue;
                const bool isSingletonProperty = m->kind == IR::Member::MemberOfSingletonObject;

                if (_function && attachedPropertiesId == 0 && !m->property->isConstant()) {
                    if (m->kind == IR::Member::MemberOfQmlContextObject) {
                        _function->contextObjectPropertyDependencies.insert(m->property->coreIndex, m->property->notifyIndex);
                        captureRequired = false;
                    } else if (m->kind == IR::Member::MemberOfQmlScopeObject) {
                        _function->scopeObjectPropertyDependencies.insert(m->property->coreIndex, m->property->notifyIndex);
                        captureRequired = false;
                    }
                }
                if (m->kind == IR::Member::MemberOfQmlScopeObject || m->kind == IR::Member::MemberOfQmlContextObject) {
                    getQmlContextProperty(m->base, (IR::Member::MemberKind)m->kind, m->property->coreIndex, s->target);
                    return;
                }
                getQObjectProperty(m->base, m->property->coreIndex, captureRequired, isSingletonProperty, attachedPropertiesId, s->target);
#endif // V4_BOOTSTRAP
                return;
            } else if (m->base->asTemp() || m->base->asConst() || m->base->asArgLocal()) {
                getProperty(m->base, *m->name, s->target);
                return;
            }
        } else if (IR::Subscript *ss = s->source->asSubscript()) {
            getElement(ss->base, ss->index, s->target);
            return;
        } else if (IR::Unop *u = s->source->asUnop()) {
            unop(u->op, u->expr, s->target);
            return;
        } else if (IR::Binop *b = s->source->asBinop()) {
            binop(b->op, b->left, b->right, s->target);
            return;
        } else if (IR::Call *c = s->source->asCall()) {
            if (c->base->asName()) {
                callBuiltin(c, s->target);
                return;
            } else if (Member *member = c->base->asMember()) {
#ifndef V4_BOOTSTRAP
                if (member->kind == IR::Member::MemberOfQmlScopeObject || member->kind == IR::Member::MemberOfQmlContextObject) {
                    callQmlContextProperty(member->base, (IR::Member::MemberKind)member->kind, member->property->coreIndex, c->args, s->target);
                    return;
                }
#endif
                callProperty(member->base, *member->name, c->args, s->target);
                return;
            } else if (Subscript *ss = c->base->asSubscript()) {
                callSubscript(ss->base, ss->index, c->args, s->target);
                return;
            } else if (c->base->asTemp() || c->base->asArgLocal() || c->base->asConst()) {
                callValue(c->base, c->args, s->target);
                return;
            }
        } else if (IR::Convert *c = s->source->asConvert()) {
            Q_ASSERT(c->expr->asTemp() || c->expr->asArgLocal());
            convertType(c->expr, s->target);
            return;
        }
    } else if (IR::Member *m = s->target->asMember()) {
        if (m->base->asTemp() || m->base->asConst() || m->base->asArgLocal()) {
            if (s->source->asTemp() || s->source->asConst() || s->source->asArgLocal()) {
                Q_ASSERT(m->kind != IR::Member::MemberOfEnum);
                const int attachedPropertiesId = m->attachedPropertiesIdOrEnumValue;
                if (m->property && attachedPropertiesId == 0) {
#ifdef V4_BOOTSTRAP
                    Q_UNIMPLEMENTED();
#else
                    if (m->kind == IR::Member::MemberOfQmlScopeObject || m->kind == IR::Member::MemberOfQmlContextObject) {
                        setQmlContextProperty(s->source, m->base, (IR::Member::MemberKind)m->kind, m->property->coreIndex);
                        return;
                    }
                    setQObjectProperty(s->source, m->base, m->property->coreIndex);
#endif
                    return;
                } else {
                    setProperty(s->source, m->base, *m->name);
                    return;
                }
            }
        }
    } else if (IR::Subscript *ss = s->target->asSubscript()) {
        if (s->source->asTemp() || s->source->asConst() || s->source->asArgLocal()) {
            setElement(s->source, ss->base, ss->index);
            return;
        }
    }

    // For anything else...:
    Q_UNIMPLEMENTED();
    QBuffer buf;
    buf.open(QIODevice::WriteOnly);
    QTextStream qout(&buf);
    IRPrinter(&qout).print(s);
    qout << endl;
    qDebug("%s", buf.data().constData());
    Q_ASSERT(!"TODO");
}

IRDecoder::~IRDecoder()
{
}

void IRDecoder::visitExp(IR::Exp *s)
{
    if (IR::Call *c = s->expr->asCall()) {
        // These are calls where the result is ignored.
        if (c->base->asName()) {
            callBuiltin(c, 0);
        } else if (c->base->asTemp() || c->base->asArgLocal() || c->base->asConst()) {
            callValue(c->base, c->args, 0);
        } else if (Member *member = c->base->asMember()) {
            Q_ASSERT(member->base->asTemp() || member->base->asArgLocal());
#ifndef V4_BOOTSTRAP
            if (member->kind == IR::Member::MemberOfQmlScopeObject || member->kind == IR::Member::MemberOfQmlContextObject) {
                callQmlContextProperty(member->base, (IR::Member::MemberKind)member->kind, member->property->coreIndex, c->args, 0);
                return;
            }
#endif
            callProperty(member->base, *member->name, c->args, 0);
        } else if (Subscript *s = c->base->asSubscript()) {
            callSubscript(s->base, s->index, c->args, 0);
        } else {
            Q_UNREACHABLE();
        }
    } else {
        Q_UNREACHABLE();
    }
}

void IRDecoder::callBuiltin(IR::Call *call, Expr *result)
{
    IR::Name *baseName = call->base->asName();
    Q_ASSERT(baseName != 0);

    switch (baseName->builtin) {
    case IR::Name::builtin_invalid:
        callBuiltinInvalid(baseName, call->args, result);
        return;

    case IR::Name::builtin_typeof: {
        if (IR::Member *m = call->args->expr->asMember()) {
            callBuiltinTypeofMember(m->base, *m->name, result);
            return;
        } else if (IR::Subscript *ss = call->args->expr->asSubscript()) {
            callBuiltinTypeofSubscript(ss->base, ss->index, result);
            return;
        } else if (IR::Name *n = call->args->expr->asName()) {
            callBuiltinTypeofName(*n->id, result);
            return;
        } else if (call->args->expr->asTemp() ||
                   call->args->expr->asConst() ||
                   call->args->expr->asArgLocal()) {
            callBuiltinTypeofValue(call->args->expr, result);
            return;
        }
    } break;

    case IR::Name::builtin_delete: {
        if (IR::Member *m = call->args->expr->asMember()) {
            callBuiltinDeleteMember(m->base, *m->name, result);
            return;
        } else if (IR::Subscript *ss = call->args->expr->asSubscript()) {
            callBuiltinDeleteSubscript(ss->base, ss->index, result);
            return;
        } else if (IR::Name *n = call->args->expr->asName()) {
            callBuiltinDeleteName(*n->id, result);
            return;
        } else if (call->args->expr->asTemp() ||
                   call->args->expr->asArgLocal()) {
            // TODO: should throw in strict mode
            callBuiltinDeleteValue(result);
            return;
        }
    } break;

    case IR::Name::builtin_throw: {
        IR::Expr *arg = call->args->expr;
        Q_ASSERT(arg->asTemp() || arg->asConst() || arg->asArgLocal());
        callBuiltinThrow(arg);
    } return;

    case IR::Name::builtin_rethrow: {
        callBuiltinReThrow();
    } return;

    case IR::Name::builtin_unwind_exception: {
        callBuiltinUnwindException(result);
    } return;

    case IR::Name::builtin_push_catch_scope: {
        IR::String *s = call->args->expr->asString();
        Q_ASSERT(s);
        callBuiltinPushCatchScope(*s->value);
    } return;

    case IR::Name::builtin_foreach_iterator_object: {
        IR::Expr *arg = call->args->expr;
        Q_ASSERT(arg != 0);
        callBuiltinForeachIteratorObject(arg, result);
    } return;

    case IR::Name::builtin_foreach_next_property_name: {
        IR::Expr *arg = call->args->expr;
        Q_ASSERT(arg != 0);
        callBuiltinForeachNextPropertyname(arg, result);
    } return;
    case IR::Name::builtin_push_with_scope: {
        if (call->args->expr->asTemp() || call->args->expr->asArgLocal())
            callBuiltinPushWithScope(call->args->expr);
        else
            Q_UNIMPLEMENTED();
    } return;

    case IR::Name::builtin_pop_scope:
        callBuiltinPopScope();
        return;

    case IR::Name::builtin_declare_vars: {
        if (!call->args)
            return;
        IR::Const *deletable = call->args->expr->asConst();
        Q_ASSERT(deletable->type == IR::BoolType);
        for (IR::ExprList *it = call->args->next; it; it = it->next) {
            IR::Name *arg = it->expr->asName();
            Q_ASSERT(arg != 0);
            callBuiltinDeclareVar(deletable->value != 0, *arg->id);
        }
    } return;

    case IR::Name::builtin_define_array:
        callBuiltinDefineArray(result, call->args);
        return;

    case IR::Name::builtin_define_object_literal: {
        IR::ExprList *args = call->args;
        const int keyValuePairsCount = args->expr->asConst()->value;
        args = args->next;

        IR::ExprList *keyValuePairs = args;
        for (int i = 0; i < keyValuePairsCount; ++i) {
            args = args->next; // name
            bool isData = args->expr->asConst()->value;
            args = args->next; // isData flag
            args = args->next; // value or getter
            if (!isData)
                args = args->next; // setter
        }

        IR::ExprList *arrayEntries = args;
        bool needSparseArray = false;
        for (IR::ExprList *it = arrayEntries; it; it = it->next) {
            uint index = it->expr->asConst()->value;
            if (index > 16)  {
                needSparseArray = true;
                break;
            }
            it = it->next;
            bool isData = it->expr->asConst()->value;
            it = it->next;
            if (!isData)
                it = it->next;
        }

        callBuiltinDefineObjectLiteral(result, keyValuePairsCount, keyValuePairs, arrayEntries, needSparseArray);
    } return;

    case IR::Name::builtin_setup_argument_object:
        callBuiltinSetupArgumentObject(result);
        return;

    case IR::Name::builtin_convert_this_to_object:
        callBuiltinConvertThisToObject();
        return;

    default:
        break;
    }

    Q_UNIMPLEMENTED();
    QBuffer buf;
    buf.open(QIODevice::WriteOnly);
    QTextStream qout(&buf);
    IRPrinter(&qout).print(call); qout << endl;
    qDebug("%s", buf.data().constData());
    Q_UNREACHABLE();
}