list.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <mach.h>
  6. #define Extern extern
  7. #include "acid.h"
  8. static List **tail;
  9. List*
  10. construct(Node *l)
  11. {
  12. List *lh, **save;
  13. save = tail;
  14. lh = 0;
  15. tail = &lh;
  16. build(l);
  17. tail = save;
  18. return lh;
  19. }
  20. int
  21. listlen(List *l)
  22. {
  23. int len;
  24. len = 0;
  25. while(l) {
  26. len++;
  27. l = l->next;
  28. }
  29. return len;
  30. }
  31. void
  32. build(Node *n)
  33. {
  34. List *l;
  35. Node res;
  36. if(n == 0)
  37. return;
  38. switch(n->op) {
  39. case OLIST:
  40. build(n->left);
  41. build(n->right);
  42. return;
  43. default:
  44. expr(n, &res);
  45. l = al(res.type);
  46. l->Store = res.Store;
  47. *tail = l;
  48. tail = &l->next;
  49. }
  50. }
  51. List*
  52. addlist(List *l, List *r)
  53. {
  54. List *f;
  55. if(l == 0)
  56. return r;
  57. for(f = l; f->next; f = f->next)
  58. ;
  59. f->next = r;
  60. return l;
  61. }
  62. void
  63. append(Node *r, Node *list, Node *val)
  64. {
  65. List *l, *f;
  66. l = al(val->type);
  67. l->Store = val->Store;
  68. l->next = 0;
  69. r->op = OCONST;
  70. r->type = TLIST;
  71. if(list->l == 0) {
  72. list->l = l;
  73. r->l = l;
  74. return;
  75. }
  76. for(f = list->l; f->next; f = f->next)
  77. ;
  78. f->next = l;
  79. r->l = list->l;
  80. }
  81. int
  82. listcmp(List *l, List *r)
  83. {
  84. if(l == r)
  85. return 1;
  86. while(l) {
  87. if(r == 0)
  88. return 0;
  89. if(l->type != r->type)
  90. return 0;
  91. switch(l->type) {
  92. case TINT:
  93. if(l->ival != r->ival)
  94. return 0;
  95. break;
  96. case TFLOAT:
  97. if(l->fval != r->fval)
  98. return 0;
  99. break;
  100. case TSTRING:
  101. if(scmp(l->string, r->string) == 0)
  102. return 0;
  103. break;
  104. case TLIST:
  105. if(listcmp(l->l, r->l) == 0)
  106. return 0;
  107. break;
  108. }
  109. l = l->next;
  110. r = r->next;
  111. }
  112. if(l != r)
  113. return 0;
  114. return 1;
  115. }
  116. void
  117. nthelem(List *l, int n, Node *res)
  118. {
  119. if(n < 0)
  120. error("negative index in []");
  121. while(l && n--)
  122. l = l->next;
  123. res->op = OCONST;
  124. if(l == 0) {
  125. res->type = TLIST;
  126. res->l = 0;
  127. return;
  128. }
  129. res->type = l->type;
  130. res->Store = l->Store;
  131. }
  132. void
  133. delete(List *l, int n, Node *res)
  134. {
  135. List **tl;
  136. if(n < 0)
  137. error("negative index in delete");
  138. res->op = OCONST;
  139. res->type = TLIST;
  140. res->l = l;
  141. for(tl = &res->l; l && n--; l = l->next)
  142. tl = &l->next;
  143. if(l == 0)
  144. error("element beyond end of list");
  145. *tl = l->next;
  146. }
  147. List*
  148. listvar(char *s, vlong v)
  149. {
  150. List *l, *tl;
  151. tl = al(TLIST);
  152. l = al(TSTRING);
  153. tl->l = l;
  154. l->fmt = 's';
  155. l->string = strnode(s);
  156. l->next = al(TINT);
  157. l = l->next;
  158. l->fmt = 'X';
  159. l->ival = v;
  160. return tl;
  161. }
  162. static List*
  163. listlocals(Map *map, Symbol *fn, uvlong fp)
  164. {
  165. int i;
  166. uvlong val;
  167. Symbol s;
  168. List **tail, *l2;
  169. l2 = 0;
  170. tail = &l2;
  171. s = *fn;
  172. for(i = 0; localsym(&s, i); i++) {
  173. if(s.class != CAUTO)
  174. continue;
  175. if(s.name[0] == '.')
  176. continue;
  177. if(geta(map, fp-s.value, &val) > 0) {
  178. *tail = listvar(s.name, val);
  179. tail = &(*tail)->next;
  180. }
  181. }
  182. return l2;
  183. }
  184. static List*
  185. listparams(Map *map, Symbol *fn, uvlong fp)
  186. {
  187. int i;
  188. Symbol s;
  189. uvlong v;
  190. List **tail, *l2;
  191. l2 = 0;
  192. tail = &l2;
  193. fp += mach->szaddr; /* skip saved pc */
  194. s = *fn;
  195. for(i = 0; localsym(&s, i); i++) {
  196. if (s.class != CPARAM)
  197. continue;
  198. if(geta(map, fp+s.value, &v) > 0) {
  199. *tail = listvar(s.name, v);
  200. tail = &(*tail)->next;
  201. }
  202. }
  203. return l2;
  204. }
  205. void
  206. trlist(Map *map, uvlong pc, uvlong sp, Symbol *sym)
  207. {
  208. List *q, *l;
  209. static List **tail;
  210. if (tracelist == 0) { /* first time */
  211. tracelist = al(TLIST);
  212. tail = &tracelist;
  213. }
  214. q = al(TLIST);
  215. *tail = q;
  216. tail = &q->next;
  217. l = al(TINT); /* Function address */
  218. q->l = l;
  219. l->ival = sym->value;
  220. l->fmt = 'X';
  221. l->next = al(TINT); /* called from address */
  222. l = l->next;
  223. l->ival = pc;
  224. l->fmt = 'Y';
  225. l->next = al(TLIST); /* make list of params */
  226. l = l->next;
  227. l->l = listparams(map, sym, sp);
  228. l->next = al(TLIST); /* make list of locals */
  229. l = l->next;
  230. l->l = listlocals(map, sym, sp);
  231. }