Skip to content

What A Fight – More Compiler Combo Rules

March 21, 2018

I spent a few pleasant hours today working on the python liveness analysis proram but i kept coming back to the fact that what i wanted to fix was extra register shuffling and that there might be an easier fix.  For example the following code generates an unnecessary register copy.

	Ccall _spiRec; CALLI2(ar)
	cpy2 R11,R15 ;LOADU2*(reg)
	str1 R11,R7; ASGNU1(indaddr,reg)		DH*

I looked at the symbolic output for it but i couldn’t really relate to the CVIU2 and CVUU1 which don’t seem to be used in my target.

;		*where++=spiRec();
 2. ADDRFP2 count=2 where
1' INDIRP2 count=2 #2
5. CNSTI2 1
4. ADDP2 #1 #5
3' ASGNP2 #2 #4 2 1
7. ADDRGP2 spiRec
6' CALLI2 #7 {int function}
10. CVIU2 #6 2
9. CVUU1 #10 2
8' ASGNU1 #1 #9 1 1

I finally hacked into the function emitasm(Node p,int nt) in module gen.c and printed the op code and rules as they were used with the following butchery(now commented out)

unsigned emitasm(Node p, int nt) {
	int rulenum;
	short *nts;
	char *fmt;
	Node kids[10];
	//printf("\nHERE WE GO %d\n",p->op);
	p = reuse(p, nt);
	rulenum = getrule(p, nt);
	nts = IR->x._nts[rulenum];
	fmt = IR->x._templates[rulenum];
	//printf("\nHERE WE ARE %s\n",fmt);

I looked at the mess that came out and picked out the op codes 2278(LOADU2),2117(INDIRI2),1078(ASGNU1),1254(LOADU1),2119(INDIRP2). The only one that wasn’t already in my rules was LOADU1 so I finally tried

stmt: ASGNU1(indaddr,LOADU1(LOADU2(reg)))  "\tstr1 R%1,%0; ASGNU1(indaddr,LOADU1(LOADU2(reg))) 18-03-21\n"  1
which generated:
;		*where=spiRec();
	Ccall _spiRec; CALLI2(ar)
	str1 R15,R7; ASGNU1(indaddr,LOADU1(LOADU2(reg))) 18-03-21

So yay, but what a fight and i’m only a tiny bit smarter in the end. I think i’ve been down this road before trying to get the symbolic target to use the same terminals as mine but i’m obviously not there yet.

This, apropos of optimization is a set of peephole optimization rules for the SDCC Z80 compiler.

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: