// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movl %edx, %r12d
movq %rdi, %r14
cmpl $0x1, %esi
jne 0x1a13b
testl %r12d, %r12d
je 0x1a473
movl (%rbx), %eax
movl %eax, 0x1b8(%r14)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %r13d
movq %r14, %rdi
callq 0x1051a
movq %rax, %r15
movl 0x1b8(%r14), %esi
movl %esi, 0x24(%rax)
leal -0x2(%r13), %eax
cmpl $0x15, %eax
ja 0x1a4d2
leaq 0x575d(%rip), %rcx      # 0x1f8c4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0xe719
movl (%rax), %eax
cmpl $0xb, %eax
ja 0x1a5b5
movl $0xbfc, %ecx            # imm = 0xBFC
btl %eax, %ecx
jae 0x1a414
movl $0x3, (%r15)
andq $0x0, 0x30(%r15)
jmp 0x1a12c
movl $0x8, (%r15)
pushq $0x18
popq %rdi
callq 0x3380
movq %rax, %r14
movq %rax, 0x30(%r15)
movl %r13d, (%rax)
movl %r12d, %r15d
andl $0x1, %r15d
shrl %r12d
movl %r12d, 0x8(%rax)
leaq (,%r12,4), %rdi
callq 0x3380
movq %rax, 0x10(%r14)
leaq (%rbx,%r15,8), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movslq %r12d, %rdx
cmpq %rdx, %rcx
jge 0x1a12c
movl (%rax), %edx
movq 0x10(%r14), %rsi
movl %edx, (%rsi,%rcx,4)
incq %rcx
movl 0x8(%r14), %r12d
addq $0x10, %rax
jmp 0x1a1e1
movl $0x7, (%r15)
movl %r12d, %edi
movq %rbx, %rsi
callq 0x1b56c
movq %rax, 0x18(%r15)
jmp 0x1a12c
movl $0x8, (%r15)
cmpl $0x2, %r12d
jbe 0x1a518
pushq $0x18
popq %rdi
callq 0x3380
movq %rax, 0x30(%r15)
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 0x1a12c
movl $0x9, (%r15)
movl %r12d, 0x30(%r15)
movslq %r12d, %rdi
shlq $0x3, %rdi
callq 0x3380
movq %rax, %r13
movq %rax, 0x18(%r15)
xorl %ebp, %ebp
movslq %r12d, %rax
cmpq %rax, %rbp
jge 0x1a12c
movl (%rbx,%rbp,8), %esi
movq %r14, %rdi
callq 0x102f1
movl (%rbx,%rbp,8), %ecx
movl %ecx, (%r13,%rbp,8)
movl 0x24(%rax), %eax
movl %eax, 0x4(%r13,%rbp,8)
incq %rbp
movl 0x30(%r15), %r12d
jmp 0x1a27a
testl %r12d, %r12d
je 0x1a530
movl $0x3, (%r15)
movq %r14, %rdi
callq 0xe719
movl (%rax), %eax
cmpl $0x9, %eax
je 0x1a3e3
cmpl $0x8, %eax
jne 0x1a58e
movl (%rbx), %eax
movl %eax, 0x30(%r15)
jmp 0x1a12c
testl %r12d, %r12d
je 0x1a54f
movl $0x3, (%r15)
movq %r14, %rdi
callq 0xe719
movl (%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0x4, %ecx
jae 0x1a42e
movq %rbx, %rdi
callq 0x1b6a7
movl %eax, 0x30(%r15)
jmp 0x1a12c
movl $0x7, (%r15)
movq %r14, %rdi
callq 0xe719
cmpl $0xa, (%rax)
jne 0x1a4a8
movl 0xc(%rax), %esi
movq %r14, %rdi
callq 0xe719
movl (%rax), %eax
addl $-0x4, %eax
cmpl $0x5, %eax
ja 0x1a4a8
leaq 0x55cc(%rip), %rcx      # 0x1f91c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %r12d, %r14d
leaq (,%r14,4), %rdi
callq 0x3380
movq %rax, 0x18(%r15)
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x1a12c
movl (%rbx,%rcx,8), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
jmp 0x1a36f
movl $0x8, (%r15)
pushq $0x18
popq %rdi
callq 0x3380
movq %rax, 0x30(%r15)
movl $0xb, (%rax)
movl (%rbx), %ecx
movl %ecx, 0x8(%rax)
movl 0x10(%rbx), %ecx
movl %ecx, 0xc(%rax)
jmp 0x1a12c
movl $0x8, (%r15)
cmpl $0x3, %r12d
jbe 0x1a56e
pushq $0x18
popq %rdi
callq 0x3380
movq %rax, 0x30(%r15)
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 0x1a12c
movq (%rbx), %rax
jmp 0x1a442
movl %r12d, %r14d
leaq (,%r14,8), %rdi
callq 0x3380
movq %rax, 0x18(%r15)
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x1a12c
movq (%rbx,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
jmp 0x1a3fe
movl $0x401, %ecx            # imm = 0x401
btl %eax, %ecx
jae 0x1a5b5
movl $0xb, (%r15)
jmp 0x1a12c
addl $-0x6, %eax
cmpl $0x2, %eax
jae 0x1a5dc
movq %rbx, %rdi
callq 0x1b6a7
movq %rax, 0x30(%r15)
jmp 0x1a12c
movl %r12d, %r14d
leaq (%r14,%r14), %rdi
callq 0x3380
movq %rax, 0x18(%r15)
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x1a12c
movzwl (%rbx,%rcx,8), %edx
movw %dx, (%rax,%rcx,2)
incq %rcx
jmp 0x1a45d
leaq 0x82c6(%rip), %rsi      # 0x22740
leaq 0x8520(%rip), %rcx      # 0x229a1
movq %r14, %rdi
movl $0x1fd, %edx            # imm = 0x1FD
xorl %eax, %eax
callq 0xbed2
leaq 0x82a9(%rip), %rsi      # 0x22740
leaq 0x86a4(%rip), %rcx      # 0x22b42
movq %r14, %rdi
movl $0x2b8, %edx            # imm = 0x2B8
jmp 0x1a489
movl 0x1b8(%r14), %esi
movq %r14, %rdi
callq 0x105cd
leaq 0x8282(%rip), %rsi      # 0x22740
leaq 0x85c0(%rip), %rcx      # 0x22a85
movq %r14, %rdi
movl $0x27a, %edx            # imm = 0x27A
jmp 0x1a601
movq %r14, %rdi
callq 0x105cd
leaq 0x825f(%rip), %rsi      # 0x22740
leaq 0x8673(%rip), %rcx      # 0x22b5b
movq %r14, %rdi
movl $0x2bc, %edx            # imm = 0x2BC
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0xbed2
leaq 0x823c(%rip), %rsi      # 0x22740
leaq 0x8600(%rip), %rcx      # 0x22b0b
movq %r14, %rdi
movl $0x2b4, %edx            # imm = 0x2B4
jmp 0x1a489
leaq 0x8221(%rip), %rsi      # 0x22740
leaq 0x8585(%rip), %rcx      # 0x22aab
movq %r14, %rdi
movl $0x29f, %edx            # imm = 0x29F
jmp 0x1a584
leaq 0x848d(%rip), %rdi      # 0x229c4
leaq 0x8202(%rip), %rsi      # 0x22740
leaq 0x8488(%rip), %rcx      # 0x229cd
movl $0x239, %edx            # imm = 0x239
callq 0x31e0
leaq 0x846e(%rip), %rdi      # 0x229c4
leaq 0x81e3(%rip), %rsi      # 0x22740
leaq 0x8469(%rip), %rcx      # 0x229cd
movl $0x222, %edx            # imm = 0x222
callq 0x31e0
leaq 0x81cb(%rip), %rsi      # 0x22740
leaq 0x8560(%rip), %rcx      # 0x22adc
movq %r14, %rdi
movl $0x2aa, %edx            # imm = 0x2AA
movl %r12d, %r8d
xorl %eax, %eax
callq 0xbed2
movl 0x1b8(%r14), %esi
movq %r14, %rdi
callq 0x105cd
leaq 0x819c(%rip), %rsi      # 0x22740
leaq 0x84b9(%rip), %rcx      # 0x22a64
movq %r14, %rdi
movl $0x244, %edx            # imm = 0x244
jmp 0x1a601
movl 0x1b8(%r14), %esi
movq %r14, %rdi
callq 0x105cd
leaq 0x8175(%rip), %rsi      # 0x22740
leaq 0x8447(%rip), %rcx      # 0x22a19
movq %r14, %rdi
movl $0x21d, %edx            # imm = 0x21D
jmp 0x1a601
movl 0x1b8(%r14), %esi
movq %r14, %rdi
callq 0x105cd
leaq 0x814e(%rip), %rsi      # 0x22740
leaq 0x8448(%rip), %rcx      # 0x22a41
movq %r14, %rdi
movl $0x232, %edx            # imm = 0x232
movq %rax, %r8
xorl %eax, %eax
callq 0xbed2