// File: /workspace/github_c_makefile_star2_O2/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)
callq 0x8234
movq %rax, %r15
movb (%rax), %cl
cmpb $0x40, %cl
jne 0x87bf
movl $0x8, (%rbx)
movb 0x1(%r15), %cl
incq %r15
pushq $0x1f
jmp 0x87c1
pushq $0x17
popq %rax
cmpb $0x2d, %cl
je 0x886f
movzbl %cl, %ecx
cmpl $0x28, %ecx
je 0x8807
cmpl $0x23, %ecx
jne 0x88d9
incq %r15
movl %eax, (%rbx)
movq %r15, %rdi
xorl %esi, %esi
callq 0x8a04
movq %rax, 0x8(%rbx)
pushq $0x1
popq %rbp
testq %r14, %r14
je 0x89b6
movq 0x8(%rax), %rax
movq %rax, (%r14)
jmp 0x89b6
incq %r15
xorl %ebp, %ebp
movq %r15, %rdi
xorl %esi, %esi
callq 0x8a04
movq %rax, %r15
movq %rax, %rdi
callq 0x5aa8
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x894f
movl %eax, %r12d
movq 0x8(%r15), %rdi
callq 0x8234
cmpb $0x29, (%rax)
jne 0x894f
incq %rax
movq %rax, %rdi
callq 0x8234
movq %rax, %r13
cmpb $0x2b, (%rax)
jne 0x89c7
testq %r14, %r14
je 0x8863
incq %r13
movq %r13, (%r14)
movl (%rbx), %eax
orl %r12d, %eax
orl $0x10, %eax
movl %eax, (%rbx)
jmp 0x88cc
leaq 0x1(%r15), %rdi
callq 0x8234
cmpb $0x28, (%rax)
jne 0x88d9
incq %rax
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x8a04
movq %rax, %r15
movq %rax, %rdi
callq 0x5aa8
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x894f
movl %eax, %r12d
movq 0x8(%r15), %rdi
callq 0x8234
cmpb $0x29, (%rax)
jne 0x894f
orl (%rbx), %r12d
orl $0x20, %r12d
movl %r12d, (%rbx)
testq %r14, %r14
je 0x88cc
incq %rax
movq %rax, (%r14)
movq %r15, %rdi
callq 0x6add
jmp 0x89b3
movq %r15, %rdi
xorl %esi, %esi
callq 0x8a04
movq %rax, %r15
movq %rax, 0x8(%rbx)
movq 0x8(%rax), %rdi
callq 0x8234
cmpb $0x28, (%rax)
jne 0x8959
incq %rax
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x8a04
movq %rax, %r15
movq %rax, %rdi
callq 0x5aa8
cmpl $0x1ff, %eax            # imm = 0x1FF
je 0x894f
movl %eax, %r13d
movq 0x8(%r15), %rdi
callq 0x8234
cmpb $0x29, (%rax)
jne 0x894f
movq %rax, %r12
orl (%rbx), %r13d
orl $0x30, %r13d
movl %r13d, (%rbx)
movq %r15, %rdi
callq 0x6add
pushq $0x1
popq %rbp
testq %r14, %r14
je 0x89b6
incq %r12
movq %r12, (%r14)
jmp 0x89b6
movq %r15, %rdi
callq 0x6add
jmp 0x89b6
testq %r14, %r14
je 0x8965
movq %rax, (%r14)
movq 0x8(%rbx), %r15
cmpl $0x2, (%r15)
jne 0x898e
movq 0x10(%r15), %r14
movq 0x18(%r14), %rax
cmpl $0x5, 0x8(%rax)
jne 0x898e
movq %r15, %rdi
callq 0x6add
andq $0x0, 0x8(%rbx)
movl 0x8(%r14), %eax
orl %eax, (%rbx)
jmp 0x89b3
movl (%rbx), %eax
testb $0x8, %al
jne 0x89a7
leaq 0xd1e1(%rip), %rcx      # 0x15b7c
cmpl $0x0, (%rcx)
je 0x89a7
orl $0x1f, %eax
movl %eax, (%rbx)
jmp 0x89b3
orl $0x37, %eax
movl %eax, (%rbx)
movl $0x1, 0x4(%rbx)
pushq $0x1
popq %rbp
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 0x89e5
xorl %edi, %edi
callq 0x7316
movq %rax, 0x8(%rbx)
movl (%rbx), %eax
orl %r12d, %eax
orl $0x30, %eax
movl %eax, (%rbx)
jmp 0x89ef
orl %eax, %r12d
orl $0x8, %r12d
movl %r12d, (%rbx)
movq %r15, %rdi
callq 0x6add
pushq $0x1
popq %rbp
testq %r14, %r14
je 0x89b6
movq %r13, (%r14)
jmp 0x89b6