Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #define export exports
  2. extern "C" {
  3.     #include "qbe/all.h"
  4. }
  5. #undef export
  6.  
  7. #include <iostream>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <cstring>
  11.  
  12. using namespace std;
  13.  
  14. static void phiinsert(Fn *fn) {
  15.     Blk *b;
  16.     uint i = 0;
  17.     while (i < fn->ntmp) {
  18.         vector<Blk*> WorkList;
  19.         auto &x = fn->tmp[i].name;
  20.         if (strcmp(reinterpret_cast<const char *>(x), "\0") == 0) {
  21.             i++;
  22.             continue;
  23.         }
  24.         bool flag = false;
  25.         uint tmp_idx = 0;
  26.         for (b = fn->start; b; b = b->link) {
  27.             for (uint j = 0; j < b->nins; j++) {
  28.                 if (strcmp(fn->tmp[b->ins[j].to.val].name, x) == 0) {
  29.                     tmp_idx = b->ins[j].to.val;
  30.                     flag = true;
  31.                     WorkList.push_back(b);
  32.                     break;
  33.                 }
  34.             }
  35.         }
  36.         if (flag) {
  37.  
  38.             for (auto & it : WorkList) {
  39.                 for (uint j = 0 ; j < it->nfron; j++) {
  40.                     Blk *d = it->fron[j];
  41.                     bool flagPhi = false;
  42.                     for (Phi *ph = d->phi; ph; ph = ph->link) {
  43.                         if (ph->to.val == tmp_idx) {
  44.                             flagPhi = true;
  45.                         }
  46.                     }
  47.                     if (!flagPhi) {
  48.                         //insert phi
  49.                         Phi *p = new Phi;
  50.                         p->cls = -1;
  51.                         p->to = TMP(tmp_idx);
  52.                         p->link = d->phi;
  53.                         fn->tmp[p->to.val].visit = p->to.val;
  54.                         d->phi = p;
  55.                     }
  56.                     bool flagD = false;
  57.                     for (auto & t : WorkList)
  58.                         if (t->name == d->name) {
  59.                             flagD = true;
  60.                             break;
  61.                         }
  62.                     if (!flagD)
  63.                         WorkList.push_back(d);
  64.                 }
  65.             }
  66.         }
  67.         i++;
  68.     }
  69.  
  70. }
  71.  
  72.  
  73. static void newname(Ref *r, Blk *b, vector<Ref> *stk, Fn * fn, int *counter) {
  74.     int tmp_idx = r->val;
  75.     int i = counter[tmp_idx];
  76.     counter[tmp_idx]++;
  77.     Ref r1 = newtmp(nullptr, fn->tmp[tmp_idx].cls, fn);
  78.     snprintf(fn->tmp[fn->ntmp-1].name, NString, "%s", fn->tmp[tmp_idx].name);
  79.     fn->tmp[fn->ntmp-1].phi = i;
  80.     fn->tmp[fn->ntmp-1].visit = tmp_idx;
  81.     stk[tmp_idx].push_back(r1);
  82.     *r = r1;
  83. }
  84.  
  85. static void rename(Fn *fn, Blk *b, vector<Ref> *stk, int *counter) {
  86.     for (Phi *p = b->phi; p; p = p->link) {
  87.         newname(&p->to, b, stk, fn, counter);
  88.     }
  89.     for (Ins *i = b->ins; i - b->ins < b->nins; i++) {
  90.         newname(&i->to, b, stk, fn, counter);
  91.     }
  92.     Blk *succ[3], **ps, *s;
  93.     int t, m;
  94.     succ[0] = b->s1;
  95.     succ[1] = b->s2 == b->s1 ? nullptr : b->s2;
  96.     succ[2] = nullptr;
  97.     for (ps = succ; (s = *ps); ps++)
  98.         for (Phi *p = s->phi; p; p = p->link) {
  99.             t = fn->tmp[p->to.val].visit;
  100.             m = p->narg++;
  101.             if (!stk[t].empty())
  102.                 p->arg[m] = stk[t].back();
  103.             else {
  104.                 fn->tmp[t].phi = -1;
  105.                 p->arg[m] = p->to;
  106.             }
  107.             p->blk[m] = b;
  108.         }
  109.     for (s = b->dom; s; s = s->dlink)
  110.         rename(fn, s, stk, counter);
  111.     for (Phi *p = b->phi; p; p = p->link) {
  112.         stk[p->to.val].pop_back();
  113.     }
  114.     for (Ins *i = b->ins; i - b->ins < b->nins; i++)
  115.         stk[i->to.val].pop_back();
  116. }
  117.  
  118. static void readfn (Fn *fn) {
  119.     fillrpo(fn);
  120.     filldom(fn);
  121.     fillfron(fn);
  122.     filllive(fn);
  123.     phiinsert(fn);
  124.     auto *stk = new vector<Ref>[fn->ntmp];
  125.     int *counter;
  126.     counter = (int *)emalloc(sizeof(int) * fn->ntmp);
  127.     memset(counter, 0, sizeof(int)*fn->ntmp);
  128.     rename(fn, fn->start, stk, counter);
  129.     for (Blk *blk = fn->start; blk; blk = blk->link) {
  130.         cout << '@' << blk->name << endl;
  131.         for (Phi *p = blk->phi; p; p = p->link) {
  132.             cout << '%' << fn->tmp[p->to.val].name << " " << fn->tmp[p->to.val].phi << " ";
  133.             for (int i = 0; p->blk[i]; i++)
  134.                 cout << '@' << p->blk[i]->name << " " << fn->tmp[p->arg[i].val].phi << " ";
  135.             cout << endl;
  136.         }
  137.     }
  138. //    delete[] counter;
  139. //    delete[] stk;
  140. }
  141.  
  142. static void readdat (Dat *dat) {
  143.     (void) dat;
  144. }
  145.  
  146. int main (void) {
  147.     parse(stdin, "<stdin>", readdat, readfn);
  148.     freeall();
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement