| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <stdlib.h> |
|---|
| 15 | #include <stdarg.h> |
|---|
| 16 | |
|---|
| 17 | #include "NewtErrs.h" |
|---|
| 18 | #include "NewtVM.h" |
|---|
| 19 | #include "NewtBC.h" |
|---|
| 20 | #include "NewtGC.h" |
|---|
| 21 | #include "NewtMem.h" |
|---|
| 22 | #include "NewtEnv.h" |
|---|
| 23 | #include "NewtObj.h" |
|---|
| 24 | #include "NewtFns.h" |
|---|
| 25 | #include "NewtStr.h" |
|---|
| 26 | #include "NewtFile.h" |
|---|
| 27 | #include "NewtIO.h" |
|---|
| 28 | #include "NewtPrint.h" |
|---|
| 29 | #include "NewtNSOF.h" |
|---|
| 30 | #include "NewtPkg.h" |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | typedef void(*instruction_t)(int16_t b); |
|---|
| 35 | typedef void(*simple_instruction_t)(void); |
|---|
| 36 | typedef newtRef(*nvm_func_t)(); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | vm_env_t vm_env; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | #if 0 |
|---|
| 44 | #pragma mark - |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | #define START_LOCALARGS 3 ///< ローカル引数の開始位置 |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | #define BC (vm_env.bc) ///< バイトコード |
|---|
| 54 | #define BCLEN (vm_env.bclen) ///< バイトコード長 |
|---|
| 55 | |
|---|
| 56 | #define CALLSTACK ((vm_reg_t *)vm_env.callstack.stackp) ///< 呼出しスタック |
|---|
| 57 | #define CALLSP (vm_env.callstack.sp) ///< 呼出しスタックのスタックポインタ |
|---|
| 58 | #define EXCPSTACK ((vm_excp_t *)vm_env.excpstack.stackp) ///< 例外スタック |
|---|
| 59 | #define EXCPSP (vm_env.excpstack.sp) ///< 例外スタックのスタックポインタ |
|---|
| 60 | #define CURREXCP (vm_env.currexcp) ///< 現在の例外 |
|---|
| 61 | |
|---|
| 62 | #define REG (vm_env.reg) ///< レジスタ |
|---|
| 63 | #define STACK ((newtRef *)vm_env.stack.stackp) ///< スタック |
|---|
| 64 | |
|---|
| 65 | #define FUNC ((REG).func) ///< 実行中の関数 |
|---|
| 66 | #define PC ((REG).pc) ///< プログラムカウンタ |
|---|
| 67 | #define SP ((REG).sp) ///< スタックポインタ |
|---|
| 68 | #define LOCALS ((REG).locals) ///< ローカルフレーム |
|---|
| 69 | #define RCVR ((REG).rcvr) ///< レシーバ |
|---|
| 70 | #define IMPL ((REG).impl) ///< インプリメンタ |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | #if 0 |
|---|
| 74 | #pragma mark - |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | static newtErr NVMGetExceptionErrCode(newtRefArg r, bool dump); |
|---|
| 81 | static newtRef NVMMakeExceptionFrame(newtRefArg name, newtRefArg data); |
|---|
| 82 | static void NVMClearCurrException(void); |
|---|
| 83 | |
|---|
| 84 | static void NVMSetFn(newtRefArg fn); |
|---|
| 85 | static void NVMNoStackFrameForReturn(void); |
|---|
| 86 | |
|---|
| 87 | static void reg_rewind(int32_t sp); |
|---|
| 88 | static void reg_pop(void); |
|---|
| 89 | static void reg_push(int32_t sp); |
|---|
| 90 | static void reg_save(int32_t sp); |
|---|
| 91 | |
|---|
| 92 | static newtRef stk_pop0(void); |
|---|
| 93 | static newtRef stk_pop(void); |
|---|
| 94 | static void stk_pop_n(int32_t n, newtRef a[]); |
|---|
| 95 | static void stk_remove(uint16_t n); |
|---|
| 96 | static newtRef stk_top(void); |
|---|
| 97 | static void stk_push(newtRefArg value); |
|---|
| 98 | static void stk_push_varg(int argc, va_list ap); |
|---|
| 99 | static void stk_push_array(newtRefVar argArray); |
|---|
| 100 | |
|---|
| 101 | static bool excp_push(newtRefArg sym, newtRefArg pc); |
|---|
| 102 | static void excp_pop(void); |
|---|
| 103 | static vm_excp_t * excp_top(void); |
|---|
| 104 | static void excp_pop_handlers(void); |
|---|
| 105 | |
|---|
| 106 | static newtRef liter_get(int16_t n); |
|---|
| 107 | |
|---|
| 108 | static newtRef iter_new(newtRefArg r, newtRefArg deeply); |
|---|
| 109 | static void iter_next(newtRefArg iter); |
|---|
| 110 | static bool iter_done(newtRefArg iter); |
|---|
| 111 | |
|---|
| 112 | static newtRef NVMMakeArgsArray(uint16_t numArgs); |
|---|
| 113 | static void NVMBindArgs(uint16_t numArgs); |
|---|
| 114 | static void NVMThrowBC(newtErr err, newtRefArg value, int16_t pop, bool push); |
|---|
| 115 | static newtErr NVMFuncCheck(newtRefArg fn, int16_t numArgs); |
|---|
| 116 | static void NVMCallNativeFn(newtRefArg fn, int16_t numArgs); |
|---|
| 117 | static void NVMCallNativeFunc(newtRefArg fn, newtRefArg rcvr, int16_t numArgs); |
|---|
| 118 | static void NVMFuncCall(newtRefArg fn, int16_t numArgs); |
|---|
| 119 | static void NVMMessageSend(newtRefArg impl, newtRefArg receiver, newtRefArg fn, int16_t numArgs); |
|---|
| 120 | |
|---|
| 121 | static newtRef vm_send(int16_t b, newtErr * errP); |
|---|
| 122 | static newtRef vm_resend(int16_t b, newtErr * errP); |
|---|
| 123 | |
|---|
| 124 | static void si_pop(void); |
|---|
| 125 | static void si_dup(void); |
|---|
| 126 | static void si_return(void); |
|---|
| 127 | static void si_pushself(void); |
|---|
| 128 | static void si_set_lex_scope(void); |
|---|
| 129 | static void si_iternext(void); |
|---|
| 130 | static void si_iterdone(void); |
|---|
| 131 | static void si_pop_handlers(void); |
|---|
| 132 | |
|---|
| 133 | static void fn_add(void); |
|---|
| 134 | static void fn_subtract(void); |
|---|
| 135 | static void fn_aref(void); |
|---|
| 136 | static void fn_set_aref(void); |
|---|
| 137 | static void fn_equals(void); |
|---|
| 138 | static void fn_not(void); |
|---|
| 139 | static void fn_not_equals(void); |
|---|
| 140 | static void fn_multiply(void); |
|---|
| 141 | static void fn_divide(void); |
|---|
| 142 | static void fn_div(void); |
|---|
| 143 | static void fn_less_than(void); |
|---|
| 144 | static void fn_greater_than(void); |
|---|
| 145 | static void fn_greater_or_equal(void); |
|---|
| 146 | static void fn_less_or_equal(void); |
|---|
| 147 | static void fn_bit_and(void); |
|---|
| 148 | static void fn_bit_or(void); |
|---|
| 149 | static void fn_bit_not(void); |
|---|
| 150 | static void fn_new_iterator(void); |
|---|
| 151 | static void fn_length(void); |
|---|
| 152 | static void fn_clone(void); |
|---|
| 153 | static void fn_set_class(void); |
|---|
| 154 | static void fn_add_array_slot(void); |
|---|
| 155 | static void fn_stringer(void); |
|---|
| 156 | static void fn_has_path(void); |
|---|
| 157 | static void fn_classof(void); |
|---|
| 158 | |
|---|
| 159 | static void is_dummy(int16_t b); |
|---|
| 160 | static void is_simple_instructions(int16_t b); |
|---|
| 161 | static void is_push(int16_t b); |
|---|
| 162 | static void is_push_constant(int16_t b); |
|---|
| 163 | static void is_call(int16_t b); |
|---|
| 164 | static void is_invoke(int16_t b); |
|---|
| 165 | static void is_send(int16_t b); |
|---|
| 166 | static void is_send_if_defined(int16_t b); |
|---|
| 167 | static void is_resend(int16_t b); |
|---|
| 168 | static void is_resend_if_defined(int16_t b); |
|---|
| 169 | static void is_branch(int16_t b); |
|---|
| 170 | static void is_branch_if_true(int16_t b); |
|---|
| 171 | static void is_branch_if_false(int16_t b); |
|---|
| 172 | static void is_find_var(int16_t b); |
|---|
| 173 | static void is_get_var(int16_t b); |
|---|
| 174 | static void is_make_frame(int16_t b); |
|---|
| 175 | static void is_make_array(int16_t b); |
|---|
| 176 | static void is_get_path(int16_t b); |
|---|
| 177 | static void is_set_path(int16_t b); |
|---|
| 178 | static void is_set_var(int16_t b); |
|---|
| 179 | static void is_find_and_set_var(int16_t b); |
|---|
| 180 | static void is_incr_var(int16_t b); |
|---|
| 181 | static void is_branch_if_loop_not_done(int16_t b); |
|---|
| 182 | static void is_freq_func(int16_t b); |
|---|
| 183 | static void is_new_handlers(int16_t b); |
|---|
| 184 | |
|---|
| 185 | static void NVMDumpInstResult(FILE * f); |
|---|
| 186 | static void NVMDumpInstCode(FILE * f, uint8_t * bc, uint32_t pc, uint16_t len); |
|---|
| 187 | |
|---|
| 188 | static void vm_env_push(vm_env_t * next); |
|---|
| 189 | static void vm_env_pop(void); |
|---|
| 190 | |
|---|
| 191 | static void NVMInitREG(void); |
|---|
| 192 | static void NVMInitSTACK(void); |
|---|
| 193 | static void NVMCleanSTACK(void); |
|---|
| 194 | |
|---|
| 195 | static void NVMInitGlobalFns0(void); |
|---|
| 196 | static void NVMInitGlobalFns1(void); |
|---|
| 197 | static void NVMInitExGlobalFns(void); |
|---|
| 198 | static void NVMInitDebugGlobalFns(void); |
|---|
| 199 | static void NVMInitGlobalFns(void); |
|---|
| 200 | |
|---|
| 201 | static void NVMInitGlobalVars(void); |
|---|
| 202 | |
|---|
| 203 | static void NVMLoop(uint32_t callsp); |
|---|
| 204 | |
|---|
| 205 | static newtRef NVMInterpret2(nps_syntax_node_t * stree, uint32_t numStree, newtErr * errP); |
|---|
| 206 | |
|---|
| 207 | static newtRef NVMFuncCallWithValist(newtRefArg fn, int argc, va_list ap); |
|---|
| 208 | static newtRef NVMMessageSendWithValist(newtRefArg impl, newtRefArg receiver, newtRefArg fn, int argc, va_list ap); |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | static simple_instruction_t simple_instructions[] = |
|---|
| 215 | { |
|---|
| 216 | si_pop, |
|---|
| 217 | si_dup, |
|---|
| 218 | si_return, |
|---|
| 219 | si_pushself, |
|---|
| 220 | si_set_lex_scope, |
|---|
| 221 | si_iternext, |
|---|
| 222 | si_iterdone, |
|---|
| 223 | si_pop_handlers |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | static simple_instruction_t fn_instructions[] = |
|---|
| 229 | { |
|---|
| 230 | fn_add, |
|---|
| 231 | fn_subtract, |
|---|
| 232 | fn_aref, |
|---|
| 233 | fn_set_aref, |
|---|
| 234 | fn_equals, |
|---|
| 235 | fn_not, |
|---|
| 236 | fn_not_equals, |
|---|
| 237 | fn_multiply, |
|---|
| 238 | fn_divide, |
|---|
| 239 | fn_div, |
|---|
| 240 | fn_less_than, |
|---|
| 241 | fn_greater_than, |
|---|
| 242 | fn_greater_or_equal, |
|---|
| 243 | fn_less_or_equal, |
|---|
| 244 | fn_bit_and, |
|---|
| 245 | fn_bit_or, |
|---|
| 246 | fn_bit_not, |
|---|
| 247 | fn_new_iterator, |
|---|
| 248 | fn_length, |
|---|
| 249 | fn_clone, |
|---|
| 250 | fn_set_class, |
|---|
| 251 | fn_add_array_slot, |
|---|
| 252 | fn_stringer, |
|---|
| 253 | fn_has_path, |
|---|
| 254 | fn_classof |
|---|
| 255 | }; |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | static instruction_t is_instructions[] = |
|---|
| 259 | { |
|---|
| 260 | is_simple_instructions, |
|---|
| 261 | is_dummy, |
|---|
| 262 | is_dummy, |
|---|
| 263 | is_push, |
|---|
| 264 | is_push_constant, |
|---|
| 265 | is_call, |
|---|
| 266 | is_invoke, |
|---|
| 267 | is_send, |
|---|
| 268 | is_send_if_defined, |
|---|
| 269 | is_resend, |
|---|
| 270 | is_resend_if_defined, |
|---|
| 271 | is_branch, |
|---|
| 272 | is_branch_if_true, |
|---|
| 273 | is_branch_if_false, |
|---|
| 274 | is_find_var, |
|---|
| 275 | is_get_var, |
|---|
| 276 | is_make_frame, |
|---|
| 277 | is_make_array, |
|---|
| 278 | is_get_path, |
|---|
| 279 | is_set_path, |
|---|
| 280 | is_set_var, |
|---|
| 281 | is_find_and_set_var, |
|---|
| 282 | is_incr_var, |
|---|
| 283 | is_branch_if_loop_not_done, |
|---|
| 284 | is_freq_func, |
|---|
| 285 | is_new_handlers |
|---|
| 286 | }; |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | static char * simple_instruction_names[] = |
|---|
| 290 | { |
|---|
| 291 | "pop", |
|---|
| 292 | "dup", |
|---|
| 293 | "return", |
|---|
| 294 | "push-self", |
|---|
| 295 | "set-lex-scope", |
|---|
| 296 | "iter-next", |
|---|
| 297 | "iter-done", |
|---|
| 298 | "pop-handlers" |
|---|
| 299 | }; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | static char * fn_instruction_names[] = |
|---|
| 303 | { |
|---|
| 304 | "add", |
|---|
| 305 | "subtract", |
|---|
| 306 | "aref", |
|---|
| 307 | "set-aref", |
|---|
| 308 | "equals", |
|---|
| 309 | "not", |
|---|
| 310 | "not-equals", |
|---|
| 311 | "multiply", |
|---|
| 312 | "divide", |
|---|
| 313 | "div", |
|---|
| 314 | "less-than", |
|---|
| 315 | "greater-than", |
|---|
| 316 | "greateror-equal", |
|---|
| 317 | "lessor-equal", |
|---|
| 318 | "bit-and", |
|---|
| 319 | "bit-or", |
|---|
| 320 | "bit-not", |
|---|
| 321 | "new-iterator", |
|---|
| 322 | "length", |
|---|
| 323 | "clone", |
|---|
| 324 | "set-class", |
|---|
| 325 | "add-array-slot", |
|---|
| 326 | "stringer", |
|---|
| 327 | "has-path", |
|---|
| 328 | "class-of" |
|---|
| 329 | }; |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | static char * vm_instruction_names[] = |
|---|
| 333 | { |
|---|
| 334 | "simple-instructions", |
|---|
| 335 | NULL, |
|---|
| 336 | NULL, |
|---|
| 337 | "push", |
|---|
| 338 | "push-constant", |
|---|
| 339 | "call", |
|---|
| 340 | "invoke", |
|---|
| 341 | "send", |
|---|
| 342 | "send-if-defined", |
|---|
| 343 | "resend", |
|---|
| 344 | "resend-if-defined", |
|---|
| 345 | "branch", |
|---|
| 346 | "branch-if-true", |
|---|
| 347 | "branch-if-false", |
|---|
| 348 | "find-var", |
|---|
| 349 | "get-var", |
|---|
| 350 | "make-frame", |
|---|
| 351 | "make-array", |
|---|
| 352 | "get-path", |
|---|
| 353 | "set-path", |
|---|
| 354 | "set-var", |
|---|
| 355 | "find-and-set-var", |
|---|
| 356 | "incr-var", |
|---|
| 357 | "branch-if-loop-not-done", |
|---|
| 358 | "freq-func", |
|---|
| 359 | "new-handlers" |
|---|
| 360 | }; |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | #if 0 |
|---|
| 364 | #pragma mark - |
|---|
| 365 | #endif |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | newtRef NVMSelf(void) |
|---|
| 375 | { |
|---|
| 376 | return RCVR; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | newtRef NVMCurrentFunction(void) |
|---|
| 386 | { |
|---|
| 387 | return FUNC; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | newtRef NVMCurrentImplementor(void) |
|---|
| 397 | { |
|---|
| 398 | return IMPL; |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | bool NVMHasVar(newtRefArg name) |
|---|
| 412 | { |
|---|
| 413 | if (NewtHasLexical(LOCALS, name)) |
|---|
| 414 | return true; |
|---|
| 415 | |
|---|
| 416 | if (NewtHasVariable(RCVR, name)) |
|---|
| 417 | return true; |
|---|
| 418 | |
|---|
| 419 | if (NewtHasGlobalVar(name)) |
|---|
| 420 | return true; |
|---|
| 421 | |
|---|
| 422 | return false; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | newtErr NVMGetExceptionErrCode(newtRefArg r, bool dump) |
|---|
| 436 | { |
|---|
| 437 | newtRefVar err; |
|---|
| 438 | |
|---|
| 439 | if (NewtRefIsNIL(r)) |
|---|
| 440 | return kNErrNone; |
|---|
| 441 | |
|---|
| 442 | if (dump) |
|---|
| 443 | NewtPrintObject(stderr, r); |
|---|
| 444 | |
|---|
| 445 | err = NcGetSlot(r, NSSYM0(error)); |
|---|
| 446 | |
|---|
| 447 | if (NewtRefIsNotNIL(err)) |
|---|
| 448 | return NewtRefToInteger(err); |
|---|
| 449 | |
|---|
| 450 | return kNErrBadExceptionName; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | newtRef NVMMakeExceptionFrame(newtRefArg name, newtRefArg data) |
|---|
| 464 | { |
|---|
| 465 | newtRefVar r; |
|---|
| 466 | |
|---|
| 467 | r = NcMakeFrame(); |
|---|
| 468 | NcSetSlot(r, NSSYM0(name), name); |
|---|
| 469 | |
|---|
| 470 | if (NewtHasSubclass(name, NSSYM0(type.ref))) |
|---|
| 471 | NcSetSlot(r, NSSYM0(data), data); |
|---|
| 472 | else if (NewtHasSubclass(name, NSSYM0(ext.ex.msg))) |
|---|
| 473 | NcSetSlot(r, NSSYM0(message), data); |
|---|
| 474 | else |
|---|
| 475 | NcSetSlot(r, NSSYM0(error), data); |
|---|
| 476 | |
|---|
| 477 | return r; |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | void NVMThrowData(newtRefArg name, newtRefArg data) |
|---|
| 491 | { |
|---|
| 492 | vm_excp_t * excp; |
|---|
| 493 | uint32_t i; |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | NVMClearCurrException(); |
|---|
| 497 | |
|---|
| 498 | CURREXCP = data; |
|---|
| 499 | |
|---|
| 500 | for (i = EXCPSP; 0 < i; i--) |
|---|
| 501 | { |
|---|
| 502 | excp = &EXCPSTACK[i - 1]; |
|---|
| 503 | |
|---|
| 504 | if (NewtHasSubclass(name, excp->sym)) |
|---|
| 505 | { |
|---|
| 506 | reg_rewind(excp->callsp); |
|---|
| 507 | PC = excp->pc; |
|---|
| 508 | return; |
|---|
| 509 | } |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | NVMNoStackFrameForReturn(); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | void NVMThrow(newtRefArg name, newtRefArg data) |
|---|
| 526 | { |
|---|
| 527 | newtRefVar r; |
|---|
| 528 | |
|---|
| 529 | r = NVMMakeExceptionFrame(name, data); |
|---|
| 530 | NVMThrowData(name, r); |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | |
|---|
| 540 | void NVMRethrow(void) |
|---|
| 541 | { |
|---|
| 542 | if (NewtRefIsNotNIL(CURREXCP)) |
|---|
| 543 | { |
|---|
| 544 | newtRefVar currexcp; |
|---|
| 545 | newtRefVar name; |
|---|
| 546 | |
|---|
| 547 | currexcp = CURREXCP; |
|---|
| 548 | name = NcGetSlot(currexcp, NSSYM0(name)); |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | NVMThrowData(name, currexcp); |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | newtRef NVMCurrentException(void) |
|---|
| 563 | { |
|---|
| 564 | return CURREXCP; |
|---|
| 565 | } |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | void NVMClearCurrException(void) |
|---|
| 575 | { |
|---|
| 576 | if (NewtRefIsNotNIL(CURREXCP)) |
|---|
| 577 | { |
|---|
| 578 | excp_pop_handlers(); |
|---|
| 579 | CURREXCP = kNewtRefUnbind; |
|---|
| 580 | } |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | void NVMClearException(void) |
|---|
| 593 | { |
|---|
| 594 | CURREXCP = kNewtRefUnbind; |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | #if 0 |
|---|
| 599 | #pragma mark - |
|---|
| 600 | #endif |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | void NVMSetFn(newtRefArg fn) |
|---|
| 610 | { |
|---|
| 611 | FUNC = fn; |
|---|
| 612 | |
|---|
| 613 | if (NewtRefIsNIL(FUNC) || ! NewtRefIsCodeBlock(FUNC)) |
|---|
| 614 | { |
|---|
| 615 | BC = NULL; |
|---|
| 616 | BCLEN = 0; |
|---|
| 617 | } |
|---|
| 618 | else |
|---|
| 619 | { |
|---|
| 620 | newtRefVar instr; |
|---|
| 621 | |
|---|
| 622 | instr = NcGetSlot(FUNC, NSSYM0(instructions)); |
|---|
| 623 | BC = NewtRefToBinary(instr); |
|---|
| 624 | BCLEN = NewtLength(instr); |
|---|
| 625 | } |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | void NVMNoStackFrameForReturn(void) |
|---|
| 636 | { |
|---|
| 637 | BC = NULL; |
|---|
| 638 | BCLEN = 0; |
|---|
| 639 | CALLSP = 0; |
|---|
| 640 | } |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | #if 0 |
|---|
| 644 | #pragma mark *** 呼出しスタック |
|---|
| 645 | #endif |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | void reg_rewind(int32_t sp) |
|---|
| 655 | { |
|---|
| 656 | if (sp == CALLSP) |
|---|
| 657 | return; |
|---|
| 658 | |
|---|
| 659 | if (sp < CALLSP) |
|---|
| 660 | { |
|---|
| 661 | CALLSP = sp; |
|---|
| 662 | REG = CALLSTACK[CALLSP]; |
|---|
| 663 | NVMSetFn(FUNC); |
|---|
| 664 | } |
|---|
| 665 | else |
|---|
| 666 | { |
|---|
| 667 | NVMNoStackFrameForReturn(); |
|---|
| 668 | } |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | void reg_pop(void) |
|---|
| 679 | { |
|---|
| 680 | reg_rewind(CALLSP - 1); |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | void reg_push(int32_t sp) |
|---|
| 693 | { |
|---|
| 694 | if (! NewtStackExpand(&vm_env.callstack, CALLSP + 1)) |
|---|
| 695 | return; |
|---|
| 696 | |
|---|
| 697 | CALLSTACK[CALLSP] = REG; |
|---|
| 698 | CALLSTACK[CALLSP].sp = sp; |
|---|
| 699 | CALLSP++; |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | |
|---|
| 710 | |
|---|
| 711 | void reg_save(int32_t sp) |
|---|
| 712 | { |
|---|
| 713 | |
|---|
| 714 | reg_push(sp); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | #if 0 |
|---|
| 719 | #pragma mark *** スタック |
|---|
| 720 | #endif |
|---|
| 721 | |
|---|
| 722 | |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | |
|---|
| 727 | newtRef stk_pop0(void) |
|---|
| 728 | { |
|---|
| 729 | newtRefVar x = kNewtRefUnbind; |
|---|
| 730 | |
|---|
| 731 | if (0 < SP) |
|---|
| 732 | { |
|---|
| 733 | SP--; |
|---|
| 734 | x = STACK[SP]; |
|---|
| 735 | } |
|---|
| 736 | |
|---|
| 737 | return x; |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | newtRef stk_pop(void) |
|---|
| 748 | { |
|---|
| 749 | return NcResolveMagicPointer(stk_pop0()); |
|---|
| 750 | } |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | void stk_pop_n(int32_t n, newtRef a[]) |
|---|
| 763 | { |
|---|
| 764 | for (n--; 0 <= n; n--) |
|---|
| 765 | { |
|---|
| 766 | a[n] = stk_pop(); |
|---|
| 767 | } |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | void stk_remove(uint16_t n) |
|---|
| 778 | { |
|---|
| 779 | if (n < SP) |
|---|
| 780 | SP -= n; |
|---|
| 781 | else |
|---|
| 782 | SP = 0; |
|---|
| 783 | } |
|---|
| 784 | |
|---|
| 785 | |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | newtRef stk_top(void) |
|---|
| 793 | { |
|---|
| 794 | if (0 < SP) |
|---|
| 795 | return STACK[SP - 1]; |
|---|
| 796 | else |
|---|
| 797 | return kNewtRefNIL; |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | void stk_push(newtRefArg value) |
|---|
| 810 | { |
|---|
| 811 | if (! NewtStackExpand(&vm_env.stack, SP + 1)) |
|---|
| 812 | return; |
|---|
| 813 | |
|---|
| 814 | STACK[SP] = value; |
|---|
| 815 | SP++; |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | |
|---|
| 820 | |
|---|
| 821 | |
|---|
| 822 | |
|---|
| 823 | |
|---|
| 824 | |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | |
|---|
| 828 | void stk_push_varg(int argc, va_list ap) |
|---|
| 829 | { |
|---|
| 830 | int i; |
|---|
| 831 | |
|---|
| 832 | for (i = 0; i < argc; i++) |
|---|
| 833 | { |
|---|
| 834 | stk_push(va_arg(ap, newtRefArg)); |
|---|
| 835 | } |
|---|
| 836 | } |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | |
|---|
| 842 | |
|---|
| 843 | |
|---|
| 844 | |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | void stk_push_array(newtRefVar argArray) |
|---|
| 848 | { |
|---|
| 849 | int numArgs; |
|---|
| 850 | int i; |
|---|
| 851 | |
|---|
| 852 | numArgs = NewtArrayLength(argArray); |
|---|
| 853 | |
|---|
| 854 | for (i = 0; i < numArgs; i++) |
|---|
| 855 | { |
|---|
| 856 | stk_push(NewtGetArraySlot(argArray, i)); |
|---|
| 857 | } |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | |
|---|
| 861 | #if 0 |
|---|
| 862 | #pragma mark *** 例外ハンドラスタック |
|---|
| 863 | #endif |
|---|
| 864 | |
|---|
| 865 | |
|---|
| 866 | |
|---|
| 867 | |
|---|
| 868 | |
|---|
| 869 | |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | |
|---|
| 873 | |
|---|
| 874 | bool excp_push(newtRefArg sym, newtRefArg pc) |
|---|
| 875 | { |
|---|
| 876 | vm_excp_t * excp; |
|---|
| 877 | |
|---|
| 878 | if (! NewtRefIsSymbol(sym)) |
|---|
| 879 | return false; |
|---|
| 880 | |
|---|
| 881 | if (! NewtRefIsInteger(pc)) |
|---|
| 882 | return false; |
|---|