[Exspiminator-commits] CVS: exspiminator/base NewAssembler.java,1.2,1.3 PowerPCInstructionEncoder.ja
Status: Alpha
Brought to you by:
nphillips
|
From: Nigel P. <nph...@us...> - 2001-11-19 19:17:04
|
Update of /cvsroot/exspiminator/exspiminator/base
In directory usw-pr-cvs1:/tmp/cvs-serv19677/base
Modified Files:
NewAssembler.java PowerPCInstructionEncoder.java
Log Message:
Move some generic functionality from platform specific to platform netural areas (PPCInstructionEncoder to NewAssembler). Implement some assembler directives.
Index: NewAssembler.java
===================================================================
RCS file: /cvsroot/exspiminator/exspiminator/base/NewAssembler.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** NewAssembler.java 2001/11/19 16:17:49 1.2
--- NewAssembler.java 2001/11/19 19:17:00 1.3
***************
*** 21,24 ****
--- 21,27 ----
public class NewAssembler
{
+ static final String LONG=".long", BYTE=".byte", ALIGN=".align", SPACE=".space", SHORT=".short";
+ static final String[] directives={LONG, BYTE, ALIGN, SPACE, SHORT};
+
InstructionSet is;
Map instructions;
***************
*** 58,63 ****
}
! /** allow differentiating by both mnemonic and num operands
Bug: change to offer different versions of it depending on number of operands.
May involve name mangling... or not: could do it properly and make a comparitor
--- 61,105 ----
}
! /** Modify to allow alises at some point?*/
! int parseInt(String s) throws OperandFormatException
! {
! try
! {
! return Integer.parseInt(s);
! }
! catch (NumberFormatException ex)
! {
! throw new OperandFormatException("Integer expected (found '" + s
! + "' at line " + getCurrentLineNum() + ".");
! }
! }
!
! /** This performs the label substitution or literal parsing for branch targets.
! Also used to allow things like <code>li rA, label</code> and <code>la rA, label(rB)</code>.
! BUG: remember we'll have to deal with aliases too at some point.*/
! int resolveLabelOrInt(String target) throws OperandFormatException
! {
! int result;
! Integer i1=resolveLabel(target);
! if (i1!=null)
! {
! result=i1.intValue();
! }
! else
! {
! try
! {
! // fall back to parseInt
! result=Integer.parseInt(target);
! }
! catch (NumberFormatException ex)
! {
! throw new UnknownSymbolException();
! }
! }
! return result;
! }
+ /** allow differentiating by both mnemonic and num operands
Bug: change to offer different versions of it depending on number of operands.
May involve name mangling... or not: could do it properly and make a comparitor
***************
*** 84,87 ****
--- 126,143 ----
}
+ /** Returns the string matching the given string iff it is contained in the directives array.
+ Just uses a linear search: the list of possible directives is short.*/
+ String lookupDirective(String directive)
+ {
+ for (int i=0; i<directives.length; i++)
+ {
+ if (directive.equals(directives[i]))
+ {
+ return directives[i];
+ }
+ }
+ return null;
+ }
+
/** Returns true if the string is empty, the first character is a digit, or it's
a mnemonic in the current instruction set (since that would mess up our crappy address
***************
*** 94,100 ****
|| is.isLegalMnemonic(s) || s.indexOf(":")!=-1;
}
-
- /** Pass a null value for incompletes when backpatching.
@param incompletes A list to which to add a list of AssemblyLines producing an UnknownSymbolException.
If this vlaue if null, a new AssemblerException will be thrown.*/
--- 150,155 ----
|| is.isLegalMnemonic(s) || s.indexOf(":")!=-1;
}
+ /** Pass a null value for incompletes when backpatching.
@param incompletes A list to which to add a list of AssemblyLines producing an UnknownSymbolException.
If this vlaue if null, a new AssemblerException will be thrown.*/
***************
*** 141,145 ****
else
{
! throw new AssemblerException("Unknown symbol at line " + lineNum);
}
}
--- 196,200 ----
else
{
! throw new AssemblerException("Unknown operand symbol at line " + lineNum);
}
}
***************
*** 149,161 ****
else
{
! /*it=lookupDirective(line.getMnemonic(), line.getNumOperands());
! if (it==null)
{
! */ throw new AssemblerException("Unknown identifier at line " + lineNum);
! /*}
! else if (it==directive[0])
{
! // handle .long etc??
! }*/
}
}
--- 204,280 ----
else
{
! String directive=lookupDirective(line.getMnemonic());
! if (directive==null)
! {
! throw new AssemblerException("Unknown symbol at line " + lineNum);
! }
! else if (directive==LONG)
! {
! for (int i=0; i<line.getNumOperands(); i++)
! {
! output.writeInt(parseInt(line.getOperand(i)));
! currentAddress+=4;
! }
! }
! else if (directive==BYTE)
! {
! for (int i=0; i<line.getNumOperands(); i++)
! {
! output.writeByte(parseInt(line.getOperand(i)));
! currentAddress+=1;
! }
! }
! else if (directive==ALIGN)
! {
! if (line.getNumOperands()!=1)
! {
! throw new AssemblerException("Wrong number of operands ("
! + line.getNumOperands() + " found, 1 expected) at line "
! + currentLineNum);
! }
! int operand=parseInt(line.getOperand(0));
! if (operand<0 || operand>3)
! {
! throw new AssemblerException("Invalid alignmnent operand at line "
! + currentLineNum);
! }
! int boundary=1<<operand;
! int rem=currentAddress % boundary;
! if (rem!=0)
! {
! int pad=boundary-rem;
! while (pad>0)
! {
! output.writeByte(0);
! currentAddress++;
! pad--;
! }
! }
! }
! else if (directive==SPACE)
{
! if (line.getNumOperands()!=1)
! {
! throw new AssemblerException("Wrong number of operands ("
! + line.getNumOperands() + " found, 1 expected) at line "
! + currentLineNum);
! }
! int operand=parseInt(line.getOperand(0));
! while (operand>0)
! {
! output.writeByte(0);
! currentAddress++;
! operand--;
! }
! }
! else if (directive==SHORT)
{
! for (int i=0; i<line.getNumOperands(); i++)
! {
! output.writeShort(parseInt(line.getOperand(i)));
! currentAddress+=2;
! }
! }
! // else we don't handle it yet
}
}
Index: PowerPCInstructionEncoder.java
===================================================================
RCS file: /cvsroot/exspiminator/exspiminator/base/PowerPCInstructionEncoder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** PowerPCInstructionEncoder.java 2001/11/19 16:17:49 1.2
--- PowerPCInstructionEncoder.java 2001/11/19 19:17:00 1.3
***************
*** 44,87 ****
}
- int parseInt(String s) throws OperandFormatException
- {
- try
- {
- return Integer.parseInt(s);
- }
- catch (NumberFormatException ex)
- {
- throw new OperandFormatException("Integer expected (found '" + s
- + "' at line " + asm.getCurrentLineNum() + ".");
- }
- }
-
- /** This performs the label substitution or literal parsing for branch targets.
- Also used to allow things like <code>li rA, label</code> and <code>la rA, label(rB)</code>.
- BUG: remember we'll have to deal with aliases too at some point.
- BUG: this functionality is quite generic and therefore better suited to the Assembler?*/
- int resolveLabelOrInt(String target) throws OperandFormatException
- {
- int result;
- Integer i1=asm.resolveLabel(target);
- if (i1!=null)
- {
- result=i1.intValue();
- }
- else
- {
- try
- {
- // fall back to parseInt
- result=Integer.parseInt(target);
- }
- catch (NumberFormatException ex)
- {
- throw new UnknownSymbolException();
- }
- }
- return result;
- }
-
int parseGpr(String s) throws OperandFormatException
{
--- 44,47 ----
***************
*** 89,98 ****
if (s.startsWith(gprPrefix))
{
! result=parseInt(s.substring(gprPrefix.length()));
}
else
{
warn(forceGprPrefix, "Missing GPR prefix");
! result=parseInt(s);
}
if (result<0 || result>31)
--- 49,58 ----
if (s.startsWith(gprPrefix))
{
! result=asm.parseInt(s.substring(gprPrefix.length()));
}
else
{
warn(forceGprPrefix, "Missing GPR prefix");
! result=asm.parseInt(s);
}
if (result<0 || result>31)
***************
*** 145,154 ****
if (s.startsWith(crbPrefix))
{
! result=parseInt(s.substring(crbPrefix.length()));
}
else
{
warn(forceCrbPrefix, "Missing CRB prefix (" + crbPrefix + ")");
! result=parseInt(s);
}
if (result<0 || result>31)
--- 105,114 ----
if (s.startsWith(crbPrefix))
{
! result=asm.parseInt(s.substring(crbPrefix.length()));
}
else
{
warn(forceCrbPrefix, "Missing CRB prefix (" + crbPrefix + ")");
! result=asm.parseInt(s);
}
if (result<0 || result>31)
***************
*** 191,195 ****
case PowerPCInstructionSet.BD:
! operand=resolveLabelOrInt(rawOperand);
if ((operand & 3) !=0)
{
--- 151,155 ----
case PowerPCInstructionSet.BD:
! operand=asm.resolveLabelOrInt(rawOperand);
if ((operand & 3) !=0)
{
***************
*** 217,226 ****
if (rawOperand.startsWith(crfPrefix))
{
! operand=parseInt(rawOperand.substring(crfPrefix.length()));
}
else
{
warn(forceCrfPrefix, "Missing CRF prefix");
! operand=parseInt(rawOperand);
}
if (operand>=0 && operand<=7)
--- 177,186 ----
if (rawOperand.startsWith(crfPrefix))
{
! operand=asm.parseInt(rawOperand.substring(crfPrefix.length()));
}
else
{
warn(forceCrfPrefix, "Missing CRF prefix");
! operand=asm.parseInt(rawOperand);
}
if (operand>=0 && operand<=7)
***************
*** 236,240 ****
case PowerPCInstructionSet.LI:
! operand=resolveLabelOrInt(rawOperand);
if ((operand & 3) !=0)
{
--- 196,200 ----
case PowerPCInstructionSet.LI:
! operand=asm.resolveLabelOrInt(rawOperand);
if ((operand & 3) !=0)
{
***************
*** 259,263 ****
case PowerPCInstructionSet.SIMM:
! operand=resolveLabelOrInt(rawOperand);
if (operand>=Short.MIN_VALUE && operand<=Short.MAX_VALUE)
{
--- 219,223 ----
case PowerPCInstructionSet.SIMM:
! operand=asm.resolveLabelOrInt(rawOperand);
if (operand>=Short.MIN_VALUE && operand<=Short.MAX_VALUE)
{
***************
*** 272,276 ****
case PowerPCInstructionSet.UIMM:
! operand=resolveLabelOrInt(rawOperand);
if (operand>=0 && operand<(1<<16))
{
--- 232,236 ----
case PowerPCInstructionSet.UIMM:
! operand=asm.resolveLabelOrInt(rawOperand);
if (operand>=0 && operand<(1<<16))
{
***************
*** 285,289 ****
case PowerPCInstructionSet.L:
! operand=parseInt(rawOperand);
if (operand==0 || operand==1)
{
--- 245,249 ----
case PowerPCInstructionSet.L:
! operand=asm.parseInt(rawOperand);
if (operand==0 || operand==1)
{
***************
*** 300,304 ****
case PowerPCInstructionSet.MB:
case PowerPCInstructionSet.ME:
! operand=parseInt(rawOperand);
if (operand>=0 && operand<=31)
{
--- 260,264 ----
case PowerPCInstructionSet.MB:
case PowerPCInstructionSet.ME:
! operand=asm.parseInt(rawOperand);
if (operand>=0 && operand<=31)
{
***************
*** 313,317 ****
case PowerPCInstructionSet.SPR:
! operand=parseInt(rawOperand);
if (operand==1 || operand==8 || operand==9)
{
--- 273,277 ----
case PowerPCInstructionSet.SPR:
! operand=asm.parseInt(rawOperand);
if (operand==1 || operand==8 || operand==9)
{
***************
*** 329,333 ****
case PowerPCInstructionSet.CRM:
! operand=parseInt(rawOperand);
if (operand>=0 && operand<=0xFF)
{
--- 289,293 ----
case PowerPCInstructionSet.CRM:
! operand=asm.parseInt(rawOperand);
if (operand>=0 && operand<=0xFF)
{
***************
*** 342,346 ****
case PowerPCInstructionSet.BI:
! operand=parseInt(rawOperand);
if (operand>=0 && operand<=31)
{
--- 302,306 ----
case PowerPCInstructionSet.BI:
! operand=asm.parseInt(rawOperand);
if (operand>=0 && operand<=31)
{
***************
*** 355,359 ****
case PowerPCInstructionSet.BO:
! operand=parseInt(rawOperand);
// BUG: could be more exclusive than this?
if (operand>=0 && operand<=31)
--- 315,319 ----
case PowerPCInstructionSet.BO:
! operand=asm.parseInt(rawOperand);
// BUG: could be more exclusive than this?
if (operand>=0 && operand<=31)
***************
*** 396,400 ****
// overcome warning as r is required for regs etc.
case SimplifiedPowerPCInstructionSet.NEGSIMM:
! operand=parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.SIMM, ""+(-operand));
--- 356,360 ----
// overcome warning as r is required for regs etc.
case SimplifiedPowerPCInstructionSet.NEGSIMM:
! operand=asm.parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.SIMM, ""+(-operand));
***************
*** 426,439 ****
case SimplifiedPowerPCInstructionSet.ME_PLUS_1:
! operand=parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.ME, ""+(operand-1));
case SimplifiedPowerPCInstructionSet.THIRTY_TWO_MINUS_MB:
! operand=parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.MB, ""+(32-operand));
case SimplifiedPowerPCInstructionSet.COMPLETE1:
{
! operand=parseInt(rawOperand);
int n=32-PowerPCUtils.extractMB(opcode);
return insertField(opcode, PowerPCInstructionSet.SH, ""+(operand+n));
--- 386,399 ----
case SimplifiedPowerPCInstructionSet.ME_PLUS_1:
! operand=asm.parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.ME, ""+(operand-1));
case SimplifiedPowerPCInstructionSet.THIRTY_TWO_MINUS_MB:
! operand=asm.parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.MB, ""+(32-operand));
case SimplifiedPowerPCInstructionSet.COMPLETE1:
{
! operand=asm.parseInt(rawOperand);
int n=32-PowerPCUtils.extractMB(opcode);
return insertField(opcode, PowerPCInstructionSet.SH, ""+(operand+n));
***************
*** 441,450 ****
case SimplifiedPowerPCInstructionSet.TEMP_ME:
! operand=parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.ME, ""+operand);
case SimplifiedPowerPCInstructionSet.COMPLETE2:
{
! operand=parseInt(rawOperand);
opcode=insertField(opcode, PowerPCInstructionSet.MB, ""+operand);
opcode=insertField(opcode, PowerPCInstructionSet.SH, ""+(32-operand));
--- 401,410 ----
case SimplifiedPowerPCInstructionSet.TEMP_ME:
! operand=asm.parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.ME, ""+operand);
case SimplifiedPowerPCInstructionSet.COMPLETE2:
{
! operand=asm.parseInt(rawOperand);
opcode=insertField(opcode, PowerPCInstructionSet.MB, ""+operand);
opcode=insertField(opcode, PowerPCInstructionSet.SH, ""+(32-operand));
***************
*** 456,460 ****
case SimplifiedPowerPCInstructionSet.COMPLETE3:
{
! operand=parseInt(rawOperand);
opcode=insertField(opcode, PowerPCInstructionSet.MB, ""+operand);
int n=PowerPCUtils.extractME(opcode);
--- 416,420 ----
case SimplifiedPowerPCInstructionSet.COMPLETE3:
{
! operand=asm.parseInt(rawOperand);
opcode=insertField(opcode, PowerPCInstructionSet.MB, ""+operand);
int n=PowerPCUtils.extractME(opcode);
***************
*** 465,473 ****
case SimplifiedPowerPCInstructionSet.THIRTY_TWO_MINUS_SH:
! operand=parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.SH, ""+(32-operand));
case SimplifiedPowerPCInstructionSet.SH_AND_31_MINUS_ME:
! operand=parseInt(rawOperand);
return insertField(
insertField(opcode, PowerPCInstructionSet.SH, ""+operand),
--- 425,433 ----
case SimplifiedPowerPCInstructionSet.THIRTY_TWO_MINUS_SH:
! operand=asm.parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.SH, ""+(32-operand));
case SimplifiedPowerPCInstructionSet.SH_AND_31_MINUS_ME:
! operand=asm.parseInt(rawOperand);
return insertField(
insertField(opcode, PowerPCInstructionSet.SH, ""+operand),
***************
*** 475,479 ****
case SimplifiedPowerPCInstructionSet.MB_AND_32_MINUS_SH:
! operand=parseInt(rawOperand);
return insertField(
insertField(opcode, PowerPCInstructionSet.MB, ""+operand),
--- 435,439 ----
case SimplifiedPowerPCInstructionSet.MB_AND_32_MINUS_SH:
! operand=asm.parseInt(rawOperand);
return insertField(
insertField(opcode, PowerPCInstructionSet.MB, ""+operand),
***************
*** 481,490 ****
case SimplifiedPowerPCInstructionSet.THIRTY_ONE_MINUS_ME:
! operand=parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.ME, ""+(31-operand));
case SimplifiedPowerPCInstructionSet.COMPLETE4:
{
! operand=parseInt(rawOperand);
opcode=insertField(opcode, PowerPCInstructionSet.SH, ""+operand);
int b=PowerPCUtils.extractME(opcode);
--- 441,450 ----
case SimplifiedPowerPCInstructionSet.THIRTY_ONE_MINUS_ME:
! operand=asm.parseInt(rawOperand);
return insertField(opcode, PowerPCInstructionSet.ME, ""+(31-operand));
case SimplifiedPowerPCInstructionSet.COMPLETE4:
{
! operand=asm.parseInt(rawOperand);
opcode=insertField(opcode, PowerPCInstructionSet.SH, ""+operand);
int b=PowerPCUtils.extractME(opcode);
|