// File: /workspace/github_c_makefile_star2_O1/atomlang[P]atomlang/src/pk_core.c
// Function: varGetAttrib
Var varGetAttrib(PKVM* vm, Var on, String* attrib) {

  if (!IS_OBJ(on)) {
    VM_SET_ERROR(vm, stringFormat(vm, "$ type is not subscriptable.",
                                  varTypeName(on)));
    return VAR_NULL;
  }

  Object* obj = AS_OBJ(on);
  switch (obj->type) {
    case OBJ_STRING:
    {
      String* str = (String*)obj;
      switch (attrib->hash) {

        case CHECK_HASH("length", 0x83d03615):
          return VAR_NUM((double)(str->length));

        case CHECK_HASH("lower", 0xb51d04ba):
          return VAR_OBJ(stringLower(vm, str));

        case CHECK_HASH("upper", 0xa8c6a47):
          return VAR_OBJ(stringUpper(vm, str));

        case CHECK_HASH("strip", 0xfd1b18d1):
          return VAR_OBJ(stringStrip(vm, str));

        default:
          ERR_NO_ATTRIB(vm, on, attrib);
          return VAR_NULL;
      }

      UNREACHABLE();
    }

    case OBJ_LIST:
    {
      List* list = (List*)obj;
      switch (attrib->hash) {

        case CHECK_HASH("length", 0x83d03615):
          return VAR_NUM((double)(list->elements.count));

        default:
          ERR_NO_ATTRIB(vm, on, attrib);
          return VAR_NULL;
      }

      UNREACHABLE();
    }

    case OBJ_MAP:
    {
      // Not sure should I allow string values could be accessed with
      // this way. ex:
      // map = { "foo" : 42, "can't access" : 32 }
      // val = map.foo ## 42
      TODO;
      UNREACHABLE();
    }

    case OBJ_RANGE:
    {
      Range* range = (Range*)obj;
      switch (attrib->hash) {

        case CHECK_HASH("as_list", 0x1562c22):
          return VAR_OBJ(rangeAsList(vm, range));

        // We can't use 'start', 'end' since 'end' in atomlang is a
        // keyword. Also we can't use 'from', 'to' since 'from' is a keyword
        // too. So, we're using 'first' and 'last' to access the range limits.

        case CHECK_HASH("first", 0x4881d841):
          return VAR_NUM(range->from);

        case CHECK_HASH("last", 0x63e1d819):
          return VAR_NUM(range->to);

        default:
          ERR_NO_ATTRIB(vm, on, attrib);
          return VAR_NULL;
      }

      UNREACHABLE();
    }

    case OBJ_SCRIPT:
    {
      Script* scr = (Script*)obj;

      // Search in types.
      int index = scriptGetClass(scr, attrib->data, attrib->length);
      if (index != -1) {
        ASSERT_INDEX((uint32_t)index, scr->classes.count);
        return VAR_OBJ(scr->classes.data[index]);
      }

      // Search in functions.
      index = scriptGetFunc(scr, attrib->data, attrib->length);
      if (index != -1) {
        ASSERT_INDEX((uint32_t)index, scr->functions.count);
        return VAR_OBJ(scr->functions.data[index]);
      }

      // Search in globals.
      index = scriptGetGlobals(scr, attrib->data, attrib->length);
      if (index != -1) {
        ASSERT_INDEX((uint32_t)index, scr->globals.count);
        return scr->globals.data[index];
      }

      ERR_NO_ATTRIB(vm, on, attrib);
      return VAR_NULL;
    }

    case OBJ_FUNC:
    {
      Function* fn = (Function*)obj;
      switch (attrib->hash) {

        case CHECK_HASH("arity", 0x3e96bd7a):
          return VAR_NUM((double)(fn->arity));

        case CHECK_HASH("name", 0x8d39bde6):
          return VAR_OBJ(newString(vm, fn->name));

        default:
          ERR_NO_ATTRIB(vm, on, attrib);
          return VAR_NULL;
      }
      UNREACHABLE();
    }

    case OBJ_FIBER:
      {
        Fiber* fb = (Fiber*)obj;
        switch (attrib->hash) {

          case CHECK_HASH("is_done", 0x789c2706):
            return VAR_BOOL(fb->state == FIBER_DONE);

          case CHECK_HASH("function", 0x9ed64249):
            return VAR_OBJ(fb->func);

          default:
            ERR_NO_ATTRIB(vm, on, attrib);
            return VAR_NULL;
          }
        UNREACHABLE();
      }

    case OBJ_CLASS:
      TODO;
      UNREACHABLE();

    case OBJ_INST:
    {
      Var value;
      if (!instGetAttrib(vm, (Instance*)obj, attrib, &value)) {
        ERR_NO_ATTRIB(vm, on, attrib);
        return VAR_NULL;
      }
      return value;
    }

    default:
      UNREACHABLE();
  }

  UNREACHABLE();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r12
movq %rdi, %rbx
movabsq $-0x4000000000000, %r15 # imm = 0xFFFC000000000000
leaq -0x1(%r15), %rax
cmpq %rax, %rsi
jbe 0x184d5
movabsq $0xffffffffffff, %r13   # imm = 0xFFFFFFFFFFFF
andq %r12, %r13
movl (%r13), %eax
cmpq $0x8, %rax
ja 0x187f4
movq %rdx, %r14
leaq 0x63cd(%rip), %rcx      # 0x1e864
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x10(%r14), %eax
cmpl $0xfd1b18d0, %eax       # imm = 0xFD1B18D0
jg 0x1861d
cmpl $0x83d03615, %eax       # imm = 0x83D03615
je 0x18759
cmpl $0xb51d04ba, %eax       # imm = 0xB51D04BA
jne 0x186fd
movq %rbx, %rdi
movq %r13, %rsi
callq 0x99a5
jmp 0x187b5
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x187e5
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0xaa1a
leaq 0x6a1a(%rip), %rsi      # 0x1ef1a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x95d0
jmp 0x1873c
movq %rsp, %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xa665
testb %al, %al
je 0x18606
movq (%rsp), %rbp
jmp 0x18747
movl 0x10(%r14), %eax
cmpl $0x8d39bde6, %eax       # imm = 0x8D39BDE6
je 0x18688
cmpl $0x3e96bd7a, %eax       # imm = 0x3E96BD7A
jne 0x186a5
cvtsi2sdl 0x20(%r13), %xmm0
jmp 0x18762
leaq 0x1c(%r14), %rbp
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa4a4
cmpl $-0x1, %eax
je 0x1865a
cmpl 0x58(%r13), %eax
jae 0x187f9
movq 0x50(%r13), %rcx
jmp 0x18680
movl 0x10(%r14), %eax
cmpl $0x63e1d819, %eax       # imm = 0x63E1D819
je 0x186e2
cmpl $0x4881d841, %eax       # imm = 0x4881D841
je 0x186d7
cmpl $0x1562c22, %eax        # imm = 0x1562C22
jne 0x186ea
movq %rbx, %rdi
movq %r13, %rsi
callq 0x98de
jmp 0x187b5
cmpl $0x83d03615, 0x10(%r14) # imm = 0x83D03615
jne 0x18643
movl 0x18(%r13), %eax
jmp 0x1875d
movl 0x10(%r14), %eax
cmpl $0x9ed64249, %eax       # imm = 0x9ED64249
je 0x186b8
cmpl $0x789c2706, %eax       # imm = 0x789C2706
jne 0x186c4
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
leaq 0x1(%rax), %rbp
cmpl $0x3, 0x10(%r13)
cmovneq %rax, %rbp
jmp 0x18747
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x1870f
callq 0x6ea2
cmpl $0xfd1b18d1, %eax       # imm = 0xFD1B18D1
je 0x18775
cmpl $0xa8c6a47, %eax        # imm = 0xA8C6A47
jne 0x186fd
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9a40
jmp 0x187b5
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x1870f
callq 0x70be
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa54c
cmpl $-0x1, %eax
je 0x18782
cmpl 0x48(%r13), %eax
jae 0x187fe
movq 0x40(%r13), %rcx
cltq
orq (%rcx,%rax,8), %r15
jmp 0x186bc
movq 0x10(%r13), %r14
testq %r14, %r14
je 0x187a8
movq %r14, %rdi
callq 0x2140
movq %rax, %rdx
jmp 0x187aa
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x1870f
callq 0x6f44
orq 0x18(%r13), %r15
movq %r15, %rbp
jmp 0x18747
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x1870f
callq 0x6f0e
movsd 0x10(%r13), %xmm0
jmp 0x18762
movsd 0x18(%r13), %xmm0
jmp 0x18762
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x1870f
callq 0x7052
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x18803
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0xaa1a
addq $0x1c, %r14
leaq 0x680b(%rip), %rsi      # 0x1ef37
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x95d0
movq 0x5f0(%rbx), %rcx
movq %rax, 0x58(%rcx)
movq %rbp, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%r13), %eax
cvtsi2sd %rax, %xmm0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x905b
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9adb
jmp 0x187b5
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa5d9
cmpl $-0x1, %eax
je 0x187bd
cmpl 0x28(%r13), %eax
jae 0x18808
movq 0x20(%r13), %rcx
cltq
movq (%rcx,%rax,8), %rbp
jmp 0x18747
xorl %edx, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7fc8
movq %rax, %rbp
orq %r15, %rbp
jmp 0x18747
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x1880d
movq %rbp, %r14
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0xaa1a
jmp 0x18725
callq 0x6e6c
callq 0x6ed8
callq 0x7088
callq 0x712a
callq 0x6f7a
callq 0x6fb0
callq 0x70f4
callq 0x6fe6
callq 0x701c