// File: /workspace/github_c_makefile_star2_O3/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 0x1790f
movabsq $0xffffffffffff, %r13   # imm = 0xFFFFFFFFFFFF
andq %r12, %r13
movl (%r13), %eax
cmpq $0x8, %rax
ja 0x17c2e
movq %rdx, %r14
leaq 0x5fd3(%rip), %rcx      # 0x1d8a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x10(%r14), %eax
cmpl $0xfd1b18d0, %eax       # imm = 0xFD1B18D0
jg 0x17a57
cmpl $0x83d03615, %eax       # imm = 0x83D03615
je 0x17b93
cmpl $0xb51d04ba, %eax       # imm = 0xB51D04BA
jne 0x17b37
movq %rbx, %rdi
movq %r13, %rsi
callq 0x98ca
jmp 0x17bef
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x17c1f
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0xa89c
leaq 0x6620(%rip), %rsi      # 0x1df5a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x94f4
jmp 0x17b76
movq %rsp, %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xa4eb
testb %al, %al
je 0x17a40
movq (%rsp), %rbp
jmp 0x17b81
movl 0x10(%r14), %eax
cmpl $0x8d39bde6, %eax       # imm = 0x8D39BDE6
je 0x17ac2
cmpl $0x3e96bd7a, %eax       # imm = 0x3E96BD7A
jne 0x17adf
cvtsi2sdl 0x20(%r13), %xmm0
jmp 0x17b9c
leaq 0x1c(%r14), %rbp
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa37e
cmpl $-0x1, %eax
je 0x17a94
cmpl 0x58(%r13), %eax
jae 0x17c33
movq 0x50(%r13), %rcx
jmp 0x17aba
movl 0x10(%r14), %eax
cmpl $0x63e1d819, %eax       # imm = 0x63E1D819
je 0x17b1c
cmpl $0x4881d841, %eax       # imm = 0x4881D841
je 0x17b11
cmpl $0x1562c22, %eax        # imm = 0x1562C22
jne 0x17b24
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9802
jmp 0x17bef
cmpl $0x83d03615, 0x10(%r14) # imm = 0x83D03615
jne 0x17a7d
movl 0x18(%r13), %eax
jmp 0x17b97
movl 0x10(%r14), %eax
cmpl $0x9ed64249, %eax       # imm = 0x9ED64249
je 0x17af2
cmpl $0x789c2706, %eax       # imm = 0x789C2706
jne 0x17afe
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
leaq 0x1(%rax), %rbp
cmpl $0x3, 0x10(%r13)
cmovneq %rax, %rbp
jmp 0x17b81
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x17b49
callq 0x6e6c
cmpl $0xfd1b18d1, %eax       # imm = 0xFD1B18D1
je 0x17baf
cmpl $0xa8c6a47, %eax        # imm = 0xA8C6A47
jne 0x17b37
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9965
jmp 0x17bef
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x17b49
callq 0x7088
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa40c
cmpl $-0x1, %eax
je 0x17bbc
cmpl 0x48(%r13), %eax
jae 0x17c38
movq 0x40(%r13), %rcx
cltq
orq (%rcx,%rax,8), %r15
jmp 0x17af6
movq 0x10(%r13), %r14
testq %r14, %r14
je 0x17be2
movq %r14, %rdi
callq 0x2140
movq %rax, %rdx
jmp 0x17be4
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x17b49
callq 0x6f0e
orq 0x18(%r13), %r15
movq %r15, %rbp
jmp 0x17b81
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x17b49
callq 0x6ed8
movsd 0x10(%r13), %xmm0
jmp 0x17b9c
movsd 0x18(%r13), %xmm0
jmp 0x17b9c
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x17b49
callq 0x701c
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x17c3d
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0xa89c
addq $0x1c, %r14
leaq 0x6411(%rip), %rsi      # 0x1df77
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x94f4
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 0x8f9f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9a00
jmp 0x17bef
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa47b
cmpl $-0x1, %eax
je 0x17bf7
cmpl 0x28(%r13), %eax
jae 0x17c42
movq 0x20(%r13), %rcx
cltq
movq (%rcx,%rax,8), %rbp
jmp 0x17b81
xorl %edx, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7f0c
movq %rax, %rbp
orq %r15, %rbp
jmp 0x17b81
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x17c47
movq %rbp, %r14
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0xa89c
jmp 0x17b5f
callq 0x6e36
callq 0x6ea2
callq 0x7052
callq 0x70f4
callq 0x6f44
callq 0x6f7a
callq 0x70be
callq 0x6fb0
callq 0x6fe6