// File: /workspace/github_c_makefile_star2_O1/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, 0x7c9e(%rip)      # 0xd3c4
je 0x5760
movl 0x6a16(%rip), %ebx      # 0xc144
movl 0x697c(%rip), %esi      # 0xc0b0
movl 0x6a02(%rip), %edx      # 0xc13c
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
callq 0x42ae
leaq 0x44ff(%rip), %rdi      # 0x9c50
movq %r14, %rsi
movl %ebx, %edx
callq 0x42f3
jmp 0x5a2f
leaq 0x68e9(%rip), %rbx      # 0xc050
movslq 0x6942(%rip), %rax      # 0xc0b0
movzbl (%rax,%rbx), %eax
cmpl $0x9, %eax
jg 0x577b
je 0x5780
jmp 0x5789
cmpl $0x20, %eax
jne 0x578f
xorl %edi, %edi
callq 0x384a
jmp 0x5767
testl %eax, %eax
je 0x5794
jmp 0x57b5
cmpl $0xa, %eax
jne 0x57b5
callq 0x4883
xorl %edi, %edi
callq 0x384a
movslq 0x6909(%rip), %rax      # 0xc0b0
movzbl (%rax,%rbx), %eax
cmpl $0x9, %eax
je 0x5799
cmpl $0x20, %eax
je 0x5799
xorl %ebp, %ebp
leaq 0x20(%rsp), %r15
movl 0x68ee(%rip), %eax      # 0xc0b0
movl %eax, 0x8(%rsp)
xorl %r14d, %r14d
movslq 0x68e0(%rip), %r12      # 0xc0b0
movzbl (%r12,%rbx), %r13d
cmpq $0x2f, %r13
ja 0x57eb
movabsq $0x800000000401, %rax   # imm = 0x800000000401
btq %r13, %rax
jb 0x5863
cmpb $0x9, %r13b
je 0x5863
cmpq $0x13, %rbp
ja 0x5863
callq 0x21b0
movl %r12d, %ecx
testl %r14d, %r14d
je 0x5807
movl %r14d, %ecx
movsbq %r13b, %rdx
movq (%rax), %rax
movzwl (%rax,%rdx,2), %eax
testb $0x8, %al
cmovel %ecx, %r14d
movslq 0x691d(%rip), %r13      # 0xc13c
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x42ae
cmpb $0x2c, (%r13,%rbx)
jne 0x583c
xorl %edi, %edi
callq 0x384a
xorl %edi, %edi
callq 0x384a
movslq 0x6866(%rip), %rax      # 0xc0b0
cmpb $0x20, (%rax,%rbx)
jne 0x5857
xorl %edi, %edi
callq 0x384a
addq $0x7, %r15
incq %rbp
jmp 0x57c9
testq %rbp, %rbp
movq %rbp, 0x18(%rsp)
je 0x587e
testl %r14d, %r14d
je 0x5887
leaq 0x4397(%rip), %rdi      # 0x9c10
movl %r14d, %esi
jmp 0x58a6
leaq 0x437b(%rip), %rdi      # 0x9c00
jmp 0x58a2
movslq 0x68c6(%rip), %r14      # 0xc154
cmpq $0x400, %r14            # imm = 0x400
jl 0x5a41
leaq 0x439e(%rip), %rdi      # 0x9c40
movl 0x8(%rsp), %esi
callq 0x4ab6
movl $0x1, %r15d
xorl %r14d, %r14d
callq 0x4883
xorl %r13d, %r13d
cmpb $0x0, 0x686d(%rip)      # 0xc130
jne 0x597d
movq %r14, 0x8(%rsp)
leaq 0x11(%rsp), %r12
xorl %edi, %edi
callq 0x384a
movslq 0x67cf(%rip), %rbp      # 0xc0b0
movzbl (%rbp,%rbx), %eax
cmpl $0x9, %eax
je 0x58d3
cmpl $0x20, %eax
je 0x58d3
movl 0x6846(%rip), %edx      # 0xc13c
movq %r12, %rdi
movl %ebp, %esi
callq 0x42ae
cmpl $0x6d726574, 0x11(%rsp) # imm = 0x6D726574
je 0x596f
movl $0x1, %r14d
testl %r15d, %r15d
jne 0x5958
movq %rbx, %rdi
callq 0x2090
movq %rax, %r15
addl %r13d, %r15d
cmpl $0x1fff, %r15d          # imm = 0x1FFF
jge 0x594a
movslq %r13d, %rax
leaq (%rsp,%rax), %rdi
addq $0xb0, %rdi
movq %rbx, %rsi
callq 0x2070
xorl %r14d, %r14d
movl %r15d, %r13d
jmp 0x5958
leaq 0x42cf(%rip), %rdi      # 0x9c20
movl %ebp, %esi
callq 0x4ab6
callq 0x4883
cmpb $0x0, 0x67cc(%rip)      # 0xc130
movl %r14d, %r15d
je 0x58d3
jmp 0x5972
movl %r15d, %r14d
movslq %r13d, %r13
movl %r14d, %r15d
movq 0x8(%rsp), %r14
testl %r15d, %r15d
jne 0x5a2f
leaq 0x200(%r13), %rsi
movl $0x1, %edi
callq 0x20e0
testq %rax, %rax
je 0x5a69
movq %rax, %rbx
leaq 0x46a6(%rip), %rax      # 0xa050
movq %rbx, (%rax,%r14,8)
leaq 0x1fc(%rbx), %rdi
leaq 0xb0(%rsp), %rsi
movq %r13, %rdx
callq 0x2040
movb $0x0, 0x1fc(%rbx,%r13)
movq 0x18(%rsp), %rax
decl %eax
movl %eax, (%rbx)
movl $0x1, 0x4(%rbx)
movw $0x52, 0x8(%rbx)
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
movq %r12, 0x10(%rbx)
testl %eax, %eax
jle 0x5a2f
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 0x5a05
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal 0x1(%r14), %eax
movl %eax, 0x6709(%rip)      # 0xc154
leaq 0x20(%rsp), %rdi
movl %r14d, %esi
movl $0x101, %edx            # imm = 0x101
movl 0x8(%rsp), %ecx
callq 0x292b
xorl %r15d, %r15d
jmp 0x58b4
callq 0x2471