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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /* Get the arguments array from the stack frame
  61. * Push <arguments array>
  62. */
  63. L2_OP_STACK_FRAME_GET_ARGS,
  64. /*
  65. * Look up a value from the current stack frame; stack_frame_lookup <key>
  66. * Find <val> in stack frame using <key>
  67. * Push <val>
  68. */
  69. L2_OP_STACK_FRAME_LOOKUP_U4,
  70. L2_OP_STACK_FRAME_LOOKUP_U1,
  71. /*
  72. * Set a value in the current stack frame; stack_frame_set <key>
  73. * Read <val>
  74. * Assign <val> to stack frame at <key>
  75. */
  76. L2_OP_STACK_FRAME_SET_U4,
  77. L2_OP_STACK_FRAME_SET_U1,
  78. /*
  79. * Replace a value on the stack; stack_frame_replace <key>
  80. * Read <val>
  81. * Assign <val> to stack frame at <key>
  82. */
  83. L2_OP_STACK_FRAME_REPLACE_U4,
  84. L2_OP_STACK_FRAME_REPLACE_U1,
  85. /*
  86. * Return from a function.
  87. * Pop <retval>
  88. * FSPop
  89. * Reset stack pointer to <stack base>
  90. * Push <retval>
  91. * Jump to <return address>
  92. */
  93. L2_OP_RET,
  94. /*
  95. * Put a reference to none at the top of the stack.
  96. * Push 0
  97. */
  98. L2_OP_ALLOC_NONE,
  99. /*
  100. * Allocate an atom from one word; alloc_atom <word>
  101. * Alloc atom <var> from <word>
  102. * Push <var>
  103. */
  104. L2_OP_ALLOC_ATOM_U4,
  105. L2_OP_ALLOC_ATOM_U1,
  106. /*
  107. * Allocate a real from two words; alloc_real <double:u8>
  108. * Alloc real <var> from <double>
  109. * Push <var>
  110. */
  111. L2_OP_ALLOC_REAL_D8,
  112. /*
  113. * Allocate a buffer from static data; alloc_buffer_static <length> <offset>
  114. * Alloc buffer <var> with <length> and <offset>
  115. * Push <var>
  116. */
  117. L2_OP_ALLOC_BUFFER_STATIC_U4,
  118. L2_OP_ALLOC_BUFFER_STATIC_U1,
  119. /*
  120. * Allocate an array; <count:u4>
  121. * Pop <count> times
  122. * Alloc array <var>
  123. * Push <var>
  124. */
  125. L2_OP_ALLOC_ARRAY_U4,
  126. L2_OP_ALLOC_ARRAY_U1,
  127. /*
  128. * Allocate an integer->value map.
  129. * Alloc namespace <var>
  130. * Push <var>
  131. */
  132. L2_OP_ALLOC_NAMESPACE,
  133. /*
  134. * Allocate a function; alloc_function <pos>
  135. * Alloc function <var> pointing to location <word>
  136. * Push <var>
  137. */
  138. L2_OP_ALLOC_FUNCTION_U4,
  139. L2_OP_ALLOC_FUNCTION_U1,
  140. /*
  141. * Set a namespace's name to a value; namespace_set <key:u4>
  142. * Read <val>
  143. * Read <ns>
  144. * Assign <val> to <ns[<key>]>
  145. */
  146. L2_OP_NAMESPACE_SET_U4,
  147. L2_OP_NAMESPACE_SET_U1,
  148. /*
  149. * Lookup a value from a namespace; namespace_lookup <key:u4>
  150. * Pop <ns>
  151. * Push <ns[<key>]>
  152. */
  153. L2_OP_NAMESPACE_LOOKUP_U4,
  154. L2_OP_NAMESPACE_LOOKUP_U1,
  155. /*
  156. * Look up a value from an array; array_lookup <key:u4>
  157. * Pop <arr>
  158. * Push <arr[<key>]>
  159. */
  160. L2_OP_ARRAY_LOOKUP_U4,
  161. L2_OP_ARRAY_LOOKUP_U1,
  162. /*
  163. * Set a value in an array; array_set <key>
  164. * Read <val>
  165. * Read <arr>
  166. * Assign <val> to <arr[<key>]>
  167. */
  168. L2_OP_ARRAY_SET_U4,
  169. L2_OP_ARRAY_SET_U1,
  170. /*
  171. * Look up a runtime value in an array or object.
  172. * Pop <key>
  173. * Pop <container>
  174. * Push <container[<key>]>
  175. */
  176. L2_OP_DYNAMIC_LOOKUP,
  177. /*
  178. * Set a value in an array or object.
  179. * Pop <val>
  180. * Pop <key>
  181. * Pop <arr>
  182. * Assign <val> to <arr[<key>]>
  183. */
  184. L2_OP_DYNAMIC_SET,
  185. /*
  186. * Halt execution.
  187. */
  188. L2_OP_HALT,
  189. };
  190. #endif