Debian Codegraph @

basic/source/comp/codegen.cxx

raw
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
21 #include <basic/sberrors.hxx>
22 #include <basic/sbx.hxx>
23 #include <basic/sbmeth.hxx>
24 #include <basic/sbmod.hxx>
25 #include <image.hxx>
26 #include <codegen.hxx>
27 #include <parser.hxx>
28 #include <sbintern.hxx>
29 #include <cstddef>
30 #include <limits>
31 #include <algorithm>
32 #include <osl/diagnose.h>
33 #include <rtl/ustrbuf.hxx>
34 #include <o3tl/string_view.hxx>
35 #include <com/sun/star/script/ModuleType.hpp>
36
37 // nInc is the increment size of the buffers
38
39 SbiCodeGen::SbiCodeGen(SbModule& r, SbiParser* p)
40 : pParser(p)
41 , rMod(r)
42 , nLine(0)
43 , nCol(0)
44 , nForLevel(0)
45 , bStmnt(false)
46 {
47 }
48
49 sal_uInt32 SbiCodeGen::GetPC() const
50 {
51 return aCode.GetSize();
52 }
53
54 // memorize the statement
55
56 void SbiCodeGen::Statement()
57 {
58 if( pParser->IsCodeCompleting() )
59 return;
60
61 bStmnt = true;
62
63 nLine = pParser->GetLine();
64 nCol = pParser->GetCol1();
65
66 // #29955 Store the information of the for-loop-layer
67 // in the upper Byte of the column
68 nCol = (nCol & 0xff) + 0x100 * nForLevel;
69 }
70
71 // Mark the beginning of a statement
72
73 void SbiCodeGen::GenStmnt()
74 {
75 if( pParser->IsCodeCompleting() )
76 return;
77
78 if( bStmnt )
79 {
80 bStmnt = false;
81 Gen( SbiOpcode::STMNT_, nLine, nCol );
82 }
83 }
84
85 // The Gen-Routines return the offset of the 1. operand,
86 // so that jumps can sink their backchain there.
87
88 sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode )
89 {
90 if( pParser->IsCodeCompleting() )
91 return 0;
92
93 #ifdef DBG_UTIL
94 if( eOpcode < SbiOpcode::SbOP0_START || eOpcode > SbiOpcode::SbOP0_END )
95 pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, u"OPCODE1"_ustr );
96 #endif
97 GenStmnt();
98 aCode += static_cast<sal_uInt8>(eOpcode);
99 return GetPC();
100 }
101
102 sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode, sal_uInt32 nOpnd )
103 {
104 if( pParser->IsCodeCompleting() )
105 return 0;
106
107 #ifdef DBG_UTIL
108 if( eOpcode < SbiOpcode::SbOP1_START || eOpcode > SbiOpcode::SbOP1_END )
109 pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, u"OPCODE2"_ustr );
110 #endif
111 GenStmnt();
112 aCode += static_cast<sal_uInt8>(eOpcode);
113 sal_uInt32 n = GetPC();
114 aCode += nOpnd;
115 return n;
116 }
117
118 sal_uInt32 SbiCodeGen::Gen( SbiOpcode eOpcode, sal_uInt32 nOpnd1, sal_uInt32 nOpnd2 )
119 {
120 if( pParser->IsCodeCompleting() )
121 return 0;
122
123 #ifdef DBG_UTIL
124 if( eOpcode < SbiOpcode::SbOP2_START || eOpcode > SbiOpcode::SbOP2_END )
125 pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, u"OPCODE3"_ustr );
126 #endif
127 GenStmnt();
128 aCode += static_cast<sal_uInt8>(eOpcode);
129 sal_uInt32 n = GetPC();
130 aCode += nOpnd1;
131 aCode += nOpnd2;
132 return n;
133 }
134
135 // Storing of the created image in the module
136
137 void SbiCodeGen::Save()
138 {
139 if( pParser->IsCodeCompleting() )
140 return;
141
142 std::unique_ptr<SbiImage> p(new SbiImage);
143 rMod.StartDefinitions();
144 // OPTION BASE-Value:
145 p->nDimBase = pParser->nBase;
146 // OPTION take over the EXPLICIT-Flag
147 if( pParser->bExplicit )
148 p->SetFlag( SbiImageFlags::EXPLICIT );
149
150 int nIfaceCount = 0;
151 if (rMod.isClassModule())
152 {
153 p->SetFlag( SbiImageFlags::CLASSMODULE );
154 GetSbData()->pClassFac->AddClassModule( &rMod );
155
156 nIfaceCount = pParser->aIfaceVector.size();
157 if( !rMod.pClassData )
158 rMod.pClassData.reset(new SbClassData);
159 if( nIfaceCount )
160 {
161 for( int i = 0 ; i < nIfaceCount ; i++ )
162 {
163 const OUString& rIfaceName = pParser->aIfaceVector[i];
164 SbxVariable* pIfaceVar = new SbxVariable( SbxVARIANT );
165 pIfaceVar->SetName( rIfaceName );
166 SbxArray* pIfaces = rMod.pClassData->mxIfaces.get();
167 pIfaces->Insert( pIfaceVar, pIfaces->Count() );
168 }
169 }
170
171 rMod.pClassData->maRequiredTypes = pParser->aRequiredTypes;
172 }
173 else
174 {
175 GetSbData()->pClassFac->RemoveClassModule( &rMod );
176 // Only a ClassModule can revert to Normal
177 if ( rMod.mnType == css::script::ModuleType::CLASS )
178 {
179 rMod.mnType = css::script::ModuleType::NORMAL;
180 }
181 }
182
183 // GlobalCode-Flag
184 if( pParser->HasGlobalCode() )
185 {
186 p->SetFlag( SbiImageFlags::INITCODE );
187 }
188 // The entry points:
189 for( SbiSymDef* pDef = pParser->aPublics.First(); pDef;
190 pDef = pParser->aPublics.Next() )
191 {
192 SbiProcDef* pProc = pDef->GetProcDef();
193 if( pProc && pProc->IsDefined() )
194 {
195 OUString aProcName = pProc->GetName();
196 OUStringBuffer aIfaceProcName;
197 OUString aIfaceName;
198 sal_uInt16 nPassCount = 1;
199 if( nIfaceCount )
200 {
201 int nPropPrefixFound = aProcName.indexOf("Property ");
202 std::u16string_view aPureProcName = aProcName;
203 std::u16string_view aPropPrefix;
204 if( nPropPrefixFound == 0 )
205 {
206 aPropPrefix = aProcName.subView( 0, 13 ); // 13 == Len( "Property ?et " )
207 aPureProcName = aProcName.subView( 13 );
208 }
209 for( int i = 0 ; i < nIfaceCount ; i++ )
210 {
211 const OUString& rIfaceName = pParser->aIfaceVector[i];
212 bool bFound = o3tl::starts_with(aPureProcName, rIfaceName );
213 if( bFound && aPureProcName[rIfaceName.getLength()] == '_' )
214 {
215 if( nPropPrefixFound == 0 )
216 {
217 aIfaceProcName.append(aPropPrefix);
218 }
219 aIfaceProcName.append(aPureProcName.substr(rIfaceName.getLength() + 1) );
220 aIfaceName = rIfaceName;
221 nPassCount = 2;
222 break;
223 }
224 }
225 }
226 SbMethod* pMeth = nullptr;
227 for( sal_uInt16 nPass = 0 ; nPass < nPassCount ; nPass++ )
228 {
229 if( nPass == 1 )
230 {
231 aProcName = aIfaceProcName.toString();
232 }
233 PropertyMode ePropMode = pProc->getPropertyMode();
234 if( ePropMode != PropertyMode::NONE )
235 {
236 SbxDataType ePropType = SbxEMPTY;
237 switch( ePropMode )
238 {
239 case PropertyMode::Get:
240 ePropType = pProc->GetType();
241 break;
242 case PropertyMode::Let:
243 {
244 // type == type of first parameter
245 ePropType = SbxVARIANT; // Default
246 SbiSymPool* pPool = &pProc->GetParams();
247 if( pPool->GetSize() > 1 )
248 {
249 SbiSymDef* pPar = pPool->Get( 1 );
250 if( pPar )
251 {
252 ePropType = pPar->GetType();
253 }
254 }
255 break;
256 }
257 case PropertyMode::Set:
258 ePropType = SbxOBJECT;
259 break;
260 default:
261 OSL_FAIL("Illegal PropertyMode");
262 break;
263 }
264 OUString aPropName = pProc->GetPropName();
265 if( nPass == 1 )
266 {
267 aPropName = aPropName.copy( aIfaceName.getLength() + 1 );
268 }
269 rMod.GetProcedureProperty( aPropName, ePropType );
270 }
271 if( nPass == 1 )
272 {
273 rMod.GetIfaceMapperMethod( aProcName, pMeth );
274 }
275 else
276 {
277 pMeth = rMod.GetMethod( aProcName, pProc->GetType() );
278
279 if( !pProc->IsPublic() )
280 {
281 pMeth->SetFlag( SbxFlagBits::Private );
282 }
283 // Declare? -> Hidden
284 if( !pProc->GetLib().isEmpty())
285 {
286 pMeth->SetFlag( SbxFlagBits::Hidden );
287 }
288 pMeth->nStart = pProc->GetAddr();
289 pMeth->nLine1 = pProc->GetLine1();
290 pMeth->nLine2 = pProc->GetLine2();
291 // The parameter:
292 SbxInfo* pInfo = pMeth->GetInfo();
293 OUString aHelpFile, aComment;
294 sal_uInt32 nHelpId = 0;
295 if( pInfo )
296 {
297 // Rescue the additional data
298 aHelpFile = pInfo->GetHelpFile();
299 aComment = pInfo->GetComment();
300 nHelpId = pInfo->GetHelpId();
301 }
302 // And reestablish the parameter list
303 pInfo = new SbxInfo( aHelpFile, nHelpId );
304 pInfo->SetComment( aComment );
305 SbiSymPool* pPool = &pProc->GetParams();
306 // The first element is always the value of the function!
307 for( sal_uInt16 i = 1; i < pPool->GetSize(); i++ )
308 {
309 SbiSymDef* pPar = pPool->Get( i );
310 SbxDataType t = pPar->GetType();
311 if( !pPar->IsByVal() )
312 {
313 t = static_cast<SbxDataType>( t | SbxBYREF );
314 }
315 if( pPar->GetDims() )
316 {
317 t = static_cast<SbxDataType>( t | SbxARRAY );
318 }
319 // #33677 hand-over an Optional-Info
320 SbxFlagBits nFlags = SbxFlagBits::Read;
321 if( pPar->IsOptional() )
322 {
323 nFlags |= SbxFlagBits::Optional;
324 }
325 pInfo->AddParam( pPar->GetName(), t, nFlags );
326
327 sal_uInt32 nUserData = 0;
328 sal_uInt16 nDefaultId = pPar->GetDefaultId();
329 if( nDefaultId )
330 {
331 nUserData |= nDefaultId;
332 }
333 if( pPar->IsParamArray() )
334 {
335 ParamInfoFlag::setFlagFor(nUserData, ParamInfo::ParamArray);
336 }
337 if( pPar->IsWithBrackets() )
338 {
339 ParamInfoFlag::setFlagFor(nUserData, ParamInfo::WithBrackets);
340 }
341 SbxParamInfo* pParam = nullptr;
342 if( nUserData )
343 {
344 pParam = const_cast<SbxParamInfo*>(pInfo->GetParam( i ));
345 }
346 if( pParam )
347 {
348 pParam->nUserData = nUserData;
349 }
350 }
351 pMeth->SetInfo( pInfo );
352 }
353 } // for( iPass...
354 }
355 }
356 if (aCode.GetErrCode())
357 {
358 pParser->Error(aCode.GetErrCode(), aCode.GetErrMessage());
359 }
360 // The code
361 p->AddCode(aCode.GetBuffer());
362
363 // The global StringPool. 0 is not occupied.
364 SbiStringPool* pPool = &pParser->aGblStrings;
365 sal_uInt16 nSize = pPool->GetSize();
366 p->MakeStrings( nSize );
367 sal_uInt32 i;
368 for( i = 1; i <= nSize; i++ )
369 {
370 p->AddString( pPool->Find( i ) );
371 }
372 // Insert types
373 sal_uInt32 nCount = pParser->rTypeArray->Count();
374 for (i = 0; i < nCount; i++)
375 {
376 p->AddType(static_cast<SbxObject *>(pParser->rTypeArray->Get(i)));
377 }
378 // Insert enum objects
379 nCount = pParser->rEnumArray->Count();
380 for (i = 0; i < nCount; i++)
381 {
382 p->AddEnum(static_cast<SbxObject *>(pParser->rEnumArray->Get(i)));
383 }
384 if( !p->IsError() )
385 {
386 rMod.pImage = std::move(p);
387 }
388 rMod.EndDefinitions();
389 }
390
391 namespace {
392
393 template < class T >
394 class PCodeVisitor
395 {
396 public:
397 virtual ~PCodeVisitor();
398
399 virtual void start( const sal_uInt8* pStart ) = 0;
400 virtual void processOpCode0( SbiOpcode eOp ) = 0;
401 virtual void processOpCode1( SbiOpcode eOp, T nOp1 ) = 0;
402 virtual void processOpCode2( SbiOpcode eOp, T nOp1, T nOp2 ) = 0;
403 virtual bool processParams() = 0;
404 };
405
406 }
407
408 template <class T> PCodeVisitor< T >::~PCodeVisitor()
409 {}
410
411 namespace {
412
413 template <class T>
414 class PCodeBufferWalker
415 {
416 private:
417 T m_nBytes;
418 const sal_uInt8* m_pCode;
419 static T readParam( sal_uInt8 const *& pCode )
420 {
421 T nOp1=0;
422 for ( std::size_t i=0; i<sizeof( T ); ++i )
423 nOp1 |= *pCode++ << ( i * 8);
424 return nOp1;
425 }
426 public:
427 PCodeBufferWalker( const sal_uInt8* pCode, T nBytes ): m_nBytes( nBytes ), m_pCode( pCode )
428 {
429 }
430 void visitBuffer( PCodeVisitor< T >& visitor )
431 {
432 const sal_uInt8* pCode = m_pCode;
433 if ( !pCode )
434 return;
435 const sal_uInt8* pEnd = pCode + m_nBytes;
436 visitor.start( m_pCode );
437 T nOp1 = 0, nOp2 = 0;
438 for( ; pCode < pEnd; )
439 {
440 SbiOpcode eOp = static_cast<SbiOpcode>(*pCode++);
441
442 if ( eOp <= SbiOpcode::SbOP0_END )
443 visitor.processOpCode0( eOp );
444 else if( eOp >= SbiOpcode::SbOP1_START && eOp <= SbiOpcode::SbOP1_END )
445 {
446 if ( visitor.processParams() )
447 nOp1 = readParam( pCode );
448 else
449 pCode += sizeof( T );
450 visitor.processOpCode1( eOp, nOp1 );
451 }
452 else if( eOp >= SbiOpcode::SbOP2_START && eOp <= SbiOpcode::SbOP2_END )
453 {
454 if ( visitor.processParams() )
455 {
456 nOp1 = readParam( pCode );
457 nOp2 = readParam( pCode );
458 }
459 else
460 pCode += ( sizeof( T ) * 2 );
461 visitor.processOpCode2( eOp, nOp1, nOp2 );
462 }
463 }
464 }
465 };
466
467 template < class T, class S >
468 class OffSetAccumulator : public PCodeVisitor< T >
469 {
470 T m_nNumOp0;
471 T m_nNumSingleParams;
472 T m_nNumDoubleParams;
473 public:
474
475 OffSetAccumulator() : m_nNumOp0(0), m_nNumSingleParams(0), m_nNumDoubleParams(0){}
476 virtual void start( const sal_uInt8* /*pStart*/ ) override {}
477 virtual void processOpCode0( SbiOpcode /*eOp*/ ) override { ++m_nNumOp0; }
478 virtual void processOpCode1( SbiOpcode /*eOp*/, T /*nOp1*/ ) override { ++m_nNumSingleParams; }
479 virtual void processOpCode2( SbiOpcode /*eOp*/, T /*nOp1*/, T /*nOp2*/ ) override { ++m_nNumDoubleParams; }
480 S offset()
481 {
482 typedef decltype(T(1) + S(1)) larger_t; // type capable to hold both value ranges of T and S
483 T result = 0 ;
484 static const S max = std::numeric_limits< S >::max();
485 result = m_nNumOp0 + ( ( sizeof(S) + 1 ) * m_nNumSingleParams ) + ( (( sizeof(S) * 2 )+ 1 ) * m_nNumDoubleParams );
486 return std::min<larger_t>(max, result);
487 }
488 virtual bool processParams() override { return false; }
489 };
490
491
492 template < class T, class S >
493 class BufferTransformer : public PCodeVisitor< T >
494 {
495 const sal_uInt8* m_pStart;
496 SbiBuffer m_ConvertedBuf;
497 public:
498 BufferTransformer():m_pStart(nullptr) {}
499 virtual void start( const sal_uInt8* pStart ) override { m_pStart = pStart; }
500 virtual void processOpCode0( SbiOpcode eOp ) override
501 {
502 m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
503 }
504 virtual void processOpCode1( SbiOpcode eOp, T nOp1 ) override
505 {
506 m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
507 switch( eOp )
508 {
509 case SbiOpcode::JUMP_:
510 case SbiOpcode::JUMPT_:
511 case SbiOpcode::JUMPF_:
512 case SbiOpcode::GOSUB_:
513 case SbiOpcode::CASEIS_:
514 case SbiOpcode::RETURN_:
515 case SbiOpcode::ERRHDL_:
516 case SbiOpcode::TESTFOR_:
517 nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
518 break;
519 case SbiOpcode::RESUME_:
520 if ( nOp1 > 1 )
521 nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
522 break;
523 default:
524 break;
525
526 }
527 m_ConvertedBuf += static_cast<S>(nOp1);
528 }
529 virtual void processOpCode2( SbiOpcode eOp, T nOp1, T nOp2 ) override
530 {
531 m_ConvertedBuf += static_cast<sal_uInt8>(eOp);
532 if ( eOp == SbiOpcode::CASEIS_ && nOp1 )
533 nOp1 = static_cast<T>( convertBufferOffSet(m_pStart, nOp1) );
534 m_ConvertedBuf += static_cast<S>(nOp1);
535 m_ConvertedBuf += static_cast<S>(nOp2);
536
537 }
538 virtual bool processParams() override { return true; }
539 // yeuch, careful here, you can only call
540 // GetBuffer on the returned SbiBuffer once, also
541 // you (as the caller) get to own the memory
542 SbiBuffer& buffer()
543 {
544 return m_ConvertedBuf;
545 }
546 static S convertBufferOffSet( const sal_uInt8* pStart, T nOp1 )
547 {
548 PCodeBufferWalker< T > aBuff( pStart, nOp1);
549 OffSetAccumulator< T, S > aVisitor;
550 aBuff.visitBuffer( aVisitor );
551 return aVisitor.offset();
552 }
553 };
554
555 }
556
557 sal_uInt32
558 SbiCodeGen::calcNewOffSet( sal_uInt8 const * pCode, sal_uInt16 nOffset )
559 {
560 return BufferTransformer< sal_uInt16, sal_uInt32 >::convertBufferOffSet( pCode, nOffset );
561 }
562
563 sal_uInt16
564 SbiCodeGen::calcLegacyOffSet( sal_uInt8 const * pCode, sal_uInt32 nOffset )
565 {
566 return BufferTransformer< sal_uInt32, sal_uInt16 >::convertBufferOffSet( pCode, nOffset );
567 }
568
569 template <class T, class S>
570 void
571 PCodeBuffConvertor<T,S>::convert()
572 {
573 PCodeBufferWalker< T > aBuf( m_pStart, m_nSize );
574 BufferTransformer< T, S > aTrnsfrmer;
575 aBuf.visitBuffer( aTrnsfrmer );
576 // TODO: handle buffer errors
577 m_aCnvtdBuf = aTrnsfrmer.buffer().GetBuffer();
578 }
579
580 template class PCodeBuffConvertor< sal_uInt16, sal_uInt32 >;
581 template class PCodeBuffConvertor< sal_uInt32, sal_uInt16 >;
582
583 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */