// File: /workspace/github_cpp_makefile_star2_O2/stacksmith[P]picoasm/cassembler.cpp
// Function: CAssembler::buildSymbolTable()
bool CAssembler::buildSymbolTable()
{
	list<CSourceLine*>::iterator it ;
	unsigned int address = 0 ;
	
	for ( it = m_source.begin() ; it != m_source.end() ; it++ ) {
		string name = toUpper( (*it)->getColumn( 0 ) ) ; 				// case insensitive
		
		if ( name == "NAMEREG" ) {
			if ( !(*it)->isColumn( 3 ) ) {
				error( (*it)->m_lineNr , "'NAMEREG register_name, new_name' expected" ) ;
				return FALSE ;
			}
			
			if ( (*it)->getColumn( 2 ) != "," ) {
				error( (*it)->m_lineNr , "Comma expected" ) ;
				return FALSE ;
			}
			
			// Check that the NAMEREG is unique

			list<CNamereg*>::iterator it1 ;
			for ( it1 = m_registerTable.begin(); it1 != m_registerTable.end() ; it1++) {
			  if( (*it1)->reg == (*it)->getColumn(1)) {
			    error ( (*it)->m_lineNr , string("NAMEREG  reg ("+ (*it)->getColumn(1)+") already aliased.").c_str());
			    return FALSE;
			  }
			  if( (*it1)->name == (*it)->getColumn(3)) {
			    error ( (*it)->m_lineNr , string ("NAMEREG alias ("+(*it)->getColumn(3)+") already used.").c_str());
			    return FALSE;
			  }
			}

			CNamereg *nr = new CNamereg ;
			nr->reg = (*it)->getColumn( 1 ) ;
			nr->name = (*it)->getColumn( 3 ) ;
			m_registerTable.push_back( nr ) ;
			(*it)->m_type = CSourceLine::stNamereg ;
			
		} else if ( name == "CONSTANT" ) {
			if ( !(*it)->isColumn( 3 ) ) {
				error( (*it)->m_lineNr , "'CONSTANT name, valued' expected" ) ;
				return FALSE ;
			}
			

		 	if ( (*it)->getColumn( 2 ) != "," ) {
				error( (*it)->m_lineNr , "Comma expected" ) ;
				return FALSE ;
			}
						
			// GES: Could enforce the 's' constant rule here.
			//  Cannot have a constant which is a register name.

			string ckname = (*it)->getColumn(1);
			if( (ckname[0] == 's' || ckname[0] == 'S') && ckname.length() == 2) {
			  if( (ckname[1] >= '0' && ckname[1] <= '9') || (ckname[1] >= 'a' && ckname[1] <= 'f')
			      || (ckname[1] >= 'A' && ckname[1] <= 'F')) {
			    error( (*it)->m_lineNr, "ILLEGAL CONSTANT: constant cannot be a register name s[0-f]");
			    return FALSE;
			  }
			}

			// Check that this constant isn't already defined
			list<CConstant*>::iterator ita;
			for ( ita = m_constantTable.begin(); ita != m_constantTable.end(); ita++) {
			  if( (*ita)->name == (*it)->getColumn( 1 )) {
			    error( (*it)->m_lineNr , string("CONSTANT ("+(*it)->getColumn(1)+") already defined.").c_str());
			    return FALSE ;
			  }
			}
			  
			CConstant *nr = new CConstant ;
			nr->value = (*it)->getColumn( 3 ) ;
			nr->name = (*it)->getColumn( 1 ) ; ;
			m_constantTable.push_back( nr ) ;
			(*it)->m_type = CSourceLine::stConstant ;
		} else if ( name == "ADDRESS" ) {
			if ( !(*it)->isColumn( 1 ) ) {
				error( (*it)->m_lineNr , "Value expected" ) ;
				return FALSE ;
			} 
			
			if ( sscanf( (*it)->getColumn( 1 ).c_str(), "%X", &address ) != 1 ) {
				error( (*it)->m_lineNr , "Invalid address" ) ;
				return FALSE ;
			}
			(*it)->m_type = CSourceLine::stAddress ;
			(*it)->m_address = address ;
			
		} else if ( getInstruction( (*it)->getColumn( 0 ) ) < 0 ) {

		  // Check for unique label..
		  list<CLabel*>::iterator it2 ;
		  for ( it2 = m_labelTable.begin() ; it2 != m_labelTable.end() ; it2++ ) {
		    if( (*it2)->name == (*it)->getColumn(0)) {
		      error( (*it)->m_lineNr , string ("Duplicate Lable ("+ (*it)->getColumn(0) +":).").c_str());
			return FALSE;
		    }
		  }
			CLabel *label = new CLabel ;
			label->name = (*it)->getColumn( 0 ) ;
			char buf[ 32 ] ;
			sprintf( buf, "%d", address ) ;
			label->value = buf ;
			m_labelTable.push_back( label ) ;
			
			(*it)->m_type = CSourceLine::stLabel ;
			(*it)->m_address = address ;
			if ( (*it)->isColumn( 1 ) && (*it)->getColumn( 1 ) == ":" ) {
				if ( (*it)->isColumn( 2 ) ) {
					if ( getInstruction( (*it)->getColumn( 2 ) ) < 0 ) {
						error( (*it)->m_lineNr , "Instruction expected" ) ;
						return FALSE ;
					} else {
						address = address + 1 ;
					}
				}
			} else {
				error( (*it)->m_lineNr , "Label or Instruction expected" ) ;
				return FALSE ;
			}
		} else {
			(*it)->m_address = address ;
			address = address + 1 ;
		}
	}	

#ifdef PRINT_DEBUG_TABLE
	// RDC 02/02/2007 - don't print symbol table data
 	cout << "Constants :" << endl ;
 	list<CConstant*>::iterator it0 ;
 	for ( it0 = m_constantTable.begin() ; it0 != m_constantTable.end() ; it0++ ) {
 		cout << (*it0)->name << " = " << (*it0)->value << endl ;
 	}
	
 	cout << "Namereg :" << endl ;
 	list<CNamereg*>::iterator it1 ;
 	for ( it1 = m_registerTable.begin() ; it1 != m_registerTable.end() ; it1++ ) {
 		cout << (*it1)->reg << " = " << (*it1)->name << endl ;
 	}
	
 	cout << "labels :" << endl ;
 	list<CLabel*>::iterator it2 ;
 	for ( it2 = m_labelTable.begin() ; it2 != m_labelTable.end() ; it2++ ) {
 		cout << (*it2)->name << " = " << (*it2)->value << endl ;
 	}
#endif
	return TRUE ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
leaq -0x2c(%rbp), %rax
andl $0x0, (%rax)
leaq 0x48(%rdi), %rax
movq %rax, -0xa8(%rbp)
leaq 0x30(%rdi), %rax
movq %rax, -0x38(%rbp)
leaq 0x18(%rdi), %rax
movq %rax, -0xb0(%rbp)
leaq -0x170(%rbp), %rbx
leaq -0xf0(%rbp), %r13
leaq -0xa0(%rbp), %r12
leaq 0x17c193(%rip), %rcx    # 0x58164c
movq %rcx, -0xc8(%rbp)
movq %rdi, %r15
movq %r13, -0xc0(%rbp)
movq %rdi, -0xd0(%rbp)
movq (%r15), %r15
cmpq %rdi, %r15
je 0x405bc0
movq 0x10(%r15), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x407856
movq %r13, %rdi
movq %rbx, %rsi
callq 0x405208
movq %rbx, %rdi
callq 0x47b9c0
movq %r13, %rdi
leaq 0x17c015(%rip), %rsi    # 0x58151d
callq 0x407845
testb %al, %al
je 0x4055d4
movq 0x10(%r15), %rsi
movq 0x10(%rsi), %rax
subq 0x8(%rsi), %rax
cmpq $0x60, %rax
jbe 0x405c34
movq %r12, %rdi
pushq $0x2
popq %rdx
callq 0x407856
movq %r12, %rdi
leaq 0x17c010(%rip), %rsi    # 0x581550
callq 0x4078a9
movl %eax, %ebx
movq %r12, %rdi
callq 0x47b9c0
movq -0xb0(%rbp), %r13
testb %bl, %bl
jne 0x405c27
movq (%r13), %r13
cmpq -0xb0(%rbp), %r13
je 0x405712
movq 0x10(%r13), %rbx
movq 0x10(%r15), %rsi
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x407856
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4078b6
movl %eax, %ebx
movq %r12, %rdi
callq 0x47b9c0
testb %bl, %bl
jne 0x405af7
movq 0x10(%r13), %rbx
movq 0x10(%r15), %rsi
movq %r12, %rdi
pushq $0x3
popq %rdx
callq 0x407856
addq $0x20, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4078b6
movl %eax, %ebx
movq %r12, %rdi
callq 0x47b9c0
testb %bl, %bl
je 0x40555e
jmp 0x405b44
movq %r13, %rdi
leaq 0x17bfc5(%rip), %rsi    # 0x5815a3
callq 0x407845
testb %al, %al
je 0x405698
movq 0x10(%r15), %rsi
movq 0x10(%rsi), %rax
subq 0x8(%rsi), %rax
cmpq $0x61, %rax
jb 0x405c3d
movq %r12, %rdi
pushq $0x2
popq %rdx
callq 0x407856
movq %r12, %rdi
leaq 0x17bf3a(%rip), %rsi    # 0x581550
callq 0x4078a9
movl %eax, %ebx
movq %r12, %rdi
callq 0x47b9c0
testb %bl, %bl
jne 0x405c27
movq 0x10(%r15), %rsi
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x407856
movq -0xa0(%rbp), %rax
movzbl (%rax), %ecx
orl $0x20, %ecx
movq -0x38(%rbp), %r13
cmpl $0x73, %ecx
jne 0x405972
cmpq $0x2, -0x98(%rbp)
movq -0x38(%rbp), %r13
jne 0x405972
movb 0x1(%rax), %al
movq -0x38(%rbp), %r13
cmpb $0x30, %al
jl 0x405972
cmpb $0x3a, %al
jb 0x405c62
cmpb $0x61, %al
jb 0x405964
movq -0x38(%rbp), %r13
cmpb $0x67, %al
jae 0x405972
jmp 0x405c62
movq %r13, %rdi
leaq 0x180ae4(%rip), %rsi    # 0x586186
callq 0x407845
movq 0x10(%r15), %rsi
testb %al, %al
je 0x4057b3
movq 0x10(%rsi), %rax
subq 0x8(%rsi), %rax
cmpq $0x21, %rax
jb 0x405c46
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x407856
movq -0xa0(%rbp), %rdi
leaq 0x17be3c(%rip), %rsi    # 0x58151a
leaq -0x2c(%rbp), %rdx
xorl %eax, %eax
callq 0x4d44b0
movl %eax, %ebx
movq %r12, %rdi
callq 0x47b9c0
movq 0x10(%r15), %rsi
cmpl $0x1, %ebx
jne 0x405c4f
movl $0x4, 0x24(%rsi)
movl -0x2c(%rbp), %eax
movl %eax, 0x20(%rsi)
jmp 0x405adc
pushq $0x40
popq %rdi
leaq -0xf0(%rbp), %r13
callq 0x40ce70
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
andq $0x0, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
movq %rax, %rcx
addq $0x30, %rcx
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movb %dl, 0x30(%rax)
movq %rax, -0x78(%rbp)
movq 0x10(%r15), %rsi
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x407856
movq -0x78(%rbp), %rdi
movq %r12, %rsi
callq 0x47ba10
movq %r12, %rdi
callq 0x47b9c0
movq 0x10(%r15), %rsi
movq %r12, %rdi
pushq $0x3
popq %rdx
callq 0x407856
movq -0x78(%rbp), %rdi
addq $0x20, %rdi
movq %r12, %rsi
callq 0x47ba10
movq %r12, %rdi
callq 0x47b9c0
movq -0xb0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x40793a
movq 0x10(%r15), %rax
movl $0x2, 0x24(%rax)
jmp 0x405adc
leaq -0x150(%rbp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x407856
movq %r14, %rsi
callq 0x4053d4
movl %eax, %ebx
movq %r14, %rdi
callq 0x47b9c0
testl %ebx, %ebx
js 0x4057ee
movl -0x2c(%rbp), %eax
movq 0x10(%r15), %rcx
movl %eax, 0x20(%rcx)
incl %eax
movl %eax, -0x2c(%rbp)
jmp 0x405adc
movq -0xa8(%rbp), %r13
movq (%r13), %r13
cmpq -0xa8(%rbp), %r13
je 0x405836
movq 0x10(%r13), %rbx
movq 0x10(%r15), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x407856
addq $0x20, %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4078b6
movl %eax, %ebx
movq %r12, %rdi
callq 0x47b9c0
testb %bl, %bl
je 0x4057f5
jmp 0x405bd8
pushq $0x40
popq %rdi
callq 0x40ce70
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
andq $0x0, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
movq %rax, %rcx
addq $0x30, %rcx
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movb %dl, 0x30(%rax)
movq %rax, -0x58(%rbp)
movq 0x10(%r15), %rsi
movq %r12, %rdi
xorl %edx, %edx
leaq -0xf0(%rbp), %r13
callq 0x407856
movq -0x58(%rbp), %rdi
addq $0x20, %rdi
movq %r12, %rsi
callq 0x47ba10
movq %r12, %rdi
callq 0x47b9c0
movl -0x2c(%rbp), %edx
movq %r12, %rdi
leaq 0x18c6d4(%rip), %rsi    # 0x591f74
xorl %eax, %eax
callq 0x4d4320
movq -0x58(%rbp), %rdi
movq %r12, %rsi
callq 0x47c970
movq -0xa8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x40795a
movq 0x10(%r15), %rsi
movl $0x1, 0x24(%rsi)
movl -0x2c(%rbp), %eax
movl %eax, 0x20(%rsi)
movq 0x10(%rsi), %rax
subq 0x8(%rsi), %rax
cmpq $0x21, %rax
jb 0x405c92
leaq -0x78(%rbp), %r14
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x407856
movq %r14, %rdi
leaq 0x17ff0e(%rip), %rsi    # 0x58580d
callq 0x407845
movl %eax, %ebx
movq %r14, %rdi
callq 0x47b9c0
movq 0x10(%r15), %rsi
testb %bl, %bl
je 0x405c92
movq 0x10(%rsi), %rax
subq 0x8(%rsi), %rax
cmpq $0x41, %rax
jb 0x405adc
leaq -0x110(%rbp), %rdi
pushq $0x2
popq %rdx
callq 0x407856
leaq -0x110(%rbp), %r14
movq %r14, %rsi
callq 0x4053d4
movl %eax, %ebx
movq %r14, %rdi
callq 0x47b9c0
testl %ebx, %ebx
js 0x405c80
incl -0x2c(%rbp)
jmp 0x405adc
addb $-0x41, %al
movq -0x38(%rbp), %r13
cmpb $0x5, %al
jbe 0x405c62
movq (%r13), %r13
cmpq -0x38(%rbp), %r13
je 0x405a2d
movq 0x10(%r13), %rbx
movq 0x10(%r15), %rsi
leaq -0x78(%rbp), %r14
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x407856
addq $0x20, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4078b6
movl %eax, %ebx
movq %r14, %rdi
callq 0x47b9c0
testb %bl, %bl
je 0x405972
movq 0x10(%r15), %rsi
movl (%rsi), %eax
movl %eax, -0xb4(%rbp)
leaq -0x130(%rbp), %r14
movq %r14, %rdi
pushq $0x1
popq %rdx
callq 0x407856
leaq -0x58(%rbp), %rbx
movq %rbx, %rdi
leaq 0x17bc29(%rip), %rsi    # 0x581609
movq %r14, %rdx
callq 0x40790b
leaq -0x78(%rbp), %rdi
movq %rbx, %rsi
leaq 0x17bc1e(%rip), %rdx    # 0x581614
callq 0x4078e2
movq -0x78(%rbp), %rdx
movl -0xb4(%rbp), %esi
callq 0x405312
leaq -0x78(%rbp), %rdi
callq 0x47b9c0
leaq -0x58(%rbp), %rdi
callq 0x47b9c0
leaq -0x130(%rbp), %rdi
callq 0x47b9c0
jmp 0x405ac3
pushq $0x40
popq %rdi
callq 0x40ce70
leaq 0x10(%rax), %rcx
movq %rcx, (%rax)
andq $0x0, 0x8(%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
movq %rax, %rcx
addq $0x30, %rcx
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movb %dl, 0x30(%rax)
movq %rax, -0x58(%rbp)
movq 0x10(%r15), %rsi
leaq -0x78(%rbp), %rbx
movq %rbx, %rdi
pushq $0x3
popq %rdx
callq 0x407856
movq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x47ba10
movq %rbx, %rdi
callq 0x47b9c0
movq 0x10(%r15), %rsi
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0x407856
movq -0x58(%rbp), %rdi
addq $0x20, %rdi
movq %rbx, %rsi
callq 0x47ba10
movq %rbx, %rdi
callq 0x47b9c0
movq -0x38(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x40794a
movq 0x10(%r15), %rax
movl $0x3, 0x24(%rax)
movq %r12, %rdi
callq 0x47b9c0
cmpq -0x38(%rbp), %r13
leaq -0xf0(%rbp), %r13
jne 0x405bad
movq %r13, %rdi
callq 0x47b9c0
movq -0xd0(%rbp), %rdi
leaq -0x170(%rbp), %rbx
jmp 0x4054d1
movq 0x10(%r15), %rsi
movl (%rsi), %ebx
leaq -0x58(%rbp), %rdi
pushq $0x1
popq %rdx
callq 0x407856
leaq 0x17ba51(%rip), %rsi    # 0x581561
leaq -0x78(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x40790b
leaq 0x17ba4c(%rip), %rdx    # 0x581570
leaq -0xa0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4078e2
movq -0xa0(%rbp), %rdx
movl %ebx, %esi
callq 0x405312
jmp 0x405b8f
movq 0x10(%r15), %rsi
movl (%rsi), %ebx
leaq -0x58(%rbp), %rdi
pushq $0x3
popq %rdx
callq 0x407856
leaq 0x17ba26(%rip), %rsi    # 0x581583
leaq -0x78(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x40790b
leaq 0x17ba22(%rip), %rdx    # 0x581593
leaq -0xa0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4078e2
movq -0xa0(%rbp), %rdx
movl %ebx, %esi
callq 0x405312
leaq -0xa0(%rbp), %rdi
callq 0x47b9c0
leaq -0x78(%rbp), %rdi
callq 0x47b9c0
leaq -0x58(%rbp), %rdi
callq 0x47b9c0
leaq -0xf0(%rbp), %rdi
callq 0x47b9c0
movq -0xd0(%rbp), %rdi
cmpq %rdi, %r15
sete %al
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r15), %rsi
movl (%rsi), %ebx
leaq -0x58(%rbp), %rdi
xorl %edx, %edx
callq 0x407856
leaq 0x17ba46(%rip), %rsi    # 0x581636
leaq -0x78(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x40790b
leaq 0x17ba44(%rip), %rdx    # 0x581648
leaq -0xa0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4078e2
movq -0xa0(%rbp), %rdx
movl %ebx, %esi
callq 0x405312
jmp 0x405b8f
movq 0x10(%r15), %rsi
leaq 0x17b920(%rip), %rdx    # 0x581552
jmp 0x405c56
leaq 0x17b8ea(%rip), %rdx    # 0x581525
jmp 0x405c56
leaq 0x17b968(%rip), %rdx    # 0x5815ac
jmp 0x405c56
leaq 0x17b9da(%rip), %rdx    # 0x581627
jmp 0x405c56
leaq 0x17c1b7(%rip), %rdx    # 0x581e0d
movl (%rsi), %esi
callq 0x405312
jmp 0x405bad
movq 0x10(%r15), %rax
movl (%rax), %esi
leaq 0x17b95e(%rip), %rdx    # 0x5815cd
callq 0x405312
leaq -0xa0(%rbp), %rdi
jmp 0x405ba8
movq 0x10(%r15), %rsi
leaq 0x17b9ca(%rip), %rax    # 0x581655
movq %rax, -0xc8(%rbp)
movl (%rsi), %esi
movq -0xc8(%rbp), %rdx
callq 0x405312
jmp 0x405bad
jmp 0x405d89
jmp 0x405d76
movq %rax, %r14
leaq -0x110(%rbp), %rdi
jmp 0x405d80
jmp 0x405d30
jmp 0x405d41
jmp 0x405d4f
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
movq %rax, %r14
leaq -0x78(%rbp), %rdi
callq 0x47b9c0
jmp 0x405cf6
movq %rax, %r14
leaq -0x58(%rbp), %rdi
callq 0x47b9c0
jmp 0x405d04
movq %rax, %r14
leaq -0x130(%rbp), %rdi
callq 0x47b9c0
jmp 0x405d79
jmp 0x405d76
jmp 0x405d76
jmp 0x405d76
jmp 0x405d89
movq %rax, %r14
leaq -0x150(%rbp), %rdi
jmp 0x405d80
jmp 0x405d89
jmp 0x405d30
jmp 0x405d41
jmp 0x405d4f
jmp 0x405d89
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0x47b9c0
jmp 0x405d44
movq %rax, %r14
leaq -0x78(%rbp), %rdi
callq 0x47b9c0
jmp 0x405d52
movq %rax, %r14
leaq -0x58(%rbp), %rdi
jmp 0x405d80
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
jmp 0x405d76
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
jmp 0x405d89
movq %rax, %r14
movq %rbx, -0xc0(%rbp)
jmp 0x405d8c
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0x47b9c0
jmp 0x405d8c
jmp 0x405d89
movq %rax, %r14
movq -0xc0(%rbp), %rdi
callq 0x47b9c0
movq %r14, %rdi
callq 0x4ad3d0