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