// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/crossassemblers/macro11/parse.c
// Function: get_mode
int get_mode(
    char *cp,
    char **endp,
    ADDR_MODE *mode)
{
    EX_TREE        *value;

    mode->offset = NULL;
    mode->rel = 0;
    mode->type = 0;

    cp = skipwhite(cp);

    /* @ means "indirect," sets bit 3 */
    if (*cp == '@') {
        cp++;
        mode->type |= 010;
    }

    /* Immediate modes #imm and @#imm */
    if (*cp == '#') {
        cp++;
        mode->type |= 027;
        mode->offset = parse_expr(cp, 0);
        if (endp)
            *endp = mode->offset->cp;
        return TRUE;
    }

    /* Check for -(Rn) */

    if (*cp == '-') {
        char           *tcp = skipwhite(cp + 1);

        if (*tcp++ == '(') {
            unsigned        reg;

            /* It's -(Rn) */
            value = parse_expr(tcp, 0);
            reg = get_register(value);
            if (reg == NO_REG || (tcp = skipwhite(value->cp), *tcp++ != ')')) {
                free_tree(value);
                return FALSE;
            }
            mode->type |= 040 | reg;
            if (endp)
                *endp = tcp;
            free_tree(value);
            return TRUE;
        }
    }

    /* Check for (Rn) */
    if (*cp == '(') {
        char           *tcp;
        unsigned        reg;

        value = parse_expr(cp + 1, 0);
        reg = get_register(value);

        if (reg == NO_REG || (tcp = skipwhite(value->cp), *tcp++ != ')')) {
            free_tree(value);
            return FALSE;
        }

        tcp = skipwhite(tcp);
        if (*tcp == '+') {
            tcp++;                     /* It's (Rn)+ */
            if (endp)
                *endp = tcp;
            mode->type |= 020 | reg;
            free_tree(value);
            return TRUE;
        }

        if (mode->type == 010) {       /* For @(Rn) there's an implied 0 offset */
            mode->offset = new_ex_lit(0);
            mode->type |= 060 | reg;
            free_tree(value);
            if (endp)
                *endp = tcp;
            return TRUE;
        }

        mode->type |= 010 | reg;       /* Mode 10 is register indirect as
                                          in (Rn) */
        free_tree(value);
        if (endp)
            *endp = tcp;
        return TRUE;
    }

    /* Modes with an offset */

    mode->offset = parse_expr(cp, 0);

    cp = skipwhite(mode->offset->cp);

    if (*cp == '(') {
        unsigned        reg;

        /* indirect register plus offset */
        value = parse_expr(cp + 1, 0);
        reg = get_register(value);
        if (reg == NO_REG || (cp = skipwhite(value->cp), *cp++ != ')')) {
            free_tree(value);
            return FALSE;              /* Syntax error in addressing mode */
        }

        mode->type |= 060 | reg;

        free_tree(value);

        if (endp)
            *endp = cp;
        return TRUE;
    }

    /* Plain old expression. */

    if (endp)
        *endp = cp;

    /* It might be a register, though. */
    if (mode->offset->type == EX_SYM) {
        SYMBOL         *sym = mode->offset->data.symbol;

        if (sym->section->type == SECTION_REGISTER) {
            free_tree(mode->offset);
            mode->offset = NULL;
            mode->type |= sym->value;
            return TRUE;
        }
    }

    /* It's either 067 (PC-relative) or 037 (absolute) mode, depending */
    /* on user option. */

    if (mode->type & 010) {            /* Have already noted indirection? */
        mode->type |= 067;             /* If so, then PC-relative is the only
                                          option */
        mode->rel = 1;                 /* Note PC-relative */
    } else if (enabl_ama) {            /* User asked for absolute adressing? */
        mode->type |= 037;             /* Give it to him. */
    } else {
        mode->type |= 067;             /* PC-relative */
        mode->rel = 1;                 /* Note PC-relative */
    }

    return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
incq %rdi
movzbl -0x1(%rdi), %ecx
cmpl $0x9, %ecx
je 0x8d1a
cmpl $0x20, %ecx
je 0x8d1a
cmpl $0x40, %ecx
jne 0x8d3f
movl $0x8, (%rbx)
movb (%rdi), %cl
movl $0x1f, %eax
jmp 0x8d47
decq %rdi
movl $0x17, %eax
cmpb $0x23, %cl
je 0x8d78
movzbl %cl, %eax
cmpl $0x28, %eax
je 0x8e04
cmpl $0x2d, %eax
jne 0x8e60
leaq 0x2(%rdi), %rax
movzbl -0x1(%rax), %edx
cmpl $0x9, %edx
je 0x8d73
cmpl $0x20, %edx
jne 0x8da2
incq %rax
jmp 0x8d65
incq %rdi
movl %eax, (%rbx)
xorl %esi, %esi
callq 0x8fc7
movq %rax, 0x8(%rbx)
movl $0x1, %ebp
testq %r14, %r14
je 0x8f77
movq 0x8(%rax), %rax
movq %rax, (%r14)
jmp 0x8f77
cmpl $0x28, %edx
jne 0x8dff
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x8fc7
movq %rax, %r15
movq %rax, %rdi
callq 0x5cd1
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x8ee5
movq 0x8(%r15), %rcx
incq %rcx
movzbl -0x1(%rcx), %edx
cmpl $0x20, %edx
je 0x8dcd
cmpl $0x9, %edx
je 0x8dcd
cmpb $0x29, %dl
jne 0x8ee5
orl (%rbx), %eax
orl $0x20, %eax
movl %eax, (%rbx)
testq %r14, %r14
je 0x8f6a
movq %rcx, (%r14)
jmp 0x8f6a
cmpb $0x28, %cl
jne 0x8e60
incq %rdi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x8fc7
movq %rax, %r15
movq %rax, %rdi
callq 0x5cd1
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x8ee5
movl %eax, %r12d
movq 0x8(%r15), %r13
incq %r13
movzbl -0x1(%r13), %eax
cmpl $0x9, %eax
je 0x8e2d
cmpl $0x20, %eax
je 0x8e2d
cmpl $0x29, %eax
jne 0x8ee5
movzbl (%r13), %eax
cmpl $0x9, %eax
je 0x8e5b
cmpl $0x20, %eax
jne 0x8f50
incq %r13
jmp 0x8e48
xorl %esi, %esi
callq 0x8fc7
movq %rax, 0x8(%rbx)
movq 0x8(%rax), %rdi
movzbl (%rdi), %ecx
cmpl $0x9, %ecx
je 0x8e7c
cmpl $0x20, %ecx
jne 0x8e81
incq %rdi
jmp 0x8e6f
cmpl $0x28, %ecx
jne 0x8ef2
incq %rdi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x8fc7
movq %rax, %r15
movq %rax, %rdi
callq 0x5cd1
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x8ee5
movq 0x8(%r15), %r12
incq %r12
movzbl -0x1(%r12), %ecx
cmpl $0x20, %ecx
je 0x8ea8
cmpl $0x9, %ecx
je 0x8ea8
cmpb $0x29, %cl
jne 0x8ee5
orl (%rbx), %eax
orl $0x30, %eax
movl %eax, (%rbx)
movq %r15, %rdi
callq 0x6d77
movl $0x1, %ebp
testq %r14, %r14
je 0x8f77
movq %r12, (%r14)
jmp 0x8f77
movq %r15, %rdi
callq 0x6d77
jmp 0x8f77
testq %r14, %r14
je 0x8efe
movq %rdi, (%r14)
movq 0x8(%rbx), %rax
cmpl $0x2, (%rax)
jne 0x8f29
movq 0x10(%rax), %r14
movq 0x18(%r14), %rcx
cmpl $0x5, 0x8(%rcx)
jne 0x8f29
movq %rax, %rdi
callq 0x6d77
movq $0x0, 0x8(%rbx)
movl 0x8(%r14), %eax
orl %eax, (%rbx)
jmp 0x8f72
movl (%rbx), %eax
testb $0x8, %al
jne 0x8f42
leaq 0xcc46(%rip), %rcx      # 0x15b7c
cmpl $0x0, (%rcx)
je 0x8f42
orl $0x1f, %eax
movl %eax, (%rbx)
jmp 0x8f72
orl $0x37, %eax
movl %eax, (%rbx)
movl $0x1, 0x4(%rbx)
jmp 0x8f72
cmpl $0x2b, %eax
jne 0x8f88
testq %r14, %r14
je 0x8f60
incq %r13
movq %r13, (%r14)
movl (%rbx), %eax
orl %r12d, %eax
orl $0x10, %eax
movl %eax, (%rbx)
movq %r15, %rdi
callq 0x6d77
movl $0x1, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%rbx), %eax
cmpl $0x8, %eax
jne 0x8fa6
xorl %edi, %edi
callq 0x7642
movq %rax, 0x8(%rbx)
movl (%rbx), %eax
orl %r12d, %eax
orl $0x30, %eax
movl %eax, (%rbx)
jmp 0x8fb0
orl %eax, %r12d
orl $0x8, %r12d
movl %r12d, (%rbx)
movq %r15, %rdi
callq 0x6d77
movl $0x1, %ebp
testq %r14, %r14
je 0x8f77
movq %r13, (%r14)
jmp 0x8f77