// File: /workspace/github_c_makefile_star2_O3/Yamsman[P]scc/src/ppd.c
// Function: ppd_define
void ppd_define(lexer *lx) {
	s_pos *loc = &lx->tgt->loc;

	//Read macro name
	token mname;
	lex_wspace(lx);
	lex_ident(lx, &mname);
	int mlen = strlen(mname.dat.sval);
	if (mlen == 0)
		c_error(loc, "Invalid macro name\n");

	//Check for and read macro parameter list
	s_type *mtype = type_new(TYPE_MACRO);
	if (lex_cur(lx) == '(')
		ppd_defparams(lx, mtype);

	//Check for whitespace
	int wspace = 0;
	char cur = lex_cur(lx);
	while (isspace(cur) && cur != '\n') {
		wspace = 1;
		cur = lex_advc(lx);
	}
	if (!wspace && cur != '\n')
		c_error(loc, "No whitespace after macro name\n");

	//Copy macro body to buffer
	int len = 0;
	int bsize = 256;
	char *buf = malloc(bsize);
	while (cur != '\n') {
		//Resize if needed
		if (len+2 == bsize) {
			bsize *= 2;
			buf = realloc(buf, bsize);
		}

		//Copy character to buffer
		buf[len++] = cur;
		cur = lex_advc(lx);
	}
	buf[len] = '\0';

	//Check for ## on edges of macro
	if (len >= 2) {
		if (buf[0] == '#' && buf[1] == '#' ||
		    buf[len-2] == '#' && buf[len-1] == '#') {
			c_error(loc, "'##' at beginning or end of macro\n");
			free(mname.dat.sval);
			free(buf);
			return;
		}
	}

	//Add to symtable
	symbol *sym = symtable_def(lx->stb, mname.dat.sval, mtype, &mname.loc);
	sym->mac_exp = buf;
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq (%rdi), %r14
addq $0x28, %r14
callq 0x7825
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x67f7
movq 0x20(%r15), %rdi
callq 0x30b0
testl %eax, %eax
jne 0xb3cd
leaq 0x3a5b(%rip), %rsi      # 0xee1e
movq %r14, %rdi
xorl %eax, %eax
callq 0x3250
movl $0x1, %edi
callq 0xbf35
movq %rax, %r13
movq %rbx, %rdi
callq 0x677e
cmpb $0x28, %al
jne 0xb3f1
movq %rbx, %rdi
movq %r13, %rsi
callq 0xb294
movq %rbx, %rdi
callq 0x677e
movl %eax, %ebp
callq 0x3230
movq %rax, %r12
movq (%rax), %rax
movsbq %bpl, %rcx
movb 0x1(%rax,%rcx,2), %r15b
andb $0x20, %r15b
shrb $0x5, %r15b
cmpb $0xa, %cl
setne %al
andb %al, %r15b
cmpb $0x1, %r15b
jne 0xb44b
movq %rbx, %rdi
callq 0x6798
movl %eax, %ebp
cmpb $0xa, %al
setne %al
je 0xb44b
movq (%r12), %rcx
movsbq %bpl, %rdx
movzwl (%rcx,%rdx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0xb426
testb %al, %al
je 0xb466
xorb $0x1, %r15b
je 0xb466
leaq 0x39d6(%rip), %rsi      # 0xee32
movq %r14, %rdi
xorl %eax, %eax
callq 0x3250
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %r12
cmpb $0xa, %bpl
jne 0xb483
movb $0x0, (%r12)
jmp 0xb520
movq %r13, (%rsp)
movl $0x100, %r15d           # imm = 0x100
xorl %r13d, %r13d
leaq 0x2(%r13), %rax
movl %r15d, %ecx
cmpq %rcx, %rax
jne 0xb4ad
addl %r15d, %r15d
movslq %r15d, %rsi
movq %r12, %rdi
callq 0x31a0
movq %rax, %r12
movb %bpl, (%r12,%r13)
incq %r13
movq %rbx, %rdi
callq 0x6798
movl %eax, %ebp
cmpb $0xa, %al
jne 0xb490
movl %r13d, %eax
movb $0x0, (%r12,%rax)
cmpq $0x1, %r13
je 0xb51c
cmpb $0x23, (%r12)
jne 0xb4df
cmpb $0x23, 0x1(%r12)
je 0xb4f7
leal -0x2(%r13), %eax
cmpb $0x23, (%r12,%rax)
jne 0xb51c
decq %r13
movl %r13d, %eax
cmpb $0x23, (%r12,%rax)
jne 0xb51c
leaq 0x3954(%rip), %rsi      # 0xee52
movq %r14, %rdi
xorl %eax, %eax
callq 0x3250
movq 0x28(%rsp), %rdi
callq 0x3030
movq %r12, %rdi
callq 0x3030
jmp 0xb539
movq (%rsp), %r13
movq 0x60(%rbx), %rdi
leaq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rsi
movq %r13, %rdx
callq 0xbe8d
movq %r12, 0x48(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq