dot.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ctype.h>
  13. #include <mach.h>
  14. #define Extern extern
  15. #include "acid.h"
  16. Type*
  17. srch(Type *t, char *s)
  18. {
  19. Type *f;
  20. f = 0;
  21. while(t) {
  22. if(strcmp(t->tag->name, s) == 0) {
  23. if(f == 0 || t->depth < f->depth)
  24. f = t;
  25. }
  26. t = t->next;
  27. }
  28. return f;
  29. }
  30. void
  31. odot(Node *n, Node *r)
  32. {
  33. char *s;
  34. Type *t;
  35. Node res;
  36. uint64_t addr;
  37. s = n->sym->name;
  38. if(s == 0)
  39. fatal("dodot: no tag");
  40. expr(n->left, &res);
  41. if(res.comt == 0)
  42. error("no type specified for (expr).%s", s);
  43. if(res.type != TINT)
  44. error("pointer must be integer for (expr).%s", s);
  45. t = srch(res.comt, s);
  46. if(t == 0)
  47. error("no tag for (expr).%s", s);
  48. /* Propagate types */
  49. if(t->type)
  50. r->comt = t->type->lt;
  51. addr = res.ival+t->offset;
  52. if(t->fmt == 'a') {
  53. r->op = OCONST;
  54. r->fmt = 'a';
  55. r->type = TINT;
  56. r->ival = addr;
  57. }
  58. else
  59. indir(cormap, addr, t->fmt, r);
  60. }
  61. static Type **tail;
  62. static Lsym *base;
  63. void
  64. buildtype(Node *m, int d)
  65. {
  66. Type *t;
  67. if(m == ZN)
  68. return;
  69. switch(m->op) {
  70. case OLIST:
  71. buildtype(m->left, d);
  72. buildtype(m->right, d);
  73. break;
  74. case OCTRUCT:
  75. buildtype(m->left, d+1);
  76. break;
  77. default:
  78. t = malloc(sizeof(Type));
  79. t->next = 0;
  80. t->depth = d;
  81. t->tag = m->sym;
  82. t->base = base;
  83. t->offset = m->ival;
  84. if(m->left) {
  85. t->type = m->left->sym;
  86. t->fmt = 'a';
  87. }
  88. else {
  89. t->type = 0;
  90. if(m->right)
  91. t->type = m->right->sym;
  92. t->fmt = m->fmt;
  93. }
  94. *tail = t;
  95. tail = &t->next;
  96. }
  97. }
  98. void
  99. defcomplex(Node *tn, Node *m)
  100. {
  101. tail = &tn->sym->lt;
  102. base = tn->sym;
  103. buildtype(m, 0);
  104. }
  105. void
  106. decl(Node *n)
  107. {
  108. Node *l;
  109. Value *v;
  110. Frtype *f;
  111. Lsym *type;
  112. type = n->sym;
  113. if(type->lt == 0)
  114. error("%s is not a complex type", type->name);
  115. l = n->left;
  116. if(l->op == ONAME) {
  117. v = l->sym->v;
  118. v->comt = type->lt;
  119. v->fmt = 'a';
  120. return;
  121. }
  122. /*
  123. * Frame declaration
  124. */
  125. for(f = l->sym->local; f; f = f->next) {
  126. if(f->var == l->left->sym) {
  127. f->type = n->sym->lt;
  128. return;
  129. }
  130. }
  131. f = malloc(sizeof(Frtype));
  132. if(f == 0)
  133. fatal("out of memory");
  134. f->type = type->lt;
  135. f->var = l->left->sym;
  136. f->next = l->sym->local;
  137. l->sym->local = f;
  138. }