// File: /workspace/github_c_makefile_star2_O2/Edilbert[P]BS9/bs9.c
// Function: EvalOperand
char *EvalOperand(char *p, int *v, int prio)
{
   int  i;    // loop index
   int  l;    // length of string
   int  o;    // priority of operator
   int  r;    // return value
   int  w;    // value of right operand
   char c;    // current character

   r = UNDEF; // preset result to UNDEF

   p = SkipSpace(p);
   c = *p;
   if (df) fprintf(df,"Eval <%s>",p);

   if (c == ',' )  return p; // comma separator

   // Start parsing unary operators
   // PC represents the current program counter

   if (*p && strchr("[(+-!~<>*&$'\"%?",*p))
   {
      for (i=0 ; i < UNAOPS ; ++i)
      if (*p == unaop[i].op)
      {
          p = unaop[i].foo(p,&r);
          break;
      }
   }
   else if (isdigit(c)) p = EvalDecValue(p,&r);    // decimal constant
   else if (isym(c))    p = EvalSymValue(p,&r);    // symbol or label
   else
   {
      ErrorLine(p);
      ErrorMsg("Illegal operand\n");
      exit(1);
   }

   // Left operand has been parsed successfully
   // now look for binary operators
   //  r is left  operand
   //  w is right operand

   // the left or right operand may be UNDEFINED in phase 1
   // in that case do not perform the operation, just parse

   if (CodeStyle == 1 && *p == ' ')
   {
      *v = r;
      p += strlen(p);
      if (df) fprintf(df,"Result: %4x %d\n",r,r);
      return p;
   }
   p = SkipSpace(p);

   while (*p && strchr("*/+-<>=!&^|",*p))
   {
      // loop through all binary operators

      for (i=0 ; i < BINOPS ; ++i)
      {
         l = strlen((binop[i].op));
         if (!strncmp(p,binop[i].op,l))
         {
            if ((o = binop[i].prio) <= prio)
            {
               *v = r;
               if (CodeStyle == 1 && *p == ' ') p += strlen(p);
               if (df) fprintf(df,"Result: %4x %d\n",r,r);
               return p;
            }
            p = EvalOperand(p+l,&w,o);
            if (w == UNDEF) r = UNDEF;
            else r = binop[i].foo(r,w);
            break;
         }
      }
      if (i == BINOPS)  // no valid operator
      {
         ErrorMsg("Syntax error: binary operator expected\n");
         ErrorLine(p);
         exit(1);
      }
   }
   *v = r;
   if (CodeStyle == 1 && *p == ' ') p += strlen(p);
   if (df)
   {
      fprintf(df," -> %4x %d",r,r);
      if (*p) fprintf(df,"  Rest:%s\n",p);
      else    fprintf(df,"\n");
   }
   return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movq %rsi, 0x8(%rsp)
movl $0xff0000, 0x4(%rsp)    # imm = 0xFF0000
callq 0x2675
movq %rax, %r15
movsbq (%rax), %rbx
movq 0x2120b(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x381b
leaq 0x7d06(%rip), %rsi      # 0xb517
movq %r15, %rdx
xorl %eax, %eax
callq 0x2210
cmpb $0x2c, %bl
jne 0x3828
movq %r15, %rbx
jmp 0x3a41
movsbl (%r15), %r12d
testl %r12d, %r12d
je 0x3875
leaq 0x7ce9(%rip), %rdi      # 0xb521
pushq $0x10
popq %rdx
movl %r12d, %esi
callq 0x21b0
testq %rax, %rax
je 0x3875
xorl %edx, %edx
leaq 0xff2f(%rip), %rax      # 0x13780
leaq 0x10(%rdx), %rcx
cmpq $0x100, %rcx            # imm = 0x100
je 0x38b2
cmpb (%rdx,%rax), %r12b
movq %rcx, %rdx
jne 0x3851
leaq 0x4(%rsp), %rsi
movq %r15, %rdi
callq *-0x8(%rcx,%rax)
jmp 0x38af
callq 0x2320
movq (%rax), %rax
testb $0x8, 0x1(%rax,%rbx,2)
jne 0x38a2
movl %ebx, %edi
callq 0x279e
testl %eax, %eax
je 0x3a71
leaq 0x4(%rsp), %rsi
movq %r15, %rdi
callq 0x3b06
jmp 0x38af
leaq 0x4(%rsp), %rsi
movq %r15, %rdi
callq 0x4065
movq %rax, %r15
cmpl $0x1, 0x8069b(%rip)     # 0x83f54
jne 0x38df
cmpb $0x20, (%r15)
jne 0x38df
movl 0x4(%rsp), %ebp
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
movq %r15, %rdi
callq 0x2110
movq %rax, %rbx
addq %r15, %rbx
jmp 0x3a16
movq %r15, %rdi
callq 0x2675
movq %rax, %rbx
leaq 0xff7f(%rip), %r15      # 0x13870
movsbl (%rbx), %esi
testl %esi, %esi
je 0x398a
leaq 0x7c4f(%rip), %rdi      # 0xb552
pushq $0xc
popq %rdx
callq 0x21b0
testq %rax, %rax
je 0x398a
xorl %ecx, %ecx
leaq 0x18(%rcx), %r12
cmpq $0x1b0, %r12            # imm = 0x1B0
je 0x3a53
movq (%rcx,%r15), %r13
movq %r13, %rdi
callq 0x2110
movslq %eax, %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2080
movq %r12, %rcx
testl %eax, %eax
jne 0x3912
movl -0x10(%r12,%r15), %edx
cmpl %ebp, %edx
jle 0x39f2
addq %r14, %rbx
movq %rbx, %rdi
leaq 0x14(%rsp), %rsi
callq 0x37d5
movq %rax, %rbx
movl 0x14(%rsp), %esi
movl $0xff0000, %eax         # imm = 0xFF0000
cmpl $0xff0000, %esi         # imm = 0xFF0000
je 0x3981
movl 0x4(%rsp), %edi
callq *-0x8(%r12,%r15)
movl %eax, 0x4(%rsp)
jmp 0x38f1
movl 0x4(%rsp), %ebp
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
cmpl $0x1, 0x805b8(%rip)     # 0x83f54
jne 0x39ae
cmpb $0x20, (%rbx)
jne 0x39ae
movq %rbx, %rdi
callq 0x2110
addq %rax, %rbx
movq 0x2105b(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x3a41
leaq 0x7bc1(%rip), %rsi      # 0xb586
movl %ebp, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2210
cmpb $0x0, (%rbx)
movq 0x21036(%rip), %rax     # 0x24a10
je 0x3a36
leaq 0x7bae(%rip), %rsi      # 0xb591
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2210
jmp 0x3a41
movl 0x4(%rsp), %ebp
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
cmpl $0x1, 0x80550(%rip)     # 0x83f54
jne 0x3a16
cmpb $0x20, (%rbx)
jne 0x3a16
movq %rbx, %rdi
callq 0x2110
addq %rax, %rbx
movq 0x20ff3(%rip), %rdi     # 0x24a10
testq %rdi, %rdi
je 0x3a41
leaq 0x7b19(%rip), %rsi      # 0xb542
movl %ebp, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2210
jmp 0x3a41
pushq $0xa
popq %rdi
movq %rax, %rsi
callq 0x21a0
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7b04(%rip), %rdi      # 0xb55e
xorl %eax, %eax
callq 0x2af7
movq %rbx, %rdi
callq 0x2a71
pushq $0x1
popq %rdi
callq 0x22f0
movq %r15, %rdi
callq 0x2a71
leaq 0x7ab1(%rip), %rdi      # 0xb531
xorl %eax, %eax
callq 0x2af7
jmp 0x3a69