ugl_bc.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. ugl_bc.h
  3. bytecode interpreter
  4. */
  5. #ifndef _UGL_BC_H
  6. #define _UGL_BC_H
  7. #include <stdint.h>
  8. #define BC_STACK_SIZE 64
  9. #define BC_RETURN_STACK_SIZE 6
  10. struct bc_struct
  11. {
  12. uint8_t *code;
  13. uint16_t code_pos;
  14. uint8_t arg_stack_pointer; /* starts at 0 and grows */
  15. uint8_t return_stack_pointer; /* starts at 0 and grows */
  16. uint16_t arg_stack[BC_STACK_SIZE]; /* parameter and return value stack */
  17. uint16_t return_stack[BC_RETURN_STACK_SIZE]; /* return from procedure stack */
  18. };
  19. typedef struct bc_struct bc_t;
  20. typedef void (*bc_buildin_fn)(bc_t *bc);
  21. extern bc_buildin_fn bc_buildin_list[];
  22. /* lower 4 bit: 0, upper 4 bit + next byte --> 12 bit value */
  23. #define BC_CMD_LOAD_12BIT (0x00)
  24. /* lower 4 bit: 1, upper 4 bit + next byte --> 12 bit value */
  25. #define BC_CMD_CALL_BUILDIN (0x01)
  26. /* lower 4 bit: 2, upper 4 bit + next byte --> 12 bit value (toplevel call, return value will be removed) */
  27. #define BC_CMD_CALL_BUILDIN_POP_STACK (0x02)
  28. /* lower 4 bit: 3, upper 4 bit + next byte --> 12 bit relative adr */
  29. #define BC_CMD_BRANCH (0x03)
  30. /* lower 4 bit: 4, upper 4 bit: 0..15, pop 1..16 bytes from the arg stack */
  31. /* only used to remove one byte, so could be moved to different section */
  32. #define BC_CMD_POP_ARG_STACK (0x04)
  33. /* lower 4 bit: 5, upper 4 bit: 0..15, push 1..16 bytes on the arg stack */
  34. /* not used any more */
  35. #define BC_CMD_PUSH_ARG_STACK (0x05)
  36. /* lower 4 bit: 5, upper 4 bit: 0..15, push 1..16 arguments, then two bytes for destination adr*/
  37. #define BC_CMD_CALL_PROCEDURE (0x06)
  38. /* lower 4 bit: 15, upper 4 bit: 0 --> put 0 on stack */
  39. #define BC_CMD_LOAD_0 (0x0f)
  40. /* lower 4 bit: 15, upper 4 bit: 1 --> put 1 on stack */
  41. #define BC_CMD_LOAD_1 (0x1f)
  42. /* lower 4 bit: 15, upper 4 bit: 2 --> but 16 bit value on stack, order: high byte, low byte */
  43. #define BC_CMD_LOAD_16BIT (0x2f)
  44. /* lower 4 bit: 15, upper 4 bit: 3 */
  45. #define BC_CMD_RETURN_FROM_PROCEDURE (0x3f)
  46. /* lower 4 bit: 15, upper 4 bit: 4 --> adr are next 16 bit*/
  47. #define BC_CMD_JUMP_NOT_ZERO (0x4f)
  48. /* lower 4 bit: 15, upper 4 bit: 5 --> adr are next 16 bit*/
  49. #define BC_CMD_JUMP_ZERO (0x05f)
  50. /* lower 4 bit: 15, upper 4 bit: 6 --> adr are next 16 bit */
  51. //#define BC_CMD_CALL_PROCEDURE (0x06f)
  52. /* lower 4 bit: 15, upper 4 bit: 7 --> adr are next 16 bit, third byte are the number of arguments */
  53. //#define BC_CMD_POP_ARG_STACK (0x07f)
  54. void bc_exec(bc_t *bc, uint8_t *code, uint16_t pos);
  55. /*======================================================*/
  56. #endif