// File: /workspace/github_c_makefile_star2_O2/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 0x12ad0
movabsq $0xffffffffffff, %r13   # imm = 0xFFFFFFFFFFFF
andq %r12, %r13
movl (%r13), %eax
cmpq $0x8, %rax
ja 0x12f55
movq %rdx, %r14
leaq 0x6df6(%rip), %rcx      # 0x19884
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x10(%r14), %eax
cmpl $0xa8c6a47, %eax        # imm = 0xA8C6A47
je 0x12ded
cmpl $0xb51d04ba, %eax       # imm = 0xB51D04BA
je 0x12dcd
cmpl $0xfd1b18d1, %eax       # imm = 0xFD1B18D1
je 0x12ddd
cmpl $0x83d03615, %eax       # imm = 0x83D03615
jne 0x12dfd
movl 0x14(%r13), %eax
jmp 0x12bca
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x12ec4
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0x4e61
leaq 0x743f(%rip), %rsi      # 0x19f3a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3a8d
jmp 0x12e3c
movq %rsp, %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x4999
testb %al, %al
je 0x12c0a
movq (%rsp), %rbp
jmp 0x12e47
movl 0x10(%r14), %eax
cmpl $0x8d39bde6, %eax       # imm = 0x8D39BDE6
je 0x12cbe
cmpl $0x3e96bd7a, %eax       # imm = 0x3E96BD7A
jne 0x12cdb
cvtsi2sdl 0x20(%r13), %xmm0
jmp 0x12d7b
leaq 0x1c(%r14), %rbp
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x482c
cmpl $-0x1, %eax
je 0x12c90
cmpl 0x58(%r13), %eax
jae 0x12f83
movq 0x50(%r13), %rcx
jmp 0x12cb6
movl 0x10(%r14), %eax
cmpl $0x63e1d819, %eax       # imm = 0x63E1D819
je 0x12d75
cmpl $0x4881d841, %eax       # imm = 0x4881D841
je 0x12d6d
cmpl $0x1562c22, %eax        # imm = 0x1562C22
jne 0x12d8e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3d94
jmp 0x12e90
cmpl $0x83d03615, 0x10(%r14) # imm = 0x83D03615
jne 0x12c4d
movl 0x18(%r13), %eax
cvtsi2sd %rax, %xmm0
jmp 0x12d7b
movl 0x10(%r14), %eax
cmpl $0x9ed64249, %eax       # imm = 0x9ED64249
je 0x12d1e
cmpl $0x789c2706, %eax       # imm = 0x789C2706
jne 0x12d2a
movabsq $0x7ffd000000000002, %rax # imm = 0x7FFD000000000002
leaq 0x1(%rax), %rbp
cmpl $0x3, 0x10(%r13)
cmovneq %rax, %rbp
jmp 0x12e47
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x12e0f
movq 0x103d5(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x54c3(%rip), %rsi      # 0x180f0
leaq 0x55d0(%rip), %rdx      # 0x18204
leaq 0x72f2(%rip), %rcx      # 0x19f2d
leaq 0x6ce4(%rip), %r8       # 0x19926
movl $0x67b, %r9d            # imm = 0x67B
jmp 0x12ef0
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x12e0f
movq 0x10392(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x5480(%rip), %rsi      # 0x180f0
leaq 0x558d(%rip), %rdx      # 0x18204
leaq 0x72af(%rip), %rcx      # 0x19f2d
leaq 0x6ca1(%rip), %r8       # 0x19926
movl $0x607, %r9d            # imm = 0x607
jmp 0x12ef0
movl 0x14(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x48ce
cmpl $-0x1, %eax
je 0x12e59
cmpl 0x48(%r13), %eax
jae 0x12fb4
movq 0x40(%r13), %rcx
cltq
orq (%rcx,%rax,8), %r15
jmp 0x12d22
movq 0x10(%r13), %r14
testq %r14, %r14
je 0x12e83
movq %r14, %rdi
callq 0x2140
movq %rax, %rdx
jmp 0x12e85
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x12e0f
movq 0x10304(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x53f2(%rip), %rsi      # 0x180f0
leaq 0x54ff(%rip), %rdx      # 0x18204
leaq 0x7221(%rip), %rcx      # 0x19f2d
leaq 0x6c13(%rip), %r8       # 0x19926
movl $0x65b, %r9d            # imm = 0x65B
jmp 0x12ef0
orq 0x18(%r13), %r15
movq %r15, %rbp
jmp 0x12e47
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x12e0f
movq 0x102b5(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x53a3(%rip), %rsi      # 0x180f0
leaq 0x54b0(%rip), %rdx      # 0x18204
leaq 0x71d2(%rip), %rcx      # 0x19f2d
leaq 0x6bc4(%rip), %r8       # 0x19926
movl $0x66d, %r9d            # imm = 0x66D
jmp 0x12ef0
movsd 0x10(%r13), %xmm0
jmp 0x12d7b
movsd 0x18(%r13), %xmm0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x34ce
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
je 0x12e0f
movq 0x10255(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x5343(%rip), %rsi      # 0x180f0
leaq 0x5450(%rip), %rdx      # 0x18204
leaq 0x7172(%rip), %rcx      # 0x19f2d
leaq 0x6b64(%rip), %r8       # 0x19926
movl $0x62b, %r9d            # imm = 0x62B
jmp 0x12ef0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3e2a
jmp 0x12e90
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3f34
jmp 0x12e90
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3eaf
jmp 0x12e90
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x12fe5
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0x4e61
addq $0x1c, %r14
leaq 0x712b(%rip), %rsi      # 0x19f57
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x3a8d
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(%r14), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4936
cmpl $-0x1, %eax
je 0x12e98
cmpl 0x28(%r13), %eax
jae 0x13016
movq 0x20(%r13), %rcx
cltq
movq (%rcx,%rax,8), %rbp
jmp 0x12e47
xorl %edx, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x261f
movq %rax, %rbp
orq %r15, %rbp
jmp 0x12e47
movq 0x5f0(%rbx), %rax
cmpq $0x0, 0x58(%rax)
jne 0x13047
movq %rbp, %r14
movabsq $0x7ffc000000000000, %rbp # imm = 0x7FFC000000000000
movq %r12, %rdi
callq 0x4e61
jmp 0x12e25
movq 0x1012d(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x521b(%rip), %rsi      # 0x180f0
leaq 0x5328(%rip), %rdx      # 0x18204
leaq 0x704a(%rip), %rcx      # 0x19f2d
leaq 0x6a3c(%rip), %r8       # 0x19926
movl $0x5df, %r9d            # imm = 0x5DF
xorl %eax, %eax
callq 0x2250
ud2
movq 0x100f8(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x51e6(%rip), %rsi      # 0x180f0
leaq 0x543e(%rip), %rdx      # 0x1834f
leaq 0x7015(%rip), %rcx      # 0x19f2d
leaq 0x6a07(%rip), %r8       # 0x19926
movl $0x674, %r9d            # imm = 0x674
jmp 0x12ef0
movq 0x100ca(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x51b8(%rip), %rsi      # 0x180f0
leaq 0x5410(%rip), %rdx      # 0x1834f
leaq 0x6fe7(%rip), %rcx      # 0x19f2d
leaq 0x69d9(%rip), %r8       # 0x19926
movl $0x614, %r9d            # imm = 0x614
jmp 0x12ef0
movq 0x1009c(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x5214(%rip), %rsi      # 0x1817a
leaq 0x6fc0(%rip), %rdx      # 0x19f2d
leaq 0x69b2(%rip), %rcx      # 0x19926
movl $0x682, %r8d            # imm = 0x682
xorl %eax, %eax
callq 0x2250
ud2
movq 0x1006e(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x515c(%rip), %rsi      # 0x180f0
leaq 0x5312(%rip), %rdx      # 0x182ad
leaq 0x6f8b(%rip), %rcx      # 0x19f2d
leaq 0x697d(%rip), %r8       # 0x19926
movl $0x639, %r9d            # imm = 0x639
jmp 0x12ef0
movq 0x1003d(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x512b(%rip), %rsi      # 0x180f0
leaq 0x52e1(%rip), %rdx      # 0x182ad
leaq 0x6f5a(%rip), %rcx      # 0x19f2d
leaq 0x694c(%rip), %r8       # 0x19926
movl $0x640, %r9d            # imm = 0x640
jmp 0x12ef0
movq 0x1000c(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0x50fa(%rip), %rsi      # 0x180f0
leaq 0x5207(%rip), %rdx      # 0x18204
leaq 0x6f29(%rip), %rcx      # 0x19f2d
leaq 0x691b(%rip), %r8       # 0x19926
movl $0x5f7, %r9d            # imm = 0x5F7
jmp 0x12ef0
movq 0xffdb(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x50c9(%rip), %rsi      # 0x180f0
leaq 0x527f(%rip), %rdx      # 0x182ad
leaq 0x6ef8(%rip), %rcx      # 0x19f2d
leaq 0x68ea(%rip), %r8       # 0x19926
movl $0x647, %r9d            # imm = 0x647
jmp 0x12ef0
movq 0xffaa(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x5098(%rip), %rsi      # 0x180f0
leaq 0x51a5(%rip), %rdx      # 0x18204
leaq 0x6ec7(%rip), %rcx      # 0x19f2d
leaq 0x68b9(%rip), %r8       # 0x19926
movl $0x64b, %r9d            # imm = 0x64B
jmp 0x12ef0