require '_h2ph_pre.ph';

no warnings qw(redefine misc);

unless(defined(&_POWERPC_LOCK_H_)) {
    eval 'sub _POWERPC_LOCK_H_ () {1;}' unless defined(&_POWERPC_LOCK_H_);
    eval 'sub __SIMPLELOCK_LOCKED () {1;}' unless defined(&__SIMPLELOCK_LOCKED);
    eval 'sub __SIMPLELOCK_UNLOCKED () {0;}' unless defined(&__SIMPLELOCK_UNLOCKED);
    eval 'sub __cpu_simple_lock_init {
        my($alp) = @_;
	    eval q({ *$alp =  &__SIMPLELOCK_UNLOCKED;  &__asm  &volatile (\\"sync\\"); });
    }' unless defined(&__cpu_simple_lock_init);
    eval 'sub __cpu_simple_lock {
        my($alp) = @_;
	    eval q({ \'int\'  &old;  &__asm  &volatile (\\"					\\\\n1:	lwarx	%0,0,%1		\\\\n	cmpwi	%0,%2		\\\\n	beq+	3f		\\\\n2:	lwzx	%0,0,%1		\\\\n	cmpwi	%0,%2		\\\\n	beq+	1b		\\\\n	b	2b		\\\\n3:	stwcx.	%3,0,%1		\\\\n	bne-	1b		\\\\n	isync			\\\\n				\\\\n\\" : \\"=&r\\"( &old) : \\"r\\"($alp), \\"I\\"( &__SIMPLELOCK_UNLOCKED), \\"r\\"( &__SIMPLELOCK_LOCKED) : \\"memory\\"); });
    }' unless defined(&__cpu_simple_lock);
    eval 'sub __cpu_simple_lock_try {
        my($alp) = @_;
	    eval q({ \'int\'  &old,  &dummy;  &__asm  &volatile (\\"					\\\\n1:	lwarx	%0,0,%1		\\\\n	cmpwi	%0,%2		\\\\n	bne	2f		\\\\n	stwcx.	%3,0,%1		\\\\n	bne-	1b		\\\\n2:	stwcx.	%3,0,%4		\\\\n	isync			\\\\n				\\\\n\\" : \\"=&r\\"( &old) : \\"r\\"($alp), \\"I\\"( &__SIMPLELOCK_UNLOCKED), \\"r\\"( &__SIMPLELOCK_LOCKED), \\"r\\"( &dummy) : \\"memory\\"); ( &old ==  &__SIMPLELOCK_UNLOCKED); });
    }' unless defined(&__cpu_simple_lock_try);
    eval 'sub __cpu_simple_unlock {
        my($alp) = @_;
	    eval q({  &__asm  &volatile (\\"sync\\"); *$alp =  &__SIMPLELOCK_UNLOCKED; });
    }' unless defined(&__cpu_simple_unlock);
    eval 'sub rw_cas () { &__cpu_cas;}' unless defined(&rw_cas);
    eval 'sub __cpu_cas {
        my($addr,$old,$new) = @_;
	    eval q({ \'int\'  &success,  &scratch;  &__asm  &volatile( \\"1: lwarx   %0, 0,  %4      \\\\n\\" \\"   cmpw    0, %0, %2       \\\\n\\" \\"   li      %1, 1	   \\\\n\\" \\"   bne     0,2f	    \\\\n\\" \\"   stwcx.  %3, 0, %4       \\\\n\\" \\"   li      %1, 0	   \\\\n\\" \\"   bne-    1b	      \\\\n\\" \\"2:				\\\\n\\" : \\"=&r\\" ( &scratch), \\"=&r\\" ( &success) : \\"r\\" ($old), \\"r\\" ($new), \\"r\\" ($addr) : \\"memory\\");  &success; });
    }' unless defined(&__cpu_cas);
}
1;
