// File: /workspace/github_c_makefile_star2_O1/Yamsman[P]scc/src/lex.c
// Function: lex_expand_macro
int lex_expand_macro(lexer *lx, char *ident, s_pos *m_loc, char **cpos, char *cch, s_pos *cloc) {
	lex_target *tgt = lx->tgt;
	char *pos = *cpos;
	char cur = *cch;
	s_pos loc = *cloc;
	char *m_name, *m_exp;

	//Check to see if there is an applicable macro for the given name
	int expanded = 0;
	int m_param = 0;
	symbol *s = symtable_search(lx->stb, ident);
	m_name = (s != NULL) ? s->name : NULL;
	m_exp = (s != NULL) ? s->mac_exp : NULL;
	for (lex_target *i = tgt; i != NULL; i = i->prev) {
		//Only check macro contexts
		if (i->type != TGT_MACRO) continue;

		//If the macro was found in the symbol table, check if it is a macro parameter
		if (m_exp == NULL) {
			symbol *m = symtable_search(lx->stb, i->name);
			if (m == NULL) continue;

			//Check the macro's parameters
			vector *ptable = &m->type->param;
			for (int j=0; j<ptable->len; j++) {
				s_param *p = ptable->table[j];

				//If a match is found, s becomes the "parent" macro
				if (!strcmp(ident, p->name)) {
					s = m;
					m_param = 1;
					m_name = p->name;
					m_exp = i->mp_exp.table[j];
					goto pfound;
				}
			}
		}

		//Check to see if the macro has been expanded
		if (!strcmp(i->name, ident)) {
			expanded = 1;
		}
	}
	if (m_exp == NULL || expanded) {
		return -1;
	}
	
	//Check for and read function macro arguments
	vector macro_params;
pfound:	vector_init(&macro_params, VECTOR_EMPTY);
	int m_error = 0;
	char *prev_pos = pos;
	char prev_cch = cur;
	s_pos prev_loc = loc;
	while (isspace(cur)) cur = lex_prcc(lx, &pos, &cur, &loc);
	if (cur == '(' && s->is_fmacro) {
		//Read macro arguments
		int paren_c = 0;
		cur = lex_prcc(lx, &pos, &cur, &loc);
		while (cur != ')') {
			//Skip whitespace
			while (isspace(cur)) cur = lex_prcc(lx, &pos, &cur, &loc);

			//Read a single argument
			int len = 0;
			int bsize = 256;
			char *buf = malloc(bsize);
			for (;;) {
				//Only stop if a ')' or ',' is encountered
				//outside of nested parentheses
				if (cur == '(') paren_c++;
				if (cur == ',' && paren_c == 0) break;
				if (cur == ')') {
					if (paren_c == 0)
						break;
					paren_c--;
				}
				if (cur == '\0') break;

				//Resize if needed
				if (len+2 == bsize) {
					bsize *= 2;
					buf = realloc(buf, bsize);
				}

				//Copy character to buffer
				buf[len++] = cur;
				lex_prcc(lx, &pos, &cur, &loc);
			}
			buf[len] = '\0';
			vector_add(&macro_params, buf);

			//Post-read checks
			if (cur == ',')
				lex_prcc(lx, &pos, &cur, &loc);
			if (cur == '\0') {
				m_error = -1;
				c_error(&loc,
					"Unexpected end of input in macro parameter list\n");
				break;
			}
		}
		lex_prcc(lx, &pos, &cur, &loc);
	} else {
		//Rewind and restore whitespace if no argument list was found
		pos = prev_pos;
		cur = prev_cch;
		loc = prev_loc;
	}

	//Fill expansion buffer for predefined __FILE__ or __LINE__ macros
	if (!strcmp(ident, "__FILE__"))
		snprintf(s->mac_exp, 256, "\"%s\"", lx->tgt->name);
	else if (!strcmp(ident, "__LINE__"))
		snprintf(s->mac_exp, 256, "%i", m_loc->line);
	
	//Ensure the number of arguments matches the amount specified in the macro definition
	if (!m_param && macro_params.len != s->type->param.len) {
		m_error = -1;
		if (!lx->m_cexpr) { //Not an error in preprocessor constant expressions
			c_error(m_loc, "Incorrect number of arguments to function-like macro '%s' "
				"(expected %i, read %i)\n",
				ident, s->type->param.len, macro_params.len);
		}
	}

	//If no issues have been encountered, expand the macro
predef:	if (!m_error) {
		//Commit to the state of the current context
		lx->tgt->pos = pos;
		lx->tgt->cch = cur;
		lx->tgt->loc = loc;

		//Open a context for the macro
		lexer_tgt_open(lx, m_name, TGT_MACRO, m_exp);
		tgt = lx->tgt;
		tgt->loc = *m_loc;

		//Update external values
		*cpos = tgt->pos;
		*cch = tgt->cch;
		*cloc = tgt->loc;

		//Set definitions for each parameter
		for (int i=0; i<macro_params.len; i++)
			vector_add(&tgt->mp_exp, macro_params.table[i]);
	} else {
		//Otherwise, free any arguments
		for (int i=0; i<macro_params.len; i++)
			free(macro_params.table[i]);
	}

	//Free the macro name and parameters
	vector_close(&macro_params);
	free(ident);
	return m_error;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, 0x48(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %r12
movq %rcx, 0x78(%rsp)
movq (%rcx), %rax
movq %rax, 0x18(%rsp)
movq %r8, 0x80(%rsp)
movb (%r8), %al
movb %al, 0xf(%rsp)
movq 0x10(%r9), %rax
movq %rax, 0x70(%rsp)
movq %r9, 0x88(%rsp)
movups (%r9), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x60(%rdi), %rdi
callq 0xc219
movq %rax, %r13
testq %rax, %rax
je 0x61c1
movq (%r13), %rax
movq %rax, 0x58(%rsp)
jmp 0x61ca
movq $0x0, 0x58(%rsp)
testq %r13, %r13
je 0x61d5
movq 0x48(%r13), %r14
jmp 0x61d8
xorl %r14d, %r14d
xorl %ebp, %ebp
testq %r12, %r12
movq %r15, 0x30(%rsp)
movq %rbx, 0x50(%rsp)
je 0x632d
movl $0x0, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
cmpl $0x1, 0x8(%r12)
jne 0x6307
testq %r14, %r14
jne 0x62e9
movq 0x60(%rbx), %rdi
movq (%r12), %rsi
callq 0xc219
testq %rax, %rax
je 0x62b5
movq %rax, %r14
movq %r13, 0x28(%rsp)
movq 0x8(%rax), %rax
movslq 0x30(%rax), %r13
movl $0x5, %ecx
testq %r13, %r13
jle 0x62bf
movq 0x28(%rax), %rbp
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movq (%rbp,%rbx,8), %rax
movq (%rax), %r15
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x3110
xorl %ecx, %ecx
testl %eax, %eax
jne 0x6291
movq 0x40(%r12), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rcx, 0x20(%rsp)
movl $0x8, %ecx
movl $0x1, 0x14(%rsp)
movq %r14, 0x28(%rsp)
movq %r15, 0x58(%rsp)
testl %eax, %eax
je 0x62c4
incq %rbx
cmpq %rbx, %r13
jne 0x6251
movq 0x30(%rsp), %r15
movq 0x50(%rsp), %rbx
movq 0x20(%rsp), %r14
xorl %ebp, %ebp
movl $0x5, %ecx
jmp 0x62d5
movl $0x4, %ecx
xorl %r14d, %r14d
jmp 0x62e0
xorl %r14d, %r14d
jmp 0x62d5
movq 0x30(%rsp), %r15
movq 0x50(%rsp), %rbx
movq 0x20(%rsp), %r14
xorl %ebp, %ebp
cmpl $0x5, %ecx
cmovel %ebp, %ecx
movq 0x28(%rsp), %r13
cmpl $0x4, %ecx
je 0x6307
testl %ecx, %ecx
jne 0x631a
movq (%r12), %rdi
movq %r15, %rsi
callq 0x3110
testl %eax, %eax
movl 0x10(%rsp), %eax
movl $0x1, %ecx
cmovel %ecx, %eax
movl %eax, 0x10(%rsp)
movq 0x50(%r12), %r12
testq %r12, %r12
jne 0x61fd
movl $0x2, %ecx
cmpl $0x0, 0x10(%rsp)
setne %bpl
cmpl $0x0, 0x14(%rsp)
sete %dl
jmp 0x6334
movl $0x2, %ecx
movb $0x1, %dl
cmpl $0x8, %ecx
je 0x6359
cmpl $0x2, %ecx
jne 0x6747
testq %r14, %r14
sete %cl
orb %bpl, %cl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x1, %cl
jne 0x6747
movb %dl, 0x14(%rsp)
movq %r14, 0x20(%rsp)
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0xc964
movq 0x18(%rsp), %rbx
movsbq 0xf(%rsp), %rbp
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0x90(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0xa0(%rsp)
callq 0x3230
movq %rax, %r14
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbp,2)
movl %ebp, %eax
movq %r13, 0x28(%rsp)
je 0x63e5
leaq 0x18(%rsp), %r15
leaq 0xf(%rsp), %r12
leaq 0x60(%rsp), %r13
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x5b5f
movb %al, 0xf(%rsp)
movq (%r14), %rcx
movsbq %al, %rdx
testb $0x20, 0x1(%rcx,%rdx,2)
jne 0x63bb
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %r13
cmpb $0x28, %al
jne 0x6577
cmpl $0x0, 0x50(%r13)
je 0x6577
leaq 0x18(%rsp), %r12
leaq 0xf(%rsp), %rbp
leaq 0x60(%rsp), %r15
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x5b5f
movb %al, (%rbp)
movl $0x0, 0x10(%rsp)
xorl %ebx, %ebx
cmpb $0x29, 0xf(%rsp)
je 0x655c
movq (%r14), %rax
movsbq 0xf(%rsp), %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x645d
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x5b5f
movb %al, 0xf(%rsp)
movq (%r14), %rcx
movsbq %al, %rax
testb $0x20, 0x1(%rcx,%rax,2)
jne 0x643d
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %r12
movb 0xf(%rsp), %al
xorl %ecx, %ecx
cmpb $0x28, %al
sete %cl
addl %ecx, %ebx
sete %cl
cmpb $0x2c, %al
jne 0x6488
testl %ebx, %ebx
jne 0x6488
xorl %eax, %eax
xorl %ebx, %ebx
jmp 0x64ff
movl $0x100, %r13d           # imm = 0x100
xorl %ebp, %ebp
cmpb $0x29, %al
jne 0x649b
testb $0x1, %cl
jne 0x64f1
decl %ebx
testb %al, %al
je 0x64f3
leaq 0x2(%rbp), %rax
movl %r13d, %ecx
cmpq %rcx, %rax
jne 0x64bc
addl %r13d, %r13d
movslq %r13d, %rsi
movq %r12, %rdi
callq 0x31a0
movq %rax, %r12
movb 0xf(%rsp), %al
movb %al, (%r12,%rbp)
incq %rbp
leaq 0x18(%rsp), %rsi
leaq 0xf(%rsp), %rdx
movq %r15, %rcx
callq 0x5b5f
movb 0xf(%rsp), %al
xorl %ecx, %ecx
cmpb $0x28, %al
sete %cl
addl %ecx, %ebx
sete %cl
cmpb $0x2c, %al
jne 0x6490
testl %ebx, %ebx
jne 0x6490
xorl %ebx, %ebx
movl %ebp, %eax
movq 0x28(%rsp), %r13
leaq 0xf(%rsp), %rbp
movb $0x0, (%r12,%rax)
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0xc9fa
cmpb $0x2c, 0xf(%rsp)
leaq 0x18(%rsp), %r12
jne 0x652b
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x5b5f
movb 0xf(%rsp), %bpl
testb %bpl, %bpl
jne 0x654e
movq %r15, %rdi
leaq 0x78ca(%rip), %rsi      # 0xde09
xorl %eax, %eax
callq 0x3250
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
testb %bpl, %bpl
leaq 0xf(%rsp), %rbp
jne 0x6422
leaq 0x18(%rsp), %rsi
leaq 0xf(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x5b5f
movq 0x30(%rsp), %r15
jmp 0x65a3
movq %rbx, 0x18(%rsp)
movb %bpl, 0xf(%rsp)
movaps 0x90(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x0, 0x10(%rsp)
leaq 0x7890(%rip), %rsi      # 0xde3a
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
movq 0x50(%rsp), %rbx
movq 0x20(%rsp), %r14
je 0x65f3
leaq 0x7881(%rip), %rsi      # 0xde48
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
jne 0x6610
movq 0x48(%r13), %rdi
movq 0x48(%rsp), %rax
movl (%rax), %ecx
leaq 0x6f11(%rip), %rdx      # 0xd4f6
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x30d0
jmp 0x6610
movq 0x48(%r13), %rdi
movq (%rbx), %rax
movq (%rax), %rcx
leaq 0x783f(%rip), %rdx      # 0xde43
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x30d0
cmpb $0x0, 0x14(%rsp)
je 0x664f
movl 0x40(%rsp), %r8d
movq 0x8(%r13), %rax
movl 0x30(%rax), %ecx
cmpl %ecx, %r8d
movl 0x10(%rsp), %ebp
je 0x6653
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, 0x6c(%rbx)
jne 0x6653
leaq 0x7813(%rip), %rsi      # 0xde51
movq 0x48(%rsp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3250
jmp 0x6653
movl 0x10(%rsp), %ebp
testl %ebp, %ebp
je 0x6684
cmpl $0x0, 0x40(%rsp)
jle 0x6733
xorl %ebx, %ebx
movq 0x38(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x3030
incq %rbx
movslq 0x40(%rsp), %rax
cmpq %rax, %rbx
jl 0x6664
jmp 0x6733
movq 0x18(%rsp), %rax
movq (%rbx), %rcx
movq %rax, 0x18(%rcx)
movb 0xf(%rsp), %al
movb %al, 0x20(%rcx)
movaps 0x60(%rsp), %xmm0
movups %xmm0, 0x28(%rcx)
movq 0x70(%rsp), %rax
movq %rax, 0x38(%rcx)
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movl $0x1, %edx
movq %r14, %rcx
callq 0x5a01
movq (%rbx), %r14
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x38(%r14)
movups (%rcx), %xmm0
movups %xmm0, 0x28(%r14)
movq 0x18(%r14), %rax
movq 0x78(%rsp), %rcx
movq %rax, (%rcx)
movb 0x20(%r14), %al
movq 0x80(%rsp), %rcx
movb %al, (%rcx)
movq 0x38(%r14), %rax
movq 0x88(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups 0x28(%r14), %xmm0
movups %xmm0, (%rcx)
cmpl $0x0, 0x40(%rsp)
jle 0x6733
addq $0x40, %r14
xorl %ebx, %ebx
movq 0x38(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r14, %rdi
callq 0xc9fa
incq %rbx
movslq 0x40(%rsp), %rax
cmpq %rax, %rbx
jl 0x6715
leaq 0x38(%rsp), %rdi
callq 0xc9b1
movq %r15, %rdi
callq 0x3030
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq