1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
* Overview
A compiled sieve program consists of a sequence of cells. Each cell
is a pointer to sieve_op_t data, i.e. it points to an instruction handler
(sieve_instr_t type) or to a memory location containing sieve_value_t
structure. The exact interpretation of this pointer depends on the
value of program counter. The very first entry in a sieve program
(pc = 0) is always a NULL pointer. The next entry (pc = 1) is always
expected to contain a valid instruction handler. When executing a
program, the sieve runtime evaluator starts from pc = 1. It reads
the cell contents, interprets it as address of an instruction handler,
increments the program counter and executes the handler. The evaluator
stops executing the program when it encounters a NULL pointer.
When invoked, the instruction handler receives a single argument: a pointer
to the sieve_machine_t structure, describing current runtime environment.
If the handler needs any surplus arguments, it is its responsibility
to retrieve them from the program cells immediately following the
handler address and increment the pc value accordingly.
* Existing handlers
** Nop
Name: instr_nop
Arguments: None
Does nothing
** Push
Name: instr_push
Arguments: None
Pushes current numeric register on stack.
** Pop
Name: instr_pop
Arguments: None
Pops the top of stack value into the numeric register.
** Unconditional branch
Name: instr_branch
Arguments: [pc ] (number) Offset of the new cell.
** Branch if zero
Name: instr_brz
Arguments: [pc ] (number) Offset of the new cell.
** Branch if not zero
Name: instr_brnz
Arguments: [pc ] (number) Offset of the new cell.
** Logical NOT
Name: instr_not
Arguments: none
** Logical AND
Name: instr_allof
Arguments: [pc ] (number) Number of items to be popped from stack
** Logical OR
Name: instr_anyof
Arguments: [pc ] (number) Number of items to be popped from stack
** Action handler
Name: instr_action
Arguments: [pc ] (sieve_handler_t*) Pointer to the action handling function.
[pc+1] (list_t of sieve_value_t) A list of required arguments.
[pc+2] (list_t of sieve_runtime_tag_t) A list of tags.
[pc+3] (string) Name of the action (for debugging purposes).
** Test handler
Name: instr_test
Arguments: [pc ] (sieve_handler_t*) Pointer to the test handling function.
[pc+1] (list_t of sieve_value_t) A list of required arguments.
[pc+2] (list_t of sieve_runtime_tag_t) A list of tags.
[pc+3] (string) Name of the test (for debugging purposes).
* Conditional statement branching
A simple statement
IF cond list1 ELSE list2
is translated into the following program
# Compute the condition
.
.
.
brz L_ELSE
# Evaluate list1
.
.
.
br L_END
L_ELSE:
# Evaluate list2
.
.
.
L_END:
A more complex statement in the form:
IF cond1 list1 ELSIF cond2 list2 ELSE list3
is translated into the following program
# Compute the condition 1
.
.
.
brz L_ELSIF
# Evaluate list1
.
.
.
br L_END
L_ELSIF:
# Compute the condition 2
.
.
.
brz L_ELSE
# Evaluate list 2
.
.
.
br L_END
L_ELSE:
# Evaluate list 3
.
.
.
L_END:
Generally speaking, each ELSIF branch generates a code, similar to usual
IF ... ELSE sequence.
* Boolean shortcuts
Boolean shortcuts are implemented for coding ALLOF and ANYOF conditions.
The code these produce is shown in the next two subsections. Notice the
insertion of the two Nop statement after the last condition. These replace
the two slots used by the Brz or Brnz instruction, which would be useless,
since the next statement after ALLOF or ANYOF is guaranteed to be a branch
statement. This replacement speeds up the execution a little.
** ALLOF
ALLOF(cond1,cond2,cond3)
# Evaluate cond1
.
.
.
brz L_end
# Evaluate cond2
.
.
.
brz L_end
# Evaluate cond3
.
.
.
nop
nop
L_end:
.
** ANYOF
ALLOF(cond1,cond2,cond3)
# Evaluate cond1
.
.
.
brnz L_end
# Evaluate cond2
.
.
.
brnz L_end
# Evaluate cond3
.
.
.
nop
nop
L_end:
.
Local variables:
mode: outline
paragraph-separate: "[ ]*$"
end:
|