// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq $0x28, %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x95f0
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x82b0
movq -0x20(%rbp), %rdi
callq 0x30b0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0xe868
movq -0x10(%rbp), %rdi
leaq 0x4af1(%rip), %rsi      # 0x13352
movb $0x0, %al
callq 0x3880
movl $0x1, %edi
callq 0xf720
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81b0
movsbl %al, %eax
cmpl $0x28, %eax
jne 0xe894
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xe6e0
movl $0x0, -0x54(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81b0
movb %al, -0x55(%rbp)
callq 0x3240
movq (%rax), %rax
movsbl -0x55(%rbp), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x71(%rbp)
je 0xe8d7
movsbl -0x55(%rbp), %eax
cmpl $0xa, %eax
setne %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0xe8e0
jmp 0xe8f5
movl $0x1, -0x54(%rbp)
movq -0x8(%rbp), %rdi
callq 0x81f0
movb %al, -0x55(%rbp)
jmp 0xe8a7
cmpl $0x0, -0x54(%rbp)
jne 0xe916
movsbl -0x55(%rbp), %eax
cmpl $0xa, %eax
je 0xe916
movq -0x10(%rbp), %rdi
leaq 0x4a57(%rip), %rsi      # 0x13366
movb $0x0, %al
callq 0x3880
movl $0x0, -0x5c(%rbp)
movl $0x100, -0x60(%rbp)     # imm = 0x100
movslq -0x60(%rbp), %rdi
callq 0x3190
movq %rax, -0x68(%rbp)
movsbl -0x55(%rbp), %eax
cmpl $0xa, %eax
je 0xe984
movl -0x5c(%rbp), %eax
addl $0x2, %eax
cmpl -0x60(%rbp), %eax
jne 0xe95e
movl -0x60(%rbp), %eax
shll %eax
movl %eax, -0x60(%rbp)
movq -0x68(%rbp), %rdi
movslq -0x60(%rbp), %rsi
callq 0x31c0
movq %rax, -0x68(%rbp)
movb -0x55(%rbp), %dl
movq -0x68(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x5c(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x8(%rbp), %rdi
callq 0x81f0
movb %al, -0x55(%rbp)
jmp 0xe931
movq -0x68(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
cmpl $0x2, -0x5c(%rbp)
jl 0xea03
movq -0x68(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0xe9af
movq -0x68(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x23, %eax
je 0xe9db
movq -0x68(%rbp), %rax
movl -0x5c(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x23, %eax
jne 0xea01
movq -0x68(%rbp), %rax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x23, %eax
jne 0xea01
movq -0x10(%rbp), %rdi
leaq 0x49a0(%rip), %rsi      # 0x13386
movb $0x0, %al
callq 0x3880
movq -0x20(%rbp), %rdi
callq 0x3030
movq -0x68(%rbp), %rdi
callq 0x3030
jmp 0xea30
jmp 0xea03
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq -0x40(%rbp), %rcx
addq $0x8, %rcx
callq 0xf5f0
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x48(%rax)
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)