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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <word>
  41. * Push <iptr> + 1
  42. * Jump to <word>
  43. */
  44. L2_OP_CALL,
  45. /*
  46. * Jump relative.
  47. * Pop <word>
  48. * Jump <word> words forwards
  49. */
  50. L2_OP_RJMP,
  51. /*
  52. * Generate a stack frame.
  53. * Alloc namespace <var>
  54. * NSPush <var>
  55. */
  56. L2_OP_GEN_STACK_FRAME,
  57. /*
  58. * Look up a value from the current stack frame.
  59. * Pop <word>
  60. * Find <val> in stack frame using <word>
  61. * Push <val>
  62. */
  63. L2_OP_STACK_FRAME_LOOKUP,
  64. /*
  65. * Set a value in the current stack frame.
  66. * Pop <key>
  67. * Read <val>
  68. * Assign <val> to stack frame
  69. */
  70. L2_OP_STACK_FRAME_SET,
  71. /*
  72. * Return from a function.
  73. * NSPop
  74. * Pop <word>
  75. * Jump to <word>
  76. */
  77. L2_OP_RET,
  78. /*
  79. * Allocate an integer from one word.
  80. * Pop <word>
  81. * Alloc integer <var> from <word>
  82. * Push <var>
  83. */
  84. L2_OP_ALLOC_INTEGER_32,
  85. /*
  86. * Allocate an integer from two words.
  87. * Pop <word1>
  88. * Pop <word2>
  89. * Alloc integer <var> from <word1> << 32 | <word2>
  90. * Push <var>
  91. */
  92. L2_OP_ALLOC_INTEGER_64,
  93. /*
  94. * Allocate a real from one word.
  95. * Pop <word>
  96. * Alloc real <var> from <word>
  97. * Push <var>
  98. */
  99. L2_OP_ALLOC_REAL_32,
  100. /*
  101. * Allocate a real from two words.
  102. * Pop <high>
  103. * Pop <low>
  104. * Alloc real <var> from <high> << 32 | <low>
  105. * Push <var>
  106. */
  107. L2_OP_ALLOC_REAL_64,
  108. /*
  109. * Allocate a buffer from static data.
  110. * Pop <word1>
  111. * Pop <word2>
  112. * Alloc buffer <var> with length=<word1>, offset=<word2>
  113. * Push <var>
  114. */
  115. L2_OP_ALLOC_BUFFER_STATIC,
  116. /*
  117. * Allocate a zeroed buffer.
  118. * Pop <word>
  119. * Alloc buffer <var> with length=<word>
  120. * Push <var>
  121. */
  122. L2_OP_ALLOC_BUFFER_ZERO,
  123. /*
  124. * Allocate an array.
  125. * Alloc array <var>
  126. * Push <var>
  127. */
  128. L2_OP_ALLOC_ARRAY,
  129. /*
  130. * Allocate an integer->value map.
  131. * Alloc namespace <var>
  132. * Push <var>
  133. */
  134. L2_OP_ALLOC_NAMESPACE,
  135. /*
  136. * Set a namespace's name to a value.
  137. * Pop <key>
  138. * Read <val>
  139. * Read <ns>
  140. * Assign <val> to <ns[<key>]>
  141. */
  142. L2_OP_NAMESPACE_SET,
  143. /*
  144. * Halt execution.
  145. */
  146. L2_OP_HALT,
  147. };
  148. #endif