opcodes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * opcodes.c -- part of ZilUtils/ZilAsm
  3. *
  4. * Copyright (C) 2016 Jason Self <j@jxself.org>
  5. *
  6. * Based on ZILF (c) 2010, 2015 Jesse McGrew
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. * SPDX-License-Identifier: GPL-3.0-or-later
  22. */
  23. #include <assert.h>
  24. #include "opcodes.h"
  25. typedef struct {
  26. unsigned opcode;
  27. const char *classic_name;
  28. const char *inform_name;
  29. int minver;
  30. int maxver;
  31. ZOpcode_flags flags;
  32. } Opcode_detailed_info;
  33. static Opcode_detailed_info detailed_opcodes[] = {
  34. { 20, "ADD", "add", 1, 6, Zop_store }, // Add
  35. { 259, "ASHIFT", "art_shift", 5, 6, Zop_store }, // Ashift
  36. { 255, "ASSIGNED?", "check_arg_count", 5, 6, Zop_branch | Zop_indvar }, // Assigned_P
  37. { 9, "BAND", "and", 1, 6, Zop_store }, // Band
  38. { 143, "BCOM", "not", 1, 4, Zop_store }, // Bcom_V1
  39. { 248, "BCOM", "not", 5, 6, Zop_store }, // Bcom_V5
  40. { 8, "BOR", "or", 1, 6, Zop_store }, // Bor
  41. { 7, "BTST", "test", 1, 6, Zop_branch }, // Btst
  42. { 242, "BUFOUT", "buffer_mode", 4, 6, 0 }, // Bufout
  43. { 224, "CALL", "call_vs", 1, 6, Zop_store | Zop_call }, // Call
  44. { 136, "CALL1", "call_1s", 4, 6, Zop_store | Zop_call }, // Call1
  45. { 25, "CALL2", "call_2s", 4, 6, Zop_store | Zop_call }, // Call2
  46. { 185, "CATCH", "catch", 5, 6, Zop_store }, // Catch
  47. { 268, "CHECKU", "check_unicode", 5, 6, Zop_store }, // Checku
  48. { 237, "CLEAR", "erase_window", 4, 6, 0 }, // Clear
  49. { 27, "COLOR", "set_colour", 5, 5, 0 }, // Color_v5
  50. { 27, "COLOR", "set_colour", 6, 6, Zop_varargs }, // Color_v6
  51. { 253, "COPYT", "copy_table", 5, 6, 0 }, // Copyt
  52. { 187, "CRLF", "new_line", 1, 6, 0 }, // Crlf
  53. { 240, "CURGET", "get_cursor", 4, 6, 0 }, // Curget
  54. { 239, "CURSET", "set_cursor", 4, 6, 0 }, // Curset
  55. { 263, "DCLEAR", "erase_picture", 6, 6, 0 }, // Dclear
  56. { 134, "DEC", "dec", 1, 6, Zop_indvar }, // Dec
  57. { 244, "DIRIN", "input_stream", 3, 6, 0 }, // Dir-In
  58. { 243, "DIROUT", "output_stream", 3, 6, 0 }, // Dir-Out
  59. { 261, "DISPLAY", "draw_picture", 6, 6, 0 }, // Display
  60. { 23, "DIV", "div", 1, 6, Zop_store }, // Div
  61. { 4, "DLESS?", "dec_chk", 1, 6, Zop_branch | Zop_indvar }, // Dless_P
  62. // { 271, "ENDMOVE", "ENDMOVE", 5, 6, Zop_store }, // Endmove = 271
  63. { 1, "EQUAL?", "jeq", 1, 6, Zop_branch | Zop_varargs }, // Equal_P
  64. { 238, "ERASE", "erase_line", 4, 6, 0 }, // Erase
  65. { 12, "FCLEAR", "clear_attr", 1, 6, 0 }, // Fclear
  66. { 130, "FIRST?", "get_child", 1, 6, Zop_store | Zop_branch }, // First_P
  67. { 260, "FONT", "set_font", 5, 6, Zop_store }, // Font
  68. { 11, "FSET", "set_attr", 1, 6, 0 }, // Fset
  69. { 10, "FSET?", "test_attr", 1, 6, Zop_branch }, // Fset_P
  70. { 185, "FSTACK", "pop", 1, 4, 0 }, // Fstack_V1
  71. { 277, "FSTACK", "pop_stack", 6, 6, 0 }, // Fstack_V6
  72. { 15, "GET", "loadw", 1, 6, Zop_store }, // Get
  73. { 16, "GETB", "loadb", 1, 6, Zop_store }, // Getb
  74. { 17, "GETP", "get_prop", 1, 6, Zop_store }, // Getp
  75. { 18, "GETPT", "get_prop_addr", 1, 6, Zop_store }, // Getpt
  76. { 3, "GRTR?", "jg", 1, 6, Zop_branch }, // Grtr_P
  77. { 241, "HLIGHT", "set_text_style", 4, 6, 0 }, // Hlight
  78. { 249, "ICALL", "call_vn", 5, 6, Zop_call }, // Icall
  79. { 143, "ICALL1", "call_1n", 5, 6, Zop_call }, // Icall1
  80. { 26, "ICALL2", "call_2n", 5, 6, Zop_call }, // Icall2
  81. { 5, "IGRTR?", "inc_chk", 1, 6, Zop_branch | Zop_indvar }, // Igrtr_P
  82. { 6, "IN?", "jin", 1, 6, Zop_branch }, // In_P
  83. { 133, "INC", "inc", 1, 6, Zop_indvar }, // Inc
  84. { 246, "INPUT", "read_char", 4, 6, Zop_store }, // Input
  85. { 247, "INTBL?", "scan_table", 4, 6, Zop_store | Zop_branch }, // Intbl_P
  86. { 266, "IRESTORE", "restore_undo", 5, 6, Zop_store }, // Irestore
  87. { 265, "ISAVE", "save_undo", 5, 6, Zop_store }, // Isave
  88. { 250, "IXCALL", "call_vn2", 5, 6, Zop_extra | Zop_call }, // Ixcall
  89. { 140, "JUMP", "jump", 1, 6, Zop_label | Zop_term }, // Jump
  90. { 2, "LESS?", "jl", 1, 6, Zop_branch }, // Less_P
  91. { 251, "LEX", "tokenise", 5, 6, 0 }, // Lex
  92. { 131, "LOC", "get_parent", 1, 6, Zop_store }, // Loc
  93. { 264, "MARGIN", "set_margins", 6, 6, 0 }, // Margin
  94. { 283, "MENU", "make_menu", 6, 6, Zop_branch }, // Menu
  95. { 24, "MOD", "mod", 1, 6, Zop_store }, // Mod
  96. { 278, "MOUSE-INFO", "read_mouse", 6, 6, 0 }, // MouseInfo
  97. { 279, "MOUSE-LIMIT", "mouse_window", 6, 6, 0 }, // MouseLimit
  98. { 14, "MOVE", "insert_obj", 1, 6, 0 }, // Move
  99. { 22, "MUL", "mul", 1, 6, Zop_store }, // Mul
  100. { 129, "NEXT?", "get_sibling", 1, 6, Zop_store | Zop_branch }, // Next_P
  101. { 19, "NEXTP", "get_next_prop", 1, 6, Zop_store }, // Nextp
  102. { 180, "NOOP", "nop", 1, 6, 0 }, // Noop
  103. { 191, "ORIGINAL?", "piracy", 5, 6, Zop_branch }, // Original_P
  104. { 262, "PICINF", "picture_data", 6, 6, Zop_branch }, // Picinf
  105. { 284, "PICSET", "picture_table", 6, 6, 0 }, // Picset
  106. { 233, "POP", "pull", 1, 5, 0 }, // Pop_v1
  107. { 233, "POP", "pull", 6, 6, Zop_store }, // Pop_v6
  108. { 141, "PRINT", "print_paddr", 1, 6, 0 }, // Print
  109. { 135, "PRINTB", "print_addr", 1, 6, 0 }, // Printb
  110. { 229, "PRINTC", "print_char", 1, 6, 0 }, // Printc
  111. { 138, "PRINTD", "print_obj", 1, 6, 0 }, // Printd
  112. { 282, "PRINTF", "print_form", 6, 6, 0 }, // Printf
  113. { 178, "PRINTI", "print", 1, 6, Zop_string }, // Printi
  114. // { 267, "PRINTMOVE", "PRINTMOVE", 5, 6, Zop_store }, // Printmove
  115. { 230, "PRINTN", "print_num", 1, 6, 0 }, // Printn
  116. { 179, "PRINTR", "print_ret", 1, 6, Zop_string | Zop_term }, // Printr
  117. { 254, "PRINTT", "print_table", 5, 6, 0 }, // Printt
  118. { 267, "PRINTU", "print_unicode", 5, 6, 0 }, // Printu
  119. { 132, "PTSIZE", "get_prop_len", 1, 6, Zop_store }, // Ptsize
  120. { 232, "PUSH", "push", 1, 6, 0 }, // Push
  121. { 225, "PUT", "storew", 1, 6, 0 }, // Put
  122. { 226, "PUTB", "storeb", 1, 6, 0 }, // Putb
  123. { 227, "PUTP", "put_prop", 1, 6, 0 }, // Putp
  124. { 186, "QUIT", "quit", 1, 6, Zop_term }, // Quit
  125. { 231, "RANDOM", "random", 1, 6, Zop_store }, // Random
  126. { 228, "READ", "sread", 1, 4, 0 }, // Read_v1
  127. { 228, "READ", "aread", 5, 6, Zop_store }, // Read_v5
  128. { 137, "REMOVE", "remove_obj", 1, 6, 0 }, // Remove
  129. { 183, "RESTART", "restart", 1, 6, Zop_term }, // Restart
  130. { 182, "RESTORE", "restore", 1, 3, Zop_branch }, // Restore_v1
  131. { 182, "RESTORE", "restore", 4, 4, Zop_store }, // Restore_v4
  132. { 257, "RESTORE", "restore", 5, 6, Zop_store }, // Restore_V5
  133. { 139, "RETURN", "ret", 1, 6, Zop_term }, // Return
  134. { 177, "RFALSE", "rfalse", 1, 6, Zop_term }, // Rfalse
  135. { 184, "RSTACK", "ret_popped", 1, 6, Zop_term }, // Rstack
  136. // { 268, "RTIME", "RTIME", 5, 6, Zop_store }, // Rtime
  137. { 176, "RTRUE", "rtrue", 1, 6, Zop_term }, // Rtrue
  138. { 181, "SAVE", "save", 1, 3, Zop_branch }, // Save_v1
  139. { 181, "SAVE", "save", 4, 4, Zop_store }, // Save_v4
  140. { 256, "SAVE", "save", 5, 6, Zop_store }, // Save_V5
  141. { 235, "SCREEN", "set_window", 3, 6, 0 }, // Screen
  142. { 276, "SCROLL", "scroll_window", 6, 6, 0 }, // Scroll
  143. // { 269, "SEND", "SEND", 5, 6, Zop_store }, // Send
  144. { 270, "SERVER", "SERVER", 5, 6, Zop_store }, // Server
  145. { 13, "SET", "store", 1, 6, Zop_indvar }, // Set
  146. { 258, "SHIFT", "log_shift", 5, 6, Zop_store }, // Shift
  147. { 245, "SOUND", "sound_effect", 3, 6, 0 }, // Sound
  148. { 234, "SPLIT", "split_window", 3, 6, 0 }, // Split
  149. { 21, "SUB", "sub", 1, 6, Zop_store }, // Sub
  150. { 28, "THROW", "throw", 5, 6, Zop_term }, // Throw
  151. { 188, "USL", "show_status", 1, 3, 0 }, // Usl
  152. { 142, "VALUE", "load", 1, 6, Zop_store | Zop_indvar }, // Value
  153. { 189, "VERIFY", "verify", 3, 6, Zop_branch }, // Verify
  154. { 274, "WINATTR", "window_style", 6, 6, 0 }, // Winattr
  155. { 275, "WINGET", "get_wind_prop", 6, 6, Zop_store }, // Winget
  156. { 272, "WINPOS", "move_window", 6, 6, 0 }, // Winpos
  157. { 281, "WINPUT", "put_wind_prop", 6, 6, 0 }, // Winput
  158. { 273, "WINSIZE", "window_size", 6, 6, 0 }, // Winsize
  159. { 236, "XCALL", "call_vs2", 4, 6, Zop_store | Zop_extra | Zop_call }, // Xcall
  160. { 280, "XPUSH", "push_stack", 6, 6, Zop_branch }, // Xpush
  161. { 128, "ZERO?", "jz", 1, 6, Zop_branch }, // Zero_P
  162. { 252, "ZWSTR", "encode_text", 5, 6, 0 } // Zwstr
  163. };
  164. Symtable *Opcodes;
  165. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  166. void init_opcodes(int version, int inform_syntax)
  167. {
  168. const unsigned maxnamelen = 16;
  169. int n;
  170. if (Opcodes) symtable_destroy(Opcodes);
  171. Opcodes = symtable_create(2 * ARRAY_SIZE(detailed_opcodes), maxnamelen, sizeof(ZOpcode));
  172. assert(Opcodes);
  173. for(n = 0; n < ARRAY_SIZE(detailed_opcodes); n++) {
  174. Opcode_detailed_info *p = &detailed_opcodes[n];
  175. if (version < p->minver) continue;
  176. if (version > p->maxver) continue;
  177. ZOpcode q = { p->opcode, p->flags };
  178. symtable_add(Opcodes, inform_syntax ? p->inform_name : p->classic_name, &q);
  179. }
  180. }