// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/crossassemblers/macro1/macro1.c
// Function: defineMacro
void
defineMacro() {
    int lexstartsave;			/* point to macro name */
    int index;				/* point to error char */
    int error;				/* error boolean */
    int i;
    int count;
    WORD32  length;
    WORD32  value;
    char    termin[SYMLEN];
    char    args[MAC_MAX_ARGS][SYMLEN];	/* macro & arg names */
    char    body[MAC_MAX_LENGTH + 1];
    struct macdef *mdp;
    SYM_T *sym;

    if (nrepeats) {
	/* we can call readLine, so throw up hands now */
	errorLexeme( &define_in_repeat, lexstartprev );
	return;
    }

    while (line[lexstart] == ' ' || line[lexstart] == '\t')
	next(0);

    /* not a tab or space */
    if (ISEND(line[lexstart])) {	/* newline or EOS? */
	/* crock; next token should invisibly skip over line boundaries? */
	readLine();
	next(0);
	while (line[lexstart] == ' ' || line[lexstart] == '\t')
	    next(0);
    }

    /* XXX pick up macro name out here */

    count = 0;
    index = 0;
    error = FALSE;
    lexstartsave = lexstart;
    while (!ISDONE(line[lexstart]) && count < MAC_MAX_ARGS) {
	if (!isalnum(line[lexstart]) && index == 0)
	    index = lexstart;		/* error pointer */
	lexemeToName( args[count++], lexstart, lexterm );
	/* XXX error if NOT a comma (& not first dummy) ? */
	if (line[lexterm] == ',')
	    next(0);			/* eat the comma */
	next(0);
	if (line[lexstart] == ' ')
	    next(0);
    }
    if( count == 0 ) {			/* No macro name. */
	errorMessage( &no_macro_name, lexstartsave );
	error = TRUE;
    }
    else if( index ) {			/* Bad argument name. */
	errorMessage( &bad_dummy_arg, index );
	error = TRUE;
    }
    else if( mac_count >= MAC_TABLE_LENGTH ) {
	errorMessage( &macro_table_full, lexstartsave );
	error = TRUE;
    }
    else {
	value = mac_count++;		/* sym value is index into mac */
	defineSymbol( args[0], value, MACRO, lexstartsave );
    }

    for( length = 0;; ) {
	readLine();
	if (end_of_input)
	    break;
	next(0);
	while (line[lexstart] == ' ' || line[lexstart] == '\t')
	    next(0);

	lexemeToName( termin, lexstart, lexterm ); /* just look at line? */
	if (strncmp( termin, "term", 4 ) == 0)
	    break;

	if (!error) {
	    int ll = strlen(line);
	    int allblank = FALSE;

	    /* don't save blank lines! */
	    for( i = 0; i < ll && allblank; i++ )
		if(!ISBLANK(line[i]))
		    allblank = FALSE;

	    if (allblank)			/* nothing but air? */
		continue;			/* skip it! */

	    if ((length + ll + 1) >= MAC_MAX_LENGTH ) {
		errorMessage (&macro_too_long, lexstart );
		error = TRUE;
		continue;
	    }

	    strcpy(body+length, line);
	    length += ll;
	}
    } /* for */
    if( error )
	return;

    mdp = calloc(1, sizeof(struct macdef) + length);
    if (mdp == NULL) {
	fprintf(stderr, "error allocating memory for macro definition\n");
	exit(1);
    }
    mac_defs[value] = mdp;

    strncpy(mdp->body, body, length);
    mdp->body[length] = '\0';
    mdp->nargs = count - 1;

    /*
     * save dummy names
     * symbol slot 0 reserved for "r" symbol
     * move SYM_T entries to macinv to allow recursion
     */
    sym = mdp->args;
    sym->type = DEFINED;
    strcpy(sym->name, "R");
    sym->val = 0;
    sym->xref_index = -1;		/* ??? allow xref? */
    sym++;

    for (i = 1; i <= mdp->nargs; i++, sym++) {
	sym->type = DEFINED;
	strcpy(sym->name, args[i]);
	sym->val = 0;
	sym->xref_index = -1;		/* don't xref!! */
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20b8, %rsp           # imm = 0x20B8
cmpl $0x0, 0x7c3a(%rip)      # 0xd3c4
je 0x57c4
movl 0x69b2(%rip), %ebx      # 0xc144
movl 0x6918(%rip), %esi      # 0xc0b0
movl 0x699e(%rip), %edx      # 0xc13c
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
callq 0x430e
leaq 0x449b(%rip), %rdi      # 0x9c50
movq %r14, %rsi
movl %ebx, %edx
callq 0x4353
jmp 0x59f1
leaq 0x6885(%rip), %rbx      # 0xc050
movslq 0x68de(%rip), %r15      # 0xc0b0
movzbl (%r15,%rbx), %eax
cmpl $0x9, %eax
jg 0x57e0
je 0x57e5
jmp 0x57ee
cmpl $0x20, %eax
jne 0x57f4
xorl %edi, %edi
callq 0x3911
jmp 0x57cb
testl %eax, %eax
je 0x57f9
jmp 0x581b
cmpl $0xa, %eax
jne 0x581b
callq 0x48f1
xorl %edi, %edi
callq 0x3911
movslq 0x68a4(%rip), %r15      # 0xc0b0
movzbl (%r15,%rbx), %eax
cmpl $0x9, %eax
je 0x57fe
cmpl $0x20, %eax
je 0x57fe
xorl %ecx, %ecx
leaq 0x20(%rsp), %r14
movl %r15d, %r12d
xorl %ebp, %ebp
movslq %r12d, %rax
movzbl (%rax,%rbx), %r13d
cmpq $0x2f, %r13
movq %rcx, 0x10(%rsp)
ja 0x584e
movabsq $0x800000000401, %rax   # imm = 0x800000000401
btq %r13, %rax
jb 0x58d0
cmpb $0x9, %r13b
je 0x58d0
cmpq $0x13, %rcx
ja 0x58d0
callq 0x21b0
movl %r12d, %ecx
testl %ebp, %ebp
je 0x5868
movl %ebp, %ecx
movsbq %r13b, %rdx
movq (%rax), %rax
movzwl (%rax,%rdx,2), %eax
testb $0x8, %al
cmovel %ecx, %ebp
movslq 0x68bd(%rip), %r13      # 0xc13c
movq %r14, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x430e
cmpb $0x2c, (%r13,%rbx)
jne 0x589c
xorl %edi, %edi
callq 0x3911
xorl %edi, %edi
callq 0x3911
movslq 0x6806(%rip), %r12      # 0xc0b0
cmpb $0x20, (%r12,%rbx)
jne 0x58bf
xorl %edi, %edi
callq 0x3911
movl 0x67f1(%rip), %r12d     # 0xc0b0
addq $0x7, %r14
movq 0x10(%rsp), %rcx
incq %rcx
jmp 0x5827
testq %rcx, %rcx
je 0x58e4
testl %ebp, %ebp
je 0x58ed
leaq 0x4330(%rip), %rdi      # 0x9c10
movl %ebp, %esi
jmp 0x590b
leaq 0x4315(%rip), %rdi      # 0x9c00
jmp 0x5908
movslq 0x6860(%rip), %rsi      # 0xc154
cmpq $0x400, %rsi            # imm = 0x400
jl 0x5ac6
leaq 0x4338(%rip), %rdi      # 0x9c40
movl %r15d, %esi
callq 0x4b12
movl $0x1, %r12d
movq $0x0, 0x8(%rsp)
callq 0x48f1
xorl %r14d, %r14d
cmpb $0x0, 0x6802(%rip)      # 0xc130
je 0x5a03
testl %r12d, %r12d
jne 0x59f1
movslq %r14d, %r15
leaq 0x200(%r15), %rsi
movl $0x1, %edi
callq 0x20e0
testq %rax, %rax
je 0x5aee
movq %rax, %rbx
leaq 0x46ec(%rip), %rax      # 0xa050
movq 0x8(%rsp), %rcx
movq %rbx, (%rax,%rcx,8)
leaq 0x1fc(%rbx), %rdi
leaq 0xb0(%rsp), %rsi
movq %r15, %rdx
callq 0x2040
movb $0x0, 0x1fc(%rbx,%r15)
movq 0x10(%rsp), %rcx
leal -0x1(%rcx), %eax
movl %eax, (%rbx)
movl $0x1, 0x4(%rbx)
movw $0x52, 0x8(%rbx)
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
movq %r12, 0x10(%rbx)
cmpq $0x2, %rcx
jb 0x59f1
leaq 0x27(%rsp), %r14
movq %rbx, %r15
addq $0x20, %r15
xorl %r13d, %r13d
movl $0x1, -0x4(%r15)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2070
movq %r12, 0x8(%r15)
movslq (%rbx), %rax
incq %r13
addq $0x7, %r14
addq $0x18, %r15
cmpq %rax, %r13
jl 0x59c7
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
leaq 0x19(%rsp), %r15
xorl %edi, %edi
callq 0x3911
movslq 0x6697(%rip), %r13      # 0xc0b0
movzbl (%r13,%rbx), %eax
cmpl $0x9, %eax
je 0x5a0b
cmpl $0x20, %eax
je 0x5a0b
movl 0x670d(%rip), %edx      # 0xc13c
movq %r15, %rdi
movl %r13d, %esi
callq 0x430e
cmpl $0x6d726574, 0x19(%rsp) # imm = 0x6D726574
je 0x5934
testl %r12d, %r12d
jne 0x5a8b
movq %rbx, %rdi
callq 0x2090
movq %rax, %rbp
addl %r14d, %ebp
cmpl $0x1ffe, %ebp           # imm = 0x1FFE
jle 0x5a93
leaq 0x41b6(%rip), %rdi      # 0x9c20
movl %r13d, %esi
callq 0x4b12
callq 0x48f1
movl $0x1, %r12d
cmpb $0x0, 0x66ac(%rip)      # 0xc130
je 0x5a0b
jmp 0x59f1
movl $0x1, %r12d
jmp 0x5aaf
movslq %r14d, %rax
leaq (%rsp,%rax), %rdi
addq $0xb0, %rdi
movq %rbx, %rsi
callq 0x2070
xorl %r12d, %r12d
movl %ebp, %r14d
callq 0x48f1
cmpb $0x0, 0x6675(%rip)      # 0xc130
je 0x5a0b
jmp 0x5934
leal 0x1(%rsi), %eax
movl %eax, 0x6685(%rip)      # 0xc154
leaq 0x20(%rsp), %rdi
movq %rsi, 0x8(%rsp)
movl $0x101, %edx            # imm = 0x101
movl %r15d, %ecx
callq 0x292b
xorl %r12d, %r12d
jmp 0x591f
callq 0x2471