ack/mach/z8000/as/mach5.c

165 lines
4 KiB
C
Raw Permalink Normal View History

1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1994-06-24 14:02:31 +00:00
#define RCSID5 "$Id$"
1986-12-05 17:09:41 +00:00
2019-03-24 16:14:38 +00:00
void settype( int type )
1986-12-05 17:09:41 +00:00
{ oprtype[ operand ] = type; }
2019-03-24 16:14:38 +00:00
int twolog(int s)
1993-11-19 09:50:20 +00:00
{ int twopower = 0;
1986-12-05 17:09:41 +00:00
while ( (s>>=1) != 0 ) twopower++;
return( twopower );
}
2019-03-24 16:14:38 +00:00
void setmod(int opr)
{
mode = modetbl[ twolog( oprtype[opr] ) ] << 12;
}
1986-12-05 17:09:41 +00:00
2019-03-24 16:14:38 +00:00
void chtype(int opr, int typerange )
1986-12-05 17:09:41 +00:00
/* Check type of 'opr' with given 'typerange' and
** set the global var 'mode'.
*/
{ if ( (oprtype[opr] & typerange) != oprtype[opr] ) argerr();
2019-03-24 16:14:38 +00:00
else /* We have a permitted type for 'opr'. */ setmod( opr );
1986-12-05 17:09:41 +00:00
}
2019-03-24 16:14:38 +00:00
void chreg(int opc, int reg)
1986-12-05 17:09:41 +00:00
{ switch( opc ) {
case 0xB10A: case 0x1B00: case 0x1900:
/* R32 expected */ if (reg & 1) regerr(); break;
case 0xB107: case 0x1A00: case 0x1800:
/* R64 expected */ if (reg & 3) regerr(); break;
}
}
2019-03-24 16:14:38 +00:00
ATYPE checkaddr( valu_t addr )
1986-12-05 17:09:41 +00:00
/* Called by functions emit_ad() and branch(). */
{ ATYPE addr_struct;
addr_struct.seg = addr >> 16;
addr_struct.off = addr & 0xFFFF;
if ( addr_struct.seg < 0 ) addr_struct.seg = 0;
#ifdef ASLD
else fit(fit7(addr_struct.seg));
#endif
return( addr_struct );
}
2019-03-24 16:14:38 +00:00
void emit_ad(expr_t ad_inf)
1986-12-05 17:09:41 +00:00
/* When the type of an operand is 'da' or 'x' this function
** emits the address.
*/
1993-11-19 09:50:20 +00:00
{ ATYPE addr;
1986-12-05 17:09:41 +00:00
addr = checkaddr( ad_inf.val );
/* Always the long format is emitted, because the binary
** will be downloaded into one z8000-segment with offset
** 0x0000 upto 0xFFFF,so the chance we can use the short
** format is very small.
*/
emit2( 1<<15 | addr.seg<<8 );
emit2( addr.off ); /* ??? relocation information ??? */
}
2019-03-24 16:14:38 +00:00
void ldmcode(int wrd1,int wrd2, int num)
1986-12-05 17:09:41 +00:00
{ fit(fit4(num-1));
emit2( mode | wrd1 );
emit2( wrd2<<8 | num-1 );
if ( mode>>12 == 4 ) emit_ad( addr_inf );
}
2019-03-24 16:14:38 +00:00
valu_t adjust(valu_t absval)
1986-12-05 17:09:41 +00:00
{ valu_t val = absval - DOTVAL - 2;
if ( pass == PASS_2 && val > 0 ) val -= DOTGAIN;
return( val );
}
2019-03-24 16:14:38 +00:00
void branch(int opc,expr_t exp)
1986-12-05 17:09:41 +00:00
/* This routine determines for the F3 format instructions whether the
** relative address is small enough to fit in normal code; If this is
** so normal code is emitted otherwise 'long' code is emitted contai-
** ning the direct address.
*/
1993-11-19 09:50:20 +00:00
{ int longopc = 0, reladdr = 0, sm2, sm4;
1986-12-05 17:09:41 +00:00
valu_t val;
ATYPE addr;
val = adjust(exp.val) >> 1;
if ( (exp.typ & ~S_DOT) != DOTTYP ) sm2 = sm4 = 0;
else
{ switch ( opc & 0xF000 )
{ case DJNZ_: sm2 = fit7( -val );
reladdr = -val;
break;
case JR_: sm2 = fits8( val );
reladdr = low8( val );
longopc = 0x5E00 | (opc>>8 & 0xF);
break;
case CALR_: sm2 = fits12( -val );
reladdr = low12( -val );
longopc = 0x5F00;
break;
}
1993-11-19 09:50:20 +00:00
sm4 = sm2 || fit8( (int)exp.val );
1986-12-05 17:09:41 +00:00
}
switch ( opc & 0xF000 )
{ case DJNZ_: fit( sm2 ); /* djnz must be short */
emit2( opc | reladdr );
break;
case JR_: case CALR_:
sm4 = small(sm4, 2);
sm2 = small(sm2, 2);
if ( sm2 ) emit2( opc | reladdr );
else /* replace by jp/call da */
{ emit2( longopc );
addr = checkaddr( exp.val );
if ( sm4 ) /* short-offset */
emit2( addr.seg<<8 | addr.off );
else
{ emit2( 1<<15 | addr.seg<<8 );
emit2( addr.off );
}
}
}
}
2019-03-24 16:14:38 +00:00
void ldrel(int opc,expr_t exp)
1986-12-05 17:09:41 +00:00
/* This routine determines for the F4 format instructions whether the
** address is within the same segment (meaning a relative address of
** less than 16 bits); If this is so normal code is emitted otherwise
** an error message is given.
*/
{ if ( pass >= PASS_2 && (exp.typ & ~S_DOT) != DOTTYP )
serror( "relative too far" );
emit2( opc );
1993-11-19 09:50:20 +00:00
emit2( (int)adjust(exp.val) );
1986-12-05 17:09:41 +00:00
}
2019-03-24 16:14:38 +00:00
void shiftcode(int w1, int w2)
1986-12-05 17:09:41 +00:00
{ switch( w1 & 0x0F04 )
{ /* Remember: w2 negative means right shift ! */
case 0x200: /*byte*/ fit( w2>=-8 && w2<=8 ); break;
case 0x300: /*word*/ fit( w2>=-16 && w2<=16 ); break;
case 0x304: /*long*/ fit( w2>=-32 && w2<=32 ); break;
}
emit2( w1 );
emit2( w2 );
}
2019-03-24 16:14:38 +00:00
void argerr(void)
1986-12-05 17:09:41 +00:00
{ serror( "illegal operand" ); }