// File: /workspace/github_c_makefile_star2_O2/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, 0x82a8(%rip)      # 0xd3c4
je 0x5141
movl 0x7020(%rip), %esi      # 0xc144
leaq 0x4b25(%rip), %rdi      # 0x9c50
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3f76
leaq 0x6f08(%rip), %rbx      # 0xc050
movslq 0x6f61(%rip), %r14      # 0xc0b0
movzbl (%r14,%rbx), %eax
cmpl $0x9, %eax
je 0x515e
cmpl $0x20, %eax
jne 0x5167
xorl %edi, %edi
callq 0x3575
jmp 0x5148
testl %eax, %eax
je 0x5170
cmpl $0xa, %eax
jne 0x5192
callq 0x4379
xorl %edi, %edi
callq 0x3575
movslq 0x6f2d(%rip), %r14      # 0xc0b0
movzbl (%r14,%rbx), %eax
cmpl $0x9, %eax
je 0x5175
cmpl $0x20, %eax
je 0x5175
xorl %ecx, %ecx
leaq 0x20(%rsp), %r15
movl %r14d, %r12d
xorl %ebp, %ebp
movslq %r12d, %rax
movzbl (%rax,%rbx), %r13d
cmpq $0x2f, %r13
movq %rcx, 0x10(%rsp)
ja 0x51c5
movabsq $0x800000000401, %rax   # imm = 0x800000000401
btq %r13, %rax
jb 0x5247
cmpb $0x9, %r13b
je 0x5247
cmpq $0x13, %rcx
ja 0x5247
callq 0x21b0
movl %r12d, %ecx
testl %ebp, %ebp
je 0x51df
movl %ebp, %ecx
movsbq %r13b, %rdx
movq (%rax), %rax
movzwl (%rax,%rdx,2), %eax
testb $0x8, %al
cmovel %ecx, %ebp
movslq 0x6f46(%rip), %r13      # 0xc13c
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x3e5d
cmpb $0x2c, (%r13,%rbx)
jne 0x5213
xorl %edi, %edi
callq 0x3575
xorl %edi, %edi
callq 0x3575
movslq 0x6e8f(%rip), %r12      # 0xc0b0
cmpb $0x20, (%r12,%rbx)
jne 0x5236
xorl %edi, %edi
callq 0x3575
movl 0x6e7a(%rip), %r12d     # 0xc0b0
addq $0x7, %r15
movq 0x10(%rsp), %rcx
incq %rcx
jmp 0x519e
testq %rcx, %rcx
je 0x525b
testl %ebp, %ebp
je 0x5264
leaq 0x49b9(%rip), %rdi      # 0x9c10
movl %ebp, %esi
jmp 0x5282
leaq 0x499e(%rip), %rdi      # 0x9c00
jmp 0x527f
movslq 0x6ee9(%rip), %rsi      # 0xc154
cmpq $0x400, %rsi            # imm = 0x400
jl 0x53ff
leaq 0x49c1(%rip), %rdi      # 0x9c40
movl %r14d, %esi
callq 0x45ba
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
callq 0x4379
cmpb $0x0, 0x6e8e(%rip)      # 0xc130
movslq %r12d, %r13
jne 0x5332
xorl %edi, %edi
callq 0x3575
movslq 0x6df7(%rip), %rbp      # 0xc0b0
movzbl (%rbp,%rbx), %eax
cmpl $0x9, %eax
je 0x52ab
cmpl $0x20, %eax
je 0x52ab
movl 0x6e6e(%rip), %edx      # 0xc13c
leaq 0x19(%rsp), %rdi
movl %ebp, %esi
callq 0x3e5d
cmpl $0x6d726574, 0x19(%rsp) # imm = 0x6D726574
je 0x5332
testb $0x1, %r14b
je 0x530f
movq %rbx, %rdi
callq 0x2090
movq %rax, %r15
addl %r12d, %r15d
cmpl $0x1ffe, %r15d          # imm = 0x1FFE
jle 0x5314
leaq 0x4918(%rip), %rdi      # 0x9c20
movl %ebp, %esi
callq 0x45ba
xorl %r14d, %r14d
jmp 0x5296
leaq (%rsp,%r13), %rdi
addq $0xb0, %rdi
movq %rbx, %rsi
callq 0x2070
movb $0x1, %r14b
movl %r15d, %r12d
jmp 0x5296
testb $0x1, %r14b
je 0x53ed
leaq 0x200(%r13), %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
je 0x5427
movq %rax, %r14
leaq 0x4cef(%rip), %rax      # 0xa050
movq 0x8(%rsp), %rcx
movq %r14, (%rax,%rcx,8)
leaq 0x1fc(%r14), %rdi
leaq 0xb0(%rsp), %rsi
movq %r13, %rdx
callq 0x2040
movb $0x0, 0x1fc(%r14,%r13)
movq 0x10(%rsp), %rax
decl %eax
movl %eax, (%r14)
movl $0x1, 0x4(%r14)
movw $0x52, 0x8(%r14)
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
movq %r13, 0x10(%r14)
leaq 0x27(%rsp), %r15
movq %r14, %r12
addq $0x20, %r12
cltq
cmpq %rax, %rbx
jg 0x53ed
movl $0x1, -0x4(%r12)
movq %r12, %rdi
movq %r15, %rsi
callq 0x2070
movq %r13, 0x8(%r12)
incq %rbx
movl (%r14), %eax
addq $0x7, %r15
addq $0x18, %r12
jmp 0x53bd
addq $0x20b8, %rsp           # imm = 0x20B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal 0x1(%rsi), %eax
movl %eax, 0x6d4c(%rip)      # 0xc154
leaq 0x20(%rsp), %rdi
movq %rsi, 0x8(%rsp)
movl $0x101, %edx            # imm = 0x101
movl %r14d, %ecx
callq 0x2623
movb $0x1, %r14b
jmp 0x5293
movq 0x3bca(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x145b(%rip), %rdi      # 0x6893
pushq $0x2d
popq %rsi
pushq $0x1
popq %rdx
callq 0x2180
pushq $0x1
popq %rdi
callq 0x2170