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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef L2_BYTECODE_H
  2. #define L2_BYTECODE_H
  3. #include <stdint.h>
  4. typedef uint32_t l2_word;
  5. enum l2_opcode {
  6. /*
  7. * Do nothing.
  8. */
  9. L2_OP_NOP,
  10. /*
  11. * Discard the top element from the stack.
  12. * Pop <word>
  13. */
  14. L2_OP_POP,
  15. /*
  16. * Swap the top and second-top elements, then pop the new top element.
  17. * Pop <word1>
  18. * Pop <word2>
  19. * Push <word1>
  20. */
  21. L2_OP_SWAP_POP,
  22. /*
  23. * Duplicate the top element on the stack.
  24. * Push <word at <sptr> - 1>
  25. */
  26. L2_OP_DUP,
  27. /*
  28. * Add two words.
  29. * Pop <word1>
  30. * Pop <word2>
  31. * Push <word1> + <word2>
  32. */
  33. L2_OP_ADD,
  34. /*
  35. * Call a function; func_call <argc>
  36. * Pop <argc> times
  37. * Pop <func>
  38. * Push array with args
  39. * Call <func>
  40. * (Before returning, the function will push a return value onto the stack)
  41. */
  42. L2_OP_FUNC_CALL,
  43. /*
  44. * Jump relative; rjmp <count>
  45. * Jump <count> words forwards
  46. */
  47. L2_OP_RJMP,
  48. /*
  49. * Look up a value from the current stack frame; stack_frame_lookup <key>
  50. * Find <val> in stack frame using <key>
  51. * Push <val>
  52. */
  53. L2_OP_STACK_FRAME_LOOKUP,
  54. /*
  55. * Set a value in the current stack frame; stack_frame_set <key>
  56. * Read <val>
  57. * Assign <val> to stack frame at <key>
  58. */
  59. L2_OP_STACK_FRAME_SET,
  60. /*
  61. * Replace a value on the stack; stack_frame_replace <key>
  62. * Read <val>
  63. * Assign <val> to stack frame at <key>
  64. */
  65. L2_OP_STACK_FRAME_REPLACE,
  66. /*
  67. * Return from a function.
  68. * Pop <retval>
  69. * FSPop
  70. * Reset stack pointer to <stack base>
  71. * Push <retval>
  72. * Jump to <return address>
  73. */
  74. L2_OP_RET,
  75. /*
  76. * Put a reference to none at the top of the stack.
  77. * Push 0
  78. */
  79. L2_OP_ALLOC_NONE,
  80. /*
  81. * Allocate an atom from one word; alloc_atom <word>
  82. * Alloc atom <var> from <word>
  83. * Push <var>
  84. */
  85. L2_OP_ALLOC_ATOM,
  86. /*
  87. * Allocate a real from two words; alloc_real <high> <low>
  88. * Alloc real <var> from <high> << 32 | <low>
  89. * Push <var>
  90. */
  91. L2_OP_ALLOC_REAL,
  92. /*
  93. * Allocate a buffer from static data; alloc_buffer_static <length> <offset>
  94. * Alloc buffer <var> with <length> and <offset>
  95. * Push <var>
  96. */
  97. L2_OP_ALLOC_BUFFER_STATIC,
  98. /*
  99. * Allocate an array; <count>
  100. * Pop <count> times
  101. * Alloc array <var>
  102. * Push <var>
  103. */
  104. L2_OP_ALLOC_ARRAY,
  105. /*
  106. * Allocate an integer->value map.
  107. * Alloc namespace <var>
  108. * Push <var>
  109. */
  110. L2_OP_ALLOC_NAMESPACE,
  111. /*
  112. * Allocate a function; alloc_function <pos>
  113. * Alloc function <var> pointing to location <word>
  114. * Push <var>
  115. */
  116. L2_OP_ALLOC_FUNCTION,
  117. /*
  118. * Set a namespace's name to a value; namespace_set <key>
  119. * Read <val>
  120. * Read <ns>
  121. * Assign <val> to <ns[<key>]>
  122. */
  123. L2_OP_NAMESPACE_SET,
  124. /*
  125. * Lookup a value from a namespace; namespace_lookup <key>
  126. * Pop <ns>
  127. * Push <ns[<key>]>
  128. */
  129. L2_OP_NAMESPACE_LOOKUP,
  130. /*
  131. * Look up a value from an array; array_lookup <key>
  132. * Pop <arr>
  133. * Push <arr[<key>]>
  134. */
  135. L2_OP_ARRAY_LOOKUP,
  136. /*
  137. * Set a value in an array; array_set <key>
  138. * Read <val>
  139. * Read <arr>
  140. * Assign <val> to <arr[<key>]>
  141. */
  142. L2_OP_ARRAY_SET,
  143. /*
  144. * Look up a runtime value in an array or object.
  145. * Pop <key>
  146. * Pop <container>
  147. * Push <container[<key>]>
  148. */
  149. L2_OP_DYNAMIC_LOOKUP,
  150. /*
  151. * Set a value in an array or object.
  152. * Pop <val>
  153. * Pop <key>
  154. * Pop <arr>
  155. * Assign <val> to <arr[<key>]>
  156. */
  157. L2_OP_DYNAMIC_SET,
  158. /*
  159. * Halt execution.
  160. */
  161. L2_OP_HALT,
  162. };
  163. #endif