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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * Push a value to the stack.
  12. * Push <word>
  13. */
  14. L2_OP_PUSH,
  15. /*
  16. * Push a value to the stack.
  17. * Push <word1>
  18. * Push <word2>
  19. */
  20. L2_OP_PUSH_2,
  21. /*
  22. * Discard the top element from the stack.
  23. * Pop <word>
  24. */
  25. L2_OP_POP,
  26. /*
  27. * Duplicate the top element on the stack.
  28. * Push <word at <sptr> - 1>
  29. */
  30. L2_OP_DUP,
  31. /*
  32. * Add two words.
  33. * Pop <word1>
  34. * Pop <word2>
  35. * Push <word1> + <word2>
  36. */
  37. L2_OP_ADD,
  38. /*
  39. * Call a function.
  40. * Pop <argc>
  41. * Pop argc times
  42. * Pop <func>
  43. * Push <iptr> + 1
  44. * Push array with args
  45. * Call <func>
  46. */
  47. L2_OP_FUNC_CALL,
  48. /*
  49. * Jump relative.
  50. * Pop <word>
  51. * Jump <word> words forwards
  52. */
  53. L2_OP_RJMP,
  54. /*
  55. * Look up a value from the current stack frame.
  56. * Pop <word>
  57. * Find <val> in stack frame using <word>
  58. * Push <val>
  59. */
  60. L2_OP_STACK_FRAME_LOOKUP,
  61. /*
  62. * Set a value in the current stack frame.
  63. * Pop <key>
  64. * Read <val>
  65. * Assign <val> to stack frame
  66. */
  67. L2_OP_STACK_FRAME_SET,
  68. /*
  69. * Return from a function.
  70. * NSPop
  71. * Pop (discard args array)
  72. * Pop <word>
  73. * Jump to <word>
  74. */
  75. L2_OP_RET,
  76. /*
  77. * Allocate an atom from one word.
  78. * Pop <word>
  79. * Alloc integer <var> from <word>
  80. * Push <var>
  81. */
  82. L2_OP_ALLOC_ATOM,
  83. /*
  84. * Allocate a real from two words.
  85. * Pop <high>
  86. * Pop <low>
  87. * Alloc real <var> from <high> << 32 | <low>
  88. * Push <var>
  89. */
  90. L2_OP_ALLOC_REAL,
  91. /*
  92. * Allocate a buffer from static data.
  93. * Pop <word1>
  94. * Pop <word2>
  95. * Alloc buffer <var> with length=<word1>, offset=<word2>
  96. * Push <var>
  97. */
  98. L2_OP_ALLOC_BUFFER_STATIC,
  99. /*
  100. * Allocate a zeroed buffer.
  101. * Pop <word>
  102. * Alloc buffer <var> with length=<word>
  103. * Push <var>
  104. */
  105. L2_OP_ALLOC_BUFFER_ZERO,
  106. /*
  107. * Allocate an array.
  108. * Alloc array <var>
  109. * Push <var>
  110. */
  111. L2_OP_ALLOC_ARRAY,
  112. /*
  113. * Allocate an integer->value map.
  114. * Alloc namespace <var>
  115. * Push <var>
  116. */
  117. L2_OP_ALLOC_NAMESPACE,
  118. /*
  119. * Allocate a function.
  120. * Pop <word>
  121. * Alloc function <var> pointing to location <word>
  122. * Push <var>
  123. */
  124. L2_OP_ALLOC_FUNCTION,
  125. /*
  126. * Set a namespace's name to a value.
  127. * Pop <key>
  128. * Read <val>
  129. * Read <ns>
  130. * Assign <val> to <ns[<key>]>
  131. */
  132. L2_OP_NAMESPACE_SET,
  133. /*
  134. * Set a namespace's name to a value.
  135. * Pop <key>
  136. * Pop <ns>
  137. * Push <ns[<key>]>
  138. */
  139. L2_OP_NAMESPACE_LOOKUP,
  140. /*
  141. * Halt execution.
  142. */
  143. L2_OP_HALT,
  144. };
  145. #endif