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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/* -*- 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 .
*/
#include <basic/sberrors.hxx>
#include <basic/sbx.hxx>
#include <basic/sbmeth.hxx>
#include <basic/sbmod.hxx>
#include <image.hxx>
#include <codegen.hxx>
#include <parser.hxx>
#include <sbintern.hxx>
#include <cstddef>
#include <limits>
#include <algorithm>
#include <string_view>
#include <osl/diagnose.h>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/script/ModuleType.hpp>
// nInc is the increment size of the buffers
SbiCodeGen::SbiCodeGen( SbModule& r, SbiParser* p, short nInc )
: rMod( r ), aCode( p, nInc )
{
pParser = p;
bStmnt = false;
nLine = 0;
nCol = 0;
nForLevel = 0;
}
sal_uInt32 SbiCodeGen::GetPC() const
{
return aCode.GetSize();
}
// memorize the statement
void SbiCodeGen::Statement()
{
if( pParser->IsCodeCompleting() )
return;
bStmnt = true;
nLine = pParser->GetLine();
nCol = pParser->GetCol1();
// #29955 Store the information of the for-loop-layer
// in the upper Byte of the column
nCol = (nCol & 0xff) + 0x100 * nForLevel;
}
// Mark the beginning of a statement
void SbiCodeGen::GenStmnt()
{
if( pParser->IsCodeCompleting() )
return;
if( bStmnt )
{
bStmnt = false;
Gen( SbiOpcode::STMNT_, nLine, nCol );
}
}
// The Gen-Routines return the offset of the 1. operand,
// so that jumps can sink their backchain there.
sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode )
{
if( pParser->IsCodeCompleting() )
return 0;
#ifdef DBG_UTIL
if( eOpcode < SbiOpcode::SbOP0_START || eOpcode > SbiOpcode::SbOP0_END )
pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, "OPCODE1" );
#endif
GenStmnt();
aCode += static_cast<sal_uInt8>(eOpcode);
return GetPC();
}
sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode, sal_uInt32 nOpnd )
{
if( pParser->IsCodeCompleting() )
return 0;
#ifdef DBG_UTIL
if( eOpcode < SbiOpcode::SbOP1_START || eOpcode > SbiOpcode::SbOP1_END )
pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, "OPCODE2" );
#endif
GenStmnt();
aCode += static_cast<sal_uInt8>(eOpcode);
sal_uInt32 n = GetPC();
aCode += nOpnd;
return n;
}
sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode, sal_uInt32 nOpnd1, sal_uInt32 nOpnd2 )
{
if( pParser->IsCodeCompleting() )
return 0;
#ifdef DBG_UTIL
if( eOpcode < SbiOpcode::SbOP2_START || eOpcode > SbiOpcode::SbOP2_END )
pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, "OPCODE3" );
#endif
GenStmnt();
aCode += static_cast<sal_uInt8>(eOpcode);
sal_uInt32 n = GetPC();
aCode += nOpnd1;
aCode += nOpnd2;
return n;
}
// Storing of the created image in the module
void SbiCodeGen::Save()
{
if( pParser->IsCodeCompleting() )
return;
SbiImage* p = new SbiImage;
rMod.StartDefinitions();
// OPTION BASE-Value:
p->nDimBase = pParser->nBase;
// OPTION take over the EXPLICIT-Flag
if( pParser->bExplicit )
p->SetFlag( SbiImageFlags::EXPLICIT );
int nIfaceCount = 0;
if( rMod.mnType == css::script::ModuleType::CLASS )
{
rMod.bIsProxyModule = true;
p->SetFlag( SbiImageFlags::CLASSMODULE );
GetSbData()->pClassFac->AddClassModule( &rMod );
nIfaceCount = pParser->aIfaceVector.size();
if( !rMod.pClassData )
rMod.pClassData.reset(new SbClassData);
if( nIfaceCount )
{
for( int i = 0 ; i < nIfaceCount ; i++ )
{
const OUString& rIfaceName = pParser->aIfaceVector[i];
SbxVariable* pIfaceVar = new SbxVariable( SbxVARIANT );
pIfaceVar->SetName( rIfaceName );
SbxArray* pIfaces = rMod.pClassData->mxIfaces.get();
pIfaces->Insert32( pIfaceVar, pIfaces->Count32() );
}
}
rMod.pClassData->maRequiredTypes = pParser->aRequiredTypes;
}
else
{
GetSbData()->pClassFac->RemoveClassModule( &rMod );
// Only a ClassModule can revert to Normal
if ( rMod.mnType == css::script::ModuleType::CLASS )
{
rMod.mnType = css::script::ModuleType::NORMAL;
}
rMod.bIsProxyModule = false;
}
// GlobalCode-Flag
if( pParser->HasGlobalCode() )
{
p->SetFlag( SbiImageFlags::INITCODE );
}
// The entry points:
for( SbiSymDef* pDef = pParser->aPublics.First(); pDef;
pDef = pParser->aPublics.Next() )
{
SbiProcDef* pProc = pDef->GetProcDef();
if( pProc && pProc->IsDefined() )
{
OUString aProcName = pProc->GetName();
OUStringBuffer aIfaceProcName;
OUString aIfaceName;
sal_uInt16 nPassCount = 1;
if( nIfaceCount )
{
int nPropPrefixFound = aProcName.indexOf("Property ");
OUString aPureProcName = aProcName;
OUString aPropPrefix;
if( nPropPrefixFound == 0 )
{
aPropPrefix = aProcName.copy( 0, 13 ); // 13 == Len( "Property ?et " )
aPureProcName = aProcName.copy( 13 );
}
for( int i = 0 ; i < nIfaceCount ; i++ )
{
const OUString& rIfaceName = pParser->aIfaceVector[i];
int nFound = aPureProcName.indexOf( rIfaceName );
if( nFound == 0 && aPureProcName[rIfaceName.getLength()] == '_' )
{
if( nPropPrefixFound == 0 )
{
aIfaceProcName.append(aPropPrefix);
}
aIfaceProcName.append(std::u16string_view(aPureProcName).substr(rIfaceName.getLength() + 1) );
aIfaceName = rIfaceName;
nPassCount = 2;
break;
}
}
}
SbMethod* pMeth = nullptr;
for( sal_uInt16 nPass = 0 ; nPass < nPassCount ; nPass++ )
{
if( nPass == 1 )
{
aProcName = aIfaceProcName.toString();
}
PropertyMode ePropMode = pProc->getPropertyMode();
if( ePropMode != PropertyMode::NONE )
{
SbxDataType ePropType = SbxEMPTY;
switch( ePropMode )
{
case PropertyMode::Get:
ePropType = pProc->GetType();
break;
case PropertyMode::Let:
{
// type == type of first parameter
ePropType = SbxVARIANT; // Default
SbiSymPool* pPool = &pProc->GetParams();
if( pPool->GetSize() > 1 )
{
SbiSymDef* pPar = pPool->Get( 1 );
if( pPar )
{
ePropType = pPar->GetType();
}
}
break;
}
case PropertyMode::Set:
ePropType = SbxOBJECT;
break;
default:
OSL_FAIL("Illegal PropertyMode");
break;
}
OUString aPropName = pProc->GetPropName();
if( nPass == 1 )
{
aPropName = aPropName.copy( aIfaceName.getLength() + 1 );
}
rMod.GetProcedureProperty( aPropName, ePropType );
}
if( nPass == 1 )
{
rMod.GetIfaceMapperMethod( aProcName, pMeth );
}
else
{
pMeth = rMod.GetMethod( aProcName, pProc->GetType() );
if( !pProc->IsPublic() )
{
pMeth->SetFlag( SbxFlagBits::Private );
}
// Declare? -> Hidden
if( !pProc->GetLib().isEmpty())
{
pMeth->SetFlag( SbxFlagBits::Hidden );
}
pMeth->nStart = pProc->GetAddr();
pMeth->nLine1 = pProc->GetLine1();
pMeth->nLine2 = pProc->GetLine2();
// The parameter:
SbxInfo* pInfo = pMeth->GetInfo();
OUString aHelpFile, aComment;
sal_uInt32 nHelpId = 0;
if( pInfo )
{
// Rescue the additional data
aHelpFile = pInfo->GetHelpFile();
aComment = pInfo->GetComment();
nHelpId = pInfo->GetHelpId();
}
// And reestablish the parameter list
pInfo = new SbxInfo( aHelpFile, nHelpId );
pInfo->SetComment( aComment );
SbiSymPool* pPool = &pProc->GetParams();
// The first element is always the value of the function!
for( sal_uInt16 i = 1; i < pPool->GetSize(); i++ )
{
SbiSymDef* pPar = pPool->Get( i );
SbxDataType t = pPar->GetType();
if( !pPar->IsByVal() )
{
t = static_cast<SbxDataType>( t | SbxBYREF );
}
if( pPar->GetDims() )
{
t = static_cast<SbxDataType>( t | SbxARRAY );
}
// #33677 hand-over an Optional-Info
SbxFlagBits nFlags = SbxFlagBits::Read;
if( pPar->IsOptional() )
{
nFlags |= SbxFlagBits::Optional;
}
pInfo->AddParam( pPar->GetName(), t, nFlags );
sal_uInt32 nUserData = 0;
sal_uInt16 nDefaultId = pPar->GetDefaultId();
if( nDefaultId )
{
nUserData |= nDefaultId;
}
if( pPar->IsParamArray() )
{
nUserData |= PARAM_INFO_PARAMARRAY;
}
if( pPar->IsWithBrackets() )
{
nUserData |= PARAM_INFO_WITHBRACKETS;
}
SbxParamInfo* pParam = nullptr;
if( nUserData )
{
pParam = const_cast<SbxParamInfo*>(pInfo->GetParam( i ));
}
if( pParam )
{
pParam->nUserData = nUserData;
}
}
pMeth->SetInfo( pInfo );
}
} // for( iPass...
}
}
// The code
p->AddCode( std::unique_ptr<char[]>(aCode.GetBuffer()), aCode.GetSize() );
// The global StringPool. 0 is not occupied.
SbiStringPool* pPool = &pParser->aGblStrings;
sal_uInt16 nSize = pPool->GetSize();
p->MakeStrings( nSize );
sal_uInt32 i;
for( i = 1; i <= nSize; i++ )
{
p->AddString( pPool->Find( i ) );
}
// Insert types
sal_uInt32 nCount = pParser->rTypeArray->Count32();
for (i = 0; i < nCount; i++)
{
p->AddType(static_cast<SbxObject *>(pParser->rTypeArray->Get32(i)));
}
// Insert enum objects
nCount = pParser->rEnumArray->Count32();
for (i = 0; i < nCount; i++)
{
p->AddEnum(static_cast<SbxObject *>(pParser->rEnumArray->Get32(i)));
}
if( !p->IsError() )
{
rMod.pImage = p;
}
else
{
delete p;
}
rMod.EndDefinitions();
}
namespace {
template < class T >
class PCodeVisitor
{
public:
virtual ~PCodeVisitor();
virtual void start( const sal_uInt8* pStart ) = 0;
virtual void processOpCode0( SbiOpcode eOp ) = 0;
virtual void processOpCode1( SbiOpcode eOp, T nOp1 ) = 0;
virtual void processOpCode2( SbiOpcode eOp, T nOp1, T nOp2 ) = 0;
virtual bool processParams() = 0;
};
}
template <class T> PCodeVisitor< T >::~PCodeVisitor()
{}
namespace {
template <class T>
class PCodeBufferWalker
{
private:
T m_nBytes;
const sal_uInt8* m_pCode;
static T readParam( sal_uInt8 const *& pCode )
{
T nOp1=0;
for ( std::size_t i=0; i<sizeof( T ); ++i )
nOp1 |= *pCode++ << ( i * 8);
return nOp1;
}
public:
PCodeBufferWalker( const sal_uInt8* pCode, T nBytes ): m_nBytes( nBytes ), m_pCode( pCode )
{
}
void visitBuffer( PCodeVisitor< T >& visitor )
{
const sal_uInt8* pCode = m_pCode;
if ( !pCode )
return;
const sal_uInt8* pEnd = pCode + m_nBytes;
visitor.start( m_pCode );
T nOp1 = 0, nOp2 = 0;
for( ; pCode < pEnd; )
{
SbiOpcode eOp = static_cast<SbiOpcode>(*pCode++);
if ( eOp <= SbiOpcode::SbOP0_END )
visitor.processOpCode0( eOp );
else if( eOp >= SbiOpcode::SbOP1_START && eOp <= SbiOpcode::SbOP1_END )
{
if ( visitor.processParams() )
nOp1 = readParam( pCode );
else
pCode += sizeof( T );
visitor.processOpCode1( eOp, nOp1 );
}
else if( eOp >= SbiOpcode::SbOP2_START && eOp <= SbiOpcode::SbOP2_END )
{
if ( visitor.processParams() )
{
nOp1 = readParam( pCode );
nOp2 = readParam( pCode );
}
else
pCode += ( sizeof( T ) * 2 );
visitor.processOpCode2( eOp, nOp1, nOp2 );
}
}
}
};
template < class T, class S >
class OffSetAccumulator : public PCodeVisitor< T >
{
T m_nNumOp0;
T m_nNumSingleParams;
T m_nNumDoubleParams;
public:
OffSetAccumulator() : m_nNumOp0(0), m_nNumSingleParams(0), m_nNumDoubleParams(0){}
virtual void start( const sal_uInt8* /*pStart*/ ) override {}
virtual void processOpCode0( SbiOpcode /*eOp*/ ) override { ++m_nNumOp0; }
virtual void processOpCode1( SbiOpcode /*eOp*/, T /*nOp1*/ ) override { ++m_nNumSingleParams; }
virtual void processOpCode2( SbiOpcode /*eOp*/, T /*nOp1*/, T /*nOp2*/ ) override { ++m_nNumDoubleParams; }
S offset()
{
typedef decltype(T(1) + S(1)) larger_t; // type capable to hold both value ranges of T and S
T result = 0 ;
static const S max = std::numeric_limits< S >::max();
result = m_nNumOp0 + ( ( sizeof(S) + 1 ) * m_nNumSingleParams ) + ( (( sizeof(S) * 2 )+ 1 ) * m_nNumDoubleParams );
return std::min<larger_t>(max, result);
}
virtual bool processParams() override { return false; }
};
template < class T, class S >
class BufferTransformer : public PCodeVisitor< T >
{
const sal_uInt8* m_pStart;
SbiBuffer m_ConvertedBuf;
public:
BufferTransformer():m_pStart(nullptr), m_ConvertedBuf( nullptr, 1024 ) {}
virtual void start( const sal_uInt8* pStart ) override { m_pStart = pStart; }
virtual void processOpCode0( SbiOpcode eOp ) override
{
m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
}
virtual void processOpCode1( SbiOpcode eOp, T nOp1 ) override
{
m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
switch( eOp )
{
case SbiOpcode::JUMP_:
case SbiOpcode::JUMPT_:
case SbiOpcode::JUMPF_:
case SbiOpcode::GOSUB_:
case SbiOpcode::CASEIS_:
case SbiOpcode::RETURN_:
case SbiOpcode::ERRHDL_:
case SbiOpcode::TESTFOR_:
nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
break;
case SbiOpcode::RESUME_:
if ( nOp1 > 1 )
nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
break;
default:
break;
}
m_ConvertedBuf += static_cast<S>(nOp1);
}
virtual void processOpCode2( SbiOpcode eOp, T nOp1, T nOp2 ) override
{
m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
if ( eOp == SbiOpcode::CASEIS_ && nOp1 )
nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
m_ConvertedBuf += static_cast<S>(nOp1);
m_ConvertedBuf += static_cast<S>(nOp2);
}
virtual bool processParams() override { return true; }
// yeuch, careful here, you can only call
// GetBuffer on the returned SbiBuffer once, also
// you (as the caller) get to own the memory
SbiBuffer& buffer()
{
return m_ConvertedBuf;
}
static S convertBufferOffSet( const sal_uInt8* pStart, T nOp1 )
{
PCodeBufferWalker< T > aBuff( pStart, nOp1);
OffSetAccumulator< T, S > aVisitor;
aBuff.visitBuffer( aVisitor );
return aVisitor.offset();
}
};
}
sal_uInt32
SbiCodeGen::calcNewOffSet( sal_uInt8 const * pCode, sal_uInt16 nOffset )
{
return BufferTransformer< sal_uInt16, sal_uInt32 >::convertBufferOffSet( pCode, nOffset );
}
sal_uInt16
SbiCodeGen::calcLegacyOffSet( sal_uInt8 const * pCode, sal_uInt32 nOffset )
{
return BufferTransformer< sal_uInt32, sal_uInt16 >::convertBufferOffSet( pCode, nOffset );
}
template <class T, class S>
void
PCodeBuffConvertor<T,S>::convert()
{
PCodeBufferWalker< T > aBuf( m_pStart, m_nSize );
BufferTransformer< T, S > aTrnsfrmer;
aBuf.visitBuffer( aTrnsfrmer );
m_pCnvtdBuf = reinterpret_cast<sal_uInt8*>(aTrnsfrmer.buffer().GetBuffer());
m_nCnvtdSize = static_cast<S>( aTrnsfrmer.buffer().GetSize() );
}
template class PCodeBuffConvertor< sal_uInt16, sal_uInt32 >;
template class PCodeBuffConvertor< sal_uInt32, sal_uInt16 >;
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|