Search in sources :

Example 1 with ExecutableCode

use of com.kasirgalabs.etumulator.lang.Linker.ExecutableCode in project ETUmulator by kasirgalabs.

the class Assembler method assemble.

/**
 * Assembles the given code in to an {@link ExecutableCode}.
 *
 * @param code T code to be assembled.
 *
 * @return The executable code.
 *
 * @throws SyntaxError If the code contains syntax error(s).
 * @throws LabelError  If an undefined label used or duplicate labels exist.
 * @see ExecutableCode
 */
public ExecutableCode assemble(String code) throws SyntaxError, LabelError {
    AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    AssemblerParser parser = new AssemblerParser(tokens);
    parser.prog();
    if (parser.getNumberOfSyntaxErrors() > 0) {
        throw new SyntaxError("You have error(s) in your code.");
    }
    ConstantValidator.validate(code);
    ExecutableCode executableCode = linker.link(code);
    loader.load(executableCode);
    return executableCode;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AssemblerLexer(com.kasirgalabs.thumb2.AssemblerLexer) ExecutableCode(com.kasirgalabs.etumulator.lang.Linker.ExecutableCode) AssemblerParser(com.kasirgalabs.thumb2.AssemblerParser)

Example 2 with ExecutableCode

use of com.kasirgalabs.etumulator.lang.Linker.ExecutableCode in project ETUmulator by kasirgalabs.

the class LinkerTest method testLink.

/**
 * Test of link method, of class Linker.
 */
@Test
public void testLink() {
    Linker linker = new Linker();
    ExecutableCode executableCode;
    String expData;
    String code = "ldr r0, =label\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, there is an undefined label.");
    } catch (LabelError ex) {
    }
    code = "ldr r0, =label\n" + "label: .asciz \"data\"\n" + "label:\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, duplicate labels.");
    } catch (LabelError ex) {
    }
    code = "ldr r0, =label\n" + "label: .asciz \"data\"\n" + "label: .asciz \"duplicate\"\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, duplicate labels.");
    } catch (LabelError ex) {
    }
    code = "b label\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, there is an undefined label.");
    } catch (LabelError ex) {
    }
    // No branch target given.
    // The given label is defines a data not branch target.
    code = "b label\n" + "label: .asciz \"DATA LABEL\"\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, there is an undefined label.");
    } catch (LabelError ex) {
    }
    code = "b label\n" + "label: .asciz \"DATA LABEL\"\n" + "label:\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, duplicate labels.");
    } catch (LabelError ex) {
    }
    code = "b label\n" + "label: .asciz \"DATA LABEL\"\n" + "label:\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, duplicate labels.");
    } catch (LabelError ex) {
    }
    code = "ldr r0, =label\n" + "label: .asciz \"DATA\"\n";
    executableCode = linker.link(code);
    expData = "DATA\0";
    assertEquals("Linker did not resolve data properly.", expData, executableCode.getData().get(0).getValue());
    code = "ldr r0, =label\n" + "label: .asciz \"DATA\"\n" + "label: .asciz \"DATA\"\n" + "label: .asciz \"DATA\"\n";
    try {
        linker.link(code);
        fail("Linker did not throw LabelError, duplicate labels.");
    } catch (LabelError ex) {
    }
    code = "ldr r0, =label0\n" + "label0: .asciz \"DATA0\"\n" + "label1: .asciz \"DATA1\"\n" + "label2: .asciz \"DATA2\"\n";
    executableCode = linker.link(code);
    List<Data> dataResult = executableCode.getData();
    expData = "DATA0\0";
    assertEquals("Linker did not resolve data properly.", expData, dataResult.get(0).getValue());
    expData = "DATA1\0";
    assertEquals("Linker did not resolve data properly.", expData, dataResult.get(1).getValue());
    expData = "DATA2\0";
    assertEquals("Linker did not resolve data properly.", expData, dataResult.get(2).getValue());
}
Also used : ExecutableCode(com.kasirgalabs.etumulator.lang.Linker.ExecutableCode) Test(org.junit.Test)

Example 3 with ExecutableCode

use of com.kasirgalabs.etumulator.lang.Linker.ExecutableCode in project ETUmulator by kasirgalabs.

the class ETUmulatorController method runButtonOnAction.

@FXML
private void runButtonOnAction(ActionEvent event) {
    processor.stop();
    processorUnits.reset();
    Assembler assembler = new Assembler(memory);
    ExecutableCode executableCode;
    try {
        executableCode = assembler.assemble(document.getText() + "\n");
    } catch (SyntaxError | LabelError | NumberFormatException ex) {
        System.err.println(ex.getMessage());
        return;
    }
    processor.run(executableCode);
}
Also used : SyntaxError(com.kasirgalabs.etumulator.lang.SyntaxError) LabelError(com.kasirgalabs.etumulator.lang.LabelError) Assembler(com.kasirgalabs.etumulator.lang.Assembler) ExecutableCode(com.kasirgalabs.etumulator.lang.Linker.ExecutableCode) FXML(javafx.fxml.FXML)

Aggregations

ExecutableCode (com.kasirgalabs.etumulator.lang.Linker.ExecutableCode)3 Assembler (com.kasirgalabs.etumulator.lang.Assembler)1 LabelError (com.kasirgalabs.etumulator.lang.LabelError)1 SyntaxError (com.kasirgalabs.etumulator.lang.SyntaxError)1 AssemblerLexer (com.kasirgalabs.thumb2.AssemblerLexer)1 AssemblerParser (com.kasirgalabs.thumb2.AssemblerParser)1 FXML (javafx.fxml.FXML)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 Test (org.junit.Test)1