// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
movzbl (%r15), %eax
cmpl $0x9, %eax
je 0x8e4c
cmpl $0x20, %eax
jne 0x8e51
incq %r15
jmp 0x8e3e
cmpl $0x40, %eax
jne 0x8e5f
incq %r15
movl $0x8, (%rbx)
movzbl (%r15), %eax
cmpl $0x23, %eax
je 0x8f56
cmpl $0x2d, %eax
jne 0x8eed
leaq 0x2(%r15), %rdi
movzbl -0x1(%rdi), %r13d
cmpl $0x20, %r13d
je 0x8e86
cmpl $0x9, %r13d
jne 0x8e8b
incq %rdi
jmp 0x8e75
cmpb $0x28, %r13b
jne 0x8ee3
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x90f8
movq %rax, %r12
movq %rax, %rdi
callq 0x5dd9
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x8edb
movq 0x8(%r12), %rcx
incq %rcx
movzbl -0x1(%rcx), %edx
cmpl $0x20, %edx
je 0x8eb1
cmpl $0x9, %edx
je 0x8eb1
cmpb $0x29, %dl
jne 0x8edb
orl (%rbx), %eax
orl $0x20, %eax
movl %eax, (%rbx)
movl $0x1, %ebp
testq %r14, %r14
je 0x8edb
movq %rcx, (%r14)
movq %r12, %rdi
callq 0x6e73
cmpb $0x28, %r13b
je 0x90a8
cmpb $0x28, (%r15)
jne 0x8f84
incq %r15
xorl %ebp, %ebp
movq %r15, %rdi
xorl %esi, %esi
callq 0x90f8
movq %rax, %r15
movq %rax, %rdi
callq 0x5dd9
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x900c
movl %eax, %r12d
movq 0x8(%r15), %r13
incq %r13
movzbl -0x1(%r13), %eax
cmpl $0x9, %eax
je 0x8f23
cmpl $0x20, %eax
je 0x8f23
cmpl $0x29, %eax
jne 0x900c
movzbl (%r13), %eax
cmpl $0x9, %eax
je 0x8f51
cmpl $0x20, %eax
jne 0x9081
incq %r13
jmp 0x8f3e
incq %r15
orb $0x17, (%rbx)
movq %r15, %rdi
xorl %esi, %esi
callq 0x90f8
movq %rax, 0x8(%rbx)
movl $0x1, %ebp
testq %r14, %r14
je 0x90a8
movq 0x8(%rax), %rax
movq %rax, (%r14)
jmp 0x90a8
movq %r15, %rdi
xorl %esi, %esi
callq 0x90f8
movq %rax, 0x8(%rbx)
movq 0x8(%rax), %rdi
movzbl (%rdi), %eax
cmpl $0x9, %eax
je 0x8fa3
cmpl $0x20, %eax
jne 0x8fa8
incq %rdi
jmp 0x8f96
cmpl $0x28, %eax
jne 0x9019
incq %rdi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x90f8
movq %rax, %r15
movq %rax, %rdi
callq 0x5dd9
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x900c
movq 0x8(%r15), %r12
incq %r12
movzbl -0x1(%r12), %ecx
cmpl $0x20, %ecx
je 0x8fcf
cmpl $0x9, %ecx
je 0x8fcf
cmpb $0x29, %cl
jne 0x900c
orl (%rbx), %eax
orl $0x30, %eax
movl %eax, (%rbx)
movq %r15, %rdi
callq 0x6e73
movl $0x1, %ebp
testq %r14, %r14
je 0x90a8
movq %r12, (%r14)
jmp 0x90a8
movq %r15, %rdi
callq 0x6e73
jmp 0x90a8
testq %r14, %r14
je 0x9021
movq %rdi, (%r14)
movq 0x8(%rbx), %rdi
cmpl $0x2, (%rdi)
jne 0x905a
movq 0x10(%rdi), %r15
movq 0x18(%r15), %rax
movl 0x8(%rax), %r14d
cmpl $0x5, %r14d
jne 0x9054
callq 0x6e73
movq $0x0, 0x8(%rbx)
movl 0x8(%r15), %eax
orl %eax, (%rbx)
movl $0x1, %ebp
cmpl $0x5, %r14d
je 0x90a8
movl (%rbx), %eax
testb $0x8, %al
jne 0x9073
leaq 0xcb15(%rip), %rcx      # 0x15b7c
cmpl $0x0, (%rcx)
je 0x9073
orl $0x1f, %eax
movl %eax, (%rbx)
jmp 0x90a3
orl $0x37, %eax
movl %eax, (%rbx)
movl $0x1, 0x4(%rbx)
jmp 0x90a3
cmpl $0x2b, %eax
jne 0x90b9
testq %r14, %r14
je 0x9091
incq %r13
movq %r13, (%r14)
movl (%rbx), %eax
orl %r12d, %eax
orl $0x10, %eax
movl %eax, (%rbx)
movq %r15, %rdi
callq 0x6e73
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 0x90d7
xorl %edi, %edi
callq 0x7766
movq %rax, 0x8(%rbx)
movl (%rbx), %eax
orl %r12d, %eax
orl $0x30, %eax
movl %eax, (%rbx)
jmp 0x90e1
orl %eax, %r12d
orl $0x8, %r12d
movl %r12d, (%rbx)
movq %r15, %rdi
callq 0x6e73
movl $0x1, %ebp
testq %r14, %r14
je 0x90a8
movq %r13, (%r14)
jmp 0x90a8