funct.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "cc.h"
  2. typedef struct Ftab Ftab;
  3. struct Ftab
  4. {
  5. char op;
  6. char* name;
  7. char typ;
  8. };
  9. typedef struct Gtab Gtab;
  10. struct Gtab
  11. {
  12. char etype;
  13. char* name;
  14. };
  15. Ftab ftabinit[OEND];
  16. Gtab gtabinit[NTYPE];
  17. int
  18. isfunct(Node *n)
  19. {
  20. Type *t, *t1;
  21. Funct *f;
  22. Node *l;
  23. Sym *s;
  24. int o;
  25. o = n->op;
  26. if(n->left == Z)
  27. goto no;
  28. t = n->left->type;
  29. if(t == T)
  30. goto no;
  31. f = t->funct;
  32. switch(o) {
  33. case OAS: // put cast on rhs
  34. case OASI:
  35. case OASADD:
  36. case OASAND:
  37. case OASASHL:
  38. case OASASHR:
  39. case OASDIV:
  40. case OASLDIV:
  41. case OASLMOD:
  42. case OASLMUL:
  43. case OASLSHR:
  44. case OASMOD:
  45. case OASMUL:
  46. case OASOR:
  47. case OASSUB:
  48. case OASXOR:
  49. if(n->right == Z)
  50. goto no;
  51. t1 = n->right->type;
  52. if(t1 == T)
  53. goto no;
  54. if(t1->funct == f)
  55. break;
  56. l = new(OXXX, Z, Z);
  57. *l = *n->right;
  58. n->right->left = l;
  59. n->right->right = Z;
  60. n->right->type = t;
  61. n->right->op = OCAST;
  62. if(!isfunct(n->right))
  63. prtree(n, "isfunc !");
  64. break;
  65. case OCAST: // t f(T) or T f(t)
  66. t1 = n->type;
  67. if(t1 == T)
  68. goto no;
  69. if(f != nil) {
  70. s = f->castfr[t1->etype];
  71. if(s == S)
  72. goto no;
  73. n->right = n->left;
  74. goto build;
  75. }
  76. f = t1->funct;
  77. if(f != nil) {
  78. s = f->castto[t->etype];
  79. if(s == S)
  80. goto no;
  81. n->right = n->left;
  82. goto build;
  83. }
  84. goto no;
  85. }
  86. if(f == nil)
  87. goto no;
  88. s = f->sym[o];
  89. if(s == S)
  90. goto no;
  91. /*
  92. * the answer is yes,
  93. * now we rewrite the node
  94. * and give diagnostics
  95. */
  96. switch(o) {
  97. default:
  98. diag(n, "isfunct op missing %O\n", o);
  99. goto bad;
  100. case OADD: // T f(T, T)
  101. case OAND:
  102. case OASHL:
  103. case OASHR:
  104. case ODIV:
  105. case OLDIV:
  106. case OLMOD:
  107. case OLMUL:
  108. case OLSHR:
  109. case OMOD:
  110. case OMUL:
  111. case OOR:
  112. case OSUB:
  113. case OXOR:
  114. case OEQ: // int f(T, T)
  115. case OGE:
  116. case OGT:
  117. case OHI:
  118. case OHS:
  119. case OLE:
  120. case OLO:
  121. case OLS:
  122. case OLT:
  123. case ONE:
  124. if(n->right == Z)
  125. goto bad;
  126. t1 = n->right->type;
  127. if(t1 == T)
  128. goto bad;
  129. if(t1->funct != f)
  130. goto bad;
  131. n->right = new(OLIST, n->left, n->right);
  132. break;
  133. case OAS: // structure copies done by the compiler
  134. case OASI:
  135. goto no;
  136. case OASADD: // T f(T*, T)
  137. case OASAND:
  138. case OASASHL:
  139. case OASASHR:
  140. case OASDIV:
  141. case OASLDIV:
  142. case OASLMOD:
  143. case OASLMUL:
  144. case OASLSHR:
  145. case OASMOD:
  146. case OASMUL:
  147. case OASOR:
  148. case OASSUB:
  149. case OASXOR:
  150. if(n->right == Z)
  151. goto bad;
  152. t1 = n->right->type;
  153. if(t1 == T)
  154. goto bad;
  155. if(t1->funct != f)
  156. goto bad;
  157. n->right = new(OLIST, new(OADDR, n->left, Z), n->right);
  158. break;
  159. case OPOS: // T f(T)
  160. case ONEG:
  161. case ONOT:
  162. case OCOM:
  163. n->right = n->left;
  164. break;
  165. }
  166. build:
  167. l = new(ONAME, Z, Z);
  168. l->sym = s;
  169. l->type = s->type;
  170. l->etype = s->type->etype;
  171. l->xoffset = s->offset;
  172. l->class = s->class;
  173. tcomo(l, 0);
  174. n->op = OFUNC;
  175. n->left = l;
  176. n->type = l->type->link;
  177. if(tcompat(n, T, l->type, tfunct))
  178. goto bad;
  179. if(tcoma(n->left, n->right, l->type->down, 1))
  180. goto bad;
  181. return 1;
  182. no:
  183. return 0;
  184. bad:
  185. diag(n, "cant rewrite typestr for op %O\n", o);
  186. prtree(n, "isfunct");
  187. n->type = T;
  188. return 1;
  189. }
  190. void
  191. dclfunct(Type *t, Sym *s)
  192. {
  193. Funct *f;
  194. Node *n;
  195. Type *f1, *f2, *f3, *f4;
  196. int o, i, c;
  197. char str[100];
  198. if(t->funct)
  199. return;
  200. // recognize generated tag of dorm _%d_
  201. if(t->tag == S)
  202. goto bad;
  203. for(i=0; c = t->tag->name[i]; i++) {
  204. if(c == '_') {
  205. if(i == 0 || t->tag->name[i+1] == 0)
  206. continue;
  207. break;
  208. }
  209. if(c < '0' || c > '9')
  210. break;
  211. }
  212. if(c == 0)
  213. goto bad;
  214. f = alloc(sizeof(*f));
  215. for(o=0; o<sizeof(f->sym); o++)
  216. f->sym[o] = S;
  217. t->funct = f;
  218. f1 = typ(TFUNC, t);
  219. f1->down = copytyp(t);
  220. f1->down->down = t;
  221. f2 = typ(TFUNC, types[TINT]);
  222. f2->down = copytyp(t);
  223. f2->down->down = t;
  224. f3 = typ(TFUNC, t);
  225. f3->down = typ(TIND, t);
  226. f3->down->down = t;
  227. f4 = typ(TFUNC, t);
  228. f4->down = t;
  229. for(i=0;; i++) {
  230. o = ftabinit[i].op;
  231. if(o == OXXX)
  232. break;
  233. sprint(str, "%s_%s_", t->tag->name, ftabinit[i].name);
  234. n = new(ONAME, Z, Z);
  235. n->sym = slookup(str);
  236. f->sym[o] = n->sym;
  237. switch(ftabinit[i].typ) {
  238. default:
  239. diag(Z, "dclfunct op missing %d\n", ftabinit[i].typ);
  240. break;
  241. case 1: // T f(T,T) +
  242. dodecl(xdecl, CEXTERN, f1, n);
  243. break;
  244. case 2: // int f(T,T) ==
  245. dodecl(xdecl, CEXTERN, f2, n);
  246. break;
  247. case 3: // void f(T*,T) +=
  248. dodecl(xdecl, CEXTERN, f3, n);
  249. break;
  250. case 4: // T f(T) ~
  251. dodecl(xdecl, CEXTERN, f4, n);
  252. break;
  253. }
  254. }
  255. for(i=0;; i++) {
  256. o = gtabinit[i].etype;
  257. if(o == TXXX)
  258. break;
  259. /*
  260. * OCAST types T1 _T2_T1_(T2)
  261. */
  262. sprint(str, "_%s%s_", gtabinit[i].name, t->tag->name);
  263. n = new(ONAME, Z, Z);
  264. n->sym = slookup(str);
  265. f->castto[o] = n->sym;
  266. f1 = typ(TFUNC, t);
  267. f1->down = types[o];
  268. dodecl(xdecl, CEXTERN, f1, n);
  269. sprint(str, "%s_%s_", t->tag->name, gtabinit[i].name);
  270. n = new(ONAME, Z, Z);
  271. n->sym = slookup(str);
  272. f->castfr[o] = n->sym;
  273. f1 = typ(TFUNC, types[o]);
  274. f1->down = t;
  275. dodecl(xdecl, CEXTERN, f1, n);
  276. }
  277. return;
  278. bad:
  279. diag(Z, "dclfunct bad %T %s\n", t, s->name);
  280. }
  281. Gtab gtabinit[NTYPE] =
  282. {
  283. TCHAR, "c",
  284. TUCHAR, "uc",
  285. TSHORT, "h",
  286. TUSHORT, "uh",
  287. TINT, "i",
  288. TUINT, "ui",
  289. TLONG, "l",
  290. TULONG, "ul",
  291. TVLONG, "v",
  292. TUVLONG, "uv",
  293. TFLOAT, "f",
  294. TDOUBLE, "d",
  295. TXXX
  296. };
  297. Ftab ftabinit[OEND] =
  298. {
  299. OADD, "add", 1,
  300. OAND, "and", 1,
  301. OASHL, "ashl", 1,
  302. OASHR, "ashr", 1,
  303. ODIV, "div", 1,
  304. OLDIV, "ldiv", 1,
  305. OLMOD, "lmod", 1,
  306. OLMUL, "lmul", 1,
  307. OLSHR, "lshr", 1,
  308. OMOD, "mod", 1,
  309. OMUL, "mul", 1,
  310. OOR, "or", 1,
  311. OSUB, "sub", 1,
  312. OXOR, "xor", 1,
  313. OEQ, "eq", 2,
  314. OGE, "ge", 2,
  315. OGT, "gt", 2,
  316. OHI, "hi", 2,
  317. OHS, "hs", 2,
  318. OLE, "le", 2,
  319. OLO, "lo", 2,
  320. OLS, "ls", 2,
  321. OLT, "lt", 2,
  322. ONE, "ne", 2,
  323. OASADD, "asadd", 3,
  324. OASAND, "asand", 3,
  325. OASASHL, "asashl", 3,
  326. OASASHR, "asashr", 3,
  327. OASDIV, "asdiv", 3,
  328. OASLDIV, "asldiv", 3,
  329. OASLMOD, "aslmod", 3,
  330. OASLMUL, "aslmul", 3,
  331. OASLSHR, "aslshr", 3,
  332. OASMOD, "asmod", 3,
  333. OASMUL, "asmul", 3,
  334. OASOR, "asor", 3,
  335. OASSUB, "assub", 3,
  336. OASXOR, "asxor", 3,
  337. OPOS, "pos", 4,
  338. ONEG, "neg", 4,
  339. OCOM, "com", 4,
  340. ONOT, "not", 4,
  341. // OPOSTDEC,
  342. // OPOSTINC,
  343. // OPREDEC,
  344. // OPREINC,
  345. OXXX,
  346. };
  347. // Node* nodtestv;
  348. // Node* nodvpp;
  349. // Node* nodppv;
  350. // Node* nodvmm;
  351. // Node* nodmmv;