hack.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. How to hack TinyScheme
  2. ----------------------
  3. TinyScheme is easy to learn and modify. It is structured like a
  4. meta-interpreter, only it is written in C. All data are Scheme
  5. objects, which facilitates both understanding/modifying the
  6. code and reifying the interpreter workings.
  7. In place of a dry description, we will pace through the addition
  8. of a useful new datatype: garbage-collected memory blocks.
  9. The interface will be:
  10. (make-block <n> [<fill>]) makes a new block of the specified size
  11. optionally filling it with a specified byte
  12. (block? <obj>)
  13. (block-length <block>)
  14. (block-ref <block> <index>) retrieves byte at location
  15. (block-set! <block> <index> <byte>) modifies byte at location
  16. In the sequel, lines that begin with '>' denote lines to add to the
  17. code. Lines that begin with '|' are just citations of existing code.
  18. Lines that begin with X denote lines to be removed from the code.
  19. First of all, we need to assign a typeid to our new type. Typeids
  20. in TinyScheme are small integers declared in the scheme_types enum
  21. located near the top of the scheme.c file; it begins with T_STRING.
  22. Add a new entry at the end, say T_MEMBLOCK. Remember to adjust the
  23. value of T_LAST_SYTEM_TYPE when adding new entries. There can be at
  24. most 31 types, but you don't have to worry about that limit yet.
  25. | T_ENVIRONMENT=14,
  26. X T_LAST_SYSTEM_TYPE=14
  27. > T_MEMBLOCK=15,
  28. > T_LAST_SYSTEM_TYPE=15
  29. | };
  30. Then, some helper macros would be useful. Go to where is_string()
  31. and the rest are defined and add:
  32. > INTERFACE INLINE int is_memblock(pointer p) { return (type(p)==T_MEMBLOCK); }
  33. This actually is a function, because it is meant to be exported by
  34. scheme.h. If no foreign function will ever manipulate a memory block,
  35. you can instead define it as a macro:
  36. > #define is_memblock(p) (type(p)==T_MEMBLOCK)
  37. Then we make space for the new type in the main data structure:
  38. struct cell. As it happens, the _string part of the union _object
  39. (that is used to hold character strings) has two fields that suit us:
  40. | struct {
  41. | char *_svalue;
  42. | int _keynum;
  43. | } _string;
  44. We can use _svalue to hold the actual pointer and _keynum to hold its
  45. length. If we couln't reuse existing fields, we could always add other
  46. alternatives in union _object.
  47. We then proceed to write the function that actually makes a new block.
  48. For conformance reasons, we name it mk_memblock
  49. > static pointer mk_memblock(scheme *sc, int len, char fill) {
  50. > pointer x;
  51. > char *p=(char*)sc->malloc(len);
  52. >
  53. > if(p==0) {
  54. > return sc->NIL;
  55. > }
  56. > x = get_cell(sc, sc->NIL, sc->NIL);
  57. >
  58. > typeflag(x) = T_MEMBLOCK|T_ATOM;
  59. > strvalue(x)=p;
  60. > keynum(x)=len;
  61. > memset(p,fill,len);
  62. > return (x);
  63. > }
  64. The memory used by the MEMBLOCK will have to be freed when the cell
  65. is reclaimed during garbage collection. There is a placeholder for
  66. that staff, function finalize_cell(), currently handling strings only.
  67. | static void finalize_cell(scheme *sc, pointer a) {
  68. | if(is_string(a)) {
  69. | sc->free(strvalue(a));
  70. > } else if(is_memblock(a)) {
  71. > sc->free(strvalue(a));
  72. | } else if(is_port(a)) {
  73. There are no MEMBLOCK literals, so we don't concern ourselves with
  74. the READER part (yet!). We must cater to the PRINTER, though. We
  75. add one case more in atom2str().
  76. | } else if (iscontinuation(l)) {
  77. | p = "#<CONTINUATION>";
  78. > } else if (is_memblock(l)) {
  79. > p = "#<MEMORY BLOCK>";
  80. | } else {
  81. Whenever a MEMBLOCK is displayed, it will look like that.
  82. Now, we must add the interface functions: constructor, predicate,
  83. accessor, modifier. We must in fact create new op-codes for the virtual
  84. machine underlying TinyScheme. Since version 1.30, TinyScheme uses
  85. macros and a single source text to keep the enums and the dispatch table
  86. in sync. The op-codes are defined in the opdefines.h file with one line
  87. for each op-code. The lines in the file have six columns between the
  88. starting _OPDEF( and ending ): A, B, C, D, E, and OP.
  89. Note that this file uses unusually long lines to accomodate all the
  90. information; adjust your editor to handle this.
  91. The purpose of the columns is:
  92. - Column A is the name of the subroutine that handles the op-code.
  93. - Column B is the name of the op-code function.
  94. - Columns C and D are the minimum and maximum number of arguments
  95. that are accepted by the op-code.
  96. - Column E is a set of flags that tells the interpreter the type of
  97. each of the arguments expected by the op-code.
  98. - Column OP is used in the scheme_opcodes enum located in the
  99. scheme-private.h file.
  100. Op-codes are really just tags for a huge C switch, only this switch
  101. is broken up in to a number of different opexe_X functions. The
  102. correspondence is made in table "dispatch_table". There, we assign
  103. the new op-codes to opexe_2, where the equivalent ones for vectors
  104. are situated. We also assign a name for them, and specify the minimum
  105. and maximum arity (number of expected arguments). INF_ARG as a maximum
  106. arity means "unlimited".
  107. For reasons of consistency, we add the new op-codes right after those
  108. for vectors:
  109. | _OP_DEF(opexe_2, "vector-set!", 3, 3, TST_VECTOR TST_NATURAL TST_ANY, OP_VECSET )
  110. > _OP_DEF(opexe_2, "make-block", 1, 2, TST_NATURAL TST_CHAR, OP_MKBLOCK )
  111. > _OP_DEF(opexe_2, "block-length", 1, 1, T_MEMBLOCK, OP_BLOCKLEN )
  112. > _OP_DEF(opexe_2, "block-ref", 2, 2, T_MEMBLOCK TST_NATURAL, OP_BLOCKREF )
  113. > _OP_DEF(opexe_2, "block-set!", 1, 1, T_MEMBLOCK TST_NATURAL TST_CHAR, OP_BLOCKSET )
  114. | _OP_DEF(opexe_3, "not", 1, 1, TST_NONE, OP_NOT )
  115. We add the predicate along with the other predicates in opexe_3:
  116. | _OP_DEF(opexe_3, "vector?", 1, 1, TST_ANY, OP_VECTORP )
  117. > _OP_DEF(opexe_3, "block?", 1, 1, TST_ANY, OP_BLOCKP )
  118. | _OP_DEF(opexe_3, "eq?", 2, 2, TST_ANY, OP_EQ )
  119. All that remains is to write the actual code to do the processing and
  120. add it to the switch statement in opexe_2, after the OP_VECSET case.
  121. > case OP_MKBLOCK: { /* make-block */
  122. > int fill=0;
  123. > int len;
  124. >
  125. > if(!isnumber(car(sc->args))) {
  126. > Error_1(sc,"make-block: not a number:",car(sc->args));
  127. > }
  128. > len=ivalue(car(sc->args));
  129. > if(len<=0) {
  130. > Error_1(sc,"make-block: not positive:",car(sc->args));
  131. > }
  132. >
  133. > if(cdr(sc->args)!=sc->NIL) {
  134. > if(!isnumber(cadr(sc->args)) || ivalue(cadr(sc->args))<0) {
  135. > Error_1(sc,"make-block: not a positive number:",cadr(sc->args));
  136. > }
  137. > fill=charvalue(cadr(sc->args))%255;
  138. > }
  139. > s_return(sc,mk_memblock(sc,len,(char)fill));
  140. > }
  141. >
  142. > case OP_BLOCKLEN: /* block-length */
  143. > if(!ismemblock(car(sc->args))) {
  144. > Error_1(sc,"block-length: not a memory block:",car(sc->args));
  145. > }
  146. > s_return(sc,mk_integer(sc,keynum(car(sc->args))));
  147. >
  148. > case OP_BLOCKREF: { /* block-ref */
  149. > char *str;
  150. > int index;
  151. >
  152. > if(!ismemblock(car(sc->args))) {
  153. > Error_1(sc,"block-ref: not a memory block:",car(sc->args));
  154. > }
  155. > str=strvalue(car(sc->args));
  156. >
  157. > if(cdr(sc->args)==sc->NIL) {
  158. > Error_0(sc,"block-ref: needs two arguments");
  159. > }
  160. > if(!isnumber(cadr(sc->args))) {
  161. > Error_1(sc,"block-ref: not a number:",cadr(sc->args));
  162. > }
  163. > index=ivalue(cadr(sc->args));
  164. >
  165. > if(index<0 || index>=keynum(car(sc->args))) {
  166. > Error_1(sc,"block-ref: out of bounds:",cadr(sc->args));
  167. > }
  168. >
  169. > s_return(sc,mk_integer(sc,str[index]));
  170. > }
  171. >
  172. > case OP_BLOCKSET: { /* block-set! */
  173. > char *str;
  174. > int index;
  175. > int c;
  176. >
  177. > if(!ismemblock(car(sc->args))) {
  178. > Error_1(sc,"block-set!: not a memory block:",car(sc->args));
  179. > }
  180. > if(isimmutable(car(sc->args))) {
  181. > Error_1(sc,"block-set!: unable to alter immutable memory block:",car(sc->args));
  182. > }
  183. > str=strvalue(car(sc->args));
  184. >
  185. > if(cdr(sc->args)==sc->NIL) {
  186. > Error_0(sc,"block-set!: needs three arguments");
  187. > }
  188. > if(!isnumber(cadr(sc->args))) {
  189. > Error_1(sc,"block-set!: not a number:",cadr(sc->args));
  190. > }
  191. > index=ivalue(cadr(sc->args));
  192. > if(index<0 || index>=keynum(car(sc->args))) {
  193. > Error_1(sc,"block-set!: out of bounds:",cadr(sc->args));
  194. > }
  195. >
  196. > if(cddr(sc->args)==sc->NIL) {
  197. > Error_0(sc,"block-set!: needs three arguments");
  198. > }
  199. > if(!isinteger(caddr(sc->args))) {
  200. > Error_1(sc,"block-set!: not an integer:",caddr(sc->args));
  201. > }
  202. > c=ivalue(caddr(sc->args))%255;
  203. >
  204. > str[index]=(char)c;
  205. > s_return(sc,car(sc->args));
  206. > }
  207. Finally, do the same for the predicate in opexe_3.
  208. | case OP_VECTORP: /* vector? */
  209. | s_retbool(is_vector(car(sc->args)));
  210. > case OP_BLOCKP: /* block? */
  211. > s_retbool(is_memblock(car(sc->args)));
  212. | case OP_EQ: /* eq? */