// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq (%rdi), %rbx
addq $0x28, %rbx
callq 0x78a3
leaq 0x8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x67ce
movq 0x20(%r15), %rdi
callq 0x30b0
testl %eax, %eax
jne 0xb45a
leaq 0x39ce(%rip), %rsi      # 0xee1e
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3250
movl $0x1, %edi
callq 0xbfcd
movq %rax, %r15
movq %r14, %rdi
callq 0x6759
cmpb $0x28, %al
jne 0xb47e
movq %r14, %rdi
movq %r15, %rsi
callq 0xb32c
movq %r15, (%rsp)
movq %r14, %rdi
callq 0x6759
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 0xb4dc
movq %r14, %rdi
callq 0x6776
movl %eax, %ebp
cmpb $0xa, %al
setne %al
je 0xb4dc
movq (%r12), %rcx
movsbq %bpl, %rdx
movzwl (%rcx,%rdx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0xb4b7
testb %al, %al
je 0xb4f7
xorb $0x1, %r15b
je 0xb4f7
leaq 0x3945(%rip), %rsi      # 0xee32
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3250
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %r12
xorl %r13d, %r13d
cmpb $0xa, %bpl
je 0xb548
movl $0x100, %r15d           # imm = 0x100
xorl %r13d, %r13d
leaq 0x2(%r13), %rax
movl %r15d, %ecx
cmpq %rcx, %rax
jne 0xb533
addl %r15d, %r15d
movslq %r15d, %rsi
movq %r12, %rdi
callq 0x31a0
movq %rax, %r12
movb %bpl, (%r12,%r13)
incq %r13
movq %r14, %rdi
callq 0x6776
movl %eax, %ebp
cmpb $0xa, %al
jne 0xb516
movl %r13d, %eax
movb $0x0, (%r12,%rax)
cmpl $0x2, %r13d
jb 0xb59f
cmpb $0x23, (%r12)
jne 0xb565
cmpb $0x23, 0x1(%r12)
je 0xb57a
leal -0x2(%r13), %eax
cmpb $0x23, (%r12,%rax)
jne 0xb59f
decl %r13d
cmpb $0x23, (%r12,%r13)
jne 0xb59f
leaq 0x38d1(%rip), %rsi      # 0xee52
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3250
movq 0x28(%rsp), %rdi
callq 0x3030
movq %r12, %rdi
callq 0x3030
jmp 0xb5b9
movq 0x60(%r14), %rdi
leaq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rsi
movq (%rsp), %rdx
callq 0xbf25
movq %r12, 0x48(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq