// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/crossassemblers/macro11/parse.c
// Function: parse_unary
EX_TREE        *parse_unary(
    char *cp)
{
    EX_TREE        *tp;

    /* Skip leading whitespace */
    cp = skipwhite(cp);

    if (*cp == '%') {                  /* Register notation */
        unsigned        reg;

        cp++;
        reg = strtoul(cp, &cp, 8);
        if (reg > 7)
            return ex_err(NULL, cp);

        /* This returns references to the built-in register symbols */
        tp = new_ex_tree();
        tp->type = EX_SYM;
        tp->data.symbol = reg_sym[reg];
        tp->cp = cp;
        return tp;
    }

    /* Unary negate */
    if (*cp == '-') {
        tp = new_ex_tree();
        tp->type = EX_NEG;
        tp->data.child.left = parse_unary(cp + 1);
        tp->cp = tp->data.child.left->cp;
        return tp;
    }

    /* Unary + I can ignore. */
    if (*cp == '+')
        return parse_unary(cp + 1);

    if (*cp == '^') {
        int             save_radix;

        switch (tolower((unsigned char)cp[1])) {
        case 'c':
            /* ^C, ones complement */
            tp = new_ex_tree();
            tp->type = EX_COM;
            tp->data.child.left = parse_unary(cp + 2);
            tp->cp = tp->data.child.left->cp;
            return tp;
        case 'b':
            /* ^B, binary radix modifier */
            save_radix = radix;
            radix = 2;
            tp = parse_unary(cp + 2);
            radix = save_radix;
            return tp;
        case 'o':
            /* ^O, octal radix modifier */
            save_radix = radix;
            radix = 8;
            tp = parse_unary(cp + 2);
            radix = save_radix;
            return tp;
        case 'd':
            /* ^D, decimal radix modifier */
            save_radix = radix;
            radix = 10;
            tp = parse_unary(cp + 2);
            radix = save_radix;
            return tp;
        case 'x':
            /* ^X, hexadecimal radix modifier */
            save_radix = radix;
            radix = 16;
            tp = parse_unary(cp + 2);
            radix = save_radix;
            return tp;
        case 'r':
            /* ^R, RAD50 literal */  {
                int             start,
                                len;
                char           *endcp;
                unsigned        value;

                cp += 2; /* bracketed range is an extension */
                if (brackrange(cp, &start, &len, &endcp))
                    value = rad50(cp + start, NULL);
                else
                    value = rad50(cp, &endcp);
                tp = new_ex_lit(value);
                tp->cp = endcp;
                return tp;
            }
        case 'f':
            /* ^F, single-word floating point literal indicator */  {
                unsigned        flt[1];
                char           *endcp;

                if (!parse_float(cp + 2, &endcp, 1, flt)) {
                    tp = ex_err(NULL, cp + 2);
                } else {
                    tp = new_ex_lit(flt[0]);
                    tp->cp = endcp;
                }
                return tp;
            }
        }

        if (ispunct((unsigned char)cp[1])) {
            char           *ecp;

            /* oddly-bracketed expression like this: ^/expression/ */
            tp = parse_binary(cp + 2, cp[1], 0);
            ecp = skipwhite(tp->cp);

            if (*ecp != cp[1])
                return ex_err(tp, ecp);

            tp->cp = ecp + 1;
            return tp;
        }
    }

    /* Bracketed subexpression */
    if (*cp == '<') {
        char           *ecp;

        tp = parse_binary(cp + 1, '>', 0);
        ecp = skipwhite(tp->cp);
        if (*ecp != '>')
            return ex_err(tp, ecp);

        tp->cp = ecp + 1;
        return tp;
    }

    /* Check for ASCII constants */

    if (*cp == '\'') {
        /* 'x single ASCII character */
        cp++;
        tp = new_ex_lit(*cp & 0xff);
        tp->cp = ++cp;
        return tp;
    }

    if (*cp == '\"') {
        /* "xx ASCII character pair */
        cp++;
        tp = new_ex_lit((cp[0] & 0xff) | ((cp[1] & 0xff) << 8));
        tp->cp = cp + 2;
        return tp;
    }

    /* Numeric constants are trickier than they need to be, */
    /* since local labels start with a digit too. */
    if (isdigit((unsigned char)*cp)) {
        char           *label;
        int             local;

        if ((label = get_symbol(cp, NULL, &local)) == NULL) {
            char           *endcp;
            unsigned long   value;
            int             rad = radix;

            /* get_symbol returning NULL assures me that it's not a
               local label.  */

            /* Look for a trailing period, to indicate decimal... */
            for (endcp = cp; isdigit((unsigned char)*endcp); endcp++) ;
            if (*endcp == '.')
                rad = 10;

            value = strtoul(cp, &endcp, rad);
            if (*endcp == '.')
                endcp++;

            tp = new_ex_lit(value);
            tp->cp = endcp;

            return tp;
        }

        free(label);
    }

    /* Now check for a symbol */
    {
        char           *label;
        int             local;
        SYMBOL         *sym;

        /* Optimization opportunity: I don't really need to call
           get_symbol a second time. */

        if (!(label = get_symbol(cp, &cp, &local))) {
            cp++;                      /*JH: eat first char of illegal label, else endless loop on implied .WORD */
            tp = ex_err(NULL, cp);     /* Not a valid label. */
            return tp;
        }

        sym = lookup_sym(label, &symbol_st);
        if (sym == NULL) {
            /* A symbol from the "PST", which means an instruction
               code. */
            sym = lookup_sym(label, &system_st);
        }

        if (sym != NULL) {
            tp = new_ex_tree();
            tp->cp = cp;
            tp->type = EX_SYM;
            tp->data.symbol = sym;

            free(label);
            return tp;
        }

        /* The symbol was not found. Create an "undefined symbol"
           reference. */
        sym = memcheck(malloc(sizeof(SYMBOL)));
        sym->label = label;
        sym->flags = SYMBOLFLAG_UNDEFINED | local;
        sym->stmtno = stmtno;
        sym->next = NULL;
        sym->section = &absolute_section;
        sym->value = 0;

        tp = new_ex_tree();
        tp->cp = cp;
        tp->type = EX_UNDEFINED_SYM;
        tp->data.symbol = sym;

        return tp;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0x69de(%rip), %r14      # 0xf61c
movq %rbx, %rdi
callq 0x8234
movq %rax, %rbx
movq %rax, 0x10(%rsp)
movzbl (%rax), %r15d
leal -0x22(%r15), %eax
cmpl $0xb, %eax
ja 0x8c69
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
incq %rbx
jmp 0x8c3e
cmpl $0x3c, %r15d
je 0x8d41
cmpl $0x5e, %r15d
jne 0x8dd8
callq 0x22f0
movq (%rax), %rax
movzbl 0x1(%rbx), %r14d
movl (%rax,%r14,4), %eax
leal -0x62(%rax), %ecx
cmpl $0x4, %ecx
ja 0x8d98
leaq 0x69ab(%rip), %rax      # 0xf64c
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0xc4ef(%rip), %r14      # 0x151a0
movl (%r14), %ebp
movl $0x2, (%r14)
jmp 0x8ff1
incq %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, (%rsi)
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x2290
movq %rax, %rbx
testl $0xfffffff8, %ebx       # imm = 0xFFFFFFF8
je 0x8d6e
movq 0x10(%rsp), %rsi
xorl %edi, %edi
jmp 0x9031
callq 0x6c26
movq %rax, %r14
movl $0x6, (%rax)
incq %rbx
movq %rbx, %rdi
callq 0x8c2a
movq %rax, 0x10(%r14)
movq 0x8(%rax), %rax
jmp 0x9066
movzwl 0x1(%rbx), %edi
callq 0x7316
movq %rax, %r14
addq $0x3, %rbx
jmp 0x8eee
movzbl 0x1(%rbx), %edi
callq 0x7316
movq %rax, %r14
addq $0x2, %rbx
jmp 0x8eee
incq %rbx
pushq $0x3e
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8b7d
movq %rax, %r14
movq 0x8(%rax), %rdi
callq 0x8234
cmpb $0x3e, (%rax)
jne 0x902b
incq %rax
jmp 0x9066
callq 0x6c26
movq %rax, %r14
movl $0x2, (%rax)
andl $0x7, %ebx
leaq 0x128fa(%rip), %rax     # 0x1b680
movq (%rax,%rbx,8), %rax
movq %rax, 0x10(%r14)
movq 0x10(%rsp), %rax
jmp 0x9066
cmpl $0x6f, %eax
je 0x8fe0
cmpl $0x72, %eax
je 0x8fb2
cmpl $0x78, %eax
jne 0x8dc5
leaq 0xc3ea(%rip), %r14      # 0x151a0
movl (%r14), %ebp
movl $0x10, (%r14)
jmp 0x8ff1
callq 0x2300
movq (%rax), %rax
testb $0x4, (%rax,%r14,2)
jne 0x9002
callq 0x2300
movq %rax, %r14
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r15,2)
je 0x8e0b
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8597
testq %rax, %rax
je 0x8e99
movq %rax, %rdi
callq 0x2050
leaq 0x10(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x8597
testq %rax, %rax
je 0x8e7d
movq %rax, %rbx
leaq 0xe864(%rip), %rsi      # 0x17690
movq %rax, %rdi
callq 0x974c
testq %rax, %rax
jne 0x8e51
leaq 0x10848(%rip), %rsi     # 0x19688
movq %rbx, %rdi
callq 0x974c
testq %rax, %rax
je 0x8f4e
movq %rax, %r15
callq 0x6c26
movq %rax, %r14
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r14)
movl $0x2, (%r14)
movq %r15, 0x10(%r14)
movq %rbx, %rdi
callq 0x2050
jmp 0x906a
movq 0x10(%rsp), %rsi
incq %rsi
movq %rsi, 0x10(%rsp)
xorl %edi, %edi
callq 0x7349
movq %rax, %r14
jmp 0x906a
leaq 0xc300(%rip), %rax      # 0x151a0
movl (%rax), %edx
movq (%r14), %rcx
movq %rbx, %rax
decq %rax
movzbl 0x1(%rax), %esi
incq %rax
testb $0x8, 0x1(%rcx,%rsi,2)
jne 0x8eab
leaq 0x18(%rsp), %r14
movq %rax, (%r14)
cmpb $0x2e, %sil
pushq $0xa
popq %rax
cmovel %eax, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2290
movq (%r14), %rcx
xorl %ebx, %ebx
cmpb $0x2e, (%rcx)
sete %bl
addq %rcx, %rbx
movl %eax, %edi
callq 0x7316
movq %rax, %r14
movq %rbx, 0x8(%rax)
jmp 0x906a
callq 0x6c26
movq %rax, %r14
movl $0x5, (%rax)
addq $0x2, %rbx
jmp 0x8d02
addq $0x2, %rbx
leaq 0x18(%rsp), %rsi
pushq $0x1
popq %rdx
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
callq 0x8a3e
testl %eax, %eax
je 0x9040
movl 0xc(%rsp), %edi
jmp 0x9059
leaq 0xc261(%rip), %r14      # 0x151a0
movl (%r14), %ebp
movl $0xa, (%r14)
jmp 0x8ff1
pushq $0x28
popq %rdi
callq 0x2230
movq %rax, %rdi
callq 0xcd2d
movq %rax, %r15
movq %rbx, (%rax)
movl 0x18(%rsp), %eax
orl $0x10, %eax
movl %eax, 0x10(%r15)
leaq 0xcbee(%rip), %rax      # 0x15b64
movl (%rax), %eax
movl %eax, 0xc(%r15)
andq $0x0, 0x20(%r15)
leaq 0xc2a8(%rip), %rax      # 0x15230
movq %rax, 0x18(%r15)
andl $0x0, 0x8(%r15)
callq 0x6c26
movq %rax, %r14
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r14)
movl $0x3, (%r14)
movq %r15, 0x10(%r14)
jmp 0x906a
addq $0x2, %rbx
leaq 0xc(%rsp), %rsi
leaq 0x24(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %rbx, %rdi
callq 0x8315
testl %eax, %eax
je 0x904a
movslq 0xc(%rsp), %rax
addq %rax, %rbx
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x9052
leaq 0xc1b9(%rip), %r14      # 0x151a0
movl (%r14), %ebp
movl $0x8, (%r14)
addq $0x2, %rbx
movq %rbx, %rdi
callq 0x8c2a
movl %ebp, (%r14)
jmp 0x906d
movq %rbx, %rdi
addq $0x2, %rdi
movsbl %r14b, %esi
xorl %edx, %edx
callq 0x8b7d
movq %rax, %r14
movq 0x8(%rax), %rdi
callq 0x8234
movb (%rax), %cl
cmpb 0x1(%rbx), %cl
je 0x8d66
movq %r14, %rdi
movq %rax, %rsi
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x7349
xorl %edi, %edi
movq %rbx, %rsi
jmp 0x8e8c
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0xcf1c
movl %eax, %edi
callq 0x7316
movq %rax, %r14
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r14)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq