dspl.backend
Class AssemblyfileGenerator

java.lang.Object
  extended by dspl.backend.AssemblyfileGenerator

public class AssemblyfileGenerator
extends java.lang.Object

Utility used to actually write the provided assembly file and instructions

Author:
dlariviere

Field Summary
private  int nextBranchLabelNum
          Used to generate unique labels for branching/control flow.
private  int nextStringID
          Used to generate labels for string constants
private  java.util.HashMap stringLabelsHashMap
           
private  java.io.PrintWriter writer
          PrintWriter responsible for writing instructions directly to the asm file
private  boolean writeStdOut
          If true, all data written to asm file will also be written to std out
 
Constructor Summary
AssemblyfileGenerator(java.lang.String outputFilename, boolean writeStdOut)
          Create an AssemblyfileGenerator to write a basic assembly program to the target file
 
Method Summary
 void closeFile()
          Called after the program is done writing instructions to the generator.
 java.lang.String getBranchLabel()
          Returns a unique branch label to be used for branch instructions
 java.lang.String getStringLabel(Variable stringVar)
          Returns the label used to refer to a particular String that should've been added to the data section of the ASM file
private  void outputILine(java.lang.String line)
          Output tabbed (indented) line
private  void outputLine(java.lang.String line)
          Output line
 void writeAsmInstr(java.lang.String op, java.lang.String arg)
          Writes one operand ASM instruction
 void writeAsmInstr(java.lang.String op, java.lang.String src, java.lang.String dest)
          Writes a standard two operand ASM instruction
private  void writeBSSSection(java.util.ArrayList variables)
          Writes the BSS section, which includes all variables that are not explicitly defined before hand.
private  void writeDataSection(java.util.ArrayList variables)
          Generate the data section for the program based on its variables.
 void writeLabel(java.lang.String label)
          Writes an ASM label
Example: "L10:"
 void writeSkeletonASM(java.util.ArrayList variables)
          Generates the initial Assembly file given it's variables
Should be called before all other write commands are called on it
 void writeStdEntrySeq()
          Writes standard entry sequence for a function according to Intel ABI
Note: does not handle allocating additional space on the stack for local variables
 void writeStdOutputSeq()
          Writes standard exit sequence for a function
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nextBranchLabelNum

private int nextBranchLabelNum
Used to generate unique labels for branching/control flow.


nextStringID

private int nextStringID
Used to generate labels for string constants


writer

private java.io.PrintWriter writer
PrintWriter responsible for writing instructions directly to the asm file


writeStdOut

private boolean writeStdOut
If true, all data written to asm file will also be written to std out


stringLabelsHashMap

private java.util.HashMap stringLabelsHashMap
Constructor Detail

AssemblyfileGenerator

public AssemblyfileGenerator(java.lang.String outputFilename,
                             boolean writeStdOut)
                      throws java.io.FileNotFoundException
Create an AssemblyfileGenerator to write a basic assembly program to the target file

Parameters:
outputFilename: - name of ASM file to write to
writeStdOut - if true, all data written to the file will also be printed to standard out
Throws:
FileNotFoundException: - thrown when file cannot be opened
java.io.FileNotFoundException
Method Detail

closeFile

public void closeFile()
Called after the program is done writing instructions to the generator.
Responsible for flushing and closing the file.


getBranchLabel

public java.lang.String getBranchLabel()
Returns a unique branch label to be used for branch instructions

Returns:

outputLine

private void outputLine(java.lang.String line)
Output line

Parameters:
line: - line to write to file

outputILine

private void outputILine(java.lang.String line)
Output tabbed (indented) line

Parameters:
line: - line to write to file, prepended by tab

getStringLabel

public java.lang.String getStringLabel(Variable stringVar)
Returns the label used to refer to a particular String that should've been added to the data section of the ASM file

Parameters:
stringVar -
Returns:

writeSkeletonASM

public void writeSkeletonASM(java.util.ArrayList variables)
Generates the initial Assembly file given it's variables
Should be called before all other write commands are called on it

Parameters:
variables -

writeDataSection

private void writeDataSection(java.util.ArrayList variables)
Generate the data section for the program based on its variables.

All atomic elements and initially declared variables go in the symbol table.

The only elements that do not are arrays whose defintion is unknown ("i.e.: int_array iA[5];"), which should instead be put in the .bss section

Parameters:
variables -

writeBSSSection

private void writeBSSSection(java.util.ArrayList variables)
Writes the BSS section, which includes all variables that are not explicitly defined before hand.

BSS declared variables differ from those in the Data segment in that variables created in the BSS section to not allocate space within the actual executable, unlike in the Data section where allocating a 1000 element array would actually make the final executable 1000 * sizeof(array element) bigger

Parameters:
variables -

writeStdEntrySeq

public void writeStdEntrySeq()
Writes standard entry sequence for a function according to Intel ABI
Note: does not handle allocating additional space on the stack for local variables


writeStdOutputSeq

public void writeStdOutputSeq()
Writes standard exit sequence for a function


writeAsmInstr

public void writeAsmInstr(java.lang.String op,
                          java.lang.String src,
                          java.lang.String dest)
Writes a standard two operand ASM instruction


writeAsmInstr

public void writeAsmInstr(java.lang.String op,
                          java.lang.String arg)
Writes one operand ASM instruction

Parameters:
op -
arg -

writeLabel

public void writeLabel(java.lang.String label)
Writes an ASM label
Example: "L10:"

Parameters:
label -