| 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 <basic/sberrors.hxx> |
| 21 |
|
| 22 |
#include <codegen.hxx> |
| 23 |
#include <expr.hxx> |
| 24 |
#include <parser.hxx> |
| 25 |
|
| 26 |
// Transform table for token operators and opcodes |
| 27 |
|
| 28 |
namespace { |
| 29 |
|
| 30 |
struct OpTable { |
| 31 |
SbiToken eTok; // Token |
| 32 |
SbiOpcode eOp; // Opcode |
| 33 |
}; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
const OpTable aOpTable [] = { |
| 38 |
{ EXPON,SbiOpcode::EXP_ }, |
| 39 |
{ MUL, SbiOpcode::MUL_ }, |
| 40 |
{ DIV, SbiOpcode::DIV_ }, |
| 41 |
{ IDIV, SbiOpcode::IDIV_ }, |
| 42 |
{ MOD, SbiOpcode::MOD_ }, |
| 43 |
{ PLUS, SbiOpcode::PLUS_ }, |
| 44 |
{ MINUS,SbiOpcode::MINUS_ }, |
| 45 |
{ EQ, SbiOpcode::EQ_ }, |
| 46 |
{ NE, SbiOpcode::NE_ }, |
| 47 |
{ LE, SbiOpcode::LE_ }, |
| 48 |
{ GE, SbiOpcode::GE_ }, |
| 49 |
{ LT, SbiOpcode::LT_ }, |
| 50 |
{ GT, SbiOpcode::GT_ }, |
| 51 |
{ AND, SbiOpcode::AND_ }, |
| 52 |
{ OR, SbiOpcode::OR_ }, |
| 53 |
{ XOR, SbiOpcode::XOR_ }, |
| 54 |
{ EQV, SbiOpcode::EQV_ }, |
| 55 |
{ IMP, SbiOpcode::IMP_ }, |
| 56 |
{ NOT, SbiOpcode::NOT_ }, |
| 57 |
{ NEG, SbiOpcode::NEG_ }, |
| 58 |
{ CAT, SbiOpcode::CAT_ }, |
| 59 |
{ LIKE, SbiOpcode::LIKE_ }, |
| 60 |
{ IS, SbiOpcode::IS_ }, |
| 61 |
}; |
| 62 |
|
| 63 |
// Output of an element |
| 64 |
void SbiExprNode::Gen( SbiCodeGen& rGen, RecursiveMode eRecMode ) |
| 65 |
{ |
| 66 |
sal_uInt16 nStringId; |
| 67 |
|
| 68 |
if( IsConstant() ) |
| 69 |
{ |
| 70 |
switch( GetType() ) |
| 71 |
{ |
| 72 |
case SbxEMPTY: |
| 73 |
rGen.Gen( SbiOpcode::EMPTY_ ); |
| 74 |
break; |
| 75 |
case SbxSTRING: |
| 76 |
nStringId = rGen.GetParser()->aGblStrings.Add( aStrVal ); |
| 77 |
rGen.Gen( SbiOpcode::SCONST_, nStringId ); |
| 78 |
break; |
| 79 |
default: |
| 80 |
// tdf#131296 - generate SbiOpcode::NUMBER_ instead of SbiOpcode::CONST_ |
| 81 |
// for SbxINTEGER and SbxLONG including their numeric value and its data type, |
| 82 |
// which will be restored in SbiRuntime::StepLOADNC. |
| 83 |
nStringId = rGen.GetParser()->aGblStrings.Add( nVal, eType ); |
| 84 |
rGen.Gen( SbiOpcode::NUMBER_, nStringId ); |
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
else if( IsOperand() ) |
| 89 |
{ |
| 90 |
SbiExprNode* pWithParent_ = nullptr; |
| 91 |
SbiOpcode eOp; |
| 92 |
if( aVar.pDef->GetScope() == SbPARAM ) |
| 93 |
{ |
| 94 |
eOp = SbiOpcode::PARAM_; |
| 95 |
if( aVar.pDef->GetPos() == 0 ) |
| 96 |
{ |
| 97 |
bool bTreatFunctionAsParam = true; |
| 98 |
if( eRecMode == FORCE_CALL ) |
| 99 |
{ |
| 100 |
bTreatFunctionAsParam = false; |
| 101 |
} |
| 102 |
else if( eRecMode == UNDEFINED ) |
| 103 |
{ |
| 104 |
if( aVar.pPar && aVar.pPar->IsBracket() ) |
| 105 |
{ |
| 106 |
bTreatFunctionAsParam = false; |
| 107 |
} |
| 108 |
} |
| 109 |
if( !bTreatFunctionAsParam ) |
| 110 |
{ |
| 111 |
eOp = aVar.pDef->IsGlobal() ? SbiOpcode::FIND_G_ : SbiOpcode::FIND_; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
// special treatment for WITH |
| 116 |
else if( (pWithParent_ = pWithParent) != nullptr ) |
| 117 |
{ |
| 118 |
eOp = SbiOpcode::ELEM_; // .-Term in WITH |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
eOp = ( aVar.pDef->GetScope() == SbRTL ) ? SbiOpcode::RTL_ : |
| 123 |
(aVar.pDef->IsGlobal() ? SbiOpcode::FIND_G_ : SbiOpcode::FIND_); |
| 124 |
} |
| 125 |
|
| 126 |
if( eOp == SbiOpcode::FIND_ ) |
| 127 |
{ |
| 128 |
|
| 129 |
SbiProcDef* pProc = aVar.pDef->GetProcDef(); |
| 130 |
if ( rGen.GetParser()->bClassModule ) |
| 131 |
{ |
| 132 |
eOp = SbiOpcode::FIND_CM_; |
| 133 |
} |
| 134 |
else if ( aVar.pDef->IsStatic() || (pProc && pProc->IsStatic()) ) |
| 135 |
{ |
| 136 |
eOp = SbiOpcode::FIND_STATIC_; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (pWithParent_ != nullptr) |
| 141 |
pWithParent_->Gen(rGen); |
| 142 |
|
| 143 |
for( SbiExprNode* p = this; p; p = p->aVar.pNext ) |
| 144 |
{ |
| 145 |
p->GenElement( rGen, eOp ); |
| 146 |
eOp = SbiOpcode::ELEM_; |
| 147 |
} |
| 148 |
} |
| 149 |
else if( eNodeType == SbxTYPEOF ) |
| 150 |
{ |
| 151 |
pLeft->Gen(rGen); |
| 152 |
rGen.Gen( SbiOpcode::TESTCLASS_, nTypeStrId ); |
| 153 |
} |
| 154 |
else if( eNodeType == SbxNEW ) |
| 155 |
{ |
| 156 |
rGen.Gen( SbiOpcode::CREATE_, 0, nTypeStrId ); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
pLeft->Gen(rGen); |
| 161 |
if( pRight ) |
| 162 |
{ |
| 163 |
pRight->Gen(rGen); |
| 164 |
} |
| 165 |
for (const OpTable& op : aOpTable) |
| 166 |
{ |
| 167 |
if (op.eTok == eTok) |
| 168 |
{ |
| 169 |
rGen.Gen(op.eOp); |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// Output of an operand element |
| 177 |
|
| 178 |
void SbiExprNode::GenElement( SbiCodeGen& rGen, SbiOpcode eOp ) |
| 179 |
{ |
| 180 |
#ifdef DBG_UTIL |
| 181 |
if ((eOp < SbiOpcode::RTL_ || eOp > SbiOpcode::CALLC_) && eOp != SbiOpcode::FIND_G_ && eOp != SbiOpcode::FIND_CM_ && eOp != SbiOpcode::FIND_STATIC_) |
| 182 |
rGen.GetParser()->Error( ERRCODE_BASIC_INTERNAL_ERROR, u"Opcode"_ustr ); |
| 183 |
#endif |
| 184 |
SbiSymDef* pDef = aVar.pDef; |
| 185 |
// The ID is either the position or the String-ID |
| 186 |
// If the bit Bit 0x8000 is set, the variable have |
| 187 |
// a parameter list. |
| 188 |
sal_uInt16 nId = ( eOp == SbiOpcode::PARAM_ ) ? pDef->GetPos() : pDef->GetId(); |
| 189 |
// Build a parameter list |
| 190 |
if( aVar.pPar && aVar.pPar->GetSize() ) |
| 191 |
{ |
| 192 |
nId |= 0x8000; |
| 193 |
aVar.pPar->Gen(rGen); |
| 194 |
} |
| 195 |
|
| 196 |
rGen.Gen( eOp, nId, sal::static_int_cast< sal_uInt16 >( GetType() ) ); |
| 197 |
|
| 198 |
if( aVar.pvMorePar ) |
| 199 |
{ |
| 200 |
for( auto& pExprList: *aVar.pvMorePar ) |
| 201 |
{ |
| 202 |
pExprList->Gen(rGen); |
| 203 |
rGen.Gen( SbiOpcode::ARRAYACCESS_ ); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// Create an Argv-Table |
| 209 |
// The first element remain available for return value etc. |
| 210 |
// See as well SbiProcDef::SbiProcDef() in symtbl.cxx |
| 211 |
|
| 212 |
void SbiExprList::Gen(SbiCodeGen& rGen) |
| 213 |
{ |
| 214 |
if( aData.empty() ) |
| 215 |
return; |
| 216 |
|
| 217 |
rGen.Gen( SbiOpcode::ARGC_ ); |
| 218 |
// Type adjustment at DECLARE |
| 219 |
|
| 220 |
for( auto& pExpr: aData ) |
| 221 |
{ |
| 222 |
pExpr->Gen(); |
| 223 |
if( !pExpr->GetName().isEmpty() ) |
| 224 |
{ |
| 225 |
// named arg |
| 226 |
sal_uInt16 nSid = rGen.GetParser()->aGblStrings.Add( pExpr->GetName() ); |
| 227 |
rGen.Gen( SbiOpcode::ARGN_, nSid ); |
| 228 |
|
| 229 |
/* TODO: Check after Declare concept change |
| 230 |
// From 1996-01-10: Type adjustment at named -> search suitable parameter |
| 231 |
if( pProc ) |
| 232 |
{ |
| 233 |
// For the present: trigger an error |
| 234 |
pParser->Error( ERRCODE_BASIC_NO_NAMED_ARGS ); |
| 235 |
|
| 236 |
// Later, if Named Args at DECLARE is possible |
| 237 |
//for( sal_uInt16 i = 1 ; i < nParAnz ; i++ ) |
| 238 |
//{ |
| 239 |
// SbiSymDef* pDef = pPool->Get( i ); |
| 240 |
// const String& rName = pDef->GetName(); |
| 241 |
// if( rName.Len() ) |
| 242 |
// { |
| 243 |
// if( pExpr->GetName().ICompare( rName ) |
| 244 |
// == COMPARE_EQUAL ) |
| 245 |
// { |
| 246 |
// pParser->aGen.Gen( ARGTYP_, pDef->GetType() ); |
| 247 |
// break; |
| 248 |
// } |
| 249 |
// } |
| 250 |
//} |
| 251 |
} |
| 252 |
*/ |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
rGen.Gen( SbiOpcode::ARGV_ ); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
void SbiExpression::Gen( RecursiveMode eRecMode ) |
| 262 |
{ |
| 263 |
// special treatment for WITH |
| 264 |
// If pExpr == .-term in With, approximately Gen for Basis-Object |
| 265 |
pExpr->Gen( pParser->aGen, eRecMode ); |
| 266 |
if( bByVal ) |
| 267 |
{ |
| 268 |
pParser->aGen.Gen( SbiOpcode::BYVAL_ ); |
| 269 |
} |
| 270 |
if( bBased ) |
| 271 |
{ |
| 272 |
sal_uInt16 uBase = pParser->nBase; |
| 273 |
if( pParser->IsCompatible() ) |
| 274 |
{ |
| 275 |
uBase |= 0x8000; // #109275 Flag compatibility |
| 276 |
} |
| 277 |
pParser->aGen.Gen( SbiOpcode::BASED_, uBase ); |
| 278 |
pParser->aGen.Gen( SbiOpcode::ARGV_ ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |