; Dude's handy MIPS macros

mon	macro
	pusho
	opt	m+,at+
	endm

moff	macros
	popo

stack	macro
	if	narg=0
	rsreset
	else
	if	strcmp('\0','w')
_fs	=	4*\1
	elseif	strcmp('\0','h')
_fs	=	2*\1
	else
_fs	=	\1
	endif
	subiu	sp,_fs
	endif
	endm

endstack macro
_fs	=	__rs
	subiu	sp,_fs
	endm

unstack	macros
	addiu	sp,_fs

parms	macros
	rsset	$10

parm	macros
	\0	\_+_fs(sp)

gpr	macros
	\0	\1,\2-GPBASE(gp)

pcr	macro
	if	narg=2
	\0	\1,((\2)-(*+4))(ra)
	else
	\0	\1,((\2)-(\3))(ra)
	endif
	endm

gpla	macros
	addiu	\1,gp,\2-GP

pcla	macro
	if	narg=2
	addiu	\1,ra,(\2)-(*+4)
	else
	addiu	\1,ra,(\2)-(\3)
	endif
	endm

clr	macros
	addu	\1,r0,r0

movei	macros
	addiu	\1,r0,\2

bt	macro
	if	narg=1
	bne	at,r0,\1
	else
	beq	\1,r0,\2
	endif
	endm

bf	macro
	if	narg=1
	beq	at,r0,\1
	else
	beq	\1,r0,\2
	endif
	endm

blo	macros
	bne	at,r0,\1

bhs	macros
	beq	at,r0,\1

bnz	macro
	if	narg=1
	bne	at,r0,\1
	else
	bne	\1,r0,\2
	endif
	endm

bz	macro
	if 	narg=1
	beq	at,r0,\1
	else
	beq	\1,r0,\2
	endif
	endm

bpl	macro
	if 	narg=1
	bgez	at,\1
	else
	bgez	\1,\2
	endif
	endm

bmi	macro
	if 	narg=1
	bltz	at,\1
	else
	bltz	\1,\2
	endif
	endm

negate	macro
	if	narg=1
	subu	\1,r0,\1
	else
	subu	\1,r0,\2
	endif
	endm

invert	macro
	if	narg=1
	nor	\1,r0,\1
	else
	nor	\1,r0,\2
	endif
	endm

testi	macros
	andi	at,\1,\2

inc	macro
	if	strcmp('\0','w')
	addiu	\1,4
	elseif	strcmp('\0','h')
	addiu	\1,2
	else
	addiu	\1,1
	endif
	endm

dec	macro
	if	strcmp('\0','w')
	addiu	\1,-4
	elseif	strcmp('\0','h')
	addiu	\1,-2
	else
	addiu	\1,-1
	endif
	endm

jsr	macros
	jal	\1

jmp	macros
	j	\1

ret	macros
	jr	ra

subi	macro
	if	narg=1
	addi	\1,\1,-\2
	else
	addi	\1,\2,-\3
	endif
	endm

subiu	macro
	if	narg=1
	addiu	\1,\1,-\2
	else
	addiu	\1,\2,-\3
	endif
	endm

cmp	macros
	slt	at,\1,\2

cmpu	macros
	sltu	at,\1,\2

cmpi	macros
	slti	at,\1,\2

cmpiu	macros
	sltiu	at,\1,\2

mac	macro
	pusho
	opt	m+,at+
	\0	\_
	popo
	endm
