Perl if 语句在幕后是如何工作的?

问题描述 投票:0回答:1

Perl 的解释器如何执行 if 语句?我想try为使用ref作为谓词的if语句实现一个简单的

Lazy-Basic-Block-Versioning
,也许随后也可以用于子例程。

在研究了源代码之后,我看到了除了控制流之外的所有操作代码。我的解释器经验非常有限,但我假设会看到一个

OP_IF
会弹出堆栈以获取其谓词 OP 代码或类似的内容。我发现
KEY_if
来自 Yacc 语法,随后在
toke.c
中使用并翻译为
KW_IF
,但我找不到
KW_IF
实际使用的位置。

我原本打算只向

peep.c
添加一些具有全局状态的基本优化,但由于没有
OP_IF
,我很难了解如何处理控制流的全局。

perl perlguts
1个回答
0
投票

if
被编译成
and
or
操作。

perl -MO=Concise,-exec -e'if ( f() ) { ... }'
1  <0> enter v
2  <;> nextstate(main 1 -e:1) v:{
3  <0> pushmark s
4  <#> gv[*f] s/EARLYCV
5  <1> entersub[t2] sKS/TARG
6  <|> and(other->7) vK/1
7      <0> pushmark s
8      <$> const[PV "Unimplemented"] s
9      <@> die vK/1
a  <@> leave[1 ref] vKP/REFC
-e syntax OK
perl -MO=Concise,-exec -e'if ( !f() ) { ... }'
1  <0> enter v
2  <;> nextstate(main 1 -e:1) v:{
3  <0> pushmark s
4  <#> gv[*f] s/EARLYCV
5  <1> entersub[t2] sKS/TARG
6  <|> or(other->7) vK/1
7      <0> pushmark s
8      <$> const[PV "Unimplemented"] s
9      <@> die vK/1
a  <@> leave[1 ref] vKP/REFC
-e syntax OK
© www.soinside.com 2019 - 2024. All rights reserved.