dot.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. Type*
  9. srch(Type *t, char *s)
  10. {
  11. Type *f;
  12. f = 0;
  13. while(t) {
  14. if(strcmp(t->tag->name, s) == 0) {
  15. if(f == 0 || t->depth < f->depth)
  16. f = t;
  17. }
  18. t = t->next;
  19. }
  20. return f;
  21. }
  22. void
  23. odot(Node *n, Node *r)
  24. {
  25. char *s;
  26. Type *t;
  27. Node res;
  28. uvlong addr;
  29. s = n->sym->name;
  30. if(s == 0)
  31. fatal("dodot: no tag");
  32. expr(n->left, &res);
  33. if(res.comt == 0)
  34. error("no type specified for (expr).%s", s);
  35. if(res.type != TINT)
  36. error("pointer must be integer for (expr).%s", s);
  37. t = srch(res.comt, s);
  38. if(t == 0)
  39. error("no tag for (expr).%s", s);
  40. /* Propagate types */
  41. if(t->type)
  42. r->comt = t->type->lt;
  43. addr = res.ival+t->offset;
  44. if(t->fmt == 'a') {
  45. r->op = OCONST;
  46. r->fmt = 'a';
  47. r->type = TINT;
  48. r->ival = addr;
  49. }
  50. else
  51. indir(cormap, addr, t->fmt, r);
  52. }
  53. static Type **tail;
  54. static Lsym *base;
  55. void
  56. buildtype(Node *m, int d)
  57. {
  58. Type *t;
  59. if(m == ZN)
  60. return;
  61. switch(m->op) {
  62. case OLIST:
  63. buildtype(m->left, d);
  64. buildtype(m->right, d);
  65. break;
  66. case OCTRUCT:
  67. buildtype(m->left, d+1);
  68. break;
  69. default:
  70. t = malloc(sizeof(Type));
  71. t->next = 0;
  72. t->depth = d;
  73. t->tag = m->sym;
  74. t->base = base;
  75. t->offset = m->ival;
  76. if(m->left) {
  77. t->type = m->left->sym;
  78. t->fmt = 'a';
  79. }
  80. else {
  81. t->type = 0;
  82. if(m->right)
  83. t->type = m->right->sym;
  84. t->fmt = m->fmt;
  85. }
  86. *tail = t;
  87. tail = &t->next;
  88. }
  89. }
  90. void
  91. defcomplex(Node *tn, Node *m)
  92. {
  93. tail = &tn->sym->lt;
  94. base = tn->sym;
  95. buildtype(m, 0);
  96. }
  97. void
  98. decl(Node *n)
  99. {
  100. Node *l;
  101. Value *v;
  102. Frtype *f;
  103. Lsym *type;
  104. type = n->sym;
  105. if(type->lt == 0)
  106. error("%s is not a complex type", type->name);
  107. l = n->left;
  108. if(l->op == ONAME) {
  109. v = l->sym->v;
  110. v->comt = type->lt;
  111. v->fmt = 'a';
  112. return;
  113. }
  114. /*
  115. * Frame declaration
  116. */
  117. for(f = l->sym->local; f; f = f->next) {
  118. if(f->var == l->left->sym) {
  119. f->type = n->sym->lt;
  120. return;
  121. }
  122. }
  123. f = malloc(sizeof(Frtype));
  124. if(f == 0)
  125. fatal("out of memory");
  126. f->type = type->lt;
  127. f->var = l->left->sym;
  128. f->next = l->sym->local;
  129. l->sym->local = f;
  130. }