Recent Resources
-
GLSL 4.0: Discarding Fragments to Create a Perf...
-
GLSL 4.0: Using Subroutines to Select Shader Fu...
-
Building a Complete Board-based Puzzle Game wit...
-
JIRA: Programming Workflows
-
.NET Generics 4.0: Container Patterns and Best...
-
Raw Meat: Game Design Tips from Team Meat's...
-
Sedge: An Automated Error Reporting Tool
Creating a Scripting System in C++ Part IV: The Stack and Program Flow
By Greg Rosenblatt | Published Apr 16 2002 06:10 PM in Game Programming
Download attached article resource
Download attached article resource
Premise
This article will introduce some new concepts that will allow us eventually to model higher-level languages with simple bytecode instructions. The variable data associated with scripts will become part of a stack, which will enhance the higher level behavior we can achieve with a handful of opcodes, and allow us to evaluate complex mathematical expressions with relative ease. With these new facilities added, it will also be possible for our scripts to have controlled program flow, including patterns for if-else and loops, as well as script-based functions.
The Reason for a Stack
A stack is a natural representation for many types of systems involving states and procedural calculation. One such use as mentioned is in evaluating expressions that involve more than a single operation, since such expressions can be broken into parts and handled step by step.
Consider a simple arithmetic expression:
Implementing a Stack
One of the first things that come to mind when thinking about stack implementations is that simple containers that could be used are already implemented as part of the standard library. They certainly are. However there will be certain features we need that they do not provide on their own.
We will start with a simple stack that uses a vector for its implementation:
This should seem pretty straightforward, except for the name. But the reason for this name will become evident once we start dealing with chunks of variable data that are members of the same "scope" within a script.
Relocation of Execution
In order to accommodate for the things we would like to achieve regarding program flow and data accessing opcodes, we are going to want to move execution-specific implementation over to the appropriately named Execution class.
At the moment, the execution-specific code lies directly in the virtual machine. Instead, we would like the execution to take place as a member function of the Execution class, and have the virtual machine call to Execute redirect the execution to the proper Execution's execution. Say that ten times fast. If that sentence was as hard to follow as it was to type, the code will explain.
This is what we would like to have happen:
Now we will need to create the Execution class's Execute() similar to that of VirtualMachine's before being changed:
This is all there is to it since the cast should be implicit:
Incorporating the Stack
What we would like is to be able to replace the data vector we currently have in the Execution class with our new stack:
The accessors are a simple change:
It will look something like this:
These accessors should now be updated accordingly. Note the change in index calculation:
A New Set of Instructions
To keep things tidy, I will be scrapping the old instructions from the provided source. Many of them would have found themselves redundant anyway. Now, here are some new instructions that will make use of the stack, each with a brief description of how they should work.
The first three of these instructions will require an additional value. For the first instruction, the data value determines the value pushed onto the stack. For the next two, the data determines the index of the variable referenced either for having its value pushed onto the stack in the former case, or being assigned a value as in the latter.
A conceptual example of flat representation of an expression:
Expression:
Certainly this flat representation could be more optimal if we made use of an instruction that specifically incremented a particular variable, but the more general instruction type for addition still gets the job done.
Here are the implementations for some of these instructions:
Program Flow
Up to this point scripts have only had a single path of execution. Each Instruction is only executed once in the order they are listed. What we will do now is incorporate instructions that actually move the instruction pointer to any position desired, rather than simply moving to the next one in the list. Such a change in position can either be absolute, or conditional. The concept is a simple yet powerful one, and will be the foundation for implementing the control structures of a higher-level language.
In the simplest case, an instruction to cause movement to a new position in the code would require a single parameter with a value of the destination index. This is an absolute jump:
Higher-Level Constructs
An explanation of how such low-level jump instructions relate to higher-level control structures is due. I will now illustrate a few examples of C-style constructs and their equivalent jump instruction patterns in our bytecode. Note that although our parser doesn't support commenting, I will be using them in the example scripts shown.
The If-Else Clause:
C:
C:
C:
Conditional Expressions
In order to drive our new conditional jumps, we need to have instructions that yield conditional results. Instructions performing value or logical comparison make the most sense in this case, and most will function much like the instructions implementing binary operations in the earlier section.
Unary:
op_not: performs a logical negation of the top of the stack
Binary:
Each of these compares the top two values on the stack, popping the top and returning an appropriate true/false to the new top.
op_and: performs logical conjunction
op_or: performs logical disjunction
op_equal
op_not_equal
op_greater
op_greater_equal
op_less
op_less_equal
This condition:
Here is code for two of these conditional evaluators:
Demonstration
For a final demonstration of what we can now do, we will write a script that calculates the factorial of a particular number of our choosing. I chose to compute a factorial since it is something most readers should be familiar with. I will show how an iterative factorial would be performed as a function in C++, and then show how it can be broken down to be expressed as a script in our bytecode.
Iterative Factorial in C++:
A script that outputs the factorial of 4:
Interesting Results
If you play around with the script's initial input value, you'll come to realize something... It only outputs the correct value for an input of 5 or less! What happened? The reason for this is our stack contains elements of type char, which implies a range of -128 to 127. The factorial of 5 is 120, just barely making the cut. So what if we want to find the factorial of 7? We only have to make a small change.
Behold the power of templates:
Conclusion
In my opinion, these have most interesting concepts developed in our low-level implementation so far. Although it is obviously tedious work, scripts can now be written to express many of the simple higher-level concepts we have always taken for granted. I leave the expression of some other high-level constructs (such as functions) as exercises for the reader until next time. The tools for expressing simple functions are here.
Check out the downloadable source for certain small changes in the test program we built last time in order to allow for our new instructions. One change was made to the parser to allow for underscores to be scanned as part of an opcode name. Other changes were made, but due to their trivial nature, they don't change the way the program behaves, and aren't really worth mentioning.
Please give me a piece of your mind in the forum discussion, or by email: glr9940@rit.edu
This article will introduce some new concepts that will allow us eventually to model higher-level languages with simple bytecode instructions. The variable data associated with scripts will become part of a stack, which will enhance the higher level behavior we can achieve with a handful of opcodes, and allow us to evaluate complex mathematical expressions with relative ease. With these new facilities added, it will also be possible for our scripts to have controlled program flow, including patterns for if-else and loops, as well as script-based functions.
The Reason for a Stack
A stack is a natural representation for many types of systems involving states and procedural calculation. One such use as mentioned is in evaluating expressions that involve more than a single operation, since such expressions can be broken into parts and handled step by step.
Consider a simple arithmetic expression:
x + y * pKnowing simple rules for the ordering of operations, we can create a procedure for evaluating such an expression:
multiply y by pPlace this expression in the context of variable assignment, and you will begin to see how it relates to programming:
add the result with x
i = x + y * pNotice that the procedure for evaluating such an expression mentions something that is only implicitly present in the expression itself. The procedure uses a term "result" which is understood to mean the result from the previous partial evaluation. It's almost an automatic assumption that we set aside the results of these sub-evaluations for use in further evaluations. What a stack will do for us is allow us to make this natural assumption. Being a LIFO (last in first out) data structure, we can push values onto it, pop them off to perform an operation, push the result back on, and so on until the parent evaluation is completed. By following a consistent set of evaluation rules, we can ensure that the stack will manage these "results" without us having to keep track of them.
multiply y by p
add the result with x
assign the final result as the value of i
Implementing a Stack
One of the first things that come to mind when thinking about stack implementations is that simple containers that could be used are already implemented as part of the standard library. They certainly are. However there will be certain features we need that they do not provide on their own.
We will start with a simple stack that uses a vector for its implementation:
template <class T>The stack can optionally be instantiated with memory reserved, and has an interface for pushing and popping individual elements. Random access, and access to the top of the stack are available, as is the size of the stack. On top of all this, assertions will guard against access that would fall out-of-bounds while debugging.
class ScopeStack
{
public:
ScopeStack() {}
ScopeStack(size_t reserveSize)
{ _stack.reserve(reserveSize); }
// todo: scope interface
// element interface
void Push(const T& element) { _stack.push_back(element); }
void Pop() { assert(!_stack.empty()); _stack.pop_back(); }
// accessors
const T& Top() const { assert(!_stack.empty()); return _stack.back(); }
T& Top() { assert(!_stack.empty()); return _stack.back(); }
const T& Get(size_t index) const { assert((index)<_stack.size());
return _stack[index]; }
T& Get(size_t index) { assert((index)<_stack.size()); return _stack[index]; }
const T& operator[](size_t index) const { return Get(index); }
T& operator[](size_t index) { return Get(index); }
// properties
size_t Size() const { return _stack.size(); }
private:
std::vector<T> _stack;
};
This should seem pretty straightforward, except for the name. But the reason for this name will become evident once we start dealing with chunks of variable data that are members of the same "scope" within a script.
Relocation of Execution
In order to accommodate for the things we would like to achieve regarding program flow and data accessing opcodes, we are going to want to move execution-specific implementation over to the appropriately named Execution class.
At the moment, the execution-specific code lies directly in the virtual machine. Instead, we would like the execution to take place as a member function of the Execution class, and have the virtual machine call to Execute redirect the execution to the proper Execution's execution. Say that ten times fast. If that sentence was as hard to follow as it was to type, the code will explain.
This is what we would like to have happen:
void VirtualMachine::Execute(size_t scriptId)The first step is to move the script and instruction pointers over to the Execution class. Simple enough. Next we will need to make a change to the SelectScript() utility:
{
_execution.Execute(SelectScript(scriptId)); // select our script by ID
}
ScriptRef SelectScript(size_t index) const // set current script by idInstead of implicitly updating the pointers to the script and instructions (which it no longer possesses), it now simply returns the proper script pointer so that it may be passed to an Execution. It will also guard against improper script ids from being accepted through assertion.
{
assert(index < _scriptList.size()); // make sure the id is valid
return &_scriptList[index];
}
Now we will need to create the Execution class's Execute() similar to that of VirtualMachine's before being changed:
void VirtualMachine::Execution::Execute(ScriptRef scriptPtr)Some changes have been made to how the loop is terminated after some re-evaluation. It will be safer this way once we bring in some new opcodes. To allow for this change, we add an additional accessor to the Script class to obtain a pointer to the end of its instruction list, and an end pointer to the Execution class.
{
_scriptPtr = scriptPtr;
SetDataSize(_scriptPtr->VarCount()); // initialize variable data
_instrPtr = _scriptPtr->InstrPtr(); // initialize root pointer
_instrEnd = _scriptPtr->End(); // initialize end marker
_instr = _instrPtr; // set our iterator to the beginning
while (_instr < _instrEnd) // ensure pointer stays in-bounds
{
switch(_instr->Code())
{
// message functionality
case op_talk:
std::cout << "I am talking." << std::endl;
++_instr; // iterate
break;
case op_print:
std::cout << _instr->Data() << std::endl; // print data
++_instr; // iterate
break;
// other functionality
. . .
// end
case op_end:
_instr = _instrEnd; // discontinue the loop
break;
}
}
}
This is all there is to it since the cast should be implicit:
// in the Script classWith all the said changes in place the loading example should still execute properly.
const Instruction* End() const { return _instrList.end(); }
Incorporating the Stack
What we would like is to be able to replace the data vector we currently have in the Execution class with our new stack:
class ExecutionDoing so will break some other parts of our implementation, unfortunately. The variable manipulation opcode handlers may need rewriting (if they are still desired), the execution's accessors have to be updated, and the function for exposing variable states will have to be changed entirely. Another function that will have to change is SetDataSize(). SetDataSize() and state exposure will need to wait until we finish implementing our custom stack.
{
public:
. . . Interface . . .
private:
ScopeStack<char> _stack;
ScriptRef _scriptPtr; // executing script
InstrRef _instrPtr; // root instruction
InstrRef _instrEnd; // end of instruction list
InstrRef _instr; // current instruction
};
The accessors are a simple change:
// data accessBefore we can advance, we will have to begin implementing the scoping part of our stack. First will be a function to initialize what is essentially the outer-most scope. It's a simple resize:
void SetVar(size_t i, char val) { _stack[i] = val; }
char GetVar(size_t i) const { return _stack[i]; }
void SetInitialScope(size_t scopeSize)Execution's SetDataSize() can now call this method:
{
// add guards to make sure this only happens once at the start
_stack.resize(scopeSize);
}
void SetDataSize(size_t varCount) { _stack.SetInitialScope(varCount); }
Now we would like to be able to create a group of variables on the stack that can be removed in the same group as well. This functionality will serve us well once we begin implementing a higher-level language with "scope" considerations. To do this, we will have to keep track of the starting positions of these scopes. I refer to these as indices in the implementation. Because we will need to store an additional index for every scope we push onto the stack, we will need to have some mechanism for storing these dynamically. The simple approach I will be taking will include an additional vector to store these indices. The index for the current scope will also be stored, and referred to as the stack base. Implementations for accessing variables from the current scope will then use the current index to find the right place on the actual data vector.It will look something like this:
ScopeStack() : _stackBase(0) {}
// scope interface
void SetInitialScope(size_t scopeSize)
{
// initial scope should only be set once
assert(_stack.empty() && _indices.empty());
_stack.resize(scopeSize);
}
void PushScope(size_t scopeSize)
{
_indices.push_back(_stackBase); // store the current index
_stackBase = _stack.size(); // get new index
_stack.resize(_stackBase+scopeSize); // accomodate new scope data
}
void PopScope()
{
assert(!_indices.empty()); // don't try to pop a non-existent scope
_stack.resize(_stackBase); // shrink back to the end of the former scope
_stackBase = _indices.back(); // grab the base index for the former scope
_indices.pop_back(); // take that index off the index stack
}
. . .
private:
std::vector<T> _stack;
std::vector<size_t> _indices;
size_t _stackBase;
You might consider storing actual pointers to these stack positions instead of an actual offset into the vector, thinking you could be more efficient with such an approach. However there is the messiness involved in reallocation of the _stack in the event that its capacity is exceeded. The pointers would be invalidated. This is why I chose to store offsets as indices instead of pointers/iterators.These accessors should now be updated accordingly. Note the change in index calculation:
const T& Get(size_t index) constSome new properties could be accessible as well:
{ assert((_stackBase+index)<_stack.size()); return _stack[_stackBase+index]; }
T& Get(size_t index)
{ assert((_stackBase+index)<_stack.size()); return _stack[_stackBase+index]; }
size_t ScopeDepth() constNew accessors for getting an element indexed from a particular depth should be included. These do the same as the normal Get(), except they don't use the current stack depth as their starting point:
{ return _indices.size(); }
size_t GetScopeIndex(size_t depth) const
{ assert(depth<_indices.size()); return _indices[depth]; }
const T& GetAtDepth(size_t index, size_t depth) constAs I said earlier, the function to expose variable state now needs to be rewritten. This function will go through every element on the stack, while keeping track of the current stack depth, and output the value under its appropriate scope heading. It will be a member of the Execution class, and will take an ostream reference as a parameter to allow for flexible output options:
{ assert((GetScopeIndex(depth)+index)<_stack.size());
return _stack[GetScopeIndex(depth)+index]; }
T& GetAtDepth(size_t index, size_t depth)
{ assert((GetScopeIndex(depth)+index)<_stack.size());
return _stack[GetScopeIndex(depth)+index]; }
void VirtualMachine::Execution::ExposeStackState(ostream& out) constThe virtual machine can now relay a call to this new function to show us the variable state through the console:
{
size_t numScopes = _stack.ScopeDepth();
size_t stackSize = _stack.Size();
size_t index, label, curDepth = 0, curEnd = 0;
// for the entire stack
for (index = 0, label = 0; index < stackSize; ++index, ++label)
{
if (index == curEnd) // for every new scope in the stack
{
label = 0;
out << "Scope Depth: " << curDepth << endl; // output the scope depth
if (curDepth < numScopes)
{
curEnd = _stack.GetScopeIndex(curDepth); // get end of this scope
++curDepth; // advance to next scope depth
}
else
curEnd = 0;
}
// output each value
out << " " << label << ": " << static_cast<int>(_stack[index]) << endl;
}
}
void ShowVariableState() const { _execution.ExposeStackState(std::cout); }
And with that, the formerly written loading code should now work again with the new implementations.A New Set of Instructions
To keep things tidy, I will be scrapping the old instructions from the provided source. Many of them would have found themselves redundant anyway. Now, here are some new instructions that will make use of the stack, each with a brief description of how they should work.
| op_output: | pops top of the stack and outputs the value |
| op_push_const: | pushes a new value onto the stack |
| op_push_var: | pushes a variable's value onto the stack (variables residing lower in the stack) |
| op_assign: | pops the top of the stack and assigns it to a chosen position (lower in the stack) |
| op_add: | pops the top of the stack and adds it to the new stack top |
| op_subtract: | pops the top of the stack and subtracts itself from the new stack top |
| op_multiply: | pops the top and multiplies the new top by the popped value |
| op_divide: | pops the top and divides the new top by the popped value |
A conceptual example of flat representation of an expression:
Expression:
x = x + 1Flat Representation (our instructions):
push_var xNote that 'x' is used as a placeholder for a particular variable index, which would correspond to the conceptual 'x' written in the higher-level expression. During a compilation process, the variable 'x' would be assigned a memory index in the stack, and all references to this 'x' would be resolved to that index.
push_const 1
add
assign x
Certainly this flat representation could be more optimal if we made use of an instruction that specifically incremented a particular variable, but the more general instruction type for addition still gets the job done.
Here are the implementations for some of these instructions:
// outputThe rest can be found in the downloadable source.
case op_output:
cout << static_cast<int>(_stack.Top()) << endl;
_stack.Pop();
++_instr;
break;
// stack
case op_push_const:
_stack.Push(_instr->Data()[0]);
++_instr;
break;
case op_push_var:
_stack.Push(_stack[_instr->Data()[0]]);
++_instr;
break;
case op_assign:
_stack[_instr->Data()[0]] = _stack.Top();
_stack.Pop();
++_instr;
break;
// arithmetic
case op_add:
{
char top = _stack.Top();
_stack.Pop();
_stack.Top() += top;
}
++_instr;
break;
Program Flow
Up to this point scripts have only had a single path of execution. Each Instruction is only executed once in the order they are listed. What we will do now is incorporate instructions that actually move the instruction pointer to any position desired, rather than simply moving to the next one in the list. Such a change in position can either be absolute, or conditional. The concept is a simple yet powerful one, and will be the foundation for implementing the control structures of a higher-level language.
In the simplest case, an instruction to cause movement to a new position in the code would require a single parameter with a value of the destination index. This is an absolute jump:
case op_jump:When executed, this instruction will always lead the flow to the position indicated by its data. What we would like in addition to this is an instruction that will jump conditionally, determined by a Boolean value at the top of the stack. For this, we will have to adopt a Boolean convention for our data. To keep things consistent with what we're familiar with, false will refer to a value of zero, and true to any non-zero value.
_instr = _instrPtr + _instr->Data()[0];
break;
case op_jump_if_true:These two instructions will behave as stated, moving to a new position after examining the value at the top of the stack, and popping it off before they are done.
if (_stack.Top() != 0)
_instr = _instrPtr + _instr->Data()[0];
else
++_instr;
_stack.Pop();
break;
case op_jump_if_false:
if (_stack.Top() == 0)
_instr = _instrPtr + _instr->Data()[0];
else
++_instr;
_stack.Pop();
break;
Higher-Level Constructs
An explanation of how such low-level jump instructions relate to higher-level control structures is due. I will now illustrate a few examples of C-style constructs and their equivalent jump instruction patterns in our bytecode. Note that although our parser doesn't support commenting, I will be using them in the example scripts shown.
The If-Else Clause:
C:
if (condition)Script:
{
// do something
}
else
{
// do something else
}
// evaluate conditionThe While Loop:
jump_if_false A // A corresponds to an appropriate value to reach the position marked A:
// do something
jump B
A:
// do something else
B:
C:
while (condition)Script:
{
// do something
}
A:The Do-While Loop:
// evaluate condition
jump_if_false B
// do something
jump A
B:
C:
doScript:
{
// do something
} while (condition);
A:As you can imagine, dealing with the low-level jump instructions can be tedious, particularly when figuring out the appropriate index to jump to. A more robust development tool would certainly include a way to label positions in the code, and have jump instructions reference the labels, as I've shown in the examples above. But the idea here was to show that in the appropriate patterns our jump instructions could represent higher-level constructs.
// do something
// evaluate condition
jump_if_true A
Conditional Expressions
In order to drive our new conditional jumps, we need to have instructions that yield conditional results. Instructions performing value or logical comparison make the most sense in this case, and most will function much like the instructions implementing binary operations in the earlier section.
Unary:
op_not: performs a logical negation of the top of the stack
Binary:
Each of these compares the top two values on the stack, popping the top and returning an appropriate true/false to the new top.
op_and: performs logical conjunction
op_or: performs logical disjunction
op_equal
op_not_equal
op_greater
op_greater_equal
op_less
op_less_equal
This condition:
(x > 1) && (x <= 8)Can be expressed as:
push_var xWhere 'x' is again a placeholder for a variable index.
push_const 1
greater
push_var x
push_const 8
less_equal
and
Here is code for two of these conditional evaluators:
case op_not:The rest of the instructions are similar to op_and, and can be found in the downloadable source.
_stack.Top() = !_stack.Top();
++_instr;
break;
case op_and:
{
char top = _stack.Top();
_stack.Pop();
_stack.Top() = (_stack.Top() && top);
}
++_instr;
break;
Demonstration
For a final demonstration of what we can now do, we will write a script that calculates the factorial of a particular number of our choosing. I chose to compute a factorial since it is something most readers should be familiar with. I will show how an iterative factorial would be performed as a function in C++, and then show how it can be broken down to be expressed as a script in our bytecode.
Iterative Factorial in C++:
int factorial (unsigned int input)We can begin deconstruction by analyzing the number of variables we will need to track, and in this case it is two (counter and result). The parameter input can be simulated as a constant supplied at the very beginning of our script. We will use index 0 as the counter, and index 1 as the result:
{
// initialize
int counter = input;
int result = 1;
// iterative loop
while (counter > 1)
{
result *= counter;
--counter;
}
// output
return result;
}
// initializeHandling the final output is also an easy section to deconstruct:
push_const Input // the actual value here is mutable
assign 0
push_const 1
assign 1
// outputThe tricky part now comes in while flattening out the iterative loop. However, all we need to do is use the pattern described earlier for simulating a while loop:
push_var 1
output
end
A:The condition to be evaluated is simply a comparison between counter and the value 1:
// evaluate condition
jump_if_false B
// do something
jump A
B:
// counter > 1And the action we are taking within the loop (do something) is multiplying result by counter, assigning the product to result, and then decrementing counter:
push_var 0
push_const 1
greater
// result *= counter;Now we put all of these pieces together, to get this:
push_var 1
push_var 0
multiply
assign 1
// --counter;
push_var 0
push_const 1
subtract
assign 0
push_const Input // the actual value here is mutableThe result is 20 instructions (not counting the labels of course). To finalize the process, we replace A and B by the proper offsets in our list. A corresponds to the 5th instruction, and B corresponds to the 18th (remember not to count A: as an instruction). Using 0 to N-1 index convention, that gives the 5th instruction an offset of 4, and the 18th an offset of 17.
assign 0
push_const 1
assign 1
A:
push_var 0
push_const 1
greater
jump_if_false B
push_var 1
push_var 0
multiply
assign 1
push_var 0
push_const 1
subtract
assign 0
jump A
B:
push_var 1
output
end
A script that outputs the factorial of 4:
push_const 4
assign 0
push_const 1
assign 1
push_var 0
push_const 1
greater
jump_if_false 17
push_var 1
push_var 0
multiply
assign 1
push_var 0
push_const 1
subtract
assign 0
jump 4
push_var 1
output
end
Interesting Results
If you play around with the script's initial input value, you'll come to realize something... It only outputs the correct value for an input of 5 or less! What happened? The reason for this is our stack contains elements of type char, which implies a range of -128 to 127. The factorial of 5 is 120, just barely making the cut. So what if we want to find the factorial of 7? We only have to make a small change.
Behold the power of templates:
class ExecutionThe factorial of 7 now comes out to be 5040, as it should be.
{
. . .
private:
ScopeStack<int> _stack; // initially of type ScopeStack<char>
. . .
};
Conclusion
In my opinion, these have most interesting concepts developed in our low-level implementation so far. Although it is obviously tedious work, scripts can now be written to express many of the simple higher-level concepts we have always taken for granted. I leave the expression of some other high-level constructs (such as functions) as exercises for the reader until next time. The tools for expressing simple functions are here.
Check out the downloadable source for certain small changes in the test program we built last time in order to allow for our new instructions. One change was made to the parser to allow for underscores to be scanned as part of an opcode name. Other changes were made, but due to their trivial nature, they don't change the way the program behaves, and aren't really worth mentioning.
Please give me a piece of your mind in the forum discussion, or by email: glr9940@rit.edu
Download attached article resource


















