summaryrefslogtreecommitdiffstats
path: root/codemaker/source/javamaker/classfile.hxx
blob: ebfef07820eed22d298d2cf89305dc4aad3ddf6f (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#pragma once

#include <codemaker/unotype.hxx>
#include <sal/types.h>

#include <map>
#include <memory>
#include <utility>
#include <vector>

class FileStream;

namespace codemaker::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::vector< 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::vector< std::unique_ptr<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 const &) = delete;
        Code& operator =(const Code&) = delete;

        explicit 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();

    std::unique_ptr<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 const &) = delete;
    ClassFile& operator =(const ClassFile&) = delete;

    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;
};

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */