22#include < wizer.h>
33#include < stdlib.h>
44#include < stdio.h>
5+ #include < assert.h>
56
67WIZER_DEFAULT_INIT ();
78WEVAL_DEFINE_GLOBALS ();
@@ -37,7 +38,7 @@ struct State {
3738};
3839
3940template <bool Specialized>
40- bool Interpret (const Inst* insts, uint32_t ninsts, State* state) {
41+ uint32_t Interpret (const Inst* insts, uint32_t ninsts, State* state) {
4142 uint32_t pc = 0 ;
4243 uint32_t steps = 0 ;
4344 uint32_t * opstack = state->opstack ;
@@ -57,67 +58,67 @@ bool Interpret(const Inst* insts, uint32_t ninsts, State* state) {
5758 switch (inst->opcode ) {
5859 case PushConst:
5960 if (sp + 1 > OPSTACK_SIZE) {
60- return false ;
61+ return 0 ;
6162 }
6263 opstack[sp++] = inst->imm ;
6364 break ;
6465 case Drop:
6566 if (sp == 0 ) {
66- return false ;
67+ return 0 ;
6768 }
6869 sp--;
6970 break ;
7071 case Dup:
7172 if (sp + 1 > OPSTACK_SIZE) {
72- return false ;
73+ return 0 ;
7374 }
7475 if (sp == 0 ) {
75- return false ;
76+ return 0 ;
7677 }
7778 opstack[sp] = opstack[sp - 1 ];
7879 sp++;
7980 break ;
8081 case GetLocal:
8182 if (sp + 1 > OPSTACK_SIZE) {
82- return false ;
83+ return 0 ;
8384 }
8485 if (inst->imm >= LOCAL_SIZE) {
85- return false ;
86+ return 0 ;
8687 }
8788 opstack[sp++] = locals[inst->imm ];
8889 break ;
8990 case SetLocal:
9091 if (sp == 0 ) {
91- return false ;
92+ return 0 ;
9293 }
9394 if (inst->imm >= LOCAL_SIZE) {
94- return false ;
95+ return 0 ;
9596 }
9697 locals[inst->imm ] = opstack[--sp];
9798 break ;
9899 case Add:
99100 if (sp < 2 ) {
100- return false ;
101+ return 0 ;
101102 }
102103 opstack[sp - 2 ] += opstack[sp - 1 ];
103104 sp--;
104105 break ;
105106 case Sub:
106107 if (sp < 2 ) {
107- return false ;
108+ return 0 ;
108109 }
109110 opstack[sp - 2 ] -= opstack[sp - 1 ];
110111 sp--;
111112 break ;
112113 case Print:
113114 if (sp == 0 ) {
114- return false ;
115+ return 0 ;
115116 }
116117 printf (" %u\n " , opstack[--sp]);
117118 break ;
118119 case Goto:
119120 if (inst->imm >= ninsts) {
120- return false ;
121+ return 0 ;
121122 }
122123 pc = inst->imm ;
123124 if (Specialized) {
@@ -126,10 +127,10 @@ bool Interpret(const Inst* insts, uint32_t ninsts, State* state) {
126127 break ;
127128 case GotoIf:
128129 if (sp == 0 ) {
129- return false ;
130+ return 0 ;
130131 }
131132 if (inst->imm >= ninsts) {
132- return false ;
133+ return 0 ;
133134 }
134135 sp--;
135136 if (opstack[sp] != 0 ) {
@@ -150,22 +151,24 @@ bool Interpret(const Inst* insts, uint32_t ninsts, State* state) {
150151 }
151152
152153 printf (" Exiting after %d steps at PC %d.\n " , steps, pc);
153- return true ;
154+ return steps ;
154155}
155156
157+ static const uint32_t kIters = 10000000 ;
156158Inst prog[] = {
157159 Inst (PushConst, 0 ),
158160 Inst (Dup),
159- Inst (PushConst, 10000000 ),
161+ Inst (PushConst, kIters ),
160162 Inst (Sub),
161163 Inst (GotoIf, 6 ),
162164 Inst (Exit),
163165 Inst (PushConst, 1 ),
164166 Inst (Add),
165167 Inst (Goto, 1 ),
166168};
169+ static const uint32_t kExpectedSteps = 7 *kIters + 6 ;
167170
168- typedef bool (*InterpretFunc)(const Inst* insts, uint32_t ninsts, State* state);
171+ typedef uint32_t (*InterpretFunc)(const Inst* insts, uint32_t ninsts, State* state);
169172
170173WEVAL_DEFINE_TARGET (1 , Interpret<true >);
171174
@@ -177,17 +180,18 @@ struct Func {
177180 Func (const Inst* insts_, uint32_t ninsts_)
178181 : insts(insts_), ninsts(ninsts_), specialized(nullptr ) {
179182 printf (" ctor: ptr %p\n " , &specialized);
180- weval::weval (
183+ auto * req = weval::weval (
181184 &specialized,
182185 &Interpret<true >,
183186 1 ,
184187 0 ,
185188 weval::SpecializeMemory<const Inst*>(insts, ninsts * sizeof (Inst)),
186189 weval::Specialize (ninsts),
187190 weval::Runtime<State*>());
191+ assert (req);
188192 }
189193
190- bool invoke (State* state) {
194+ uint32_t invoke (State* state) {
191195 printf (" Inspecting func ptr at: %p -> %p (size %lu)\n " , &specialized, specialized, sizeof (specialized));
192196 if (specialized) {
193197 printf (" Calling specialized function: %p\n " , specialized);
@@ -201,6 +205,7 @@ Func prog_func(prog, sizeof(prog)/sizeof(Inst));
201205
202206int main (int argc, char ** argv) {
203207 State* state = (State*)calloc (sizeof (State), 1 );
204- prog_func.invoke (state);
208+ uint32_t steps = prog_func.invoke (state);
209+ assert (kExpectedSteps == steps);
205210 fflush (stdout);
206211}
0 commit comments