mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-18 15:55:36 +00:00
Bug 1456039 - Part 1: Add comment for IfThenElseEmitter. r=Yoric
This commit is contained in:
parent
3e80a2a52f
commit
766d2a56e0
@ -1807,23 +1807,90 @@ class MOZ_STACK_CLASS TryEmitter
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Class for emitting bytecode for blocks like if-then-else.
|
||||||
|
//
|
||||||
|
// This class can be used to emit single if-then-else block. Cascading
|
||||||
|
// elseif's need multiple instances of this class.
|
||||||
|
//
|
||||||
|
// Usage: (check for the return value is omitted for simplicity)
|
||||||
|
//
|
||||||
|
// `if (cond) then_block`
|
||||||
|
// IfThenElseEmitter ifThen(this);
|
||||||
|
// emit(cond);
|
||||||
|
// ifThen.emitIf();
|
||||||
|
// emit(then_block);
|
||||||
|
// ifThen.emitEnd();
|
||||||
|
//
|
||||||
|
// `if (cond) then_block else else_block`
|
||||||
|
// IfThenElseEmitter ifThenElse(this);
|
||||||
|
// emit(cond);
|
||||||
|
// ifThenElse.emitIfElse();
|
||||||
|
// emit(then_block);
|
||||||
|
// ifThenElse.emitElse();
|
||||||
|
// emit(else_block);
|
||||||
|
// ifThenElse.emitEnd();
|
||||||
|
//
|
||||||
|
// `cond ? then_expr : else_expr`
|
||||||
|
// IfThenElseEmitter condElse(this);
|
||||||
|
// emit(cond);
|
||||||
|
// condElse.emitCond();
|
||||||
|
// emit(then_block);
|
||||||
|
// condElse.emitElse();
|
||||||
|
// emit(else_block);
|
||||||
|
// condElse.emitEnd();
|
||||||
|
//
|
||||||
class MOZ_STACK_CLASS IfThenElseEmitter
|
class MOZ_STACK_CLASS IfThenElseEmitter
|
||||||
{
|
{
|
||||||
BytecodeEmitter* bce_;
|
BytecodeEmitter* bce_;
|
||||||
|
|
||||||
JumpList jumpAroundThen_;
|
JumpList jumpAroundThen_;
|
||||||
JumpList jumpsAroundElse_;
|
JumpList jumpsAroundElse_;
|
||||||
|
|
||||||
|
// The source note index for SRC_IF, SRC_IF_ELSE, or SRC_COND.
|
||||||
unsigned noteIndex_;
|
unsigned noteIndex_;
|
||||||
|
|
||||||
|
// The stack depth before emitting the then block.
|
||||||
|
// Used for restoring stack depth before emitting the else block.
|
||||||
|
// Also used for assertion to make sure then and else blocks pushed the
|
||||||
|
// same number of values.
|
||||||
int32_t thenDepth_;
|
int32_t thenDepth_;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
// The number of values pushed in the then and else blocks.
|
||||||
int32_t pushed_;
|
int32_t pushed_;
|
||||||
bool calculatedPushed_;
|
bool calculatedPushed_;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// The state of this emitter.
|
||||||
|
//
|
||||||
|
// +-------+ emitIf +----+ emitEnd +-----+
|
||||||
|
// | Start |-+----------->| If |-------------------------+-------->| End |
|
||||||
|
// +-------+ | +----+ | +-----+
|
||||||
|
// | |
|
||||||
|
// | emitCond +------+ emitElse +------+ |
|
||||||
|
// +----------->| Cond |---+--------->| Else |-+
|
||||||
|
// | +------+ | +------+
|
||||||
|
// | |
|
||||||
|
// | emitIfElse +--------+ |
|
||||||
|
// +----------->| IfElse |-+
|
||||||
|
// +--------+
|
||||||
enum State {
|
enum State {
|
||||||
|
// The initial state.
|
||||||
Start,
|
Start,
|
||||||
|
|
||||||
|
// After calling emitIf.
|
||||||
If,
|
If,
|
||||||
|
|
||||||
|
// After calling emitCond.
|
||||||
Cond,
|
Cond,
|
||||||
|
|
||||||
|
// After calling emitIfElse.
|
||||||
IfElse,
|
IfElse,
|
||||||
|
|
||||||
|
// After calling Else.
|
||||||
Else,
|
Else,
|
||||||
|
|
||||||
|
// After calling emitEnd.
|
||||||
End
|
End
|
||||||
};
|
};
|
||||||
State state_;
|
State state_;
|
||||||
@ -1871,6 +1938,17 @@ class MOZ_STACK_CLASS IfThenElseEmitter
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void calculateOrCheckPushed() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
if (!calculatedPushed_) {
|
||||||
|
pushed_ = bce_->stackDepth - thenDepth_;
|
||||||
|
calculatedPushed_ = true;
|
||||||
|
} else {
|
||||||
|
MOZ_ASSERT(pushed_ == bce_->stackDepth - thenDepth_);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool emitIf() {
|
bool emitIf() {
|
||||||
return emitIf(If);
|
return emitIf(If);
|
||||||
@ -1935,22 +2013,17 @@ class MOZ_STACK_CLASS IfThenElseEmitter
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculateOrCheckPushed() {
|
|
||||||
#ifdef DEBUG
|
|
||||||
if (!calculatedPushed_) {
|
|
||||||
pushed_ = bce_->stackDepth - thenDepth_;
|
|
||||||
calculatedPushed_ = true;
|
|
||||||
} else {
|
|
||||||
MOZ_ASSERT(pushed_ == bce_->stackDepth - thenDepth_);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
// Returns the number of values pushed onto the value stack inside
|
||||||
|
// `then_block` and `else_block`.
|
||||||
|
// Can be used in assertion after emitting if-then-else.
|
||||||
int32_t pushed() const {
|
int32_t pushed() const {
|
||||||
return pushed_;
|
return pushed_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the number of values popped onto the value stack inside
|
||||||
|
// `then_block` and `else_block`.
|
||||||
|
// Can be used in assertion after emitting if-then-else.
|
||||||
int32_t popped() const {
|
int32_t popped() const {
|
||||||
return -pushed_;
|
return -pushed_;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user