MS/CW-style inline assembly for GCC

Andrew Pinski pinskia@physics.uc.edu
Fri May 7 02:35:00 GMT 2004


On May 6, 2004, at 22:14, Andrew Pinski wrote:

>
> On May 6, 2004, at 22:10, Stan Shebs wrote:
>> You're not the first person to suggest the separate tool idea! It
>> would have to know types and decls well enough to come up with the
>> right constraint letters. Take a look at MacMAME sources to see
>> some hairy usage of CW inline asm; much of it is assembled from
>> combinations of macros.
>
> How can he as the source comes in .dmg format.  I am downloading it
> right now and I will post some examples.

Here is an example (which I think is stupid to use asm in this case as 
it
can be written using function pointers (or maybe libffi) and there is no
reason why this has to be in the source file except for inlining 
reasons,
but then again this example is a case where GCC would not get right
as the asm wants to allocate the stack frame it self.

static asm void execute_core(void)
{
#define rROM		r28
#define rICount		r29
#define rState		r30
#define rOpTable	r31

#if (__MWERKS__ >= 0x2300)
	nofralloc
#endif

	mflr	r0
	stmw	r28,-16(sp)
	stw		r0,8(sp)
	stwu	sp,-96(sp)
	
	_asm_get_global_ptr(rROM,OP_ROM)
	_asm_get_global_ptr(rICount,tms34010_ICount)
	_asm_get_global_ptr(rState,state)
	_asm_get_global_ptr(rOpTable,opcode_table)

MainLoop:
	lwz		r3,tms34010_regs.pc(rState)			// r3 = current PC
	lwz		r6,0(rROM)							// r6 = ROM base
	rlwinm	r5,r3,32-3,3,31						// r5 = PC >> 3
	lhzx	r7,r6,r5							// r7 = opcode
	addi	r4,r3,0x10							// r4 = PC + 10
	rlwinm	r8,r7,32-2,18,29					// r8 = (opcode >> 4) << 2
	lwzx	r9,rOpTable,r8						// r9 = opcode handler address
	stw		r4,tms34010_regs.pc(rState)			// save new PC
	mtctr	r9									// store to counter
	stw		r7,tms34010_regs.op(rState)			// save opcode
	bctrl
	
	lwz		r3,tms34010_regs.pc(rState)			// r3 = current PC
	lwz		r6,0(rROM)							// r6 = ROM base
	rlwinm	r5,r3,32-3,3,31						// r5 = PC >> 3
	lhzx	r7,r6,r5							// r7 = opcode
	addi	r4,r3,0x10							// r4 = PC + 10
	rlwinm	r8,r7,32-2,18,29					// r8 = (opcode >> 4) << 2
	lwzx	r9,rOpTable,r8						// r9 = opcode handler address
	stw		r4,tms34010_regs.pc(rState)			// save new PC
	mtctr	r9									// store to counter
	stw		r7,tms34010_regs.op(rState)			// save opcode
	bctrl
	
	lwz		r3,tms34010_regs.pc(rState)			// r3 = current PC
	lwz		r6,0(rROM)							// r6 = ROM base
	rlwinm	r5,r3,32-3,3,31						// r5 = PC >> 3
	lhzx	r7,r6,r5							// r7 = opcode
	addi	r4,r3,0x10							// r4 = PC + 10
	rlwinm	r8,r7,32-2,18,29					// r8 = (opcode >> 4) << 2
	lwzx	r9,rOpTable,r8						// r9 = opcode handler address
	stw		r4,tms34010_regs.pc(rState)			// save new PC
	mtctr	r9									// store to counter
	stw		r7,tms34010_regs.op(rState)			// save opcode
	bctrl
	
	lwz		r3,tms34010_regs.pc(rState)			// r3 = current PC
	lwz		r6,0(rROM)							// r6 = ROM base
	rlwinm	r5,r3,32-3,3,31						// r5 = PC >> 3
	lhzx	r7,r6,r5							// r7 = opcode
	addi	r4,r3,0x10							// r4 = PC + 10
	rlwinm	r8,r7,32-2,18,29					// r8 = (opcode >> 4) << 2
	lwzx	r9,rOpTable,r8						// r9 = opcode handler address
	stw		r4,tms34010_regs.pc(rState)			// save new PC
	mtctr	r9									// store to counter
	stw		r7,tms34010_regs.op(rState)			// save opcode
	bctrl
	
	lwz		r3,0(rICount)						// r3 = ICount
	cmpwi	r3,0								// done?
	bgt		MainLoop							// if not, loop
	
	lwz		r0,104(sp)
	addi	sp,sp,96
	mtlr	r0
	lmw		r28,-16(sp)
	blr
}


It looks like all the examples in MacMame really should be in
an asm file instead of a C file because they all allocate the
stackframe themselves.  They are just using the preprocessor
really which already can be done for assembly.

This is where the use of inline-asm is abused.

Thanks,
Andrew Pinski



More information about the Gcc mailing list