aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/v4/qv4ir_p.h
blob: 701f76d9e43dc637491e912ed9a25728005b1f74 (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QV4IR_P_H
#define QV4IR_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <private/qqmljsast_p.h>
#include <private/qqmljsengine_p.h>
#include <private/qqmlscript_p.h>
#include <private/qqmlimport_p.h>
#include <private/qqmlengine_p.h>
#include <private/qv4compiler_p.h>

#include <private/qqmlpool_p.h>
#include <QtCore/qvarlengtharray.h>

// #define DEBUG_IR_STRUCTURE

#ifdef CONST
#  undef CONST
#endif

QT_BEGIN_NAMESPACE

class QTextStream;
class QQmlType;

namespace QQmlJS {

namespace IR {

struct BasicBlock;
struct Function;

struct Stmt;
struct Expr;

// expressions
struct Const;
struct String;
struct Name;
struct Temp;
struct Unop;
struct Binop;
struct Call;

// statements
struct Exp;
struct Move;
struct Jump;
struct CJump;
struct Ret;

enum AluOp {
    OpInvalid = 0,

    OpIfTrue,
    OpNot,
    OpUMinus,
    OpUPlus,
    OpCompl,

    OpBitAnd,
    OpBitOr,
    OpBitXor,

    OpAdd,
    OpSub,
    OpMul,
    OpDiv,
    OpMod,

    OpLShift,
    OpRShift,
    OpURShift,

    OpGt,
    OpLt,
    OpGe,
    OpLe,
    OpEqual,
    OpNotEqual,
    OpStrictEqual,
    OpStrictNotEqual,

    OpAnd,
    OpOr
};
AluOp binaryOperator(int op);

enum Type {
    InvalidType,
    UndefinedType,
    NullType,
    VoidType,
    StringType,
    UrlType,
    ColorType,
    SGAnchorLineType,
    AttachType,
    ObjectType,
    VariantType,
    VarType,
    JSValueType,

    FirstNumberType,
    BoolType = FirstNumberType,
    IntType,
    FloatType,
    NumberType
};
Type maxType(IR::Type left, IR::Type right);
bool isRealType(IR::Type type);
const char *typeName(IR::Type t);

struct ExprVisitor {
    virtual ~ExprVisitor() {}
    virtual void visitConst(Const *) {}
    virtual void visitString(String *) {}
    virtual void visitName(Name *) {}
    virtual void visitTemp(Temp *) {}
    virtual void visitUnop(Unop *) {}
    virtual void visitBinop(Binop *) {}
    virtual void visitCall(Call *) {}
};

struct StmtVisitor {
    virtual ~StmtVisitor() {}
    virtual void visitExp(Exp *) {}
    virtual void visitMove(Move *) {}
    virtual void visitJump(Jump *) {}
    virtual void visitCJump(CJump *) {}
    virtual void visitRet(Ret *) {}
};

struct Expr: QQmlPool::POD {
    Type type;

    Expr(): type(InvalidType) {}
    virtual ~Expr() {}
    virtual void accept(ExprVisitor *) = 0;
    virtual Const *asConst() { return 0; }
    virtual String *asString() { return 0; }
    virtual Name *asName() { return 0; }
    virtual Temp *asTemp() { return 0; }
    virtual Unop *asUnop() { return 0; }
    virtual Binop *asBinop() { return 0; }
    virtual Call *asCall() { return 0; }
    virtual void dump(QTextStream &out) = 0;
};

struct ExprList: QQmlPool::POD {
    Expr *expr;
    ExprList *next;

    void init(Expr *expr, ExprList *next = 0)
    {
        this->expr = expr;
        this->next = next;
    }
};

struct Const: Expr {
    double value;

    void init(Type type, double value)
    {
        this->type = type;
        this->value = value;
    }

    virtual void accept(ExprVisitor *v) { v->visitConst(this); }
    virtual Const *asConst() { return this; }

    virtual void dump(QTextStream &out);
};

struct String: Expr {
    QStringRef value;

    void init(const QStringRef &value)
    {
        this->type = StringType;
        this->value = value;
    }

    virtual void accept(ExprVisitor *v) { v->visitString(this); }
    virtual String *asString() { return this; }

    virtual void dump(QTextStream &out);
    static QString escape(const QStringRef &s);
};

enum BuiltinSymbol {
    NoBuiltinSymbol,
    MathSinBultinFunction,
    MathCosBultinFunction,
    MathRoundBultinFunction,
    MathFloorBultinFunction,
    MathCeilBuiltinFunction,
    MathAbsBuiltinFunction,
    MathMaxBuiltinFunction,
    MathMinBuiltinFunction,

    MathPIBuiltinConstant
};

struct Name: Expr {
    enum Symbol {
        Unbound,
        IdObject,        // This is a load of a id object.  Storage will always be IdStorage
        AttachType,      // This is a load of an attached object
        SingletonObject, // This is a load of a singleton object
        Object,          // XXX what is this for?
        Property,        // This is a load of a regular property
        Slot             // XXX what is this for?
    };

    enum Storage {
        MemberStorage, // This is a property of a previously fetched object
        IdStorage,     // This is a load of a id object.  Symbol will always be IdObject
        RootStorage,   // This is a property of the root object
        ScopeStorage   // This is a property of the scope object
    };

    Name *base;
    const QString *id;
    Symbol symbol;
    union {
        void *ptr;
        const QQmlType *declarativeType;
        const QQmlScript::Object *idObject;
    };

    QQmlMetaObject meta;
    QQmlPropertyData *property;
    Storage storage;
    BuiltinSymbol builtin;
    quint16 line;
    quint16 column;

    void init(Name *base, Type type, const QString *id, Symbol symbol, quint16 line, quint16 column);

    inline bool is(Symbol s) const { return s == symbol; }
    inline bool isNot(Symbol s) const { return s != symbol; }

    virtual void accept(ExprVisitor *v) { v->visitName(this); }
    virtual Name *asName() { return this; }

    virtual void dump(QTextStream &out);
};

struct Temp: Expr {
    int index;

    void init(Type type, int index)
    {
        this->type = type;
        this->index = index;
    }

    virtual void accept(ExprVisitor *v) { v->visitTemp(this); }
    virtual Temp *asTemp() { return this; }

    virtual void dump(QTextStream &out);
};

struct Unop: Expr {
    AluOp op;
    Expr *expr;

    void init(AluOp op, Expr *expr)
    {
        this->type = this->typeForOp(op, expr);
        this->op = op;
        this->expr = expr;
    }

    virtual void accept(ExprVisitor *v) { v->visitUnop(this); }
    virtual Unop *asUnop() { return this; }

    virtual void dump(QTextStream &out);

private:
    static Type typeForOp(AluOp op, Expr *expr);
};

struct Binop: Expr {
    AluOp op;
    Expr *left;
    Expr *right;

    void init(AluOp op, Expr *left, Expr *right)
    {
        this->type = typeForOp(op, left, right);
        this->op = op;
        this->left = left;
        this->right = right;
    }

    virtual void accept(ExprVisitor *v) { v->visitBinop(this); }
    virtual Binop *asBinop() { return this; }

    virtual void dump(QTextStream &out);

    static Type typeForOp(AluOp op, Expr *left, Expr *right);
};

struct Call: Expr {
    Expr *base;
    ExprList *args;

    void init(Expr *base, ExprList *args)
    {
        this->type = typeForFunction(base);
        this->base = base;
        this->args = args;
    }

    Expr *onlyArgument() const {
        if (args && ! args->next)
            return args->expr;
        return 0;
    }

    virtual void accept(ExprVisitor *v) { v->visitCall(this); }
    virtual Call *asCall() { return this; }

    virtual void dump(QTextStream &out);

private:
    static Type typeForFunction(Expr *base);
};

struct Stmt: QQmlPool::POD {
    enum Mode {
        HIR,
        MIR
    };

    virtual ~Stmt() {}
    virtual Stmt *asTerminator() { return 0; }

    virtual void accept(StmtVisitor *) = 0;
    virtual Exp *asExp() { return 0; }
    virtual Move *asMove() { return 0; }
    virtual Jump *asJump() { return 0; }
    virtual CJump *asCJump() { return 0; }
    virtual Ret *asRet() { return 0; }
    virtual void dump(QTextStream &out, Mode mode = HIR) = 0;
};

struct Exp: Stmt {
    Expr *expr;

    void init(Expr *expr)
    {
        this->expr = expr;
    }

    virtual void accept(StmtVisitor *v) { v->visitExp(this); }
    virtual Exp *asExp() { return this; }

    virtual void dump(QTextStream &out, Mode);
};

struct Move: Stmt {
    Expr *target;
    Expr *source;
    bool isMoveForReturn;

    void init(Expr *target, Expr *source, bool isMoveForReturn)
    {
        this->target = target;
        this->source = source;
        this->isMoveForReturn = isMoveForReturn;
    }

    virtual void accept(StmtVisitor *v) { v->visitMove(this); }
    virtual Move *asMove() { return this; }

    virtual void dump(QTextStream &out, Mode);
};

struct Jump: Stmt {
    BasicBlock *target;

    void init(BasicBlock *target)
    {
        this->target = target;
    }

    virtual Stmt *asTerminator() { return this; }

    virtual void accept(StmtVisitor *v) { v->visitJump(this); }
    virtual Jump *asJump() { return this; }

    virtual void dump(QTextStream &out, Mode mode);
};

struct CJump: Stmt {
    Expr *cond;
    BasicBlock *iftrue;
    BasicBlock *iffalse;

    void init(Expr *cond, BasicBlock *iftrue, BasicBlock *iffalse)
    {
        this->cond = cond;
        this->iftrue = iftrue;
        this->iffalse = iffalse;
    }

    virtual Stmt *asTerminator() { return this; }

    virtual void accept(StmtVisitor *v) { v->visitCJump(this); }
    virtual CJump *asCJump() { return this; }

    virtual void dump(QTextStream &out, Mode mode);
};

struct Ret: Stmt {
    Expr *expr;
    Type type;
    quint16 line;
    quint16 column;

    void init(Expr *expr, Type type, quint16 line, quint16 column)
    {
        this->expr = expr;
        this->type = type;
        this->line = line;
        this->column = column;
    }

    virtual Stmt *asTerminator() { return this; }

    virtual void accept(StmtVisitor *v) { v->visitRet(this); }
    virtual Ret *asRet() { return this; }

    virtual void dump(QTextStream &out, Mode);
};

struct Function {
    QQmlPool *pool;
    QVarLengthArray<BasicBlock *, 8> basicBlocks;
    int tempCount;

    Function(QQmlPool *pool)
      : pool(pool), tempCount(0) {}

    virtual ~Function();

    BasicBlock *newBasicBlock();
    QString *newString(const QString &text);

    inline BasicBlock *i(BasicBlock *block) { basicBlocks.append(block); return block; }

    virtual void dump(QTextStream &out);
};

struct BasicBlock {
    Function *function;
    int index;
    int offset;
    QVarLengthArray<Stmt *, 32> statements;

    BasicBlock(Function *function, int index): function(function), index(index), offset(-1) {}
    ~BasicBlock() {}

    template <typename Instr> inline Instr i(Instr i) { statements.append(i); return i; }

    inline bool isEmpty() const {
        return statements.isEmpty();
    }

    inline Stmt *terminator() const {
        if (! statements.isEmpty() && statements.at(statements.size() - 1)->asTerminator() != 0)
            return statements.at(statements.size() - 1);
        return 0;
    }

    inline bool isTerminated() const {
        if (terminator() != 0)
            return true;
        return false;
    }

    Temp *TEMP(Type type, int index);
    Temp *TEMP(Type type);

    Expr *CONST(Type type, double value);
    Expr *STRING(const QStringRef &value);

    Name *NAME(const QString &id, quint16 line, quint16 column);
    Name *NAME(Name *base, const QString &id, quint16 line, quint16 column);
    Name *SYMBOL(Type type, const QString &id, const QQmlMetaObject &meta, QQmlPropertyData *property, Name::Storage storage, quint16 line, quint16 column);
    Name *SYMBOL(Name *base, Type type, const QString &id, const QQmlMetaObject &meta, QQmlPropertyData *property, quint16 line, quint16 column);
    Name *SYMBOL(Name *base, Type type, const QString &id, const QQmlMetaObject &meta, QQmlPropertyData *property, Name::Storage storage, quint16 line, quint16 column);
    Name *ID_OBJECT(const QString &id, const QQmlScript::Object *object, quint16 line, quint16 column);
    Name *ATTACH_TYPE(const QString &id, const QQmlType *attachType, Name::Storage storage, quint16 line, quint16 column);
    Name *SINGLETON_OBJECT(const QString &id, const QQmlMetaObject &meta, Name::Storage storage, quint16 line, quint16 column);

    Expr *UNOP(AluOp op, Expr *expr);
    Expr *BINOP(AluOp op, Expr *left, Expr *right);
    Expr *CALL(Expr *base, ExprList *args);

    Stmt *EXP(Expr *expr);
    Stmt *MOVE(Expr *target, Expr *source, bool = false);

    Stmt *JUMP(BasicBlock *target);
    Stmt *CJUMP(Expr *cond, BasicBlock *iftrue, BasicBlock *iffalse);
    Stmt *RET(Expr *expr, Type type, quint16 line, quint16 column);

    void dump(QTextStream &out);
};

#ifdef DEBUG_IR_STRUCTURE
struct IRDump : public ExprVisitor,
                public StmtVisitor 
{
public:
    IRDump();

    void expression(QQmlJS::IR::Expr *);
    void basicblock(QQmlJS::IR::BasicBlock *);
    void statement(QQmlJS::IR::Stmt *);
    void function(QQmlJS::IR::Function *);
protected:

    const char *indent();

    //
    // expressions
    //
    virtual void visitConst(QQmlJS::IR::Const *e);
    virtual void visitString(QQmlJS::IR::String *e);
    virtual void visitName(QQmlJS::IR::Name *e);
    virtual void visitTemp(QQmlJS::IR::Temp *e);
    virtual void visitUnop(QQmlJS::IR::Unop *e);
    virtual void visitBinop(QQmlJS::IR::Binop *e);
    virtual void visitCall(QQmlJS::IR::Call *e);

    //
    // statements
    //
    virtual void visitExp(QQmlJS::IR::Exp *s);
    virtual void visitMove(QQmlJS::IR::Move *s);
    virtual void visitJump(QQmlJS::IR::Jump *s);
    virtual void visitCJump(QQmlJS::IR::CJump *s);
    virtual void visitRet(QQmlJS::IR::Ret *s);

private:
    int indentSize;
    QByteArray indentData;
    void inc();
    void dec();
};
#endif

} // end of namespace IR

} // end of namespace QQmlJS

QT_END_NAMESPACE

#endif // QV4IR_P_H