vcg.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifdef VCG
  2. #ifdef PC
  3. #define MAXINT 0x7FFFFFFF
  4. #endif
  5. #ifndef __HAVE_STACK_IMAGE
  6. #define __HAVE_STACK_IMAGE
  7. typedef struct _stackimg {
  8. int next_data,next_addr;
  9. #ifndef INFINITE_REGISTERS
  10. int reg_alloc_ptr,reg_stack_ptr;
  11. char dreg_in_use[MAX_DATA+1];
  12. char areg_in_use[MAX_ADDR+1];
  13. struct reg_struct reg_stack[MAX_REG_STACK+1],reg_alloc[MAX_REG_STACK+1];
  14. int act_scratch;
  15. #endif
  16. } STACK_IMAGE;
  17. #endif
  18. STACK_IMAGE vcg_img[VCG_MAX+1];
  19. int vcg_nxl[VCG_MAX+1];
  20. int vcg_aborted[VCG_MAX+1];
  21. int vcg_init() {
  22. if (--vcg_lvl<0) {
  23. vcg_lvl++;
  24. return 0;
  25. }
  26. // tmp_use();
  27. usestack(&vcg_img[vcg_lvl]);
  28. vcg_peep_head[vcg_lvl]=0;
  29. vcg_aborted[vcg_lvl]=0;
  30. vcg_nxl[vcg_lvl]=nextlabel;
  31. g_code(op_label,0,0,0);
  32. return 1;
  33. // vcg_on++;
  34. // vcg_cost[vcg_lvl]=0;
  35. }
  36. int en_dir_cost(struct enode *ep) {
  37. switch (ep->nodetype) {
  38. case en_icon:
  39. return (ep->v.i>=-32768 && ep->v.i<32767)?1:2;
  40. case en_labcon:
  41. case en_nacon:
  42. return 1;
  43. case en_add:
  44. case en_sub:
  45. return max(en_dir_cost(ep->v.p[0]),en_dir_cost(ep->v.p[1]));
  46. }
  47. }
  48. int cost_tab[] = {
  49. 0,0,0,0,0,1,1,1,-MAXINT-1,2,1,1,1,1,2,0,0
  50. };
  51. int vcg_cost() {
  52. int cost=0;
  53. if (!vcg_aborted[vcg_lvl]) {
  54. struct ocode *ip;
  55. opt3();
  56. ip = peep_head;
  57. while (ip != 0) {
  58. #define am_cost(x) (x?(x->mode==am_direct?en_dir_cost(x->offset):cost_tab[x->mode]):0)
  59. cost++;
  60. switch (ip->opcode) {
  61. case op_label:
  62. case op_even:
  63. cost--;
  64. break;
  65. case op_moveq:
  66. case op_addq: case op_subq:
  67. case op_lsl: case op_lsr: case op_asl: case op_asr:
  68. case op_rol: case op_ror: case op_roxl: case op_roxr:
  69. case op_trap:
  70. cost+=am_cost(ip->oper2);
  71. break;
  72. case op_bxx:
  73. /* what should we do here? */
  74. break;
  75. case op_dbxx:
  76. cost++;
  77. break;
  78. default:
  79. cost+=am_cost(ip->oper1)+am_cost(ip->oper2);
  80. break;
  81. }
  82. if (cost<0) cost+=(-MAXINT-1)+((ip->length+1)>>1);
  83. ip = ip->fwd;
  84. }
  85. // vcg_on--;
  86. // tmp_free();
  87. } else cost=12345;
  88. return cost;
  89. }
  90. int vcg_done() {
  91. freestack(&vcg_img[vcg_lvl]);
  92. nextlabel=vcg_nxl[vcg_lvl];
  93. int cost=vcg_cost();
  94. vcg_lvl++;
  95. return cost;
  96. }
  97. #endif
  98. // vim:ts=4:sw=4