1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the Qt Solutions component.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 **     of its contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 
42 #ifndef QSCRIPTASM_P_H
43 #define QSCRIPTASM_P_H
44 
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include <qglobal.h>
57 
58 
59 #include <qvector.h>
60 
61 #include "qscriptvalueimplfwd_p.h"
62 
63 QT_BEGIN_NAMESPACE
64 
65 class QTextStream;
66 
67 class QScriptInstruction
68 {
69 public:
70     enum Operator {
71 #define Q_SCRIPT_DEFINE_OPERATOR(op) OP_##op,
72 #include "instruction.table"
73 #undef Q_SCRIPT_DEFINE_OPERATOR
74         OP_Dummy
75     };
76 
77 public:
78     Operator op;
79     QScriptValueImpl operand[2];
80 #if defined(Q_SCRIPT_DIRECT_CODE)
81     void *code;
82 #endif
83 
84     void print(QTextStream &out) const;
85 
86     static const char *opcode[];
87 };
88 
89 namespace QScript {
90 
91 class NodePool;
92 
93 class ExceptionHandlerDescriptor
94 {
95 public:
ExceptionHandlerDescriptor()96     ExceptionHandlerDescriptor()
97         : m_startInstruction(0),
98           m_endInstruction(0),
99           m_handlerInstruction(0) {}
100 
ExceptionHandlerDescriptor(int startInstruction,int endInstruction,int handlerInstruction)101     ExceptionHandlerDescriptor(
102         int startInstruction,
103         int endInstruction,
104         int handlerInstruction)
105         : m_startInstruction(startInstruction),
106           m_endInstruction(endInstruction),
107           m_handlerInstruction(handlerInstruction) {}
108 
startInstruction()109     inline int startInstruction() const { return m_startInstruction; }
endInstruction()110     inline int endInstruction() const { return m_endInstruction; }
handlerInstruction()111     inline int handlerInstruction() const { return m_handlerInstruction; }
112 
113 private:
114     int m_startInstruction;
115     int m_endInstruction;
116     int m_handlerInstruction;
117 };
118 
119 class CompilationUnit
120 {
121 public:
CompilationUnit()122     CompilationUnit(): m_valid(true),
123         m_errorLineNumber(-1) {}
124 
isValid()125     bool isValid() const { return m_valid; }
126 
setError(const QString & message,int lineNumber)127     void setError(const QString &message, int lineNumber)
128     {
129         m_errorMessage = message;
130         m_errorLineNumber = lineNumber;
131         m_valid = false;
132     }
133 
errorMessage()134     QString errorMessage() const
135         { return m_errorMessage; }
errorLineNumber()136     int errorLineNumber() const
137         { return m_errorLineNumber; }
138 
instructions()139     QVector<QScriptInstruction> instructions() const
140         { return m_instructions; }
setInstructions(const QVector<QScriptInstruction> & instructions)141     void setInstructions(const QVector<QScriptInstruction> &instructions)
142         { m_instructions = instructions; }
143 
exceptionHandlers()144     QVector<ExceptionHandlerDescriptor> exceptionHandlers() const
145         { return m_exceptionHandlers; }
setExceptionHandlers(const QVector<ExceptionHandlerDescriptor> & exceptionHandlers)146     void setExceptionHandlers(const QVector<ExceptionHandlerDescriptor> &exceptionHandlers)
147         { m_exceptionHandlers = exceptionHandlers; }
148 
149 private:
150     bool m_valid;
151     QString m_errorMessage;
152     int m_errorLineNumber;
153     QVector<QScriptInstruction> m_instructions;
154     QVector<ExceptionHandlerDescriptor> m_exceptionHandlers;
155 };
156 
157 class Code
158 {
159 public:
160     Code();
161     ~Code();
162 
163     void init(const CompilationUnit &compilation, NodePool *astPool);
164 
165 public: // attributes
166     bool optimized;
167     QScriptInstruction *firstInstruction;
168     QScriptInstruction *lastInstruction;
169     QVector<ExceptionHandlerDescriptor> exceptionHandlers;
170     NodePool *astPool;
171 
172 private:
173     Q_DISABLE_COPY(Code)
174 };
175 
176 
177 } // namespace QScript
178 
179 QT_END_NAMESPACE
180 
181 #endif // QSCRIPTASM_P_H
182