// File: /workspace/github_c_makefile_star2_O1/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 0x1e263
testl %r15d, %r15d
je 0x1e6e0
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 0xf5a0
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 0x1e758
leaq 0x6449(%rip), %rcx      # 0x246e4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpq %r8, 0x170(%r14)
jbe 0x1e6fd
movq 0x168(%r14), %rax
leaq (%r8,%r8,4), %rcx
movl (%rax,%rcx,8), %eax
leal -0x2(%rax), %ecx
cmpl $0x8, %ecx
jae 0x1e5b9
movl $0x3, (%r13)
movq $0x0, 0x30(%r13)
jmp 0x1e259
movl $0x8, (%r13)
movl $0x18, %edi
callq 0x3380
movq %rax, %r14
movq %rax, 0x30(%r13)
movl %r12d, (%rax)
movl %r15d, %eax
shrl %eax
movl %eax, 0x8(%r14)
movq 0x30(%r13), %rax
movslq 0x8(%rax), %rdi
shlq $0x2, %rdi
callq 0x3380
movq %rax, 0x10(%r14)
cmpl $0x0, 0x8(%r14)
jle 0x1e259
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 0x1e337
jmp 0x1e259
movl $0x7, (%r13)
leal 0x1(%r15), %edi
callq 0x3380
movl %r15d, %ecx
movb $0x0, (%rax,%rcx)
testl %r15d, %r15d
je 0x1e384
xorl %edx, %edx
movb (%rbx,%rdx,8), %sil
movb %sil, (%rax,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x1e374
movq %rax, 0x18(%r13)
jmp 0x1e259
movl $0x8, (%r13)
cmpl $0x2, %r15d
jbe 0x1e7a1
movl $0x18, %edi
callq 0x3380
movq %rax, 0x30(%r13)
movl %r12d, (%rax)
movl (%rbx), %ecx
movl %ecx, 0x8(%rax)
movl 0x8(%rbx), %ecx
movl %ecx, 0xc(%rax)
movl 0x10(%rbx), %ecx
movl %ecx, 0x10(%rax)
jmp 0x1e259
movl $0x9, (%r13)
movl %r15d, 0x30(%r13)
movslq %r15d, %rdi
shlq $0x3, %rdi
callq 0x3380
movq %rax, 0x18(%r13)
testl %r15d, %r15d
jle 0x1e259
movq 0x188(%r14), %rcx
xorl %edx, %edx
movq (%rbx,%rdx,8), %r8
movl %r8d, %esi
cmpq %rsi, %rcx
jbe 0x1e6c8
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
movslq 0x30(%r13), %rsi
cmpq %rsi, %rdx
jl 0x1e3f4
jmp 0x1e259
testl %r15d, %r15d
je 0x1e7b9
movl $0x3, (%r13)
cmpq %r8, 0x170(%r14)
jbe 0x1e6fd
movq 0x168(%r14), %rax
leaq (%r8,%r8,4), %rcx
movl (%rax,%rcx,8), %eax
cmpl $0x9, %eax
je 0x1e5f1
cmpl $0x8, %eax
jne 0x1e833
movl (%rbx), %eax
movl %eax, 0x30(%r13)
jmp 0x1e259
testl %r15d, %r15d
je 0x1e7d8
movl $0x3, (%r13)
cmpq %r8, 0x170(%r14)
jbe 0x1e6fd
movq 0x168(%r14), %rax
leaq (%r8,%r8,4), %rcx
movl (%rax,%rcx,8), %eax
leal -0x2(%rax), %ecx
cmpl $0x4, %ecx
jae 0x1e63e
movq (%rbx), %rax
testb $0x1, %al
jne 0x1e656
shrq %rax
jmp 0x1e6b0
movl $0x7, (%r13)
movq 0x170(%r14), %rcx
cmpq %r8, %rcx
jbe 0x1e6fd
movq 0x168(%r14), %rdx
leaq (%r8,%r8,4), %rax
cmpl $0xa, (%rdx,%rax,8)
jne 0x1e732
leaq (%rdx,%rax,8), %rax
movl 0xc(%rax), %eax
cmpq %rax, %rcx
jbe 0x1e815
leaq (%rax,%rax,4), %rax
movl (%rdx,%rax,8), %eax
addl $-0x4, %eax
cmpl $0x5, %eax
ja 0x1e732
leaq 0x6226(%rip), %rcx      # 0x2473c
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 0x1e259
xorl %ecx, %ecx
movl (%rbx,%rcx,8), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %r14
jne 0x1e53e
jmp 0x1e259
movl $0x8, (%r13)
movl $0x18, %edi
callq 0x3380
movq %rax, 0x30(%r13)
movl %r12d, (%rax)
movl (%rbx), %ecx
movl %ecx, 0x8(%rax)
movl 0x10(%rbx), %ecx
movl %ecx, 0xc(%rax)
jmp 0x1e259
movl $0x8, (%r13)
cmpl $0x3, %r15d
jbe 0x1e7f7
movl $0x18, %edi
callq 0x3380
movq %rax, 0x30(%r13)
movl %r12d, (%rax)
movl (%rbx), %ecx
movl %ecx, 0x8(%rax)
movl 0x8(%rbx), %ecx
movl %ecx, 0xc(%rax)
movl 0x10(%rbx), %ecx
movl %ecx, 0x10(%rax)
movl 0x18(%rbx), %ecx
movl %ecx, 0x14(%rax)
jmp 0x1e259
testl %eax, %eax
je 0x1e631
cmpl $0xa, %eax
je 0x1e631
cmpl $0xb, %eax
je 0x1e2cb
movq %r14, %rdi
movl %r8d, %esi
callq 0x12335
leaq 0x8fae(%rip), %rsi      # 0x2758b
leaq 0x9280(%rip), %rcx      # 0x27864
movq %r14, %rdi
movl $0x21d, %edx            # imm = 0x21D
jmp 0x1e877
movq (%rbx), %rax
jmp 0x1e6bf
movl %r15d, %r14d
leaq (,%r14,8), %rdi
callq 0x3380
movq %rax, 0x18(%r13)
testl %r15d, %r15d
je 0x1e259
shlq $0x3, %r14
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 0x1e259
addl $-0x6, %eax
cmpl $0x2, %eax
jae 0x1e856
movq (%rbx), %rax
testb $0x1, %al
jne 0x1e698
shrq %rax
jmp 0x1e6bf
cmpq $0x1, %rax
jne 0x1e6aa
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
jmp 0x1e6b0
movl %r15d, %r14d
leaq (%r14,%r14), %rdi
callq 0x3380
movq %rax, 0x18(%r13)
testl %r15d, %r15d
je 0x1e259
xorl %ecx, %ecx
movzwl (%rbx,%rcx,8), %edx
movw %dx, (%rax,%rcx,2)
incq %rcx
cmpq %rcx, %r14
jne 0x1e683
jmp 0x1e259
cmpq $0x1, %rax
jne 0x1e6b9
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
jmp 0x1e6bf
shrq %rax
negq %rax
movl %eax, 0x30(%r13)
jmp 0x1e259
shrq %rax
negq %rax
movq %rax, 0x30(%r13)
jmp 0x1e259
leaq 0x6df1(%rip), %rsi      # 0x254c0
leaq 0x759e(%rip), %rcx      # 0x25c74
movq %r14, %rdi
movl $0x143, %edx            # imm = 0x143
jmp 0x1e713
leaq 0x8ea4(%rip), %rsi      # 0x2758b
leaq 0x90fe(%rip), %rcx      # 0x277ec
movq %r14, %rdi
movl $0x1fd, %edx            # imm = 0x1FD
xorl %eax, %eax
callq 0xca73
leaq 0x7308(%rip), %rsi      # 0x25a0c
leaq 0x733a(%rip), %rcx      # 0x25a45
movq %r14, %rdi
movl $0x7c, %edx
xorl %eax, %eax
callq 0xca73
leaq 0x8e6a(%rip), %rsi      # 0x2758b
leaq 0x9265(%rip), %rcx      # 0x2798d
movq %r14, %rdi
movl $0x2b8, %edx            # imm = 0x2B8
jmp 0x1e6f6
movq %r14, %rdi
movl %r8d, %esi
callq 0x12335
leaq 0x8e47(%rip), %rsi      # 0x2758b
leaq 0x9185(%rip), %rcx      # 0x278d0
movq %r14, %rdi
movl $0x27a, %edx            # imm = 0x27A
jmp 0x1e877
movq %r14, %rdi
movl %r8d, %esi
callq 0x12335
leaq 0x8e21(%rip), %rsi      # 0x2758b
leaq 0x9235(%rip), %rcx      # 0x279a6
movq %r14, %rdi
movl $0x2bc, %edx            # imm = 0x2BC
movl %r12d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0xca73
leaq 0x8dfe(%rip), %rsi      # 0x2758b
leaq 0x91c2(%rip), %rcx      # 0x27956
movq %r14, %rdi
movl $0x2b4, %edx            # imm = 0x2B4
jmp 0x1e6f6
leaq 0x8de3(%rip), %rsi      # 0x2758b
leaq 0x9147(%rip), %rcx      # 0x278f6
movq %r14, %rdi
movl $0x29f, %edx            # imm = 0x29F
jmp 0x1e80d
leaq 0x904f(%rip), %rdi      # 0x2780f
leaq 0x8dc4(%rip), %rsi      # 0x2758b
leaq 0x904a(%rip), %rcx      # 0x27818
movl $0x239, %edx            # imm = 0x239
callq 0x31e0
leaq 0x9030(%rip), %rdi      # 0x2780f
leaq 0x8da5(%rip), %rsi      # 0x2758b
leaq 0x902b(%rip), %rcx      # 0x27818
movl $0x222, %edx            # imm = 0x222
callq 0x31e0
leaq 0x8d8d(%rip), %rsi      # 0x2758b
leaq 0x9122(%rip), %rcx      # 0x27927
movq %r14, %rdi
movl $0x2aa, %edx            # imm = 0x2AA
movl %r15d, %r8d
jmp 0x1e713
leaq 0x71f0(%rip), %rsi      # 0x25a0c
leaq 0x7222(%rip), %rcx      # 0x25a45
movq %r14, %rdi
movl $0x7c, %edx
movl %eax, %r8d
jmp 0x1e713
movq %r14, %rdi
movl %r8d, %esi
callq 0x12335
leaq 0x8d46(%rip), %rsi      # 0x2758b
leaq 0x9063(%rip), %rcx      # 0x278af
movq %r14, %rdi
movl $0x244, %edx            # imm = 0x244
jmp 0x1e877
movq %r14, %rdi
movl %r8d, %esi
callq 0x12335
leaq 0x8d23(%rip), %rsi      # 0x2758b
leaq 0x901d(%rip), %rcx      # 0x2788c
movq %r14, %rdi
movl $0x232, %edx            # imm = 0x232
movq %rax, %r8
xorl %eax, %eax
callq 0xca73