// File: /workspace/github_c_makefile_star2_O3/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, 0x50(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movq (%rdi), %r15
movq %rcx, 0x78(%rsp)
movq (%rcx), %rax
movq %rax, 0x10(%rsp)
movq %r8, 0x80(%rsp)
movb (%r8), %al
movb %al, 0xf(%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 0xc187
testq %rax, %rax
movq %rax, 0x18(%rsp)
je 0x6251
movq %rax, %rcx
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x48(%rcx), %r13
jmp 0x625d
movq $0x0, 0x58(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
testq %r15, %r15
movq %r14, 0x60(%rsp)
movq %r12, 0x70(%rsp)
je 0x631e
movl $0x1, %ebx
cmpl $0x1, 0x8(%r15)
jne 0x630a
testq %r13, %r13
jne 0x62fa
movq 0x60(%r12), %rdi
movq (%r15), %rsi
callq 0xc187
testq %rax, %rax
je 0x62e8
movq %rax, %rbx
movq 0x8(%rax), %rax
movslq 0x30(%rax), %r13
testq %r13, %r13
jle 0x62f2
movq %rbp, 0x48(%rsp)
movq 0x28(%rax), %r12
xorl %ebp, %ebp
movq (%r12,%rbp,8), %rax
movq (%rax), %r14
movq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x3110
testl %eax, %eax
je 0x633e
incq %rbp
cmpq %rbp, %r13
jne 0x62b4
xorl %r13d, %r13d
movq 0x60(%rsp), %r14
movq 0x70(%rsp), %r12
movq 0x48(%rsp), %rbp
jmp 0x62f5
xorl %r13d, %r13d
movl $0x1, %ebx
jmp 0x630a
xorl %r13d, %r13d
movl $0x1, %ebx
movq (%r15), %rdi
movq %r14, %rsi
callq 0x3110
testl %eax, %eax
cmovel %ebx, %ebp
movq 0x50(%r15), %r15
testq %r15, %r15
jne 0x6277
testl %ebp, %ebp
setne %cl
jmp 0x6320
xorl %ecx, %ecx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r13, %r13
je 0x6766
movb $0x1, %al
testb %cl, %cl
movq 0x18(%rsp), %r14
je 0x6350
jmp 0x6766
movq 0x40(%r15), %rax
movq (%rax,%rbp,8), %r13
xorl %eax, %eax
movq %r14, 0x58(%rsp)
movq %rbx, %r14
movl %eax, 0x6c(%rsp)
movq %r13, 0x48(%rsp)
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0xc88c
movq 0x10(%rsp), %r12
movsbq 0xf(%rsp), %rbp
movaps 0x30(%rsp), %xmm0
movaps %xmm0, 0x90(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0xa0(%rsp)
callq 0x3230
movq %rax, %r13
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbp,2)
movl %ebp, %eax
je 0x63d8
movq %r14, 0x18(%rsp)
leaq 0x10(%rsp), %rbx
leaq 0xf(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x5be3
movb %al, 0xf(%rsp)
movq (%r13), %rcx
movsbq %al, %rdx
testb $0x20, 0x1(%rcx,%rdx,2)
jne 0x63b2
movq 0x18(%rsp), %r14
cmpb $0x28, %al
jne 0x6583
cmpl $0x0, 0x50(%r14)
je 0x6583
leaq 0x10(%rsp), %rsi
leaq 0xf(%rsp), %rbx
leaq 0x30(%rsp), %rcx
movq %rbx, %rdx
callq 0x5be3
movb %al, (%rbx)
xorl %ebp, %ebp
cmpb $0x29, %al
je 0x65b2
movl %eax, %r12d
movq %r14, 0x18(%rsp)
xorl %r14d, %r14d
leaq 0x20(%rsp), %r15
leaq 0x10(%rsp), %rbp
leaq 0xf(%rsp), %rbx
movq (%r13), %rax
movsbq %r12b, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x6465
leaq 0x30(%rsp), %r15
movq %rbp, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x5be3
movl %eax, %r12d
movb %al, 0xf(%rsp)
movq (%r13), %rax
movsbq %r12b, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x643c
leaq 0x20(%rsp), %r15
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %rbx
xorl %eax, %eax
cmpb $0x28, %r12b
sete %al
addl %r14d, %eax
sete %cl
cmpb $0x2c, %r12b
jne 0x6495
testl %eax, %eax
jne 0x6495
xorl %ecx, %ecx
xorl %r14d, %r14d
jmp 0x6523
movl $0x100, %ebp            # imm = 0x100
xorl %r15d, %r15d
cmpb $0x29, %r12b
je 0x64b0
movzbl %r12b, %ecx
testl %ecx, %ecx
je 0x6513
movl %eax, %r14d
jmp 0x64b8
testb $0x1, %cl
jne 0x6511
decl %r14d
leaq 0x2(%r15), %rax
movl %ebp, %ecx
cmpq %rcx, %rax
jne 0x64d8
addl %ebp, %ebp
movslq %ebp, %rsi
movq %rbx, %rdi
callq 0x31a0
movq %rax, %rbx
movb 0xf(%rsp), %r12b
leaq 0x30(%rsp), %rcx
movb %r12b, (%rbx,%r15)
incq %r15
leaq 0x10(%rsp), %rsi
leaq 0xf(%rsp), %rdx
callq 0x5be3
movb 0xf(%rsp), %r12b
xorl %eax, %eax
cmpb $0x28, %r12b
sete %al
addl %r14d, %eax
sete %cl
cmpb $0x2c, %r12b
jne 0x649d
testl %eax, %eax
jne 0x649d
xorl %eax, %eax
movl %r15d, %ecx
movl %eax, %r14d
leaq 0x20(%rsp), %r15
leaq 0x10(%rsp), %rbp
movb $0x0, (%rbx,%rcx)
movq %r15, %rdi
movq %rbx, %rsi
callq 0xc922
movb 0xf(%rsp), %r12b
cmpb $0x2c, %r12b
leaq 0xf(%rsp), %rbx
jne 0x6557
movq %rbp, %rsi
movq %rbx, %rdx
leaq 0x30(%rsp), %rcx
callq 0x5be3
movb 0xf(%rsp), %r12b
cmpb $0x29, %r12b
je 0x65ab
movzbl %r12b, %eax
testl %eax, %eax
jne 0x6428
leaq 0x7899(%rip), %rsi      # 0xde09
leaq 0x30(%rsp), %rdi
xorl %eax, %eax
callq 0x3250
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x65ad
movq %r12, 0x10(%rsp)
movb %bpl, 0xf(%rsp)
movaps 0x90(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
jmp 0x65c6
xorl %ebp, %ebp
movq 0x18(%rsp), %r14
leaq 0x10(%rsp), %rsi
leaq 0xf(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x5be3
leaq 0x786d(%rip), %rsi      # 0xde3a
movq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
movq 0x70(%rsp), %rbx
movq 0x48(%rsp), %r12
je 0x661b
leaq 0x7859(%rip), %rsi      # 0xde48
movq %r15, %rdi
callq 0x3110
testl %eax, %eax
jne 0x6638
movq 0x48(%r14), %rdi
movq 0x50(%rsp), %rax
movl (%rax), %ecx
leaq 0x6ee9(%rip), %rdx      # 0xd4f6
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x30d0
jmp 0x6638
movq 0x48(%r14), %rdi
movq (%rbx), %rax
movq (%rax), %rcx
leaq 0x7817(%rip), %rdx      # 0xde43
movl $0x100, %esi            # imm = 0x100
xorl %eax, %eax
callq 0x30d0
cmpb $0x0, 0x6c(%rsp)
je 0x6654
movl 0x28(%rsp), %r8d
movq 0x8(%r14), %rax
movl 0x30(%rax), %ecx
cmpl %ecx, %r8d
jne 0x670f
testl %ebp, %ebp
jne 0x672b
movq 0x10(%rsp), %rax
movq (%rbx), %rcx
movq %rax, 0x18(%rcx)
movb 0xf(%rsp), %al
movb %al, 0x20(%rcx)
movaps 0x30(%rsp), %xmm0
movups %xmm0, 0x28(%rcx)
movq 0x40(%rsp), %rax
movq %rax, 0x38(%rcx)
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movl $0x1, %edx
movq %r12, %rcx
callq 0x5a85
movq (%rbx), %rbx
movq 0x50(%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)
cmpl $0x0, 0x28(%rsp)
jle 0x677a
addq $0x40, %rbx
xorl %r14d, %r14d
movq 0x20(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0xc922
incq %r14
movslq 0x28(%rsp), %rax
cmpq %rax, %r14
jl 0x66ef
jmp 0x6754
cmpl $0x0, 0x6c(%rbx)
jne 0x672b
leaq 0x7735(%rip), %rsi      # 0xde51
movq 0x50(%rsp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3250
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, 0x28(%rsp)
jle 0x6754
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x3030
incq %rbx
movslq 0x28(%rsp), %rax
cmpq %rax, %rbx
jl 0x6739
leaq 0x20(%rsp), %rdi
callq 0xc8d9
movq %r15, %rdi
callq 0x3030
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x6754