You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bytecode.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifndef L2_BYTECODE_H
  2. #define L2_BYTECODE_H
  3. #include <stdint.h>
  4. typedef uint32_t l2_word;
  5. #define l2_bytecode_version 2
  6. enum l2_opcode {
  7. /*
  8. * Do nothing.
  9. */
  10. L2_OP_NOP,
  11. /*
  12. * Discard the top element from the stack.
  13. * Pop <word>
  14. */
  15. L2_OP_DISCARD,
  16. /*
  17. * Swap the top and second-top elements, then pop the new top element.
  18. * Pop <word1>
  19. * Pop <word2>
  20. * Push <word1>
  21. */
  22. L2_OP_SWAP_DISCARD,
  23. /*
  24. * Duplicate the top element on the stack.
  25. * Push <word at <sptr> - 1>
  26. */
  27. L2_OP_DUP,
  28. /*
  29. * Add two words.
  30. * Pop <word1>
  31. * Pop <word2>
  32. * Push <word1> + <word2>
  33. */
  34. L2_OP_ADD,
  35. /*
  36. * Call a function; func_call <argc>
  37. * Pop <argc> times
  38. * Pop <func>
  39. * Push array with args
  40. * Call <func>
  41. * (Before returning, the function will push a return value onto the stack)
  42. */
  43. L2_OP_FUNC_CALL_U4,
  44. L2_OP_FUNC_CALL_U1,
  45. /*
  46. * Call an infix function
  47. * Pop <rhs>
  48. * Pop <func>
  49. * Pop <lhs>
  50. * Call <func>
  51. * (Before returning, the function will push a return value onto the stack)
  52. */
  53. L2_OP_FUNC_CALL_INFIX,
  54. /*
  55. * Jump relative; rjmp <count>
  56. * Jump <count> words forwards
  57. */
  58. L2_OP_RJMP_U4,
  59. L2_OP_RJMP_U1,
  60. /*
  61. * Look up a value from the current stack frame; stack_frame_lookup <key>
  62. * Find <val> in stack frame using <key>
  63. * Push <val>
  64. */
  65. L2_OP_STACK_FRAME_LOOKUP_U4,
  66. L2_OP_STACK_FRAME_LOOKUP_U1,
  67. /*
  68. * Set a value in the current stack frame; stack_frame_set <key>
  69. * Read <val>
  70. * Assign <val> to stack frame at <key>
  71. */
  72. L2_OP_STACK_FRAME_SET_U4,
  73. L2_OP_STACK_FRAME_SET_U1,
  74. /*
  75. * Replace a value on the stack; stack_frame_replace <key>
  76. * Read <val>
  77. * Assign <val> to stack frame at <key>
  78. */
  79. L2_OP_STACK_FRAME_REPLACE_U4,
  80. L2_OP_STACK_FRAME_REPLACE_U1,
  81. /*
  82. * Return from a function.
  83. * Pop <retval>
  84. * FSPop
  85. * Reset stack pointer to <stack base>
  86. * Push <retval>
  87. * Jump to <return address>
  88. */
  89. L2_OP_RET,
  90. /*
  91. * Put a reference to none at the top of the stack.
  92. * Push 0
  93. */
  94. L2_OP_ALLOC_NONE,
  95. /*
  96. * Allocate an atom from one word; alloc_atom <word>
  97. * Alloc atom <var> from <word>
  98. * Push <var>
  99. */
  100. L2_OP_ALLOC_ATOM_U4,
  101. L2_OP_ALLOC_ATOM_U1,
  102. /*
  103. * Allocate a real from two words; alloc_real <double:u8>
  104. * Alloc real <var> from <double>
  105. * Push <var>
  106. */
  107. L2_OP_ALLOC_REAL_D8,
  108. /*
  109. * Allocate a buffer from static data; alloc_buffer_static <length> <offset>
  110. * Alloc buffer <var> with <length> and <offset>
  111. * Push <var>
  112. */
  113. L2_OP_ALLOC_BUFFER_STATIC_U4,
  114. L2_OP_ALLOC_BUFFER_STATIC_U1,
  115. /*
  116. * Allocate an array; <count:u4>
  117. * Pop <count> times
  118. * Alloc array <var>
  119. * Push <var>
  120. */
  121. L2_OP_ALLOC_ARRAY_U4,
  122. L2_OP_ALLOC_ARRAY_U1,
  123. /*
  124. * Allocate an integer->value map.
  125. * Alloc namespace <var>
  126. * Push <var>
  127. */
  128. L2_OP_ALLOC_NAMESPACE,
  129. /*
  130. * Allocate a function; alloc_function <pos>
  131. * Alloc function <var> pointing to location <word>
  132. * Push <var>
  133. */
  134. L2_OP_ALLOC_FUNCTION_U4,
  135. L2_OP_ALLOC_FUNCTION_U1,
  136. /*
  137. * Set a namespace's name to a value; namespace_set <key:u4>
  138. * Read <val>
  139. * Read <ns>
  140. * Assign <val> to <ns[<key>]>
  141. */
  142. L2_OP_NAMESPACE_SET_U4,
  143. L2_OP_NAMESPACE_SET_U1,
  144. /*
  145. * Lookup a value from a namespace; namespace_lookup <key:u4>
  146. * Pop <ns>
  147. * Push <ns[<key>]>
  148. */
  149. L2_OP_NAMESPACE_LOOKUP_U4,
  150. L2_OP_NAMESPACE_LOOKUP_U1,
  151. /*
  152. * Look up a value from an array; array_lookup <key:u4>
  153. * Pop <arr>
  154. * Push <arr[<key>]>
  155. */
  156. L2_OP_ARRAY_LOOKUP_U4,
  157. L2_OP_ARRAY_LOOKUP_U1,
  158. /*
  159. * Set a value in an array; array_set <key>
  160. * Read <val>
  161. * Read <arr>
  162. * Assign <val> to <arr[<key>]>
  163. */
  164. L2_OP_ARRAY_SET_U4,
  165. L2_OP_ARRAY_SET_U1,
  166. /*
  167. * Look up a runtime value in an array or object.
  168. * Pop <key>
  169. * Pop <container>
  170. * Push <container[<key>]>
  171. */
  172. L2_OP_DYNAMIC_LOOKUP,
  173. /*
  174. * Set a value in an array or object.
  175. * Pop <val>
  176. * Pop <key>
  177. * Pop <arr>
  178. * Assign <val> to <arr[<key>]>
  179. */
  180. L2_OP_DYNAMIC_SET,
  181. /*
  182. * Halt execution.
  183. */
  184. L2_OP_HALT,
  185. };
  186. #endif