// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl (%rdx), %r8d
leal -0xa(%r8), %eax
cmpl $0xa, %eax
ja 0x1c4d2
movq %rdx, %r13
movq %rsi, %r15
leaq 0x39a6(%rip), %rcx      # 0x1fa44
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %r12, (%rsp)
jmpq *%rax
movq %r15, 0x8(%rsp)
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movl (%rax), %esi
movq %r12, %rdi
callq 0x102f1
movq %rax, %r14
cmpl $0x8, (%rax)
jne 0x1c0da
movq 0x30(%r14), %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x1c070
movq %r12, %rdi
movq %r14, %rsi
callq 0x14006
movl %eax, %ebp
addq $0x24, %r14
pushq $0x1
popq %rbx
movl (%r14), %esi
movslq 0x8(%r13), %rax
cmpq %rax, %rbx
jge 0x1c19b
movq %r13, %r15
movq %r12, %rdi
callq 0xe719
movq %r12, %r14
movq %rax, %r12
movq 0x10(%rsp), %rax
movl (%rax,%rbx,4), %esi
movq %r14, %rdi
callq 0x102f1
movq %r14, %rdi
movq %rax, %rsi
callq 0x14006
movl %eax, %r13d
movl (%r12), %eax
testl %eax, %eax
je 0x1c16f
cmpl $0xa, %eax
je 0x1c151
cmpl $0xb, %eax
jne 0x1c482
movq %r12, %r14
addq $0x8, %r14
movl 0x8(%r12), %esi
jmp 0x1c15d
movq %r12, %r14
addq $0xc, %r14
movl 0xc(%r12), %esi
movq (%rsp), %r12
movq %r12, %rdi
callq 0xe68d
imull %r13d, %eax
jmp 0x1c18e
cmpl 0x18(%r12), %r13d
jge 0x1c4b1
movq 0x8(%r12), %rax
movslq %r13d, %rcx
leaq (%rax,%rcx,8), %r14
movl 0x4(%r14), %eax
movq (%rsp), %r12
addl %eax, %ebp
incq %rbx
movq %r15, %r13
jmp 0x1c0ee
pushq $0x1
popq %rdx
movq %r12, %rdi
callq 0xe37f
movq 0x8(%rsp), %rcx
movl %eax, 0x24(%rcx)
movl %ebp, 0x30(%rcx)
movl $0x3, (%rcx)
movq 0x10(%r13), %rdi
callq 0x3030
jmp 0x1c46c
movl 0xc(%r13), %esi
movq %r12, %rdi
callq 0x102f1
movq %rax, %r14
movl 0x10(%r13), %esi
movq %r12, %rdi
callq 0x102f1
movq %r12, %rdi
movq %rax, %r12
movl 0x8(%r13), %esi
callq 0xe719
movl (%rax), %eax
cmpl $0xb, %eax
je 0x1c1ff
cmpl $0x5, %eax
jne 0x1c4ea
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14006
movl %eax, %ebp
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14006
movl 0x14(%r13), %ecx
addl $-0x20, %ecx
cmpl $0x9, %ecx
ja 0x1c57a
leaq 0x383e(%rip), %rdx      # 0x1fa70
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl %eax, %ebp
sete %al
jmp 0x1c410
movl 0x24(%r15), %esi
movq %r12, %rdi
callq 0xe719
movl (%rax), %ebx
movl 0xc(%r13), %esi
movq %r12, %rdi
callq 0x102f1
movq %rax, %r12
movl (%rax), %eax
cmpl $0x8, %eax
jne 0x1c27e
movq 0x30(%r12), %rdx
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x1c070
movl (%r12), %eax
cmpl $0xa, %eax
jne 0x1c294
movl 0x30(%r12), %esi
movq (%rsp), %rdi
callq 0x102f1
movq %rax, %r12
movl $0x3, (%r15)
shll $0x10, %ebx
movl 0x8(%r13), %eax
shll $0x8, %eax
orl %ebx, %eax
orl (%r12), %eax
leal -0x30902(%rax), %ecx
cmpl $0x2, %ecx
jb 0x1c396
leal -0x50902(%rax), %ecx
cmpl $0x2, %ecx
jb 0x1c396
cmpl $0x50901, %eax          # imm = 0x50901
je 0x1c3c9
cmpl $0xb0b03, %eax          # imm = 0xB0B03
je 0x1c396
cmpl $0xb0b01, %eax          # imm = 0xB0B01
je 0x1c3ab
cmpl $0xb0b02, %eax          # imm = 0xB0B02
je 0x1c396
cmpl $0xb0a03, %eax          # imm = 0xB0A03
jne 0x1c52b
movl 0x30(%r12), %eax
jmp 0x1c3a2
movl 0xc(%r13), %esi
movq %r12, %rdi
callq 0x102f1
movq %rax, %r14
movl 0x10(%r13), %esi
movq %r12, %rdi
callq 0x102f1
movq %r12, %rdi
movq %rax, %r12
movq %r15, %rbx
movl 0x24(%r15), %esi
callq 0xe719
movl (%rax), %r8d
cmpl $0xb, %r8d
ja 0x1c512
movl $0x828, %eax            # imm = 0x828
btl %r8d, %eax
jae 0x1c512
movq (%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x14006
movl %eax, %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x14006
movl 0x8(%r13), %r8d
cmpq $0xc, %r8
ja 0x1c599
movl %eax, %ecx
leaq 0x3712(%rip), %rax      # 0x1fa98
movslq (%rax,%r8,4), %rdx
addq %rax, %rdx
jmpq *%rdx
addl %ebp, %ecx
jmp 0x1c463
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x14006
movl %eax, 0x30(%r15)
jmp 0x1c46c
movl $0x1, (%r15)
movq 0x30(%r12), %rax
movq %rax, 0x30(%r15)
movl 0x24(%r12), %eax
movl %eax, 0x24(%r15)
jmp 0x1c46c
movq %r12, %rdi
callq 0x196e0
jmp 0x1c3a2
cmpl %eax, %ebp
setb %al
jmp 0x1c410
cmpl %eax, %ebp
setle %al
jmp 0x1c410
cmpl %eax, %ebp
seta %al
jmp 0x1c410
cmpl %eax, %ebp
setae %al
jmp 0x1c410
cmpl %eax, %ebp
setge %al
jmp 0x1c410
cmpl %eax, %ebp
setne %al
jmp 0x1c410
cmpl %eax, %ebp
setbe %al
jmp 0x1c410
cmpl %eax, %ebp
setg %al
jmp 0x1c410
cmpl %eax, %ebp
setl %al
movzbl %al, %eax
movl %eax, 0x30(%r15)
movl $0x3, (%r15)
jmp 0x1c46c
orl %ebp, %ecx
jmp 0x1c463
movl %ebp, %eax
cltd
idivl %ecx
jmp 0x1c43e
xorl %ebp, %ecx
jmp 0x1c463
sarl %cl, %ebp
jmp 0x1c461
imull %ebp, %ecx
jmp 0x1c463
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
jmp 0x1c463
shll %cl, %ebp
jmp 0x1c461
subl %ecx, %ebp
jmp 0x1c461
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
jmp 0x1c457
movl %ebp, %eax
cltd
idivl %ecx
movl %edx, %ecx
jmp 0x1c463
andl %ebp, %ecx
jmp 0x1c463
shrl %cl, %ebp
movl %ebp, %ecx
movl %ecx, 0x30(%rbx)
movl $0x3, (%rbx)
movq %r13, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3030
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x408f
leaq 0x420e(%rip), %rsi      # 0x206a6
leaq 0x6b99(%rip), %rcx      # 0x23038
movq %rbx, %rdi
movl $0x3eb, %edx            # imm = 0x3EB
movq %rax, %r8
xorl %eax, %eax
callq 0xbed2
leaq 0x41ee(%rip), %rsi      # 0x206a6
leaq 0x4e45(%rip), %rcx      # 0x21304
movq (%rsp), %rdi
movl $0x3f4, %edx            # imm = 0x3F4
movl %r13d, %r8d
xorl %eax, %eax
callq 0xbed2
leaq 0x41cd(%rip), %rsi      # 0x206a6
leaq 0x6b82(%rip), %rcx      # 0x23062
movq %r12, %rdi
movl $0x408, %edx            # imm = 0x408
jmp 0x1c4cb
movl 0x8(%r13), %esi
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x105cd
leaq 0x41a5(%rip), %rsi      # 0x206a6
leaq 0x6b06(%rip), %rcx      # 0x2300e
movq %rbx, %rdi
movl $0x3d1, %edx            # imm = 0x3D1
jmp 0x1c4a7
leaq 0x418d(%rip), %rsi      # 0x206a6
leaq 0x6a93(%rip), %rcx      # 0x22fb3
movq (%rsp), %rdi
movl $0x3a9, %edx            # imm = 0x3A9
jmp 0x1c4cb
movl 0x24(%r15), %esi
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x105cd
movq %rax, %r14
movl 0x24(%r12), %esi
movq %rbx, %rdi
callq 0x105cd
movl 0x8(%r13), %r10d
movl (%r12), %r11d
leaq 0x414c(%rip), %rsi      # 0x206a6
leaq 0x69ed(%rip), %rcx      # 0x22f4e
movq %rbx, %rdi
movl $0x37d, %edx            # imm = 0x37D
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0xbed2
movl 0x8(%r13), %r8d
leaq 0x4121(%rip), %rsi      # 0x206a6
leaq 0x6a57(%rip), %rcx      # 0x22fe3
movq %rbx, %rdi
movl $0x3ca, %edx            # imm = 0x3CA
jmp 0x1c4cb
leaq 0x4106(%rip), %rsi      # 0x206a6
leaq 0x69df(%rip), %rcx      # 0x22f86
movq %r15, %rdi
movl $0x3a2, %edx            # imm = 0x3A2
jmp 0x1c4e8