1
0

stackops.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/common/utils.h"
  3. #include "pocketpy/objects/object.h"
  4. #include "pocketpy/interpreter/vm.h"
  5. py_Ref py_getdict(const py_Ref self, py_Name name){
  6. assert(self && self->is_ptr);
  7. return pk_NameDict__try_get(PyObject__dict(self->_obj), name);
  8. }
  9. void py_setdict(py_Ref self, py_Name name, const py_Ref val){
  10. assert(self && self->is_ptr);
  11. pk_NameDict__set(PyObject__dict(self->_obj), name, *val);
  12. }
  13. py_Ref py_getslot(const py_Ref self, int i){
  14. assert(self && self->is_ptr);
  15. assert(i >= 0 && i < self->_obj->slots);
  16. return PyObject__slots(self->_obj) + i;
  17. }
  18. void py_setslot(py_Ref self, int i, const py_Ref val){
  19. assert(self && self->is_ptr);
  20. assert(i >= 0 && i < self->_obj->slots);
  21. PyObject__slots(self->_obj)[i] = *val;
  22. }
  23. void py_copyref(const py_Ref src, py_Ref dst){
  24. *dst = *src;
  25. }
  26. /* Stack References */
  27. py_Ref py_gettop(){
  28. return pk_current_vm->stack.sp - 1;
  29. }
  30. void py_settop(const py_Ref val){
  31. pk_current_vm->stack.sp[-1] = *val;
  32. }
  33. py_Ref py_getsecond(){
  34. return pk_current_vm->stack.sp - 2;
  35. }
  36. void py_setsecond(const py_Ref val){
  37. pk_current_vm->stack.sp[-2] = *val;
  38. }
  39. py_Ref py_peek(int i){
  40. assert(i < 0);
  41. return pk_current_vm->stack.sp + i;
  42. }
  43. py_Ref py_push(){
  44. pk_VM* vm = pk_current_vm;
  45. py_Ref top = vm->stack.sp;
  46. vm->stack.sp++;
  47. return top;
  48. }
  49. void py_pop(){
  50. pk_VM* vm = pk_current_vm;
  51. vm->stack.sp--;
  52. }
  53. void py_shrink(int n){
  54. pk_VM* vm = pk_current_vm;
  55. vm->stack.sp -= n;
  56. }
  57. void py_pushref(const py_Ref src){
  58. *py_push() = *src;
  59. }
  60. py_Ref py_pushtmp(){
  61. py_Ref r = py_push();
  62. py_newnull(r);
  63. return r;
  64. }
  65. void py_poptmp(int n){
  66. py_shrink(n);
  67. }