// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rdi, %r15
movq (%rdi), %r13
movq %rcx, 0x78(%rsp)
movq (%rcx), %rax
movq %rax, 0x8(%rsp)
movq %r8, 0x80(%rsp)
movb (%r8), %al
movb %al, 0x7(%rsp)
movq 0x10(%r9), %rax
movq %rax, 0x40(%rsp)
movq %r9, 0x88(%rsp)
movups (%r9), %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x60(%rdi), %rdi
callq 0xb822
movq %rax, %r12
testq %rax, %rax
je 0x5fc4
movq (%r12), %rax
movq %rax, 0x50(%rsp)
movq 0x48(%r12), %rbx
jmp 0x5fcf
movq $0x0, 0x50(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r14, 0x28(%rsp)
movq %r15, 0x58(%rsp)
movq %r12, 0x10(%rsp)
testq %r13, %r13
je 0x60a2
cmpl $0x1, 0x8(%r13)
jne 0x6009
testq %rbx, %rbx
je 0x600f
movq (%r13), %rdi
movq %r14, %rsi
callq 0x3110
testl %eax, %eax
pushq $0x1
popq %rax
cmovel %eax, %ebp
movq 0x50(%r13), %r13
jmp 0x5fe0
movq 0x60(%r15), %rdi
movq (%r13), %rsi
callq 0xb822
testq %rax, %rax
je 0x6066
movq %rax, %rbx
movq %r13, 0x18(%rsp)
movq 0x8(%rax), %r13
movl 0x30(%r13), %r15d
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x606a
movq 0x28(%r13), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %r14
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x3110
incq %r12
testl %eax, %eax
jne 0x6040
jmp 0x6085
xorl %ebx, %ebx
jmp 0x6009
xorl %ebx, %ebx
movq 0x28(%rsp), %r14
movq 0x58(%rsp), %r15
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %r12
jmp 0x5ff5
movq 0x18(%rsp), %r13
movq 0x40(%r13), %rax
movq -0x8(%rax,%r12,8), %rax
movq %rax, 0x70(%rsp)
movq %r14, 0x50(%rsp)
movq %rbx, %r12
jmp 0x60bb
pushq $-0x1
popq %rax
testq %rbx, %rbx
je 0x645c
movq %rbx, 0x70(%rsp)
testl %ebp, %ebp
jne 0x645c
movq %r12, 0x10(%rsp)
movq %r13, 0x18(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x24(%rsp)
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0xbe8a
leaq 0x8(%rsp), %rbx
movq (%rbx), %r12
leaq 0x7(%rsp), %r14
movb (%r14), %bpl
leaq 0x30(%rsp), %r15
movaps (%r15), %xmm0
movaps %xmm0, 0x90(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0xa0(%rsp)
callq 0x3230
movq %rax, %r13
movl %ebp, %eax
movq (%r13), %rcx
movsbq %al, %rdx
testb $0x20, 0x1(%rcx,%rdx,2)
je 0x6132
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x59a3
movb %al, 0x7(%rsp)
jmp 0x610f
cmpb $0x28, %al
jne 0x629d
movq 0x10(%rsp), %rax
cmpl $0x0, 0x50(%rax)
je 0x629d
leaq 0x8(%rsp), %rbx
leaq 0x7(%rsp), %r15
leaq 0x30(%rsp), %rbp
movq %rbx, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x59a3
movl %eax, %r12d
movb %al, (%r15)
xorl %r14d, %r14d
cmpb $0x29, %r12b
jne 0x618f
jmp 0x62c6
movq %rbx, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x59a3
movl %eax, %r12d
movb %al, 0x7(%rsp)
movq (%r13), %rax
movsbq %r12b, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x617a
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %rbx
movl $0x100, %ebp            # imm = 0x100
xorl %r15d, %r15d
movl %r14d, %eax
xorl %r14d, %r14d
cmpb $0x28, %r12b
sete %r14b
cmpb $0x2c, %r12b
sete %cl
addl %eax, %r14d
sete %dl
testb %dl, %cl
jne 0x6237
cmpb $0x29, %r12b
je 0x61e2
movzbl %r12b, %eax
testl %eax, %eax
jne 0x61f2
jmp 0x623a
testl %r14d, %r14d
movl $0x0, %r14d
je 0x623a
decl %eax
movl %eax, %r14d
leaq 0x2(%r15), %rax
movl %ebp, %ecx
cmpq %rcx, %rax
jne 0x6212
addl %ebp, %ebp
movslq %ebp, %rsi
movq %rbx, %rdi
callq 0x31a0
movq %rax, %rbx
movb 0x7(%rsp), %r12b
movb %r12b, (%rbx,%r15)
incq %r15
leaq 0x8(%rsp), %rsi
leaq 0x7(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x59a3
movb 0x7(%rsp), %r12b
jmp 0x61b3
xorl %r14d, %r14d
movl %r15d, %eax
movb $0x0, (%rbx,%rax)
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0xbf10
movb 0x7(%rsp), %r12b
cmpb $0x2c, %r12b
leaq 0x8(%rsp), %rbx
leaq 0x7(%rsp), %r15
leaq 0x30(%rsp), %rbp
jne 0x627b
movq %rbx, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x59a3
movb 0x7(%rsp), %r12b
testb %r12b, %r12b
jne 0x616f
leaq 0x6b7e(%rip), %rsi      # 0xce09
leaq 0x30(%rsp), %rdi
xorl %eax, %eax
callq 0x35e4
pushq $-0x1
popq %r13
jmp 0x62c9
movq %r12, 0x8(%rsp)
movb %bpl, 0x7(%rsp)
movaps 0x90(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
jmp 0x62dd
xorl %r13d, %r13d
leaq 0x8(%rsp), %rsi
leaq 0x7(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x59a3
movq 0x28(%rsp), %r15
movq 0x58(%rsp), %rbx
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r12
leaq 0x6b42(%rip), %rsi      # 0xce3a
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
je 0x6338
leaq 0x6b3d(%rip), %rsi      # 0xce48
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
jne 0x6356
movq 0x48(%r12), %rdi
movq 0x48(%rsp), %rax
movl (%rax), %ecx
leaq 0x61cc(%rip), %rdx      # 0xc4f6
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x30d0
jmp 0x6356
movq 0x48(%r12), %rdi
movq (%rbx), %rax
movq (%rax), %rcx
leaq 0x6af9(%rip), %rdx      # 0xce43
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x30d0
testq %r14, %r14
jne 0x6371
movl 0x68(%rsp), %r8d
movq 0x8(%r12), %rax
movl 0x30(%rax), %ecx
cmpl %ecx, %r8d
jne 0x646e
testl %r13d, %r13d
je 0x6399
xorl %ebx, %ebx
movslq 0x68(%rsp), %rax
cmpq %rax, %rbx
jge 0x6446
movq 0x60(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x3030
incq %rbx
jmp 0x6378
movq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq %rax, 0x18(%rcx)
movb 0x7(%rsp), %al
movb %al, 0x20(%rcx)
movaps 0x30(%rsp), %xmm0
movups %xmm0, 0x28(%rcx)
movq 0x40(%rsp), %rax
movq %rax, 0x38(%rcx)
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
movq 0x70(%rsp), %rcx
callq 0x5855
movq (%rbx), %rbx
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x38(%rbx)
movups (%rcx), %xmm0
movups %xmm0, 0x28(%rbx)
movq 0x18(%rbx), %rax
movq 0x78(%rsp), %rcx
movq %rax, (%rcx)
movb 0x20(%rbx), %al
movq 0x80(%rsp), %rcx
movb %al, (%rcx)
movq 0x38(%rbx), %rax
movq 0x88(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups 0x28(%rbx), %xmm0
movups %xmm0, (%rcx)
addq $0x40, %rbx
xorl %r14d, %r14d
movslq 0x68(%rsp), %rax
cmpq %rax, %r14
jge 0x6441
movq 0x60(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0xbf10
incq %r14
jmp 0x6421
movl %r13d, 0x24(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xbecc
movq %r15, %rdi
callq 0x3030
movl 0x24(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x6c(%rbx)
jne 0x6376
leaq 0x69d2(%rip), %rsi      # 0xce51
movq 0x48(%rsp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x35e4
jmp 0x6376