Advertisement
Guest User

Untitled

a guest
Jun 27th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.52 KB | None | 0 0
  1. /*
  2.  * an attempt is done to construct smaller objects of larger
  3.  * fields whenever it is possible.
  4.  *
  5.  * the vm_slot can contain any value.
  6.  * the vm_slot_array can contain many.
  7.  */
  8. typedef struct
  9. {
  10.     uint64_t data;
  11. } vm_slot;
  12.  
  13. typedef struct
  14. {
  15.     size_t      used;
  16.     size_t      size;
  17.     vm_slot    *slots;
  18. } vm_slot_array;
  19.  
  20. /*
  21.  * everything sitting in heap, accessible as value is an object.
  22.  */
  23. typedef struct
  24. {
  25.     uint64_t    type;
  26.     vm_slot     interface;
  27. } vm_object;
  28.  
  29. typedef enum
  30. {
  31.     vm_t_object = 0,
  32.     vm_t_constant,
  33.     vm_t_string,
  34.     vm_t_arraybuffer,
  35.     vm_t_integer,
  36.     vm_t_double = 8,
  37.     vm_t_dict,
  38.     vm_t_list,
  39.     vm_t_indexing_iterator,
  40.     vm_t_property,
  41.     vm_t_upval,
  42.     vm_t_string_stream,
  43.     vm_t_file_stream,
  44.     vm_t_descriptor,
  45.     vm_t_closure,
  46.     vm_t_stack,
  47.     vm_t_greenlet,
  48.     vm_t_context,
  49.     vm_t_builtin
  50. } vm_object_type;
  51.  
  52. /*
  53.  * strings and arraybuffers have a lot in common.
  54.  * both are buffers, devoid of pointers.
  55.  */
  56. typedef struct
  57. {
  58.     vm_object   object;
  59.     size_t      length;
  60.     char        string[];
  61. } vm_string;
  62.  
  63. typedef struct
  64. {
  65.     vm_object   object;
  66.     size_t      length;
  67.     uint8_t     data[];
  68. } vm_arraybuffer;
  69.  
  70. /*
  71.  * dictionaries and lists
  72.  */
  73. typedef struct
  74. {
  75.     vm_object     object;
  76.     vm_slot_array array;
  77. } vm_dict;
  78.  
  79. typedef struct
  80. {
  81.     vm_object     object;
  82.     vm_slot_array array;
  83. } vm_list;
  84.  
  85. /*
  86.  * everything that has +getitem and .length can be iterated using an indexing iterator.
  87.  */
  88. typedef struct
  89. {
  90.     vm_object   object;
  91.     vm_slot     slot;
  92.     size_t      index;
  93.     size_t      length;
  94. } vm_indexing_iterator;
  95.  
  96. /*
  97.  * there are a lot of objects with property fields, so this is a builtin.
  98.  */
  99. typedef struct
  100. {
  101.     vm_object   object;
  102.     vm_slot     get;
  103.     vm_slot     set;
  104.     vm_slot     call;
  105. } vm_property;
  106.  
  107. /*
  108.  * upvalues. they never see the stack.
  109.  */
  110. typedef struct
  111. {
  112.     vm_object       object;
  113.     vm_slot         slot;
  114. } vm_upval;
  115.  
  116. /*
  117.  * lets say these two belong to posix.
  118.  */
  119. typedef struct
  120. {
  121.     vm_object       object;
  122.     vm_arraybuffer* buffer;
  123.     int             index;
  124. } vm_string_stream;
  125.  
  126. typedef struct
  127. {
  128.     vm_object       object;
  129.     int             fd;
  130. } vm_file_stream;
  131.  
  132. /*
  133.  * virtual machine without functions.. what would it be?
  134.  */
  135. struct vm_descriptor
  136. {
  137.     vm_object       object;
  138.     size_t          argc;
  139.     size_t          valc;
  140.     size_t          nupvalc;    /* how many new upvalues? */
  141.     vm_arraybuffer *upcopy;     /* interpreted as uint16_t internally */
  142.     vm_arraybuffer *program;
  143.     vm_list        *functions;
  144.     vm_list        *constants;
  145. };
  146.  
  147. /*
  148.  * should be immutable after created.
  149.  */
  150. struct vm_closure
  151. {
  152.     vm_object       object;
  153.     vm_descriptor  *desc;
  154.     size_t          count;
  155.     vm_upval       *upvalues[];
  156. };
  157.  
  158. /*
  159.  */
  160. typedef struct
  161. {
  162.     vm_closure     *clos;
  163.     vm_upval      **upvalues;
  164.     vm_slot         self;
  165.     int             base;
  166.     int             top;
  167.     size_t          pc;
  168.     /* alter soon, for C calls */
  169. } vm_frame;
  170.  
  171. typedef struct
  172. {
  173.     size_t      used;
  174.     size_t      size;
  175.     vm_frame   *frames;
  176. } vm_frame_array;
  177.  
  178. /*
  179.  */
  180. typedef struct
  181. {
  182.     int         frame;
  183.     int         type;
  184.     int         base;
  185.     size_t      pc;
  186. } vm_except;
  187.  
  188. typedef struct
  189. {
  190.     size_t      used;
  191.     size_t      size;
  192.     vm_except  *excepts;
  193. } vm_except_array;
  194.  
  195. /*
  196.  *
  197.  */
  198. typedef struct vm_stack vm_stack;
  199.  
  200. struct vm_stack
  201. {
  202.     vm_object       object;
  203.     vm_stack*       parent;
  204.     vm_slot_array   slots;
  205.     vm_frame_array  frames;
  206.     vm_except_array excepts;
  207. };
  208.  
  209. /*
  210.  * whereas stack might be an active generator, greenlets are here for
  211.  * async io without the ills.
  212.  */
  213. typedef struct vm_greenlet vm_greenlet;
  214.  
  215. struct vm_greenlet
  216. {
  217.     vm_object       object;
  218.     vm_greenlet    *parent;
  219.     vm_stack       *stack;
  220. };
  221.  
  222. /*
  223.  * ultimately we have a context.
  224.  */
  225. typedef struct
  226. {
  227.     vm_stack       *stack;
  228.     vm_greenlet    *greenlet;
  229.     jmp_buf         exception_return;
  230.     vm_slot         exception_type;
  231.     vm_slot         exception_value;
  232.     /* vm_list     *exception_traceback; */
  233. } vm_context;
  234.  
  235. /*
  236.  */
  237. typedef struct
  238. {
  239.     const char* name;
  240.     void (*func)(vm_context* ctx);
  241. } vm_builtin;
  242.  
  243. /*
  244.  * might be deprecated, for the VM the nature of the object matters, not it's interface.
  245. struct vm_typespec
  246. {
  247.     size_t   size;
  248.     vm_value interface;
  249.     vm_value (*factory)();
  250. };
  251. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement