LibreOffice Module bridges (master) 1
gcc3_linux_x86-64/callvirtualmethod.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <cppu/macros.hxx>
23#include <sal/types.h>
24#include <typelib/typeclass.h>
25#include <typelib/typedescription.h>
26
27#include "abi.hxx"
28#include "callvirtualmethod.hxx"
29
30// The call instruction within the asm block of callVirtualMethod may throw
31// exceptions. At least GCC 4.7.0 with -O0 would create (unnecessary)
32// .gcc_exception_table call-site table entries around all other calls in this
33// function that can throw, leading to std::terminate if the asm call throws an
34// exception and the unwinding C++ personality routine finds the unexpected hole
35// in the .gcc_exception_table. Therefore, make sure this function explicitly
36// only calls nothrow-functions (so GCC 4.7.0 with -O0 happens to not create a
37// .gcc_exception_table section at all for this function). For some reason,
38// this also needs to be in a source file of its own.
39//
40// Also, this file should be compiled with -fnon-call-exceptions, and ideally
41// there would be a way to tell the compiler that the asm block contains calls
42// to functions that can potentially throw; see the mail thread starting at
43// <http://gcc.gnu.org/ml/gcc/2012-03/msg00454.html> "C++: Letting compiler know
44// asm block can call function that can throw?"
45
47 void * pThis, sal_uInt32 nVtableIndex, void * pRegisterReturn,
48 typelib_TypeDescriptionReference * pReturnTypeRef, bool bSimpleReturn,
49 sal_uInt64 *pStack, sal_uInt32 nStack, sal_uInt64 *pGPR, double * pFPR)
50{
51 // Work around Clang -fsanitize=address "inline assembly requires more
52 // registers than available" error:
53 struct Data {
54 sal_uInt64 pMethod;
55 sal_uInt64 * pStack;
56 sal_uInt32 nStack;
57 sal_uInt64 * pGPR;
58 double * pFPR;
59 // Return values:
60 sal_uInt64 rax;
61 sal_uInt64 rdx;
62 double xmm0;
63 double xmm1;
64 } data;
65 data.pStack = pStack;
66 data.nStack = nStack;
67 data.pGPR = pGPR;
68 data.pFPR = pFPR;
69
70 // Get pointer to method
71 sal_uInt64 pMethod = *static_cast<sal_uInt64 *>(pThis);
72 pMethod += 8 * nVtableIndex;
73 data.pMethod = *reinterpret_cast<sal_uInt64 *>(pMethod);
74
75 asm volatile (
76 // Push arguments to stack
77 "movq %%rsp, %%r12\n\t"
78 "movl 16%0, %%ecx\n\t"
79 "jrcxz .Lpushed\n\t"
80 "xor %%rax, %%rax\n\t"
81 "leaq (%%rax, %%rcx, 8), %%rax\n\t"
82 "subq %%rax, %%rsp\n\t"
83 "andq $-9, %%rsp\n\t" // 16-bytes aligned
84 "movq 8%0, %%rsi\n\t"
85 "\n.Lpush:\n\t"
86 "decq %%rcx\n\t"
87 "movq (%%rsi, %%rcx, 8), %%rax\n\t"
88 "movq %%rax, (%%rsp, %%rcx, 8)\n\t"
89 "jnz .Lpush\n\t"
90 "\n.Lpushed:\n\t"
91
92 // Fill the xmm registers
93 "movq 32%0, %%rax\n\t"
94
95 "movsd (%%rax), %%xmm0\n\t"
96 "movsd 8(%%rax), %%xmm1\n\t"
97 "movsd 16(%%rax), %%xmm2\n\t"
98 "movsd 24(%%rax), %%xmm3\n\t"
99 "movsd 32(%%rax), %%xmm4\n\t"
100 "movsd 40(%%rax), %%xmm5\n\t"
101 "movsd 48(%%rax), %%xmm6\n\t"
102 "movsd 56(%%rax), %%xmm7\n\t"
103
104 // Fill the general purpose registers
105 "movq 24%0, %%rax\n\t"
106
107 "movq (%%rax), %%rdi\n\t"
108 "movq 8(%%rax), %%rsi\n\t"
109 "movq 16(%%rax), %%rdx\n\t"
110 "movq 24(%%rax), %%rcx\n\t"
111 "movq 32(%%rax), %%r8\n\t"
112 "movq 40(%%rax), %%r9\n\t"
113
114 // Perform the call
115 "movq 0%0, %%r11\n\t"
116 "call *%%r11\n\t"
117
118 // Fill the return values
119 "movq %%rax, 40%0\n\t"
120 "movq %%rdx, 48%0\n\t"
121 "movsd %%xmm0, 56%0\n\t"
122 "movsd %%xmm1, 64%0\n\t"
123
124 // Reset %rsp
125 "movq %%r12, %%rsp\n\t"
126 :: "o" (data)
127 : "rax", "rdi", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11", "r12",
128 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
129 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
130 "memory"
131 );
132
133 switch (pReturnTypeRef->eTypeClass)
134 {
135 case typelib_TypeClass_HYPER:
136 case typelib_TypeClass_UNSIGNED_HYPER:
137 *static_cast<sal_uInt64 *>( pRegisterReturn ) = data.rax;
138 break;
139 case typelib_TypeClass_LONG:
140 case typelib_TypeClass_UNSIGNED_LONG:
141 case typelib_TypeClass_ENUM:
142 *static_cast<sal_uInt32 *>( pRegisterReturn ) = *reinterpret_cast<sal_uInt32*>( &data.rax );
143 break;
144 case typelib_TypeClass_CHAR:
145 case typelib_TypeClass_SHORT:
146 case typelib_TypeClass_UNSIGNED_SHORT:
147 *static_cast<sal_uInt16 *>( pRegisterReturn ) = *reinterpret_cast<sal_uInt16*>( &data.rax );
148 break;
149 case typelib_TypeClass_BOOLEAN:
150 case typelib_TypeClass_BYTE:
151 *static_cast<sal_uInt8 *>( pRegisterReturn ) = *reinterpret_cast<sal_uInt8*>( &data.rax );
152 break;
153 case typelib_TypeClass_FLOAT:
154 case typelib_TypeClass_DOUBLE:
155 *static_cast<double *>( pRegisterReturn ) = data.xmm0;
156 break;
157 default:
158 {
159 sal_Int32 const nRetSize = pReturnTypeRef->pType->nSize;
160 if (bSimpleReturn && nRetSize <= 16 && nRetSize > 0)
161 {
162 sal_uInt64 longs[2];
163 longs[0] = data.rax;
164 longs[1] = data.rdx;
165
166 double doubles[2];
167 doubles[0] = data.xmm0;
168 doubles[1] = data.xmm1;
169 x86_64::fill_struct( pReturnTypeRef, &longs[0], &doubles[0], pRegisterReturn);
170 }
171 break;
172 }
173 }
174}
175
176/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
if(aStr !=aBuf) UpdateName_Impl(m_xFollowLb.get()
void callVirtualMethod(void *pAdjustedThisPtr, sal_Int32 nVtableIndex, void *pRegisterReturn, typelib_TypeDescription *pReturnTypeDescr, bool bSimpleReturn, sal_Int32 *pStackLongs, sal_Int32 nStackLongs)
void fill_struct(typelib_TypeDescriptionReference *pTypeRef, const sal_uInt64 *pGPR, const double *pSSE, void *pStruct) noexcept
unsigned char sal_uInt8