Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / eval / eval-abc.h
blob1b62a5db06ae6ef3900b69742f44c2fe713de329
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 // This file is included into eval.h
41 namespace avmplus {
42 namespace RTC {
44 /* Structures and methods for constructing and emitting ABC code. */
46 /* An interface implemented by any part of the ABC file */
48 class ABCChunk {
49 public:
50 virtual ~ABCChunk();
51 virtual uint32_t size() = 0;
52 virtual uint8_t* serialize(uint8_t* b) = 0;
54 uint32_t reported_size; // number of bytes reported by size(), checked by serialize()
57 /* ABCFile container & helper class.
59 * Every argument to an addWhatever() method is retained by
60 * reference. When getBytes() is finally called, each object is
61 * serialized. The order of serialization is the order they will
62 * have in the ABCFile, and the order among items of the same type
63 * is the order in which they were added.
65 * Performance ought to be good; nothing is serialized more than
66 * once and no data are copied except during serialization.
68 class ABCFile : public ABCChunk {
69 public:
70 ABCFile(Compiler* compiler);
72 uint32_t addInt(int32_t i);
73 uint32_t addUInt(uint32_t u);
74 uint32_t addDouble(double d);
75 uint32_t addString(Str* s);
76 uint32_t addString(const char* s);
77 uint32_t addNamespace(uint8_t kind, uint32_t ns);
78 uint32_t addNsset(Seq<uint32_t>* nss);
79 uint32_t addQName(uint32_t ns, uint32_t name, bool is_attr=false);
80 uint32_t addRTQName(uint32_t name, bool is_attr=false);
81 uint32_t addRTQNameL(bool is_attr=false);
82 uint32_t addMultiname(uint32_t nsset, uint32_t name, bool is_attr=false);
83 uint32_t addMultinameL(uint32_t nsset, bool is_attr=false);
84 uint32_t addMethod(ABCMethodInfo* m);
85 uint32_t addMetadata(ABCMetadataInfo* m);
86 uint32_t addClassAndInstance(ABCClassInfo* c, ABCInstanceInfo* i);
87 uint32_t addInstance(ABCInstanceInfo* i);
88 uint32_t addClass(ABCClassInfo* c);
89 uint32_t addScript(ABCScriptInfo* s);
90 uint32_t addMethodBody(ABCMethodBodyInfo* m);
91 bool hasRTNS(uint32_t index);
92 bool hasRTName(uint32_t index);
94 uint32_t size();
95 uint8_t* serialize(uint8_t* b);
97 private:
98 uint32_t multinameLookup(uint8_t kind, uint32_t ns_or_nsset, uint32_t name);
99 ABCMultinameInfo* getMultiname(uint32_t index);
101 const uint16_t major_version;
102 const uint16_t minor_version;
103 const int NO_VALUE;
105 Compiler* const compiler;
106 Allocator* const allocator;
108 uint32_t intCount;
109 uint32_t uintCount;
110 uint32_t doubleCount;
111 uint32_t stringCount;
112 uint32_t namespaceCount;
113 uint32_t nssetCount;
114 uint32_t multinameCount;
115 uint32_t methodCount;
116 uint32_t metadataCount;
117 uint32_t instanceCount;
118 uint32_t classCount;
119 uint32_t scriptCount;
120 uint32_t methodbodyCount;
122 ByteBuffer intBuf;
123 ByteBuffer uintBuf;
124 ByteBuffer doubleBuf;
125 ByteBuffer stringBuf;
126 ByteBuffer namespaceBuf;
127 ByteBuffer nssetBuf;
128 ByteBuffer multinameBuf;
130 SeqBuilder<ABCNamespaceInfo*> namespaces; // few enough of these that a sequentially searchable list is sufficient
131 SeqBuilder<ABCNamespaceSetInfo*> namespaceSets; // few enough of these that a sequentially searchable list is sufficient
132 SeqBuilder<ABCMultinameInfo*> multinames; // FIXME - *NOT* few enough of these that a sequential list is sufficient, but OK for now
133 SeqBuilder<ABCMethodInfo*> methods;
134 SeqBuilder<ABCMetadataInfo*> metadatas;
135 SeqBuilder<ABCInstanceInfo*> instances;
136 SeqBuilder<ABCClassInfo*> classes;
137 SeqBuilder<ABCScriptInfo*> scripts;
138 SeqBuilder<ABCMethodBodyInfo*> bodies;
141 class ABCTraitsTable : public ABCChunk {
142 public:
143 ABCTraitsTable(Compiler* compiler);
144 uint32_t addTrait(ABCTrait* t);
146 uint32_t getCount() const { return traitsCount; }
148 virtual uint32_t size();
149 virtual uint8_t* serialize(uint8_t* b);
151 protected:
152 uint32_t traitsCount;
153 SeqBuilder<ABCTrait*> traits;
156 class ABCScriptInfo : public ABCChunk {
157 public:
158 // init_method is the index of a MethodInfo
159 ABCScriptInfo(Compiler* compiler, ABCMethodInfo* init_method, ABCTraitsTable* traits);
161 virtual uint32_t size();
162 virtual uint8_t* serialize(uint8_t* b);
164 const uint32_t index; // script index in compiler->abc
165 ABCMethodInfo * const init_method;
166 ABCTraitsTable * const traits;
169 class ABCMethodInfo : public ABCChunk {
170 public:
171 ABCMethodInfo(Compiler* compiler,
172 uint32_t name,
173 uint32_t param_count,
174 Seq<uint32_t>* params,
175 uint32_t option_count,
176 Seq<DefaultValue*>* default_values,
177 uint32_t return_type);
179 virtual uint32_t size();
180 virtual uint8_t* serialize(uint8_t* b);
182 void setFlags(uint8_t flags);
184 const uint32_t index;
185 const uint32_t name;
186 const uint32_t param_count;
187 Seq<uint32_t> * const param_types;
188 const uint32_t option_count;
189 Seq<DefaultValue*> * const default_values;
190 const uint32_t return_type;
192 private:
193 uint32_t flags;
196 class ABCExceptionInfo : public ABCChunk {
197 public:
198 ABCExceptionInfo(uint32_t from, uint32_t to, uint32_t target, uint32_t exception_type, uint32_t var_name);
200 virtual uint32_t size();
201 virtual uint8_t* serialize(uint8_t* b);
203 const uint32_t from;
204 const uint32_t to;
205 const uint32_t target;
206 const uint32_t exception_type;
207 const uint32_t var_name;
210 class ABCExceptionTable : public ABCChunk {
211 public:
212 ABCExceptionTable(Compiler* compiler);
214 virtual uint32_t size();
215 virtual uint8_t* serialize(uint8_t* b);
217 uint32_t addAtEnd(ABCExceptionInfo* e);
219 private:
220 uint32_t exceptionCount;
221 SeqBuilder<ABCExceptionInfo*> exceptions;
224 class ABCMethodBodyInfo : public ABCChunk {
225 public:
226 ABCMethodBodyInfo(Compiler* compiler, ABCMethodInfo* method_info, ABCTraitsTable* traits, uint32_t first_temp);
228 virtual uint32_t size();
229 virtual uint8_t* serialize(uint8_t* b);
231 uint8_t getFlags();
232 void clearTraits(); // a hack
234 Cogen cogen;
235 ABCExceptionTable exceptions;
236 const uint32_t index;
237 ABCMethodInfo * const method_info;
238 ABCTraitsTable * traits;
241 class ABCTrait : public ABCChunk {
242 public:
243 ABCTrait(uint32_t name, uint32_t kind) : name(name), kind(kind) {}
245 virtual uint32_t size();
246 virtual uint8_t* serialize(uint8_t* b);
248 virtual uint32_t dataSize() = 0;
249 virtual uint8_t* serializeData(uint8_t* b) = 0;
251 const uint32_t name;
252 const uint32_t kind;
255 class ABCSlotTrait : public ABCTrait {
256 public:
257 ABCSlotTrait(uint32_t name, uint32_t type_name, TraitKind kind)
258 : ABCTrait(name, kind)
259 , type_name(type_name)
261 AvmAssert(kind == TRAIT_Slot || kind == TRAIT_Const);
264 uint32_t type_name;
265 virtual uint32_t dataSize();
266 virtual uint8_t* serializeData(uint8_t* b);
269 class ABCMethodTrait : public ABCTrait {
270 public:
271 ABCMethodTrait(uint32_t name, uint32_t method_info) : ABCTrait(name, TRAIT_Method), method_info(method_info) {}
273 virtual uint32_t dataSize();
274 virtual uint8_t* serializeData(uint8_t* b);
276 const uint32_t method_info;
279 class ABCMetadataInfo : public ABCChunk {
280 public:
281 virtual uint32_t size();
282 virtual uint8_t* serialize(uint8_t* b);
285 class ABCInstanceInfo : public ABCChunk {
286 public:
287 virtual uint32_t size();
288 virtual uint8_t* serialize(uint8_t* b);
291 class ABCClassInfo : public ABCChunk {
292 public:
293 virtual uint32_t size();
294 virtual uint8_t* serialize(uint8_t* b);
297 class ABCNamespaceInfo {
298 public:
299 ABCNamespaceInfo(uint8_t kind, uint32_t name) : kind(kind), name(name) {}
300 const uint8_t kind;
301 const uint32_t name;
304 class ABCNamespaceSetInfo {
305 public:
306 ABCNamespaceSetInfo(uint32_t length) : length(length) {}
307 const uint32_t length;
308 uint32_t ns[1]; // really longer than that
311 class ABCMultinameInfo {
312 public:
313 ABCMultinameInfo(uint8_t kind, uint32_t ns_or_nsset, uint32_t name) : kind(kind), ns_or_nsset(ns_or_nsset), name(name) {}
314 const uint8_t kind;
315 const uint32_t ns_or_nsset;
316 const uint32_t name;