summaryrefslogtreecommitdiff
path: root/codemaker/source/javamaker/classfile.hxx
blob: d00dd9753a3a7bf6d14a85d45614a00ef2d16ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: classfile.hxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 02:17:17 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef INCLUDED_codemaker_source_javamaker_classfile_hxx
#define INCLUDED_codemaker_source_javamaker_classfile_hxx

#include "codemaker/unotype.hxx"

#include "sal/types.h"

#include <list>
#include <map>
#include <utility>
#include <vector>

class FileStream;
namespace rtl { class OString; }

namespace codemaker { namespace javamaker {

class ClassFile {
public:
    enum AccessFlags {
        ACC_PUBLIC = 0x0001,
        ACC_PRIVATE = 0x0002,
        ACC_STATIC = 0x0008,
        ACC_FINAL = 0x0010,
        ACC_SUPER = 0x0020,
        ACC_VARARGS = 0x0080,
        ACC_INTERFACE = 0x0200,
        ACC_ABSTRACT = 0x0400,
        ACC_SYNTHETIC = 0x1000
    };

    class Code {
    public:
        typedef std::vector< unsigned char >::size_type Branch;
        typedef std::vector< unsigned char >::size_type Position;

        ~Code();

        void instrAastore();

        void instrAconstNull();

        void instrAnewarray(rtl::OString const & type);

        void instrAreturn();

        void instrAthrow();

        void instrCheckcast(rtl::OString const & type);

        void instrDup();

        void instrGetstatic(
            rtl::OString const & type, rtl::OString const & name,
            rtl::OString const & descriptor);

        Branch instrIfAcmpne();

        Branch instrIfeq();

        Branch instrIfnull();

        void instrInstanceof(rtl::OString const & type);

        void instrInvokeinterface(
            rtl::OString const & type, rtl::OString const & name,
            rtl::OString const & descriptor, sal_uInt8 args);

        void instrInvokespecial(
            rtl::OString const & type, rtl::OString const & name,
            rtl::OString const & descriptor);

        void instrInvokestatic(
            rtl::OString const & type, rtl::OString const & name,
            rtl::OString const & descriptor);

        void instrInvokevirtual(
            rtl::OString const & type, rtl::OString const & name,
            rtl::OString const & descriptor);

        void instrLookupswitch(
            Code const * defaultBlock,
            std::list< std::pair< sal_Int32, Code * > > const & blocks);

        void instrNew(rtl::OString const & type);

        void instrNewarray(codemaker::UnoType::Sort sort);

        void instrPop();

        void instrPutfield(
            rtl::OString const & type, rtl::OString const & name,
            rtl::OString const & descriptor);

        void instrPutstatic(
            rtl::OString const & type, rtl::OString const & name,
            rtl::OString const & descriptor);

        void instrReturn();

        void instrSwap();

        void instrTableswitch(
            Code const * defaultBlock, sal_Int32 low,
            std::list< Code * > const & blocks);

        void loadIntegerConstant(sal_Int32 value);

        void loadStringConstant(rtl::OString const & value);

        void loadLocalInteger(sal_uInt16 index);

        void loadLocalLong(sal_uInt16 index);

        void loadLocalFloat(sal_uInt16 index);

        void loadLocalDouble(sal_uInt16 index);

        void loadLocalReference(sal_uInt16 index);

        void storeLocalReference(sal_uInt16 index);

        void branchHere(Branch branch);

        void addException(
            Position start, Position end, Position handler,
            rtl::OString const & type);

        void setMaxStackAndLocals(sal_uInt16 maxStack, sal_uInt16 maxLocals)
        { m_maxStack = maxStack; m_maxLocals = maxLocals; }

        Position getPosition() const;

    private:
        Code(Code &); // not implemented
        void operator =(Code); // not implemented

        Code(ClassFile & classFile);

        void ldc(sal_uInt16 index);

        void accessLocal(
            sal_uInt16 index, sal_uInt8 fastOp, sal_uInt8 normalOp);

        ClassFile & m_classFile;
        sal_uInt16 m_maxStack;
        sal_uInt16 m_maxLocals;
        std::vector< unsigned char > m_code;
        sal_uInt16 m_exceptionTableLength;
        std::vector< unsigned char > m_exceptionTable;

        friend class ClassFile;
    };

    ClassFile(
        AccessFlags accessFlags, rtl::OString const & thisClass,
        rtl::OString const & superClass, rtl::OString const & signature);

    ~ClassFile();

    Code * newCode();

    sal_uInt16 addIntegerInfo(sal_Int32 value);

    sal_uInt16 addFloatInfo(float value);

    sal_uInt16 addLongInfo(sal_Int64 value);

    sal_uInt16 addDoubleInfo(double value);

    void addInterface(rtl::OString const & interface);

    void addField(
        AccessFlags accessFlags, rtl::OString const & name,
        rtl::OString const & descriptor, sal_uInt16 constantValueIndex,
        rtl::OString const & signature);

    void addMethod(
        AccessFlags accessFlags, rtl::OString const & name,
        rtl::OString const & descriptor, Code const * code,
        std::vector< rtl::OString > const & exceptions,
        rtl::OString const & signature);

    void write(FileStream & file) const; //TODO

private:
    typedef std::map< rtl::OString, sal_uInt16 > Map;

    ClassFile(ClassFile &); // not implemented
    void operator =(ClassFile); // not implemented

    sal_uInt16 nextConstantPoolIndex(sal_uInt16 width);

    sal_uInt16 addUtf8Info(rtl::OString const & value);

    sal_uInt16 addClassInfo(rtl::OString const & type);

    sal_uInt16 addStringInfo(rtl::OString const & value);

    sal_uInt16 addFieldrefInfo(
        rtl::OString const & type, rtl::OString const & name,
        rtl::OString const & descriptor);

    sal_uInt16 addMethodrefInfo(
        rtl::OString const & type, rtl::OString const & name,
        rtl::OString const & descriptor);

    sal_uInt16 addInterfaceMethodrefInfo(
        rtl::OString const & type, rtl::OString const & name,
        rtl::OString const & descriptor);

    sal_uInt16 addNameAndTypeInfo(
        rtl::OString const & name, rtl::OString const & descriptor);

    void appendSignatureAttribute(
        std::vector< unsigned char > & stream, rtl::OString const & signature);

    sal_uInt16 m_constantPoolCount;
    std::vector< unsigned char > m_constantPool;
    std::map< rtl::OString, sal_uInt16 > m_utf8Infos;
    std::map< sal_Int32, sal_uInt16 > m_integerInfos;
    std::map< sal_Int64, sal_uInt16 > m_longInfos;
    std::map< float, sal_uInt16 > m_floatInfos;
    std::map< double, sal_uInt16 > m_doubleInfos;
    std::map< sal_uInt16, sal_uInt16 > m_classInfos;
    std::map< sal_uInt16, sal_uInt16 > m_stringInfos;
    std::map< sal_uInt32, sal_uInt16 > m_fieldrefInfos;
    std::map< sal_uInt32, sal_uInt16 > m_methodrefInfos;
    std::map< sal_uInt32, sal_uInt16 > m_interfaceMethodrefInfos;
    std::map< sal_uInt32, sal_uInt16 > m_nameAndTypeInfos;
    AccessFlags m_accessFlags;
    sal_uInt16 m_thisClass;
    sal_uInt16 m_superClass;
    sal_uInt16 m_interfacesCount;
    std::vector< unsigned char > m_interfaces;
    sal_uInt16 m_fieldsCount;
    std::vector< unsigned char > m_fields;
    sal_uInt16 m_methodsCount;
    std::vector< unsigned char > m_methods;
    sal_uInt16 m_attributesCount;
    std::vector< unsigned char > m_attributes;

    friend class Code;
};

} }

#endif