Chinaunix首页 | 论坛 | 博客
  • 博客访问: 264556
  • 博文数量: 52
  • 博客积分: 1379
  • 博客等级: 大尉
  • 技术积分: 525
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-18 17:34
文章分类

全部博文(52)

文章存档

2011年(48)

2010年(4)

分类: C/C++

2011-03-01 22:05:54

把r寄存器保存在栈上,  释放寄存器

  1. /* save r to the memory stack, and mark it as being free */
  2. ST_FUNC void save_reg(int r)
  3. {
  4.     int l, saved, size, align;
  5.     SValue *p, sv;
  6.     CType *type;

  7.     /* modify all stack values */
  8.     saved = 0;
  9.     l = 0;
  10.     for(p=vstack;p<=vtop;p++) {
  11.         if ((p->r & VT_VALMASK) == r ||
  12.             ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
  13.             确实有在被使用才会真正的保存,否则保存备有意义
  14.             /* must save value on stack if not already done */
  15.             if (!saved) {
  16.                 /* NOTE: must reload 'r' because r might be equal to r2 */
  17.                 r = p->r & VT_VALMASK;
  18.                 /* store register in the stack */
  19.                 type = &p->type;
  20.                 if ((p->r & VT_LVAL) ||
  21.                     (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
  22. #ifdef TCC_TARGET_X86_64
  23.                     type = &char_pointer_type;
  24. #else
  25.                     type = &int_type;
  26. #endif
  27.                 用于计算大小,从而计算放在栈上的什么位置上,loc标记栈分配的位置
  28.                 size = type_size(type, &align);
  29.                 loc = (loc - size) & -align;
  30.                 sv.type.t = type->t;
  31.                 VT_LVAL是多余的
  32.                 sv.r = VT_LOCAL | VT_LVAL;
  33.                 sv.c.ul = loc;
  34.                 store 函数生成指令保存
  35.                 store(r, &sv);
  36.                 x86 浮点寄存器形成栈,释放栈顶st0
  37. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  38.                 /* x86 specific: need to pop fp register ST0 if saved */
  39.                 if (r == TREG_ST0) {
  40.                     o(0xd8dd); /* fstp %st(0) */
  41.                 }
  42. #endif
  43. #ifndef TCC_TARGET_X86_64
  44.                 /* special long long case */
  45.                 if ((type->t & VT_BTYPE) == VT_LLONG) {
  46.                     sv.c.ul += 4;
  47.                     store(p->r2, &sv);
  48.                 }
  49. #endif
  50.                 l = loc;
  51.                 saved = 1;
  52.             }
  53.             释放寄存器, 在需要时应该重新分配寄存器并load
  54.             /* mark that stack entry as being saved on the stack */
  55.             if (p->r & VT_LVAL) {
  56.                 /* also clear the bounded flag because the
  57.                    relocation address of the function was stored in
  58.                    p->c.ul */
  59.                 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
  60.             } else {
  61.                 p->r = lvalue_type(p->type.t) | VT_LOCAL;
  62.             }

  63.             p->r2 = VT_CONST;
  64.             p->c.ul = l;
  65.         }
  66.     }
  67. }



阅读(1516) | 评论(0) | 转发(0) |
0

上一篇:gen_modrm

下一篇:store

给主人留下些什么吧!~~