1*2037b4deSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2037b4deSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2037b4deSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2037b4deSAndrew Rist  * distributed with this work for additional information
6*2037b4deSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2037b4deSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2037b4deSAndrew Rist  * "License"); you may not use this file except in compliance
9*2037b4deSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2037b4deSAndrew Rist  *
11*2037b4deSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2037b4deSAndrew Rist  *
13*2037b4deSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2037b4deSAndrew Rist  * software distributed under the License is distributed on an
15*2037b4deSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2037b4deSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2037b4deSAndrew Rist  * specific language governing permissions and limitations
18*2037b4deSAndrew Rist  * under the License.
19*2037b4deSAndrew Rist  *
20*2037b4deSAndrew Rist  *************************************************************/
21*2037b4deSAndrew Rist 
22*2037b4deSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_CODEMAKER_SOURCE_JAVAMAKER_CLASSFILE_HXX
25cdf0e10cSrcweir #define INCLUDED_CODEMAKER_SOURCE_JAVAMAKER_CLASSFILE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "codemaker/unotype.hxx"
28cdf0e10cSrcweir #include "sal/types.h"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <list>
31cdf0e10cSrcweir #include <map>
32cdf0e10cSrcweir #include <utility>
33cdf0e10cSrcweir #include <vector>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir class FileStream;
36cdf0e10cSrcweir namespace rtl { class OString; }
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace codemaker { namespace javamaker {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir class ClassFile {
41cdf0e10cSrcweir public:
42cdf0e10cSrcweir     enum AccessFlags {
43cdf0e10cSrcweir         ACC_PUBLIC = 0x0001,
44cdf0e10cSrcweir         ACC_PRIVATE = 0x0002,
45cdf0e10cSrcweir         ACC_STATIC = 0x0008,
46cdf0e10cSrcweir         ACC_FINAL = 0x0010,
47cdf0e10cSrcweir         ACC_SUPER = 0x0020,
48cdf0e10cSrcweir         ACC_VARARGS = 0x0080,
49cdf0e10cSrcweir         ACC_INTERFACE = 0x0200,
50cdf0e10cSrcweir         ACC_ABSTRACT = 0x0400,
51cdf0e10cSrcweir         ACC_SYNTHETIC = 0x1000
52cdf0e10cSrcweir     };
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     class Code {
55cdf0e10cSrcweir     public:
56cdf0e10cSrcweir         typedef std::vector< unsigned char >::size_type Branch;
57cdf0e10cSrcweir         typedef std::vector< unsigned char >::size_type Position;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir         ~Code();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir         void instrAastore();
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         void instrAconstNull();
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         void instrAnewarray(rtl::OString const & type);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         void instrAreturn();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         void instrAthrow();
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         void instrCheckcast(rtl::OString const & type);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         void instrDup();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         void instrGetstatic(
76cdf0e10cSrcweir             rtl::OString const & type, rtl::OString const & name,
77cdf0e10cSrcweir             rtl::OString const & descriptor);
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         Branch instrIfAcmpne();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         Branch instrIfeq();
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         Branch instrIfnull();
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         void instrInstanceof(rtl::OString const & type);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         void instrInvokeinterface(
88cdf0e10cSrcweir             rtl::OString const & type, rtl::OString const & name,
89cdf0e10cSrcweir             rtl::OString const & descriptor, sal_uInt8 args);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         void instrInvokespecial(
92cdf0e10cSrcweir             rtl::OString const & type, rtl::OString const & name,
93cdf0e10cSrcweir             rtl::OString const & descriptor);
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         void instrInvokestatic(
96cdf0e10cSrcweir             rtl::OString const & type, rtl::OString const & name,
97cdf0e10cSrcweir             rtl::OString const & descriptor);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         void instrInvokevirtual(
100cdf0e10cSrcweir             rtl::OString const & type, rtl::OString const & name,
101cdf0e10cSrcweir             rtl::OString const & descriptor);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         void instrLookupswitch(
104cdf0e10cSrcweir             Code const * defaultBlock,
105cdf0e10cSrcweir             std::list< std::pair< sal_Int32, Code * > > const & blocks);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         void instrNew(rtl::OString const & type);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         void instrNewarray(codemaker::UnoType::Sort sort);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         void instrPop();
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         void instrPutfield(
114cdf0e10cSrcweir             rtl::OString const & type, rtl::OString const & name,
115cdf0e10cSrcweir             rtl::OString const & descriptor);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         void instrPutstatic(
118cdf0e10cSrcweir             rtl::OString const & type, rtl::OString const & name,
119cdf0e10cSrcweir             rtl::OString const & descriptor);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         void instrReturn();
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         void instrSwap();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         void instrTableswitch(
126cdf0e10cSrcweir             Code const * defaultBlock, sal_Int32 low,
127cdf0e10cSrcweir             std::list< Code * > const & blocks);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         void loadIntegerConstant(sal_Int32 value);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         void loadStringConstant(rtl::OString const & value);
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         void loadLocalInteger(sal_uInt16 index);
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         void loadLocalLong(sal_uInt16 index);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         void loadLocalFloat(sal_uInt16 index);
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         void loadLocalDouble(sal_uInt16 index);
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         void loadLocalReference(sal_uInt16 index);
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         void storeLocalReference(sal_uInt16 index);
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         void branchHere(Branch branch);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         void addException(
148cdf0e10cSrcweir             Position start, Position end, Position handler,
149cdf0e10cSrcweir             rtl::OString const & type);
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         void setMaxStackAndLocals(sal_uInt16 maxStack, sal_uInt16 maxLocals)
152cdf0e10cSrcweir         { m_maxStack = maxStack; m_maxLocals = maxLocals; }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         Position getPosition() const;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     private:
157cdf0e10cSrcweir         Code(Code &); // not implemented
158cdf0e10cSrcweir         void operator =(Code); // not implemented
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         Code(ClassFile & classFile);
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         void ldc(sal_uInt16 index);
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         void accessLocal(
165cdf0e10cSrcweir             sal_uInt16 index, sal_uInt8 fastOp, sal_uInt8 normalOp);
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         ClassFile & m_classFile;
168cdf0e10cSrcweir         sal_uInt16 m_maxStack;
169cdf0e10cSrcweir         sal_uInt16 m_maxLocals;
170cdf0e10cSrcweir         std::vector< unsigned char > m_code;
171cdf0e10cSrcweir         sal_uInt16 m_exceptionTableLength;
172cdf0e10cSrcweir         std::vector< unsigned char > m_exceptionTable;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         friend class ClassFile;
175cdf0e10cSrcweir     };
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     ClassFile(
178cdf0e10cSrcweir         AccessFlags accessFlags, rtl::OString const & thisClass,
179cdf0e10cSrcweir         rtl::OString const & superClass, rtl::OString const & signature);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     ~ClassFile();
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     Code * newCode();
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     sal_uInt16 addIntegerInfo(sal_Int32 value);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     sal_uInt16 addFloatInfo(float value);
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     sal_uInt16 addLongInfo(sal_Int64 value);
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     sal_uInt16 addDoubleInfo(double value);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     void addInterface(rtl::OString const & interface);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     void addField(
196cdf0e10cSrcweir         AccessFlags accessFlags, rtl::OString const & name,
197cdf0e10cSrcweir         rtl::OString const & descriptor, sal_uInt16 constantValueIndex,
198cdf0e10cSrcweir         rtl::OString const & signature);
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     void addMethod(
201cdf0e10cSrcweir         AccessFlags accessFlags, rtl::OString const & name,
202cdf0e10cSrcweir         rtl::OString const & descriptor, Code const * code,
203cdf0e10cSrcweir         std::vector< rtl::OString > const & exceptions,
204cdf0e10cSrcweir         rtl::OString const & signature);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     void write(FileStream & file) const; //TODO
207cdf0e10cSrcweir 
208cdf0e10cSrcweir private:
209cdf0e10cSrcweir     typedef std::map< rtl::OString, sal_uInt16 > Map;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     ClassFile(ClassFile &); // not implemented
212cdf0e10cSrcweir     void operator =(ClassFile); // not implemented
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     sal_uInt16 nextConstantPoolIndex(sal_uInt16 width);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     sal_uInt16 addUtf8Info(rtl::OString const & value);
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     sal_uInt16 addClassInfo(rtl::OString const & type);
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     sal_uInt16 addStringInfo(rtl::OString const & value);
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     sal_uInt16 addFieldrefInfo(
223cdf0e10cSrcweir         rtl::OString const & type, rtl::OString const & name,
224cdf0e10cSrcweir         rtl::OString const & descriptor);
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     sal_uInt16 addMethodrefInfo(
227cdf0e10cSrcweir         rtl::OString const & type, rtl::OString const & name,
228cdf0e10cSrcweir         rtl::OString const & descriptor);
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     sal_uInt16 addInterfaceMethodrefInfo(
231cdf0e10cSrcweir         rtl::OString const & type, rtl::OString const & name,
232cdf0e10cSrcweir         rtl::OString const & descriptor);
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     sal_uInt16 addNameAndTypeInfo(
235cdf0e10cSrcweir         rtl::OString const & name, rtl::OString const & descriptor);
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     void appendSignatureAttribute(
238cdf0e10cSrcweir         std::vector< unsigned char > & stream, rtl::OString const & signature);
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     sal_uInt16 m_constantPoolCount;
241cdf0e10cSrcweir     std::vector< unsigned char > m_constantPool;
242cdf0e10cSrcweir     std::map< rtl::OString, sal_uInt16 > m_utf8Infos;
243cdf0e10cSrcweir     std::map< sal_Int32, sal_uInt16 > m_integerInfos;
244cdf0e10cSrcweir     std::map< sal_Int64, sal_uInt16 > m_longInfos;
245cdf0e10cSrcweir     std::map< float, sal_uInt16 > m_floatInfos;
246cdf0e10cSrcweir     std::map< double, sal_uInt16 > m_doubleInfos;
247cdf0e10cSrcweir     std::map< sal_uInt16, sal_uInt16 > m_classInfos;
248cdf0e10cSrcweir     std::map< sal_uInt16, sal_uInt16 > m_stringInfos;
249cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 > m_fieldrefInfos;
250cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 > m_methodrefInfos;
251cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 > m_interfaceMethodrefInfos;
252cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 > m_nameAndTypeInfos;
253cdf0e10cSrcweir     AccessFlags m_accessFlags;
254cdf0e10cSrcweir     sal_uInt16 m_thisClass;
255cdf0e10cSrcweir     sal_uInt16 m_superClass;
256cdf0e10cSrcweir     sal_uInt16 m_interfacesCount;
257cdf0e10cSrcweir     std::vector< unsigned char > m_interfaces;
258cdf0e10cSrcweir     sal_uInt16 m_fieldsCount;
259cdf0e10cSrcweir     std::vector< unsigned char > m_fields;
260cdf0e10cSrcweir     sal_uInt16 m_methodsCount;
261cdf0e10cSrcweir     std::vector< unsigned char > m_methods;
262cdf0e10cSrcweir     sal_uInt16 m_attributesCount;
263cdf0e10cSrcweir     std::vector< unsigned char > m_attributes;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     friend class Code;
266cdf0e10cSrcweir };
267cdf0e10cSrcweir 
268cdf0e10cSrcweir } }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir #endif // INCLUDED_CODEMAKER_SOURCE_JAVAMAKER_CLASSFILE_HXX
271