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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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:u4>
  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. /*
  45. * Call an infix function
  46. * Pop <rhs>
  47. * Pop <func>
  48. * Pop <lhs>
  49. * Call <func>
  50. * (Before returning, the function will push a return value onto the stack)
  51. */
  52. L2_OP_FUNC_CALL_INFIX,
  53. /*
  54. * Jump relative; rjmp <count:u4>
  55. * Jump <count> words forwards
  56. */
  57. L2_OP_RJMP_U4,
  58. /*
  59. * Look up a value from the current stack frame; stack_frame_lookup <key:u4>
  60. * Find <val> in stack frame using <key>
  61. * Push <val>
  62. */
  63. L2_OP_STACK_FRAME_LOOKUP_U4,
  64. /*
  65. * Set a value in the current stack frame; stack_frame_set <key:u4>
  66. * Read <val>
  67. * Assign <val> to stack frame at <key>
  68. */
  69. L2_OP_STACK_FRAME_SET_U4,
  70. /*
  71. * Replace a value on the stack; stack_frame_replace <key:U4>
  72. * Read <val>
  73. * Assign <val> to stack frame at <key>
  74. */
  75. L2_OP_STACK_FRAME_REPLACE_U4,
  76. /*
  77. * Return from a function.
  78. * Pop <retval>
  79. * FSPop
  80. * Reset stack pointer to <stack base>
  81. * Push <retval>
  82. * Jump to <return address>
  83. */
  84. L2_OP_RET,
  85. /*
  86. * Put a reference to none at the top of the stack.
  87. * Push 0
  88. */
  89. L2_OP_ALLOC_NONE,
  90. /*
  91. * Allocate an atom from one word; alloc_atom <word:u4>
  92. * Alloc atom <var> from <word>
  93. * Push <var>
  94. */
  95. L2_OP_ALLOC_ATOM_U4,
  96. /*
  97. * Allocate a real from two words; alloc_real <double:u8>
  98. * Alloc real <var> from <double>
  99. * Push <var>
  100. */
  101. L2_OP_ALLOC_REAL_D8,
  102. /*
  103. * Allocate a buffer from static data; alloc_buffer_static <length:u4> <offset:u4>
  104. * Alloc buffer <var> with <length> and <offset>
  105. * Push <var>
  106. */
  107. L2_OP_ALLOC_BUFFER_STATIC_U4,
  108. /*
  109. * Allocate an array; <count:u4>
  110. * Pop <count> times
  111. * Alloc array <var>
  112. * Push <var>
  113. */
  114. L2_OP_ALLOC_ARRAY_U4,
  115. /*
  116. * Allocate an integer->value map.
  117. * Alloc namespace <var>
  118. * Push <var>
  119. */
  120. L2_OP_ALLOC_NAMESPACE,
  121. /*
  122. * Allocate a function; alloc_function <pos:u4>
  123. * Alloc function <var> pointing to location <word>
  124. * Push <var>
  125. */
  126. L2_OP_ALLOC_FUNCTION_U4,
  127. /*
  128. * Set a namespace's name to a value; namespace_set <key:u4>
  129. * Read <val>
  130. * Read <ns>
  131. * Assign <val> to <ns[<key>]>
  132. */
  133. L2_OP_NAMESPACE_SET_U4,
  134. /*
  135. * Lookup a value from a namespace; namespace_lookup <key:u4>
  136. * Pop <ns>
  137. * Push <ns[<key>]>
  138. */
  139. L2_OP_NAMESPACE_LOOKUP_U4,
  140. /*
  141. * Look up a value from an array; array_lookup <key:u4>
  142. * Pop <arr>
  143. * Push <arr[<key>]>
  144. */
  145. L2_OP_ARRAY_LOOKUP_U4,
  146. /*
  147. * Set a value in an array; array_set <key>
  148. * Read <val>
  149. * Read <arr>
  150. * Assign <val> to <arr[<key>]>
  151. */
  152. L2_OP_ARRAY_SET,
  153. /*
  154. * Look up a runtime value in an array or object.
  155. * Pop <key>
  156. * Pop <container>
  157. * Push <container[<key>]>
  158. */
  159. L2_OP_DYNAMIC_LOOKUP,
  160. /*
  161. * Set a value in an array or object.
  162. * Pop <val>
  163. * Pop <key>
  164. * Pop <arr>
  165. * Assign <val> to <arr[<key>]>
  166. */
  167. L2_OP_DYNAMIC_SET,
  168. /*
  169. * Halt execution.
  170. */
  171. L2_OP_HALT,
  172. };
  173. #endif