lisp_cell.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #define CELL_SIZE 1
  20. //CONSTANT CELL_SIZE sizeof(struct cell)
  21. struct cell *free_cells;
  22. struct cell *gc_block_start;
  23. struct cell *top_allocated;
  24. void update_remaining()
  25. {
  26. int count = 0;
  27. struct cell* i = free_cells;
  28. while(NULL != i)
  29. {
  30. count = count + 1;
  31. i = i->cdr;
  32. }
  33. left_to_take = count;
  34. }
  35. struct cell* insert_ordered(struct cell* i, struct cell* list)
  36. {
  37. if(NULL == list)
  38. {
  39. return i;
  40. }
  41. if(i < list)
  42. {
  43. i->cdr = list;
  44. return i;
  45. }
  46. list->cdr = insert_ordered(i, list->cdr);
  47. return list;
  48. }
  49. void reclaim_marked()
  50. {
  51. struct cell* i;
  52. for(i= top_allocated; i >= gc_block_start ; i = i - CELL_SIZE)
  53. {
  54. if(i->type & MARKED)
  55. {
  56. i->type = FREE;
  57. i->car = NULL;
  58. i->cdr = NULL;
  59. i->env = NULL;
  60. free_cells = insert_ordered(i, free_cells);
  61. }
  62. }
  63. }
  64. void relocate_cell(struct cell* current, struct cell* target, struct cell* list)
  65. {
  66. for(; NULL != list; list = list->cdr)
  67. {
  68. if(list->car == current)
  69. {
  70. list->car = target;
  71. }
  72. if(list->cdr == current)
  73. {
  74. list->cdr = target;
  75. }
  76. if(list->env == current)
  77. {
  78. list->env = target;
  79. }
  80. if((list->type & CONS)|| list->type & PROC )
  81. {
  82. relocate_cell(current, target, list->car);
  83. }
  84. }
  85. }
  86. struct cell* pop_cons();
  87. void compact(struct cell* list)
  88. {
  89. struct cell* temp;
  90. for(; NULL != list; list = list->cdr)
  91. {
  92. if((FREE != list->type) && (list > free_cells ))
  93. {
  94. temp = pop_cons();
  95. temp->type = list->type;
  96. temp->car = list->car;
  97. temp->cdr = list->cdr;
  98. temp->env = list->env;
  99. relocate_cell(list, temp, all_symbols);
  100. relocate_cell(list, temp, top_env);
  101. }
  102. if((list->type & CONS)|| list->type & PROC )
  103. {
  104. compact(list->car);
  105. }
  106. }
  107. }
  108. void mark_all_cells()
  109. {
  110. struct cell* i;
  111. for(i= gc_block_start; i < top_allocated; i = i + CELL_SIZE)
  112. {
  113. /* if not in the free list */
  114. if(!(i->type & FREE))
  115. {
  116. /* Mark it */
  117. i->type = i->type | MARKED;
  118. }
  119. }
  120. }
  121. void unmark_cells(struct cell* list, struct cell* stop, int count)
  122. {
  123. if(count > 1) return;
  124. for(; NULL != list; list = list->cdr)
  125. {
  126. if(list == stop) count = count + 1;
  127. list->type = list->type & ~MARKED;
  128. if(list->type & PROC)
  129. {
  130. unmark_cells(list->car, stop, count);
  131. if(NULL != list->env)
  132. {
  133. unmark_cells(list->env, stop, count);
  134. }
  135. }
  136. if(list->type & CONS)
  137. {
  138. unmark_cells(list->car, stop, count);
  139. }
  140. }
  141. }
  142. void garbage_collect()
  143. {
  144. mark_all_cells();
  145. unmark_cells(current, current, 0);
  146. unmark_cells(all_symbols, all_symbols, 0);
  147. unmark_cells(top_env, top_env, 0);
  148. reclaim_marked();
  149. update_remaining();
  150. compact(all_symbols);
  151. compact(top_env);
  152. top_allocated = NULL;
  153. }
  154. void garbage_init(int number_of_cells)
  155. {
  156. gc_block_start = calloc(number_of_cells + 1, sizeof(struct cell));
  157. top_allocated = gc_block_start + number_of_cells;
  158. free_cells = NULL;
  159. garbage_collect();
  160. top_allocated = NULL;
  161. }
  162. struct cell* pop_cons()
  163. {
  164. if(NULL == free_cells)
  165. {
  166. file_print("OOOPS we ran out of cells", stderr);
  167. exit(EXIT_FAILURE);
  168. }
  169. struct cell* i;
  170. i = free_cells;
  171. free_cells = i->cdr;
  172. i->cdr = NULL;
  173. if(i > top_allocated)
  174. {
  175. top_allocated = i;
  176. }
  177. left_to_take = left_to_take - 1;
  178. return i;
  179. }
  180. struct cell* make_cell(int type, struct cell* a, struct cell* b, struct cell* env)
  181. {
  182. struct cell* c = pop_cons();
  183. c->type = type;
  184. c->car = a;
  185. c->cdr = b;
  186. c->env = env;
  187. return c;
  188. }
  189. struct cell* make_int(int a)
  190. {
  191. struct cell* c = pop_cons();
  192. c->type = INT;
  193. c->value = a;
  194. return c;
  195. }
  196. struct cell* make_char(int a)
  197. {
  198. struct cell* c = pop_cons();
  199. c->type = CHAR;
  200. c->value = a;
  201. return c;
  202. }
  203. struct cell* make_string(char* a)
  204. {
  205. struct cell* c = pop_cons();
  206. c->type = STRING;
  207. c->string = a;
  208. return c;
  209. }
  210. struct cell* make_sym(char* name)
  211. {
  212. struct cell* c = pop_cons();
  213. c->type = SYM;
  214. c->string = name;
  215. return c;
  216. }
  217. struct cell* make_cons(struct cell* a, struct cell* b)
  218. {
  219. return make_cell(CONS, a, b, nil);
  220. }
  221. struct cell* make_proc(struct cell* a, struct cell* b, struct cell* env)
  222. {
  223. return make_cell(PROC, a, b, env);
  224. }
  225. struct cell* make_prim(void* fun)
  226. {
  227. struct cell* c = pop_cons();
  228. c->type = PRIMOP;
  229. c->function = fun;
  230. return c;
  231. }