refactor: Use strategy for CodeGenerator backend

This makes it easier to switch between the code generator backends
which may help debugging.
This commit is contained in:
Laborratte 5 2025-05-10 01:46:13 +02:00
parent 70528858cc
commit 56536cc64b
Signed by: Laborratte5
GPG key ID: 3A30072E35202C02
3 changed files with 17 additions and 3 deletions

View file

@ -1,6 +1,7 @@
package edu.kit.kastel.vads.compiler;
import edu.kit.kastel.vads.compiler.backend.aasm.CodeGenerator;
import edu.kit.kastel.vads.compiler.backend.CodeGenerator;
import edu.kit.kastel.vads.compiler.backend.aasm.AasmCodeGenerator;
import edu.kit.kastel.vads.compiler.ir.IrGraph;
import edu.kit.kastel.vads.compiler.ir.SsaTranslation;
import edu.kit.kastel.vads.compiler.ir.optimize.LocalValueNumbering;
@ -50,7 +51,8 @@ public class Main {
}
// TODO: generate assembly and invoke gcc instead of generating abstract assembly
String s = new CodeGenerator().generateCode(graphs);
CodeGenerator generator = new AasmCodeGenerator();
String s = generator.generateCode(graphs);
Files.writeString(output, s);
}

View file

@ -0,0 +1,11 @@
package edu.kit.kastel.vads.compiler.backend;
import java.util.List;
import edu.kit.kastel.vads.compiler.ir.IrGraph;
public interface CodeGenerator {
public String generateCode(List<IrGraph> program);
}

View file

@ -1,5 +1,6 @@
package edu.kit.kastel.vads.compiler.backend.aasm;
import edu.kit.kastel.vads.compiler.backend.CodeGenerator;
import edu.kit.kastel.vads.compiler.backend.regalloc.Register;
import edu.kit.kastel.vads.compiler.ir.IrGraph;
import edu.kit.kastel.vads.compiler.ir.node.AddNode;
@ -23,7 +24,7 @@ import java.util.Set;
import static edu.kit.kastel.vads.compiler.ir.util.NodeSupport.predecessorSkipProj;
public class CodeGenerator {
public class AasmCodeGenerator implements CodeGenerator {
public String generateCode(List<IrGraph> program) {
StringBuilder builder = new StringBuilder();