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

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