📄 opt_constantexpression.mx
字号:
@' The contents of this file are subject to the MonetDB Public License@' Version 1.1 (the "License"); you may not use this file except in@' compliance with the License. You may obtain a copy of the License at@' http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html@'@' Software distributed under the License is distributed on an "AS IS"@' basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the@' License for the specific language governing rights and limitations@' under the License.@'@' The Original Code is the MonetDB Database System.@'@' The Initial Developer of the Original Code is CWI.@' Portions created by CWI are Copyright (C) 1997-2007 CWI.@' All Rights Reserved.@f opt_constantExpression@a M. Kersten@- Constant Expression EvaluationExpressions produced by compilers involving only constantarguments can be evaluated once. It is particular relevantin functions that are repeatably called. One time querieswould not benefit from this extra step.Consider the following snippet, which contains recursiveuse of constant arguments@verbatim a:= 1+1; io.print(a); b:= 2; io.print(b); c:= 3*b; io.print(c); d:= calc.flt(c);io.print(d); e:= mmath.sin(d);io.print(e); optimizer.aliasRemoval(); optimizer.constantExpression();@end verbatimThe code produced by the optimizer would be@verbatim io.print(2); io.print(2); io.print(6); io.print(6); io.print(-0.279415488);@end verbatim@{@malpattern optimizer.constantExpression():straddress OPTconstantExpression;pattern optimizer.constantExpression(mod:str, fcn:str):straddress OPTconstantExpressioncomment "Evaluate constant expressions once.";@h#ifndef _OPT_CX_#define _OPT_CX_#include "mal_stack.h"#include "mal_interpreter.h" /* for showErrors() */#include "opt_prelude.h"#include "opt_support.h"/* #define DEBUG_OPT_CX */#endif@c #include "mal_config.h"#include "opt_constantExpression.h"#include "opt_aliases.h"static intOPTallConstant(MalBlkPtr mb, InstrPtr p){ int i; for( i= p->retc; i < p->argc; i++) if( isConstant(mb, getArg(p,i)) == FALSE) return FALSE; return ( getModuleId(p) == calcRef || getModuleId(p) == mmathRef);}static intOPTconstantExpressionImplementation(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ InstrPtr p; int first=0, nxt, i, j, k, limit, done=0, again=0; Client c= MCgetClient(); MalStkPtr env; (void) stk; (void) pci;#ifdef DEBUG_OPT_CX stream_printf(GDKout,"Constant expression optimizer started\n");#endif do{ again = 0; limit = mb->stop; env= prepareMALstack(mb); env->keepAlive= TRUE; nxt= first; for (k=i= first ; i < limit; i++) { p = getInstrPtr(mb,i); if( p->argc - p->retc > 0 ) j = OPTallConstant(mb,p); else j = 0; if(j && nxt == first) nxt=i;#ifdef DEBUG_OPT_CX stream_printf(GDKout,"Constants:%d\n",j); printInstruction(GDKout, mb, p, LIST_OPT_ALL); #endif if( j && p->retc== 1 && p->barrier == 0 && getLastUpdate(mb,getArg(p,0)) == i && reenterMAL(c,mb,i,i+1,env,0,0) == MAL_SUCCEED){@-The last assignment of a constant expression turns the variable intoa constant itself.@c isConstant(mb,getArg(p,0))= TRUE; setFixed(mb,getArg(p,0)); getVarConstant(mb,getArg(p,0)) = env->stk[getArg(p,0)]; env->stk[getArg(p,0)].vtype= TYPE_int; /* no garbage */ freeInstruction(p);#ifdef DEBUG_OPT_CX stream_printf(GDKout,"Evaluated new constant=%d\n",getArg(p,0));#endif done++; again++; } else mb->stmt[k++]=p; } if( k != mb->stop){ mb->stop= k; setLifespan(mb); /* the lastUpdate property changes */ } else mb->stop= k; first= nxt; } while( again ); clearStack(env); /* garbage collect */ freeStack(env); return 1;}@include optimizerWrapper.mx@h@:exportOptimizer(constantExpression)@@c@:wrapOptimizer(constantExpression,OPT_CHECK_ALL)@@}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -