expr.c 15 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 int fsize[] =
  9. {
  10. ['A'] 4,
  11. ['B'] 4,
  12. ['C'] 1,
  13. ['D'] 4,
  14. ['F'] 8,
  15. ['G'] 8,
  16. ['O'] 4,
  17. ['Q'] 4,
  18. ['R'] 4,
  19. ['S'] 4,
  20. ['U'] 4,
  21. ['V'] 8,
  22. ['W'] 8,
  23. ['X'] 4,
  24. ['Y'] 8,
  25. ['Z'] 8,
  26. ['a'] 4,
  27. ['b'] 1,
  28. ['c'] 1,
  29. ['d'] 2,
  30. ['f'] 4,
  31. ['g'] 4,
  32. ['o'] 2,
  33. ['q'] 2,
  34. ['r'] 2,
  35. ['s'] 4,
  36. ['u'] 2,
  37. ['x'] 2,
  38. ['3'] 10,
  39. ['8'] 10,
  40. };
  41. int
  42. fmtsize(Value *v)
  43. {
  44. int ret;
  45. switch(v->fmt) {
  46. default:
  47. return fsize[v->fmt];
  48. case 'i':
  49. case 'I':
  50. if(v->type != TINT || machdata == 0)
  51. error("no size for i fmt pointer ++/--");
  52. ret = (*machdata->instsize)(cormap, v->ival);
  53. if(ret < 0) {
  54. ret = (*machdata->instsize)(symmap, v->ival);
  55. if(ret < 0)
  56. error("%r");
  57. }
  58. return ret;
  59. }
  60. }
  61. void
  62. chklval(Node *lp)
  63. {
  64. if(lp->op != ONAME)
  65. error("need l-value");
  66. }
  67. void
  68. olist(Node *n, Node *res)
  69. {
  70. expr(n->left, res);
  71. expr(n->right, res);
  72. }
  73. void
  74. oeval(Node *n, Node *res)
  75. {
  76. expr(n->left, res);
  77. if(res->type != TCODE)
  78. error("bad type for eval");
  79. expr(res->cc, res);
  80. }
  81. void
  82. ocast(Node *n, Node *res)
  83. {
  84. if(n->sym->lt == 0)
  85. error("%s is not a complex type", n->sym->name);
  86. expr(n->left, res);
  87. res->comt = n->sym->lt;
  88. res->fmt = 'a';
  89. }
  90. void
  91. oindm(Node *n, Node *res)
  92. {
  93. Map *m;
  94. Node l;
  95. m = cormap;
  96. if(m == 0)
  97. m = symmap;
  98. expr(n->left, &l);
  99. if(l.type != TINT)
  100. error("bad type for *");
  101. if(m == 0)
  102. error("no map for *");
  103. indir(m, l.ival, l.fmt, res);
  104. res->comt = l.comt;
  105. }
  106. void
  107. oindc(Node *n, Node *res)
  108. {
  109. Map *m;
  110. Node l;
  111. m = symmap;
  112. if(m == 0)
  113. m = cormap;
  114. expr(n->left, &l);
  115. if(l.type != TINT)
  116. error("bad type for @");
  117. if(m == 0)
  118. error("no map for @");
  119. indir(m, l.ival, l.fmt, res);
  120. res->comt = l.comt;
  121. }
  122. void
  123. oframe(Node *n, Node *res)
  124. {
  125. char *p;
  126. Node *lp;
  127. uvlong ival;
  128. Frtype *f;
  129. p = n->sym->name;
  130. while(*p && *p == '$')
  131. p++;
  132. lp = n->left;
  133. if(localaddr(cormap, p, lp->sym->name, &ival, rget) < 0)
  134. error("colon: %r");
  135. res->ival = ival;
  136. res->op = OCONST;
  137. res->fmt = 'X';
  138. res->type = TINT;
  139. /* Try and set comt */
  140. for(f = n->sym->local; f; f = f->next) {
  141. if(f->var == lp->sym) {
  142. res->comt = f->type;
  143. res->fmt = 'a';
  144. break;
  145. }
  146. }
  147. }
  148. void
  149. oindex(Node *n, Node *res)
  150. {
  151. Node l, r;
  152. expr(n->left, &l);
  153. expr(n->right, &r);
  154. if(r.type != TINT)
  155. error("bad type for []");
  156. switch(l.type) {
  157. default:
  158. error("lhs[] has bad type");
  159. case TINT:
  160. indir(cormap, l.ival+(r.ival*fsize[l.fmt]), l.fmt, res);
  161. res->comt = l.comt;
  162. res->fmt = l.fmt;
  163. break;
  164. case TLIST:
  165. nthelem(l.l, r.ival, res);
  166. break;
  167. case TSTRING:
  168. res->ival = 0;
  169. if(r.ival >= 0 && r.ival < l.string->len) {
  170. int xx8; /* to get around bug in vc */
  171. xx8 = r.ival;
  172. res->ival = l.string->string[xx8];
  173. }
  174. res->op = OCONST;
  175. res->type = TINT;
  176. res->fmt = 'c';
  177. break;
  178. }
  179. }
  180. void
  181. oappend(Node *n, Node *res)
  182. {
  183. Value *v;
  184. Node r, l;
  185. int empty;
  186. expr(n->left, &l);
  187. expr(n->right, &r);
  188. if(l.type != TLIST)
  189. error("must append to list");
  190. empty = (l.l == nil && (n->left->op == ONAME));
  191. append(res, &l, &r);
  192. if(empty) {
  193. v = n->left->sym->v;
  194. v->type = res->type;
  195. v->Store = res->Store;
  196. v->comt = res->comt;
  197. }
  198. }
  199. void
  200. odelete(Node *n, Node *res)
  201. {
  202. Node l, r;
  203. expr(n->left, &l);
  204. expr(n->right, &r);
  205. if(l.type != TLIST)
  206. error("must delete from list");
  207. if(r.type != TINT)
  208. error("delete index must be integer");
  209. delete(l.l, r.ival, res);
  210. }
  211. void
  212. ohead(Node *n, Node *res)
  213. {
  214. Node l;
  215. expr(n->left, &l);
  216. if(l.type != TLIST)
  217. error("head needs list");
  218. res->op = OCONST;
  219. if(l.l) {
  220. res->type = l.l->type;
  221. res->Store = l.l->Store;
  222. }
  223. else {
  224. res->type = TLIST;
  225. res->l = 0;
  226. }
  227. }
  228. void
  229. otail(Node *n, Node *res)
  230. {
  231. Node l;
  232. expr(n->left, &l);
  233. if(l.type != TLIST)
  234. error("tail needs list");
  235. res->op = OCONST;
  236. res->type = TLIST;
  237. if(l.l)
  238. res->l = l.l->next;
  239. else
  240. res->l = 0;
  241. }
  242. void
  243. oconst(Node *n, Node *res)
  244. {
  245. res->op = OCONST;
  246. res->type = n->type;
  247. res->Store = n->Store;
  248. res->comt = n->comt;
  249. }
  250. void
  251. oname(Node *n, Node *res)
  252. {
  253. Value *v;
  254. v = n->sym->v;
  255. if(v->set == 0)
  256. error("%s used but not set", n->sym->name);
  257. res->op = OCONST;
  258. res->type = v->type;
  259. res->Store = v->Store;
  260. res->comt = v->comt;
  261. }
  262. void
  263. octruct(Node *n, Node *res)
  264. {
  265. res->op = OCONST;
  266. res->type = TLIST;
  267. res->l = construct(n->left);
  268. }
  269. void
  270. oasgn(Node *n, Node *res)
  271. {
  272. Node *lp, r;
  273. Value *v;
  274. lp = n->left;
  275. switch(lp->op) {
  276. case OINDM:
  277. windir(cormap, lp->left, n->right, res);
  278. break;
  279. case OINDC:
  280. windir(symmap, lp->left, n->right, res);
  281. break;
  282. default:
  283. chklval(lp);
  284. v = lp->sym->v;
  285. expr(n->right, &r);
  286. v->set = 1;
  287. v->type = r.type;
  288. v->Store = r.Store;
  289. res->op = OCONST;
  290. res->type = v->type;
  291. res->Store = v->Store;
  292. res->comt = v->comt;
  293. }
  294. }
  295. void
  296. oadd(Node *n, Node *res)
  297. {
  298. Node l, r;
  299. if(n->right == nil){ /* unary + */
  300. expr(n->left, res);
  301. return;
  302. }
  303. expr(n->left, &l);
  304. expr(n->right, &r);
  305. res->fmt = l.fmt;
  306. res->op = OCONST;
  307. res->type = TFLOAT;
  308. switch(l.type) {
  309. default:
  310. error("bad lhs type +");
  311. case TINT:
  312. switch(r.type) {
  313. case TINT:
  314. res->type = TINT;
  315. res->ival = l.ival+r.ival;
  316. break;
  317. case TFLOAT:
  318. res->fval = l.ival+r.fval;
  319. break;
  320. default:
  321. error("bad rhs type +");
  322. }
  323. break;
  324. case TFLOAT:
  325. switch(r.type) {
  326. case TINT:
  327. res->fval = l.fval+r.ival;
  328. break;
  329. case TFLOAT:
  330. res->fval = l.fval+r.fval;
  331. break;
  332. default:
  333. error("bad rhs type +");
  334. }
  335. break;
  336. case TSTRING:
  337. if(r.type == TSTRING) {
  338. res->type = TSTRING;
  339. res->fmt = 's';
  340. res->string = stradd(l.string, r.string);
  341. break;
  342. }
  343. if(r.type == TINT) {
  344. res->type = TSTRING;
  345. res->fmt = 's';
  346. res->string = straddrune(l.string, r.ival);
  347. break;
  348. }
  349. error("bad rhs for +");
  350. case TLIST:
  351. res->type = TLIST;
  352. switch(r.type) {
  353. case TLIST:
  354. res->l = addlist(l.l, r.l);
  355. break;
  356. default:
  357. r.left = 0;
  358. r.right = 0;
  359. res->l = addlist(l.l, construct(&r));
  360. break;
  361. }
  362. }
  363. }
  364. void
  365. osub(Node *n, Node *res)
  366. {
  367. Node l, r;
  368. expr(n->left, &l);
  369. expr(n->right, &r);
  370. res->fmt = l.fmt;
  371. res->op = OCONST;
  372. res->type = TFLOAT;
  373. switch(l.type) {
  374. default:
  375. error("bad lhs type -");
  376. case TINT:
  377. switch(r.type) {
  378. case TINT:
  379. res->type = TINT;
  380. res->ival = l.ival-r.ival;
  381. break;
  382. case TFLOAT:
  383. res->fval = l.ival-r.fval;
  384. break;
  385. default:
  386. error("bad rhs type -");
  387. }
  388. break;
  389. case TFLOAT:
  390. switch(r.type) {
  391. case TINT:
  392. res->fval = l.fval-r.ival;
  393. break;
  394. case TFLOAT:
  395. res->fval = l.fval-r.fval;
  396. break;
  397. default:
  398. error("bad rhs type -");
  399. }
  400. break;
  401. }
  402. }
  403. void
  404. omul(Node *n, Node *res)
  405. {
  406. Node l, r;
  407. expr(n->left, &l);
  408. expr(n->right, &r);
  409. res->fmt = l.fmt;
  410. res->op = OCONST;
  411. res->type = TFLOAT;
  412. switch(l.type) {
  413. default:
  414. error("bad lhs type *");
  415. case TINT:
  416. switch(r.type) {
  417. case TINT:
  418. res->type = TINT;
  419. res->ival = l.ival*r.ival;
  420. break;
  421. case TFLOAT:
  422. res->fval = l.ival*r.fval;
  423. break;
  424. default:
  425. error("bad rhs type *");
  426. }
  427. break;
  428. case TFLOAT:
  429. switch(r.type) {
  430. case TINT:
  431. res->fval = l.fval*r.ival;
  432. break;
  433. case TFLOAT:
  434. res->fval = l.fval*r.fval;
  435. break;
  436. default:
  437. error("bad rhs type *");
  438. }
  439. break;
  440. }
  441. }
  442. void
  443. odiv(Node *n, Node *res)
  444. {
  445. Node l, r;
  446. expr(n->left, &l);
  447. expr(n->right, &r);
  448. res->fmt = l.fmt;
  449. res->op = OCONST;
  450. res->type = TFLOAT;
  451. switch(l.type) {
  452. default:
  453. error("bad lhs type /");
  454. case TINT:
  455. switch(r.type) {
  456. case TINT:
  457. res->type = TINT;
  458. if(r.ival == 0)
  459. error("zero divide");
  460. res->ival = l.ival/r.ival;
  461. break;
  462. case TFLOAT:
  463. if(r.fval == 0)
  464. error("zero divide");
  465. res->fval = l.ival/r.fval;
  466. break;
  467. default:
  468. error("bad rhs type /");
  469. }
  470. break;
  471. case TFLOAT:
  472. switch(r.type) {
  473. case TINT:
  474. res->fval = l.fval/r.ival;
  475. break;
  476. case TFLOAT:
  477. res->fval = l.fval/r.fval;
  478. break;
  479. default:
  480. error("bad rhs type /");
  481. }
  482. break;
  483. }
  484. }
  485. void
  486. omod(Node *n, Node *res)
  487. {
  488. Node l, r;
  489. expr(n->left, &l);
  490. expr(n->right, &r);
  491. res->fmt = l.fmt;
  492. res->op = OCONST;
  493. res->type = TINT;
  494. if(l.type != TINT || r.type != TINT)
  495. error("bad expr type %");
  496. res->ival = l.ival%r.ival;
  497. }
  498. void
  499. olsh(Node *n, Node *res)
  500. {
  501. Node l, r;
  502. expr(n->left, &l);
  503. expr(n->right, &r);
  504. res->fmt = l.fmt;
  505. res->op = OCONST;
  506. res->type = TINT;
  507. if(l.type != TINT || r.type != TINT)
  508. error("bad expr type <<");
  509. res->ival = l.ival<<r.ival;
  510. }
  511. void
  512. orsh(Node *n, Node *res)
  513. {
  514. Node l, r;
  515. expr(n->left, &l);
  516. expr(n->right, &r);
  517. res->fmt = l.fmt;
  518. res->op = OCONST;
  519. res->type = TINT;
  520. if(l.type != TINT || r.type != TINT)
  521. error("bad expr type >>");
  522. res->ival = (uvlong)l.ival>>r.ival;
  523. }
  524. void
  525. olt(Node *n, Node *res)
  526. {
  527. Node l, r;
  528. expr(n->left, &l);
  529. expr(n->right, &r);
  530. res->fmt = l.fmt;
  531. res->op = OCONST;
  532. res->type = TINT;
  533. switch(l.type) {
  534. default:
  535. error("bad lhs type <");
  536. case TINT:
  537. switch(r.type) {
  538. case TINT:
  539. res->ival = l.ival < r.ival;
  540. break;
  541. case TFLOAT:
  542. res->ival = l.ival < r.fval;
  543. break;
  544. default:
  545. error("bad rhs type <");
  546. }
  547. break;
  548. case TFLOAT:
  549. switch(r.type) {
  550. case TINT:
  551. res->ival = l.fval < r.ival;
  552. break;
  553. case TFLOAT:
  554. res->ival = l.fval < r.fval;
  555. break;
  556. default:
  557. error("bad rhs type <");
  558. }
  559. break;
  560. }
  561. }
  562. void
  563. ogt(Node *n, Node *res)
  564. {
  565. Node l, r;
  566. expr(n->left, &l);
  567. expr(n->right, &r);
  568. res->fmt = 'D';
  569. res->op = OCONST;
  570. res->type = TINT;
  571. switch(l.type) {
  572. default:
  573. error("bad lhs type >");
  574. case TINT:
  575. switch(r.type) {
  576. case TINT:
  577. res->ival = l.ival > r.ival;
  578. break;
  579. case TFLOAT:
  580. res->ival = l.ival > r.fval;
  581. break;
  582. default:
  583. error("bad rhs type >");
  584. }
  585. break;
  586. case TFLOAT:
  587. switch(r.type) {
  588. case TINT:
  589. res->ival = l.fval > r.ival;
  590. break;
  591. case TFLOAT:
  592. res->ival = l.fval > r.fval;
  593. break;
  594. default:
  595. error("bad rhs type >");
  596. }
  597. break;
  598. }
  599. }
  600. void
  601. oleq(Node *n, Node *res)
  602. {
  603. Node l, r;
  604. expr(n->left, &l);
  605. expr(n->right, &r);
  606. res->fmt = 'D';
  607. res->op = OCONST;
  608. res->type = TINT;
  609. switch(l.type) {
  610. default:
  611. error("bad expr type <=");
  612. case TINT:
  613. switch(r.type) {
  614. case TINT:
  615. res->ival = l.ival <= r.ival;
  616. break;
  617. case TFLOAT:
  618. res->ival = l.ival <= r.fval;
  619. break;
  620. default:
  621. error("bad expr type <=");
  622. }
  623. break;
  624. case TFLOAT:
  625. switch(r.type) {
  626. case TINT:
  627. res->ival = l.fval <= r.ival;
  628. break;
  629. case TFLOAT:
  630. res->ival = l.fval <= r.fval;
  631. break;
  632. default:
  633. error("bad expr type <=");
  634. }
  635. break;
  636. }
  637. }
  638. void
  639. ogeq(Node *n, Node *res)
  640. {
  641. Node l, r;
  642. expr(n->left, &l);
  643. expr(n->right, &r);
  644. res->fmt = 'D';
  645. res->op = OCONST;
  646. res->type = TINT;
  647. switch(l.type) {
  648. default:
  649. error("bad lhs type >=");
  650. case TINT:
  651. switch(r.type) {
  652. case TINT:
  653. res->ival = l.ival >= r.ival;
  654. break;
  655. case TFLOAT:
  656. res->ival = l.ival >= r.fval;
  657. break;
  658. default:
  659. error("bad rhs type >=");
  660. }
  661. break;
  662. case TFLOAT:
  663. switch(r.type) {
  664. case TINT:
  665. res->ival = l.fval >= r.ival;
  666. break;
  667. case TFLOAT:
  668. res->ival = l.fval >= r.fval;
  669. break;
  670. default:
  671. error("bad rhs type >=");
  672. }
  673. break;
  674. }
  675. }
  676. void
  677. oeq(Node *n, Node *res)
  678. {
  679. Node l, r;
  680. expr(n->left, &l);
  681. expr(n->right, &r);
  682. res->fmt = 'D';
  683. res->op = OCONST;
  684. res->type = TINT;
  685. res->ival = 0;
  686. switch(l.type) {
  687. default:
  688. break;
  689. case TINT:
  690. switch(r.type) {
  691. case TINT:
  692. res->ival = l.ival == r.ival;
  693. break;
  694. case TFLOAT:
  695. res->ival = l.ival == r.fval;
  696. break;
  697. default:
  698. break;
  699. }
  700. break;
  701. case TFLOAT:
  702. switch(r.type) {
  703. case TINT:
  704. res->ival = l.fval == r.ival;
  705. break;
  706. case TFLOAT:
  707. res->ival = l.fval == r.fval;
  708. break;
  709. default:
  710. break;
  711. }
  712. break;
  713. case TSTRING:
  714. if(r.type == TSTRING) {
  715. res->ival = scmp(r.string, l.string);
  716. break;
  717. }
  718. break;
  719. case TLIST:
  720. if(r.type == TLIST) {
  721. res->ival = listcmp(l.l, r.l);
  722. break;
  723. }
  724. break;
  725. }
  726. if(n->op == ONEQ)
  727. res->ival = !res->ival;
  728. }
  729. void
  730. oland(Node *n, Node *res)
  731. {
  732. Node l, r;
  733. expr(n->left, &l);
  734. expr(n->right, &r);
  735. res->fmt = l.fmt;
  736. res->op = OCONST;
  737. res->type = TINT;
  738. if(l.type != TINT || r.type != TINT)
  739. error("bad expr type &");
  740. res->ival = l.ival&r.ival;
  741. }
  742. void
  743. oxor(Node *n, Node *res)
  744. {
  745. Node l, r;
  746. expr(n->left, &l);
  747. expr(n->right, &r);
  748. res->fmt = l.fmt;
  749. res->op = OCONST;
  750. res->type = TINT;
  751. if(l.type != TINT || r.type != TINT)
  752. error("bad expr type ^");
  753. res->ival = l.ival^r.ival;
  754. }
  755. void
  756. olor(Node *n, Node *res)
  757. {
  758. Node l, r;
  759. expr(n->left, &l);
  760. expr(n->right, &r);
  761. res->fmt = l.fmt;
  762. res->op = OCONST;
  763. res->type = TINT;
  764. if(l.type != TINT || r.type != TINT)
  765. error("bad expr type |");
  766. res->ival = l.ival|r.ival;
  767. }
  768. void
  769. ocand(Node *n, Node *res)
  770. {
  771. Node l, r;
  772. res->fmt = l.fmt;
  773. res->op = OCONST;
  774. res->type = TINT;
  775. res->ival = 0;
  776. expr(n->left, &l);
  777. if(bool(&l) == 0)
  778. return;
  779. expr(n->right, &r);
  780. if(bool(&r) == 0)
  781. return;
  782. res->ival = 1;
  783. }
  784. void
  785. onot(Node *n, Node *res)
  786. {
  787. Node l;
  788. res->op = OCONST;
  789. res->type = TINT;
  790. res->ival = 0;
  791. expr(n->left, &l);
  792. if(bool(&l) == 0)
  793. res->ival = 1;
  794. }
  795. void
  796. ocor(Node *n, Node *res)
  797. {
  798. Node l, r;
  799. res->op = OCONST;
  800. res->type = TINT;
  801. res->ival = 0;
  802. expr(n->left, &l);
  803. if(bool(&l)) {
  804. res->ival = 1;
  805. return;
  806. }
  807. expr(n->right, &r);
  808. if(bool(&r)) {
  809. res->ival = 1;
  810. return;
  811. }
  812. }
  813. void
  814. oeinc(Node *n, Node *res)
  815. {
  816. Value *v;
  817. chklval(n->left);
  818. v = n->left->sym->v;
  819. res->op = OCONST;
  820. res->type = v->type;
  821. switch(v->type) {
  822. case TINT:
  823. if(n->op == OEDEC)
  824. v->ival -= fmtsize(v);
  825. else
  826. v->ival += fmtsize(v);
  827. break;
  828. case TFLOAT:
  829. if(n->op == OEDEC)
  830. v->fval--;
  831. else
  832. v->fval++;
  833. break;
  834. default:
  835. error("bad type for pre --/++");
  836. }
  837. res->Store = v->Store;
  838. }
  839. void
  840. opinc(Node *n, Node *res)
  841. {
  842. Value *v;
  843. chklval(n->left);
  844. v = n->left->sym->v;
  845. res->op = OCONST;
  846. res->type = v->type;
  847. res->Store = v->Store;
  848. switch(v->type) {
  849. case TINT:
  850. if(n->op == OPDEC)
  851. v->ival -= fmtsize(v);
  852. else
  853. v->ival += fmtsize(v);
  854. break;
  855. case TFLOAT:
  856. if(n->op == OPDEC)
  857. v->fval--;
  858. else
  859. v->fval++;
  860. break;
  861. default:
  862. error("bad type for post --/++");
  863. }
  864. }
  865. void
  866. ocall(Node *n, Node *res)
  867. {
  868. Lsym *s;
  869. Rplace *rsav;
  870. res->op = OCONST; /* Default return value */
  871. res->type = TLIST;
  872. res->l = 0;
  873. chklval(n->left);
  874. s = n->left->sym;
  875. if(n->builtin && !s->builtin){
  876. error("no builtin %s", s->name);
  877. return;
  878. }
  879. if(s->builtin && (n->builtin || s->proc == 0)) {
  880. (*s->builtin)(res, n->right);
  881. return;
  882. }
  883. if(s->proc == 0)
  884. error("no function %s", s->name);
  885. rsav = ret;
  886. call(s->name, n->right, s->proc->left, s->proc->right, res);
  887. ret = rsav;
  888. }
  889. void
  890. ofmt(Node *n, Node *res)
  891. {
  892. expr(n->left, res);
  893. res->fmt = n->right->ival;
  894. }
  895. void
  896. owhat(Node *n, Node *res)
  897. {
  898. res->op = OCONST; /* Default return value */
  899. res->type = TLIST;
  900. res->l = 0;
  901. whatis(n->sym);
  902. }
  903. void (*expop[])(Node*, Node*) =
  904. {
  905. [ONAME] oname,
  906. [OCONST] oconst,
  907. [OMUL] omul,
  908. [ODIV] odiv,
  909. [OMOD] omod,
  910. [OADD] oadd,
  911. [OSUB] osub,
  912. [ORSH] orsh,
  913. [OLSH] olsh,
  914. [OLT] olt,
  915. [OGT] ogt,
  916. [OLEQ] oleq,
  917. [OGEQ] ogeq,
  918. [OEQ] oeq,
  919. [ONEQ] oeq,
  920. [OLAND] oland,
  921. [OXOR] oxor,
  922. [OLOR] olor,
  923. [OCAND] ocand,
  924. [OCOR] ocor,
  925. [OASGN] oasgn,
  926. [OINDM] oindm,
  927. [OEDEC] oeinc,
  928. [OEINC] oeinc,
  929. [OPINC] opinc,
  930. [OPDEC] opinc,
  931. [ONOT] onot,
  932. [OIF] 0,
  933. [ODO] 0,
  934. [OLIST] olist,
  935. [OCALL] ocall,
  936. [OCTRUCT] octruct,
  937. [OWHILE] 0,
  938. [OELSE] 0,
  939. [OHEAD] ohead,
  940. [OTAIL] otail,
  941. [OAPPEND] oappend,
  942. [ORET] 0,
  943. [OINDEX] oindex,
  944. [OINDC] oindc,
  945. [ODOT] odot,
  946. [OLOCAL] 0,
  947. [OFRAME] oframe,
  948. [OCOMPLEX] 0,
  949. [ODELETE] odelete,
  950. [OCAST] ocast,
  951. [OFMT] ofmt,
  952. [OEVAL] oeval,
  953. [OWHAT] owhat,
  954. };