cgcode.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #include "cgstd.h"
  2. #include "cgcode.h"
  3. #include "cgutil.h"
  4. CGStm *CGVisitor::visit( CGStm *t ){
  5. return t;
  6. }
  7. CGExp *CGVisitor::visit( CGExp *e ){
  8. return e;
  9. }
  10. CGNop *CGStm::nop(){ return 0; }
  11. CGXop *CGStm::xop(){ return 0; }
  12. CGRem *CGStm::rem(){ return 0; }
  13. CGAti *CGStm::ati(){ return 0; }
  14. CGAtd *CGStm::atd(){ return 0; }
  15. CGMov *CGStm::mov(){ return 0; }
  16. CGLab *CGStm::lab(){ return 0; }
  17. CGBra *CGStm::bra(){ return 0; }
  18. CGBcc *CGStm::bcc(){ return 0; }
  19. CGEva *CGStm::eva(){ return 0; }
  20. CGRet *CGStm::ret(){ return 0; }
  21. CGSeq *CGStm::seq(){ return 0; }
  22. CGMem *CGExp::mem(){ return 0; }
  23. CGLea *CGExp::lea(){ return 0; }
  24. CGCvt *CGExp::cvt(){ return 0; }
  25. CGUop *CGExp::uop(){ return 0; }
  26. CGBop *CGExp::bop(){ return 0; }
  27. CGJsr *CGExp::jsr(){ return 0; }
  28. CGVfn *CGExp::vfn(){ return 0; }
  29. CGScc *CGExp::scc(){ return 0; }
  30. CGEsq *CGExp::esq(){ return 0; }
  31. CGFrm *CGExp::frm(){ return 0; }
  32. CGTmp *CGExp::tmp(){ return 0; }
  33. CGLit *CGExp::lit(){ return 0; }
  34. CGSym *CGExp::sym(){ return 0; }
  35. CGDat *CGExp::dat(){ return 0; }
  36. CGReg *CGExp::reg(){ return 0; }
  37. CGStm::~CGStm(){
  38. }
  39. CGStm *CGStm::visit( CGVisitor &vis ){
  40. assert(0);
  41. return 0;
  42. }
  43. CGExp *CGExp::nonEsq(){
  44. return this;
  45. }
  46. CGExp *CGExp::visit( CGVisitor &vis ){
  47. assert(0);
  48. return 0;
  49. }
  50. CGExp::~CGExp(){
  51. }
  52. bool CGExp::sideEffects(){
  53. return false;
  54. }
  55. bool CGExp::equals( CGExp *exp ){
  56. return false;
  57. }
  58. //***** CGNop *****
  59. CGNop *CGNop::nop(){
  60. return this;
  61. }
  62. CGStm *CGNop::visit( CGVisitor &vis ){
  63. return vis.visit( this );
  64. }
  65. //***** CGXop *****
  66. CGXop *CGXop::xop(){
  67. return this;
  68. }
  69. CGStm *CGXop::visit( CGVisitor &vis ){
  70. CGReg *r=def ? vis.visit(def)->reg() : 0;
  71. CGExp *e=exp ? exp->visit(vis) : 0;
  72. return vis.visit( r==def && e==exp ? this : CG::xop(op,r,e) );
  73. }
  74. //***** CGRem *****
  75. CGRem *CGRem::rem(){
  76. return this;
  77. }
  78. CGStm *CGRem::visit( CGVisitor &vis ){
  79. return vis.visit( this );
  80. }
  81. //***** CGAti *****
  82. CGAti *CGAti::ati(){
  83. return this;
  84. }
  85. CGStm *CGAti::visit( CGVisitor &vis ){
  86. CGMem *e=mem->visit(vis)->mem();
  87. return vis.visit( e==mem ? this : CG::ati(e) );
  88. }
  89. //***** CGAtd *****
  90. CGAtd *CGAtd::atd(){
  91. return this;
  92. }
  93. CGStm *CGAtd::visit( CGVisitor &vis ){
  94. CGMem *a=mem->visit(vis)->mem();
  95. CGSym *b=sym->visit(vis)->sym();
  96. return vis.visit( a==mem && b==sym ? this : CG::atd(a,b) );
  97. }
  98. //***** CGMov *****
  99. CGMov *CGMov::mov(){
  100. return this;
  101. }
  102. CGStm *CGMov::visit( CGVisitor &vis ){
  103. CGExp *b=rhs->visit(vis);
  104. CGExp *a=lhs->visit(vis);
  105. return vis.visit( a==lhs && b==rhs ? this : CG::mov(a,b) );
  106. }
  107. //***** CGLab *****
  108. CGLab *CGLab::lab(){
  109. return this;
  110. }
  111. CGStm *CGLab::visit( CGVisitor &vis ){
  112. CGSym *e=vis.visit(sym)->sym();
  113. return vis.visit( e==sym ? this : CG::lab(e) );
  114. }
  115. //***** CGBra *****
  116. CGBra *CGBra::bra(){
  117. return this;
  118. }
  119. CGStm *CGBra::visit( CGVisitor &vis ){
  120. CGSym *e=vis.visit(sym)->sym();
  121. return vis.visit( e==sym ? this : CG::bra(e) );
  122. }
  123. //***** CGBcc *****
  124. CGBcc *CGBcc::bcc(){
  125. return this;
  126. }
  127. CGStm *CGBcc::visit( CGVisitor &vis ){
  128. CGExp *a=lhs->visit(vis);
  129. CGExp *b=rhs->visit(vis);
  130. CGSym *c=vis.visit(sym)->sym();
  131. return vis.visit( a==lhs && b==rhs && c==sym ? this : CG::bcc(cc,a,b,c) );
  132. }
  133. //***** CGEva *****
  134. CGEva *CGEva::eva(){
  135. return this;
  136. }
  137. CGStm *CGEva::visit( CGVisitor &vis ){
  138. CGExp *e=exp->visit(vis);
  139. return vis.visit( e==exp ? this : CG::eva(e) );
  140. }
  141. //***** CGRet *****
  142. CGRet *CGRet::ret(){
  143. return this;
  144. }
  145. CGStm *CGRet::visit( CGVisitor &vis ){
  146. CGExp *e=exp ? exp->visit(vis) : 0;
  147. return vis.visit( e==exp ? this : CG::ret(e) );
  148. }
  149. //***** CGSeq *****
  150. CGSeq *CGSeq::seq(){
  151. return this;
  152. }
  153. CGStm *CGSeq::visit( CGVisitor &vis ){
  154. bool dup=false;
  155. vector<CGStm*> t_stms;
  156. int i;
  157. for( i=0;i<stms.size();++i ){
  158. CGStm *t=stms[i]->visit(vis);
  159. if( t!=stms[i] ) dup=true;
  160. t_stms.push_back( t );
  161. }
  162. return vis.visit( dup ? CG::seq( t_stms ) : this );
  163. }
  164. void CGSeq::push_back( CGStm *stm ){
  165. stms.push_back( stm );
  166. }
  167. void CGSeq::push_front( CGStm *stm ){
  168. stms.insert( stms.begin(),stm );
  169. }
  170. //***** CGMem *****
  171. CGMem *CGMem::mem(){
  172. return this;
  173. }
  174. bool CGMem::sideEffects(){
  175. return exp->sideEffects();
  176. }
  177. bool CGMem::equals( CGExp *e ){
  178. CGMem *t=e->mem();
  179. if( !t || type!=t->type || offset!=t->offset || flags!=t->flags ) return false;
  180. return exp->equals(t->exp);
  181. }
  182. CGExp *CGMem::visit( CGVisitor &vis ){
  183. CGExp *e=exp->visit(vis);
  184. return vis.visit( e==exp ? this : CG::mem(type,e,offset) );
  185. }
  186. //***** CGLea *****
  187. CGLea *CGLea::lea(){
  188. return this;
  189. }
  190. bool CGLea::sideEffects(){
  191. return exp->sideEffects();
  192. }
  193. bool CGLea::equals( CGExp *e ){
  194. CGLea *t=e->lea();
  195. if( !t || type!=t->type ) return false;
  196. return exp->equals(t->exp);
  197. }
  198. CGExp *CGLea::visit( CGVisitor &vis ){
  199. CGExp *e=exp->visit(vis);
  200. return vis.visit( e==exp ? this : CG::lea(e) );
  201. }
  202. //***** CGCvt *****
  203. CGCvt *CGCvt::cvt(){
  204. return this;
  205. }
  206. bool CGCvt::sideEffects(){
  207. return exp->sideEffects();
  208. }
  209. bool CGCvt::equals( CGExp *e ){
  210. CGCvt *t=e->cvt();
  211. if( !t || type!=t->type ) return false;
  212. return exp->equals(t->exp);
  213. }
  214. CGExp *CGCvt::visit( CGVisitor &vis ){
  215. CGExp *e=exp->visit(vis);
  216. return vis.visit( e==exp ? this : CG::cvt(type,e) );
  217. }
  218. //***** CGUop *****
  219. CGUop *CGUop::uop(){
  220. return this;
  221. }
  222. bool CGUop::sideEffects(){
  223. return exp->sideEffects();
  224. }
  225. bool CGUop::equals( CGExp *e ){
  226. CGUop *t=e->uop();
  227. return t && type==t->type && exp->equals(t->exp);
  228. }
  229. CGExp *CGUop::visit( CGVisitor &vis ){
  230. CGExp *e=exp->visit(vis);
  231. return vis.visit( e==exp ? this : CG::uop(op,e) );
  232. }
  233. //***** CGBop *****
  234. CGBop *CGBop::bop(){
  235. return this;
  236. }
  237. bool CGBop::commutes(){
  238. switch(op){
  239. case CG_ADD:case CG_MUL:case CG_AND:case CG_ORL:case CG_XOR:
  240. return true;
  241. }
  242. return false;
  243. }
  244. bool CGBop::sideEffects(){
  245. return lhs->sideEffects() || rhs->sideEffects();
  246. }
  247. bool CGBop::equals( CGExp *e ){
  248. CGBop *t=e->bop();
  249. if( !t || type!=t->type || op!=t->op ) return false;
  250. if( lhs->equals(t->lhs) && rhs->equals(t->rhs) ) return true;
  251. switch(op){
  252. case CG_ADD:case CG_MUL:break;
  253. default:return false;
  254. }
  255. return lhs->equals(t->rhs) && rhs->equals(t->lhs);
  256. }
  257. CGExp *CGBop::visit( CGVisitor &vis ){
  258. CGExp *a=lhs->visit(vis);
  259. CGExp *b=rhs->visit(vis);
  260. return vis.visit( a==lhs && b==rhs ? this : CG::bop(op,a,b) );
  261. }
  262. //***** CGJsr *****
  263. CGJsr *CGJsr::jsr(){
  264. return this;
  265. }
  266. bool CGJsr::sideEffects(){
  267. return true;
  268. }
  269. bool CGJsr::equals( CGExp *e ){
  270. CGJsr *t=e->jsr();
  271. if( !t || type!=t->type || args.size()!=t->args.size() ) return false;
  272. if( exp->equals(t->exp) ) return false;
  273. for( int k=0;k<args.size();++k ){
  274. if( !args[k]->equals(t->args[k]) ) return false;
  275. }
  276. return true;
  277. }
  278. CGExp *CGJsr::visit( CGVisitor &vis ){
  279. CGExp *t_exp=exp->visit(vis);
  280. bool copy=t_exp!=exp;
  281. vector<CGExp*> t_args;
  282. t_args.resize(args.size());
  283. for( int k=0;k<args.size();++k ){
  284. t_args[k]=args[k]->visit(vis);
  285. if( t_args[k]!=args[k] ) copy=true;
  286. }
  287. if( !copy ) return vis.visit( this );
  288. CGJsr *t=CG::jsr(type,call_conv,t_exp,t_args );
  289. return vis.visit( t );
  290. }
  291. //***** CGVfn *****
  292. CGVfn *CGVfn::vfn(){
  293. return this;
  294. }
  295. bool CGVfn::sideEffects(){
  296. return exp->sideEffects() || self->sideEffects();
  297. }
  298. bool CGVfn::equals( CGExp *e ){
  299. CGVfn *t=e->vfn();
  300. if( !t || type!=t->type ) return false;
  301. return exp->equals(t->exp) && self->equals(t->self);
  302. }
  303. CGExp *CGVfn::visit( CGVisitor &vis ){
  304. CGExp *a=exp->visit(vis);
  305. CGExp *b=self->visit(vis);
  306. return vis.visit( a==exp && b==self ? this : CG::vfn(a,b) );
  307. }
  308. //***** CGScc *****
  309. CGScc *CGScc::scc(){
  310. return this;
  311. }
  312. bool CGScc::sideEffects(){
  313. return lhs->sideEffects() || rhs->sideEffects();
  314. }
  315. bool CGScc::equals( CGExp *e ){
  316. CGScc *t=e->scc();
  317. if( !t || type!=t->type || cc!=t->cc ) return false;
  318. return lhs->equals(t->lhs) && rhs->equals(t->rhs);
  319. }
  320. CGExp *CGScc::visit( CGVisitor &vis ){
  321. CGExp *a=lhs->visit(vis);
  322. CGExp *b=rhs->visit(vis);
  323. return vis.visit( a==lhs && b==rhs ? this : CG::scc(cc,a,b) );
  324. }
  325. //***** CGEsq *****
  326. CGEsq *CGEsq::esq(){
  327. return this;
  328. }
  329. CGExp *CGEsq::nonEsq(){
  330. return rhs->nonEsq();
  331. }
  332. bool CGEsq::sideEffects(){
  333. return true;
  334. }
  335. bool CGEsq::equals( CGExp *e ){
  336. CGEsq *t=e->esq();
  337. if( !t || type!=t->type ) return false;
  338. return rhs->equals(t->rhs);
  339. }
  340. CGExp *CGEsq::visit( CGVisitor &vis ){
  341. CGStm *a=lhs->visit(vis);
  342. CGExp *b=rhs->visit(vis);
  343. return vis.visit( a==lhs && b==rhs ? this : CG::esq(a,b) );
  344. }
  345. //***** CGFrm *****
  346. CGFrm *CGFrm::frm(){
  347. return this;
  348. }
  349. bool CGFrm::equals( CGExp *e ){
  350. return e->frm() ? true : false;
  351. }
  352. CGExp *CGFrm::visit( CGVisitor &vis ){
  353. return vis.visit(this);
  354. }
  355. //***** CGTmp *****
  356. CGTmp *CGTmp::tmp(){
  357. return this;
  358. }
  359. bool CGTmp::equals( CGExp *e ){
  360. CGTmp *t=e->tmp();
  361. if( !t || type!=t->type ) return false;
  362. return ident==t->ident;
  363. }
  364. CGExp *CGTmp::visit( CGVisitor &vis ){
  365. return vis.visit(this);
  366. }
  367. //***** CGReg *****
  368. CGReg *CGReg::reg(){
  369. return this;
  370. }
  371. bool CGReg::equals( CGExp *e ){
  372. CGReg *t=e->reg();
  373. return t ? id==t->id : false;
  374. }
  375. CGExp *CGReg::visit( CGVisitor &vis ){
  376. return vis.visit(this);
  377. }
  378. //***** CGLit *****
  379. CGLit *CGLit::lit(){
  380. return this;
  381. }
  382. bool CGLit::equals( CGExp *e ){
  383. CGLit *t=e->lit();
  384. if( !t || type!=t->type ) return false;
  385. if( isfloat() ) return float_value==t->float_value;
  386. return int_value==t->int_value;
  387. }
  388. CGExp *CGLit::visit( CGVisitor &vis ){
  389. return vis.visit(this);
  390. }
  391. //***** CGSym *****
  392. CGSym *CGSym::sym(){
  393. return this;
  394. }
  395. bool CGSym::equals( CGExp *e ){
  396. CGSym *t=e->sym();
  397. return t && value==t->value && linkage==t->linkage;
  398. }
  399. CGExp *CGSym::visit( CGVisitor &vis ){
  400. return vis.visit(this);
  401. }
  402. //***** CGDat *****
  403. CGDat *CGDat::dat(){
  404. return this;
  405. }
  406. bool CGDat::equals( CGExp *e ){
  407. CGDat *t=e->dat();
  408. if( !t || value!=t->value || linkage!=t->linkage || exps.size()!=t->exps.size() ) return false;
  409. for( int k=0;k<t->exps.size();++k ){
  410. if( !exps[k]->equals(t->exps[k]) ) return false;
  411. }
  412. return true;
  413. }
  414. CGExp *CGDat::visit( CGVisitor &vis ){
  415. bool copy=false;
  416. vector<CGExp*> t_exps;
  417. t_exps.resize( exps.size() );
  418. for( int k=0;k<exps.size();++k ){
  419. t_exps[k]=exps[k]->visit(vis);
  420. if( t_exps[k]!=exps[k] ) copy=true;
  421. }
  422. if( !copy ) return vis.visit(this);
  423. CGDat *t=new CGDat;
  424. t->type=type;t->value=value;t->linkage=linkage;t->exps=t_exps;
  425. return vis.visit( t );
  426. }
  427. void CGDat::push_back( CGExp *exp ){
  428. exps.push_back(exp);
  429. }