lisp_cell.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * stage0 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "lisp.h"
  18. /* Deal with the fact GCC converts the 1 to the size of the structs being iterated over */
  19. #if __GCC__
  20. #define CELL_SIZE 1
  21. #else
  22. //CONSTANT CELL_SIZE sizeof(struct cell)
  23. #define CELL_SIZE sizeof(struct cell)
  24. #endif
  25. struct cell *free_cells;
  26. struct cell *gc_block_start;
  27. struct cell *top_allocated;
  28. void update_remaining()
  29. {
  30. int count = 0;
  31. struct cell* i = free_cells;
  32. while(NULL != i)
  33. {
  34. count = count + 1;
  35. i = i->cdr;
  36. }
  37. left_to_take = count;
  38. }
  39. struct cell* insert_ordered(struct cell* i, struct cell* list)
  40. {
  41. if(NULL == list)
  42. {
  43. return i;
  44. }
  45. if(i < list)
  46. {
  47. i->cdr = list;
  48. return i;
  49. }
  50. list->cdr = insert_ordered(i, list->cdr);
  51. return list;
  52. }
  53. void reclaim_marked()
  54. {
  55. struct cell* i;
  56. for(i= top_allocated; i >= gc_block_start ; i = i - CELL_SIZE)
  57. {
  58. if(i->type & MARKED)
  59. {
  60. i->type = FREE;
  61. i->car = NULL;
  62. i->cdr = NULL;
  63. i->env = NULL;
  64. free_cells = insert_ordered(i, free_cells);
  65. }
  66. }
  67. }
  68. void relocate_cell(struct cell* current, struct cell* target, struct cell* list)
  69. {
  70. for(; NULL != list; list = list->cdr)
  71. {
  72. if(list->car == current)
  73. {
  74. list->car = target;
  75. }
  76. if(list->cdr == current)
  77. {
  78. list->cdr = target;
  79. }
  80. if(list->env == current)
  81. {
  82. list->env = target;
  83. }
  84. if((list->type & CONS)|| list->type & PROC )
  85. {
  86. relocate_cell(current, target, list->car);
  87. }
  88. }
  89. }
  90. struct cell* pop_cons();
  91. void compact(struct cell* list)
  92. {
  93. struct cell* temp;
  94. for(; NULL != list; list = list->cdr)
  95. {
  96. if((FREE != list->type) && (list > free_cells ))
  97. {
  98. temp = pop_cons();
  99. temp->type = list->type;
  100. temp->car = list->car;
  101. temp->cdr = list->cdr;
  102. temp->env = list->env;
  103. relocate_cell(list, temp, all_symbols);
  104. relocate_cell(list, temp, top_env);
  105. }
  106. if((list->type & CONS)|| list->type & PROC )
  107. {
  108. compact(list->car);
  109. }
  110. }
  111. }
  112. void mark_all_cells()
  113. {
  114. struct cell* i;
  115. for(i= gc_block_start; i < top_allocated; i = i + CELL_SIZE)
  116. {
  117. /* if not in the free list */
  118. if(!(i->type & FREE))
  119. {
  120. /* Mark it */
  121. i->type = i->type | MARKED;
  122. }
  123. }
  124. }
  125. void unmark_cells(struct cell* list, struct cell* stop, int count)
  126. {
  127. if(count > 1) return;
  128. for(; NULL != list; list = list->cdr)
  129. {
  130. if(list == stop) count = count + 1;
  131. list->type = list->type & ~MARKED;
  132. if(list->type & PROC)
  133. {
  134. unmark_cells(list->car, stop, count);
  135. if(NULL != list->env)
  136. {
  137. unmark_cells(list->env, stop, count);
  138. }
  139. }
  140. if(list->type & CONS)
  141. {
  142. unmark_cells(list->car, stop, count);
  143. }
  144. }
  145. }
  146. void garbage_collect()
  147. {
  148. mark_all_cells();
  149. unmark_cells(current, current, 0);
  150. unmark_cells(all_symbols, all_symbols, 0);
  151. unmark_cells(top_env, top_env, 0);
  152. reclaim_marked();
  153. update_remaining();
  154. compact(all_symbols);
  155. compact(top_env);
  156. top_allocated = NULL;
  157. }
  158. void garbage_init(int number_of_cells)
  159. {
  160. gc_block_start = calloc(number_of_cells + 1, sizeof(struct cell));
  161. top_allocated = gc_block_start + number_of_cells;
  162. free_cells = NULL;
  163. garbage_collect();
  164. top_allocated = NULL;
  165. }
  166. struct cell* pop_cons()
  167. {
  168. if(NULL == free_cells)
  169. {
  170. fputs("OOOPS we ran out of cells", stderr);
  171. exit(EXIT_FAILURE);
  172. }
  173. struct cell* i;
  174. i = free_cells;
  175. free_cells = i->cdr;
  176. i->cdr = NULL;
  177. if(i > top_allocated)
  178. {
  179. top_allocated = i;
  180. }
  181. left_to_take = left_to_take - 1;
  182. return i;
  183. }
  184. struct cell* make_cell(int type, struct cell* a, struct cell* b, struct cell* env)
  185. {
  186. struct cell* c = pop_cons();
  187. c->type = type;
  188. c->car = a;
  189. c->cdr = b;
  190. c->env = env;
  191. return c;
  192. }
  193. struct cell* make_int(int a)
  194. {
  195. struct cell* c = pop_cons();
  196. c->type = INT;
  197. c->value = a;
  198. return c;
  199. }
  200. struct cell* make_char(int a)
  201. {
  202. struct cell* c = pop_cons();
  203. c->type = CHAR;
  204. c->value = a;
  205. return c;
  206. }
  207. struct cell* make_string(char* a)
  208. {
  209. struct cell* c = pop_cons();
  210. c->type = STRING;
  211. c->string = a;
  212. return c;
  213. }
  214. struct cell* make_sym(char* name)
  215. {
  216. struct cell* c = pop_cons();
  217. c->type = SYM;
  218. c->string = name;
  219. return c;
  220. }
  221. struct cell* make_cons(struct cell* a, struct cell* b)
  222. {
  223. return make_cell(CONS, a, b, nil);
  224. }
  225. struct cell* make_proc(struct cell* a, struct cell* b, struct cell* env)
  226. {
  227. return make_cell(PROC, a, b, env);
  228. }
  229. struct cell* make_prim(void* fun)
  230. {
  231. struct cell* c = pop_cons();
  232. c->type = PRIMOP;
  233. c->function = fun;
  234. return c;
  235. }