File: abi.cxx

package info (click to toggle)
libreoffice 4%3A24.2.5-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 3,900,068 kB
  • sloc: cpp: 4,280,526; xml: 465,341; java: 272,372; python: 77,014; ansic: 33,848; perl: 30,415; javascript: 18,244; sh: 11,573; makefile: 9,830; yacc: 8,549; cs: 6,703; objc: 1,969; lex: 1,379; asm: 1,220; awk: 987; pascal: 940; csh: 20; sed: 5
file content (304 lines) | stat: -rw-r--r-- 10,791 bytes parent folder | download
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
 */

#include <com/sun/star/uno/genfunc.hxx>
#include <uno/data.h>
#include <typelib/typedescription.hxx>
#include "types.hxx"
#include "abi.hxx"
#include <stdio.h>
#include <cstring>

//#define BRIDGE_DEBUG

namespace abi_riscv64
{
/*
   F: floating point reg
   G: general purpose reg
*/
enum class ReturnKind
{
    FF_Align4,
    FF_Align8,
    FG_Align4,
    FG_Align8,
    GF_Align4,
    GF_Align8,
    DEFAULT
};

void countnGreg(sal_Int32& nGreg, sal_Int32& nFreg, bool& firstIsGreg, sal_Int32& align,
                const typelib_CompoundTypeDescription* pTypeDescr)
{
    for (int i = 0; i < pTypeDescr->nMembers; i++)
    {
        typelib_TypeDescriptionReference* pTypeInStruct = pTypeDescr->ppTypeRefs[i];
        switch (pTypeInStruct->eTypeClass)
        {
            case typelib_TypeClass_STRUCT:
            case typelib_TypeClass_EXCEPTION:
            {
                typelib_TypeDescription* childTypeDescr = nullptr;
                TYPELIB_DANGER_GET(&childTypeDescr, pTypeInStruct);
                countnGreg(
                    nGreg, nFreg, firstIsGreg, align,
                    reinterpret_cast<typelib_CompoundTypeDescription const*>(childTypeDescr));
                TYPELIB_DANGER_RELEASE(childTypeDescr);
            }
            break;
            case typelib_TypeClass_DOUBLE:
                // Align to the larger type
                align = 8;
                [[fallthrough]];
            case typelib_TypeClass_FLOAT:
                nFreg++;
                break;
            case typelib_TypeClass_HYPER:
            case typelib_TypeClass_UNSIGNED_HYPER:
                align = 8;
                [[fallthrough]];
            default:
                if (nFreg > 0)
                {
                    firstIsGreg = false;
                }
                nGreg++;
                break;
        }
    }
}

ReturnKind getReturnKind(const typelib_TypeDescription* pTypeDescr)
{
    sal_Int32 nGreg = 0;
    sal_Int32 nFreg = 0;
    sal_Int32 align = 4;
    bool firstIsGreg = true;
    countnGreg(nGreg, nFreg, firstIsGreg, align,
               reinterpret_cast<typelib_CompoundTypeDescription const*>(pTypeDescr));
    if (nGreg == 0 && nFreg == 2)
    {
        if (align == 4)
            return ReturnKind::FF_Align4;
        else
            return ReturnKind::FF_Align8;
    }
    else if (nGreg == 1 && nFreg == 1)
    {
        if (firstIsGreg)
        {
            if (align == 4)
                return ReturnKind::GF_Align4;
            else
                return ReturnKind::GF_Align8;
        }
        else
        {
            if (align == 4)
                return ReturnKind::FG_Align4;
            else
                return ReturnKind::FG_Align8;
        }
    }
    else
    {
        return ReturnKind::DEFAULT;
    }
}

/*
    Transform the returned cpp data to uno.
    This happens at the end of uno2cpp, when callee cpp func finished.

                  |    returned data saved in
    default cases |     gret[0] and gret[1]
    2 float       |     fret[0] and fret[1]
    1 float 1 int |     gret[0] and fret[0]

    There is a complex problem -- alignment. For example, 4 byte float and 8 byte
    integer take 16 bytes rather than 12 bytes.

    There is also another complex problem. e.g. Two 4 byte integer is compacted
    in a0, but two 4 byte float is seperately set in fa0 and fa1. However, return
    size is 8 bytes. We need to cut the lower 32bit of fa0 and fa1 seperately and
    combine them in 8 bytes.
*/
void fillUNOStruct(const typelib_TypeDescription* pTypeDescr, sal_Int64* gret, double* fret,
                   void* pRegisterReturn)
{
#ifdef BRIDGE_DEBUG
    printf("In fillStruct, pTypeDescr = %p, gret = %p, fret = %p, pRegisterReturn = %p\n",
           pTypeDescr, gret, fret, pRegisterReturn);
#endif
    ReturnKind returnKind = getReturnKind(pTypeDescr);
    switch (returnKind)
    {
        case ReturnKind::FF_Align4:
            memcpy(reinterpret_cast<char*>(pRegisterReturn), &(fret[0]), 4);
            memcpy(reinterpret_cast<char*>(pRegisterReturn) + 4, &(fret[1]), 4);
            break;
        case ReturnKind::FF_Align8:
            reinterpret_cast<double*>(pRegisterReturn)[0] = fret[0];
            reinterpret_cast<double*>(pRegisterReturn)[1] = fret[1];
            break;
        case ReturnKind::FG_Align4:
            memcpy(reinterpret_cast<char*>(pRegisterReturn), &(fret[0]), 4);
            memcpy(reinterpret_cast<char*>(pRegisterReturn) + 4, &(gret[0]), 4);
            break;
        case ReturnKind::FG_Align8:
            reinterpret_cast<double*>(pRegisterReturn)[0] = fret[0];
            reinterpret_cast<sal_Int64*>(pRegisterReturn)[1] = gret[0];
            break;
        case ReturnKind::GF_Align4:
            memcpy(reinterpret_cast<char*>(pRegisterReturn), &(gret[0]), 4);
            memcpy(reinterpret_cast<char*>(pRegisterReturn) + 4, &(fret[0]), 4);
            break;
        case ReturnKind::GF_Align8:
            reinterpret_cast<sal_Int64*>(pRegisterReturn)[0] = gret[0];
            reinterpret_cast<double*>(pRegisterReturn)[1] = fret[0];
            break;
        default:
            reinterpret_cast<sal_Int64*>(pRegisterReturn)[0] = gret[0];
            reinterpret_cast<sal_Int64*>(pRegisterReturn)[1] = gret[1];
            break;
    }
}

/*
    Split zipped unoreturn to cpp func. This happens at the end of cpp2uno.

    The data in pTarget will be extrat to return regs in privateSnippetExecutor:
                  |  pTarget[0]  |  pTarget[1]  | return type |
    default cases |     $a0      |     $a1      |      0      |
    2 float       |     $fa0     |     $fa1     |      0      |
    1 float 1 int |     $a0      |     $fa0     |      1      |

    This looks like a reverse version of fillUNOStruct. The reason for such
    "meaningless" effort is that java return a compact struct, but cpp not.
*/
void splitUNOStruct(const typelib_TypeDescription* pTypeDescr, sal_uInt64* pTarget,
                    sal_uInt64* pSource, sal_Int32& returnType)
{
#ifdef BRIDGE_DEBUG
    printf("In splitUNOStruct, pTypeDescr = %p, pTarget = %p, pSource = %p\n", pTypeDescr, pTarget,
           pSource);
#endif
    sal_uInt64* pTemp = (sal_uInt64*)calloc(2, sizeof(sal_uInt64));
    ReturnKind returnKind = getReturnKind(pTypeDescr);
    switch (returnKind)
    {
        case ReturnKind::FF_Align4:
            memcpy(reinterpret_cast<char*>(pTemp), reinterpret_cast<char*>(pSource), 4);
            memset(reinterpret_cast<char*>(pTemp) + 4, 0xFF, 4);
            memcpy(reinterpret_cast<char*>(pTemp) + 8, reinterpret_cast<char*>(pSource) + 4, 4);
            memset(reinterpret_cast<char*>(pTemp) + 12, 0xFF, 4);
            returnType = 0;
            break;
        case ReturnKind::FF_Align8:
            pTemp[0] = pSource[0];
            pTemp[1] = pSource[1];
            returnType = 0;
            break;
        case ReturnKind::FG_Align4:
            memcpy(reinterpret_cast<char*>(pTemp), reinterpret_cast<char*>(pSource) + 4, 4);
            memcpy(reinterpret_cast<char*>(pTemp) + 8, reinterpret_cast<char*>(pSource), 4);
            memset(reinterpret_cast<char*>(pTemp) + 12, 0xFF, 4);
            returnType = 1;
            break;
        case ReturnKind::FG_Align8:
            pTemp[0] = pSource[1];
            pTemp[1] = pSource[0];
            returnType = 1;
            break;
        case ReturnKind::GF_Align4:
            memcpy(reinterpret_cast<char*>(pTemp), reinterpret_cast<char*>(pSource), 4);
            memcpy(reinterpret_cast<char*>(pTemp) + 8, reinterpret_cast<char*>(pSource) + 4, 4);
            memset(reinterpret_cast<char*>(pTemp) + 12, 0xFF, 4);
            returnType = 1;
            break;
        case ReturnKind::GF_Align8:
            pTemp[0] = pSource[0];
            pTemp[1] = pSource[1];
            returnType = 1;
            break;
        default:
            pTemp[0] = pSource[0];
            pTemp[1] = pSource[1];
            returnType = 0;
            break;
    }
    pTarget[0] = pTemp[0];
    pTarget[1] = pTemp[1];
    free(pTemp);
}

/*
    Extend higher bits for integer types.

    According to
    https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#integer-calling-convention

    > When passed in registers or on the stack, integer scalars narrower than XLEN bits
    > are widened according to the sign of their type up to 32 bits, then sign-extended
    > to XLEN bits.
*/
void extIntBits(sal_uInt64* outData, const sal_uInt64* inData, bool isSigned, sal_uInt32 dataBytes)
{
    if (dataBytes > 8)
    {
        //SAL_WARN("bridges", "illegal dataBytes in dataBytes, please check the bridge.");
        return;
    }

    sal_uInt64 data = *inData;
    char* dataPointer = reinterpret_cast<char*>(&data);

    // Clear bits which are not data
    sal_uInt64* dataMask = (sal_uInt64*)calloc(1, 8);
    memset(dataMask, 0xFF, dataBytes);
    data = data & *dataMask;
    free(dataMask);

    // extend to 32 bit
    if (dataBytes < 4)
    {
        if (isSigned)
        {
            // Detect the highest bit of the data.
            // For example, if a one-byte integer data passed in, we need to detect the 8th bit(8 x 1)
            // So left shift 1 three-times(8-1) we can get mask 1000 0000
            sal_uInt64 detectMask = 1 << (dataBytes * 8 - 1);

            if (detectMask & data)
                // Is negative
                memset(dataPointer + dataBytes, 0xFF, 4 - dataBytes);
            else
                // Is positive
                memset(dataPointer + dataBytes, 0x0, 4 - dataBytes);
        }
        else
            memset(dataPointer + dataBytes, 0x0, 4 - dataBytes);

        // The highest data bit turns into 8 * 4 = 32 bit
        dataBytes = 4;
    }

    // Sign extend to 64 bit
    sal_uInt64 detectMask = 1 << (dataBytes * 8 - 1);
    if (detectMask & data)
        memset(dataPointer + dataBytes, 0xFF, 8 - dataBytes);
    else
        memset(dataPointer + dataBytes, 0x00, 8 - dataBytes);

    *outData = data;
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */