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