// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdi, %r14
movq (%rdi), %r13
addq $0x28, %r13
callq 0x7370
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x650a
movq 0x20(%r15), %rdi
callq 0x30b0
testl %eax, %eax
jne 0xabd5
leaq 0x3253(%rip), %rsi      # 0xde1e
movq %r13, %rdi
xorl %eax, %eax
callq 0x35e4
pushq $0x1
popq %rdi
callq 0xb5ed
movq %rax, %rbx
movq %r14, %rdi
callq 0x6493
cmpb $0x28, %al
jne 0xabf7
movq %r14, %rdi
movq %rbx, %rsi
callq 0xaaac
movq %rbx, 0x10(%rsp)
movq %r14, %rdi
callq 0x6493
movl %eax, %ebp
movb $0x1, %bl
callq 0x3230
movq %rax, %r12
cmpb $0xa, %bpl
je 0xac37
movq (%r12), %rax
movsbq %bpl, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
je 0xac37
movq %r14, %rdi
callq 0x64ad
movl %eax, %ebp
xorl %ebx, %ebx
jmp 0xac10
cmpb $0xa, %bpl
sete %al
notb %bl
orb %al, %bl
testb $0x1, %bl
jne 0xac58
leaq 0x31e4(%rip), %rsi      # 0xde32
movq %r13, %rdi
xorl %eax, %eax
callq 0x35e4
movq %r13, 0x8(%rsp)
movl $0x100, %edi            # imm = 0x100
callq 0x3170
movq %rax, %r12
movl $0x100, %r15d           # imm = 0x100
xorl %r13d, %r13d
xorl %ebx, %ebx
cmpb $0xa, %bpl
je 0xacae
leaq 0x2(%rbx), %rax
movl %r15d, %ecx
cmpq %rcx, %rax
jne 0xac98
addl %r15d, %r15d
movslq %r15d, %rsi
movq %r12, %rdi
callq 0x31a0
movq %rax, %r12
movb %bpl, (%r12,%rbx)
incq %rbx
movq %r14, %rdi
callq 0x64ad
movl %eax, %ebp
decq %r13
jmp 0xac75
movq %r13, %rax
negq %rax
movl %eax, %ecx
movb $0x0, (%r12,%rcx)
cmpq $0x2, %rax
jb 0xad17
cmpb $0x23, (%r12)
jne 0xacd0
cmpb $0x23, 0x1(%r12)
je 0xacf0
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
subl %r13d, %ecx
cmpb $0x23, (%r12,%rcx)
jne 0xad17
subl %r13d, %eax
incl %eax
cmpb $0x23, (%r12,%rax)
jne 0xad17
leaq 0x315b(%rip), %rsi      # 0xde52
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x35e4
movq 0x38(%rsp), %rdi
callq 0x3030
movq %r12, %rdi
callq 0x3030
jmp 0xad32
movq 0x60(%r14), %rdi
leaq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rsi
movq 0x10(%rsp), %rdx
callq 0xb545
movq %r12, 0x48(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq