// File: /workspace/github_c_makefile_star2_O1/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 0x211d9
movq %rdx, %r13
movq %rsi, %r14
leaq 0x3bc7(%rip), %rcx      # 0x24864
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 0x2118e
movq %r14, 0x8(%rsp)
movq 0x180(%r15), %rax
movq (%rax,%r8,8), %r12
cmpl $0x8, (%r12)
jne 0x20ce6
movq 0x30(%r12), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x20c6f
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a3f
movl %eax, %ebp
movl 0x24(%r12), %esi
cmpl $0x2, 0x8(%r13)
jl 0x20dbc
movl $0x1, %ebx
movl %esi, %r8d
cmpq %r8, 0x170(%r15)
jbe 0x2112b
movq 0x10(%rsp), %rax
movl (%rax,%rbx,4), %eax
cmpq %rax, 0x188(%r15)
jbe 0x21110
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 0x16a3f
movl %eax, %r14d
movl (%r12), %eax
testl %eax, %eax
je 0x20d8a
cmpl $0xa, %eax
je 0x20d73
cmpl $0xb, %eax
jne 0x21143
movl 0x8(%r12), %esi
addq $0x8, %r12
jmp 0x20d7c
movl 0xc(%r12), %esi
addq $0xc, %r12
movq %r15, %rdi
callq 0xf67c
imull %r14d, %eax
jmp 0x20da6
cmpl 0x18(%r12), %r14d
jge 0x2116e
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 0x20d08
movq %r15, %rdi
movl $0x1, %edx
callq 0xf30d
movq 0x8(%rsp), %rcx
movl %eax, 0x24(%rcx)
movl %ebp, 0x30(%rcx)
movl $0x3, (%rcx)
movq 0x10(%r13), %rdi
callq 0x3030
jmp 0x210fa
movl 0xc(%r13), %eax
movq 0x188(%r15), %rcx
cmpq %rax, %rcx
jbe 0x21110
movl 0x10(%r13), %r9d
cmpq %r9, %rcx
jbe 0x211a6
movl 0x8(%r13), %r8d
cmpq %r8, 0x170(%r15)
jbe 0x2112b
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 0x20e45
cmpl $0x5, %eax
jne 0x211f4
movq %r15, %rdi
callq 0x16a3f
movl %eax, %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a3f
movl 0x14(%r13), %ecx
addl $-0x20, %ecx
cmpl $0x9, %ecx
ja 0x212a2
leaq 0x3a1f(%rip), %rdx      # 0x24890
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl %eax, %ebp
sete %al
jmp 0x210a3
movl 0x24(%r14), %r8d
cmpq %r8, 0x170(%r15)
jbe 0x2112b
movl 0xc(%r13), %eax
cmpq %rax, 0x188(%r15)
jbe 0x21110
leaq (%r8,%r8,4), %rcx
movq 0x168(%r15), %rdx
movl (%rdx,%rcx,8), %ebx
movq 0x180(%r15), %rcx
movq (%rcx,%rax,8), %r12
cmpl $0x8, (%r12)
jne 0x20ed6
movq 0x30(%r12), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x20c6f
cmpl $0xa, (%r12)
jne 0x20efa
movl 0x30(%r12), %r8d
cmpq %r8, 0x188(%r15)
jbe 0x2118e
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 0x21002
leal -0x30902(%rax), %edx
cmpl $0x2, %edx
jb 0x2104b
leal -0x50902(%rax), %edx
cmpl $0x2, %edx
jb 0x2104b
cmpl $0x50901, %eax          # imm = 0x50901
jne 0x21238
cmpl $0x1, %ecx
jne 0x21283
movq 0x30(%r12), %rax
movl 0x34(%rax), %eax
jmp 0x21056
movl 0xc(%r13), %r8d
movq 0x188(%r15), %rcx
cmpq %r8, %rcx
jbe 0x2118e
movl 0x10(%r13), %eax
cmpq %rax, %rcx
jbe 0x21110
movl 0x24(%r14), %r9d
cmpq %r9, 0x170(%r15)
jbe 0x211be
movq 0x168(%r15), %rcx
leaq (%r9,%r9,4), %rdx
movl (%rcx,%rdx,8), %r9d
cmpl $0xb, %r9d
ja 0x2121a
movl $0x828, %ecx            # imm = 0x828
btl %r9d, %ecx
jae 0x2121a
movq 0x180(%r15), %rcx
movq (%rcx,%r8,8), %rsi
movq (%rcx,%rax,8), %r12
movq %r15, %rdi
callq 0x16a3f
movl %eax, %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a3f
movl 0x8(%r13), %r8d
cmpq $0xc, %r8
ja 0x212c1
movl %eax, %ecx
leaq 0x38c6(%rip), %rax      # 0x248b8
movslq (%rax,%r8,4), %rdx
addq %rax, %rdx
jmpq *%rdx
addl %ebp, %ecx
jmp 0x210ef
cmpl $0xb0b01, %eax          # imm = 0xB0B01
jg 0x21039
cmpl $0xb0a03, %eax          # imm = 0xB0A03
je 0x2105f
cmpl $0xb0b01, %eax          # imm = 0xB0B01
jne 0x21238
movl $0x1, (%r14)
movq 0x30(%r12), %rax
movq %rax, 0x30(%r14)
movl 0x24(%r12), %eax
movl %eax, 0x24(%r14)
jmp 0x210fa
cmpl $0xb0b02, %eax          # imm = 0xB0B02
je 0x2104b
cmpl $0xb0b03, %eax          # imm = 0xB0B03
jne 0x21238
movq %r15, %rdi
movq %r12, %rsi
callq 0x16a3f
movl %eax, 0x30(%r14)
jmp 0x210fa
movl 0x30(%r12), %eax
jmp 0x21056
cmpl %eax, %ebp
setb %al
jmp 0x210a3
cmpl %eax, %ebp
setle %al
jmp 0x210a3
cmpl %eax, %ebp
seta %al
jmp 0x210a3
cmpl %eax, %ebp
setae %al
jmp 0x210a3
cmpl %eax, %ebp
setge %al
jmp 0x210a3
cmpl %eax, %ebp
setne %al
jmp 0x210a3
cmpl %eax, %ebp
setbe %al
jmp 0x210a3
cmpl %eax, %ebp
setg %al
jmp 0x210a3
cmpl %eax, %ebp
setl %al
movzbl %al, %eax
movl %eax, 0x30(%r14)
jmp 0x210f3
orl %ebp, %ecx
jmp 0x210ef
movl %ebp, %eax
cltd
idivl %ecx
jmp 0x210ca
xorl %ebp, %ecx
jmp 0x210ef
sarl %cl, %ebp
jmp 0x210ed
imull %ebp, %ecx
jmp 0x210ef
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
jmp 0x210ef
shll %cl, %ebp
jmp 0x210ed
subl %ecx, %ebp
jmp 0x210ed
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
jmp 0x210e3
movl %ebp, %eax
cltd
idivl %ecx
movl %edx, %ecx
jmp 0x210ef
andl %ebp, %ecx
jmp 0x210ef
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 0x43a9(%rip), %rsi      # 0x254c0
leaq 0x4b56(%rip), %rcx      # 0x25c74
movq %r15, %rdi
movl $0x143, %edx            # imm = 0x143
movl %eax, %r8d
jmp 0x21187
leaq 0x48da(%rip), %rsi      # 0x25a0c
leaq 0x490c(%rip), %rcx      # 0x25a45
movq %r15, %rdi
movl $0x7c, %edx
jmp 0x21187
movq %r15, %rdi
movq %r12, %rsi
callq 0x4112
leaq 0x436b(%rip), %rsi      # 0x254c0
leaq 0x6d27(%rip), %rcx      # 0x27e83
movq %r15, %rdi
movl $0x3eb, %edx            # imm = 0x3EB
movq %rax, %r8
xorl %eax, %eax
callq 0xca73
leaq 0x434b(%rip), %rsi      # 0x254c0
leaq 0x4fd3(%rip), %rcx      # 0x2614f
movq %r15, %rdi
movl $0x3f4, %edx            # imm = 0x3F4
movl %r14d, %r8d
xorl %eax, %eax
callq 0xca73
leaq 0x432b(%rip), %rsi      # 0x254c0
leaq 0x4ad8(%rip), %rcx      # 0x25c74
movq %r15, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x21141
leaq 0x4313(%rip), %rsi      # 0x254c0
leaq 0x4ac0(%rip), %rcx      # 0x25c74
movq %r15, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x211d4
leaq 0x4847(%rip), %rsi      # 0x25a0c
leaq 0x4879(%rip), %rcx      # 0x25a45
movq %r15, %rdi
movl $0x7c, %edx
movl %r9d, %r8d
jmp 0x21187
leaq 0x42e0(%rip), %rsi      # 0x254c0
leaq 0x6cc6(%rip), %rcx      # 0x27ead
movq %r15, %rdi
movl $0x408, %edx            # imm = 0x408
jmp 0x21141
movq %r15, %rdi
movl %r8d, %esi
callq 0x12335
leaq 0x42ba(%rip), %rsi      # 0x254c0
leaq 0x6c4c(%rip), %rcx      # 0x27e59
movq %r15, %rdi
movl $0x3d1, %edx            # imm = 0x3D1
jmp 0x21164
leaq 0x429f(%rip), %rsi      # 0x254c0
leaq 0x6bd6(%rip), %rcx      # 0x27dfe
movq %r15, %rdi
movl $0x3a9, %edx            # imm = 0x3A9
movl %r9d, %r8d
jmp 0x21187
movl 0x24(%r14), %esi
movq %r15, %rdi
callq 0x12335
movq %rax, %r14
movl 0x24(%r12), %esi
movq %r15, %rdi
callq 0x12335
movl 0x8(%r13), %r10d
movl (%r12), %r11d
leaq 0x425d(%rip), %rsi      # 0x254c0
leaq 0x6b2f(%rip), %rcx      # 0x27d99
movq %r15, %rdi
movl $0x37d, %edx            # imm = 0x37D
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0xca73
leaq 0x5ba6(%rip), %rdi      # 0x26e30
leaq 0x422f(%rip), %rsi      # 0x254c0
leaq 0x5bb7(%rip), %rcx      # 0x26e4f
movl $0x12b, %edx            # imm = 0x12B
callq 0x31e0
movl 0x8(%r13), %r8d
leaq 0x4213(%rip), %rsi      # 0x254c0
leaq 0x6b7a(%rip), %rcx      # 0x27e2e
movq %r15, %rdi
movl $0x3ca, %edx            # imm = 0x3CA
jmp 0x21187
leaq 0x41f8(%rip), %rsi      # 0x254c0
leaq 0x6b02(%rip), %rcx      # 0x27dd1
movq %r15, %rdi
movl $0x3a2, %edx            # imm = 0x3A2
jmp 0x21141