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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * FSPop
  69. * Pop (discard args array)
  70. * Jump to <return address>
  71. */
  72. L2_OP_RET,
  73. /*
  74. * Put a reference to none at the top of the stack.
  75. * Push 0
  76. */
  77. L2_OP_ALLOC_NONE,
  78. /*
  79. * Allocate an atom from one word; alloc_atom <word>
  80. * Alloc atom <var> from <word>
  81. * Push <var>
  82. */
  83. L2_OP_ALLOC_ATOM,
  84. /*
  85. * Allocate a real from two words; alloc_real <high> <low>
  86. * Alloc real <var> from <high> << 32 | <low>
  87. * Push <var>
  88. */
  89. L2_OP_ALLOC_REAL,
  90. /*
  91. * Allocate a buffer from static data; alloc_buffer_static <length> <offset>
  92. * Alloc buffer <var> with <length> and <offset>
  93. * Push <var>
  94. */
  95. L2_OP_ALLOC_BUFFER_STATIC,
  96. /*
  97. * Allocate an array; <count>
  98. * Pop <count> times
  99. * Alloc array <var>
  100. * Push <var>
  101. */
  102. L2_OP_ALLOC_ARRAY,
  103. /*
  104. * Allocate an integer->value map.
  105. * Alloc namespace <var>
  106. * Push <var>
  107. */
  108. L2_OP_ALLOC_NAMESPACE,
  109. /*
  110. * Allocate a function; alloc_function <pos>
  111. * Alloc function <var> pointing to location <word>
  112. * Push <var>
  113. */
  114. L2_OP_ALLOC_FUNCTION,
  115. /*
  116. * Set a namespace's name to a value; namespace_set <key>
  117. * Read <val>
  118. * Read <ns>
  119. * Assign <val> to <ns[<key>]>
  120. */
  121. L2_OP_NAMESPACE_SET,
  122. /*
  123. * Lookup a value from a namespace; namespace_lookup <key>
  124. * Pop <ns>
  125. * Push <ns[<key>]>
  126. */
  127. L2_OP_NAMESPACE_LOOKUP,
  128. /*
  129. * Look up a value from an array; array_lookup <key>
  130. * Pop <arr>
  131. * Push <arr[<key>]>
  132. */
  133. L2_OP_ARRAY_LOOKUP,
  134. /*
  135. * Set a value in an array; array_set <key>
  136. * Read <val>
  137. * Read <arr>
  138. * Assign <val> to <arr[<key>]>
  139. */
  140. L2_OP_ARRAY_SET,
  141. /*
  142. * Look up a runtime value in an array or object.
  143. * Pop <key>
  144. * Pop <container>
  145. * Push <container[<key>]>
  146. */
  147. L2_OP_DYNAMIC_LOOKUP,
  148. /*
  149. * Set a value in an array or object.
  150. * Pop <val>
  151. * Pop <key>
  152. * Pop <arr>
  153. * Assign <val> to <arr[<key>]>
  154. */
  155. L2_OP_DYNAMIC_SET,
  156. /*
  157. * Halt execution.
  158. */
  159. L2_OP_HALT,
  160. };
  161. #endif