// File: /workspace/github_c_makefile_star2_O3/cogbee[P]vmir-latest/src/vmir_bitcode_parser.c
// Function: constants_rec_handler
static void
constants_rec_handler(ir_unit_t *iu, int op,
                      unsigned int argc, const int64_t *argv)
{
  ir_value_t *iv;
  ir_constexpr_t *ic;

  if(op == CST_CODE_SETTYPE) {
    if(argc < 1)
      parser_error(iu, "Bad # of args for CST_CODE_SETTYPE");
    assert(argc > 0);
    iu->iu_current_type = argv[0];
    return;
  }

  iv = value_append_and_get(iu);
  iv->iv_type = iu->iu_current_type;

  switch(op) {

  case CST_CODE_UNDEF:
  case CST_CODE_NULL:
    switch(type_get(iu, iv->iv_type)->it_code) {
    case IR_TYPE_INT1:
    case IR_TYPE_INT8:
    case IR_TYPE_INT16:
    case IR_TYPE_INT32:
    case IR_TYPE_POINTER:
    case IR_TYPE_DOUBLE:
    case IR_TYPE_INT64:
    case IR_TYPE_FLOAT:
    case IR_TYPE_INTx:
      iv->iv_class = IR_VC_CONSTANT;
      iv->iv_u64 = 0;
      break;
    case IR_TYPE_ARRAY:
    case IR_TYPE_STRUCT:
      iv->iv_class = IR_VC_ZERO_INITIALIZER;
      break;
    default:
      parser_error(iu, "Bad type (%s) for NULL integer constant",
                   type_str_index(iu, iu->iu_current_type));
    }
    break;

  case CST_CODE_INTEGER:
    assert(argc > 0);
    iv->iv_class = IR_VC_CONSTANT;
    switch(type_get(iu, iv->iv_type)->it_code) {
    case IR_TYPE_INT1:
    case IR_TYPE_INT8:
    case IR_TYPE_INT16:
    case IR_TYPE_INT32:
      iv->iv_u32 = read_sign_rotated(argv);
      break;

    case IR_TYPE_INT64:
    case IR_TYPE_INTx:
      iv->iv_u64 = read_sign_rotated(argv);
      break;
    default:
      parser_error(iu, "Bad type (%s) for integer constant",
                          type_str_index(iu, iu->iu_current_type));
    }

    break;


  case CST_CODE_FLOAT:
    assert(argc > 0);
    iv->iv_class = IR_VC_CONSTANT;
    switch(type_get(iu, iv->iv_type)->it_code) {
    case IR_TYPE_FLOAT:
      iv->iv_u32 = argv[0]; // iv_u32 is union with iv_float
      break;
    case IR_TYPE_DOUBLE:
      iv->iv_u64 = argv[0]; // iv_u64 is union with iv_double
      break;
    default:
      parser_error(iu, "Bad type (%s) for float constant",
                          type_str_index(iu, iu->iu_current_type));
    }

    break;


  case CST_CODE_AGGREGATE:
    iv->iv_class = IR_VC_AGGREGATE;
    iv->iv_num_values = argc;
    ir_valuetype_t *values = malloc(sizeof(ir_valuetype_t) * iv->iv_num_values);
    iv->iv_data = values;
    for(int i = 0; i < iv->iv_num_values; i++) {
      ir_value_t *ivx = value_get(iu, argv[i]);
      values[i].value = argv[i];
      values[i].type = ivx->iv_type;
    }
    break;

  case CST_CODE_DATA:
    {
      iv->iv_class = IR_VC_DATA;
      ir_type_t *ty = type_get(iu, iu->iu_current_type);

      switch(ty->it_code) {
      case IR_TYPE_ARRAY:
        switch(type_get(iu, ty->it_array.element_type)->it_code) {

        case IR_TYPE_INT64:
        case IR_TYPE_DOUBLE:
          iv->iv_data = malloc(sizeof(uint64_t) * argc);
          for(int i = 0; i < argc; i++)
            host_wr64(iv->iv_data + i * sizeof(uint64_t), argv[i]);
          return;

        case IR_TYPE_INT32:
        case IR_TYPE_FLOAT:
          iv->iv_data = malloc(sizeof(uint32_t) * argc);
          for(int i = 0; i < argc; i++)
            host_wr32(iv->iv_data + i * sizeof(uint32_t), argv[i]);
          return;

        case IR_TYPE_INT16:
          iv->iv_data = malloc(sizeof(uint16_t) * argc);
          for(int i = 0; i < argc; i++)
            host_wr16(iv->iv_data + i * sizeof(uint16_t), argv[i]);
          return;

        default:
          break;
        }
      default:
        break;
      }
      parser_error(iu, "Can't parse constant data for type %s",
                   type_str_index(iu, iu->iu_current_type));
    }
    break;

  case CST_CODE_CSTRING:
  case CST_CODE_STRING:
    iv->iv_class = IR_VC_DATA;
    iv->iv_data = read_str_from_argv(argc, argv);
    break;

  case CST_CODE_CE_CAST:
    iv->iv_class = IR_VC_CE;
    ic = iv->iv_ce = malloc(sizeof(ir_constexpr_t));
    ic->ic_code = op;
    ic->ic_cast.op  = argv[0];
    ic->ic_cast.src = argv[2];
    break;

  case CST_CODE_CE_GEP:
  case CST_CODE_CE_INBOUNDS_GEP:
    iv->iv_class = IR_VC_CE;
    ic = iv->iv_ce = malloc(sizeof(ir_constexpr_t));
    ic->ic_code = op;
    if(argc & 1) {
      // explicit return type
      argc--;
      argv++;
    }
    ic->ic_gep.num_values = argc / 2;
    ic->ic_gep.values = malloc(iv->iv_ce->ic_gep.num_values * sizeof(int));
    for(int i = 0; i < ic->ic_gep.num_values; i++)
      ic->ic_gep.values[i] = argv[1 + i * 2];
    break;

  case CST_CODE_CE_BINOP:
    iv->iv_class = IR_VC_CE;
    if(argc < 3)
      parser_error(iu, "CST_CODE_CE_BINOP with invalid number of args %d", argc);
    ic = iv->iv_ce = malloc(sizeof(ir_constexpr_t));
    ic->ic_code = op;
    ic->ic_binop.op  = argv[0];
    ic->ic_binop.lhs = argv[1];
    ic->ic_binop.rhs = argv[2];
    break;

  case CST_CODE_CE_CMP:
    iv->iv_class = IR_VC_CE;
    if(argc < 4)
      parser_error(iu, "CST_CODE_CE_CMP with invalid number of args %d", argc);
    ic = iv->iv_ce = malloc(sizeof(ir_constexpr_t));
    ic->ic_code = op;
    ic->ic_cmp.opty = argv[0];
    ic->ic_cmp.lhs  = argv[1];
    ic->ic_cmp.rhs  = argv[2];
    ic->ic_cmp.pred = argv[3];
    break;

  case CST_CODE_BLOCKADDRESS:
    parser_error(iu, "CST_CODE_BLOCKADDRESS (computed goto) is not supported");

  case CST_CODE_INLINEASM:
  case CST_CODE_INLINEASM_OLD:
    parser_error(iu, "Inline asm not supported");

  default:
    parser_error(iu, "Unknown op %d in constant block curty=%s",
                 op, type_str_index(iu, iu->iu_current_type));
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %rbx
movl %edx, %r15d
movq %rdi, %r14
cmpl $0x1, %esi
jne 0x1e4fd
testl %r15d, %r15d
je 0x1e96f
movl (%rbx), %eax
movl %eax, 0x1b8(%r14)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movl %esi, %r12d
movq %r14, %rdi
callq 0xf50d
movq 0x180(%r14), %rcx
cltq
movq (%rcx,%rax,8), %r13
movl 0x1b8(%r14), %r8d
movl %r8d, 0x24(%r13)
leal -0x2(%r12), %eax
cmpl $0x15, %eax
ja 0x1e9e7
leaq 0x621b(%rip), %rcx      # 0x24750
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpq %r8, 0x170(%r14)
jbe 0x1e98c
movq 0x168(%r14), %rax
leaq (%r8,%r8,4), %rcx
movl (%rax,%rcx,8), %eax
leal -0x2(%rax), %ecx
cmpl $0x8, %ecx
jae 0x1e84d
movl $0x3, (%r13)
movq $0x0, 0x30(%r13)
jmp 0x1e4f3
movl $0x8, (%r13)
movl $0x18, %edi
callq 0x3380
movq %rax, %r14
movq %rax, 0x30(%r13)
movl %r12d, (%rax)
movl %r15d, %edi
shrl %edi
movl %edi, 0x8(%rax)
shlq $0x2, %rdi
callq 0x3380
movq %rax, 0x10(%r14)
cmpl $0x2, %r15d
jb 0x1e4f3
shll $0x3, %r15d
andl $0x8, %r15d
leaq (%r15,%rbx), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movl (%rax), %edx
movq 0x10(%r14), %rsi
movl %edx, (%rsi,%rcx,4)
incq %rcx
movslq 0x8(%r14), %rdx
addq $0x10, %rax
cmpq %rdx, %rcx
jl 0x1e5c7
jmp 0x1e4f3
movl $0x7, (%r13)
leal 0x1(%r15), %edi
callq 0x3380
movl %r15d, %ecx
movb $0x0, (%rax,%rcx)
testl %r15d, %r15d
je 0x1e614
xorl %edx, %edx
movb (%rbx,%rdx,8), %sil
movb %sil, (%rax,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x1e604
movq %rax, 0x18(%r13)
jmp 0x1e4f3
movl $0x8, (%r13)
cmpl $0x2, %r15d
jbe 0x1ea30
movl $0x18, %edi
callq 0x3380
movq %rax, 0x30(%r13)
movl $0xa, (%rax)
movdqu (%rbx), %xmm0
pshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
movq %xmm0, 0x8(%rax)
movl 0x10(%rbx), %ecx
movl %ecx, 0x10(%rax)
jmp 0x1e4f3
movl $0x9, (%r13)
movl %r15d, 0x30(%r13)
movslq %r15d, %r12
leaq (,%r12,8), %rdi
callq 0x3380
movq %rax, 0x18(%r13)
testl %r15d, %r15d
jle 0x1e4f3
movq 0x188(%r14), %rcx
xorl %edx, %edx
movq (%rbx,%rdx,8), %r8
movl %r8d, %esi
cmpq %rsi, %rcx
jbe 0x1e957
movq 0x180(%r14), %rdi
movq (%rdi,%rsi,8), %rsi
movl %r8d, (%rax,%rdx,8)
movl 0x24(%rsi), %esi
movl %esi, 0x4(%rax,%rdx,8)
incq %rdx
cmpq %r12, %rdx
jl 0x1e68e
jmp 0x1e4f3
testl %r15d, %r15d
je 0x1ea48
movl $0x3, (%r13)
cmpq %r8, 0x170(%r14)
jbe 0x1e98c
movq 0x168(%r14), %rax
leaq (%r8,%r8,4), %rcx
movl (%rax,%rcx,8), %eax
cmpl $0x9, %eax
je 0x1e885
cmpl $0x8, %eax
jne 0x1eac2
movl (%rbx), %eax
movl %eax, 0x30(%r13)
jmp 0x1e4f3
testl %r15d, %r15d
je 0x1ea67
movl $0x3, (%r13)
cmpq %r8, 0x170(%r14)
jbe 0x1e98c
movq 0x168(%r14), %rax
leaq (%r8,%r8,4), %rcx
movl (%rax,%rcx,8), %eax
leal -0x2(%rax), %ecx
cmpl $0x4, %ecx
jae 0x1e8cd
movq (%rbx), %rax
testb $0x1, %al
jne 0x1e8e5
shrq %rax
jmp 0x1e93f
movl $0x7, (%r13)
movq 0x170(%r14), %rcx
cmpq %r8, %rcx
jbe 0x1e98c
movq 0x168(%r14), %rdx
leaq (%r8,%r8,4), %rax
cmpl $0xa, (%rdx,%rax,8)
jne 0x1e9c1
leaq (%rdx,%rax,8), %rax
movl 0xc(%rax), %eax
cmpq %rax, %rcx
jbe 0x1eaa4
leaq (%rax,%rax,4), %rax
movl (%rdx,%rax,8), %eax
addl $-0x4, %eax
cmpl $0x5, %eax
ja 0x1e9c1
leaq 0x5ffc(%rip), %rcx      # 0x247a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %r15d, %r14d
leaq (,%r14,4), %rdi
callq 0x3380
movq %rax, 0x18(%r13)
testl %r15d, %r15d
je 0x1e4f3
xorl %ecx, %ecx
movl (%rbx,%rcx,8), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r14
jne 0x1e7d4
jmp 0x1e4f3
movl $0x8, (%r13)
movl $0x18, %edi
callq 0x3380
movq %rax, 0x30(%r13)
movl $0xb, (%rax)
movl (%rbx), %ecx
movl %ecx, 0x8(%rax)
movl 0x10(%rbx), %ecx
movl %ecx, 0xc(%rax)
jmp 0x1e4f3
movl $0x8, (%r13)
cmpl $0x3, %r15d
jbe 0x1ea86
movl $0x18, %edi
callq 0x3380
movq %rax, 0x30(%r13)
movl $0x11, (%rax)
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
shufps $0x88, %xmm1, %xmm0     # xmm0 = xmm0[0,2],xmm1[0,2]
movups %xmm0, 0x8(%rax)
jmp 0x1e4f3
testl %eax, %eax
je 0x1e8c0
cmpl $0xa, %eax
je 0x1e8c0
cmpl $0xb, %eax
je 0x1e565
movq %r14, %rdi
movl %r8d, %esi
callq 0x120fa
leaq 0x8d64(%rip), %rsi      # 0x275d5
leaq 0x9036(%rip), %rcx      # 0x278ae
movq %r14, %rdi
movl $0x21d, %edx            # imm = 0x21D
jmp 0x1eb06
movq (%rbx), %rax
jmp 0x1e94e
movl %r15d, %r14d
shlq $0x3, %r14
movq %r14, %rdi
callq 0x3380
movq %rax, 0x18(%r13)
testl %r15d, %r15d
je 0x1e4f3
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x3360
movl $0xb, (%r13)
jmp 0x1e4f3
addl $-0x6, %eax
cmpl $0x2, %eax
jae 0x1eae5
movq (%rbx), %rax
testb $0x1, %al
jne 0x1e927
shrq %rax
jmp 0x1e94e
cmpq $0x1, %rax
jne 0x1e939
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
jmp 0x1e93f
movl %r15d, %r14d
leaq (%r14,%r14), %rdi
callq 0x3380
movq %rax, 0x18(%r13)
testl %r15d, %r15d
je 0x1e4f3
xorl %ecx, %ecx
movzwl (%rbx,%rcx,8), %edx
movw %dx, (%rax,%rcx,2)
incq %rcx
cmpq %rcx, %r14
jne 0x1e912
jmp 0x1e4f3
cmpq $0x1, %rax
jne 0x1e948
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
jmp 0x1e94e
shrq %rax
negq %rax
movl %eax, 0x30(%r13)
jmp 0x1e4f3
shrq %rax
negq %rax
movq %rax, 0x30(%r13)
jmp 0x1e4f3
leaq 0x6c12(%rip), %rsi      # 0x25570
leaq 0x73bf(%rip), %rcx      # 0x25d24
movq %r14, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x1e9a2
leaq 0x8c5f(%rip), %rsi      # 0x275d5
leaq 0x8eb9(%rip), %rcx      # 0x27836
movq %r14, %rdi
movl $0x1fd, %edx            # imm = 0x1FD
xorl %eax, %eax
callq 0xca98
leaq 0x7129(%rip), %rsi      # 0x25abc
leaq 0x715b(%rip), %rcx      # 0x25af5
movq %r14, %rdi
movl $0x7c, %edx
xorl %eax, %eax
callq 0xca98
leaq 0x8c25(%rip), %rsi      # 0x275d5
leaq 0x9020(%rip), %rcx      # 0x279d7
movq %r14, %rdi
movl $0x2b8, %edx            # imm = 0x2B8
jmp 0x1e985
movq %r14, %rdi
movl %r8d, %esi
callq 0x120fa
leaq 0x8c02(%rip), %rsi      # 0x275d5
leaq 0x8f40(%rip), %rcx      # 0x2791a
movq %r14, %rdi
movl $0x27a, %edx            # imm = 0x27A
jmp 0x1eb06
movq %r14, %rdi
movl %r8d, %esi
callq 0x120fa
leaq 0x8bdc(%rip), %rsi      # 0x275d5
leaq 0x8ff0(%rip), %rcx      # 0x279f0
movq %r14, %rdi
movl $0x2bc, %edx            # imm = 0x2BC
movl %r12d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0xca98
leaq 0x8bb9(%rip), %rsi      # 0x275d5
leaq 0x8f7d(%rip), %rcx      # 0x279a0
movq %r14, %rdi
movl $0x2b4, %edx            # imm = 0x2B4
jmp 0x1e985
leaq 0x8b9e(%rip), %rsi      # 0x275d5
leaq 0x8f02(%rip), %rcx      # 0x27940
movq %r14, %rdi
movl $0x29f, %edx            # imm = 0x29F
jmp 0x1ea9c
leaq 0x8e0a(%rip), %rdi      # 0x27859
leaq 0x8b7f(%rip), %rsi      # 0x275d5
leaq 0x8e05(%rip), %rcx      # 0x27862
movl $0x239, %edx            # imm = 0x239
callq 0x31e0
leaq 0x8deb(%rip), %rdi      # 0x27859
leaq 0x8b60(%rip), %rsi      # 0x275d5
leaq 0x8de6(%rip), %rcx      # 0x27862
movl $0x222, %edx            # imm = 0x222
callq 0x31e0
leaq 0x8b48(%rip), %rsi      # 0x275d5
leaq 0x8edd(%rip), %rcx      # 0x27971
movq %r14, %rdi
movl $0x2aa, %edx            # imm = 0x2AA
movl %r15d, %r8d
jmp 0x1e9a2
leaq 0x7011(%rip), %rsi      # 0x25abc
leaq 0x7043(%rip), %rcx      # 0x25af5
movq %r14, %rdi
movl $0x7c, %edx
movl %eax, %r8d
jmp 0x1e9a2
movq %r14, %rdi
movl %r8d, %esi
callq 0x120fa
leaq 0x8b01(%rip), %rsi      # 0x275d5
leaq 0x8e1e(%rip), %rcx      # 0x278f9
movq %r14, %rdi
movl $0x244, %edx            # imm = 0x244
jmp 0x1eb06
movq %r14, %rdi
movl %r8d, %esi
callq 0x120fa
leaq 0x8ade(%rip), %rsi      # 0x275d5
leaq 0x8dd8(%rip), %rcx      # 0x278d6
movq %r14, %rdi
movl $0x232, %edx            # imm = 0x232
movq %rax, %r8
xorl %eax, %eax
callq 0xca98