// File: /workspace/github_c_makefile_star2_O3/cogbee[P]vmir-latest/src/vmir_value.c
// Function: eval_constexpr
static void
eval_constexpr(ir_unit_t *iu, ir_value_t *iv, ir_constexpr_t *ic)
{
  switch(ic->ic_code) {
  case CST_CODE_CE_CAST:
    {
      ir_type_code_t tc = type_get(iu, iv->iv_type)->it_code;
      ir_value_t *src = value_get(iu, ic->ic_cast.src);

      if(src->iv_class == IR_VC_CE)
        eval_constexpr(iu, src, src->iv_ce);

      if(src->iv_class == IR_VC_ALIAS) {
        src = value_get(iu, src->iv_reg);
      }

      iv->iv_class = IR_VC_CONSTANT;

      switch(COMBINE3(tc, ic->ic_cast.op, src->iv_class)) {
      case COMBINE3(IR_TYPE_POINTER, CAST_INTTOPTR, IR_VC_CONSTANT):
        iv->iv_u32 = src->iv_u32;
        break;

      case COMBINE3(IR_TYPE_POINTER, CAST_BITCAST, IR_VC_GLOBALVAR):
        iv->iv_u32 = value_get_const32(iu, src);
        break;

      case COMBINE3(IR_TYPE_POINTER, CAST_BITCAST, IR_VC_CONSTANT):
        iv->iv_u32 = value_get_const32(iu, src);
        break;

      case COMBINE3(IR_TYPE_POINTER, CAST_BITCAST, IR_VC_FUNCTION):
        iv->iv_class = IR_VC_FUNCTION;
        iv->iv_func = src->iv_func;
        iv->iv_type = src->iv_type;
        break;

      case COMBINE3(IR_TYPE_INT32, CAST_PTRTOINT, IR_VC_CONSTANT):
      case COMBINE3(IR_TYPE_INT32, CAST_PTRTOINT, IR_VC_GLOBALVAR):
      case COMBINE3(IR_TYPE_INT8, CAST_PTRTOINT, IR_VC_CONSTANT):
      case COMBINE3(IR_TYPE_INT8, CAST_PTRTOINT, IR_VC_GLOBALVAR):
        iv->iv_u32 = value_get_const32(iu, src);
        break;

      case COMBINE3(IR_TYPE_INT32, CAST_PTRTOINT, IR_VC_FUNCTION):
        iv->iv_u32 = value_function_addr(src);
        break;

      default:
        parser_error(iu,
                     "Unable to constant-cast %s from %s using op %d class=%d",
                     type_str_index(iu, iv->iv_type),
                     type_str_index(iu, src->iv_type),
                     ic->ic_cast.op, src->iv_class);
      }
    }
    break;

  case CST_CODE_CE_BINOP:
    {
      ir_value_t *lhs = value_get(iu, ic->ic_binop.lhs);
      ir_value_t *rhs = value_get(iu, ic->ic_binop.rhs);
      ir_type_code_t tc = type_get(iu, iv->iv_type)->it_code;

      switch(tc) {
      case IR_TYPE_INT8:
      case IR_TYPE_INT32:
      case IR_TYPE_POINTER:
        {
          const uint32_t l = value_get_const32(iu, lhs);
          const uint32_t r = value_get_const32(iu, rhs);

          switch(ic->ic_binop.op) {
          case BINOP_ADD:  iv->iv_u32 = l + r; break;
          case BINOP_SUB:  iv->iv_u32 = l - r; break;
          case BINOP_MUL:  iv->iv_u32 = l * r; break;

          case BINOP_UDIV: iv->iv_u32 = l / r; break;
          case BINOP_SDIV: iv->iv_u32 = (int32_t)l / (int32_t)r; break;
          case BINOP_UREM: iv->iv_u32 = l % r; break;
          case BINOP_SREM: iv->iv_u32 = (int32_t)l % (int32_t)r; break;

          case BINOP_SHL:  iv->iv_u32 = l << r; break;
          case BINOP_LSHR: iv->iv_u32 = l >> r; break;
          case BINOP_ASHR: iv->iv_u32 = (int32_t)l >> r; break;
          case BINOP_AND:  iv->iv_u32 = l & r; break;
          case BINOP_OR:   iv->iv_u32 = l | r; break;
          case BINOP_XOR:  iv->iv_u32 = l ^ r; break;
          default:
            parser_error(iu, "Unable to handle opcode %d in constant binop",
                         ic->ic_binop.op);
          }
        }
        break;

      default:
        parser_error(iu, "Unable to handle constant binop for typecode %d",
                     tc);
      }
      iv->iv_class = IR_VC_CONSTANT;
    }
    break;

  case CST_CODE_CE_CMP:
    {
      ir_value_t *lhs = value_get(iu, ic->ic_cmp.lhs);
      ir_value_t *rhs = value_get(iu, ic->ic_cmp.rhs);
      ir_type_code_t opty = type_get(iu, ic->ic_cmp.opty)->it_code;

      switch(opty) {
      case IR_TYPE_INT32:
      case IR_TYPE_POINTER:
        {
          const uint32_t l = value_get_const32(iu, lhs);
          const uint32_t r = value_get_const32(iu, rhs);

          switch(ic->ic_cmp.pred) {
          case ICMP_EQ:  iv->iv_u32 = l == r; break;
          case ICMP_NE:  iv->iv_u32 = l != r; break;
          case ICMP_UGT: iv->iv_u32 = l >  r; break;
          case ICMP_UGE: iv->iv_u32 = l >= r; break;
          case ICMP_ULT: iv->iv_u32 = l <  r; break;
          case ICMP_ULE: iv->iv_u32 = l <= r; break;
          case ICMP_SGT: iv->iv_u32 = (int32_t)l >  (int32_t)r; break;
          case ICMP_SGE: iv->iv_u32 = (int32_t)l >= (int32_t)r; break;
          case ICMP_SLT: iv->iv_u32 = (int32_t)l <  (int32_t)r; break;
          case ICMP_SLE: iv->iv_u32 = (int32_t)l <= (int32_t)r; break;

          default:
            parser_error(iu, "Unable to handle opcode %d in constant cmp",
                         ic->ic_binop.op);
          }
        }
        break;

      default:
        parser_error(iu, "Unable to handle constant cmp for type %s",
                     type_str_index(iu, ic->ic_cmp.opty));
      }
      iv->iv_class = IR_VC_CONSTANT;
    }
    break;

  case CST_CODE_CE_GEP:
  case CST_CODE_CE_INBOUNDS_GEP:
    {
      const int *vv = ic->ic_gep.values;
      ir_value_t *curval = value_get(iu, vv[0]);

      if(curval->iv_class == IR_VC_CE)
        eval_constexpr(iu, curval, curval->iv_ce);

      uint32_t addr = value_get_const32(iu, curval);
      int type_index = curval->iv_type;
      for(int i = 1; i < ic->ic_gep.num_values; i++) {
        ir_type_t *curtype = type_get(iu, type_index);
        curval = value_get(iu, vv[i]);

        int x = value_get_const32(iu, curval);

        switch(curtype->it_code) {
        default:
          parser_error(iu, "Unable to handle type %s in constexpr GEP",
                       type_str(iu, curtype));

        case IR_TYPE_POINTER:
          addr += x * type_sizeof(iu, curtype->it_pointer.pointee);
          type_index = curtype->it_pointer.pointee;
          break;

        case IR_TYPE_STRUCT:
          if(x >= curtype->it_struct.num_elements)
            parser_error(iu, "Bad index %d info struct", x);
          type_index = curtype->it_struct.elements[x].type;
          addr += curtype->it_struct.elements[x].offset;
          break;

        case IR_TYPE_ARRAY:
          addr +=  x * type_sizeof(iu, curtype->it_array.element_type);
          type_index = curtype->it_array.element_type;
          break;
        }
      }
      iv->iv_type = type_make_pointer(iu, type_index, 1);
      iv->iv_u32 = addr;
      iv->iv_class = IR_VC_CONSTANT;

      free(ic->ic_gep.values);
    }
    break;

  default:
    parser_error(iu, "Unable to eval constexpr for code %d", ic->ic_code);
  }
  free(ic);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r15
movl (%rdx), %r8d
leal -0xa(%r8), %eax
cmpl $0xa, %eax
ja 0x2130a
movq %rdx, %r13
movq %rsi, %r14
leaq 0x3b06(%rip), %rcx      # 0x248d0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movl (%rax), %r8d
cmpq %r8, 0x188(%r15)
jbe 0x212bf
movq %r14, 0x8(%rsp)
movq 0x180(%r15), %rax
movq (%rax,%r8,8), %r12
cmpl $0x8, (%r12)
jne 0x20e13
movq 0x30(%r12), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x20d9c
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a31
movl %eax, %ebp
movl 0x24(%r12), %esi
cmpl $0x2, 0x8(%r13)
jl 0x20ee9
movl $0x1, %ebx
movl %esi, %r8d
cmpq %r8, 0x170(%r15)
jbe 0x2125c
movq 0x10(%rsp), %rax
movl (%rax,%rbx,4), %eax
cmpq %rax, 0x188(%r15)
jbe 0x21241
leaq (%r8,%r8,4), %r12
shlq $0x3, %r12
addq 0x168(%r15), %r12
movq 0x180(%r15), %rcx
movq (%rcx,%rax,8), %rsi
movq %r15, %rdi
callq 0x16a31
movl %eax, %r14d
movl (%r12), %eax
testl %eax, %eax
je 0x20eb7
cmpl $0xa, %eax
je 0x20ea0
cmpl $0xb, %eax
jne 0x21274
movl 0x8(%r12), %esi
addq $0x8, %r12
jmp 0x20ea9
movl 0xc(%r12), %esi
addq $0xc, %r12
movq %r15, %rdi
callq 0xf5f0
imull %r14d, %eax
jmp 0x20ed3
cmpl 0x18(%r12), %r14d
jge 0x2129f
movq 0x8(%r12), %rax
movslq %r14d, %rcx
leaq (%rax,%rcx,8), %r12
movl 0x4(%r12), %eax
addl %eax, %ebp
incq %rbx
movl (%r12), %esi
movslq 0x8(%r13), %rax
cmpq %rax, %rbx
jl 0x20e35
movq %r15, %rdi
movl $0x1, %edx
callq 0xf297
movq 0x8(%rsp), %rcx
movl %eax, 0x24(%rcx)
movl %ebp, 0x30(%rcx)
movl $0x3, (%rcx)
movq 0x10(%r13), %rdi
callq 0x3030
jmp 0x2122b
movl 0xc(%r13), %eax
movq 0x188(%r15), %rcx
cmpq %rax, %rcx
jbe 0x21241
movl 0x10(%r13), %r9d
cmpq %r9, %rcx
jbe 0x212d7
movl 0x8(%r13), %r8d
cmpq %r8, 0x170(%r15)
jbe 0x2125c
movq 0x180(%r15), %rcx
movq (%rcx,%rax,8), %rsi
movq (%rcx,%r9,8), %r12
movq 0x168(%r15), %rax
leaq (%r8,%r8,4), %rcx
movl (%rax,%rcx,8), %eax
cmpl $0xb, %eax
je 0x20f72
cmpl $0x5, %eax
jne 0x21325
movq %r15, %rdi
callq 0x16a31
movl %eax, %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a31
movl 0x14(%r13), %ecx
addl $-0x20, %ecx
cmpl $0x9, %ecx
ja 0x213d3
leaq 0x395e(%rip), %rdx      # 0x248fc
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl %eax, %ebp
sete %al
jmp 0x211d4
movl 0x24(%r14), %r8d
cmpq %r8, 0x170(%r15)
jbe 0x2125c
movl 0xc(%r13), %eax
cmpq %rax, 0x188(%r15)
jbe 0x21241
leaq (%r8,%r8,4), %rcx
movq 0x168(%r15), %rdx
movl (%rdx,%rcx,8), %ebx
movq 0x180(%r15), %rcx
movq (%rcx,%rax,8), %r12
movl (%r12), %eax
cmpl $0x8, %eax
jne 0x21009
movq 0x30(%r12), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x20d9c
movl (%r12), %eax
cmpl $0xa, %eax
jne 0x2102b
movl 0x30(%r12), %r8d
cmpq %r8, 0x188(%r15)
jbe 0x212bf
movq 0x180(%r15), %rax
movq (%rax,%r8,8), %r12
movl $0x3, (%r14)
shll $0x10, %ebx
movl 0x8(%r13), %eax
shll $0x8, %eax
orl %ebx, %eax
movl (%r12), %ecx
orl %ecx, %eax
cmpl $0xb0a02, %eax          # imm = 0xB0A02
jg 0x21133
leal -0x30902(%rax), %edx
cmpl $0x2, %edx
jb 0x2117c
leal -0x50902(%rax), %edx
cmpl $0x2, %edx
jb 0x2117c
cmpl $0x50901, %eax          # imm = 0x50901
jne 0x21369
cmpl $0x1, %ecx
jne 0x213b4
movq 0x30(%r12), %rax
movl 0x34(%rax), %eax
jmp 0x21187
movl 0xc(%r13), %r8d
movq 0x188(%r15), %rcx
cmpq %r8, %rcx
jbe 0x212bf
movl 0x10(%r13), %eax
cmpq %rax, %rcx
jbe 0x21241
movl 0x24(%r14), %r9d
cmpq %r9, 0x170(%r15)
jbe 0x212ef
movq 0x168(%r15), %rcx
leaq (%r9,%r9,4), %rdx
movl (%rcx,%rdx,8), %r9d
cmpl $0xb, %r9d
ja 0x2134b
movl $0x828, %ecx            # imm = 0x828
btl %r9d, %ecx
jae 0x2134b
movq 0x180(%r15), %rcx
movq (%rcx,%r8,8), %rsi
movq (%rcx,%rax,8), %r12
movq %r15, %rdi
callq 0x16a31
movl %eax, %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a31
movl 0x8(%r13), %r8d
cmpq $0xc, %r8
ja 0x213f2
movl %eax, %ecx
leaq 0x3801(%rip), %rax      # 0x24924
movslq (%rax,%r8,4), %rdx
addq %rax, %rdx
jmpq *%rdx
addl %ebp, %ecx
jmp 0x21220
cmpl $0xb0b01, %eax          # imm = 0xB0B01
jg 0x2116a
cmpl $0xb0a03, %eax          # imm = 0xB0A03
je 0x21190
cmpl $0xb0b01, %eax          # imm = 0xB0B01
jne 0x21369
movl $0x1, (%r14)
movq 0x30(%r12), %rax
movq %rax, 0x30(%r14)
movl 0x24(%r12), %eax
movl %eax, 0x24(%r14)
jmp 0x2122b
cmpl $0xb0b02, %eax          # imm = 0xB0B02
je 0x2117c
cmpl $0xb0b03, %eax          # imm = 0xB0B03
jne 0x21369
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a31
movl %eax, 0x30(%r14)
jmp 0x2122b
movl 0x30(%r12), %eax
jmp 0x21187
cmpl %eax, %ebp
setb %al
jmp 0x211d4
cmpl %eax, %ebp
setle %al
jmp 0x211d4
cmpl %eax, %ebp
seta %al
jmp 0x211d4
cmpl %eax, %ebp
setae %al
jmp 0x211d4
cmpl %eax, %ebp
setge %al
jmp 0x211d4
cmpl %eax, %ebp
setne %al
jmp 0x211d4
cmpl %eax, %ebp
setbe %al
jmp 0x211d4
cmpl %eax, %ebp
setg %al
jmp 0x211d4
cmpl %eax, %ebp
setl %al
movzbl %al, %eax
movl %eax, 0x30(%r14)
jmp 0x21224
orl %ebp, %ecx
jmp 0x21220
movl %ebp, %eax
cltd
idivl %ecx
jmp 0x211fb
xorl %ebp, %ecx
jmp 0x21220
sarl %cl, %ebp
jmp 0x2121e
imull %ebp, %ecx
jmp 0x21220
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
jmp 0x21220
shll %cl, %ebp
jmp 0x2121e
subl %ecx, %ebp
jmp 0x2121e
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
jmp 0x21214
movl %ebp, %eax
cltd
idivl %ecx
movl %edx, %ecx
jmp 0x21220
andl %ebp, %ecx
jmp 0x21220
shrl %cl, %ebp
movl %ebp, %ecx
movl %ecx, 0x30(%r14)
movl $0x3, (%r14)
movq %r13, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3030
leaq 0x4328(%rip), %rsi      # 0x25570
leaq 0x4ad5(%rip), %rcx      # 0x25d24
movq %r15, %rdi
movl $0x143, %edx            # imm = 0x143
movl %eax, %r8d
jmp 0x212b8
leaq 0x4859(%rip), %rsi      # 0x25abc
leaq 0x488b(%rip), %rcx      # 0x25af5
movq %r15, %rdi
movl $0x7c, %edx
jmp 0x212b8
movq %r15, %rdi
movq %r12, %rsi
callq 0x415e
leaq 0x42ea(%rip), %rsi      # 0x25570
leaq 0x6c40(%rip), %rcx      # 0x27ecd
movq %r15, %rdi
movl $0x3eb, %edx            # imm = 0x3EB
movq %rax, %r8
xorl %eax, %eax
callq 0xca98
leaq 0x42ca(%rip), %rsi      # 0x25570
leaq 0x4eec(%rip), %rcx      # 0x26199
movq %r15, %rdi
movl $0x3f4, %edx            # imm = 0x3F4
movl %r14d, %r8d
xorl %eax, %eax
callq 0xca98
leaq 0x42aa(%rip), %rsi      # 0x25570
leaq 0x4a57(%rip), %rcx      # 0x25d24
movq %r15, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x21272
leaq 0x4292(%rip), %rsi      # 0x25570
leaq 0x4a3f(%rip), %rcx      # 0x25d24
movq %r15, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x21305
leaq 0x47c6(%rip), %rsi      # 0x25abc
leaq 0x47f8(%rip), %rcx      # 0x25af5
movq %r15, %rdi
movl $0x7c, %edx
movl %r9d, %r8d
jmp 0x212b8
leaq 0x425f(%rip), %rsi      # 0x25570
leaq 0x6bdf(%rip), %rcx      # 0x27ef7
movq %r15, %rdi
movl $0x408, %edx            # imm = 0x408
jmp 0x21272
movq %r15, %rdi
movl %r8d, %esi
callq 0x120fa
leaq 0x4239(%rip), %rsi      # 0x25570
leaq 0x6b65(%rip), %rcx      # 0x27ea3
movq %r15, %rdi
movl $0x3d1, %edx            # imm = 0x3D1
jmp 0x21295
leaq 0x421e(%rip), %rsi      # 0x25570
leaq 0x6aef(%rip), %rcx      # 0x27e48
movq %r15, %rdi
movl $0x3a9, %edx            # imm = 0x3A9
movl %r9d, %r8d
jmp 0x212b8
movl 0x24(%r14), %esi
movq %r15, %rdi
callq 0x120fa
movq %rax, %r14
movl 0x24(%r12), %esi
movq %r15, %rdi
callq 0x120fa
movl 0x8(%r13), %r10d
movl (%r12), %r11d
leaq 0x41dc(%rip), %rsi      # 0x25570
leaq 0x6a48(%rip), %rcx      # 0x27de3
movq %r15, %rdi
movl $0x37d, %edx            # imm = 0x37D
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0xca98
leaq 0x5abf(%rip), %rdi      # 0x26e7a
leaq 0x41ae(%rip), %rsi      # 0x25570
leaq 0x5ad0(%rip), %rcx      # 0x26e99
movl $0x12b, %edx            # imm = 0x12B
callq 0x31e0
movl 0x8(%r13), %r8d
leaq 0x4192(%rip), %rsi      # 0x25570
leaq 0x6a93(%rip), %rcx      # 0x27e78
movq %r15, %rdi
movl $0x3ca, %edx            # imm = 0x3CA
jmp 0x212b8
leaq 0x4177(%rip), %rsi      # 0x25570
leaq 0x6a1b(%rip), %rcx      # 0x27e1b
movq %r15, %rdi
movl $0x3a2, %edx            # imm = 0x3A2
jmp 0x21272